@nxtedition/nxt-undici 1.1.0 → 1.2.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/lib/index.js CHANGED
@@ -126,7 +126,8 @@ class Readable extends stream.Readable {
126
126
  }
127
127
 
128
128
  const dispatchers = {
129
- abort: require('./interceptor/abort.js'),
129
+ requestBody: require('./interceptor/request-body.js'),
130
+ dump: require('./interceptor/dump.js'),
130
131
  catch: require('./interceptor/catch.js'),
131
132
  responseContent: require('./interceptor/response-content.js'),
132
133
  requestContent: require('./interceptor/request-content.js'),
@@ -142,6 +143,8 @@ const dispatchers = {
142
143
  }
143
144
 
144
145
  async function request(url, opts) {
146
+ // TODO (fix): More argument validation...
147
+
145
148
  if (typeof url === 'string') {
146
149
  url = new URL(url)
147
150
  } else if (url instanceof URL) {
@@ -162,7 +165,8 @@ async function request(url, opts) {
162
165
  let headers
163
166
  if (Array.isArray(opts.headers)) {
164
167
  headers = parseHeaders(opts.headers)
165
- } else {
168
+ } else if (opts.headers != null) {
169
+ // TODO (fix): Object.values(opts.headers)?
166
170
  headers = opts.headers
167
171
  }
168
172
 
@@ -202,7 +206,8 @@ async function request(url, opts) {
202
206
  if (dispatch == null) {
203
207
  dispatch = (opts, handler) => dispatcher.dispatch(opts, handler)
204
208
  dispatch = dispatchers.catch(dispatch)
205
- dispatch = dispatchers.abort(dispatch)
209
+ dispatch = dispatchers.requestBody(dispatch)
210
+ dispatch = dispatchers.dump(dispatch)
206
211
  dispatch = dispatchers.requestId(dispatch)
207
212
  dispatch = dispatchers.log(dispatch)
208
213
  dispatch = dispatchers.responseRetry(dispatch)
@@ -217,12 +222,6 @@ async function request(url, opts) {
217
222
  dispatcherCache.set(dispatcher, dispatch)
218
223
  }
219
224
 
220
- // TODO (fix): retry support
221
- let body = opts.body
222
- if (typeof body === 'function') {
223
- body = await opts.body({ signal: opts.signal })
224
- }
225
-
226
225
  return new Promise((resolve, reject) =>
227
226
  dispatch(
228
227
  {
@@ -239,6 +238,7 @@ async function request(url, opts) {
239
238
  headersTimeout: opts.headersTimeout,
240
239
  bodyTimeout: opts.bodyTimeout,
241
240
  idempotent,
241
+ dump: opts.dump ?? {},
242
242
  signal: opts.signal,
243
243
  retry: opts.retry ?? 8,
244
244
  proxy: opts.proxy,
@@ -1,16 +1,25 @@
1
1
  const { AbortError } = require('../utils')
2
2
 
3
+ // TODO (fix): Configrable max body size
4
+ // TODO (fix): Dump true?
5
+
3
6
  class Handler {
4
7
  constructor(opts, { handler }) {
8
+ this.opts = opts
5
9
  this.handler = handler
6
10
  this.pos = 0
7
11
  this.reason = null
12
+ this.timeout = null
8
13
  }
9
14
 
10
15
  onConnect(abort) {
11
16
  this.abort = abort
12
17
  this.handler.onConnect((reason) => {
13
18
  this.reason = reason ?? new AbortError()
19
+ this.timeout = setTimeout(() => {
20
+ this.timeout = null
21
+ this.abort(this.reason)
22
+ }, this.opts.dump?.timeout ?? 10e3)
14
23
  })
15
24
  }
16
25
 
@@ -56,17 +65,22 @@ class Handler {
56
65
  }
57
66
 
58
67
  onComplete(rawTrailers) {
59
- return this.reason == null
60
- ? this.handler.onComplete(rawTrailers)
61
- : this.handler.onError(this.reason)
68
+ return this.onFinally(this.reason, rawTrailers)
62
69
  }
63
70
 
64
71
  onError(err) {
65
- return this.handler.onError(err)
72
+ return this.onFinally(err)
73
+ }
74
+
75
+ onFinally(err, rawTrailers) {
76
+ if (this.timeout) {
77
+ clearTimeout(this.timeout)
78
+ this.timeout = null
79
+ }
80
+
81
+ return err == null ? this.handler.onComplete(rawTrailers) : this.handler.onError(err)
66
82
  }
67
83
  }
68
84
 
69
85
  module.exports = (dispatch) => (opts, handler) =>
70
- opts.method === 'GET' || opts.method === 'HEAD'
71
- ? dispatch(opts, new Handler(opts, { handler }))
72
- : dispatch(opts, handler)
86
+ opts.dump ? dispatch(opts, new Handler(opts, { handler })) : dispatch(opts, handler)
@@ -42,7 +42,7 @@ class Handler {
42
42
  this.logger.debug(
43
43
  {
44
44
  ures: { statusCode, headers: parseHeaders(rawHeaders) },
45
- elapsedTime: this.startTime - performance.now(),
45
+ elapsedTime: performance.now() - this.startTime,
46
46
  },
47
47
  'upstream request response',
48
48
  )
@@ -56,7 +56,7 @@ class Handler {
56
56
 
57
57
  onComplete(rawTrailers) {
58
58
  this.logger.debug(
59
- { bytesRead: this.pos, elapsedTime: this.startTime - performance.now() },
59
+ { bytesRead: this.pos, elapsedTime: performance.now() - this.startTime },
60
60
  'upstream request completed',
61
61
  )
62
62
  return this.handler.onComplete(rawTrailers)
@@ -65,12 +65,12 @@ class Handler {
65
65
  onError(err) {
66
66
  if (this.aborted) {
67
67
  this.logger.debug(
68
- { bytesRead: this.pos, elapsedTime: this.startTime - performance.now(), err },
68
+ { bytesRead: this.pos, elapsedTime: performance.now() - this.startTime, err },
69
69
  'upstream request aborted',
70
70
  )
71
71
  } else {
72
72
  this.logger.error(
73
- { bytesRead: this.pos, elapsedTime: this.startTime - performance.now(), err },
73
+ { bytesRead: this.pos, elapsedTime: performance.now() - this.startTime, err },
74
74
  'upstream request failed',
75
75
  )
76
76
  }
@@ -0,0 +1,7 @@
1
+ module.exports = (dispatch) => (opts, handler) =>
2
+ typeof opts.body !== 'function'
3
+ ? dispatch(opts, handler)
4
+ : Promise.resolve(opts.body({ signal: opts.signal })).then(
5
+ (body) => dispatch({ ...opts, body }, handler),
6
+ (err) => handler.onError(err),
7
+ )
package/lib/utils.js CHANGED
@@ -1,7 +1,12 @@
1
1
  const tp = require('node:timers/promises')
2
2
 
3
3
  function isDisturbed(body) {
4
- if (body == null || typeof body === 'string' || Buffer.isBuffer(body)) {
4
+ if (
5
+ body == null ||
6
+ typeof body === 'string' ||
7
+ Buffer.isBuffer(body) ||
8
+ typeof body === 'function'
9
+ ) {
5
10
  return false
6
11
  }
7
12
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/nxt-undici",
3
- "version": "1.1.0",
3
+ "version": "1.2.1",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "main": "lib/index.js",