From 4f7bde7035f7135db98c9503c11d11cbb96b157b Mon Sep 17 00:00:00 2001 From: Jackson Tian Date: Tue, 28 May 2019 11:32:56 +0800 Subject: [PATCH 1/2] Add typescript support --- lib/index.d.ts | 19 +++++++++++++++++++ lib/index.js | 16 ++++++++++------ package.json | 2 ++ 3 files changed, 31 insertions(+), 6 deletions(-) create mode 100644 lib/index.d.ts diff --git a/lib/index.d.ts b/lib/index.d.ts new file mode 100644 index 0000000..9ec9efc --- /dev/null +++ b/lib/index.d.ts @@ -0,0 +1,19 @@ +import { IncomingMessage, Agent, OutgoingHttpHeaders } from "http"; + +export interface Options { + 'method'?: string; + 'headers'?: {[key: string]: string}; + 'readTimeout'?: number; + 'connectTimeout'?: number; + 'timeout'?: number; + 'agent'?: Agent; + 'headers'?: OutgoingHttpHeaders; + 'rejectUnauthorized'?: boolean; + 'compression'?: boolean; + 'beforeRequest'?(options: Options): void; + 'data'?: string|Buffer|undefined; +} + +export function request(url: string, options: Options): Promise; + +export function read(response: IncomingMessage, encoding: string): Promise; diff --git a/lib/index.js b/lib/index.js index c615400..6c105a0 100644 --- a/lib/index.js +++ b/lib/index.js @@ -108,7 +108,10 @@ exports.request = function (url, opts) { const startResponseTimer = function (socket) { const timer = setTimeout(() => { - socket[READ_TIMER] = null; + if (socket[READ_TIMER]) { + clearTimeout(socket[READ_TIMER]); + socket[READ_TIMER] = null; + } var err = new Error(); var message = `ReadTimeout(${readTimeout})`; abort(append(err, 'RequestTimeout', message)); @@ -159,27 +162,28 @@ exports.read = function (response, encoding) { } return new Promise((resolve, reject) => { - const makeReadTimeoutError = (err) => { + const makeReadTimeoutError = () => { const req = response.req; + var err = new Error(); err.name = 'RequestTimeoutError'; err.message = `ReadTimeout: ${response.socket[READ_TIME_OUT]}. ${req.method} ${req.path} failed.`; return err; - } + }; // check read-timer let readTimer; const oldReadTimer = response.socket[READ_TIMER]; if (!oldReadTimer) { - reject(makeReadTimeoutError(new Error())); + reject(makeReadTimeoutError()); return; } const remainTime = response.socket[READ_TIME_OUT] - (Date.now() - oldReadTimer.startTime); clearTimeout(oldReadTimer); if (remainTime <= 0) { - reject(makeReadTimeoutError(new Error())); + reject(makeReadTimeoutError()); return; } readTimer = setTimeout(function () { - reject(makeReadTimeoutError(new Error())); + reject(makeReadTimeoutError()); }, remainTime); // start reading data diff --git a/package.json b/package.json index 7ce8139..8a05a47 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "2.1.4", "description": "http(s) module with power", "main": "lib/index.js", + "types": "lib/index.d.ts", "scripts": { "lint": "eslint --fix lib", "test": "mocha --reporter spec --timeout 3000 test/*.test.js", @@ -26,6 +27,7 @@ "nyc": "^12.0.2" }, "dependencies": { + "@types/node": "^12.0.2", "debug": "^2.2.0" }, "files": [ From d75c9f8eb555e26f1ddd57b95612cb0c31d055dd Mon Sep 17 00:00:00 2001 From: Jackson Tian Date: Tue, 28 May 2019 12:38:03 +0800 Subject: [PATCH 2/2] upgrade debug to 4.x --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8a05a47..548e202 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ }, "dependencies": { "@types/node": "^12.0.2", - "debug": "^2.2.0" + "debug": "^4.1.1" }, "files": [ "lib"