Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9962459

Browse files
nodejs-github-bottargos
authored andcommittedMay 2, 2023
deps: update undici to 5.22.0
PR-URL: #47679 Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com> Reviewed-By: Debadree Chatterjee <debadree333@gmail.com> Reviewed-By: Matthew Aitken <maitken033380023@gmail.com>
1 parent f3ee312 commit 9962459

19 files changed

+235
-182
lines changed
 

‎deps/undici/src/lib/api/api-request.js

+22-45
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
const Readable = require('./readable')
44
const {
55
InvalidArgumentError,
6-
RequestAbortedError,
7-
ResponseStatusCodeError
6+
RequestAbortedError
87
} = require('../core/errors')
98
const util = require('../core/util')
9+
const { getResolveErrorBodyCallback } = require('./util')
1010
const { AsyncResource } = require('async_hooks')
1111
const { addSignal, removeSignal } = require('./abort-signal')
1212

@@ -16,13 +16,17 @@ class RequestHandler extends AsyncResource {
1616
throw new InvalidArgumentError('invalid opts')
1717
}
1818

19-
const { signal, method, opaque, body, onInfo, responseHeaders, throwOnError } = opts
19+
const { signal, method, opaque, body, onInfo, responseHeaders, throwOnError, highWaterMark } = opts
2020

2121
try {
2222
if (typeof callback !== 'function') {
2323
throw new InvalidArgumentError('invalid callback')
2424
}
2525

26+
if (highWaterMark && (typeof highWaterMark !== 'number' || highWaterMark < 0)) {
27+
throw new InvalidArgumentError('invalid highWaterMark')
28+
}
29+
2630
if (signal && typeof signal.on !== 'function' && typeof signal.addEventListener !== 'function') {
2731
throw new InvalidArgumentError('signal must be an EventEmitter or EventTarget')
2832
}
@@ -53,6 +57,7 @@ class RequestHandler extends AsyncResource {
5357
this.context = null
5458
this.onInfo = onInfo || null
5559
this.throwOnError = throwOnError
60+
this.highWaterMark = highWaterMark
5661

5762
if (util.isStream(body)) {
5863
body.on('error', (err) => {
@@ -73,40 +78,39 @@ class RequestHandler extends AsyncResource {
7378
}
7479

7580
onHeaders (statusCode, rawHeaders, resume, statusMessage) {
76-
const { callback, opaque, abort, context } = this
81+
const { callback, opaque, abort, context, responseHeaders, highWaterMark } = this
82+
83+
const headers = responseHeaders === 'raw' ? util.parseRawHeaders(rawHeaders) : util.parseHeaders(rawHeaders)
7784

7885
if (statusCode < 200) {
7986
if (this.onInfo) {
80-
const headers = this.responseHeaders === 'raw' ? util.parseRawHeaders(rawHeaders) : util.parseHeaders(rawHeaders)
8187
this.onInfo({ statusCode, headers })
8288
}
8389
return
8490
}
8591

86-
const parsedHeaders = util.parseHeaders(rawHeaders)
92+
const parsedHeaders = responseHeaders === 'raw' ? util.parseHeaders(rawHeaders) : headers
8793
const contentType = parsedHeaders['content-type']
88-
const body = new Readable(resume, abort, contentType)
94+
const body = new Readable({ resume, abort, contentType, highWaterMark })
8995

9096
this.callback = null
9197
this.res = body
92-
const headers = this.responseHeaders === 'raw' ? util.parseRawHeaders(rawHeaders) : util.parseHeaders(rawHeaders)
9398

9499
if (callback !== null) {
95100
if (this.throwOnError && statusCode >= 400) {
96101
this.runInAsyncScope(getResolveErrorBodyCallback, null,
97102
{ callback, body, contentType, statusCode, statusMessage, headers }
98103
)
99-
return
104+
} else {
105+
this.runInAsyncScope(callback, null, null, {
106+
statusCode,
107+
headers,
108+
trailers: this.trailers,
109+
opaque,
110+
body,
111+
context
112+
})
100113
}
101-
102-
this.runInAsyncScope(callback, null, null, {
103-
statusCode,
104-
headers,
105-
trailers: this.trailers,
106-
opaque,
107-
body,
108-
context
109-
})
110114
}
111115
}
112116

@@ -153,33 +157,6 @@ class RequestHandler extends AsyncResource {
153157
}
154158
}
155159

156-
async function getResolveErrorBodyCallback ({ callback, body, contentType, statusCode, statusMessage, headers }) {
157-
if (statusCode === 204 || !contentType) {
158-
body.dump()
159-
process.nextTick(callback, new ResponseStatusCodeError(`Response status code ${statusCode}${statusMessage ? `: ${statusMessage}` : ''}`, statusCode, headers))
160-
return
161-
}
162-
163-
try {
164-
if (contentType.startsWith('application/json')) {
165-
const payload = await body.json()
166-
process.nextTick(callback, new ResponseStatusCodeError(`Response status code ${statusCode}${statusMessage ? `: ${statusMessage}` : ''}`, statusCode, headers, payload))
167-
return
168-
}
169-
170-
if (contentType.startsWith('text/')) {
171-
const payload = await body.text()
172-
process.nextTick(callback, new ResponseStatusCodeError(`Response status code ${statusCode}${statusMessage ? `: ${statusMessage}` : ''}`, statusCode, headers, payload))
173-
return
174-
}
175-
} catch (err) {
176-
// Process in a fallback if error
177-
}
178-
179-
body.dump()
180-
process.nextTick(callback, new ResponseStatusCodeError(`Response status code ${statusCode}${statusMessage ? `: ${statusMessage}` : ''}`, statusCode, headers))
181-
}
182-
183160
function request (opts, callback) {
184161
if (callback === undefined) {
185162
return new Promise((resolve, reject) => {

‎deps/undici/src/lib/api/api-stream.js

+46-57
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ const { finished, PassThrough } = require('stream')
44
const {
55
InvalidArgumentError,
66
InvalidReturnValueError,
7-
RequestAbortedError,
8-
ResponseStatusCodeError
7+
RequestAbortedError
98
} = require('../core/errors')
109
const util = require('../core/util')
10+
const { getResolveErrorBodyCallback } = require('./util')
1111
const { AsyncResource } = require('async_hooks')
1212
const { addSignal, removeSignal } = require('./abort-signal')
1313

@@ -79,77 +79,66 @@ class StreamHandler extends AsyncResource {
7979
}
8080

8181
onHeaders (statusCode, rawHeaders, resume, statusMessage) {
82-
const { factory, opaque, context, callback } = this
82+
const { factory, opaque, context, callback, responseHeaders } = this
83+
84+
const headers = responseHeaders === 'raw' ? util.parseRawHeaders(rawHeaders) : util.parseHeaders(rawHeaders)
8385

8486
if (statusCode < 200) {
8587
if (this.onInfo) {
86-
const headers = this.responseHeaders === 'raw' ? util.parseRawHeaders(rawHeaders) : util.parseHeaders(rawHeaders)
8788
this.onInfo({ statusCode, headers })
8889
}
8990
return
9091
}
9192

9293
this.factory = null
93-
const headers = this.responseHeaders === 'raw' ? util.parseRawHeaders(rawHeaders) : util.parseHeaders(rawHeaders)
94-
const res = this.runInAsyncScope(factory, null, {
95-
statusCode,
96-
headers,
97-
opaque,
98-
context
99-
})
10094

101-
if (this.throwOnError && statusCode >= 400) {
102-
const headers = this.responseHeaders === 'raw' ? util.parseRawHeaders(rawHeaders) : util.parseHeaders(rawHeaders)
103-
const chunks = []
104-
const pt = new PassThrough()
105-
pt
106-
.on('data', (chunk) => chunks.push(chunk))
107-
.on('end', () => {
108-
const payload = Buffer.concat(chunks).toString('utf8')
109-
this.runInAsyncScope(
110-
callback,
111-
null,
112-
new ResponseStatusCodeError(
113-
`Response status code ${statusCode}${statusMessage ? `: ${statusMessage}` : ''}`,
114-
statusCode,
115-
headers,
116-
payload
117-
)
118-
)
119-
})
120-
.on('error', (err) => {
121-
this.onError(err)
122-
})
123-
this.res = pt
124-
return
125-
}
95+
let res
12696

127-
if (
128-
!res ||
129-
typeof res.write !== 'function' ||
130-
typeof res.end !== 'function' ||
131-
typeof res.on !== 'function'
132-
) {
133-
throw new InvalidReturnValueError('expected Writable')
134-
}
97+
if (this.throwOnError && statusCode >= 400) {
98+
const parsedHeaders = responseHeaders === 'raw' ? util.parseHeaders(rawHeaders) : headers
99+
const contentType = parsedHeaders['content-type']
100+
res = new PassThrough()
135101

136-
res.on('drain', resume)
137-
// TODO: Avoid finished. It registers an unnecessary amount of listeners.
138-
finished(res, { readable: false }, (err) => {
139-
const { callback, res, opaque, trailers, abort } = this
102+
this.callback = null
103+
this.runInAsyncScope(getResolveErrorBodyCallback, null,
104+
{ callback, body: res, contentType, statusCode, statusMessage, headers }
105+
)
106+
} else {
107+
res = this.runInAsyncScope(factory, null, {
108+
statusCode,
109+
headers,
110+
opaque,
111+
context
112+
})
140113

141-
this.res = null
142-
if (err || !res.readable) {
143-
util.destroy(res, err)
114+
if (
115+
!res ||
116+
typeof res.write !== 'function' ||
117+
typeof res.end !== 'function' ||
118+
typeof res.on !== 'function'
119+
) {
120+
throw new InvalidReturnValueError('expected Writable')
144121
}
145122

146-
this.callback = null
147-
this.runInAsyncScope(callback, null, err || null, { opaque, trailers })
123+
// TODO: Avoid finished. It registers an unnecessary amount of listeners.
124+
finished(res, { readable: false }, (err) => {
125+
const { callback, res, opaque, trailers, abort } = this
148126

149-
if (err) {
150-
abort()
151-
}
152-
})
127+
this.res = null
128+
if (err || !res.readable) {
129+
util.destroy(res, err)
130+
}
131+
132+
this.callback = null
133+
this.runInAsyncScope(callback, null, err || null, { opaque, trailers })
134+
135+
if (err) {
136+
abort()
137+
}
138+
})
139+
}
140+
141+
res.on('drain', resume)
153142

154143
this.res = res
155144

‎deps/undici/src/lib/api/readable.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@ const kAbort = Symbol('abort')
1717
const kContentType = Symbol('kContentType')
1818

1919
module.exports = class BodyReadable extends Readable {
20-
constructor (resume, abort, contentType = '') {
20+
constructor ({
21+
resume,
22+
abort,
23+
contentType = '',
24+
highWaterMark = 64 * 1024 // Same as nodejs fs streams.
25+
}) {
2126
super({
2227
autoDestroy: true,
2328
read: resume,
24-
highWaterMark: 64 * 1024 // Same as nodejs fs streams.
29+
highWaterMark
2530
})
2631

2732
this._readableState.dataEmitted = false

‎deps/undici/src/lib/api/util.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const assert = require('assert')
2+
const {
3+
ResponseStatusCodeError
4+
} = require('../core/errors')
5+
const { toUSVString } = require('../core/util')
6+
7+
async function getResolveErrorBodyCallback ({ callback, body, contentType, statusCode, statusMessage, headers }) {
8+
assert(body)
9+
10+
let chunks = []
11+
let limit = 0
12+
13+
for await (const chunk of body) {
14+
chunks.push(chunk)
15+
limit += chunk.length
16+
if (limit > 128 * 1024) {
17+
chunks = null
18+
break
19+
}
20+
}
21+
22+
if (statusCode === 204 || !contentType || !chunks) {
23+
process.nextTick(callback, new ResponseStatusCodeError(`Response status code ${statusCode}${statusMessage ? `: ${statusMessage}` : ''}`, statusCode, headers))
24+
return
25+
}
26+
27+
try {
28+
if (contentType.startsWith('application/json')) {
29+
const payload = JSON.parse(toUSVString(Buffer.concat(chunks)))
30+
process.nextTick(callback, new ResponseStatusCodeError(`Response status code ${statusCode}${statusMessage ? `: ${statusMessage}` : ''}`, statusCode, headers, payload))
31+
return
32+
}
33+
34+
if (contentType.startsWith('text/')) {
35+
const payload = toUSVString(Buffer.concat(chunks))
36+
process.nextTick(callback, new ResponseStatusCodeError(`Response status code ${statusCode}${statusMessage ? `: ${statusMessage}` : ''}`, statusCode, headers, payload))
37+
return
38+
}
39+
} catch (err) {
40+
// Process in a fallback if error
41+
}
42+
43+
process.nextTick(callback, new ResponseStatusCodeError(`Response status code ${statusCode}${statusMessage ? `: ${statusMessage}` : ''}`, statusCode, headers))
44+
}
45+
46+
module.exports = { getResolveErrorBodyCallback }

0 commit comments

Comments
 (0)