@nxtedition/lib 20.3.9 → 20.4.1

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/http.js +102 -16
  2. package/package.json +1 -1
  3. package/serializers.js +0 -1
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'
@@ -180,44 +181,129 @@ export class ServerResponse extends http.ServerResponse {
180
181
  }
181
182
 
182
183
  flushHeaders() {
183
- if (this.#timing.headers === -1) {
184
- this.#timing.headers = performance.now() - this.#now
185
- this.#now += this.#timing.headers
184
+ if (!this.destroyed) {
185
+ if (this.#timing.headers === -1) {
186
+ this.#timing.headers = performance.now() - this.#now
187
+ this.#now += this.#timing.headers
188
+ }
186
189
  }
187
190
  return super.flushHeaders()
188
191
  }
189
192
 
190
193
  write(chunk, encoding, callback) {
191
- if (this.#timing.data === -1) {
192
- this.#timing.data = performance.now() - this.#now
193
- this.#now += this.#timing.data
194
- }
194
+ if (!this.destroyed) {
195
+ if (this.#timing.data === -1) {
196
+ this.#timing.data = performance.now() - this.#now
197
+ this.#now += this.#timing.data
198
+ }
195
199
 
196
- if (this.#timing.headers === -1) {
197
- this.#timing.headers = this.#timing.data
200
+ if (this.#timing.headers === -1) {
201
+ this.#timing.headers = this.#timing.data
202
+ }
198
203
  }
199
204
 
200
205
  return super.write(chunk, encoding, callback)
201
206
  }
202
207
 
203
208
  end(chunk, encoding, callback) {
204
- if (this.#timing.data === -1) {
205
- this.#timing.data = performance.now() - this.#now
206
- this.#now += this.#timing.data
207
- }
209
+ if (!this.destroyed) {
210
+ if (this.#timing.data === -1) {
211
+ this.#timing.data = performance.now() - this.#now
212
+ this.#now += this.#timing.data
213
+ }
214
+
215
+ if (this.#timing.headers === -1) {
216
+ this.#timing.headers = this.#timing.data
217
+ }
208
218
 
209
- if (this.#timing.headers === -1) {
210
- this.#timing.headers = this.#timing.data
219
+ this.#timing.complete = performance.now() - this.#created
211
220
  }
212
221
 
213
222
  return super.end(chunk, encoding, callback)
214
223
  }
215
224
 
216
225
  destroy(err) {
217
- if (this.#timing.complete === -1) {
226
+ if (!this.destroyed) {
227
+ if (this.#timing.complete === -1) {
228
+ this.#timing.complete = performance.now() - this.#created
229
+ }
230
+ }
231
+
232
+ return super.destroy(err)
233
+ }
234
+ }
235
+
236
+ export class Http2ServerResponse extends http2.Http2ServerResponse {
237
+ #now = 0
238
+ #created = 0
239
+ #timing = {
240
+ connect: -1,
241
+ headers: -1,
242
+ data: -1,
243
+ complete: -1,
244
+ }
245
+
246
+ get timing() {
247
+ return this.#timing
248
+ }
249
+
250
+ constructor(req) {
251
+ super(req)
252
+
253
+ this.#created = performance.now()
254
+ this.#now = this.#created
255
+ this.#timing.connect = 0
256
+ }
257
+
258
+ flushHeaders() {
259
+ if (!this.destroyed) {
260
+ if (this.#timing.headers === -1) {
261
+ this.#timing.headers = performance.now() - this.#now
262
+ this.#now += this.#timing.headers
263
+ }
264
+ }
265
+ return super.flushHeaders()
266
+ }
267
+
268
+ write(chunk, encoding, callback) {
269
+ if (!this.destroyed) {
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
+
280
+ return super.write(chunk, encoding, callback)
281
+ }
282
+
283
+ end(chunk, encoding, callback) {
284
+ if (!this.destroyed) {
285
+ if (this.#timing.data === -1) {
286
+ this.#timing.data = performance.now() - this.#now
287
+ this.#now += this.#timing.data
288
+ }
289
+
290
+ if (this.#timing.headers === -1) {
291
+ this.#timing.headers = this.#timing.data
292
+ }
293
+
218
294
  this.#timing.complete = performance.now() - this.#created
219
295
  }
220
296
 
297
+ return super.end(chunk, encoding, callback)
298
+ }
299
+
300
+ destroy(err) {
301
+ if (!this.destroyed) {
302
+ if (this.#timing.complete === -1) {
303
+ this.#timing.complete = performance.now() - this.#created
304
+ }
305
+ }
306
+
221
307
  return super.destroy(err)
222
308
  }
223
309
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "20.3.9",
3
+ "version": "20.4.1",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",
package/serializers.js CHANGED
@@ -27,7 +27,6 @@ function getTiming(obj) {
27
27
  return undefined
28
28
  }
29
29
  return {
30
- created: timing.created ?? -1,
31
30
  connect: timing.connect ?? -1,
32
31
  headers: timing.headers ?? -1,
33
32
  data: timing.data ?? -1,