From 2bdeeeb9b9fdedd1e0c133006ea503bb63494bd7 Mon Sep 17 00:00:00 2001 From: Zoltan Kochan Date: Sat, 5 May 2018 22:11:56 +0300 Subject: [PATCH] feat: save server connection info in a subdir inside the store --- src/cmd/server/start.ts | 14 +++++++------- src/cmd/server/stop.ts | 4 +++- src/createStoreController.ts | 6 ++++-- src/serverConnectionInfoDir.ts | 3 +++ test/recursive.ts | 2 +- test/server.ts | 16 ++++++++-------- 6 files changed, 26 insertions(+), 19 deletions(-) create mode 100644 src/serverConnectionInfoDir.ts diff --git a/src/cmd/server/start.ts b/src/cmd/server/start.ts index bcf5c28e38..b72a90c0c7 100644 --- a/src/cmd/server/start.ts +++ b/src/cmd/server/start.ts @@ -10,6 +10,7 @@ import path = require('path') import onExit = require('signal-exit') import writeJsonFile = require('write-json-file') import createStore from '../../createStore' +import serverConnectionInfoDir from '../../serverConnectionInfoDir' import { PnpmOptions } from '../../types' export default async ( @@ -33,18 +34,17 @@ export default async ( store: await storePath(opts.prefix, opts.store), })) - // the store folder will be needed because server will want to create a file there - // for the IPC connection - await mkdirp(store.path) + const connectionInfoDir = serverConnectionInfoDir(store.path) + await mkdirp(connectionInfoDir) const protocol = opts.protocol || opts.port && 'tcp' || 'auto' - const serverOptions = await getServerOptions(store.path, {protocol, port: opts.port}) + const serverOptions = await getServerOptions(connectionInfoDir, {protocol, port: opts.port}) const connectionOptions = { remotePrefix: serverOptions.path ? `http://unix:${serverOptions.path}:` : `http://${serverOptions.hostname}:${serverOptions.port}`, } - const serverJsonPath = path.join(store.path, 'server.json') + const serverJsonPath = path.join(connectionInfoDir, 'server.json') await writeJsonFile(serverJsonPath, { connectionOptions, pid: process.pid, @@ -63,7 +63,7 @@ export default async ( } async function getServerOptions ( - fsPath: string, + connectionInfoDir: string, opts: { protocol: 'auto' | 'tcp' | 'ipc', port?: number, @@ -95,7 +95,7 @@ async function getServerOptions ( function getIpcOptions () { return { - path: path.normalize(fsPath) + path.sep + 'socket', + path: path.join(connectionInfoDir, 'socket'), } } } diff --git a/src/cmd/server/stop.ts b/src/cmd/server/stop.ts index 101d934d66..4c7357d760 100644 --- a/src/cmd/server/stop.ts +++ b/src/cmd/server/stop.ts @@ -7,6 +7,7 @@ import path = require('path') import processExists = require('process-exists') import killcb = require('tree-kill') import promisify = require('util.promisify') +import serverConnectionInfoDir from '../../serverConnectionInfoDir' const kill = promisify(killcb) @@ -19,7 +20,8 @@ export default async ( const store = await storePath(opts.prefix, opts.store) let serverJson: any | undefined // tslint:disable-line try { - serverJson = await loadJsonFile(path.join(store, 'server.json')) + const connectionInfoDir = serverConnectionInfoDir(store) + serverJson = await loadJsonFile(path.join(connectionInfoDir, 'server.json')) } catch (err) { if (err.code !== 'ENOENT') { throw err diff --git a/src/createStoreController.ts b/src/createStoreController.ts index 99641da027..db17bddd88 100644 --- a/src/createStoreController.ts +++ b/src/createStoreController.ts @@ -7,6 +7,7 @@ import path = require('path') import retry = require('retry') import createStore from './createStore' import runServerInBackground from './runServerInBackground' +import serverConnectionInfoDir from './serverConnectionInfoDir' export default async function ( opts: { @@ -39,8 +40,9 @@ export default async function ( path: string, }> { const store = await storePath(opts.prefix, opts.store) + const connectionInfoDir = serverConnectionInfoDir(store) try { - const serverJson = await loadJsonFile(path.join(store, 'server.json')) + const serverJson = await loadJsonFile(path.join(connectionInfoDir, 'server.json')) logger.info('A store server is running. All store manipulations are delegated to it.') return { ctrl: await connectStoreController(serverJson.connectionOptions), // tslint:disable-line @@ -59,7 +61,7 @@ export default async function ( }>((resolve, reject) => { operation.attempt(async (currentAttempt) => { try { - const serverJson = await loadJsonFile(path.join(store, 'server.json')) + const serverJson = await loadJsonFile(path.join(connectionInfoDir, 'server.json')) logger.info('A store server has been started. To stop it, use \`pnpm server stop\`') resolve({ ctrl: await connectStoreController(serverJson.connectionOptions), // tslint:disable-line diff --git a/src/serverConnectionInfoDir.ts b/src/serverConnectionInfoDir.ts new file mode 100644 index 0000000000..d3db7e80aa --- /dev/null +++ b/src/serverConnectionInfoDir.ts @@ -0,0 +1,3 @@ +import path = require('path') + +export default (storePath: string) => path.join(storePath, 'server') diff --git a/test/recursive.ts b/test/recursive.ts index af16d31dc9..c9a93d4311 100644 --- a/test/recursive.ts +++ b/test/recursive.ts @@ -99,7 +99,7 @@ test('recursive installation using server', async t => { const server = spawn(['server', 'start']) - const serverJsonPath = path.resolve('..', 'store', '2', 'server.json') + const serverJsonPath = path.resolve('..', 'store', '2', 'server', 'server.json') const serverJson = await retryLoadJsonFile(serverJsonPath) t.ok(serverJson) t.ok(serverJson.connectionOptions) diff --git a/test/server.ts b/test/server.ts index d5c02823b3..488235e99a 100644 --- a/test/server.ts +++ b/test/server.ts @@ -25,7 +25,7 @@ test('installation using pnpm server', async (t: tape.Test) => { const server = spawn(['server', 'start']) - const serverJsonPath = path.resolve('..', 'store', '2', 'server.json') + const serverJsonPath = path.resolve('..', 'store', '2', 'server', 'server.json') const serverJson = await retryLoadJsonFile(serverJsonPath) t.ok(serverJson) t.ok(serverJson.connectionOptions) @@ -54,7 +54,7 @@ test('store server: headless installation', async (t: tape.Test) => { const server = spawn(['server', 'start']) - const serverJsonPath = path.resolve('..', 'store', '2', 'server.json') + const serverJsonPath = path.resolve('..', 'store', '2', 'server', 'server.json') const serverJson = await retryLoadJsonFile(serverJsonPath) t.ok(serverJson) t.ok(serverJson.connectionOptions) @@ -75,7 +75,7 @@ test('installation using pnpm server that runs in the background', async (t: tap await execPnpm('server', 'start', '--background') - const serverJsonPath = path.resolve('..', 'store', '2', 'server.json') + const serverJsonPath = path.resolve('..', 'store', '2', 'server', 'server.json') const serverJson = await retryLoadJsonFile(serverJsonPath) t.ok(serverJson) t.ok(serverJson.connectionOptions) @@ -104,7 +104,7 @@ test('installation using pnpm server via TCP', async (t: tape.Test) => { const server = spawn(['server', 'start', '--protocol', 'tcp']) - const serverJsonPath = path.resolve('..', 'store', '2', 'server.json') + const serverJsonPath = path.resolve('..', 'store', '2', 'server', 'server.json') const serverJson = await retryLoadJsonFile(serverJsonPath) t.ok(serverJson) t.ok(serverJson.connectionOptions.remotePrefix.indexOf('http://localhost:') === 0, 'TCP is used for communication') @@ -133,7 +133,7 @@ test('pnpm server uses TCP when port specified', async (t: tape.Test) => { const server = spawn(['server', 'start', '--port', '7856']) - const serverJsonPath = path.resolve('..', 'store', '2', 'server.json') + const serverJsonPath = path.resolve('..', 'store', '2', 'server', 'server.json') const serverJson = await retryLoadJsonFile(serverJsonPath) t.ok(serverJson) t.equal(serverJson.connectionOptions.remotePrefix, 'http://localhost:7856', 'TCP with specified port is used for communication') @@ -188,7 +188,7 @@ test('installation using store server started in the background', async (t: tape await execPnpm('install', 'is-positive@1.0.0', '--use-store-server') - const serverJsonPath = path.resolve('..', 'store', '2', 'server.json') + const serverJsonPath = path.resolve('..', 'store', '2', 'server', 'server.json') const serverJson = await retryLoadJsonFile(serverJsonPath) t.ok(serverJson) t.ok(serverJson.connectionOptions) @@ -215,7 +215,7 @@ test('store server started in the background should use store location wanted by await execPnpm('install', 'is-positive@1.0.0', '--use-store-server', '--store', '../store2') - const serverJsonPath = path.resolve('..', 'store2', '2', 'server.json') + const serverJsonPath = path.resolve('..', 'store2', '2', 'server', 'server.json') const serverJson = await retryLoadJsonFile(serverJsonPath) t.ok(serverJson) t.ok(serverJson.connectionOptions) @@ -242,7 +242,7 @@ test('installation without store server running in the background', async (t: ta await execPnpm('install', 'is-positive@1.0.0', '--no-use-store-server') - const serverJsonPath = path.resolve('..', 'store', '2', 'server.json') + const serverJsonPath = path.resolve('..', 'store', '2', 'server', 'server.json') t.notOk(await pathExists(serverJsonPath), 'store server not running') t.ok(project.requireModule('is-positive'))