Skip to content

Commit

Permalink
Add support for binary in lua
Browse files Browse the repository at this point in the history
1. Change Script consturctor to accept string or uint8array
2. Change ScriptOptions to have args,keys of string or uint8array
  • Loading branch information
Yulazari committed Jul 1, 2024
1 parent 09ba7b5 commit dbcf91f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 10 deletions.
10 changes: 6 additions & 4 deletions node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
},
"homepage": "https://github.com/aws/glide-for-redis#readme",
"dependencies": {
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.29.1",
"glide-rs": "file:rust-client",
"long": "^5.2.3",
"npmignore": "^0.3.0",
"prettier": "^3.2.5",
"prettier": "^3.3.2",
"protobufjs": "^7.2.2"
},
"bundleDependencies": [
Expand Down Expand Up @@ -46,10 +48,10 @@
"@types/minimist": "^1.2.5",
"@types/redis-server": "^1.2.0",
"@types/uuid": "^9.0.1",
"@typescript-eslint/eslint-plugin": "^5.54.1",
"@typescript-eslint/parser": "^5.54.1",
"@typescript-eslint/eslint-plugin": "^5.62.0",
"@typescript-eslint/parser": "^5.62.0",
"babel-jest": "^28.1.3",
"eslint": "^8.36.0",
"eslint": "^8.57.0",
"eslint-plugin-tsdoc": "^0.2.17",
"find-free-port": "^2.0.0",
"jest": "^28.1.3",
Expand Down
8 changes: 6 additions & 2 deletions node/rust-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use glide_core::start_socket_listener;
use glide_core::MAX_REQUEST_ARGS_LENGTH;
#[cfg(feature = "testing_utilities")]
use napi::bindgen_prelude::BigInt;
use napi::bindgen_prelude::Either;
use napi::bindgen_prelude::Uint8Array;
use napi::{Env, Error, JsObject, JsUnknown, Result, Status};
use napi_derive::napi;
Expand Down Expand Up @@ -350,8 +351,11 @@ impl Script {
/// Construct with the script's code.
#[napi(constructor)]
#[allow(dead_code)]
pub fn new(code: String) -> Self {
let hash = glide_core::scripts_container::add_script(&code);
pub fn new(code: Either<String, Uint8Array>) -> Self {
let hash = match code {
Either::A(code_str) => glide_core::scripts_container::add_script(code_str.as_bytes()),
Either::B(code_bytes) => glide_core::scripts_container::add_script(&code_bytes),
};
Self { hash }
}

Expand Down
24 changes: 20 additions & 4 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,11 @@ export type ScriptOptions = {
/**
* The keys that are used in the script.
*/
keys?: string[];
keys?: (string | Uint8Array)[];
/**
* The arguments for the script.
*/
args?: string[];
args?: (string | Uint8Array)[];
};

function getRequestErrorClass(
Expand Down Expand Up @@ -1629,8 +1629,24 @@ export class BaseClient {
): Promise<ReturnType> {
const scriptInvocation = redis_request.ScriptInvocation.create({
hash: script.getHash(),
keys: option?.keys,
args: option?.args,
keys: option?.keys?.map((item) => {
if (typeof item === "string") {
// Convert the string to a Uint8Array
return new TextEncoder().encode(item);
} else {
// If it's already a Uint8Array, just return it
return item;
}
}),
args: option?.args?.map((item) => {
if (typeof item === "string") {
// Convert the string to a Uint8Array
return new TextEncoder().encode(item);
} else {
// If it's already a Uint8Array, just return it
return item;
}
}),
});
return this.createWritePromise(scriptInvocation);
}
Expand Down
41 changes: 41 additions & 0 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1494,6 +1494,47 @@ export function runBaseTests<Context>(config: {

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`script test_%p`,
async (protocol) => {
await runTest(async (client: BaseClient) => {
const key1 = Buffer.from(uuidv4());
const key2 = Buffer.from(uuidv4());

let script = new Script(Buffer.from("return 'Hello'"));
checkSimple(await client.invokeScript(script)).toEqual("Hello");

script = new Script(
Buffer.from( "return redis.call('SET', KEYS[1], ARGV[1])"),
);
checkSimple(
await client.invokeScript(script, {
keys: [key1],
args: [Buffer.from("value1")],
}),
).toEqual("OK");

/// Reuse the same script with different parameters.
checkSimple(
await client.invokeScript(script, {
keys: [key2],
args: [Buffer.from("value2")],
}),
).toEqual("OK");

script = new Script(Buffer.from("return redis.call('GET', KEYS[1])"));
checkSimple(
await client.invokeScript(script, { keys: [key1] }),
).toEqual("value1");

checkSimple(
await client.invokeScript(script, { keys: [key2] }),
).toEqual("value2");
}, protocol);
},
config.timeout,
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`script test_binary_%p`,
async (protocol) => {
await runTest(async (client: BaseClient) => {
const key1 = uuidv4();
Expand Down

0 comments on commit dbcf91f

Please sign in to comment.