@@ -11,59 +11,126 @@ import { maintainedNodeVersions } from './maintained-node-versions'
1111const NODE_VERSION = process . version
1212
1313// Version detection.
14+ /**
15+ * Get the full Node.js version string from `process.version`.
16+ *
17+ * @returns The runtime version, including the leading `v` (e.g. `v22.11.0`).
18+ */
1419export function getNodeVersion ( ) : string {
1520 return NODE_VERSION
1621}
1722
23+ /**
24+ * Get the major component of the current Node.js version.
25+ *
26+ * @returns The major version number, or `0` if it cannot be parsed.
27+ */
1828export function getNodeMajorVersion ( ) : number {
1929 const major = NODE_VERSION . slice ( 1 ) . split ( '.' ) [ 0 ] ?? '0'
2030 return Number . parseInt ( major , 10 ) || 0
2131}
2232
33+ /**
34+ * Get the minor component of the current Node.js version.
35+ *
36+ * @returns The minor version number, or `0` if it cannot be parsed.
37+ */
2338export function getNodeMinorVersion ( ) : number {
2439 return Number . parseInt ( NODE_VERSION . split ( '.' ) [ 1 ] ?? '0' , 10 )
2540}
2641
42+ /**
43+ * Get the patch component of the current Node.js version.
44+ *
45+ * @returns The patch version number, or `0` if it cannot be parsed.
46+ */
2747export function getNodePatchVersion ( ) : number {
2848 return Number . parseInt ( NODE_VERSION . split ( '.' ) [ 2 ] ?? '0' , 10 )
2949}
3050
3151// Maintained Node.js versions.
52+ /**
53+ * Get the list of Node.js major versions currently under long-term support.
54+ *
55+ * @returns The static `maintainedNodeVersions` array shared across the library.
56+ */
3257export function getMaintainedNodeVersions ( ) {
3358 return maintainedNodeVersions
3459}
3560
3661// Feature detection.
62+ /**
63+ * Check whether the current runtime exposes the `module.enableCompileCache()` API.
64+ * The API is available on Node.js 24+.
65+ *
66+ * @returns `true` when the current runtime is Node.js 24 or newer.
67+ */
3768export function supportsNodeCompileCacheApi ( ) : boolean {
3869 const major = getNodeMajorVersion ( )
3970 return major >= 24
4071}
4172
73+ /**
74+ * Check whether the current runtime honors the `NODE_COMPILE_CACHE` env var.
75+ * Env-var-based compile caching is available on Node.js 22+.
76+ *
77+ * @returns `true` when the current runtime is Node.js 22 or newer.
78+ */
4279export function supportsNodeCompileCacheEnvVar ( ) : boolean {
4380 const major = getNodeMajorVersion ( )
4481 return major >= 22
4582}
4683
84+ /**
85+ * Check whether the current runtime supports the `--disable-warning` CLI flag.
86+ * The flag is available on Node.js 21+.
87+ *
88+ * @returns `true` when the current runtime is Node.js 21 or newer.
89+ */
4790export function supportsNodeDisableWarningFlag ( ) : boolean {
4891 const major = getNodeMajorVersion ( )
4992 return major >= 21
5093}
5194
95+ /**
96+ * Check whether the current runtime supports the permission model CLI flags
97+ * (`--experimental-permission` on Node 20-23, `--permission` on Node 24+).
98+ *
99+ * @returns `true` when the current runtime is Node.js 20 or newer.
100+ */
52101export function supportsNodePermissionFlag ( ) : boolean {
53102 const major = getNodeMajorVersion ( )
54103 return major >= 20
55104}
56105
106+ /**
107+ * Check whether `require()` can synchronously load ESM modules.
108+ * Requires Node.js 22.12+ or Node.js 23+.
109+ *
110+ * @returns `true` when the runtime supports `require()`-ing ES modules.
111+ */
57112export function supportsNodeRequireModule ( ) : boolean {
58113 const major = getNodeMajorVersion ( )
59114 return major >= 23 || ( major === 22 && getNodeMinorVersion ( ) >= 12 )
60115}
61116
117+ /**
118+ * Check whether the current runtime supports `node --run <script>`.
119+ * Requires Node.js 22.11+ or Node.js 23+.
120+ *
121+ * @returns `true` when the runtime can execute package.json scripts via `--run`.
122+ */
62123export function supportsNodeRun ( ) : boolean {
63124 const major = getNodeMajorVersion ( )
64125 return major >= 23 || ( major === 22 && getNodeMinorVersion ( ) >= 11 )
65126}
66127
128+ /**
129+ * Check whether the current runtime supports the `--disable-sigusr1` CLI flag.
130+ * Flag landed in v22.14.0 and v23.7.0 and was stabilized in v22.20.0 / v24.8.0.
131+ *
132+ * @returns `true` when the runtime exposes `--disable-sigusr1`.
133+ */
67134export function supportsNodeDisableSigusr1Flag ( ) : boolean {
68135 const major = getNodeMajorVersion ( )
69136 const minor = getNodeMinorVersion ( )
@@ -82,6 +149,13 @@ export function supportsNodeDisableSigusr1Flag(): boolean {
82149}
83150
84151let _nodeDisableSigusr1Flags : string [ ]
152+ /**
153+ * Get the flags used to block Node.js debugger attachment via SIGUSR1.
154+ * Returns `['--disable-sigusr1']` on runtimes that support it and falls back
155+ * to `['--no-inspect']` on older versions.
156+ *
157+ * @returns A non-empty array of CLI flags suitable for passing to `node`.
158+ */
85159export function getNodeDisableSigusr1Flags ( ) : string [ ] {
86160 if ( _nodeDisableSigusr1Flags === undefined ) {
87161 // SIGUSR1 is reserved by Node.js for starting the debugger/inspector.
@@ -99,13 +173,27 @@ export function getNodeDisableSigusr1Flags(): string[] {
99173 return _nodeDisableSigusr1Flags
100174}
101175
176+ /**
177+ * Check whether this process was spawned with an IPC channel.
178+ * When `true`, `process.send()` is callable to message the parent process.
179+ *
180+ * @returns `true` when the current process has an IPC channel to its parent.
181+ */
102182export function supportsProcessSend ( ) : boolean {
103183 return typeof process . send === 'function'
104184}
105185
106186// Node.js flags.
107187let _nodeHardenFlags : string [ ]
108188let _nodePermissionFlags : string [ ]
189+ /**
190+ * Get the permission-grant flags needed to run npm under Node.js 24+'s
191+ * `--permission` model. The array is non-empty only on Node.js 24+ and
192+ * includes `--allow-fs-read=*`, `--allow-fs-write=*`, and
193+ * `--allow-child-process`. Older versions return an empty array.
194+ *
195+ * @returns The permission flag list (possibly empty) for the current runtime.
196+ */
109197export function getNodePermissionFlags ( ) : string [ ] {
110198 if ( _nodePermissionFlags === undefined ) {
111199 const major = getNodeMajorVersion ( )
@@ -129,6 +217,15 @@ export function getNodePermissionFlags(): string[] {
129217 return _nodePermissionFlags
130218}
131219
220+ /**
221+ * Get the hardening flags Socket applies when spawning Node.js subprocesses.
222+ * Always includes `--disable-proto=delete`. Also adds `--permission` plus the
223+ * grants from {@link getNodePermissionFlags} on Node 24+,
224+ * `--experimental-permission` on Node 20-23, and
225+ * `--force-node-api-uncaught-exceptions-policy` on Node 22+.
226+ *
227+ * @returns A non-empty array of CLI flags suitable for passing to `node`.
228+ */
132229export function getNodeHardenFlags ( ) : string [ ] {
133230 if ( _nodeHardenFlags === undefined ) {
134231 const major = getNodeMajorVersion ( )
@@ -156,6 +253,12 @@ export function getNodeHardenFlags(): string[] {
156253}
157254
158255let _nodeNoWarningsFlags : string [ ]
256+ /**
257+ * Get the flags that silence Node.js runtime warnings and deprecation notices.
258+ * Always returns `['--no-warnings', '--no-deprecation']` across all versions.
259+ *
260+ * @returns A non-empty array of CLI flags suitable for passing to `node`.
261+ */
159262export function getNodeNoWarningsFlags ( ) : string [ ] {
160263 if ( _nodeNoWarningsFlags === undefined ) {
161264 _nodeNoWarningsFlags = [ '--no-warnings' , '--no-deprecation' ]
@@ -164,6 +267,11 @@ export function getNodeNoWarningsFlags(): string[] {
164267}
165268
166269// Execution path.
270+ /**
271+ * Get the absolute path to the currently running Node.js binary.
272+ *
273+ * @returns The value of `process.execPath`.
274+ */
167275export function getExecPath ( ) : string {
168276 return process . execPath
169277}
0 commit comments