@johnhenry/wsh 0.36.0 → 0.38.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.
@@ -117,12 +117,43 @@ export async function startWebTransport({ options, host, attach, log }) {
117
117
 
118
118
  let controlWriter = null;
119
119
  const decoder = new FrameDecoder();
120
+ // Datagrams (#124): `createWritable()` is the standard sender (`writable` is the deprecated one); a sender that is
121
+ // not draining drops rather than queues, which is what a datagram is.
122
+ let dgWriter = null;
123
+ try {
124
+ const dg = session.datagrams;
125
+ if (dg && typeof dg.maxDatagramSize === 'number') dgWriter = (typeof dg.createWritable === 'function' ? dg.createWritable() : dg.writable).getWriter();
126
+ } catch { dgWriter = null; }
127
+ const datagrams = dgWriter ? {
128
+ send(bytes) {
129
+ if (closed || (dgWriter.desiredSize !== null && dgWriter.desiredSize <= 0)) return false;
130
+ dgWriter.write(bytes).catch(() => {});
131
+ return true;
132
+ },
133
+ maxSize: () => session.datagrams.maxDatagramSize,
134
+ } : null;
120
135
  conn = attach({
136
+ datagrams,
121
137
  sendMessage: (msg) => (controlWriter ? controlWriter.write(frameEncode(msg)) : undefined),
122
138
  remote: { address: session.peerAddress, headers: session.header ?? {} },
123
139
  closeTransport: () => { try { session.close(); } catch { /* already closed */ } },
140
+ // A client that pinned this self-signed certificate can bind its login signature to it (#119).
141
+ channelBinding: current?.hash ? { 'cert-hash': [current.hash] } : null,
124
142
  });
125
143
 
144
+ if (datagrams) {
145
+ (async () => {
146
+ const reader = session.datagrams.readable.getReader();
147
+ try {
148
+ for (;;) {
149
+ const { done, value } = await reader.read();
150
+ if (done) break;
151
+ conn.receiveDatagram(value);
152
+ }
153
+ } catch { /* session went away */ }
154
+ })();
155
+ }
156
+
126
157
  const incoming = session.incomingBidirectionalStreams.getReader();
127
158
  let first = true;
128
159
  try {
@@ -324,6 +324,22 @@ export class WebSocketTransport extends WshTransport {
324
324
  this._emitClose();
325
325
  }
326
326
 
327
+ /**
328
+ * The TLS exporter, when the WebSocket implementation exposes its TLS socket (the `ws` package does, as
329
+ * `_socket`, over `wss://`); `null` for a browser, Node's global `WebSocket`, or a plain `ws://`. See `WshTransport`.
330
+ * @param {string} label
331
+ * @param {number} length
332
+ * @returns {Uint8Array | null}
333
+ */
334
+ tlsExporter(label, length) {
335
+ try {
336
+ const sock = this.#ws?._socket;
337
+ if (!sock || typeof sock.exportKeyingMaterial !== 'function' || sock.encrypted !== true) return null;
338
+ const out = sock.exportKeyingMaterial(length, label);
339
+ return out && out.length === length ? new Uint8Array(out.buffer, out.byteOffset, out.byteLength).slice() : null;
340
+ } catch { return null; }
341
+ }
342
+
327
343
  // ── Outbound helper ──────────────────────────────────────────────
328
344
 
329
345
  /**
package/src/transport.mjs CHANGED
@@ -136,6 +136,35 @@ 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
+
157
+ /**
158
+ * TLS keying material for channel binding (#119): `length` bytes exported with `label` (RFC 5705 / 8446 7.5), or
159
+ * `null` where the platform does not expose it (every browser, and Node's own global `WebSocket`).
160
+ * @param {string} label
161
+ * @param {number} length
162
+ * @returns {Uint8Array | null}
163
+ */
164
+ tlsExporter(label, length) { // eslint-disable-line no-unused-vars
165
+ return null;
166
+ }
167
+
139
168
  /** @protected Update internal state. */
140
169
  _setState(s) {
141
170
  this.#state = s;
@@ -505,6 +534,16 @@ export class WebTransportTransport extends WshTransport {
505
534
  const controlStream = await wt.createBidirectionalStream();
506
535
  this.#controlWriter = controlStream.writable.getWriter();
507
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
+
508
547
  // Start reading control messages in the background.
509
548
  this.#controlReaderDone = this.#readControlStream(controlStream.readable);
510
549
 
@@ -515,8 +554,40 @@ export class WebTransportTransport extends WshTransport {
515
554
  this.#monitorClosed(wt);
516
555
  }
517
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
+
518
588
  /** @override */
519
589
  async _doClose() {
590
+ this.#datagramWriter = null;
520
591
  this.#abort.abort();
521
592
  try {
522
593
  this.#controlWriter?.releaseLock?.();