Skip to content

Commit 959e07c

Browse files
committed
refactor: add IPC utility for shadow npm inject script
Adds src/utils/ipc.mts to centralize IPC data handling. The inject script calls initializeIpc() at startup to wait for the handshake and extract the extra field. The arborist then uses getIpcExtra() to access shadow configuration (API token, bin name, etc.).
1 parent f4682e9 commit 959e07c

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

packages/cli/src/shadow/npm/arborist/lib/arborist/index.mts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { NPX } from '../../../../../constants/agents.mts'
1010
import ENV from '../../../../../constants/env.mts'
1111
import { NODE_MODULES } from '../../../../../constants/packages.mts'
1212
import {
13-
getInternals,
1413
SOCKET_CLI_ACCEPT_RISKS,
1514
SOCKET_CLI_SHADOW_ACCEPT_RISKS,
1615
SOCKET_CLI_SHADOW_API_TOKEN,
@@ -20,6 +19,7 @@ import {
2019
SOCKET_CLI_VIEW_ALL_RISKS,
2120
} from '../../../../../constants/shadow.mts'
2221
import { findUp } from '../../../../../utils/fs/find-up.mjs'
22+
import { getIpcExtra } from '../../../../../utils/ipc.mjs'
2323
import { logAlertsMap } from '../../../../../utils/socket/package-alert.mts'
2424
import {
2525
getAlertsMapFromArborist,
@@ -32,9 +32,6 @@ import type {
3232
NodeClass,
3333
} from '../../types.mts'
3434

35-
const internals = getInternals()
36-
const getIpc = internals.getIpc
37-
3835
export const SAFE_NO_SAVE_ARBORIST_REIFY_OPTIONS_OVERRIDES = {
3936
__proto__: null,
4037
audit: false,
@@ -106,7 +103,7 @@ export class SafeArborist extends Arborist {
106103
...(args.length ? args[0] : undefined),
107104
} as ArboristReifyOptions
108105

109-
const ipc = getIpc ? await getIpc() : undefined
106+
const ipc = getIpcExtra()
110107

111108
const binName = ipc?.[SOCKET_CLI_SHADOW_BIN]
112109
if (!binName) {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import { initializeIpc } from '../../utils/ipc.mjs'
12
import { installSafeArborist } from './arborist/index.mts'
23

4+
// Initialize IPC data handling.
5+
initializeIpc()
6+
37
installSafeArborist()

packages/cli/src/utils/ipc.mts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* IPC data handling for shadow npm processes.
3+
*
4+
* Provides access to IPC data passed via bootstrap handshake.
5+
* The handshake includes shadow configuration like API tokens,
6+
* bin names, and other settings needed by the arborist.
7+
*/
8+
9+
import { waitForBootstrapHandshake } from './sea/boot.mjs'
10+
11+
import type { IpcObject } from '../constants/shadow.mts'
12+
13+
// Store for IPC extra data received via handshake.
14+
let ipcExtra: IpcObject | undefined
15+
16+
/**
17+
* Get IPC extra data from handshake.
18+
* Returns the extra field from the bootstrap handshake.
19+
*/
20+
export function getIpcExtra(): IpcObject | undefined {
21+
return ipcExtra
22+
}
23+
24+
/**
25+
* Initialize IPC data handling.
26+
* Waits for the bootstrap handshake and extracts the extra field.
27+
*/
28+
export async function initializeIpc(): Promise<void> {
29+
try {
30+
const handshake = await waitForBootstrapHandshake(1000)
31+
if (handshake?.['extra']) {
32+
ipcExtra = handshake['extra'] as IpcObject
33+
}
34+
} catch {
35+
// No handshake - running without IPC.
36+
}
37+
}

0 commit comments

Comments
 (0)