-
Notifications
You must be signed in to change notification settings - Fork 5
/
ndjson.ts
501 lines (478 loc) Β· 14.3 KB
/
ndjson.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/**
* Utilities for handling [NDJSON](https://github.com/ndjson/ndjson-spec) which
* is a method for encoding JSON in a way that supports streaming, where each
* JSON entity is separated with a newline.
*
* ## Exporting Deno KV entries
*
* {@linkcode exportEntries} works like {@linkcode Deno.Kv} `.list()` but the
* response is a stream of records encoded as NDJSON. By default it provides
* the response as a byte stream. Using the `text` option will cause the stream
* to emit individual records.
*
* {@linkcode exportToResponse} also works like {@linkcode Deno.Kv} `.list()`
* but returns a {@linkcode Response} which can be sent to a client where the
* body of the response are the entries encoded as NDJSON records. Using the
* `filename` options will also set the `"Content-Disposition"` header in the
* response which will indicate to a client to treat the file as a download.
*
* ## Decoding NDJSON
*
* The {@linkcode LinesTransformStream} takes a byte stream and outputs
* individual lines which can be used to attempt to parse them as JSON strings.
*
* ## Importing Deno KV entries
*
* The {@linkcode importEntries} function can be used to import data encoded as
* NDJSON into a {@linkcode Deno.Kv} store. The data can be in provided in
* several forms, including byte {@linkcode ReadableStream}, {@linkcode Blob},
* {@linkcode File}, {@linkcode ArrayBuffer}, typed array or string.
*
* @module
*/
import {
entryToJSON,
type KvEntryJSON,
toKey,
toValue,
} from "@deno/kv-utils/json";
import { concat } from "@std/bytes/concat";
export interface ExportEntriesOptionsJSON extends Deno.KvListOptions {
/**
* Determines if the function should close the provided KV store once all the
* entities are exported. By default, the store won't be closed.
*/
close?: boolean;
/**
* Determines if the chunks of the readable stream will be "raw" JSON entries
* or already encoded as a byte stream of NDJSON. If `true` they will be
* individual JSON entries, otherwise a byte stream.
*/
text: true;
}
export interface ExportEntriesOptionsBytes extends Deno.KvListOptions {
/**
* Determines if the function should close the provided KV store once all the
* entities are exported. By default, the store won't be closed.
*/
close?: boolean;
/**
* Determines if the chunks of the readable stream will be "raw" JSON entries
* or already encoded as a byte stream of NDJSON. If `true` they will be
* individual JSON entries, otherwise a byte stream.
*/
text?: boolean;
}
/**
* Extends {@linkcode Deno.KvListOptions} with the `json` option.
*/
export type ExportEntriesOptions =
| ExportEntriesOptionsJSON
| ExportEntriesOptionsBytes;
/**
* Options which can be set on {@linkcode exportToResponse}.
*/
export interface ExportToResponseOptions extends Deno.KvListOptions {
/**
* Determines if the function should close the provided KV store once all the
* entities are exported. By default, the store won't be closed.
*/
close?: boolean;
/**
* If provided, the response will include a header that indicates the file is
* meant to be downloaded (`Content-Disposition`). The extension `.ndjson`
* will be appended to the filename.
*/
filename?: string;
}
/**
* Options which can be set when calling {@linkcode importEntries}.
*/
export interface ImportEntriesOptions {
/**
* Determines what happens when a key already exists in the target store for
* an entry being being import. By default the entry will be skipped. Setting
* the `overwrite` option to `true` will cause any existing value to be
* overwritten with the imported value.
*/
overwrite?: boolean;
/**
* An optional callback which occurs when an error is encountered when
* importing entries. The supplied error will provide details about what was
* occurring.
*
* See {@linkcode ImportError} for more details.
*/
onError?: (error: ImportError) => void;
/**
* An optional callback which occurs every time an entry has been successfully
* processed, providing an update of the number of entries processed, the
* number of those that were skipped and the number of those that errored.
*/
onProgress?: (count: number, skipped: number, errors: number) => void;
/**
* The prefix which should be prepended to the front of each entry key when
* importing. This makes it useful to "namespace" imported data. For example
* if you were bring in a data set of people, you might supply the
* {@linkcode Deno.KvKey} of `["person"]`. The imported entry key of `[1]`
* would then become `["person", 1]`.
*/
prefix?: Deno.KvKey;
/**
* Used to stop the import process. When the signal is aborted, the current
* import entry will be completed and then the function will return.
*/
signal?: AbortSignal;
/**
* By default, {@linkcode importEntries} will not throw on errors that occur
* while processing the import data, but just increment the `errors` value
* and call the `onError()` callback if provided.
*
* By setting this to `true`, an {@linkcode ImportError} will be thrown when
* an error is encountered and terminate the import process.
*/
throwOnError?: boolean;
}
/**
* The result returned from calling {@linkcode importEntries}.
*/
export interface ImportEntriesResult {
/** If set, the import process was aborted prior to completing. */
aborted?: true;
/** The number of entries read from the input data. */
count: number;
/** The number of entries skipped from the input data. Entries are skipped
* if a matching entry key is already present in the target, unless the
* `overwrite` option is set to `true`.
*/
skipped: number;
/**
* The number of entries that errored while processing the data.
*/
errors: number;
}
interface ImportErrorOptions extends ErrorOptions {
count: number;
errors: number;
json?: string;
kv: Deno.Kv;
skipped: number;
}
/**
* The media type associated with NDJSON.
*/
export const MEDIA_TYPE_NDJSON = "application/x-ndjson";
/**
* The media type for JSONL which is compatible with NDJSON.
*/
export const MEDIA_TYPE_JSONL = "application/jsonl";
/**
* The media type for JSON Lines which is compatible with NDJSON.
*/
export const MEDIA_TYPE_JSON_LINES = "application/json-lines";
/**
* The file extension to use with NDJSON files.
*/
export const EXT_NDJSON = ".ndjson";
const LF = 0x0a;
const CR = 0x0d;
const encoder = new TextEncoder();
const decoder = new TextDecoder();
function stripEol(u8: Uint8Array): Uint8Array {
const length = u8.byteLength;
if (u8[length - 1] === LF) {
let drop = 1;
if (length > 1 && u8[length - 2] === CR) {
drop = 2;
}
return u8.subarray(0, length - drop);
}
return u8;
}
/**
* Like {@linkcode Deno.Kv} `.list()` method, but returns a
* {@linkcode ReadableStream} where entries are converted to a JSON structure.
*
* This is ideal for streaming ndjson as part of a response.
*/
export function exportEntries(
kv: Deno.Kv,
selector: Deno.KvListSelector,
options: ExportEntriesOptionsJSON,
): ReadableStream<string>;
/**
* Like {@linkcode Deno.Kv} `.list()` method, but returns a
* {@linkcode ReadableStream} where entries are already converted to their
* raw byte representation after being encoded as JSON.
*
* This is ideal for streaming ndjson as part of a response.
*/
export function exportEntries(
kv: Deno.Kv,
selector: Deno.KvListSelector,
options?: ExportEntriesOptionsBytes,
): ReadableStream<Uint8Array>;
export function exportEntries(
kv: Deno.Kv,
selector: Deno.KvListSelector,
options: ExportEntriesOptions = {},
): ReadableStream<string | Uint8Array> {
const text = options.text ?? false;
let cancelled = false;
return new ReadableStream<string | Uint8Array>({
async start(controller) {
try {
for await (const entry of kv.list(selector, options)) {
const chunk = entryToJSON(entry);
controller.enqueue(
text
? `${JSON.stringify(chunk)}\n`
: encoder.encode(`${JSON.stringify(chunk)}\n`),
);
if (cancelled) {
return;
}
}
if (options.close) {
kv.close();
}
controller.close();
} catch (error) {
controller.error(error);
}
},
cancel(_reason) {
cancelled = true;
},
});
}
/**
* Like {@linkcode Deno.Kv} `.list()` method, but returns a {@linkcode Response}
* which will have a body that will be the exported entries that match the
* selector.
*
* The response will contain the appropriate content type and the `filename`
* option can be used to set the content disposition header so the client
* understands a file is being downloaded.
*/
export function exportToResponse(
kv: Deno.Kv,
selector: Deno.KvListSelector,
options: ExportToResponseOptions = {},
): Response {
const body = exportEntries(kv, selector, options);
const init = {
headers: {
"content-type": MEDIA_TYPE_NDJSON,
} as Record<string, string>,
} satisfies ResponseInit;
if (options.filename) {
init.headers["content-disposition"] =
`attachment; filename="${options.filename}${EXT_NDJSON}"`;
}
return new Response(body, init);
}
export class LinesTransformStream extends TransformStream<Uint8Array, string> {
#buffer = new Uint8Array(0);
#pos = 0;
constructor() {
super({
transform: (chunk, controller) => {
this.#transform(chunk, controller);
},
flush: (controller) => {
const slice = stripEol(this.#buffer.subarray(this.#pos));
if (slice.length) {
try {
controller.enqueue(decoder.decode(slice));
} catch (error) {
controller.error(error);
}
}
},
});
}
#readLineBytes(): Uint8Array | null {
let slice: Uint8Array | null = null;
const i = this.#buffer.subarray(this.#pos).indexOf(LF);
if (i >= 0) {
slice = this.#buffer.subarray(this.#pos, this.#pos + i + 1);
this.#pos += i + 1;
return stripEol(slice);
}
return null;
}
*#lines(): IterableIterator<string | null> {
while (true) {
const bytes = this.#readLineBytes();
if (!bytes) {
this.#truncate();
return null;
}
yield decoder.decode(bytes);
}
}
#transform(
chunk: Uint8Array,
controller: TransformStreamDefaultController<string>,
) {
this.#buffer = concat([this.#buffer, chunk]);
const iterator = this.#lines();
while (true) {
try {
const result = iterator.next();
if (result.value) {
controller.enqueue(result.value);
}
if (result.done) {
break;
}
} catch (error) {
controller.error(error);
}
}
}
#truncate() {
this.#buffer = this.#buffer.slice(this.#pos);
this.#pos = 0;
}
}
/**
* An error that can occur when importing records into a {@linkcode Deno.Kv}
* store. Information associated with the error is available with the `cause`
* being set to the original error that was thrown.
*/
export class ImportError extends Error {
#count: number;
#errors: number;
#json?: string;
#kv: Deno.Kv;
#skipped: number;
/**
* The number of entries that had been read from the stream when the
* error occurred.
*/
get count(): number {
return this.#count;
}
/**
* The number of errors in aggregate that had occurred to this point.
*/
get errors(): number {
return this.#errors;
}
/**
* If available, the most recent JSON string what had been read from the data.
*/
get json(): string | undefined {
return this.#json;
}
/**
* Reference to the {@linkcode Deno.Kv} store that was the target for the
* import.
*/
get kv(): Deno.Kv {
return this.#kv;
}
/**
* The aggregate number of records that had been skipped.
*/
get skipped(): number {
return this.#skipped;
}
constructor(
message: string,
{ count, errors, json, kv, skipped, ...options }: ImportErrorOptions,
) {
super(message, options);
this.#count = count;
this.#errors = errors;
this.#json = json;
this.#kv = kv;
this.#skipped = skipped;
}
}
/**
* Allows NDJSON to be imported in a target {@linkcode Deno.Kv}.
*
* The `data` can be in multiple forms, including {@linkcode ReadableStream},
* {@linkcode Blob}, {@linkcode File}, {@linkcode ArrayBuffer}, typed array, or
* string.
*/
export async function importEntries(
kv: Deno.Kv,
data:
| ReadableStream<Uint8Array>
| Blob
| ArrayBufferView
| ArrayBuffer
| string,
options: ImportEntriesOptions = {},
): Promise<ImportEntriesResult> {
const {
overwrite = false,
prefix = [],
onError,
onProgress,
signal,
throwOnError,
} = options;
let stream: ReadableStream<string>;
const transformer = new LinesTransformStream();
if (data instanceof ReadableStream) {
stream = data.pipeThrough(transformer);
} else if (data instanceof Blob) {
stream = data.stream().pipeThrough(transformer);
} else {
stream = new Blob([data]).stream().pipeThrough(transformer);
}
const reader = stream.getReader();
let count = 0;
let errors = 0;
let skipped = 0;
while (true) {
let result: ReadableStreamReadResult<string> | undefined = undefined;
try {
result = await reader.read();
if (result.value) {
count++;
const entry: KvEntryJSON = JSON.parse(result.value);
const { key, value } = entry;
const entryKey = prefix.length
? [...prefix, ...toKey(key)]
: toKey(key);
if (!overwrite) {
const { versionstamp } = await kv.get(entryKey);
if (versionstamp) {
skipped++;
continue;
}
}
await kv.set(entryKey, toValue(value));
onProgress?.(count, skipped, errors);
}
if (result.done) {
break;
}
if (signal?.aborted) {
reader.releaseLock();
return { aborted: true, count, skipped, errors };
}
} catch (cause) {
errors++;
if (onError || throwOnError) {
const error = new ImportError(
cause instanceof Error ? cause.message : "An import error occurred.",
{ cause, json: result?.value, count, kv, skipped, errors },
);
onError?.(error);
if (throwOnError) {
reader.releaseLock();
throw error;
}
}
}
}
reader.releaseLock();
return { count, skipped, errors };
}