-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix server side close unexpected exit closes #541 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Release Notes - **New Features** - Introduced new example scripts demonstrating HTTP/2 requests with enhanced error handling. - Added support for logging response status and memory usage in long-running request scenarios. - Added a new `FormData` class to handle file uploads with non-ASCII filenames. - Enhanced the `HttpClient` with improved handling of connection closures and multiple concurrent requests. - Updated test suite to ensure robust handling of edge cases, including file uploads with special characters. - **Bug Fixes** - Improved error handling in HTTP request processes to better manage socket errors and timeouts. - Enhanced diagnostics logging to provide better context for missing data during request processing. - **Documentation** - Updated package configuration to reflect new dependencies and module structure. - **Refactor** - Enhanced type specificity in various components to improve clarity and maintainability. - Improved clarity in test cases with better type handling and naming consistency. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
- Loading branch information
Showing
19 changed files
with
499 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} | ||
|
||
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
})); | ||
|
||
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; | ||
} | ||
} | ||
} | ||
} | ||
|
||
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
// 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); | ||
} | ||
} | ||
} | ||
|
||
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
let filename; | ||
let contentDisposition; | ||
|
||
if (typeof options.filepath === 'string') { | ||
// custom filepath for relative paths | ||
filename = path.normalize(options.filepath).replace(/\\/g, '/'); | ||
} 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 || ''); | ||
} | ||
|
||
if (filename) { | ||
// https://datatracker.ietf.org/doc/html/rfc6266#section-4.1 | ||
// support non-ascii filename | ||
contentDisposition = 'filename="' + filename + '"; filename*=UTF-8\'\'' + encodeURIComponent(filename); | ||
} | ||
|
||
return contentDisposition; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.