Skip to content

Commit 8a03a79

Browse files
authored
fix(kv): handle binary files (#76)
1 parent 4b8b0e1 commit 8a03a79

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

app/__tests__/kv-namespace_spec.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ async function createFileNamespace(initialData) {
3232
};
3333
}
3434

35+
let nsObjTestVals = {
36+
memory: 'value',
37+
file: Buffer.from('value'),
38+
};
39+
3540
const namespaceCreators = {
3641
memory: createMemoryNamespace,
3742
file: createFileNamespace,
@@ -59,7 +64,7 @@ describe('kv-namespace', () => {
5964
metadata: null,
6065
},
6166
});
62-
expect(await ns.get('key')).toBe('value');
67+
expect(await ns.get('key')).toStrictEqual(nsObjTestVals[namespaceType]);
6368
});
6469

6570
test('it gets text', async () => {
@@ -70,7 +75,7 @@ describe('kv-namespace', () => {
7075
metadata: null,
7176
},
7277
});
73-
expect(await ns.get('key', 'text')).toBe('value');
78+
expect(await ns.get('key', 'text')).toStrictEqual(nsObjTestVals[namespaceType]);
7479
});
7580

7681
test('it gets json', async () => {
@@ -136,7 +141,7 @@ describe('kv-namespace', () => {
136141
},
137142
});
138143
expect(await ns.getWithMetadata('key')).toStrictEqual({
139-
value: 'value',
144+
value: nsObjTestVals[namespaceType],
140145
metadata: { testing: true },
141146
});
142147
});
@@ -150,7 +155,7 @@ describe('kv-namespace', () => {
150155
},
151156
});
152157
expect(await ns.getWithMetadata('key', 'text')).toStrictEqual({
153-
value: 'value',
158+
value: nsObjTestVals[namespaceType],
154159
metadata: { testing: true },
155160
});
156161
});
@@ -231,7 +236,7 @@ describe('kv-namespace', () => {
231236
const { ns, storedFor } = await createNamespace();
232237
await ns.put('key', 'value');
233238
await expect(await storedFor('key')).toStrictEqual({
234-
value: 'value',
239+
value: nsObjTestVals[namespaceType],
235240
expiration: -1,
236241
metadata: null,
237242
});
@@ -241,7 +246,7 @@ describe('kv-namespace', () => {
241246
const { ns, storedFor } = await createNamespace();
242247
await ns.put('key', new Uint8Array([1, 2, 3]).buffer);
243248
await expect(await storedFor('key')).toStrictEqual({
244-
value: '\x01\x02\x03',
249+
value: namespaceType === 'memory' ? '\x01\x02\x03' : Buffer.from([1, 2, 3]),
245250
expiration: -1,
246251
metadata: null,
247252
});
@@ -251,7 +256,7 @@ describe('kv-namespace', () => {
251256
const { ns, storedFor } = await createNamespace();
252257
await ns.put('key', 'value', { expiration: 1000 });
253258
await expect(await storedFor('key')).toStrictEqual({
254-
value: 'value',
259+
value: nsObjTestVals[namespaceType],
255260
expiration: 1000,
256261
metadata: null,
257262
});
@@ -261,7 +266,7 @@ describe('kv-namespace', () => {
261266
const { ns, storedFor } = await createNamespace();
262267
await ns.put('key', 'value', { expiration: '1000' });
263268
await expect(await storedFor('key')).toStrictEqual({
264-
value: 'value',
269+
value: nsObjTestVals[namespaceType],
265270
expiration: 1000,
266271
metadata: null,
267272
});
@@ -272,7 +277,7 @@ describe('kv-namespace', () => {
272277
const { ns, storedFor } = await createNamespace();
273278
await ns.put('key', 'value', { expirationTtl: 1000 });
274279
await expect(await storedFor('key')).toStrictEqual({
275-
value: 'value',
280+
value: nsObjTestVals[namespaceType],
276281
expiration: 2000,
277282
metadata: null,
278283
});
@@ -283,7 +288,7 @@ describe('kv-namespace', () => {
283288
const { ns, storedFor } = await createNamespace();
284289
await ns.put('key', 'value', { expirationTtl: '1000' });
285290
await expect(await storedFor('key')).toStrictEqual({
286-
value: 'value',
291+
value: nsObjTestVals[namespaceType],
287292
expiration: 2000,
288293
metadata: null,
289294
});
@@ -293,7 +298,7 @@ describe('kv-namespace', () => {
293298
const { ns, storedFor } = await createNamespace();
294299
await ns.put('key', 'value', { metadata: { testing: true } });
295300
await expect(await storedFor('key')).toStrictEqual({
296-
value: 'value',
301+
value: nsObjTestVals[namespaceType],
297302
expiration: -1,
298303
metadata: { testing: true },
299304
});
@@ -306,7 +311,7 @@ describe('kv-namespace', () => {
306311
metadata: { testing: true },
307312
});
308313
await expect(await storedFor('key')).toStrictEqual({
309-
value: 'value',
314+
value: nsObjTestVals[namespaceType],
310315
expiration: 1000,
311316
metadata: { testing: true },
312317
});
@@ -320,7 +325,7 @@ describe('kv-namespace', () => {
320325
metadata: { testing: true },
321326
});
322327
await expect(await storedFor('key')).toStrictEqual({
323-
value: 'value',
328+
value: nsObjTestVals[namespaceType],
324329
expiration: 2000,
325330
metadata: { testing: true },
326331
});
@@ -339,7 +344,7 @@ describe('kv-namespace', () => {
339344
metadata: { testing: true },
340345
});
341346
await expect(await storedFor('key')).toStrictEqual({
342-
value: 'value2',
347+
value: namespaceType === 'memory' ? 'value2' : Buffer.from('value2', 'utf-8'),
343348
expiration: 1000,
344349
metadata: { testing: true },
345350
});

app/file-kv-store.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const { KVNamespace, allLister } = require('./kv-namespace');
77
/** Reads a file's contents, returning null if the file couldn't be found */
88
async function readFile(filePath) {
99
return new Promise((resolve, reject) => {
10-
fs.readFile(filePath, 'utf8', (err, data) => {
10+
fs.readFile(filePath, (err, data) => {
1111
if (err) {
1212
if (err.code === 'ENOENT') {
1313
resolve(null); // File not found

app/kv-namespace.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class KVNamespace {
8585
if (type === 'json') {
8686
typedValue = JSON.parse(value);
8787
} else if (type === 'arrayBuffer') {
88-
const buffer = new TextEncoder().encode(value).buffer;
88+
const buffer = typeof value === 'string' ? new TextEncoder().encode(value) : value;
8989
// The API expects an ArrayBuffer to be returned, but Workers Sites expects there to be a length property (equal
9090
// to the number of bytes in the buffer) which doesn't exist by default on ArrayBuffers. So we add a read-only
9191
// length property equal to the byteLength.

0 commit comments

Comments
 (0)