UNPKG

5.52 kBJavaScriptView Raw
1var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2 if (kind === "m") throw new TypeError("Private method is not writable");
3 if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4 if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5 return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6};
7var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8 if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9 if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10 return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11};
12var _JSONParseStream_jsonPath, _JSONParseNexus_queues, _JSONParseNexus_reader;
13// deno-lint-ignore-file no-explicit-any no-cond-assign ban-unused-ignore no-unused-vars
14import { streamToAsyncIter } from 'whatwg-stream-to-async-iter';
15import { JSONParser } from './json-parser.js';
16import { TaskPromise } from './task-promise.js';
17import { normalize, match } from './json-path.js';
18// FIXME: avoid string concatenation/joining
19const mkPath = (parser) => {
20 const path = [...parser.stack.map((_) => _.key), parser.key]; // TODO: modify parser to provide key efficiently
21 path[0] = path[0] || '$';
22 return normalize(path.join('.')); // FIXME: avoid string concatenation/joining
23};
24export class JSONParseStream extends TransformStream {
25 constructor(jsonPath = '$.*') {
26 let parser;
27 const expr = normalize(jsonPath);
28 super({
29 start: (controller) => {
30 parser = new JSONParser();
31 parser.onValue = (value) => {
32 const path = mkPath(parser);
33 if (match(expr, path)) {
34 controller.enqueue(value);
35 }
36 else if (expr.startsWith(path + ';')) {
37 controller.terminate();
38 }
39 };
40 },
41 transform: (chunk) => {
42 parser.write(chunk);
43 },
44 });
45 _JSONParseStream_jsonPath.set(this, void 0);
46 __classPrivateFieldSet(this, _JSONParseStream_jsonPath, expr, "f");
47 }
48 get path() { return __classPrivateFieldGet(this, _JSONParseStream_jsonPath, "f"); }
49}
50_JSONParseStream_jsonPath = new WeakMap();
51const remove = (m, k) => { const v = m.get(k); m.delete(k); return v; };
52/** @deprecated Rename!!! */
53export class JSONParseNexus extends TransformStream {
54 constructor() {
55 let parser;
56 super({
57 start: (controller) => {
58 parser = new JSONParser();
59 parser.onValue = (value) => {
60 const path = mkPath(parser);
61 for (const expr of __classPrivateFieldGet(this, _JSONParseNexus_queues, "f").keys()) {
62 if (match(expr, path)) {
63 __classPrivateFieldGet(this, _JSONParseNexus_queues, "f").get(expr).enqueue(value);
64 } // no else if => can both be true
65 if (expr.startsWith(path + ';')) {
66 remove(__classPrivateFieldGet(this, _JSONParseNexus_queues, "f"), expr).close();
67 }
68 }
69 controller.enqueue([path, value]);
70 };
71 },
72 transform(buffer) {
73 // console.log('transform', buffer, controller.desiredSize)
74 parser.write(buffer);
75 },
76 });
77 _JSONParseNexus_queues.set(this, new Map());
78 _JSONParseNexus_reader.set(this, void 0);
79 __classPrivateFieldSet(this, _JSONParseNexus_reader, this.readable.getReader(), "f");
80 }
81 promise(jsonPath) {
82 const reader = this.stream(jsonPath).getReader();
83 return TaskPromise.from(async () => {
84 const x = await reader.read();
85 return x.done ? undefined : x.value;
86 });
87 }
88 stream(jsonPath) {
89 const path = normalize(jsonPath);
90 return new ReadableStream({
91 start: (queue) => {
92 __classPrivateFieldGet(this, _JSONParseNexus_queues, "f").set(path, queue);
93 },
94 pull: async () => {
95 while (true) {
96 const { done, value } = await __classPrivateFieldGet(this, _JSONParseNexus_reader, "f").read();
97 // FIXME: avoid duplicate match
98 if (done || match(value[0], path))
99 break;
100 }
101 },
102 cancel: (err) => {
103 // If one of the child streams errors, error the whole pipeline.
104 // TODO: Or should it?
105 __classPrivateFieldGet(this, _JSONParseNexus_reader, "f").cancel(err);
106 },
107 }, { highWaterMark: 0 }); // does not pull on its own
108 }
109 iterable(jsonPath) {
110 return streamToAsyncIter(this.stream(jsonPath));
111 }
112}
113_JSONParseNexus_queues = new WeakMap(), _JSONParseNexus_reader = new WeakMap();
114//# sourceMappingURL=json-parse-stream.js.map
\No newline at end of file