diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 69fb2b2..3418357 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -2,13 +2,10 @@ "name": "Node.js", "build": { "dockerfile": "dockerfile", - "args": { - "VARIANT": "26-bookworm", - "NODE_ENV": "docker-dev" - } + "args": { "VARIANT": "26-bookworm", "NODE_ENV": "docker-dev" } }, "settings": {}, "extensions": ["oxc.oxc-vscode", "wix.vscode-import-cost", "eamodio.gitlens"], - "postCreateCommand": "npm ci --ignore-scripts", + "postCreateCommand": "chomp setup", "remoteUser": "node" } diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3024981..d384fc6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,18 +14,13 @@ jobs: - name: Checkout uses: actions/checkout@v3 - - name: Install Node.js 26 - uses: actions/setup-node@v3 - with: - node-version: 26.x - - name: Setup Chomp uses: guybedford/chomp-action@v1 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - name: Install dependencies - run: npm ci --ignore-scripts + - name: Setup development runtime and dependencies + run: chomp setup - name: Build run: chomp build diff --git a/.nvmrc b/.nvmrc deleted file mode 100644 index 6f4247a..0000000 --- a/.nvmrc +++ /dev/null @@ -1 +0,0 @@ -26 diff --git a/chompfile.toml b/chompfile.toml index 2402fc1..6f58b39 100644 --- a/chompfile.toml +++ b/chompfile.toml @@ -2,17 +2,35 @@ version = 0.1 default-task = 'build' +[env] +NODE_VERSION = '26.13.0' + +[[task]] +name = 'node:install' +run = 'sh scripts/setup-node.sh' +deps = ['scripts/setup-node.sh', 'chompfile.toml'] +targets = ['.bin/node', '.bin/npm'] + +[[task]] +name = 'node:version' +run = 'node --version' +dep = 'node:install' + +[[task]] +name = 'setup' +dep = 'npm:install' + [[task]] name = 'build' run = 'tsup src/loader.ts --format esm --target node22 --dts' -dep = 'src/#.ts' -target = 'dist/loader.js' +deps = ['npm:install', 'src/**/*.ts'] +targets = ['dist/loader.js', 'dist/loader.d.ts'] [[task]] name = 'npm:install' -run = 'npm install' -dep = 'package.json' -target = 'package-lock.json' +run = 'npm ci --ignore-scripts' +deps = ['node:install', 'package.json', 'package-lock.json'] +target = 'node_modules/.package-lock.json' [[task]] name = 'clean' @@ -21,18 +39,22 @@ run = 'rm -rf dist' [[task]] name = 'format' run = 'npm run format' +dep = 'npm:install' [[task]] name = 'format:check' run = 'npm run format:check' +dep = 'npm:install' [[task]] name = 'lint' run = 'npm run lint' +dep = 'npm:install' [[task]] name = 'lint:fix' run = 'npm run lint:fix' +dep = 'npm:install' [[task]] name = 'post-merge' @@ -45,6 +67,7 @@ deps = ['typecheck', 'test:changed', 'lint', 'format:check'] [[task]] name = 'prepare' run = 'npm run hooks:install' +dep = 'npm:install' [[task]] name = 'prepublishOnly' @@ -56,10 +79,11 @@ deps = ['server:build', 'server:start'] [[task]] name = 'server:build' -run = "echo 'Building...' && chomp build && echo 'Build complete.'" +dep = 'build' [[task]] name = 'server:start' +dep = 'build' run = ''' echo "Starting server..." node --loader ./dist/loader.js tests/e2e/test-server.js @@ -78,14 +102,17 @@ deps = ['build'] [[task]] name = 'test:unit' run = 'npm run test:unit' +dep = 'npm:install' [[task]] name = 'test:changed' run = 'npm run test:unit' +dep = 'npm:install' [[task]] name = 'typecheck' run = 'tsc --noEmit' +dep = 'npm:install' [[task]] name = 'dir-size' diff --git a/readme.md b/readme.md index 219839f..73db52b 100644 --- a/readme.md +++ b/readme.md @@ -18,7 +18,11 @@ decrease security risk 🚔! `@jspm/node-importmap-loader` supports Node 22+ and uses Node's [module loaders](https://nodejs.org/api/esm.html#loaders) and native `fetch`. -Development uses Node 26 (see `.nvmrc`). Tests use `node:test` and Node's built-in mocks. `npm run test:unit` compiles the TypeScript tests with the existing `tsup` compiler before running `node --test`. `npm test` also runs the end-to-end tests. +Development uses the Node 26 version pinned in `chompfile.toml`. With standalone [Chomp](https://github.com/guybedford/chomp) installed, run `chomp setup`. It downloads the official Node archive, verifies its SHA-256 checksum, and installs the development dependencies. Setup supports macOS and Linux on x64 and arm64 and requires `curl`, `tar`, and either `sha256sum` or `shasum`. + +Chomp tasks use the project-local Node installation through `.bin/`; the downloaded runtime stays in `.cache/dev-node/`. `chomp node:version` prints the selected version. Development CI and the devcontainer use the same setup task. + +Tests use `node:test` and Node's built-in mocks. `chomp test` compiles the TypeScript tests with the existing `tsup` compiler and runs the unit and end-to-end tests. Chomp remains the task runner. `npm run lint` uses Oxlint; `npm run format` uses Oxfmt, and `npm run format:check` checks formatting without writing files. @@ -75,7 +79,7 @@ du -sh .cache #### 1. Clone ```sh -git clone git@github.com:jspm/node-importmap-http-loader.git +git clone git@github.com:jspm/node-importmap-loader.git ``` #### 2. Devcontainer @@ -90,13 +94,14 @@ Then, via [devcontainers](https://code.visualstudio.com/docs/remote/containers), --- -#### Old school contributing setup +#### Local development -Unrecommended setup +Install standalone Chomp, then: ```sh -git clone git@github.com:jspm/node-importmap-http-loader.git -n install auto && npm install +git clone git@github.com:jspm/node-importmap-loader.git +cd node-importmap-loader +chomp setup ``` --- diff --git a/scripts/setup-node.sh b/scripts/setup-node.sh new file mode 100644 index 0000000..dac29c9 --- /dev/null +++ b/scripts/setup-node.sh @@ -0,0 +1,83 @@ +#!/bin/sh +set -eu + +has_pinned_node() { + [ -x .bin/node ] || return 1 + current_version=$(.bin/node --version) + [ "$current_version" = "v$NODE_VERSION" ] && [ -x .bin/npm ] +} + +detect_system() { + system=$(uname -s) + case "$system" in + Darwin) system=darwin ;; + Linux) system=linux ;; + *) + printf 'Unsupported development platform: %s\n' "$system" >&2 + exit 1 + ;; + esac +} + +detect_machine() { + machine=$(uname -m) + case "$machine" in + arm64 | aarch64) machine=arm64 ;; + x86_64 | amd64) machine=x64 ;; + *) + printf 'Unsupported development architecture: %s\n' "$machine" >&2 + exit 1 + ;; + esac +} + +needs_download() { + [ ! -d "$runtime_dir" ] +} + +verify_archive() { + awk -v file="$archive" '$2 == file { print }' "$work_dir/SHASUMS256.txt" >"$work_dir/checksum.txt" + [ -s "$work_dir/checksum.txt" ] + if command -v sha256sum >/dev/null 2>&1; then + (cd "$work_dir" && sha256sum -c checksum.txt) + else + (cd "$work_dir" && shasum -a 256 -c checksum.txt) + fi +} + +download_node() { + mkdir -p .cache/dev-node + work_dir=$(mktemp -d .cache/dev-node/download.XXXXXX) + trap 'rm -rf "$work_dir"' EXIT + archive="$distribution.tar.gz" + release_url="https://nodejs.org/dist/v$NODE_VERSION" + curl --fail --location --silent --show-error --retry 3 "$release_url/$archive" -o "$work_dir/$archive" + curl --fail --location --silent --show-error --retry 3 "$release_url/SHASUMS256.txt" -o "$work_dir/SHASUMS256.txt" + verify_archive + tar -xzf "$work_dir/$archive" -C "$work_dir" + mv "$work_dir/$distribution" "$runtime_dir" +} + +link_node() { + mkdir -p .bin + for tool in node npm npx; do + ln -sfn "../$runtime_dir/bin/$tool" ".bin/$tool" + done +} + +main() { + : "${NODE_VERSION:?Run this script through Chomp.}" + printf '%s\n' "$NODE_VERSION" | grep -Eq '^26\.[0-9]+\.[0-9]+$' + if has_pinned_node; then return; fi + detect_system + detect_machine + platform="$system-$machine" + distribution="node-v$NODE_VERSION-$platform" + runtime_dir=".cache/dev-node/$distribution" + if needs_download; then download_node; fi + link_node + has_pinned_node + .bin/node --version +} + +main "$@" diff --git a/tests/setup-node.test.ts b/tests/setup-node.test.ts new file mode 100644 index 0000000..a9dba30 --- /dev/null +++ b/tests/setup-node.test.ts @@ -0,0 +1,72 @@ +import { test } from "node:test"; +import type { TestContext } from "node:test"; +import assert from "node:assert/strict"; +import { spawnSync } from "node:child_process"; +import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { join, resolve } from "node:path"; +import { env } from "node:process"; + +const setupScript = resolve("scripts/setup-node.sh"); +const nodeVersion = "26.13.0"; + +const createSetupDirectory = (t: TestContext): string => { + mkdirSync(".cache", { recursive: true }); + const directory = mkdtempSync(resolve(".cache/node-setup-test-")); + t.after(() => rmSync(directory, { recursive: true, force: true })); + return directory; +}; + +const writeExecutable = (directory: string, name: string, source: string): void => { + const path = join(directory, name); + writeFileSync(path, source, { mode: 0o755 }); +}; + +const runSetup = (directory: string, version = nodeVersion) => { + const executablePath = `${directory}/commands:/usr/bin:/bin`; + const environment = Object.assign({}, env, { NODE_VERSION: version, PATH: executablePath }); + const result = spawnSync("/bin/sh", [setupScript], { cwd: directory, env: environment, encoding: "utf8" }); + return result; +}; + +test("Node setup rejects an invalid version before downloading", (t) => { + const directory = createSetupDirectory(t); + const result = runSetup(directory, "26/../../unexpected"); + assert.equal(result.status, 1); + assert.equal(existsSync(join(directory, ".cache")), false); +}); + +test("Node setup reuses its pinned runtime without network access", (t) => { + const directory = createSetupDirectory(t); + const binDirectory = join(directory, ".bin"); + mkdirSync(binDirectory); + writeExecutable(binDirectory, "node", `#!/bin/sh\nprintf 'v${nodeVersion}\\n'\n`); + writeExecutable(binDirectory, "npm", "#!/bin/sh\nexit 0\n"); + const commands = join(directory, "commands"); + mkdirSync(commands); + writeExecutable(commands, "curl", "#!/bin/sh\nexit 99\n"); + writeExecutable(commands, "uname", "#!/bin/sh\nexit 99\n"); + const result = runSetup(directory); + assert.equal(result.status, 0, result.stderr); +}); + +test("Node setup rejects a bad checksum before extracting the archive", (t) => { + const directory = createSetupDirectory(t); + const commands = join(directory, "commands"); + mkdirSync(commands); + writeExecutable(commands, "uname", '#!/bin/sh\ncase "$1" in -s) echo Linux;; -m) echo x86_64;; esac\n'); + const manifest = `${"0".repeat(64)} node-v${nodeVersion}-linux-x64.tar.gz`; + const curlScript = `#!/bin/sh +for output do :; done +case "$*" in + *SHASUMS256.txt*) printf '%s\\n' '${manifest}' > "$output";; + *) printf 'unverified archive' > "$output";; +esac +`; + writeExecutable(commands, "curl", curlScript); + writeExecutable(commands, "tar", "#!/bin/sh\ntouch extracted\n"); + const result = runSetup(directory); + assert.notEqual(result.status, 0); + assert.match(`${result.stdout}${result.stderr}`, /FAILED|did NOT match/); + assert.equal(existsSync(join(directory, "extracted")), false); + assert.equal(existsSync(join(directory, ".bin")), false); +});