@nxtedition/lib 19.0.11 → 19.0.13
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 -60
- 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) {
|
|
@@ -260,59 +257,7 @@ export function makeCouch(opts) {
|
|
|
260
257
|
|
|
261
258
|
return stream.pipeline(
|
|
262
259
|
res.body,
|
|
263
|
-
split2(),
|
|
264
|
-
new stream.Transform({
|
|
265
|
-
objectMode: true,
|
|
266
|
-
construct(callback) {
|
|
267
|
-
this.state = 0
|
|
268
|
-
callback(null)
|
|
269
|
-
},
|
|
270
|
-
transform(line, encoding, callback) {
|
|
271
|
-
assert(typeof line === 'string', 'invalid line: ' + line)
|
|
272
|
-
if (line) {
|
|
273
|
-
if (live) {
|
|
274
|
-
const data = JSON.parse(line)
|
|
275
|
-
if (data.last_seq) {
|
|
276
|
-
assert(data.last_seq, 'invalid last_seq: ' + data.last_seq)
|
|
277
|
-
params.since = data.last_seq
|
|
278
|
-
} else {
|
|
279
|
-
params.since = data.seq || params.since
|
|
280
|
-
this.push(data)
|
|
281
|
-
}
|
|
282
|
-
} else {
|
|
283
|
-
// NOTE: This makes some assumptions about the format of the JSON.
|
|
284
|
-
if (this.state === 0) {
|
|
285
|
-
if (line === HEAD) {
|
|
286
|
-
this.state = 1
|
|
287
|
-
} else {
|
|
288
|
-
assert(line.length < HEAD.length, 'invalid line: ' + line)
|
|
289
|
-
}
|
|
290
|
-
} else if (this.state === 1) {
|
|
291
|
-
if (line === TAIL) {
|
|
292
|
-
this.state = 2
|
|
293
|
-
} else {
|
|
294
|
-
const idx = line.lastIndexOf('}') + 1
|
|
295
|
-
assert(idx > 0, 'invalid line; ' + line)
|
|
296
|
-
const data = JSON.parse(line.slice(0, idx))
|
|
297
|
-
params.since = data.seq || params.since
|
|
298
|
-
this.push(data)
|
|
299
|
-
}
|
|
300
|
-
} else if (this.state === 2) {
|
|
301
|
-
this.state = 3
|
|
302
|
-
params.since = JSON.parse('{' + line).last_seq
|
|
303
|
-
assert(params.since, 'invalid last_seq: ' + params.since)
|
|
304
|
-
} else {
|
|
305
|
-
assert(false, 'invalid state: ' + this.state)
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
callback(null)
|
|
310
|
-
},
|
|
311
|
-
flush(callback) {
|
|
312
|
-
assert(live || this.state === 3, 'invalid state: ' + this.state)
|
|
313
|
-
callback(null)
|
|
314
|
-
},
|
|
315
|
-
}),
|
|
260
|
+
split2({ writableHighWaterMark: highWaterMark ?? 128 * 1024 }),
|
|
316
261
|
() => {},
|
|
317
262
|
)
|
|
318
263
|
}
|
|
@@ -337,6 +282,7 @@ export function makeCouch(opts) {
|
|
|
337
282
|
let resume = null
|
|
338
283
|
let error = null
|
|
339
284
|
let ended = false
|
|
285
|
+
let state = 0
|
|
340
286
|
|
|
341
287
|
src
|
|
342
288
|
.on('readable', () => {
|
|
@@ -358,9 +304,40 @@ export function makeCouch(opts) {
|
|
|
358
304
|
})
|
|
359
305
|
|
|
360
306
|
while (true) {
|
|
361
|
-
const
|
|
362
|
-
if (
|
|
363
|
-
|
|
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
|
+
}
|
|
364
341
|
} else if (changes.length) {
|
|
365
342
|
remaining -= changes.length
|
|
366
343
|
assert(remaining >= 0, 'invalid remaining: ' + remaining)
|