UNPKG

@farmfe/core

Version:

Farm is a extremely fast web build tool written in Rust. Farm can start a project in milliseconds and perform HMR within 10ms, making it much faster than similar tools like webpack and vite.

85 lines 2.9 kB
/** * The following is modified based on source found in * https://github.com/vitejs/vite * * MIT Licensed * Copyright (c) 2019-present, (Evan) You and Vite contributors * https://github.com/vitejs/vite/blob/main/LICENSE */ import os from 'node:os'; export const urlRegex = /^(https?:)?\/\/([^/]+)/; export const loopbackHosts = new Set([ 'localhost', '127.0.0.1', '::1', '0000:0000:0000:0000:0000:0000:0000:0001' ]); export const wildcardHosts = new Set([ '0.0.0.0', '::', '0000:0000:0000:0000:0000:0000:0000:0000' ]); export function resolveServerUrls(server, options, publicPath) { const address = server.address(); const isAddressInfo = (x) => x?.address; if (!isAddressInfo(address)) { return { local: [], network: [] }; } const local = []; const network = []; const hostname = resolveHostname(options.host); const protocol = options.https ? 'https' : 'http'; const { port } = getAddressHostnamePort(address); const base = publicPath || ''; if (hostname.host !== undefined && !wildcardHosts.has(hostname.host)) { const url = createServerUrl(protocol, hostname.name, port, base); if (loopbackHosts.has(hostname.host)) { local.push(url); } else { network.push(url); } } else { const networkInterfaces = Object.values(os.networkInterfaces()).flatMap((nInterface) => nInterface || []); networkInterfaces .filter((detail) => detail && detail.address && (detail.family === 'IPv4' || // @ts-expect-error Node 18.0 - 18.3 returns number detail.family === 4)) .forEach((detail) => { let host = detail.address.replace('127.0.0.1', hostname.name); host = host.includes(':') ? `[${host}]` : host; const url = createServerUrl(protocol, host, port, base); detail.address.includes('127.0.0.1') ? local.push(url) : network.push(url); }); } return { local, network }; } export function resolveHostname(optionsHost) { let host; if (optionsHost === undefined || optionsHost === false) { host = 'localhost'; } else if (optionsHost === true) { host = undefined; } else { host = optionsHost; } const name = host === undefined || wildcardHosts.has(host) ? 'localhost' : host; return { host, name }; } function getAddressHostnamePort(server) { const hostname = server.address || 'localhost'; const port = server.port; return { host: hostname, port }; } function createServerUrl(protocol, hostname, port, publicPath) { const hostnameName = hostname.includes(':') ? `[${hostname}]` : hostname; return `${protocol}://${hostnameName}:${port}${publicPath}`; } //# sourceMappingURL=http.js.map