@nxtedition/lib 20.3.0 → 20.3.2

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 CHANGED
@@ -1136,10 +1136,17 @@ const defaultDispatcher = new Agent({
1136
1136
  connections: 8,
1137
1137
  })
1138
1138
 
1139
- export function request(url, { dispatcher = defaultDispatcher, signal, ...opts } = {}) {
1140
- url = new URL(url)
1141
- if (url.search) {
1142
- throw new Error('invalid url: ' + url.href)
1139
+ export function request(url, opts) {
1140
+ if (typeof url === 'string') {
1141
+ opts = { url: new URL(url), ...opts }
1142
+ } else if (url instanceof URL) {
1143
+ opts = { url, ...opts }
1144
+ } else if (typeof url.origin === 'string' && typeof (url.path ?? url.pathname) === 'string') {
1145
+ opts = opts ? { ...url, ...opts } : url
1146
+ }
1147
+
1148
+ if (opts == null && typeof url === 'object' && url != null) {
1149
+ opts = url
1143
1150
  }
1144
1151
 
1145
1152
  const ureq = {
@@ -1157,6 +1164,9 @@ export function request(url, { dispatcher = defaultDispatcher, signal, ...opts }
1157
1164
  opts.body != null && typeof opts.body === 'object' ? JSON.stringify(opts.body) : opts.body,
1158
1165
  }
1159
1166
 
1167
+ const dispatcher = opts.dispatcher ?? defaultDispatcher
1168
+ const signal = opts.signal
1169
+
1160
1170
  if (stream) {
1161
1171
  const handler = new StreamOutput({ signal, ...stream })
1162
1172
  dispatch(dispatcher, ureq, handler)
package/http.js CHANGED
@@ -163,41 +163,68 @@ export async function request(ctx, next) {
163
163
  }
164
164
 
165
165
  export class ServerResponse extends http.ServerResponse {
166
+ #now = 0
167
+ #created = 0
168
+ #timing = {
169
+ connect: -1,
170
+ headers: -1,
171
+ data: -1,
172
+ complete: -1,
173
+ }
174
+
175
+ get timing() {
176
+ return this.#timing
177
+ }
178
+
166
179
  constructor(req) {
167
180
  super(req)
168
- this.startTime = performance.now()
169
- this.stats = {
170
- headers: -1,
171
- ttfb: -1,
172
- }
181
+
182
+ this.#created = performance.now()
183
+ this.#now = this.#created
184
+ this.#timing.connect = 0
173
185
  }
174
186
 
175
187
  flushHeaders() {
176
- if (this.stats.headers === -1) {
177
- this.stats.headers = performance.now() - this.startTime
188
+ if (this.#timing.headers === -1) {
189
+ this.#timing.headers = performance.now() - this.#now
190
+ this.#now += this.#timing.headers
178
191
  }
179
192
  return super.flushHeaders()
180
193
  }
181
194
 
182
195
  write(chunk, encoding, callback) {
183
- if (this.stats.ttfb === -1) {
184
- this.stats.ttfb = performance.now() - this.startTime
196
+ if (this.#timing.data === -1) {
197
+ this.#timing.data = performance.now() - this.#now
198
+ this.#now += this.#timing.data
185
199
  }
186
- if (this.stats.headers === -1) {
187
- this.stats.headers = this.stats.ttfb
200
+
201
+ if (this.#timing.headers === -1) {
202
+ this.#timing.headers = this.#timing.data
188
203
  }
204
+
189
205
  return super.write(chunk, encoding, callback)
190
206
  }
191
207
 
192
208
  end(chunk, encoding, callback) {
193
- if (this.stats.ttfb === -1) {
194
- this.stats.ttfb = performance.now() - this.startTime
209
+ if (this.#timing.data === -1) {
210
+ this.#timing.data = performance.now() - this.#now
211
+ this.#now += this.#timing.data
195
212
  }
196
- if (this.stats.headers === -1) {
197
- this.stats.headers = this.stats.ttfb
213
+
214
+ if (this.#timing.headers === -1) {
215
+ this.#timing.headers = this.#timing.data
198
216
  }
217
+
199
218
  return super.end(chunk, encoding, callback)
200
219
  }
220
+
221
+ destroy(err) {
222
+ if (this.#timing.complete === -1) {
223
+ this.#timing.complete = performance.now() - this.#created
224
+ }
225
+
226
+ return super.destroy(err)
227
+ }
201
228
  }
202
229
 
203
230
  export function createServer(options, ctx, middleware) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "20.3.0",
3
+ "version": "20.3.2",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",
package/serializers.js CHANGED
@@ -21,19 +21,25 @@ function getHeaders(obj) {
21
21
  )
22
22
  }
23
23
 
24
+ function getTiming(obj) {
25
+ const timing = obj?.timing ?? obj.stats
26
+ if (!timing) {
27
+ return undefined
28
+ }
29
+ return {
30
+ created: timing.created ?? -1,
31
+ connect: timing.connect ?? -1,
32
+ headers: timing.headers ?? -1,
33
+ data: timing.data ?? -1,
34
+ complete: timing.complete ?? -1,
35
+ error: timing.error ?? -1,
36
+ }
37
+ }
38
+
24
39
  export default {
25
40
  data: (data) =>
26
41
  data != null && typeof data === 'object' ? JSON.stringify(data, undefined, 2) : data,
27
42
  err: (err) => errSerializer(err),
28
- res: (res) =>
29
- res && {
30
- id: res.id || res.req?.id || getHeader(res, 'request-id') || getHeader(res.req, 'request-id'),
31
- stats: res.stats,
32
- statusCode: res.statusCode || res.status,
33
- bytesWritten: res.bytesWritten,
34
- headers: getHeaders(res),
35
- headersSent: res.headersSent,
36
- },
37
43
  socket: (socket) =>
38
44
  socket && {
39
45
  id: socket.id || null,
@@ -43,21 +49,35 @@ export default {
43
49
  remoteAddress: socket.remoteAddress ?? null,
44
50
  headers: socket.headers,
45
51
  },
52
+ res: (res) =>
53
+ res && {
54
+ id: res.id || res.req?.id || getHeader(res, 'request-id') || getHeader(res.req, 'request-id'),
55
+ timing: getTiming(res),
56
+ statusCode: res.statusCode || res.status,
57
+ bytesWritten: res.bytesWritten,
58
+ bytesWrittenPerSecond: res.bytesWrittenPerSecond,
59
+ headers: getHeaders(res),
60
+ headersSent: res.headersSent,
61
+ },
46
62
  req: (req) =>
47
63
  req && {
48
64
  id: req.id || getHeader(req, 'request-id'),
65
+ timing: getTiming(req),
49
66
  method: req.method,
50
67
  url: req.url,
51
68
  headers: getHeaders(req),
52
69
  bytesRead: req.bytesRead,
70
+ bytesReadPerSecond: req.bytesReadPerSecond,
53
71
  remoteAddress: req.socket?.remoteAddress,
54
72
  remotePort: req.socket?.remotePort,
55
73
  },
56
74
  ures: (ures) =>
57
75
  ures && {
58
76
  id: ures.id || getHeader(ures, 'request-id') || getHeader(ures.req, 'request-id'),
77
+ timing: getTiming(ures),
59
78
  statusCode: ures.statusCode ?? ures.status,
60
79
  bytesRead: ures.bytesRead,
80
+ bytesReadPerSecond: ures.bytesReadPerSecond,
61
81
  body: typeof ures.body === 'string' ? ures.body : null,
62
82
  headers: getHeaders(ures),
63
83
  },
@@ -80,10 +100,12 @@ export default {
80
100
 
81
101
  return {
82
102
  id: ureq.id || getHeader(ureq, 'request-id'),
103
+ timing: getTiming(ureq),
83
104
  method: ureq.method,
84
105
  url,
85
106
  body: typeof ureq.body === 'string' ? ureq.body : null,
86
107
  bytesWritten: ureq.bytesWritten,
108
+ bytesWrittenPerSecond: ureq.bytesWrittenPerSecond,
87
109
  headers: getHeaders(ureq),
88
110
  query: ureq.query,
89
111
  }