-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathindex.js
More file actions
88 lines (68 loc) · 2.05 KB
/
Copy pathindex.js
File metadata and controls
88 lines (68 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
'use strict';
/**
* Serializable value
*
* @typedef {number|boolean|string|object|array} Serializable
*
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Description|Promise}
*/
/**
* A proxy of the return value in the future
*
* @typedef {Promise} Promise
*
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise|Promise}
*/
/**
* Event emitter
*
* @typedef {EventEmitter} EventEmitter
*
* @see {@link https://nodejs.org/dist/latest-v6.x/docs/api/events.html#events_class_eventemitter|Node.JS - Event module}
*/
/**
* Binary buffer
*
* @typedef {Buffer} Buffer
*
* @see {@link https://nodejs.org/dist/latest-v6.x/docs/api/buffer.html#buffer_buffer|Node.JS - Buffer}
*/
const Browser = require('./Browser'), stack = [ ];
/**
* Puppeteer API
*
* @class
*/
class Puppeteer {
/**
* Launch a browser instance with given arguments.
* The browser will be closed when the parent NodeJS process is closed.
*
* @param {object} [options] Set of configurable options to set on the browser
* @param {boolean} [options.headless = true] Whether to run browser in headless mode
*
* @return {Promise<Browser>}
*/
static async launch({headless = true} = { }) {
return stack[stack.push(new Browser( headless )) - 1];
}
/**
* @return {string} A path where Puppeteer expects to find Internet Explorer
*/
static executablePath() {
const page = stack[0] && stack[0]._page[0];
return page && (page._target.FullName + '');
}
/**
* @param {?Error} error
*/
static async exit(error) {
await Promise.all( stack.map(browser => browser.close()) );
if (!(error instanceof Error)) process.exit(0);
console.error( error );
process.exit(1);
}
}
for (let event of ['uncaughtException', 'unhandledRejection', 'SIGINT', 'exit'])
process.on(event, Puppeteer.exit);
module.exports = Puppeteer;