Skip to content

Commit 818271c

Browse files
nodejs-github-botruyadorno
authored andcommitted
deps: update undici to 5.8.2
PR-URL: #44187 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Mohammed Keyvanzadeh <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 340ca4d commit 818271c

File tree

7 files changed

+64
-19
lines changed

7 files changed

+64
-19
lines changed

deps/undici/src/docs/api/MockAgent.md

+17
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,23 @@ for await (const data of result2.body) {
177177
console.log('data', data.toString('utf8')) // data hello
178178
}
179179
```
180+
#### Example - Mock different requests within the same file
181+
```js
182+
const { MockAgent, setGlobalDispatcher } = require('undici');
183+
const agent = new MockAgent();
184+
agent.disableNetConnect();
185+
setGlobalDispatcher(agent);
186+
describe('Test', () => {
187+
it('200', async () => {
188+
const mockAgent = agent.get('http://test.com');
189+
// your test
190+
});
191+
it('200', async () => {
192+
const mockAgent = agent.get('http://testing.com');
193+
// your test
194+
});
195+
});
196+
```
180197

181198
#### Example - Mocked request with query body, headers and trailers
182199

deps/undici/src/index.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ function makeDispatcher (fn) {
5353
throw new InvalidArgumentError('invalid opts.path')
5454
}
5555

56-
url = new URL(opts.path, util.parseOrigin(url))
56+
let path = opts.path
57+
if (!opts.path.startsWith('/')) {
58+
path = `/${path}`
59+
}
60+
61+
url = new URL(util.parseOrigin(url).origin + path)
5762
} else {
5863
if (!opts) {
5964
opts = typeof url === 'object' ? url : {}

deps/undici/src/lib/core/request.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,8 @@ function processHeader (request, key, val) {
297297
} else if (
298298
request.contentType === null &&
299299
key.length === 12 &&
300-
key.toLowerCase() === 'content-type'
300+
key.toLowerCase() === 'content-type' &&
301+
headerCharRegex.exec(val) === null
301302
) {
302303
request.contentType = val
303304
request.headers += `${key}: ${val}\r\n`

deps/undici/src/lib/core/util.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,25 @@ function parseURL (url) {
108108
const port = url.port != null
109109
? url.port
110110
: (url.protocol === 'https:' ? 443 : 80)
111-
const origin = url.origin != null
111+
let origin = url.origin != null
112112
? url.origin
113113
: `${url.protocol}//${url.hostname}:${port}`
114-
const path = url.path != null
114+
let path = url.path != null
115115
? url.path
116116
: `${url.pathname || ''}${url.search || ''}`
117117

118-
url = new URL(path, origin)
118+
if (origin.endsWith('/')) {
119+
origin = origin.substring(0, origin.length - 1)
120+
}
121+
122+
if (path && !path.startsWith('/')) {
123+
path = `/${path}`
124+
}
125+
// new URL(path, origin) is unsafe when `path` contains an absolute URL
126+
// From https://developer.mozilla.org/en-US/docs/Web/API/URL/URL:
127+
// If first parameter is a relative URL, second param is required, and will be used as the base URL.
128+
// If first parameter is an absolute URL, a given second param will be ignored.
129+
url = new URL(origin + path)
119130
}
120131

121132
return url

deps/undici/src/lib/fetch/webidl.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -388,10 +388,6 @@ webidl.converters.DOMString = function (V, opts = {}) {
388388
return String(V)
389389
}
390390

391-
// Check for 0 or more characters outside of the latin1 range.
392-
// eslint-disable-next-line no-control-regex
393-
const isLatin1 = /^[\u0000-\u00ff]{0,}$/
394-
395391
// https://webidl.spec.whatwg.org/#es-ByteString
396392
webidl.converters.ByteString = function (V) {
397393
// 1. Let x be ? ToString(V).
@@ -400,8 +396,15 @@ webidl.converters.ByteString = function (V) {
400396

401397
// 2. If the value of any element of x is greater than
402398
// 255, then throw a TypeError.
403-
if (!isLatin1.test(x)) {
404-
throw new TypeError('Argument is not a ByteString')
399+
for (let index = 0; index < x.length; index++) {
400+
const charCode = x.charCodeAt(index)
401+
402+
if (charCode > 255) {
403+
throw new TypeError(
404+
'Cannot convert argument to a ByteString because the character at' +
405+
`index ${index} has a value of ${charCode} which is greater than 255.`
406+
)
407+
}
405408
}
406409

407410
// 3. Return an IDL ByteString value whose length is the

deps/undici/src/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "undici",
3-
"version": "5.8.1",
3+
"version": "5.8.2",
44
"description": "An HTTP/1.1 client, written from scratch for Node.js",
55
"homepage": "https://undici.nodejs.org",
66
"bugs": {

deps/undici/undici.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -737,9 +737,15 @@ var require_util = __commonJS({
737737
}
738738
if (!(url instanceof URL)) {
739739
const port = url.port != null ? url.port : url.protocol === "https:" ? 443 : 80;
740-
const origin = url.origin != null ? url.origin : `${url.protocol}//${url.hostname}:${port}`;
741-
const path = url.path != null ? url.path : `${url.pathname || ""}${url.search || ""}`;
742-
url = new URL(path, origin);
740+
let origin = url.origin != null ? url.origin : `${url.protocol}//${url.hostname}:${port}`;
741+
let path = url.path != null ? url.path : `${url.pathname || ""}${url.search || ""}`;
742+
if (origin.endsWith("/")) {
743+
origin = origin.substring(0, origin.length - 1);
744+
}
745+
if (path && !path.startsWith("/")) {
746+
path = `/${path}`;
747+
}
748+
url = new URL(origin + path);
743749
}
744750
return url;
745751
}
@@ -1281,11 +1287,13 @@ var require_webidl = __commonJS({
12811287
}
12821288
return String(V);
12831289
};
1284-
var isLatin1 = /^[\u0000-\u00ff]{0,}$/;
12851290
webidl.converters.ByteString = function(V) {
12861291
const x = webidl.converters.DOMString(V);
1287-
if (!isLatin1.test(x)) {
1288-
throw new TypeError("Argument is not a ByteString");
1292+
for (let index = 0; index < x.length; index++) {
1293+
const charCode = x.charCodeAt(index);
1294+
if (charCode > 255) {
1295+
throw new TypeError(`Cannot convert argument to a ByteString because the character atindex ${index} has a value of ${charCode} which is greater than 255.`);
1296+
}
12891297
}
12901298
return x;
12911299
};
@@ -2580,7 +2588,7 @@ var require_request = __commonJS({
25802588
if (!Number.isFinite(request.contentLength)) {
25812589
throw new InvalidArgumentError("invalid content-length header");
25822590
}
2583-
} else if (request.contentType === null && key.length === 12 && key.toLowerCase() === "content-type") {
2591+
} else if (request.contentType === null && key.length === 12 && key.toLowerCase() === "content-type" && headerCharRegex.exec(val) === null) {
25842592
request.contentType = val;
25852593
request.headers += `${key}: ${val}\r
25862594
`;

0 commit comments

Comments
 (0)