Skip to content

Commit 7283486

Browse files
nodejs-github-botMoLow
authored andcommitted
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 323881f commit 7283486

19 files changed

Lines changed: 235 additions & 182 deletions

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

Lines changed: 22 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
constReadable=require('./readable')
44
const{
55
InvalidArgumentError,
6-
RequestAbortedError,
7-
ResponseStatusCodeError
6+
RequestAbortedError
87
}=require('../core/errors')
98
constutil=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
thrownewInvalidArgumentError('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(typeofcallback!=='function'){
2323
thrownewInvalidArgumentError('invalid callback')
2424
}
2525

26+
if(highWaterMark&&(typeofhighWaterMark!=='number'||highWaterMark<0)){
27+
thrownewInvalidArgumentError('invalid highWaterMark')
28+
}
29+
2630
if(signal&&typeofsignal.on!=='function'&&typeofsignal.addEventListener!=='function'){
2731
thrownewInvalidArgumentError('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+
constheaders=responseHeaders==='raw' ? util.parseRawHeaders(rawHeaders) : util.parseHeaders(rawHeaders)
7784

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

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

9096
this.callback=null
9197
this.res=body
92-
constheaders=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-
asyncfunctiongetResolveErrorBodyCallback({ callback, body, contentType, statusCode, statusMessage, headers }){
157-
if(statusCode===204||!contentType){
158-
body.dump()
159-
process.nextTick(callback,newResponseStatusCodeError(`Response status code ${statusCode}${statusMessage ? `: ${statusMessage}` : ''}`,statusCode,headers))
160-
return
161-
}
162-
163-
try{
164-
if(contentType.startsWith('application/json')){
165-
constpayload=awaitbody.json()
166-
process.nextTick(callback,newResponseStatusCodeError(`Response status code ${statusCode}${statusMessage ? `: ${statusMessage}` : ''}`,statusCode,headers,payload))
167-
return
168-
}
169-
170-
if(contentType.startsWith('text/')){
171-
constpayload=awaitbody.text()
172-
process.nextTick(callback,newResponseStatusCodeError(`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,newResponseStatusCodeError(`Response status code ${statusCode}${statusMessage ? `: ${statusMessage}` : ''}`,statusCode,headers))
181-
}
182-
183160
functionrequest(opts,callback){
184161
if(callback===undefined){
185162
returnnewPromise((resolve,reject)=>{

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

Lines changed: 46 additions & 57 deletions
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
constutil=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+
constheaders=responseHeaders==='raw' ? util.parseRawHeaders(rawHeaders) : util.parseHeaders(rawHeaders)
8385

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

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

101-
if(this.throwOnError&&statusCode>=400){
102-
constheaders=this.responseHeaders==='raw' ? util.parseRawHeaders(rawHeaders) : util.parseHeaders(rawHeaders)
103-
constchunks=[]
104-
constpt=newPassThrough()
105-
pt
106-
.on('data',(chunk)=>chunks.push(chunk))
107-
.on('end',()=>{
108-
constpayload=Buffer.concat(chunks).toString('utf8')
109-
this.runInAsyncScope(
110-
callback,
111-
null,
112-
newResponseStatusCodeError(
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+
letres
12696

127-
if(
128-
!res||
129-
typeofres.write!=='function'||
130-
typeofres.end!=='function'||
131-
typeofres.on!=='function'
132-
){
133-
thrownewInvalidReturnValueError('expected Writable')
134-
}
97+
if(this.throwOnError&&statusCode>=400){
98+
constparsedHeaders=responseHeaders==='raw' ? util.parseHeaders(rawHeaders) : headers
99+
constcontentType=parsedHeaders['content-type']
100+
res=newPassThrough()
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+
typeofres.write!=='function'||
117+
typeofres.end!=='function'||
118+
typeofres.on!=='function'
119+
){
120+
thrownewInvalidReturnValueError('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‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@ const kAbort = Symbol('abort')
1717
constkContentType=Symbol('kContentType')
1818

1919
module.exports=classBodyReadableextendsReadable{
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‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
constassert=require('assert')
2+
const{
3+
ResponseStatusCodeError
4+
}=require('../core/errors')
5+
const{ toUSVString }=require('../core/util')
6+
7+
asyncfunctiongetResolveErrorBodyCallback({ callback, body, contentType, statusCode, statusMessage, headers }){
8+
assert(body)
9+
10+
letchunks=[]
11+
letlimit=0
12+
13+
forawait(constchunkofbody){
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,newResponseStatusCodeError(`Response status code ${statusCode}${statusMessage ? `: ${statusMessage}` : ''}`,statusCode,headers))
24+
return
25+
}
26+
27+
try{
28+
if(contentType.startsWith('application/json')){
29+
constpayload=JSON.parse(toUSVString(Buffer.concat(chunks)))
30+
process.nextTick(callback,newResponseStatusCodeError(`Response status code ${statusCode}${statusMessage ? `: ${statusMessage}` : ''}`,statusCode,headers,payload))
31+
return
32+
}
33+
34+
if(contentType.startsWith('text/')){
35+
constpayload=toUSVString(Buffer.concat(chunks))
36+
process.nextTick(callback,newResponseStatusCodeError(`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,newResponseStatusCodeError(`Response status code ${statusCode}${statusMessage ? `: ${statusMessage}` : ''}`,statusCode,headers))
44+
}
45+
46+
module.exports={ getResolveErrorBodyCallback }

0 commit comments

Comments
 (0)