From c9794ebaf80357bf305a9653951dda98a9272846 Mon Sep 17 00:00:00 2001 From: mc007 Date: Mon, 9 Aug 2021 12:03:09 +0200 Subject: [PATCH] package cleanup --- package.json | 3 +- src/serve.js | 103 +++++++++++++++++++++++++++++++-------------------- 2 files changed, 64 insertions(+), 42 deletions(-) diff --git a/package.json b/package.json index b34f5a7..e4e8c86 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ ], "repository": "vercel/serve", "bin": { - "serve": "./bin/serve.js" + "serve": "./src/serve.js" }, "keywords": [ "now", @@ -45,6 +45,7 @@ }, "git": {}, "dependencies": { + "@plastichub/fs": "^0.13.26", "@zeit/schemas": "2.6.0", "ajv": "6.12.6", "arg": "2.0.0", diff --git a/src/serve.js b/src/serve.js index 2fbcd88..182332d 100644 --- a/src/serve.js +++ b/src/serve.js @@ -5,8 +5,12 @@ const http = require('http'); const https = require('https'); const path = require('path'); const fs = require('fs'); -const {promisify} = require('util'); -const {parse} = require('url'); +const { + promisify +} = require('util'); +const { + parse +} = require('url'); const os = require('os'); // Packages @@ -14,7 +18,9 @@ const Ajv = require('ajv'); const checkForUpdate = require('update-check'); const chalk = require('chalk'); const arg = require('arg'); -const {write: copy} = require('clipboardy'); +const { + write: copy +} = require('clipboardy'); const handler = require('./server-handler'); const schema = require('@zeit/schemas/deployment/config-static'); const boxen = require('boxen'); @@ -28,9 +34,9 @@ const compressionHandler = promisify(compression()); const interfaces = os.networkInterfaces(); -const warning = (message) => chalk`{yellow WARNING:} ${message}`; -const info = (message) => chalk`{magenta INFO:} ${message}`; -const error = (message) => chalk`{red ERROR:} ${message}`; +const warning = (message) => chalk `{yellow WARNING:} ${message}`; +const info = (message) => chalk `{magenta INFO:} ${message}`; +const error = (message) => chalk `{red ERROR:} ${message}`; const updateCheck = async (isDebugging) => { let update = null; @@ -53,7 +59,7 @@ const updateCheck = async (isDebugging) => { console.log(`${chalk.bgRed('UPDATE AVAILABLE')} The latest version of \`serve\` is ${update.latest}`); }; -const getHelp = () => chalk` +const getHelp = () => chalk ` {bold.cyan serve} - Static file serving and directory listing {bold USAGE} @@ -135,27 +141,27 @@ const parseEndpoint = (str) => { const url = parse(str); switch (url.protocol) { - case 'pipe:': { - // some special handling - const cutStr = str.replace(/^pipe:/, ''); + case 'pipe:': { + // some special handling + const cutStr = str.replace(/^pipe:/, ''); - if (cutStr.slice(0, 4) !== '\\\\.\\') { - throw new Error(`Invalid Windows named pipe endpoint: ${str}`); + if (cutStr.slice(0, 4) !== '\\\\.\\') { + throw new Error(`Invalid Windows named pipe endpoint: ${str}`); + } + + return [cutStr]; } + case 'unix:': + if (!url.pathname) { + throw new Error(`Invalid UNIX domain socket endpoint: ${str}`); + } - return [cutStr]; - } - case 'unix:': - if (!url.pathname) { - throw new Error(`Invalid UNIX domain socket endpoint: ${str}`); - } - - return [url.pathname]; - case 'tcp:': - url.port = url.port || '5000'; - return [parseInt(url.port, 10), url.hostname]; - default: - throw new Error(`Unknown --listen endpoint scheme (protocol): ${url.protocol}`); + return [url.pathname]; + case 'tcp:': + url.port = url.port || '5000'; + return [parseInt(url.port, 10), url.hostname]; + default: + throw new Error(`Unknown --listen endpoint scheme (protocol): ${url.protocol}`); } }; @@ -177,7 +183,11 @@ const registerShutdown = (fn) => { const getNetworkAddress = () => { for (const name of Object.keys(interfaces)) { for (const interface of interfaces[name]) { - const {address, family, internal} = interface; + const { + address, + family, + internal + } = interface; if (family === 'IPv4' && !internal) { return address; } @@ -186,7 +196,9 @@ const getNetworkAddress = () => { }; const startEndpoint = (endpoint, config, args, previous) => { - const {isTTY} = process.stdout; + const { + isTTY + } = process.stdout; const clipboard = args['--no-clipboard'] !== true; const compress = args['--no-compression'] !== true; const httpMode = args['--ssl-cert'] && args['--ssl-key'] ? 'https' : 'http'; @@ -204,13 +216,13 @@ const startEndpoint = (endpoint, config, args, previous) => { const sslPass = args['--ssl-pass']; - const server = httpMode === 'https' - ? https.createServer({ + const server = httpMode === 'https' ? + https.createServer({ key: fs.readFileSync(args['--ssl-key']), cert: fs.readFileSync(args['--ssl-cert']), passphrase: sslPass ? fs.readFileSync(sslPass) : '' - }, serverHandler) - : http.createServer(serverHandler); + }, serverHandler) : + http.createServer(serverHandler); server.on('error', (err) => { if (err.code === 'EADDRINUSE' && endpoint.length === 1 && !isNaN(endpoint[0]) && args['--no-port-switching'] !== true) { @@ -320,12 +332,12 @@ const loadConfig = async (cwd, entry, args) => { try { switch (file) { - case 'now.json': - content = content.static; - break; - case 'package.json': - content = content.now.static; - break; + case 'now.json': + content = content.static; + break; + case 'package.json': + content = content.now.static; + break; } } catch (err) { continue; @@ -342,7 +354,9 @@ const loadConfig = async (cwd, entry, args) => { } if (entry) { - const {public} = config; + const { + public + } = config; config.public = path.relative(cwd, (public ? path.resolve(entry, public) : entry)); } @@ -352,7 +366,10 @@ const loadConfig = async (cwd, entry, args) => { if (!validateSchema(config)) { const defaultMessage = error('The configuration you provided is wrong:'); - const {message, params} = validateSchema.errors[0]; + const { + message, + params + } = validateSchema.errors[0]; console.error(`${defaultMessage}\n${message}\n${JSON.stringify(params)}`); process.exit(1); @@ -419,7 +436,9 @@ const loadConfig = async (cwd, entry, args) => { if (!args['--listen']) { // Default endpoint - args['--listen'] = [[process.env.PORT || 5000]]; + args['--listen'] = [ + [process.env.PORT || 5000] + ]; } if (args._.length > 1) { @@ -433,7 +452,9 @@ const loadConfig = async (cwd, entry, args) => { const config = await loadConfig(cwd, entry, args); if (args['--single']) { - const {rewrites} = config; + const { + rewrites + } = config; const existingRewrites = Array.isArray(rewrites) ? rewrites : []; // As the first rewrite rule, make `--single` work