Skip to content

Commit da807de

Browse files
committed
update workerd version
1 parent c9f0edd commit da807de

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

packages/miniflare/src/workers/kv/namespace.worker.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import {
33
DeferredPromise,
44
DELETE,
55
GET,
6-
POST,
76
HttpError,
7+
KeyValueEntry,
88
KeyValueStorage,
99
maybeApply,
1010
MiniflareDurableObject,
11+
POST,
1112
PUT,
1213
RouteHandler,
13-
KeyValueEntry,
1414
} from "miniflare:shared";
1515
import { KVHeaders, KVLimits, KVParams } from "./constants";
1616
import {
@@ -75,10 +75,14 @@ function secondsToMillis(seconds: number): number {
7575
return seconds * 1000;
7676
}
7777

78-
async function processKeyValue(obj: KeyValueEntry<unknown> | null, type: string = "text", withMetadata: boolean = false) {
78+
async function processKeyValue(
79+
obj: KeyValueEntry<unknown> | null,
80+
type: string = "text",
81+
withMetadata: boolean = false
82+
) {
7983
const decoder = new TextDecoder();
8084
let r = "";
81-
if(obj?.value) {
85+
if (obj?.value) {
8286
for await (const chunk of obj?.value) {
8387
r += decoder.decode(chunk, { stream: true });
8488
}
@@ -89,16 +93,22 @@ async function processKeyValue(obj: KeyValueEntry<unknown> | null, type: string
8993
try {
9094
val = obj?.value == null ? null : type === "json" ? JSON.parse(r) : r;
9195
} catch (err: any) {
92-
throw new HttpError(400, "At least of of the requested keys corresponds to a non-JSON value");
96+
throw new HttpError(
97+
400,
98+
"At least of of the requested keys corresponds to a non-JSON value"
99+
);
93100
}
94101
if (val == null) {
95102
return null;
96103
}
97104
if (withMetadata) {
98-
return { value: val, metadata: obj?.metadata ? JSON.stringify(obj?.metadata) : null };
105+
return {
106+
value: val,
107+
metadata: obj?.metadata ? JSON.stringify(obj?.metadata) : null,
108+
};
99109
}
100110
return val;
101-
}
111+
}
102112

103113
export class KVNamespaceObject extends MiniflareDurableObject {
104114
#storage?: KeyValueStorage;
@@ -115,7 +125,8 @@ export class KVNamespaceObject extends MiniflareDurableObject {
115125
const cacheTtlParam = url.searchParams.get(KVParams.CACHE_TTL);
116126
const cacheTtl =
117127
cacheTtlParam === null ? undefined : parseInt(cacheTtlParam);
118-
if(req.body != null) { // get bulk
128+
if (req.method === "POST" && req.body != null) {
129+
// get bulk
119130
// get bulk
120131
let r = "";
121132
const decoder = new TextDecoder();
@@ -125,11 +136,15 @@ export class KVNamespaceObject extends MiniflareDurableObject {
125136
r += decoder.decode();
126137
const parsedBody = JSON.parse(r);
127138
const keys: string[] = parsedBody.keys;
128-
const obj: {[key: string]: any} = {};
129-
for(const key of keys) {
139+
const obj: { [key: string]: any } = {};
140+
for (const key of keys) {
130141
validateGetOptions(key, { cacheTtl: parsedBody?.cacheTtl });
131142
const entry = await this.storage.get(key);
132-
obj[key] = await processKeyValue(entry, parsedBody?.type, parsedBody?.withMetadata);
143+
obj[key] = await processKeyValue(
144+
entry,
145+
parsedBody?.type,
146+
parsedBody?.withMetadata
147+
);
133148
}
134149
return new Response(JSON.stringify(obj));
135150
}

packages/miniflare/test/plugins/kv/index.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,17 @@ test("get: returns value", async (t) => {
122122
const result = await kv.get("key");
123123
t.is(result, "value");
124124
});
125+
126+
// test("bulk get: returns value", async (t) => {
127+
// const { kv } = t.context;
128+
// await kv.put("key", "value");
129+
// const result = await kv.get(["key"]);
130+
// const expectedResult = new Map([
131+
// ['key', 'value1'],
132+
// ]);
133+
// t.is(result, expectedResult);
134+
// });
135+
125136
test("get: returns null for non-existent keys", async (t) => {
126137
const { kv } = t.context;
127138
t.is(await kv.get("key"), null);

0 commit comments

Comments
 (0)