nyro
Version:
A simple and effective promise-based HTTP & HTTP/2 request library that supports all HTTP methods.
55 lines (54 loc) • 2.03 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;
var package_json_1 = __importDefault(require("../../package.json"));
var 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 === null || res === void 0 ? void 0 : 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) {
var socket = (res === null || res === void 0 ? void 0 : res.socket) || (res === null || res === void 0 ? void 0 : 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() {
var platform = os_1.default.platform();
var arch = os_1.default.arch();
var nodeVersion = process.version;
return "Nyro/".concat(package_json_1.default.version, " (").concat(platform, "; ").concat(arch, " ").concat(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);
}
;