Skip to content

Commit

Permalink
Require Node.js 10 and upgrade to Puppeteer 3
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed May 10, 2020
1 parent bfdc771 commit b397dbe
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
os: osx
language: node_js
node_js:
- '12'

This comment has been minimized.

Copy link
@fisker

fisker Jul 3, 2020

Contributor

Mean to test v14?

This comment has been minimized.

Copy link
@sindresorhus

sindresorhus Jul 3, 2020

Author Owner
- '12'
- '10'
- '8'
13 changes: 6 additions & 7 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/// <reference lib="dom"/>
/// <reference types="puppeteer"/>
import {SetCookie, LaunchOptions, Page, Browser} from 'puppeteer';

declare namespace captureWebsite {
Expand Down Expand Up @@ -147,7 +146,7 @@ declare namespace captureWebsite {
/**
Scroll to the DOM element matching the given [CSS selector](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors).
*/
readonly scrollToElement?: string | captureWebsite.ScrollToElementOptions
readonly scrollToElement?: string | ScrollToElementOptions;

/**
Disable CSS [animations](https://developer.mozilla.org/en-US/docs/Web/CSS/animation) and [transitions](https://developer.mozilla.org/en-US/docs/Web/CSS/transition).
Expand Down Expand Up @@ -201,7 +200,7 @@ declare namespace captureWebsite {
Tip: Go to the website you want a cookie for and [copy-paste it from DevTools](https://stackoverflow.com/a/24961735/64949).
*/
readonly cookies?: (string | SetCookie)[];
readonly cookies?: Array<string | SetCookie>;

/**
Credentials for [HTTP authentication](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication).
Expand Down Expand Up @@ -277,27 +276,27 @@ declare const captureWebsite: {
})();
```
*/
file(
file: (
input: string,
outputFilePath: string,
options?: captureWebsite.FileOptions
): Promise<void>;
) => Promise<void>;

/**
Capture a screenshot of the given `input`.
@param input - The URL, file URL, data URL, local file path to the website, or HTML.
@returns The screenshot as binary.
*/
buffer(input: string, options?: captureWebsite.Options): Promise<Buffer>;
buffer: (input: string, options?: captureWebsite.Options) => Promise<Buffer>;

/**
Capture a screenshot of the given `input`.
@param input - The URL, file URL, data URL, local file path to the website, or HTML.
@returns The screenshot as [Base64](https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding).
*/
base64(input: string, options?: captureWebsite.Options): Promise<string>;
base64: (input: string, options?: captureWebsite.Options) => Promise<string>;
};

export = captureWebsite;
20 changes: 10 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const {promisify} = require('util');
const fs = require('fs');
const fileUrl = require('file-url');
const puppeteer = require('puppeteer');
const devices = require('puppeteer/DeviceDescriptors');
const {devicesMap} = require('puppeteer/DeviceDescriptors');
const toughCookie = require('tough-cookie');

const writeFile = promisify(fs.writeFile);
Expand Down Expand Up @@ -104,20 +104,20 @@ const parseCookie = (url, cookie) => {

const jar = new toughCookie.CookieJar(undefined, {rejectPublicSuffixes: false});
jar.setCookieSync(cookie, url);
const ret = jar.serializeSync().cookies[0];
const returnValue = jar.serializeSync().cookies[0];

// Use this instead of the above when the following issue is fixed:
// https://github.com/salesforce/tough-cookie/issues/149
// const ret = toughCookie.parse(cookie).serializeSync();

ret.name = ret.key;
delete ret.key;
returnValue.name = returnValue.key;
delete returnValue.key;

if (ret.expires) {
ret.expires = Math.floor(new Date(ret.expires) / 1000);
if (returnValue.expires) {
returnValue.expires = Math.floor(new Date(returnValue.expires) / 1000);
}

return ret;
return returnValue;
};

const captureWebsite = async (input, options) => {
Expand Down Expand Up @@ -216,11 +216,11 @@ const captureWebsite = async (input, options) => {
await page.setViewport(viewportOptions);

if (options.emulateDevice) {
if (!(options.emulateDevice in devices)) {
if (!(options.emulateDevice in devicesMap)) {
throw new Error(`The device name \`${options.emulateDevice}\` is not supported`);
}

await page.emulate(devices[options.emulateDevice]);
await page.emulate(devicesMap[options.emulateDevice]);
}

await page.emulateMediaFeatures([{
Expand Down Expand Up @@ -340,7 +340,7 @@ module.exports.buffer = async (url, options) => captureWebsite(url, {...options,

module.exports.base64 = async (url, options) => captureWebsite(url, {...options, encoding: 'base64'});

module.exports.devices = Object.values(devices).map(device => device.name);
module.exports.devices = Object.values(devicesMap).map(device => device.name);

if (process.env.NODE_ENV === 'test') {
module.exports._startBrowser = puppeteer.launch.bind(puppeteer);
Expand Down
26 changes: 16 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
"description": "Capture screenshots of websites",
"license": "MIT",
"repository": "sindresorhus/capture-website",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "[email protected]",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"engines": {
"node": ">=8"
"node": ">=10"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -36,21 +37,26 @@
],
"dependencies": {
"file-url": "^3.0.0",
"puppeteer": "^2.0.0",
"tough-cookie": "^3.0.1"
"puppeteer": "^3.0.4",
"tough-cookie": "^4.0.0"
},
"devDependencies": {
"@types/puppeteer": "^1.20.2",
"@types/puppeteer": "^2.0.1",
"ava": "^2.0.0",
"create-test-server": "^3.0.1",
"delay": "^4.1.0",
"image-size": "^0.8.3",
"is-jpg": "^2.0.0",
"is-png": "^2.0.0",
"pify": "^4.0.1",
"png-js": "^0.1.1",
"tempy": "^0.3.0",
"tsd": "^0.10.0",
"xo": "^0.25.3"
"pify": "^5.0.0",
"png-js": "^1.0.0",
"tempy": "^0.5.0",
"tsd": "^0.11.0",
"xo": "^0.30.0"
},
"xo": {
"rules": {
"@typescript-eslint/prefer-readonly-parameter-types": "off"
}
}
}
4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import isPng from 'is-png';
import pify from 'pify';
import PNG from 'png-js';
import createTestServer from 'create-test-server';
import devices from 'puppeteer/DeviceDescriptors';
import {devicesMap} from 'puppeteer/DeviceDescriptors';
import tempy from 'tempy';
import delay from 'delay';
import toughCookie from 'tough-cookie';
Expand Down Expand Up @@ -133,7 +133,7 @@ test('`scaleFactor` option', async t => {
});

test('`emulateDevice` option', async t => {
const device = devices['iPhone X'];
const device = devicesMap['iPhone X'];

const size = imageSize(await instance(server.url, {
emulateDevice: device.name
Expand Down

0 comments on commit b397dbe

Please sign in to comment.