Skip to content

Commit 7da6d67

Browse files
committed
Update docs
1 parent 8b53f23 commit 7da6d67

File tree

5 files changed

+214
-63
lines changed

5 files changed

+214
-63
lines changed

README.md

Lines changed: 113 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- High level automation API on top [Headless Chrome](https://github.com/GoogleChrome/puppeteer).
1717
- Oriented for production & performance scenarios.
1818
- Aborting unnecessary requests based on MIME types.
19+
- [Pooling](#pool) support to keep multiple browsers ready.
1920
- Blocking [ads trackers](https://npm.im/is-tracking-domain) by default.
2021

2122
## Install
@@ -76,8 +77,33 @@ const browserless = require('browserless')({
7677
})
7778
```
7879

80+
#### options
81+
82+
See [puppeteer.launch#options](
83+
https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerlaunchoptions).
84+
7985
By default the library will be pass a well known list of flags, so probably you don't need any additional setup.
8086

87+
### .pool(options)
88+
89+
Tha main **browserless** constructor expose a singleton browser. This is enough for most scenarios, but in case you need you can intialize a **pool of instances**.
90+
91+
```js
92+
const createBrowserless = require('browserless')
93+
const browserless = createBrowserless.pool()
94+
```
95+
96+
#### options
97+
98+
See [puppeteer.launch#options](
99+
https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerlaunchoptions).
100+
101+
It follows the same API than [constructor](constructoroptions) but accept a configurable parameter called `poolOpts` for setup specific pool options
102+
103+
##### poolOpts
104+
105+
See [generic-pool#options](https://github.com/coopernurse/node-pool#opts).
106+
81107
### .html(url, options)
82108

83109
It returns the full HTML content from the target `url`.
@@ -92,33 +118,35 @@ const browserless = require('browserless')
92118
})()
93119
```
94120

95-
This method accepts `options` that will be pased to [page.goto](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagegotourl-options).
121+
#### options
122+
123+
See [page.goto](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagegotourl-options).
96124

97125
Additionally, you can setup:
98126

99-
#### waitFor
127+
##### waitFor
100128

101-
type:`string|function|number`</br>
129+
type:`string`|`function`|`number`</br>
102130
default: `0`
103131

104132
Wait a quantity of time, selector or function using [page.waitFor](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagewaitforselectororfunctionortimeout-options-args).
105133

106-
#### waitUntil
134+
##### waitUntil
107135

108136
type:`array`</br>
109137
default: `['networkidle2', 'load', 'domcontentloaded']`
110138

111139
Specify a list of events until consider navigation succeeded, using [page.waitForNavigation](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagewaitfornavigationoptions).
112140

113-
#### userAgent
141+
##### userAgent
114142

115143
It will setup a custom user agent, using [page.setUserAgent](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagesetuseragentuseragent) method.
116144

117-
#### viewport
145+
##### viewport
118146

119147
It will setup a custom viewport, using [page.setViewport](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagesetviewportviewport) method.
120148

121-
#### abortTypes
149+
##### abortTypes
122150

123151
type: `array` </br>
124152
default: `['image', 'media', 'stylesheet', 'font', 'xhr']`
@@ -146,7 +174,9 @@ const browserless = require('browserless')
146174
})()
147175
```
148176

149-
All `options` that you can pass are the same than [`.html`](#html) method.
177+
#### options
178+
179+
They are the same than [`.html`](#html) method.
150180

151181
### .pdf(url, options)
152182

@@ -171,31 +201,35 @@ const browserless = require('browserless')
171201

172202
It returns an [tmpStream](https://github.com/Kikobeats/create-temp-file2#create-temp-file2), with `path` where the temporal file live and `cleanup`/`cleanupSync` methods for clean the temporal file.
173203

174-
The `options` provided are passed to [page.pdf](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagepdfoptions).
204+
#### options
175205

176-
If you want to customize tmpStream settings, pass [opts.tmpOptions](https://github.com/Kikobeats/create-temp-file2#createtempfileoptions).
206+
See [page.pdf](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagepdfoptions).
177207

178208
Additionally, you can setup:
179209

180-
#### media
210+
##### tmpOptions
211+
212+
See [createTempFile#options](https://github.com/Kikobeats/create-temp-file2#createtempfileoptions).
213+
214+
##### media
181215

182216
Changes the CSS media type of the page using [page.emulateMedia](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pageemulatemediamediatype).
183217

184-
#### device
218+
##### device
185219

186-
It generate the PDF using the [device](https://github.com/GoogleChrome/puppeteer/blob/master/DeviceDescriptors.js) descriptor name settings, like `userAgent` and `viewport`.
220+
It generate the PDF using the [device](#devices) descriptor name settings, like `userAgent` and `viewport`.
187221

188-
#### userAgent
222+
##### userAgent
189223

190224
It will setup a custom user agent, using [page.setUserAgent](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagesetuseragentuseragent) method.
191225

192-
#### viewport
226+
##### viewport
193227

194228
It will setup a custom viewport, using [page.setViewport](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagesetviewportviewport) method.
195229

196230
### .screenshot(url, options)
197231

198-
It takes a screenshot from the target `url`
232+
It takes a screenshot from the target `url`.
199233

200234
```js
201235
const browserless = require('browserless')
@@ -216,24 +250,61 @@ const browserless = require('browserless')
216250

217251
It returns an [tmpStream](https://github.com/Kikobeats/create-temp-file2#create-temp-file2), with `path` where the temporal file live and `cleanup`/`cleanupSync` methods for clean the temporal file.
218252

219-
The `options` provided are passed to [page.pdf](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagepdfoptions).
253+
#### options
220254

221-
If you want to customize tmpStream settings, pass [opts.tmpOptions](https://github.com/Kikobeats/create-temp-file2#createtempfileoptions).
255+
See [page.screenshot](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagescreenshotoptions).
256+
257+
Additionally, you can setup:
258+
259+
##### tmpOptions
260+
261+
See [createTempFile#options](https://github.com/Kikobeats/create-temp-file2#createtempfileoptions).
262+
263+
The `options` provided are passed to [page.pdf](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagepdfoptions).
222264

223265
Additionally, you can setup:
224266

225-
#### device
267+
##### device
226268

227-
It generate the PDF using the [device](https://github.com/GoogleChrome/puppeteer/blob/master/DeviceDescriptors.js) descriptor name settings, like `userAgent` and `viewport`.
269+
It generate the PDF using the [device](#devices) descriptor name settings, like `userAgent` and `viewport`.
228270

229-
#### userAgent
271+
##### userAgent
230272

231273
It will setup a custom user agent, using [page.setUserAgent](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagesetuseragentuseragent) method.
232274

233-
#### viewport
275+
##### viewport
234276

235277
It will setup a custom viewport, using [page.setViewport](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagesetviewportviewport) method.
236278

279+
### .devices
280+
281+
List of all available devices preconfigured with `deviceName`, `viewport` and `userAgent` settings.
282+
283+
These devices are used for emulation purposes.
284+
285+
#### .getDevice(deviceName)
286+
287+
Get an specific device descriptor settings by descriptor name.
288+
289+
```js
290+
const browserless = require('browserless')
291+
292+
browserless.getDevice('Macbook Pro 15')
293+
294+
// {
295+
// name: 'Macbook Pro 15',
296+
// userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X …',
297+
// viewport: {
298+
// width: 1440,
299+
// height: 900,
300+
// deviceScaleFactor: 1,
301+
// isMobile: false,
302+
// hasTouch: false,
303+
// isLandscape: false
304+
// }
305+
// }
306+
```
307+
237308
## Advanced
238309

239310
The following methods are exposed to be used in scenarios where you need more granuality control and less magic.
@@ -250,7 +321,7 @@ const browserless = require('browserless')
250321
})()
251322
```
252323

253-
### .evaluate
324+
### .evaluate(page, response)
254325

255326
It exposes an interface for creating your own evaluate function.
256327

@@ -278,18 +349,6 @@ const getUrlInfo = browserless.evaluate((page, response) => ({
278349

279350
Internally the method performs a [.goto](#gotopage-options) operation and it will pass you the `page` and `reponse`.
280351

281-
### .page()
282-
283-
It returns a standalone [browser new page](https://github.com/GoogleChrome/puppeteer/blob/ddc59b247282774ccc53e3cc925efc30d4e25675/docs/api.md#browsernewpage).
284-
285-
```js
286-
const browserless = require('browserless')
287-
288-
;(async () => {
289-
const page = await browserless.page()
290-
})()
291-
```
292-
293352
### .goto(page, options)
294353

295354
It performs a smart [page.goto](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagegotourl-options), blocking [ads trackers](https://npm.im/is-tracking-domain)) requests and other requests based on `resourceType`.
@@ -363,6 +422,23 @@ type: `object`
363422

364423
The settings to be passed to [page.goto](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagegotourl-options).
365424

425+
### .page()
426+
427+
It returns a standalone [browser new page](https://github.com/GoogleChrome/puppeteer/blob/ddc59b247282774ccc53e3cc925efc30d4e25675/docs/api.md#browsernewpage).
428+
429+
```js
430+
const browserless = require('browserless')
431+
432+
;(async () => {
433+
const page = await browserless.page()
434+
})()
435+
```
436+
437+
## Benchmark
438+
439+
![](/static/bench.png)
440+
441+
We included a tiny [benchmark](https://github.com/Kikobeats/browserless/tree/master/bench) utility for make easier testing multiple configuration settings.
366442

367443
## FAQ
368444

@@ -380,10 +456,10 @@ In order to speed up the process, we block ads scripts by default because they a
380456

381457
Probably **browserless** was too smart and it blocked a request that you need.
382458

383-
You can active debug mode using `DEBUG=browserless` environment variable in order to see what is happening behind the code:
459+
You can active debug mode using `DEBUG=browserless*` environment variable in order to see what is happening behind the code:
384460

385461
```
386-
DEBUG=browserless node index.js
462+
DEBUG=browserless* node index.js
387463
```
388464

389465
Consider open an [issue](https://github.com/Kikobeats/browserless/issues/new) with the debug trace.

bench/index.js

Lines changed: 94 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,102 @@
1-
const browserless = require('..')()
2-
const percentile = require('percentile')
1+
'use strict'
2+
3+
const createBrowserless = require('..')
4+
const asciichart = require('asciichart')
35
const prettyMs = require('pretty-ms')
6+
const prettyObj = require('fmt-obj')
7+
const Measured = require('measured')
8+
const pSeries = require('p-series')
9+
const meow = require('meow')
10+
11+
const cli = meow(
12+
`
13+
browserless benchmark utility
14+
$ bench <url> [flags] [options]
15+
16+
Flags
17+
--method Choose what method to run (html, text, pdf, screenshot).
18+
--pool Enable pool mode.
19+
--iterations Number of iterations.
20+
--pool-min Mininum of instances in pool mode.
21+
--pool-max Maximum of instances in pool mode.
422
5-
const benchmark = async n => {
6-
console.log(`benchmark ${n} iterations\n`)
7-
let results = []
8-
9-
for (let i = 0; i < n; i++) {
10-
const start = Date.now()
11-
await browserless.screenshot('https://kikobeats.com')
12-
const end = Date.now()
13-
const time = end - start
14-
console.log(`browser-${i} ${prettyMs(time)}`)
15-
results.push(time)
23+
Options
24+
The rest of parameters provided are passed as options.
25+
`,
26+
{
27+
description: false,
28+
flags: {
29+
method: {
30+
type: 'string'
31+
},
32+
pool: {
33+
type: 'boolean',
34+
default: false
35+
},
36+
iterations: {
37+
type: 'number',
38+
default: 10
39+
}
40+
}
1641
}
42+
)
1743

18-
return results
44+
const benchmark = async ({ browserless, method, url, opts, iterations }) => {
45+
const timer = new Measured.Timer()
46+
const promises = [...Array(iterations).keys()].map(n => {
47+
return async () => {
48+
const stopwatch = timer.start()
49+
await browserless[method](url, opts)
50+
const time = stopwatch.end()
51+
console.log(
52+
prettyObj({ iteration: n, time: prettyMs(time), rawTime: time })
53+
)
54+
return time
55+
}
56+
})
57+
58+
const times = await pSeries(promises)
59+
const histogram = timer.toJSON().histogram
60+
return { times, histogram }
1961
}
62+
;(async () => {
63+
const [url] = cli.input
64+
if (!url) throw new TypeError('Need to provide an URL as target.')
65+
66+
const {
67+
method,
68+
pool: isPool,
69+
poolMin,
70+
poolMax,
71+
iterations,
72+
...opts
73+
} = cli.flags
2074

21-
const n = process.argv[2] || 10
75+
if (!method) throw new TypeError('Need to provide a method to run.')
2276

23-
benchmark(n)
24-
.then(results => {
25-
const p95 = percentile(95, results)
26-
const avg = results.reduce((acc, item) => acc + item, 0)
27-
console.log('\n---\n')
28-
console.log('avg\t', prettyMs(avg / results.length))
29-
console.log('p95\t', prettyMs(p95))
30-
process.exit(0)
77+
const browserless = isPool
78+
? createBrowserless.pool({ min: poolMin, max: poolMax, ...opts })
79+
: createBrowserless(opts)
80+
81+
console.log(prettyObj(cli.flags))
82+
const { times, histogram } = await benchmark({
83+
browserless,
84+
iterations,
85+
method,
86+
url,
87+
opts
3188
})
32-
.catch(console.error)
89+
90+
const stats = Object.keys(histogram).reduce((acc, key) => {
91+
const value = histogram[key]
92+
return { ...acc, [key]: prettyMs(value) }
93+
}, {})
94+
95+
const graph = asciichart.plot(times, { height: 6 })
96+
97+
console.log()
98+
console.log(graph)
99+
console.log(prettyObj(stats))
100+
101+
process.exit()
102+
})()

0 commit comments

Comments
 (0)