diff --git a/js/net/src/ietf/object.test.ts b/js/net/src/ietf/object.test.ts new file mode 100644 index 00000000000..ba6cec1acdb --- /dev/null +++ b/js/net/src/ietf/object.test.ts @@ -0,0 +1,32 @@ +import { expect, test } from "bun:test"; +import { Cursor } from "../stream.ts"; +import { Timescale, Timestamp } from "../time.ts"; +import { decodeObjectTime, encodeObjectExtensions } from "./object.ts"; +import { Version } from "./version.ts"; + +const VERSION = Version.DRAFT_17; + +/** `timestamp` written at `timescale` and read back. */ +async function roundtrip(timestamp: Timestamp, timescale: Timescale): Promise { + const properties = await encodeObjectExtensions(timestamp, timescale, VERSION); + return decodeObjectTime(new Cursor(properties, VERSION), timescale); +} + +test("a same-scale timestamp is written exactly, up to the largest a Timestamp holds", async () => { + for (const value of [0, 1, 9_007_199_254_740_985, Number.MAX_SAFE_INTEGER]) { + const read = await roundtrip(new Timestamp(value, Timescale.NANO), Timescale.NANO); + expect(read?.value).toBe(value); + } +}); + +test("a timestamp is converted to the track's scale", async () => { + expect((await roundtrip(Timestamp.fromMillis(1234), Timescale.NANO))?.value).toBe(1_234_000_000); + expect((await roundtrip(Timestamp.fromMicros(1_500), Timescale.MILLI))?.value).toBe(2); +}); + +test("a conversion past what a Timestamp holds is refused before anything is written", async () => { + // 10^10 ms is 10^16 ns, past 2^53. + await expect(encodeObjectExtensions(Timestamp.fromMillis(10_000_000_000), Timescale.NANO, VERSION)).rejects.toThrow( + RangeError, + ); +}); diff --git a/js/net/src/ietf/object.ts b/js/net/src/ietf/object.ts index dae2c83b0f0..4315e666ea4 100644 --- a/js/net/src/ietf/object.ts +++ b/js/net/src/ietf/object.ts @@ -59,7 +59,9 @@ async function encodeObjectTime( timescale: Timescale, version: IetfVersion, ): Promise { - const value = Math.round((timestamp.value * timescale) / timestamp.scale); + // The shared conversion: exact at the same scale, and refused before anything is written when + // the result leaves the range a Timestamp, and so the receiver, can hold. + const value = Math.round(timestamp.as(timescale)); await encodeObjectPropertyType(w, PROP_TIMESTAMP, 0n, version); await w.u62(BigInt(value)); } diff --git a/quest/m1/release-audit/README.md b/quest/m1/release-audit/README.md index 7a5bccd119b..fc27d074b8f 100644 --- a/quest/m1/release-audit/README.md +++ b/quest/m1/release-audit/README.md @@ -110,7 +110,6 @@ child records whether its behavior is new on main or inherited from release. - [Release finished FFI codecs](/quest/m1/release-audit/ffi-finished.md) - preserving finished tracks does not preserve drained encoders and scratch buffers - [Release HLS server state](/quest/m1/release-audit/hls-shutdown.md) - final server drop retires its watchers while publishers remain live - [Retire expiry metadata](/quest/m1/release-audit/expiry-index.md) - empty deadline entries do not accumulate with historical read budgets -- [Preserve IETF timestamps](/quest/m1/release-audit/ietf-timestamps.md) - JS encoding uses exact same-scale conversion and rejects unsupported values before sending - [Settle Go reservation release](/quest/m1/release-audit/go-reservation.md) - investigate prompt allocation release through the public wrapper - [Settle finite HLS import](/quest/m1/release-audit/hls-finite.md) - initially ended playlists have an explicit and tested start position - [Settle HLS timestamp resets](/quest/m1/release-audit/hls-discontinuity.md) - unchanged-map discontinuities preserve timeline identity or are explicitly refused diff --git a/quest/m1/release-audit/ietf-timestamps.md b/quest/m1/release-audit/ietf-timestamps.md deleted file mode 100644 index 2c7cc900434..00000000000 --- a/quest/m1/release-audit/ietf-timestamps.md +++ /dev/null @@ -1,30 +0,0 @@ -# [S] Preserve timestamp conversion rules in JS IETF objects - -## Goal - -The JS IETF object encoder preserves exact same-scale timestamps and refuses -unrepresentable conversions before emitting data its receiver rejects. - -## Plan - -P2 inherited correctness defect at main 1d43c096e0. -`js/net/src/ietf/object.ts::encodeObjectTime` multiplies and divides Numbers -directly, bypassing Timestamp.as and its same-scale shortcut and range check. -A valid NANO timestamp 9007199254740985 roundtrips as 9007199254740984 through -IETF. The normal conversion, Lite path, and native same-scale conversion -preserve it. This becomes reachable around 104 days on a nanosecond timeline. - -Converting Timestamp.fromMillis(10_000_000_000) to the IETF nanosecond scale -also emits 10^16 ticks, exceeding the JS Timestamp range. Its own decoder -rejects the result, whereas Timestamp.as and Lite reject the conversion -before sending. These are local encoder/decoder reproductions using valid -API inputs, not end-to-end transport tests. - -Use the shared timestamp conversion contract in IETF framing. Test exact -same-scale boundaries, supported conversions, and refusal before encoding -out-of-range results. Keep supported wire values unchanged; report any -observable error-behavior change. No new timestamp API is needed. - -## Related - -- [Binding timestamps](/quest/m1/binding-timestamps.md) - exact values through language bindings, separate from JS protocol encoding