@nxtedition/lib 20.3.7 → 20.4.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/http.js CHANGED
@@ -1,3 +1,4 @@
1
+ import http2 from 'node:http2'
1
2
  import createError from 'http-errors'
2
3
  import { performance } from 'perf_hooks'
3
4
  import requestTarget from 'request-target'
@@ -222,6 +223,71 @@ export class ServerResponse extends http.ServerResponse {
222
223
  }
223
224
  }
224
225
 
226
+ export class Http2ServerResponse extends http2.Http2ServerResponse {
227
+ #now = 0
228
+ #created = 0
229
+ #timing = {
230
+ connect: -1,
231
+ headers: -1,
232
+ data: -1,
233
+ complete: -1,
234
+ }
235
+
236
+ get timing() {
237
+ return this.#timing
238
+ }
239
+
240
+ constructor(req) {
241
+ super(req)
242
+
243
+ this.#created = performance.now()
244
+ this.#now = this.#created
245
+ this.#timing.connect = 0
246
+ }
247
+
248
+ flushHeaders() {
249
+ if (this.#timing.headers === -1) {
250
+ this.#timing.headers = performance.now() - this.#now
251
+ this.#now += this.#timing.headers
252
+ }
253
+ return super.flushHeaders()
254
+ }
255
+
256
+ write(chunk, encoding, callback) {
257
+ if (this.#timing.data === -1) {
258
+ this.#timing.data = performance.now() - this.#now
259
+ this.#now += this.#timing.data
260
+ }
261
+
262
+ if (this.#timing.headers === -1) {
263
+ this.#timing.headers = this.#timing.data
264
+ }
265
+
266
+ return super.write(chunk, encoding, callback)
267
+ }
268
+
269
+ end(chunk, encoding, callback) {
270
+ if (this.#timing.data === -1) {
271
+ this.#timing.data = performance.now() - this.#now
272
+ this.#now += this.#timing.data
273
+ }
274
+
275
+ if (this.#timing.headers === -1) {
276
+ this.#timing.headers = this.#timing.data
277
+ }
278
+
279
+ return super.end(chunk, encoding, callback)
280
+ }
281
+
282
+ destroy(err) {
283
+ if (this.#timing.complete === -1) {
284
+ this.#timing.complete = performance.now() - this.#created
285
+ }
286
+
287
+ return super.destroy(err)
288
+ }
289
+ }
290
+
225
291
  export function createServer(options, ctx, middleware) {
226
292
  middleware = Array.isArray(middleware) ? middleware : [middleware]
227
293
  middleware = fp.values(middleware)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "20.3.7",
3
+ "version": "20.4.0",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",
@@ -12,29 +12,29 @@ export function compareRevStringString(a, b) {
12
12
  }
13
13
  }
14
14
 
15
- let indexA = 0
16
15
  const endA = a.length
16
+ let idxA = 0
17
17
  let lenA = endA
18
18
 
19
- let indexB = 0
20
19
  const endB = b.length
20
+ let idxB = 0
21
21
  let lenB = endB
22
22
 
23
23
  // Skip leading zeroes
24
- while (a[indexA] === '0') {
25
- ++indexA
24
+ while (a[idxA] === '0') {
25
+ ++idxA
26
26
  --lenA
27
27
  }
28
- while (b[indexB] === '0') {
29
- ++indexB
28
+ while (b[idxB] === '0') {
29
+ ++idxB
30
30
  --lenB
31
31
  }
32
32
 
33
33
  // Compare the revision number
34
34
  let result = 0
35
- while (indexA < endA && indexB < endB) {
36
- const ac = a[indexA++]
37
- const bc = b[indexB++]
35
+ while (idxA < endA && idxB < endB) {
36
+ const ac = a[idxA++]
37
+ const bc = b[idxB++]
38
38
 
39
39
  if (ac === '-') {
40
40
  if (bc === '-') {
@@ -53,9 +53,9 @@ export function compareRevStringString(a, b) {
53
53
  }
54
54
 
55
55
  // Compare the rest
56
- while (indexA < endA && indexB < endB) {
57
- const ac = a[indexA++]
58
- const bc = b[indexB++]
56
+ while (idxA < endA && idxB < endB) {
57
+ const ac = a[idxA++]
58
+ const bc = b[idxB++]
59
59
  if (ac !== bc) {
60
60
  return ac < bc ? -1 : 1
61
61
  }
@@ -78,29 +78,29 @@ export function compareRevBufferBuffer(a, b) {
78
78
  }
79
79
  }
80
80
 
81
- let indexA = 0
82
81
  const endA = a.length
82
+ let idxA = 0
83
83
  let lenA = endA
84
84
 
85
- let indexB = 0
86
85
  const endB = b.length
86
+ let idxB = 0
87
87
  let lenB = endB
88
88
 
89
89
  // Skip leading zeroes
90
- while (a[indexA] === ordZero) {
91
- ++indexA
90
+ while (a[idxA] === ordZero) {
91
+ ++idxA
92
92
  --lenA
93
93
  }
94
- while (b[indexB] === ordZero) {
95
- ++indexB
94
+ while (b[idxB] === ordZero) {
95
+ ++idxB
96
96
  --lenB
97
97
  }
98
98
 
99
99
  // Compare the revision number
100
100
  let result = 0
101
- while (indexA < endA && indexB < endB) {
102
- const ac = a[indexA++]
103
- const bc = b[indexB++]
101
+ while (idxA < endA && idxB < endB) {
102
+ const ac = a[idxA++]
103
+ const bc = b[idxB++]
104
104
 
105
105
  const isDashA = ac === ordDash
106
106
  const isDashB = bc === ordDash
@@ -121,9 +121,9 @@ export function compareRevBufferBuffer(a, b) {
121
121
  }
122
122
 
123
123
  // Compare the rest
124
- while (indexA < endA && indexB < endB) {
125
- const ac = a[indexA++]
126
- const bc = b[indexB++]
124
+ while (idxA < endA && idxB < endB) {
125
+ const ac = a[idxA++]
126
+ const bc = b[idxB++]
127
127
  result = ac - bc
128
128
  if (result) {
129
129
  return result
@@ -143,29 +143,29 @@ export function compareRevBufferString(a, b) {
143
143
  }
144
144
  }
145
145
 
146
- let indexA = 0
146
+ let idxA = 0
147
147
  const endA = a.length
148
148
  let lenA = endA
149
149
 
150
- let indexB = 0
150
+ let idxB = 0
151
151
  const endB = b.length
152
152
  let lenB = endB
153
153
 
154
154
  // Skip leading zeroes
155
- while (a[indexA] === ordZero) {
156
- ++indexA
155
+ while (a[idxA] === ordZero) {
156
+ ++idxA
157
157
  --lenA
158
158
  }
159
- while (b[indexB] === '0') {
160
- ++indexB
159
+ while (b[idxB] === '0') {
160
+ ++idxB
161
161
  --lenB
162
162
  }
163
163
 
164
164
  // Compare the revision number
165
165
  let result = 0
166
- while (indexA < endA && indexB < endB) {
167
- const ac = String.fromCharCode(a[indexA++])
168
- const bc = b[indexB++]
166
+ while (idxA < endA && idxB < endB) {
167
+ const ac = String.fromCharCode(a[idxA++])
168
+ const bc = b[idxB++]
169
169
 
170
170
  const isDashA = ac === '-'
171
171
  const isDashB = bc === '-'
@@ -186,9 +186,9 @@ export function compareRevBufferString(a, b) {
186
186
  }
187
187
 
188
188
  // Compare the rest
189
- while (indexA < endA && indexB < endB) {
190
- const ac = String.fromCharCode(a[indexA++])
191
- const bc = b[indexB++]
189
+ while (idxA < endA && idxB < endB) {
190
+ const ac = String.fromCharCode(a[idxA++])
191
+ const bc = b[idxB++]
192
192
  if (ac !== bc) {
193
193
  return ac < bc ? -1 : 1
194
194
  }
@@ -209,40 +209,60 @@ export function compareRevSliceSlice(a, b) {
209
209
  return 0
210
210
  }
211
211
 
212
- let indexA = a.byteOffset
213
212
  const endA = a.byteOffset + a.byteLength
213
+ let idxA = a.byteOffset
214
214
  let lenA = endA
215
215
  a = a.buffer
216
216
 
217
- let indexB = b.byteOffset
217
+ if (
218
+ !Buffer.isBuffer(a) ||
219
+ !Number.isInteger(idxA) ||
220
+ idxA < 0 ||
221
+ !Number.isInteger(lenA) ||
222
+ lenA < 0
223
+ ) {
224
+ throw new Error('invalid argument: a')
225
+ }
226
+
218
227
  const endB = b.byteOffset + b.byteLength
228
+ let idxB = b.byteOffset
219
229
  let lenB = endB
220
230
  b = b.buffer
221
231
 
232
+ if (
233
+ !Buffer.isBuffer(b) ||
234
+ !Number.isInteger(idxB) ||
235
+ idxB < 0 ||
236
+ !Number.isInteger(lenB) ||
237
+ lenB < 0
238
+ ) {
239
+ throw new Error('invalid argument: b')
240
+ }
241
+
222
242
  // Handle INF-XXXXXXXX
223
243
  {
224
- const isInfA = a[indexA] === ordI
225
- const isInfB = b[indexB] === ordI
244
+ const isInfA = a[idxA] === ordI
245
+ const isInfB = b[idxB] === ordI
226
246
  if (isInfA !== isInfB) {
227
247
  return isInfB ? -1 : 1
228
248
  }
229
249
  }
230
250
 
231
251
  // Skip leading zeroes
232
- while (a[indexA] === ordZero) {
233
- ++indexA
252
+ while (a[idxA] === ordZero) {
253
+ ++idxA
234
254
  --lenA
235
255
  }
236
- while (b[indexB] === ordZero) {
237
- ++indexB
256
+ while (b[idxB] === ordZero) {
257
+ ++idxB
238
258
  --lenB
239
259
  }
240
260
 
241
261
  // Compare the revision number
242
262
  let result = 0
243
- while (indexA < endA && indexB < endB) {
244
- const ac = a[indexA++]
245
- const bc = b[indexB++]
263
+ while (idxA < endA && idxB < endB) {
264
+ const ac = a[idxA++]
265
+ const bc = b[idxB++]
246
266
 
247
267
  const isDashA = ac === ordDash
248
268
  const isDashB = bc === ordDash
@@ -263,9 +283,9 @@ export function compareRevSliceSlice(a, b) {
263
283
  }
264
284
 
265
285
  // Compare the rest
266
- while (indexA < endA && indexB < endB) {
267
- const ac = a[indexA++]
268
- const bc = b[indexB++]
286
+ while (idxA < endA && idxB < endB) {
287
+ const ac = a[idxA++]
288
+ const bc = b[idxB++]
269
289
  result = ac - bc
270
290
  if (result) {
271
291
  return result