-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
229 lines (217 loc) · 6.67 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
219
220
221
222
223
224
225
226
227
228
229
const puppeteer = require("puppeteer");
const devices = require("puppeteer/DeviceDescriptors");
const _ = require("lodash");
const Promise = require("bluebird");
/**
* BrowserScreenshot Highlevel library to export
* screenshots from website using headless chrome
*
* This library internally uses puppeteer
* @type {[type]}
*/
module.exports = class BrowserScreenshot {
/**
* constructor initialization options
* [see] (https://github.com/GoogleChrome/puppeteer/blob/3ae85e4649d4406a02882c543dc44dcf462a921f/docs/api.md#puppeteerlaunchoptions)
* @param {object} options Takes same set of parameters as puppeteer launch options.
*/
constructor(options) {
this.options = options || {};
this.options.args = _.concat(this.options.args || [], [
"--no-sandbox",
"--disable-setuid-sandbox"
]);
this.browser = null;
this.pages = [];
this.usingPages = new Map();
}
getPage(opts) {
let pageId = _.uniqueId("page_");
let device = _.get(opts, "device", null);
let foundDevice = null;
let foundDeviceKey = null;
if (device && _.isString(device)) {
foundDeviceKey = _.findKey(devices, function(dev) {
return dev.name.toLowerCase() === device.toLowerCase();
});
if (!_.isNil(foundDeviceKey)) {
foundDevice = devices[foundDeviceKey];
}
}
/*
let pageResource = null;
if (this.pages.length) {
pageResource = this.pages.pop();
} else {
if (_.isNil(this.browser)) {
throw new Error('browser not initialized');
} else {
pageResource = this.browser.newPage();
}
}
return pageResource.then(page => {
if (foundDevice) {
return page.emulate(foundDevice);
} else {
return page;
}
}).then(page => {
this.usingPages.set(pageId, page);
return {
id: pageId,
page: page
};
})
*/
let getPageResouce = () =>
new Promise((resolve, reject) => {
if (this.pages.length) {
resolve(this.pages.pop());
} else {
if (this.browser) {
Promise.try(() => this.browser.newPage())
.then(page => resolve(page))
.catch(e => reject(e));
} else {
reject(new Error("browser not initialized"));
}
}
});
let pagePromise = () =>
new Promise((resolve, reject) => {
getPageResouce()
.then(page => {
if (foundDevice) {
page
.emulate(foundDevice)
.then(() => resolve(page))
.catch(e => reject(e));
} else {
return resolve(page);
}
})
.catch(e => {
reject(e);
});
});
return new Promise((resolve, reject) => {
pagePromise()
.then(page => {
this.usingPages.set(pageId, page);
return resolve({
id: pageId,
page: page
});
})
.catch(e => reject(e));
});
}
destroy() {
return new Promise(resolve => {
if (this.browser) {
if (this.pages.length || !_.isEmpty(this.usingPages)) {
_.concat(this.pages, Array.from(this.usingPages.values())).forEach(
page => _.attempt(() => page.close().catch(e => {}))
);
}
_.attempt(() => this.browser.close().catch(e => {}));
this.browser = null;
}
resolve();
});
}
releasePage(pageData) {
this.usingPages.delete(pageData.id);
pageData.page.close().catch(e => {});
}
queuePage(pageData) {
if (_.isObject(pageData)) {
this.usingPages.delete(pageData.id);
this.pages.push(pageData.page);
}
}
/**
* setup - to be called before using the getScreenshot
* It will launch the embedded chromium browser instance
* @return {Promise} When browser instance is available it returns promise with
* object instance.
*/
setup() {
return new Promise((resolve, reject) => {
puppeteer
.launch(this.options)
.then(browser => {
this.browser = browser;
return resolve(this);
})
.catch(e => reject(e));
});
}
/**
* Get screenshot
* @param {String} url Request url address
* @param {Object} [options={ Object same as page.screenshot option of puppeteer with additional properties
* - @param {Number} timeout: defaulted to 10000 (seconds), if waitFor option is not specified - this is used.
* - @param {Boolean} fullPage if full page screenshot should be taken. Default is full page.
* - @param {String} type output type as png or jpg.
* - @param {String} encoding binary or base64
* - @param {Boolean} [reuse=false] reuse page for further requests after request is over - Pages are put inside a pool
* @return {Promise[Object]} Returns a promise - with result containing
* - @param {String} url original url.
* - @param {String} resolvedUrl url which got resolved by the browser
* - @param {Buffer} image output type as png or jpg. having encoding of binary or jpg ( specified under options)
* - @param {Boolean} isReachable If the website was reachable - some cases website is not reachable and blank screenshot is presented,
* checking this option will handle these edge cases.
*/
getScreenshot(
url,
options = {
timeout: 10000,
fullPage: true,
type: "png",
encoding: "binary"
},
reuse = false
) {
let opts = _.extend(this.options, options);
let waitFor = _.get(opts, "waitFor", null);
let timeout = _.get(opts, "timeout", 10000);
let pageData = null;
return this.getPage(opts)
.then(data => {
pageData = data;
let page = data.page;
let getWaitingPage = () => {
if (waitFor) {
return page.goto(url, {
waitFor: waitFor
});
} else {
return Promise.race([page.goto(url), page.waitFor(timeout)]);
}
};
return getWaitingPage().then(() => {
return page.screenshot(opts).then(data => {
let resolvedUrl = page.url();
return {
url: url,
resolvedUrl: resolvedUrl,
data: data,
isReachable: !resolvedUrl
.toLowerCase()
.startsWith("chrome-error:")
};
});
});
})
.finally(() => {
if (pageData) {
if (reuse) {
this.queuePage(pageData);
} else {
this.releasePage(pageData);
}
}
});
}
};