Skip to content

Commit 98f0cfa

Browse files
committed
feat(bootstrap): add system node detection and forwarding control
Add which-based system Node.js detection with SOCKET_DISABLE_NODE_FORWARDING env var for e2e testing control. - Add which dependency to catalog and bootstrap package - Add detectSystemNode() using which to find system node - Add shouldForwardToSystemNode() to check version requirements - Add SOCKET_DISABLE_NODE_FORWARDING env var (1/true/yes to disable) - Export helper functions for use in bootstrap entry points
1 parent 4a8fade commit 98f0cfa

6 files changed

Lines changed: 138 additions & 85 deletions

File tree

packages/bootstrap/dist/bootstrap-npm.js

Lines changed: 42 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/bootstrap/dist/bootstrap-smol.js

Lines changed: 42 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/bootstrap/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"@socketsecurity/lib": "catalog:",
1818
"del-cli": "catalog:",
1919
"esbuild": "catalog:",
20-
"semver": "catalog:"
20+
"semver": "catalog:",
21+
"which": "catalog:"
2122
}
2223
}

packages/bootstrap/src/shared/bootstrap-shared.mjs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,18 @@ import { dlxPackage } from '@socketsecurity/lib/dlx-package'
1111
import { logger } from '@socketsecurity/lib/logger'
1212
import { spawn } from '@socketsecurity/lib/spawn'
1313
import { gte } from 'semver'
14+
import which from 'which'
1415

1516
export const SOCKET_DLX_DIR = path.join(homedir(), '.socket', '_dlx')
1617

18+
/**
19+
* Environment variable to disable node forwarding and use stub instead.
20+
* Set to '1', 'true', or 'yes' to disable forwarding (useful for e2e testing).
21+
*/
22+
const SOCKET_DISABLE_NODE_FORWARDING = ['1', 'true', 'yes'].includes(
23+
process.env.SOCKET_DISABLE_NODE_FORWARDING?.toLowerCase()
24+
)
25+
1726
/**
1827
* Minimum Node.js version with SEA support.
1928
* This constant is injected at build time by esbuild from .config/node-version.mjs.
@@ -52,6 +61,42 @@ export function hasModernNode() {
5261
}
5362
}
5463

64+
/**
65+
* Detect if Node.js is available on the system.
66+
*/
67+
export async function detectSystemNode() {
68+
try {
69+
const nodePath = await which('node', { nothrow: true })
70+
return nodePath || null
71+
} catch {
72+
return null
73+
}
74+
}
75+
76+
/**
77+
* Check if we should forward to system Node.js.
78+
* Returns false if SOCKET_DISABLE_NODE_FORWARDING is set (for e2e testing).
79+
*/
80+
export async function shouldForwardToSystemNode() {
81+
if (SOCKET_DISABLE_NODE_FORWARDING) {
82+
return false
83+
}
84+
const nodePath = await detectSystemNode()
85+
if (!nodePath) {
86+
return false
87+
}
88+
// Check if system node meets minimum version requirements.
89+
try {
90+
const result = await spawn(nodePath, ['--version'], {
91+
stdio: 'pipe',
92+
})
93+
const version = result.stdout.trim()
94+
return gte(version, MIN_NODE_VERSION)
95+
} catch {
96+
return false
97+
}
98+
}
99+
55100
/**
56101
* Execute the CLI with the given arguments.
57102
*/

pnpm-lock.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ catalog:
1111
esbuild: 0.24.0
1212
postject: 1.0.0-alpha.6
1313
semver: 7.6.3
14+
which: 5.0.0

0 commit comments

Comments
 (0)