fix(core): Improve streaming support and timeout handling; add browser streaming & timeout tests and README clarifications

This commit is contained in:
2025-08-19 01:36:44 +00:00
parent 35867d9148
commit 361d97f440
8 changed files with 181 additions and 11 deletions

View File

@@ -119,10 +119,11 @@ export class CoreRequest extends AbstractCoreRequest<
}
// Perform the request
let timeoutId: NodeJS.Timeout | null = null;
const request = requestModule.request(this.options, async (response) => {
// Handle hard timeout
if (this.options.hardDataCuttingTimeout) {
setTimeout(() => {
timeoutId = setTimeout(() => {
response.destroy();
done.reject(new Error('Request timed out'));
}, this.options.hardDataCuttingTimeout);
@@ -132,6 +133,14 @@ export class CoreRequest extends AbstractCoreRequest<
done.resolve(response);
});
// Set request timeout (Node.js built-in timeout)
if (this.options.timeout) {
request.setTimeout(this.options.timeout, () => {
request.destroy();
done.reject(new Error('Request timed out'));
});
}
// Write request body
if (this.options.requestBody) {
if (this.options.requestBody instanceof plugins.formData) {
@@ -159,11 +168,23 @@ export class CoreRequest extends AbstractCoreRequest<
request.on('error', (e) => {
console.error(e);
request.destroy();
// Clear timeout on error
if (timeoutId) {
clearTimeout(timeoutId);
timeoutId = null;
}
done.reject(e);
});
// Get response and handle response errors
const response = await done.promise;
// Clear timeout on successful response
if (timeoutId) {
clearTimeout(timeoutId);
timeoutId = null;
}
response.on('error', (err) => {
console.error(err);
response.destroy();