Skip to content

Commit

Permalink
Merge pull request #292 from karthik2804/fix_kv
Browse files Browse the repository at this point in the history
  • Loading branch information
radu-matei authored Nov 8, 2024
2 parents d66e95a + e10a929 commit 514df56
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fermyon/spin-sdk",
"version": "2.3.0-rc1",
"version": "2.3.0-rc2",
"description": "",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down
10 changes: 6 additions & 4 deletions src/keyValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ function createKvStore(store: spinKv.store): Store {
return store.get(key);
},
set: (key: string, value: Uint8Array | string | object) => {
if (typeof value === 'string') {
value = encoder.encode(value);
} else if (typeof value === 'object') {
value = encoder.encode(JSON.stringify(value));
if (!(value instanceof Uint8Array)) {
if (typeof value === 'string') {
value = encoder.encode(value);
} else if (typeof value === 'object') {
value = encoder.encode(JSON.stringify(value));
}
}
store.set(key, value);
},
Expand Down
31 changes: 31 additions & 0 deletions test/test-app/src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ async function kvTest(req: Request, res: ResponseBuilder) {
let store = Kv.openDefault()
store.set("test", "try")
decoder.decode(store.get("test") || new Uint8Array()) == "try" ? res.status(200) : res.status(500)

// Test for setting uint*arrays directly
let arr = new Uint8Array([1, 2, 3])
store.set("arr", arr)
let ret = store.get("arr")

if (ret == null || !isEqualBytes(ret, arr)) {
res.status(500)
res.send()
return
}
res.send()
}

Expand Down Expand Up @@ -71,4 +82,24 @@ export {
statusTest,
outboundHttp,
kvTest
}

function isEqualBytes(
bytes1: Uint8Array,
bytes2: Uint8Array

): boolean {

if (bytes1.length !== bytes2.length) {
return false;
}

for (let i = 0; i < bytes1.length; i++) {
if (bytes1[i] !== bytes2[i]) {
return false;
}
}

return true;

}

0 comments on commit 514df56

Please sign in to comment.