feat: save server connection info in a subdir inside the store

This commit is contained in:
Zoltan Kochan
2018-05-05 22:11:56 +03:00
committed by Zoltan Kochan
parent d9ea0dbc83
commit 2bdeeeb9b9
6 changed files with 26 additions and 19 deletions

View File

@@ -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'),
}
}
}

View File

@@ -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

View File

@@ -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

View File

@@ -0,0 +1,3 @@
import path = require('path')
export default (storePath: string) => path.join(storePath, 'server')

View File

@@ -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)

View File

@@ -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'))