Skip to content

Commit f9ecfdb

Browse files
committed
fix handling of KV set and throw wrapped errors
Signed-off-by: Karthik Ganeshram <[email protected]>
1 parent cc30aca commit f9ecfdb

File tree

7 files changed

+43
-10
lines changed

7 files changed

+43
-10
lines changed

packages/spin-kv/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.

packages/spin-kv/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@spinframework/spin-kv",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

packages/spin-kv/src/index.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ function createKvStore(store: spinKv.Store): Store {
5959
if (!(value instanceof Uint8Array)) {
6060
if (typeof value === 'string') {
6161
value = encoder.encode(value);
62+
} else if (value instanceof ArrayBuffer) {
63+
value = new Uint8Array(value);
6264
} else if (typeof value === 'object') {
6365
value = encoder.encode(JSON.stringify(value));
6466
}
@@ -90,13 +92,29 @@ function createKvStore(store: spinKv.Store): Store {
9092
* @returns {Store} The key-value store object.
9193
*/
9294
export function open(label: string): Store {
93-
return createKvStore(spinKv.Store.open(label));
95+
try {
96+
return createKvStore(spinKv.Store.open(label));
97+
} catch (error: any) {
98+
// Wrapping the Spin KV error in a plain JS Error prevents cyclic object issues
99+
// that occur if the original ComponentError is thrown or spread directly.
100+
const e = new Error(error);
101+
(e as any).payload = error.payload ?? { tag: 'unknown', val: String(error) };
102+
throw e;
103+
}
94104
}
95105

96106
/**
97107
* Opens the default key-value store.
98108
* @returns {Store} The default key-value store object.
99109
*/
100110
export function openDefault(): Store {
101-
return createKvStore(spinKv.Store.open('default'));
111+
try {
112+
return createKvStore(spinKv.Store.open('default'));
113+
} catch (error: any) {
114+
// Wrapping the Spin KV error in a plain JS Error prevents cyclic object issues
115+
// that occur if the original ComponentError is thrown or spread directly.
116+
const e = new Error(error);
117+
(e as any).payload = error.payload ?? { tag: 'unknown', val: String(error) };
118+
throw e;
119+
}
102120
}

packages/spin-kv/tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"compilerOptions": {
33
"target": "ES2020",
4-
"module": "ES2020",
4+
"module": "nodenext",
55
"lib": [
66
"ES2020",
77
"WebWorker"
88
],
9-
"moduleResolution": "node",
9+
"moduleResolution": "nodenext",
1010
"declaration": true,
1111
"outDir": "dist",
1212
"strict": true,

test/test-app/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AutoRouter } from "itty-router"
2-
import { headersTest, health, kvTest, kvTestUint8Array, outboundHttp, statusTest, stream, streamTest, testFunctionality } from "./test";
2+
import { headersTest, health, kvTest, kvTestArrayBuffer, kvTestUint8Array, outboundHttp, statusTest, stream, streamTest, testFunctionality } from "./test";
33

44
let router = AutoRouter()
55

@@ -9,6 +9,7 @@ router.get("/statusTest", statusTest)
99
router.get("/headersTest", headersTest)
1010
router.get("/outboundHttp", outboundHttp)
1111
router.get("/kvTest", kvTest)
12+
router.get("/kvTestArrayBuffer", kvTestArrayBuffer)
1213
router.get("/kvTestUint8Array", kvTestUint8Array)
1314
router.get("/streamTest", streamTest)
1415
router.get("/testFunctionality", testFunctionality)

test/test-app/src/test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ function kvTest(req: Request) {
4646
return new Response("failed", { status: 500 })
4747
}
4848

49+
function kvTestArrayBuffer(req: Request) {
50+
let store = Kv.openDefault()
51+
52+
let arr = new Uint8Array([1, 2, 3])
53+
store.set("arr", arr.buffer)
54+
let ret = store.get("arr")
55+
if (ret == null || !isEqualBytes(new Uint8Array(ret), arr)) {
56+
return new Response("failed", { status: 500 })
57+
}
58+
return new Response("success", { status: 200 })
59+
}
60+
4961
function kvTestUint8Array(req: Request) {
5062
let store = Kv.openDefault()
5163

@@ -87,6 +99,7 @@ async function testFunctionality(req: Request) {
8799
{ name: "headersTest", validate: (resp: Response) => resp.status === 200 && resp.headers.get("Content-Type") === "text/html" },
88100
{ name: "outboundHttp", validate: (resp: Response) => resp.status === 200 },
89101
{ name: "kvTest", validate: (resp: Response) => resp.status === 200 },
102+
{ name: "kvTestArrayBuffer", validate: (resp: Response) => resp.status === 200 },
90103
{ name: "kvTestUint8Array", validate: (resp: Response) => resp.status === 200 },
91104
{ name: "streamTest", validate: (resp: Response) => resp.status === 200 },
92105
];
@@ -112,6 +125,7 @@ export {
112125
statusTest,
113126
outboundHttp,
114127
kvTest,
128+
kvTestArrayBuffer,
115129
kvTestUint8Array,
116130
streamTest
117131
}

test/test-app/tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"compilerOptions": {
33
"outDir": "./dist/",
44
"noImplicitAny": true,
5-
"module": "es6",
5+
"module": "nodenext",
66
"target": "es2020",
77
"jsx": "react",
88
"skipLibCheck": true,
@@ -13,6 +13,6 @@
1313
"allowJs": true,
1414
"strict": true,
1515
"noImplicitReturns": true,
16-
"moduleResolution": "node"
16+
"moduleResolution": "nodenext"
1717
}
1818
}

0 commit comments

Comments
 (0)