-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAsObjects.js
118 lines (106 loc) · 3.35 KB
/
AsObjects.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
'use strict';
const {Transform} = require('stream');
const withParser = require('stream-json/utils/withParser');
class AsObjects extends Transform {
static make(options) {
return new AsObjects(options);
}
static withParser(options) {
return withParser(AsObjects.make, options);
}
constructor(options) {
super(Object.assign({}, options, {writableObjectMode: true, readableObjectMode: true}));
this._fieldPrefix = 'field';
this._useStringValues = false;
this._packKeys = this._streamKeys = true;
if (options) {
'packValues' in options && (this._packStrings = options.packValues);
'packKeys' in options && (this._packKeys = options.packKeys);
'streamValues' in options && (this._streamStrings = options.streamValues);
'streamKeys' in options && (this._streamKeys = options.streamKeys);
'useValues' in options && (this._useStringValues = options.useValues);
'useStringValues' in options && (this._useStringValues = options.useStringValues);
'fieldPrefix' in options && (this._fieldPrefix = options.fieldPrefix);
}
!this._packKeys && (this._streamKeys = true);
this._useStringValues && (this._transform = this._valueTransform);
this._keys = [];
this._buffer = '';
this._index = 0;
}
_transform(chunk, _, callback) {
switch (chunk.name) {
case 'endArray':
this._transform = this._transformToObject;
break;
case 'stringChunk':
this._buffer += chunk.value;
break;
case 'endString':
this._keys.push(this._buffer);
this._buffer = '';
break;
}
callback(null);
}
_valueTransform(chunk, _, callback) {
switch (chunk.name) {
case 'endArray':
this._transform = this._transformToObject;
break;
case 'stringValue':
this._keys.push(chunk.value);
break;
}
callback(null);
}
_transformToObject(chunk, encoding, callback) {
switch (chunk.name) {
case 'startArray':
this.push({name: 'startObject'});
break;
case 'endArray':
this.push({name: 'endObject'});
this._index = 0;
break;
case 'startString':
case 'stringValue':
const key = (this._index < this._keys.length && this._keys[this._index]) || this._fieldPrefix + this._index;
++this._index;
if (this._streamKeys) {
this.push({name: 'startKey'});
this.push({name: 'stringChunk', value: key});
this.push({name: 'endKey'});
}
this._packKeys && this.push({name: 'keyValue', value: key});
if (chunk.name === 'startString') {
this._transform = this._passString;
return this._transform(chunk, encoding, callback);
}
this.push(chunk);
break;
}
callback(null);
}
_passString(chunk, _, callback) {
if (this._expected) {
const expected = this._expected;
this._expected = '';
this._transform = this._transformToObject;
if (expected === chunk.name) {
this.push(chunk);
} else {
return this._transform(chunk, _, callback);
}
} else {
this.push(chunk);
if (chunk.name === 'endString') {
this._expected = 'stringValue';
}
}
callback(null);
}
}
AsObjects.asObjects = AsObjects.make;
AsObjects.make.Constructor = AsObjects;
module.exports = AsObjects;