-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare.js
378 lines (330 loc) · 13.4 KB
/
prepare.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/* == Dependency includes == */
var jsdom = require('jsdom'),
fs = require('fs'),
path = require('path'),
url = require('url'),
UglifyJS = require('uglify-js'),
fibrous = require('fibrous'),
Module = require('module');
/* === Utility functions === */
// Checks whether a string ends with the specified substring
String.prototype.endsWith = function(suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
// Converts a string into a plain but readable HTML format.
function toHtml(str) {
return str.replace(/\n/g, '<br/>').replace(/\t/g, '  ').replace(/ /g, ' ');
}
// Converts a path into a file:/// url.
function pathToFileUrl(p) {
p = p.replace(/\\/g, '/');
if (p[0] !== '/') {
p = '/' + p;
}
return 'file://' + p;
}
// Joins the arguments into a path, and reads that file (as utf8).
function readFile() {
return fs.readFileSync(path.join.apply(path, arguments), 'utf8');
}
// Copies the "props" properties from object "obj" into a new object.
function copySome(obj, props) {
var copy = {};
props.forEach(function (prop) {
copy[prop] = obj[prop];
});
return copy;
}
// Includes scripts in a document, before a certain node.
function include(window, scripts, before) {
var insertNode = before;
for (var i = 0; i < scripts.length; i++) {
var node = window.document.createElement('script');
for (var n in scripts[i]) {
node[n] = scripts[i][n];
}
if (insertNode)
insertNode.parentNode.insertBefore(node, insertNode);
else
window.document.body.appendChild(node);
}
};
/* ==== The Prepare preprocessor ==== */
// Default environment setup scripts for client and server.
var env = {
server: readFile(__dirname, 'env', 'server.js'),
client: UglifyJS.minify(readFile(__dirname, 'env', 'client.js'), {fromString: true}).code
};
var NO_FILE_TO_PROCESS = new Error('No file to process');
// The main Prepare function. Processes a .js.html file, executing server-side scripts as
// specified, or a .html.js file, which is executed in the context of an empty DOM, leaving
// the user to fill it in any way possible (or just directly write a response).
var prepare = module.exports = function(options, callback) {
var req = options.req, res = options.res,
dir = options.dir || options.directory || '',
src = options.src || options.source,
callback = callback || options.callback;
var reqpath = options.file || (req ? '.' + url.parse(req.url).pathname : '');
var html, script;
if (src) {
html = src;
} else {
try {
if (reqpath.endsWith('.js.html')) {
html = readFile(dir, reqpath);
} else if (reqpath.endsWith('.html.js')) {
script = readFile(dir, reqpath);
html = '<html>';
} else {
callback(NO_FILE_TO_PROCESS);
return;
}
} catch (e) {
callback(NO_FILE_TO_PROCESS);
return;
}
}
// Indicates whether the scripts on the page have finished running.
var responseState;
var errors = [];
// Set up the DOM level object for executing server-side script elements.
var level = jsdom.level(3, 'html');
var filename = path.resolve(dir, reqpath);
var processScript = function(element, code, src) {
var type = (element.type || '').split('/');
if (type.indexOf('server') != -1) {
var doc = element.ownerDocument, window = doc && doc.parentWindow;
if (window) {
if (!responseState) {
// Prepare the window (just in time) for script execution.
responseState = prepareWindow(window, filename, req, res, !!script);
}
// Set up the currentScript property, so the code can find the element it
// belongs to.
window.document.currentScript = element;
try {
window.run(code, src);
} catch (e) {
element.raise(
'error', 'Running ' + src + ' failed.',
{error: e, filename: src}
);
errors.push(e);
}
}
}
};
level.languageProcessors = {
server: processScript,
client: processScript,
keep: processScript
};
// Run inside a fiber for synchronous code execution support.
fibrous.run(function() {
try {
// Construct a window, executing all the script tags inside as well.
var window = jsdom.jsdom(html, level, {
features: {
FetchExternalResources: 'script',
ProcessExternalResources: 'script'
},
url: pathToFileUrl(filename)
}).parentWindow;
// Remove the currentScript property, so asynchronous scripts won't think
// they're actually the last one.
delete window.document.currentScript;
// If no scripts have been executed, set up the environment anyway, so "finish"
// works correctly (for the client side).
if (!responseState) {
responseState = prepareWindow(window, filename, req, res, !!script);
}
if (script) {
window.document.write('');
window.run(script);
}
finish(responseState, window, req, errors, callback);
} catch (e) {
window.close();
callback(e);
return;
}
});
};
// Creates a properly working require function from the context of filename as if it were a
// module. Based on code from node's module.js.
function createRequire(filename) {
var newModule = new Module(filename, module);
newModule.filename = filename;
newModule.paths = Module._nodeModulePaths(path.dirname(filename));
function require(path) {
return newModule.require(path);
}
require.resolve = function(request) {
return Module._resolveFilename(request, newModule);
};
Object.defineProperty(require, 'paths', { get: function() {
throw new Error('require.paths is removed. Use ' +
'node_modules folders, or the NODE_PATH ' +
'environment variable instead.');
}});
require.main = process.mainModule;
// Enable support to add extra extension types
require.extensions = Module._extensions;
require.registerExtension = function() {
throw new Error('require.registerExtension() removed. Use ' +
'require.extensions instead.');
};
require.cache = Module._cache;
return require;
}
// This is the function that adds additional properties to the window object to make sure it
// is able to handle user code. Because some scripts are run to set up the correct
// environment, the window object must be fully constructed before this function is executed.
function prepareWindow(window, filename, req, res, fullResponse) {
window.console = Object.create(console);
window.console.log = window.console.info = window.console.error;
window.request = req;
window.require = createRequire(filename);
if (fullResponse) {
window.response = Object.create(res);
} else {
window.response = {};
var responseProps = ['setHeader', 'getHeader', 'removeHeader'];
for (var i = 0; i < responseProps.length; i++) {
var n = responseProps[i];
window.response[n] = res[n];
}
}
var _res = {
scriptHold: 0,
execFinished: false,
registeredScripts: []
};
window.response.hold = function() {
_res.scriptHold++;
};
window.response.release = function() {
if (_res.scriptHold > 0)
_res.scriptHold--;
if (_res.scriptHold === 0 && _res.execFinished && _res.onFinish) {
_res.onFinish();
}
};
// Make sure the response is held until the window is fully loaded.
window.response.hold();
window.addEventListener('load', function() {
window.response.release();
});
window.response.data = {};
var minify = function(code) {
return UglifyJS.minify(code, {fromString: true}).code;
};
window.response.register = function(script) {
if (typeof script === 'string')
_res.registeredScripts.push({ text: minify(script) });
else if (typeof script === 'function') {
var code = 'var f = ' + script.toString() + ';f(';
for (var i = 1; i < arguments.length; i++) {
var arg = JSON.stringify(arguments[i]);
if (typeof arguments[i] === 'function')
arg = arguments[i].toString();
code += arg + ',';
}
code = code.substring(0, code.length - 1) + ')';
_res.registeredScripts.push({ text: minify(code) });
} else if (typeof script === 'object') {
_res.registeredScripts.push(script);
}
};
window.run(env.server);
return _res;
};
// Collects the final document from the window object, cleans it up, and returns it to the
// caller.
function finish(responseState, window, req, errors, callback) {
// Set a handler function for when all scripts have finished execution.
responseState.onFinish = function() {
if (errors.length > 0) {
window.close();
callback(errors);
return;
}
var scripts = window.document.getElementsByTagName('script');
// Remove server-side code from the document.
Array.prototype.forEach.call(scripts, function(script) {
var type = (script.type || '').split('/');
var server = type.indexOf('server') != -1,
client = type.indexOf('client') != -1;
if (server || client) {
script.removeAttribute('type');
if (!client) {
if (type.indexOf('keep') != -1) {
script.innerHTML = '';
script.removeAttribute('src');
} else {
script.parentNode.removeChild(script);
}
}
}
});
if (responseState.registeredScripts.length > 0) {
scripts = window.document.getElementsByTagName('script');
var firstScript = scripts.length > 0 ? scripts[0] : undefined;
include(window, responseState.registeredScripts, firstScript);
}
scripts = window.document.getElementsByTagName('script');
// Copy abridged request and response objects to the client.
if (scripts.length > 0) {
var baseReq = copySome(window.request, ['httpVersion', 'headers',
'trailers', 'url', 'method', 'httpVersionMajor', 'httpVersionMinor']);
var prepareJs = env.client + 'request=' + JSON.stringify(baseReq)
+ ';response=' + JSON.stringify(window.response);
include(window, [{text: prepareJs}], scripts[0]);
}
// TODO: remove empty body style tag
// Render the HTML into a string, free resources, and return.
var html = jsdom.serializeDocument(window.document);
// Closing the window here to free possible resources seems reasonable, but it
// causes (native) crashes when functions that clase over variables in the window
// context get passed outside and executed at a later time. Not closing it does not
// seem to have much of an effect on memory usage and makes sure node doesn't crash
// when passing things to other contexts in complicated ways.
//window.close();
callback(null, html);
};
// If the response has not been held, execute the finish handler immediately.
responseState.execFinished = true;
if (!responseState.scriptHold)
responseState.onFinish();
}
// This returns a "middleware" function relative to the specified directory, for use with
// systems like ''connect''.
prepare.middleware = function(dir) {
return function(req, res, next) {
prepare({
req: req,
res: res,
dir: dir,
callback: function(err, html) {
res.setHeader('Content-Type', 'text/html');
if (err) {
if (err === NO_FILE_TO_PROCESS) {
next();
} else {
res.statusCode = 500;
if (err.length) {
err.forEach(function (e) {
res.write('<p>' + toHtml((e.stack || e).toString()) + '</p>');
});
res.end();
} else {
res.end('<p>' + toHtml((err.stack || err).toString()) + '</p>');
}
}
} else {
res.end(html);
}
}
});
};
};