@norskvideo/moq-net 0.1.8 → 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/README.md +2 -2
- package/announce.d.ts +7 -0
- package/announce.d.ts.map +1 -0
- package/announce.js +8 -0
- package/announce.js.map +1 -0
- package/announced.d.ts +49 -91
- package/announced.d.ts.map +1 -1
- package/announced.js +21 -156
- package/announced.js.map +1 -1
- package/bandwidth.d.ts +163 -0
- package/bandwidth.d.ts.map +1 -0
- package/bandwidth.js +304 -0
- package/bandwidth.js.map +1 -0
- package/bandwidth_api.d.ts +7 -0
- package/bandwidth_api.d.ts.map +1 -0
- package/bandwidth_api.js +8 -0
- package/bandwidth_api.js.map +1 -0
- package/broadcast.d.ts +44 -35
- package/broadcast.d.ts.map +1 -1
- package/broadcast.js +104 -60
- package/broadcast.js.map +1 -1
- package/connection/accept.d.ts +16 -1
- package/connection/accept.d.ts.map +1 -1
- package/connection/accept.js +52 -28
- package/connection/accept.js.map +1 -1
- package/connection/browser.d.ts.map +1 -1
- package/connection/browser.js +9 -7
- package/connection/browser.js.map +1 -1
- package/connection/connect.d.ts +30 -6
- package/connection/connect.d.ts.map +1 -1
- package/connection/connect.js +110 -54
- package/connection/connect.js.map +1 -1
- package/connection/established.d.ts +17 -21
- package/connection/established.d.ts.map +1 -1
- package/connection/established.js.map +1 -1
- package/connection/forward.d.ts +2 -0
- package/connection/forward.d.ts.map +1 -0
- package/connection/forward.js +173 -0
- package/connection/forward.js.map +1 -0
- package/connection/handshake.d.ts +1 -0
- package/connection/handshake.d.ts.map +1 -1
- package/connection/handshake.js +5 -2
- package/connection/handshake.js.map +1 -1
- package/connection/index.d.ts +5 -5
- package/connection/index.d.ts.map +1 -1
- package/connection/index.js +4 -5
- package/connection/index.js.map +1 -1
- package/connection/pool.d.ts +186 -0
- package/connection/pool.d.ts.map +1 -0
- package/connection/pool.js +361 -0
- package/connection/pool.js.map +1 -0
- package/connection/reload.d.ts +14 -93
- package/connection/reload.d.ts.map +1 -1
- package/connection/reload.js +217 -81
- package/connection/reload.js.map +1 -1
- package/connection/stats.d.ts +3 -26
- package/connection/stats.d.ts.map +1 -1
- package/connection/stats.js.map +1 -1
- package/connection/transport.d.ts +0 -7
- package/connection/transport.d.ts.map +1 -1
- package/consume.d.ts +1 -43
- package/consume.d.ts.map +1 -1
- package/consume.js +1 -1
- package/consume.js.map +1 -1
- package/error.d.ts +180 -29
- package/error.d.ts.map +1 -1
- package/error.js +331 -16
- package/error.js.map +1 -1
- package/errors.d.ts +7 -0
- package/errors.d.ts.map +1 -0
- package/errors.js +8 -0
- package/errors.js.map +1 -0
- package/group.d.ts +9 -43
- package/group.d.ts.map +1 -1
- package/group.js +284 -69
- package/group.js.map +1 -1
- package/hop.d.ts +115 -0
- package/hop.d.ts.map +1 -0
- package/hop.js +119 -0
- package/hop.js.map +1 -0
- package/ietf/adapter.d.ts +5 -1
- package/ietf/adapter.d.ts.map +1 -1
- package/ietf/adapter.js +105 -60
- package/ietf/adapter.js.map +1 -1
- package/ietf/aliases.d.ts +1 -78
- package/ietf/aliases.d.ts.map +1 -1
- package/ietf/cluster.d.ts +9 -123
- package/ietf/cluster.d.ts.map +1 -1
- package/ietf/cluster.js +84 -44
- package/ietf/cluster.js.map +1 -1
- package/ietf/connection.d.ts +6 -72
- package/ietf/connection.d.ts.map +1 -1
- package/ietf/connection.js +62 -56
- package/ietf/connection.js.map +1 -1
- package/ietf/error.d.ts +11 -0
- package/ietf/error.d.ts.map +1 -0
- package/ietf/error.js +193 -0
- package/ietf/error.js.map +1 -0
- package/ietf/fetch.d.ts +7 -20
- package/ietf/fetch.d.ts.map +1 -1
- package/ietf/fetch.js +52 -22
- package/ietf/fetch.js.map +1 -1
- package/ietf/filter.d.ts +2 -0
- package/ietf/filter.d.ts.map +1 -1
- package/ietf/filter.js +10 -1
- package/ietf/filter.js.map +1 -1
- package/ietf/goaway.d.ts.map +1 -1
- package/ietf/goaway.js +22 -5
- package/ietf/goaway.js.map +1 -1
- package/ietf/hidden.d.ts +2 -0
- package/ietf/hidden.d.ts.map +1 -0
- package/ietf/hidden.js +30 -0
- package/ietf/hidden.js.map +1 -0
- package/ietf/index.d.ts +2 -0
- package/ietf/index.d.ts.map +1 -1
- package/ietf/index.js +2 -0
- package/ietf/index.js.map +1 -1
- package/ietf/object.d.ts +15 -8
- package/ietf/object.d.ts.map +1 -1
- package/ietf/object.js +51 -36
- package/ietf/object.js.map +1 -1
- package/ietf/parameters.d.ts +14 -2
- package/ietf/parameters.d.ts.map +1 -1
- package/ietf/parameters.js +97 -29
- package/ietf/parameters.js.map +1 -1
- package/ietf/properties.d.ts +1 -0
- package/ietf/properties.d.ts.map +1 -1
- package/ietf/properties.js +14 -0
- package/ietf/properties.js.map +1 -1
- package/ietf/publish.d.ts +19 -2
- package/ietf/publish.d.ts.map +1 -1
- package/ietf/publish.js +40 -6
- package/ietf/publish.js.map +1 -1
- package/ietf/publish_namespace.d.ts +25 -0
- package/ietf/publish_namespace.d.ts.map +1 -1
- package/ietf/publish_namespace.js +63 -0
- package/ietf/publish_namespace.js.map +1 -1
- package/ietf/publisher.d.ts +1 -82
- package/ietf/publisher.d.ts.map +1 -1
- package/ietf/publisher.js +483 -235
- package/ietf/publisher.js.map +1 -1
- package/ietf/solicit.d.ts +1 -40
- package/ietf/solicit.d.ts.map +1 -1
- package/ietf/subscribe.d.ts +8 -6
- package/ietf/subscribe.d.ts.map +1 -1
- package/ietf/subscribe.js +33 -27
- package/ietf/subscribe.js.map +1 -1
- package/ietf/subscribe_namespace.d.ts +8 -2
- package/ietf/subscribe_namespace.d.ts.map +1 -1
- package/ietf/subscribe_namespace.js +18 -8
- package/ietf/subscribe_namespace.js.map +1 -1
- package/ietf/subscriber.d.ts +1 -65
- package/ietf/subscriber.d.ts.map +1 -1
- package/ietf/subscriber.js +337 -123
- package/ietf/subscriber.js.map +1 -1
- package/ietf/token.d.ts +2 -0
- package/ietf/token.d.ts.map +1 -0
- package/ietf/token.js +99 -0
- package/ietf/token.js.map +1 -0
- package/ietf/track.d.ts +4 -0
- package/ietf/track.d.ts.map +1 -1
- package/ietf/track.js +6 -20
- package/ietf/track.js.map +1 -1
- package/ietf/version.d.ts +12 -1
- package/ietf/version.d.ts.map +1 -1
- package/ietf/version.js +13 -0
- package/ietf/version.js.map +1 -1
- package/index.d.ts +12 -7
- package/index.d.ts.map +1 -1
- package/index.js +10 -5
- package/index.js.map +1 -1
- package/internal.d.ts +115 -1
- package/internal.d.ts.map +1 -1
- package/internal.js +108 -0
- package/internal.js.map +1 -1
- package/lite/announce.d.ts +60 -10
- package/lite/announce.d.ts.map +1 -1
- package/lite/announce.js +176 -31
- package/lite/announce.js.map +1 -1
- package/lite/connection.d.ts +9 -59
- package/lite/connection.d.ts.map +1 -1
- package/lite/connection.js +34 -35
- package/lite/connection.js.map +1 -1
- package/lite/datagram.d.ts +3 -2
- package/lite/datagram.d.ts.map +1 -1
- package/lite/datagram.js +7 -8
- package/lite/datagram.js.map +1 -1
- package/lite/fetch.d.ts +15 -1
- package/lite/fetch.d.ts.map +1 -1
- package/lite/fetch.js +39 -7
- package/lite/fetch.js.map +1 -1
- package/lite/goaway.d.ts.map +1 -1
- package/lite/goaway.js +7 -1
- package/lite/goaway.js.map +1 -1
- package/lite/group.d.ts +30 -12
- package/lite/group.d.ts.map +1 -1
- package/lite/group.js +68 -26
- package/lite/group.js.map +1 -1
- package/lite/message.d.ts +2 -2
- package/lite/message.d.ts.map +1 -1
- package/lite/message.js +14 -5
- package/lite/message.js.map +1 -1
- package/lite/priority.d.ts +1 -61
- package/lite/priority.d.ts.map +1 -1
- package/lite/priority.js +4 -5
- package/lite/priority.js.map +1 -1
- package/lite/publisher.d.ts +1 -69
- package/lite/publisher.d.ts.map +1 -1
- package/lite/publisher.js +630 -262
- package/lite/publisher.js.map +1 -1
- package/lite/setup.d.ts +8 -8
- package/lite/setup.d.ts.map +1 -1
- package/lite/setup.js +33 -30
- package/lite/setup.js.map +1 -1
- package/lite/subscribe.d.ts +71 -17
- package/lite/subscribe.d.ts.map +1 -1
- package/lite/subscribe.js +205 -53
- package/lite/subscribe.js.map +1 -1
- package/lite/subscriber.d.ts +15 -56
- package/lite/subscriber.d.ts.map +1 -1
- package/lite/subscriber.js +451 -247
- package/lite/subscriber.js.map +1 -1
- package/lite/track.d.ts +4 -10
- package/lite/track.d.ts.map +1 -1
- package/lite/track.js +34 -29
- package/lite/track.js.map +1 -1
- package/lite/version.d.ts +42 -6
- package/lite/version.d.ts.map +1 -1
- package/lite/version.js +126 -10
- package/lite/version.js.map +1 -1
- package/origin.d.ts +256 -29
- package/origin.d.ts.map +1 -1
- package/origin.js +1427 -37
- package/origin.js.map +1 -1
- package/package.json +8 -3
- package/path.d.ts +25 -7
- package/path.d.ts.map +1 -1
- package/path.js +5 -3
- package/path.js.map +1 -1
- package/stream.d.ts +73 -14
- package/stream.d.ts.map +1 -1
- package/stream.js +372 -141
- package/stream.js.map +1 -1
- package/tail.d.ts +18 -0
- package/tail.d.ts.map +1 -0
- package/tail.js +167 -0
- package/tail.js.map +1 -0
- package/time.d.ts +15 -2
- package/time.d.ts.map +1 -1
- package/time.js +28 -9
- package/time.js.map +1 -1
- package/track.d.ts +211 -83
- package/track.d.ts.map +1 -1
- package/track.js +816 -205
- package/track.js.map +1 -1
- package/util/abort.d.ts +2 -0
- package/util/abort.d.ts.map +1 -0
- package/util/abort.js +20 -0
- package/util/abort.js.map +1 -0
- package/util/log.d.ts +5 -0
- package/util/log.d.ts.map +1 -0
- package/util/log.js +17 -0
- package/util/log.js.map +1 -0
- package/util/u64.d.ts +39 -0
- package/util/u64.d.ts.map +1 -0
- package/util/u64.js +83 -0
- package/util/u64.js.map +1 -0
- package/util/varint.d.ts +29 -0
- package/util/varint.d.ts.map +1 -0
- package/util/varint.js +198 -0
- package/util/varint.js.map +1 -0
- package/varint.d.ts +10 -6
- package/varint.d.ts.map +1 -1
- package/varint.js +40 -237
- package/varint.js.map +1 -1
- package/wire.d.ts +80 -0
- package/wire.d.ts.map +1 -0
- package/wire.js +32 -0
- package/wire.js.map +1 -0
- package/zod.d.ts +1 -1
- package/zod.d.ts.map +1 -1
- package/zod.js.map +1 -1
- package/mock.d.ts +0 -66
- package/mock.d.ts.map +0 -1
- package/mock.js +0 -243
- package/mock.js.map +0 -1
package/stream.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAC9D,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,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;;;;;;GAMG;AACH,MAAM,UAAU,WAAW,CAAC,OAAqB;IAChD,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,SAAS,EAAE,kBAAkB,EAAE,OAAO,EAAE,kBAAkB,IAAI,IAAI,EAAE,CAAC;AACnG,CAAC;AAED,wFAAwF;AACxF,wFAAwF;AACxF,yFAAyF;AACzF,gCAAgC;AAChC,MAAM,eAAe,GAAG,MAAM,CAAC;AAE/B;;;;GAIG;AACH,KAAK,UAAU,UAAU,CAAI,OAAmB,EAAE,OAAe,EAAE,OAA4B;IAC9F,IAAI,CAAC;QACJ,OAAO,MAAM,WAAW,CAAC,OAAO,EAAE,OAAO,EAAE,+BAA+B,OAAO,uBAAuB,CAAC,CAAC;IAC3G,CAAC;IAAC,OAAO,GAAY,EAAE,CAAC;QACvB,OAAO,CAAC,IAAI,C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CAAC,CAAC;IACrC,IAAI,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IACrB,OAAO,IAAI,UAAU,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;AACtE,CAAC;AAED,SAAS,QAAQ,CAAC,GAAgB,EAAE,CAAS;IAC5C,MAAM,IAAI,GAAG,IAAI,QAAQ,CAAC,GAAG,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;IACrC,IAAI,CAAC,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IACpB,OAAO,IAAI,UAAU,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;AACtE,CAAC;AAED,8CAA8C;AAC9C,MAAM,OAAO,OAAO;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 { fromTransport } from \"./error.ts\";\nimport type { IetfVersion } from \"./ietf/version.ts\";\nimport { Version } from \"./ietf/version.ts\";\nimport { TimeoutError, withTimeout } from \"./util/timeout.ts\";\nimport { decodeUtf8 } from \"./util/utf8.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\n/**\n * Options handed to the transport for one outgoing stream.\n *\n * `waitUntilAvailable` waits for the peer's concurrent stream limit to free a slot instead\n * of rejecting with a `QuotaExceededError`, which is what we want for the streams a session\n * opens occasionally. `@types/web` doesn't declare it yet, hence the intersection.\n */\nexport function sendOptions(options?: OpenOptions): WebTransportSendStreamOptions & { waitUntilAvailable?: boolean } {\n\treturn { sendOrder: options?.sendOrder, waitUntilAvailable: options?.waitUntilAvailable ?? true };\n}\n\n// How long any open may wait for the peer to free a stream slot. Every open needs this,\n// whether or not it asked to wait: an implementation may park an over-limit open rather\n// than rejecting it, and a peer can advertise a stream limit of zero and never raise it.\n// Matches the subscribe budget.\nconst OPEN_TIMEOUT_MS = 10_000;\n\n/**\n * Bound an open that would otherwise park until the peer grants stream credit, restoring\n * the pre-waitUntilAvailable failure after a grace period. A stream arriving after the\n * deadline is discarded, since nobody is waiting for it any more.\n */\nasync function openWithin<T>(opening: Promise<T>, timeout: number, discard: (stream: T) => void): Promise<T> {\n\ttry {\n\t\treturn await withTimeout(opening, timeout, `stream open timed out after ${timeout}ms waiting for a slot`);\n\t} catch (err: unknown) {\n\t\topening.then(discard).catch(() => void 0);\n\t\tthrow err;\n\t}\n}\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\n/**\n * The `WebTransportSendStream` that every outgoing stream is, narrowed to the one attribute\n * this package uses. The DOM types available here still describe them as plain\n * `WritableStream`s, so the interface has to be named structurally.\n *\n * `sendOrder` is optional because it is absent until set, and stays absent on an\n * implementation that doesn't carry the interface at all.\n *\n * @see https://www.w3.org/TR/webtransport/#webtransportsendstream\n */\nexport type SendStream = WritableStream<Uint8Array> & { sendOrder?: number };\n\n/** Options for opening an outgoing stream. */\nexport interface OpenOptions {\n\t/** The negotiated IETF version, which selects the varint encoding. */\n\tversion?: IetfVersion;\n\n\t/**\n\t * The transport send order, where HIGHER values are transmitted first.\n\t * Left to the transport's default when unset.\n\t */\n\tsendOrder?: number;\n\n\t/**\n\t * Reject if the peer hasn't freed a stream slot within this many milliseconds. Defaults\n\t * to 10s. There is no way to wait indefinitely: a peer can advertise a stream limit of\n\t * zero and never raise it, so every open needs a way out.\n\t */\n\ttimeout?: number;\n\n\t/**\n\t * Ask the transport to wait for a stream slot rather than failing when the peer's\n\t * concurrent stream limit is exhausted. Defaults to true.\n\t *\n\t * Set it false on a path that opens streams faster than the peer can retire them. The\n\t * transport queues the opens it can't satisfy and serves them in order, with no way to\n\t * cancel one, so waiting there spends returning credit on whatever was requested first\n\t * rather than on what matters now.\n\t */\n\twaitUntilAvailable?: boolean;\n}\n\n/** Options for {@link Writer.tryOpen}. */\nexport interface TryOpenOptions extends OpenOptions {\n\t/** Give up once this settles, however it settles. */\n\tcancel: Promise<unknown>;\n}\n\nexport class Stream {\n\treader: Reader;\n\twriter: Writer;\n\n\t/** Wrap the two halves of a transport stream. */\n\tconstructor(props: {\n\t\twritable: WritableStream<Uint8Array>;\n\t\treadable: ReadableStream<Uint8Array>;\n\t\tversion?: IetfVersion;\n\t});\n\t/** Pair halves that were opened separately, as the SETUP exchange does. */\n\tconstructor(props: { writer: Writer; reader: Reader });\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\tconst writer = props.writer ?? (props.writable && new Writer(props.writable, props.version));\n\t\tconst reader = props.reader ?? (props.readable && new Reader(props.readable, undefined, props.version));\n\t\tif (!writer || !reader) throw new Error(\"stream needs both halves\");\n\n\t\tthis.writer = writer;\n\t\tthis.reader = reader;\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\t/**\n\t * Open an outgoing bidirectional stream.\n\t * @param quic - The session to open it on\n\t * @param options - The version its varints encode with, and the send order ranking it\n\t * against the session's other streams\n\t */\n\tstatic async open(quic: WebTransport, options?: OpenOptions): Promise<Stream> {\n\t\tconst { readable, writable } = await openWithin(\n\t\t\tquic.createBidirectionalStream(sendOptions(options)),\n\t\t\toptions?.timeout ?? OPEN_TIMEOUT_MS,\n\t\t\t(stream) => {\n\t\t\t\tvoid stream.writable.abort().catch(() => void 0);\n\t\t\t\tvoid stream.readable.cancel().catch(() => void 0);\n\t\t\t},\n\t\t);\n\t\treturn new Stream({ readable, writable, version: options?.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 decodeUtf8(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\tasync u53(): Promise<number> {\n\t\tconst v = await this.u62();\n\t\tif (v > Varint.MAX_U53) {\n\t\t\tthrow new Error(`value larger than 53-bits: ${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\t/**\n\t * Rank this stream against the session's others, where HIGHER values are sent first.\n\t *\n\t * A send order only schedules the local end, so a stream the peer opened has to be ranked\n\t * here rather than at the peer's {@link open}.\n\t *\n\t * The spec makes `sendOrder` a settable attribute on every {@link SendStream}. Where the\n\t * interface isn't implemented (Chrome as of writing, a mock, a polyfill) this just sets an\n\t * ignored property, the same way an ignored `sendOrder` option does at {@link open}.\n\t */\n\tsetPriority(sendOrder: number) {\n\t\t(this.#stream as SendStream).sendOrder = sendOrder;\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\t/**\n\t * Open an outgoing unidirectional stream.\n\t * @param quic - The session to open it on\n\t * @param options - The version its varints encode with, and the send order ranking it\n\t * against the session's other streams\n\t */\n\tstatic async open(quic: WebTransport, options?: OpenOptions): Promise<Writer> {\n\t\tconst writable = await openWithin(\n\t\t\tquic.createUnidirectionalStream(sendOptions(options)) as Promise<WritableStream<Uint8Array>>,\n\t\t\toptions?.timeout ?? OPEN_TIMEOUT_MS,\n\t\t\t(stream) => void stream.abort().catch(() => void 0),\n\t\t);\n\n\t\treturn new Writer(writable, options?.version);\n\t}\n\n\t/**\n\t * Like {@link Writer.open}, but gives up when `cancel` settles or `timeout` elapses,\n\t * returning undefined so the caller can drop whatever it meant to send. A stream that\n\t * opens after that is reset rather than leaked. A real transport failure still throws.\n\t *\n\t * Worth using even with `waitUntilAvailable: false`, since an implementation may park\n\t * an over-limit open instead of rejecting it.\n\t */\n\tstatic async tryOpen(quic: WebTransport, options: TryOpenOptions): Promise<Writer | undefined> {\n\t\t// A rejected `cancel` (STOP_SENDING) means the peer is gone too, so both settle\n\t\t// paths mean \"give up\" and neither is left unhandled. Built before the open, and\n\t\t// raced ahead of it, so an already-cancelled caller wins even against a slot that\n\t\t// is free right now.\n\t\tconst cancelled = options.cancel.then(\n\t\t\t() => undefined,\n\t\t\t() => undefined,\n\t\t);\n\t\tconst open = Writer.open(quic, options);\n\n\t\ttry {\n\t\t\tconst stream = await Promise.race([cancelled, open]);\n\t\t\tif (stream) return stream;\n\t\t} catch (err: unknown) {\n\t\t\t// open already discarded the late stream on its way out.\n\t\t\tif (!(err instanceof TimeoutError)) throw err;\n\t\t\treturn undefined;\n\t\t}\n\n\t\tconst abandoned = new Error(\"abandoned waiting for a stream slot\");\n\t\topen.then((w) => w.reset(abandoned)).catch(() => void 0);\n\t\treturn undefined;\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,IAAI,EAAE,MAAM,cAAc,CAAC;AACpC,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,WAAW,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE/F,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,OAAO,EAAE,OAAO,IAAI,IAAI,EAA+B,MAAM,mBAAmB,CAAC;AACjF,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAC9D,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,QAAQ,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAC/D,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAC5C,OAAO,EACN,iBAAiB,EACjB,UAAU,EACV,KAAK,EACL,eAAe,EACf,QAAQ,EACR,eAAe,EACf,QAAQ,EACR,KAAK,EACL,gBAA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AS,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 { race } from \"@moq/signals\";\nimport { fromTransport, StreamCode, StreamError, toStreamCode, toTransport } from \"./error.ts\";\nimport type { IetfVersion } from \"./ietf/version.ts\";\nimport { Version } from \"./ietf/version.ts\";\nimport { Version as Lite, type Version as LiteVersion } from \"./lite/version.ts\";\nimport { TimeoutError, withTimeout } from \"./util/timeout.ts\";\nimport { POW32, toBigInt, toNumber, U64 } from \"./util/u64.ts\";\nimport { decodeUtf8 } from \"./util/utf8.ts\";\nimport {\n\tlengthLeadingOnes,\n\tlengthQuic,\n\tparts,\n\tpeekLeadingOnes,\n\tpeekQuic,\n\treadLeadingOnes,\n\treadQuic,\n\tsplit,\n\twriteLeadingOnes,\n\twriteQuic,\n} from \"./util/varint.ts\";\n\n// Decode raw transport errors before mapping so they cannot bypass the negotiated\n// registry. Ordinary errors already send 0 and retain their local identity.\nfunction withCode(reason: unknown, stream: StreamVersion): unknown {\n\tconst version = asIetf(stream);\n\tconst decoded = fromTransport(reason, { version });\n\tconst code = toStreamCode(decoded, { version });\n\treturn code === StreamCode.Internal && decoded === reason ? reason : toTransport(code, decoded.message);\n}\n\nconst MAX_U31 = 2 ** 31 - 1;\nconst MAX_READ_SIZE = 1024 * 1024 * 64; // don't allocate more than 64MB for a message\n\n/**\n * Options handed to the transport for one outgoing stream.\n *\n * `waitUntilAvailable` waits for the peer's concurrent stream limit to free a slot instead\n * of rejecting with a `QuotaExceededError`, which is what we want for the streams a session\n * opens occasionally. `@types/web` doesn't declare it yet, hence the intersection.\n */\nexport function sendOptions(options?: OpenOptions): WebTransportSendStreamOptions & { waitUntilAvailable?: boolean } {\n\treturn { sendOrder: options?.sendOrder, waitUntilAvailable: options?.waitUntilAvailable ?? true };\n}\n\n// How long any open may wait for the peer to free a stream slot. Every open needs this,\n// whether or not it asked to wait: an implementation may park an over-limit open rather\n// than rejecting it, and a peer can advertise a stream limit of zero and never raise it.\n// Matches the subscribe budget.\nconst OPEN_TIMEOUT_MS = 10_000;\n\n/**\n * Bound an open that would otherwise park until the peer grants stream credit, restoring\n * the pre-waitUntilAvailable failure after a grace period. A stream arriving after the\n * deadline is discarded, since nobody is waiting for it any more.\n */\nasync function openWithin<T>(opening: Promise<T>, timeout: number, discard: (stream: T) => void): Promise<T> {\n\ttry {\n\t\treturn await withTimeout(opening, timeout, `stream open timed out after ${timeout}ms waiting for a slot`);\n\t} catch (err: unknown) {\n\t\topening.then(discard).catch(() => void 0);\n\t\tthrow err;\n\t}\n}\n\n/**\n * The version a stream's bytes follow: a moq-transport draft or a moq-lite draft. Required on\n * every stream so the varint form never defaults silently; a stream opened before negotiation\n * names the version its handshake is encoded with.\n */\nexport type StreamVersion = IetfVersion | LiteVersion;\n\nconst LITE: ReadonlySet<number> = new Set(Object.values(Lite));\n\nfunction isLite(version: StreamVersion): version is LiteVersion {\n\treturn LITE.has(version);\n}\n\n/** The moq-transport draft a stream follows, or undefined on moq-lite, whose stream codes are the same on every draft. */\nexport function asIetf(version: StreamVersion): IetfVersion | undefined {\n\treturn isLite(version) ? undefined : version;\n}\n\n// Every draft newer than these counts leading ones, so a new version falls forward.\nfunction isLeadingOnes(version: StreamVersion): boolean {\n\tswitch (version) {\n\t\tcase Version.DRAFT_14:\n\t\tcase Version.DRAFT_15:\n\t\tcase Version.DRAFT_16:\n\t\tcase Lite.DRAFT_01:\n\t\tcase Lite.DRAFT_02:\n\t\tcase Lite.DRAFT_03:\n\t\tcase Lite.DRAFT_04:\n\t\tcase Lite.DRAFT_05:\n\t\tcase Lite.DRAFT_06:\n\t\t\treturn false;\n\t\tdefault:\n\t\t\treturn true;\n\t}\n}\n\n// Encode `hi`/`lo` into `dst` in the varint form `version` uses: QUIC up to 2^62-1, leading-ones up to 2^64-1.\nfunction encodeTo(dst: ArrayBuffer, hi: number, lo: number, version: StreamVersion): Uint8Array {\n\tlet buf: Uint8Array;\n\tif (isLeadingOnes(version)) {\n\t\tbuf = new Uint8Array(dst, 0, lengthLeadingOnes(hi, lo));\n\t\twriteLeadingOnes(buf, hi, lo, buf.length);\n\t} else {\n\t\tbuf = new Uint8Array(dst, 0, lengthQuic(hi, lo));\n\t\twriteQuic(buf, hi, lo, buf.length);\n\t}\n\treturn buf;\n}\n\n/** Encode one varint in the form `version` uses, for a body written outside a {@link Writer}. */\nexport function encodeVarint(v: number | bigint, version: StreamVersion): Uint8Array {\n\tconst lo = split(v);\n\treturn encodeTo(new ArrayBuffer(9), parts.hi, lo, version);\n}\n\n/**\n * The `WebTransportSendStream` that every outgoing stream is, narrowed to the one attribute\n * this package uses. The DOM types available here still describe them as plain\n * `WritableStream`s, so the interface has to be named structurally.\n *\n * `sendOrder` is optional because it is absent until set, and stays absent on an\n * implementation that doesn't carry the interface at all.\n *\n * @see https://www.w3.org/TR/webtransport/#webtransportsendstream\n */\nexport type SendStream = WritableStream<Uint8Array> & { sendOrder?: number };\n\n/** Options for opening an outgoing stream. */\nexport interface OpenOptions {\n\t/** The negotiated version, which selects the varint encoding. */\n\tversion: StreamVersion;\n\n\t/**\n\t * The transport send order, where HIGHER values are transmitted first.\n\t * Left to the transport's default when unset.\n\t */\n\tsendOrder?: number;\n\n\t/**\n\t * Reject if the peer hasn't freed a stream slot within this many milliseconds. Defaults\n\t * to 10s. There is no way to wait indefinitely: a peer can advertise a stream limit of\n\t * zero and never raise it, so every open needs a way out.\n\t */\n\ttimeout?: number;\n\n\t/**\n\t * Ask the transport to wait for a stream slot rather than failing when the peer's\n\t * concurrent stream limit is exhausted. Defaults to true.\n\t *\n\t * Set it false on a path that opens streams faster than the peer can retire them. The\n\t * transport queues the opens it can't satisfy and serves them in order, with no way to\n\t * cancel one, so waiting there spends returning credit on whatever was requested first\n\t * rather than on what matters now.\n\t */\n\twaitUntilAvailable?: boolean;\n}\n\n/** Options for {@link Writer.tryOpen}. */\nexport interface TryOpenOptions extends OpenOptions {\n\t/** Give up once this resolves. */\n\tcancel: Promise<void>;\n}\n\nexport class Stream {\n\treader: Reader;\n\twriter: Writer;\n\n\t/** Wrap the two halves of a transport stream. */\n\tconstructor(props: {\n\t\twritable: WritableStream<Uint8Array>;\n\t\treadable: ReadableStream<Uint8Array>;\n\t\tversion: StreamVersion;\n\t});\n\t/** Pair halves that were opened separately, as the SETUP exchange does. */\n\tconstructor(props: { writer: Writer; reader: Reader });\n\tconstructor(props: {\n\t\twritable?: WritableStream<Uint8Array>;\n\t\treadable?: ReadableStream<Uint8Array>;\n\t\twriter?: Writer;\n\t\treader?: Reader;\n\t\tversion?: StreamVersion;\n\t}) {\n\t\tconst version = props.version;\n\t\tconst writer = props.writer ?? (props.writable && version !== undefined && new Writer(props.writable, version));\n\t\tconst reader =\n\t\t\tprops.reader ?? (props.readable && version !== undefined && new Reader(props.readable, undefined, version));\n\t\tif (!writer || !reader) throw new Error(\"stream needs both halves\");\n\n\t\tthis.writer = writer;\n\t\tthis.reader = reader;\n\t}\n\n\tstatic async accept(quic: WebTransport, version: StreamVersion): 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\t/**\n\t * Open an outgoing bidirectional stream.\n\t * @param quic - The session to open it on\n\t * @param options - The version its varints encode with, and the send order ranking it\n\t * against the session's other streams\n\t */\n\tstatic async open(quic: WebTransport, options: OpenOptions): Promise<Stream> {\n\t\tconst { readable, writable } = await openWithin(\n\t\t\tquic.createBidirectionalStream(sendOptions(options)),\n\t\t\toptions?.timeout ?? OPEN_TIMEOUT_MS,\n\t\t\t(stream) => {\n\t\t\t\tvoid stream.writable.abort().catch(() => void 0);\n\t\t\t\tvoid stream.readable.cancel().catch(() => void 0);\n\t\t\t},\n\t\t);\n\t\treturn new Stream({ readable, writable, version: options.version });\n\t}\n\n\tclose() {\n\t\tthis.writer.close();\n\t\t// A routine unsubscribe, so send CANCELLED. A bare Error would put 0 on the wire,\n\t\t// which the stream registry reads as INTERNAL_ERROR: the peer would log a failure\n\t\t// for every subscription we walk away from.\n\t\tthis.reader.stop(new StreamError(StreamCode.Cancel, { message: \"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// Contiguous unread bytes, followed by chunks not yet joined onto it. Joining only once a\n\t// read needs the bytes keeps a frame arriving in N chunks linear rather than quadratic.\n\t#buffer: Uint8Array;\n\t#chunks: Uint8Array[] = [];\n\t#chunked = 0; // bytes across #chunks\n\t#stream?: ReadableStream<Uint8Array>; // if undefined, the buffer is consumed then EOF\n\t#reader?: ReadableStreamDefaultReader<Uint8Array>;\n\t#closed?: Promise<void>;\n\t// The decode that last ran short and how far, so a retry can wait for those bytes.\n\t#short?: { decode: (c: Cursor) => unknown; err: Short };\n\tversion: StreamVersion;\n\n\t// Either stream or buffer MUST be provided.\n\tconstructor(stream: ReadableStream<Uint8Array>, buffer: Uint8Array | undefined, version: StreamVersion);\n\tconstructor(stream: undefined, buffer: Uint8Array, version: StreamVersion);\n\tconstructor(\n\t\tstream: ReadableStream<Uint8Array> | undefined,\n\t\tbuffer: Uint8Array | undefined,\n\t\tversion: StreamVersion,\n\t) {\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, { version: asIetf(this.version) });\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\tthis.#chunks.push(result.value);\n\t\tthis.#chunked += result.value.byteLength;\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\tif (this.#buffer.byteLength >= size) return;\n\n\t\twhile (this.#buffer.byteLength + this.#chunked < 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\n\t\tthis.#join();\n\t}\n\n\t// Move every pending chunk into the buffer, copying only when there's more than one piece.\n\t#join() {\n\t\tif (this.#chunks.length === 0) return;\n\n\t\tif (this.#buffer.byteLength === 0 && this.#chunks.length === 1) {\n\t\t\tthis.#buffer = this.#chunks[0];\n\t\t} else {\n\t\t\tconst joined = new Uint8Array(this.#buffer.byteLength + this.#chunked);\n\t\t\tjoined.set(this.#buffer);\n\t\t\tlet offset = this.#buffer.byteLength;\n\t\t\tfor (const chunk of this.#chunks) {\n\t\t\t\tjoined.set(chunk, offset);\n\t\t\t\toffset += chunk.byteLength;\n\t\t\t}\n\t\t\tthis.#buffer = joined;\n\t\t}\n\n\t\tthis.#chunks = [];\n\t\tthis.#chunked = 0;\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\t/**\n\t * Run a synchronous decode over the buffered bytes and consume what it read.\n\t *\n\t * Returns undefined and consumes nothing when the decode ran past the buffered bytes, so a\n\t * caller can drain every complete message already here without waiting on the stream.\n\t */\n\ttryDecode<T extends NonNullable<unknown>>(decode: (c: Cursor) => T): T | undefined {\n\t\tconst result = this.#try(decode);\n\t\treturn result instanceof Short ? undefined : result;\n\t}\n\n\t/** Run a synchronous decode, filling from the stream until it has the bytes it needs. */\n\tasync decode<T>(decode: (c: Cursor) => T): Promise<T> {\n\t\tfor (;;) {\n\t\t\tconst result = this.#try(decode);\n\t\t\tif (!(result instanceof Short)) return result;\n\t\t\tawait this.#fillTo(result.need);\n\t\t}\n\t}\n\n\t/** Like {@link decode}, but returns undefined if the stream ends cleanly first. */\n\tasync decodeMaybe<T>(decode: (c: Cursor) => T): Promise<T | undefined> {\n\t\tif (await this.done()) return undefined;\n\t\treturn this.decode(decode);\n\t}\n\n\t#try<T>(decode: (c: Cursor) => T): T | Short {\n\t\t// A retry of the decode that last ran short, before the bytes it needs have arrived,\n\t\t// would only throw again. Every decode reads at least a byte, so none can succeed on\n\t\t// an empty buffer either.\n\t\tconst available = this.#buffer.byteLength + this.#chunked;\n\t\tif (available === 0) return EMPTY;\n\t\tif (decode === this.#short?.decode && available < this.#short.err.need) return this.#short.err;\n\n\t\tthis.#join();\n\t\tconst cursor = new Cursor(this.#buffer, this.version);\n\t\ttry {\n\t\t\tconst result = decode(cursor);\n\t\t\tthis.#slice(cursor.offset);\n\t\t\tthis.#short = undefined;\n\t\t\treturn result;\n\t\t} catch (err: unknown) {\n\t\t\tif (!(err instanceof Short)) throw err;\n\t\t\t// Filling could never satisfy it, so retrying would spin.\n\t\t\tif (err.need <= this.#buffer.byteLength) throw new Error(\"decode ran short of bytes it already had\");\n\t\t\tthis.#short = { decode, err };\n\t\t\treturn err;\n\t\t}\n\t}\n\n\tasync read(size: number): Promise<Uint8Array> {\n\t\tif (size === 0) return new Uint8Array();\n\t\treturn this.decode((c) => c.read(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\tthis.#join();\n\t\treturn this.#slice(this.#buffer.byteLength);\n\t}\n\n\t// Reads to the end of the stream, dropping every byte instead of buffering it.\n\tasync discard(): Promise<void> {\n\t\tthis.#buffer = new Uint8Array();\n\t\tdo {\n\t\t\tthis.#chunks = [];\n\t\t\tthis.#chunked = 0;\n\t\t} while (await this.#fill());\n\t}\n\n\tasync string(): Promise<string> {\n\t\treturn this.decode(STRING);\n\t}\n\n\tasync bool(): Promise<boolean> {\n\t\treturn this.decode(BOOL);\n\t}\n\n\tasync u8(): Promise<number> {\n\t\treturn this.decode(U8);\n\t}\n\n\tasync u16(): Promise<number> {\n\t\treturn this.decode(U16);\n\t}\n\n\t// Returns a Number using 53-bits, the max Javascript can use for integer math.\n\tasync u53(): Promise<number> {\n\t\treturn this.decode(U53);\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\treturn this.decode(U62);\n\t}\n\n\tasync varint(): Promise<U64> {\n\t\treturn this.decode(VARINT);\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 || this.#chunked > 0) return false;\n\t\treturn !(await this.#fill());\n\t}\n\n\tstop(reason: unknown) {\n\t\tthis.#reader?.cancel(withCode(reason, this.version)).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, { version: asIetf(this.version) });\n\t\t});\n\t\treturn this.#closed;\n\t}\n}\n\n// Thrown by a Cursor read that runs past the buffered bytes, carrying how many bytes from the\n// start of the buffer the decode needs. Not an Error: it ends every chunk, so it must not\n// capture a stack.\nclass Short {\n\treadonly need: number;\n\n\tconstructor(need: number) {\n\t\tthis.need = need;\n\t}\n}\n\nconst EMPTY = new Short(1);\n\n/**\n * A synchronous view over a {@link Reader}'s buffered bytes, handed to {@link Reader.decode}.\n *\n * A read past the buffered bytes throws an internal signal that the Reader catches: it consumes\n * nothing, fills, and runs the decode again from the start. A decode must therefore not mutate\n * anything before its last read, must not swallow what it throws, and must read at least a byte.\n */\nexport class Cursor {\n\treadonly version: StreamVersion;\n\t#buffer: Uint8Array;\n\t#offset = 0;\n\t// Resolved once, since every varint read branches on it.\n\t#leadingOnes: boolean;\n\t// First bytes below this are a whole 1-byte varint, and below this + 0x40 a 2-byte one whose\n\t// value is the low 6 bits and the next byte. Both formats share that shape; only the bound moves.\n\t#short: number;\n\t// First bytes below this are a varint of at most 4 bytes: 0xc0 for QUIC, 0xf0 for leading-ones.\n\t#word: number;\n\n\tconstructor(buffer: Uint8Array, version: StreamVersion) {\n\t\tthis.#buffer = buffer;\n\t\tthis.version = version;\n\t\tthis.#leadingOnes = isLeadingOnes(version);\n\t\tthis.#short = this.#leadingOnes ? 0x80 : 0x40;\n\t\tthis.#word = this.#leadingOnes ? 0xf0 : 0xc0;\n\t}\n\n\t/** How many bytes have been read. */\n\tget offset(): number {\n\t\treturn this.#offset;\n\t}\n\n\t/** How many buffered bytes are left to read. */\n\tget remaining(): number {\n\t\treturn this.#buffer.byteLength - this.#offset;\n\t}\n\n\t/**\n\t * Decode the next `size` bytes on their own. Running past them, or leaving any unread, is\n\t * malformed rather than a reason to wait for more.\n\t */\n\texact<T>(size: number, decode: (c: Cursor) => T): T {\n\t\tconst inner = new Cursor(this.read(size), this.version);\n\t\tlet result: T;\n\t\ttry {\n\t\t\tresult = decode(inner);\n\t\t} catch (err: unknown) {\n\t\t\tif (err instanceof Short) throw new Error(`message is shorter than its fields: ${size} bytes`);\n\t\t\tthrow err;\n\t\t}\n\t\tif (inner.remaining > 0) throw new Error(`message has ${inner.remaining} unread bytes`);\n\t\treturn result;\n\t}\n\n\t#ensure(size: number) {\n\t\tconst need = this.#offset + size;\n\t\t// Checked here too, and on the whole decode like the fill, since bytes that are already\n\t\t// buffered never reach the fill.\n\t\tif (need > MAX_READ_SIZE) throw new Error(`read size ${need} exceeds max size ${MAX_READ_SIZE}`);\n\t\tif (need > this.#buffer.byteLength) throw new Short(need);\n\t}\n\n\t/** Read `size` bytes, as a view onto the buffer rather than a copy. */\n\tread(size: number): Uint8Array {\n\t\tthis.#ensure(size);\n\t\tconst start = this.#offset;\n\t\tthis.#offset += size;\n\t\treturn this.#buffer.subarray(start, this.#offset);\n\t}\n\n\tstring(): string {\n\t\treturn decodeUtf8(this.read(this.u53()));\n\t}\n\n\tbool(): boolean {\n\t\tconst v = 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\tu8(): number {\n\t\tthis.#ensure(1);\n\t\treturn this.#buffer[this.#offset++];\n\t}\n\n\tu16(): number {\n\t\tthis.#ensure(2);\n\t\tconst b = this.#buffer;\n\t\tconst o = this.#offset;\n\t\tthis.#offset += 2;\n\t\treturn (b[o] << 8) | b[o + 1];\n\t}\n\n\t/** Read a varint as a `number`, throwing if it is above `Number.MAX_SAFE_INTEGER`. */\n\tu53(): number {\n\t\t// Most varints are 1 or 2 bytes, which skip the general decode.\n\t\tthis.#ensure(1);\n\t\tconst b = this.#buffer;\n\t\tconst o = this.#offset;\n\t\tconst first = b[o];\n\t\tif (first < this.#short) {\n\t\t\tthis.#offset = o + 1;\n\t\t\treturn first;\n\t\t}\n\t\tif (first < this.#short + 0x40) {\n\t\t\tthis.#ensure(2);\n\t\t\tthis.#offset = o + 2;\n\t\t\treturn ((first & 0x3f) << 8) | b[o + 1];\n\t\t}\n\t\t// Up to 4 bytes still fits 28 (leading-ones) or 30 (QUIC) bits, with no upper half.\n\t\tif (first < this.#word) {\n\t\t\tconst size = this.#leadingOnes ? peekLeadingOnes(first) : 4;\n\t\t\tthis.#ensure(size);\n\t\t\tthis.#offset = o + size;\n\t\t\tif (size === 3) return ((first & 0x1f) << 16) | (b[o + 1] << 8) | b[o + 2];\n\t\t\treturn ((first & (this.#leadingOnes ? 0x0f : 0x3f)) << 24) | (b[o + 1] << 16) | (b[o + 2] << 8) | b[o + 3];\n\t\t}\n\t\tconst lo = this.#varint();\n\t\treturn toNumber(parts.hi, lo);\n\t}\n\n\t/** Read a varint as a bigint. A leading-ones varint may exceed 62 bits. */\n\tu62(): bigint {\n\t\tconst lo = this.#varint();\n\t\treturn toBigInt(parts.hi, lo);\n\t}\n\n\t/** Read a varint. */\n\tvarint(): U64 {\n\t\tconst lo = this.#varint();\n\t\treturn new U64(parts.hi, lo);\n\t}\n\n\t// Decode the next varint in the version's format, returning its lower half and leaving the upper in `parts`.\n\t#varint(): number {\n\t\tthis.#ensure(1);\n\t\tconst b = this.#buffer;\n\t\tconst o = this.#offset;\n\t\tlet size: number;\n\t\tif (this.#leadingOnes) {\n\t\t\tsize = peekLeadingOnes(b[o]);\n\t\t\t// 1111110x is a 7-byte form. Draft-17 rejects it; draft-18+ allows it per #1595.\n\t\t\tif (size === 7 && this.version === Version.DRAFT_17) {\n\t\t\t\tthrow new Error(\"invalid leading-ones varint: 1111110x prefix is reserved on draft-17\");\n\t\t\t}\n\t\t\tthis.#ensure(size);\n\t\t\tthis.#offset += size;\n\t\t\treturn readLeadingOnes(b, o, size);\n\t\t}\n\t\tsize = peekQuic(b[o]);\n\t\tthis.#ensure(size);\n\t\tthis.#offset += size;\n\t\treturn readQuic(b, o, size);\n\t}\n}\n\n// Shared decodes for the Reader's async primitives, so a read allocates no closure.\nconst STRING = (c: Cursor) => c.string();\nconst BOOL = (c: Cursor) => c.bool();\nconst U8 = (c: Cursor) => c.u8();\nconst U16 = (c: Cursor) => c.u16();\nconst U53 = (c: Cursor) => c.u53();\nconst U62 = (c: Cursor) => c.u62();\nconst VARINT = (c: Cursor) => c.varint();\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 each primitive write, sized for the longest (a 9-byte leading-ones varint).\n\t#scratch: ArrayBuffer;\n\n\tversion: StreamVersion;\n\n\tconstructor(stream: WritableStream<Uint8Array>, version: StreamVersion) {\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\t/**\n\t * Rank this stream against the session's others, where HIGHER values are sent first.\n\t *\n\t * A send order only schedules the local end, so a stream the peer opened has to be ranked\n\t * here rather than at the peer's {@link open}.\n\t *\n\t * The spec makes `sendOrder` a settable attribute on every {@link SendStream}. Where the\n\t * interface isn't implemented (Chrome as of writing, a mock, a polyfill) this just sets an\n\t * ignored property, the same way an ignored `sendOrder` option does at {@link open}.\n\t */\n\tsetPriority(sendOrder: number) {\n\t\t(this.#stream as SendStream).sendOrder = sendOrder;\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\tif (!Number.isInteger(v) || v < 0 || v > 255) {\n\t\t\tthrow new RangeError(`invalid u8: ${v}`);\n\t\t}\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 (!Number.isSafeInteger(v) || v < 0) {\n\t\t\tthrow new RangeError(`invalid u53: ${v}`);\n\t\t}\n\t\tawait this.#varint(Math.floor(v / POW32), v >>> 0);\n\t}\n\n\tasync u62(v: bigint) {\n\t\tconst lo = split(v);\n\t\tawait this.#varint(parts.hi, lo);\n\t}\n\n\tasync varint(v: U64) {\n\t\tawait this.#varint(v.hi, v.lo);\n\t}\n\n\t#varint(hi: number, lo: number): Promise<void> {\n\t\treturn this.write(encodeTo(this.#scratch, hi, lo, this.version));\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, { version: asIetf(this.version) });\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, { version: asIetf(this.version) });\n\t\t});\n\t\treturn this.#closed;\n\t}\n\n\treset(reason: unknown) {\n\t\tthis.#writer.abort(withCode(reason, this.version)).catch(() => void 0);\n\t}\n\n\t/**\n\t * Open an outgoing unidirectional stream.\n\t * @param quic - The session to open it on\n\t * @param options - The version its varints encode with, and the send order ranking it\n\t * against the session's other streams\n\t */\n\tstatic async open(quic: WebTransport, options: OpenOptions): Promise<Writer> {\n\t\tconst writable = await openWithin(\n\t\t\tquic.createUnidirectionalStream(sendOptions(options)) as Promise<WritableStream<Uint8Array>>,\n\t\t\toptions?.timeout ?? OPEN_TIMEOUT_MS,\n\t\t\t(stream) => void stream.abort().catch(() => void 0),\n\t\t);\n\n\t\treturn new Writer(writable, options.version);\n\t}\n\n\t/**\n\t * Like {@link Writer.open}, but gives up when `cancel` settles or `timeout` elapses,\n\t * returning undefined so the caller can drop whatever it meant to send. A stream that\n\t * opens after that is reset rather than leaked. A real transport failure still throws.\n\t *\n\t * Worth using even with `waitUntilAvailable: false`, since an implementation may park\n\t * an over-limit open instead of rejecting it.\n\t */\n\tstatic async tryOpen(quic: WebTransport, options: TryOpenOptions): Promise<Writer | undefined> {\n\t\t// Raced ahead of the open, so an already-cancelled caller wins even against a slot\n\t\t// that is free right now. `race` rather than `Promise.race`: the caller shares one\n\t\t// `cancel` across every group of a subscription, which must not gain a reaction per call.\n\t\tconst open = Writer.open(quic, options);\n\n\t\t// Resets a stream that opens after we gave up; a no-op if the open itself failed.\n\t\tconst abandon = () => {\n\t\t\tconst abandoned = new Error(\"abandoned waiting for a stream slot\");\n\t\t\topen.then((w) => w.reset(abandoned)).catch(() => void 0);\n\t\t};\n\n\t\ttry {\n\t\t\tconst stream = await race([options.cancel, open]);\n\t\t\tif (stream) return stream;\n\t\t} catch (err: unknown) {\n\t\t\t// open already discarded the late stream on its way out.\n\t\t\tif (err instanceof TimeoutError) return undefined;\n\t\t\t// A rejected `cancel` still leaves the open pending.\n\t\t\tabandon();\n\t\t\tthrow err;\n\t\t}\n\n\t\tabandon();\n\t\treturn undefined;\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: StreamVersion;\n\n\tconstructor(quic: WebTransport, version: StreamVersion) {\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/tail.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Milli } from "./time";
|
|
2
|
+
/**
|
|
3
|
+
* How long a subscriber waits for a group stream it cannot account for once the publisher
|
|
4
|
+
* has ended the subscription.
|
|
5
|
+
*
|
|
6
|
+
* A group reset before its header arrived leaves no trace, and QUIC does not order streams,
|
|
7
|
+
* so a stream opened before the end can still be in flight after it. This bounds the wait on
|
|
8
|
+
* IETF, and on moq-lite when the subscription has no max age to bound it with.
|
|
9
|
+
*/
|
|
10
|
+
export declare const TAIL_GRACE_MS: Milli;
|
|
11
|
+
/** How a {@link Tail} measures its grace. */
|
|
12
|
+
export interface TailOptions {
|
|
13
|
+
/** How long to wait for a group that cannot be accounted for. Read each time it is needed. */
|
|
14
|
+
grace?: () => Milli;
|
|
15
|
+
/** The clock, in `performance.now()` milliseconds. */
|
|
16
|
+
now?: () => number;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=tail.d.ts.map
|
package/tail.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tail.d.ts","sourceRoot":"","sources":["../src/tail.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,WAAW,CAAC;AAElC;;;;;;;GAOG;AACH,eAAO,MAAM,aAAa,OAAc,CAAC;AASzC,6CAA6C;AAC7C,MAAM,WAAW,WAAW;IAC3B,8FAA8F;IAC9F,KAAK,CAAC,EAAE,MAAM,KAAK,CAAC;IACpB,sDAAsD;IACtD,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;CACnB"}
|
package/tail.js
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/* @ts-self-types="./tail.d.ts" */
|
|
2
|
+
import { Signal } from "@norskvideo/moq-signals";
|
|
3
|
+
import { Milli } from "./time.js";
|
|
4
|
+
/**
|
|
5
|
+
* How long a subscriber waits for a group stream it cannot account for once the publisher
|
|
6
|
+
* has ended the subscription.
|
|
7
|
+
*
|
|
8
|
+
* A group reset before its header arrived leaves no trace, and QUIC does not order streams,
|
|
9
|
+
* so a stream opened before the end can still be in flight after it. This bounds the wait on
|
|
10
|
+
* IETF, and on moq-lite when the subscription has no max age to bound it with.
|
|
11
|
+
*/
|
|
12
|
+
export const TAIL_GRACE_MS = Milli(1000);
|
|
13
|
+
// setTimeout truncates a longer delay to a signed 32-bit int and fires at once.
|
|
14
|
+
const MAX_TIMEOUT_MS = 2 ** 31 - 1;
|
|
15
|
+
/**
|
|
16
|
+
* The group streams a subscription has received, so its end can wait for the ones still owed.
|
|
17
|
+
*
|
|
18
|
+
* A publisher ends a subscription before every group stream it opened has necessarily
|
|
19
|
+
* arrived. This records which sequences are accounted for (a stream's header arrived, or
|
|
20
|
+
* the publisher dropped them) and how many streams are still being read, and {@link settle}
|
|
21
|
+
* waits on them.
|
|
22
|
+
*
|
|
23
|
+
* A lost datagram is not owed: it leaves a hole that waits out the grace like a stream reset
|
|
24
|
+
* before its header.
|
|
25
|
+
*
|
|
26
|
+
* @internal
|
|
27
|
+
*/
|
|
28
|
+
export class Tail {
|
|
29
|
+
// Disjoint, sorted, non-adjacent runs of accounted sequences. A gap older than the grace
|
|
30
|
+
// can no longer be waited for, so it folds into the runs around it, which bounds this by
|
|
31
|
+
// the gaps opened within the grace rather than every gap in the subscription. A fold is
|
|
32
|
+
// final: a later, longer grace or a lowered floor cannot reopen it.
|
|
33
|
+
#runs = [];
|
|
34
|
+
// Group streams whose header arrived, and those still being read.
|
|
35
|
+
#streams = 0;
|
|
36
|
+
#active = 0;
|
|
37
|
+
#changed = new Signal(0);
|
|
38
|
+
#grace;
|
|
39
|
+
#now;
|
|
40
|
+
constructor(options = {}) {
|
|
41
|
+
this.#grace = options.grace ?? (() => TAIL_GRACE_MS);
|
|
42
|
+
this.#now = options.now ?? (() => performance.now());
|
|
43
|
+
}
|
|
44
|
+
/** Group streams whose header arrived, whether they finished or were reset. */
|
|
45
|
+
get streams() {
|
|
46
|
+
return this.#streams;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Record a group stream whose header arrived. Returns the call that marks it read to
|
|
50
|
+
* its end, which is idempotent.
|
|
51
|
+
*/
|
|
52
|
+
open(sequence) {
|
|
53
|
+
this.#streams += 1;
|
|
54
|
+
this.#active += 1;
|
|
55
|
+
this.#account(sequence, sequence + 1);
|
|
56
|
+
let closed = false;
|
|
57
|
+
return () => {
|
|
58
|
+
if (closed)
|
|
59
|
+
return;
|
|
60
|
+
closed = true;
|
|
61
|
+
this.#active -= 1;
|
|
62
|
+
this.#bump();
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
/** Record sequences `[start, end)` as accounted for without a stream: dropped, or a datagram. */
|
|
66
|
+
account(start, end) {
|
|
67
|
+
this.#account(start, end);
|
|
68
|
+
}
|
|
69
|
+
/** Restart the age of every gap reaching into `[start, end)`, which the demand newly asks for. */
|
|
70
|
+
demand(start, end) {
|
|
71
|
+
if (start >= end)
|
|
72
|
+
return;
|
|
73
|
+
const now = this.#now();
|
|
74
|
+
let below = 0;
|
|
75
|
+
this.#runs = this.#runs.map((run) => {
|
|
76
|
+
const reached = below < end && start < run.start;
|
|
77
|
+
below = run.end;
|
|
78
|
+
return reached ? { ...run, since: now } : run;
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
/** Whether every sequence in `[start, end)` is accounted for. */
|
|
82
|
+
covers(start, end) {
|
|
83
|
+
if (start >= end)
|
|
84
|
+
return true;
|
|
85
|
+
// Runs are merged on insert, so one run covers the span or none does.
|
|
86
|
+
return this.#runs.some((run) => run.start <= start && end <= run.end);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Wait until every stream is read to its end and `complete()` holds, or the grace passes
|
|
90
|
+
* with nothing left being read, or `closed` settles.
|
|
91
|
+
*
|
|
92
|
+
* A stream still being read is always waited for: a group ends on its own stream's FIN
|
|
93
|
+
* or reset, never because its track ended. The grace only gives up on streams that never
|
|
94
|
+
* arrived.
|
|
95
|
+
*/
|
|
96
|
+
async settle(complete, closed) {
|
|
97
|
+
// A gap that aged past the grace since the last insert is no longer waited for either.
|
|
98
|
+
this.#expire(this.#now());
|
|
99
|
+
let expired = false;
|
|
100
|
+
const timer = setTimeout(() => {
|
|
101
|
+
expired = true;
|
|
102
|
+
this.#bump();
|
|
103
|
+
}, Math.min(this.#grace(), MAX_TIMEOUT_MS));
|
|
104
|
+
try {
|
|
105
|
+
while (closed.peek() === undefined) {
|
|
106
|
+
if (this.#active === 0 && (expired || complete()))
|
|
107
|
+
return;
|
|
108
|
+
await Signal.race(this.#changed, closed);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
finally {
|
|
112
|
+
clearTimeout(timer);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
#account(start, end) {
|
|
116
|
+
if (start >= end)
|
|
117
|
+
return;
|
|
118
|
+
const now = this.#now();
|
|
119
|
+
const runs = [];
|
|
120
|
+
let merged;
|
|
121
|
+
let placed = false;
|
|
122
|
+
for (const run of this.#runs) {
|
|
123
|
+
if (run.end < start) {
|
|
124
|
+
runs.push(run);
|
|
125
|
+
}
|
|
126
|
+
else if (end < run.start) {
|
|
127
|
+
// Splitting a gap leaves both halves as late as it was.
|
|
128
|
+
if (!placed)
|
|
129
|
+
runs.push(merged ?? { start, end, since: run.since });
|
|
130
|
+
placed = true;
|
|
131
|
+
runs.push(run);
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
merged = {
|
|
135
|
+
start: Math.min(merged?.start ?? start, run.start),
|
|
136
|
+
end: Math.max(merged?.end ?? end, run.end),
|
|
137
|
+
since: merged?.since ?? run.since,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
// A run past every other one opens a new gap.
|
|
142
|
+
if (!placed)
|
|
143
|
+
runs.push(merged ?? { start, end, since: now });
|
|
144
|
+
this.#runs = runs;
|
|
145
|
+
this.#expire(now);
|
|
146
|
+
this.#bump();
|
|
147
|
+
}
|
|
148
|
+
// Fold every gap older than the grace into the runs around it.
|
|
149
|
+
#expire(now) {
|
|
150
|
+
const grace = this.#grace();
|
|
151
|
+
const runs = [];
|
|
152
|
+
for (const run of this.#runs) {
|
|
153
|
+
const below = runs.at(-1);
|
|
154
|
+
if (below && now - run.since >= grace) {
|
|
155
|
+
runs[runs.length - 1] = { ...below, end: run.end };
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
runs.push(run);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
this.#runs = runs;
|
|
162
|
+
}
|
|
163
|
+
#bump() {
|
|
164
|
+
this.#changed.update((revision) => revision + 1);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
//# sourceMappingURL=tail.js.map
|
package/tail.js.map
ADDED
|
@@ -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CAAC;iBAAM,CAAC;gBACP,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YAChB,CAAC;QACF,CAAC;QACD,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;IACnB,CAAC;IAED,KAAK;QACJ,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,GAAG,CAAC,CAAC,CAAC;IAClD,CAAC;CACD","sourcesContent":["import { type GetPromise, Signal } from \"@moq/signals\";\nimport { Milli } from \"./time.ts\";\n\n/**\n * How long a subscriber waits for a group stream it cannot account for once the publisher\n * has ended the subscription.\n *\n * A group reset before its header arrived leaves no trace, and QUIC does not order streams,\n * so a stream opened before the end can still be in flight after it. This bounds the wait on\n * IETF, and on moq-lite when the subscription has no max age to bound it with.\n */\nexport const TAIL_GRACE_MS = Milli(1000);\n\n// setTimeout truncates a longer delay to a signed 32-bit int and fires at once.\nconst MAX_TIMEOUT_MS = 2 ** 31 - 1;\n\n// A run of accounted sequences `[start, end)`, and when the gap below it opened (in\n// `performance.now()` milliseconds), which is when its missing groups became late.\ntype Run = { start: number; end: number; since: number };\n\n/** How a {@link Tail} measures its grace. */\nexport interface TailOptions {\n\t/** How long to wait for a group that cannot be accounted for. Read each time it is needed. */\n\tgrace?: () => Milli;\n\t/** The clock, in `performance.now()` milliseconds. */\n\tnow?: () => number;\n}\n\n/**\n * The group streams a subscription has received, so its end can wait for the ones still owed.\n *\n * A publisher ends a subscription before every group stream it opened has necessarily\n * arrived. This records which sequences are accounted for (a stream's header arrived, or\n * the publisher dropped them) and how many streams are still being read, and {@link settle}\n * waits on them.\n *\n * A lost datagram is not owed: it leaves a hole that waits out the grace like a stream reset\n * before its header.\n *\n * @internal\n */\nexport class Tail {\n\t// Disjoint, sorted, non-adjacent runs of accounted sequences. A gap older than the grace\n\t// can no longer be waited for, so it folds into the runs around it, which bounds this by\n\t// the gaps opened within the grace rather than every gap in the subscription. A fold is\n\t// final: a later, longer grace or a lowered floor cannot reopen it.\n\t#runs: Run[] = [];\n\t// Group streams whose header arrived, and those still being read.\n\t#streams = 0;\n\t#active = 0;\n\t#changed = new Signal(0);\n\t#grace: () => Milli;\n\t#now: () => number;\n\n\tconstructor(options: TailOptions = {}) {\n\t\tthis.#grace = options.grace ?? (() => TAIL_GRACE_MS);\n\t\tthis.#now = options.now ?? (() => performance.now());\n\t}\n\n\t/** Group streams whose header arrived, whether they finished or were reset. */\n\tget streams(): number {\n\t\treturn this.#streams;\n\t}\n\n\t/**\n\t * Record a group stream whose header arrived. Returns the call that marks it read to\n\t * its end, which is idempotent.\n\t */\n\topen(sequence: number): () => void {\n\t\tthis.#streams += 1;\n\t\tthis.#active += 1;\n\t\tthis.#account(sequence, sequence + 1);\n\n\t\tlet closed = false;\n\t\treturn () => {\n\t\t\tif (closed) return;\n\t\t\tclosed = true;\n\t\t\tthis.#active -= 1;\n\t\t\tthis.#bump();\n\t\t};\n\t}\n\n\t/** Record sequences `[start, end)` as accounted for without a stream: dropped, or a datagram. */\n\taccount(start: number, end: number): void {\n\t\tthis.#account(start, end);\n\t}\n\n\t/** Restart the age of every gap reaching into `[start, end)`, which the demand newly asks for. */\n\tdemand(start: number, end: number): void {\n\t\tif (start >= end) return;\n\t\tconst now = this.#now();\n\t\tlet below = 0;\n\t\tthis.#runs = this.#runs.map((run) => {\n\t\t\tconst reached = below < end && start < run.start;\n\t\t\tbelow = run.end;\n\t\t\treturn reached ? { ...run, since: now } : run;\n\t\t});\n\t}\n\n\t/** Whether every sequence in `[start, end)` is accounted for. */\n\tcovers(start: number, end: number): boolean {\n\t\tif (start >= end) return true;\n\t\t// Runs are merged on insert, so one run covers the span or none does.\n\t\treturn this.#runs.some((run) => run.start <= start && end <= run.end);\n\t}\n\n\t/**\n\t * Wait until every stream is read to its end and `complete()` holds, or the grace passes\n\t * with nothing left being read, or `closed` settles.\n\t *\n\t * A stream still being read is always waited for: a group ends on its own stream's FIN\n\t * or reset, never because its track ended. The grace only gives up on streams that never\n\t * arrived.\n\t */\n\tasync settle(complete: () => boolean, closed: GetPromise<unknown>): Promise<void> {\n\t\t// A gap that aged past the grace since the last insert is no longer waited for either.\n\t\tthis.#expire(this.#now());\n\n\t\tlet expired = false;\n\t\tconst timer = setTimeout(\n\t\t\t() => {\n\t\t\t\texpired = true;\n\t\t\t\tthis.#bump();\n\t\t\t},\n\t\t\tMath.min(this.#grace(), MAX_TIMEOUT_MS),\n\t\t);\n\t\ttry {\n\t\t\twhile (closed.peek() === undefined) {\n\t\t\t\tif (this.#active === 0 && (expired || complete())) return;\n\t\t\t\tawait Signal.race(this.#changed, closed);\n\t\t\t}\n\t\t} finally {\n\t\t\tclearTimeout(timer);\n\t\t}\n\t}\n\n\t#account(start: number, end: number): void {\n\t\tif (start >= end) return;\n\n\t\tconst now = this.#now();\n\t\tconst runs: Run[] = [];\n\t\tlet merged: Run | undefined;\n\t\tlet placed = false;\n\t\tfor (const run of this.#runs) {\n\t\t\tif (run.end < start) {\n\t\t\t\truns.push(run);\n\t\t\t} else if (end < run.start) {\n\t\t\t\t// Splitting a gap leaves both halves as late as it was.\n\t\t\t\tif (!placed) runs.push(merged ?? { start, end, since: run.since });\n\t\t\t\tplaced = true;\n\t\t\t\truns.push(run);\n\t\t\t} else {\n\t\t\t\tmerged = {\n\t\t\t\t\tstart: Math.min(merged?.start ?? start, run.start),\n\t\t\t\t\tend: Math.max(merged?.end ?? end, run.end),\n\t\t\t\t\tsince: merged?.since ?? run.since,\n\t\t\t\t};\n\t\t\t}\n\t\t}\n\t\t// A run past every other one opens a new gap.\n\t\tif (!placed) runs.push(merged ?? { start, end, since: now });\n\t\tthis.#runs = runs;\n\t\tthis.#expire(now);\n\t\tthis.#bump();\n\t}\n\n\t// Fold every gap older than the grace into the runs around it.\n\t#expire(now: number): void {\n\t\tconst grace = this.#grace();\n\t\tconst runs: Run[] = [];\n\t\tfor (const run of this.#runs) {\n\t\t\tconst below = runs.at(-1);\n\t\t\tif (below && now - run.since >= grace) {\n\t\t\t\truns[runs.length - 1] = { ...below, end: run.end };\n\t\t\t} else {\n\t\t\t\truns.push(run);\n\t\t\t}\n\t\t}\n\t\tthis.#runs = runs;\n\t}\n\n\t#bump(): void {\n\t\tthis.#changed.update((revision) => revision + 1);\n\t}\n}\n"]}
|
package/time.d.ts
CHANGED
|
@@ -71,7 +71,13 @@ export declare const Milli: ((value: number) => Milli) & {
|
|
|
71
71
|
export type Timescale = number & {
|
|
72
72
|
readonly _brand: "timescale";
|
|
73
73
|
};
|
|
74
|
-
/**
|
|
74
|
+
/**
|
|
75
|
+
* Named timescales and a checked constructor.
|
|
76
|
+
*
|
|
77
|
+
* Units per second must be a safe integer ≥ 1. JavaScript numbers are exact only
|
|
78
|
+
* through `Number.MAX_SAFE_INTEGER` (`2^53 - 1`); the wire varint range goes
|
|
79
|
+
* to `2^62 - 1`, which Rust accepts and this constructor refuses.
|
|
80
|
+
*/
|
|
75
81
|
export declare const Timescale: ((unitsPerSecond: number) => Timescale) & {
|
|
76
82
|
/** One unit per second. */
|
|
77
83
|
SECOND: Timescale;
|
|
@@ -87,6 +93,13 @@ export declare const Timescale: ((unitsPerSecond: number) => Timescale) & {
|
|
|
87
93
|
*
|
|
88
94
|
* Mirrors the Rust `Timestamp`. Unlike the bare `Milli`/`Micro` aliases it carries its
|
|
89
95
|
* own scale, so a track can pick its units and conversions can't silently mix them up.
|
|
96
|
+
*
|
|
97
|
+
* The value must be finite and in `[0, Number.MAX_SAFE_INTEGER]`. Fractions are allowed
|
|
98
|
+
* (`performance.now()`, scale conversion). Encoding rounds to the nearest integer at the
|
|
99
|
+
* track timescale. Integer timestamps are exact through `2^53 - 1` units; this package
|
|
100
|
+
* does not use BigInt for timestamps. {@link Timestamp.as} also refuses a converted
|
|
101
|
+
* value that leaves that range, so a legal source timestamp cannot become an unencodable
|
|
102
|
+
* wire timestamp after a scale change.
|
|
90
103
|
*/
|
|
91
104
|
export declare class Timestamp {
|
|
92
105
|
/** The raw value, in `scale` units. */
|
|
@@ -101,7 +114,7 @@ export declare class Timestamp {
|
|
|
101
114
|
static fromMillis(ms: number): Timestamp;
|
|
102
115
|
/** A timestamp of `us` microseconds. */
|
|
103
116
|
static fromMicros(us: number): Timestamp;
|
|
104
|
-
/** This timestamp's value re-expressed at `scale
|
|
117
|
+
/** This timestamp's value re-expressed at `scale`; throws if the result is not in the safe range. */
|
|
105
118
|
as(scale: Timescale): number;
|
|
106
119
|
/** The value in milliseconds. */
|
|
107
120
|
asMillis(): number;
|
package/time.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"time.d.ts","sourceRoot":"","sources":["../src/time.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,gFAAgF;AAChF,MAAM,MAAM,IAAI,GAAG,MAAM,GAAG;IAAE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAExD,yEAAyE;AAGzE,eAAO,MAAM,IAAI,WAAyB,MAAM,KAAG,IAAI;UAC3C,IAAI;oBACC,KAAK,KAAG,IAAI;oBACZ,KAAK,KAAG,IAAI;oBACZ,MAAM,KAAG,IAAI;kBACf,IAAI,KAAG,KAAK;kBACZ,IAAI,KAAG,KAAK;mBACX,IAAI,KAAG,MAAM;eACnB,IAAI;aACJ,IAAI,KAAK,IAAI,KAAG,IAAI;aACpB,IAAI,KAAK,IAAI,KAAG,IAAI;aACpB,IAAI,KAAK,MAAM,KAAG,IAAI;aACtB,IAAI,KAAK,MAAM,KAAG,IAAI;aACtB,IAAI,KAAK,IAAI,KAAG,IAAI;aACpB,IAAI,KAAK,IAAI,KAAG,IAAI;CAC5B,CAAC;AAEH,iFAAiF;AACjF,MAAM,MAAM,KAAK,GAAG,MAAM,GAAG;IAAE,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAA;CAAE,CAAC;AAE1D,0EAA0E;AAG1E,eAAO,MAAM,KAAK,WAAyB,MAAM,KAAG,KAAK;UAC7C,KAAK;mBACD,IAAI,KAAG,KAAK;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,CAK3B;IAED,iCAAiC;IACjC,QAAQ,IAAI,MAAM,CAEjB;IAED,iCAAiC;IACjC,QAAQ,IAAI,MAAM,CAEjB;CACD;AAED,4EAA4E;AAC5E,MAAM,MAAM,MAAM,GAAG,MAAM,GAAG;IAAE,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAA;CAAE,CAAC;AAE5D,2EAA2E;AAG3E,eAAO,MAAM,MAAM,WAAyB,MAAM,KAAG,MAAM;UAC/C,MAAM;mBACF,IAAI,KAAG,MAAM;oBACZ,KAAK,KAAG,MAAM;oBACd,KAAK,KAAG,MAAM;gBAClB,MAAM,KAAG,IAAI;iBACZ,MAAM,KAAG,KAAK;iBACd,MAAM,KAAG,KAAK;eAClB,MAAM;aACN,MAAM,KAAK,MAAM,KAAG,MAAM;aAC1B,MAAM,KAAK,MAAM,KAAG,MAAM;aAC1B,MAAM,KAAK,MAAM,KAAG,MAAM;aAC1B,MAAM,KAAK,MAAM,KAAG,MAAM;aAC1B,MAAM,KAAK,MAAM,KAAG,MAAM;aAC1B,MAAM,KAAK,MAAM,KAAG,MAAM;CAClC,CAAC"}
|
package/time.js
CHANGED
|
@@ -62,10 +62,16 @@ export const Milli = Object.assign((value) => value, {
|
|
|
62
62
|
max: (a, b) => Math.max(a, b),
|
|
63
63
|
min: (a, b) => Math.min(a, b),
|
|
64
64
|
});
|
|
65
|
-
/**
|
|
65
|
+
/**
|
|
66
|
+
* Named timescales and a checked constructor.
|
|
67
|
+
*
|
|
68
|
+
* Units per second must be a safe integer ≥ 1. JavaScript numbers are exact only
|
|
69
|
+
* through `Number.MAX_SAFE_INTEGER` (`2^53 - 1`); the wire varint range goes
|
|
70
|
+
* to `2^62 - 1`, which Rust accepts and this constructor refuses.
|
|
71
|
+
*/
|
|
66
72
|
export const Timescale = Object.assign((unitsPerSecond) => {
|
|
67
|
-
if (!Number.
|
|
68
|
-
throw new
|
|
73
|
+
if (!Number.isSafeInteger(unitsPerSecond) || unitsPerSecond <= 0) {
|
|
74
|
+
throw new RangeError(`invalid timescale: ${unitsPerSecond}`);
|
|
69
75
|
}
|
|
70
76
|
return unitsPerSecond;
|
|
71
77
|
}, {
|
|
@@ -78,11 +84,23 @@ export const Timescale = Object.assign((unitsPerSecond) => {
|
|
|
78
84
|
/** 1,000,000,000 units per second. */
|
|
79
85
|
NANO: 1_000_000_000,
|
|
80
86
|
});
|
|
87
|
+
function assertTimestampValue(value) {
|
|
88
|
+
if (!Number.isFinite(value) || value < 0 || value > Number.MAX_SAFE_INTEGER) {
|
|
89
|
+
throw new RangeError(`invalid timestamp: ${value}`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
81
92
|
/**
|
|
82
93
|
* A presentation timestamp: a raw value in a given {@link Timescale}.
|
|
83
94
|
*
|
|
84
95
|
* Mirrors the Rust `Timestamp`. Unlike the bare `Milli`/`Micro` aliases it carries its
|
|
85
96
|
* own scale, so a track can pick its units and conversions can't silently mix them up.
|
|
97
|
+
*
|
|
98
|
+
* The value must be finite and in `[0, Number.MAX_SAFE_INTEGER]`. Fractions are allowed
|
|
99
|
+
* (`performance.now()`, scale conversion). Encoding rounds to the nearest integer at the
|
|
100
|
+
* track timescale. Integer timestamps are exact through `2^53 - 1` units; this package
|
|
101
|
+
* does not use BigInt for timestamps. {@link Timestamp.as} also refuses a converted
|
|
102
|
+
* value that leaves that range, so a legal source timestamp cannot become an unencodable
|
|
103
|
+
* wire timestamp after a scale change.
|
|
86
104
|
*/
|
|
87
105
|
export class Timestamp {
|
|
88
106
|
/** The raw value, in `scale` units. */
|
|
@@ -91,11 +109,9 @@ export class Timestamp {
|
|
|
91
109
|
scale;
|
|
92
110
|
/** Build a timestamp of `value` units at `scale`. */
|
|
93
111
|
constructor(value, scale) {
|
|
94
|
-
|
|
95
|
-
throw new Error(`invalid timestamp: ${value}`);
|
|
96
|
-
}
|
|
112
|
+
assertTimestampValue(value);
|
|
97
113
|
this.value = value;
|
|
98
|
-
this.scale = scale;
|
|
114
|
+
this.scale = Timescale(scale);
|
|
99
115
|
}
|
|
100
116
|
/** Monotonic now (`performance.now()`, milliseconds since page load), not wall-clock time. */
|
|
101
117
|
static now() {
|
|
@@ -109,9 +125,12 @@ export class Timestamp {
|
|
|
109
125
|
static fromMicros(us) {
|
|
110
126
|
return new Timestamp(us, Timescale.MICRO);
|
|
111
127
|
}
|
|
112
|
-
/** This timestamp's value re-expressed at `scale
|
|
128
|
+
/** This timestamp's value re-expressed at `scale`; throws if the result is not in the safe range. */
|
|
113
129
|
as(scale) {
|
|
114
|
-
|
|
130
|
+
const dest = Timescale(scale);
|
|
131
|
+
const converted = dest === this.scale ? this.value : (this.value * dest) / this.scale;
|
|
132
|
+
assertTimestampValue(converted);
|
|
133
|
+
return converted;
|
|
115
134
|
}
|
|
116
135
|
/** The value in milliseconds. */
|
|
117
136
|
asMillis() {
|
package/time.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"time.js","sourceRoot":"","sources":["../src/time.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAKH,yEAAyE;AACzE,+FAA+F;AAC/F,oGAAoG;AACpG,MAAM,CAAC,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,KAAa,EAAQ,EAAE,CAAC,KAAa,EAAE;IACzE,IAAI,EAAE,CAAS;IACf,SAAS,EAAE,CAAC,EAAS,EAAQ,EAAE,CAAC,CAAC,EAAE,GAAG,KAAK,CAAS;IACpD,SAAS,EAAE,CAAC,EAAS,EAAQ,EAAE,CAAC,CAAC,EAAE,GAAG,SAAS,CAAS;IACxD,UAAU,EAAE,CAAC,CAAS,EAAQ,EAAE,CAAC,CAAC,CAAC,GAAG,aAAa,CAAS;IAC5D,OAAO,EAAE,CAAC,EAAQ,EAAS,EAAE,CAAC,CAAC,EAAE,GAAG,KAAK,CAAU;IACnD,OAAO,EAAE,CAAC,EAAQ,EAAS,EAAE,CAAC,CAAC,EAAE,GAAG,SAAS,CAAU;IACvD,QAAQ,EAAE,CAAC,EA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G,EAAE,CAAC,CAAS,EAAE,CAAS,EAAU,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAW;IACxD,GAAG,EAAE,CAAC,CAAS,EAAE,CAAS,EAAU,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAW;IAC/D,GAAG,EAAE,CAAC,CAAS,EAAE,CAAS,EAAU,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAW;CAC/D,CAAC,CAAC","sourcesContent":["/**\n * Branded time types (nanoseconds, microseconds, milliseconds, seconds) with conversions,\n * plus {@link Timescale} and {@link Timestamp} for presentation timing.\n *\n * @module\n */\n\n/** A duration in nanoseconds, branded so it can't be mixed with other units. */\nexport type Nano = number & { readonly _brand: \"nano\" };\n\n/** Constructors, conversions, and arithmetic for {@link Nano} values. */\n// Calling `Nano(x)` brands a raw number as nanoseconds. The unit is the caller's assertion: no\n// conversion happens, so reach for `fromMicro`/`fromMilli`/`fromSecond` when the source has a unit.\nexport const Nano = Object.assign((value: number): Nano => value as Nano, {\n\tzero: 0 as Nano,\n\tfromMicro: (us: Micro): Nano => (us * 1_000) as Nano,\n\tfromMilli: (ms: Milli): Nano => (ms * 1_000_000) as Nano,\n\tfromSecond: (s: Second): Nano => (s * 1_000_000_000) as Nano,\n\ttoMicro: (ns: Nano): Micro => (ns / 1_000) as Micro,\n\ttoMilli: (ns: Nano): Milli => (ns / 1_000_000) as Milli,\n\ttoSecond: (ns: Nano): Second => (ns / 1_000_000_000) as Second,\n\tnow: (): Nano => (performance.now() * 1_000_000) as Nano,\n\tadd: (a: Nano, b: Nano): Nano => (a + b) as Nano,\n\tsub: (a: Nano, b: Nano): Nano => (a - b) as Nano,\n\tmul: (a: Nano, b: number): Nano => (a * b) as Nano,\n\tdiv: (a: Nano, b: number): Nano => (a / b) as Nano,\n\tmax: (a: Nano, b: Nano): Nano => Math.max(a, b) as Nano,\n\tmin: (a: Nano, b: Nano): Nano => Math.min(a, b) as Nano,\n});\n\n/** A duration in microseconds, branded so it can't be mixed with other units. */\nexport type Micro = number & { readonly _brand: \"micro\" };\n\n/** Constructors, conversions, and arithmetic for {@link Micro} values. */\n// Calling `Micro(x)` brands a raw number as microseconds. See the `Nano` note: this asserts the unit\n// rather than converting, so use `fromNano`/`fromMilli`/`fromSecond` to convert from another unit.\nexport const Micro = Object.assign((value: number): Micro => value as Micro, {\n\tzero: 0 as Micro,\n\tfromNano: (ns: Nano): Micro => (ns / 1_000) as Micro,\n\tfromMilli: (ms: Milli): Micro => (ms * 1_000) as Micro,\n\tfromSecond: (s: Second): Micro => (s * 1_000_000) as Micro,\n\ttoNano: (us: Micro): Nano => (us * 1_000) as Nano,\n\ttoMilli: (us: Micro): Milli => (us / 1_000) as Milli,\n\ttoSecond: (us: Micro): Second => (us / 1_000_000) as Second,\n\tnow: (): Micro => (performance.now() * 1_000) as Micro,\n\tadd: (a: Micro, b: Micro): Micro => (a + b) as Micro,\n\tsub: (a: Micro, b: Micro): Micro => (a - b) as Micro,\n\tmul: (a: Micro, b: number): Micro => (a * b) as Micro,\n\tdiv: (a: Micro, b: number): Micro => (a / b) as Micro,\n\tmax: (a: Micro, b: Micro): Micro => Math.max(a, b) as Micro,\n\tmin: (a: Micro, b: Micro): Micro => Math.min(a, b) as Micro,\n});\n\n/** A duration in milliseconds, branded so it can't be mixed with other units. */\nexport type Milli = number & { readonly _brand: \"milli\" };\n\n/** Constructors, conversions, and arithmetic for {@link Milli} values. */\n// Calling `Milli(x)` brands a raw number as milliseconds. See the `Nano` note: this asserts the unit\n// rather than converting, so use `fromNano`/`fromMicro`/`fromSecond` to convert from another unit.\nexport const Milli = Object.assign((value: number): Milli => value as Milli, {\n\tzero: 0 as Milli,\n\tfromNano: (ns: Nano): Milli => (ns / 1_000_000) as Milli,\n\tfromMicro: (us: Micro): Milli => (us / 1_000) as Milli,\n\tfromSecond: (s: Second): Milli => (s * 1_000) as Milli,\n\ttoNano: (ms: Milli): Nano => (ms * 1_000_000) as Nano,\n\ttoMicro: (ms: Milli): Micro => (ms * 1_000) as Micro,\n\ttoSecond: (ms: Milli): Second => (ms / 1_000) as Second,\n\tnow: (): Milli => performance.now() as Milli,\n\tadd: (a: Milli, b: Milli): Milli => (a + b) as Milli,\n\tsub: (a: Milli, b: Milli): Milli => (a - b) as Milli,\n\tmul: (a: Milli, b: number): Milli => (a * b) as Milli,\n\tdiv: (a: Milli, b: number): Milli => (a / b) as Milli,\n\tmax: (a: Milli, b: Milli): Milli => Math.max(a, b) as Milli,\n\tmin: (a: Milli, b: Milli): Milli => Math.min(a, b) as Milli,\n});\n\n/** Units per second for a {@link Timestamp}'s value, e.g. `1000` for milliseconds. */\nexport type Timescale = number & { readonly _brand: \"timescale\" };\n\n/** Named timescales and a checked constructor (rejects non-positive / non-integer values). */\nexport const Timescale = Object.assign(\n\t(unitsPerSecond: number): Timescale => {\n\t\tif (!Number.isInteger(unitsPerSecond) || unitsPerSecond <= 0) {\n\t\t\tthrow new Error(`invalid timescale: ${unitsPerSecond}`);\n\t\t}\n\t\treturn unitsPerSecond as Timescale;\n\t},\n\t{\n\t\t/** One unit per second. */\n\t\tSECOND: 1 as Timescale,\n\t\t/** 1,000 units per second. */\n\t\tMILLI: 1_000 as Timescale,\n\t\t/** 1,000,000 units per second. */\n\t\tMICRO: 1_000_000 as Timescale,\n\t\t/** 1,000,000,000 units per second. */\n\t\tNANO: 1_000_000_000 as Timescale,\n\t},\n);\n\n/**\n * A presentation timestamp: a raw value in a given {@link Timescale}.\n *\n * Mirrors the Rust `Timestamp`. Unlike the bare `Milli`/`Micro` aliases it carries its\n * own scale, so a track can pick its units and conversions can't silently mix them up.\n */\nexport class Timestamp {\n\t/** The raw value, in `scale` units. */\n\treadonly value: number;\n\t/** Units per second the {@link value} is measured in. */\n\treadonly scale: Timescale;\n\n\t/** Build a timestamp of `value` units at `scale`. */\n\tconstructor(value: number, scale: Timescale) {\n\t\tif (!Number.isFinite(value) || value < 0) {\n\t\t\tthrow new Error(`invalid timestamp: ${value}`);\n\t\t}\n\t\tthis.value = value;\n\t\tthis.scale = scale;\n\t}\n\n\t/** Monotonic now (`performance.now()`, milliseconds since page load), not wall-clock time. */\n\tstatic now(): Timestamp {\n\t\treturn new Timestamp(performance.now(), Timescale.MILLI);\n\t}\n\n\t/** A timestamp of `ms` milliseconds. */\n\tstatic fromMillis(ms: number): Timestamp {\n\t\treturn new Timestamp(ms, Timescale.MILLI);\n\t}\n\n\t/** A timestamp of `us` microseconds. */\n\tstatic fromMicros(us: number): Timestamp {\n\t\treturn new Timestamp(us, Timescale.MICRO);\n\t}\n\n\t/** This timestamp's value re-expressed at `scale` (a raw number, not a new Timestamp). */\n\tas(scale: Timescale): number {\n\t\treturn scale === this.scale ? this.value : (this.value * scale) / this.scale;\n\t}\n\n\t/** The value in milliseconds. */\n\tasMillis(): number {\n\t\treturn this.as(Timescale.MILLI);\n\t}\n\n\t/** The value in microseconds. */\n\tasMicros(): number {\n\t\treturn this.as(Timescale.MICRO);\n\t}\n}\n\n/** A duration in seconds, branded so it can't be mixed with other units. */\nexport type Second = number & { readonly _brand: \"second\" };\n\n/** Constructors, conversions, and arithmetic for {@link Second} values. */\n// Calling `Second(x)` brands a raw number as seconds. See the `Nano` note: this asserts the unit\n// rather than converting, so use `fromNano`/`fromMicro`/`fromMilli` to convert from another unit.\nexport const Second = Object.assign((value: number): Second => value as Second, {\n\tzero: 0 as Second,\n\tfromNano: (ns: Nano): Second => (ns / 1_000_000_000) as Second,\n\tfromMicro: (us: Micro): Second => (us / 1_000_000) as Second,\n\tfromMilli: (ms: Milli): Second => (ms / 1_000) as Second,\n\ttoNano: (s: Second): Nano => (s * 1_000_000_000) as Nano,\n\ttoMicro: (s: Second): Micro => (s * 1_000_000) as Micro,\n\ttoMilli: (s: Second): Milli => (s * 1_000) as Milli,\n\tnow: (): Second => (performance.now() / 1_000) as Second,\n\tadd: (a: Second, b: Second): Second => (a + b) as Second,\n\tsub: (a: Second, b: Second): Second => (a - b) as Second,\n\tmul: (a: Second, b: number): Second => (a * b) as Second,\n\tdiv: (a: Second, b: number): Second => (a / b) as Second,\n\tmax: (a: Second, b: Second): Second => Math.max(a, b) as Second,\n\tmin: (a: Second, b: Second): Second => Math.min(a, b) as Second,\n});\n"]}
|
|
1
|
+
{"version":3,"file":"time.js","sourceRoot":"","sources":["../src/time.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAKH,yEAAyE;AACzE,+FAA+F;AAC/F,oGAAoG;AACpG,MAAM,CAAC,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,KAAa,EAAQ,EAAE,CAAC,KAAa,EAAE;IACzE,IAAI,EAAE,CAAS;IACf,SAAS,EAAE,CAAC,EAAS,EAAQ,EAAE,CAAC,CAAC,EAAE,GAAG,KAAK,CAAS;IACpD,SAAS,EAAE,CAAC,EAAS,EAAQ,EAAE,CAAC,CAAC,EAAE,GAAG,SAAS,CAAS;IACxD,UAAU,EAAE,CAAC,CAAS,EAAQ,EAAE,CAAC,CAAC,CAAC,GAAG,aAAa,CAAS;IAC5D,OAAO,EAAE,CAAC,EAAQ,EAAS,EAAE,CAAC,CAAC,EAAE,GAAG,KAAK,CAAU;IACnD,OAAO,EAAE,CAAC,EAAQ,EAAS,EAAE,CAAC,CAAC,EAAE,GAAG,SAAS,CAAU;IACvD,QAAQ,EAAE,CAAC,EAAQ,EAAU,EAAE,CAAC,CAAC,EAAE,GAAG,aAAa,CAAW;IAC9D,GAAG,EAAE,GAAS,EAAE,CAAC,CAAC,WAAW,CAAC,GAAG,EAAE,GAAG,SAAS,CAAS;IACxD,GAAG,EAAE,CAAC,CAAO,EAAE,CAAO,EAAQ,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAS;IAChD,GAAG,EAAE,CAAC,CAAO,EAAE,CAAO,EAAQ,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAS;IAChD,GAAG,EAAE,CAAC,CAAO,EAAE,CAAS,EAAQ,EAAE,CAA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AC,CAAS,EAAE,CAAS,EAAU,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAW;IAC/D,GAAG,EAAE,CAAC,CAAS,EAAE,CAAS,EAAU,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAW;CAC/D,CAAC,CAAC","sourcesContent":["/**\n * Branded time types (nanoseconds, microseconds, milliseconds, seconds) with conversions,\n * plus {@link Timescale} and {@link Timestamp} for presentation timing.\n *\n * @module\n */\n\n/** A duration in nanoseconds, branded so it can't be mixed with other units. */\nexport type Nano = number & { readonly _brand: \"nano\" };\n\n/** Constructors, conversions, and arithmetic for {@link Nano} values. */\n// Calling `Nano(x)` brands a raw number as nanoseconds. The unit is the caller's assertion: no\n// conversion happens, so reach for `fromMicro`/`fromMilli`/`fromSecond` when the source has a unit.\nexport const Nano = Object.assign((value: number): Nano => value as Nano, {\n\tzero: 0 as Nano,\n\tfromMicro: (us: Micro): Nano => (us * 1_000) as Nano,\n\tfromMilli: (ms: Milli): Nano => (ms * 1_000_000) as Nano,\n\tfromSecond: (s: Second): Nano => (s * 1_000_000_000) as Nano,\n\ttoMicro: (ns: Nano): Micro => (ns / 1_000) as Micro,\n\ttoMilli: (ns: Nano): Milli => (ns / 1_000_000) as Milli,\n\ttoSecond: (ns: Nano): Second => (ns / 1_000_000_000) as Second,\n\tnow: (): Nano => (performance.now() * 1_000_000) as Nano,\n\tadd: (a: Nano, b: Nano): Nano => (a + b) as Nano,\n\tsub: (a: Nano, b: Nano): Nano => (a - b) as Nano,\n\tmul: (a: Nano, b: number): Nano => (a * b) as Nano,\n\tdiv: (a: Nano, b: number): Nano => (a / b) as Nano,\n\tmax: (a: Nano, b: Nano): Nano => Math.max(a, b) as Nano,\n\tmin: (a: Nano, b: Nano): Nano => Math.min(a, b) as Nano,\n});\n\n/** A duration in microseconds, branded so it can't be mixed with other units. */\nexport type Micro = number & { readonly _brand: \"micro\" };\n\n/** Constructors, conversions, and arithmetic for {@link Micro} values. */\n// Calling `Micro(x)` brands a raw number as microseconds. See the `Nano` note: this asserts the unit\n// rather than converting, so use `fromNano`/`fromMilli`/`fromSecond` to convert from another unit.\nexport const Micro = Object.assign((value: number): Micro => value as Micro, {\n\tzero: 0 as Micro,\n\tfromNano: (ns: Nano): Micro => (ns / 1_000) as Micro,\n\tfromMilli: (ms: Milli): Micro => (ms * 1_000) as Micro,\n\tfromSecond: (s: Second): Micro => (s * 1_000_000) as Micro,\n\ttoNano: (us: Micro): Nano => (us * 1_000) as Nano,\n\ttoMilli: (us: Micro): Milli => (us / 1_000) as Milli,\n\ttoSecond: (us: Micro): Second => (us / 1_000_000) as Second,\n\tnow: (): Micro => (performance.now() * 1_000) as Micro,\n\tadd: (a: Micro, b: Micro): Micro => (a + b) as Micro,\n\tsub: (a: Micro, b: Micro): Micro => (a - b) as Micro,\n\tmul: (a: Micro, b: number): Micro => (a * b) as Micro,\n\tdiv: (a: Micro, b: number): Micro => (a / b) as Micro,\n\tmax: (a: Micro, b: Micro): Micro => Math.max(a, b) as Micro,\n\tmin: (a: Micro, b: Micro): Micro => Math.min(a, b) as Micro,\n});\n\n/** A duration in milliseconds, branded so it can't be mixed with other units. */\nexport type Milli = number & { readonly _brand: \"milli\" };\n\n/** Constructors, conversions, and arithmetic for {@link Milli} values. */\n// Calling `Milli(x)` brands a raw number as milliseconds. See the `Nano` note: this asserts the unit\n// rather than converting, so use `fromNano`/`fromMicro`/`fromSecond` to convert from another unit.\nexport const Milli = Object.assign((value: number): Milli => value as Milli, {\n\tzero: 0 as Milli,\n\tfromNano: (ns: Nano): Milli => (ns / 1_000_000) as Milli,\n\tfromMicro: (us: Micro): Milli => (us / 1_000) as Milli,\n\tfromSecond: (s: Second): Milli => (s * 1_000) as Milli,\n\ttoNano: (ms: Milli): Nano => (ms * 1_000_000) as Nano,\n\ttoMicro: (ms: Milli): Micro => (ms * 1_000) as Micro,\n\ttoSecond: (ms: Milli): Second => (ms / 1_000) as Second,\n\tnow: (): Milli => performance.now() as Milli,\n\tadd: (a: Milli, b: Milli): Milli => (a + b) as Milli,\n\tsub: (a: Milli, b: Milli): Milli => (a - b) as Milli,\n\tmul: (a: Milli, b: number): Milli => (a * b) as Milli,\n\tdiv: (a: Milli, b: number): Milli => (a / b) as Milli,\n\tmax: (a: Milli, b: Milli): Milli => Math.max(a, b) as Milli,\n\tmin: (a: Milli, b: Milli): Milli => Math.min(a, b) as Milli,\n});\n\n/** Units per second for a {@link Timestamp}'s value, e.g. `1000` for milliseconds. */\nexport type Timescale = number & { readonly _brand: \"timescale\" };\n\n/**\n * Named timescales and a checked constructor.\n *\n * Units per second must be a safe integer ≥ 1. JavaScript numbers are exact only\n * through `Number.MAX_SAFE_INTEGER` (`2^53 - 1`); the wire varint range goes\n * to `2^62 - 1`, which Rust accepts and this constructor refuses.\n */\nexport const Timescale = Object.assign(\n\t(unitsPerSecond: number): Timescale => {\n\t\tif (!Number.isSafeInteger(unitsPerSecond) || unitsPerSecond <= 0) {\n\t\t\tthrow new RangeError(`invalid timescale: ${unitsPerSecond}`);\n\t\t}\n\t\treturn unitsPerSecond as Timescale;\n\t},\n\t{\n\t\t/** One unit per second. */\n\t\tSECOND: 1 as Timescale,\n\t\t/** 1,000 units per second. */\n\t\tMILLI: 1_000 as Timescale,\n\t\t/** 1,000,000 units per second. */\n\t\tMICRO: 1_000_000 as Timescale,\n\t\t/** 1,000,000,000 units per second. */\n\t\tNANO: 1_000_000_000 as Timescale,\n\t},\n);\n\nfunction assertTimestampValue(value: number): void {\n\tif (!Number.isFinite(value) || value < 0 || value > Number.MAX_SAFE_INTEGER) {\n\t\tthrow new RangeError(`invalid timestamp: ${value}`);\n\t}\n}\n\n/**\n * A presentation timestamp: a raw value in a given {@link Timescale}.\n *\n * Mirrors the Rust `Timestamp`. Unlike the bare `Milli`/`Micro` aliases it carries its\n * own scale, so a track can pick its units and conversions can't silently mix them up.\n *\n * The value must be finite and in `[0, Number.MAX_SAFE_INTEGER]`. Fractions are allowed\n * (`performance.now()`, scale conversion). Encoding rounds to the nearest integer at the\n * track timescale. Integer timestamps are exact through `2^53 - 1` units; this package\n * does not use BigInt for timestamps. {@link Timestamp.as} also refuses a converted\n * value that leaves that range, so a legal source timestamp cannot become an unencodable\n * wire timestamp after a scale change.\n */\nexport class Timestamp {\n\t/** The raw value, in `scale` units. */\n\treadonly value: number;\n\t/** Units per second the {@link value} is measured in. */\n\treadonly scale: Timescale;\n\n\t/** Build a timestamp of `value` units at `scale`. */\n\tconstructor(value: number, scale: Timescale) {\n\t\tassertTimestampValue(value);\n\t\tthis.value = value;\n\t\tthis.scale = Timescale(scale);\n\t}\n\n\t/** Monotonic now (`performance.now()`, milliseconds since page load), not wall-clock time. */\n\tstatic now(): Timestamp {\n\t\treturn new Timestamp(performance.now(), Timescale.MILLI);\n\t}\n\n\t/** A timestamp of `ms` milliseconds. */\n\tstatic fromMillis(ms: number): Timestamp {\n\t\treturn new Timestamp(ms, Timescale.MILLI);\n\t}\n\n\t/** A timestamp of `us` microseconds. */\n\tstatic fromMicros(us: number): Timestamp {\n\t\treturn new Timestamp(us, Timescale.MICRO);\n\t}\n\n\t/** This timestamp's value re-expressed at `scale`; throws if the result is not in the safe range. */\n\tas(scale: Timescale): number {\n\t\tconst dest = Timescale(scale);\n\t\tconst converted = dest === this.scale ? this.value : (this.value * dest) / this.scale;\n\t\tassertTimestampValue(converted);\n\t\treturn converted;\n\t}\n\n\t/** The value in milliseconds. */\n\tasMillis(): number {\n\t\treturn this.as(Timescale.MILLI);\n\t}\n\n\t/** The value in microseconds. */\n\tasMicros(): number {\n\t\treturn this.as(Timescale.MICRO);\n\t}\n}\n\n/** A duration in seconds, branded so it can't be mixed with other units. */\nexport type Second = number & { readonly _brand: \"second\" };\n\n/** Constructors, conversions, and arithmetic for {@link Second} values. */\n// Calling `Second(x)` brands a raw number as seconds. See the `Nano` note: this asserts the unit\n// rather than converting, so use `fromNano`/`fromMicro`/`fromMilli` to convert from another unit.\nexport const Second = Object.assign((value: number): Second => value as Second, {\n\tzero: 0 as Second,\n\tfromNano: (ns: Nano): Second => (ns / 1_000_000_000) as Second,\n\tfromMicro: (us: Micro): Second => (us / 1_000_000) as Second,\n\tfromMilli: (ms: Milli): Second => (ms / 1_000) as Second,\n\ttoNano: (s: Second): Nano => (s * 1_000_000_000) as Nano,\n\ttoMicro: (s: Second): Micro => (s * 1_000_000) as Micro,\n\ttoMilli: (s: Second): Milli => (s * 1_000) as Milli,\n\tnow: (): Second => (performance.now() / 1_000) as Second,\n\tadd: (a: Second, b: Second): Second => (a + b) as Second,\n\tsub: (a: Second, b: Second): Second => (a - b) as Second,\n\tmul: (a: Second, b: number): Second => (a * b) as Second,\n\tdiv: (a: Second, b: number): Second => (a / b) as Second,\n\tmax: (a: Second, b: Second): Second => Math.max(a, b) as Second,\n\tmin: (a: Second, b: Second): Second => Math.min(a, b) as Second,\n});\n"]}
|