Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions packages/react-client/src/ReactFlightClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
enableComponentPerformanceTrack,
enableAsyncDebugInfo,
enableFlightWeakThenables,
enableFlightObjectReferences,
} from 'shared/ReactFeatureFlags';

import {
Expand All @@ -69,6 +70,7 @@ import {
import {
createBoundServerReference,
registerBoundServerReference,
createServerObjectReference,
} from './ReactFlightReplyClient';

import {readTemporaryReference} from './ReactFlightTemporaryReferences';
Expand Down Expand Up @@ -2226,6 +2228,28 @@ function loadServerReference<A: Iterable<any>, T>(
return null as any;
}

function loadServerObjectReference(
response: Response,
metaData: {id: any, $$reference?: Object},
parentObject: Object,
key: string,
): mixed {
// A Server Reference to an object is always opaque on the client, even if
// we have a module mapping for Server References: unlike a function
// reference, the module that produced it is only meant to be evaluated in a
// true server environment. The reference can only be passed back to the
// server, where it resolves to the object.
// The server reuses this metadata when the same object occurs again.
// Cache its handle so reference equality is preserved within this response.
const existingReference = metaData.$$reference;
if (existingReference !== undefined) {
return existingReference;
}
const reference = createServerObjectReference(metaData.id);
metaData.$$reference = reference;
return reference;
}

function resolveLazy(value: any): mixed {
while (
typeof value === 'object' &&
Expand Down Expand Up @@ -2718,6 +2742,20 @@ function parseModelString(
loadServerReference,
);
}
case 'H': {
if (enableFlightObjectReferences) {
// Server Reference to an object
const ref = value.slice(2);
return getOutlinedModel(
response,
ref,
parentObject,
key,
loadServerObjectReference,
);
}
return undefined;
}
case 'T': {
// Temporary Reference
const reference = '$' + value.slice(2);
Expand Down
105 changes: 105 additions & 0 deletions packages/react-client/src/ReactFlightReplyClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import {writeTemporaryReference} from './ReactFlightTemporaryReferences';
import isArray from 'shared/isArray';
import getPrototypeOf from 'shared/getPrototypeOf';

import {enableFlightObjectReferences} from 'shared/ReactFeatureFlags';

const ObjectPrototype = Object.prototype;

// Passed to replyLifetimeController.abort(). Nothing reads the reason, but a
Expand Down Expand Up @@ -78,6 +80,12 @@ type ServerReferenceClosure = {
const knownServerReferences: WeakMap<Function, ServerReferenceClosure> =
new WeakMap();

// Server References to objects are tracked separately from function
// references. They have no bound arguments and are never callable, so a
// reference of one kind must not be usable where the other is expected.
const knownServerObjectReferences: WeakMap<Object, ServerReferenceId> =
new WeakMap();

// Serializable values
export type ReactServerValue =
// References are passed by their value
Expand Down Expand Up @@ -118,6 +126,10 @@ function serializeServerReferenceID(id: number): string {
return '$h' + id.toString(16);
}

function serializeServerObjectReferenceID(id: number): string {
return '$H' + id.toString(16);
}

function serializeTemporaryReferenceMarker(): string {
return '$T';
}
Expand Down Expand Up @@ -465,6 +477,40 @@ export function processReply(
}

if (typeof value === 'object') {
if (enableFlightObjectReferences) {
// Check before traversing the object or falling back to a temporary
// reference. A Server Reference must always be encoded by id.
const objectReferenceId = knownServerObjectReferences.get(value);
if (objectReferenceId !== undefined) {
const existingReference = writtenObjects.get(value);
if (existingReference !== undefined) {
if (modelRoot === value) {
// serializeModel registered this root before we recognized it
// as a Server Reference. Encode its metadata on this first visit.
modelRoot = null;
} else {
return existingReference;
}
}
const referenceJSON = JSON.stringify(
{id: objectReferenceId},
resolveToJSON,
);
if (formData === null) {
// Upgrade to use FormData to allow us to stream this value.
formData = new FormData();
}
// The reference to this object came from the same client so we can
// pass it back to the server where it resolves to the object.
const refId = nextPartId++;
formData.set(formFieldPrefix + refId, referenceJSON);
const serverObjectReferenceId =
serializeServerObjectReferenceID(refId);
// Store the reference ID for deduplication.
writtenObjects.set(value, serverObjectReferenceId);
return serverObjectReferenceId;
}
}
switch ((value as any).$$typeof) {
case REACT_ELEMENT_TYPE: {
if (temporaryReferences !== undefined && key.indexOf(':') === -1) {
Expand Down Expand Up @@ -1301,6 +1347,65 @@ export function registerServerReference<T: Function>(
return reference;
}

const serverObjectReferenceProxyHandlers: Proxy$traps<mixed> = {
get: function (target: Object, name: string | symbol) {
switch (name) {
// These names are read by the Flight runtime if you end up passing this
// reference along.
case '$$typeof':
return target.$$typeof;
case 'name':
return undefined;
case 'displayName':
return undefined;
// We need to special case this because createElement reads it if we pass this
// reference.
case 'defaultProps':
return undefined;
// React looks for debugInfo on thenables.
case '_debugInfo':
return undefined;
// React also probes streamed objects for async iterator debug info.
case ASYNC_ITERATOR:
return undefined;
// Avoid this attempting to be serialized.
case 'toJSON':
return undefined;
case Symbol.toPrimitive:
// $FlowFixMe[prop-missing]
return Object.prototype[Symbol.toPrimitive];
case Symbol.toStringTag:
// $FlowFixMe[prop-missing]
return Object.prototype[Symbol.toStringTag];
case 'then':
// Even if the referenced object is a Promise, it must not appear
// thenable on the client or it would be awaited instead of passed
// along by reference.
return undefined;
}
throw new Error(
// eslint-disable-next-line react-internal/safe-string-coercion
`Cannot access ${String(name)} on the client. ` +
'You cannot read a Server Reference to an object on the client. ' +
'You can only pass it back to the server.',
);
},
set: function () {
throw new Error(
'Cannot assign to a Server Reference to an object on the client.',
);
},
};

export function createServerObjectReference(id: ServerReferenceId): Object {
const reference = new Proxy({}, serverObjectReferenceProxyHandlers);
// Register the reference so that passing it back to the server encodes it
// by id. Object references are tracked separately from function references
// so that one kind can never be encoded as the other.
knownServerObjectReferences.set(reference, id);
return reference;
}

// $FlowFixMe[method-unbinding]
const FunctionBind = Function.prototype.bind;
// $FlowFixMe[method-unbinding]
Expand Down
1 change: 1 addition & 0 deletions packages/react-server-dom-turbopack/npm/server.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ exports.decodeReply = s.decodeReply;
exports.decodeAction = s.decodeAction;
exports.decodeFormState = s.decodeFormState;
exports.registerServerReference = s.registerServerReference;
exports.registerServerObjectReference = s.registerServerObjectReference;
exports.registerClientReference = s.registerClientReference;
exports.createClientModuleProxy = s.createClientModuleProxy;
exports.createTemporaryReferenceSet = s.createTemporaryReferenceSet;
1 change: 1 addition & 0 deletions packages/react-server-dom-turbopack/npm/server.edge.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ exports.decodeReplyFromAsyncIterable = s.decodeReplyFromAsyncIterable;
exports.decodeAction = s.decodeAction;
exports.decodeFormState = s.decodeFormState;
exports.registerServerReference = s.registerServerReference;
exports.registerServerObjectReference = s.registerServerObjectReference;
exports.registerClientReference = s.registerClientReference;
exports.createClientModuleProxy = s.createClientModuleProxy;
exports.createTemporaryReferenceSet = s.createTemporaryReferenceSet;
1 change: 1 addition & 0 deletions packages/react-server-dom-turbopack/npm/server.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ exports.decodeReplyFromAsyncIterable = s.decodeReplyFromAsyncIterable;
exports.decodeAction = s.decodeAction;
exports.decodeFormState = s.decodeFormState;
exports.registerServerReference = s.registerServerReference;
exports.registerServerObjectReference = s.registerServerObjectReference;
exports.registerClientReference = s.registerClientReference;
exports.createClientModuleProxy = s.createClientModuleProxy;
exports.createTemporaryReferenceSet = s.createTemporaryReferenceSet;
1 change: 1 addition & 0 deletions packages/react-server-dom-turbopack/server.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export {
decodeAction,
decodeFormState,
registerServerReference,
registerServerObjectReference,
registerClientReference,
createClientModuleProxy,
createTemporaryReferenceSet,
Expand Down
1 change: 1 addition & 0 deletions packages/react-server-dom-turbopack/server.edge.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export {
decodeAction,
decodeFormState,
registerServerReference,
registerServerObjectReference,
registerClientReference,
createClientModuleProxy,
createTemporaryReferenceSet,
Expand Down
1 change: 1 addition & 0 deletions packages/react-server-dom-turbopack/server.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export {
decodeAction,
decodeFormState,
registerServerReference,
registerServerObjectReference,
registerClientReference,
createClientModuleProxy,
createTemporaryReferenceSet,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,34 @@ export function registerServerReference<T: Function>(
);
}

export type ServerObjectReference<T> = T & {
$$typeof: symbol,
$$id: string,
};

export function registerServerObjectReference<T>(
reference: T,
id: string,
exportName: null | string,
): ServerObjectReference<T> {
if (__DEV__) {
if (typeof reference === 'function') {
console.error(
'registerServerObjectReference: The reference is a function. Use ' +
'registerServerReference to register a function as a Server ' +
'Reference.',
);
}
}
return Object.defineProperties(reference as any, {
$$typeof: {value: SERVER_REFERENCE_TAG},
$$id: {
value: exportName === null ? id : id + '#' + exportName,
configurable: true,
},
});
}

const PROMISE_PROTOTYPE = Promise.prototype;

const deepProxyHandlers: Proxy$traps<mixed> = {
Expand Down
Loading
Loading