Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: upgrade to undici v7 #547

Merged
merged 10 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions examples/h2-other-side-closed-exit-0-fetch.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const { fetch, setGlobalDispatcher, Agent } = require('..');

setGlobalDispatcher(new Agent({
allowH2: true,
}));

async function main() {
for (let i = 0; i < 100; i++) {
try {
const r = await fetch('https://edgeupdates.microsoft.com/api/products');
console.log(r.status, r.headers, (await r.text()).length);
} catch (err) {
// console.error(err);
// throw err;
if (err.code === 'UND_ERR_SOCKET') {
continue;
} else {
throw err;
}
fengmk2 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

main().then(() => {
console.log('main end');
}).catch(err => {
console.error('main error throw: %s', err);
// console.error(err);
process.exit(1);
});

process.on('beforeExit', (...args) => {
console.error('beforeExit', args);
});
34 changes: 34 additions & 0 deletions examples/h2-other-side-closed-exit-0.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const { request, Agent, setGlobalDispatcher } = require('undici');

setGlobalDispatcher(new Agent({
allowH2: true,
}));
fengmk2 marked this conversation as resolved.
Show resolved Hide resolved

async function main() {
for (let i = 0; i < 100; i++) {
try {
const r = await request('https://edgeupdates.microsoft.com/api/products');
console.log(r.statusCode, r.headers, (await r.body.blob()).size);
} catch (err) {
// console.error(err);
// throw err;
if (err.code === 'UND_ERR_SOCKET') {
continue;
} else {
throw err;
}
}
}
}
fengmk2 marked this conversation as resolved.
Show resolved Hide resolved

main().then(() => {
console.log('main end');
}).catch(err => {
console.error('main error throw: %s', err);
// console.error(err);
process.exit(1);
});

process.on('beforeExit', (...args) => {
console.error('beforeExit', args);
});
fengmk2 marked this conversation as resolved.
Show resolved Hide resolved
49 changes: 49 additions & 0 deletions examples/longruning.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const { HttpClient } = require('..');

const httpClient = new HttpClient({
allowH2: true,
});

async function main() {
for (let i = 0; i < 1000000; i++) {
// await httpClient.request('https://registry.npmmirror.com/');
// console.log(r.status, r.headers, r.res.timing);
try {
const r = await httpClient.request('https://edgeupdates.microsoft.com/api/products');
// console.log(r.status, r.headers, r.data.length, r.res.timing);
if (i % 10 === 0) {
fengmk2 marked this conversation as resolved.
Show resolved Hide resolved
// console.log(r.status, r.headers, r.data.length, r.res.timing);
console.log(i, r.status, process.memoryUsage());
}
} catch (err) {
console.error('%s error: %s', i, err.message);
}
}
fengmk2 marked this conversation as resolved.
Show resolved Hide resolved
}

main().then(() => {
console.log('main end');
}).catch(err => {
console.error('main error throw: %s', err);
console.error(err);
process.exit(1);
});

// process.on('uncaughtException', (...args) => {
// console.error('uncaughtException', args);
// process.exit(1);
// });

// process.on('unhandledRejection', (...args) => {
// console.error('unhandledRejection', args);
// process.exit(2);
// });

// process.on('uncaughtExceptionMonitor', (...args) => {
// console.error('uncaughtExceptionMonitor', args);
// process.exit(2);
// });

process.on('beforeExit', (...args) => {
console.error('beforeExit', args);
});
fengmk2 marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "urllib",
"version": "4.4.0",
"version": "4.5.0-beta.3",
"publishConfig": {
"tag": "latest"
},
Expand Down Expand Up @@ -44,11 +44,12 @@
"prepublishOnly": "npm run build"
},
"dependencies": {
"form-data": "^4.0.1",
"formstream": "^1.5.1",
"mime-types": "^2.1.35",
"qs": "^6.12.1",
"type-fest": "^4.20.1",
"undici": "^6.19.2",
"undici": "^7.0.0",
"ylru": "^2.0.0"
},
"devDependencies": {
Expand All @@ -68,6 +69,7 @@
"cross-env": "^7.0.3",
"eslint": "8",
"eslint-config-egg": "14",
"https-pem": "^3.0.0",
"iconv-lite": "^0.6.3",
"proxy": "^1.0.2",
"selfsigned": "^2.0.1",
Expand Down
5 changes: 3 additions & 2 deletions scripts/replace_urllib_version.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ async function main() {
for (const file of files) {
const content = await fs.readFile(file, 'utf-8');
// replace "const VERSION = 'VERSION';" to "const VERSION = '4.0.0';"
const newContent = content.replace(/const VERSION = 'VERSION';/, match => {
const after = `const VERSION = '${pkg.version}';`;
// "exports.VERSION = 'VERSION';" => "exports.VERSION = '4.0.0';"
const newContent = content.replace(/ = 'VERSION';/, match => {
const after = ` = '${pkg.version}';`;
console.log('[%s] %s => %s', file, match, after);
return after;
});
Expand Down
2 changes: 1 addition & 1 deletion src/FetchOpaqueInterceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
export function fetchOpaqueInterceptor(opts: OpaqueInterceptorOptions) {
const opaqueLocalStorage = opts?.opaqueLocalStorage;
return (dispatch: Dispatcher['dispatch']): Dispatcher['dispatch'] => {
return function redirectInterceptor(opts: Dispatcher.DispatchOptions, handler: Dispatcher.DispatchHandlers) {
return function redirectInterceptor(opts: Dispatcher.DispatchOptions, handler: Dispatcher.DispatchHandler) {

Check warning on line 35 in src/FetchOpaqueInterceptor.ts

View check run for this annotation

Codecov / codecov/patch

src/FetchOpaqueInterceptor.ts#L35

Added line #L35 was not covered by tests
const opaque = opaqueLocalStorage?.getStore();
(handler as any).opaque = opaque;
return dispatch(opts, handler);
Expand Down
32 changes: 32 additions & 0 deletions src/FormData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import path from 'node:path';
import _FormData from 'form-data';

export class FormData extends _FormData {
_getContentDisposition(value: any, options: any) {
// support non-ascii filename
// https://github.com/form-data/form-data/pull/571
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let filename;
let contentDisposition;

if (typeof options.filepath === 'string') {
// custom filepath for relative paths
filename = path.normalize(options.filepath).replace(/\\/g, '/');

Check warning on line 13 in src/FormData.ts

View check run for this annotation

Codecov / codecov/patch

src/FormData.ts#L13

Added line #L13 was not covered by tests
} else if (options.filename || value.name || value.path) {
// custom filename take precedence
// formidable and the browser add a name property
// fs- and request- streams have path property
filename = path.basename(options.filename || value.name || value.path);
} else if (value.readable && value.hasOwnProperty('httpVersion')) {
// or try http response
filename = path.basename(value.client._httpMessage.path || '');
fengmk2 marked this conversation as resolved.
Show resolved Hide resolved
}

Check warning on line 22 in src/FormData.ts

View check run for this annotation

Codecov / codecov/patch

src/FormData.ts#L21-L22

Added lines #L21 - L22 were not covered by tests

if (filename) {
// https://datatracker.ietf.org/doc/html/rfc6266#section-4.1
// support non-ascii filename
contentDisposition = 'filename="' + filename + '"; filename*=UTF-8\'\'' + encodeURIComponent(filename);
}
fengmk2 marked this conversation as resolved.
Show resolved Hide resolved

return contentDisposition;
}
}
2 changes: 1 addition & 1 deletion src/HttpAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class HttpAgent extends Agent {
this.#checkAddress = options.checkAddress;
}

dispatch(options: Agent.DispatchOptions, handler: Dispatcher.DispatchHandlers): boolean {
dispatch(options: Agent.DispatchOptions, handler: Dispatcher.DispatchHandler): boolean {
if (this.#checkAddress && options.origin) {
const originUrl = typeof options.origin === 'string' ? new URL(options.origin) : options.origin;
let hostname = originUrl.hostname;
Expand Down
Loading