@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.
@@ -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
  }