This repository has been archived by the owner on Jul 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
304 lines (286 loc) · 8.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
'use strict';
const browserify = require('browserify');
const cdp = require('chrome-remote-interface');
const chromeLauncher = require('chrome-launcher');
const fs = require('fs');
const httpServer = require('http-server');
const portfinder = require('portfinder');
/** Async function to convert stream to a string */
function streamToString(stream) {
return new Promise((resolve, reject) => {
const chunks = [];
stream.on('data', chunk => chunks.push(chunk));
stream.on('end', () => resolve(Buffer.concat(chunks).toString()));
stream.on('error', err => reject(err));
});
}
async function fetchObject(Runtime, obj) {
// object, function
if (obj.type === 'undefined') {
return undefined;
} else if (obj.type === 'number') {
return obj.unserializableValue ? parseFloat(obj.unserializableValue) : obj.value;
} else if (obj.type === 'string') {
return obj.value;
} else if (obj.type === 'boolean') {
return obj.value;
} else if (obj.type === 'function') {
return obj.description;
} else if (obj.type === 'symbol') {
return obj.description;
// TODO node, regexp, date, map, set, weakmap, weakset, iterator, generator, error, proxy, promise, typedarray
} else if (obj.subtype === 'null') {
return null;
} else if (obj.subtype === 'array') {
const {
result
} = await Runtime.getProperties({
objectId: obj.objectId,
ownProperties: true
});
const length = result.filter(p => p.name === 'length')[0].value.value;
const array = new Array(length);
result.forEach(e => {
if (e.name !== '__proto__' && e.name !== 'length') {
array[e.name] = fetchObject(Runtime, e.value);
}
});
return await Promise.all(array);
} else if (obj.subtype === 'regexp') {
return obj.description;
} else if (obj.preview === undefined) {
return {};
} else { // object
const valid = {};
const sent = Symbol();
obj.preview.properties.forEach(p => valid[p.name] = sent);
const {
result
} = await Runtime.getProperties({
objectId: obj.objectId
});
const props = await Promise.all(result.filter(p => valid[p.name] === sent).map(
async p => {
const value = await fetchObject(Runtime, p.value);
return await [p.name, value];
}));
const ret = {};
props.forEach(([name, value]) => ret[name] = value);
return ret;
}
}
/** Setup chrome for usage */
async function prepareChrome(chrome, protocol) {
const {
Page,
DOM,
Runtime,
CSS,
} = protocol;
await Promise.all([Page.enable(), DOM.enable(), Runtime.enable(), CSS.enable()]);
// Forward console messages, this doesn't work that well
await Runtime.consoleAPICalled(async evt => {
try {
const args = await Promise.all(evt.args.map(a => fetchObject(Runtime,
a)));
console[evt.type](...args);
} catch (ex) {
console.log(ex);
process.exit(1);
}
});
}
/** Start a file server at the cwd */
function startServer(server, host, port) {
return new Promise((resolve, reject) => {
server.listen(port, host, resolve);
});
}
/** Start a new local file server for fs */
async function prepareFileServer() {
const server = httpServer.createServer({
showDir: 'false',
});
const host = 'localhost';
// XXX There is a race condition here where we pick a "free" port before
// actually using it, so especially if two of these processes are started
// close together, one will likely request a busy port. We solve this by
// starting at a random port, but this is mostly a stopgap and doesn't remove
// the race condition. Unfortunately, it's impossible to listen for the
// connection error, and as such, this is the only option.
// TODO Maybe it's possible to try starting it up with a timeout and then
// checking to see if the server is still up?
portfinder.basePort = Math.floor(Math.random() * 16384) + 49152;
const port = await portfinder.getPortPromise();
await startServer(server, host, port);
return {
server: server,
host: host,
port: port
};
}
/** Load and process user script */
async function prepareUserScript(scriptLocation) {
// FIXME Debug doesn't seem to debug exactly. Line numbers are still listed
// as being in the anonymous megafile these are all concatenated into.
return await streamToString(browserify(scriptLocation, {
insertGlobalVars: {
'process': undefined,
},
debug: true,
}).require(__dirname + '/fs.js', {
expose: 'fs'
}).bundle());
}
/** Add all style sheets in order */
// TODO For some reason, this doesn't modify the document, it only modifies the
// style as far the execution is concerned. Hopefully it's possible to actually
// update in the document itself so that writes of the HTML preserve the style.
async function addStyleSheets(CSS, frameId, styles) {
for (const css of styles) {
const {
styleSheetId
} = await CSS.createStyleSheet({
frameId: frameId
});
await CSS.setStyleSheetText({
styleSheetId: styleSheetId,
text: css,
});
}
}
/** Run users script */
async function executeUserScript(Runtime, script, host, port, argv) {
// Most of these defaults were copied from browserify, some are added for our
// own purposes.
const setupScript =
`process = {
argv: ${JSON.stringify(argv)},
cwd: () => '/',
env: ${JSON.stringify(process.env)},
_host: ${JSON.stringify(host)},
_port: ${JSON.stringify(port)},
title: 'browser',
browser: true,
version: '',
versions: {},
on: () => {},
addListener: () => {},
once: () => {},
off: () => {},
removeListener: () => {},
removeAllListeners: () => {},
emit: () => {},
prependListener: () => {},
prependOnceListener: () => {},
listeners: name => [],
binding: name => { throw new Error('process.binding is not supported'); },
chdir: dir => { throw new Error('process.chdir is not supported'); },
umask: () => 0,
};`
await Runtime.evaluate({
expression: setupScript,
});
const {
result,
exceptionDetails
} = await Runtime.evaluate({
expression: script,
});
if (exceptionDetails) {
throw new Error(exceptionDetails.exception.description);
}
}
/** Measure size of html in inches */
async function measureSize(Runtime) {
const rectId = await Runtime.evaluate({
expression: 'document.documentElement.getBoundingClientRect()',
});
const {
result
} = await Runtime.getProperties({
objectId: rectId.result.objectId,
accessorPropertiesOnly: true,
});
return {
width: result.filter(prop => prop.name === 'width')[0].value.value / 96,
height: result.filter(prop => prop.name === 'height')[0].value.value / 96,
};
}
/** The public api
* @param {string | stream} scriptLocation Where the script to execute is located
* @param {object} options {styles, argv} for execution
*/
async function headlesspdf(scriptLocation, options) {
const {
styles = [],
argv = [],
} = options || {};
let chrome, protocol, server, host, port;
try {
// These are all done serially, so if one fails, the rest will be cleaned
chrome = await chromeLauncher.launch({
chromeFlags: [
'--disable-gpu',
'--headless',
]
});
protocol = await cdp({
port: chrome.port
});
({
server,
host,
port,
} = await prepareFileServer());
const [script, ] = await Promise.all([prepareUserScript(scriptLocation),
prepareChrome(chrome, protocol)
]);
const {
Page,
Runtime,
CSS,
} = protocol;
// Load initial frame loaded from same host as file server
const {
frameId
} = await Page.navigate({
url: `http://${host}:${port}`,
});
await Page.loadEventFired();
Page.setDocumentContent({
frameId: frameId,
html: '<html><head></head><body></body></html>'
});
// add sheets and execute
await addStyleSheets(CSS, frameId, styles);
await executeUserScript(Runtime, script, host, port, argv);
// Generate pdf data
const size = await measureSize(Runtime);
if (!size.height || !size.width) {
throw new Error("No bounding box found, content probably wasn't rendered");
}
const pdf = await Page.printToPDF({
printBackground: true,
marginTop: 0,
marginBottom: 0,
marginLeft: 0,
marginRight: 0,
paperWidth: size.width,
paperHeight: size.height,
pageRanges: '1',
});
return pdf.data;
} finally {
if (server !== undefined) {
server.close();
}
if (protocol !== undefined) {
await protocol.close();
}
if (chrome !== undefined) {
await chrome.kill();
}
}
}
module.exports = headlesspdf;