-
Notifications
You must be signed in to change notification settings - Fork 5
/
urlRenderer.js
173 lines (150 loc) · 4.09 KB
/
urlRenderer.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
#!/usr/bin/env node
'use strict';
var path = require('path');
var urlLib = require('url');
var request = require('request');
var jsdom = require('jsdom');
var spawn = require('child_process').spawn;
function filterHeaders(headers) {
var newHeaders = {};
for (var key in headers) {
if (key === 'host' || key === 'cookie' ||
(key.lastIndexOf('accept', 0) === 0 && key !== 'accept-encoding')) {
newHeaders[key] = headers[key];
}
}
return newHeaders;
}
function renderURL(url, headers, options, callback) {
var timeout = (options && options.timeout ? options.timeout : 3000);
var cookieDomain = options && options.cookieDomain;
request({
uri: urlLib.parse(url),
headers: filterHeaders(headers)
}, function(err, res, body) {
if (err) {
callback(err);
return;
}
if (res.statusCode !== 200) {
callback(res.statusCode);
return;
}
var document;
var timer = null;
var done = function() {
if (!timer) return;
clearTimeout(timer);
timer = null;
var content;
try {
document.body.setAttribute('data-prerendered', 'true');
content = jsdom.serializeDocument(document);
} catch (err) {
return callback(err);
}
callback(null, content, res.headers);
};
timer = setTimeout(function() {
console.error('prerenderer[err]: Timouted after ' + timeout + 'ms…');
done();
}, timeout);
try {
document = jsdom.jsdom(body, {
url: url,
cookie: headers.cookie,
cookieDomain: cookieDomain,
features: {
FetchExternalResources: ['script'],
ProcessExternalResources: ['script']
},
created: function(err, window) {
if (err) return;
if (options && options.attachConsole) {
window.console.log = function() {
Array.prototype.unshift.call(arguments, 'prerenderer[out]:');
console.error.apply(console, arguments);
};
window.console.error = function() {
Array.prototype.unshift.call(arguments, 'prerenderer[err]:');
console.error.apply(console, arguments);
};
}
},
done: done
});
document.onprerendered = done;
} catch (err) {
if (timer) {
clearTimeout(timer);
timer = null;
}
callback(err);
return;
}
});
}
module.exports.renderURL = renderURL;
module.exports.subprocessRenderURL = function subprocessRenderURL(url, headers, options, callback) {
var renderer = spawn(process.argv[0], [path.join(__dirname, 'urlRenderer.js'), JSON.stringify([url, headers, options])], {
stdio: ['ignore', 'pipe', 'pipe']
});
renderer.stderr.pipe(process.stderr);
var allData = new Buffer('');
renderer.stdout.on('data', function(data) {
allData = Buffer.concat([allData, data]);
});
var timeout = (options && options.timeout ? options.timeout : 3000) + 1500;
var timer = null;
function done(err) {
if (timer) {
clearTimeout(timer);
timer = null;
}
renderer.kill('SIGKILL');
var args;
try {
args = JSON.parse(allData);
} catch (e) {
args = [e];
}
if (args[0] && !(args[0] instanceof Error)) {
args[0] = new Error(args[0]);
} else if (err) {
args[0] = err;
}
callback.apply(null, args);
}
timer = setTimeout(function() {
console.log('Timouted after ' + timeout + ' ms');
done();
}, timeout);
renderer.stdout.on('end', done);
renderer.stdout.on('error', function(err) {
renderer.kill('SIGKILL');
if (timer) {
clearTimeout(timer);
timer = null;
}
callback(err);
});
};
if (module.parent) {
return;
}
var args = [];
try {
args = JSON.parse(process.argv[2] || '[]');
} catch (e) {
console.log(JSON.stringify([e.stack]));
process.exit(1);
}
if (args.length !== 3) {
console.log('["Bad argument"]');
process.exit(1);
}
args.push(function(err, content, headers) {
console.log(JSON.stringify([err && err.stack, content, headers]));
process.exit(0);
});
renderURL.apply(null, args);