@depup/stream-json 1.9.1-depup.0

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/Assembler.js ADDED
@@ -0,0 +1,154 @@
1
+ 'use strict';
2
+
3
+ const EventEmitter = require('events');
4
+
5
+ const startObject = Ctr =>
6
+ function () {
7
+ if (this.done) {
8
+ this.done = false;
9
+ } else {
10
+ this.stack.push(this.current, this.key);
11
+ }
12
+ this.current = new Ctr();
13
+ this.key = null;
14
+ };
15
+
16
+ class Assembler extends EventEmitter {
17
+ static connectTo(stream, options) {
18
+ return new Assembler(options).connectTo(stream);
19
+ }
20
+
21
+ constructor(options) {
22
+ super();
23
+ this.stack = [];
24
+ this.current = this.key = null;
25
+ this.done = true;
26
+ if (options) {
27
+ this.reviver = typeof options.reviver == 'function' && options.reviver;
28
+ if (this.reviver) {
29
+ this.stringValue = this._saveValue = this._saveValueWithReviver;
30
+ }
31
+ if (options.numberAsString) {
32
+ this.numberValue = this.stringValue;
33
+ }
34
+ }
35
+ }
36
+
37
+ connectTo(stream) {
38
+ stream.on('data', chunk => {
39
+ if (this[chunk.name]) {
40
+ this[chunk.name](chunk.value);
41
+ if (this.done) this.emit('done', this);
42
+ }
43
+ });
44
+ return this;
45
+ }
46
+
47
+ get depth() {
48
+ return (this.stack.length >> 1) + (this.done ? 0 : 1);
49
+ }
50
+
51
+ get path() {
52
+ const path = [];
53
+ for (let i = 0; i < this.stack.length; i += 2) {
54
+ const key = this.stack[i + 1];
55
+ path.push(key === null ? this.stack[i].length : key);
56
+ }
57
+ return path;
58
+ }
59
+
60
+ dropToLevel(level) {
61
+ if (level < this.depth) {
62
+ if (level) {
63
+ const index = (level - 1) << 1;
64
+ this.current = this.stack[index];
65
+ this.key = this.stack[index + 1];
66
+ this.stack.splice(index);
67
+ } else {
68
+ this.stack = [];
69
+ this.current = this.key = null;
70
+ this.done = true;
71
+ }
72
+ }
73
+ return this;
74
+ }
75
+
76
+ consume(chunk) {
77
+ this[chunk.name] && this[chunk.name](chunk.value);
78
+ return this;
79
+ }
80
+
81
+ keyValue(value) {
82
+ this.key = value;
83
+ }
84
+
85
+ //stringValue() - aliased below to _saveValue()
86
+
87
+ numberValue(value) {
88
+ this._saveValue(parseFloat(value));
89
+ }
90
+ nullValue() {
91
+ this._saveValue(null);
92
+ }
93
+ trueValue() {
94
+ this._saveValue(true);
95
+ }
96
+ falseValue() {
97
+ this._saveValue(false);
98
+ }
99
+
100
+ //startObject() - assigned below
101
+
102
+ endObject() {
103
+ if (this.stack.length) {
104
+ const value = this.current;
105
+ this.key = this.stack.pop();
106
+ this.current = this.stack.pop();
107
+ this._saveValue(value);
108
+ } else {
109
+ this.done = true;
110
+ }
111
+ }
112
+
113
+ //startArray() - assigned below
114
+ //endArray() - aliased below to endObject()
115
+
116
+ _saveValue(value) {
117
+ if (this.done) {
118
+ this.current = value;
119
+ } else {
120
+ if (this.current instanceof Array) {
121
+ this.current.push(value);
122
+ } else {
123
+ this.current[this.key] = value;
124
+ this.key = null;
125
+ }
126
+ }
127
+ }
128
+ _saveValueWithReviver(value) {
129
+ if (this.done) {
130
+ this.current = this.reviver('', value);
131
+ } else {
132
+ if (this.current instanceof Array) {
133
+ value = this.reviver('' + this.current.length, value);
134
+ this.current.push(value);
135
+ if (value === undefined) {
136
+ delete this.current[this.current.length - 1];
137
+ }
138
+ } else {
139
+ value = this.reviver(this.key, value);
140
+ if (value !== undefined) {
141
+ this.current[this.key] = value;
142
+ }
143
+ this.key = null;
144
+ }
145
+ }
146
+ }
147
+ }
148
+
149
+ Assembler.prototype.stringValue = Assembler.prototype._saveValue;
150
+ Assembler.prototype.startObject = startObject(Object);
151
+ Assembler.prototype.startArray = startObject(Array);
152
+ Assembler.prototype.endArray = Assembler.prototype.endObject;
153
+
154
+ module.exports = Assembler;
@@ -0,0 +1,209 @@
1
+ 'use strict';
2
+
3
+ const {Duplex} = require('stream');
4
+
5
+ class Pump {
6
+ constructor(iterator, readable) {
7
+ this.iterator = iterator;
8
+ this.readable = readable;
9
+ this.done = false;
10
+ }
11
+ start() {
12
+ if (this.done) return Promise.resolve();
13
+
14
+ for (;;) {
15
+ const item = this.iterator.next();
16
+ if (item.done) return Promise.resolve();
17
+ if (!this.readable.push(item.value)) break;
18
+ }
19
+
20
+ return new Promise((resolve, reject) => {
21
+ this.resolve = resolve;
22
+ this.reject = reject;
23
+ });
24
+ }
25
+ resume() {
26
+ if (!this.resolve) return;
27
+
28
+ for (;;) {
29
+ const item = this.iterator.next();
30
+ if (item.done) {
31
+ this.done = true;
32
+ this.resolve();
33
+ return;
34
+ }
35
+ if (!this.readable.push(item.value)) break;
36
+ }
37
+ }
38
+ }
39
+
40
+ function* dump(value, options, processed) {
41
+ if (!processed) {
42
+ if (typeof value?.toJSON == 'function') {
43
+ value = value.toJSON('');
44
+ }
45
+ if (options.replacer) {
46
+ value = options.replacer('', value);
47
+ }
48
+ }
49
+
50
+ switch (typeof value) {
51
+ case 'function':
52
+ case 'symbol':
53
+ case 'undefined':
54
+ return;
55
+ case 'number':
56
+ if (isNaN(value) || !isFinite(value)) {
57
+ yield {name: 'nullValue', value: null};
58
+ }
59
+ value = String(value);
60
+ if (options.streamNumbers) {
61
+ yield {name: 'startNumber'};
62
+ yield {name: 'numberChunk', value};
63
+ yield {name: 'endNumber'};
64
+ }
65
+ if (options.packNumbers) {
66
+ yield {name: 'numberValue', value};
67
+ }
68
+ return;
69
+ case 'string':
70
+ if (options.streamStrings) {
71
+ yield {name: 'startString'};
72
+ yield {name: 'stringChunk', value};
73
+ yield {name: 'endString'};
74
+ }
75
+ if (options.packStrings) {
76
+ yield {name: 'stringValue', value};
77
+ }
78
+ return;
79
+ case 'boolean':
80
+ yield value ? {name: 'trueValue', value: true} : {name: 'falseValue', value: false};
81
+ return;
82
+ case 'object':
83
+ break;
84
+ default:
85
+ return; // skip anything else
86
+ }
87
+
88
+ // null
89
+ if (value === null) {
90
+ yield {name: 'nullValue', value: null};
91
+ return;
92
+ }
93
+
94
+ // Array
95
+ if (Array.isArray(value)) {
96
+ yield {name: 'startArray'};
97
+ for (let i = 0; i < value.length; ++i) {
98
+ let v = value[i];
99
+ if (typeof v?.toJSON == 'function') {
100
+ v = v.toJSON(String(i));
101
+ }
102
+ if (options.replacer) {
103
+ v = options.replacer(String(i), v);
104
+ }
105
+ switch (typeof v) {
106
+ case 'function':
107
+ case 'symbol':
108
+ case 'undefined':
109
+ v = null; // force null
110
+ break;
111
+ }
112
+ yield* dump(v, options, true);
113
+ }
114
+ yield {name: 'endArray'};
115
+ return;
116
+ }
117
+
118
+ // Object
119
+ yield {name: 'startObject'};
120
+ for (let [k, v] of Object.entries(value)) {
121
+ if (options.dict && options.dict[k] !== 1) continue;
122
+ if (typeof v?.toJSON == 'function') {
123
+ v = v.toJSON(k);
124
+ }
125
+ if (options.replacer) {
126
+ v = options.replacer(k, v);
127
+ }
128
+ switch (typeof v) {
129
+ case 'function':
130
+ case 'symbol':
131
+ case 'undefined':
132
+ continue;
133
+ }
134
+ if (options.streamKeys) {
135
+ yield {name: 'startKey'};
136
+ yield {name: 'stringChunk', value: k};
137
+ yield {name: 'endKey'};
138
+ }
139
+ if (options.packKeys) {
140
+ yield {name: 'keyValue', value: k};
141
+ }
142
+ yield* dump(v, options, true);
143
+ }
144
+ yield {name: 'endObject'};
145
+ }
146
+
147
+ const kOptions = Symbol('options'),
148
+ kPump = Symbol('pump');
149
+
150
+ class Disassembler extends Duplex {
151
+ static make(options) {
152
+ return new Disassembler(options);
153
+ }
154
+
155
+ constructor(options) {
156
+ super(Object.assign({}, options, {writableObjectMode: true, readableObjectMode: true}));
157
+
158
+ const opt = {packKeys: true, packStrings: true, packNumbers: true, streamKeys: true, streamStrings: true, streamNumbers: true};
159
+
160
+ if (options) {
161
+ 'packValues' in options && (opt.packKeys = opt.packStrings = opt.packNumbers = options.packValues);
162
+ 'packKeys' in options && (opt.packKeys = options.packKeys);
163
+ 'packStrings' in options && (opt.packStrings = options.packStrings);
164
+ 'packNumbers' in options && (opt.packNumbers = options.packNumbers);
165
+
166
+ 'streamValues' in options && (opt.streamKeys = opt.streamStrings = opt.streamNumbers = options.streamValues);
167
+ 'streamKeys' in options && (opt.streamKeys = options.streamKeys);
168
+ 'streamStrings' in options && (opt.streamStrings = options.streamStrings);
169
+ 'streamNumbers' in options && (opt.streamNumbers = options.streamNumbers);
170
+
171
+ if (typeof options.replacer == 'function') {
172
+ opt.replacer = options.replacer;
173
+ } else if (Array.isArray(options.replacer)) {
174
+ opt.dict = options.replacer.reduce((acc, k) => ((acc[k] = 1), acc), {});
175
+ }
176
+ }
177
+
178
+ !opt.packKeys && (opt.streamKeys = true);
179
+ !opt.packStrings && (opt.streamStrings = true);
180
+ !opt.packNumbers && (opt.streamNumbers = true);
181
+
182
+ this[kOptions] = opt;
183
+ }
184
+
185
+ _read() {
186
+ this[kPump]?.resume();
187
+ }
188
+
189
+ _write(chunk, _, callback) {
190
+ const iterator = dump(chunk, this[kOptions]),
191
+ pump = (this[kPump] = new Pump(iterator, this));
192
+
193
+ pump.start().then(
194
+ () => ((this[kPump] = null), callback()),
195
+ error => ((this[kPump] = null), callback(error))
196
+ );
197
+ }
198
+
199
+ _final(callback) {
200
+ this.push(null);
201
+ callback();
202
+ }
203
+ }
204
+ Disassembler.disassembler = Disassembler.make;
205
+ Disassembler.make.Constructor = Disassembler;
206
+
207
+ module.exports = Disassembler;
208
+
209
+ module.exports.dump = dump;
package/Emitter.js ADDED
@@ -0,0 +1,22 @@
1
+ 'use strict';
2
+
3
+ const {Writable} = require('stream');
4
+
5
+ class Emitter extends Writable {
6
+ static make(options) {
7
+ return new Emitter(options);
8
+ }
9
+
10
+ constructor(options) {
11
+ super(Object.assign({}, options, {objectMode: true}));
12
+ }
13
+
14
+ _write(chunk, encoding, callback) {
15
+ this.emit(chunk.name, chunk.value);
16
+ callback(null);
17
+ }
18
+ }
19
+ Emitter.emitter = Emitter.make;
20
+ Emitter.make.Constructor = Emitter;
21
+
22
+ module.exports = Emitter;
package/LICENSE ADDED
@@ -0,0 +1,34 @@
1
+ This library is available under the terms of the modified BSD license. No external contributions
2
+ are allowed under licenses which are fundamentally incompatible with the BSD license that this library is distributed under.
3
+
4
+ The text of the BSD license is reproduced below.
5
+
6
+ -------------------------------------------------------------------------------
7
+ The "New" BSD License:
8
+ **********************
9
+
10
+ Copyright (c) 2005-2024, Eugene Lazutkin
11
+ All rights reserved.
12
+
13
+ Redistribution and use in source and binary forms, with or without
14
+ modification, are permitted provided that the following conditions are met:
15
+
16
+ * Redistributions of source code must retain the above copyright notice, this
17
+ list of conditions and the following disclaimer.
18
+ * Redistributions in binary form must reproduce the above copyright notice,
19
+ this list of conditions and the following disclaimer in the documentation
20
+ and/or other materials provided with the distribution.
21
+ * Neither the name of Eugene Lazutkin nor the names of other contributors
22
+ may be used to endorse or promote products derived from this software
23
+ without specific prior written permission.
24
+
25
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
26
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
29
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.