@johnhenry/wsh 0.16.1 → 0.17.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 +349 -28
- package/package.json +16 -3
- package/src/auth.mjs +63 -1
- package/src/client.mjs +379 -35
- package/src/control-listener.mjs +130 -0
- package/src/e2e-frame.mjs +32 -1
- package/src/file-transfer.mjs +47 -192
- package/src/index.d.ts +261 -8
- package/src/index.mjs +10 -2
- package/src/known-hosts.mjs +139 -0
- package/src/mcp-bridge.mjs +70 -51
- package/src/messages.gen.mjs +30 -16
- package/src/mlkem.mjs +27 -0
- package/src/session.mjs +73 -21
- package/src/transport-ws.mjs +16 -2
- package/src/transport.mjs +201 -7
package/src/transport.mjs
CHANGED
|
@@ -111,7 +111,7 @@ const STATE_CLOSED = 'closed';
|
|
|
111
111
|
* Abstract transport for the wsh protocol.
|
|
112
112
|
*
|
|
113
113
|
* Subclasses must implement:
|
|
114
|
-
* - _doConnect(url)
|
|
114
|
+
* - _doConnect(url, options)
|
|
115
115
|
* - _doClose()
|
|
116
116
|
* - _doSendControl(msg)
|
|
117
117
|
* - _doOpenStream()
|
|
@@ -144,17 +144,40 @@ export class WshTransport {
|
|
|
144
144
|
/**
|
|
145
145
|
* Connect to a wsh server.
|
|
146
146
|
* @param {string} url
|
|
147
|
+
* @param {object} [options] - Transport-specific connect options.
|
|
148
|
+
* Ignored by transports that have none; `WebTransportTransport`
|
|
149
|
+
* forwards them to the `WebTransport` constructor (see its docs for
|
|
150
|
+
* `serverCertificateHashes`).
|
|
147
151
|
*/
|
|
148
|
-
async connect(url) {
|
|
152
|
+
async connect(url, options) {
|
|
149
153
|
if (this.#state === STATE_CONNECTED || this.#state === STATE_CONNECTING) {
|
|
150
154
|
throw new Error(`Transport already ${this.#state}`);
|
|
151
155
|
}
|
|
152
156
|
this.#state = STATE_CONNECTING;
|
|
153
157
|
try {
|
|
154
|
-
await this._doConnect(url);
|
|
158
|
+
await this._doConnect(url, options);
|
|
155
159
|
this.#state = STATE_CONNECTED;
|
|
156
160
|
} catch (err) {
|
|
161
|
+
// A connect can fail *after* acquiring a real resource -- an open
|
|
162
|
+
// WebSocket whose QMux handshake then threw, a WebTransport whose
|
|
163
|
+
// session came up but whose control stream did not. Tear that down
|
|
164
|
+
// here, because a later `close()` cannot: it treats the `closed`
|
|
165
|
+
// state this line sets as "already torn down" and returns without
|
|
166
|
+
// calling `_doClose()`, so the socket would stay open for the life
|
|
167
|
+
// of the page. Every rung of WshClient's transport ladder that
|
|
168
|
+
// fails hits this path.
|
|
157
169
|
this.#state = STATE_CLOSED;
|
|
170
|
+
try {
|
|
171
|
+
await this._doClose();
|
|
172
|
+
} catch {
|
|
173
|
+
// Best effort: the connect error is the one worth reporting, and
|
|
174
|
+
// tearing down a half-built transport can fail for its own
|
|
175
|
+
// uninteresting reasons.
|
|
176
|
+
}
|
|
177
|
+
// Deliberately no `_emitClose()`: a transport that never reached
|
|
178
|
+
// `connected` never announced itself, callers have not attached
|
|
179
|
+
// their handlers yet, and a close event for a connection that never
|
|
180
|
+
// opened is a lie.
|
|
158
181
|
throw err;
|
|
159
182
|
}
|
|
160
183
|
}
|
|
@@ -235,7 +258,7 @@ export class WshTransport {
|
|
|
235
258
|
// ── Abstract methods (must be overridden) ────────────────────────────
|
|
236
259
|
|
|
237
260
|
/** @protected */
|
|
238
|
-
async _doConnect(_url) {
|
|
261
|
+
async _doConnect(_url, _options) {
|
|
239
262
|
throw new Error('_doConnect not implemented');
|
|
240
263
|
}
|
|
241
264
|
|
|
@@ -257,6 +280,140 @@ export class WshTransport {
|
|
|
257
280
|
|
|
258
281
|
// ── WebTransport implementation ──────────────────────────────────────
|
|
259
282
|
|
|
283
|
+
// ── WebTransport options ─────────────────────────────────────────────
|
|
284
|
+
|
|
285
|
+
/** Digest length in bytes, keyed by the algorithm name WebTransport accepts. */
|
|
286
|
+
const CERT_HASH_LENGTHS = Object.freeze({ 'sha-256': 32 });
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Decode one `serverCertificateHashes` value into the `BufferSource` the
|
|
290
|
+
* WebTransport constructor requires.
|
|
291
|
+
*
|
|
292
|
+
* Accepts, in addition to a `BufferSource` passed straight through:
|
|
293
|
+
*
|
|
294
|
+
* - **Hex**, with or without separators — this is what
|
|
295
|
+
* `openssl x509 -fingerprint -sha256` prints
|
|
296
|
+
* (`SHA256 Fingerprint=AB:CD:...`), and pasting that string in is the
|
|
297
|
+
* single most likely thing a caller will try.
|
|
298
|
+
* - **Base64** or **base64url**, which is how the same digest usually
|
|
299
|
+
* arrives over a QR code or a pairing payload.
|
|
300
|
+
*
|
|
301
|
+
* Throws on anything else, and on a digest whose decoded length is wrong
|
|
302
|
+
* for the algorithm. Failing here is the whole point: a wrong hash
|
|
303
|
+
* otherwise surfaces as an opaque `WebTransportError` from `wt.ready`
|
|
304
|
+
* with no indication that the *input* was malformed rather than the
|
|
305
|
+
* certificate mismatched.
|
|
306
|
+
*
|
|
307
|
+
* @param {*} value
|
|
308
|
+
* @param {string} algorithm - Lower-cased algorithm name (e.g. 'sha-256').
|
|
309
|
+
* @returns {Uint8Array}
|
|
310
|
+
*/
|
|
311
|
+
export function parseCertificateHash(value, algorithm = 'sha-256') {
|
|
312
|
+
const expected = CERT_HASH_LENGTHS[algorithm];
|
|
313
|
+
let bytes;
|
|
314
|
+
|
|
315
|
+
if (value instanceof Uint8Array) {
|
|
316
|
+
bytes = value;
|
|
317
|
+
} else if (ArrayBuffer.isView(value)) {
|
|
318
|
+
bytes = new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
|
|
319
|
+
} else if (value instanceof ArrayBuffer) {
|
|
320
|
+
bytes = new Uint8Array(value);
|
|
321
|
+
} else if (typeof value === 'string') {
|
|
322
|
+
bytes = decodeHashString(value);
|
|
323
|
+
} else {
|
|
324
|
+
throw new TypeError(
|
|
325
|
+
'serverCertificateHashes value must be a BufferSource or a hex/base64 string, ' +
|
|
326
|
+
`got ${value === null ? 'null' : typeof value}`
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
if (expected !== undefined && bytes.length !== expected) {
|
|
331
|
+
throw new RangeError(
|
|
332
|
+
`serverCertificateHashes value for "${algorithm}" must be ${expected} bytes, got ${bytes.length}`
|
|
333
|
+
);
|
|
334
|
+
}
|
|
335
|
+
return bytes;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* @param {string} str
|
|
340
|
+
* @returns {Uint8Array}
|
|
341
|
+
*/
|
|
342
|
+
function decodeHashString(str) {
|
|
343
|
+
// Strip the separators OpenSSL and friends emit, plus any surrounding
|
|
344
|
+
// whitespace: "SHA256 Fingerprint=" style output is colon-separated,
|
|
345
|
+
// and hand-copied values often carry spaces or dashes.
|
|
346
|
+
const compact = str.trim().replace(/^[^=]*fingerprint[^=]*=\s*/i, '');
|
|
347
|
+
const hexish = compact.replace(/[\s:-]/g, '');
|
|
348
|
+
|
|
349
|
+
if (/^[0-9a-fA-F]+$/.test(hexish) && hexish.length % 2 === 0) {
|
|
350
|
+
const out = new Uint8Array(hexish.length / 2);
|
|
351
|
+
for (let i = 0; i < out.length; i++) {
|
|
352
|
+
out[i] = Number.parseInt(hexish.slice(i * 2, i * 2 + 2), 16);
|
|
353
|
+
}
|
|
354
|
+
return out;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// Not hex — try base64 / base64url.
|
|
358
|
+
const b64 = compact.replace(/-/g, '+').replace(/_/g, '/');
|
|
359
|
+
if (!/^[A-Za-z0-9+/]+={0,2}$/.test(b64)) {
|
|
360
|
+
throw new TypeError(
|
|
361
|
+
`serverCertificateHashes value is neither hex nor base64: ${JSON.stringify(str)}`
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
const bin = atob(b64);
|
|
365
|
+
const out = new Uint8Array(bin.length);
|
|
366
|
+
for (let i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i);
|
|
367
|
+
return out;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* Normalise a caller-supplied options bag into a `WebTransportOptions`
|
|
372
|
+
* dictionary, or `undefined` when there is nothing to pass.
|
|
373
|
+
*
|
|
374
|
+
* Only `serverCertificateHashes` is transformed; every other key is
|
|
375
|
+
* forwarded verbatim, so options the platform gains later
|
|
376
|
+
* (`congestionControl`, `allowPooling`, `requireUnreliable`, the
|
|
377
|
+
* `anticipatedConcurrentIncoming*Streams` hints, …) work without a
|
|
378
|
+
* change here.
|
|
379
|
+
*
|
|
380
|
+
* @param {object} [options]
|
|
381
|
+
* @returns {object|undefined}
|
|
382
|
+
*/
|
|
383
|
+
export function normalizeWebTransportOptions(options) {
|
|
384
|
+
if (!options) return undefined;
|
|
385
|
+
const keys = Object.keys(options).filter((k) => options[k] !== undefined);
|
|
386
|
+
if (keys.length === 0) return undefined;
|
|
387
|
+
|
|
388
|
+
const out = {};
|
|
389
|
+
for (const key of keys) out[key] = options[key];
|
|
390
|
+
|
|
391
|
+
if (out.serverCertificateHashes !== undefined) {
|
|
392
|
+
const list = out.serverCertificateHashes;
|
|
393
|
+
if (!Array.isArray(list)) {
|
|
394
|
+
throw new TypeError('serverCertificateHashes must be an array');
|
|
395
|
+
}
|
|
396
|
+
if (list.length === 0) {
|
|
397
|
+
throw new TypeError(
|
|
398
|
+
'serverCertificateHashes must not be empty -- omit the option entirely to use the ' +
|
|
399
|
+
'normal certificate-authority path'
|
|
400
|
+
);
|
|
401
|
+
}
|
|
402
|
+
out.serverCertificateHashes = list.map((entry) => {
|
|
403
|
+
if (entry === null || typeof entry !== 'object' || Array.isArray(entry) ||
|
|
404
|
+
ArrayBuffer.isView(entry) || entry instanceof ArrayBuffer) {
|
|
405
|
+
// A bare digest is a natural thing to write; accept it rather
|
|
406
|
+
// than making the caller wrap every value in { algorithm, value }.
|
|
407
|
+
return { algorithm: 'sha-256', value: parseCertificateHash(entry, 'sha-256') };
|
|
408
|
+
}
|
|
409
|
+
const algorithm = String(entry.algorithm ?? 'sha-256').toLowerCase();
|
|
410
|
+
return { algorithm, value: parseCertificateHash(entry.value, algorithm) };
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
return out;
|
|
415
|
+
}
|
|
416
|
+
|
|
260
417
|
/**
|
|
261
418
|
* wsh transport over the WebTransport API.
|
|
262
419
|
*
|
|
@@ -287,16 +444,53 @@ export class WebTransportTransport extends WshTransport {
|
|
|
287
444
|
/** @type {Promise<void>} Resolves when the incoming-stream acceptor finishes. */
|
|
288
445
|
#incomingAcceptorDone = null;
|
|
289
446
|
|
|
447
|
+
/** @type {object|undefined} Default WebTransport options for every connect. */
|
|
448
|
+
#options;
|
|
449
|
+
|
|
450
|
+
/**
|
|
451
|
+
* @param {object} [options] - Forwarded to the `WebTransport`
|
|
452
|
+
* constructor. Most usefully `serverCertificateHashes`, which lets
|
|
453
|
+
* page JavaScript pin a specific short-lived self-signed certificate
|
|
454
|
+
* by digest instead of requiring one a certificate authority signed:
|
|
455
|
+
*
|
|
456
|
+
* ```js
|
|
457
|
+
* new WebTransportTransport({
|
|
458
|
+
* serverCertificateHashes: [
|
|
459
|
+
* // hex (with or without colons), base64, or raw bytes
|
|
460
|
+
* 'a1:b2:c3:...',
|
|
461
|
+
* ],
|
|
462
|
+
* });
|
|
463
|
+
* ```
|
|
464
|
+
*
|
|
465
|
+
* The web platform imposes the rules, not wsh: the URL must be
|
|
466
|
+
* `https:`, the connection is HTTP/3 only (there is no fallback to
|
|
467
|
+
* HTTP/2 for a pinned certificate), the certificate must use an
|
|
468
|
+
* ECDSA P-256 key and be valid for no more than 14 days, and
|
|
469
|
+
* connection pooling is disabled. Options passed to `connect()`
|
|
470
|
+
* override these per call.
|
|
471
|
+
*/
|
|
472
|
+
constructor(options) {
|
|
473
|
+
super();
|
|
474
|
+
this.#options = options;
|
|
475
|
+
}
|
|
476
|
+
|
|
290
477
|
// ── Lifecycle ──────────────────────────────────────────────────────
|
|
291
478
|
|
|
292
479
|
/** @override */
|
|
293
|
-
async _doConnect(url) {
|
|
480
|
+
async _doConnect(url, options) {
|
|
294
481
|
this.#abort = new AbortController();
|
|
295
482
|
this.#decoder.reset();
|
|
296
483
|
this.#nextStreamId = 1;
|
|
297
484
|
|
|
298
|
-
// Create the WebTransport session.
|
|
299
|
-
|
|
485
|
+
// Create the WebTransport session. Pass the options dictionary only
|
|
486
|
+
// when there is something in it: `new WebTransport(url)` and
|
|
487
|
+
// `new WebTransport(url, {})` are equivalent per spec, but an empty
|
|
488
|
+
// second argument is noise in a stack trace and in any engine that
|
|
489
|
+
// predates the dictionary.
|
|
490
|
+
const wtOptions = normalizeWebTransportOptions(
|
|
491
|
+
(this.#options || options) && { ...this.#options, ...options }
|
|
492
|
+
);
|
|
493
|
+
const wt = wtOptions ? new WebTransport(url, wtOptions) : new WebTransport(url);
|
|
300
494
|
this.#wt = wt;
|
|
301
495
|
|
|
302
496
|
// Wait for the connection to be ready.
|