/
_stringify.ts
58 lines (56 loc) · 2.68 KB
/
_stringify.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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
/** Optional object interface for `JSONStringifyStream`. */
export interface StringifyStreamOptions {
/** Prefix to be added after stringify. The default is "". */
readonly prefix?: string;
/** Suffix to be added after stringify. The default is "\n". */
readonly suffix?: string;
/** Controls the buffer of the TransformStream used internally. Check https://developer.mozilla.org/en-US/docs/Web/API/TransformStream/TransformStream. */
readonly writableStrategy?: QueuingStrategy<unknown>;
/** Controls the buffer of the TransformStream used internally. Check https://developer.mozilla.org/en-US/docs/Web/API/TransformStream/TransformStream. */
readonly readableStrategy?: QueuingStrategy<string>;
}
/**
* Convert each chunk to JSON string.
*
* This can be used to stringify [JSON lines](https://jsonlines.org/), [NDJSON](http://ndjson.org/), [JSON Text Sequences](https://datatracker.ietf.org/doc/html/rfc7464), and [Concatenated JSON](https://en.wikipedia.org/wiki/JSON_streaming#Concatenated_JSON).
* You can optionally specify a prefix and suffix for each chunk. The default prefix is "" and the default suffix is "\n".
*
* ```ts
* import { readableStreamFromIterable } from "https://deno.land/std@$STD_VERSION/streams/mod.ts";
* import { JSONStringifyStream } from "https://deno.land/std@$STD_VERSION/encoding/json/stream.ts";
*
* const file = await Deno.open("./tmp.jsonl", { create: true, write: true });
*
* readableStreamFromIterable([{ foo: "bar" }, { baz: 100 }])
* .pipeThrough(new JSONStringifyStream())
* .pipeThrough(new TextEncoderStream())
* .pipeTo(file.writable)
* .then(() => console.log("write success"));
* ```
*/
export class JSONStringifyStream extends TransformStream<unknown, string> {
/**
* @param options
* @param options.prefix Prefix to be added after stringify. The default is "".
* @param options.suffix Suffix to be added after stringify. The default is "\n".
* @param options.writableStrategy Controls the buffer of the TransformStream used internally. Check https://developer.mozilla.org/en-US/docs/Web/API/TransformStream/TransformStream.
* @param options.readableStrategy Controls the buffer of the TransformStream used internally. Check https://developer.mozilla.org/en-US/docs/Web/API/TransformStream/TransformStream.
*/
constructor({
prefix = "",
suffix = "\n",
writableStrategy,
readableStrategy,
}: StringifyStreamOptions = {}) {
super(
{
transform(chunk, controller) {
controller.enqueue(`${prefix}${JSON.stringify(chunk)}${suffix}`);
},
},
writableStrategy,
readableStrategy,
);
}
}