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 <nanomiratus@gmail.com> Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Nano Miratus
Claude Opus 4.6
parent
b790c804ee
commit
0015710909
@@ -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 - <ref *1> { a: 1, b: [Circular *1] } 1`] = `'[{"a":1,"b":0},1]'`;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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,
|
||||
];
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user