lms-discovery
Advanced tools
Comparing version
@@ -11,3 +11,4 @@ /// <reference types="node" /> | ||
* How long in milliseconds to wait for a discovered server to respond to a | ||
* subsqeuent discovery request before it is presumed lost. | ||
* subsqeuent discovery request before it is presumed lost. Only applicable | ||
* for servers that do not advertise `cliPort`. | ||
* @default 60000 (60 seconds) | ||
@@ -14,0 +15,0 @@ */ |
@@ -16,3 +16,3 @@ "use strict"; | ||
}; | ||
var _LMSDiscovery_instances, _LMSDiscovery_opts, _LMSDiscovery_discovered, _LMSDiscovery_discoveredTTLTimers, _LMSDiscovery_discoverIntervalTimer, _LMSDiscovery_socket, _LMSDiscovery_debug, _LMSDiscovery_initSocket, _LMSDiscovery_findDiscoveredByIP, _LMSDiscovery_findDiscoveredByUUID, _LMSDiscovery_sendDiscoveryRequest, _LMSDiscovery_parseDiscoveryResponse, _LMSDiscovery_debugLog; | ||
var _LMSDiscovery_instances, _LMSDiscovery_opts, _LMSDiscovery_discovered, _LMSDiscovery_discoveredTTLTimers, _LMSDiscovery_cliConnections, _LMSDiscovery_discoverIntervalTimer, _LMSDiscovery_socket, _LMSDiscovery_debug, _LMSDiscovery_initSocket, _LMSDiscovery_handleCLIDisconnected, _LMSDiscovery_findDiscoveredByIP, _LMSDiscovery_findDiscoveredByUUID, _LMSDiscovery_sendDiscoveryRequest, _LMSDiscovery_parseDiscoveryResponse, _LMSDiscovery_debugLog; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -23,2 +23,3 @@ exports.LMSDiscovery = void 0; | ||
const events_1 = __importDefault(require("events")); | ||
const telnet_client_1 = require("telnet-client"); | ||
const DISCOVER_MSG = 'eIPAD\0NAME\0VERS\0UUID\0JSON\0CLIP\0'; | ||
@@ -46,2 +47,3 @@ const BROADCAST_PORT = 3483; | ||
_LMSDiscovery_discoveredTTLTimers.set(this, void 0); | ||
_LMSDiscovery_cliConnections.set(this, void 0); | ||
_LMSDiscovery_discoverIntervalTimer.set(this, void 0); | ||
@@ -56,2 +58,3 @@ _LMSDiscovery_socket.set(this, void 0); | ||
__classPrivateFieldSet(this, _LMSDiscovery_discoveredTTLTimers, {}, "f"); | ||
__classPrivateFieldSet(this, _LMSDiscovery_cliConnections, {}, "f"); | ||
} | ||
@@ -100,2 +103,7 @@ /** | ||
__classPrivateFieldSet(this, _LMSDiscovery_discoveredTTLTimers, {}, "f"); | ||
Object.values(__classPrivateFieldGet(this, _LMSDiscovery_cliConnections, "f")).forEach((connection) => { | ||
connection.removeAllListeners('close'); | ||
connection.end(); | ||
}); | ||
__classPrivateFieldSet(this, _LMSDiscovery_cliConnections, {}, "f"); | ||
__classPrivateFieldSet(this, _LMSDiscovery_discovered, {}, "f"); | ||
@@ -138,3 +146,3 @@ if (__classPrivateFieldGet(this, _LMSDiscovery_socket, "f")) { | ||
exports.LMSDiscovery = LMSDiscovery; | ||
_LMSDiscovery_opts = new WeakMap(), _LMSDiscovery_discovered = new WeakMap(), _LMSDiscovery_discoveredTTLTimers = new WeakMap(), _LMSDiscovery_discoverIntervalTimer = new WeakMap(), _LMSDiscovery_socket = new WeakMap(), _LMSDiscovery_debug = new WeakMap(), _LMSDiscovery_instances = new WeakSet(), _LMSDiscovery_initSocket = function _LMSDiscovery_initSocket() { | ||
_LMSDiscovery_opts = new WeakMap(), _LMSDiscovery_discovered = new WeakMap(), _LMSDiscovery_discoveredTTLTimers = new WeakMap(), _LMSDiscovery_cliConnections = new WeakMap(), _LMSDiscovery_discoverIntervalTimer = new WeakMap(), _LMSDiscovery_socket = new WeakMap(), _LMSDiscovery_debug = new WeakMap(), _LMSDiscovery_instances = new WeakSet(), _LMSDiscovery_initSocket = function _LMSDiscovery_initSocket() { | ||
__classPrivateFieldGet(this, _LMSDiscovery_instances, "m", _LMSDiscovery_debugLog).call(this, 'Initializing socket...'); | ||
@@ -155,3 +163,3 @@ const socket = __classPrivateFieldSet(this, _LMSDiscovery_socket, dgram_1.default.createSocket({ type: 'udp4', reuseAddr: true }), "f"); | ||
}); | ||
socket.on('message', (data, rinfo) => { | ||
socket.on('message', async (data, rinfo) => { | ||
__classPrivateFieldGet(this, _LMSDiscovery_instances, "m", _LMSDiscovery_debugLog).call(this, `Message received from ${rinfo.address}`); | ||
@@ -169,8 +177,39 @@ const server = __classPrivateFieldGet(this, _LMSDiscovery_instances, "m", _LMSDiscovery_parseDiscoveryResponse).call(this, data, rinfo); | ||
__classPrivateFieldGet(this, _LMSDiscovery_discovered, "f")[server.ip] = server; | ||
__classPrivateFieldGet(this, _LMSDiscovery_discoveredTTLTimers, "f")[server.ip] = setTimeout(() => { | ||
delete __classPrivateFieldGet(this, _LMSDiscovery_discovered, "f")[server.ip]; | ||
delete __classPrivateFieldGet(this, _LMSDiscovery_discoveredTTLTimers, "f")[server.ip]; | ||
__classPrivateFieldGet(this, _LMSDiscovery_instances, "m", _LMSDiscovery_debugLog).call(this, `Detected lost server: ${JSON.stringify(server)}. Emitting 'lost' event...`); | ||
this.emit('lost', server); | ||
}, __classPrivateFieldGet(this, _LMSDiscovery_opts, "f").discoveredTTL); | ||
/** | ||
* Establish telnet connection with servers that advertise cliPort, | ||
* so we can emit server lost event immediately on disconnect. | ||
*/ | ||
let cliConnected = !!__classPrivateFieldGet(this, _LMSDiscovery_cliConnections, "f")[server.ip]; | ||
if (!cliConnected && server.cliPort) { | ||
const cliConnection = new telnet_client_1.Telnet(); | ||
cliConnection.on('close', __classPrivateFieldGet(this, _LMSDiscovery_instances, "m", _LMSDiscovery_handleCLIDisconnected).bind(this, server)); | ||
const connectParams = { | ||
host: server.ip, | ||
port: server.cliPort, | ||
negotiationMandatory: false, | ||
timeout: 1500, | ||
irs: '\n' | ||
}; | ||
try { | ||
await cliConnection.connect(connectParams); | ||
__classPrivateFieldGet(this, _LMSDiscovery_cliConnections, "f")[server.ip] = cliConnection; | ||
__classPrivateFieldGet(this, _LMSDiscovery_instances, "m", _LMSDiscovery_debugLog).call(this, `Established connection to ${server.ip}:${server.cliPort}`); | ||
cliConnected = true; | ||
} | ||
catch (error) { | ||
__classPrivateFieldGet(this, _LMSDiscovery_instances, "m", _LMSDiscovery_debugLog).call(this, `Failed to connect to ${server.ip}:${server.cliPort}: ${error}`); | ||
} | ||
} | ||
/** | ||
* For servers that do not advertise cliPort, or if connection to cliPort failed, | ||
* we would have to rely on TTL timers. | ||
*/ | ||
if (!cliConnected) { | ||
__classPrivateFieldGet(this, _LMSDiscovery_discoveredTTLTimers, "f")[server.ip] = setTimeout(() => { | ||
delete __classPrivateFieldGet(this, _LMSDiscovery_discovered, "f")[server.ip]; | ||
delete __classPrivateFieldGet(this, _LMSDiscovery_discoveredTTLTimers, "f")[server.ip]; | ||
__classPrivateFieldGet(this, _LMSDiscovery_instances, "m", _LMSDiscovery_debugLog).call(this, `Detected lost server: ${JSON.stringify(server)}. Emitting 'lost' event...`); | ||
this.emit('lost', server); | ||
}, __classPrivateFieldGet(this, _LMSDiscovery_opts, "f").discoveredTTL); | ||
} | ||
if (!existing) { // Newly-discovered | ||
@@ -190,2 +229,11 @@ __classPrivateFieldGet(this, _LMSDiscovery_instances, "m", _LMSDiscovery_debugLog).call(this, 'This is a newly-discovered server. Emitting \'discovered\' event...'); | ||
}); | ||
}, _LMSDiscovery_handleCLIDisconnected = function _LMSDiscovery_handleCLIDisconnected(server) { | ||
const cliConnection = __classPrivateFieldGet(this, _LMSDiscovery_cliConnections, "f")[server.ip]; | ||
if (cliConnection) { | ||
cliConnection.removeAllListeners('close'); | ||
} | ||
delete __classPrivateFieldGet(this, _LMSDiscovery_cliConnections, "f")[server.ip]; | ||
delete __classPrivateFieldGet(this, _LMSDiscovery_discovered, "f")[server.ip]; | ||
__classPrivateFieldGet(this, _LMSDiscovery_instances, "m", _LMSDiscovery_debugLog).call(this, `Disconnected from server: ${JSON.stringify(server)}. Emitting 'lost' event...`); | ||
this.emit('lost', server); | ||
}, _LMSDiscovery_findDiscoveredByIP = function _LMSDiscovery_findDiscoveredByIP(ip) { | ||
@@ -192,0 +240,0 @@ return __classPrivateFieldGet(this, _LMSDiscovery_discovered, "f")[ip] || null; |
@@ -11,3 +11,4 @@ /// <reference types="node" /> | ||
* How long in milliseconds to wait for a discovered server to respond to a | ||
* subsqeuent discovery request before it is presumed lost. | ||
* subsqeuent discovery request before it is presumed lost. Only applicable | ||
* for servers that do not advertise `cliPort`. | ||
* @default 60000 (60 seconds) | ||
@@ -14,0 +15,0 @@ */ |
@@ -12,6 +12,7 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { | ||
}; | ||
var _LMSDiscovery_instances, _LMSDiscovery_opts, _LMSDiscovery_discovered, _LMSDiscovery_discoveredTTLTimers, _LMSDiscovery_discoverIntervalTimer, _LMSDiscovery_socket, _LMSDiscovery_debug, _LMSDiscovery_initSocket, _LMSDiscovery_findDiscoveredByIP, _LMSDiscovery_findDiscoveredByUUID, _LMSDiscovery_sendDiscoveryRequest, _LMSDiscovery_parseDiscoveryResponse, _LMSDiscovery_debugLog; | ||
var _LMSDiscovery_instances, _LMSDiscovery_opts, _LMSDiscovery_discovered, _LMSDiscovery_discoveredTTLTimers, _LMSDiscovery_cliConnections, _LMSDiscovery_discoverIntervalTimer, _LMSDiscovery_socket, _LMSDiscovery_debug, _LMSDiscovery_initSocket, _LMSDiscovery_handleCLIDisconnected, _LMSDiscovery_findDiscoveredByIP, _LMSDiscovery_findDiscoveredByUUID, _LMSDiscovery_sendDiscoveryRequest, _LMSDiscovery_parseDiscoveryResponse, _LMSDiscovery_debugLog; | ||
import dgram from 'dgram'; | ||
import deepEqual from 'deep-equal'; | ||
import EventEmitter from 'events'; | ||
import { Telnet } from 'telnet-client'; | ||
const DISCOVER_MSG = 'eIPAD\0NAME\0VERS\0UUID\0JSON\0CLIP\0'; | ||
@@ -39,2 +40,3 @@ const BROADCAST_PORT = 3483; | ||
_LMSDiscovery_discoveredTTLTimers.set(this, void 0); | ||
_LMSDiscovery_cliConnections.set(this, void 0); | ||
_LMSDiscovery_discoverIntervalTimer.set(this, void 0); | ||
@@ -49,2 +51,3 @@ _LMSDiscovery_socket.set(this, void 0); | ||
__classPrivateFieldSet(this, _LMSDiscovery_discoveredTTLTimers, {}, "f"); | ||
__classPrivateFieldSet(this, _LMSDiscovery_cliConnections, {}, "f"); | ||
} | ||
@@ -93,2 +96,7 @@ /** | ||
__classPrivateFieldSet(this, _LMSDiscovery_discoveredTTLTimers, {}, "f"); | ||
Object.values(__classPrivateFieldGet(this, _LMSDiscovery_cliConnections, "f")).forEach((connection) => { | ||
connection.removeAllListeners('close'); | ||
connection.end(); | ||
}); | ||
__classPrivateFieldSet(this, _LMSDiscovery_cliConnections, {}, "f"); | ||
__classPrivateFieldSet(this, _LMSDiscovery_discovered, {}, "f"); | ||
@@ -130,3 +138,3 @@ if (__classPrivateFieldGet(this, _LMSDiscovery_socket, "f")) { | ||
} | ||
_LMSDiscovery_opts = new WeakMap(), _LMSDiscovery_discovered = new WeakMap(), _LMSDiscovery_discoveredTTLTimers = new WeakMap(), _LMSDiscovery_discoverIntervalTimer = new WeakMap(), _LMSDiscovery_socket = new WeakMap(), _LMSDiscovery_debug = new WeakMap(), _LMSDiscovery_instances = new WeakSet(), _LMSDiscovery_initSocket = function _LMSDiscovery_initSocket() { | ||
_LMSDiscovery_opts = new WeakMap(), _LMSDiscovery_discovered = new WeakMap(), _LMSDiscovery_discoveredTTLTimers = new WeakMap(), _LMSDiscovery_cliConnections = new WeakMap(), _LMSDiscovery_discoverIntervalTimer = new WeakMap(), _LMSDiscovery_socket = new WeakMap(), _LMSDiscovery_debug = new WeakMap(), _LMSDiscovery_instances = new WeakSet(), _LMSDiscovery_initSocket = function _LMSDiscovery_initSocket() { | ||
__classPrivateFieldGet(this, _LMSDiscovery_instances, "m", _LMSDiscovery_debugLog).call(this, 'Initializing socket...'); | ||
@@ -147,3 +155,3 @@ const socket = __classPrivateFieldSet(this, _LMSDiscovery_socket, dgram.createSocket({ type: 'udp4', reuseAddr: true }), "f"); | ||
}); | ||
socket.on('message', (data, rinfo) => { | ||
socket.on('message', async (data, rinfo) => { | ||
__classPrivateFieldGet(this, _LMSDiscovery_instances, "m", _LMSDiscovery_debugLog).call(this, `Message received from ${rinfo.address}`); | ||
@@ -161,8 +169,39 @@ const server = __classPrivateFieldGet(this, _LMSDiscovery_instances, "m", _LMSDiscovery_parseDiscoveryResponse).call(this, data, rinfo); | ||
__classPrivateFieldGet(this, _LMSDiscovery_discovered, "f")[server.ip] = server; | ||
__classPrivateFieldGet(this, _LMSDiscovery_discoveredTTLTimers, "f")[server.ip] = setTimeout(() => { | ||
delete __classPrivateFieldGet(this, _LMSDiscovery_discovered, "f")[server.ip]; | ||
delete __classPrivateFieldGet(this, _LMSDiscovery_discoveredTTLTimers, "f")[server.ip]; | ||
__classPrivateFieldGet(this, _LMSDiscovery_instances, "m", _LMSDiscovery_debugLog).call(this, `Detected lost server: ${JSON.stringify(server)}. Emitting 'lost' event...`); | ||
this.emit('lost', server); | ||
}, __classPrivateFieldGet(this, _LMSDiscovery_opts, "f").discoveredTTL); | ||
/** | ||
* Establish telnet connection with servers that advertise cliPort, | ||
* so we can emit server lost event immediately on disconnect. | ||
*/ | ||
let cliConnected = !!__classPrivateFieldGet(this, _LMSDiscovery_cliConnections, "f")[server.ip]; | ||
if (!cliConnected && server.cliPort) { | ||
const cliConnection = new Telnet(); | ||
cliConnection.on('close', __classPrivateFieldGet(this, _LMSDiscovery_instances, "m", _LMSDiscovery_handleCLIDisconnected).bind(this, server)); | ||
const connectParams = { | ||
host: server.ip, | ||
port: server.cliPort, | ||
negotiationMandatory: false, | ||
timeout: 1500, | ||
irs: '\n' | ||
}; | ||
try { | ||
await cliConnection.connect(connectParams); | ||
__classPrivateFieldGet(this, _LMSDiscovery_cliConnections, "f")[server.ip] = cliConnection; | ||
__classPrivateFieldGet(this, _LMSDiscovery_instances, "m", _LMSDiscovery_debugLog).call(this, `Established connection to ${server.ip}:${server.cliPort}`); | ||
cliConnected = true; | ||
} | ||
catch (error) { | ||
__classPrivateFieldGet(this, _LMSDiscovery_instances, "m", _LMSDiscovery_debugLog).call(this, `Failed to connect to ${server.ip}:${server.cliPort}: ${error}`); | ||
} | ||
} | ||
/** | ||
* For servers that do not advertise cliPort, or if connection to cliPort failed, | ||
* we would have to rely on TTL timers. | ||
*/ | ||
if (!cliConnected) { | ||
__classPrivateFieldGet(this, _LMSDiscovery_discoveredTTLTimers, "f")[server.ip] = setTimeout(() => { | ||
delete __classPrivateFieldGet(this, _LMSDiscovery_discovered, "f")[server.ip]; | ||
delete __classPrivateFieldGet(this, _LMSDiscovery_discoveredTTLTimers, "f")[server.ip]; | ||
__classPrivateFieldGet(this, _LMSDiscovery_instances, "m", _LMSDiscovery_debugLog).call(this, `Detected lost server: ${JSON.stringify(server)}. Emitting 'lost' event...`); | ||
this.emit('lost', server); | ||
}, __classPrivateFieldGet(this, _LMSDiscovery_opts, "f").discoveredTTL); | ||
} | ||
if (!existing) { // Newly-discovered | ||
@@ -182,2 +221,11 @@ __classPrivateFieldGet(this, _LMSDiscovery_instances, "m", _LMSDiscovery_debugLog).call(this, 'This is a newly-discovered server. Emitting \'discovered\' event...'); | ||
}); | ||
}, _LMSDiscovery_handleCLIDisconnected = function _LMSDiscovery_handleCLIDisconnected(server) { | ||
const cliConnection = __classPrivateFieldGet(this, _LMSDiscovery_cliConnections, "f")[server.ip]; | ||
if (cliConnection) { | ||
cliConnection.removeAllListeners('close'); | ||
} | ||
delete __classPrivateFieldGet(this, _LMSDiscovery_cliConnections, "f")[server.ip]; | ||
delete __classPrivateFieldGet(this, _LMSDiscovery_discovered, "f")[server.ip]; | ||
__classPrivateFieldGet(this, _LMSDiscovery_instances, "m", _LMSDiscovery_debugLog).call(this, `Disconnected from server: ${JSON.stringify(server)}. Emitting 'lost' event...`); | ||
this.emit('lost', server); | ||
}, _LMSDiscovery_findDiscoveredByIP = function _LMSDiscovery_findDiscoveredByIP(ip) { | ||
@@ -184,0 +232,0 @@ return __classPrivateFieldGet(this, _LMSDiscovery_discovered, "f")[ip] || null; |
{ | ||
"name": "lms-discovery", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"type": "module", | ||
@@ -57,3 +57,4 @@ "description": "Logitech Media Server discovery for Node.js", | ||
"dependencies": { | ||
"deep-equal": "^2.2.1" | ||
"deep-equal": "^2.2.1", | ||
"telnet-client": "^2.0.8" | ||
}, | ||
@@ -60,0 +61,0 @@ "keywords": [ |
@@ -63,3 +63,3 @@ # lms-discovery | ||
- `broadcastAddress`: (string) network address used to transmit discovery requests. Default: `255.255.255.255`. | ||
- `discoveredTTL`: (number) how long in milliseconds to wait for a discovered server to respond to a subsqeuent discovery request before it is presumed lost. Default: `60000` (60 seconds). | ||
- `discoveredTTL`: (number) how long in milliseconds to wait for a discovered server to respond to a subsqeuent discovery request before it is presumed lost. *Only applicable for servers that do not advertise `cliPort`.* Default: `60000` (60 seconds). | ||
- `discoverInterval`: (number) how often in milliseconds to broadcast discovery requests. Default: `30000` (30 seconds). | ||
@@ -140,4 +140,3 @@ | ||
Emitted when a server is presumed lost, i.e. when it no longer responds to | ||
discovery requests within the default or `discoveredTTL` period passed to [`start()`](#api). | ||
Emitted when a server is lost. | ||
@@ -165,2 +164,5 @@ **Listener Params** | ||
1.1.0: | ||
- Instead of detecting server lost through `discoveredTTL` timers, emit 'lost' events immediately as they occur for servers that advertise `cliPort`. | ||
1.0.0: | ||
@@ -167,0 +169,0 @@ - Migrate to TypeScript and package as ESM / CJS hybrid module |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
91479
16.8%793
14.1%173
1.17%2
-33.33%2
100%+ Added
+ Added
+ Added
+ Added
+ Added