Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return new Promise((resolve, reject) => {
// if the request was already aborted, prevent doing extra work
if (abortSignal && abortSignal.aborted) {
const abortError = new Error("Request aborted");
abortError.name = "AbortError";
reject(abortError);
return;
}
const { hostname, method, port, protocol, path, query } = request;
const queryString = buildQueryString(query || {});
// create the http2 request
const req = this.getSession(
`${protocol}//${hostname}${port ? `:${port}` : ""}`
).request({
...request.headers,
[constants.HTTP2_HEADER_PATH]: queryString
? `${path}?${queryString}`
: path,
[constants.HTTP2_HEADER_METHOD]: method
});
req.on("response", headers => {
const httpResponse = new HttpResponse({
statusCode: headers[":status"] || -1,
headers: getTransformedHeaders(headers),
return new Promise((resolve, reject) => {
// if the request was already aborted, prevent doing extra work
if (abortSignal && abortSignal.aborted) {
const abortError = new Error("Request aborted");
abortError.name = "AbortError";
reject(abortError);
return;
}
// determine which http(s) client to use
const isSSL = request.protocol === "https:";
const queryString = buildQueryString(request.query || {});
const nodeHttpsOptions: https.RequestOptions = {
headers: request.headers,
host: request.hostname,
method: request.method,
path: queryString ? `${request.path}?${queryString}` : request.path,
port: request.port,
agent: isSSL ? this.httpsAgent : this.httpAgent
};
// create the http request
const req = (isSSL ? https : http).request(nodeHttpsOptions, res => {
const httpResponse = new HttpResponse({
statusCode: res.statusCode || -1,
headers: getTransformedHeaders(res.headers),
body: res
});
export function formatUrl(request: HttpRequest): string {
let { protocol, path, hostname, port, query } = request;
if (protocol && protocol.substr(-1) !== ":") {
protocol += ":";
}
if (port) {
hostname += `:${port}`;
}
if (path && path.charAt(0) !== "/") {
path = `/${path}`;
}
let queryString = query ? buildQueryString(query) : "";
if (queryString && queryString[0] !== "?") {
queryString = `?${queryString}`;
}
return `${protocol}//${hostname}${path}${queryString}`;
}