diff --git a/src/async/serializeAsync.ts b/src/async/serializeAsync.ts index 78eb8772..f2e63c7f 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, getNonceDefault } 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 ?? getNonceDefault) 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..d5beeaba 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 { getNonceDefault } = await import("./getNonce.js"); - expect(getNonce()).toBe("test"); + expect(getNonceDefault()).toBe("test"); global.crypto = before; }); diff --git a/src/internals/getNonce.nocrypto.test.ts b/src/internals/getNonce.nocrypto.test.ts index 42123fa0..db6d2511 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 { getNonceDefault } = await import("./getNonce.js"); - expect(getNonce().length).toBeGreaterThan(20); + expect(getNonceDefault().length).toBeGreaterThan(20); global.crypto = before; }); diff --git a/src/internals/getNonce.ts b/src/internals/getNonce.ts index cdee3d6a..3d0ba006 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 getNonceDefault: 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..49809a46 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, getNonceDefault } 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) + : getNonceDefault; - return [nonceFn, nonPrimitives, byPrimitive] as const; + return [getNonce, nonPrimitives, byPrimitive] as const; } export function createTsonStringify(opts: TsonOptions): TsonStringifyFn {