|
| 1 | +// Checks the runtime before the CLI loads. On an older Node, a static import of a |
| 2 | +// newer API (such as `setGlobalProxyFromEnv` from `node:http`) fails at link time |
| 3 | +// with a SyntaxError before any code can run, and child processes reuse |
| 4 | +// `process.execPath`, so a deploy would stop halfway. This file must therefore |
| 5 | +// load on any Node or Bun release: it has no imports and uses no new APIs. |
| 6 | +// |
| 7 | +// packages/void/src/cli/runtime-support.ts and packages/create-void/src/runtime-support.ts |
| 8 | +// are byte-identical copies, because create-void runs before `void` is installed. |
| 9 | +// A unit test keeps the two files and both `engines.node` fields equal. |
| 10 | + |
| 11 | +/** Must equal `engines.node` in packages/void and packages/create-void. */ |
| 12 | +export const NODE_ENGINES = '^24.22.0 || >=26.12.0'; |
| 13 | + |
| 14 | +/** |
| 15 | + * Bun is not a documented runtime, but `bunx --bun` and Bun-only images (where |
| 16 | + * `node` is Bun) run the CLI on it. Bun reports a Node version below 26.12, so |
| 17 | + * it gets its own floor: 1.4.0 is the first release where Void's Cloudflare |
| 18 | + * helper process loads. This lets working setups keep working. |
| 19 | + */ |
| 20 | +export const BUN_MINIMUM = '1.4.0'; |
| 21 | + |
| 22 | +type Version = [major: number, minor: number, patch: number]; |
| 23 | + |
| 24 | +export type RuntimeVersions = { readonly node?: string; readonly bun?: string }; |
| 25 | + |
| 26 | +function isDigits(text: string): boolean { |
| 27 | + if (text.length === 0) { |
| 28 | + return false; |
| 29 | + } |
| 30 | + for (let i = 0; i < text.length; i++) { |
| 31 | + const code = text.charCodeAt(i); |
| 32 | + if (code < 48 || code > 57) { |
| 33 | + return false; |
| 34 | + } |
| 35 | + } |
| 36 | + return true; |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Parses `1.2.3`, with an optional leading `v`. A pre-release or build tag |
| 41 | + * (`27.0.0-nightly2026...`, `1.4.0+abc`) is ignored, so a nightly counts as its |
| 42 | + * release line. |
| 43 | + */ |
| 44 | +export function parseVersion(text: string): Version | null { |
| 45 | + let core = text.trim(); |
| 46 | + if (core.startsWith('v')) { |
| 47 | + core = core.slice(1); |
| 48 | + } |
| 49 | + for (const separator of ['-', '+']) { |
| 50 | + const index = core.indexOf(separator); |
| 51 | + if (index !== -1) { |
| 52 | + core = core.slice(0, index); |
| 53 | + } |
| 54 | + } |
| 55 | + const parts = core.split('.'); |
| 56 | + if (parts.length !== 3 || !parts.every(isDigits)) { |
| 57 | + return null; |
| 58 | + } |
| 59 | + return [Number(parts[0]), Number(parts[1]), Number(parts[2])]; |
| 60 | +} |
| 61 | + |
| 62 | +function compareVersions(a: Version, b: Version): number { |
| 63 | + return a[0] - b[0] || a[1] - b[1] || a[2] - b[2]; |
| 64 | +} |
| 65 | + |
| 66 | +type Clause = { caret: boolean; version: Version }; |
| 67 | + |
| 68 | +/** Supports only the forms used in `engines.node`: `^x.y.z` and `>=x.y.z`, joined by `||`. */ |
| 69 | +function parseRange(range: string): Array<Clause> { |
| 70 | + return range.split('||').map((raw) => { |
| 71 | + const clause = raw.trim(); |
| 72 | + const caret = clause.startsWith('^'); |
| 73 | + const prefix = caret ? '^' : clause.startsWith('>=') ? '>=' : null; |
| 74 | + const version = prefix === null ? null : parseVersion(clause.slice(prefix.length)); |
| 75 | + if (!version) { |
| 76 | + throw new Error(`runtime-support: unsupported range clause '${clause}'`); |
| 77 | + } |
| 78 | + return { caret, version }; |
| 79 | + }); |
| 80 | +} |
| 81 | + |
| 82 | +export function satisfiesRange(version: Version, range: string): boolean { |
| 83 | + return parseRange(range).some( |
| 84 | + ({ caret, version: floor }) => |
| 85 | + compareVersions(version, floor) >= 0 && (!caret || version[0] === floor[0]), |
| 86 | + ); |
| 87 | +} |
| 88 | + |
| 89 | +/** `^24.22.0 || >=26.12.0` -> `24.22+ or 26.12+`. */ |
| 90 | +function describeRange(range: string): string { |
| 91 | + return parseRange(range) |
| 92 | + .map(({ version: [major, minor, patch] }) => |
| 93 | + patch === 0 ? `${major}.${minor}+` : `${major}.${minor}.${patch}+`, |
| 94 | + ) |
| 95 | + .join(' or '); |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * Returns the error to print when this runtime cannot run the CLI, or `null`. |
| 100 | + * The text names only the runtime version and executable path, never env or argv. |
| 101 | + */ |
| 102 | +export function unsupportedRuntimeMessage( |
| 103 | + versions: RuntimeVersions, |
| 104 | + execPath: string, |
| 105 | + command = 'void', |
| 106 | +): string | null { |
| 107 | + const nodeRange = describeRange(NODE_ENGINES); |
| 108 | + if (versions.bun !== undefined) { |
| 109 | + const bun = parseVersion(versions.bun); |
| 110 | + const minimum = parseVersion(BUN_MINIMUM); |
| 111 | + if (bun && minimum && compareVersions(bun, minimum) >= 0) { |
| 112 | + return null; |
| 113 | + } |
| 114 | + return ( |
| 115 | + `${command}: Bun ${BUN_MINIMUM} or later is required. Found Bun ${versions.bun} (${execPath}).\n` + |
| 116 | + `Update Bun, or run ${command} on Node.js ${nodeRange}.` |
| 117 | + ); |
| 118 | + } |
| 119 | + const node = versions.node === undefined ? null : parseVersion(versions.node); |
| 120 | + if (node && satisfiesRange(node, NODE_ENGINES)) { |
| 121 | + return null; |
| 122 | + } |
| 123 | + const found = versions.node === undefined ? 'an unknown version' : `v${versions.node}`; |
| 124 | + return ( |
| 125 | + `${command}: Node.js ${nodeRange} is required. Found Node.js ${found} (${execPath}).\n` + |
| 126 | + 'bunx, npx and package scripts use the `node` on PATH; vp/vpx use .node-version.\n' + |
| 127 | + 'Update .node-version or the node on PATH. In GitHub Actions add actions/setup-node\n' + |
| 128 | + 'with `node-version: lts/*` and `check-latest: true` before this step.' |
| 129 | + ); |
| 130 | +} |
0 commit comments