From 001571090915fd38c1b835fee134897d1762c951 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sun, 29 Mar 2026 08:23:38 +0000 Subject: [PATCH] feat: allow passing Temporal objects to islands (#3701) ## Summary - Add serialization/deserialization for all 8 Temporal types in the jsonify module (used for island props): Instant, ZonedDateTime, PlainDate, PlainTime, PlainDateTime, PlainYearMonth, PlainMonthDay, Duration - Stringify side guards with `typeof Temporal !== "undefined"` to avoid ReferenceError when Temporal is unavailable - Uses `.toString()` for serialization and `Type.from()` for deserialization Co-authored-by: Nano Miratus Co-authored-by: Claude Opus 4.6 (1M context) --- .../__snapshots__/round_trip_test.ts.snap | 16 +++++++ packages/fresh/src/jsonify/parse.ts | 16 +++++++ packages/fresh/src/jsonify/round_trip_test.ts | 8 ++++ packages/fresh/src/jsonify/stringify.ts | 44 +++++++++++++++++++ 4 files changed, 84 insertions(+) diff --git a/packages/fresh/src/jsonify/__snapshots__/round_trip_test.ts.snap b/packages/fresh/src/jsonify/__snapshots__/round_trip_test.ts.snap index ae21ba2a..63f78585 100644 --- a/packages/fresh/src/jsonify/__snapshots__/round_trip_test.ts.snap +++ b/packages/fresh/src/jsonify/__snapshots__/round_trip_test.ts.snap @@ -54,6 +54,22 @@ snapshot[`round trip - [ 1, <1 empty item>, 3 ] 1`] = `"[[1,-7,2],1,3]"`; snapshot[`round trip - /foo["]/ 1`] = `'[["RegExp","foo[\\\\"]", ""]]'`; +snapshot[`round trip - 2020-01-01T12:34:56.789101112Z 1`] = `'[["Temporal.Instant","2020-01-01T12:34:56.789101112Z"]]'`; + +snapshot[`round trip - 2020-01-01T12:34:56.789101112+00:00[UTC] 1`] = `'[["Temporal.ZonedDateTime","2020-01-01T12:34:56.789101112+00:00[UTC]"]]'`; + +snapshot[`round trip - 2020-01-01 1`] = `'[["Temporal.PlainDate","2020-01-01"]]'`; + +snapshot[`round trip - 12:34:56.789101112 1`] = `'[["Temporal.PlainTime","12:34:56.789101112"]]'`; + +snapshot[`round trip - 2020-01-01T12:34:56.789101112 1`] = `'[["Temporal.PlainDateTime","2020-01-01T12:34:56.789101112"]]'`; + +snapshot[`round trip - 2020-01 1`] = `'[["Temporal.PlainYearMonth","2020-01"]]'`; + +snapshot[`round trip - 01-01 1`] = `'[["Temporal.PlainMonthDay","01-01"]]'`; + +snapshot[`round trip - P1Y2M3W4DT5H6M7.00800901S 1`] = `'[["Temporal.Duration","P1Y2M3W4DT5H6M7.00800901S"]]'`; + snapshot[`round trip - { a: { foo: 123 }, b: [ { foo: 123 }, { foo: 123 } ] } 1`] = `'[{"a":1,"b":3},{"foo":2},123,[1,1]]'`; snapshot[`round trip - { a: 1, b: [Circular *1] } 1`] = `'[{"a":1,"b":0},1]'`; diff --git a/packages/fresh/src/jsonify/parse.ts b/packages/fresh/src/jsonify/parse.ts index 7ba069b1..1fc73ef3 100644 --- a/packages/fresh/src/jsonify/parse.ts +++ b/packages/fresh/src/jsonify/parse.ts @@ -93,6 +93,22 @@ function unpack( } case "Uint8Array": return hydrated[idx] = b64decode(current[1]); + case "Temporal.Instant": + return hydrated[idx] = Temporal.Instant.from(current[1]); + case "Temporal.ZonedDateTime": + return hydrated[idx] = Temporal.ZonedDateTime.from(current[1]); + case "Temporal.PlainDate": + return hydrated[idx] = Temporal.PlainDate.from(current[1]); + case "Temporal.PlainTime": + return hydrated[idx] = Temporal.PlainTime.from(current[1]); + case "Temporal.PlainDateTime": + return hydrated[idx] = Temporal.PlainDateTime.from(current[1]); + case "Temporal.PlainYearMonth": + return hydrated[idx] = Temporal.PlainYearMonth.from(current[1]); + case "Temporal.PlainMonthDay": + return hydrated[idx] = Temporal.PlainMonthDay.from(current[1]); + case "Temporal.Duration": + return hydrated[idx] = Temporal.Duration.from(current[1]); } } else { const actual = new Array(current.length); diff --git a/packages/fresh/src/jsonify/round_trip_test.ts b/packages/fresh/src/jsonify/round_trip_test.ts index c44e5f9b..5c44d042 100644 --- a/packages/fresh/src/jsonify/round_trip_test.ts +++ b/packages/fresh/src/jsonify/round_trip_test.ts @@ -39,6 +39,14 @@ const TESTS = [ new Set([1, 2, null, -2, NaN]), [1, , 3], /foo["]/, + Temporal.Instant.from("2020-01-01T12:34:56.789101112Z"), + Temporal.ZonedDateTime.from("2020-01-01T12:34:56.789101112Z[UTC]"), + Temporal.PlainDate.from("2020-01-01"), + Temporal.PlainTime.from("12:34:56.789101112"), + Temporal.PlainDateTime.from("2020-01-01T12:34:56.789101112"), + Temporal.PlainYearMonth.from("2020-01"), + Temporal.PlainMonthDay.from("2020-01-01"), + Temporal.Duration.from("P1Y2M3W4DT5H6M7.00800901S"), references, circular, ]; diff --git a/packages/fresh/src/jsonify/stringify.ts b/packages/fresh/src/jsonify/stringify.ts index 00231115..cf788070 100644 --- a/packages/fresh/src/jsonify/stringify.ts +++ b/packages/fresh/src/jsonify/stringify.ts @@ -31,6 +31,14 @@ export type Stringifiers = Record< * - `RegExp` * - `Set` * - `Map` + * - `Temporal.Instant` + * - `Temporal.ZonedDateTime` + * - `Temporal.PlainDate` + * - `Temporal.PlainTime` + * - `Temporal.PlainDateTime` + * - `Temporal.PlainYearMonth` + * - `Temporal.PlainMonthDay` + * - `Temporal.Duration` * * Circular references are supported and objects with the same reference are * serialized only once. @@ -134,6 +142,42 @@ function serializeInner( items[i++] = serializeInner(out, indexes, v, custom); }); str += `["Map",[${items.join(",")}]]`; + } else if ( + typeof Temporal !== "undefined" && value instanceof Temporal.Instant + ) { + str += `["Temporal.Instant","${value.toString()}"]`; + } else if ( + typeof Temporal !== "undefined" && + value instanceof Temporal.ZonedDateTime + ) { + str += `["Temporal.ZonedDateTime","${value.toString()}"]`; + } else if ( + typeof Temporal !== "undefined" && value instanceof Temporal.PlainDate + ) { + str += `["Temporal.PlainDate","${value.toString()}"]`; + } else if ( + typeof Temporal !== "undefined" && value instanceof Temporal.PlainTime + ) { + str += `["Temporal.PlainTime","${value.toString()}"]`; + } else if ( + typeof Temporal !== "undefined" && + value instanceof Temporal.PlainDateTime + ) { + str += `["Temporal.PlainDateTime","${value.toString()}"]`; + } else if ( + typeof Temporal !== "undefined" && + value instanceof Temporal.PlainYearMonth + ) { + str += `["Temporal.PlainYearMonth","${value.toString()}"]`; + } else if ( + typeof Temporal !== "undefined" && + value instanceof Temporal.PlainMonthDay + ) { + str += `["Temporal.PlainMonthDay","${value.toString()}"]`; + } else if ( + typeof Temporal !== "undefined" && value instanceof Temporal.Duration + ) { + str += `["Temporal.Duration","${value.toString()}"]`; } else { str += "{"; const keys = Object.keys(value);