@nxtedition/nxt-undici 7.0.0 → 7.1.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
@@ -7,6 +7,7 @@ const dispatcherCache = new WeakMap()
7
7
 
8
8
  export const interceptors = {
9
9
  query: (await import('./interceptor/query.js')).default,
10
+ requestBodyFactory: (await import('./interceptor/request-body-factory.js')).default,
10
11
  responseError: (await import('./interceptor/response-error.js')).default,
11
12
  responseRetry: (await import('./interceptor/response-retry.js')).default,
12
13
  responseVerify: (await import('./interceptor/response-verify.js')).default,
@@ -84,6 +85,7 @@ function wrapDispatch(dispatcher) {
84
85
  dispatcher,
85
86
  interceptors.dns(),
86
87
  interceptors.lookup(),
88
+ interceptors.requestBodyFactory(),
87
89
  interceptors.responseRetry(),
88
90
  interceptors.responseVerify(),
89
91
  interceptors.proxy(),
@@ -0,0 +1,64 @@
1
+ import { Readable } from 'node:stream'
2
+ import { isStream } from '../utils.js'
3
+
4
+ class FactoryStream extends Readable {
5
+ #factory
6
+ #ac
7
+ #body
8
+
9
+ constructor(factory) {
10
+ super()
11
+ this.#factory = factory
12
+ }
13
+
14
+ _construct(callback) {
15
+ this.#ac = new AbortController()
16
+ Promise.resolve(this.#factory({ signal: this.#ac.signal })).then(
17
+ (body) => {
18
+ try {
19
+ if (typeof body === 'string' || body instanceof Buffer) {
20
+ this.push(body)
21
+ this.push(null)
22
+ } else if (isStream(body)) {
23
+ this.#body = body
24
+ } else {
25
+ this.#body = Readable.from(body)
26
+ }
27
+
28
+ if (this.#body != null) {
29
+ this.#body
30
+ .on('readable', () => this._read())
31
+ .on('end', () => this.push(null))
32
+ .on('error', (err) => this.destroy(err))
33
+ }
34
+
35
+ callback(null)
36
+ } catch (err) {
37
+ callback(err)
38
+ }
39
+ },
40
+ (err) => callback(err),
41
+ )
42
+ }
43
+
44
+ _read() {
45
+ const chunk = this.#body?.read()
46
+ if (chunk !== null) {
47
+ this.push(chunk)
48
+ }
49
+ }
50
+
51
+ _destroy(err, callback) {
52
+ if (this.#ac != null) {
53
+ this.#ac.abort()
54
+ this.#ac = null
55
+ }
56
+
57
+ callback(err)
58
+ }
59
+ }
60
+
61
+ export default () => (dispatch) => (opts, handler) =>
62
+ typeof opts.body !== 'function'
63
+ ? dispatch(opts, handler)
64
+ : dispatch({ ...opts, body: new FactoryStream(opts.body) }, handler)
package/lib/request.js CHANGED
@@ -16,10 +16,6 @@ export class RequestHandler {
16
16
  throw new InvalidArgumentError('invalid resolve')
17
17
  }
18
18
 
19
- if (typeof body === 'function') {
20
- throw new InvalidArgumentError('body cannot be a function')
21
- }
22
-
23
19
  if (highWaterMark && (typeof highWaterMark !== 'number' || highWaterMark < 0)) {
24
20
  throw new InvalidArgumentError('invalid highWaterMark')
25
21
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/nxt-undici",
3
- "version": "7.0.0",
3
+ "version": "7.1.1",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "main": "lib/index.js",