-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
273 lines (226 loc) · 8.62 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
var path = require('path');
var fs = require('fs');
var url = require('url');
var crypto = require('crypto');
var convert = require('convert-source-map');
var Ready = require('ready-signal');
var mime = require('mime');
var uglifyjs = require('uglify-js');
var browserify = require('browserify');
var debug = require('debug')('enchilada');
var filewatcher = require('filewatcher');
uglifyjs.AST_Node.warn_function = function(msg) {
debug('warn: %s', msg);
};
module.exports = function enchilada(opt) {
// if just a path is passed in, treat as public file dir
if (typeof opt === 'string') {
opt = { src: opt };
}
var pubdir = opt.src;
var routes = opt.routes || {};
var bundles = {};
var compress = false || opt.compress;
var cache = {}; // cache of sourcefiles
var generating = {};
var maps = {}; // cache of sourcemaps
var debug_opt = false || opt.debug;
var watch = !opt.cache;
var watchCallback = opt.watchCallback;
function makeBundle(options) {
var bundle = browserify(options);
if (opt.transforms) {
opt.transforms.forEach(function(transform) {
bundle.transform(transform)
});
}
if (opt.externals) {
opt.externals.forEach(function(external) {
bundle.external(external);
});
}
return bundle;
}
// TODO(shtylman) externs that use other externs?
Object.keys(routes).map(function(id) {
var name = routes[id];
debug('route: %s -> %s', id, name);
var bundle = makeBundle({ exposeAll: true });
bundle.require(name, { entry: true, expose: name, basedir: pubdir });
return bundles[id] = bundle;
});
return function(req, res, next) {
var req_path = req.path || url.parse(req.url).path;
if (/.map.json$/.test(req_path) && maps[req_path]) {
return res.json(maps[req_path]);
}
// if no extension, then don't process
// handles case of directories and other random urls
if (!path.extname(req_path)) {
return notFound();
}
else if (mime.lookup(req_path) !== 'application/javascript') {
return notFound();
}
// check cache
var cached = cache[req_path];
if (cached) {
return sendResponse(null, cached);
}
// check for bundle
var bundle = bundles[req_path];
if (bundle) {
return safeGenerate(bundle, sendResponse);
}
var local_file = path.normalize(path.join(pubdir, req_path));
// check for malicious attempts to access outside of pubdir
if (local_file.indexOf(pubdir) !== 0) {
return notFound();
}
// lookup in filesystem
fs.exists(local_file, function(exists) {
if (!exists) {
return notFound();
}
var bundle = makeBundle(local_file);
Object.keys(bundles).forEach(function(id) {
bundle.external(bundles[id]);
});
safeGenerate(bundle, sendResponse);
});
// safeGenerate joins multiple simultaneous generates per req_path
function safeGenerate(bundle, callback) {
var ready = generating[req_path];
if (!ready) {
ready = generating[req_path] = Ready();
generate(bundle, function(error, src) {
delete generating[req_path];
ready.signal(error, src);
});
}
ready(callback);
}
function generate(bundle, callback) {
var dependencies = {};
// typically SyntaxError
var otherError;
bundle.once('error', function(err) { otherError = err; });
var collect_deps = function(file) {
dependencies[file] = true;
};
if (watch) {
bundle.on('file', collect_deps);
}
bundle.bundle({ debug: debug_opt }, function(err, src) {
bundle.removeListener('file', collect_deps);
if (watch) {
watchFiles(bundle, dependencies, req_path);
}
if (err) {
return callback(err);
}
if (otherError) {
return callback(otherError);
}
var srcmap = undefined;
var map_path = undefined;
if (debug_opt) {
// output sourcemap
srcmap = convert.fromComment(src);
src = convert.removeComments(src);
srcmap.setProperty('file', req_path);
map_path = req_path.replace(/.js$/, '.map.json');
}
if (compress) {
var ugly_opt = {};
if (typeof compress == 'object') {
ugly_opt = compress
}
ugly_opt.fromString = true;
if (srcmap) {
ugly_opt.inSourceMap = srcmap.toObject(),
ugly_opt.outSourceMap = req_path
}
var result = uglifyjs.minify(src, ugly_opt);
src = result.code;
if (srcmap) {
// prepare new sourcemap
// we need to get the sources from bundled sources
// uglify does not carry those through
var srcs = srcmap.getProperty('sourcesContent');
srcmap = convert.fromJSON(result.map);
srcmap.setProperty('sourcesContent', srcs);
}
}
if (srcmap) {
src += '//# sourceMappingURL=' + path.basename(map_path);
maps[map_path] = srcmap.toObject();
}
cache[req_path] = src;
callback(null, src);
});
}
function notFound() {
if (typeof next === 'function') {
return next();
}
res.setHeader('Content-Type', 'text/plain');
respond(404, 'Not Found');
}
function sendResponse(err, src) {
if (err) {
return sendError(err);
}
res.setHeader('Content-Type', 'application/javascript');
var etag = crypto.createHash('md5').update(src).digest('hex').slice(0, 6);
if (req.get && (etag === req.get('If-None-Match'))) {
respond(304, '');
}
else {
res.setHeader('ETag', etag);
res.setHeader('Vary', 'Accept-Encoding');
respond(200, src);
}
}
function sendError(err) {
if (typeof next === 'function') {
return next(err);
}
res.setHeader('Content-Type', 'text/plain');
respond(500, err.toString());
}
function respond(code, data) {
data = new Buffer(data);
res.setHeader('Content-Length', data.length);
res.writeHeader(code);
res.end(data);
}
function watchFiles(bundle, dependencies, path) {
// close any current watchers to avoid double watching
// this happens when we are already bundling (not yet done)
// and a file change (or request) triggers a new build
// leading to generate being called again (which will add watchers)
// but our first build will also add watchers since nothing will stop it
// here we remove any watchers first
var watcher = filewatcher();
watcher.once('change', function(file) {
debug("rebuilding %s due to change in %s", req_path, file);
watcher.removeAll();
delete cache[path];
safeGenerate(bundle, function(error) {
if (watchCallback) {
watchCallback(error, path);
}
});
});
watcher.once('fallback', function(limit) {
debug('Ran out of file handles after watching %s files.', limit);
debug('Falling back to polling which uses more CPU.');
debug('Run ulimit -n 10000 to increase the limit for open files.');
});
Object.keys(dependencies).map(function(filename) {
watcher.add(filename);
});
}
};
};