Skip to content

Commit

Permalink
Move socket setup to a function
Browse files Browse the repository at this point in the history
Makes the start() fn a bit easier to understand
  • Loading branch information
goto-bus-stop committed Aug 17, 2018
1 parent a98b679 commit 515c5f9
Showing 1 changed file with 30 additions and 22 deletions.
52 changes: 30 additions & 22 deletions autocannon.js
Expand Up @@ -161,29 +161,16 @@ function start (argv) {
process.exit(1)
}

const pipeName = `${process.pid}.autocannon`
const socketPath = process.platform === 'win32'
? `\\\\?\\pipe\\${pipeName}`
: path.join(os.tmpdir(), pipeName)
const server = net.createServer((socket) => {
socket.once('data', (chunk) => {
const port = chunk.toString()
const url = new URL(argv.url, `http://localhost:${port}`).href
const opts = Object.assign({}, argv, {
onPort: false,
url: url
})
runTracker(opts, () => {
proc.kill('SIGINT')
server.close()
})
const { socketPath, server } = createChannel((port) => {
const url = new URL(argv.url, `http://localhost:${port}`).href
const opts = Object.assign({}, argv, {
onPort: false,
url: url
})
runTracker(opts, () => {
proc.kill('SIGINT')
server.close()
})
})
server.listen(socketPath)
server.on('close', () => {
try {
fs.unlinkSync(socketPath)
} catch (err) {}
})

// manage-path always uses the $PATH variable, but we can pretend
Expand All @@ -204,6 +191,27 @@ function start (argv) {
}
}

function createChannel (onport) {
const pipeName = `${process.pid}.autocannon`
const socketPath = process.platform === 'win32'
? `\\\\?\\pipe\\${pipeName}`
: path.join(os.tmpdir(), pipeName)
const server = net.createServer((socket) => {
socket.once('data', (chunk) => {
const port = chunk.toString()
onport(port)
})
})
server.listen(socketPath)
server.on('close', () => {
try {
fs.unlinkSync(socketPath)
} catch (err) {}
})

return { socketPath, server }
}

function runTracker (argv, ondone) {
const tracker = run(argv)

Expand Down

0 comments on commit 515c5f9

Please sign in to comment.