idb: suggest encoding improvements

This commit is contained in:
Florian Dold 2023-06-28 17:13:08 +02:00
parent 28dce57f92
commit 3f4526847b
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B

View File

@ -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);
}
/**