web-utility-streams 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/lib/JsonDecoderStream.d.mts +10 -2
- package/lib/JsonDecoderStream.mjs +5 -4
- package/lib/JsonTokenDecoderStream.d.mts +6 -0
- package/lib/JsonTokenDecoderStream.mjs +1 -1
- package/lib/{JsonTokenizer-BFV5P0dO.mjs → JsonTokenizer-BtRpnxKn.mjs} +2 -1
- package/lib/consumers.d.mts +3 -2
- package/lib/consumers.mjs +17 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -163,7 +163,7 @@ const strings = values.pipeThrough(
|
|
|
163
163
|
|
|
164
164
|
Decode a JSON array or object from a stream of text, emitting each element as soon as it has been parsed, without waiting for the rest of the document. This lets you start processing a large JSON response before it has finished downloading. For an array, each element is emitted. For an object, each property is emitted as a `[key, value]` tuple. By default the root of the document is streamed; if the array or object is nested inside other objects, pass the keys that lead to it as `path`.
|
|
165
165
|
|
|
166
|
-
The whole document is validated, but only the elements being emitted are kept in memory, unless you pass `onExtraData` (see below). If the document isn't valid JSON, the stream errors when the problem is found. For speed, each element (and each object or array being discarded) is parsed in one go once its end is reached, so a problem inside one is found at its end, or
|
|
166
|
+
The whole document is validated, but only the elements being emitted are kept in memory, unless you pass `onExtraData` (see below). If the document isn't valid JSON, the stream errors when the problem is found. For speed, each element (and each object or array being discarded) is parsed in one go once its end is reached, so a problem inside one is found at its end. Any single value longer than `maxValueLength` characters (default 100 MiB) errors the stream as soon as that length is exceeded, so a huge or never-ending value can't use unlimited memory. If there's no array or object at `path`, the stream errors once the whole document has been parsed. Because elements are emitted as soon as they're parsed, some may be emitted before an error later in the document is found. If a streamed object repeats a key, a tuple is emitted for each occurrence.
|
|
167
167
|
|
|
168
168
|
```ts
|
|
169
169
|
import JsonDecoderStream from "web-utility-streams/JsonDecoderStream";
|
|
@@ -20,6 +20,13 @@ export interface JsonDecoderStreamOptions {
|
|
|
20
20
|
* in memory until the end of the document, rather than being discarded.
|
|
21
21
|
*/
|
|
22
22
|
readonly onExtraData?: (extraData: unknown) => void;
|
|
23
|
+
/**
|
|
24
|
+
* The maximum length, in characters, of any single value in the document
|
|
25
|
+
* (each emitted element, each discarded object/array, and each string or
|
|
26
|
+
* number). If a value is longer than this, the stream is errored, so that
|
|
27
|
+
* an unexpectedly large (or never-ending) value can't use unlimited memory.
|
|
28
|
+
* Defaults to 100 MiB (`100 * 2 ** 20` characters).
|
|
29
|
+
*/
|
|
23
30
|
readonly maxValueLength?: number;
|
|
24
31
|
}
|
|
25
32
|
/**
|
|
@@ -39,8 +46,9 @@ export interface JsonDecoderStreamOptions {
|
|
|
39
46
|
* `onExtraData` callback. If the document is not valid JSON, the stream is
|
|
40
47
|
* errored when the problem is found. For speed, each element (and each
|
|
41
48
|
* object/array that is being discarded) is parsed in one go when its end is
|
|
42
|
-
* reached, so a problem inside one is found when its end is reached
|
|
43
|
-
*
|
|
49
|
+
* reached, so a problem inside one is found when its end is reached. A value
|
|
50
|
+
* longer than `maxValueLength` errors the stream as soon as that length is
|
|
51
|
+
* exceeded. If there is no array/object at `path`, the
|
|
44
52
|
* stream is errored once the whole document has been parsed. Because elements
|
|
45
53
|
* are emitted as soon as they are parsed, some elements may be emitted before
|
|
46
54
|
* an error is found later in the document.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { t as JsonTokenizer } from "./JsonTokenizer-
|
|
1
|
+
import { t as JsonTokenizer } from "./JsonTokenizer-BtRpnxKn.mjs";
|
|
2
2
|
// src/JsonDecoderStream.ts
|
|
3
3
|
/**
|
|
4
4
|
* Decode a JSON array or object from a stream of text, emitting each element
|
|
@@ -17,8 +17,9 @@ import { t as JsonTokenizer } from "./JsonTokenizer-BFV5P0dO.mjs";
|
|
|
17
17
|
* `onExtraData` callback. If the document is not valid JSON, the stream is
|
|
18
18
|
* errored when the problem is found. For speed, each element (and each
|
|
19
19
|
* object/array that is being discarded) is parsed in one go when its end is
|
|
20
|
-
* reached, so a problem inside one is found when its end is reached
|
|
21
|
-
*
|
|
20
|
+
* reached, so a problem inside one is found when its end is reached. A value
|
|
21
|
+
* longer than `maxValueLength` errors the stream as soon as that length is
|
|
22
|
+
* exceeded. If there is no array/object at `path`, the
|
|
22
23
|
* stream is errored once the whole document has been parsed. Because elements
|
|
23
24
|
* are emitted as soon as they are parsed, some elements may be emitted before
|
|
24
25
|
* an error is found later in the document.
|
|
@@ -29,7 +30,7 @@ import { t as JsonTokenizer } from "./JsonTokenizer-BFV5P0dO.mjs";
|
|
|
29
30
|
* `JSON.parse`, which keeps only the last value).
|
|
30
31
|
*/
|
|
31
32
|
var JsonDecoderStream = class extends TransformStream {
|
|
32
|
-
constructor({ path = [], maxValueLength = 100 *
|
|
33
|
+
constructor({ path = [], maxValueLength = 100 * 2 ** 20, onExtraData } = {}) {
|
|
33
34
|
const stack = [];
|
|
34
35
|
let foundTarget = false;
|
|
35
36
|
let extraData;
|
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
export interface JsonTokenDecoderStreamOptions {
|
|
2
|
+
/**
|
|
3
|
+
* The maximum length, in characters, of any single string, number or
|
|
4
|
+
* literal token. If a token is longer than this, the stream is errored, so
|
|
5
|
+
* that an unexpectedly large (or never-ending) token can't use unlimited
|
|
6
|
+
* memory. Defaults to 100 MiB (`100 * 2 ** 20` characters).
|
|
7
|
+
*/
|
|
2
8
|
readonly maxValueLength?: number;
|
|
3
9
|
}
|
|
4
10
|
/**
|
|
@@ -206,6 +206,7 @@ var JsonTokenizer = class {
|
|
|
206
206
|
this._state = isEscaped ? this._state | STATE_ESCAPED : this._state & -1025;
|
|
207
207
|
if (end === chunk.length) {
|
|
208
208
|
this._tokenText += chunk.slice(i);
|
|
209
|
+
if (this._tokenText.length > this._maxValueLength) this._valueExceedsMaxLength(this._tokenPosition);
|
|
209
210
|
i = end;
|
|
210
211
|
} else {
|
|
211
212
|
this._tokenText += chunk.slice(i, end + 1);
|
|
@@ -225,7 +226,7 @@ var JsonTokenizer = class {
|
|
|
225
226
|
if (end < chunk.length) {
|
|
226
227
|
if (isNumber) this._endNumber();
|
|
227
228
|
else this._endLiteral();
|
|
228
|
-
}
|
|
229
|
+
} else if (this._tokenText.length > this._maxValueLength) this._valueExceedsMaxLength(this._tokenPosition);
|
|
229
230
|
continue;
|
|
230
231
|
}
|
|
231
232
|
const code = chunk.charCodeAt(i);
|
package/lib/consumers.d.mts
CHANGED
|
@@ -9,8 +9,9 @@ export type ConsumableChunk = string | ArrayBuffer | ArrayBufferView;
|
|
|
9
9
|
*/
|
|
10
10
|
export type ConsumableStream = ReadableStream<ConsumableChunk> | AsyncIterable<ConsumableChunk>;
|
|
11
11
|
/**
|
|
12
|
-
* Read the entire stream, and decode its contents as UTF-8 text.
|
|
13
|
-
* byte order mark is removed
|
|
12
|
+
* Read the entire stream, and decode its contents as UTF-8 text. If the stream
|
|
13
|
+
* starts with bytes, a leading UTF-8 byte order mark is removed (string chunks
|
|
14
|
+
* are used as they are). Rejects if the stream errors.
|
|
14
15
|
*/
|
|
15
16
|
export declare function textFromStream(stream: ConsumableStream): Promise<string>;
|
|
16
17
|
/**
|
package/lib/consumers.mjs
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
// src/consumers.ts
|
|
2
2
|
const encoder = new TextEncoder();
|
|
3
3
|
/**
|
|
4
|
-
* Read the entire stream, and decode its contents as UTF-8 text.
|
|
5
|
-
* byte order mark is removed
|
|
4
|
+
* Read the entire stream, and decode its contents as UTF-8 text. If the stream
|
|
5
|
+
* starts with bytes, a leading UTF-8 byte order mark is removed (string chunks
|
|
6
|
+
* are used as they are). Rejects if the stream errors.
|
|
6
7
|
*/
|
|
7
8
|
async function textFromStream(stream) {
|
|
8
9
|
const chunks = stream[Symbol.asyncIterator]();
|
|
@@ -12,23 +13,32 @@ async function textFromStream(stream) {
|
|
|
12
13
|
if (typeof firstChunk.value === "string") return await textFromStreamOfStrings(chunks, firstChunk.value);
|
|
13
14
|
return await textFromStreamOfBytes(chunks, toUint8Array(firstChunk.value));
|
|
14
15
|
} catch (ex) {
|
|
15
|
-
|
|
16
|
+
try {
|
|
17
|
+
await chunks.return?.();
|
|
18
|
+
} catch {}
|
|
16
19
|
throw ex;
|
|
17
20
|
}
|
|
18
21
|
}
|
|
19
22
|
async function textFromStreamOfStrings(stream, firstChunk) {
|
|
20
23
|
let decoder;
|
|
24
|
+
let decoderHasBytes = false;
|
|
21
25
|
let result = firstChunk;
|
|
22
26
|
let next = await stream.next();
|
|
23
27
|
while (!next.done) {
|
|
24
|
-
if (typeof next.value === "string")
|
|
25
|
-
|
|
26
|
-
|
|
28
|
+
if (typeof next.value === "string") {
|
|
29
|
+
if (decoderHasBytes) {
|
|
30
|
+
result += decoder.decode();
|
|
31
|
+
decoderHasBytes = false;
|
|
32
|
+
}
|
|
33
|
+
result += next.value;
|
|
34
|
+
} else {
|
|
35
|
+
decoder ??= new TextDecoder("utf-8", { ignoreBOM: true });
|
|
27
36
|
result += decoder.decode(toUint8Array(next.value), { stream: true });
|
|
37
|
+
decoderHasBytes = true;
|
|
28
38
|
}
|
|
29
39
|
next = await stream.next();
|
|
30
40
|
}
|
|
31
|
-
if (!
|
|
41
|
+
if (!decoderHasBytes) return result;
|
|
32
42
|
return result + decoder.decode();
|
|
33
43
|
}
|
|
34
44
|
async function textFromStreamOfBytes(stream, firstChunk) {
|