Skip to content

Commit e10a929

Browse files
committed
fix kv serialization and bump package version
Signed-off-by: karthik2804 <[email protected]>
1 parent d66e95a commit e10a929

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@fermyon/spin-sdk",
3-
"version": "2.3.0-rc1",
3+
"version": "2.3.0-rc2",
44
"description": "",
55
"main": "lib/index.js",
66
"typings": "lib/index.d.ts",

src/keyValue.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,12 @@ function createKvStore(store: spinKv.store): Store {
5757
return store.get(key);
5858
},
5959
set: (key: string, value: Uint8Array | string | object) => {
60-
if (typeof value === 'string') {
61-
value = encoder.encode(value);
62-
} else if (typeof value === 'object') {
63-
value = encoder.encode(JSON.stringify(value));
60+
if (!(value instanceof Uint8Array)) {
61+
if (typeof value === 'string') {
62+
value = encoder.encode(value);
63+
} else if (typeof value === 'object') {
64+
value = encoder.encode(JSON.stringify(value));
65+
}
6466
}
6567
store.set(key, value);
6668
},

test/test-app/src/test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ async function kvTest(req: Request, res: ResponseBuilder) {
3535
let store = Kv.openDefault()
3636
store.set("test", "try")
3737
decoder.decode(store.get("test") || new Uint8Array()) == "try" ? res.status(200) : res.status(500)
38+
39+
// Test for setting uint*arrays directly
40+
let arr = new Uint8Array([1, 2, 3])
41+
store.set("arr", arr)
42+
let ret = store.get("arr")
43+
44+
if (ret == null || !isEqualBytes(ret, arr)) {
45+
res.status(500)
46+
res.send()
47+
return
48+
}
3849
res.send()
3950
}
4051

@@ -71,4 +82,24 @@ export {
7182
statusTest,
7283
outboundHttp,
7384
kvTest
85+
}
86+
87+
function isEqualBytes(
88+
bytes1: Uint8Array,
89+
bytes2: Uint8Array
90+
91+
): boolean {
92+
93+
if (bytes1.length !== bytes2.length) {
94+
return false;
95+
}
96+
97+
for (let i = 0; i < bytes1.length; i++) {
98+
if (bytes1[i] !== bytes2[i]) {
99+
return false;
100+
}
101+
}
102+
103+
return true;
104+
74105
}

0 commit comments

Comments
 (0)