Skip to content

Commit 26fd25d

Browse files
committed
fix: compatible with urllib@2 timeout string format
close hyj1991/easy-monitor#166
1 parent 36641dd commit 26fd25d

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/HttpClient.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,8 @@ export class HttpClient extends EventEmitter {
369369
headersTimeout = args.timeout[0] ?? headersTimeout;
370370
bodyTimeout = args.timeout[1] ?? bodyTimeout;
371371
} else {
372-
headersTimeout = bodyTimeout = args.timeout;
372+
// compatible with urllib@2 timeout string format
373+
headersTimeout = bodyTimeout = typeof args.timeout === 'string' ? parseInt(args.timeout) : args.timeout;
373374
}
374375
}
375376
if (originalHeaders) {

test/options.timeout.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,27 @@ describe('options.timeout.test.ts', () => {
4141
});
4242
});
4343

44+
it('should timeout support string format', async () => {
45+
await assert.rejects(async () => {
46+
await urllib.request(`${_url}?timeout=2000`, {
47+
// @ts-expect-error check string format
48+
timeout: '10',
49+
});
50+
}, (err: any) => {
51+
// console.error(err);
52+
assert.equal(err.name, 'HttpClientRequestTimeoutError');
53+
assert.equal(err.message, 'Request timeout for 10 ms');
54+
assert.equal(err.cause.name, 'HeadersTimeoutError');
55+
assert.equal(err.cause.message, 'Headers Timeout Error');
56+
assert.equal(err.cause.code, 'UND_ERR_HEADERS_TIMEOUT');
57+
58+
assert.equal(err.res.status, -1);
59+
assert(err.res.rt > 10, `actual ${err.res.rt}`);
60+
assert.equal(typeof err.res.rt, 'number');
61+
return true;
62+
});
63+
});
64+
4465
it('should timeout on h2', async () => {
4566
const httpClient = new HttpClient({
4667
allowH2: true,

0 commit comments

Comments
 (0)