nyro
Version:
A simple and effective promise-based HTTP & HTTP/2 request library that supports all HTTP methods.
55 lines (54 loc) • 1.88 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getReusedSocket = getReusedSocket;
exports.getServerIp = getServerIp;
exports.getDefaultUserAgent = getDefaultUserAgent;
exports.generateUniqueId = generateUniqueId;
const package_json_1 = __importDefault(require("../../package.json"));
const os_1 = __importDefault(require("os"));
/**
* Checks if the socket used for the response was reused.
*
* @param res - The HTTP response object.
* @returns A boolean indicating if the socket was reused, or undefined if the information is not available.
*/
function getReusedSocket(res) {
return res?.reusedSocket;
}
;
/**
* Retrieves the IP address of the server from the HTTP response.
*
* @param res - The HTTP response object.
* @returns The server's IP address as a string, or undefined if the information is not available.
*/
function getServerIp(res) {
const socket = res?.socket || res?.connection;
return socket ? socket.remoteAddress : undefined;
}
;
/**
* Generates a default User-Agent string based on the current system's platform, architecture, and Node.js version.
*
* @returns A string representing the default User-Agent.
*/
function getDefaultUserAgent() {
const platform = os_1.default.platform();
const arch = os_1.default.arch();
const nodeVersion = process.version;
return `Nyro/${package_json_1.default.version} (${platform}; ${arch} ${nodeVersion})`;
}
;
/**
* Generates a unique ID based on the current timestamp and a random number.
*
* @returns A unique ID string.
* @example generateUniqueId() // 'id-1633456789000-123456'
*/
function generateUniqueId() {
return 'nyro-' + Date.now() + '-' + Math.floor(Math.random() * 1000000);
}
;