2022-09-05 16:57:11 -04:00
|
|
|
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2023-10-09 20:27:56 -04:00
|
|
|
exports.getUrl = exports.createGetUrl = void 0;
|
2023-04-25 20:04:48 +09:00
|
|
|
const tslib_1 = require("tslib");
|
|
|
|
const http_1 = tslib_1.__importDefault(require("http"));
|
|
|
|
const https_1 = tslib_1.__importDefault(require("https"));
|
2022-09-05 16:57:11 -04:00
|
|
|
const zlib_1 = require("zlib");
|
2022-09-15 22:58:45 -04:00
|
|
|
const errors_js_1 = require("./errors.js");
|
|
|
|
const data_js_1 = require("./data.js");
|
2022-12-10 01:31:42 -05:00
|
|
|
/**
|
|
|
|
* @_ignore:
|
|
|
|
*/
|
2023-10-09 20:27:56 -04:00
|
|
|
function createGetUrl(options) {
|
|
|
|
async function getUrl(req, signal) {
|
|
|
|
const protocol = req.url.split(":")[0].toLowerCase();
|
|
|
|
(0, errors_js_1.assert)(protocol === "http" || protocol === "https", `unsupported protocol ${protocol}`, "UNSUPPORTED_OPERATION", {
|
|
|
|
info: { protocol },
|
|
|
|
operation: "request"
|
|
|
|
});
|
|
|
|
(0, errors_js_1.assert)(protocol === "https" || !req.credentials || req.allowInsecureAuthentication, "insecure authorized connections unsupported", "UNSUPPORTED_OPERATION", {
|
|
|
|
operation: "request"
|
|
|
|
});
|
|
|
|
const method = req.method;
|
|
|
|
const headers = Object.assign({}, req.headers);
|
|
|
|
const reqOptions = { method, headers };
|
|
|
|
if (options) {
|
|
|
|
if (options.agent) {
|
|
|
|
reqOptions.agent = options.agent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const request = ((protocol === "http") ? http_1.default : https_1.default).request(req.url, reqOptions);
|
|
|
|
request.setTimeout(req.timeout);
|
|
|
|
const body = req.body;
|
|
|
|
if (body) {
|
|
|
|
request.write(Buffer.from(body));
|
|
|
|
}
|
|
|
|
request.end();
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// @TODO: Node 15 added AbortSignal; once we drop support for
|
|
|
|
// Node14, we can add that in here too
|
|
|
|
request.once("response", (resp) => {
|
|
|
|
const statusCode = resp.statusCode || 0;
|
|
|
|
const statusMessage = resp.statusMessage || "";
|
|
|
|
const headers = Object.keys(resp.headers || {}).reduce((accum, name) => {
|
|
|
|
let value = resp.headers[name] || "";
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
value = value.join(", ");
|
2022-09-05 16:57:11 -04:00
|
|
|
}
|
2023-10-09 20:27:56 -04:00
|
|
|
accum[name] = value;
|
|
|
|
return accum;
|
|
|
|
}, {});
|
|
|
|
let body = null;
|
|
|
|
//resp.setEncoding("utf8");
|
|
|
|
resp.on("data", (chunk) => {
|
|
|
|
if (signal) {
|
|
|
|
try {
|
|
|
|
signal.checkSignal();
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
return reject(error);
|
|
|
|
}
|
2022-09-05 16:57:11 -04:00
|
|
|
}
|
2023-10-09 20:27:56 -04:00
|
|
|
if (body == null) {
|
|
|
|
body = chunk;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const newBody = new Uint8Array(body.length + chunk.length);
|
|
|
|
newBody.set(body, 0);
|
|
|
|
newBody.set(chunk, body.length);
|
|
|
|
body = newBody;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
resp.on("end", () => {
|
|
|
|
if (headers["content-encoding"] === "gzip" && body) {
|
|
|
|
body = (0, data_js_1.getBytes)((0, zlib_1.gunzipSync)(body));
|
|
|
|
}
|
|
|
|
resolve({ statusCode, statusMessage, headers, body });
|
|
|
|
});
|
|
|
|
resp.on("error", (error) => {
|
|
|
|
//@TODO: Should this just return nornal response with a server error?
|
|
|
|
error.response = { statusCode, statusMessage, headers, body };
|
|
|
|
reject(error);
|
|
|
|
});
|
2022-09-05 16:57:11 -04:00
|
|
|
});
|
2023-10-09 20:27:56 -04:00
|
|
|
request.on("error", (error) => { reject(error); });
|
2022-09-05 16:57:11 -04:00
|
|
|
});
|
2023-10-09 20:27:56 -04:00
|
|
|
}
|
|
|
|
return getUrl;
|
|
|
|
}
|
|
|
|
exports.createGetUrl = createGetUrl;
|
|
|
|
// @TODO: remove in v7; provided for backwards compat
|
|
|
|
const defaultGetUrl = createGetUrl({});
|
|
|
|
/**
|
|
|
|
* @_ignore:
|
|
|
|
*/
|
|
|
|
async function getUrl(req, signal) {
|
|
|
|
return defaultGetUrl(req, signal);
|
2022-09-05 16:57:11 -04:00
|
|
|
}
|
|
|
|
exports.getUrl = getUrl;
|
|
|
|
//# sourceMappingURL=geturl.js.map
|