Skip to content

Commit

Permalink
improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
uriva committed Sep 26, 2024
1 parent ef8f8b6 commit 4782f11
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
8 changes: 6 additions & 2 deletions client/src/crypto.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import { hash } from "./crypto.ts";
import { inputToCacheKey } from "./crypto.ts";

Deno.test("hash stability", () => assertEquals(hash("hello"), "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"));
Deno.test("hash stability", () =>
assertEquals(
inputToCacheKey("my secret", undefined)("hello"),
"413ddc7b60b9f03f95a4091496d05d785478a8b2ac36907c635076879f2d73c3",
));
15 changes: 14 additions & 1 deletion client/src/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
bufferToHex,
hexToBuffer,
} from "https://deno.land/x/[email protected]/mod.ts";
import { jsonStringify } from "https://deno.land/x/[email protected]/jsonStringify.ts";

import { decode as base64UrlDecode } from "https://deno.land/[email protected]/encoding/base64url.ts";
import { sha256 } from "https://denopkg.com/chiefbiiko/[email protected]/mod.ts";
Expand All @@ -11,7 +12,7 @@ const algo = "AES-CBC";
const getKey = (scope: "encrypt" | "decrypt", key: string) =>
crypto.subtle.importKey("raw", base64UrlDecode(key), algo, true, [scope]);

export const hash = (x: string): string => sha256(x, "utf8", "hex") as string;
const hash = (x: string): string => sha256(x, "utf8", "hex") as string;

export const encrypt =
(key: string) => async (plainText: string): Promise<Encrypted> => {
Expand Down Expand Up @@ -45,3 +46,15 @@ export const decrypt = (key: string) => async ({ iv, cipher }: Encrypted) =>
),
),
);

// deno-lint-ignore no-explicit-any
export type CustomKeyFn = (..._: any[]) => any;

export const jsonStableStringify = <Args>(x: Args) =>
jsonStringify(x) as string;

export const inputToCacheKey =
// deno-lint-ignore no-explicit-any
<Args extends any[]>(secret: string, customKeyFn: CustomKeyFn | undefined) =>
(...x: Args): string =>
hash(jsonStableStringify(customKeyFn ? customKeyFn(...x) : x) + secret);
2 changes: 1 addition & 1 deletion client/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CacheParams, cache, waitAllWrites } from "./index.ts";
import { cache, type CacheParams, waitAllWrites } from "./index.ts";

import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import { config } from "https://deno.land/x/[email protected]/mod.ts";
Expand Down
22 changes: 8 additions & 14 deletions client/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { decrypt, encrypt, hash } from "./crypto.ts";
import {
CustomKeyFn,
decrypt,
encrypt,
inputToCacheKey,
jsonStableStringify,
} from "./crypto.ts";

import { dirname } from "https://deno.land/[email protected]/path/mod.ts";
import { jsonStringify } from "https://deno.land/x/[email protected]/jsonStringify.ts";

const jsonStableStringify = <Args>(x: Args) => jsonStringify(x) as string;

const writeStringToFile = (filePath: string, s: string) =>
Deno.mkdir(dirname(filePath), { recursive: true }).then(() =>
Expand Down Expand Up @@ -100,18 +103,9 @@ const abstractCache = <F extends Func>({

export const waitAllWrites = () => Promise.all(writePromises);

// deno-lint-ignore no-explicit-any
type CustomKeyFn = (..._: any[]) => any;

export const inputToCacheKey =
// deno-lint-ignore no-explicit-any
<Args extends any[]>(secret: string, customKeyFn: CustomKeyFn | undefined) =>
(...x: Args): string =>
hash(jsonStableStringify(customKeyFn ? customKeyFn(...x) : x) + secret);

type MemParams = {
ttl?: number;
customKeyFn?: (..._: any[]) => string;
customKeyFn?: CustomKeyFn;
forceWrite?: boolean;
};

Expand Down

0 comments on commit 4782f11

Please sign in to comment.