nyro
Version:
A simple and effective promise-based HTTP & HTTP/2 request library that supports all HTTP methods.
1,166 lines (1,165 loc) • 64.8 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const http = __importStar(require("http"));
const https = __importStar(require("https"));
const http2 = __importStar(require("http2-wrapper"));
const zlib = __importStar(require("zlib"));
const url_1 = require("url");
const utils_1 = require("./utils");
const combineUrl_1 = __importDefault(require("../helpers/combineUrl"));
const errorHandler_1 = __importDefault(require("../helpers/errorHandler"));
const pluginManager_1 = __importDefault(require("./pluginManager"));
const http_proxy_agent_1 = require("http-proxy-agent");
const https_proxy_agent_1 = require("https-proxy-agent");
const socks_proxy_agent_1 = require("socks-proxy-agent");
const stream_1 = require("stream");
const events_1 = require("events");
const package_json_1 = __importDefault(require("../../package.json"));
;
const cacheStore = new Map();
;
;
;
;
class Core extends events_1.EventEmitter {
baseRequestOptions;
pluginManager = new pluginManager_1.default();
constructor(baseRequestOptions) {
super();
this.baseRequestOptions = baseRequestOptions || {};
}
;
use(plugin) {
return this.pluginManager.use(plugin);
}
;
on(event, listener) {
return super.on(event, listener);
}
once(event, listener) {
return super.once(event, listener);
}
off(event, listener) {
return super.off(event, listener);
}
emit(event, ...args) {
return super.emit(event, ...args);
}
/**
* The version of the Nyro library.
*/
static version = `${package_json_1.default.version}`;
/**
* The package.json file for the Nyro library.
*/
static pkg = package_json_1.default;
/**
* @param url
* @returns this
* @example Nyro.setURL('https://jsonplaceholder.typicode.com/posts');
* @description This function sets the URL for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setURL(url) {
this.baseRequestOptions.url = url;
return this;
}
;
/**
* @param baseURL
* @returns this
* @example Nyro.setBaseURL('https://jsonplaceholder.typicode.com');
* @description This function sets the base URL for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setBaseURL(baseURL) {
this.baseRequestOptions.baseURL = baseURL;
return this;
}
;
/**
* @param path
* @returns this
* @example Nyro.setPath('/posts');
* @description This function sets the path for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setPath(path) {
this.baseRequestOptions.path = path;
return this;
}
;
/**
* @param bodySchema
* @returns this
* @example Nyro.setBodySchema({ title: String, body: String });
* @description This function sets the body schema for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setBodySchema(bodySchema) {
this.baseRequestOptions.bodySchema = bodySchema;
return this;
}
;
/**
* @param auth
* @returns this
* @example Nyro.setAuth({ username: 'user', password: 'pass' });
* @description This function sets the authentication credentials for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setAuth(auth) {
this.baseRequestOptions.auth = auth;
return this;
}
;
/**
* @param proxy
* @returns this
* @example Nyro.setProxy({ host: 'localhost', port: 8080, protocol: 'http' });
* @description This function sets the proxy for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setProxy(proxy) {
this.baseRequestOptions.proxy = proxy;
return this;
}
;
/**
* @param method
* @returns this
* @example Nyro.setMethod('GET');
* @description This function sets the method for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setMethod(method) {
this.baseRequestOptions.method = method;
return this;
}
;
/**
* @param headers
* @returns this
* @example Nyro.setHeaders({ 'Content-Type': 'application/json' });
* @description This function sets the headers for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setHeaders(headers) {
this.baseRequestOptions.headers = headers;
return this;
}
;
/**
* @param params
* @returns this
* @example Nyro.setParams({ id: '1' });
* @description This function sets the query parameters for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setParams(params) {
this.baseRequestOptions.params = params;
return this;
}
;
/**
* @param query
* @returns this
* @example Nyro.setQuery({ id: '1' });
* @description This function sets the query parameters for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setQuery(query) {
this.baseRequestOptions.query = query;
return this;
}
;
/**
* @param body
* @returns this
* @example Nyro.setBody({ title: 'foo', body: 'bar', userId: 1 });
* @description This function sets the body for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setBody(body) {
this.baseRequestOptions.body = body;
return this;
}
;
/**
* @param timeout
* @returns this
* @example Nyro.setTimeout(5000);
* @description This function sets the timeout for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setTimeout(timeout) {
this.baseRequestOptions.timeout = timeout;
return this;
}
;
/**
* @param retryOn
* @returns this
* @example Nyro.setRetryOn((req, error) => error.code === 'ETIMEDOUT');
* @description This function sets the retry condition for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setRetryOn(retryOn) {
this.baseRequestOptions.onRetry = retryOn;
return this;
}
;
/**
* @param retries
* @returns this
* @example Nyro.setRetries(3);
* @description This function sets the number of retries for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setRetries(retries) {
this.baseRequestOptions.retries = retries;
return this;
}
;
/**
* @param validateStatus
* @returns this
* @example Nyro.setValidateStatus((status) => status >= 200 && status < 300);
* @description This function sets the status validation for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setValidateStatus(validateStatus) {
this.baseRequestOptions.validateStatus = validateStatus;
return this;
}
;
/**
* @param maxBodyLength
* @returns this
* @example Nyro.setMaxBodyLength(1000);
* @description This function sets the maximum body length for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setMaxBodyLength(maxBodyLength) {
this.baseRequestOptions.maxBodyLength = maxBodyLength;
return this;
}
;
/**
* @param maxContentLength
* @returns this
* @example Nyro.setMaxContentLength(1000);
* @description This function sets the maximum content length for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setMaxContentLength(maxContentLength) {
this.baseRequestOptions.maxContentLength = maxContentLength;
return this;
}
;
/**
* @param maxRate
* @returns this
* @example Nyro.setMaxRate(1000);
* @description This function sets the maximum rate for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setMaxRate(maxRate) {
this.baseRequestOptions.maxRate = maxRate;
return this;
}
;
/**
* @param signal
* @returns this
* @example Nyro.setSignal(signal);
* @description This function sets the signal for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setSignal(signal) {
this.baseRequestOptions.signal = signal;
return this;
}
;
/**
* @param onDownloadProgress
* @returns this
* @example Nyro.setOnDownloadProgress((progress) => console.log(progress));
* @description This function sets the download progress for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setOnDownloadProgress(onDownloadProgress) {
this.baseRequestOptions.onDownloadProgress = onDownloadProgress;
return this;
}
;
/**
* @param timeoutErrorMessage
* @returns this
* @example Nyro.setTimeoutErrorMessage('Request timed out');
* @description This function sets the timeout error message for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setTimeoutErrorMessage(timeoutErrorMessage) {
this.baseRequestOptions.timeoutErrorMessage = timeoutErrorMessage;
return this;
}
;
/**
* @param responseType
* @returns this
* @example Nyro.setResponseType('json');
* @description This function sets the response type for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setResponseType(responseType) {
this.baseRequestOptions.responseType = responseType;
return this;
}
;
/**
* @param responseEncoding
* @returns this
* @example Nyro.setResponseEncoding('utf8');
* @description This function sets the response encoding for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setResponseEncoding(responseEncoding) {
this.baseRequestOptions.responseEncoding = responseEncoding;
return this;
}
;
/**
* @param maxRedirects
* @returns this
* @example Nyro.setMaxRedirects(3);
* @description This function sets the maximum number of redirects for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setMaxRedirects(maxRedirects) {
this.baseRequestOptions.maxRedirects = maxRedirects;
return this;
}
;
/**
* @param retryDelay
* @returns this
* @example Nyro.setRetryDelay(1000);
* @description This function sets the retry delay for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setRetryDelay(retryDelay) {
this.baseRequestOptions.retryDelay = retryDelay;
return this;
}
;
/**
* @param decompress
* @returns this
* @example Nyro.setDecompress(true);
* @description This function sets the decompress option for the request.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
*/
setDecompress(decompress) {
this.baseRequestOptions.decompress = decompress;
return this;
}
;
/*-------------------------------------------------------*/
/*----------------LINE-------------BREAK-----------------*/
/*-------------------------------------------------------*/
/**
* Sends a GET request to the specified URL.
* @param url - The URL to send the request to.
* @param options - The request options.
* @returns A promise that resolves with the HTTP response.
* @example Nyro.get('https://jsonplaceholder.typicode.com/posts');
* @description This function sends a GET request to the specified URL and returns a promise that resolves with the HTTP response.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET|MDN web docs}
*/
async get(url, options) {
let method = 'GET';
if (this.baseRequestOptions && this.baseRequestOptions.method !== method)
this.baseRequestOptions.method = method;
return this.request(!url ? this.baseRequestOptions : { ...options, method: this.baseRequestOptions.method || method, url });
}
;
/**
* Sends a POST request to the specified URL.
* @param url - The URL to send the request to.
* @param options - The request options.
* @returns A promise that resolves with the HTTP response.
* @example Nyro.post('https://jsonplaceholder.typicode.com/posts');
* @description This function sends a POST request to the specified URL and returns a promise that resolves with the HTTP response.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST|MDN web docs}
*/
async post(url, options) {
let method = 'POST';
if (this.baseRequestOptions && this.baseRequestOptions.method !== method)
this.baseRequestOptions.method = method;
return this.request(!url ? this.baseRequestOptions : { ...options, method: this.baseRequestOptions.method || method, url });
}
;
/**
* Sends a PUT request to the specified URL.
* @param url - The URL to send the request to.
* @param options - The request options.
* @returns A promise that resolves with the HTTP response.
* @example Nyro.put('https://jsonplaceholder.typicode.com/posts');
* @description This function sends a PUT request to the specified URL and returns a promise that resolves with the HTTP response.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT|MDN web docs}
*/
async put(url, options) {
let method = 'PUT';
if (this.baseRequestOptions && this.baseRequestOptions.method !== method)
this.baseRequestOptions.method = method;
return this.request(!url ? this.baseRequestOptions : { ...options, method: this.baseRequestOptions.method || method, url });
}
;
/**
* Sends a DELETE request to the specified URL.
* @param url - The URL to send the request to.
* @param options - The request options.
* @returns A promise that resolves with the HTTP response.
* @example Nyro.delete('https://jsonplaceholder.typicode.com/posts');
* @description This function sends a DELETE request to the specified URL and returns a promise that resolves with the HTTP response.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE|MDN web docs}
*/
async delete(url, options) {
let method = 'DELETE';
if (this.baseRequestOptions && this.baseRequestOptions.method !== method)
this.baseRequestOptions.method = method;
return this.request(!url ? this.baseRequestOptions : { ...options, method: this.baseRequestOptions.method || method, url });
}
;
/**
* Sends a PATCH request to the specified URL.
* @param url - The URL to send the request to.
* @param options - The request options.
* @returns A promise that resolves with the HTTP response.
* @example Nyro.patch('https://jsonplaceholder.typicode.com/posts');
* @description This function sends a PATCH request to the specified URL and returns a promise that resolves with the HTTP response.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH|MDN web docs}
*/
async patch(url, options) {
let method = 'PATCH';
if (this.baseRequestOptions && this.baseRequestOptions.method !== method)
this.baseRequestOptions.method = method;
return this.request(!url ? this.baseRequestOptions : { ...options, method: this.baseRequestOptions.method || method, url });
}
;
/**
* Sends a HEAD request to the specified URL.
* @param url - The URL to send the request to.
* @param options - The request options.
* @returns A promise that resolves with the HTTP response.
* @example Nyro.head('https://jsonplaceholder.typicode.com/posts');
* @description This function sends a HEAD request to the specified URL and returns a promise that resolves with the HTTP response.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD|MDN web docs}
*/
async head(url, options) {
let method = 'HEAD';
if (this.baseRequestOptions && this.baseRequestOptions.method !== method)
this.baseRequestOptions.method = method;
return this.request(!url ? this.baseRequestOptions : { ...options, method: this.baseRequestOptions.method || method, url });
}
;
/**
* Sends an OPTIONS request to the specified URL.
* @param url - The URL to send the request to.
* @param options - The request options.
* @returns A promise that resolves with the HTTP response.
* @example Nyro.options('https://jsonplaceholder.typicode.com/posts');
* @description This function sends an OPTIONS request to the specified URL and returns a promise that resolves with the HTTP response.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS|MDN web docs}
*/
async options(url, options) {
let method = 'OPTIONS';
if (this.baseRequestOptions && this.baseRequestOptions.method !== method)
this.baseRequestOptions.method = method;
return this.request(!url ? this.baseRequestOptions : { ...options, method: this.baseRequestOptions.method || method, url });
}
;
/**
* Sends a CONNECT request to the specified URL.
* @param url - The URL to send the request to.
* @param options - The request options.
* @returns A promise that resolves with the HTTP response.
* @example Nyro.connect('https://jsonplaceholder.typicode.com/posts');
* @description This function sends a CONNECT request to the specified URL and returns a promise that resolves with the HTTP response.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/CONNECT|MDN web docs}
*/
async connect(url, options) {
let method = 'CONNECT';
if (this.baseRequestOptions && this.baseRequestOptions.method !== method)
this.baseRequestOptions.method = method;
return this.request(!url ? this.baseRequestOptions : { ...options, method: this.baseRequestOptions.method || method, url });
}
;
/**
* Sends a TRACE request to the specified URL.
* @param url - The URL to send the request to.
* @param options - The request options.
* @returns A promise that resolves with the HTTP response.
* @example Nyro.trace('https://jsonplaceholder.typicode.com/posts');
* @description This function sends a TRACE request to the specified URL and returns a promise that resolves with the HTTP response.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/TRACE|MDN web docs}
*/
async trace(url, options) {
return this.request(!url ? this.baseRequestOptions : { ...options, method: 'TRACE', url });
}
;
/**
* Downloads a file from the specified URL.
* @param url - The URL to download the file from.
* @param options - The request options.
* @returns A promise that resolves with the HTTP response.
* @example Nyro.download('https://jsonplaceholder.typicode.com/posts');
* @description This function downloads a file from the specified URL and returns a promise that resolves with the HTTP response.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET|MDN web docs}
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition|MDN web docs}
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type|MDN web docs}
*/
async download(url, options) {
return this.request({ ...options, responseType: 'stream', isStream: true, method: 'GET', url });
}
;
/**
* Sends a request to the specified URL with pagination.
* @param options - The request options.
* @param paginationOptions - The pagination options.
* @returns A promise that resolves with an array of HTTP responses.
* @example Nyro.pagination({ url: 'https://jsonplaceholder.typicode.com/posts', method: 'GET' }, { pageParam: 'page', limitParam: 'limit', maxPages: 3 });
* @description This function sends a request to the specified URL with pagination and returns a promise that resolves with an array of HTTP responses.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods|MDN web docs}
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Status|MDN web docs}
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Response_headers|MDN web docs}
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Link|MDN web docs}
*/
async pagination(options, paginationOptions) {
const results = [];
let page = 1;
let hasMore = true;
while (hasMore) {
const paginatedOptions = {
...options,
params: { ...options?.params, [paginationOptions?.pageParam || 'page']: page }
};
const response = await this.request(paginatedOptions);
if (Array.isArray(response)) {
results.push(...response);
}
else {
results.push(response);
}
hasMore = Array.isArray(response) && response.length > 0 && (!(paginationOptions?.maxPages ?? 0) || page < (paginationOptions?.maxPages ?? 0));
page++;
}
return results;
}
;
/**
* Sends multiple requests to the specified URLs.
* @param requests - The request options.
* @returns A promise that resolves with an array of HTTP responses.
* @example Nyro.queue([
* { url: 'https://jsonplaceholder.typicode.com/posts/1', method: 'GET' },
* { url: 'https://jsonplaceholder.typicode.com/posts/2', method: 'POST' }
* ]);
* @description This function sends multiple requests to the specified URLs and returns a promise that resolves with an array of HTTP responses.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods|MDN web docs}
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Status|MDN web docs}
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Response_headers|MDN web docs}
*/
async queue(requests, queueOptions) {
return Promise.all(requests.map((request, index) => new Promise((resolve) => setTimeout(() => resolve(this.request(request)), index * (queueOptions?.delay ?? 0)))));
}
;
/**
* Extends the default request options with the provided options.
*
* @param extendOptions - The options to extend the default request options with.
* @returns An object with the execute function to make the request and the options used for the request.
* @example Nyro.extend({
* url: 'https://jsonplaceholder.typicode.com/posts',
* method: 'GET',
* headers: {
* 'Content-Type': 'application/json'
* }
* });
* @description This function allows you to create a new request with the provided options, while keeping the default options for future requests.
*/
async extend(extendOptions) {
var options = { ...this.baseRequestOptions, ...extendOptions };
return new Core(options);
}
;
/**
* Creates a new instance of the Nyro library with the provided options.
*
* @param options - The request options.
* @returns A new instance of the Nyro library with the provided options.
* @example Nyro.create({
* url: 'https://jsonplaceholder.typicode.com/posts',
* method: 'GET',
* headers: {
* 'Content-Type': 'application/json'
* }
* });
* @description This function creates a new instance of the Nyro library with the provided options.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods|MDN web docs}
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers|MDN web docs}
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Status|MDN web docs}
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Response_headers|MDN web docs}
*/
async create(options) {
return new Core(options);
}
/**
* Core function for handling HTTP requests.
*
* @param options - The request options.
* @param currentRedirects - The number of redirects that have occurred.
* @returns A promise that resolves with the HTTP response.
*/
async request(options, currentRedirects = 0, attempt = 1, visitedUrls = new Set()) {
if (!options) {
options = { ...this.baseRequestOptions };
}
else {
options = { ...this.baseRequestOptions, ...options };
}
;
const combinedURL = (0, combineUrl_1.default)(options?.baseURL || '', options?.url || "", options?.path || '');
try {
var fullUrl = new url_1.URL(combinedURL);
}
catch (error) {
/*
this.emit('error', ({
statusCode: 400,
message: `Invalid URL: ${combinedURL}`,
name: 'Request',
requestOptions: options,
}));
*/
return Promise.reject(new errorHandler_1.default({
statusCode: 400,
message: `Invalid URL: ${combinedURL}`,
name: 'Request',
requestOptions: options,
}));
}
visitedUrls.add(fullUrl.toString());
if (options?.signal?.aborted) {
/*
this.emit('error',({
statusCode: 0,
message: 'Request aborted',
name: 'Request',
requestOptions: options,
}));
*/
return Promise.reject(new errorHandler_1.default({
statusCode: 0,
message: 'Request aborted',
name: 'Request',
requestOptions: options,
}));
}
;
if (options?.port) {
options.port = options?.port || (fullUrl.protocol === 'https:' ? 443 : 80);
}
;
if (options?.path) {
fullUrl.pathname += options.path;
}
if (options?.isStream) {
options.responseType = 'stream';
}
if (options?.params) {
const params = new url_1.URLSearchParams(options.params);
fullUrl.search = params.toString();
}
if (['json', 'text', 'blob', 'stream', 'arrayBuffer', 'document'].indexOf(options?.responseType || 'json') === -1) {
/*
this.emit('error',({
statusCode: 400,
message: `Invalid response type: ${options?.responseType}`,
name: 'Request',
requestOptions: options,
}));
*/
return Promise.reject(new errorHandler_1.default({
statusCode: 400,
message: `Invalid response type: ${options?.responseType}`,
name: 'Request',
requestOptions: options,
}));
}
;
options.method = options.method?.toUpperCase() || 'GET';
if (options && options.method && ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS', 'CONNECT', 'TRACE'].indexOf(options.method) === -1) {
/*
this.emit('error', ({
statusCode: 400,
message: `Invalid request method: ${options.method}`,
name: 'Request',
requestOptions: options,
}));
*/
return Promise.reject(new errorHandler_1.default({
statusCode: 400,
message: `Invalid request method: ${options.method}`,
name: 'Request',
requestOptions: options,
}));
}
;
if (options?.timeout && options.timeout < 0) {
/*
this.emit('error', ({
statusCode: 400,
message: `Invalid timeout: ${options.timeout}`,
name: 'Request',
requestOptions: options,
}));
*/
return Promise.reject(new errorHandler_1.default({
statusCode: 400,
message: `Invalid timeout: ${options.timeout}`,
name: 'Request',
requestOptions: options,
}));
}
;
if (options?.maxRedirects && options.maxRedirects < 0) {
/*
this.emit('error', ({
statusCode: 400,
message: `Invalid number of redirects: ${options.maxRedirects}`,
name: 'Request',
requestOptions: options,
}));
*/
return Promise.reject(new errorHandler_1.default({
statusCode: 400,
message: `Invalid number of redirects: ${options.maxRedirects}`,
name: 'Request',
requestOptions: options,
}));
}
;
if (options?.maxBodyLength && options.maxBodyLength < 0) {
/*
this.emit('error', ({
statusCode: 400,
message: `Invalid max body length: ${options.maxBodyLength}`,
name: 'Request',
requestOptions: options,
}));
*/
return Promise.reject(new errorHandler_1.default({
statusCode: 400,
message: `Invalid max body length: ${options.maxBodyLength}`,
name: 'Request',
requestOptions: options,
}));
}
;
if (options?.maxContentLength && options.maxContentLength < 0) {
/*
this.emit('error', ({
statusCode: 400,
message: `Invalid max content length: ${options.maxContentLength}`,
name: 'Request',
requestOptions: options,
}));
*/
return Promise.reject(new errorHandler_1.default({
statusCode: 400,
message: `Invalid max content length: ${options.maxContentLength}`,
name: 'Request',
requestOptions: options,
}));
}
;
if (options?.maxRate && options.maxRate < 0) {
/*
this.emit('error', ({
statusCode: 400,
message: `Invalid max rate: ${options.maxRate}`,
name: 'Request',
requestOptions: options,
}));
*/
return Promise.reject(new errorHandler_1.default({
statusCode: 400,
message: `Invalid max rate: ${options.maxRate}`,
name: 'Request',
requestOptions: options,
}));
}
;
if (options?.retryDelay && options.retryDelay < 0) {
/*
this.emit('error', ({
statusCode: 400,
message: `Invalid retry delay: ${options.retryDelay}`,
name: 'Request',
requestOptions: options,
}));
*/
return Promise.reject(new errorHandler_1.default({
statusCode: 400,
message: `Invalid retry delay: ${options.retryDelay}`,
name: 'Request',
requestOptions: options,
}));
}
;
if (options?.retries && options.retries < 0) {
/*
this.emit('error', ({
statusCode: 400,
message: `Invalid number of retries: ${options.retries}`,
name: 'Request',
requestOptions: options
}));
*/
return Promise.reject(new errorHandler_1.default({
statusCode: 400,
message: `Invalid number of retries: ${options.retries}`,
name: 'Request',
requestOptions: options
}));
}
;
if (options?.query) {
const query = new url_1.URLSearchParams();
for (const key in options.query) {
if (Object.prototype.hasOwnProperty.call(options.query, key)) {
query.append(key, String(options.query[key]));
}
}
fullUrl.search += (fullUrl.search ? '&' : '') + query.toString();
}
if (options.useHttp2 == undefined)
options.useHttp2 = true;
if (options.useHttp2 && !http2) {
/*
this.emit('error', ({
statusCode: 400,
message: 'http2 is not available in this environment',
name: 'Request',
requestOptions: options,
}));
*/
return Promise.reject(new errorHandler_1.default({
statusCode: 400,
message: 'http2 is not available in this environment',
name: 'Request',
requestOptions: options,
}));
}
var isHttps = fullUrl.protocol === 'https:';
if (!isHttps && options.sslOptions?.passphrase || options.sslOptions?.ca || options.sslOptions?.cert || options.sslOptions?.key || options.sslOptions?.rejectUnauthorized || options.sslOptions?.secureProtocol) {
/*
this.emit('error', ({
statusCode: 400,
message: 'SSL options are only supported for HTTPS requests',
name: 'Request',
requestOptions: options,
}));
*/
return Promise.reject(new errorHandler_1.default({
statusCode: 400,
message: 'SSL options are only supported for HTTPS requests',
name: 'Request',
requestOptions: options,
}));
}
if (!options.headers)
options.headers = {};
if (options?.headers) {
if (!options.headers['User-Agent'])
options.headers['User-Agent'] = (0, utils_1.getDefaultUserAgent)();
if (!options.headers['Accept'])
options.headers['Accept'] = `*/*`;
if (!options.headers['Content-Type'])
options.headers['Content-Type'] = 'application/json';
if (!options.headers['Content-Length'])
options.headers['Content-Length'] = '0';
}
if (options && !options?.responseType) {
options.responseType = 'json';
}
if (options?.auth && options?.headers) {
const { username, password } = options.auth;
const token = Buffer.from(`${username}:${password}`).toString('base64');
options.headers['Authorization'] = `Basic ${token}`;
}
const onRequest = options?.onRequest || ((requestOptions) => requestOptions);
const validateStatus = options?.validateStatus || ((status) => status >= 200 && status < 300);
const onResponse = options?.onResponse || ((response) => response);
const onTimeout = options?.onTimeout || (() => { });
const onRedirect = options?.onRedirect || ((response) => response);
const onChunk = options?.onChunk || ((chunk) => chunk);
if (!options?.requestId)
options.requestId = (0, utils_1.generateUniqueId)();
if (!options?.defaultMode) {
var onRequestOptions = onRequest(options);
if (onRequestOptions)
options = { ...onRequest(options), ...options };
if (this.pluginManager)
options = this.pluginManager.applyOnRequest(options);
this.emit('beforeRequest', options);
}
;
var requestOptions = {
method: options.method,
headers: options?.headers,
...options.sslOptions,
};
if (options?.timeout) {
requestOptions.timeout = options.timeout;
}
if (options?.signal) {
requestOptions.signal = options.signal;
}
if (options?.proxy) {
var proxyAuth = options.proxy.auth ? `${options.proxy.auth.username}:${options.proxy.auth.password}` : '';
var proxyUrl = `${options.proxy.host}:${options.proxy.port}`;
var protocol = options.proxy?.protocol ? options.proxy.protocol : 'http';
requestOptions.agent = protocol.includes('socks')
? new socks_proxy_agent_1.SocksProxyAgent(`${protocol}://${proxyAuth ? `${proxyAuth}@` : ''}${proxyUrl}`) : isHttps
? new https_proxy_agent_1.HttpsProxyAgent(`${protocol}://${proxyAuth ? `${proxyAuth}@` : ''}${proxyUrl}`)
: new http_proxy_agent_1.HttpProxyAgent(`${protocol}://${proxyAuth ? `${proxyAuth}@` : ''}${proxyUrl}`);
}
const dataString = options?.body ? JSON.stringify(options.body) : null;
if (dataString) {
if (options?.maxBodyLength && Buffer.byteLength(dataString) > options.maxBodyLength) {
return Promise.reject(new errorHandler_1.default({
statusCode: 413,
message: `Request body size exceeds maxBodyLength of ${options.maxBodyLength} bytes`,
name: 'Request',
requestOptions: options
}));
}
requestOptions.headers['Content-Length'] = Buffer.byteLength(dataString).toString();
}
const startTimestamp = Date.now();
var lib = options.useHttp2 && isHttps ? http2.request(fullUrl.toString(), requestOptions) : isHttps ? https.request(fullUrl.toString(), requestOptions) : http.request(fullUrl.toString(), requestOptions);
return new Promise((resolve, reject) => {
const req = lib.on('response', (res) => {
var cacheKey = `${options.method}:${fullUrl.toString()}`;
if (options.cache && cacheStore.has(cacheKey)) {
const cachedItem = cacheStore.get(cacheKey);
if (cachedItem && Date.now() < cachedItem.expiry) {
cachedItem.response.isCached = true;
resolve(cachedItem.response);
}
else {
cacheStore.delete(cacheKey);
}
}
var chunks = [];
let responseData;
let totalLength = 0;
let responseSize = 0;
let downloaded = 0;
let lastTimestamp = startTimestamp;
const contentLength = parseInt(res.headers['content-length'] ?? '0', 10) || null;
const connectionReused = (0, utils_1.getReusedSocket)(res);
const serverIp = (0, utils_1.getServerIp)(res);
if (options?.responseType === 'stream') {
if (options.cache)
reject(new errorHandler_1.default({
statusCode: 400,
message: `Stream responses cannot be cached`,
name: 'Request',
requestOptions: options,
}));
const stream = new stream_1.PassThrough();
res.pipe(stream);
const response = {
request: req,
response: res,
headers: res.headers,
config: options,
requestInfo: {
method: options?.method,
url: options?.url,
fullUrl: fullUrl.href,
headers: options?.headers || {},
body: options?.body,
httpVersion: res.httpVersion,
startTimestamp,
timeout: options?.timeout,
contentLength: dataString ? Buffer.byteLength(dataString) : 0,
},
body: stream,
statusCode: res.statusCode,
statusText: res.statusMessage || '',
timestamp: {
startTimestamp,
endTimestamp: Date.now(),
},
responseTime: Date.now() - startTimestamp,
responseSize: 0,
serverIp,
connectionReused: connectionReused || false,
isStream: true,
isCached: false,
};
if (!options?.defaultMode) {
stream.on('data', (chunk) => { onChunk(chunk); });
this.emit('afterResponse', response);
}
;
resolve(response);
}
else {
res.on('data', (chunk) => {
totalLength += chunk.length;
responseSize += chunk.length;
downloaded += chunk.length;
const currentTimestamp = Date.now();
const timeElapsed = (currentTimestamp - lastTimestamp) / 1000;
lastTimestamp = currentTimestamp;
const rate = chunk.length / timeElapsed;
if (contentLength && options?.onDownloadProgress) {
const progress = Math.min(1, downloaded / contentLength);
options.onDownloadProgress({
percent: progress * 100,
transferredBytes: downloaded,
totalBytes: contentLength,
});
}
;
if (options?.maxContentLength && responseSize > options.maxContentLength) {
req.destroy();
/*
this.emit('error', ({
statusCode: 413,
message: `Response size exceeds maxContentLength of ${options.maxContentLength} bytes`,
name: 'Request',
requestOptions: options,
}));
*/
reject(new errorHandler_1.default({
statusCode: 413,
message: `Response size exceeds maxContentLength of ${options.maxContentLength} bytes`,
name: 'Request',
requestOptions: options,
}));
return;
}
;
if (options?.maxRate && rate > options.maxRate) {
res.pause();
setTimeout(() => {
res.resume();
}, (chunk.length / options.maxRate) * 1000);
}
;
chunks.push(chunk);
});
res.on('end', async () => {
const endTime = Date.now();
const responseTime = endTime - startTimestamp;
let rawData = Buffer.concat(chunks);
if (!options?.decompress) {
const encoding = res.headers['content-encoding'];
if (encoding === 'gzip') {
rawData = zlib.gunzipSync(rawData);
}
else if (encoding === 'deflate') {
rawData = zlib.inflateSync(rawData);
}
else if (encoding === 'br') {
rawData = zlib.brotliDecompressSync(rawData);
}
}
if (res.statusCode && [301, 302, 303, 307, 308].includes(res.statusCode)) {
if (currentRedirects >= (options?.maxRedirects || 5)) {
/*
this.emit('error', ({
statusCode: 310,
message: `Exceeded maximum number of redirects: ${options?.maxRedirects || 5}`,
name: 'Request',
requestOptions: options,
}));
*/
reject(new errorHandler_1.default({
statusCode: 310,
message: `Exceeded maximum number of redirects: ${options?.maxRedirects || 5}`,
name: 'Request',
requestOptions: options,
}));
return;
}
if (!options.defaultMode) {
onRedirect(res);
}
if (!res.headers.location) {
/*
this.emit('error', ({
statusCode: 310,
message: `Redirect location header missing`,
name: 'Request',
requestOptions: options,
}));
*/
reject(new errorHandler_1.default({
statusCode: 310,
message: `Redirect location header missing`,
name: 'Request',
requestOptions: options,
}));
return;
}
var newUrl = new url_1.URL(res.headers.location);
if (visitedUrls.has(newUrl.toString())) {
/*
this.emit('error', ({
statusCode: 508,
message: `Redirect loop detected`,
name: 'Request',
requestOptions: options,
}));
*/
reject(new errorHandler_1.default({
statusCode: 508,
message: `Redirect loop detected`,
name: 'Request',
requestOptions: options,