Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/spin-kv/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 packages/spin-kv/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@spinframework/spin-kv",
"version": "1.0.0",
"version": "1.0.1",
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
22 changes: 20 additions & 2 deletions packages/spin-kv/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ function createKvStore(store: spinKv.Store): Store {
if (!(value instanceof Uint8Array)) {
if (typeof value === 'string') {
value = encoder.encode(value);
} else if (value instanceof ArrayBuffer) {
value = new Uint8Array(value);
Comment on lines +62 to +63
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tiniest of nits: I would move this to the top so the encode cases stay grouped

} else if (typeof value === 'object') {
value = encoder.encode(JSON.stringify(value));
}
Expand Down Expand Up @@ -90,13 +92,29 @@ function createKvStore(store: spinKv.Store): Store {
* @returns {Store} The key-value store object.
*/
export function open(label: string): Store {
return createKvStore(spinKv.Store.open(label));
try {
return createKvStore(spinKv.Store.open(label));
} catch (error: any) {
// Wrapping the Spin KV error in a plain JS Error prevents cyclic object issues
// that occur if the original ComponentError is thrown or spread directly.
const e = new Error(error);
(e as any).payload = error.payload ?? { tag: 'unknown', val: String(error) };
throw e;
}
}

/**
* Opens the default key-value store.
* @returns {Store} The default key-value store object.
*/
export function openDefault(): Store {
return createKvStore(spinKv.Store.open('default'));
try {
return createKvStore(spinKv.Store.open('default'));
} catch (error: any) {
// Wrapping the Spin KV error in a plain JS Error prevents cyclic object issues
// that occur if the original ComponentError is thrown or spread directly.
const e = new Error(error);
(e as any).payload = error.payload ?? { tag: 'unknown', val: String(error) };
throw e;
}
}
4 changes: 2 additions & 2 deletions packages/spin-kv/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"compilerOptions": {
"target": "ES2020",
"module": "ES2020",
"module": "nodenext",
"lib": [
"ES2020",
"WebWorker"
],
"moduleResolution": "node",
"moduleResolution": "nodenext",
"declaration": true,
"outDir": "dist",
"strict": true,
Expand Down
3 changes: 2 additions & 1 deletion test/test-app/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AutoRouter } from "itty-router"
import { headersTest, health, kvTest, kvTestUint8Array, outboundHttp, statusTest, stream, streamTest, testFunctionality } from "./test";
import { headersTest, health, kvTest, kvTestArrayBuffer, kvTestUint8Array, outboundHttp, statusTest, stream, streamTest, testFunctionality } from "./test";

let router = AutoRouter()

Expand All @@ -9,6 +9,7 @@ router.get("/statusTest", statusTest)
router.get("/headersTest", headersTest)
router.get("/outboundHttp", outboundHttp)
router.get("/kvTest", kvTest)
router.get("/kvTestArrayBuffer", kvTestArrayBuffer)
router.get("/kvTestUint8Array", kvTestUint8Array)
router.get("/streamTest", streamTest)
router.get("/testFunctionality", testFunctionality)
Expand Down
14 changes: 14 additions & 0 deletions test/test-app/src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ function kvTest(req: Request) {
return new Response("failed", { status: 500 })
}

function kvTestArrayBuffer(req: Request) {
let store = Kv.openDefault()

let arr = new Uint8Array([1, 2, 3])
store.set("arr", arr.buffer)
let ret = store.get("arr")
if (ret == null || !isEqualBytes(new Uint8Array(ret), arr)) {
return new Response("failed", { status: 500 })
}
return new Response("success", { status: 200 })
}

function kvTestUint8Array(req: Request) {
let store = Kv.openDefault()

Expand Down Expand Up @@ -87,6 +99,7 @@ async function testFunctionality(req: Request) {
{ name: "headersTest", validate: (resp: Response) => resp.status === 200 && resp.headers.get("Content-Type") === "text/html" },
{ name: "outboundHttp", validate: (resp: Response) => resp.status === 200 },
{ name: "kvTest", validate: (resp: Response) => resp.status === 200 },
{ name: "kvTestArrayBuffer", validate: (resp: Response) => resp.status === 200 },
{ name: "kvTestUint8Array", validate: (resp: Response) => resp.status === 200 },
{ name: "streamTest", validate: (resp: Response) => resp.status === 200 },
];
Expand All @@ -112,6 +125,7 @@ export {
statusTest,
outboundHttp,
kvTest,
kvTestArrayBuffer,
kvTestUint8Array,
streamTest
}
4 changes: 2 additions & 2 deletions test/test-app/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"outDir": "./dist/",
"noImplicitAny": true,
"module": "es6",
"module": "nodenext",
"target": "es2020",
"jsx": "react",
"skipLibCheck": true,
Expand All @@ -13,6 +13,6 @@
"allowJs": true,
"strict": true,
"noImplicitReturns": true,
"moduleResolution": "node"
"moduleResolution": "nodenext"
}
}