Skip to content

Commit c2b041b

Browse files
committed
Fix unresolved promise
1 parent 349dbfe commit c2b041b

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

src/commands/optimize.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -918,19 +918,16 @@ export const optimize: CliSubcommand = {
918918
spinner.start(`Updating ${lockName}...`)
919919
try {
920920
if (isNpm) {
921-
await shadowNpmInstall({
922-
ipc: {
923-
[SOCKET_CLI_UPDATE_OVERRIDES_IN_PACKAGE_LOCK_FILE]: true
924-
}
925-
})
921+
const ipc = {
922+
[SOCKET_CLI_UPDATE_OVERRIDES_IN_PACKAGE_LOCK_FILE]: true
923+
}
924+
await shadowNpmInstall({ ipc })
926925
// TODO: This is a temporary workaround for a `npm ci` bug where it
927926
// will error out after Socket Optimize generates a lock file. More
928927
// investigation is needed.
929928
await shadowNpmInstall({
930929
flags: ['--ignore-scripts', '--package-lock-only'],
931-
ipc: {
932-
[SOCKET_CLI_UPDATE_OVERRIDES_IN_PACKAGE_LOCK_FILE]: true
933-
}
930+
ipc
934931
})
935932
} else {
936933
// All package managers support the "install" command.

src/constants.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { realpathSync } from 'node:fs'
1+
import { realpathSync, writeFileSync } from 'node:fs'
22
import path from 'node:path'
33
import process from 'node:process'
44

@@ -103,27 +103,32 @@ const LAZY_IPC = (() => {
103103
_ipc[key] = false
104104
defineGetter(ipc, key, () => _ipc[key])
105105
}
106-
void new Promise<void>(resolve => {
107-
const onmessage = (ipcData_: Serializable) => {
108-
const ipcData: { [key: string]: any } = {
109-
__proto__: null,
110-
...(isObject(ipcData_) ? ipcData_ : {})
106+
// A forked subprocess will have the 'send' method.
107+
// https://nodejs.org/api/child_process.html#subprocesssendmessage-sendhandle-options-callback
108+
if (typeof process.send === 'function') {
109+
void new Promise<void>(resolve => {
110+
const onmessage = (ipcData_: Serializable) => {
111+
finish()
112+
const ipcData: { [key: string]: any } = {
113+
__proto__: null,
114+
...(isObject(ipcData_) ? ipcData_ : {})
115+
}
116+
for (const key of keys) {
117+
_ipc[key] = ipcData[key]
118+
}
111119
}
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-
() => {
120+
const finish = () => {
121+
abortSignal.removeEventListener('abort', finish)
121122
process.removeListener('message', onmessage)
122123
resolve()
123-
},
124-
{ once: true }
125-
)
126-
})
124+
}
125+
abortSignal.addEventListener('abort', finish, { once: true })
126+
process.on('message', onmessage)
127+
// The timeout of 100ms is to prevent an unresolved promised. It should be
128+
// more than enough time for the IPC handshake.
129+
setTimeout(finish, 100)
130+
})
131+
}
127132
return () => Object.freeze(ipc)
128133
})()
129134

0 commit comments

Comments
 (0)