diff options
Diffstat (limited to 'packages/idb-bridge/src/util')
-rw-r--r-- | packages/idb-bridge/src/util/makeStoreKeyValue.ts | 10 | ||||
-rw-r--r-- | packages/idb-bridge/src/util/structuredClone.test.ts | 12 |
2 files changed, 19 insertions, 3 deletions
diff --git a/packages/idb-bridge/src/util/makeStoreKeyValue.ts b/packages/idb-bridge/src/util/makeStoreKeyValue.ts index 243e46e04..c0fdb19a7 100644 --- a/packages/idb-bridge/src/util/makeStoreKeyValue.ts +++ b/packages/idb-bridge/src/util/makeStoreKeyValue.ts @@ -15,7 +15,7 @@ */ import { extractKey } from "./extractKey"; -import { DataError } from "./errors"; +import { DataCloneError, DataError } from "./errors"; import { valueToKey } from "./valueToKey"; import { structuredClone } from "./structuredClone"; import { IDBKeyPath, IDBValidKey } from "../idbtypes"; @@ -26,7 +26,7 @@ export interface StoreKeyResult { value: any; } -export function injectKey( +function injectKey( keyPath: IDBKeyPath | IDBKeyPath[], value: any, key: IDBValidKey, @@ -87,7 +87,11 @@ export function makeStoreKeyValue( // This models a decision table on (haveKey, haveKeyPath, autoIncrement) - value = structuredClone(value); + try { + value = structuredClone(value); + } catch (e) { + throw new DataCloneError(); + } if (haveKey) { if (haveKeyPath) { diff --git a/packages/idb-bridge/src/util/structuredClone.test.ts b/packages/idb-bridge/src/util/structuredClone.test.ts index 58a7f32c1..ed404c6be 100644 --- a/packages/idb-bridge/src/util/structuredClone.test.ts +++ b/packages/idb-bridge/src/util/structuredClone.test.ts @@ -29,6 +29,18 @@ test("structured clone", (t) => { checkClone(t, [new Date()]); checkClone(t, undefined); checkClone(t, [undefined]); + + t.throws(() => { + structuredClone({ foo: () => {} }); + }); + + t.throws(() => { + structuredClone(Promise); + }); + + t.throws(() => { + structuredClone(Promise.resolve()); + }); }); test("structured clone (cycles)", (t) => { |