Skip to content

Commit fe7620c

Browse files
committed
Lazily access new lazy properties
1 parent e67e49d commit fe7620c

File tree

7 files changed

+146
-68
lines changed

7 files changed

+146
-68
lines changed

.dep-stats.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"@socketregistry/hyrious__bun.lockb": "1.0.6",
77
"@socketregistry/yocto-spinner": "^1.0.2",
88
"@socketsecurity/config": "^2.1.3",
9-
"@socketsecurity/registry": "^1.0.55",
9+
"@socketsecurity/registry": "^1.0.56",
1010
"@socketsecurity/sdk": "^1.3.0",
1111
"blessed": "^0.1.81",
1212
"blessed-contrib": "^4.11.0",

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"@socketregistry/hyrious__bun.lockb": "1.0.6",
6262
"@socketregistry/yocto-spinner": "^1.0.2",
6363
"@socketsecurity/config": "^2.1.3",
64-
"@socketsecurity/registry": "^1.0.55",
64+
"@socketsecurity/registry": "^1.0.56",
6565
"@socketsecurity/sdk": "^1.3.0",
6666
"blessed": "^0.1.81",
6767
"blessed-contrib": "^4.11.0",

scripts/constants.js

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,70 @@ const path = require('node:path')
44

55
const registryConstants = require('@socketsecurity/registry/lib/constants')
66
const {
7-
kInternalsSymbol,
87
PACKAGE_JSON,
8+
kInternalsSymbol,
99
[kInternalsSymbol]: { createConstantsObject }
1010
} = registryConstants
1111

1212
const ROLLUP_ENTRY_SUFFIX = '?commonjs-entry'
1313
const ROLLUP_EXTERNAL_SUFFIX = '?commonjs-external'
1414
const SLASH_NODE_MODULES_SLASH = '/node_modules/'
1515

16-
const rootPath = path.resolve(__dirname, '..')
17-
const rootConfigPath = path.join(rootPath, '.config')
18-
const rootDistPath = path.join(rootPath, 'dist')
19-
const rootPackageJsonPath = path.join(rootPath, PACKAGE_JSON)
20-
const rootSrcPath = path.join(rootPath, 'src')
16+
const lazyBabelConfigPath = () =>
17+
// Lazily access constants.rootConfigPath.
18+
path.join(constants.rootConfigPath, 'babel.config.js')
19+
20+
const lazyDepStatsPath = () =>
21+
// Lazily access constants.rootPath.
22+
path.join(constants.rootPath, '.dep-stats.json')
23+
24+
const lazyRootConfigPath = () =>
25+
// Lazily access constants.rootPath.
26+
path.join(constants.rootPath, '.config')
27+
28+
const lazyRootDistPath = () =>
29+
// Lazily access constants.rootPath.
30+
path.join(constants.rootPath, 'dist')
31+
32+
const lazyRootPackageJsonPath = () =>
33+
// Lazily access constants.rootPath.
34+
path.join(constants.rootPath, PACKAGE_JSON)
35+
36+
const lazyRootPath = () => path.resolve(__dirname, '..')
37+
38+
const lazyRootSrcPath = () =>
39+
// Lazily access constants.rootPath.
40+
path.join(constants.rootPath, 'src')
2141

22-
const babelConfigPath = path.join(rootConfigPath, 'babel.config.js')
23-
const depStatsPath = path.join(rootPath, '.dep-stats.json')
24-
const tsconfigPath = path.join(rootConfigPath, 'tsconfig.rollup.json')
42+
const lazyTsconfigPath = () =>
43+
// Lazily access constants.rootConfigPath.
44+
path.join(constants.rootConfigPath, 'tsconfig.rollup.json')
2545

2646
const constants = createConstantsObject(
2747
{
2848
ROLLUP_ENTRY_SUFFIX,
2949
ROLLUP_EXTERNAL_SUFFIX,
3050
SLASH_NODE_MODULES_SLASH,
31-
babelConfigPath,
32-
depStatsPath,
33-
rootConfigPath,
34-
rootDistPath,
35-
rootPackageJsonPath,
36-
rootPath,
37-
rootSrcPath,
38-
tsconfigPath
51+
babelConfigPath: undefined,
52+
depStatsPath: undefined,
53+
rootConfigPath: undefined,
54+
rootDistPath: undefined,
55+
rootPackageJsonPath: undefined,
56+
rootPath: undefined,
57+
rootSrcPath: undefined,
58+
tsconfigPath: undefined
3959
},
4060
{
61+
getters: {
62+
babelConfigPath: lazyBabelConfigPath,
63+
depStatsPath: lazyDepStatsPath,
64+
rootConfigPath: lazyRootConfigPath,
65+
rootDistPath: lazyRootDistPath,
66+
rootPackageJsonPath: lazyRootPackageJsonPath,
67+
rootPath: lazyRootPath,
68+
rootSrcPath: lazyRootSrcPath,
69+
tsconfigPath: lazyTsconfigPath
70+
},
4171
mixin: registryConstants
4272
}
4373
)

src/constants.ts

Lines changed: 63 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ type Constants = {
2828
} & typeof registryConstants
2929

3030
const {
31-
kInternalsSymbol,
3231
PACKAGE_JSON,
32+
kInternalsSymbol,
3333
[kInternalsSymbol as unknown as 'Symbol(kInternalsSymbol)']: {
3434
createConstantsObject
3535
}
@@ -41,52 +41,87 @@ const NPM_REGISTRY_URL = 'https://registry.npmjs.org'
4141
const SOCKET_CLI_ISSUES_URL = 'https://github.com/SocketDev/socket-cli/issues'
4242
const UPDATE_SOCKET_OVERRIDES_IN_PACKAGE_LOCK_FILE =
4343
'UPDATE_SOCKET_OVERRIDES_IN_PACKAGE_LOCK_FILE'
44-
const ENV: Constants['ENV'] = Object.freeze({
45-
...registryConstants.ENV,
46-
// Flag set by the optimize command to bypass the packagesHaveRiskyIssues check.
47-
[UPDATE_SOCKET_OVERRIDES_IN_PACKAGE_LOCK_FILE]: envAsBoolean(
48-
process.env[UPDATE_SOCKET_OVERRIDES_IN_PACKAGE_LOCK_FILE]
49-
)
50-
})
51-
52-
const rootPath = path.resolve(realpathSync(__dirname), '..')
53-
const rootDistPath = path.join(rootPath, 'dist')
54-
const rootBinPath = path.join(rootPath, 'bin')
55-
const rootPkgJsonPath = path.join(rootPath, PACKAGE_JSON)
56-
const nmBinPath = path.join(rootPath, 'node_modules/.bin')
57-
const cdxgenBinPath = path.join(nmBinPath, 'cdxgen')
58-
const shadowBinPath = path.join(rootPath, 'shadow-bin')
59-
const synpBinPath = path.join(nmBinPath, 'synp')
6044

6145
const LAZY_DIST_TYPE = () =>
6246
registryConstants.SUPPORTS_NODE_REQUIRE_MODULE ? 'module-sync' : 'require'
6347

64-
const lazyDistPath = () => path.join(rootDistPath, constants.DIST_TYPE)
48+
const LAZY_ENV = () =>
49+
Object.freeze({
50+
// Lazily access registryConstants.ENV.
51+
...registryConstants.ENV,
52+
// Flag set by the optimize command to bypass the packagesHaveRiskyIssues check.
53+
[UPDATE_SOCKET_OVERRIDES_IN_PACKAGE_LOCK_FILE]: envAsBoolean(
54+
process.env[UPDATE_SOCKET_OVERRIDES_IN_PACKAGE_LOCK_FILE]
55+
)
56+
})
57+
58+
const lazyCdxgenBinPath = () =>
59+
// Lazily access constants.nmBinPath.
60+
path.join(constants.nmBinPath, 'cdxgen')
61+
62+
const lazyDistPath = () =>
63+
// Lazily access constants.rootDistPath and constants.DIST_TYPE.
64+
path.join(constants.rootDistPath, constants.DIST_TYPE)
65+
66+
const lazyNmBinPath = () =>
67+
// Lazily access constants.rootPath.
68+
path.join(constants.rootPath, 'node_modules/.bin')
69+
70+
const lazyRootBinPath = () =>
71+
// Lazily access constants.rootPath.
72+
path.join(constants.rootPath, 'bin')
73+
74+
const lazyRootDistPath = () =>
75+
// Lazily access constants.rootPath.
76+
path.join(constants.rootPath, 'dist')
77+
78+
const lazyRootPath = () => path.resolve(realpathSync(__dirname), '..')
79+
80+
const lazyRootPkgJsonPath = () =>
81+
// Lazily access constants.rootPath.
82+
path.join(constants.rootPath, PACKAGE_JSON)
83+
84+
const lazyShadowBinPath = () =>
85+
// Lazily access constants.rootPath.
86+
path.join(constants.rootPath, 'shadow-bin')
87+
88+
const lazySynpBinPath = () =>
89+
// Lazily access constants.nmBinPath.
90+
path.join(constants.nmBinPath, 'synp')
6591

6692
const constants = <Constants>createConstantsObject(
6793
{
6894
API_V0_URL,
6995
BABEL_RUNTIME,
70-
ENV,
96+
ENV: undefined,
7197
// Lazily defined values are initialized as `undefined` to keep their key order.
7298
DIST_TYPE: undefined,
7399
NPM_REGISTRY_URL,
74100
SOCKET_CLI_ISSUES_URL,
75101
UPDATE_SOCKET_OVERRIDES_IN_PACKAGE_LOCK_FILE,
76-
cdxgenBinPath,
102+
cdxgenBinPath: undefined,
77103
distPath: undefined,
78-
nmBinPath,
79-
rootBinPath,
80-
rootDistPath,
81-
rootPath,
82-
rootPkgJsonPath,
83-
shadowBinPath,
84-
synpBinPath
104+
nmBinPath: undefined,
105+
rootBinPath: undefined,
106+
rootDistPath: undefined,
107+
rootPath: undefined,
108+
rootPkgJsonPath: undefined,
109+
shadowBinPath: undefined,
110+
synpBinPath: undefined
85111
},
86112
{
87113
getters: {
88114
DIST_TYPE: LAZY_DIST_TYPE,
89-
distPath: lazyDistPath
115+
ENV: LAZY_ENV,
116+
distPath: lazyDistPath,
117+
cdxgenBinPath: lazyCdxgenBinPath,
118+
nmBinPath: lazyNmBinPath,
119+
rootBinPath: lazyRootBinPath,
120+
rootDistPath: lazyRootDistPath,
121+
rootPath: lazyRootPath,
122+
rootPkgJsonPath: lazyRootPkgJsonPath,
123+
shadowBinPath: lazyShadowBinPath,
124+
synpBinPath: lazySynpBinPath
90125
},
91126
mixin: registryConstants
92127
}

test/socket-cdxgen.test.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ type PromiseSpawnOptions = Exclude<Parameters<typeof spawn>[2], undefined> & {
1010
encoding?: BufferEncoding | undefined
1111
}
1212

13-
const { abortSignal, execPath, rootBinPath } = constants
13+
const { abortSignal } = constants
1414

15-
const entryPath = path.join(rootBinPath, 'cli.js')
1615
const testPath = __dirname
1716
const npmFixturesPath = path.join(testPath, 'socket-npm-fixtures')
1817

@@ -23,11 +22,15 @@ const spawnOpts: PromiseSpawnOptions = {
2322
}
2423

2524
describe('Socket cdxgen command', async () => {
25+
// Lazily access constants.rootBinPath.
26+
const entryPath = path.join(constants.rootBinPath, 'cli.js')
27+
2628
it('should forwards known commands to cdxgen', async () => {
2729
for (const command of ['-h', '--help']) {
2830
// eslint-disable-next-line no-await-in-loop
2931
const ret = await spawn(
30-
execPath,
32+
// Lazily access constants.execPath.
33+
constants.execPath,
3134
[entryPath, 'cdxgen', command],
3235
spawnOpts
3336
)
@@ -38,15 +41,18 @@ describe('Socket cdxgen command', async () => {
3841
for (const command of ['-u', '--unknown']) {
3942
// eslint-disable-next-line no-await-in-loop
4043
await assert.rejects(
41-
() => spawn(execPath, [entryPath, 'cdxgen', command], spawnOpts),
44+
() =>
45+
// Lazily access constants.execPath.
46+
spawn(constants.execPath, [entryPath, 'cdxgen', command], spawnOpts),
4247
e => e?.['stderr']?.startsWith(`Unknown argument: ${command}`),
4348
'singular'
4449
)
4550
}
4651
await assert.rejects(
4752
() =>
4853
spawn(
49-
execPath,
54+
// Lazily access constants.execPath.
55+
constants.execPath,
5056
[entryPath, 'cdxgen', '-u', '-h', '--unknown'],
5157
spawnOpts
5258
),

test/socket-npm.test.cjs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ const { describe, it } = require('node:test')
88
const spawn = require('@npmcli/promise-spawn')
99

1010
const constants = require('../dist/constants.js')
11-
const { abortSignal, execPath, rootBinPath } = constants
11+
const { abortSignal } = constants
1212

13-
const entryPath = path.join(rootBinPath, 'cli.js')
1413
const testPath = __dirname
1514
const npmFixturesPath = path.join(testPath, 'socket-npm-fixtures')
1615

@@ -26,19 +25,27 @@ for (const npm of ['npm8', 'npm10']) {
2625
})
2726

2827
describe(`Socket npm wrapper for ${npm}`, () => {
28+
// Lazily access constants.rootBinPath.
29+
const entryPath = path.join(constants.rootBinPath, 'cli.js')
30+
2931
it('should bail on new typosquat', async () => {
3032
await assert.rejects(
3133
() =>
32-
spawn(execPath, [entryPath, 'npm', 'install', 'bowserify'], {
33-
cwd: path.join(npmFixturesPath, 'lacking-typosquat'),
34-
encoding: 'utf8',
35-
env: {
36-
// Make sure we don't borrow TTY from parent.
37-
SOCKET_SECURITY_TTY_IPC: undefined,
38-
PATH: `${npmBinPath}:${process.env.PATH}`
39-
},
40-
signal: abortSignal
41-
}),
34+
spawn(
35+
// Lazily access constants.execPath.
36+
constants.execPath,
37+
[entryPath, 'npm', 'install', 'bowserify'],
38+
{
39+
cwd: path.join(npmFixturesPath, 'lacking-typosquat'),
40+
encoding: 'utf8',
41+
env: {
42+
// Make sure we don't borrow TTY from parent.
43+
SOCKET_SECURITY_TTY_IPC: undefined,
44+
PATH: `${npmBinPath}:${process.env.PATH}`
45+
},
46+
signal: abortSignal
47+
}
48+
),
4249
e => e?.stderr.includes('Unable to prompt')
4350
)
4451
})

0 commit comments

Comments
 (0)