Skip to content

Commit

Permalink
Fix unresolved promise
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed Jan 17, 2025
1 parent 349dbfe commit c2b041b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
13 changes: 5 additions & 8 deletions src/commands/optimize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -918,19 +918,16 @@ export const optimize: CliSubcommand = {
spinner.start(`Updating ${lockName}...`)
try {
if (isNpm) {
await shadowNpmInstall({
ipc: {
[SOCKET_CLI_UPDATE_OVERRIDES_IN_PACKAGE_LOCK_FILE]: true
}
})
const ipc = {
[SOCKET_CLI_UPDATE_OVERRIDES_IN_PACKAGE_LOCK_FILE]: true
}
await shadowNpmInstall({ ipc })
// TODO: This is a temporary workaround for a `npm ci` bug where it
// will error out after Socket Optimize generates a lock file. More
// investigation is needed.
await shadowNpmInstall({
flags: ['--ignore-scripts', '--package-lock-only'],
ipc: {
[SOCKET_CLI_UPDATE_OVERRIDES_IN_PACKAGE_LOCK_FILE]: true
}
ipc
})
} else {
// All package managers support the "install" command.
Expand Down
43 changes: 24 additions & 19 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { realpathSync } from 'node:fs'
import { realpathSync, writeFileSync } from 'node:fs'

Check failure on line 1 in src/constants.ts

View workflow job for this annotation

GitHub Actions / Linting / Test (20, ubuntu-latest)

'writeFileSync' is declared but its value is never read.

Check failure on line 1 in src/constants.ts

View workflow job for this annotation

GitHub Actions / type-check / TS 5.4, "esnext", ./.

'writeFileSync' is declared but its value is never read.

Check failure on line 1 in src/constants.ts

View workflow job for this annotation

GitHub Actions / type-check / TS 5.4, "esnext", ./.

'writeFileSync' is declared but its value is never read.
import path from 'node:path'
import process from 'node:process'

Expand Down Expand Up @@ -103,27 +103,32 @@ const LAZY_IPC = (() => {
_ipc[key] = false
defineGetter(ipc, key, () => _ipc[key])
}
void new Promise<void>(resolve => {
const onmessage = (ipcData_: Serializable) => {
const ipcData: { [key: string]: any } = {
__proto__: null,
...(isObject(ipcData_) ? ipcData_ : {})
// A forked subprocess will have the 'send' method.
// https://nodejs.org/api/child_process.html#subprocesssendmessage-sendhandle-options-callback
if (typeof process.send === 'function') {
void new Promise<void>(resolve => {
const onmessage = (ipcData_: Serializable) => {
finish()
const ipcData: { [key: string]: any } = {
__proto__: null,
...(isObject(ipcData_) ? ipcData_ : {})
}
for (const key of keys) {
_ipc[key] = ipcData[key]
}
}
for (const key of keys) {
_ipc[key] = ipcData[key]
}
resolve()
}
process.once('message', onmessage)
abortSignal.addEventListener(
'abort',
() => {
const finish = () => {
abortSignal.removeEventListener('abort', finish)
process.removeListener('message', onmessage)
resolve()
},
{ once: true }
)
})
}
abortSignal.addEventListener('abort', finish, { once: true })
process.on('message', onmessage)
// The timeout of 100ms is to prevent an unresolved promised. It should be
// more than enough time for the IPC handshake.
setTimeout(finish, 100)
})
}
return () => Object.freeze(ipc)
})()

Expand Down

0 comments on commit c2b041b

Please sign in to comment.