Skip to content

Commit 61e9d09

Browse files
committed
Finish switch to IPC for flags
1 parent d9d8b8e commit 61e9d09

File tree

6 files changed

+84
-54
lines changed

6 files changed

+84
-54
lines changed

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
@@ -63,7 +63,7 @@
6363
"@socketregistry/is-interactive": "^1.0.1",
6464
"@socketregistry/is-unicode-supported": "^1.0.0",
6565
"@socketsecurity/config": "^2.1.3",
66-
"@socketsecurity/registry": "^1.0.72",
66+
"@socketsecurity/registry": "^1.0.73",
6767
"@socketsecurity/sdk": "^1.4.5",
6868
"blessed": "^0.1.81",
6969
"blessed-contrib": "^4.11.0",

src/constants.ts

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,16 @@ import registryConstants from '@socketsecurity/registry/lib/constants'
66
import { envAsBoolean } from '@socketsecurity/registry/lib/env'
77
import { isObject } from '@socketsecurity/registry/lib/objects'
88

9+
import type { Serializable } from 'node:child_process'
10+
911
type RegistryEnv = typeof registryConstants.ENV
1012

13+
type IPCObject = {
14+
SOCKET_CLI_FIX_PACKAGE_LOCK_FILE: boolean
15+
SOCKET_CLI_UPDATE_OVERRIDES_IN_PACKAGE_LOCK_FILE: boolean
16+
[key: string]: any
17+
}
18+
1119
type Constants = {
1220
readonly API_V0_URL: 'https://api.socket.dev/v0'
1321
readonly BABEL_RUNTIME: '@babel/runtime'
@@ -17,10 +25,7 @@ type Constants = {
1725
SOCKET_CLI_DEBUG: boolean
1826
}
1927
readonly DIST_TYPE: 'module-sync' | 'require'
20-
readonly IPC: () => Promise<{
21-
SOCKET_CLI_FIX_PACKAGE_LOCK_FILE: boolean
22-
SOCKET_CLI_UPDATE_OVERRIDES_IN_PACKAGE_LOCK_FILE: boolean
23-
}>
28+
readonly IPC: IPCObject
2429
readonly LOCK_EXT: '.lock'
2530
readonly MODULE_SYNC: 'module-sync'
2631
readonly NPM_REGISTRY_URL: 'https://registry.npmjs.org'
@@ -47,32 +52,12 @@ type Constants = {
4752

4853
const { abortSignal } = registryConstants
4954

50-
const IPC = (() => {
51-
const promise = new Promise((resolve, reject) => {
52-
process.once('message', ipcData => {
53-
console.log('hi')
54-
const {
55-
[SOCKET_CLI_FIX_PACKAGE_LOCK_FILE]: a,
56-
[SOCKET_CLI_UPDATE_OVERRIDES_IN_PACKAGE_LOCK_FILE]: b
57-
} = <any>{ __proto__: null, ...(isObject(ipcData) ? ipcData : {}) }
58-
console.log('ok')
59-
resolve({
60-
[SOCKET_CLI_FIX_PACKAGE_LOCK_FILE]: !!a,
61-
[SOCKET_CLI_UPDATE_OVERRIDES_IN_PACKAGE_LOCK_FILE]: !!b
62-
})
63-
})
64-
abortSignal.addEventListener('abort', reject, { once: true })
65-
})
66-
return function IPC() {
67-
return promise
68-
}
69-
})()
70-
7155
const {
7256
PACKAGE_JSON,
7357
kInternalsSymbol,
7458
[kInternalsSymbol as unknown as 'Symbol(kInternalsSymbol)']: {
75-
createConstantsObject
59+
createConstantsObject,
60+
defineGetter
7661
}
7762
} = registryConstants
7863

@@ -106,6 +91,42 @@ const LAZY_ENV = () =>
10691
[SOCKET_CLI_DEBUG]: envAsBoolean(process.env[SOCKET_CLI_DEBUG])
10792
})
10893

94+
const LAZY_IPC = (() => {
95+
// Initialize and wire-up immediately.
96+
const keys = [
97+
SOCKET_CLI_FIX_PACKAGE_LOCK_FILE,
98+
SOCKET_CLI_UPDATE_OVERRIDES_IN_PACKAGE_LOCK_FILE
99+
]
100+
const _ipc = (<unknown>{ __proto__: null }) as IPCObject
101+
const ipc = (<unknown>{ __proto__: null }) as IPCObject
102+
for (const key of keys) {
103+
_ipc[key] = false
104+
defineGetter(ipc, key, () => _ipc[key])
105+
}
106+
void new Promise<void>(resolve => {
107+
const onmessage = (ipcData_: Serializable) => {
108+
const ipcData: { [key: string]: any } = {
109+
__proto__: null,
110+
...(isObject(ipcData_) ? ipcData_ : {})
111+
}
112+
for (const key of keys) {
113+
_ipc[key] = ipcData[key]
114+
}
115+
resolve()
116+
}
117+
process.once('message', onmessage)
118+
abortSignal.addEventListener(
119+
'abort',
120+
() => {
121+
process.removeListener('message', onmessage)
122+
resolve()
123+
},
124+
{ once: true }
125+
)
126+
})
127+
return () => Object.freeze(ipc)
128+
})()
129+
109130
const lazyCdxgenBinPath = () =>
110131
// Lazily access constants.nmBinPath.
111132
path.join(constants.nmBinPath, 'cdxgen')
@@ -149,10 +170,10 @@ const constants = <Constants>createConstantsObject(
149170
BABEL_RUNTIME,
150171
BINARY_LOCK_EXT,
151172
BUN,
152-
ENV: undefined,
153173
// Lazily defined values are initialized as `undefined` to keep their key order.
154174
DIST_TYPE: undefined,
155-
IPC,
175+
ENV: undefined,
176+
IPC: undefined,
156177
LOCK_EXT,
157178
MODULE_SYNC,
158179
NPM_REGISTRY_URL,
@@ -180,6 +201,7 @@ const constants = <Constants>createConstantsObject(
180201
getters: {
181202
DIST_TYPE: LAZY_DIST_TYPE,
182203
ENV: LAZY_ENV,
204+
IPC: LAZY_IPC,
183205
distPath: lazyDistPath,
184206
cdxgenBinPath: lazyCdxgenBinPath,
185207
nmBinPath: lazyNmBinPath,

src/shadow/arborist/lib/arborist/alerts.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ export type SocketArtifact = {
6767

6868
const {
6969
API_V0_URL,
70-
ENV,
7170
LOOP_SENTINEL,
7271
SOCKET_CLI_FIX_PACKAGE_LOCK_FILE,
7372
abortSignal
@@ -128,11 +127,15 @@ export function walk(
128127
diff_: Diff | null,
129128
options?: WalkOptions
130129
): InstallEffect[] {
131-
const { fix = ENV[SOCKET_CLI_FIX_PACKAGE_LOCK_FILE] } = <WalkOptions>{
130+
const {
131+
// Lazily access constants.IPC.
132+
fix = constants.IPC[SOCKET_CLI_FIX_PACKAGE_LOCK_FILE]
133+
} = <WalkOptions>{
132134
__proto__: null,
133135
...options
134136
}
135137
const needInfoOn: InstallEffect[] = []
138+
// `diff_` is `null` when `npm install --package-lock-only` is passed.
136139
if (!diff_) {
137140
return needInfoOn
138141
}

src/shadow/arborist/lib/arborist/reify.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from 'node:path'
2+
import process from 'node:process'
23

34
import semver from 'semver'
45

@@ -38,7 +39,6 @@ type SocketPackageAlert = {
3839
const pacote: typeof import('pacote') = require(pacotePath)
3940

4041
const {
41-
ENV,
4242
LOOP_SENTINEL,
4343
NPM,
4444
NPM_REGISTRY_URL,
@@ -154,7 +154,8 @@ async function getPackagesAlerts(
154154
fixable: isFixable
155155
})
156156
}
157-
if (!fixable && !ENV[SOCKET_CLI_FIX_PACKAGE_LOCK_FILE]) {
157+
// Lazily access constants.IPC.
158+
if (!fixable && !constants.IPC[SOCKET_CLI_FIX_PACKAGE_LOCK_FILE]) {
158159
// Before we ask about problematic issues, check to see if they
159160
// already existed in the old version if they did, be quiet.
160161
const existing = pkgs.find(p =>
@@ -321,7 +322,7 @@ async function updateAdvisoryDependencies(
321322
})
322323
node.package.version = targetVersion
323324
// Update resolved and clear integrity for the new version.
324-
node.resolved = `https://registry.npmjs.org/${name}/-/${name}-${targetVersion}.tgz`
325+
node.resolved = `${NPM_REGISTRY_URL}/${name}/-/${name}-${targetVersion}.tgz`
325326
if (node.integrity) {
326327
delete node.integrity
327328
}
@@ -360,24 +361,27 @@ export async function reify(
360361
this: SafeArborist,
361362
...args: Parameters<InstanceType<ArboristClass>['reify']>
362363
): Promise<SafeNode> {
363-
// `this.diff` is `null` when `options.packageLockOnly`, --package-lock-only,
364-
// is `true`.
365-
const needInfoOn = walk(this.diff)
364+
const needInfoOn = await walk(this.diff)
366365
if (
367366
needInfoOn.length! ||
368367
needInfoOn.findIndex(c => c.repository_url === NPM_REGISTRY_URL) === -1
369368
) {
370369
// Nothing to check, hmmm already installed or all private?
371370
return await this[kRiskyReify](...args)
372371
}
373-
const input = process.stdin
374-
const output = process.stderr
372+
// Lazily access constants.IPC.
373+
const {
374+
[SOCKET_CLI_FIX_PACKAGE_LOCK_FILE]: bypassConfirms,
375+
[SOCKET_CLI_UPDATE_OVERRIDES_IN_PACKAGE_LOCK_FILE]: bypassAlerts
376+
} = constants.IPC
377+
const { stderr: output, stdin: input } = process
378+
375379
let alerts: SocketPackageAlert[] | undefined
376380
const proceed =
377-
ENV[SOCKET_CLI_UPDATE_OVERRIDES_IN_PACKAGE_LOCK_FILE] ||
381+
bypassAlerts ||
378382
(await (async () => {
379383
alerts = await getPackagesAlerts(this, needInfoOn, { output })
380-
if (!alerts.length || ENV[SOCKET_CLI_FIX_PACKAGE_LOCK_FILE]) {
384+
if (bypassConfirms || !alerts.length) {
381385
return true
382386
}
383387
return await confirm(
@@ -395,7 +399,7 @@ export async function reify(
395399
if (proceed) {
396400
const fix =
397401
!!alerts?.length &&
398-
(ENV[SOCKET_CLI_FIX_PACKAGE_LOCK_FILE] ||
402+
(bypassConfirms ||
399403
(await confirm(
400404
{
401405
message: 'Try to fix alerts?',
@@ -416,9 +420,13 @@ export async function reify(
416420
ret = await this[kRiskyReify](...args)
417421
await this.loadActual()
418422
await this.buildIdealTree()
419-
alerts = await getPackagesAlerts(this, walk(this.diff, { fix: true }), {
420-
fixable: true
421-
})
423+
alerts = await getPackagesAlerts(
424+
this,
425+
await walk(this.diff, { fix: true }),
426+
{
427+
fixable: true
428+
}
429+
)
422430
alerts = alerts.filter(a => {
423431
const { key } = a
424432
if (prev.has(key)) {

src/utils/promise-fork.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,11 @@ export type ForkResult<Output, Extra> = Promise<
2828
} & Extra
2929
> & { process: BuiltinForkResult; stdio: BuiltinForkResult['stdio'] }
3030

31-
function isPipe(stdio: StdioOptions = 'pipe', fd: number) {
31+
function isPipe(stdio: StdioOptions = 'pipe', fd: number): boolean {
3232
if (stdio === 'pipe' || stdio === null) {
3333
return true
3434
}
35-
if (Array.isArray(stdio)) {
36-
return isPipe((stdio as any)[fd], fd)
37-
}
38-
return false
35+
return Array.isArray(stdio) ? isPipe((stdio as any)[fd], fd) : false
3936
}
4037

4138
function stdioResult(

0 commit comments

Comments
 (0)