Skip to content

Commit ccfe06e

Browse files
committed
Add keepAlive and browser options
1 parent 74864f4 commit ccfe06e

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module.exports = async (url, opts) => {
3434

3535
const uri = isUrl(url) ? url : fileUrl(url);
3636
const {
37-
cookies, crop, format, headers, height, hide, password, scale,
37+
cookies, crop, format, headers, height, hide, keepAlive, password, scale,
3838
script, selector, timeout, transparent, userAgent, username, width
3939
} = opts;
4040

@@ -52,7 +52,7 @@ module.exports = async (url, opts) => {
5252
opts.omitBackground = true;
5353
}
5454

55-
const browser = await puppeteer.launch();
55+
const browser = opts.browser || await puppeteer.launch();
5656
const page = await browser.newPage();
5757
const viewport = {
5858
height,
@@ -96,7 +96,11 @@ module.exports = async (url, opts) => {
9696
}
9797

9898
const buf = await page.screenshot(opts);
99-
await browser.close();
99+
await page.close();
100+
101+
if (keepAlive !== true) {
102+
await browser.close();
103+
}
100104

101105
return buf;
102106
};

test.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,24 @@ import isJpg from 'is-jpg';
55
import isPng from 'is-png';
66
import pify from 'pify';
77
import PNG from 'png-js';
8+
import puppeteer from 'puppeteer';
89
import server from './fixtures/server';
9-
import m from '.';
10+
import screenshotStream from '.';
11+
12+
let browser;
13+
let m;
14+
15+
test.before(async () => {
16+
browser = await puppeteer.launch();
17+
m = (url, opts) => screenshotStream(url, Object.assign({}, opts, {
18+
browser,
19+
keepAlive: true
20+
}));
21+
});
22+
23+
test.after(async () => {
24+
await browser.close();
25+
});
1026

1127
test('generate screenshot', async t => {
1228
t.true(isPng(await m('http://yeoman.io', {

0 commit comments

Comments
 (0)