@johnhenry/wsh 0.37.0 → 0.38.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/src/transport.mjs CHANGED
@@ -136,6 +136,24 @@ export class WshTransport {
136
136
  return this.#state;
137
137
  }
138
138
 
139
+ /** @type {function(Uint8Array): void} Callback for an unreliable datagram (WebTransport only). */
140
+ onDatagram = null;
141
+
142
+ /** True where this transport can carry unreliable datagrams (WebTransport); false for WebSocket and the rest. */
143
+ get supportsDatagrams() { return false; }
144
+
145
+ /** The largest datagram the connection can carry right now, in bytes (0 = none). Read it again before sending: it can change. */
146
+ get maxDatagramSize() { return 0; }
147
+
148
+ /**
149
+ * Send one datagram. Never waits and never queues without bound.
150
+ * @param {Uint8Array} bytes
151
+ * @returns {boolean} false when it was dropped (no datagram support, closed, or the sender is not draining)
152
+ */
153
+ sendDatagram(bytes) { // eslint-disable-line no-unused-vars
154
+ return false;
155
+ }
156
+
139
157
  /**
140
158
  * TLS keying material for channel binding (#119): `length` bytes exported with `label` (RFC 5705 / 8446 7.5), or
141
159
  * `null` where the platform does not expose it (every browser, and Node's own global `WebSocket`).
@@ -516,6 +534,16 @@ export class WebTransportTransport extends WshTransport {
516
534
  const controlStream = await wt.createBidirectionalStream();
517
535
  this.#controlWriter = controlStream.writable.getWriter();
518
536
 
537
+ // Datagrams: `createWritable()` is the standard sender, `writable` the deprecated one older engines have.
538
+ this.#datagramWriter = null;
539
+ try {
540
+ const dg = wt.datagrams;
541
+ if (dg && dg.readable) {
542
+ this.#datagramWriter = (typeof dg.createWritable === 'function' ? dg.createWritable() : dg.writable).getWriter();
543
+ this.#readDatagrams(dg.readable);
544
+ }
545
+ } catch { this.#datagramWriter = null; }
546
+
519
547
  // Start reading control messages in the background.
520
548
  this.#controlReaderDone = this.#readControlStream(controlStream.readable);
521
549
 
@@ -526,8 +554,40 @@ export class WebTransportTransport extends WshTransport {
526
554
  this.#monitorClosed(wt);
527
555
  }
528
556
 
557
+ /** @type {WritableStreamDefaultWriter|null} */
558
+ #datagramWriter = null;
559
+
560
+ /** @override */
561
+ get supportsDatagrams() { return this.#datagramWriter !== null; }
562
+
563
+ /** @override */
564
+ get maxDatagramSize() {
565
+ try { return this.#datagramWriter ? Number(this.#wt?.datagrams?.maxDatagramSize) || 0 : 0; } catch { return 0; }
566
+ }
567
+
568
+ /** @override */
569
+ sendDatagram(bytes) {
570
+ const w = this.#datagramWriter;
571
+ if (!w || this.state !== 'connected') return false;
572
+ if (w.desiredSize !== null && w.desiredSize <= 0) return false; // not draining: drop, do not queue
573
+ w.write(bytes).catch(() => {});
574
+ return true;
575
+ }
576
+
577
+ async #readDatagrams(readable) {
578
+ const reader = readable.getReader();
579
+ try {
580
+ for (;;) {
581
+ const { done, value } = await reader.read();
582
+ if (done) break;
583
+ try { this.onDatagram?.(value); } catch (err) { console.error('[wsh:transport] onDatagram handler error:', err); }
584
+ }
585
+ } catch { /* session closed */ }
586
+ }
587
+
529
588
  /** @override */
530
589
  async _doClose() {
590
+ this.#datagramWriter = null;
531
591
  this.#abort.abort();
532
592
  try {
533
593
  this.#controlWriter?.releaseLock?.();