2020-04-18 02:46:52 -04:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
import http from "http";
|
|
|
|
import https from "https";
|
2020-10-07 19:40:28 -04:00
|
|
|
import { gunzipSync } from "zlib";
|
2020-06-12 04:38:25 -04:00
|
|
|
import { parse } from "url"
|
2020-04-18 02:46:52 -04:00
|
|
|
|
2020-10-07 19:40:28 -04:00
|
|
|
import { arrayify, concat } from "@ethersproject/bytes";
|
|
|
|
import { shallowCopy } from "@ethersproject/properties";
|
2020-07-30 17:03:36 -04:00
|
|
|
|
|
|
|
import type { GetUrlResponse, Options } from "./types";
|
|
|
|
|
2020-04-18 02:46:52 -04:00
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
|
|
|
|
2020-07-30 17:03:36 -04:00
|
|
|
export { GetUrlResponse, Options };
|
2020-04-18 02:46:52 -04:00
|
|
|
|
|
|
|
function getResponse(request: http.ClientRequest): Promise<GetUrlResponse> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
request.once("response", (resp: http.IncomingMessage) => {
|
|
|
|
const response: GetUrlResponse = {
|
|
|
|
statusCode: resp.statusCode,
|
|
|
|
statusMessage: resp.statusMessage,
|
|
|
|
headers: Object.keys(resp.headers).reduce((accum, name) => {
|
|
|
|
let value = resp.headers[name];
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
value = value.join(", ");
|
|
|
|
}
|
|
|
|
accum[name] = value;
|
|
|
|
return accum;
|
|
|
|
}, <{ [ name: string ]: string }>{ }),
|
|
|
|
body: null
|
|
|
|
};
|
2020-07-30 17:03:36 -04:00
|
|
|
//resp.setEncoding("utf8");
|
2020-04-18 02:46:52 -04:00
|
|
|
|
2020-07-30 17:03:36 -04:00
|
|
|
resp.on("data", (chunk: Uint8Array) => {
|
|
|
|
if (response.body == null) { response.body = new Uint8Array(0); }
|
|
|
|
response.body = concat([ response.body, chunk ]);
|
2020-04-18 02:46:52 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
resp.on("end", () => {
|
2020-10-07 19:40:28 -04:00
|
|
|
if (response.headers["content-encoding"] === "gzip") {
|
|
|
|
//const size = response.body.length;
|
|
|
|
response.body = arrayify(gunzipSync(response.body));
|
|
|
|
//console.log("Delta:", response.body.length - size, Buffer.from(response.body).toString());
|
|
|
|
}
|
2020-04-18 02:46:52 -04:00
|
|
|
resolve(response);
|
|
|
|
});
|
|
|
|
|
|
|
|
resp.on("error", (error) => {
|
2020-07-13 06:48:33 -04:00
|
|
|
/* istanbul ignore next */
|
2020-04-18 02:46:52 -04:00
|
|
|
(<any>error).response = response;
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
request.on("error", (error) => { reject(error); });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-12 04:38:25 -04:00
|
|
|
// The URL.parse uses null instead of the empty string
|
|
|
|
function nonnull(value: string): string {
|
|
|
|
if (value == null) { return ""; }
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2020-04-18 02:46:52 -04:00
|
|
|
export async function getUrl(href: string, options?: Options): Promise<GetUrlResponse> {
|
|
|
|
if (options == null) { options = { }; }
|
|
|
|
|
2020-04-18 06:32:35 -04:00
|
|
|
// @TODO: Once we drop support for node 8, we can pass the href
|
2021-10-04 11:46:24 -04:00
|
|
|
// directly into request and skip adding the components
|
2020-04-18 06:32:35 -04:00
|
|
|
// to this request object
|
2020-06-12 04:38:25 -04:00
|
|
|
const url = parse(href);
|
2020-04-18 02:46:52 -04:00
|
|
|
|
|
|
|
const request = {
|
2020-06-12 04:38:25 -04:00
|
|
|
protocol: nonnull(url.protocol),
|
|
|
|
hostname: nonnull(url.hostname),
|
|
|
|
port: nonnull(url.port),
|
|
|
|
path: (nonnull(url.pathname) + nonnull(url.search)),
|
2020-04-18 06:32:35 -04:00
|
|
|
|
2020-04-18 02:46:52 -04:00
|
|
|
method: (options.method || "GET"),
|
2020-10-07 19:40:28 -04:00
|
|
|
headers: shallowCopy(options.headers || { }),
|
2020-04-18 02:46:52 -04:00
|
|
|
};
|
|
|
|
|
2020-10-07 19:40:28 -04:00
|
|
|
if (options.allowGzip) {
|
|
|
|
request.headers["accept-encoding"] = "gzip";
|
|
|
|
}
|
|
|
|
|
2020-04-18 02:46:52 -04:00
|
|
|
let req: http.ClientRequest = null;
|
2020-06-12 04:38:25 -04:00
|
|
|
switch (nonnull(url.protocol)) {
|
2020-04-18 06:32:35 -04:00
|
|
|
case "http:":
|
|
|
|
req = http.request(request);
|
2020-04-18 02:46:52 -04:00
|
|
|
break;
|
|
|
|
case "https:":
|
2020-04-18 06:32:35 -04:00
|
|
|
req = https.request(request);
|
2020-04-18 02:46:52 -04:00
|
|
|
break;
|
|
|
|
default:
|
2020-07-13 06:48:33 -04:00
|
|
|
/* istanbul ignore next */
|
2020-04-18 02:46:52 -04:00
|
|
|
logger.throwError(`unsupported protocol ${ url.protocol }`, Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
protocol: url.protocol,
|
|
|
|
operation: "request"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.body) {
|
2020-07-30 17:03:36 -04:00
|
|
|
req.write(Buffer.from(options.body));
|
2020-04-18 02:46:52 -04:00
|
|
|
}
|
|
|
|
req.end();
|
|
|
|
|
|
|
|
const response = await getResponse(req);
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|