|
| 1 | +import { |
| 2 | + type ChildProcessWithoutNullStreams, |
| 3 | + type SpawnOptionsWithoutStdio, |
| 4 | + type SpawnSyncOptions, |
| 5 | + spawn, |
| 6 | + spawnSync, |
| 7 | +} from 'child_process'; |
| 8 | +import fs from 'fs'; |
| 9 | +import { createConnection } from 'net'; |
| 10 | +import nodeFetch from 'node-fetch'; |
| 11 | +import path from 'path'; |
| 12 | + |
| 13 | +export type RuntimeRequestInit = { |
| 14 | + method?: string; |
| 15 | + headers?: Record<string, string>; |
| 16 | + body?: any; |
| 17 | + signal?: AbortSignal; |
| 18 | +}; |
| 19 | + |
| 20 | +export type RuntimeResponse = { |
| 21 | + status: number; |
| 22 | + statusText: string; |
| 23 | + text: () => Promise<string>; |
| 24 | +}; |
| 25 | + |
| 26 | +export type PackageManager = 'bun' | 'npm' | 'pnpm' | 'yarn'; |
| 27 | +export type JavaScriptRuntime = 'bun' | 'node'; |
| 28 | + |
| 29 | +export const isBunRuntime = |
| 30 | + typeof (process.versions as NodeJS.ProcessVersions & { bun?: string }).bun === |
| 31 | + 'string'; |
| 32 | + |
| 33 | +export function runtimeFetch( |
| 34 | + url: string, |
| 35 | + options?: RuntimeRequestInit, |
| 36 | +): Promise<RuntimeResponse> { |
| 37 | + const fetchImpl = |
| 38 | + isBunRuntime && typeof globalThis.fetch === 'function' |
| 39 | + ? globalThis.fetch.bind(globalThis) |
| 40 | + : nodeFetch; |
| 41 | + |
| 42 | + return fetchImpl(url, options as any) as Promise<RuntimeResponse>; |
| 43 | +} |
| 44 | + |
| 45 | +function resolveTcpTarget(input: string): { host: string; port: number } { |
| 46 | + try { |
| 47 | + const parsed = new URL(input); |
| 48 | + const port = parsed.port || (parsed.protocol === 'http:' ? '80' : '443'); |
| 49 | + return { |
| 50 | + host: parsed.hostname, |
| 51 | + port: Number(port), |
| 52 | + }; |
| 53 | + } catch { |
| 54 | + const [host, port] = input.split(':'); |
| 55 | + return { |
| 56 | + host, |
| 57 | + port: port ? Number(port) : 443, |
| 58 | + }; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +function measureTcpConnectOnce( |
| 63 | + host: string, |
| 64 | + port: number, |
| 65 | + timeout: number, |
| 66 | +): Promise<number> { |
| 67 | + return new Promise((resolve) => { |
| 68 | + const startedAt = Date.now(); |
| 69 | + const socket = createConnection({ host, port }); |
| 70 | + |
| 71 | + const finish = (latency: number) => { |
| 72 | + socket.removeAllListeners(); |
| 73 | + socket.destroy(); |
| 74 | + resolve(latency); |
| 75 | + }; |
| 76 | + |
| 77 | + socket.setTimeout(timeout); |
| 78 | + socket.once('connect', () => { |
| 79 | + finish(Date.now() - startedAt); |
| 80 | + }); |
| 81 | + socket.once('timeout', () => { |
| 82 | + finish(Number.POSITIVE_INFINITY); |
| 83 | + }); |
| 84 | + socket.once('error', () => { |
| 85 | + finish(Number.POSITIVE_INFINITY); |
| 86 | + }); |
| 87 | + }); |
| 88 | +} |
| 89 | + |
| 90 | +export async function measureTcpLatency( |
| 91 | + input: string, |
| 92 | + { |
| 93 | + attempts = 4, |
| 94 | + timeout = 1000, |
| 95 | + }: { |
| 96 | + attempts?: number; |
| 97 | + timeout?: number; |
| 98 | + } = {}, |
| 99 | +): Promise<number> { |
| 100 | + const { host, port } = resolveTcpTarget(input); |
| 101 | + const latencies: number[] = []; |
| 102 | + |
| 103 | + for (let i = 0; i < attempts; i++) { |
| 104 | + const latency = await measureTcpConnectOnce(host, port, timeout); |
| 105 | + if (Number.isFinite(latency)) { |
| 106 | + latencies.push(latency); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + if (latencies.length === 0) { |
| 111 | + return Number.POSITIVE_INFINITY; |
| 112 | + } |
| 113 | + return ( |
| 114 | + latencies.reduce((sum, latency) => sum + latency, 0) / latencies.length |
| 115 | + ); |
| 116 | +} |
| 117 | + |
| 118 | +export function detectPackageManager( |
| 119 | + cwd = process.cwd(), |
| 120 | + env: NodeJS.ProcessEnv = process.env, |
| 121 | +): PackageManager { |
| 122 | + const userAgent = env.npm_config_user_agent ?? ''; |
| 123 | + if (userAgent.startsWith('bun/')) { |
| 124 | + return 'bun'; |
| 125 | + } |
| 126 | + if (userAgent.startsWith('pnpm/')) { |
| 127 | + return 'pnpm'; |
| 128 | + } |
| 129 | + if (userAgent.startsWith('yarn/')) { |
| 130 | + return 'yarn'; |
| 131 | + } |
| 132 | + if (userAgent.startsWith('npm/')) { |
| 133 | + return 'npm'; |
| 134 | + } |
| 135 | + |
| 136 | + const lockFiles: Array<[string, PackageManager]> = [ |
| 137 | + ['bun.lock', 'bun'], |
| 138 | + ['bun.lockb', 'bun'], |
| 139 | + ['pnpm-lock.yaml', 'pnpm'], |
| 140 | + ['yarn.lock', 'yarn'], |
| 141 | + ['package-lock.json', 'npm'], |
| 142 | + ]; |
| 143 | + for (const [lockFile, manager] of lockFiles) { |
| 144 | + if (fs.existsSync(path.join(cwd, lockFile))) { |
| 145 | + return manager; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + return isBunRuntime ? 'bun' : 'npm'; |
| 150 | +} |
| 151 | + |
| 152 | +export function getInstallCommand( |
| 153 | + installArgs: string[], |
| 154 | + cwd = process.cwd(), |
| 155 | +): { command: string; args: string[] } { |
| 156 | + const packageManager = detectPackageManager(cwd); |
| 157 | + if (packageManager === 'npm') { |
| 158 | + return { command: 'npm', args: ['install', ...installArgs] }; |
| 159 | + } |
| 160 | + return { command: packageManager, args: ['add', ...installArgs] }; |
| 161 | +} |
| 162 | + |
| 163 | +export function getJavaScriptRuntime( |
| 164 | + env: NodeJS.ProcessEnv = process.env, |
| 165 | +): JavaScriptRuntime { |
| 166 | + const configured = env.RNU_JS_RUNTIME?.toLowerCase(); |
| 167 | + if (configured === 'bun') { |
| 168 | + return 'bun'; |
| 169 | + } |
| 170 | + if (configured === 'auto') { |
| 171 | + return isBunRuntime ? 'bun' : 'node'; |
| 172 | + } |
| 173 | + return 'node'; |
| 174 | +} |
| 175 | + |
| 176 | +export function spawnJavaScript( |
| 177 | + args: string[], |
| 178 | + options?: SpawnOptionsWithoutStdio, |
| 179 | + env: NodeJS.ProcessEnv = process.env, |
| 180 | +): ChildProcessWithoutNullStreams { |
| 181 | + return spawn(getJavaScriptRuntime(env), args, options ?? {}); |
| 182 | +} |
| 183 | + |
| 184 | +export function spawnJavaScriptSync( |
| 185 | + args: string[], |
| 186 | + options?: SpawnSyncOptions, |
| 187 | + env: NodeJS.ProcessEnv = process.env, |
| 188 | +) { |
| 189 | + return spawnSync(getJavaScriptRuntime(env), args, options ?? {}); |
| 190 | +} |
0 commit comments