UNPKG

xud

Version:
45 lines 1.86 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const assert_1 = __importDefault(require("assert")); /** Helper methods for interacting with the [[Address]] type. */ const addressUtils = { /** * Create an [[Address]] using the remote host and port of a socket. */ fromSocket: (socket) => { const { remoteAddress, remotePort } = socket; assert_1.default(remoteAddress, 'socket must have a remoteAddress value'); assert_1.default(remotePort, 'socket must have a remotePort value'); return { host: remoteAddress, port: remotePort }; }, /** * Create an [[Address]] from a string. * @param addressString a string in the "{host}:{port}" format * @param port a port number to use if no port is specified in the string, defaults to 8885 */ fromString: (addressString, port = 8885) => { const arr = addressString.split(':'); return { host: arr[0], port: arr[1] ? parseInt(arr[1], 10) : port, }; }, /** Convert an [[Address]] to a string in the "{host}:{port}" format. */ toString: (address) => `${address.host}:${address.port}`, /** Checks whether two [[Address]] instances are equal, based solely on `host` and `port` fields */ areEqual: (a, b) => a.host === b.host && a.port === b.port, sortByLastConnected: (addresses) => { return [...addresses].sort((a, b) => { if (!a.lastConnected) return 1; if (!b.lastConnected) return -1; return b.lastConnected - a.lastConnected; }); }, }; exports.default = addressUtils; //# sourceMappingURL=addressUtils.js.map