@nxtedition/nxt-undici 1.0.13 → 1.2.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/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'),
@@ -202,7 +203,8 @@ async function request(url, opts) {
202
203
  if (dispatch == null) {
203
204
  dispatch = (opts, handler) => dispatcher.dispatch(opts, handler)
204
205
  dispatch = dispatchers.catch(dispatch)
205
- dispatch = dispatchers.abort(dispatch)
206
+ dispatch = dispatchers.requestBody(dispatch)
207
+ dispatch = dispatchers.dump(dispatch)
206
208
  dispatch = dispatchers.requestId(dispatch)
207
209
  dispatch = dispatchers.log(dispatch)
208
210
  dispatch = dispatchers.responseRetry(dispatch)
@@ -229,9 +231,11 @@ async function request(url, opts) {
229
231
  path: url.path ?? (url.search ? `${url.pathname}${url.search ?? ''}` : url.pathname),
230
232
  query: opts.query,
231
233
  reset: opts.reset ?? false,
234
+ blocking: opts.blocking ?? false,
232
235
  headersTimeout: opts.headersTimeout,
233
236
  bodyTimeout: opts.bodyTimeout,
234
237
  idempotent,
238
+ dump: opts.dump ?? 'abort',
235
239
  signal: opts.signal,
236
240
  retry: opts.retry ?? 8,
237
241
  proxy: opts.proxy,
@@ -1,16 +1,25 @@
1
1
  const { AbortError } = require('../utils')
2
2
 
3
+ // TODO (fix): Configurable timeout
4
+ // TODO (fix): Dump?
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,14 +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
- module.exports = (dispatch) => (opts, handler) => dispatch(opts, new Handler(opts, { handler }))
85
+ module.exports = (dispatch) => (opts, handler) =>
86
+ opts.dump ? dispatch(opts, new Handler(opts, { handler })) : dispatch(opts, handler)
@@ -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.0.13",
3
+ "version": "1.2.0",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "main": "lib/index.js",