Skip to content

Commit 1c88f0b

Browse files
committed
Add constants.processEnv
1 parent 3fe231f commit 1c88f0b

File tree

10 files changed

+52
-14
lines changed

10 files changed

+52
-14
lines changed

bin/cli.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ const { spawn } = require(
1212
path.join(rootPath, 'external/@socketsecurity/registry/lib/spawn.js'),
1313
)
1414

15-
const { NODE_COMPILE_CACHE } = constants
16-
1715
process.exitCode = 1
1816

1917
spawn(
@@ -39,7 +37,8 @@ spawn(
3937
{
4038
env: {
4139
...process.env,
42-
...(NODE_COMPILE_CACHE ? { NODE_COMPILE_CACHE } : undefined),
40+
// Lazily access constants.processEnv.
41+
...constants.processEnv,
4342
},
4443
stdio: 'inherit',
4544
},

src/constants.mts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ type ENV = Remap<
8282
}>
8383
>
8484

85+
type ProcessEnv = {
86+
[K in keyof ENV]?: string
87+
}
88+
8589
type IPC = Readonly<{
8690
SOCKET_CLI_FIX?: string | undefined
8791
SOCKET_CLI_OPTIMIZE?: boolean | undefined
@@ -162,6 +166,7 @@ type Constants = Remap<
162166
readonly minimumVersionByAgent: Map<Agent, string>
163167
readonly nmBinPath: string
164168
readonly nodeHardenFlags: string[]
169+
readonly processEnv: ProcessEnv
165170
readonly rootPath: string
166171
readonly shadowBinPath: string
167172
readonly shadowNpmBinPath: string
@@ -508,6 +513,28 @@ const lazyNpmNmNodeGypPath = () =>
508513
'../../node_modules/node-gyp/bin/node-gyp.js',
509514
)
510515

516+
const lazyProcessEnv = () =>
517+
// Lazily access constants.ENV.
518+
Object.setPrototypeOf(
519+
Object.fromEntries(
520+
Object.entries(constants.ENV).reduce(
521+
(entries, entry) => {
522+
const { 0: key, 1: value } = entry
523+
if (typeof value === 'string') {
524+
if (value) {
525+
entries.push(entry as [string, string])
526+
}
527+
} else if (typeof value === 'boolean') {
528+
entries.push([key, value ? '1' : '0'])
529+
}
530+
return entries
531+
},
532+
[] as Array<[string, string]>,
533+
),
534+
),
535+
null,
536+
)
537+
511538
const lazyRootPath = () => path.join(realpathSync.native(__dirname), '..')
512539

513540
const lazyShadowBinPath = () =>
@@ -638,6 +665,7 @@ const constants: Constants = createConstantsObject(
638665
nmBinPath: undefined,
639666
nodeHardenFlags: undefined,
640667
npmNmNodeGypPath: undefined,
668+
processEnv: undefined,
641669
rootPath: undefined,
642670
shadowBinPath: undefined,
643671
shadowNpmInjectPath: undefined,
@@ -669,6 +697,7 @@ const constants: Constants = createConstantsObject(
669697
nmBinPath: lazyNmBinPath,
670698
nodeHardenFlags: lazyNodeHardenFlags,
671699
npmNmNodeGypPath: lazyNpmNmNodeGypPath,
700+
processEnv: lazyProcessEnv,
672701
rootPath: lazyRootPath,
673702
shadowBinPath: lazyShadowBinPath,
674703
shadowNpmBinPath: lazyShadowNpmBinPath,

src/shadow/npm/bin.mts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ export default async function shadowBin(
2020
args = process.argv.slice(2),
2121
) {
2222
process.exitCode = 1
23-
// Lazily access constants.ENV.NODE_COMPILE_CACHE
24-
const { NODE_COMPILE_CACHE } = constants.ENV
2523
const terminatorPos = args.indexOf('--')
2624
const rawBinArgs = terminatorPos === -1 ? args : args.slice(0, terminatorPos)
2725
const binArgs = rawBinArgs.filter(
@@ -101,7 +99,8 @@ export default async function shadowBin(
10199
{
102100
env: {
103101
...process.env,
104-
...(NODE_COMPILE_CACHE ? { NODE_COMPILE_CACHE } : undefined),
102+
// Lazily access constants.processEnv.
103+
...constants.processEnv,
105104
},
106105
// 'inherit' + 'ipc'
107106
stdio: [0, 1, 2, 'ipc'],

src/shadow/npm/install.mts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ export function safeNpmInstall(
4141
spinner,
4242
...spawnOptions
4343
} = { __proto__: null, ...options } as SafeNpmInstallOptions
44-
// Lazily access constants.ENV.NODE_COMPILE_CACHE
45-
const { NODE_COMPILE_CACHE } = constants.ENV
4644
let stdio = spawnOptions.stdio
4745
const useIpc = isObject(ipc)
4846
// Include 'ipc' in the spawnOptions.stdio when an options.ipc object is provided.
@@ -101,7 +99,8 @@ export function safeNpmInstall(
10199
stdio,
102100
env: {
103101
...process.env,
104-
...(NODE_COMPILE_CACHE ? { NODE_COMPILE_CACHE } : undefined),
102+
// Lazily access constants.processEnv.
103+
...constants.processEnv,
105104
...spawnOptions.env,
106105
},
107106
},

src/utils/agent.mts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ export function runAgentInstall(
4545
...spawnOptions,
4646
env: {
4747
...process.env,
48+
// Lazily access constants.processEnv.
49+
...constants.processEnv,
4850
NODE_OPTIONS: cmdFlagsToString([
4951
...(skipNodeHardenFlags
5052
? []

src/utils/coana.mts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ export async function spawnCoana(
2929
...options,
3030
env: {
3131
...process.env,
32-
...spawnEnv,
33-
SOCKET_CLI_API_BASE_URL:
34-
constants.ENV.SOCKET_CLI_API_BASE_URL || undefined,
32+
// Lazily access constants.processEnv.
33+
...constants.processEnv,
3534
SOCKET_CLI_API_TOKEN: getDefaultToken(),
35+
...spawnEnv,
3636
},
3737
},
3838
extra,

src/utils/sdk.mts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ export function hasDefaultToken(): boolean {
7171

7272
export function getPublicToken(): string {
7373
return (
74+
getDefaultToken() ||
7475
// Lazily access constants.ENV.SOCKET_CLI_API_TOKEN.
75-
(constants.ENV.SOCKET_CLI_API_TOKEN || getDefaultToken()) ??
76+
constants.ENV.SOCKET_CLI_API_TOKEN ||
7677
// Lazily access constants.SOCKET_PUBLIC_API_TOKEN.
7778
constants.SOCKET_PUBLIC_API_TOKEN
7879
)

test/socket-cdxgen.test.mts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ const spawnOpts: PromiseSpawnOptions = {
2323
cwd: npmFixturesPath,
2424
env: {
2525
...process.env,
26+
// Lazily access constants.processEnv.
27+
...constants.processEnv,
2628
SOCKET_CLI_DEBUG: '1',
2729
},
2830
}

test/socket-npm.test.mts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ for (const npmDir of ['npm9', 'npm10', 'npm11']) {
5757
cwd: path.join(npmFixturesPath, 'lacking-typosquat'),
5858
env: {
5959
...process.env,
60+
// Lazily access constants.processEnv.
61+
...constants.processEnv,
6062
// Lazily access constants.ENV.PATH.
6163
PATH: `${npmBinPath}:${constants.ENV.PATH}`,
6264
},

test/utils.mts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@ export async function invokeNpm(
5252
[entryPath, ...args],
5353
{
5454
cwd: npmFixturesPath,
55-
env: { ...process.env, ...env },
55+
env: {
56+
...process.env,
57+
// Lazily access constants.processEnv.
58+
...constants.processEnv,
59+
...env,
60+
},
5661
},
5762
)
5863
return {

0 commit comments

Comments
 (0)