UNPKG

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