@trivikr-test/node-http-handler
Advanced tools
Comparing version 3.170.0-es2016 to 3.170.0-es2017
@@ -1,2 +0,1 @@ | ||
import { __awaiter } from "tslib"; | ||
import { HttpResponse } from "@trivikr-test/protocol-http"; | ||
@@ -43,58 +42,56 @@ import { buildQueryString } from "@trivikr-test/querystring-builder"; | ||
} | ||
handle(request, { abortSignal } = {}) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
async handle(request, { abortSignal } = {}) { | ||
if (!this.config) { | ||
this.config = await this.configProvider; | ||
} | ||
return new Promise((resolve, reject) => { | ||
if (!this.config) { | ||
this.config = yield this.configProvider; | ||
throw new Error("Node HTTP request handler config is not resolved"); | ||
} | ||
return new Promise((resolve, reject) => { | ||
if (!this.config) { | ||
throw new Error("Node HTTP request handler config is not resolved"); | ||
if (abortSignal === null || abortSignal === void 0 ? void 0 : abortSignal.aborted) { | ||
const abortError = new Error("Request aborted"); | ||
abortError.name = "AbortError"; | ||
reject(abortError); | ||
return; | ||
} | ||
const isSSL = request.protocol === "https:"; | ||
const queryString = buildQueryString(request.query || {}); | ||
const nodeHttpsOptions = { | ||
headers: request.headers, | ||
host: request.hostname, | ||
method: request.method, | ||
path: queryString ? `${request.path}?${queryString}` : request.path, | ||
port: request.port, | ||
agent: isSSL ? this.config.httpsAgent : this.config.httpAgent, | ||
}; | ||
const requestFunc = isSSL ? hsRequest : hRequest; | ||
const req = requestFunc(nodeHttpsOptions, (res) => { | ||
const httpResponse = new HttpResponse({ | ||
statusCode: res.statusCode || -1, | ||
headers: getTransformedHeaders(res.headers), | ||
body: res, | ||
}); | ||
resolve({ response: httpResponse }); | ||
}); | ||
req.on("error", (err) => { | ||
if (NODEJS_TIMEOUT_ERROR_CODES.includes(err.code)) { | ||
reject(Object.assign(err, { name: "TimeoutError" })); | ||
} | ||
if (abortSignal === null || abortSignal === void 0 ? void 0 : abortSignal.aborted) { | ||
else { | ||
reject(err); | ||
} | ||
}); | ||
setConnectionTimeout(req, reject, this.config.connectionTimeout); | ||
setSocketTimeout(req, reject, this.config.socketTimeout); | ||
if (abortSignal) { | ||
abortSignal.onabort = () => { | ||
req.abort(); | ||
const abortError = new Error("Request aborted"); | ||
abortError.name = "AbortError"; | ||
reject(abortError); | ||
return; | ||
} | ||
const isSSL = request.protocol === "https:"; | ||
const queryString = buildQueryString(request.query || {}); | ||
const nodeHttpsOptions = { | ||
headers: request.headers, | ||
host: request.hostname, | ||
method: request.method, | ||
path: queryString ? `${request.path}?${queryString}` : request.path, | ||
port: request.port, | ||
agent: isSSL ? this.config.httpsAgent : this.config.httpAgent, | ||
}; | ||
const requestFunc = isSSL ? hsRequest : hRequest; | ||
const req = requestFunc(nodeHttpsOptions, (res) => { | ||
const httpResponse = new HttpResponse({ | ||
statusCode: res.statusCode || -1, | ||
headers: getTransformedHeaders(res.headers), | ||
body: res, | ||
}); | ||
resolve({ response: httpResponse }); | ||
}); | ||
req.on("error", (err) => { | ||
if (NODEJS_TIMEOUT_ERROR_CODES.includes(err.code)) { | ||
reject(Object.assign(err, { name: "TimeoutError" })); | ||
} | ||
else { | ||
reject(err); | ||
} | ||
}); | ||
setConnectionTimeout(req, reject, this.config.connectionTimeout); | ||
setSocketTimeout(req, reject, this.config.socketTimeout); | ||
if (abortSignal) { | ||
abortSignal.onabort = () => { | ||
req.abort(); | ||
const abortError = new Error("Request aborted"); | ||
abortError.name = "AbortError"; | ||
reject(abortError); | ||
}; | ||
} | ||
writeRequestBody(req, request); | ||
}); | ||
} | ||
writeRequestBody(req, request); | ||
}); | ||
} | ||
} |
@@ -1,2 +0,1 @@ | ||
import { __awaiter } from "tslib"; | ||
import { HttpResponse } from "@trivikr-test/protocol-http"; | ||
@@ -30,77 +29,75 @@ import { buildQueryString } from "@trivikr-test/querystring-builder"; | ||
} | ||
handle(request, { abortSignal } = {}) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!this.config) { | ||
this.config = yield this.configProvider; | ||
async handle(request, { abortSignal } = {}) { | ||
if (!this.config) { | ||
this.config = await this.configProvider; | ||
} | ||
const { requestTimeout, disableConcurrentStreams } = this.config; | ||
return new Promise((resolve, rejectOriginal) => { | ||
let fulfilled = false; | ||
if (abortSignal === null || abortSignal === void 0 ? void 0 : abortSignal.aborted) { | ||
fulfilled = true; | ||
const abortError = new Error("Request aborted"); | ||
abortError.name = "AbortError"; | ||
rejectOriginal(abortError); | ||
return; | ||
} | ||
const { requestTimeout, disableConcurrentStreams } = this.config; | ||
return new Promise((resolve, rejectOriginal) => { | ||
let fulfilled = false; | ||
if (abortSignal === null || abortSignal === void 0 ? void 0 : abortSignal.aborted) { | ||
fulfilled = true; | ||
const { hostname, method, port, protocol, path, query } = request; | ||
const authority = `${protocol}//${hostname}${port ? `:${port}` : ""}`; | ||
const session = this.getSession(authority, disableConcurrentStreams || false); | ||
const reject = (err) => { | ||
if (disableConcurrentStreams) { | ||
this.destroySession(session); | ||
} | ||
fulfilled = true; | ||
rejectOriginal(err); | ||
}; | ||
const queryString = buildQueryString(query || {}); | ||
const req = session.request(Object.assign(Object.assign({}, request.headers), { [constants.HTTP2_HEADER_PATH]: queryString ? `${path}?${queryString}` : path, [constants.HTTP2_HEADER_METHOD]: method })); | ||
session.ref(); | ||
req.on("response", (headers) => { | ||
const httpResponse = new HttpResponse({ | ||
statusCode: headers[":status"] || -1, | ||
headers: getTransformedHeaders(headers), | ||
body: req, | ||
}); | ||
fulfilled = true; | ||
resolve({ response: httpResponse }); | ||
if (disableConcurrentStreams) { | ||
session.close(); | ||
this.deleteSessionFromCache(authority, session); | ||
} | ||
}); | ||
if (requestTimeout) { | ||
req.setTimeout(requestTimeout, () => { | ||
req.close(); | ||
const timeoutError = new Error(`Stream timed out because of no activity for ${requestTimeout} ms`); | ||
timeoutError.name = "TimeoutError"; | ||
reject(timeoutError); | ||
}); | ||
} | ||
if (abortSignal) { | ||
abortSignal.onabort = () => { | ||
req.close(); | ||
const abortError = new Error("Request aborted"); | ||
abortError.name = "AbortError"; | ||
rejectOriginal(abortError); | ||
return; | ||
} | ||
const { hostname, method, port, protocol, path, query } = request; | ||
const authority = `${protocol}//${hostname}${port ? `:${port}` : ""}`; | ||
const session = this.getSession(authority, disableConcurrentStreams || false); | ||
const reject = (err) => { | ||
if (disableConcurrentStreams) { | ||
this.destroySession(session); | ||
} | ||
fulfilled = true; | ||
rejectOriginal(err); | ||
reject(abortError); | ||
}; | ||
const queryString = buildQueryString(query || {}); | ||
const req = session.request(Object.assign(Object.assign({}, request.headers), { [constants.HTTP2_HEADER_PATH]: queryString ? `${path}?${queryString}` : path, [constants.HTTP2_HEADER_METHOD]: method })); | ||
session.ref(); | ||
req.on("response", (headers) => { | ||
const httpResponse = new HttpResponse({ | ||
statusCode: headers[":status"] || -1, | ||
headers: getTransformedHeaders(headers), | ||
body: req, | ||
}); | ||
fulfilled = true; | ||
resolve({ response: httpResponse }); | ||
if (disableConcurrentStreams) { | ||
session.close(); | ||
this.deleteSessionFromCache(authority, session); | ||
} | ||
}); | ||
if (requestTimeout) { | ||
req.setTimeout(requestTimeout, () => { | ||
req.close(); | ||
const timeoutError = new Error(`Stream timed out because of no activity for ${requestTimeout} ms`); | ||
timeoutError.name = "TimeoutError"; | ||
reject(timeoutError); | ||
}); | ||
} | ||
req.on("frameError", (type, code, id) => { | ||
reject(new Error(`Frame type id ${type} in stream id ${id} has failed with code ${code}.`)); | ||
}); | ||
req.on("error", reject); | ||
req.on("aborted", () => { | ||
reject(new Error(`HTTP/2 stream is abnormally aborted in mid-communication with result code ${req.rstCode}.`)); | ||
}); | ||
req.on("close", () => { | ||
session.unref(); | ||
if (disableConcurrentStreams) { | ||
session.destroy(); | ||
} | ||
if (abortSignal) { | ||
abortSignal.onabort = () => { | ||
req.close(); | ||
const abortError = new Error("Request aborted"); | ||
abortError.name = "AbortError"; | ||
reject(abortError); | ||
}; | ||
if (!fulfilled) { | ||
reject(new Error("Unexpected error: http2 request did not get a response")); | ||
} | ||
req.on("frameError", (type, code, id) => { | ||
reject(new Error(`Frame type id ${type} in stream id ${id} has failed with code ${code}.`)); | ||
}); | ||
req.on("error", reject); | ||
req.on("aborted", () => { | ||
reject(new Error(`HTTP/2 stream is abnormally aborted in mid-communication with result code ${req.rstCode}.`)); | ||
}); | ||
req.on("close", () => { | ||
session.unref(); | ||
if (disableConcurrentStreams) { | ||
session.destroy(); | ||
} | ||
if (!fulfilled) { | ||
reject(new Error("Unexpected error: http2 request did not get a response")); | ||
} | ||
}); | ||
writeRequestBody(req, request); | ||
}); | ||
writeRequestBody(req, request); | ||
}); | ||
@@ -107,0 +104,0 @@ } |
{ | ||
"name": "@trivikr-test/node-http-handler", | ||
"version": "3.170.0-es2016", | ||
"version": "3.170.0-es2017", | ||
"description": "Provides a way to make requests", | ||
@@ -25,6 +25,6 @@ "scripts": { | ||
"dependencies": { | ||
"@trivikr-test/abort-controller": "3.170.0-es2016", | ||
"@trivikr-test/protocol-http": "3.170.0-es2016", | ||
"@trivikr-test/querystring-builder": "3.170.0-es2016", | ||
"@trivikr-test/types": "3.170.0-es2016", | ||
"@trivikr-test/abort-controller": "3.170.0-es2017", | ||
"@trivikr-test/protocol-http": "3.170.0-es2017", | ||
"@trivikr-test/querystring-builder": "3.170.0-es2017", | ||
"@trivikr-test/types": "3.170.0-es2017", | ||
"tslib": "^2.3.1" | ||
@@ -31,0 +31,0 @@ }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
59345
1177
+ Added@trivikr-test/abort-controller@3.170.0-es2017(transitive)
+ Added@trivikr-test/protocol-http@3.170.0-es2017(transitive)
+ Added@trivikr-test/querystring-builder@3.170.0-es2017(transitive)
+ Added@trivikr-test/types@3.170.0-es2017(transitive)
+ Added@trivikr-test/util-uri-escape@3.170.0-es2017(transitive)
- Removed@trivikr-test/abort-controller@3.170.0-es2016(transitive)
- Removed@trivikr-test/protocol-http@3.170.0-es2016(transitive)
- Removed@trivikr-test/querystring-builder@3.170.0-es2016(transitive)
- Removed@trivikr-test/types@3.170.0-es2016(transitive)
- Removed@trivikr-test/util-uri-escape@3.170.0-es2016(transitive)