1 | const { Readable } = require('stream');
|
---|
2 | const {
|
---|
3 | normalizeReplacer,
|
---|
4 | normalizeSpace,
|
---|
5 | replaceValue,
|
---|
6 | getTypeAsync,
|
---|
7 | type: {
|
---|
8 | PRIMITIVE,
|
---|
9 | OBJECT,
|
---|
10 | ARRAY,
|
---|
11 | PROMISE,
|
---|
12 | STRING_STREAM,
|
---|
13 | OBJECT_STREAM
|
---|
14 | }
|
---|
15 | } = require('./utils');
|
---|
16 | const noop = () => {};
|
---|
17 | const hasOwnProperty = Object.prototype.hasOwnProperty;
|
---|
18 |
|
---|
19 | // TODO: Remove when drop support for Node.js 10
|
---|
20 | // Node.js 10 has no well-formed JSON.stringify()
|
---|
21 | // https://github.com/tc39/proposal-well-formed-stringify
|
---|
22 | // Adopted code from https://bugs.chromium.org/p/v8/issues/detail?id=7782#c12
|
---|
23 | const wellformedStringStringify = JSON.stringify('\ud800') === '"\\ud800"'
|
---|
24 | ? JSON.stringify
|
---|
25 | : s => JSON.stringify(s).replace(
|
---|
26 | /\p{Surrogate}/gu,
|
---|
27 | m => `\\u${m.charCodeAt(0).toString(16)}`
|
---|
28 | );
|
---|
29 |
|
---|
30 | function push() {
|
---|
31 | this.push(this._stack.value);
|
---|
32 | this.popStack();
|
---|
33 | }
|
---|
34 |
|
---|
35 | function pushPrimitive(value) {
|
---|
36 | switch (typeof value) {
|
---|
37 | case 'string':
|
---|
38 | this.push(this.encodeString(value));
|
---|
39 | break;
|
---|
40 |
|
---|
41 | case 'number':
|
---|
42 | this.push(Number.isFinite(value) ? this.encodeNumber(value) : 'null');
|
---|
43 | break;
|
---|
44 |
|
---|
45 | case 'boolean':
|
---|
46 | this.push(value ? 'true' : 'false');
|
---|
47 | break;
|
---|
48 |
|
---|
49 | case 'undefined':
|
---|
50 | case 'object': // typeof null === 'object'
|
---|
51 | this.push('null');
|
---|
52 | break;
|
---|
53 |
|
---|
54 | default:
|
---|
55 | this.destroy(new TypeError(`Do not know how to serialize a ${value.constructor && value.constructor.name || typeof value}`));
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | function processObjectEntry(key) {
|
---|
60 | const current = this._stack;
|
---|
61 |
|
---|
62 | if (!current.first) {
|
---|
63 | current.first = true;
|
---|
64 | } else {
|
---|
65 | this.push(',');
|
---|
66 | }
|
---|
67 |
|
---|
68 | if (this.space) {
|
---|
69 | this.push(`\n${this.space.repeat(this._depth)}${this.encodeString(key)}: `);
|
---|
70 | } else {
|
---|
71 | this.push(this.encodeString(key) + ':');
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | function processObject() {
|
---|
76 | const current = this._stack;
|
---|
77 |
|
---|
78 | // when no keys left, remove obj from stack
|
---|
79 | if (current.index === current.keys.length) {
|
---|
80 | if (this.space && current.first) {
|
---|
81 | this.push(`\n${this.space.repeat(this._depth - 1)}}`);
|
---|
82 | } else {
|
---|
83 | this.push('}');
|
---|
84 | }
|
---|
85 |
|
---|
86 | this.popStack();
|
---|
87 | return;
|
---|
88 | }
|
---|
89 |
|
---|
90 | const key = current.keys[current.index];
|
---|
91 |
|
---|
92 | this.processValue(current.value, key, current.value[key], processObjectEntry);
|
---|
93 | current.index++;
|
---|
94 | }
|
---|
95 |
|
---|
96 | function processArrayItem(index) {
|
---|
97 | if (index !== 0) {
|
---|
98 | this.push(',');
|
---|
99 | }
|
---|
100 |
|
---|
101 | if (this.space) {
|
---|
102 | this.push(`\n${this.space.repeat(this._depth)}`);
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | function processArray() {
|
---|
107 | const current = this._stack;
|
---|
108 |
|
---|
109 | if (current.index === current.value.length) {
|
---|
110 | if (this.space && current.index > 0) {
|
---|
111 | this.push(`\n${this.space.repeat(this._depth - 1)}]`);
|
---|
112 | } else {
|
---|
113 | this.push(']');
|
---|
114 | }
|
---|
115 |
|
---|
116 | this.popStack();
|
---|
117 | return;
|
---|
118 | }
|
---|
119 |
|
---|
120 | this.processValue(current.value, current.index, current.value[current.index], processArrayItem);
|
---|
121 | current.index++;
|
---|
122 | }
|
---|
123 |
|
---|
124 | function createStreamReader(fn) {
|
---|
125 | return function() {
|
---|
126 | const current = this._stack;
|
---|
127 | const data = current.value.read(this._readSize);
|
---|
128 |
|
---|
129 | if (data !== null) {
|
---|
130 | current.first = false;
|
---|
131 | fn.call(this, data, current);
|
---|
132 | } else {
|
---|
133 | if (current.first && !current.value._readableState.reading) {
|
---|
134 | this.popStack();
|
---|
135 | } else {
|
---|
136 | current.first = true;
|
---|
137 | current.awaiting = true;
|
---|
138 | }
|
---|
139 | }
|
---|
140 | };
|
---|
141 | }
|
---|
142 |
|
---|
143 | const processReadableObject = createStreamReader(function(data, current) {
|
---|
144 | this.processValue(current.value, current.index, data, processArrayItem);
|
---|
145 | current.index++;
|
---|
146 | });
|
---|
147 |
|
---|
148 | const processReadableString = createStreamReader(function(data) {
|
---|
149 | this.push(data);
|
---|
150 | });
|
---|
151 |
|
---|
152 | class JsonStringifyStream extends Readable {
|
---|
153 | constructor(value, replacer, space) {
|
---|
154 | super({
|
---|
155 | autoDestroy: true
|
---|
156 | });
|
---|
157 |
|
---|
158 | this.getKeys = Object.keys;
|
---|
159 | this.replacer = normalizeReplacer(replacer);
|
---|
160 |
|
---|
161 | if (Array.isArray(this.replacer)) {
|
---|
162 | const allowlist = this.replacer;
|
---|
163 |
|
---|
164 | this.getKeys = (value) => allowlist.filter(key => hasOwnProperty.call(value, key));
|
---|
165 | this.replacer = null;
|
---|
166 | }
|
---|
167 |
|
---|
168 | this.space = normalizeSpace(space);
|
---|
169 | this._depth = 0;
|
---|
170 |
|
---|
171 | this.error = null;
|
---|
172 | this._processing = false;
|
---|
173 | this._ended = false;
|
---|
174 |
|
---|
175 | this._readSize = 0;
|
---|
176 | this._buffer = '';
|
---|
177 |
|
---|
178 | this._stack = null;
|
---|
179 | this._visited = new WeakSet();
|
---|
180 |
|
---|
181 | this.pushStack({
|
---|
182 | handler: () => {
|
---|
183 | this.popStack();
|
---|
184 | this.processValue({ '': value }, '', value, noop);
|
---|
185 | }
|
---|
186 | });
|
---|
187 | }
|
---|
188 |
|
---|
189 | encodeString(value) {
|
---|
190 | if (/[^\x20-\uD799]|[\x22\x5c]/.test(value)) {
|
---|
191 | return wellformedStringStringify(value);
|
---|
192 | }
|
---|
193 |
|
---|
194 | return '"' + value + '"';
|
---|
195 | }
|
---|
196 |
|
---|
197 | encodeNumber(value) {
|
---|
198 | return value;
|
---|
199 | }
|
---|
200 |
|
---|
201 | processValue(holder, key, value, callback) {
|
---|
202 | value = replaceValue(holder, key, value, this.replacer);
|
---|
203 |
|
---|
204 | let type = getTypeAsync(value);
|
---|
205 |
|
---|
206 | switch (type) {
|
---|
207 | case PRIMITIVE:
|
---|
208 | if (callback !== processObjectEntry || value !== undefined) {
|
---|
209 | callback.call(this, key);
|
---|
210 | pushPrimitive.call(this, value);
|
---|
211 | }
|
---|
212 | break;
|
---|
213 |
|
---|
214 | case OBJECT:
|
---|
215 | callback.call(this, key);
|
---|
216 |
|
---|
217 | // check for circular structure
|
---|
218 | if (this._visited.has(value)) {
|
---|
219 | return this.destroy(new TypeError('Converting circular structure to JSON'));
|
---|
220 | }
|
---|
221 |
|
---|
222 | this._visited.add(value);
|
---|
223 | this._depth++;
|
---|
224 | this.push('{');
|
---|
225 | this.pushStack({
|
---|
226 | handler: processObject,
|
---|
227 | value,
|
---|
228 | index: 0,
|
---|
229 | first: false,
|
---|
230 | keys: this.getKeys(value)
|
---|
231 | });
|
---|
232 | break;
|
---|
233 |
|
---|
234 | case ARRAY:
|
---|
235 | callback.call(this, key);
|
---|
236 |
|
---|
237 | // check for circular structure
|
---|
238 | if (this._visited.has(value)) {
|
---|
239 | return this.destroy(new TypeError('Converting circular structure to JSON'));
|
---|
240 | }
|
---|
241 |
|
---|
242 | this._visited.add(value);
|
---|
243 |
|
---|
244 | this.push('[');
|
---|
245 | this.pushStack({
|
---|
246 | handler: processArray,
|
---|
247 | value,
|
---|
248 | index: 0
|
---|
249 | });
|
---|
250 | this._depth++;
|
---|
251 | break;
|
---|
252 |
|
---|
253 | case PROMISE:
|
---|
254 | this.pushStack({
|
---|
255 | handler: noop,
|
---|
256 | awaiting: true
|
---|
257 | });
|
---|
258 |
|
---|
259 | Promise.resolve(value)
|
---|
260 | .then(resolved => {
|
---|
261 | this.popStack();
|
---|
262 | this.processValue(holder, key, resolved, callback);
|
---|
263 | this.processStack();
|
---|
264 | })
|
---|
265 | .catch(error => {
|
---|
266 | this.destroy(error);
|
---|
267 | });
|
---|
268 | break;
|
---|
269 |
|
---|
270 | case STRING_STREAM:
|
---|
271 | case OBJECT_STREAM:
|
---|
272 | callback.call(this, key);
|
---|
273 |
|
---|
274 | // TODO: Remove when drop support for Node.js 10
|
---|
275 | // Used `_readableState.endEmitted` as fallback, since Node.js 10 has no `readableEnded` getter
|
---|
276 | if (value.readableEnded || value._readableState.endEmitted) {
|
---|
277 | return this.destroy(new Error('Readable Stream has ended before it was serialized. All stream data have been lost'));
|
---|
278 | }
|
---|
279 |
|
---|
280 | if (value.readableFlowing) {
|
---|
281 | return this.destroy(new Error('Readable Stream is in flowing mode, data may have been lost. Trying to pause stream.'));
|
---|
282 | }
|
---|
283 |
|
---|
284 | if (type === OBJECT_STREAM) {
|
---|
285 | this.push('[');
|
---|
286 | this.pushStack({
|
---|
287 | handler: push,
|
---|
288 | value: this.space ? '\n' + this.space.repeat(this._depth) + ']' : ']'
|
---|
289 | });
|
---|
290 | this._depth++;
|
---|
291 | }
|
---|
292 |
|
---|
293 | const self = this.pushStack({
|
---|
294 | handler: type === OBJECT_STREAM ? processReadableObject : processReadableString,
|
---|
295 | value,
|
---|
296 | index: 0,
|
---|
297 | first: false,
|
---|
298 | awaiting: !value.readable || value.readableLength === 0
|
---|
299 | });
|
---|
300 | const continueProcessing = () => {
|
---|
301 | if (self.awaiting) {
|
---|
302 | self.awaiting = false;
|
---|
303 | this.processStack();
|
---|
304 | }
|
---|
305 | };
|
---|
306 |
|
---|
307 | value.once('error', error => this.destroy(error));
|
---|
308 | value.once('end', continueProcessing);
|
---|
309 | value.on('readable', continueProcessing);
|
---|
310 | break;
|
---|
311 | }
|
---|
312 | }
|
---|
313 |
|
---|
314 | pushStack(node) {
|
---|
315 | node.prev = this._stack;
|
---|
316 | return this._stack = node;
|
---|
317 | }
|
---|
318 |
|
---|
319 | popStack() {
|
---|
320 | const { handler, value } = this._stack;
|
---|
321 |
|
---|
322 | if (handler === processObject || handler === processArray || handler === processReadableObject) {
|
---|
323 | this._visited.delete(value);
|
---|
324 | this._depth--;
|
---|
325 | }
|
---|
326 |
|
---|
327 | this._stack = this._stack.prev;
|
---|
328 | }
|
---|
329 |
|
---|
330 | processStack() {
|
---|
331 | if (this._processing || this._ended) {
|
---|
332 | return;
|
---|
333 | }
|
---|
334 |
|
---|
335 | try {
|
---|
336 | this._processing = true;
|
---|
337 |
|
---|
338 | while (this._stack !== null && !this._stack.awaiting) {
|
---|
339 | this._stack.handler.call(this);
|
---|
340 |
|
---|
341 | if (!this._processing) {
|
---|
342 | return;
|
---|
343 | }
|
---|
344 | }
|
---|
345 |
|
---|
346 | this._processing = false;
|
---|
347 | } catch (error) {
|
---|
348 | this.destroy(error);
|
---|
349 | return;
|
---|
350 | }
|
---|
351 |
|
---|
352 | if (this._stack === null && !this._ended) {
|
---|
353 | this._finish();
|
---|
354 | this.push(null);
|
---|
355 | }
|
---|
356 | }
|
---|
357 |
|
---|
358 | push(data) {
|
---|
359 | if (data !== null) {
|
---|
360 | this._buffer += data;
|
---|
361 |
|
---|
362 | // check buffer overflow
|
---|
363 | if (this._buffer.length < this._readSize) {
|
---|
364 | return;
|
---|
365 | }
|
---|
366 |
|
---|
367 | // flush buffer
|
---|
368 | data = this._buffer;
|
---|
369 | this._buffer = '';
|
---|
370 | this._processing = false;
|
---|
371 | }
|
---|
372 |
|
---|
373 | super.push(data);
|
---|
374 | }
|
---|
375 |
|
---|
376 | _read(size) {
|
---|
377 | // start processing
|
---|
378 | this._readSize = size || this.readableHighWaterMark;
|
---|
379 | this.processStack();
|
---|
380 | }
|
---|
381 |
|
---|
382 | _finish() {
|
---|
383 | this._ended = true;
|
---|
384 | this._processing = false;
|
---|
385 | this._stack = null;
|
---|
386 | this._visited = null;
|
---|
387 |
|
---|
388 | if (this._buffer && this._buffer.length) {
|
---|
389 | super.push(this._buffer); // flush buffer
|
---|
390 | }
|
---|
391 |
|
---|
392 | this._buffer = '';
|
---|
393 | }
|
---|
394 |
|
---|
395 | _destroy(error, cb) {
|
---|
396 | this.error = this.error || error;
|
---|
397 | this._finish();
|
---|
398 | cb(error);
|
---|
399 | }
|
---|
400 | }
|
---|
401 |
|
---|
402 | module.exports = function createJsonStringifyStream(value, replacer, space) {
|
---|
403 | return new JsonStringifyStream(value, replacer, space);
|
---|
404 | };
|
---|