From ea64dbed9dbc7273d50a18e1db18fb8c038fb990 Mon Sep 17 00:00:00 2001 From: simonhaenisch Date: Mon, 10 Sep 2018 23:23:51 +1200 Subject: [PATCH] replace serve with serve-handler --- package.json | 2 +- test.js | 21 --------------------- util/wait-for-localhost.js | 28 ---------------------------- util/write-pdf.js | 26 ++++++++++++++++++++------ 4 files changed, 21 insertions(+), 56 deletions(-) delete mode 100644 util/wait-for-localhost.js diff --git a/package.json b/package.json index dfc90a5..96801be 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ "iconv-lite": "0.4.24", "marked": "0.5.0", "puppeteer": "1.8.0", - "serve": "6.5.8" + "serve-handler": "5.0.2" }, "devDependencies": { "ava": "0.25.0", diff --git a/test.js b/test.js index fc21e62..0d7eaee 100644 --- a/test.js +++ b/test.js @@ -1,14 +1,11 @@ const path = require('path'); const test = require('ava'); -const serve = require('serve'); -const getPort = require('get-port'); const config = require('./util/config'); const getHtml = require('./util/get-html'); const getMarkedWithHighlighter = require('./util/get-marked-with-highlighter'); const getPdfFilePath = require('./util/get-pdf-file-path'); const { getDir, getMarginObject } = require('./util/helpers'); const readFile = require('./util/read-file'); -const waitForLocalhost = require('./util/wait-for-localhost'); // -- // get-html @@ -79,21 +76,3 @@ test('readFile should return the content of a file', t => { t.is(gitignoreContent, readFile('.gitignore')); t.is(gitignoreContent, readFile('.gitignore', 'windows1252')); }); - -// -- -// wait-for-localhost -test('waitForLocalhost should resolve once the server is available', async t => { - t.plan(2); - - const port = await getPort(); - - t.true(typeof port === 'number'); - - const server = serve(__dirname, { port, local: true, clipless: true, silent: true }); - - await waitForLocalhost(port); - - t.pass(); - - server.stop(); -}); diff --git a/util/wait-for-localhost.js b/util/wait-for-localhost.js deleted file mode 100644 index 3c350ce..0000000 --- a/util/wait-for-localhost.js +++ /dev/null @@ -1,28 +0,0 @@ -const http = require('http'); - -/** - * Wait for localhost to be available. - * - * @see https://github.com/sindresorhus/wait-for-localhost/blob/master/cli.js - * - * @param {number} port Port that the desired server runs on - * @param {number} [interval] Polling interval - * - * @returns promise that resolves once localhost: is available - */ -module.exports = (port, interval = 100) => - new Promise(resolve => { - const retry = () => setTimeout(main, interval); - - function main() { - const request = http.request( - { method: 'HEAD', port }, - response => (response.statusCode === 200 ? resolve() : retry()), - ); - - request.on('error', retry); - request.end(); - } - - main(); - }); diff --git a/util/write-pdf.js b/util/write-pdf.js index 6d0aa54..6c406fc 100644 --- a/util/write-pdf.js +++ b/util/write-pdf.js @@ -1,10 +1,26 @@ +const { createServer } = require('http'); +const serveHandler = require('serve-handler'); const puppeteer = require('puppeteer'); -const serve = require('serve'); const getPort = require('get-port'); const getPdfFilePath = require('./get-pdf-file-path'); -const waitForLocalhost = require('./wait-for-localhost'); const { getDir } = require('./helpers'); +/** + * Serve a directory using a HTTP server and the Serve handler. + * + * @param {string} path the directory to be served + * @param {number} port the port to run the server on + * + * @returns a promise that resolves with the server instance once the server is + * listening + */ +const serveDirectory = (path, port) => + new Promise(resolve => { + const server = createServer((req, res) => serveHandler(req, res, { public: path })); + + server.listen(port, () => resolve(server)); + }); + /** * Create a PDF and write it to disk. * @@ -25,9 +41,7 @@ module.exports = async (mdFilePath, outputPath, html, config) => { const assetsBasePath = getDir(mdFilePath); const port = await getPort(); - const server = serve(assetsBasePath, { port, local: true, clipless: true, silent: true }); - - await waitForLocalhost(port); + const server = await serveDirectory(assetsBasePath, port); const browser = await puppeteer.launch({ devtools: config.devtools }); @@ -62,7 +76,7 @@ module.exports = async (mdFilePath, outputPath, html, config) => { } browser.close(); - server.stop(); + server.close(); return config.devtools ? {} : { filename: pdfFilePath }; };