@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.
@@ -0,0 +1,131 @@
1
+ 'use strict';
2
+
3
+ const Utf8Stream = require('../utils/Utf8Stream');
4
+
5
+ class JsonlParser extends Utf8Stream {
6
+ static make(options) {
7
+ return new JsonlParser(options);
8
+ }
9
+
10
+ static checkedParse(input, reviver, errorIndicator) {
11
+ try {
12
+ return JSON.parse(input, reviver);
13
+ } catch (error) {
14
+ if (typeof errorIndicator == 'function') return errorIndicator(error, input, reviver);
15
+ }
16
+ return errorIndicator;
17
+ }
18
+
19
+ constructor(options) {
20
+ super(Object.assign({}, options, {readableObjectMode: true}));
21
+ this._rest = '';
22
+ this._counter = 0;
23
+ this._reviver = options && options.reviver;
24
+ this._errorIndicator = options && options.errorIndicator;
25
+ if (options && options.checkErrors) {
26
+ this._processBuffer = this._checked_processBuffer;
27
+ this._flush = this._checked_flush;
28
+ }
29
+ if (options && 'errorIndicator' in options) {
30
+ this._processBuffer = this._suppressed_processBuffer;
31
+ this._flush = this._suppressed_flush;
32
+ }
33
+ }
34
+
35
+ _processBuffer(callback) {
36
+ const lines = this._buffer.split('\n');
37
+ this._rest += lines[0];
38
+ if (lines.length > 1) {
39
+ this._rest && this.push({key: this._counter++, value: JSON.parse(this._rest, this._reviver)});
40
+ this._rest = lines.pop();
41
+ for (let i = 1; i < lines.length; ++i) {
42
+ lines[i] && this.push({key: this._counter++, value: JSON.parse(lines[i], this._reviver)});
43
+ }
44
+ }
45
+ this._buffer = '';
46
+ callback(null);
47
+ }
48
+
49
+ _flush(callback) {
50
+ super._flush(error => {
51
+ if (error) return callback(error);
52
+ if (this._rest) {
53
+ this.push({key: this._counter++, value: JSON.parse(this._rest, this._reviver)});
54
+ this._rest = '';
55
+ }
56
+ callback(null);
57
+ });
58
+ }
59
+
60
+ _suppressed_processBuffer(callback) {
61
+ const lines = this._buffer.split('\n');
62
+ this._rest += lines[0];
63
+ if (lines.length > 1) {
64
+ if (this._rest) {
65
+ const value = JsonlParser.checkedParse(this._rest, this._reviver, this._errorIndicator);
66
+ value !== undefined && this.push({key: this._counter++, value});
67
+ }
68
+ this._rest = lines.pop();
69
+ for (let i = 1; i < lines.length; ++i) {
70
+ if (!lines[i]) continue;
71
+ const value = JsonlParser.checkedParse(lines[i], this._reviver, this._errorIndicator);
72
+ value !== undefined && this.push({key: this._counter++, value});
73
+ }
74
+ }
75
+ this._buffer = '';
76
+ callback(null);
77
+ }
78
+
79
+ _suppressed_flush(callback) {
80
+ super._flush(error => {
81
+ if (error) return callback(error);
82
+ if (this._rest) {
83
+ const value = JsonlParser.checkedParse(this._rest, this._reviver, this._errorIndicator);
84
+ value !== undefined && this.push({key: this._counter++, value});
85
+ this._rest = '';
86
+ }
87
+ callback(null);
88
+ });
89
+ }
90
+
91
+ _checked_processBuffer(callback) {
92
+ const lines = this._buffer.split('\n');
93
+ this._rest += lines[0];
94
+ if (lines.length > 1) {
95
+ try {
96
+ this._rest && this.push({key: this._counter++, value: JSON.parse(this._rest, this._reviver)});
97
+ this._rest = lines.pop();
98
+ for (let i = 1; i < lines.length; ++i) {
99
+ lines[i] && this.push({key: this._counter++, value: JSON.parse(lines[i], this._reviver)});
100
+ }
101
+ } catch (cbErr) {
102
+ this._buffer = '';
103
+ callback(cbErr);
104
+ return;
105
+ }
106
+ }
107
+ this._buffer = '';
108
+ callback(null);
109
+ }
110
+
111
+ _checked_flush(callback) {
112
+ super._flush(error => {
113
+ if (error) return callback(error);
114
+ if (this._rest) {
115
+ try {
116
+ this.push({key: this._counter++, value: JSON.parse(this._rest, this._reviver)});
117
+ } catch (cbErr) {
118
+ this._rest = '';
119
+ callback(cbErr);
120
+ return;
121
+ }
122
+ this._rest = '';
123
+ }
124
+ callback(null);
125
+ });
126
+ }
127
+ }
128
+ JsonlParser.parser = JsonlParser.make;
129
+ JsonlParser.make.Constructor = JsonlParser;
130
+
131
+ module.exports = JsonlParser;
@@ -0,0 +1,29 @@
1
+ 'use strict';
2
+
3
+ const {Transform} = require('stream');
4
+
5
+ class JsonlStringer extends Transform {
6
+ static make(options) {
7
+ return new JsonlStringer(options);
8
+ }
9
+
10
+ constructor(options) {
11
+ super(Object.assign({}, options, {writableObjectMode: true, readableObjectMode: false}));
12
+ this._replacer = options && options.replacer;
13
+ }
14
+
15
+ _transform(chunk, _, callback) {
16
+ this.push(JSON.stringify(chunk, this._replacer));
17
+ this._transform = this._nextTransform;
18
+ callback(null);
19
+ }
20
+
21
+ _nextTransform(chunk, _, callback) {
22
+ this.push('\n' + JSON.stringify(chunk, this._replacer));
23
+ callback(null);
24
+ }
25
+ }
26
+ JsonlStringer.stringer = JsonlStringer.make;
27
+ JsonlStringer.make.Constructor = JsonlStringer;
28
+
29
+ module.exports = JsonlStringer;
package/package.json ADDED
@@ -0,0 +1,64 @@
1
+ {
2
+ "name": "@depup/stream-json",
3
+ "version": "1.9.1-depup.0",
4
+ "description": "[DepUp] stream-json is the micro-library of Node.js stream components for creating custom JSON processing pipelines with a minimal memory footprint. It can parse JSON files far exceeding available memory streaming individual primitives using a SAX-inspired API. Includes utilities to stream JSON database dumps.",
5
+ "homepage": "http://github.com/uhop/stream-json",
6
+ "bugs": "http://github.com/uhop/stream-json/issues",
7
+ "main": "index.js",
8
+ "directories": {
9
+ "test": "tests"
10
+ },
11
+ "dependencies": {
12
+ "stream-chain": "^3.4.1"
13
+ },
14
+ "devDependencies": {
15
+ "heya-unit": "^0.3.0"
16
+ },
17
+ "scripts": {
18
+ "test": "node tests/tests.js",
19
+ "debug": "node --inspect-brk tests/tests.js"
20
+ },
21
+ "github": "http://github.com/uhop/stream-json",
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "git://github.com/uhop/stream-json.git"
25
+ },
26
+ "keywords": [
27
+ "depup",
28
+ "dependency-bumped",
29
+ "updated-deps",
30
+ "stream-json",
31
+ "scanner",
32
+ "lexer",
33
+ "tokenizer",
34
+ "parser",
35
+ "django",
36
+ "stream",
37
+ "streaming",
38
+ "json"
39
+ ],
40
+ "author": "Eugene Lazutkin <eugene.lazutkin@gmail.com> (http://lazutkin.com/)",
41
+ "license": "BSD-3-Clause",
42
+ "files": [
43
+ "/*.js",
44
+ "/filters",
45
+ "/jsonl",
46
+ "/streamers",
47
+ "/utils",
48
+ "changes.json",
49
+ "README.md"
50
+ ],
51
+ "depup": {
52
+ "changes": {
53
+ "stream-chain": {
54
+ "from": "^2.2.5",
55
+ "to": "^3.4.1"
56
+ }
57
+ },
58
+ "depsUpdated": 1,
59
+ "originalPackage": "stream-json",
60
+ "originalVersion": "1.9.1",
61
+ "processedAt": "2026-03-17T16:39:26.281Z",
62
+ "smokeTest": "passed"
63
+ }
64
+ }
@@ -0,0 +1,44 @@
1
+ 'use strict';
2
+
3
+ const StreamBase = require('./StreamBase');
4
+ const withParser = require('../utils/withParser');
5
+
6
+ class StreamArray extends StreamBase {
7
+ static make(options) {
8
+ return new StreamArray(options);
9
+ }
10
+
11
+ static withParser(options) {
12
+ return withParser(StreamArray.make, options);
13
+ }
14
+
15
+ constructor(options) {
16
+ super(options);
17
+ this._level = 1;
18
+ this._counter = 0;
19
+ }
20
+
21
+ _wait(chunk, _, callback) {
22
+ // first chunk should open an array
23
+ if (chunk.name !== 'startArray') {
24
+ return callback(new Error('Top-level object should be an array.'));
25
+ }
26
+ this._transform = this._filter;
27
+ return this._transform(chunk, _, callback);
28
+ }
29
+
30
+ _push(discard) {
31
+ if (this._assembler.current.length) {
32
+ if (discard) {
33
+ ++this._counter;
34
+ this._assembler.current.pop();
35
+ } else {
36
+ this.push({key: this._counter++, value: this._assembler.current.pop()});
37
+ }
38
+ }
39
+ }
40
+ }
41
+ StreamArray.streamArray = StreamArray.make;
42
+ StreamArray.make.Constructor = StreamArray;
43
+
44
+ module.exports = StreamArray;
@@ -0,0 +1,101 @@
1
+ 'use strict';
2
+
3
+ const {Transform} = require('stream');
4
+ const Assembler = require('../Assembler');
5
+
6
+ class Counter {
7
+ constructor(initialDepth) {
8
+ this.depth = initialDepth;
9
+ }
10
+ startObject() {
11
+ ++this.depth;
12
+ }
13
+ endObject() {
14
+ --this.depth;
15
+ }
16
+ startArray() {
17
+ ++this.depth;
18
+ }
19
+ endArray() {
20
+ --this.depth;
21
+ }
22
+ }
23
+
24
+ class StreamBase extends Transform {
25
+ constructor(options) {
26
+ super(Object.assign({}, options, {writableObjectMode: true, readableObjectMode: true}));
27
+ if (options) {
28
+ this.objectFilter = options.objectFilter;
29
+ this.includeUndecided = options.includeUndecided;
30
+ }
31
+ if (typeof this.objectFilter != 'function') {
32
+ this._filter = this._transform;
33
+ }
34
+ this._transform = this._wait || this._filter;
35
+ this._assembler = new Assembler(options);
36
+ }
37
+
38
+ _transform(chunk, encoding, callback) {
39
+ if (this._assembler[chunk.name]) {
40
+ this._assembler[chunk.name](chunk.value);
41
+ if (this._assembler.depth === this._level) {
42
+ this._push();
43
+ }
44
+ }
45
+ callback(null);
46
+ }
47
+
48
+ _filter(chunk, encoding, callback) {
49
+ if (this._assembler[chunk.name]) {
50
+ this._assembler[chunk.name](chunk.value);
51
+ const result = this.objectFilter(this._assembler);
52
+ if (result) {
53
+ if (this._assembler.depth === this._level) {
54
+ this._push();
55
+ this._transform = this._filter;
56
+ }
57
+ this._transform = this._accept;
58
+ return callback(null);
59
+ }
60
+ if (result === false) {
61
+ this._saved_assembler = this._assembler;
62
+ this._assembler = new Counter(this._saved_assembler.depth);
63
+ this._saved_assembler.dropToLevel(this._level);
64
+ if (this._assembler.depth === this._level) {
65
+ this._assembler = this._saved_assembler;
66
+ this._transform = this._filter;
67
+ }
68
+ this._transform = this._reject;
69
+ return callback(null);
70
+ }
71
+ if (this._assembler.depth === this._level) {
72
+ this._push(!this.includeUndecided);
73
+ }
74
+ }
75
+ callback(null);
76
+ }
77
+
78
+ _accept(chunk, encoding, callback) {
79
+ if (this._assembler[chunk.name]) {
80
+ this._assembler[chunk.name](chunk.value);
81
+ if (this._assembler.depth === this._level) {
82
+ this._push();
83
+ this._transform = this._filter;
84
+ }
85
+ }
86
+ callback(null);
87
+ }
88
+
89
+ _reject(chunk, encoding, callback) {
90
+ if (this._assembler[chunk.name]) {
91
+ this._assembler[chunk.name](chunk.value);
92
+ if (this._assembler.depth === this._level) {
93
+ this._assembler = this._saved_assembler;
94
+ this._transform = this._filter;
95
+ }
96
+ }
97
+ callback(null);
98
+ }
99
+ }
100
+
101
+ module.exports = StreamBase;
@@ -0,0 +1,43 @@
1
+ 'use strict';
2
+
3
+ const StreamBase = require('./StreamBase');
4
+ const withParser = require('../utils/withParser');
5
+
6
+ class StreamObject extends StreamBase {
7
+ static make(options) {
8
+ return new StreamObject(options);
9
+ }
10
+
11
+ static withParser(options) {
12
+ return withParser(StreamObject.make, options);
13
+ }
14
+
15
+ constructor(options) {
16
+ super(options);
17
+ this._level = 1;
18
+ this._lastKey = null;
19
+ }
20
+
21
+ _wait(chunk, _, callback) {
22
+ // first chunk should open an array
23
+ if (chunk.name !== 'startObject') {
24
+ return callback(new Error('Top-level object should be an object.'));
25
+ }
26
+ this._transform = this._filter;
27
+ return this._transform(chunk, _, callback);
28
+ }
29
+
30
+ _push(discard) {
31
+ if (this._lastKey === null) {
32
+ this._lastKey = this._assembler.key;
33
+ } else {
34
+ !discard && this.push({key: this._lastKey, value: this._assembler.current[this._lastKey]});
35
+ this._assembler.current = {};
36
+ this._lastKey = null;
37
+ }
38
+ }
39
+ }
40
+ StreamObject.streamObject = StreamObject.make;
41
+ StreamObject.make.Constructor = StreamObject;
42
+
43
+ module.exports = StreamObject;
@@ -0,0 +1,33 @@
1
+ 'use strict';
2
+
3
+ const StreamBase = require('./StreamBase');
4
+ const withParser = require('../utils/withParser');
5
+
6
+ class StreamValues extends StreamBase {
7
+ static make(options) {
8
+ return new StreamValues(options);
9
+ }
10
+
11
+ static withParser(options) {
12
+ return withParser(StreamValues.make, Object.assign({}, options, {jsonStreaming: true}));
13
+ }
14
+
15
+ constructor(options) {
16
+ super(options);
17
+ this._counter = 0;
18
+ this._level = 0;
19
+ }
20
+
21
+ _push(discard) {
22
+ if (discard) {
23
+ ++this._counter;
24
+ } else {
25
+ this.push({key: this._counter++, value: this._assembler.current});
26
+ }
27
+ this._assembler.current = this._assembler.key = null;
28
+ }
29
+ }
30
+ StreamValues.streamValues = StreamValues.make;
31
+ StreamValues.make.Constructor = StreamValues;
32
+
33
+ module.exports = StreamValues;
package/utils/Batch.js ADDED
@@ -0,0 +1,44 @@
1
+ 'use strict';
2
+
3
+ const {Transform} = require('stream');
4
+ const withParser = require('./withParser');
5
+
6
+ class Batch extends Transform {
7
+ static make(options) {
8
+ return new Batch(options);
9
+ }
10
+
11
+ static withParser(options) {
12
+ return withParser(Batch.make, options);
13
+ }
14
+
15
+ constructor(options) {
16
+ super(Object.assign({}, options, {writableObjectMode: true, readableObjectMode: true}));
17
+ this._batchSize = 1000;
18
+ if (options && typeof options.batchSize == 'number' && options.batchSize > 0) {
19
+ this._batchSize = options.batchSize;
20
+ }
21
+ this._accumulator = [];
22
+ }
23
+
24
+ _transform(chunk, _, callback) {
25
+ this._accumulator.push(chunk);
26
+ if (this._accumulator.length >= this._batchSize) {
27
+ this.push(this._accumulator);
28
+ this._accumulator = [];
29
+ }
30
+ callback(null);
31
+ }
32
+
33
+ _flush(callback) {
34
+ if (this._accumulator.length) {
35
+ this.push(this._accumulator);
36
+ this._accumulator = null;
37
+ }
38
+ callback(null);
39
+ }
40
+ }
41
+ Batch.batch = Batch.make;
42
+ Batch.make.Constructor = Batch;
43
+
44
+ module.exports = Batch;
@@ -0,0 +1,53 @@
1
+ 'use strict';
2
+
3
+ const {Transform} = require('stream');
4
+ const {StringDecoder} = require('string_decoder');
5
+
6
+ class Utf8Stream extends Transform {
7
+ constructor(options) {
8
+ super(Object.assign({}, options, {writableObjectMode: false}));
9
+ this._buffer = '';
10
+ }
11
+
12
+ _transform(chunk, encoding, callback) {
13
+ if (typeof chunk == 'string') {
14
+ this._transform = this._transformString;
15
+ } else {
16
+ this._stringDecoder = new StringDecoder();
17
+ this._transform = this._transformBuffer;
18
+ }
19
+ this._transform(chunk, encoding, callback);
20
+ }
21
+
22
+ _transformBuffer(chunk, _, callback) {
23
+ this._buffer += this._stringDecoder.write(chunk);
24
+ this._processBuffer(callback);
25
+ }
26
+
27
+ _transformString(chunk, _, callback) {
28
+ this._buffer += chunk.toString();
29
+ this._processBuffer(callback);
30
+ }
31
+
32
+ _processBuffer(callback) {
33
+ if (this._buffer) {
34
+ this.push(this._buffer, 'utf8');
35
+ this._buffer = '';
36
+ }
37
+ callback(null);
38
+ }
39
+
40
+ _flushInput() {
41
+ // meant to be called from _flush()
42
+ if (this._stringDecoder) {
43
+ this._buffer += this._stringDecoder.end();
44
+ }
45
+ }
46
+
47
+ _flush(callback) {
48
+ this._flushInput();
49
+ this._processBuffer(callback);
50
+ }
51
+ }
52
+
53
+ module.exports = Utf8Stream;