@nxtedition/nxt-undici 2.0.17 → 2.0.19

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
@@ -2,7 +2,7 @@ import assert from 'node:assert'
2
2
  import stream from 'node:stream'
3
3
  import createError from 'http-errors'
4
4
  import undici from 'undici'
5
- import { findHeader, parseHeaders, AbortError } from './utils.js'
5
+ import { findHeader, parseHeaders, AbortError, isStream } from './utils.js'
6
6
  import CacheableLookup from 'cacheable-lookup'
7
7
 
8
8
  const dispatcherCache = new WeakMap()
@@ -78,7 +78,10 @@ class Readable extends stream.Readable {
78
78
  this[kHandler].signal = null
79
79
  }
80
80
 
81
- callback(err)
81
+ // Workaround: https://github.com/nodejs/undici/pull/2497
82
+ queueMicrotask(() => {
83
+ callback(err)
84
+ })
82
85
  }
83
86
 
84
87
  async text() {
@@ -117,7 +120,8 @@ class Readable extends stream.Readable {
117
120
 
118
121
  dump() {
119
122
  return new Promise((resolve) => {
120
- if (this.closed) {
123
+ const rState = this._readableState
124
+ if (rState.closeEmitted) {
121
125
  resolve(null)
122
126
  } else {
123
127
  let n = 0
@@ -137,7 +141,6 @@ class Readable extends stream.Readable {
137
141
 
138
142
  const dispatchers = {
139
143
  responseError: (await import('./interceptor/response-error.js')).default,
140
- requestBody: (await import('./interceptor/request-body.js')).default,
141
144
  requestBodyFactory: (await import('./interceptor/request-body-factory.js')).default,
142
145
  responseContent: (await import('./interceptor/response-content.js')).default,
143
146
  requestContent: (await import('./interceptor/request-content.js')).default,
@@ -225,6 +228,11 @@ export async function request(url, opts) {
225
228
  delete headers['content-length']
226
229
  }
227
230
 
231
+ if (isStream(opts.body)) {
232
+ // Workaround: https://github.com/nodejs/undici/pull/2497
233
+ opts.body.on('error', () => {})
234
+ }
235
+
228
236
  const dispatcher = opts.dispatcher ?? defaultDispatcher
229
237
 
230
238
  let dispatch = dispatcherCache.get(dispatcher)
@@ -241,7 +249,6 @@ export async function request(url, opts) {
241
249
  dispatch = dispatchers.cache(dispatch)
242
250
  dispatch = dispatchers.redirect(dispatch)
243
251
  dispatch = dispatchers.proxy(dispatch)
244
- dispatch = dispatchers.requestBody(dispatch)
245
252
  dispatcherCache.set(dispatcher, dispatch)
246
253
  }
247
254
 
@@ -100,7 +100,7 @@ class Handler {
100
100
  )
101
101
  } else {
102
102
  this.logger.error(
103
- { bytesRead: this.pos, elapsedTime: this.stats.end, err },
103
+ { ureq: this.opts, bytesRead: this.pos, elapsedTime: this.stats.end, err },
104
104
  'upstream request failed',
105
105
  )
106
106
  }
@@ -10,8 +10,15 @@ export default (dispatch) => (opts, handler) => {
10
10
 
11
11
  if (typeof body?.then === 'function') {
12
12
  body.then(
13
- (body) => dispatch({ ...opts, body }, handler),
14
- (err) => handler.onError(err),
13
+ (body) => {
14
+ // Workaround: https://github.com/nodejs/undici/pull/2497
15
+ body.on('error', () => {})
16
+
17
+ dispatch({ ...opts, body }, handler)
18
+ },
19
+ (err) => {
20
+ handler.onError(err)
21
+ },
15
22
  )
16
23
  } else {
17
24
  dispatch({ ...opts, body }, handler)
@@ -31,20 +31,20 @@ class Handler {
31
31
  onRequestSent() {
32
32
  const hash = this.hasher?.digest('base64')
33
33
 
34
- if (this.md5 != null && hash !== this.md5) {
34
+ if (this.length != null && this.pos !== Number(this.length)) {
35
35
  this.abort(
36
36
  Object.assign(new Error('Request Content-Length mismatch'), {
37
- expected: this.md5,
38
- actual: hash,
37
+ expected: Number(this.length),
38
+ actual: this.pos,
39
39
  }),
40
40
  )
41
41
  }
42
42
 
43
- if (this.length != null && this.pos !== Number(this.length)) {
43
+ if (this.md5 != null && hash !== this.md5) {
44
44
  this.abort(
45
- Object.assign(new Error('Request Content-Length mismatch'), {
46
- expected: Number(this.length),
47
- actual: this.pos,
45
+ Object.assign(new Error('Request Content-MD5 mismatch'), {
46
+ expected: this.md5,
47
+ actual: hash,
48
48
  }),
49
49
  )
50
50
  }
@@ -44,15 +44,6 @@ class Handler {
44
44
  onComplete(rawTrailers) {
45
45
  const hash = this.hasher?.digest('base64')
46
46
 
47
- if (this.md5 != null && hash !== this.md5) {
48
- return this.handler.onError(
49
- Object.assign(new Error('Request Content-Length mismatch'), {
50
- expected: this.md5,
51
- actual: hash,
52
- }),
53
- )
54
- }
55
-
56
47
  if (this.length != null && this.pos !== Number(this.length)) {
57
48
  return this.handler.onError(
58
49
  Object.assign(new Error('Request Content-Length mismatch'), {
@@ -60,9 +51,16 @@ class Handler {
60
51
  actual: this.pos,
61
52
  }),
62
53
  )
54
+ } else if (this.md5 != null && hash !== this.md5) {
55
+ return this.handler.onError(
56
+ Object.assign(new Error('Request Content-MD5 mismatch'), {
57
+ expected: this.md5,
58
+ actual: hash,
59
+ }),
60
+ )
61
+ } else {
62
+ return this.handler.onComplete(rawTrailers)
63
63
  }
64
-
65
- return this.handler.onComplete(rawTrailers)
66
64
  }
67
65
 
68
66
  onError(err) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/nxt-undici",
3
- "version": "2.0.17",
3
+ "version": "2.0.19",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "main": "lib/index.js",
@@ -9,22 +9,22 @@
9
9
  "lib/*"
10
10
  ],
11
11
  "dependencies": {
12
- "cache-control-parser": "^2.0.4",
12
+ "cache-control-parser": "^2.0.5",
13
13
  "cacheable-lookup": "^7.0.0",
14
14
  "http-errors": "^2.0.0",
15
- "lru-cache": "^10.0.1",
16
- "undici": "^5.27.0"
15
+ "lru-cache": "^10.1.0",
16
+ "undici": "^6.0.0"
17
17
  },
18
18
  "devDependencies": {
19
- "@types/node": "^20.9.1",
20
- "eslint": "^8.54.0",
21
- "eslint-config-prettier": "^9.0.0",
19
+ "@types/node": "^20.10.3",
20
+ "eslint": "^8.55.0",
21
+ "eslint-config-prettier": "^9.1.0",
22
22
  "eslint-config-standard": "^17.0.0",
23
23
  "eslint-plugin-import": "^2.29.0",
24
24
  "eslint-plugin-n": "^16.2.0",
25
25
  "eslint-plugin-promise": "^6.1.1",
26
26
  "husky": "^8.0.3",
27
- "lint-staged": "^15.0.2",
27
+ "lint-staged": "^15.2.0",
28
28
  "pinst": "^3.0.0",
29
29
  "prettier": "^3.0.3",
30
30
  "tap": "^18.6.1"
@@ -1,72 +0,0 @@
1
- import { isStream } from '../utils.js'
2
-
3
- class Handler {
4
- constructor(opts, { handler }) {
5
- this.handler = handler
6
- this.body = opts.body
7
- this.error = null
8
- this.abort = null
9
-
10
- this.errorHandler = (err) => {
11
- if (this.abort) {
12
- this.abort(err)
13
- } else {
14
- this.error = err
15
- }
16
- }
17
-
18
- this.body.on('error', this.errorHandler)
19
- }
20
-
21
- onConnect(abort) {
22
- if (this.error) {
23
- abort(this.error)
24
- } else {
25
- this.abort = abort
26
- return this.handler.onConnect(abort)
27
- }
28
- }
29
-
30
- onUpgrade(statusCode, rawHeaders, socket, headers) {
31
- return this.handler.onUpgrade(statusCode, rawHeaders, socket, headers)
32
- }
33
-
34
- onBodySent(chunk) {
35
- return this.handler.onBodySent(chunk)
36
- }
37
-
38
- onRequestSent() {
39
- return this.handler.onRequestSent()
40
- }
41
-
42
- onHeaders(statusCode, rawHeaders, resume, statusMessage, headers) {
43
- return this.handler.onHeaders(statusCode, rawHeaders, resume, statusMessage, headers)
44
- }
45
-
46
- onData(chunk) {
47
- return this.handler.onData(chunk)
48
- }
49
-
50
- onComplete(rawTrailers) {
51
- this.body.off('error', this.errorHandler)
52
- return this.handler.onComplete(rawTrailers)
53
- }
54
-
55
- onError(err) {
56
- this.body.off('error', this.errorHandler)
57
- return this.handler.onError(err)
58
- }
59
- }
60
-
61
- export default (dispatch) => (opts, handler) => {
62
- if (isStream(opts.body)) {
63
- if (opts.method === 'GET' || opts.method === 'HEAD') {
64
- opts.body.resume() // dump
65
- return dispatch({ ...opts, body: undefined }, new Handler(opts, { handler }))
66
- } else {
67
- return dispatch(opts, new Handler(opts, { handler }))
68
- }
69
- } else {
70
- return dispatch(opts, handler)
71
- }
72
- }