@johnhenry/wsh 0.45.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 +337 -35
- package/bin/wsh-mcp.mjs +3 -1
- package/package.json +14 -2
- package/src/cbor.mjs +40 -7
- package/src/client.mjs +104 -24
- package/src/errors.mjs +90 -0
- package/src/host-backends.mjs +23 -0
- package/src/host.d.ts +1 -1
- package/src/index.d.ts +66 -0
- package/src/index.mjs +5 -0
- package/src/key-options.mjs +3 -1
- package/src/mcp-stdio.d.ts +2 -0
- package/src/mcp-stdio.mjs +4 -2
- package/src/memory-host.d.ts +12 -1
- package/src/memory-host.mjs +8 -21
- package/src/objects-core.mjs +805 -0
- package/src/objects.d.ts +152 -0
- package/src/objects.mjs +97 -0
- package/src/page-host.d.ts +213 -0
- package/src/page-host.mjs +381 -0
- package/src/pipe-relay-node.mjs +43 -0
- package/src/pipe-relay.d.ts +19 -4
- package/src/pipe-relay.mjs +108 -39
- package/src/reverse-host.d.ts +4 -0
- package/src/reverse-host.mjs +14 -4
- package/src/rpc-virtual.mjs +127 -0
- package/src/rpc.mjs +34 -2
- package/src/server/connection.mjs +70 -10
- package/src/server/index.d.ts +41 -2
- package/src/server/index.mjs +5 -2
- package/src/server/password.mjs +142 -0
- package/src/server/reverse-host.mjs +7 -2
- package/src/server/socket.mjs +9 -1
- package/src/server/username.mjs +57 -0
- package/src/session.mjs +2 -1
- package/src/testing.d.ts +50 -0
- package/src/testing.mjs +27 -0
- package/src/transport-ws.mjs +16 -2
- package/src/transport.mjs +21 -5
- package/src/webmcp.mjs +694 -0
package/src/transport-ws.mjs
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
}
|