UNPKG

everything-dev

Version:

A consolidated product package for building Module Federation apps with oRPC APIs.

114 lines (112 loc) 5.26 kB
const require_runtime = require('./_virtual/_rolldown/runtime.cjs'); const require_config = require('./config.cjs'); const require_process_registry = require('./process-registry.cjs'); let node_fs = require("node:fs"); let node_path = require("node:path"); let effect = require("effect"); let node_net = require("node:net"); //#region src/app.ts const PROBE_TIMEOUT_MS = 250; const MAX_PORT_SCAN_STEPS = 1e3; const PARALLEL_PROBE_WINDOW = 8; var PortAllocationError = class extends effect.Data.TaggedError("PortAllocationError") {}; var PortAllocator = class extends effect.Context.Tag("PortAllocator")() {}; function detectLocalPackages(bosConfig, runtimeConfig) { const packages = []; const configDir = require_config.getProjectRoot(); const uiLocalPath = runtimeConfig?.ui.localPath ?? require_config.resolveLocalDevelopmentPath(bosConfig?.app.ui.development, configDir); if (uiLocalPath && (0, node_fs.existsSync)((0, node_path.join)(uiLocalPath, "package.json"))) packages.push("ui"); const apiLocalPath = runtimeConfig?.api.localPath ?? require_config.resolveLocalDevelopmentPath(bosConfig?.app.api.development, configDir); if (apiLocalPath && (0, node_fs.existsSync)((0, node_path.join)(apiLocalPath, "package.json"))) packages.push("api"); const hostLocalPath = runtimeConfig?.host?.localPath ?? require_config.resolveLocalDevelopmentPath(bosConfig?.app.host.development, configDir); if (hostLocalPath && (0, node_fs.existsSync)((0, node_path.join)(hostLocalPath, "package.json"))) packages.push("host"); else if ((0, node_fs.existsSync)((0, node_path.join)(configDir, "host", "package.json"))) packages.push("host"); for (const [pluginId, pluginConfig] of Object.entries(runtimeConfig?.plugins ?? {})) { if (pluginConfig.localPath && (0, node_fs.existsSync)((0, node_path.join)(pluginConfig.localPath, "package.json"))) packages.push(`plugin:${pluginId}`); if (pluginConfig.ui?.localPath && (0, node_fs.existsSync)((0, node_path.join)(pluginConfig.ui.localPath, "package.json"))) packages.push(`plugin-ui:${pluginId}`); } const authLocalPath = runtimeConfig?.auth?.localPath ?? require_config.resolveLocalDevelopmentPath(bosConfig?.app.auth?.development, configDir); if (authLocalPath && (0, node_fs.existsSync)((0, node_path.join)(authLocalPath, "package.json"))) packages.push("auth"); return packages; } function buildRuntimeConfig(bosConfig, options) { return require_config.buildRuntimeConfig(bosConfig, require_config.getProjectRoot(), options.env ?? "development", { hostSource: options.hostSource, uiSource: options.uiSource, apiSource: options.apiSource, authSource: options.authSource, proxy: options.proxy, plugins: options.plugins }); } function probePortBindable(port) { return effect.Effect.async((resume) => { const server = (0, node_net.createServer)(); server.once("listening", () => { server.close(() => { resume(effect.Effect.succeed(true)); }); }); server.once("error", () => { server.removeAllListeners(); resume(effect.Effect.succeed(false)); }); server.listen(port, "127.0.0.1"); const timer = setTimeout(() => { server.removeAllListeners(); try { server.close(); } catch {} resume(effect.Effect.succeed(false)); }, PROBE_TIMEOUT_MS); server.once("listening", () => clearTimeout(timer)); server.once("error", () => clearTimeout(timer)); }); } function pickAvailablePort(preferred, usedPorts, budget) { return effect.Effect.gen(function* () { const within = (candidate) => !budget || candidate >= budget.min && candidate <= budget.max; let port = preferred; if (!within(port)) port = budget ? budget.min : port; const ceiling = budget ? budget.max + 1 : Number.MAX_SAFE_INTEGER; let steps = 0; const fail = () => effect.Effect.fail(new PortAllocationError({ preferred, budget, cause: budget ? `No free port in budget [${budget.min}, ${budget.max}] starting from ${preferred}` : `No free port found starting from ${preferred} within ${MAX_PORT_SCAN_STEPS} steps` })); while (true) { if (port >= ceiling || steps > MAX_PORT_SCAN_STEPS) yield* fail(); const candidates = []; for (let i = 0; i < PARALLEL_PROBE_WINDOW && port + i < ceiling; i++) { const candidate = port + i; if (!usedPorts.has(candidate)) candidates.push(candidate); } if (candidates.length === 0) { port += PARALLEL_PROBE_WINDOW; steps += PARALLEL_PROBE_WINDOW; continue; } const firstFree = (yield* effect.Effect.forEach(candidates, (c) => probePortBindable(c).pipe(effect.Effect.map((free) => ({ port: c, free }))), { concurrency: "unbounded" })).find((r) => r.free); if (firstFree) { usedPorts.add(firstFree.port); return firstFree.port; } port += PARALLEL_PROBE_WINDOW; steps += PARALLEL_PROBE_WINDOW; } }); } const PortAllocatorLive = effect.Layer.sync(PortAllocator, () => { const usedPorts = require_process_registry.claimedPorts(); return { pickAvailable: (preferred, budget) => pickAvailablePort(preferred, usedPorts, budget) }; }); //#endregion exports.PortAllocator = PortAllocator; exports.PortAllocatorLive = PortAllocatorLive; exports.buildRuntimeConfig = buildRuntimeConfig; exports.detectLocalPackages = detectLocalPackages; //# sourceMappingURL=app.cjs.map