From 3f4526847b76ddafa9a3e23d94e973c30d8c72ef Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Wed, 28 Jun 2023 17:13:08 +0200 Subject: idb: suggest encoding improvements --- packages/idb-bridge/src/util/structuredClone.ts | 38 ++++++++++++++++++++----- 1 file changed, 31 insertions(+), 7 deletions(-) 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); } /** -- cgit v1.2.3