@nxtedition/lib 19.0.15 → 19.0.17

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.
Files changed (2) hide show
  1. package/couch.js +51 -55
  2. package/package.json +1 -1
package/couch.js CHANGED
@@ -72,10 +72,9 @@ export function makeCouch(opts) {
72
72
  keepAliveTimeout: 2 * 60e3,
73
73
  headersTimeout: 10 * 60e3,
74
74
  bodyTimeout: 2 * 60e3,
75
- connections: 256,
76
75
  }
77
76
 
78
- const userAgent = config.userAgent
77
+ const userAgent = config.userAgent || globalThis.userAgent
79
78
  const defaultClient = new undici.Pool(dbOrigin, defaultClientOpts)
80
79
 
81
80
  const getClient =
@@ -177,7 +176,7 @@ export function makeCouch(opts) {
177
176
  }
178
177
 
179
178
  if (options.heartbeat != null) {
180
- params.heartbeat = options.heartbeat
179
+ params.heartbeat = Number(options.heartbeat)
181
180
  } else {
182
181
  params.heartbeat = 10e3
183
182
  }
@@ -228,55 +227,50 @@ export function makeCouch(opts) {
228
227
  }
229
228
  }
230
229
 
231
- async function parse(live, params) {
232
- const req = {
233
- path: `${dbPathname}/_changes?${new URLSearchParams(params)}`,
234
- idempotent: false,
235
- blocking: true,
236
- method,
237
- body: JSON.stringify(body),
238
- signal: ac.signal,
239
- headers: {
240
- 'user-agent': userAgent,
241
- 'request-id': genReqId(),
242
- ...(body ? { 'content-type': 'application/json' } : {}),
243
- },
244
- highWaterMark: 256 * 1024, // TODO (fix): Needs support in undici...
245
- bodyTimeout: 2 * (params.heartbeat || 60e3),
246
- }
247
-
248
- const res = await client.request(req)
249
-
250
- if (res.statusCode < 200 || res.statusCode >= 300) {
251
- throw makeError(req, {
252
- status: res.statusCode,
253
- headers: res.headers,
254
- data: await res.body.text(),
255
- })
256
- }
257
-
258
- return stream.pipeline(
259
- res.body,
260
- split2('\n', { writableHighWaterMark: highWaterMark ?? 128 * 1024 }),
261
- () => {},
262
- )
263
- }
264
-
265
230
  let remaining = Number(options.limit) || Infinity
266
231
  try {
267
232
  while (true) {
233
+ let src
268
234
  try {
269
- const params2 = {
270
- ...params,
271
- ...options.query,
272
- feed: live ? 'continuous' : 'normal',
273
- }
235
+ const query = { ...params, feed: live ? 'continuous' : 'normal' }
274
236
 
275
237
  if (Number.isFinite(remaining)) {
276
- params.limit = remaining
238
+ query.limit = remaining
239
+ }
240
+
241
+ const ureq = {
242
+ path: `${dbPathname}/_changes`,
243
+ query,
244
+ idempotent: false,
245
+ blocking: true,
246
+ method,
247
+ body: JSON.stringify(body),
248
+ signal: ac.signal,
249
+ headers: {
250
+ 'user-agent': userAgent,
251
+ 'request-id': genReqId(),
252
+ ...(body ? { 'content-type': 'application/json' } : {}),
253
+ },
254
+ highWaterMark: 256 * 1024, // TODO (fix): Needs support in undici...
255
+ bodyTimeout: 2 * (params.heartbeat || 60e3),
277
256
  }
278
257
 
279
- const src = await parse(live, params2)
258
+ const ures = await client.request(ureq)
259
+
260
+ if (ures.statusCode < 200 || ures.statusCode >= 300) {
261
+ throw makeError(ureq, {
262
+ status: ures.statusCode,
263
+ headers: ures.headers,
264
+ data: await ures.body.text(),
265
+ })
266
+ }
267
+
268
+ src = stream.pipeline(
269
+ ures.body,
270
+ split2('\n', { writableHighWaterMark: highWaterMark ?? 256 * 1024 }),
271
+ () => {},
272
+ )
273
+
280
274
  const changes = []
281
275
 
282
276
  let resume = null
@@ -284,24 +278,24 @@ export function makeCouch(opts) {
284
278
  let ended = false
285
279
  let state = 0
286
280
 
281
+ function maybeResume() {
282
+ if (resume) {
283
+ resume()
284
+ resume = null
285
+ }
286
+ }
287
+
287
288
  src
288
- .on('readable', () => {
289
- if (resume) {
290
- resume()
291
- resume = null
292
- }
293
- })
289
+ .on('readable', maybeResume)
294
290
  .on('error', (err) => {
295
291
  error = err
296
-
297
- if (resume) {
298
- resume()
299
- resume = null
300
- }
292
+ maybeResume()
301
293
  })
302
294
  .on('end', () => {
303
295
  ended = true
296
+ maybeResume()
304
297
  })
298
+ .on('close', maybeResume)
305
299
 
306
300
  while (true) {
307
301
  const line = src.read()
@@ -372,6 +366,8 @@ export function makeCouch(opts) {
372
366
  } else {
373
367
  await delay(err, retryCount, { signal })
374
368
  }
369
+ } finally {
370
+ src.destroy()
375
371
  }
376
372
  }
377
373
  } finally {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "19.0.15",
3
+ "version": "19.0.17",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",