@@ -6,8 +6,16 @@ import registryConstants from '@socketsecurity/registry/lib/constants'
66import { envAsBoolean } from '@socketsecurity/registry/lib/env'
77import { isObject } from '@socketsecurity/registry/lib/objects'
88
9+ import type { Serializable } from 'node:child_process'
10+
911type 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+
1119type 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
4853const { 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-
7155const {
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+
109130const 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 ,
0 commit comments