diff --git a/client/src/crypto.test.ts b/client/src/crypto.test.ts index 53c92bb..fc8a4b5 100644 --- a/client/src/crypto.test.ts +++ b/client/src/crypto.test.ts @@ -1,4 +1,8 @@ import { assertEquals } from "https://deno.land/std@0.174.0/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", + )); diff --git a/client/src/crypto.ts b/client/src/crypto.ts index c193722..1d854cd 100644 --- a/client/src/crypto.ts +++ b/client/src/crypto.ts @@ -2,6 +2,7 @@ import { bufferToHex, hexToBuffer, } from "https://deno.land/x/hextools@v1.0.0/mod.ts"; +import { jsonStringify } from "https://deno.land/x/stable_stringify@v0.2.1/jsonStringify.ts"; import { decode as base64UrlDecode } from "https://deno.land/std@0.82.0/encoding/base64url.ts"; import { sha256 } from "https://denopkg.com/chiefbiiko/sha256@v1.0.0/mod.ts"; @@ -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 => { @@ -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 = (x: Args) => + jsonStringify(x) as string; + +export const inputToCacheKey = + // deno-lint-ignore no-explicit-any + (secret: string, customKeyFn: CustomKeyFn | undefined) => + (...x: Args): string => + hash(jsonStableStringify(customKeyFn ? customKeyFn(...x) : x) + secret); diff --git a/client/src/index.test.ts b/client/src/index.test.ts index 045c317..b32c3bd 100644 --- a/client/src/index.test.ts +++ b/client/src/index.test.ts @@ -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/std@0.174.0/testing/asserts.ts"; import { config } from "https://deno.land/x/dotenv@v3.2.2/mod.ts"; diff --git a/client/src/index.ts b/client/src/index.ts index a646fe6..4527502 100644 --- a/client/src/index.ts +++ b/client/src/index.ts @@ -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/std@0.179.0/path/mod.ts"; -import { jsonStringify } from "https://deno.land/x/stable_stringify@v0.2.1/jsonStringify.ts"; - -const jsonStableStringify = (x: Args) => jsonStringify(x) as string; const writeStringToFile = (filePath: string, s: string) => Deno.mkdir(dirname(filePath), { recursive: true }).then(() => @@ -100,18 +103,9 @@ const abstractCache = ({ 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 - (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; };