@moq/net 0.2.0 → 0.2.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/stream.js CHANGED
@@ -1,4 +1,5 @@
1
1
  /* @ts-self-types="./stream.d.ts" */
2
+ import { fromTransport } from "./error.js";
2
3
  import { Version } from "./ietf/version.js";
3
4
  import * as Varint from "./varint.js";
4
5
  const MAX_U31 = 2 ** 31 - 1;
@@ -46,6 +47,7 @@ export class Reader {
46
47
  #buffer;
47
48
  #stream; // if undefined, the buffer is consumed then EOF
48
49
  #reader;
50
+ #closed;
49
51
  version;
50
52
  constructor(stream, buffer, version) {
51
53
  this.#buffer = buffer ?? new Uint8Array();
@@ -58,7 +60,11 @@ export class Reader {
58
60
  if (!this.#reader) {
59
61
  return false;
60
62
  }
61
- const result = await this.#reader.read();
63
+ // Every read of this stream funnels through here, so decoding the peer's reset code
64
+ // once is enough to keep the raw transport error out of every caller (and every app).
65
+ const result = await this.#reader.read().catch((err) => {
66
+ throw fromTransport(err);
67
+ });
62
68
  if (result.done) {
63
69
  return false;
64
70
  }
@@ -210,14 +216,20 @@ export class Reader {
210
216
  stop(reason) {
211
217
  this.#reader?.cancel(reason).catch(() => void 0);
212
218
  }
219
+ // Decoded like #fill: a caller racing this against a read must not get a different error
220
+ // shape depending on which one won. Derived once, so racing it per frame doesn't allocate.
213
221
  get closed() {
214
- return this.#reader?.closed ?? Promise.resolve();
222
+ this.#closed ??= (this.#reader?.closed ?? Promise.resolve()).catch((err) => {
223
+ throw fromTransport(err);
224
+ });
225
+ return this.#closed;
215
226
  }
216
227
  }
217
228
  // Writer wraps a stream and writes chunks of data
218
229
  export class Writer {
219
230
  #writer;
220
231
  #stream;
232
+ #closed;
221
233
  // Scratch buffer for writing varints.
222
234
  // Fixed at 9 bytes (leading-ones max).
223
235
  #scratch;
@@ -269,7 +281,11 @@ export class Writer {
269
281
  }
270
282
  }
271
283
  async write(v) {
272
- await this.#writer.write(v);
284
+ // Mirrors Reader.#fill: every write funnels through here, so a STOP_SENDING from the
285
+ // peer surfaces as a typed code rather than the transport's own error shape.
286
+ await this.#writer.write(v).catch((err) => {
287
+ throw fromTransport(err);
288
+ });
273
289
  }
274
290
  async string(str) {
275
291
  const data = new TextEncoder().encode(str);
@@ -279,8 +295,13 @@ export class Writer {
279
295
  close() {
280
296
  this.#writer.close().catch(() => void 0);
281
297
  }
298
+ // Mirrors Reader.closed: a STOP_SENDING reaches a caller racing this with the same
299
+ // typed code it would get from a write.
282
300
  get closed() {
283
- return this.#writer.closed;
301
+ this.#closed ??= this.#writer.closed.catch((err) => {
302
+ throw fromTransport(err);
303
+ });
304
+ return this.#closed;
284
305
  }
285
306
  reset(reason) {
286
307
  this.#writer.abort(reason).catch(() => void 0);
package/stream.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"stream.js","sourceRoot":"","sources":["../src/stream.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,OAAO,KAAK,MAAM,MAAM,aAAa,CAAC;AAEtC,MAAM,OAAO,GAAG,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;AAC5B,MAAM,aAAa,GAAG,IAAI,GAAG,IAAI,GAAG,EAAE,CAAC,CAAC,8CAA8C;AAEtF,SAAS,aAAa,CAAC,OAAqB;IAC3C,OAAO,CACN,OAAO,KAAK,SAAS;QACrB,OAAO,KAAK,OAAO,CAAC,QAAQ;QAC5B,OAAO,KAAK,OAAO,CAAC,QAAQ;QAC5B,OAAO,KAAK,OAAO,CAAC,QAAQ,CAC5B,CAAC;AACH,CAAC;AAED,MAAM,OAAO,MAAM;IAClB,MAAM,CAAS;IACf,MAAM,CAAS;IAEf,YAAY,KAMX;QACA,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC,MAAM,IAAI,IAAI,MAAM,CAAC,KAAK,CAAC,QAAQ,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC;QACxE,IAAI,CAAC,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AAO;IACnB,OAAO,CAA0D;IACjE,QAAQ,CAAe;IAEvB,YAAY,IAAkB,EAAE,OAAqB;QACpD,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,6BAA6B,CAAC,SAAS,EAE1D,CAAC;QACF,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;IACzB,CAAC;IAED,KAAK,CAAC,IAAI;QACT,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC;QACvC,IAAI,IAAI,CAAC,IAAI;YAAE,OAAO;QACtB,OAAO,IAAI,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,SAAS,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;IACzD,CAAC;IAED,KAAK;QACJ,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC;IACvB,CAAC;CACD","sourcesContent":["import type { IetfVersion } from \"./ietf/version.ts\";\nimport { Version } from \"./ietf/version.ts\";\nimport * as Varint from \"./varint.ts\";\n\nconst MAX_U31 = 2 ** 31 - 1;\nconst MAX_READ_SIZE = 1024 * 1024 * 64; // don't allocate more than 64MB for a message\n\nfunction isLeadingOnes(version?: IetfVersion): boolean {\n\treturn (\n\t\tversion !== undefined &&\n\t\tversion !== Version.DRAFT_14 &&\n\t\tversion !== Version.DRAFT_15 &&\n\t\tversion !== Version.DRAFT_16\n\t);\n}\n\nexport class Stream {\n\treader: Reader;\n\twriter: Writer;\n\n\tconstructor(props: {\n\t\twritable: WritableStream<Uint8Array>;\n\t\treadable: ReadableStream<Uint8Array>;\n\t\twriter?: Writer;\n\t\treader?: Reader;\n\t\tversion?: IetfVersion;\n\t}) {\n\t\tthis.writer = props.writer ?? new Writer(props.writable, props.version);\n\t\tthis.reader = props.reader ?? new Reader(props.readable, undefined, props.version);\n\t}\n\n\tstatic async accept(quic: WebTransport, version?: IetfVersion): Promise<Stream | undefined> {\n\t\tfor (;;) {\n\t\t\tconst reader =\n\t\t\t\tquic.incomingBidirectionalStreams.getReader() as ReadableStreamDefaultReader<WebTransportBidirectionalStream>;\n\t\t\tconst next = await reader.read();\n\t\t\treader.releaseLock();\n\n\t\t\tif (next.done) return;\n\t\t\tconst { readable, writable } = next.value;\n\t\t\treturn new Stream({ readable, writable, version });\n\t\t}\n\t}\n\n\tstatic async open(quic: WebTransport, version?: IetfVersion, priority?: number): Promise<Stream> {\n\t\tconst { readable, writable } = await quic.createBidirectionalStream({ sendOrder: priority });\n\t\treturn new Stream({ readable, writable, version });\n\t}\n\n\tclose() {\n\t\tthis.writer.close();\n\t\tthis.reader.stop(new Error(\"cancel\"));\n\t}\n\n\tabort(reason: Error) {\n\t\tthis.writer.reset(reason);\n\t\tthis.reader.stop(reason);\n\t}\n}\n\n// Reader wraps a stream and provides convience methods for reading pieces from a stream\n// Unfortunately we can't use a BYOB reader because it's not supported with WebTransport+WebWorkers yet.\nexport class Reader {\n\t#buffer: Uint8Array;\n\t#stream?: ReadableStream<Uint8Array>; // if undefined, the buffer is consumed then EOF\n\t#reader?: ReadableStreamDefaultReader<Uint8Array>;\n\tversion?: IetfVersion;\n\n\t// Either stream or buffer MUST be provided.\n\tconstructor(stream: ReadableStream<Uint8Array>, buffer?: Uint8Array, version?: IetfVersion);\n\tconstructor(stream: undefined, buffer: Uint8Array, version?: IetfVersion);\n\tconstructor(stream?: ReadableStream<Uint8Array>, buffer?: Uint8Array, version?: IetfVersion) {\n\t\tthis.#buffer = buffer ?? new Uint8Array();\n\t\tthis.#stream = stream;\n\t\tthis.#reader = this.#stream?.getReader();\n\t\tthis.version = version;\n\t}\n\n\t// Adds more data to the buffer, returning true if more data was added.\n\tasync #fill(): Promise<boolean> {\n\t\tif (!this.#reader) {\n\t\t\treturn false;\n\t\t}\n\n\t\tconst result = await this.#reader.read();\n\t\tif (result.done) {\n\t\t\treturn false;\n\t\t}\n\n\t\tif (result.value.byteLength === 0) {\n\t\t\tthrow new Error(\"unexpected empty chunk\");\n\t\t}\n\n\t\tconst buffer = new Uint8Array(result.value);\n\n\t\tif (this.#buffer.byteLength === 0) {\n\t\t\tthis.#buffer = buffer;\n\t\t} else {\n\t\t\tconst temp = new Uint8Array(this.#buffer.byteLength + buffer.byteLength);\n\t\t\ttemp.set(this.#buffer);\n\t\t\ttemp.set(buffer, this.#buffer.byteLength);\n\t\t\tthis.#buffer = temp;\n\t\t}\n\n\t\treturn true;\n\t}\n\n\t// Add more data to the buffer until it's at least size bytes.\n\tasync #fillTo(size: number) {\n\t\tif (size > MAX_READ_SIZE) {\n\t\t\tthrow new Error(`read size ${size} exceeds max size ${MAX_READ_SIZE}`);\n\t\t}\n\n\t\twhile (this.#buffer.byteLength < size) {\n\t\t\tif (!(await this.#fill())) {\n\t\t\t\tthrow new Error(\"unexpected end of stream\");\n\t\t\t}\n\t\t}\n\t}\n\n\t// Consumes the first size bytes of the buffer.\n\t#slice(size: number): Uint8Array {\n\t\tconst result = new Uint8Array(this.#buffer.buffer, this.#buffer.byteOffset, size);\n\t\tthis.#buffer = new Uint8Array(\n\t\t\tthis.#buffer.buffer,\n\t\t\tthis.#buffer.byteOffset + size,\n\t\t\tthis.#buffer.byteLength - size,\n\t\t);\n\n\t\treturn result;\n\t}\n\n\tasync read(size: number): Promise<Uint8Array> {\n\t\tif (size === 0) return new Uint8Array();\n\n\t\tawait this.#fillTo(size);\n\t\treturn this.#slice(size);\n\t}\n\n\tasync readAll(): Promise<Uint8Array> {\n\t\twhile (await this.#fill()) {\n\t\t\t// keep going\n\t\t}\n\t\treturn this.#slice(this.#buffer.byteLength);\n\t}\n\n\tasync string(): Promise<string> {\n\t\tconst length = await this.u53();\n\t\tconst buffer = await this.read(length);\n\t\treturn new TextDecoder().decode(buffer);\n\t}\n\n\tasync bool(): Promise<boolean> {\n\t\tconst v = await this.u8();\n\t\tif (v === 0) return false;\n\t\tif (v === 1) return true;\n\t\tthrow new Error(\"invalid bool value\");\n\t}\n\n\tasync u8(): Promise<number> {\n\t\tawait this.#fillTo(1);\n\t\treturn this.#slice(1)[0];\n\t}\n\n\tasync u16(): Promise<number> {\n\t\tawait this.#fillTo(2);\n\t\tconst view = new DataView(this.#buffer.buffer, this.#buffer.byteOffset, 2);\n\t\tconst result = view.getUint16(0);\n\t\tthis.#slice(2);\n\t\treturn result;\n\t}\n\n\t// Returns a Number using 53-bits, the max Javascript can use for integer math.\n\t// Values > 2^53-1 are coerced to a Number (precision is lost) and logged. We\n\t// downgrade overflow from throw to warn so a stray u64 field on the wire (e.g.\n\t// a peer's session-level Origin id) doesn't tear down the whole stream/session.\n\tasync u53(): Promise<number> {\n\t\tconst v = await this.u62();\n\t\tif (v > Varint.MAX_U53) {\n\t\t\tconsole.warn(`value larger than 53-bits; use u62 instead (precision lost): ${v.toString()}`);\n\t\t}\n\n\t\treturn Number(v);\n\t}\n\n\t// NOTE: Returns a bigint instead of a number since it may be larger than 53-bits\n\tasync u62(): Promise<bigint> {\n\t\tif (isLeadingOnes(this.version)) {\n\t\t\treturn this.#readLeadingOnes();\n\t\t}\n\t\treturn this.#readQuicVarint();\n\t}\n\n\tasync #readQuicVarint(): Promise<bigint> {\n\t\tawait this.#fillTo(1);\n\t\tconst size = (this.#buffer[0] & 0xc0) >> 6;\n\n\t\tif (size === 0) {\n\t\t\tconst first = this.#slice(1)[0];\n\t\t\treturn BigInt(first) & 0x3fn;\n\t\t}\n\t\tif (size === 1) {\n\t\t\tawait this.#fillTo(2);\n\t\t\tconst slice = this.#slice(2);\n\t\t\tconst view = new DataView(slice.buffer, slice.byteOffset, slice.byteLength);\n\n\t\t\treturn BigInt(view.getUint16(0)) & 0x3fffn;\n\t\t}\n\t\tif (size === 2) {\n\t\t\tawait this.#fillTo(4);\n\t\t\tconst slice = this.#slice(4);\n\t\t\tconst view = new DataView(slice.buffer, slice.byteOffset, slice.byteLength);\n\n\t\t\treturn BigInt(view.getUint32(0)) & 0x3fffffffn;\n\t\t}\n\t\tawait this.#fillTo(8);\n\t\tconst slice = this.#slice(8);\n\t\tconst view = new DataView(slice.buffer, slice.byteOffset, slice.byteLength);\n\n\t\treturn view.getBigUint64(0) & 0x3fffffffffffffffn;\n\t}\n\n\tasync #readLeadingOnes(): Promise<bigint> {\n\t\tawait this.#fillTo(1);\n\t\tconst b = this.#buffer[0];\n\n\t\t// Count leading 1-bits\n\t\tlet ones = 0;\n\t\tfor (let bit = 7; bit >= 0; bit--) {\n\t\t\tif (b & (1 << bit)) ones++;\n\t\t\telse break;\n\t\t}\n\n\t\t// 1111110x is a 7-byte form. Draft-17 rejects it; draft-18+ allows it per #1595.\n\t\tif (ones === 6 && this.version === Version.DRAFT_17) {\n\t\t\tthrow new Error(\"invalid leading-ones varint: 1111110x prefix is reserved on draft-17\");\n\t\t}\n\n\t\tlet totalSize: number;\n\t\tif (ones <= 5) totalSize = ones + 1;\n\t\telse if (ones === 6) totalSize = 7;\n\t\telse if (ones === 7) totalSize = 8;\n\t\telse totalSize = 9; // ones === 8\n\n\t\tawait this.#fillTo(totalSize);\n\t\tconst slice = this.#slice(totalSize);\n\n\t\tconst [value] = Varint.decodeLeadingOnes(slice);\n\t\treturn value;\n\t}\n\n\t// Returns false if there is more data to read, blocking if it hasn't been received yet.\n\tasync done(): Promise<boolean> {\n\t\tif (this.#buffer.byteLength > 0) return false;\n\t\treturn !(await this.#fill());\n\t}\n\n\tstop(reason: unknown) {\n\t\tthis.#reader?.cancel(reason).catch(() => void 0);\n\t}\n\n\tget closed(): Promise<void> {\n\t\treturn this.#reader?.closed ?? Promise.resolve();\n\t}\n}\n\n// Writer wraps a stream and writes chunks of data\nexport class Writer {\n\t#writer: WritableStreamDefaultWriter<Uint8Array>;\n\t#stream: WritableStream<Uint8Array>;\n\n\t// Scratch buffer for writing varints.\n\t// Fixed at 9 bytes (leading-ones max).\n\t#scratch: ArrayBuffer;\n\n\tversion?: IetfVersion;\n\n\tconstructor(stream: WritableStream<Uint8Array>, version?: IetfVersion) {\n\t\tthis.#stream = stream;\n\t\tthis.#scratch = new ArrayBuffer(9);\n\t\tthis.#writer = this.#stream.getWriter();\n\t\tthis.version = version;\n\t}\n\n\tasync bool(v: boolean) {\n\t\tawait this.write(setUint8(this.#scratch, v ? 1 : 0));\n\t}\n\n\tasync u8(v: number) {\n\t\tawait this.write(setUint8(this.#scratch, v));\n\t}\n\n\tasync u16(v: number) {\n\t\tawait this.write(setUint16(this.#scratch, v));\n\t}\n\n\tasync i32(v: number) {\n\t\tif (Math.abs(v) > MAX_U31) {\n\t\t\tthrow new Error(`overflow, value larger than 32-bits: ${v.toString()}`);\n\t\t}\n\n\t\t// We don't use a VarInt, so it always takes 4 bytes.\n\t\t// This could be improved but nothing is standardized yet.\n\t\tawait this.write(setInt32(this.#scratch, v));\n\t}\n\n\tasync u53(v: number) {\n\t\tif (v > Varint.MAX_U53) {\n\t\t\t// Number values above 2^53-1 have already lost precision before reaching\n\t\t\t// the wire, but downgrade overflow to warn so an upstream miscount\n\t\t\t// doesn't tear down the whole stream. The encoded varint will reflect\n\t\t\t// the truncated Number value.\n\t\t\tconsole.warn(`value larger than 53-bits; use u62 instead (precision lost): ${v.toString()}`);\n\t\t}\n\t\tif (isLeadingOnes(this.version)) {\n\t\t\tawait this.write(Varint.encodeLeadingOnesTo(this.#scratch, v));\n\t\t} else {\n\t\t\tawait this.write(Varint.encodeTo(this.#scratch, v));\n\t\t}\n\t}\n\n\tasync u62(v: bigint) {\n\t\tif (isLeadingOnes(this.version)) {\n\t\t\tawait this.write(Varint.encodeLeadingOnesTo(this.#scratch, v));\n\t\t} else {\n\t\t\tawait this.write(Varint.encodeTo(this.#scratch, v));\n\t\t}\n\t}\n\n\tasync write(v: Uint8Array) {\n\t\tawait this.#writer.write(v);\n\t}\n\n\tasync string(str: string) {\n\t\tconst data = new TextEncoder().encode(str);\n\t\tawait this.u53(data.byteLength);\n\t\tawait this.write(data);\n\t}\n\n\tclose() {\n\t\tthis.#writer.close().catch(() => void 0);\n\t}\n\n\tget closed(): Promise<void> {\n\t\treturn this.#writer.closed;\n\t}\n\n\treset(reason: unknown) {\n\t\tthis.#writer.abort(reason).catch(() => void 0);\n\t}\n\n\tstatic async open(quic: WebTransport, version?: IetfVersion): Promise<Writer> {\n\t\tconst writable = (await quic.createUnidirectionalStream()) as WritableStream<Uint8Array>;\n\t\treturn new Writer(writable, version);\n\t}\n}\n\nfunction setUint8(dst: ArrayBuffer, v: number): Uint8Array {\n\tconst buffer = new Uint8Array(dst, 0, 1);\n\tbuffer[0] = v;\n\treturn buffer;\n}\n\nfunction setUint16(dst: ArrayBuffer, v: number): Uint8Array {\n\tconst view = new DataView(dst, 0, 2);\n\tview.setUint16(0, v);\n\treturn new Uint8Array(view.buffer, view.byteOffset, view.byteLength);\n}\n\nfunction setInt32(dst: ArrayBuffer, v: number): Uint8Array {\n\tconst view = new DataView(dst, 0, 4);\n\tview.setInt32(0, v);\n\treturn new Uint8Array(view.buffer, view.byteOffset, view.byteLength);\n}\n\n// Returns the next stream from the connection\nexport class Readers {\n\t#reader: ReadableStreamDefaultReader<ReadableStream<Uint8Array>>;\n\t#version?: IetfVersion;\n\n\tconstructor(quic: WebTransport, version?: IetfVersion) {\n\t\tthis.#reader = quic.incomingUnidirectionalStreams.getReader() as ReadableStreamDefaultReader<\n\t\t\tReadableStream<Uint8Array>\n\t\t>;\n\t\tthis.#version = version;\n\t}\n\n\tasync next(): Promise<Reader | undefined> {\n\t\tconst next = await this.#reader.read();\n\t\tif (next.done) return;\n\t\treturn new Reader(next.value, undefined, this.#version);\n\t}\n\n\tclose() {\n\t\tthis.#reader.cancel();\n\t}\n}\n"]}
1
+ {"version":3,"file":"stream.js","sourceRoot":"","sources":["../src/stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAE3C,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,OAAO,KAAK,MAAM,MAAM,aAAa,CAAC;AAEtC,MAAM,OAAO,GAAG,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;AAC5B,MAAM,aAAa,GAAG,IAAI,GAAG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ACpD,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,6BAA6B,CAAC,SAAS,EAE1D,CAAC;QACF,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;IACzB,CAAC;IAED,KAAK,CAAC,IAAI;QACT,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC;QACvC,IAAI,IAAI,CAAC,IAAI;YAAE,OAAO;QACtB,OAAO,IAAI,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,SAAS,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;IACzD,CAAC;IAED,KAAK;QACJ,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC;IACvB,CAAC;CACD","sourcesContent":["import { fromTransport } from \"./error.ts\";\nimport type { IetfVersion } from \"./ietf/version.ts\";\nimport { Version } from \"./ietf/version.ts\";\nimport * as Varint from \"./varint.ts\";\n\nconst MAX_U31 = 2 ** 31 - 1;\nconst MAX_READ_SIZE = 1024 * 1024 * 64; // don't allocate more than 64MB for a message\n\nfunction isLeadingOnes(version?: IetfVersion): boolean {\n\treturn (\n\t\tversion !== undefined &&\n\t\tversion !== Version.DRAFT_14 &&\n\t\tversion !== Version.DRAFT_15 &&\n\t\tversion !== Version.DRAFT_16\n\t);\n}\n\nexport class Stream {\n\treader: Reader;\n\twriter: Writer;\n\n\tconstructor(props: {\n\t\twritable: WritableStream<Uint8Array>;\n\t\treadable: ReadableStream<Uint8Array>;\n\t\twriter?: Writer;\n\t\treader?: Reader;\n\t\tversion?: IetfVersion;\n\t}) {\n\t\tthis.writer = props.writer ?? new Writer(props.writable, props.version);\n\t\tthis.reader = props.reader ?? new Reader(props.readable, undefined, props.version);\n\t}\n\n\tstatic async accept(quic: WebTransport, version?: IetfVersion): Promise<Stream | undefined> {\n\t\tfor (;;) {\n\t\t\tconst reader =\n\t\t\t\tquic.incomingBidirectionalStreams.getReader() as ReadableStreamDefaultReader<WebTransportBidirectionalStream>;\n\t\t\tconst next = await reader.read();\n\t\t\treader.releaseLock();\n\n\t\t\tif (next.done) return;\n\t\t\tconst { readable, writable } = next.value;\n\t\t\treturn new Stream({ readable, writable, version });\n\t\t}\n\t}\n\n\tstatic async open(quic: WebTransport, version?: IetfVersion, priority?: number): Promise<Stream> {\n\t\tconst { readable, writable } = await quic.createBidirectionalStream({ sendOrder: priority });\n\t\treturn new Stream({ readable, writable, version });\n\t}\n\n\tclose() {\n\t\tthis.writer.close();\n\t\tthis.reader.stop(new Error(\"cancel\"));\n\t}\n\n\tabort(reason: Error) {\n\t\tthis.writer.reset(reason);\n\t\tthis.reader.stop(reason);\n\t}\n}\n\n// Reader wraps a stream and provides convience methods for reading pieces from a stream\n// Unfortunately we can't use a BYOB reader because it's not supported with WebTransport+WebWorkers yet.\nexport class Reader {\n\t#buffer: Uint8Array;\n\t#stream?: ReadableStream<Uint8Array>; // if undefined, the buffer is consumed then EOF\n\t#reader?: ReadableStreamDefaultReader<Uint8Array>;\n\t#closed?: Promise<void>;\n\tversion?: IetfVersion;\n\n\t// Either stream or buffer MUST be provided.\n\tconstructor(stream: ReadableStream<Uint8Array>, buffer?: Uint8Array, version?: IetfVersion);\n\tconstructor(stream: undefined, buffer: Uint8Array, version?: IetfVersion);\n\tconstructor(stream?: ReadableStream<Uint8Array>, buffer?: Uint8Array, version?: IetfVersion) {\n\t\tthis.#buffer = buffer ?? new Uint8Array();\n\t\tthis.#stream = stream;\n\t\tthis.#reader = this.#stream?.getReader();\n\t\tthis.version = version;\n\t}\n\n\t// Adds more data to the buffer, returning true if more data was added.\n\tasync #fill(): Promise<boolean> {\n\t\tif (!this.#reader) {\n\t\t\treturn false;\n\t\t}\n\n\t\t// Every read of this stream funnels through here, so decoding the peer's reset code\n\t\t// once is enough to keep the raw transport error out of every caller (and every app).\n\t\tconst result = await this.#reader.read().catch((err: unknown) => {\n\t\t\tthrow fromTransport(err);\n\t\t});\n\n\t\tif (result.done) {\n\t\t\treturn false;\n\t\t}\n\n\t\tif (result.value.byteLength === 0) {\n\t\t\tthrow new Error(\"unexpected empty chunk\");\n\t\t}\n\n\t\tconst buffer = new Uint8Array(result.value);\n\n\t\tif (this.#buffer.byteLength === 0) {\n\t\t\tthis.#buffer = buffer;\n\t\t} else {\n\t\t\tconst temp = new Uint8Array(this.#buffer.byteLength + buffer.byteLength);\n\t\t\ttemp.set(this.#buffer);\n\t\t\ttemp.set(buffer, this.#buffer.byteLength);\n\t\t\tthis.#buffer = temp;\n\t\t}\n\n\t\treturn true;\n\t}\n\n\t// Add more data to the buffer until it's at least size bytes.\n\tasync #fillTo(size: number) {\n\t\tif (size > MAX_READ_SIZE) {\n\t\t\tthrow new Error(`read size ${size} exceeds max size ${MAX_READ_SIZE}`);\n\t\t}\n\n\t\twhile (this.#buffer.byteLength < size) {\n\t\t\tif (!(await this.#fill())) {\n\t\t\t\tthrow new Error(\"unexpected end of stream\");\n\t\t\t}\n\t\t}\n\t}\n\n\t// Consumes the first size bytes of the buffer.\n\t#slice(size: number): Uint8Array {\n\t\tconst result = new Uint8Array(this.#buffer.buffer, this.#buffer.byteOffset, size);\n\t\tthis.#buffer = new Uint8Array(\n\t\t\tthis.#buffer.buffer,\n\t\t\tthis.#buffer.byteOffset + size,\n\t\t\tthis.#buffer.byteLength - size,\n\t\t);\n\n\t\treturn result;\n\t}\n\n\tasync read(size: number): Promise<Uint8Array> {\n\t\tif (size === 0) return new Uint8Array();\n\n\t\tawait this.#fillTo(size);\n\t\treturn this.#slice(size);\n\t}\n\n\tasync readAll(): Promise<Uint8Array> {\n\t\twhile (await this.#fill()) {\n\t\t\t// keep going\n\t\t}\n\t\treturn this.#slice(this.#buffer.byteLength);\n\t}\n\n\tasync string(): Promise<string> {\n\t\tconst length = await this.u53();\n\t\tconst buffer = await this.read(length);\n\t\treturn new TextDecoder().decode(buffer);\n\t}\n\n\tasync bool(): Promise<boolean> {\n\t\tconst v = await this.u8();\n\t\tif (v === 0) return false;\n\t\tif (v === 1) return true;\n\t\tthrow new Error(\"invalid bool value\");\n\t}\n\n\tasync u8(): Promise<number> {\n\t\tawait this.#fillTo(1);\n\t\treturn this.#slice(1)[0];\n\t}\n\n\tasync u16(): Promise<number> {\n\t\tawait this.#fillTo(2);\n\t\tconst view = new DataView(this.#buffer.buffer, this.#buffer.byteOffset, 2);\n\t\tconst result = view.getUint16(0);\n\t\tthis.#slice(2);\n\t\treturn result;\n\t}\n\n\t// Returns a Number using 53-bits, the max Javascript can use for integer math.\n\t// Values > 2^53-1 are coerced to a Number (precision is lost) and logged. We\n\t// downgrade overflow from throw to warn so a stray u64 field on the wire (e.g.\n\t// a peer's session-level Origin id) doesn't tear down the whole stream/session.\n\tasync u53(): Promise<number> {\n\t\tconst v = await this.u62();\n\t\tif (v > Varint.MAX_U53) {\n\t\t\tconsole.warn(`value larger than 53-bits; use u62 instead (precision lost): ${v.toString()}`);\n\t\t}\n\n\t\treturn Number(v);\n\t}\n\n\t// NOTE: Returns a bigint instead of a number since it may be larger than 53-bits\n\tasync u62(): Promise<bigint> {\n\t\tif (isLeadingOnes(this.version)) {\n\t\t\treturn this.#readLeadingOnes();\n\t\t}\n\t\treturn this.#readQuicVarint();\n\t}\n\n\tasync #readQuicVarint(): Promise<bigint> {\n\t\tawait this.#fillTo(1);\n\t\tconst size = (this.#buffer[0] & 0xc0) >> 6;\n\n\t\tif (size === 0) {\n\t\t\tconst first = this.#slice(1)[0];\n\t\t\treturn BigInt(first) & 0x3fn;\n\t\t}\n\t\tif (size === 1) {\n\t\t\tawait this.#fillTo(2);\n\t\t\tconst slice = this.#slice(2);\n\t\t\tconst view = new DataView(slice.buffer, slice.byteOffset, slice.byteLength);\n\n\t\t\treturn BigInt(view.getUint16(0)) & 0x3fffn;\n\t\t}\n\t\tif (size === 2) {\n\t\t\tawait this.#fillTo(4);\n\t\t\tconst slice = this.#slice(4);\n\t\t\tconst view = new DataView(slice.buffer, slice.byteOffset, slice.byteLength);\n\n\t\t\treturn BigInt(view.getUint32(0)) & 0x3fffffffn;\n\t\t}\n\t\tawait this.#fillTo(8);\n\t\tconst slice = this.#slice(8);\n\t\tconst view = new DataView(slice.buffer, slice.byteOffset, slice.byteLength);\n\n\t\treturn view.getBigUint64(0) & 0x3fffffffffffffffn;\n\t}\n\n\tasync #readLeadingOnes(): Promise<bigint> {\n\t\tawait this.#fillTo(1);\n\t\tconst b = this.#buffer[0];\n\n\t\t// Count leading 1-bits\n\t\tlet ones = 0;\n\t\tfor (let bit = 7; bit >= 0; bit--) {\n\t\t\tif (b & (1 << bit)) ones++;\n\t\t\telse break;\n\t\t}\n\n\t\t// 1111110x is a 7-byte form. Draft-17 rejects it; draft-18+ allows it per #1595.\n\t\tif (ones === 6 && this.version === Version.DRAFT_17) {\n\t\t\tthrow new Error(\"invalid leading-ones varint: 1111110x prefix is reserved on draft-17\");\n\t\t}\n\n\t\tlet totalSize: number;\n\t\tif (ones <= 5) totalSize = ones + 1;\n\t\telse if (ones === 6) totalSize = 7;\n\t\telse if (ones === 7) totalSize = 8;\n\t\telse totalSize = 9; // ones === 8\n\n\t\tawait this.#fillTo(totalSize);\n\t\tconst slice = this.#slice(totalSize);\n\n\t\tconst [value] = Varint.decodeLeadingOnes(slice);\n\t\treturn value;\n\t}\n\n\t// Returns false if there is more data to read, blocking if it hasn't been received yet.\n\tasync done(): Promise<boolean> {\n\t\tif (this.#buffer.byteLength > 0) return false;\n\t\treturn !(await this.#fill());\n\t}\n\n\tstop(reason: unknown) {\n\t\tthis.#reader?.cancel(reason).catch(() => void 0);\n\t}\n\n\t// Decoded like #fill: a caller racing this against a read must not get a different error\n\t// shape depending on which one won. Derived once, so racing it per frame doesn't allocate.\n\tget closed(): Promise<void> {\n\t\tthis.#closed ??= (this.#reader?.closed ?? Promise.resolve()).catch((err: unknown) => {\n\t\t\tthrow fromTransport(err);\n\t\t});\n\t\treturn this.#closed;\n\t}\n}\n\n// Writer wraps a stream and writes chunks of data\nexport class Writer {\n\t#writer: WritableStreamDefaultWriter<Uint8Array>;\n\t#stream: WritableStream<Uint8Array>;\n\t#closed?: Promise<void>;\n\n\t// Scratch buffer for writing varints.\n\t// Fixed at 9 bytes (leading-ones max).\n\t#scratch: ArrayBuffer;\n\n\tversion?: IetfVersion;\n\n\tconstructor(stream: WritableStream<Uint8Array>, version?: IetfVersion) {\n\t\tthis.#stream = stream;\n\t\tthis.#scratch = new ArrayBuffer(9);\n\t\tthis.#writer = this.#stream.getWriter();\n\t\tthis.version = version;\n\t}\n\n\tasync bool(v: boolean) {\n\t\tawait this.write(setUint8(this.#scratch, v ? 1 : 0));\n\t}\n\n\tasync u8(v: number) {\n\t\tawait this.write(setUint8(this.#scratch, v));\n\t}\n\n\tasync u16(v: number) {\n\t\tawait this.write(setUint16(this.#scratch, v));\n\t}\n\n\tasync i32(v: number) {\n\t\tif (Math.abs(v) > MAX_U31) {\n\t\t\tthrow new Error(`overflow, value larger than 32-bits: ${v.toString()}`);\n\t\t}\n\n\t\t// We don't use a VarInt, so it always takes 4 bytes.\n\t\t// This could be improved but nothing is standardized yet.\n\t\tawait this.write(setInt32(this.#scratch, v));\n\t}\n\n\tasync u53(v: number) {\n\t\tif (v > Varint.MAX_U53) {\n\t\t\t// Number values above 2^53-1 have already lost precision before reaching\n\t\t\t// the wire, but downgrade overflow to warn so an upstream miscount\n\t\t\t// doesn't tear down the whole stream. The encoded varint will reflect\n\t\t\t// the truncated Number value.\n\t\t\tconsole.warn(`value larger than 53-bits; use u62 instead (precision lost): ${v.toString()}`);\n\t\t}\n\t\tif (isLeadingOnes(this.version)) {\n\t\t\tawait this.write(Varint.encodeLeadingOnesTo(this.#scratch, v));\n\t\t} else {\n\t\t\tawait this.write(Varint.encodeTo(this.#scratch, v));\n\t\t}\n\t}\n\n\tasync u62(v: bigint) {\n\t\tif (isLeadingOnes(this.version)) {\n\t\t\tawait this.write(Varint.encodeLeadingOnesTo(this.#scratch, v));\n\t\t} else {\n\t\t\tawait this.write(Varint.encodeTo(this.#scratch, v));\n\t\t}\n\t}\n\n\tasync write(v: Uint8Array) {\n\t\t// Mirrors Reader.#fill: every write funnels through here, so a STOP_SENDING from the\n\t\t// peer surfaces as a typed code rather than the transport's own error shape.\n\t\tawait this.#writer.write(v).catch((err: unknown) => {\n\t\t\tthrow fromTransport(err);\n\t\t});\n\t}\n\n\tasync string(str: string) {\n\t\tconst data = new TextEncoder().encode(str);\n\t\tawait this.u53(data.byteLength);\n\t\tawait this.write(data);\n\t}\n\n\tclose() {\n\t\tthis.#writer.close().catch(() => void 0);\n\t}\n\n\t// Mirrors Reader.closed: a STOP_SENDING reaches a caller racing this with the same\n\t// typed code it would get from a write.\n\tget closed(): Promise<void> {\n\t\tthis.#closed ??= this.#writer.closed.catch((err: unknown) => {\n\t\t\tthrow fromTransport(err);\n\t\t});\n\t\treturn this.#closed;\n\t}\n\n\treset(reason: unknown) {\n\t\tthis.#writer.abort(reason).catch(() => void 0);\n\t}\n\n\tstatic async open(quic: WebTransport, version?: IetfVersion): Promise<Writer> {\n\t\tconst writable = (await quic.createUnidirectionalStream()) as WritableStream<Uint8Array>;\n\t\treturn new Writer(writable, version);\n\t}\n}\n\nfunction setUint8(dst: ArrayBuffer, v: number): Uint8Array {\n\tconst buffer = new Uint8Array(dst, 0, 1);\n\tbuffer[0] = v;\n\treturn buffer;\n}\n\nfunction setUint16(dst: ArrayBuffer, v: number): Uint8Array {\n\tconst view = new DataView(dst, 0, 2);\n\tview.setUint16(0, v);\n\treturn new Uint8Array(view.buffer, view.byteOffset, view.byteLength);\n}\n\nfunction setInt32(dst: ArrayBuffer, v: number): Uint8Array {\n\tconst view = new DataView(dst, 0, 4);\n\tview.setInt32(0, v);\n\treturn new Uint8Array(view.buffer, view.byteOffset, view.byteLength);\n}\n\n// Returns the next stream from the connection\nexport class Readers {\n\t#reader: ReadableStreamDefaultReader<ReadableStream<Uint8Array>>;\n\t#version?: IetfVersion;\n\n\tconstructor(quic: WebTransport, version?: IetfVersion) {\n\t\tthis.#reader = quic.incomingUnidirectionalStreams.getReader() as ReadableStreamDefaultReader<\n\t\t\tReadableStream<Uint8Array>\n\t\t>;\n\t\tthis.#version = version;\n\t}\n\n\tasync next(): Promise<Reader | undefined> {\n\t\tconst next = await this.#reader.read();\n\t\tif (next.done) return;\n\t\treturn new Reader(next.value, undefined, this.#version);\n\t}\n\n\tclose() {\n\t\tthis.#reader.cancel();\n\t}\n}\n"]}
package/stream.test.js CHANGED
@@ -1,5 +1,6 @@
1
1
  /* @ts-self-types="./stream.test.d.ts" */
2
2
  import { expect, test } from "bun:test";
3
+ import { RemoteError } from "./error.js";
3
4
  import { Reader, Writer } from "./stream.js";
4
5
  // Helper to create a writable stream that captures written data
5
6
  function createTestWritableStream() {
@@ -217,4 +218,35 @@ test("Reader u53 decodes two-byte stream type prefixes", async () => {
217
218
  expect(await reader.u53()).toBe(0x40);
218
219
  expect(await reader.done()).toBe(true);
219
220
  });
221
+ /** A stream reset as a transport delivers one: the peer's code, and nothing else useful. */
222
+ class Reset extends Error {
223
+ source = "stream";
224
+ streamErrorCode;
225
+ constructor(code) {
226
+ super("");
227
+ this.streamErrorCode = code;
228
+ }
229
+ }
230
+ test("Reader closed rejects with the decoded reset code", async () => {
231
+ // Protocol paths race `closed` against a read, so the two must agree on the error
232
+ // shape: whichever wins, the reset code reaches the caller the same way.
233
+ const reader = new Reader(new ReadableStream({
234
+ start: (controller) => controller.error(new Reset(2)),
235
+ }));
236
+ const err = await reader.closed.then(() => undefined, (e) => e);
237
+ expect(err).toBeInstanceOf(RemoteError);
238
+ expect(err.code).toBe(2);
239
+ });
240
+ test("Writer closed rejects with the decoded reset code", async () => {
241
+ const writer = new Writer(new WritableStream({
242
+ start: (controller) => controller.error(new Reset(31)),
243
+ }));
244
+ const err = await writer.closed.then(() => undefined, (e) => e);
245
+ expect(err).toBeInstanceOf(RemoteError);
246
+ expect(err.code).toBe(31);
247
+ });
248
+ test("closed is stable, so racing it per frame does not allocate", async () => {
249
+ const reader = new Reader(new ReadableStream());
250
+ expect(reader.closed).toBe(reader.closed);
251
+ });
220
252
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G,MAAM,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACnC,MAAM,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,IAAI,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAE3C,MAAM,CAAC,MAAM,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACxC,CAAC,CAAC,CAAC;AAEH,IAAI,CAAC,kDAAkD,EAAE,KAAK,IAAI,EAAE;IACnE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,wBAAwB,EAAE,CAAC;IACvD,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,MAAM,CAAC,CAAC;IAElC,MAAM,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IACvB,MAAM,CAAC,KAAK,EAAE,CAAC;IACf,MAAM,MAAM,CAAC,MAAM,CAAC;IAEpB,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,SAAS,EAAE,YAAY,CAAC,OAAO,CAAC,CAAC,CAAC;IAC5D,MAAM,CAAC,MAAM,MAAM,CAAC,GAAG,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACtC,MAAM,CAAC,MAAM,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACxC,CAAC,CAAC,CAAC","sourcesContent":["import { expect, test } from \"bun:test\";\nimport { Reader, Writer } from \"./stream.ts\";\n\n// Helper to create a writable stream that captures written data\nfunction createTestWritableStream(): { stream: WritableStream<Uint8Array>; written: Uint8Array[] } {\n\tconst written: Uint8Array[] = [];\n\tconst stream = new WritableStream<Uint8Array>({\n\t\twrite(chunk) {\n\t\t\twritten.push(new Uint8Array(chunk));\n\t\t},\n\t});\n\treturn { stream, written };\n}\n\n// Helper to concatenate written chunks\nfunction concatChunks(chunks: Uint8Array[]): Uint8Array {\n\tconst totalLength = chunks.reduce((sum, chunk) => sum + chunk.byteLength, 0);\n\tconst result = new Uint8Array(totalLength);\n\tlet offset = 0;\n\tfor (const chunk of chunks) {\n\t\tresult.set(chunk, offset);\n\t\toffset += chunk.byteLength;\n\t}\n\treturn result;\n}\n\ntest(\"Writer u8\", async () => {\n\tconst { stream, written } = createTestWritableStream();\n\tconst writer = new Writer(stream);\n\n\tawait writer.u8(42);\n\tawait writer.u8(255);\n\n\twriter.close();\n\tawait writer.closed;\n\n\texpect(written.length).toBe(2);\n\texpect(written[0]).toEqual(new Uint8Array([42]));\n\texpect(written[1]).toEqual(new Uint8Array([255]));\n});\n\ntest(\"Writer i32\", async () => {\n\tconst { stream, written } = createTestWritableStream();\n\tconst writer = new Writer(stream);\n\n\tawait writer.i32(0);\n\tawait writer.i32(-1);\n\tawait writer.i32(1000);\n\n\twriter.close();\n\tawait writer.closed;\n\n\tconst result = concatChunks(written);\n\texpect(result.byteLength).toBe(12); // 3 * 4 bytes\n\n\t// Read back the values\n\tconst view = new DataView(result.buffer, result.byteOffset, result.byteLength);\n\texpect(view.getInt32(0)).toBe(0);\n\texpect(view.getInt32(4)).toBe(-1);\n\texpect(view.getInt32(8)).toBe(1000);\n});\n\ntest(\"Writer u53\", async () => {\n\tconst { stream, written } = createTestWritableStream();\n\tconst writer = new Writer(stream);\n\n\tawait writer.u53(0);\n\tawait writer.u53(63); // MAX_U6\n\tawait writer.u53(64); // MIN for 2-byte varint\n\tawait writer.u53(16383); // MAX_U14\n\tawait writer.u53(16384); // MIN for 4-byte varint\n\n\twriter.close();\n\tawait writer.closed;\n\n\t// Verify the varint encoding sizes\n\texpect(written[0].byteLength).toBe(1); // 0 fits in 1 byte\n\texpect(written[1].byteLength).toBe(1); // 63 fits in 1 byte\n\texpect(written[2].byteLength).toBe(2); // 64 needs 2 bytes\n\texpect(written[3].byteLength).toBe(2); // 16383 fits in 2 bytes\n\texpect(written[4].byteLength).toBe(4); // 16384 needs 4 bytes\n});\n\ntest(\"Writer string\", async () => {\n\tconst { stream, written } = createTestWritableStream();\n\tconst writer = new Writer(stream);\n\n\tawait writer.string(\"hello\");\n\tawait writer.string(\"🎉\");\n\n\twriter.close();\n\tawait writer.closed;\n\n\tconst result = concatChunks(written);\n\n\t// Create a reader to parse the result\n\tconst reader = new Reader(undefined, result);\n\n\tconst str1 = await reader.string();\n\tconst str2 = await reader.string();\n\n\texpect(str1).toBe(\"hello\");\n\texpect(str2).toBe(\"🎉\");\n});\n\ntest(\"Reader u8\", async () => {\n\tconst data = new Uint8Array([42, 255, 0, 128]);\n\tconst reader = new Reader(undefined, data);\n\n\texpect(await reader.u8()).toBe(42);\n\texpect(await reader.u8()).toBe(255);\n\texpect(await reader.u8()).toBe(0);\n\texpect(await reader.u8()).toBe(128);\n\n\texpect(await reader.done()).toBe(true);\n});\n\ntest(\"Reader read with exact sizes\", async () => {\n\tconst data = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);\n\tconst reader = new Reader(undefined, data);\n\n\tconst chunk1 = await reader.read(3);\n\texpect(chunk1).toEqual(new Uint8Array([1, 2, 3]));\n\n\tconst chunk2 = await reader.read(2);\n\texpect(chunk2).toEqual(new Uint8Array([4, 5]));\n\n\tconst chunk3 = await reader.read(3);\n\texpect(chunk3).toEqual(new Uint8Array([6, 7, 8]));\n\n\texpect(await reader.done()).toBe(true);\n});\n\ntest(\"Reader read with zero size\", async () => {\n\tconst data = new Uint8Array([1, 2, 3]);\n\tconst reader = new Reader(undefined, data);\n\n\tconst chunk = await reader.read(0);\n\texpect(chunk).toEqual(new Uint8Array([]));\n\n\t// Original data should still be available\n\tconst remaining = await reader.read(3);\n\texpect(remaining).toEqual(new Uint8Array([1, 2, 3]));\n});\n\ntest(\"Reader readAll\", async () => {\n\tconst data = new Uint8Array([1, 2, 3, 4, 5]);\n\tconst reader = new Reader(undefined, data);\n\n\t// Read some data first\n\tawait reader.read(2);\n\n\t// readAll should get the remaining data\n\tconst remaining = await reader.readAll();\n\texpect(remaining).toEqual(new Uint8Array([3, 4, 5]));\n\n\texpect(await reader.done()).toBe(true);\n});\n\ntest(\"Reader u53 varint decoding\", async () => {\n\t// Test various varint sizes\n\tconst testValues = [0, 63, 64, 16383, 16384, 1073741823, 1073741824];\n\n\tconst { stream, written } = createTestWritableStream();\n\tconst testWriter = new Writer(stream);\n\n\tfor (const value of testValues) {\n\t\tawait testWriter.u53(value);\n\t}\n\n\ttestWriter.close();\n\tawait testWriter.closed;\n\n\tconst data = concatChunks(written);\n\tconst reader = new Reader(undefined, data);\n\n\tfor (const expectedValue of testValues) {\n\t\tconst actualValue = await reader.u53();\n\t\texpect(actualValue).toBe(expectedValue);\n\t}\n\n\texpect(await reader.done()).toBe(true);\n});\n\ntest(\"Reader u62 varint decoding\", async () => {\n\tconst testValues = [0n, 63n, 64n, 16383n, 16384n, 1073741823n, 1073741824n, 9007199254740991n]; // MAX_U53\n\n\tconst { stream, written } = createTestWritableStream();\n\tconst testWriter = new Writer(stream);\n\n\tfor (const value of testValues) {\n\t\tawait testWriter.u62(value);\n\t}\n\n\ttestWriter.close();\n\tawait testWriter.closed;\n\n\tconst data = concatChunks(written);\n\tconst reader = new Reader(undefined, data);\n\n\tfor (const expectedValue of testValues) {\n\t\tconst actualValue = await reader.u62();\n\t\texpect(actualValue).toBe(expectedValue);\n\t}\n\n\texpect(await reader.done()).toBe(true);\n});\n\ntest(\"Reader string decoding\", async () => {\n\tconst testStrings = [\"hello\", \"🎉\", \"\", \"world with spaces\", \"multi\\nline\\nstring\"];\n\n\tconst { stream, written } = createTestWritableStream();\n\tconst writer = new Writer(stream);\n\n\tfor (const str of testStrings) {\n\t\tawait writer.string(str);\n\t}\n\n\twriter.close();\n\tawait writer.closed;\n\n\tconst data = concatChunks(written);\n\tconst reader = new Reader(undefined, data);\n\n\tfor (const expectedString of testStrings) {\n\t\tconst actualString = await reader.string();\n\t\texpect(actualString).toBe(expectedString);\n\t}\n\n\texpect(await reader.done()).toBe(true);\n});\n\ntest(\"Reader from stream\", async () => {\n\tconst chunks = [new Uint8Array([1, 2, 3]), new Uint8Array([4, 5]), new Uint8Array([6, 7, 8, 9])];\n\n\tconst stream = new ReadableStream({\n\t\tstart(controller) {\n\t\t\tfor (const chunk of chunks) {\n\t\t\t\tcontroller.enqueue(chunk);\n\t\t\t}\n\t\t\tcontroller.close();\n\t\t},\n\t});\n\n\tconst reader = new Reader(stream);\n\n\t// Read all data\n\tconst result = await reader.readAll();\n\texpect(result).toEqual(new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9]));\n});\n\ntest(\"Reader stream with partial reads\", async () => {\n\tconst chunks = [new Uint8Array([1, 2]), new Uint8Array([3, 4, 5]), new Uint8Array([6])];\n\n\tconst stream = new ReadableStream({\n\t\tstart(controller) {\n\t\t\tfor (const chunk of chunks) {\n\t\t\t\tcontroller.enqueue(chunk);\n\t\t\t}\n\t\t\tcontroller.close();\n\t\t},\n\t});\n\n\tconst reader = new Reader(stream);\n\n\t// Read specific amounts that cross chunk boundaries\n\tconst first = await reader.read(3); // Should span first two chunks\n\texpect(first).toEqual(new Uint8Array([1, 2, 3]));\n\n\tconst second = await reader.read(2);\n\texpect(second).toEqual(new Uint8Array([4, 5]));\n\n\tconst third = await reader.read(1);\n\texpect(third).toEqual(new Uint8Array([6]));\n\n\texpect(await reader.done()).toBe(true);\n});\n\ntest(\"Reader u53 decodes two-byte stream type prefixes\", async () => {\n\tconst { stream, written } = createTestWritableStream();\n\tconst writer = new Writer(stream);\n\n\tawait writer.u53(0x40);\n\twriter.close();\n\tawait writer.closed;\n\n\tconst reader = new Reader(undefined, concatChunks(written));\n\texpect(await reader.u53()).toBe(0x40);\n\texpect(await reader.done()).toBe(true);\n});\n"]}
1
+ {"version":3,"file":"stream.test.js","sourceRoot":"","sources":["../src/stream.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AACxC,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACzC,OAAO,EAAE,MAAM,EAAE,MAA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C,SAAS,EACf,CAAC,CAAU,EAAE,EAAE,CAAC,CAAC,CACjB,CAAC;IACF,MAAM,CAAC,GAAG,CAAC,CAAC,cAAc,CAAC,WAAW,CAAC,CAAC;IACxC,MAAM,CAAE,GAAmB,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAC3C,CAAC,CAAC,CAAC;AAEH,IAAI,CAAC,mDAAmD,EAAE,KAAK,IAAI,EAAE;IACpE,MAAM,MAAM,GAAG,IAAI,MAAM,CACxB,IAAI,cAAc,CAAa;QAC9B,KAAK,EAAE,CAAC,UAAU,EAAE,EAAE,CAAC,UAAU,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,EAAE,CAAC,CAAC;KACtD,CAAC,CACF,CAAC;IAEF,MAAM,GAAG,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,IAAI,CACnC,GAAG,EAAE,CAAC,SAAS,EACf,CAAC,CAAU,EAAE,EAAE,CAAC,CAAC,CACjB,CAAC;IACF,MAAM,CAAC,GAAG,CAAC,CAAC,cAAc,CAAC,WAAW,CAAC,CAAC;IACxC,MAAM,CAAE,GAAmB,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AAC5C,CAAC,CAAC,CAAC;AAEH,IAAI,CAAC,4DAA4D,EAAE,KAAK,IAAI,EAAE;IAC7E,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,cAAc,EAAc,CAAC,CAAC;IAC5D,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;AAC3C,CAAC,CAAC,CAAC","sourcesContent":["import { expect, test } from \"bun:test\";\nimport { RemoteError } from \"./error.ts\";\nimport { Reader, Writer } from \"./stream.ts\";\n\n// Helper to create a writable stream that captures written data\nfunction createTestWritableStream(): { stream: WritableStream<Uint8Array>; written: Uint8Array[] } {\n\tconst written: Uint8Array[] = [];\n\tconst stream = new WritableStream<Uint8Array>({\n\t\twrite(chunk) {\n\t\t\twritten.push(new Uint8Array(chunk));\n\t\t},\n\t});\n\treturn { stream, written };\n}\n\n// Helper to concatenate written chunks\nfunction concatChunks(chunks: Uint8Array[]): Uint8Array {\n\tconst totalLength = chunks.reduce((sum, chunk) => sum + chunk.byteLength, 0);\n\tconst result = new Uint8Array(totalLength);\n\tlet offset = 0;\n\tfor (const chunk of chunks) {\n\t\tresult.set(chunk, offset);\n\t\toffset += chunk.byteLength;\n\t}\n\treturn result;\n}\n\ntest(\"Writer u8\", async () => {\n\tconst { stream, written } = createTestWritableStream();\n\tconst writer = new Writer(stream);\n\n\tawait writer.u8(42);\n\tawait writer.u8(255);\n\n\twriter.close();\n\tawait writer.closed;\n\n\texpect(written.length).toBe(2);\n\texpect(written[0]).toEqual(new Uint8Array([42]));\n\texpect(written[1]).toEqual(new Uint8Array([255]));\n});\n\ntest(\"Writer i32\", async () => {\n\tconst { stream, written } = createTestWritableStream();\n\tconst writer = new Writer(stream);\n\n\tawait writer.i32(0);\n\tawait writer.i32(-1);\n\tawait writer.i32(1000);\n\n\twriter.close();\n\tawait writer.closed;\n\n\tconst result = concatChunks(written);\n\texpect(result.byteLength).toBe(12); // 3 * 4 bytes\n\n\t// Read back the values\n\tconst view = new DataView(result.buffer, result.byteOffset, result.byteLength);\n\texpect(view.getInt32(0)).toBe(0);\n\texpect(view.getInt32(4)).toBe(-1);\n\texpect(view.getInt32(8)).toBe(1000);\n});\n\ntest(\"Writer u53\", async () => {\n\tconst { stream, written } = createTestWritableStream();\n\tconst writer = new Writer(stream);\n\n\tawait writer.u53(0);\n\tawait writer.u53(63); // MAX_U6\n\tawait writer.u53(64); // MIN for 2-byte varint\n\tawait writer.u53(16383); // MAX_U14\n\tawait writer.u53(16384); // MIN for 4-byte varint\n\n\twriter.close();\n\tawait writer.closed;\n\n\t// Verify the varint encoding sizes\n\texpect(written[0].byteLength).toBe(1); // 0 fits in 1 byte\n\texpect(written[1].byteLength).toBe(1); // 63 fits in 1 byte\n\texpect(written[2].byteLength).toBe(2); // 64 needs 2 bytes\n\texpect(written[3].byteLength).toBe(2); // 16383 fits in 2 bytes\n\texpect(written[4].byteLength).toBe(4); // 16384 needs 4 bytes\n});\n\ntest(\"Writer string\", async () => {\n\tconst { stream, written } = createTestWritableStream();\n\tconst writer = new Writer(stream);\n\n\tawait writer.string(\"hello\");\n\tawait writer.string(\"🎉\");\n\n\twriter.close();\n\tawait writer.closed;\n\n\tconst result = concatChunks(written);\n\n\t// Create a reader to parse the result\n\tconst reader = new Reader(undefined, result);\n\n\tconst str1 = await reader.string();\n\tconst str2 = await reader.string();\n\n\texpect(str1).toBe(\"hello\");\n\texpect(str2).toBe(\"🎉\");\n});\n\ntest(\"Reader u8\", async () => {\n\tconst data = new Uint8Array([42, 255, 0, 128]);\n\tconst reader = new Reader(undefined, data);\n\n\texpect(await reader.u8()).toBe(42);\n\texpect(await reader.u8()).toBe(255);\n\texpect(await reader.u8()).toBe(0);\n\texpect(await reader.u8()).toBe(128);\n\n\texpect(await reader.done()).toBe(true);\n});\n\ntest(\"Reader read with exact sizes\", async () => {\n\tconst data = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);\n\tconst reader = new Reader(undefined, data);\n\n\tconst chunk1 = await reader.read(3);\n\texpect(chunk1).toEqual(new Uint8Array([1, 2, 3]));\n\n\tconst chunk2 = await reader.read(2);\n\texpect(chunk2).toEqual(new Uint8Array([4, 5]));\n\n\tconst chunk3 = await reader.read(3);\n\texpect(chunk3).toEqual(new Uint8Array([6, 7, 8]));\n\n\texpect(await reader.done()).toBe(true);\n});\n\ntest(\"Reader read with zero size\", async () => {\n\tconst data = new Uint8Array([1, 2, 3]);\n\tconst reader = new Reader(undefined, data);\n\n\tconst chunk = await reader.read(0);\n\texpect(chunk).toEqual(new Uint8Array([]));\n\n\t// Original data should still be available\n\tconst remaining = await reader.read(3);\n\texpect(remaining).toEqual(new Uint8Array([1, 2, 3]));\n});\n\ntest(\"Reader readAll\", async () => {\n\tconst data = new Uint8Array([1, 2, 3, 4, 5]);\n\tconst reader = new Reader(undefined, data);\n\n\t// Read some data first\n\tawait reader.read(2);\n\n\t// readAll should get the remaining data\n\tconst remaining = await reader.readAll();\n\texpect(remaining).toEqual(new Uint8Array([3, 4, 5]));\n\n\texpect(await reader.done()).toBe(true);\n});\n\ntest(\"Reader u53 varint decoding\", async () => {\n\t// Test various varint sizes\n\tconst testValues = [0, 63, 64, 16383, 16384, 1073741823, 1073741824];\n\n\tconst { stream, written } = createTestWritableStream();\n\tconst testWriter = new Writer(stream);\n\n\tfor (const value of testValues) {\n\t\tawait testWriter.u53(value);\n\t}\n\n\ttestWriter.close();\n\tawait testWriter.closed;\n\n\tconst data = concatChunks(written);\n\tconst reader = new Reader(undefined, data);\n\n\tfor (const expectedValue of testValues) {\n\t\tconst actualValue = await reader.u53();\n\t\texpect(actualValue).toBe(expectedValue);\n\t}\n\n\texpect(await reader.done()).toBe(true);\n});\n\ntest(\"Reader u62 varint decoding\", async () => {\n\tconst testValues = [0n, 63n, 64n, 16383n, 16384n, 1073741823n, 1073741824n, 9007199254740991n]; // MAX_U53\n\n\tconst { stream, written } = createTestWritableStream();\n\tconst testWriter = new Writer(stream);\n\n\tfor (const value of testValues) {\n\t\tawait testWriter.u62(value);\n\t}\n\n\ttestWriter.close();\n\tawait testWriter.closed;\n\n\tconst data = concatChunks(written);\n\tconst reader = new Reader(undefined, data);\n\n\tfor (const expectedValue of testValues) {\n\t\tconst actualValue = await reader.u62();\n\t\texpect(actualValue).toBe(expectedValue);\n\t}\n\n\texpect(await reader.done()).toBe(true);\n});\n\ntest(\"Reader string decoding\", async () => {\n\tconst testStrings = [\"hello\", \"🎉\", \"\", \"world with spaces\", \"multi\\nline\\nstring\"];\n\n\tconst { stream, written } = createTestWritableStream();\n\tconst writer = new Writer(stream);\n\n\tfor (const str of testStrings) {\n\t\tawait writer.string(str);\n\t}\n\n\twriter.close();\n\tawait writer.closed;\n\n\tconst data = concatChunks(written);\n\tconst reader = new Reader(undefined, data);\n\n\tfor (const expectedString of testStrings) {\n\t\tconst actualString = await reader.string();\n\t\texpect(actualString).toBe(expectedString);\n\t}\n\n\texpect(await reader.done()).toBe(true);\n});\n\ntest(\"Reader from stream\", async () => {\n\tconst chunks = [new Uint8Array([1, 2, 3]), new Uint8Array([4, 5]), new Uint8Array([6, 7, 8, 9])];\n\n\tconst stream = new ReadableStream({\n\t\tstart(controller) {\n\t\t\tfor (const chunk of chunks) {\n\t\t\t\tcontroller.enqueue(chunk);\n\t\t\t}\n\t\t\tcontroller.close();\n\t\t},\n\t});\n\n\tconst reader = new Reader(stream);\n\n\t// Read all data\n\tconst result = await reader.readAll();\n\texpect(result).toEqual(new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9]));\n});\n\ntest(\"Reader stream with partial reads\", async () => {\n\tconst chunks = [new Uint8Array([1, 2]), new Uint8Array([3, 4, 5]), new Uint8Array([6])];\n\n\tconst stream = new ReadableStream({\n\t\tstart(controller) {\n\t\t\tfor (const chunk of chunks) {\n\t\t\t\tcontroller.enqueue(chunk);\n\t\t\t}\n\t\t\tcontroller.close();\n\t\t},\n\t});\n\n\tconst reader = new Reader(stream);\n\n\t// Read specific amounts that cross chunk boundaries\n\tconst first = await reader.read(3); // Should span first two chunks\n\texpect(first).toEqual(new Uint8Array([1, 2, 3]));\n\n\tconst second = await reader.read(2);\n\texpect(second).toEqual(new Uint8Array([4, 5]));\n\n\tconst third = await reader.read(1);\n\texpect(third).toEqual(new Uint8Array([6]));\n\n\texpect(await reader.done()).toBe(true);\n});\n\ntest(\"Reader u53 decodes two-byte stream type prefixes\", async () => {\n\tconst { stream, written } = createTestWritableStream();\n\tconst writer = new Writer(stream);\n\n\tawait writer.u53(0x40);\n\twriter.close();\n\tawait writer.closed;\n\n\tconst reader = new Reader(undefined, concatChunks(written));\n\texpect(await reader.u53()).toBe(0x40);\n\texpect(await reader.done()).toBe(true);\n});\n\n/** A stream reset as a transport delivers one: the peer's code, and nothing else useful. */\nclass Reset extends Error {\n\treadonly source = \"stream\" as const;\n\treadonly streamErrorCode: number;\n\n\tconstructor(code: number) {\n\t\tsuper(\"\");\n\t\tthis.streamErrorCode = code;\n\t}\n}\n\ntest(\"Reader closed rejects with the decoded reset code\", async () => {\n\t// Protocol paths race `closed` against a read, so the two must agree on the error\n\t// shape: whichever wins, the reset code reaches the caller the same way.\n\tconst reader = new Reader(\n\t\tnew ReadableStream<Uint8Array>({\n\t\t\tstart: (controller) => controller.error(new Reset(2)),\n\t\t}),\n\t);\n\n\tconst err = await reader.closed.then(\n\t\t() => undefined,\n\t\t(e: unknown) => e,\n\t);\n\texpect(err).toBeInstanceOf(RemoteError);\n\texpect((err as RemoteError).code).toBe(2);\n});\n\ntest(\"Writer closed rejects with the decoded reset code\", async () => {\n\tconst writer = new Writer(\n\t\tnew WritableStream<Uint8Array>({\n\t\t\tstart: (controller) => controller.error(new Reset(31)),\n\t\t}),\n\t);\n\n\tconst err = await writer.closed.then(\n\t\t() => undefined,\n\t\t(e: unknown) => e,\n\t);\n\texpect(err).toBeInstanceOf(RemoteError);\n\texpect((err as RemoteError).code).toBe(31);\n});\n\ntest(\"closed is stable, so racing it per frame does not allocate\", async () => {\n\tconst reader = new Reader(new ReadableStream<Uint8Array>());\n\texpect(reader.closed).toBe(reader.closed);\n});\n"]}
package/util/error.d.ts DELETED
@@ -1,12 +0,0 @@
1
- export declare function error(err: unknown): Error;
2
- /**
3
- * Format an error into a non-empty, human-readable string for logging.
4
- *
5
- * Safari always leaves `WebTransportError.message` blank, so a bare `err.message`
6
- * degrades to an empty string and the reason is lost. This falls back to the error
7
- * type name and appends the WebTransport `source` and application `streamErrorCode`
8
- * (the code that identifies a RESET_STREAM), so the log line always says something.
9
- */
10
- export declare function reason(err: unknown): string;
11
- export declare function unreachable(value: never): never;
12
- //# sourceMappingURL=error.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../../src/util/error.ts"],"names":[],"mappings":"AACA,wBAAgB,KAAK,CAAC,GAAG,EAAE,OAAO,GAAG,KAAK,CAEzC;AAED;;;;;;;GAOG;AACH,wBAAgB,MAAM,CAAC,GAAG,EAAE,OAAO,GAAG,MAAM,CAa3C;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK,CAE/C"}
package/util/error.js DELETED
@@ -1,30 +0,0 @@
1
- /* @ts-self-types="./error.d.ts" */
2
- // I hate javascript.
3
- export function error(err) {
4
- return err instanceof Error ? err : new Error(String(err));
5
- }
6
- /**
7
- * Format an error into a non-empty, human-readable string for logging.
8
- *
9
- * Safari always leaves `WebTransportError.message` blank, so a bare `err.message`
10
- * degrades to an empty string and the reason is lost. This falls back to the error
11
- * type name and appends the WebTransport `source` and application `streamErrorCode`
12
- * (the code that identifies a RESET_STREAM), so the log line always says something.
13
- */
14
- export function reason(err) {
15
- const e = error(err);
16
- // WebTransportError carries the failure origin and the peer's application error code,
17
- // often the only identifying detail since WebKit leaves `message` empty.
18
- if (typeof WebTransportError !== "undefined" && e instanceof WebTransportError) {
19
- const parts = [`source=${e.source}`];
20
- if (e.streamErrorCode !== null)
21
- parts.push(`code=${e.streamErrorCode}`);
22
- const detail = parts.join(" ");
23
- return e.message ? `${e.message} (${detail})` : `WebTransportError: ${detail}`;
24
- }
25
- return e.message || e.name || "unknown error";
26
- }
27
- export function unreachable(value) {
28
- throw new Error(`unreachable: ${value}`);
29
- }
30
- //# sourceMappingURL=error.js.map
package/util/error.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"error.js","sourceRoot":"","sources":["../../src/util/error.ts"],"names":[],"mappings":"AAAA,qBAAqB;AACrB,MAAM,UAAU,KAAK,CAAC,GAAY;IACjC,OAAO,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;AAC5D,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,MAAM,CAAC,GAAY;IAClC,MAAM,CAAC,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC;IAErB,sFAAsF;IACtF,yEAAyE;IACzE,IAAI,OAAO,iBAAiB,KAAK,WAAW,IAAI,CAAC,YAAY,iBAAiB,EAAE,CAAC;QAChF,MAAM,KAAK,GAAG,CAAC,UAAU,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC;QACrC,IAAI,CAAC,CAAC,eAAe,KAAK,IAAI;YAAE,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,eAAe,EAAE,CAAC,CAAC;QACxE,MAAM,MAAM,GAAG,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAC/B,OAAO,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,OAAO,KAAK,MAAM,GAAG,CAAC,CAAC,CAAC,sBAAsB,MAAM,EAAE,CAAC;IAChF,CAAC;IAED,OAAO,CAAC,CAAC,OAAO,IAAI,CAAC,CAAC,IAAI,IAAI,eAAe,CAAC;AAC/C,CAAC;AAED,MAAM,UAAU,WAAW,CAAC,KAAY;IACvC,MAAM,IAAI,KAAK,CAAC,gBAAgB,KAAK,EAAE,CAAC,CAAC;AAC1C,CAAC","sourcesContent":["// I hate javascript.\nexport function error(err: unknown): Error {\n\treturn err instanceof Error ? err : new Error(String(err));\n}\n\n/**\n * Format an error into a non-empty, human-readable string for logging.\n *\n * Safari always leaves `WebTransportError.message` blank, so a bare `err.message`\n * degrades to an empty string and the reason is lost. This falls back to the error\n * type name and appends the WebTransport `source` and application `streamErrorCode`\n * (the code that identifies a RESET_STREAM), so the log line always says something.\n */\nexport function reason(err: unknown): string {\n\tconst e = error(err);\n\n\t// WebTransportError carries the failure origin and the peer's application error code,\n\t// often the only identifying detail since WebKit leaves `message` empty.\n\tif (typeof WebTransportError !== \"undefined\" && e instanceof WebTransportError) {\n\t\tconst parts = [`source=${e.source}`];\n\t\tif (e.streamErrorCode !== null) parts.push(`code=${e.streamErrorCode}`);\n\t\tconst detail = parts.join(\" \");\n\t\treturn e.message ? `${e.message} (${detail})` : `WebTransportError: ${detail}`;\n\t}\n\n\treturn e.message || e.name || \"unknown error\";\n}\n\nexport function unreachable(value: never): never {\n\tthrow new Error(`unreachable: ${value}`);\n}\n"]}
@@ -1,45 +0,0 @@
1
- /* @ts-self-types="./error.test.d.ts" */
2
- import { afterEach, beforeEach, expect, test } from "bun:test";
3
- import { reason } from "./error.js";
4
- test("reason: plain Error keeps its message", () => {
5
- expect(reason(new Error("boom"))).toBe("boom");
6
- });
7
- test("reason: non-Error value is stringified", () => {
8
- expect(reason("nope")).toBe("nope");
9
- });
10
- test("reason: empty message falls back to the type name", () => {
11
- // Safari's WebTransportError.message is always blank; the reason must never be empty.
12
- expect(reason(new Error(""))).toBe("Error");
13
- });
14
- // Minimal stand-in for the DOM WebTransportError, which the test runtime may not define.
15
- class FakeWebTransportError extends Error {
16
- source;
17
- streamErrorCode;
18
- constructor(source, streamErrorCode, message = "") {
19
- super(message);
20
- this.name = "WebTransportError";
21
- this.source = source;
22
- this.streamErrorCode = streamErrorCode;
23
- }
24
- }
25
- const globals = globalThis;
26
- let originalWebTransportError;
27
- beforeEach(() => {
28
- // Force `instanceof WebTransportError` in reason() to match our stand-in.
29
- originalWebTransportError = globals.WebTransportError;
30
- globals.WebTransportError = FakeWebTransportError;
31
- });
32
- afterEach(() => {
33
- globals.WebTransportError = originalWebTransportError;
34
- });
35
- test("reason: WebTransportError with a blank message surfaces source and code", () => {
36
- // The Safari case from the bug report: a RESET_STREAM with no message.
37
- expect(reason(new FakeWebTransportError("stream", 0))).toBe("WebTransportError: source=stream code=0");
38
- });
39
- test("reason: WebTransportError omits a null stream error code", () => {
40
- expect(reason(new FakeWebTransportError("session", null))).toBe("WebTransportError: source=session");
41
- });
42
- test("reason: WebTransportError keeps a populated message and appends details", () => {
43
- expect(reason(new FakeWebTransportError("stream", 42, "Received RESET_STREAM."))).toBe("Received RESET_STREAM. (source=stream code=42)");
44
- });
45
- //# sourceMappingURL=error.test.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"error.test.js","sourceRoot":"","sources":["../../src/util/error.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AAC/D,OAAO,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAEpC,IAAI,CAAC,uCAAuC,EAAE,GAAG,EAAE;IAClD,MAAM,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;AAChD,CAAC,CAAC,CAAC;AAEH,IAAI,CAAC,wCAAwC,EAAE,GAAG,EAAE;IACnD,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;AACrC,CAAC,CAAC,CAAC;AAEH,IAAI,CAAC,mDAAmD,EAAE,GAAG,EAAE;IAC9D,sFAAsF;IACtF,MAAM,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;AAC7C,CAAC,CAAC,CAAC;AAEH,yFAAyF;AACzF,MAAM,qBAAsB,SAAQ,KAAK;IAC/B,MAAM,CAAS;IACf,eAAe,CAAgB;IAExC,YAAY,MAAc,EAAE,eAA8B,EAAE,OAAO,GAAG,EAAE;QACvE,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;QAChC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,eAAe,GAAG,eAAe,CAAC;IACxC,CAAC;CACD;AAED,MAAM,OAAO,GAAG,UAA6C,CAAC;AAC9D,IAAI,yBAAkC,CAAC;AAEvC,UAAU,CAAC,GAAG,EAAE;IACf,0EAA0E;IAC1E,yBAAyB,GAAG,OAAO,CAAC,iBAAiB,CAAC;IACtD,OAAO,CAAC,iBAAiB,GAAG,qBAAqB,CAAC;AACnD,CAAC,CAAC,CAAC;AAEH,SAAS,CAAC,GAAG,EAAE;IACd,OAAO,CAAC,iBAAiB,GAAG,yBAAyB,CAAC;AACvD,CAAC,CAAC,CAAC;AAEH,IAAI,CAAC,yEAAyE,EAAE,GAAG,EAAE;IACpF,uEAAuE;IACvE,MAAM,CAAC,MAAM,CAAC,IAAI,qBAAqB,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,yCAAyC,CAAC,CAAC;AACxG,CAAC,CAAC,CAAC;AAEH,IAAI,CAAC,0DAA0D,EAAE,GAAG,EAAE;IACrE,MAAM,CAAC,MAAM,CAAC,IAAI,qBAAqB,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,mCAAmC,CAAC,CAAC;AACtG,CAAC,CAAC,CAAC;AAEH,IAAI,CAAC,yEAAyE,EAAE,GAAG,EAAE;IACpF,MAAM,CAAC,MAAM,CAAC,IAAI,qBAAqB,CAAC,QAAQ,EAAE,EAAE,EAAE,wBAAwB,CAAC,CAAC,CAAC,CAAC,IAAI,CACrF,gDAAgD,CAChD,CAAC;AACH,CAAC,CAAC,CAAC","sourcesContent":["import { afterEach, beforeEach, expect, test } from \"bun:test\";\nimport { reason } from \"./error.ts\";\n\ntest(\"reason: plain Error keeps its message\", () => {\n\texpect(reason(new Error(\"boom\"))).toBe(\"boom\");\n});\n\ntest(\"reason: non-Error value is stringified\", () => {\n\texpect(reason(\"nope\")).toBe(\"nope\");\n});\n\ntest(\"reason: empty message falls back to the type name\", () => {\n\t// Safari's WebTransportError.message is always blank; the reason must never be empty.\n\texpect(reason(new Error(\"\"))).toBe(\"Error\");\n});\n\n// Minimal stand-in for the DOM WebTransportError, which the test runtime may not define.\nclass FakeWebTransportError extends Error {\n\treadonly source: string;\n\treadonly streamErrorCode: number | null;\n\n\tconstructor(source: string, streamErrorCode: number | null, message = \"\") {\n\t\tsuper(message);\n\t\tthis.name = \"WebTransportError\";\n\t\tthis.source = source;\n\t\tthis.streamErrorCode = streamErrorCode;\n\t}\n}\n\nconst globals = globalThis as { WebTransportError?: unknown };\nlet originalWebTransportError: unknown;\n\nbeforeEach(() => {\n\t// Force `instanceof WebTransportError` in reason() to match our stand-in.\n\toriginalWebTransportError = globals.WebTransportError;\n\tglobals.WebTransportError = FakeWebTransportError;\n});\n\nafterEach(() => {\n\tglobals.WebTransportError = originalWebTransportError;\n});\n\ntest(\"reason: WebTransportError with a blank message surfaces source and code\", () => {\n\t// The Safari case from the bug report: a RESET_STREAM with no message.\n\texpect(reason(new FakeWebTransportError(\"stream\", 0))).toBe(\"WebTransportError: source=stream code=0\");\n});\n\ntest(\"reason: WebTransportError omits a null stream error code\", () => {\n\texpect(reason(new FakeWebTransportError(\"session\", null))).toBe(\"WebTransportError: source=session\");\n});\n\ntest(\"reason: WebTransportError keeps a populated message and appends details\", () => {\n\texpect(reason(new FakeWebTransportError(\"stream\", 42, \"Received RESET_STREAM.\"))).toBe(\n\t\t\"Received RESET_STREAM. (source=stream code=42)\",\n\t);\n});\n"]}
package/util/index.d.ts DELETED
@@ -1,3 +0,0 @@
1
- export * from "./error";
2
- export * from "./timeout";
3
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/util/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC"}
package/util/index.js DELETED
@@ -1,4 +0,0 @@
1
- /* @ts-self-types="./index.d.ts" */
2
- export * from "./error.js";
3
- export * from "./timeout.js";
4
- //# sourceMappingURL=index.js.map
package/util/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/util/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC","sourcesContent":["export * from \"./error.ts\";\nexport * from \"./timeout.ts\";\n"]}
File without changes