lms-discovery
Advanced tools
Comparing version
{ | ||
"name": "lms-discovery", | ||
"version": "0.1.0", | ||
"version": "1.0.0", | ||
"type": "module", | ||
"description": "Logitech Media Server discovery for Node.js", | ||
"main": "./lib/index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"build": "npm run prepare", | ||
"build:esm": "npx tsc -p tsconfig-esm.json", | ||
"build:cjs": "npx tsc -p tsconfig.json", | ||
"prepare": "rm -rf dist && npm run build:esm && npm run build:cjs && bash fixup.sh", | ||
"lint": "npx eslint ./src && npx eslint ./example", | ||
"lint:fix": "npx eslint ./src --fix && npx eslint ./example --fix", | ||
"doc": "npx typedoc", | ||
"example": "npx ts-node -P ./tsconfig-esm.json --esm ./example" | ||
}, | ||
"main": "./dist/cjs/index-cjs.js", | ||
"module": "./dist/mjs/index.js", | ||
"types": "./dist/mjs/index.d.ts", | ||
"exports": { | ||
".": { | ||
"import": { | ||
"types": "./dist/mjs/index.d.ts", | ||
"default": "./dist/mjs/index.js" | ||
}, | ||
"require": { | ||
"types": "./dist/cjs/index.d.ts", | ||
"default": "./dist/cjs/index-cjs.js" | ||
} | ||
} | ||
}, | ||
"author": "Patrick Kan <patrickkfkan@gmail.com> (https://github.com/patrickkfkan)", | ||
"repository": { | ||
@@ -13,2 +36,25 @@ "type": "git", | ||
}, | ||
"license": "MIT", | ||
"directories": { | ||
"dist": "./dist" | ||
}, | ||
"engines": { | ||
"node": ">=14" | ||
}, | ||
"devDependencies": { | ||
"@types/deep-equal": "^1.0.1", | ||
"@types/node": "^14.18.38", | ||
"@typescript-eslint/eslint-plugin": "^5.56.0", | ||
"@typescript-eslint/parser": "^5.56.0", | ||
"eslint": "^8.36.0", | ||
"eslint-plugin-tsdoc": "^0.2.17", | ||
"ts-node": "^10.9.1", | ||
"typedoc": "^0.24.0", | ||
"typedoc-plugin-markdown": "^3.14.0", | ||
"typedoc-plugin-rename-defaults": "^0.6.4", | ||
"typescript": "^4.9.5" | ||
}, | ||
"dependencies": { | ||
"deep-equal": "^2.2.1" | ||
}, | ||
"keywords": [ | ||
@@ -19,9 +65,3 @@ "logitech media server", | ||
"discovery" | ||
], | ||
"author": "Patrick Kan", | ||
"license": "MIT", | ||
"dependencies": { | ||
"deep-equal": "^2.0.5", | ||
"eventemitter3": "^4.0.7" | ||
} | ||
] | ||
} |
179
README.md
@@ -14,6 +14,9 @@ # lms-discovery | ||
``` | ||
// ESM | ||
import discovery from 'lms-discovery'; | ||
// CJS | ||
const discovery = require('lms-discovery'); | ||
discovery.on('discovered', (server) => { | ||
// Do something with discovered server | ||
console.log('Server discovered:', server); | ||
}); | ||
@@ -30,108 +33,136 @@ | ||
discovery.stop(); | ||
// Output | ||
Server discovered: { | ||
ip: '192.168.1.85', // Server's IP address | ||
name: 'my-lms-server', // Server name | ||
ver: '8.2.1', // Server version | ||
uuid: '187fa185-d108-408b-a8bd-8f5a4bb855bd', // Unique identifier | ||
jsonPort: '9000', // Port for JSON-RPC requests | ||
cliPort: '9090' // Port for CLI commands and queries | ||
} | ||
``` | ||
Run [example](example/index.ts): | ||
``` | ||
npm run example | ||
``` | ||
## API | ||
### Event functions | ||
<details> | ||
<summary><code>start([options])</code></summary> | ||
<br /> | ||
`on(event, listener)`<br> | ||
`once(event, listener)`<br> | ||
`off(event[, listener])`<br> | ||
<p>Starts the discovery service.</p> | ||
- `event` \<string\> | ||
- `listener` \<Function\> | ||
**Params** | ||
|Function |Description | | ||
|--------------------------|------------------------------------------------| | ||
|`on(event, listener)` |Adds `listener` function for `event`. | | ||
|`once(event, listener)` |Adds one-time `listener` function for `event`. | | ||
|`off(event[, listener])` |Removes `listener` function for `event`. If `listener` is not specified, removes all listeners for the event.| | ||
- `options`: (*optional* and *all properties optional*) | ||
- `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). | ||
- `discoverInterval`: (number) how often in milliseconds to broadcast discovery requests. Default: `30000` (30 seconds). | ||
Events: | ||
> `discoveredTTL` must be larger than `discoverInterval`. | ||
|Event | Description | | ||
|---------------|-----------------------------------------------------------| | ||
|`discovered` |Emitted when a new server is discovered. | | ||
|`lost` |Emitted when a previously-discovered server is lost. A server is considered lost when it no longer responds to discovery requests within a stipulated period.| | ||
--- | ||
</details> | ||
Data passed to `listener`: | ||
<details> | ||
<summary><code>stop()</code></summary> | ||
<br /> | ||
|Event |Data | | ||
|---------------|-----------------------------------------------------------| | ||
|`discovered` |\<Object\> Server info of discovered server | | ||
|`lost` |\<Object\> Server info of lost server | | ||
<p>Stops the discovery service.</p> | ||
Example server info: | ||
``` | ||
{ | ||
ip: '192.168.1.85', // Server's IP address | ||
name: 'my-lms-server', // Server name | ||
ver: '8.2.1', // Server version | ||
uuid: '187fa185-d108-408b-a8bd-8f5a4bb855bd', | ||
jsonPort: '9000', // Port for JSON-RPC requests | ||
cliPort: '9090' // Port for CLI commands and queries | ||
} | ||
``` | ||
--- | ||
</details> | ||
### `start([options])` | ||
<details> | ||
<summary><code>getStatus()</code></summary> | ||
<br /> | ||
- `options` \<Object\> | ||
- `broadcastAddress`: the broadcast address to which discovery requests are to be sent. Default: `255.255.255.255`. | ||
- `discoveredTTL`: period in milliseconds. If a previously-discovered server does not respond to subsequent discovery requests made within this period, it is considered lost. Default: `60000` (60 seconds). | ||
- `discoverInterval`: the interval in milliseconds at which discovery requests are sent. Default: `30000` (30 seconds). | ||
<p>Gets the status of the discovery service.</p> | ||
Starts the discovery service. | ||
**Returns** | ||
After discovery has started, you must stop it before calling `start()` again. If you don't do this, an error will be thrown. | ||
`running` or `stop` | ||
Normally, you would register listeners for the `discover` and `lost` events before calling `start()`. | ||
--- | ||
</details> | ||
> If you specify `discoveredTTL` and / or `discoverInterval` in `options`, make sure the former is larger than the latter, otherwise an error will be thrown. Also ensure that `discoveredTTL` is reasonably larger than `discoverInterval`, so as to avoid the situation where a discovered server actually responds but appears momentarily lost because a discovery request is not made in time. | ||
<details> | ||
<summary><code>getAllDiscovered()</code></summary> | ||
<br /> | ||
### `stop()` | ||
<p>Gets all servers discovered so far.</p> | ||
Stops the discovery service. | ||
**Returns** | ||
### `getStatus()` | ||
Array<[ServerInfo](docs/api/interfaces/ServerInfo.md)> | ||
- Returns: \<string\> | ||
--- | ||
</details> | ||
Returns the status of the discovery service: `running` or `stop`. | ||
<details> | ||
<summary><code>setDebug(enabled[, callback])</code></summary> | ||
<br /> | ||
### `getAllDiscovered()` | ||
<p>Whether to enable debug messages.</p> | ||
- Returns: \<Array\> | ||
**Params** | ||
- `enabled`: (boolean) | ||
- `callback`: (function) | ||
- If specified, debug messages will be passed to `callback`. | ||
- If not specified, debug messages will be printed to console. | ||
Returns an array listing all servers discovered so far. The values in the array are Objects: | ||
--- | ||
</details> | ||
``` | ||
[ | ||
{ | ||
ip: '192.168.1.85', | ||
name: 'my-lms-server', | ||
ver: '8.2.1', | ||
uuid: '187fa185-d108-408b-a8bd-8f5a4bb855bd', | ||
jsonPort: '9000', | ||
cliPort: '9090' | ||
}, | ||
{ | ||
ip: '192.168.1.132', | ||
name: 'my-lms-server2', | ||
... | ||
}, | ||
... | ||
] | ||
``` | ||
### Events | ||
### `setDebug(enabled[, callback])` | ||
<details> | ||
<summary><code>on('discovered', (server) => ...)</code></summary> | ||
<br /> | ||
- `enabled` \<boolean\> | ||
- `callback` \<Function\> | ||
- If not specified, output debug messages to console. | ||
- If specified, pass debug messages to `callback` function instead of outputting to console. | ||
<p>Emitted when a server is discovered.</p> | ||
Specifies whether to enable debug messages. | ||
**Listener Params** | ||
- `server`: [ServerInfo](docs/api/interfaces/ServerInfo.md) | ||
--- | ||
</details> | ||
<details> | ||
<summary><code>on('lost', (server) => ...)</code></summary> | ||
<br /> | ||
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). | ||
**Listener Params** | ||
- `server`: [ServerInfo](docs/api/interfaces/ServerInfo.md) | ||
--- | ||
</details> | ||
<details> | ||
<summary><code>on('error', (error) => ...)</code></summary> | ||
<br /> | ||
<p>Emitted when an error has occurred.</p> | ||
**Listener Params** | ||
- `error`: (any) | ||
--- | ||
</details> | ||
## Changelog | ||
1.0.0: | ||
- Migrate to TypeScript and package as ESM / CJS hybrid module | ||
0.1.0: | ||
@@ -138,0 +169,0 @@ - Initial release |
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
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
78319
559.7%1
-50%16
220%695
207.52%1
-66.67%171
22.14%Yes
NaN11
Infinity%3
200%- Removed
- Removed
Updated