forked from bnoguchi/browser-require
/
browser-require.old.js
259 lines (248 loc) · 8.76 KB
/
browser-require.old.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
var fs = require('fs')
, path = require('path')
, npm = require('npm');
module.exports = function (opts) {
var baseDir = opts.base
, compile = opts.compile
, cache = {} // {moduleName: { mtime: ..., compiled: ...}}
, templates = {
response: fs.readFileSync(__dirname + '/templates/response.js', 'utf8')
, src: fs.readFileSync(__dirname + '/templates/src.js', 'utf8')
};
/**
* Loads the module's parameters into the template.
* In dynamic mode, the template is sent to the browser.
* In compile mode, the template is used as a fragment in the aggregate compiled output.
*
* @param {String} module is the name of the module (e.g., 'somemodule.js')
* @param {String} src is the src of the module
* @param {Array} deps is the array of the module's dependencies, as Strings
* @param {Boolean} isIndex is true if module's src existed at an index.js
* @return {String} the template that is filled in with the module's information
*/
function fillinTemplate (module, src, deps, isIndex) {
var dir = isIndex
? module.replace(/\.js$/, '').split('/')
: module.split('/').slice(0, -1)
, src = templates.src
.replace('$src', src)
.replace(/\$dir/g, JSON.stringify(dir));
return templates.response
.replace('$module', JSON.stringify(module))
.replace('$src', JSON.stringify(src))
.replace('$deps', JSON.stringify(deps))
.replace('$isIndex', isIndex);
}
/**
* Returns the names of the dependencies found in the source text.
*
* @param {String} src
* @return {Array} dependencies
*/
function depsFor (src) {
var re = /require\(['"]([^'"]+)['"]\)/g
, match
, deps = [];
while (match = re.exec(src)) {
deps.push(match[1]);
}
return deps;
}
function NpmModule (url) {
var modulePath = url.replace(NpmModule.npmFlag, '').replace(/\.js$/, '')
, moduleChain = modulePath.split('/')
, pkgName = this.pkgName = moduleChain[0]
, relChain = this.relChain = moduleChain.slice(1);
this.isSubmodule = !!relChain.length;
var nv = pkgName.split('@')
, n = this.name = nv[0]
, v = this.ver = nv[1] || 'active';
this.dir = path.join(npm.dir, n, v, 'package');
}
NpmModule.npmFlag = /^\/NPM\//;
NpmModule.prototype = {
__pkgJson: function (fn) {
if (this._pkg) return fn(null, this._pkg);
if (this._pkgerr) return fn(this._pkgerr);
var self = this;
fs.readFile(this.dir + '/package.json', 'utf8', function (err, body) {
if (err) {
self._pkgerr = err;
console.error("package.json missing for " + self.pkgName);
return fn(err);
}
var pkg = self._pkg = JSON.parse(body);
fn(null, pkg);
});
},
__mainSrc: function (fn) {
if (this._mainSrc) return fn(null, this._mainSrc);
if (this._mainSrcErr) return fn(this._mainSrcErr);
var self = this;
this.pkgJson( function (err, pkg) {
if (err) return fn(self._mainSrcErr = err);
if (pkg.main) {
fs.stat(self.dir + '/' + pkg.main, function (err, stat) {
if (err) {
console.error(err);
fn(self._mainSrcErr = err);
} else if (stat.isDirectory()) {
self._mainSrc = fs.readFileSync(self.dir + '/' + pkg.main + '/index.js', 'utf8');
fn(null, self._mainSrc);
} else {
self._mainSrc = fs.readFileSync(self.dir + '/' + pkg.main, 'utf8');
fn(null, self._mainSrc);
}
});
} else {
fn(self.mainSrcErr = "Missing main in package.json for " + self.pkgName);
}
});
},
__relSrc: function (fn) {
if (this._relSrc) return fn(null, this._relSrc);
if (this._relSrcErr) return fn(this._relSrcErr);
var self = this;
this.pkgJson( function (err, pkg) {
if (err) return fn(self._relSrcErr = err);
var directories = pkg.directory || pkg.directories
, lib = directories && directories.lib
, chain = [self.dir], direct, index;
if (lib) chain.push(lib);
direct = path.join.apply(this, chain.concat([self.relChain.join('/') + '.js']) );
index = path.join.apply(this, chain.concat([self.relChain.join('/'), 'index.js']) );
if (path.existsSync(direct)) {
fn(null, fs.readFileSync(direct, 'utf8'), false);
} else if (path.existsSync(index)) {
fn(null, fs.readFileSync(index, 'utf8'), true);
} else {
if (lib) {
throw new Error("Unimplemented - could not find package " + self.relChain.join('/'));
} else {
self._relSrcErr = 'Missing ' + self.relChain.join('/') + ' in ' + self.pkgName + ' package';
fn(self._relSrcErr);
}
}
});
},
__src: function (fn) {
if (this._src) return fn(null, this._src);
if (this._srcerr) return fn(this._srcerr);
var self = this;
if (this.isSubmodule) { // Handle e.g., require("npm-module/sub-module")
this.relSrc( function (err, relSrc, isIndex) {
if (err) return fn(self._srcerr = err);
fn(null, self._src = relSrc, isIndex);
});
} else { // Handle e.g., require("npm-module")
this.mainSrc( function (err, mainSrc) {
if (err) return fn(self._srcerr = err);
fn(null, self._src = mainSrc);
});
}
},
__isInstalled: function (fn) {
var self = this;
fs.stat(this.dir, function (err, stat) {
if (err || !stat.isDirectory()) {
console.error(self.name + " is not installed via npm.");
fn(err, false);
} else {
fn(null, true);
}
});
}
};
// Wrap npm in a promise, for manageable async middleware
NpmModule.isNpmLoaded = false;
NpmModule.callbacks = []; // Callbacks for loaded event
var proto = NpmModule.prototype;
for (var k in proto) {
proto[k.slice(2)] = (function (k) {
return function () {
if (NpmModule.isNpmLoaded) return proto[k].apply(this, arguments);
NpmModule.callbacks.push([proto[k], this, arguments]);
};
})(k);
}
npm.load( function () {
NpmModule.isNpmLoaded = true;
var callbacks = NpmModule.callbacks;
for (var i = 0, l = callbacks.length; i < l; i++) {
callbacks[i][0].apply(callbacks[i][1], callbacks[i][2]);
}
});
/**
* The incoming url does not always verbatim point to the src file.
* This is not the case for
*/
function extractUrl (url) {
// The following continuous block handles incoming requires
// that exist above the base dir
var uq = url.split('?')
, chain = uq[0].split('/')
, q = uq[1]
, match
, nAboveBase;
// prefix carries ..,..,.. information - i.e., how many levels above
if (q) {
match = q.match(/n=([^&]+)/);
if (match) nAboveBase = parseInt(match[1], 10);
}
if (nAboveBase) {
url = chain.join('/');
while (nAboveBase--) url = '/..' + url;
}
return url;
}
return function (req, res, next) {
var src
, url = extractUrl(req.url)
, body
, filepath;
if (src = cache[url]) {
res.writeHead(200, {'Content-Type': 'text/javascript'});
res.end(src);
} else if ('.js' === path.extname(url)) {
if (url === '/browser_require.js') {
src =
cache[url] = fs.readFileSync(
path.dirname(__filename) +
'/client/browser_require.js', 'utf8');
res.writeHead(200, {'Content-Type': 'text/javascript'});
res.end(src);
} else if (NpmModule.npmFlag.test(url)) { // Handle npm modules
var npmModule = new NpmModule(url);
npmModule.isInstalled(function (err, isInstalled) {
if (isInstalled) {
npmModule.src(function (err, body, isIndex) {
var src =
cache[url] = fillinTemplate(url, body, depsFor(body), isIndex);
res.writeHead(200, {'Content-Type': 'text/javascript'});
res.end(src);
});
} else {
console.error("Could not find " + npmModule.pkgName +
". Make sure it's installed via npm.");
res.writeHead(404);
res.end();
}
});
} else { // Handle local, relative modules
filepath = path.join(baseDir, url);
if (path.existsSync(filepath)) {
body = fs.readFileSync(filepath, 'utf8');
src =
cache[url] = fillinTemplate(url, body, depsFor(body));
res.writeHead(200, {'Content-Type': 'text/javascript'});
res.end(src);
} else {
console.error("Could not find " + filepath);
next();
}
}
} else {
next();
}
};
};