diff --git a/lib/_http_client.js b/lib/_http_client.js index 727dc649c01..7bf485b47e6 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -40,6 +40,7 @@ const { urlToOptions, searchParamsSymbol } = require('internal/url'); const { outHeadersKey, ondrain } = require('internal/http'); const { nextTick } = require('internal/process/next_tick'); const errors = require('internal/errors'); +const { validateTimerDuration } = require('internal/timers'); const INVALID_PATH_REGEX = /[^\u0021-\u00ff]/; @@ -678,6 +679,7 @@ function _deferToConnect(method, arguments_, cb) { } ClientRequest.prototype.setTimeout = function setTimeout(msecs, callback) { + msecs = validateTimerDuration(msecs); if (callback) this.once('timeout', callback); const emitTimeout = () => this.emit('timeout'); diff --git a/test/parallel/test-http-timeout-client-warning.js b/test/parallel/test-http-timeout-client-warning.js new file mode 100644 index 00000000000..f11515b95fe --- /dev/null +++ b/test/parallel/test-http-timeout-client-warning.js @@ -0,0 +1,21 @@ +'use strict'; +const common = require('../common'); +const http = require('http'); +const assert = require('assert'); + +// Checks that the setTimeout duration overflow warning is emitted +// synchronously and therefore contains a meaningful stacktrace. + +process.on('warning', common.mustCall((warning) => { + assert(warning.stack.includes(__filename)); +})); + +const server = http.createServer((req, resp) => resp.end()); +server.listen(common.mustCall(() => { + http.request(`http://localhost:${server.address().port}`) + .setTimeout(2 ** 40) + .on('response', common.mustCall(() => { + server.close(); + })) + .end(); +}));