-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
29 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"; | ||
|
@@ -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> => { | ||
|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(() => | ||
|
@@ -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; | ||
}; | ||
|
||
|