Skip to content

Commit 4788556

Browse files
committed
[Flight] Serialize Date (#26622)
This is kind of annoying because Date implements toJSON so JSON.stringify turns it into a string before calling our replacer function. DiffTrain build for [c6db19f9cdec34bca3625a483a2f85181193b885](facebook/react@c6db19f)
1 parent 3bf9c64 commit 4788556

9 files changed

+73
-7
lines changed

compiled/facebook-www/REVISION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
b433c379d55d9684945217c7d375de1082a1abb8
1+
c6db19f9cdec34bca3625a483a2f85181193b885

compiled/facebook-www/ReactFlightDOMRelayClient-dev.classic.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,11 @@ function parseModelString(response, parentObject, key, value) {
583583
return undefined;
584584
}
585585

586+
case "D": {
587+
// Date
588+
return new Date(Date.parse(value.substring(2)));
589+
}
590+
586591
case "n": {
587592
// BigInt
588593
return BigInt(value.substring(2));

compiled/facebook-www/ReactFlightDOMRelayClient-dev.modern.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,11 @@ function parseModelString(response, parentObject, key, value) {
583583
return undefined;
584584
}
585585

586+
case "D": {
587+
// Date
588+
return new Date(Date.parse(value.substring(2)));
589+
}
590+
586591
case "n": {
587592
// BigInt
588593
return BigInt(value.substring(2));

compiled/facebook-www/ReactFlightDOMRelayClient-prod.classic.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ function parseModelString(response, parentObject, key, value) {
293293
return NaN;
294294
case "u":
295295
return;
296+
case "D":
297+
return new Date(Date.parse(value.substring(2)));
296298
case "n":
297299
return BigInt(value.substring(2));
298300
default:

compiled/facebook-www/ReactFlightDOMRelayClient-prod.modern.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ function parseModelString(response, parentObject, key, value) {
293293
return NaN;
294294
case "u":
295295
return;
296+
case "D":
297+
return new Date(Date.parse(value.substring(2)));
296298
case "n":
297299
return BigInt(value.substring(2));
298300
default:

compiled/facebook-www/ReactFlightDOMRelayServer-dev.classic.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1464,6 +1464,12 @@ function serializeUndefined() {
14641464
return "$undefined";
14651465
}
14661466

1467+
function serializeDateFromDateJSON(dateJSON) {
1468+
// JSON.stringify automatically calls Date.prototype.toJSON which calls toISOString.
1469+
// We need only tack on a $D prefix.
1470+
return "$D" + dateJSON;
1471+
}
1472+
14671473
function serializeBigInt(n) {
14681474
return "$n" + n.toString(10);
14691475
}
@@ -1536,11 +1542,16 @@ function escapeStringValue(value) {
15361542
var insideContextProps = null;
15371543
var isInsideContextValue = false;
15381544
function resolveModelToJSON(request, parent, key, value) {
1545+
// Make sure that `parent[key]` wasn't JSONified before `value` was passed to us
15391546
{
15401547
// $FlowFixMe[incompatible-use]
15411548
var originalValue = parent[key];
15421549

1543-
if (typeof originalValue === "object" && originalValue !== value) {
1550+
if (
1551+
typeof originalValue === "object" &&
1552+
originalValue !== value &&
1553+
!(originalValue instanceof Date)
1554+
) {
15441555
if (objectName(originalValue) !== "Object") {
15451556
var jsxParentType = jsxChildrenParents.get(parent);
15461557

@@ -1748,6 +1759,17 @@ function resolveModelToJSON(request, parent, key, value) {
17481759
}
17491760

17501761
if (typeof value === "string") {
1762+
// TODO: Maybe too clever. If we support URL there's no similar trick.
1763+
if (value[value.length - 1] === "Z") {
1764+
// Possibly a Date, whose toJSON automatically calls toISOString
1765+
// $FlowFixMe[incompatible-use]
1766+
var _originalValue = parent[key]; // $FlowFixMe[method-unbinding]
1767+
1768+
if (_originalValue instanceof Date) {
1769+
return serializeDateFromDateJSON(value);
1770+
}
1771+
}
1772+
17511773
return escapeStringValue(value);
17521774
}
17531775

compiled/facebook-www/ReactFlightDOMRelayServer-dev.modern.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1464,6 +1464,12 @@ function serializeUndefined() {
14641464
return "$undefined";
14651465
}
14661466

1467+
function serializeDateFromDateJSON(dateJSON) {
1468+
// JSON.stringify automatically calls Date.prototype.toJSON which calls toISOString.
1469+
// We need only tack on a $D prefix.
1470+
return "$D" + dateJSON;
1471+
}
1472+
14671473
function serializeBigInt(n) {
14681474
return "$n" + n.toString(10);
14691475
}
@@ -1536,11 +1542,16 @@ function escapeStringValue(value) {
15361542
var insideContextProps = null;
15371543
var isInsideContextValue = false;
15381544
function resolveModelToJSON(request, parent, key, value) {
1545+
// Make sure that `parent[key]` wasn't JSONified before `value` was passed to us
15391546
{
15401547
// $FlowFixMe[incompatible-use]
15411548
var originalValue = parent[key];
15421549

1543-
if (typeof originalValue === "object" && originalValue !== value) {
1550+
if (
1551+
typeof originalValue === "object" &&
1552+
originalValue !== value &&
1553+
!(originalValue instanceof Date)
1554+
) {
15441555
if (objectName(originalValue) !== "Object") {
15451556
var jsxParentType = jsxChildrenParents.get(parent);
15461557

@@ -1748,6 +1759,17 @@ function resolveModelToJSON(request, parent, key, value) {
17481759
}
17491760

17501761
if (typeof value === "string") {
1762+
// TODO: Maybe too clever. If we support URL there's no similar trick.
1763+
if (value[value.length - 1] === "Z") {
1764+
// Possibly a Date, whose toJSON automatically calls toISOString
1765+
// $FlowFixMe[incompatible-use]
1766+
var _originalValue = parent[key]; // $FlowFixMe[method-unbinding]
1767+
1768+
if (_originalValue instanceof Date) {
1769+
return serializeDateFromDateJSON(value);
1770+
}
1771+
}
1772+
17511773
return escapeStringValue(value);
17521774
}
17531775

compiled/facebook-www/ReactFlightDOMRelayServer-prod.classic.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -751,8 +751,12 @@ function resolveModelToJSON(request, parent, key, value) {
751751
? Array.from(value)
752752
: value;
753753
}
754-
if ("string" === typeof value)
755-
return (request = "$" === value[0] ? "$" + value : value), request;
754+
if ("string" === typeof value) {
755+
if ("Z" === value[value.length - 1] && parent[key] instanceof Date)
756+
return "$D" + value;
757+
request = "$" === value[0] ? "$" + value : value;
758+
return request;
759+
}
756760
if ("boolean" === typeof value) return value;
757761
if ("number" === typeof value)
758762
return (

compiled/facebook-www/ReactFlightDOMRelayServer-prod.modern.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -751,8 +751,12 @@ function resolveModelToJSON(request, parent, key, value) {
751751
? Array.from(value)
752752
: value;
753753
}
754-
if ("string" === typeof value)
755-
return (request = "$" === value[0] ? "$" + value : value), request;
754+
if ("string" === typeof value) {
755+
if ("Z" === value[value.length - 1] && parent[key] instanceof Date)
756+
return "$D" + value;
757+
request = "$" === value[0] ? "$" + value : value;
758+
return request;
759+
}
756760
if ("boolean" === typeof value) return value;
757761
if ("number" === typeof value)
758762
return (

0 commit comments

Comments
 (0)