Skip to content

Commit

Permalink
[peterbe#112] List what timed out
Browse files Browse the repository at this point in the history
  • Loading branch information
stereobooster committed Jun 19, 2018
1 parent f5302c9 commit ad3ad9d
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
yarn-error.log
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ key is `urls`. Other optional options are:
* `puppeteerArgs` - Args sent to puppeteer when launching. [List
of strings for headless Chrome](https://peter.sh/experiments/chromium-command-line-switches/).
* `cssoOptions` - CSSO compress function [options](https://github.com/css/csso#compressast-options)
* `timeout` - Maximum navigation time in milliseconds, defaults to 30 seconds, pass 0 to disable timeout.

## Warnings

Expand Down
22 changes: 21 additions & 1 deletion src/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const csso = require('csso');
const csstree = require('css-tree');
const cheerio = require('cheerio');
const utils = require('./utils');
const { createTracker } = require('./tracker');
const url = require('url');

const isOk = response => response.ok() || response.status() === 304;
Expand Down Expand Up @@ -124,8 +125,20 @@ const processPage = ({
// a second time.
let fulfilledPromise = false;

const tracker = createTracker(page);
const safeReject = error => {
if (!fulfilledPromise) {
if (error.message.startsWith('Navigation Timeout Exceeded')) {
const urls = tracker.urls();
if (urls.length > 1) {
error.message += `\nFor one of the following urls: ${urls.join(
','
)}`;
} else if (urls.length > 0) {
error.message += `\nFor ${urls[0]}`;
}
}
tracker.dispose();
reject(error);
}
};
Expand All @@ -146,6 +159,10 @@ const processPage = ({
await page.setViewport(options.viewport);
}

if (options.timeout !== undefined) {
page.setDefaultNavigationTimeout(options.timeout);
}

// A must or else you can't do console.log from within page.evaluate()
page.on('console', msg => {
if (debug) {
Expand Down Expand Up @@ -311,7 +328,10 @@ const processPage = ({
allHrefs.push(href);
});

if (!fulfilledPromise) resolve();
if (!fulfilledPromise) {
tracker.dispose();
resolve();
}
} catch (e) {
return safeReject(e);
}
Expand Down
18 changes: 18 additions & 0 deletions src/tracker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const createTracker = page => {
const requests = new Set();
const onStarted = request => requests.add(request);
const onFinished = request => requests.delete(request);
page.on('request', onStarted);
page.on('requestfinished', onFinished);
page.on('requestfailed', onFinished);
return {
urls: () => Array.from(requests).map(r => r.url()),
dispose: () => {
page.removeListener('request', onStarted);
page.removeListener('requestfinished', onFinished);
page.removeListener('requestfailed', onFinished);
}
};
};

module.exports = { createTracker };
14 changes: 14 additions & 0 deletions tests/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ fastify.get('/307.html', (req, reply) => {
reply.redirect(307, '/redirected.html');
});

fastify.get('/timeout.html', (req, reply) => {
setTimeout(() => reply.send('timeout'), 3000);
});

let browser;

const runMinimalcss = (path, options = {}) => {
Expand Down Expand Up @@ -216,3 +220,13 @@ test('accept CSSO options', async () => {
({ finalCss } = await runMinimalcss('comments', { cssoOptions }));
expect(finalCss).not.toMatch('test css comment');
});

test('timeout', async () => {
expect.assertions(2);
try {
await runMinimalcss('timeout', { timeout: 2000 });
} catch (e) {
expect(e.message).toMatch('Navigation Timeout Exceeded: 2000ms exceeded');
expect(e.message).toMatch('For http://localhost:3000/timeout.html');
}
});

0 comments on commit ad3ad9d

Please sign in to comment.