Skip to content

Commit e79c00d

Browse files
committed
initial commit
Signed-off-by: karthik Ganeshram <[email protected]>
1 parent 379fa5c commit e79c00d

File tree

4 files changed

+82
-1
lines changed

4 files changed

+82
-1
lines changed

Cargo.lock

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/spin-js-engine/src/lib.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@ use {
1414
spin_sdk::{
1515
config,
1616
http::{Request, Response},
17+
<<<<<<< HEAD
1718
http_component,
1819
key_value::Store,
1920
outbound_http, redis,
21+
=======
22+
http_component, outbound_http, redis,
23+
redis::RedisResult,
24+
>>>>>>> 0081caf (initial commit)
2025
},
2126
std::{
2227
collections::HashMap,
@@ -331,6 +336,60 @@ fn math_rand(context: &Context, _this: &Value, _args: &[Value]) -> Result<Value>
331336
context.value_from_f64(thread_rng().gen_range(0.0_f64..1.0))
332337
}
333338

339+
fn redis_exec(context: &Context, _this: &Value, args: &[Value]) -> Result<Value> {
340+
println!("execcuting exec");
341+
match args {
342+
[address, command, arguments] => {
343+
let address = &deserialize_helper(address)?;
344+
let command = &deserialize_helper(command)?;
345+
let mut arg: Vec<redis::RedisParameter> = vec![];
346+
if arguments.is_array() {
347+
let mut props = arguments.properties()?;
348+
while let Some(ref _x) = props.next_key()? {
349+
let val = props.next_value()?;
350+
if val.is_big_int() {
351+
let deserializer = &mut Deserializer::from(val.clone());
352+
let temp = i64::deserialize(deserializer)?;
353+
arg.push(redis::RedisParameter::Int64(temp));
354+
} else if val.is_array_buffer() {
355+
let deserializer = &mut Deserializer::from(val.clone());
356+
let temp = ByteBuf::deserialize(deserializer)?;
357+
arg.push(redis::RedisParameter::Binary(&temp));
358+
} else {
359+
bail!("invalid argument type, must be bigint or arraybuffer")
360+
}
361+
}
362+
} else {
363+
bail!("invalid argument type, must be array")
364+
}
365+
let results = redis::execute(address, command, &arg)
366+
.map_err(|_| anyhow!("Error executing Redis execute command"))?;
367+
let arr = context.array_value()?;
368+
for result in results.iter() {
369+
match result {
370+
RedisResult::Nil => arr.append_property(context.undefined_value()?)?,
371+
RedisResult::Status(val) => {
372+
arr.append_property(context.value_from_str(&val)?)?
373+
}
374+
RedisResult::Int64(val) => {
375+
arr.append_property(context.value_from_i64(val.to_owned())?)?
376+
}
377+
RedisResult::Binary(val) => {
378+
let mut serializer = Serializer::from_context(context)?;
379+
val.serialize(&mut serializer)?;
380+
arr.append_property(serializer.value)?;
381+
}
382+
}
383+
}
384+
return Ok(arr);
385+
}
386+
_ => bail!(
387+
"expected a two arguments (address, key), got {} arguments",
388+
args.len()
389+
),
390+
}
391+
}
392+
334393
fn redis_get(context: &Context, _this: &Value, args: &[Value]) -> Result<Value> {
335394
match args {
336395
[address, key] => {
@@ -680,6 +739,7 @@ fn do_init() -> Result<()> {
680739
redis.set_property("sadd", context.wrap_callback(redis_sadd)?)?;
681740
redis.set_property("smembers", context.wrap_callback(redis_smembers)?)?;
682741
redis.set_property("srem", context.wrap_callback(redis_srem)?)?;
742+
redis.set_property("execute", context.wrap_callback(redis_exec)?)?;
683743

684744
let kv = context.object_value()?;
685745
kv.set_property("open", context.wrap_callback(open_kv)?)?;

examples/typescript/outbound_redis/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"build": "npx webpack --mode=production && mkdir -p target && spin js2wasm -o target/spin-http-js.wasm dist/spin.js",
7+
"build": "npx webpack --mode=production && mkdir -p target && ../../../target/release/spinjs -o target/spin-http-js.wasm dist/spin.js",
88
"test": "echo \"Error: no test specified\" && exit 1"
99
},
1010
"keywords": [],

examples/typescript/outbound_redis/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ export const handleRequest: HandleRequest = async function (request: HttpRequest
1818

1919
spinSdk.redis.publish(redisAddress, "test", encoder.encode("This is a test").buffer)
2020

21+
//@ts-ignore
22+
spinSdk.redis.execute(redisAddress, "sadd", [BigInt(64), BigInt(617124)])
23+
2124
return {
2225
status: 200,
2326
headers: {"foo": "bar"},

0 commit comments

Comments
 (0)