@johnhenry/wsh 0.31.0 → 0.32.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 +17 -0
- package/package.json +1 -1
- package/src/client.mjs +51 -2
- package/src/index.d.ts +13 -0
- package/src/session.mjs +36 -1
package/README.md
CHANGED
|
@@ -170,6 +170,23 @@ arrives on it, so against a host that does not advertise `stream-announce`
|
|
|
170
170
|
the client writes a one-byte primer for you (a host that strips it, as those
|
|
171
171
|
hosts do). Pass `primer: false` to never send it.
|
|
172
172
|
|
|
173
|
+
### Stdin and EOF
|
|
174
|
+
|
|
175
|
+
`stdin` feeds the command and then closes its write side, so `cat`, `wc -c` and `sh -s` finish:
|
|
176
|
+
|
|
177
|
+
```js
|
|
178
|
+
const { stdout } = await WshClient.exec(url, 'wc -c', { username, keyPair, stdin: new Uint8Array(1000) });
|
|
179
|
+
// stdin: a string, a Uint8Array, a ReadableStream, or an (async) iterable of strings / bytes
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
On an open exec session, `await session.write(data); await session.closeStdin()` sends EOF without closing the
|
|
183
|
+
session: output keeps arriving and `onExit` / `onClose` fire as usual. Without `stdin`, `exec` never touches stdin.
|
|
184
|
+
|
|
185
|
+
EOF is the end of the session's stdin data stream, so it needs a **stream-mode** host: `@johnhenry/wsh/server` (any
|
|
186
|
+
version, nothing to upgrade server-side). The Rust `wsh-server` runs exec on a PTY with stdin carried as control
|
|
187
|
+
messages (`data_mode: virtual`) and has no EOF message; `closeStdin()` throws there and `exec({ stdin })` rejects
|
|
188
|
+
rather than silently hanging (tracked in #105).
|
|
189
|
+
|
|
173
190
|
## Node Server
|
|
174
191
|
|
|
175
192
|
`@johnhenry/wsh/server` is a Node host for the same protocol, over WebSocket
|
package/package.json
CHANGED
package/src/client.mjs
CHANGED
|
@@ -173,6 +173,42 @@ async function normalizeExpectedHostKey(v) {
|
|
|
173
173
|
|
|
174
174
|
// ── Client class ──────────────────────────────────────────────────────
|
|
175
175
|
|
|
176
|
+
const STDIN_CHUNK = 64 * 1024;
|
|
177
|
+
|
|
178
|
+
function assertStdinSource(stdin) {
|
|
179
|
+
if (typeof stdin === 'string' || stdin instanceof Uint8Array) return;
|
|
180
|
+
if (stdin && (typeof stdin.getReader === 'function' || typeof stdin[Symbol.asyncIterator] === 'function' || typeof stdin[Symbol.iterator] === 'function')) return;
|
|
181
|
+
throw new TypeError('exec: stdin must be a string, Uint8Array, ReadableStream or (async) iterable of those');
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** Write `stdin` to `session` in bounded chunks, then close the write side (EOF). */
|
|
185
|
+
async function pumpStdin(session, stdin) {
|
|
186
|
+
const send = async (chunk) => {
|
|
187
|
+
if (typeof chunk === 'string') chunk = new TextEncoder().encode(chunk);
|
|
188
|
+
if (!(chunk instanceof Uint8Array)) throw new TypeError('exec: stdin chunks must be strings or Uint8Arrays');
|
|
189
|
+
for (let i = 0; i < chunk.byteLength; i += STDIN_CHUNK) {
|
|
190
|
+
await session.write(chunk.subarray(i, i + STDIN_CHUNK));
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
if (typeof stdin === 'string' || stdin instanceof Uint8Array) {
|
|
194
|
+
await send(stdin);
|
|
195
|
+
} else if (typeof stdin.getReader === 'function') {
|
|
196
|
+
const reader = stdin.getReader();
|
|
197
|
+
try {
|
|
198
|
+
for (;;) {
|
|
199
|
+
const { value, done } = await reader.read();
|
|
200
|
+
if (done) break;
|
|
201
|
+
await send(value);
|
|
202
|
+
}
|
|
203
|
+
} finally {
|
|
204
|
+
reader.releaseLock?.();
|
|
205
|
+
}
|
|
206
|
+
} else {
|
|
207
|
+
for await (const chunk of stdin) await send(chunk);
|
|
208
|
+
}
|
|
209
|
+
await session.closeStdin();
|
|
210
|
+
}
|
|
211
|
+
|
|
176
212
|
export class WshClient {
|
|
177
213
|
|
|
178
214
|
/** @type {'disconnected'|'connecting'|'connected'|'authenticated'|'closed'} */
|
|
@@ -1000,9 +1036,13 @@ export class WshClient {
|
|
|
1000
1036
|
* @param {string} [opts.password]
|
|
1001
1037
|
* @param {number} [opts.timeout=60000] - Overall timeout in ms
|
|
1002
1038
|
* @param {boolean} [opts.primer=true] - See `openSession({ primer })`
|
|
1039
|
+
* @param {string|Uint8Array|ReadableStream|AsyncIterable|Iterable} [opts.stdin] - Input for the
|
|
1040
|
+
* command. It is written, then stdin is closed (EOF), so `cat`, `wc -c` and `sh -s` finish.
|
|
1041
|
+
* Omitted: stdin is never touched (unchanged behaviour). Needs a stream-mode host.
|
|
1003
1042
|
* @returns {Promise<{stdout: Uint8Array, exitCode: number}>}
|
|
1004
1043
|
*/
|
|
1005
|
-
static async exec(url, command, { username, keyPair, signer, password, timeout = DEFAULT_EXEC_TIMEOUT, primer = true, ...hostKeyOpts } = {}) {
|
|
1044
|
+
static async exec(url, command, { username, keyPair, signer, password, timeout = DEFAULT_EXEC_TIMEOUT, primer = true, stdin, ...hostKeyOpts } = {}) {
|
|
1045
|
+
if (stdin !== undefined) assertStdinSource(stdin);
|
|
1006
1046
|
const client = new WshClient();
|
|
1007
1047
|
const chunks = [];
|
|
1008
1048
|
let exitCode = -1;
|
|
@@ -1010,7 +1050,9 @@ export class WshClient {
|
|
|
1010
1050
|
try {
|
|
1011
1051
|
await client.connect(url, { username, keyPair, signer, password, ...hostKeyOpts });
|
|
1012
1052
|
|
|
1013
|
-
|
|
1053
|
+
// The first stdin byte (or the EOF itself) announces the data stream to a
|
|
1054
|
+
// legacy host, so the primer byte is only needed when there is no stdin.
|
|
1055
|
+
const session = await client.openSession({ type: 'exec', command, primer: stdin === undefined ? primer : false });
|
|
1014
1056
|
|
|
1015
1057
|
await new Promise((resolve, reject) => {
|
|
1016
1058
|
const timer = setTimeout(() => {
|
|
@@ -1029,6 +1071,13 @@ export class WshClient {
|
|
|
1029
1071
|
clearTimeout(timer);
|
|
1030
1072
|
resolve();
|
|
1031
1073
|
};
|
|
1074
|
+
|
|
1075
|
+
if (stdin !== undefined) {
|
|
1076
|
+
pumpStdin(session, stdin).catch((err) => {
|
|
1077
|
+
clearTimeout(timer);
|
|
1078
|
+
reject(err);
|
|
1079
|
+
});
|
|
1080
|
+
}
|
|
1032
1081
|
});
|
|
1033
1082
|
|
|
1034
1083
|
} finally {
|
package/src/index.d.ts
CHANGED
|
@@ -1700,6 +1700,13 @@ export class WshSession {
|
|
|
1700
1700
|
*/
|
|
1701
1701
|
write(data: Uint8Array | string): Promise<void>;
|
|
1702
1702
|
|
|
1703
|
+
/**
|
|
1704
|
+
* Send EOF on stdin without closing the session: output keeps arriving and
|
|
1705
|
+
* `onExit`/`onClose` fire as usual. Idempotent; `write()` rejects afterwards.
|
|
1706
|
+
* Stream-mode sessions only (throws on a virtual-mode session).
|
|
1707
|
+
*/
|
|
1708
|
+
closeStdin(): Promise<void>;
|
|
1709
|
+
|
|
1703
1710
|
/**
|
|
1704
1711
|
* Opt in to end-to-end encryption for this session's data plane
|
|
1705
1712
|
* (virtual-mode sessions only). `sharedSecret` must come from a fresh
|
|
@@ -1916,6 +1923,12 @@ export interface WshExecOptions extends WshHostKeyOptions {
|
|
|
1916
1923
|
timeout?: number;
|
|
1917
1924
|
/** See {@link WshOpenSessionOptions.primer}. */
|
|
1918
1925
|
primer?: boolean;
|
|
1926
|
+
/**
|
|
1927
|
+
* Input for the command: written, then stdin is closed (EOF), so `cat`,
|
|
1928
|
+
* `wc -c` and `sh -s` finish. Omitted: stdin is not touched. Needs a
|
|
1929
|
+
* stream-mode host (`@johnhenry/wsh/server`).
|
|
1930
|
+
*/
|
|
1931
|
+
stdin?: string | Uint8Array | ReadableStream<Uint8Array | string> | AsyncIterable<Uint8Array | string> | Iterable<Uint8Array | string>;
|
|
1919
1932
|
}
|
|
1920
1933
|
|
|
1921
1934
|
/** Result from WshClient.exec(). */
|
package/src/session.mjs
CHANGED
|
@@ -95,6 +95,9 @@ export class WshSession {
|
|
|
95
95
|
*/
|
|
96
96
|
#stdinWriter = null;
|
|
97
97
|
|
|
98
|
+
/** True once `closeStdin()` has sent EOF on the stdin stream (#104). */
|
|
99
|
+
#stdinClosed = false;
|
|
100
|
+
|
|
98
101
|
/** @type {WshVirtualSessionBackend|null} */
|
|
99
102
|
#virtualBackend = null;
|
|
100
103
|
|
|
@@ -548,6 +551,9 @@ export class WshSession {
|
|
|
548
551
|
if (this.#stdinWriter === null) {
|
|
549
552
|
throw new Error('Session not yet bound — stdin writer unavailable');
|
|
550
553
|
}
|
|
554
|
+
if (this.#stdinClosed) {
|
|
555
|
+
throw new Error('Cannot write to a session whose stdin is closed');
|
|
556
|
+
}
|
|
551
557
|
|
|
552
558
|
const bytes = typeof data === 'string' ? textEncoder.encode(data) : data;
|
|
553
559
|
|
|
@@ -563,6 +569,34 @@ export class WshSession {
|
|
|
563
569
|
await this.#stdinWriter.write(bytes);
|
|
564
570
|
}
|
|
565
571
|
|
|
572
|
+
/**
|
|
573
|
+
* Close the write side of the session (send EOF on stdin) without closing
|
|
574
|
+
* the session: the process sees end-of-input (`cat` finishes, `sh -s` runs
|
|
575
|
+
* what it read), its output keeps arriving, and `onExit`/`onClose` fire as
|
|
576
|
+
* usual. Bytes still batched by write-coalescing are flushed first. Safe to
|
|
577
|
+
* call more than once; later `write()` calls reject.
|
|
578
|
+
*
|
|
579
|
+
* Stream-mode sessions only (the `stdin` data stream ends, which every
|
|
580
|
+
* `@johnhenry/wsh/server` host treats as EOF). A virtual-mode session has
|
|
581
|
+
* no separate stdin stream and no EOF message, so this throws there
|
|
582
|
+
* (the Rust `wsh-server` is such a host, see the README).
|
|
583
|
+
*
|
|
584
|
+
* @returns {Promise<void>}
|
|
585
|
+
*/
|
|
586
|
+
async closeStdin() {
|
|
587
|
+
if (this.#stdinClosed) return;
|
|
588
|
+
if (this.#state === STATE_CLOSED) return;
|
|
589
|
+
if (this.#dataMode !== 'stream') {
|
|
590
|
+
throw new Error('closeStdin() needs a stream-mode session; this session\'s host carries stdin as control messages, which have no EOF');
|
|
591
|
+
}
|
|
592
|
+
if (this.#stdinWriter === null) {
|
|
593
|
+
throw new Error('Session not yet bound — stdin writer unavailable');
|
|
594
|
+
}
|
|
595
|
+
this.#stdinClosed = true;
|
|
596
|
+
if (this.#streamCoalescer) await this.#streamCoalescer.flush();
|
|
597
|
+
await this.#stdinWriter.close();
|
|
598
|
+
}
|
|
599
|
+
|
|
566
600
|
/**
|
|
567
601
|
* Read one chunk from the session data plane.
|
|
568
602
|
*
|
|
@@ -1021,7 +1055,8 @@ export class WshSession {
|
|
|
1021
1055
|
*/
|
|
1022
1056
|
#releaseStreams() {
|
|
1023
1057
|
try {
|
|
1024
|
-
|
|
1058
|
+
// Already closed after closeStdin(): the rejection is expected.
|
|
1059
|
+
this.#stdinWriter?.close()?.catch?.(() => {});
|
|
1025
1060
|
} catch {
|
|
1026
1061
|
// Ignore.
|
|
1027
1062
|
}
|