ember-router 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -2
- package/index.js +41 -6
- package/package.json +1 -1
- package/request-socket.js +102 -0
package/README.md
CHANGED
|
@@ -35,8 +35,9 @@ ember-router reset -p <path> --yes [options]
|
|
|
35
35
|
- `--timeout <ms>` for scan and network callbacks (default `45000`).
|
|
36
36
|
- `-v, --verbose` for more diagnostics on stderr. Repeat for debug output during `run`.
|
|
37
37
|
- `--write-timeout <ms>` for `run`, `write`, and `read` to set the APS delivery timeout (default `15000`).
|
|
38
|
-
- `--read-timeout <ms>` for `read` to set the ZCL reply timeout (default `5000`, range `100`–`300000`).
|
|
39
|
-
- `--expect <commandId>` for `read` to select the ZCL reply command by decimal or `0x` value.
|
|
38
|
+
- `--read-timeout <ms>` for `read` and `run --socket` to set the ZCL reply timeout (default `5000`, range `100`–`300000`).
|
|
39
|
+
- `--expect <commandId>` for `read` and `run --socket` to select the ZCL reply command by decimal or `0x` value.
|
|
40
|
+
- `--socket <path>` for `run` to accept requests over a local Unix domain socket or a Windows named pipe.
|
|
40
41
|
|
|
41
42
|
#### Joining networks
|
|
42
43
|
|
|
@@ -119,3 +120,13 @@ The router automatically expects Read Attributes Response (`0x01`) or Write Attr
|
|
|
119
120
|
```json
|
|
120
121
|
{"type":"aps","source":4660,"sourceEndpoint":1,"destinationEndpoint":1,"profileId":260,"clusterId":513,"lqi":180,"payload":"180101120000299e07"}
|
|
121
122
|
```
|
|
123
|
+
|
|
124
|
+
### Local socket
|
|
125
|
+
|
|
126
|
+
```sh
|
|
127
|
+
ember-router run -p tcp://192.168.1.42:6638 --socket /path/to/private/ember-router.sock
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
Send one packet per JSON line and receive one reply per line in the same order. A failed request returns `null` and logs the error on stderr. Keep the connection open until replies arrive. The socket supports 16 clients and 8192 bytes per line, with one request active on the adapter at a time.
|
|
131
|
+
|
|
132
|
+
Use a private directory for the Unix socket. If a crash leaves a stale socket, confirm it is unused before removing it. On Windows, use a named pipe such as `\\.\pipe\ember-router`.
|
package/index.js
CHANGED
|
@@ -3,6 +3,7 @@ import { once } from 'node:events';
|
|
|
3
3
|
import { createInterface } from 'node:readline';
|
|
4
4
|
import { Command, InvalidArgumentError, Option } from 'commander';
|
|
5
5
|
import packageJson from './package.json' with { type: 'json' };
|
|
6
|
+
import { openRequestSocket } from './request-socket.js';
|
|
6
7
|
import { attachNetwork, capture, openRouter, resetRouter, sendPacket, SLStatus, Zcl } from './router.js';
|
|
7
8
|
|
|
8
9
|
function integer(min, max) {
|
|
@@ -43,7 +44,21 @@ function parsePacket(line) {
|
|
|
43
44
|
return packet;
|
|
44
45
|
}
|
|
45
46
|
|
|
46
|
-
|
|
47
|
+
function createSender(ezsp, timeout, signal) {
|
|
48
|
+
let tag = 0;
|
|
49
|
+
let pending = Promise.resolve();
|
|
50
|
+
return (packet, expect, requestSignal = signal) => {
|
|
51
|
+
const operation = pending.then(() => {
|
|
52
|
+
requestSignal.throwIfAborted();
|
|
53
|
+
tag = (tag + 1) & 0x7f;
|
|
54
|
+
return sendPacket(ezsp, packet, tag, timeout, requestSignal, expect);
|
|
55
|
+
});
|
|
56
|
+
pending = operation.catch(() => {});
|
|
57
|
+
return operation;
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async function readWrites(send, signal) {
|
|
47
62
|
const { stdin } = process;
|
|
48
63
|
if (stdin.readableEnded || stdin.destroyed) {
|
|
49
64
|
return;
|
|
@@ -66,7 +81,7 @@ async function readWrites(ezsp, timeout, signal) {
|
|
|
66
81
|
}
|
|
67
82
|
|
|
68
83
|
try {
|
|
69
|
-
await
|
|
84
|
+
await send(parsePacket(line));
|
|
70
85
|
} catch (error) {
|
|
71
86
|
if (signal.aborted) {
|
|
72
87
|
break;
|
|
@@ -126,6 +141,7 @@ async function run(options, mode, packet) {
|
|
|
126
141
|
process.on('SIGTERM', interrupt);
|
|
127
142
|
process.stdout.on('error', brokenPipe);
|
|
128
143
|
let session;
|
|
144
|
+
let closeSocket;
|
|
129
145
|
try {
|
|
130
146
|
session = await openRouter(options, log);
|
|
131
147
|
signal.throwIfAborted();
|
|
@@ -162,12 +178,26 @@ async function run(options, mode, packet) {
|
|
|
162
178
|
}
|
|
163
179
|
|
|
164
180
|
signal.throwIfAborted();
|
|
181
|
+
const expect = { commandId: options.expect, timeout: options.readTimeout };
|
|
165
182
|
if (mode === 'read') {
|
|
166
|
-
const expect = { commandId: options.expect, timeout: options.readTimeout };
|
|
167
183
|
return await sendPacket(ezsp, packet, 1, options.writeTimeout, signal, expect);
|
|
168
184
|
}
|
|
169
185
|
|
|
170
|
-
|
|
186
|
+
const send = createSender(ezsp, options.writeTimeout, signal);
|
|
187
|
+
if (options.socket !== undefined) {
|
|
188
|
+
const read = async (line, requestSignal) => {
|
|
189
|
+
try {
|
|
190
|
+
return await send(parsePacket(line), expect, requestSignal);
|
|
191
|
+
} catch (error) {
|
|
192
|
+
if (!requestSignal.aborted) {
|
|
193
|
+
writeLog('error', `Socket request: ${error.message}`);
|
|
194
|
+
}
|
|
195
|
+
return null;
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
closeSocket = await openRequestSocket(options.socket, read, signal, error => stop.abort(error));
|
|
199
|
+
}
|
|
200
|
+
await readWrites(send, signal);
|
|
171
201
|
if (!options.exitAfterWrite && !signal.aborted) {
|
|
172
202
|
await once(signal, 'abort');
|
|
173
203
|
}
|
|
@@ -178,7 +208,11 @@ async function run(options, mode, packet) {
|
|
|
178
208
|
}
|
|
179
209
|
} finally {
|
|
180
210
|
try {
|
|
181
|
-
|
|
211
|
+
try {
|
|
212
|
+
await closeSocket?.();
|
|
213
|
+
} finally {
|
|
214
|
+
await session?.close();
|
|
215
|
+
}
|
|
182
216
|
} finally {
|
|
183
217
|
process.off('SIGINT', interrupt);
|
|
184
218
|
process.off('SIGTERM', interrupt);
|
|
@@ -218,8 +252,9 @@ function readOptions(command) {
|
|
|
218
252
|
.option('--expect <commandId>', 'Expected ZCL reply command ID (inferred for attribute reads and writes)', integer(0, 255));
|
|
219
253
|
}
|
|
220
254
|
|
|
221
|
-
|
|
255
|
+
readOptions(program.command('run', { isDefault: true }))
|
|
222
256
|
.description('Resume, stream received packets, and accept writes on stdin')
|
|
257
|
+
.option('--socket <path>', 'Listen for requests on a local IPC socket')
|
|
223
258
|
.action((_options, command) => {
|
|
224
259
|
const options = command.optsWithGlobals();
|
|
225
260
|
return run(options, 'run');
|
package/package.json
CHANGED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { once } from 'node:events';
|
|
2
|
+
import { createServer } from 'node:net';
|
|
3
|
+
|
|
4
|
+
const MAX_CLIENTS = 16;
|
|
5
|
+
const MAX_LINE_BYTES = 8192;
|
|
6
|
+
|
|
7
|
+
function validateSocketPath(path) {
|
|
8
|
+
if (process.platform === 'win32') {
|
|
9
|
+
if (typeof path !== 'string' || !/^\\\\[.?]\\pipe\\[^\\/\0]+$/i.test(path)) {
|
|
10
|
+
throw new Error('Expected a Windows named pipe path (\\\\.\\pipe\\name).');
|
|
11
|
+
}
|
|
12
|
+
} else {
|
|
13
|
+
if (typeof path !== 'string' || !path.startsWith('/') || path.includes('\0')) {
|
|
14
|
+
throw new Error('Expected an absolute Unix socket path.');
|
|
15
|
+
}
|
|
16
|
+
const maxPathBytes = process.platform === 'linux' ? 107 : 103;
|
|
17
|
+
if (Buffer.byteLength(path) > maxPathBytes) {
|
|
18
|
+
throw new Error(`Socket path is longer than ${maxPathBytes} bytes.`);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function checkLineLength(line) {
|
|
24
|
+
if (Buffer.byteLength(line) > MAX_LINE_BYTES) {
|
|
25
|
+
throw new Error(`Request line exceeds ${MAX_LINE_BYTES} bytes.`);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async function serveClient(socket, handleRequest, signal) {
|
|
30
|
+
const disconnected = new AbortController();
|
|
31
|
+
socket.once('close', () => disconnected.abort());
|
|
32
|
+
const requestSignal = AbortSignal.any([signal, disconnected.signal]);
|
|
33
|
+
|
|
34
|
+
const reply = async line => {
|
|
35
|
+
checkLineLength(line);
|
|
36
|
+
if (!line.trim()) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
requestSignal.throwIfAborted();
|
|
40
|
+
const result = await handleRequest(line, requestSignal);
|
|
41
|
+
requestSignal.throwIfAborted();
|
|
42
|
+
await new Promise((resolve, reject) => {
|
|
43
|
+
socket.write(`${JSON.stringify(result)}\n`, error => error ? reject(error) : resolve());
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
let buffer = '';
|
|
48
|
+
// Keep replies writable after a client half-closes.
|
|
49
|
+
for await (const chunk of socket.setEncoding('utf8').iterator({ destroyOnReturn: false })) {
|
|
50
|
+
buffer += chunk;
|
|
51
|
+
let newline;
|
|
52
|
+
while ((newline = buffer.indexOf('\n')) !== -1) {
|
|
53
|
+
const line = buffer.slice(0, newline);
|
|
54
|
+
buffer = buffer.slice(newline + 1);
|
|
55
|
+
await reply(line);
|
|
56
|
+
}
|
|
57
|
+
checkLineLength(buffer);
|
|
58
|
+
}
|
|
59
|
+
await reply(buffer);
|
|
60
|
+
socket.end();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export async function openRequestSocket(path, handleRequest, signal, onError) {
|
|
64
|
+
validateSocketPath(path);
|
|
65
|
+
signal.throwIfAborted();
|
|
66
|
+
|
|
67
|
+
const clients = new Set();
|
|
68
|
+
const server = createServer({ allowHalfOpen: true }, socket => {
|
|
69
|
+
clients.add(socket);
|
|
70
|
+
socket.on('error', () => socket.destroy());
|
|
71
|
+
socket.once('close', () => clients.delete(socket));
|
|
72
|
+
serveClient(socket, handleRequest, signal).catch(() => socket.destroy());
|
|
73
|
+
});
|
|
74
|
+
server.maxConnections = MAX_CLIENTS;
|
|
75
|
+
|
|
76
|
+
let closing;
|
|
77
|
+
const close = () => {
|
|
78
|
+
if (!closing) {
|
|
79
|
+
signal.removeEventListener('abort', abort);
|
|
80
|
+
closing = new Promise(resolve => server.close(() => resolve()));
|
|
81
|
+
for (const socket of clients) {
|
|
82
|
+
socket.destroy();
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return closing;
|
|
86
|
+
};
|
|
87
|
+
const abort = () => {
|
|
88
|
+
void close();
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
await once(server.listen(path), 'listening');
|
|
92
|
+
if (signal.aborted) {
|
|
93
|
+
await close();
|
|
94
|
+
signal.throwIfAborted();
|
|
95
|
+
}
|
|
96
|
+
server.on('error', error => {
|
|
97
|
+
void close();
|
|
98
|
+
onError?.(error);
|
|
99
|
+
});
|
|
100
|
+
signal.addEventListener('abort', abort, { once: true });
|
|
101
|
+
return close;
|
|
102
|
+
}
|