Skip to content

Commit

Permalink
replace serve with serve-handler
Browse files Browse the repository at this point in the history
  • Loading branch information
simonhaenisch committed Sep 10, 2018
1 parent c6b2b83 commit ea64dbe
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 56 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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",
Expand Down
21 changes: 0 additions & 21 deletions 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
Expand Down Expand Up @@ -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();
});
28 changes: 0 additions & 28 deletions util/wait-for-localhost.js

This file was deleted.

26 changes: 20 additions & 6 deletions 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.
*
Expand All @@ -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 });

Expand Down Expand Up @@ -62,7 +76,7 @@ module.exports = async (mdFilePath, outputPath, html, config) => {
}

browser.close();
server.stop();
server.close();

return config.devtools ? {} : { filename: pdfFilePath };
};

0 comments on commit ea64dbe

Please sign in to comment.