UNPKG

@deepkit/core

Version:
70 lines 2.51 kB
"use strict"; /* * Deepkit Framework * Copyright (C) 2021 Deepkit UG, Marc J. Schmidt * * This program is free software: you can redistribute it and/or modify * it under the terms of the MIT License. * * You should have received a copy of the MIT License along with this program. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.parseHost = exports.ParsedHost = void 0; class ParsedHost { constructor() { this.host = '127.0.0.1'; this.port = 0; this.unixSocket = ''; } get isUnixSocket() { return this.unixSocket !== ''; } get isHostname() { return this.unixSocket === ''; } get hostWithIp() { return this.host + (this.port ? (this.host && ':') + this.port : ''); } toString() { return this.isUnixSocket ? this.unixSocket : this.hostWithIp; } getWebSocketUrl(secure = false) { const protocol = secure ? 'wss' : 'ws'; if (this.isUnixSocket) { return `${protocol}+unix://${this.unixSocket}`; } return `${protocol}://${this.hostWithIp}`; } getHttpUrl(secure = false) { if (this.isUnixSocket) { return `file://${this.unixSocket}`; } const protocol = secure ? 'https' : 'http'; return `${protocol}://${this.hostWithIp}`; } } exports.ParsedHost = ParsedHost; ParsedHost.__type = ['host', function () { return '127.0.0.1'; }, 'port', function () { return 0; }, 'unixSocket', function () { return ''; }, 'toString', 'secure', () => false, 'getWebSocketUrl', () => false, 'getHttpUrl', '&3!>"\'3#>$&3%>&!!!P&0\'P)2(>)"0*P)2(>+"0,5']; function parseHost(hostWithIpOrUnixPath) { const parsedHost = new ParsedHost(); if (hostWithIpOrUnixPath.includes('/') || hostWithIpOrUnixPath.includes('\\') || hostWithIpOrUnixPath.endsWith('.sock')) { parsedHost.unixSocket = hostWithIpOrUnixPath; } else { if (hostWithIpOrUnixPath.includes(':')) { const [host, port] = hostWithIpOrUnixPath.split(':'); if (host) parsedHost.host = host; if (port) parsedHost.port = parseInt(port, 10); } else { if (hostWithIpOrUnixPath) parsedHost.host = hostWithIpOrUnixPath; } } return parsedHost; } exports.parseHost = parseHost; parseHost.__type = ['hostWithIpOrUnixPath', () => ParsedHost, 'parseHost', 'P&2!P7"/#']; //# sourceMappingURL=network.js.map