@nxtedition/lib 19.0.12 → 19.0.14
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/couch.js +37 -62
- package/package.json +1 -1
package/couch.js
CHANGED
|
@@ -134,7 +134,7 @@ export function makeCouch(opts) {
|
|
|
134
134
|
})
|
|
135
135
|
}
|
|
136
136
|
|
|
137
|
-
async function* changes({ client = defaultClient, signal, ...options } = {}) {
|
|
137
|
+
async function* changes({ client = defaultClient, signal, highWaterMark, ...options } = {}) {
|
|
138
138
|
const params = {}
|
|
139
139
|
|
|
140
140
|
let body
|
|
@@ -245,9 +245,6 @@ export function makeCouch(opts) {
|
|
|
245
245
|
bodyTimeout: 2 * (params.heartbeat || 60e3),
|
|
246
246
|
}
|
|
247
247
|
|
|
248
|
-
const HEAD = '{"results":['
|
|
249
|
-
const TAIL = '],'
|
|
250
|
-
|
|
251
248
|
const res = await client.request(req)
|
|
252
249
|
|
|
253
250
|
if (res.statusCode < 200 || res.statusCode >= 300) {
|
|
@@ -258,63 +255,9 @@ export function makeCouch(opts) {
|
|
|
258
255
|
})
|
|
259
256
|
}
|
|
260
257
|
|
|
261
|
-
// TODO (perf): Avoid stream pipeline
|
|
262
258
|
return stream.pipeline(
|
|
263
259
|
res.body,
|
|
264
|
-
split2(),
|
|
265
|
-
new stream.Transform({
|
|
266
|
-
objectMode: true,
|
|
267
|
-
readableHighWaterMark: 256, // TODO (fix): We would like to limit this to bytes and not elements...
|
|
268
|
-
construct(callback) {
|
|
269
|
-
this.state = 0
|
|
270
|
-
callback(null)
|
|
271
|
-
},
|
|
272
|
-
transform(line, encoding, callback) {
|
|
273
|
-
assert(typeof line === 'string', 'invalid line: ' + line)
|
|
274
|
-
if (line) {
|
|
275
|
-
if (live) {
|
|
276
|
-
const data = JSON.parse(line)
|
|
277
|
-
if (data.last_seq) {
|
|
278
|
-
assert(data.last_seq, 'invalid last_seq: ' + data.last_seq)
|
|
279
|
-
params.since = data.last_seq
|
|
280
|
-
} else {
|
|
281
|
-
params.since = data.seq || params.since
|
|
282
|
-
this.push(data)
|
|
283
|
-
}
|
|
284
|
-
} else {
|
|
285
|
-
// NOTE: This makes some assumptions about the format of the JSON.
|
|
286
|
-
if (this.state === 0) {
|
|
287
|
-
if (line === HEAD) {
|
|
288
|
-
this.state = 1
|
|
289
|
-
} else {
|
|
290
|
-
assert(line.length < HEAD.length, 'invalid line: ' + line)
|
|
291
|
-
}
|
|
292
|
-
} else if (this.state === 1) {
|
|
293
|
-
if (line === TAIL) {
|
|
294
|
-
this.state = 2
|
|
295
|
-
} else {
|
|
296
|
-
const idx = line.lastIndexOf('}') + 1
|
|
297
|
-
assert(idx > 0, 'invalid line; ' + line)
|
|
298
|
-
const data = JSON.parse(line.slice(0, idx))
|
|
299
|
-
params.since = data.seq || params.since
|
|
300
|
-
this.push(data)
|
|
301
|
-
}
|
|
302
|
-
} else if (this.state === 2) {
|
|
303
|
-
this.state = 3
|
|
304
|
-
params.since = JSON.parse('{' + line).last_seq
|
|
305
|
-
assert(params.since, 'invalid last_seq: ' + params.since)
|
|
306
|
-
} else {
|
|
307
|
-
assert(false, 'invalid state: ' + this.state)
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
callback(null)
|
|
312
|
-
},
|
|
313
|
-
flush(callback) {
|
|
314
|
-
assert(live || this.state === 3, 'invalid state: ' + this.state)
|
|
315
|
-
callback(null)
|
|
316
|
-
},
|
|
317
|
-
}),
|
|
260
|
+
split2('\n', { writableHighWaterMark: highWaterMark ?? 128 * 1024 }),
|
|
318
261
|
() => {},
|
|
319
262
|
)
|
|
320
263
|
}
|
|
@@ -339,6 +282,7 @@ export function makeCouch(opts) {
|
|
|
339
282
|
let resume = null
|
|
340
283
|
let error = null
|
|
341
284
|
let ended = false
|
|
285
|
+
let state = 0
|
|
342
286
|
|
|
343
287
|
src
|
|
344
288
|
.on('readable', () => {
|
|
@@ -360,9 +304,40 @@ export function makeCouch(opts) {
|
|
|
360
304
|
})
|
|
361
305
|
|
|
362
306
|
while (true) {
|
|
363
|
-
const
|
|
364
|
-
if (
|
|
365
|
-
|
|
307
|
+
const line = src.read()
|
|
308
|
+
if (line) {
|
|
309
|
+
if (live) {
|
|
310
|
+
const data = JSON.parse(line)
|
|
311
|
+
if (data.last_seq) {
|
|
312
|
+
params.since = data.last_seq
|
|
313
|
+
} else {
|
|
314
|
+
params.since = data.seq || params.since
|
|
315
|
+
this.push(data)
|
|
316
|
+
}
|
|
317
|
+
} else {
|
|
318
|
+
// NOTE: This makes some assumptions about the format of the JSON.
|
|
319
|
+
if (state === 0) {
|
|
320
|
+
if (line.endsWith('[')) {
|
|
321
|
+
state = 1
|
|
322
|
+
} else {
|
|
323
|
+
assert(false, 'invalid head: ' + line)
|
|
324
|
+
}
|
|
325
|
+
} else if (state === 1) {
|
|
326
|
+
if (line.startsWith(']')) {
|
|
327
|
+
state = 2
|
|
328
|
+
} else {
|
|
329
|
+
const idx = line.lastIndexOf('}') + 1
|
|
330
|
+
assert(idx > 0, 'invalid row: ' + line)
|
|
331
|
+
const change = JSON.parse(line.slice(0, idx))
|
|
332
|
+
params.since = change.seq || params.since
|
|
333
|
+
changes.push(change)
|
|
334
|
+
}
|
|
335
|
+
} else if (state === 2) {
|
|
336
|
+
state = 3
|
|
337
|
+
params.since = JSON.parse('{' + line).last_seq
|
|
338
|
+
assert(params.since, 'invalid trailer: ' + line)
|
|
339
|
+
}
|
|
340
|
+
}
|
|
366
341
|
} else if (changes.length) {
|
|
367
342
|
remaining -= changes.length
|
|
368
343
|
assert(remaining >= 0, 'invalid remaining: ' + remaining)
|