@nxtedition/lib 20.3.0 → 20.3.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.
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,73 @@ 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
+ error: -1,
174
+ }
175
+
176
+ get timing() {
177
+ return this.#timing
178
+ }
179
+
166
180
  constructor(req) {
167
181
  super(req)
168
- this.startTime = performance.now()
169
- this.stats = {
170
- headers: -1,
171
- ttfb: -1,
172
- }
182
+
183
+ this.#created = performance.now()
184
+ this.#now = this.#created
185
+ this.#timing.connect = 0
173
186
  }
174
187
 
175
188
  flushHeaders() {
176
- if (this.stats.headers === -1) {
177
- this.stats.headers = performance.now() - this.startTime
189
+ if (this.#timing.headers === -1) {
190
+ this.#timing.headers = performance.now() - this.#now
191
+ this.#now += this.#timing.headers
178
192
  }
179
193
  return super.flushHeaders()
180
194
  }
181
195
 
182
196
  write(chunk, encoding, callback) {
183
- if (this.stats.ttfb === -1) {
184
- this.stats.ttfb = performance.now() - this.startTime
197
+ if (this.#timing.data === -1) {
198
+ this.#timing.data = performance.now() - this.#now
199
+ this.#now += this.#timing.data
185
200
  }
186
- if (this.stats.headers === -1) {
187
- this.stats.headers = this.stats.ttfb
201
+
202
+ if (this.#timing.headers === -1) {
203
+ this.#timing.headers = this.#timing.data
188
204
  }
205
+
189
206
  return super.write(chunk, encoding, callback)
190
207
  }
191
208
 
192
209
  end(chunk, encoding, callback) {
193
- if (this.stats.ttfb === -1) {
194
- this.stats.ttfb = performance.now() - this.startTime
210
+ if (this.#timing.data === -1) {
211
+ this.#timing.data = performance.now() - this.#now
212
+ this.#now += this.#timing.data
195
213
  }
196
- if (this.stats.headers === -1) {
197
- this.stats.headers = this.stats.ttfb
214
+
215
+ if (this.#timing.headers === -1) {
216
+ this.#timing.headers = this.#timing.data
217
+ }
218
+
219
+ if (this.#timing.complete === -1) {
220
+ this.#timing.complete = performance.now() - this.#created
198
221
  }
222
+
199
223
  return super.end(chunk, encoding, callback)
200
224
  }
225
+
226
+ destroy(err) {
227
+ if (err != null && this.#timing.error === -1) {
228
+ this.#timing.error = performance.now() - this.#created
229
+ }
230
+
231
+ return super.destroy(err)
232
+ }
201
233
  }
202
234
 
203
235
  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.1",
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
  }