2020-04-18 05:14:55 -04:00
|
|
|
"use strict";
|
|
|
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
|
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
|
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
|
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
|
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
|
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
|
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
|
|
});
|
|
|
|
};
|
|
|
|
import http from "http";
|
|
|
|
import https from "https";
|
2020-06-12 04:57:38 -04:00
|
|
|
import { parse } from "url";
|
2020-07-30 17:04:53 -04:00
|
|
|
import { concat } from "@ethersproject/bytes";
|
2020-04-18 05:14:55 -04:00
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
|
|
|
function getResponse(request) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
request.once("response", (resp) => {
|
|
|
|
const response = {
|
|
|
|
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;
|
|
|
|
}, {}),
|
|
|
|
body: null
|
|
|
|
};
|
2020-07-30 17:04:53 -04:00
|
|
|
//resp.setEncoding("utf8");
|
2020-04-18 05:14:55 -04:00
|
|
|
resp.on("data", (chunk) => {
|
|
|
|
if (response.body == null) {
|
2020-07-30 17:04:53 -04:00
|
|
|
response.body = new Uint8Array(0);
|
2020-04-18 05:14:55 -04:00
|
|
|
}
|
2020-07-30 17:04:53 -04:00
|
|
|
response.body = concat([response.body, chunk]);
|
2020-04-18 05:14:55 -04:00
|
|
|
});
|
|
|
|
resp.on("end", () => {
|
|
|
|
resolve(response);
|
|
|
|
});
|
|
|
|
resp.on("error", (error) => {
|
2020-07-13 08:03:56 -04:00
|
|
|
/* istanbul ignore next */
|
2020-04-18 05:14:55 -04:00
|
|
|
error.response = response;
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
request.on("error", (error) => { reject(error); });
|
|
|
|
});
|
|
|
|
}
|
2020-06-12 04:57:38 -04:00
|
|
|
// The URL.parse uses null instead of the empty string
|
|
|
|
function nonnull(value) {
|
|
|
|
if (value == null) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
2020-04-18 05:14:55 -04:00
|
|
|
export function getUrl(href, options) {
|
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
if (options == null) {
|
|
|
|
options = {};
|
|
|
|
}
|
2020-04-18 06:40:36 -04:00
|
|
|
// @TODO: Once we drop support for node 8, we can pass the href
|
|
|
|
// firectly into request and skip adding the components
|
|
|
|
// to this request object
|
2020-06-12 04:57:38 -04:00
|
|
|
const url = parse(href);
|
2020-04-18 05:14:55 -04:00
|
|
|
const request = {
|
2020-06-12 04:57:38 -04:00
|
|
|
protocol: nonnull(url.protocol),
|
|
|
|
hostname: nonnull(url.hostname),
|
|
|
|
port: nonnull(url.port),
|
|
|
|
path: (nonnull(url.pathname) + nonnull(url.search)),
|
2020-04-18 05:14:55 -04:00
|
|
|
method: (options.method || "GET"),
|
|
|
|
headers: (options.headers || {}),
|
|
|
|
};
|
|
|
|
let req = null;
|
2020-06-12 04:57:38 -04:00
|
|
|
switch (nonnull(url.protocol)) {
|
2020-04-18 06:40:36 -04:00
|
|
|
case "http:":
|
|
|
|
req = http.request(request);
|
2020-04-18 05:14:55 -04:00
|
|
|
break;
|
|
|
|
case "https:":
|
2020-04-18 06:40:36 -04:00
|
|
|
req = https.request(request);
|
2020-04-18 05:14:55 -04:00
|
|
|
break;
|
|
|
|
default:
|
2020-07-13 08:03:56 -04:00
|
|
|
/* istanbul ignore next */
|
2020-04-18 05:14:55 -04:00
|
|
|
logger.throwError(`unsupported protocol ${url.protocol}`, Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
protocol: url.protocol,
|
|
|
|
operation: "request"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (options.body) {
|
2020-07-30 17:04:53 -04:00
|
|
|
req.write(Buffer.from(options.body));
|
2020-04-18 05:14:55 -04:00
|
|
|
}
|
|
|
|
req.end();
|
|
|
|
const response = yield getResponse(req);
|
|
|
|
return response;
|
|
|
|
});
|
|
|
|
}
|
2020-07-13 08:03:56 -04:00
|
|
|
//# sourceMappingURL=geturl.js.map
|