diff --git a/lib/redirect.js b/lib/redirect.js index ab4604d72..f7f4187aa 100644 --- a/lib/redirect.js +++ b/lib/redirect.js @@ -214,7 +214,7 @@ Redirect.prototype.onResponse = function (response) { // Wait for request to end before firing redirect request request.once('finish', () => { - request.init(options); + request.init(options) }) return true diff --git a/request.js b/request.js index ca64a6284..3658459ee 100644 --- a/request.js +++ b/request.js @@ -3,8 +3,8 @@ var tls = require('tls') var http = require('http') var https = require('https') -var http2 = require('./lib/http2').default; -var autohttp2 = require('./lib/autohttp').default; +var http2 = require('./lib/http2').default +var autohttp2 = require('./lib/autohttp').default var url = require('url') var util = require('util') var stream = require('stream') @@ -595,11 +595,12 @@ Request.prototype.init = function (options) { // If user defines httpModules, respect if they have different httpModules for different http versions, else use the tls specific http module // If the user defines nothing, revert to default modules - self.httpModule = httpModules[protocol]?.[self.protocolVersion] || httpModules[protocol] || defaultModules[protocol][self.protocolVersion] + self.httpModule = (httpModules[protocol] && httpModules[protocol][self.protocolVersion]) || httpModules[protocol] || + defaultModules[protocol][self.protocolVersion] - if (!httpModules[protocol]?.[options.protocolVersion] && httpModules[protocol]){ + if (httpModules[protocol] && !(httpModules[protocol][options.protocolVersion])) { // If the user is only specifying https/http modules, revert to http1 - self.protocolVersion = 'http1'; + self.protocolVersion = 'http1' } if (!self.httpModule) { @@ -922,7 +923,7 @@ Request.prototype.start = function () { // at the _exact_ same time, they should be close enough to be able to calculate // high-resolution, monotonically non-decreasing timestamps relative to startTime. var startTime = new Date().getTime() - var startTimeNow = performance.now() + var startTimeNow = now() } if (self._aborted) { @@ -1212,13 +1213,13 @@ Request.prototype.onRequestResponse = function (response) { var self = this if (self.timing) { - self.timings.response = performance.now() - self.startTimeNow + self.timings.response = now() - self.startTimeNow } debug('onRequestResponse', self.uri.href, response.statusCode, response.headers) response.on('end', function () { if (self.timing) { - self.timings.end = performance.now() - self.startTimeNow + self.timings.end = now() - self.startTimeNow response.timingStart = self.startTime response.timingStartTimer = self.startTimeNow diff --git a/test.js b/test.js deleted file mode 100644 index 225686416..000000000 --- a/test.js +++ /dev/null @@ -1,47 +0,0 @@ -const request = require('./index'); - -const fs = require('fs') -const { isError } = require('node:util') -// request("https://postman-echo.com/get",{protocol: 'http1'}, (err, resp, body)=>console.log(body)) -const TEST_URL = "https://postman-echo.com/get"; -const httpsurl = 'https://localhost:443' -const http2url = 'https://localhost:3000/h2' -const pool = {}; -const proto = 'h2'; -const r = request(TEST_URL,{ - protocolVersion: proto, - timing:true, - strictSSL:true, - gzip:true, - brotli: true, - agentOptions: { - timeout: 300 - }, - pool - // ca: fs.readFileSync('/etc/ssl/cert.pem'), - // key: fs.readFileSync('/Users/parth.verma@postman.com/temp/t/key.pem'), - // cert: fs.readFileSync('/Users/parth.verma@postman.com/temp/t/cert.pem') -}, (err, resp, body)=> { - console.log(resp?.timings, err, body); - request(TEST_URL,{ - protocolVersion: proto, - timing:true, - strictSSL:true, - gzip:true, - brotli: true, - agentOptions: { - timeout: 300 - }, - pool - // ca: fs.readFileSync('/etc/ssl/cert.pem'), - // key: fs.readFileSync('/Users/parth.verma@postman.com/temp/t/key.pem'), - // cert: fs.readFileSync('/Users/parth.verma@postman.com/temp/t/cert.pem') - }, (err, resp, body)=> { - console.log(resp?.timings) - console.log(resp.httpVersion ) - // console.log({body}); - // - }) - -}) - diff --git a/tests/server.js b/tests/server.js index c1f71a634..0c0728a8d 100644 --- a/tests/server.js +++ b/tests/server.js @@ -184,7 +184,7 @@ exports.createHttp2Server = function (opts) { } var s = http2.createSecureServer(options, function (req, resp) { - s.emit(req.url, req, resp); + s.emit(req.url, req, resp) }) s.on('listening', function () { s.port = this.address().port diff --git a/tests/test-body-http2.js b/tests/test-body-http2.js index 04eaa7bda..2968c50da 100644 --- a/tests/test-body-http2.js +++ b/tests/test-body-http2.js @@ -1,45 +1,44 @@ -"use strict"; +'use strict' -var server = require("./server"); -var request = require("../index"); -var tape = require("tape"); -var http = require("http"); -var path = require("path"); -var fs = require("fs"); +var server = require('./server') +var request = require('../index') +var tape = require('tape') +var path = require('path') +var fs = require('fs') -var s = server.createHttp2Server(); +var s = server.createHttp2Server() -tape("setup", function (t) { +tape('setup', function (t) { s.listen(0, function () { - t.end(); - }); -}); - -function addTest(name, data) { - tape("test " + name, function (t) { - s.on("/" + name, data.resp); - data.uri = s.url + "/" + name; + t.end() + }) +}) + +function addTest (name, data) { + tape('test ' + name, function (t) { + s.on('/' + name, data.resp) + data.uri = s.url + '/' + name request( - { ...data, protocolVersion: "h2", strictSSL: false }, + { ...data, protocolVersion: 'h2', strictSSL: false }, function (err, resp, body) { - t.equal(err, null); + t.equal(err, null) if (data.expectBody && Buffer.isBuffer(data.expectBody)) { - t.deepEqual(data.expectBody.toString(), body.toString()); + t.deepEqual(data.expectBody.toString(), body.toString()) } else if (data.expectBody) { - t.deepEqual(data.expectBody, body); + t.deepEqual(data.expectBody, body) } - t.end(); + t.end() } - ); - }); + ) + }) } -addTest("testGet", { - resp: server.createGetResponse("TESTING!"), - expectBody: "TESTING!", -}); +addTest('testGet', { + resp: server.createGetResponse('TESTING!'), + expectBody: 'TESTING!' +}) -addTest("testGetChunkBreak", { +addTest('testGetChunkBreak', { resp: server.createChunkResponse([ Buffer.from([239]), Buffer.from([163]), @@ -48,163 +47,163 @@ addTest("testGetChunkBreak", { Buffer.from([169]), Buffer.from([226]), Buffer.from([152]), - Buffer.from([131]), + Buffer.from([131]) ]), - expectBody: "\uF8FF\u03A9\u2603", -}); + expectBody: '\uF8FF\u03A9\u2603' +}) -addTest("testGetBuffer", { - resp: server.createGetResponse(Buffer.from("TESTING!")), +addTest('testGetBuffer', { + resp: server.createGetResponse(Buffer.from('TESTING!')), encoding: null, - expectBody: Buffer.from("TESTING!"), -}); + expectBody: Buffer.from('TESTING!') +}) -addTest("testGetEncoding", { - resp: server.createGetResponse(Buffer.from("efa3bfcea9e29883", "hex")), - encoding: "hex", - expectBody: "efa3bfcea9e29883", -}); +addTest('testGetEncoding', { + resp: server.createGetResponse(Buffer.from('efa3bfcea9e29883', 'hex')), + encoding: 'hex', + expectBody: 'efa3bfcea9e29883' +}) -addTest("testGetUTF", { +addTest('testGetUTF', { resp: server.createGetResponse( Buffer.from([0xef, 0xbb, 0xbf, 226, 152, 131]) ), - encoding: "utf8", - expectBody: "\u2603", -}); + encoding: 'utf8', + expectBody: '\u2603' +}) -addTest("testGetJSON", { - resp: server.createGetResponse('{"test":true}', "application/json"), +addTest('testGetJSON', { + resp: server.createGetResponse('{"test":true}', 'application/json'), json: true, - expectBody: { test: true }, -}); - -addTest("testPutString", { - resp: server.createPostValidator("PUTTINGDATA"), - method: "PUT", - body: "PUTTINGDATA", -}); - -addTest("testPutBuffer", { - resp: server.createPostValidator("PUTTINGDATA"), - method: "PUT", - body: Buffer.from("PUTTINGDATA"), -}); - -addTest("testPutJSON", { - resp: server.createPostValidator(JSON.stringify({ foo: "bar" })), - method: "PUT", - json: { foo: "bar" }, -}); - -addTest("testPutMultipart", { + expectBody: { test: true } +}) + +addTest('testPutString', { + resp: server.createPostValidator('PUTTINGDATA'), + method: 'PUT', + body: 'PUTTINGDATA' +}) + +addTest('testPutBuffer', { + resp: server.createPostValidator('PUTTINGDATA'), + method: 'PUT', + body: Buffer.from('PUTTINGDATA') +}) + +addTest('testPutJSON', { + resp: server.createPostValidator(JSON.stringify({ foo: 'bar' })), + method: 'PUT', + json: { foo: 'bar' } +}) + +addTest('testPutMultipart', { resp: server.createPostValidator( - "--__BOUNDARY__\r\n" + - "content-type: text/html\r\n" + - "\r\n" + - "Oh hi." + - "\r\n--__BOUNDARY__\r\n\r\n" + - "Oh hi." + - "\r\n--__BOUNDARY__--" + '--__BOUNDARY__\r\n' + + 'content-type: text/html\r\n' + + '\r\n' + + 'Oh hi.' + + '\r\n--__BOUNDARY__\r\n\r\n' + + 'Oh hi.' + + '\r\n--__BOUNDARY__--' ), - method: "PUT", + method: 'PUT', multipart: [ - { "content-type": "text/html", body: "Oh hi." }, - { body: "Oh hi." }, - ], -}); + { 'content-type': 'text/html', body: 'Oh hi.' }, + { body: 'Oh hi.' } + ] +}) -addTest("testPutMultipartPreambleCRLF", { +addTest('testPutMultipartPreambleCRLF', { resp: server.createPostValidator( - "\r\n--__BOUNDARY__\r\n" + - "content-type: text/html\r\n" + - "\r\n" + - "Oh hi." + - "\r\n--__BOUNDARY__\r\n\r\n" + - "Oh hi." + - "\r\n--__BOUNDARY__--" + '\r\n--__BOUNDARY__\r\n' + + 'content-type: text/html\r\n' + + '\r\n' + + 'Oh hi.' + + '\r\n--__BOUNDARY__\r\n\r\n' + + 'Oh hi.' + + '\r\n--__BOUNDARY__--' ), - method: "PUT", + method: 'PUT', preambleCRLF: true, multipart: [ - { "content-type": "text/html", body: "Oh hi." }, - { body: "Oh hi." }, - ], -}); + { 'content-type': 'text/html', body: 'Oh hi.' }, + { body: 'Oh hi.' } + ] +}) -addTest("testPutMultipartPostambleCRLF", { +addTest('testPutMultipartPostambleCRLF', { resp: server.createPostValidator( - "\r\n--__BOUNDARY__\r\n" + - "content-type: text/html\r\n" + - "\r\n" + - "Oh hi." + - "\r\n--__BOUNDARY__\r\n\r\n" + - "Oh hi." + - "\r\n--__BOUNDARY__--" + - "\r\n" + '\r\n--__BOUNDARY__\r\n' + + 'content-type: text/html\r\n' + + '\r\n' + + 'Oh hi.' + + '\r\n--__BOUNDARY__\r\n\r\n' + + 'Oh hi.' + + '\r\n--__BOUNDARY__--' + + '\r\n' ), - method: "PUT", + method: 'PUT', preambleCRLF: true, postambleCRLF: true, multipart: [ - { "content-type": "text/html", body: "Oh hi." }, - { body: "Oh hi." }, - ], -}); - -tape("testBinaryFile", function (t) { - s.on("/", function (req, res) { - req.pipe(res); + { 'content-type': 'text/html', body: 'Oh hi.' }, + { body: 'Oh hi.' } + ] +}) + +tape('testBinaryFile', function (t) { + s.on('/', function (req, res) { + req.pipe(res) // Close the session if it's a HTTP/2 request. This is not representative of a true http/2 server that might keep the session open. But we need this to close the server in the tests. - req?.stream?.session?.close?.() - }); + req.stream && req.stream.session && req.stream.session.close && req.stream.session.close() + }) request( { - uri: "https://localhost:" + s.port, - method: "POST", + uri: 'https://localhost:' + s.port, + method: 'POST', strictSSL: false, - protocolVersion: "h2", - body: fs.createReadStream(path.join(__dirname, "raw.file")), + protocolVersion: 'h2', + body: fs.createReadStream(path.join(__dirname, 'raw.file')) }, function (err, res, body) { - t.error(err); // defaults to 'application/octet-stream' content-type - t.equal(res.request.headers["Content-Type"], "application/octet-stream"); - s.removeAllListeners("/"); - t.end(); + t.error(err) // defaults to 'application/octet-stream' content-type + t.equal(res.request.headers['Content-Type'], 'application/octet-stream') + s.removeAllListeners('/') + t.end() } - ); -}); + ) +}) -tape("typed array", function (t) { - s.on("/", function (req, res) { - req.pipe(res); +tape('typed array', function (t) { + s.on('/', function (req, res) { + req.pipe(res) // Close the session if it's a HTTP/2 request. This is not representative of a true http/2 server that might keep the session open. But we need this to close the server in the tests. - req?.stream?.session?.close?.() - }); + req.stream && req.stream.session && req.stream.session.close && req.stream.session.close() + }) - var data = new Uint8Array([1, 2, 3]); + var data = new Uint8Array([1, 2, 3]) request( { - uri: "https://localhost:" + s.port, - method: "POST", + uri: 'https://localhost:' + s.port, + method: 'POST', body: data, encoding: null, strictSSL: false, - protocolVersion: "h2", + protocolVersion: 'h2' }, function (err, res, body) { - t.error(err); - t.deepEqual(Buffer.from(data), body); - s.removeAllListeners("/"); - - t.end(); + t.error(err) + t.deepEqual(Buffer.from(data), body) + s.removeAllListeners('/') + + t.end() } - ); -}); + ) +}) -tape("cleanup", function (t) { +tape('cleanup', function (t) { s.close(function () { - t.end(); - }); -}); + t.end() + }) +}) diff --git a/tests/test-brotli-http2.js b/tests/test-brotli-http2.js index 6c9c1755b..dec9e8ae8 100644 --- a/tests/test-brotli-http2.js +++ b/tests/test-brotli-http2.js @@ -237,19 +237,19 @@ tape('transparently supports brotli error to pipes', function (t) { }) }) -tape("pause when streaming from a brotli request object", function (t) { +tape('pause when streaming from a brotli request object', function (t) { var options = { - url: s.url + "/chunks", + url: s.url + '/chunks', brotli: true, - protocolVersion: "h2", - strictSSL: false, - }; + protocolVersion: 'h2', + strictSSL: false + } request.get(options, function (err, res, body) { - t.equal(err, null); - t.equal(body, testContentBig.toString()); - t.end(); - }); -}); + t.equal(err, null) + t.equal(body, testContentBig.toString()) + t.end() + }) +}) tape('pause before streaming from a brotli request object', function (t) { var paused = true diff --git a/tests/test-cookies-http2.js b/tests/test-cookies-http2.js index 35b5af65a..4aa9c30c4 100644 --- a/tests/test-cookies-http2.js +++ b/tests/test-cookies-http2.js @@ -3,7 +3,6 @@ var request = require('../index') var tape = require('tape') var server = require('./server') -const { strict } = require('is-typedarray') var validUrl var malformedUrl diff --git a/tests/test-timing-http2.js b/tests/test-timing-http2.js index 5280138d9..096cc8579 100644 --- a/tests/test-timing-http2.js +++ b/tests/test-timing-http2.js @@ -11,7 +11,7 @@ var httpsServer = server.createHttp2Server() var redirectMockTime = 10 destroyable(plainServer) -destroyable(httpsServer); +destroyable(httpsServer) tape('setup', function (t) { plainServer.listen(0, function () {