Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
chore: rename getNonce -> getNonceDefault
Browse files Browse the repository at this point in the history
  • Loading branch information
KATT committed Oct 7, 2023
1 parent 8f0a4e9 commit 1eb737c
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 12 deletions.
7 changes: 3 additions & 4 deletions src/async/serializeAsync.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -192,10 +192,9 @@ type TsonAsyncSerializer = <T>(
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 [
Expand Down
4 changes: 2 additions & 2 deletions src/internals/getNonce.crypto.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
4 changes: 2 additions & 2 deletions src/internals/getNonce.nocrypto.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
2 changes: 1 addition & 1 deletion src/internals/getNonce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
: () =>
Expand Down
8 changes: 5 additions & 3 deletions src/sync/serialize.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 1eb737c

Please sign in to comment.