-
-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathindex.js
218 lines (172 loc) · 6.92 KB
/
index.js
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* phantomas CommonJS module
*/
"use strict";
const Browser = require("./browser"),
EventEmitter = require("./AwaitEventEmitter"),
debug = require("debug")("phantomas:core"),
loader = require("./loader"),
puppeteer = require("puppeteer"),
path = require("path"),
Results = require("../core/results"),
VERSION = require("./../package").version;
/**
* Main CommonJS module entry point
*
* @param {string} url
* @param {Object} opts
* @returns {browser}
*/
function phantomas(url, opts) {
var events = new EventEmitter(),
browser,
options;
debug("OS: %s %s", process.platform, process.arch);
debug("Node.js: %s", process.version);
debug("phantomas: %s", VERSION);
debug(
"Puppeteer: preferred revision r%s",
puppeteer.default._preferredRevision
);
debug("URL: <%s>", url);
// options handling
options = Object.assign({}, opts || {}); // avoid #563
options.url = options.url || url || false;
debug("Options: %s", JSON.stringify(options));
events.setMaxListeners(250); // MaxListenersExceededWarning: Possible EventEmitter memory leak detected.
var results = new Results();
results.setUrl(url);
results.setGenerator("phantomas v" + VERSION);
// set up and run Puppeteer
browser = new Browser();
browser.bind(events);
var promise = new Promise(async (resolve, reject) => {
try {
if (typeof options.url !== "string") {
return reject(Error("URL must be a string"));
}
const page = await browser.init(options),
debugScope = require("debug")("phantomas:scope:log");
// prepare a small instance object that will be passed to modules and extensions on init
const scope = {
getParam: (param, _default) => {
return options[param] || _default;
},
getVersion: () => VERSION,
emit: events.emit.bind(events),
on: events.on.bind(events),
once: events.once.bind(events),
log: debugScope.bind(debug),
addOffender: results.addOffender.bind(results),
incrMetric: results.incrMetric.bind(results),
setMetric: results.setMetric,
addToAvgMetric: results.addToAvgMetric,
getMetric: results.getMetric,
// @see https://github.com/GoogleChrome/puppeteer/blob/v1.11.0/docs/api.md#pageevaluatepagefunction-args
evaluate: page.evaluate.bind(page),
// @see https://github.com/GoogleChrome/puppeteer/blob/v1.11.0/docs/api.md#pageselector-1
querySelectorAll: async function querySelectorAll(
selector
) /* istanbul ignore next */ {
debug('querySelectorAll("%s")', selector);
return page.$$(selector);
},
// @see https://github.com/GoogleChrome/puppeteer/blob/v1.11.0/docs/api.md#pageevaluateonnewdocumentpagefunction-args
injectJs: async (script) => {
const debug = require("debug")("phantomas:injectJs");
// Make sure we're on an HTML document, not an XML document for example
const prefix = "if (document.constructor.name === 'HTMLDocument') {",
suffix = "}";
const preloadFile =
prefix + require("fs").readFileSync(script, "utf8") + suffix;
await page.evaluateOnNewDocument(preloadFile);
debug(script + " JavaScript file has been injected into page scope");
},
};
// pass phantomas options to page scope
// https://github.com/GoogleChrome/puppeteer/blob/v1.11.0/docs/api.md#pageevaluateonnewdocumentpagefunction-args
/* istanbul ignore next */
await page.evaluateOnNewDocument((options) => {
window.__phantomas_options = options;
}, options);
// expose the function that will pass events from page scope code into Node.js layer
// @see https://github.com/GoogleChrome/puppeteer/blob/v1.11.0/docs/api.md#pageexposefunctionname-puppeteerfunction
await page.exposeFunction("__phantomas_emit", scope.emit);
// Inject helper code into the browser's scope
events.on("init", () => {
scope.injectJs(__dirname + "/../core/scope.js");
});
// bind to sendMsg calls from page scope code
events.on("scopeMessage", (type, args) => {
const debug = require("debug")("phantomas:core:scopeEvents");
// debug(type + ' [' + args + ']');
switch (type) {
case "addOffender":
case "incrMetric":
case "log":
case "setMetric":
scope[type].apply(scope, args);
break;
/* istanbul ignore next */
default:
debug("Unrecognized event type: " + type);
}
});
// bind to a first response
// and reject a promise if the first response is 4xx / 5xx HTTP error
var firstResponseReceived = false;
events.once("recv", async (entry) => {
if (!firstResponseReceived && entry.status >= 400) {
debug(
"<%s> response code is HTTP %d %s",
entry.url,
entry.status,
entry.statusText
);
// close the browser before leaving here, otherwise subsequent instances will have problems
await browser.close();
reject(
new Error(
"HTTP response code from <" + entry.url + "> is " + entry.status
)
);
}
firstResponseReceived = true;
});
// load modules and extensions
debug("Loading core modules...");
loader.loadCoreModules(scope);
debug("Loading extensions...");
loader.loadExtensions(scope);
debug("Loading modules...");
loader.loadModules(scope);
await events.emit("init", page, browser.getPuppeteerBrowser()); // @desc Browser's scope and modules are set up, the page is about to be loaded
// https://github.com/GoogleChrome/puppeteer/blob/v1.11.0/docs/api.md#pagegotourl-options
const waitUntil = options["wait-for-network-idle"]
? "networkidle0"
: undefined,
timeout = options.timeout;
await browser.visit(url, waitUntil, timeout);
// resolve our run
// https://github.com/GoogleChrome/puppeteer/blob/v1.11.0/docs/api.md#browserclose
await events.emit("beforeClose", page); // @desc Called before the Chromium (and all of its pages) is closed
await browser.close();
// your last chance to add metrics
await events.emit("report"); // @desc Called just before the phantomas results are returned to the caller
resolve(results);
} catch (ex) {
debug("Exception caught: " + ex);
debug(ex);
// close the browser before leaving here, otherwise subsequent instances will have problems
await browser.close();
reject(ex);
}
});
promise.on = events.on.bind(events);
promise.once = events.once.bind(events);
return promise;
}
phantomas.metadata = require(__dirname + "/metadata/metadata.json");
phantomas.path = path.normalize(__dirname + "/..");
phantomas.version = VERSION;
module.exports = phantomas;