@johnhenry/wsh 0.46.0 → 0.46.1

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 CHANGED
@@ -2015,7 +2015,7 @@ instead of dragging its own toolchain and CI job into an unrelated
2015
2015
  browser-app repo. It was previously deleted from clawser by accident
2016
2016
  (2026-03-14) and restored (2026-07-05) before this move.
2017
2017
 
2018
- Four crates, workspace-versioned (currently `0.17.0`):
2018
+ Four crates, workspace-versioned (currently `0.17.1`):
2019
2019
 
2020
2020
  | Crate | Kind | Description |
2021
2021
  |-------|------|--------------|
@@ -2263,7 +2263,7 @@ plus one `SHA256SUMS` file covering every archive in the release. These
2263
2263
  names are a stable contract other repos (clawser) pin and download by --
2264
2264
  do not change them without coordinating downstream.
2265
2265
 
2266
- `wsh --version` and `wsh-server --version` print the workspace version the binary was built from (`wsh 0.17.0`), which is the
2266
+ `wsh --version` and `wsh-server --version` print the workspace version the binary was built from (`wsh 0.17.1`), which is the
2267
2267
  number in the `rust-vX.Y.Z` tag. Binaries of `rust-v0.9.0` to `rust-v0.10.0` printed a fixed `0.1.0`, so against one of those the
2268
2268
  version cannot be read from the binary (look for a flag in `wsh --help` instead).
2269
2269
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@johnhenry/wsh",
3
- "version": "0.46.0",
3
+ "version": "0.46.1",
4
4
  "description": "Web Shell \u2014 browser-native remote command execution over WebTransport and WebSocket with Ed25519 authentication",
5
5
  "type": "module",
6
6
  "main": "./src/index.mjs",
@@ -91,7 +91,7 @@
91
91
  "@modelcontextprotocol/sdk": "^1.17.0"
92
92
  },
93
93
  "scripts": {
94
- "test": "node --test test/*.test.mjs",
94
+ "test": "node scripts/run-tests.mjs",
95
95
  "test:rust": "node --test --test-timeout=180000 test/rust/*.test.mjs",
96
96
  "test:browser": "playwright test --config test/browser/playwright.config.mjs",
97
97
  "size": "node scripts/bundle-size.mjs",
package/src/client.mjs CHANGED
@@ -2815,7 +2815,9 @@ export class WshClient {
2815
2815
  */
2816
2816
  #attachTransportHandlers(transport) {
2817
2817
  transport.onControl = (msg) => this.#onTransportControl(msg);
2818
- transport.onClose = () => this.#handleTransportClose();
2818
+ // A sealed frame is opened asynchronously, so the close of the socket behind the last frame must wait for it: the frames
2819
+ // that arrived before the close are delivered first (the host's last words, the exit of a command), then the close is handled.
2820
+ transport.onClose = () => this.#afterOpens(() => this.#handleTransportClose());
2819
2821
  transport.onError = (err) => this.#handleTransportError(err);
2820
2822
  // A datagram is not a frame: on a sealed session a relay could inject one, so none is heard (UDP uses the reliable path).
2821
2823
  transport.onDatagram = (bytes) => { if (!this.#sessionSeal) this.#gateway?.handleDatagram(bytes); };
@@ -3046,16 +3048,35 @@ export class WshClient {
3046
3048
  this.#countDrop({ reason: 'plaintext-on-sealed-session', messageType: msg.type });
3047
3049
  return;
3048
3050
  }
3049
- seal.bridge.open(msg).then(
3051
+ this.#trackOpen(seal.bridge.open(msg).then(
3050
3052
  (inner) => { if (this.#sessionSeal === seal) this.#handleControl(inner); },
3051
3053
  (err) => this.#sealedFrameFailed(seal, err),
3052
- );
3054
+ ));
3053
3055
  return;
3054
3056
  }
3055
3057
  if (msg.type === MSG.KEY_EXCHANGE && msg.session_id === seal.sessionId) { this.#handleControl(msg); return; }
3056
3058
  this.#countDrop({ reason: 'plaintext-before-seal', messageType: msg.type });
3057
3059
  }
3058
3060
 
3061
+ /** Sealed frames being opened (each is one pending promise), and what is waiting for them to be delivered. */
3062
+ #opening = 0;
3063
+ #afterOpening = [];
3064
+
3065
+ /** @param {Promise<unknown>} p a frame being opened and delivered; never rejects */
3066
+ #trackOpen(p) {
3067
+ this.#opening++;
3068
+ const done = () => {
3069
+ if (--this.#opening > 0) return;
3070
+ for (const fn of this.#afterOpening.splice(0)) fn();
3071
+ };
3072
+ p.then(done, done);
3073
+ }
3074
+
3075
+ /** Run `fn` now, or once every sealed frame that has already arrived has been opened and delivered. */
3076
+ #afterOpens(fn) {
3077
+ if (this.#opening === 0) fn(); else this.#afterOpening.push(fn);
3078
+ }
3079
+
3059
3080
  #sealedFrameFailed(seal, err) {
3060
3081
  if (this.#sessionSeal !== seal || this.#state === STATE_CLOSED) return;
3061
3082
  this.#sessionSeal = { ...seal, failed: true };
@@ -3231,9 +3252,9 @@ export class WshClient {
3231
3252
  const sealState = this.#sealState;
3232
3253
  if (sealState) {
3233
3254
  if (sealState.bridge?.isFrame(inner)) {
3234
- sealState.bridge.open(inner)
3255
+ this.#trackOpen(sealState.bridge.open(inner)
3235
3256
  .then((opened) => this.#handleControl(opened, msg.from_fingerprint))
3236
- .catch((err) => this.#countDrop({ reason: 'unopenable-bridge-frame', detail: err.message }));
3257
+ .catch((err) => this.#countDrop({ reason: 'unopenable-bridge-frame', detail: err.message })));
3237
3258
  return;
3238
3259
  }
3239
3260
  const setup = !sealState.bridge && inner.type === MSG.KEY_EXCHANGE && inner.session_id === sealState.sessionId;
@@ -77,8 +77,16 @@ export function bindSocket(socket, ready, { remote = {}, dataStreams } = {}) {
77
77
  closeTransport: () => { try { socket.close(1000); } catch { /* already closed */ } },
78
78
  });
79
79
  });
80
- const drop = () => { if (closed) return; closed = true; chain = chain.then(() => conn?.close()).catch(() => {}); };
80
+ // The close joins the chain behind the messages that were received before it: each message is handled asynchronously, and
81
+ // marking the connection closed at once would drop the peer's last messages (its final write, the end of its input).
82
+ let closing = false;
83
+ const drop = () => {
84
+ if (closing) return;
85
+ closing = true;
86
+ chain = chain.then(() => { closed = true; return conn?.close(); }).catch(() => {});
87
+ };
81
88
  listen('message', (data) => {
89
+ if (closing) return;
82
90
  chain = chain.then(async () => {
83
91
  if (closed || !conn) return;
84
92
  const bytes = await toBytes(data);
@@ -62,7 +62,14 @@ class QMuxStreamAdapter {
62
62
  constructor(qs) {
63
63
  this.id = qs.id;
64
64
 
65
+ // `controller.error()` throws away what is queued, so a connection that ends right behind a stream's last bytes would
66
+ // take them with it. The failure is held until the reader has taken what had arrived (`pull` runs when the queue is
67
+ // empty), and delivered then.
68
+ let destroyedWith = null;
65
69
  this.readable = new ReadableStream({
70
+ pull: (controller) => {
71
+ if (destroyedWith) { try { controller.error(destroyedWith); } catch { /* already settled */ } }
72
+ },
66
73
  start: (controller) => {
67
74
  qs.onData = (data) => {
68
75
  try { controller.enqueue(data); } catch { /* controller already closed/errored */ }
@@ -74,7 +81,10 @@ class QMuxStreamAdapter {
74
81
  try { controller.error(new Error(`Stream ${qs.id} was reset by peer (error code ${errorCode})`)); } catch { /* already settled */ }
75
82
  };
76
83
  qs.onDestroy = (err) => {
77
- try { controller.error(err); } catch { /* already settled */ }
84
+ destroyedWith = err;
85
+ if (controller.desiredSize === null || controller.desiredSize > 0) {
86
+ try { controller.error(err); } catch { /* already settled */ }
87
+ }
78
88
  };
79
89
  },
80
90
  cancel: () => {
@@ -321,7 +331,11 @@ export class WebSocketTransport extends WshTransport {
321
331
  if (!this.#closedByUs) {
322
332
  this._emitError(err);
323
333
  }
324
- this._emitClose();
334
+ // What arrived before the close is delivered before it is reported: messages are dispatched one per turn, so the close
335
+ // (a separate event of the socket) could otherwise be handled while earlier ones are still waiting (a host's last words,
336
+ // the exit of a command).
337
+ if (this.#inbox.busy) this.#inbox.idle().then(() => this._emitClose(), () => this._emitClose());
338
+ else this._emitClose();
325
339
  }
326
340
 
327
341
  /**
package/src/transport.mjs CHANGED
@@ -73,10 +73,22 @@ export class SerialQueue {
73
73
  this.#handler = handler;
74
74
  }
75
75
 
76
+ #loop = Promise.resolve();
77
+
76
78
  /** @param {*} item */
77
79
  push(item) {
78
80
  this.#items.push(item);
79
- this.#drain();
81
+ if (!this.#draining) this.#loop = this.#drain();
82
+ }
83
+
84
+ /** Whether anything pushed is still waiting for, or being given to, the handler. */
85
+ get busy() {
86
+ return this.#draining || this.#items.length > 0;
87
+ }
88
+
89
+ /** Resolves once everything pushed so far has been dispatched (immediately when nothing is). */
90
+ idle() {
91
+ return this.#loop;
80
92
  }
81
93
 
82
94
  /** Discard any queued-but-not-yet-dispatched items (e.g. on reconnect). */
@@ -694,19 +706,23 @@ export class WebTransportTransport extends WshTransport {
694
706
  * @param {WebTransport} wt
695
707
  */
696
708
  #monitorClosed(wt) {
709
+ // What the control stream had delivered before the session ended is dispatched (one message per turn) before the close
710
+ // is reported: the end of the session is a separate event, and reporting it at once would overtake the last messages.
711
+ // The reader ends with the session, so this wait is bounded by it.
712
+ const afterControl = () => this.#controlReaderDone?.catch(() => {}) ?? Promise.resolve();
697
713
  wt.closed
698
- .then(() => {
714
+ .then(() => afterControl().then(() => {
699
715
  if (this.state !== 'closed') {
700
716
  this._setState('closed');
701
717
  this._emitClose();
702
718
  }
703
- })
704
- .catch((err) => {
719
+ }))
720
+ .catch((err) => afterControl().then(() => {
705
721
  if (this.state !== 'closed') {
706
722
  this._setState('closed');
707
723
  this._emitError(err);
708
724
  this._emitClose();
709
725
  }
710
- });
726
+ }));
711
727
  }
712
728
  }