-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect.ts
More file actions
67 lines (62 loc) · 2 KB
/
Copy pathdetect.ts
File metadata and controls
67 lines (62 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* @file SEA (Single Executable Application) binary detection + path accessor.
* Two responsibilities (mirror of `src/smol/detect.ts` shape):
*
* 1. `isSeaBinary()` — memoized boolean detector for whether the current process
* is running as a Node.js Single Executable Application. Probes via Node
* 24+'s `node:sea.isSea()` native API; falls back to `false` on older
* runtimes.
* 2. `getSeaBinaryPath()` — returns the path of the SEA binary
* (`process.argv[0]` normalized) when running as SEA, otherwise
* `undefined`. Defensive across runtimes: returns `false` / `undefined`
* cleanly on stock Node < 24, browsers, Deno, Bun.
*/
import process from 'node:process'
import { normalizePath } from '../paths/normalize'
/**
* Cached SEA detection result.
*/
let isSeaCache: boolean | undefined
/**
* Get the current SEA binary path. Only valid when running as a SEA binary.
*
* @example
* ```typescript
* const binPath = getSeaBinaryPath()
* if (binPath) {
* console.log(`Running as SEA binary: ${binPath}`)
* }
* ```
*/
export function getSeaBinaryPath(): string | undefined {
return isSeaBinary() && process.argv[0]
? normalizePath(process.argv[0])
: undefined
}
/**
* Detect if the current process is running as a SEA binary. Uses Node.js 24+
* native API with caching for performance.
*
* @example
* ;```typescript
* if (isSeaBinary()) {
* console.log('Running as a Single Executable Application')
* }
* ```
*/
export function isSeaBinary(): boolean {
if (isSeaCache === undefined) {
try {
// Use Node.js 24+ native SEA detection API.
// eslint-disable-next-line n/no-unsupported-features/node-builtins
const seaModule = require('node:sea')
isSeaCache = seaModule.isSea()
/* c8 ignore start - Node.js < 24 fallback; node:sea is in
supported Node 22+ as of this codebase. */
} catch {
isSeaCache = false
}
/* c8 ignore stop */
}
return isSeaCache ?? false
}