Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ jobs:
- name: Setup nodejs
uses: actions/setup-node@v3
with:
node-version: 18
node-version: 20

- name: Setup Bun
uses: oven-sh/setup-bun@v1
Expand Down Expand Up @@ -298,7 +298,7 @@ jobs:
- name: Setup nodejs
uses: actions/setup-node@v3
with:
node-version: 18
node-version: 20

- name: Setup Bun
uses: oven-sh/setup-bun@v1
Expand Down Expand Up @@ -336,7 +336,7 @@ jobs:
- name: Setup nodejs
uses: actions/setup-node@v3
with:
node-version: 18
node-version: 20

- name: Setup Bun
uses: oven-sh/setup-bun@v1
Expand Down Expand Up @@ -391,7 +391,7 @@ jobs:
- name: Setup nodejs
uses: actions/setup-node@v3
with:
node-version: 18
node-version: 20

- name: Setup Bun
uses: oven-sh/setup-bun@v1
Expand Down
Binary file modified bun.lockb
Binary file not shown.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
"@biomejs/biome": "latest",
"@commitlint/cli": "^19.3.0",
"@commitlint/config-conventional": "^19.2.2",
"@types/crypto-js": "^4.1.3",
"@typescript-eslint/eslint-plugin": "8.4.0",
"@typescript-eslint/parser": "8.4.0",
"bun-types": "1.0.33",
Expand All @@ -88,6 +87,6 @@
"typescript": "latest"
},
"dependencies": {
"crypto-js": "^4.2.0"
"uncrypto": "^0.1.3"
}
}
3 changes: 2 additions & 1 deletion pkg/auto-pipeline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,12 @@ describe("Auto pipeline", () => {
latencyLogging: false,
enableAutoPipelining: true,
});
await redis.flushdb();
// @ts-expect-error pipelineCounter is not in type but accessible
expect(redis.pipelineCounter).toBe(0);

// following five commands are added to the pipeline
void redis.flushdb();
void redis.del("baz");
void redis.incr("baz");
void redis.incr("baz");
void redis.set("foo", "bar");
Expand Down
10 changes: 8 additions & 2 deletions pkg/script.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,23 @@ describe("create a new script", () => {
});

describe("sha1", () => {
test("calculates the correct sha1", () => {
test("calculates the correct sha1", async () => {
const redis = new Redis(client);
const script = redis.createScript("The quick brown fox jumps over the lazy dog");

// Wait one tick
await new Promise((resolve) => setTimeout(resolve, 0));

expect(script.sha1).toEqual("2fd4e1c67a2d28fced849ee1bb76e7391b93eb12");
});

test("calculates the correct sha1 for empty string", () => {
test("calculates the correct sha1 for empty string", async () => {
const redis = new Redis(client);
const script = redis.createScript("");

// Wait one tick
await new Promise((resolve) => setTimeout(resolve, 0));

expect(script.sha1).toEqual("da39a3ee5e6b4b0d3255bfef95601890afd80709");
});
});
Expand Down
36 changes: 30 additions & 6 deletions pkg/script.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Hex from "crypto-js/enc-hex.js";
import sha1 from "crypto-js/sha1.js";
import { subtle } from "uncrypto";
import type { Redis } from "./redis";
/**
* Creates a new script.
Expand All @@ -19,26 +18,46 @@ import type { Redis } from "./redis";
*/
export class Script<TResult = unknown> {
public readonly script: string;
public readonly sha1: string;
/**
* @deprecated This property is initialized to an empty string and will be set in the init method
* asynchronously. Do not use this property immidiately after the constructor.
*
* This property is only exposed for backwards compatibility and will be removed in the
* future major release.
*/
public sha1: string;
private readonly redis: Redis;

constructor(redis: Redis, script: string) {
this.redis = redis;
this.sha1 = this.digest(script);
this.script = script;
this.sha1 = "";
void this.init(script);
}

/**
* Initialize the script by computing its SHA-1 hash.
*/
private async init(script: string): Promise<void> {
if (this.sha1) return;
this.sha1 = await this.digest(script);
}

/**
* Send an `EVAL` command to redis.
*/
public async eval(keys: string[], args: string[]): Promise<TResult> {
await this.init(this.script);

return await this.redis.eval(this.script, keys, args);
}

/**
* Calculates the sha1 hash of the script and then calls `EVALSHA`.
*/
public async evalsha(keys: string[], args: string[]): Promise<TResult> {
await this.init(this.script);

return await this.redis.evalsha(this.sha1, keys, args);
}

Expand All @@ -49,6 +68,8 @@ export class Script<TResult = unknown> {
* Following calls will be able to use the cached script
*/
public async exec(keys: string[], args: string[]): Promise<TResult> {
await this.init(this.script);

const res = await this.redis.evalsha(this.sha1, keys, args).catch(async (error) => {
if (error instanceof Error && error.message.toLowerCase().includes("noscript")) {
return await this.redis.eval(this.script, keys, args);
Expand All @@ -61,7 +82,10 @@ export class Script<TResult = unknown> {
/**
* Compute the sha1 hash of the script and return its hex representation.
*/
private digest(s: string): string {
return Hex.stringify(sha1(s));
private async digest(s: string): Promise<string> {
const data = new TextEncoder().encode(s);
const hashBuffer = await subtle.digest("SHA-1", data);
const hashArray = [...new Uint8Array(hashBuffer)];
return hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
}
}
33 changes: 27 additions & 6 deletions pkg/scriptRo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Hex from "crypto-js/enc-hex.js";
import sha1 from "crypto-js/sha1.js";
import { subtle } from "uncrypto";
import type { Redis } from "./redis";

/**
Expand All @@ -20,26 +19,43 @@ import type { Redis } from "./redis";
*/
export class ScriptRO<TResult = unknown> {
public readonly script: string;
public readonly sha1: string;
/**
* @deprecated This property is initialized to an empty string and will be set in the init method
* asynchronously. Do not use this property immidiately after the constructor.
*
* This property is only exposed for backwards compatibility and will be removed in the
* future major release.
*/
public sha1: string;
private readonly redis: Redis;

constructor(redis: Redis, script: string) {
this.redis = redis;
this.sha1 = this.digest(script);
this.sha1 = "";
this.script = script;
void this.init(script);
}

private async init(script: string): Promise<void> {
if (this.sha1) return;
this.sha1 = await this.digest(script);
}

/**
* Send an `EVAL_RO` command to redis.
*/
public async evalRo(keys: string[], args: string[]): Promise<TResult> {
await this.init(this.script);

return await this.redis.evalRo(this.script, keys, args);
}

/**
* Calculates the sha1 hash of the script and then calls `EVALSHA_RO`.
*/
public async evalshaRo(keys: string[], args: string[]): Promise<TResult> {
await this.init(this.script);

return await this.redis.evalshaRo(this.sha1, keys, args);
}

Expand All @@ -50,6 +66,8 @@ export class ScriptRO<TResult = unknown> {
* Following calls will be able to use the cached script
*/
public async exec(keys: string[], args: string[]): Promise<TResult> {
await this.init(this.script);

const res = await this.redis.evalshaRo(this.sha1, keys, args).catch(async (error) => {
if (error instanceof Error && error.message.toLowerCase().includes("noscript")) {
return await this.redis.evalRo(this.script, keys, args);
Expand All @@ -62,7 +80,10 @@ export class ScriptRO<TResult = unknown> {
/**
* Compute the sha1 hash of the script and return its hex representation.
*/
private digest(s: string): string {
return Hex.stringify(sha1(s));
private async digest(s: string): Promise<string> {
const data = new TextEncoder().encode(s);
const hashBuffer = await subtle.digest("SHA-1", data);
const hashArray = [...new Uint8Array(hashBuffer)];
return hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
}
}
Loading