From 48d0b5807159d1eef308a95eb9f25b3f120b2562 Mon Sep 17 00:00:00 2001 From: Aleksandr Borovskii Date: Thu, 26 Nov 2026 17:49:15 +0100 Subject: [PATCH 1/2] fix(windows): run vcpkg only when the prebuilt binary is unavailable The preinstall hook ran scripts/vcpkg-setup.js unconditionally on Windows, so every npm install built curl and nine dependencies from source (20-30 minutes, needs git and Visual Studio) before node-pre-gyp downloaded the prebuilt binary that made that build unnecessary. scripts/install.js replaces the preinstall and install hooks. On Windows it tries the prebuilt download first and runs vcpkg plus the fallback build only when the download fails. Explicit source builds (npm_config_build_from_source) and git checkouts still set up vcpkg first, so CI builds and local `pnpm pregyp rebuild` keep working. Other platforms are unchanged. A new windows-consumer-install job installs the packed tarball with npm and fails if vcpkg ran or the install takes longer than 15 minutes. --- .../workflows/windows-consumer-install.yaml | 53 ++++++++++++- CHANGELOG.md | 1 + package.json | 3 +- scripts/ci/windows/build.ps1 | 4 +- scripts/install.js | 60 ++++++++++++++ test/scripts/install.spec.ts | 78 +++++++++++++++++++ 6 files changed, 192 insertions(+), 7 deletions(-) create mode 100644 scripts/install.js create mode 100644 test/scripts/install.spec.ts 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..9d1b9d02 --- /dev/null +++ b/scripts/install.js @@ -0,0 +1,60 @@ +// 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) + } + + // 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', + ) + } + + const vcpkgStatus = run(vcpkgSetup, [], env) + if (vcpkgStatus !== 0) return vcpkgStatus + return run(nodePreGyp, fallbackInstallArgs, 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..6fbc443c --- /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', '--fallback-to-build']], + ]) + }) + + 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) + }) +}) From 72895dd4aed88cf85510fa30784f3717058f7148 Mon Sep 17 00:00:00 2001 From: Aleksandr Borovskii Date: Thu, 26 Nov 2026 19:41:13 +0100 Subject: [PATCH 2/2] fix(windows): rebuild from source after a failed prebuilt download A failed extraction can leave a partial lib/binding/node_libcurl.node. A second `node-pre-gyp install` treats any file at that path as installed and skips the build, so npm could finish with a corrupt addon. Use --build-from-source, whose rebuild deletes module_path before compiling. --- scripts/install.js | 7 ++++++- test/scripts/install.spec.ts | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/scripts/install.js b/scripts/install.js index 9d1b9d02..1842f1ba 100644 --- a/scripts/install.js +++ b/scripts/install.js @@ -36,6 +36,8 @@ function install({ 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 @@ -46,11 +48,14 @@ function install({ 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, fallbackInstallArgs, env) + return run(nodePreGyp, buildArgs, env) } module.exports = { install, nodePreGyp, vcpkgSetup } diff --git a/test/scripts/install.spec.ts b/test/scripts/install.spec.ts index 6fbc443c..d68e4d11 100644 --- a/test/scripts/install.spec.ts +++ b/test/scripts/install.spec.ts @@ -37,7 +37,7 @@ describe('install', () => { expect(run.mock.calls.map(([script, args]) => [script, args])).toEqual([ [nodePreGyp, ['install']], [vcpkgSetup, []], - [nodePreGyp, ['install', '--fallback-to-build']], + [nodePreGyp, ['install', '--build-from-source']], ]) })