diff --git a/dist/lib/appletv.js b/dist/lib/appletv.js index b28a551..89ada53 100644 --- a/dist/lib/appletv.js +++ b/dist/lib/appletv.js @@ -18,12 +18,7 @@ class AppleTV extends typed_events_1.default { this.pairingId = uuid_1.v4(); this.service = service; this.name = service.txtRecord.Name; - if (service.addresses.length > 1) { - this.address = service.addresses[1]; - } - else { - this.address = service.addresses[0]; - } + this.address = service.host; this.port = service.port; this.uid = service.txtRecord.UniqueIdentifier; this.connection = new connection_1.Connection(this); diff --git a/dist/lib/browser.js b/dist/lib/browser.js index 0c918c9..5f15922 100644 --- a/dist/lib/browser.js +++ b/dist/lib/browser.js @@ -1,44 +1,68 @@ "use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); +Object.defineProperty(exports, "__esModule", { + value: true +}); const mdns = require("mdns"); +const mdnsResolver = require("mdns-resolver"); const appletv_1 = require("./appletv"); class Browser { /** - * Creates a new Browser - * @param log An optional function that takes a string to provide verbose logging. - */ + * Creates a new Browser + * @param log An optional function that takes a string to provide verbose logging. + */ constructor() { let sequence = [ - mdns.rst.DNSServiceResolve(), - 'DNSServiceGetAddrInfo' in mdns.dns_sd ? mdns.rst.DNSServiceGetAddrInfo() : mdns.rst.getaddrinfo({ families: [4] }), - mdns.rst.makeAddressesUnique() + mdns.rst.DNSServiceResolve() ]; - this.browser = mdns.createBrowser(mdns.tcp('mediaremotetv'), { resolverSequence: sequence }); + + this.browser = mdns.createBrowser(mdns.tcp('mediaremotetv'), { + resolverSequence: sequence + }); this.services = []; this.devices = []; let that = this; this.browser.on('serviceUp', function (service) { - let device = new appletv_1.AppleTV(service); - if (that.uniqueIdentifier && device.uid == that.uniqueIdentifier) { - that.browser.stop(); - that.onComplete([device]); - } - else { - if(that.devices.find(uuid => uuid == device.uid)) { - return; - } else { - that.devices.push(device.uid); - that.services.push(device); - } - } + + mdnsResolver.resolve4(service.host) + .then(host => { + service.host = host; + return service; + }) + .catch(e => { + console.log("Failed to resolve IPv4"); + console.log("ERROR: " + e.message); + return mdnsResolver.resolve6(service.host) + .then(host => { + service.host = `[${host}]`; + return service; + }); + }) + .catch(e => { + console.log("Failed to resolve IPv4 and IPv6"); + console.log("ERROR: " + e.message); + }) + .then(service => { + let device = new appletv_1.AppleTV(service); + if (that.uniqueIdentifier && device.uid == that.uniqueIdentifier) { + that.browser.stop(); + that.onComplete([device]); + } else { + if (that.devices.find(uuid => uuid == device.uid)) { + return; + } else { + that.devices.push(device.uid); + that.services.push(device); + } + } + }); }); } /** - * Scans for AppleTVs on the local network. - * @param uniqueIdentifier An optional identifier for the AppleTV to scan for. The AppleTV advertises this via Bonjour. - * @param timeout An optional timeout value (in seconds) to give up the search after. - * @returns A promise that resolves to an array of AppleTV objects. If you provide a `uniqueIdentifier` the array is guaranteed to only contain one object. - */ + * Scans for AppleTVs on the local network. + * @param uniqueIdentifier An optional identifier for the AppleTV to scan for. The AppleTV advertises this via Bonjour. + * @param timeout An optional timeout value (in seconds) to give up the search after. + * @returns A promise that resolves to an array of AppleTV objects. If you provide a `uniqueIdentifier` the array is guaranteed to only contain one object. + */ scan(uniqueIdentifier, timeout) { this.services = []; this.uniqueIdentifier = uniqueIdentifier; @@ -52,15 +76,14 @@ class Browser { that.browser.stop(); if (that.uniqueIdentifier) { reject(new Error("Failed to locate specified AppleTV on the network")); - } - else { + } else { resolve(that.services .sort((a, b) => { - return a > b ? 1 : -1; - })); + return a > b ? 1 : -1; + })); } }, to * 1000); }); } } -exports.Browser = Browser; +exports.Browser = Browser; \ No newline at end of file diff --git a/package.json b/package.json index 568e23e..13d12c3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-appletv-x", - "version": "1.0.12", + "version": "1.0.13", "description": "A Node.js library for communicating with an Apple TV", "homepage": "https://github.com/stickpin/node-appletv-x", "bugs": "https://github.com/stickpin/node-appletv-x/issues", @@ -35,6 +35,7 @@ "fast-srp-hap": "^1.0.1", "inquirer": "^7.0.3", "mdns": "^2.5.1", + "mdns-resolver": "^0.0.3", "ora": "^4.0.3", "protobufjs": "^6.8.8", "snake-case": "^3.0.3",