@nxtedition/lib 20.3.4 → 20.3.6

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 (3) hide show
  1. package/couch.js +19 -104
  2. package/http.js +1 -6
  3. package/package.json +1 -1
package/couch.js CHANGED
@@ -14,19 +14,6 @@ import {
14
14
  request as undiciRequest,
15
15
  } from '@nxtedition/nxt-undici'
16
16
 
17
- // https://github.com/fastify/fastify/blob/main/lib/reqIdGenFactory.js
18
- // 2,147,483,647 (2^31 − 1) stands for max SMI value (an internal optimization of V8).
19
- // With this upper bound, if you'll be generating 1k ids/sec, you're going to hit it in ~25 days.
20
- // This is very likely to happen in real-world applications, hence the limit is enforced.
21
- // Growing beyond this value will make the id generation slower and cause a deopt.
22
- // In the worst cases, it will become a float, losing accuracy.
23
- const maxInt = 2147483647
24
- let nextReqId = Math.floor(Math.random() * maxInt)
25
- function genReqId() {
26
- nextReqId = (nextReqId + 1) & maxInt
27
- return `req-${nextReqId.toString(36)}`
28
- }
29
-
30
17
  export function makeCouch(opts) {
31
18
  let config
32
19
  if (typeof opts === 'string') {
@@ -228,36 +215,16 @@ export function makeCouch(opts) {
228
215
  return next()
229
216
  })
230
217
 
231
- const limit = options.limit
232
-
233
218
  try {
234
- // Limit request duration to allow server to load balance.
235
- const socketCount = 64 * 1024
236
- let remaining = Number(limit) || Infinity
237
- while (remaining > 0) {
238
- let count = 0
239
- const limit = Math.min(remaining, socketCount)
240
- for await (const changes of _changes({
241
- live,
242
- retry,
243
- limit,
244
- params,
245
- method,
246
- body,
247
- signal: ac.signal,
248
- client,
249
- })) {
250
- yield changes
251
- if (changes != null) {
252
- count += changes.length
253
- remaining -= changes.length
254
- }
255
- }
256
- assert(remaining >= 0)
257
- if (!live && count < socketCount) {
258
- return
259
- }
260
- }
219
+ yield* _changes({
220
+ method,
221
+ live,
222
+ retry,
223
+ params,
224
+ body,
225
+ signal: ac.signal,
226
+ client,
227
+ })
261
228
  } finally {
262
229
  ac.abort()
263
230
  if (signal) {
@@ -273,39 +240,35 @@ export function makeCouch(opts) {
273
240
  async function* _changes({
274
241
  live,
275
242
  retry,
276
- limit,
277
243
  params,
278
244
  method,
279
245
  body,
280
246
  signal,
281
247
  client,
282
- blocking = live || !limit || limit > 256,
248
+ blocking = live || !params.limit || params.limit > 256,
283
249
  }) {
284
250
  let retryCount = 0
285
- let remaining = Number(limit) || Infinity
286
251
  while (true) {
287
252
  let src
288
253
  try {
289
254
  const query = { ...params, feed: live ? 'continuous' : 'normal' }
290
255
 
291
- if (Number.isFinite(remaining)) {
292
- query.limit = remaining
293
- }
294
-
295
256
  const ureq = {
296
257
  path: `${dbPathname}/_changes`,
297
258
  origin: dbOrigin,
298
259
  query,
299
- idempotent: false,
260
+ headers: body
261
+ ? {
262
+ 'content-type': 'application/json',
263
+ }
264
+ : null,
300
265
  blocking,
301
266
  method,
302
267
  body: JSON.stringify(body),
268
+ retry: false,
269
+ idempotent: false,
303
270
  signal,
304
- headers: {
305
- 'user-agent': userAgent,
306
- 'request-id': genReqId(),
307
- ...(body ? { 'content-type': 'application/json' } : {}),
308
- },
271
+ headersTimeout: 2 * 60e3,
309
272
  bodyTimeout: 2 * (params.heartbeat || 60e3),
310
273
  highWaterMark: 256 * 1024,
311
274
  dispatcher: client,
@@ -404,9 +367,6 @@ export function makeCouch(opts) {
404
367
  }
405
368
  }
406
369
  } else if (changes.length) {
407
- remaining -= changes.length
408
- assert(remaining >= 0, 'invalid remaining: ' + remaining)
409
-
410
370
  const ret = changes.splice(0)
411
371
  ret.lastSeq = params.since
412
372
  yield ret
@@ -943,12 +903,6 @@ class StreamOutput extends stream.Readable {
943
903
  #state
944
904
  #decoder
945
905
  #headers
946
- #startTime = performance.now()
947
- #stats = {
948
- connect: -1,
949
- headers: -1,
950
- ttfb: -1,
951
- }
952
906
 
953
907
  constructor({ signal, highWaterMark = 256 }) {
954
908
  super({ objectMode: true, highWaterMark, signal })
@@ -958,10 +912,6 @@ class StreamOutput extends stream.Readable {
958
912
  return this.#headers
959
913
  }
960
914
 
961
- get stats() {
962
- return this.#stats
963
- }
964
-
965
915
  _read() {
966
916
  this.#resume?.()
967
917
  }
@@ -972,8 +922,6 @@ class StreamOutput extends stream.Readable {
972
922
  }
973
923
 
974
924
  onConnect(abort) {
975
- this.#stats.connect = performance.now() - this.#startTime
976
-
977
925
  if (this.destroyed) {
978
926
  abort(this.errored)
979
927
  } else if (this.#state) {
@@ -987,10 +935,6 @@ class StreamOutput extends stream.Readable {
987
935
  }
988
936
 
989
937
  onHeaders(statusCode, rawHeaders, resume, statusText, headers = parseHeaders(rawHeaders)) {
990
- if (this.#stats.headers === -1) {
991
- this.#stats.headers = performance.now() - this.#startTime - this.#stats.connect
992
- }
993
-
994
938
  if (statusCode >= 300 || statusCode < 200) {
995
939
  throw new Error('invalid status code: ' + statusCode)
996
940
  }
@@ -1000,10 +944,6 @@ class StreamOutput extends stream.Readable {
1000
944
  }
1001
945
 
1002
946
  onData(data) {
1003
- if (this.#stats.ttfb === -1) {
1004
- this.#stats.ttfb = performance.now() - this.#startTime - this.#stats.headers
1005
- }
1006
-
1007
947
  const lines = this.#decoder.decode(data, { stream: true }).split(/\r?\n+/)
1008
948
  lines[0] = this.#str + lines[0]
1009
949
  this.#str = lines.pop() ?? ''
@@ -1049,16 +989,8 @@ class PromiseOutput {
1049
989
  #str
1050
990
  #decoder
1051
991
  #headers
1052
- #startTime = performance.now()
1053
992
  #signal
1054
993
  #abort
1055
- #stats = {
1056
- connect: -1,
1057
- headers: -1,
1058
- data: -1,
1059
- complete: -1,
1060
- error: -1,
1061
- }
1062
994
 
1063
995
  constructor({ resolve, reject, signal }) {
1064
996
  this.#resolve = resolve
@@ -1067,7 +999,6 @@ class PromiseOutput {
1067
999
  }
1068
1000
 
1069
1001
  onConnect(abort) {
1070
- this.#stats.connect = performance.now() - this.#startTime
1071
1002
  this.#decoder = new TextDecoder()
1072
1003
  this.#str = ''
1073
1004
 
@@ -1080,10 +1011,6 @@ class PromiseOutput {
1080
1011
  }
1081
1012
 
1082
1013
  onHeaders(statusCode, rawHeaders, resume, statusText, headers = parseHeaders(rawHeaders)) {
1083
- if (this.#stats.headers === -1) {
1084
- this.#stats.headers = performance.now() - this.#startTime - this.#stats.connect
1085
- }
1086
-
1087
1014
  if (statusCode >= 300 || statusCode < 200) {
1088
1015
  throw new Error('invalid status code: ' + statusCode)
1089
1016
  }
@@ -1092,29 +1019,17 @@ class PromiseOutput {
1092
1019
  }
1093
1020
 
1094
1021
  onData(data) {
1095
- if (this.#stats.data === -1) {
1096
- this.#stats.data = performance.now() - this.#startTime - this.#stats.headers
1097
- }
1098
-
1099
1022
  this.#str += this.#decoder.decode(data, { stream: true })
1100
1023
  }
1101
1024
 
1102
1025
  onComplete() {
1103
- if (this.#stats.complete === -1) {
1104
- this.#stats.complete = performance.now() - this.#startTime - this.#stats.data
1105
- }
1106
-
1107
1026
  this.#str += this.#decoder.decode(undefined, { stream: false })
1108
1027
 
1109
- const val = Object.assign(JSON.parse(this.#str), { headers: this.#headers, stats: this.#stats })
1028
+ const val = Object.assign(JSON.parse(this.#str), { headers: this.#headers })
1110
1029
  this.#onDone(null, val)
1111
1030
  }
1112
1031
 
1113
1032
  onError(err) {
1114
- if (this.#stats.error === -1) {
1115
- this.#stats.error = performance.now() - this.#startTime - this.#stats.data
1116
- }
1117
-
1118
1033
  this.#onDone(err)
1119
1034
  }
1120
1035
 
package/http.js CHANGED
@@ -138,8 +138,6 @@ export async function request(ctx, next) {
138
138
  reqLogger.error('request error')
139
139
  }
140
140
 
141
- reqLogger.debug('request ended')
142
-
143
141
  res.end()
144
142
  } else {
145
143
  reqLogger = reqLogger.child({ res, err, responseTime })
@@ -152,10 +150,7 @@ export async function request(ctx, next) {
152
150
  reqLogger.error('request error')
153
151
  }
154
152
 
155
- if (res.writableEnded) {
156
- reqLogger.debug('response completed')
157
- } else {
158
- reqLogger.debug('response destroyed')
153
+ if (!res.writableEnded) {
159
154
  res.destroy()
160
155
  }
161
156
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "20.3.4",
3
+ "version": "20.3.6",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",