-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·105 lines (90 loc) · 2.34 KB
/
server.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
#!/usr/bin/env node
var express = require('express'),
app = express.createServer(),
util = require('./util'),
https = require('https');
var cache = {
};
var getRemote = function(url, cb) {
console.log('Fetching Remote Resource', url);
https.get({
host: 'raw.github.com',
path: '/yui/yui3/master/build/' + url
}, function(res) {
var d = '';
res.on('data', function(chunk) {
d += chunk;
});
res.on('end', function() {
cb(d);
});
});
};
setInterval(function() {
console.log('Clearing Cache');
cache = {};
}, (5 * 60 * 1000));
var get = function(urls, cb) {
if (typeof urls === 'string') {
urls = [urls];
}
var stack = new util.Stack(),
data = [];
urls.forEach(function(u, k) {
if (cache[u]) {
data[k] = cache[u].data;
} else {
getRemote(u, stack.add(function(d) {
cache[u] = {
data: d
};
data[k] = d;
}));
}
});
stack.done(function() {
cb(data.join('\n'));
});
};
var stamp = function(str) {
return str.replace(/@VERSION@/g, 'yui-master-combo');
};
app.get('/', function(req, res) {
var filter = req.query.filter || 'min',
url = 'yui/yui-min.js';
switch (filter) {
case 'raw':
url = 'yui/yui.js';
break;
case 'debug':
url = 'yui/yui-debug.js';
break;
}
get(url, function(data) {
res.contentType('.js');
var host = req.headers.host;
var append = '\n/* Adding global config */\n\n';
append += 'YUI.applyConfig({\n';
append += ' root: "",\n';
append += ' filter: "' + filter + '",\n';
append += ' comboBase: "http:/'+'/' + host + '/combo?"\n';
append += '});\n';
append += 'YUI.version = "yui-master-combo";\n';
data += append;
data = stamp(data);
res.send(data);
});
});
app.get('/combo', function(req, res) {
var urls = Object.keys(req.query);
console.log('Fetching', urls);
get(urls, function(data) {
res.contentType(urls[0]);
data = stamp(data);
res.send(data);
});
});
app.get('*', function(req, res) {
res.send(404);
});
app.listen(20713);