Skip to content

Commit 4ff4fa4

Browse files
committed
fix: address pr comments
1 parent 45f8af8 commit 4ff4fa4

File tree

4 files changed

+19
-17
lines changed

4 files changed

+19
-17
lines changed

packages/miniflare/src/workers/kv/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export const SiteBindings = {
3434
// This ensures edge caching of Workers Sites files is disabled, and the latest
3535
// local version is always served.
3636
export const SITES_NO_CACHE_PREFIX = "$__MINIFLARE_SITES__$/";
37+
export const MAX_BULK_GET_KEYS = 100;
3738

3839
export function encodeSitesKey(key: string): string {
3940
// `encodeURIComponent()` ensures `ETag`s used by `@cloudflare/kv-asset-handler`

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
PUT,
1313
RouteHandler,
1414
} from "miniflare:shared";
15-
import { KVHeaders, KVLimits, KVParams } from "./constants";
15+
import { KVHeaders, KVLimits, KVParams, MAX_BULK_GET_KEYS } from "./constants";
1616
import {
1717
decodeKey,
1818
decodeListOptions,
@@ -22,7 +22,6 @@ import {
2222
validatePutOptions,
2323
} from "./validator.worker";
2424

25-
const MAX_BULK_GET_KEYS = 100;
2625
interface KVParams {
2726
key: string;
2827
}
@@ -78,8 +77,8 @@ function secondsToMillis(seconds: number): number {
7877

7978
async function processKeyValue(
8079
obj: KeyValueEntry<unknown> | null,
81-
type: string = "text",
82-
withMetadata: boolean = false
80+
type: "text" | "json" = "text",
81+
withMetadata = false
8382
) {
8483
const decoder = new TextDecoder();
8584
let decodedValue = "";
@@ -92,19 +91,18 @@ async function processKeyValue(
9291

9392
let val = null;
9493
try {
95-
val =
96-
obj?.value == null
97-
? null
98-
: type === "json"
99-
? JSON.parse(decodedValue)
100-
: decodedValue;
94+
val = !obj?.value
95+
? null
96+
: type === "json"
97+
? JSON.parse(decodedValue)
98+
: decodedValue;
10199
} catch (err: any) {
102100
throw new HttpError(
103101
400,
104-
"At least of of the requested keys corresponds to a non-JSON value"
102+
"At least one of the requested keys corresponds to a non-JSON value"
105103
);
106104
}
107-
if (val == null) {
105+
if (val === null) {
108106
return null;
109107
}
110108
if (withMetadata) {
@@ -137,11 +135,13 @@ export class KVNamespaceObject extends MiniflareDurableObject {
137135
const keys: string[] = parsedBody.keys;
138136
const type = parsedBody?.type;
139137
if (type && type !== "text" && type !== "json") {
140-
return new Response("", { status: 400 });
138+
return new Response(`Type ${type} is invalid`, { status: 400 });
141139
}
142140
const obj: { [key: string]: any } = {};
143141
if (keys.length > MAX_BULK_GET_KEYS) {
144-
return new Response("", { status: 400 });
142+
return new Response(`Accepting a max of 100 keys, got ${keys.length}`, {
143+
status: 400,
144+
});
145145
}
146146
for (const key of keys) {
147147
validateGetOptions(key, { cacheTtl: parsedBody?.cacheTtl });

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import consumers from "stream/consumers";
66
import { Macro, ThrowsExpectation } from "ava";
77
import {
88
KV_PLUGIN_NAME,
9+
MAX_BULK_GET_KEYS,
910
Miniflare,
1011
MiniflareOptions,
1112
ReplaceWorkersTypes,
@@ -139,7 +140,7 @@ test("bulk get: check max keys", async (t) => {
139140
const { kv } = t.context;
140141
await kv.put("key1", "value1");
141142
const keyArray = [];
142-
for (let i = 0; i <= 100; i++) {
143+
for (let i = 0; i <= MAX_BULK_GET_KEYS; i++) {
143144
keyArray.push(`key${i}`);
144145
}
145146
try {
@@ -167,7 +168,7 @@ test("bulk get: request json type", async (t) => {
167168
} catch (error: any) {
168169
t.is(
169170
error.message,
170-
"KV GET_BULK failed: 400 At least of of the requested keys corresponds to a non-JSON value"
171+
"KV GET_BULK failed: 400 At least one of the requested keys corresponds to a non-JSON value"
171172
);
172173
}
173174
});

packages/miniflare/test/test-shared/miniflare.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export function namespace<T>(ns: string, binding: T): Namespaced<T> {
4545
if (result instanceof Promise) {
4646
return result.then((res) => {
4747
// KV.get([a,b,c]) would be prefixed with ns, so we strip this prefix from response.
48-
// Map keys => [ns-a, ns-b, ns-c] -> [a,b,c]
48+
// Map keys => [{ns}{a}, {ns}{b}, {ns}{b}] -> [a,b,c]
4949
if (res instanceof Map) {
5050
const newResult = new Map<string, unknown>();
5151
for (const [key, value] of res) {

0 commit comments

Comments
 (0)