From 06521766b99054a732002995fe13f381ec954473 Mon Sep 17 00:00:00 2001 From: Alex / KATT Date: Sat, 7 Oct 2023 17:51:35 +0200 Subject: [PATCH] chore: rename `getNonce()` -> `getDefaultNonce()` (#53) --- src/async/serializeAsync.ts | 7 +++---- src/internals/getNonce.crypto.test.ts | 4 ++-- src/internals/getNonce.nocrypto.test.ts | 4 ++-- src/internals/getNonce.ts | 2 +- src/sync/serialize.ts | 8 +++++--- 5 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/async/serializeAsync.ts b/src/async/serializeAsync.ts index 78eb8772..3f8e6dd8 100644 --- a/src/async/serializeAsync.ts +++ b/src/async/serializeAsync.ts @@ -1,6 +1,6 @@ import { TsonCircularReferenceError } from "../errors.js"; import { assert } from "../internals/assert.js"; -import { getNonce } from "../internals/getNonce.js"; +import { GetNonce, getDefaultNonce } from "../internals/getNonce.js"; import { mapOrReturn } from "../internals/mapOrReturn.js"; import { TsonAllTypes, @@ -192,10 +192,9 @@ type TsonAsyncSerializer = ( export function createAsyncTsonSerialize( opts: TsonAsyncOptions, ): TsonAsyncSerializer { + const getNonce: GetNonce = (opts.nonce ?? getDefaultNonce) as GetNonce; return (value) => { - const nonce: TsonNonce = opts.nonce - ? (opts.nonce() as TsonNonce) - : getNonce(); + const nonce = getNonce(); const [walk, iterator] = walkerFactory(nonce, opts.types); return [ diff --git a/src/internals/getNonce.crypto.test.ts b/src/internals/getNonce.crypto.test.ts index e57bd0ef..6022c36a 100644 --- a/src/internals/getNonce.crypto.test.ts +++ b/src/internals/getNonce.crypto.test.ts @@ -6,9 +6,9 @@ test("with crypto", async () => { global.crypto = { randomUUID: () => "test", } as any; - const { getNonce } = await import("./getNonce.js"); + const { getDefaultNonce } = await import("./getNonce.js"); - expect(getNonce()).toBe("test"); + expect(getDefaultNonce()).toBe("test"); global.crypto = before; }); diff --git a/src/internals/getNonce.nocrypto.test.ts b/src/internals/getNonce.nocrypto.test.ts index 42123fa0..1d6f5022 100644 --- a/src/internals/getNonce.nocrypto.test.ts +++ b/src/internals/getNonce.nocrypto.test.ts @@ -5,9 +5,9 @@ test("without crypto", async () => { global.crypto = undefined as any; - const { getNonce } = await import("./getNonce.js"); + const { getDefaultNonce } = await import("./getNonce.js"); - expect(getNonce().length).toBeGreaterThan(20); + expect(getDefaultNonce().length).toBeGreaterThan(20); global.crypto = before; }); diff --git a/src/internals/getNonce.ts b/src/internals/getNonce.ts index cdee3d6a..e3b0abe4 100644 --- a/src/internals/getNonce.ts +++ b/src/internals/getNonce.ts @@ -4,7 +4,7 @@ const randomString = () => Math.random().toString(36).slice(2); export type GetNonce = () => TsonNonce; -export const getNonce: GetNonce = +export const getDefaultNonce: GetNonce = typeof crypto === "object" && typeof crypto.randomUUID === "function" ? () => crypto.randomUUID() as TsonNonce : () => diff --git a/src/sync/serialize.ts b/src/sync/serialize.ts index 0b33bdfa..bc1d2d6f 100644 --- a/src/sync/serialize.ts +++ b/src/sync/serialize.ts @@ -1,5 +1,5 @@ import { TsonCircularReferenceError } from "../errors.js"; -import { GetNonce, getNonce } from "../internals/getNonce.js"; +import { GetNonce, getDefaultNonce } from "../internals/getNonce.js"; import { mapOrReturn } from "../internals/mapOrReturn.js"; import { TsonAllTypes, @@ -38,9 +38,11 @@ function getHandlers(opts: TsonOptions) { } } - const nonceFn: GetNonce = opts.nonce ? (opts.nonce as GetNonce) : getNonce; + const getNonce: GetNonce = opts.nonce + ? (opts.nonce as GetNonce) + : getDefaultNonce; - return [nonceFn, nonPrimitives, byPrimitive] as const; + return [getNonce, nonPrimitives, byPrimitive] as const; } export function createTsonStringify(opts: TsonOptions): TsonStringifyFn {