forked from browserify/wzrd.in
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiple.js
146 lines (123 loc) · 3.8 KB
/
multiple.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
var stringifyError = require('./stringify-error');
module.exports = function (app, bundle) {
app.post('/multi', jsonParser, create(bundle));
app.get('/multi/:bundle', get(bundle));
};
//
// We're only doing JSON bodies, content-type be damned.
//
function jsonParser(req, res, next) {
req.chunks = '';
req.on('data', function (buff) {
req.chunks += buff.toString();
});
req.on('end', function () {
req.body = JSON.parse(req.chunks);
next();
});
};
function create(bundle) {
var cache = bundle.cache;
return function (req, res) {
var opts = req.body;
var deps = opts.dependencies,
options = opts.options || {};
if (typeof deps === 'undefined' || deps === null) {
res.statusCode = 500;
res.setHeader('content-type', 'text/plain');
res.write(stringifyError.hello);
res.write(stringifyError(new Error(
'Multibundles *must* have specified dependencies.\n' +
'Otherwise this exercise is pretty much pointless.'
)));
return res.end(stringifyError.goodbye);
}
cache.multibundles(cache.defaultHashFxn(opts), function multibundle(cb) {
var keys = Object.keys(deps),
count = keys.length,
modules = {},
errors = [];
keys.forEach(function (k) {
var o = JSON.parse(JSON.stringify(options));
o.module = k;
o.version = deps[k];
bundle(o, function (err, b) {
if (err) {
errors.push(err);
}
else {
modules[k] = b;
}
count--;
if (!count) {
handleBundle();
}
});
});
function handleBundle() {
if (errors.length) {
//
// End the request. Don't try caching.
//
res.statusCode = 500;
res.setHeader('content-type', 'text/plain');
res.write(stringifyError.hello);
errors.forEach(function (e, i) {
res.write('\n--- error #' + i + ': ---\n\n');
res.write(stringifyError(e));
res.write('\n');
});
res.write('\n------\n');
return res.end(stringifyError.goodbye);
}
return cb(null, JSON.stringify(modules, true, 2));
}
}, function (err, _b) {
if (err) {
res.statusCode = 500;
res.setHeader('content-type', 'text/plain');
res.write(stringifyError.hello);
res.write(stringifyError(err));
return res.end(stringifyError.goodbye);
}
//
// It seems 302s cause problems with certain use cases.
// I still include the Location header here though, so that one may
// pull it out programmatically if they wish.
//
// res.statusCode = 302;
res.setHeader('Location', '/multi/' + encodeURIComponent(cache.defaultHashFxn(opts)));
res.setHeader('content-type', 'application/json');
res.end(_b);
});
};
};
function get(bundle) {
var cache = bundle.cache;
return function (req, res) {
var hash = req.params.bundle;
cache.multibundles(decodeURIComponent(hash), function nope(cb) {
res.statusCode = 404;
res.setHeader('content-type', 'text/plain');
res.write(stringifyError.hello);
res.write(stringifyError(new Error(
'The requested bundle does not exist.\n' +
'Have you tried POSTING to `/multi`?'
)));
return res.end(stringifyError.goodbye);
}, function yup(err, b) {
if (err) {
//
// This should not happen.
//
res.statusCode = 500;
res.setHeader('content-type', 'text/plain');
res.write(stringifyError.hello);
res.write(stringifyError(err));
return res.end(stringifyError.goodbye);
}
res.setHeader('content-type', 'application/json');
return res.end(b);
});
};
};