eslint-plugin-json-schema-validator
Version:
ESLint plugin that validates data using JSON Schema Validator.
103 lines (100 loc) • 3.1 kB
JavaScript
import { isAbsolute } from "path";
import { pathToFileURL } from "url";
import https from "https";
import http from "http";
import tunnel from "tunnel-agent";
import { createRequire } from "module";
//#region src/utils/http-client/get-modules/http.ts
const TIMEOUT = 6e4;
/**
* GET Method using http modules.
*/
function get$1(url, options) {
return get0(url, options, 0);
}
/** Implementation of HTTP GET method */
function get0(url, options, redirectCount) {
const client = url.startsWith("https") ? https : http;
const parsedOptions = parseUrlAndOptions(url, options || {});
return new Promise((resolve, reject) => {
let result = "";
const req = client.get(parsedOptions, (res) => {
res.on("data", (chunk) => {
result += chunk;
});
res.on("end", () => {
if (res.statusCode && res.statusCode >= 300 && res.statusCode < 400 && redirectCount < 3) {
const location = res.headers.location;
try {
resolve(get0(new URL(location, url).toString(), options, redirectCount + 1));
} catch (e) {
reject(e);
}
return;
}
resolve(result);
});
});
req.on("error", (e) => {
reject(e);
});
req.setTimeout(TIMEOUT, function handleRequestTimeout() {
if (req.destroy) req.destroy();
else req.abort();
reject(/* @__PURE__ */ new Error(`Timeout of ${TIMEOUT}ms exceeded`));
});
});
}
/** Parse URL and options */
function parseUrlAndOptions(urlStr, baseOptions) {
const url = new URL(urlStr);
const hostname = typeof url.hostname === "string" && url.hostname.startsWith("[") ? url.hostname.slice(1, -1) : url.hostname;
const options = {
agent: false,
...baseOptions,
protocol: url.protocol,
hostname,
path: `${url.pathname || ""}${url.search || ""}`
};
if (url.port !== "") options.port = Number(url.port);
if (url.username || url.password) options.auth = `${url.username}:${url.password}`;
const proxyStr = options?.proxy || [
"https_proxy",
"HTTPS_PROXY",
"http_proxy",
"HTTP_PROXY",
"npm_config_https_proxy",
"npm_config_http_proxy"
].map((k) => process.env[k]).find((v) => v);
if (proxyStr) {
const proxyUrl = new URL(proxyStr);
options.agent = tunnel[`http${url.protocol === "https:" ? "s" : ""}OverHttp${proxyUrl.protocol === "https:" ? "s" : ""}`]({ proxy: {
host: proxyUrl.hostname,
port: Number(proxyUrl.port),
proxyAuth: proxyUrl.username || proxyUrl.password ? `${proxyUrl.username}:${proxyUrl.password}` : void 0
} });
}
return options;
}
//#endregion
//#region src/utils/http-client/http.ts
/**
* GET Method
*/
async function get(url, options, httpModulePath) {
const client = httpModulePath ? await loadModule(httpModulePath) : get$1;
return client.default ? client.default(url, options) : client(url, options);
}
/**
* Load module by path
*/
async function loadModule(modulePath) {
const adjustedPath = !modulePath.startsWith("file://") && isAbsolute(modulePath) ? pathToFileURL(modulePath).href : modulePath;
try {
return createRequire(import.meta.url)(adjustedPath);
} catch {
return await import(adjustedPath);
}
}
//#endregion
export { get as t };