@johnhenry/wsh 0.30.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/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
- this.#stdinWriter?.close();
1058
+ // Already closed after closeStdin(): the rejection is expected.
1059
+ this.#stdinWriter?.close()?.catch?.(() => {});
1025
1060
  } catch {
1026
1061
  // Ignore.
1027
1062
  }
@@ -97,6 +97,9 @@ class QMuxStreamAdapter {
97
97
  }
98
98
  }
99
99
 
100
+ /** `WebSocket.OPEN` (spelled out so a substitute constructor need not define it). */
101
+ const WS_OPEN = 1;
102
+
100
103
  // ── WebSocket transport ──────────────────────────────────────────────
101
104
 
102
105
  /**
@@ -133,6 +136,19 @@ export class WebSocketTransport extends WshTransport {
133
136
  */
134
137
  #inbox = new SerialQueue((raw) => this.#handleControlBytes(raw));
135
138
 
139
+ /** @type {typeof WebSocket} */
140
+ #WebSocket;
141
+
142
+ /**
143
+ * @param {object} [options]
144
+ * @param {typeof WebSocket} [options.WebSocket] - A WebSocket constructor to dial with instead of the global one: a
145
+ * test double, or the in-memory host's (`createMemoryHost().WebSocket`, see `memory-host.mjs`).
146
+ */
147
+ constructor({ WebSocket: Impl } = {}) {
148
+ super();
149
+ this.#WebSocket = Impl;
150
+ }
151
+
136
152
  // ── Lifecycle ──────────────────────────────────────────────────────
137
153
 
138
154
  /** @override */
@@ -149,7 +165,7 @@ export class WebSocketTransport extends WshTransport {
149
165
  .replace(/^https:\/\//, 'wss://')
150
166
  .replace(/^http:\/\//, 'ws://');
151
167
 
152
- const ws = new WebSocket(wsUrl);
168
+ const ws = new (this.#WebSocket ?? WebSocket)(wsUrl);
153
169
  ws.binaryType = 'arraybuffer';
154
170
  this.#ws = ws;
155
171
 
@@ -316,7 +332,7 @@ export class WebSocketTransport extends WshTransport {
316
332
  * @param {Uint8Array} data
317
333
  */
318
334
  #send(data) {
319
- if (!this.#ws || this.#ws.readyState !== WebSocket.OPEN) {
335
+ if (!this.#ws || this.#ws.readyState !== WS_OPEN) {
320
336
  throw new Error('WebSocket is not open');
321
337
  }
322
338
  this.#ws.send(data);