bfj 8.0.0 → 9.0.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/.eslintrc +1 -1
- package/HISTORY.md +15 -0
- package/README.md +13 -44
- package/package.json +7 -7
- package/src/eventify.js +121 -113
- package/src/match.js +3 -5
- package/src/parse.js +3 -12
- package/src/read.js +1 -3
- package/src/streamify.js +45 -56
- package/src/stringify.js +2 -6
- package/src/unpipe.js +11 -4
- package/src/walk.js +257 -333
- package/src/write.js +2 -7
- package/test/integration.js +0 -1
- package/test/performance.js +38 -18
- package/test/unit/eventify.js +12 -13
- package/test/unit/parse.js +0 -7
- package/test/unit/stringify.js +0 -1
- package/test/unit/unpipe.js +0 -1
- package/test/unit/walk.js +3 -3
- package/test/unit/write.js +0 -1
- package/src/promise.js +0 -3
package/src/streamify.js
CHANGED
|
@@ -5,10 +5,9 @@ const eventify = require('./eventify')
|
|
|
5
5
|
const events = require('./events')
|
|
6
6
|
const JsonStream = require('./jsonstream')
|
|
7
7
|
const Hoopy = require('hoopy')
|
|
8
|
-
const promise = require('./promise')
|
|
9
8
|
const tryer = require('tryer')
|
|
10
9
|
|
|
11
|
-
const DEFAULT_BUFFER_LENGTH =
|
|
10
|
+
const DEFAULT_BUFFER_LENGTH = 256
|
|
12
11
|
|
|
13
12
|
module.exports = streamify
|
|
14
13
|
|
|
@@ -34,19 +33,16 @@ module.exports = streamify
|
|
|
34
33
|
* @option circular: 'error' or 'ignore', default is 'error'.
|
|
35
34
|
*
|
|
36
35
|
* @option yieldRate: The number of data items to process per timeslice,
|
|
37
|
-
* default is
|
|
36
|
+
* default is 1024.
|
|
38
37
|
*
|
|
39
|
-
* @option bufferLength: The length of the buffer, default is
|
|
38
|
+
* @option bufferLength: The length of the buffer, default is 256.
|
|
40
39
|
*
|
|
41
40
|
* @option highWaterMark: If set, will be passed to the readable stream constructor
|
|
42
41
|
* as the value for the highWaterMark option.
|
|
43
|
-
*
|
|
44
|
-
* @option Promise: The promise constructor to use, defaults to bluebird.
|
|
45
42
|
**/
|
|
46
43
|
function streamify (data, options = {}) {
|
|
47
44
|
const emitter = eventify(data, options)
|
|
48
45
|
const json = new Hoopy(options.bufferLength || DEFAULT_BUFFER_LENGTH)
|
|
49
|
-
const Promise = promise(options)
|
|
50
46
|
const space = normaliseSpace(options)
|
|
51
47
|
let streamOptions
|
|
52
48
|
const { highWaterMark } = options
|
|
@@ -125,20 +121,23 @@ function streamify (data, options = {}) {
|
|
|
125
121
|
}
|
|
126
122
|
|
|
127
123
|
function noRacing (handler) {
|
|
128
|
-
return
|
|
124
|
+
return async (eventData) => {
|
|
125
|
+
await mutex
|
|
126
|
+
mutex = await handler(eventData)
|
|
127
|
+
}
|
|
129
128
|
}
|
|
130
129
|
|
|
131
|
-
function array () {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
130
|
+
async function array () {
|
|
131
|
+
await beforeScope()
|
|
132
|
+
await addJson('[')
|
|
133
|
+
afterScope()
|
|
135
134
|
}
|
|
136
135
|
|
|
137
136
|
function beforeScope () {
|
|
138
137
|
return before(true)
|
|
139
138
|
}
|
|
140
139
|
|
|
141
|
-
function before (isScope) {
|
|
140
|
+
async function before (isScope) {
|
|
142
141
|
if (isProperty) {
|
|
143
142
|
isProperty = false
|
|
144
143
|
|
|
@@ -146,28 +145,22 @@ function streamify (data, options = {}) {
|
|
|
146
145
|
return addJson(' ')
|
|
147
146
|
}
|
|
148
147
|
|
|
149
|
-
return
|
|
148
|
+
return
|
|
150
149
|
}
|
|
151
150
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
needsComma = false
|
|
157
|
-
}
|
|
151
|
+
if (needsComma) {
|
|
152
|
+
if (isScope) {
|
|
153
|
+
needsComma = false
|
|
154
|
+
}
|
|
158
155
|
|
|
159
|
-
|
|
160
|
-
|
|
156
|
+
await addJson(',')
|
|
157
|
+
} else if (! isScope) {
|
|
158
|
+
needsComma = true
|
|
159
|
+
}
|
|
161
160
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
})
|
|
166
|
-
.then(() => {
|
|
167
|
-
if (space && indentation) {
|
|
168
|
-
return indent()
|
|
169
|
-
}
|
|
170
|
-
})
|
|
161
|
+
if (space && indentation) {
|
|
162
|
+
return indent()
|
|
163
|
+
}
|
|
171
164
|
}
|
|
172
165
|
|
|
173
166
|
function addJson (chunk) {
|
|
@@ -207,53 +200,49 @@ function streamify (data, options = {}) {
|
|
|
207
200
|
}
|
|
208
201
|
}
|
|
209
202
|
|
|
210
|
-
function object () {
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
203
|
+
async function object () {
|
|
204
|
+
await beforeScope()
|
|
205
|
+
await addJson('{')
|
|
206
|
+
afterScope()
|
|
214
207
|
}
|
|
215
208
|
|
|
216
|
-
function property (name) {
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
isProperty = true
|
|
221
|
-
})
|
|
209
|
+
async function property (name) {
|
|
210
|
+
await before()
|
|
211
|
+
await addJson(`"${name}":`)
|
|
212
|
+
isProperty = true
|
|
222
213
|
}
|
|
223
214
|
|
|
224
215
|
function string (s) {
|
|
225
216
|
return value(`"${s}"`)
|
|
226
217
|
}
|
|
227
218
|
|
|
228
|
-
function value (v) {
|
|
229
|
-
|
|
230
|
-
|
|
219
|
+
async function value (v) {
|
|
220
|
+
await before()
|
|
221
|
+
await addJson(`${v}`)
|
|
231
222
|
}
|
|
232
223
|
|
|
233
|
-
function endArray () {
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
224
|
+
async function endArray () {
|
|
225
|
+
await beforeScopeEnd()
|
|
226
|
+
await addJson(']')
|
|
227
|
+
afterScopeEnd()
|
|
237
228
|
}
|
|
238
229
|
|
|
239
|
-
function beforeScopeEnd () {
|
|
230
|
+
async function beforeScopeEnd () {
|
|
240
231
|
if (space) {
|
|
241
232
|
indentation = indentation.substr(space.length)
|
|
242
233
|
|
|
243
|
-
|
|
234
|
+
await indent()
|
|
244
235
|
}
|
|
245
|
-
|
|
246
|
-
return Promise.resolve()
|
|
247
236
|
}
|
|
248
237
|
|
|
249
238
|
function afterScopeEnd () {
|
|
250
239
|
needsComma = true
|
|
251
240
|
}
|
|
252
241
|
|
|
253
|
-
function endObject () {
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
242
|
+
async function endObject () {
|
|
243
|
+
await beforeScopeEnd()
|
|
244
|
+
await addJson('}')
|
|
245
|
+
afterScopeEnd()
|
|
257
246
|
}
|
|
258
247
|
|
|
259
248
|
function end () {
|
package/src/stringify.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const promise = require('./promise')
|
|
4
3
|
const streamify = require('./streamify')
|
|
5
4
|
|
|
6
5
|
module.exports = stringify
|
|
@@ -27,18 +26,15 @@ module.exports = stringify
|
|
|
27
26
|
* @option circular: 'error' or 'ignore', default is 'error'.
|
|
28
27
|
*
|
|
29
28
|
* @option yieldRate: The number of data items to process per timeslice,
|
|
30
|
-
* default is
|
|
29
|
+
* default is 1024.
|
|
31
30
|
*
|
|
32
|
-
* @option bufferLength: The length of the buffer, default is
|
|
31
|
+
* @option bufferLength: The length of the buffer, default is 256.
|
|
33
32
|
*
|
|
34
33
|
* @option highWaterMark: If set, will be passed to the readable stream constructor
|
|
35
34
|
* as the value for the highWaterMark option.
|
|
36
|
-
*
|
|
37
|
-
* @option Promise: The promise constructor to use, defaults to bluebird.
|
|
38
35
|
**/
|
|
39
36
|
function stringify (data, options) {
|
|
40
37
|
const json = []
|
|
41
|
-
const Promise = promise(options)
|
|
42
38
|
const stream = streamify(data, options)
|
|
43
39
|
|
|
44
40
|
let resolve, reject
|
package/src/unpipe.js
CHANGED
|
@@ -22,16 +22,23 @@ module.exports = unpipe
|
|
|
22
22
|
* to save memory. The default value is `1048576`.
|
|
23
23
|
*
|
|
24
24
|
* @option yieldRate: The number of data items to process per timeslice,
|
|
25
|
-
* default is
|
|
25
|
+
* default is 1024.
|
|
26
26
|
**/
|
|
27
27
|
function unpipe (callback, options) {
|
|
28
28
|
check.assert.function(callback, 'Invalid callback argument')
|
|
29
29
|
|
|
30
30
|
const jsonstream = new stream.PassThrough()
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
.then(data => callback(null, data))
|
|
34
|
-
.catch(error => callback(error))
|
|
32
|
+
initStream(jsonstream, callback, options)
|
|
35
33
|
|
|
36
34
|
return jsonstream
|
|
37
35
|
}
|
|
36
|
+
|
|
37
|
+
async function initStream (jsonstream, callback, options) {
|
|
38
|
+
try {
|
|
39
|
+
const data = await parse(jsonstream, { ...options, ndjson: false })
|
|
40
|
+
await callback(null, data)
|
|
41
|
+
} catch (error) {
|
|
42
|
+
await callback(error)
|
|
43
|
+
}
|
|
44
|
+
}
|