@nxtedition/nxt-undici 1.1.0 → 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)
@@ -217,12 +219,6 @@ async function request(url, opts) {
217
219
  dispatcherCache.set(dispatcher, dispatch)
218
220
  }
219
221
 
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
222
  return new Promise((resolve, reject) =>
227
223
  dispatch(
228
224
  {
@@ -239,6 +235,7 @@ async function request(url, opts) {
239
235
  headersTimeout: opts.headersTimeout,
240
236
  bodyTimeout: opts.bodyTimeout,
241
237
  idempotent,
238
+ dump: opts.dump ?? 'abort',
242
239
  signal: opts.signal,
243
240
  retry: opts.retry ?? 8,
244
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,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)
@@ -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.0",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "main": "lib/index.js",