-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #120 from upstash/DX-1116
Hardcode script hash values and deprecate cacheScripts
- Loading branch information
Showing
6 changed files
with
174 additions
and
95 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
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Redis } from "@upstash/redis"; | ||
import { describe, expect, test } from "bun:test"; | ||
import { RESET_SCRIPT, SCRIPTS } from "./hash"; | ||
|
||
describe("should use correct hash for lua scripts", () => { | ||
const redis = Redis.fromEnv(); | ||
|
||
const validateHash = async (script: string, expectedHash: string) => { | ||
const hash = await redis.scriptLoad(script) | ||
expect(hash).toBe(expectedHash) | ||
} | ||
|
||
const algorithms = [ | ||
...Object.entries(SCRIPTS.singleRegion), ...Object.entries(SCRIPTS.multiRegion) | ||
] | ||
|
||
// for each algorithm (fixedWindow, slidingWindow etc) | ||
for (const [algorithm, scripts] of algorithms) { | ||
describe(`${algorithm}`, () => { | ||
// for each method (limit & getRemaining) | ||
for (const [method, scriptInfo] of Object.entries(scripts)) { | ||
test(method, async () => { | ||
await validateHash(scriptInfo.script, scriptInfo.hash) | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
test("reset script", async () => { | ||
await validateHash(RESET_SCRIPT.script, RESET_SCRIPT.hash) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import * as Single from "./single" | ||
import * as Multi from "./multi" | ||
import { resetScript } from "./reset" | ||
|
||
export type ScriptInfo = { | ||
script: string, | ||
hash: string | ||
} | ||
|
||
type Algorithm = { | ||
limit: ScriptInfo, | ||
getRemaining: ScriptInfo, | ||
} | ||
|
||
type AlgorithmKind = | ||
| "fixedWindow" | ||
| "slidingWindow" | ||
| "tokenBucket" | ||
| "cachedFixedWindow" | ||
|
||
export const SCRIPTS: { | ||
singleRegion: Record<AlgorithmKind, Algorithm>, | ||
multiRegion: Record<Exclude<AlgorithmKind, "tokenBucket" | "cachedFixedWindow">, Algorithm>, | ||
} = { | ||
singleRegion: { | ||
fixedWindow: { | ||
limit: { | ||
script: Single.fixedWindowLimitScript, | ||
hash: "b13943e359636db027ad280f1def143f02158c13" | ||
}, | ||
getRemaining: { | ||
script: Single.fixedWindowRemainingTokensScript, | ||
hash: "8c4c341934502aee132643ffbe58ead3450e5208" | ||
}, | ||
}, | ||
slidingWindow: { | ||
limit: { | ||
script: Single.slidingWindowLimitScript, | ||
hash: "e1391e429b699c780eb0480350cd5b7280fd9213" | ||
}, | ||
getRemaining: { | ||
script: Single.slidingWindowRemainingTokensScript, | ||
hash: "65a73ac5a05bf9712903bc304b77268980c1c417" | ||
}, | ||
}, | ||
tokenBucket: { | ||
limit: { | ||
script: Single.tokenBucketLimitScript, | ||
hash: "5bece90aeef8189a8cfd28995b479529e270b3c6" | ||
}, | ||
getRemaining: { | ||
script: Single.tokenBucketRemainingTokensScript, | ||
hash: "a15be2bb1db2a15f7c82db06146f9d08983900d0" | ||
}, | ||
}, | ||
cachedFixedWindow: { | ||
limit: { | ||
script: Single.cachedFixedWindowLimitScript, | ||
hash: "c26b12703dd137939b9a69a3a9b18e906a2d940f" | ||
}, | ||
getRemaining: { | ||
script: Single.cachedFixedWindowRemainingTokenScript, | ||
hash: "8e8f222ccae68b595ee6e3f3bf2199629a62b91a" | ||
}, | ||
} | ||
}, | ||
multiRegion: { | ||
fixedWindow: { | ||
limit: { | ||
script: Multi.fixedWindowLimitScript, | ||
hash: "a8c14f3835aa87bd70e5e2116081b81664abcf5c" | ||
}, | ||
getRemaining: { | ||
script: Multi.fixedWindowRemainingTokensScript, | ||
hash: "8ab8322d0ed5fe5ac8eb08f0c2e4557f1b4816fd" | ||
}, | ||
}, | ||
slidingWindow: { | ||
limit: { | ||
script: Multi.slidingWindowLimitScript, | ||
hash: "cb4fdc2575056df7c6d422764df0de3a08d6753b" | ||
}, | ||
getRemaining: { | ||
script: Multi.slidingWindowRemainingTokensScript, | ||
hash: "558c9306b7ec54abb50747fe0b17e5d44bd24868" | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
/** COMMON */ | ||
export const RESET_SCRIPT: ScriptInfo = { | ||
script: resetScript, | ||
hash: "54bd274ddc59fb3be0f42deee2f64322a10e2b50" | ||
} |
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
Oops, something went wrong.