mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-13 19:45:26 +00:00
use port-authority
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
@@ -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, [], {
|
||||
|
||||
@@ -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<number> {
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user