diff options
Diffstat (limited to 'packages/idb-bridge/src/util')
| -rw-r--r-- | packages/idb-bridge/src/util/injectKey.ts | 4 | ||||
| -rw-r--r-- | packages/idb-bridge/src/util/makeStoreKeyValue.ts | 10 | ||||
| -rw-r--r-- | packages/idb-bridge/src/util/normalizeKeyPath.ts | 41 | ||||
| -rw-r--r-- | packages/idb-bridge/src/util/structuredClone.ts | 21 | 
4 files changed, 64 insertions, 12 deletions
| diff --git a/packages/idb-bridge/src/util/injectKey.ts b/packages/idb-bridge/src/util/injectKey.ts index 678f42d28..63c8deda4 100644 --- a/packages/idb-bridge/src/util/injectKey.ts +++ b/packages/idb-bridge/src/util/injectKey.ts @@ -16,10 +16,10 @@  */  import { IDBKeyPath, IDBValidKey } from "../idbtypes"; -import structuredClone from "./structuredClone"; +import { structuredClone } from "./structuredClone";  export function injectKey( -  keyPath: IDBKeyPath, +  keyPath: IDBKeyPath | IDBKeyPath[],    value: any,    key: IDBValidKey,  ): any { diff --git a/packages/idb-bridge/src/util/makeStoreKeyValue.ts b/packages/idb-bridge/src/util/makeStoreKeyValue.ts index b535bced5..2281e983d 100644 --- a/packages/idb-bridge/src/util/makeStoreKeyValue.ts +++ b/packages/idb-bridge/src/util/makeStoreKeyValue.ts @@ -17,7 +17,7 @@  import extractKey from "./extractKey";  import { DataError } from "./errors";  import valueToKey from "./valueToKey"; -import structuredClone from "./structuredClone"; +import { structuredClone } from "./structuredClone";  import injectKey from "./injectKey";  import { IDBKeyPath, IDBValidKey } from "../idbtypes"; @@ -32,7 +32,7 @@ export function makeStoreKeyValue(    key: IDBValidKey | undefined,    currentKeyGenerator: number,    autoIncrement: boolean, -  keyPath: IDBKeyPath | null, +  keyPath: IDBKeyPath | IDBKeyPath[] | null,  ): StoreKeyResult {    const haveKey = key !== null && key !== undefined;    const haveKeyPath = keyPath !== null && keyPath !== undefined; @@ -63,7 +63,11 @@ export function makeStoreKeyValue(          };        } else {          // (yes, no, no) -        throw new DataError(); +        return { +          key: key!, +          value: value, +          updatedKeyGenerator: currentKeyGenerator, +        };        }      }    } else { diff --git a/packages/idb-bridge/src/util/normalizeKeyPath.ts b/packages/idb-bridge/src/util/normalizeKeyPath.ts new file mode 100644 index 000000000..4e194b2d1 --- /dev/null +++ b/packages/idb-bridge/src/util/normalizeKeyPath.ts @@ -0,0 +1,41 @@ +/* + Copyright 2017 Jeremy Scheff + Copyright 2019 Florian Dold + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + or implied. See the License for the specific language governing + permissions and limitations under the License. +*/ + +import { IDBKeyPath } from "../idbtypes"; + +export function normalizeKeyPath( +  keyPath: IDBKeyPath | IDBKeyPath[], +): string | string[] { +  if (Array.isArray(keyPath)) { +    const path: string[] = []; +    for (let item of keyPath) { +      // This doesn't make sense to me based on the spec, but it is needed to pass the W3C KeyPath tests (see same +      // comment in validateKeyPath) +      if ( +        item !== undefined && +        item !== null && +        typeof item !== "string" && +        (item as any).toString +      ) { +        item = (item as any).toString(); +      } +      path.push(item); +    } +    return path; +  } +  return keyPath; +} diff --git a/packages/idb-bridge/src/util/structuredClone.ts b/packages/idb-bridge/src/util/structuredClone.ts index c49d0377f..9bbeb7151 100644 --- a/packages/idb-bridge/src/util/structuredClone.ts +++ b/packages/idb-bridge/src/util/structuredClone.ts @@ -14,17 +14,24 @@   permissions and limitations under the License.  */ -function structuredCloneImpl(val: any, visited: WeakMap<any, boolean>): any { -  // FIXME: replace with real implementation! -  return JSON.parse(JSON.stringify(val)); +// @ts-ignore +import Typeson from "typeson"; +// @ts-ignore +import structuredCloningThrowing from "typeson-registry/dist/presets/structured-cloning-throwing"; + +const TSON = new Typeson().register(structuredCloningThrowing); + +export function structuredEncapsulate(val: any): any { +  return TSON.encapsulate(val); +} + +export function structuredRevive(val: any): any { +  return TSON.revive(val);  }  /**   * Structured clone for IndexedDB.   */  export function structuredClone(val: any): any { -  const visited: WeakMap<any, boolean> = new WeakMap<any, boolean>(); -  return structuredCloneImpl(val, visited); +  return structuredRevive(structuredEncapsulate(val));  } - -export default structuredClone; | 
