forked from uhop/stream-json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Disassembler.js
170 lines (164 loc) · 5.97 KB
/
Disassembler.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
'use strict';
const {Transform} = require('stream');
class Emit {
constructor(tokenName) {
this.tokenName = tokenName;
}
}
class Disassembler extends Transform {
static make(options) {
return new Disassembler(options);
}
constructor(options) {
super(Object.assign({}, options, {writableObjectMode: true, readableObjectMode: true}));
this._packKeys = this._packStrings = this._packNumbers = this._streamKeys = this._streamStrings = this._streamNumbers = true;
if (options) {
'packValues' in options && (this._packKeys = this._packStrings = this._packNumbers = options.packValues);
'packKeys' in options && (this._packKeys = options.packKeys);
'packStrings' in options && (this._packStrings = options.packStrings);
'packNumbers' in options && (this._packNumbers = options.packNumbers);
'streamValues' in options && (this._streamKeys = this._streamStrings = this._streamNumbers = options.streamValues);
'streamKeys' in options && (this._streamKeys = options.streamKeys);
'streamStrings' in options && (this._streamStrings = options.streamStrings);
'streamNumbers' in options && (this._streamNumbers = options.streamNumbers);
if (typeof options.replacer == 'function') {
this._replacer = options.replacer;
} else if (Array.isArray(options.replacer)) {
this._replacerDict = options.replacer.reduce((acc, k) => (acc[k] = 1, acc), {});
}
}
!this._packKeys && (this._streamKeys = true);
!this._packStrings && (this._streamStrings = true);
!this._packNumbers && (this._streamNumbers = true);
}
_transform(chunk, _, callback) {
const stack = [],
isArray = [];
if (chunk && typeof chunk == 'object' && typeof chunk.toJSON == 'function') {
chunk = chunk.toJSON('');
}
if (this._replacer) {
chunk = this._replacer('', chunk);
}
stack.push(chunk);
while (stack.length) {
const top = stack.pop();
main: switch (typeof top) {
case 'object':
if (top instanceof Emit) {
switch (top.tokenName) {
case 'keyValue':
const key = stack.pop();
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});
break main;
case 'startArray':
isArray.push(true);
break;
case 'startObject':
isArray.push(false);
break;
case 'endArray':
case 'endObject':
isArray.pop();
break;
}
this.push({name: top.tokenName});
break;
}
if (Array.isArray(top)) {
stack.push(new Emit('endArray'));
for (let i = top.length - 1; i >= 0; --i) {
let value = top[i];
if (value && typeof value == 'object' && typeof value.toJSON == 'function') {
value = value.toJSON('' + i);
}
if (this._replacer) {
value = this._replacer('' + i, value);
}
switch (typeof value) {
case 'function':
case 'symbol':
case 'undefined':
value = null;
break;
}
stack.push(value);
}
stack.push(new Emit('startArray'));
break;
}
if (top === null) {
this.push({name: 'nullValue', value: null});
break;
}
// all other objects are just objects
const keys = Object.keys(top);
stack.push(new Emit('endObject'));
for (let i = keys.length - 1; i >= 0; --i) {
const key = keys[i];
if (this._replacerDict && this._replacerDict[key] !== 1) continue;
let value = top[key];
if (value && typeof value == 'object' && typeof value.toJSON == 'function') {
value = value.toJSON(key);
}
if (this._replacer) {
value = this._replacer(key, value);
}
switch (typeof value) {
case 'function':
case 'symbol':
case 'undefined':
continue;
}
stack.push(value, key, new Emit('keyValue'));
}
stack.push(new Emit('startObject'));
break;
case 'string':
if (this._streamStrings) {
this.push({name: 'startString'});
this.push({name: 'stringChunk', value: top});
this.push({name: 'endString'});
}
this._packStrings && this.push({name: 'stringValue', value: top});
break;
case 'number':
const number = top.toString();
if (isNaN(number) || !isFinite(number)) {
this.push({name: 'nullValue', value: null});
break;
}
if (this._streamNumbers) {
this.push({name: 'startNumber'});
this.push({name: 'numberChunk', value: number});
this.push({name: 'endNumber'});
}
this._packNumbers && this.push({name: 'numberValue', value: number});
break;
case 'function':
case 'symbol':
case 'undefined':
if (isArray.length && isArray[isArray.length - 1]) {
// replace with null inside arrays
this.push({name: 'nullValue', value: null});
}
break;
case 'boolean':
this.push(top ? {name: 'trueValue', value: true} : {name: 'falseValue', value: false});
break;
default:
// skip everything else
break;
}
}
callback(null);
}
}
Disassembler.disassembler = Disassembler.make;
Disassembler.make.Constructor = Disassembler;
module.exports = Disassembler;