Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 50 additions & 3 deletions .github/workflows/windows-consumer-install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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).
Expand Down Expand Up @@ -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"
}
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions scripts/ci/windows/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
65 changes: 65 additions & 0 deletions scripts/install.js
Original file line number Diff line number Diff line change
@@ -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()
}
78 changes: 78 additions & 0 deletions test/scripts/install.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
Loading