-
Notifications
You must be signed in to change notification settings - Fork 3
/
combo.js
49 lines (36 loc) · 1.16 KB
/
combo.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
'use strict';
var path = require('path');
var combo = require('combo-url');
var request = require('co-request');
var log = require('spm-log');
module.exports = function(opts) {
var contentTypes = {
'.css': 'text/css',
'.js': 'application/javascript'
};
return function*(next) {
var url = decodeURIComponent(this.url);
if (!combo.isCombo(url)) {
return yield next;
}
var data = combo.parse(url);
var ret = [];
var contentType = contentTypes[path.extname(data.combo[0])] || 'text/plain';
for (var i = 0; i < data.combo.length; i++) {
var item = data.combo[i];
var _url = this.protocol + '://' + opts.hostname + ':' + opts.port + item;
log.info('combo fetch', _url);
var result = yield request(_url);
if (result.statusCode === 404 || result.statusCode === 204) {
log.error('' + result.statusCode, _url);
this.status = 404;
this.body = result.statusMessage + ':\n' + _url;
return;
}
ret.push(result.body);
}
// Set content-type by the first file in combo request
this.set('content-type', contentType);
this.body = ret.join('\n');
};
};