parse.js
3.81 KB
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
171
172
173
174
175
176
177
178
179
180
181
'use strict'
const check = require('check-types')
const events = require('./events')
const promise = require('./promise')
const walk = require('./walk')
module.exports = parse
const NDJSON_STATE = new Map()
/**
* Public function `parse`.
*
* Returns a promise and asynchronously parses a stream of JSON data. If
* there are no errors, the promise is resolved with the parsed data. If
* errors occur, the promise is rejected with the first error.
*
* @param stream: Readable instance representing the incoming JSON.
*
* @option reviver: Transformation function, invoked depth-first.
*
* @option yieldRate: The number of data items to process per timeslice,
* default is 16384.
*
* @option Promise: The promise constructor to use, defaults to bluebird.
*
* @option ndjson: Set this to true to parse newline-delimited JSON. In
* this case, each call will be resolved with one value
* from the stream. To parse the entire stream, calls
* should be made sequentially one-at-a-time until the
* returned promise resolves to `undefined`.
**/
function parse (stream, options = {}) {
const Promise = promise(options)
try {
check.assert.maybe.function(options.reviver, 'Invalid reviver option')
} catch (err) {
return Promise.reject(err)
}
const errors = []
const scopes = []
const reviver = options.reviver
const shouldHandleNdjson = !! options.ndjson
let emitter, resolve, reject, scopeKey
if (shouldHandleNdjson && NDJSON_STATE.has(stream)) {
const state = NDJSON_STATE.get(stream)
NDJSON_STATE.delete(stream)
emitter = state.emitter
setImmediate(state.resume)
} else {
emitter = walk(stream, options)
}
emitter.on(events.array, array)
emitter.on(events.object, object)
emitter.on(events.property, property)
emitter.on(events.string, value)
emitter.on(events.number, value)
emitter.on(events.literal, value)
emitter.on(events.endArray, endScope)
emitter.on(events.endObject, endScope)
emitter.on(events.end, end)
emitter.on(events.error, error)
emitter.on(events.dataError, error)
if (shouldHandleNdjson) {
emitter.on(events.endLine, endLine)
}
return new Promise((res, rej) => {
resolve = res
reject = rej
})
function array () {
if (errors.length > 0) {
return
}
beginScope([])
}
function beginScope (parsed) {
if (errors.length > 0) {
return
}
if (scopes.length > 0) {
value(parsed)
}
scopes.push(parsed)
}
function value (v) {
if (errors.length > 0) {
return
}
if (scopes.length === 0) {
return scopes.push(v)
}
const scope = scopes[scopes.length - 1]
if (scopeKey) {
scope[scopeKey] = v
scopeKey = null
} else {
scope.push(v)
}
}
function object () {
if (errors.length > 0) {
return
}
beginScope({})
}
function property (name) {
if (errors.length > 0) {
return
}
scopeKey = name
}
function endScope () {
if (errors.length > 0) {
return
}
if (scopes.length > 1) {
scopes.pop()
}
}
function end () {
if (shouldHandleNdjson) {
const resume = emitter.pause()
emitter.removeAllListeners()
NDJSON_STATE.set(stream, { emitter, resume })
}
if (errors.length > 0) {
return reject(errors[0])
}
if (reviver) {
scopes[0] = transform(scopes[0], '')
}
resolve(scopes[0])
}
function transform (obj, key) {
if (obj && typeof obj === 'object') {
Object.entries(obj).forEach(([ k, v ]) => {
obj[k] = transform(v, k)
})
}
return reviver(key, obj)
}
function error (e) {
errors.push(e)
}
function endLine () {
if (scopes.length > 0) {
end()
}
}
}