From 0f8c04b03d15d6029ed578bb5565ac87ba5a1a97 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Mon, 5 Mar 2018 15:09:33 -0500 Subject: [PATCH] use port-authority --- src/cli/dev.ts | 6 ++-- src/cli/export.ts | 7 +++-- src/cli/index.ts | 10 +++---- src/cli/port-utils.ts | 66 ------------------------------------------- 4 files changed, 12 insertions(+), 77 deletions(-) delete mode 100644 src/cli/port-utils.ts diff --git a/src/cli/dev.ts b/src/cli/dev.ts index b0692df..eef7fb1 100644 --- a/src/cli/dev.ts +++ b/src/cli/dev.ts @@ -8,7 +8,7 @@ import mkdirp from 'mkdirp'; import rimraf from 'rimraf'; import format_messages from 'webpack-format-messages'; import prettyMs from 'pretty-ms'; -import * as port_utils from './port-utils'; +import * as ports from 'port-authority'; import { dest } from '../config'; import { create_compilers, create_app, create_routes, create_serviceworker } from 'sapper/core.js'; @@ -77,7 +77,7 @@ export default async function dev(port: number) { rimraf.sync(dir); mkdirp.sync(dir); - const dev_port = await port_utils.find(10000); + const dev_port = await ports.find(10000); const routes = create_routes(); create_app({ routes, dev_port }); @@ -205,7 +205,7 @@ export default async function dev(port: number) { deferreds.client.promise.then(() => { function restart() { - port_utils.wait(3000).then(deferreds.server.fulfil); // TODO control port + ports.wait(3000).then(deferreds.server.fulfil); // TODO control port } if (proc) { diff --git a/src/cli/export.ts b/src/cli/export.ts index b3e9625..05bf4be 100644 --- a/src/cli/export.ts +++ b/src/cli/export.ts @@ -5,7 +5,7 @@ import express from 'express'; import cheerio from 'cheerio'; import URL from 'url-parse'; import fetch from 'node-fetch'; -import * as port_utils from './port-utils'; +import * as ports from 'port-authority'; import { dest } from '../config'; const app = express(); @@ -27,7 +27,7 @@ export default async function exporter(export_dir: string) { sander.copyFileSync(build_dir, 'service-worker.js').to(export_dir, 'service-worker.js'); } - const port = await port_utils.find(3000); + const port = await ports.find(3000); const origin = `http://localhost:${port}`; @@ -36,6 +36,7 @@ export default async function exporter(export_dir: string) { env: { PORT: port, NODE_ENV: 'production', + SAPPER_DEST: build_dir, SAPPER_EXPORT: 'true' } }); @@ -88,7 +89,7 @@ export default async function exporter(export_dir: string) { }); } - return port_utils.wait(port) + return ports.wait(port) .then(() => handle(new URL(origin))) // TODO all static routes .then(() => proc.kill()); } \ No newline at end of file diff --git a/src/cli/index.ts b/src/cli/index.ts index 3c3e06b..e96df3a 100755 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -9,7 +9,7 @@ import build from './build'; import exporter from './export'; import dev from './dev'; import upgrade from './upgrade'; -import * as port_utils from './port-utils'; +import * as ports from 'port-authority'; import { exists } from '../utils'; import * as pkg from '../../package.json'; @@ -22,12 +22,12 @@ prog.command('dev') .option('-p, --port', 'Specify a port') .action(async ({ port }: { port: number }) => { if (port) { - if (!await port_utils.check(port)) { + if (!await ports.check(port)) { console.log(chalk.bold.red(`> Port ${port} is unavailable`)); return; } } else { - port = await port_utils.find(3000); + port = await ports.find(3000); } dev(port); @@ -66,12 +66,12 @@ prog.command('start [dir]') } if (port) { - if (!await port_utils.check(port)) { + if (!await ports.check(port)) { console.log(chalk.bold.red(`> Port ${port} is unavailable`)); return; } } else { - port = await port_utils.find(3000); + port = await ports.find(3000); } child_process.fork(server, [], { diff --git a/src/cli/port-utils.ts b/src/cli/port-utils.ts deleted file mode 100644 index 7719171..0000000 --- a/src/cli/port-utils.ts +++ /dev/null @@ -1,66 +0,0 @@ -import * as net from 'net'; - -export function check(port: number) { - return new Promise(fulfil => { - const server = net.createServer(); - - server.unref(); - - server.on('error', () => { - fulfil(false); - }); - - server.listen({ port }, () => { - server.close(() => { - fulfil(true); - }); - }); - }); -} - -export function find(port: number): Promise { - return new Promise((fulfil) => { - get_port(port, fulfil); - }); -} - -function get_port(port: number, cb: (port: number) => void) { - const server = net.createServer(); - - server.unref(); - - server.on('error', () => { - get_port(port + 1, cb); - }); - - server.listen({ port }, () => { - server.close(() => { - cb(port); - }); - }); -} - -export function wait(port: number, timeout = 5000) { - return new Promise((fulfil, reject) => { - get_connection(port, fulfil); - setTimeout(() => reject(new Error(`timed out waiting for connection`)), timeout); - }); -} - -function get_connection(port: number, cb: () => void) { - const socket = net.connect(port, 'localhost', () => { - cb(); - socket.destroy(); - }); - - socket.on('error', err => { - setTimeout(() => { - get_connection(port, cb); - }, 10); - }); - - setTimeout(() => { - socket.destroy(); - }, 1000); -} -