Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
opts: CreateStoreControllerOptions & {
background?: boolean,
protocol?: 'auto' | 'tcp' | 'ipc',
port?: number,
ignoreStopRequests?: boolean,
ignoreUploadRequests?: boolean,
},
) => {
if (opts.protocol === 'ipc' && opts.port) {
throw new Error('Port cannot be selected when server communicates via IPC')
}
if (opts.background && !Diable.isDaemon()) {
Diable()
}
const storeDir = await storePath(opts.dir, opts.storeDir)
const connectionInfoDir = serverConnectionInfoDir(storeDir)
const serverJsonPath = path.join(connectionInfoDir, 'server.json')
await makeDir(connectionInfoDir)
// Open server.json with exclusive write access to ensure only one process can successfully
// start the server. Note: NFS does not support exclusive writing, but do we really care?
// Source: https://github.com/moxystudio/node-proper-lockfile#user-content-comparison
let fd: number|null
try {
fd = await fs.open(serverJsonPath, 'wx')
} catch (error) {
if (error.code !== 'EEXIST') {
throw error
}
throw new PnpmError('SERVER_MANIFEST_LOCKED', `Canceling startup of server (pid ${process.pid}) because another process got exclusive access to server.json`)
}
let server: null|{close (): Promise} = null
export default async (
opts: Pick,
) => {
const storeDir = await storePath(opts.dir, opts.storeDir)
const connectionInfoDir = serverConnectionInfoDir(storeDir)
const serverJson = await tryLoadServerJson({
serverJsonPath: path.join(connectionInfoDir, 'server.json'),
shouldRetryOnNoent: false,
})
if (serverJson === null) {
globalInfo(`No server is running for the store at ${storeDir}`)
return
}
console.log(stripIndents`
store: ${storeDir}
process id: ${serverJson.pid}
remote prefix: ${serverJson.connectionOptions.remotePrefix}
`)
}
export default async (
opts: {
storeDir?: string,
dir: string,
},
) => {
const storeDir = await storePath(opts.dir, opts.storeDir)
const connectionInfoDir = serverConnectionInfoDir(storeDir)
const serverJson = await tryLoadServerJson({
serverJsonPath: path.join(connectionInfoDir, 'server.json'),
shouldRetryOnNoent: false,
})
if (serverJson === null) {
globalInfo(`Nothing to stop. No server is running for the store at ${storeDir}`)
return
}
const storeController = await connectStoreController(serverJson.connectionOptions)
await storeController.stop()
if (await serverGracefullyStops(serverJson.pid)) {
globalInfo('Server gracefully stopped')
return
}
globalWarn('Graceful shutdown failed')