-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathindex.js
146 lines (115 loc) · 3.79 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
var trumpet = require('trumpet');
var zlib = require('zlib');
module.exports = function harmonBinary(reqSelectors, resSelectors, htmlOnly) {
var _reqSelectors = reqSelectors || [];
var _resSelectors = resSelectors || [];
var _htmlOnly = (typeof htmlOnly == 'undefined') ? false : htmlOnly;
function prepareRequestSelectors(req, res) {
var tr = trumpet();
prepareSelectors(tr, _reqSelectors, req, res);
req.on('data', function(data) {
tr.write(data);
});
}
function prepareResponseSelectors(req, res) {
var tr = trumpet();
var _write = res.write;
var _end = res.end;
var _writeHead = res.writeHead;
var gunzip = zlib.Gunzip();
prepareSelectors(tr, _resSelectors, req, res);
// Assume response is binary by default
res.isHtml = false;
// Assume response is uncompressed by default
res.isGziped = false;
res.writeHead = function () {
var code = arguments[0];
var headers = (arguments.length > 2) ? arguments[2] : arguments[1]; // writeHead supports (statusCode, headers) as well as (statusCode, statusMessage, headers)
var contentType = this.getHeader('content-type');
var contentEncoding = this.getHeader('content-encoding');
/* Sniff out the content-type header.
* If the response is HTML, we're safe to modify it.
*/
if (!_htmlOnly || ((typeof contentType != 'undefined') && (contentType.indexOf('text/html') == 0))) {
res.isHtml = true;
// Strip off the content length since it will change.
res.removeHeader('Content-Length');
if (headers) {
delete headers['content-length'];
}
}
/* Sniff out the content-type header.
* If the response is Gziped, we're have to gunzip content before and ungzip content after.
*/
if (res.isHtml && contentEncoding && contentEncoding.toLowerCase() == 'gzip') {
res.isGziped = true;
// Strip off the content encoding since it will change.
res.removeHeader('Content-Encoding');
if (headers) {
delete headers['content-encoding'];
}
}
_writeHead.apply(res, arguments);
};
res.write = function (data, encoding) {
// Only run data through trumpet if we have HTML
if (res.isHtml) {
if (res.isGziped) {
gunzip.write(data);
} else {
tr.write(data, encoding);
}
} else {
_write.apply(res, arguments);
}
};
tr.on('data', function (buf) {
_write.call(res, buf);
});
gunzip.on('data', function (buf) {
tr.write(buf);
});
res.end = function (data, encoding) {
if (res.isGziped) {
gunzip.end(data);
} else {
tr.end(data, encoding);
}
};
gunzip.on('end', function (data) {
tr.end(data);
});
tr.on('end', function () {
_end.call(res);
});
}
function prepareSelectors(tr, selectors, req, res) {
for (var i = 0; i < selectors.length; i++) {
(function (callback, req, res) {
var callbackInvoker = function(element) {
callback(element, req, res);
};
tr.selectAll(selectors[i].query, callbackInvoker);
})(selectors[i].func, req, res);
}
}
return function harmonBinary(req, res, next) {
var ignore = false;
if (_htmlOnly) {
var lowercaseUrl = req.url.toLowerCase();
if ((lowercaseUrl.indexOf('.js', req.url.length - 3) !== -1) ||
(lowercaseUrl.indexOf('.css', req.url.length - 4) !== -1)) {
ignore = true;
}
}
if (!ignore) {
if (_reqSelectors.length) {
prepareRequestSelectors(req, res);
}
if (_resSelectors.length) {
prepareResponseSelectors(req, res);
}
}
next();
};
};