From e10a929e813c491504dc38696752e6816f53fb23 Mon Sep 17 00:00:00 2001 From: karthik2804 Date: Fri, 8 Nov 2024 12:12:26 +0100 Subject: [PATCH] fix kv serialization and bump package version Signed-off-by: karthik2804 --- package-lock.json | 4 ++-- package.json | 2 +- src/keyValue.ts | 10 ++++++---- test/test-app/src/test.ts | 31 +++++++++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 7 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2d267d9e..c43e5c9e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@fermyon/spin-sdk", - "version": "2.3.0-rc1", + "version": "2.3.0-rc2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@fermyon/spin-sdk", - "version": "2.3.0-rc1", + "version": "2.3.0-rc2", "hasInstallScript": true, "license": "Apache-2.0", "dependencies": { diff --git a/package.json b/package.json index ecc85082..1521e889 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/keyValue.ts b/src/keyValue.ts index 5cc0b2d9..b5b5836b 100644 --- a/src/keyValue.ts +++ b/src/keyValue.ts @@ -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); }, diff --git a/test/test-app/src/test.ts b/test/test-app/src/test.ts index 2191d18a..c8ac0070 100644 --- a/test/test-app/src/test.ts +++ b/test/test-app/src/test.ts @@ -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() } @@ -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; + } \ No newline at end of file