diff --git a/packages/idb-bridge/src/util/structuredClone.ts b/packages/idb-bridge/src/util/structuredClone.ts index 5ed269db3..a3842b8c4 100644 --- a/packages/idb-bridge/src/util/structuredClone.ts +++ b/packages/idb-bridge/src/util/structuredClone.ts @@ -14,6 +14,29 @@ permissions and limitations under the License. */ +/** + * Encoding (new, compositional version): + * + * Encapsulate object that itself might contain a "$" field: + * { $: { E... } } + * Circular reference: + * { $: ["ref", uplevel, field...] } + * Date: + * { $: ["data"], val: datestr } + * Bigint: + * { $: ["bigint"], val: bigintstr } + * Array with special (non-number) attributes: + * { $: ["array"], val: arrayobj } + * Undefined field + * { $: "undef" } + * + * Legacy (top-level only), for backwards compatibility: + * { $types: [...] } + */ + +/** + * Imports. + */ import { DataCloneError } from "./errors.js"; const { toString: toStr } = {}; @@ -73,10 +96,6 @@ function isUserObject(val: any): boolean { return hasConstructorOf(val, Object) || isUserObject(proto); } -function isRegExp(val: any): boolean { - return toStringTag(val) === "RegExp"; -} - function copyBuffer(cur: any) { if (cur instanceof Buffer) { return Buffer.from(cur); @@ -334,8 +353,7 @@ export function structuredEncapsulate(val: any): any { return res; } -export function internalStructuredRevive(val: any): any { - val = JSON.parse(JSON.stringify(val)); +export function applyLegacyTypeAnnotations(val: any): any { if (val === null) { return null; } @@ -417,8 +435,14 @@ export function internalStructuredRevive(val: any): any { return outRoot; } +export function internalStructuredRevive(val: any): any { + // FIXME: Do the newly specified, compositional encoding here. + val = JSON.parse(JSON.stringify(val)); +} + export function structuredRevive(val: any): any { - return internalStructuredRevive(val); + const r = internalStructuredRevive(val); + return applyLegacyTypeAnnotations(r); } /**