diff --git a/.github/workflows/windows-consumer-install.yaml b/.github/workflows/windows-consumer-install.yaml index dd532d21..0dda77e1 100644 --- a/.github/workflows/windows-consumer-install.yaml +++ b/.github/workflows/windows-consumer-install.yaml @@ -72,7 +72,7 @@ jobs: # native command unless we tell it to. Without this, a failed # `pnpm add` (gyp couldn't find VS, native build threw, etc.) # would slide right past and the curl.exe check below would - # happily find the vcpkg-built curl from preinstall and report + # happily find the vcpkg-built curl from scripts/install.js and report # success — exactly what fooled us on the last run. $ErrorActionPreference = 'Stop' $PSNativeCommandErrorActionPreference = $true @@ -94,8 +94,8 @@ jobs: New-Item -ItemType Directory -Force -Path $consumerRoot Set-Location $consumerRoot # pnpm v10 blocks dependency install scripts by default. The consumer - # has to opt in for any package that needs its preinstall/install/ - # postinstall to run — and node-libcurl needs all three to run the + # has to opt in for any package that needs its install/postinstall + # to run — and node-libcurl needs both to run the # vcpkg setup and build the native addon. Without this approval the # install completes "successfully" but leaves the package non- # functional (no vcpkg-installed curl.exe, no native binding). @@ -172,3 +172,50 @@ jobs: if ($curlVersionText -match 'libidn2') { throw 'Windows curl build still links libidn2' } + + npm-prebuilt-install: + runs-on: windows-latest + # A prebuilt install takes about a minute. Hitting this limit means the + # install fell back to the 20-30 minute vcpkg source build. + timeout-minutes: 15 + + steps: + - name: Checkout + uses: actions/checkout@v5 + with: + submodules: true + + - name: Setup Node and PNPM + uses: ./.github/actions/setup-node-pnpm + with: + node-version: '24' + + - name: Build package tarball + run: | + pnpm install --frozen-lockfile --ignore-scripts + pnpm build:dist + pnpm pack --pack-destination $env:RUNNER_TEMP + + - name: Install as npm consumer from the prebuilt binary + run: | + $ErrorActionPreference = 'Stop' + $PSNativeCommandErrorActionPreference = $true + + $package = Get-ChildItem $env:RUNNER_TEMP -Filter '*node-libcurl-*.tgz' | + Select-Object -First 1 + if (-not $package) { + throw 'Packed node-libcurl tarball was not found' + } + + $consumerRoot = Join-Path $env:RUNNER_TEMP 'node-libcurl-npm-consumer' + New-Item -ItemType Directory -Force -Path $consumerRoot + Set-Location $consumerRoot + npm init -y | Out-Null + npm install $package.FullName + + node -e "console.log(require('@brightsec/node-libcurl').Curl.getVersionInfoString())" + + $vcpkgCacheRoot = Join-Path $env:LOCALAPPDATA 'node-libcurl-vcpkg' + if (Test-Path $vcpkgCacheRoot) { + throw "vcpkg ran although the prebuilt binary was available: $vcpkgCacheRoot exists" + } diff --git a/CHANGELOG.md b/CHANGELOG.md index 291970be..9e9fd20b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Added ### Changed +- On Windows, installing from npm no longer builds curl and its dependencies with vcpkg when the prebuilt binary is available. vcpkg used to run unconditionally in `preinstall`, so every install took 20-30 minutes and needed git and Visual Studio, even though the prebuilt binary was downloaded afterwards. vcpkg now runs only when the download fails, when `npm_config_build_from_source` is set, or in a git checkout. ## [5.2.0] - 2026-11-25 diff --git a/package.json b/package.json index b3171acc..5934a026 100644 --- a/package.json +++ b/package.json @@ -51,8 +51,7 @@ "gen:compile_commands": "node-pre-gyp configure -- -f compile_commands_json && cp ./build/Debug/compile_commands.json ./compile_commands.json", "gen:constants": "node scripts/build-constants.js", "gen:docs": "typedoc", - "preinstall": "node scripts/vcpkg-setup.js", - "install": "node-pre-gyp install --fallback-to-build", + "install": "node scripts/install.js", "postinstall": "node scripts/postinstall", "lint": "eslint lib/ scripts/ test/ examples/", "pregyp": "node-pre-gyp", diff --git a/scripts/ci/windows/build.ps1 b/scripts/ci/windows/build.ps1 index 3eb214ae..97f467bd 100644 --- a/scripts/ci/windows/build.ps1 +++ b/scripts/ci/windows/build.ps1 @@ -48,7 +48,7 @@ try { # Git is used for a couple of steps Write-Host "Git Check..." -ForegroundColor Blue try { - # vcpkg will be set up by the preinstall script + # vcpkg will be set up by scripts/install.js # Just verify git is available for vcpkg bootstrap $gitVersion = git --version Write-Host "Git version: $gitVersion" -ForegroundColor Cyan @@ -99,7 +99,7 @@ if (-not $env:PUBLISH_BINARY) { Write-Host "Binary publish setting overridden via environment variable: $env:PUBLISH_BINARY" -ForegroundColor Cyan } -# Note: vcpkg setup will be handled by the preinstall script in package.json +# Note: vcpkg setup is handled by scripts/install.js (npm_config_build_from_source is set below) # No need for git submodules or Python configuration anymore # Set up build environment variables diff --git a/scripts/install.js b/scripts/install.js new file mode 100644 index 00000000..1842f1ba --- /dev/null +++ b/scripts/install.js @@ -0,0 +1,65 @@ +// On Windows a source build first needs vcpkg to compile curl and its +// dependencies, which takes 20-30 minutes, so vcpkg only runs when the +// prebuilt binary cannot be used. +const { spawnSync } = require('child_process') +const path = require('path') + +const packageJson = require('../package.json') +const buildFlags = require('./utils/buildFlags') + +const nodePreGyp = require.resolve('@mapbox/node-pre-gyp/bin/node-pre-gyp') +const vcpkgSetup = path.join(__dirname, 'vcpkg-setup.js') + +function isSourceBuildRequested(env) { + const value = env.npm_config_build_from_source + return value === 'true' || value === packageJson.name +} + +function runNode(script, args, env) { + const { status, error } = spawnSync(process.execPath, [script, ...args], { + stdio: 'inherit', + env, + }) + if (error) throw error + return status ?? 1 +} + +function install({ + platform = process.platform, + env = process.env, + isGitRepo = buildFlags.isGitRepo, + run = runNode, +} = {}) { + const fallbackInstallArgs = ['install', '--fallback-to-build'] + + if (platform !== 'win32') { + return run(nodePreGyp, fallbackInstallArgs, env) + } + + let buildArgs = fallbackInstallArgs + + // A git checkout keeps vcpkg ready for later `pnpm pregyp rebuild` runs. + if (!isSourceBuildRequested(env) && !isGitRepo) { + // node-pre-gyp lets npm_config_* env vars override its argv. Dropping the + // fallback flag keeps this attempt download-only, because a build without + // vcpkg would fail. + const { npm_config_fallback_to_build: _fallback, ...downloadEnv } = env + if (run(nodePreGyp, ['install'], downloadEnv) === 0) return 0 + console.log( + 'node-libcurl: prebuilt binary is not available, building from source with vcpkg', + ) + // A failed extraction can leave a partial node_libcurl.node, which another + // `install` would accept as installed. A source build deletes it first. + buildArgs = ['install', '--build-from-source'] + } + + const vcpkgStatus = run(vcpkgSetup, [], env) + if (vcpkgStatus !== 0) return vcpkgStatus + return run(nodePreGyp, buildArgs, env) +} + +module.exports = { install, nodePreGyp, vcpkgSetup } + +if (require.main === module) { + process.exitCode = install() +} diff --git a/test/scripts/install.spec.ts b/test/scripts/install.spec.ts new file mode 100644 index 00000000..d68e4d11 --- /dev/null +++ b/test/scripts/install.spec.ts @@ -0,0 +1,78 @@ +import { describe, expect, it, vi } from 'vitest' + +const { install, nodePreGyp, vcpkgSetup } = require('../../scripts/install.js') + +type Call = [script: string, args: string[], env: NodeJS.ProcessEnv] + +function runner(...statuses: number[]) { + return vi.fn((..._call: Call) => statuses.shift() ?? 0) +} + +describe('install', () => { + it('keeps the download-or-build install on non-Windows platforms', () => { + const run = runner(0) + const env = { npm_config_build_from_source: 'true' } + + expect(install({ platform: 'linux', env, isGitRepo: false, run })).toBe(0) + expect(run.mock.calls).toEqual([ + [nodePreGyp, ['install', '--fallback-to-build'], env], + ]) + }) + + it('skips vcpkg on Windows when the prebuilt binary installs', () => { + const run = runner(0) + const env = { PATH: 'C:\\bin', npm_config_fallback_to_build: 'true' } + + expect(install({ platform: 'win32', env, isGitRepo: false, run })).toBe(0) + expect(run.mock.calls).toEqual([ + [nodePreGyp, ['install'], { PATH: 'C:\\bin' }], + ]) + }) + + it('runs vcpkg and builds on Windows when the prebuilt binary is unavailable', () => { + const run = runner(1, 0, 0) + const env = { PATH: 'C:\\bin' } + + expect(install({ platform: 'win32', env, isGitRepo: false, run })).toBe(0) + expect(run.mock.calls.map(([script, args]) => [script, args])).toEqual([ + [nodePreGyp, ['install']], + [vcpkgSetup, []], + [nodePreGyp, ['install', '--build-from-source']], + ]) + }) + + it.each(['true', '@brightsec/node-libcurl'])( + 'runs vcpkg first on Windows when npm_config_build_from_source is %s', + (value) => { + const run = runner(0, 0) + const env = { npm_config_build_from_source: value } + + expect(install({ platform: 'win32', env, isGitRepo: false, run })).toBe(0) + expect(run.mock.calls).toEqual([ + [vcpkgSetup, [], env], + [nodePreGyp, ['install', '--fallback-to-build'], env], + ]) + }, + ) + + it('runs vcpkg first on Windows in a git checkout', () => { + const run = runner(0, 0) + + expect(install({ platform: 'win32', env: {}, isGitRepo: true, run })).toBe( + 0, + ) + expect(run.mock.calls.map(([script]) => script)).toEqual([ + vcpkgSetup, + nodePreGyp, + ]) + }) + + it('stops with the vcpkg exit code when vcpkg setup fails', () => { + const run = runner(1, 3) + + expect(install({ platform: 'win32', env: {}, isGitRepo: false, run })).toBe( + 3, + ) + expect(run).toHaveBeenCalledTimes(2) + }) +})