forked from axiomzen/express-interceptor
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
147 lines (124 loc) · 4.06 KB
/
Copy pathindex.js
File metadata and controls
147 lines (124 loc) · 4.06 KB
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
require('es6-promise').polyfill();
module.exports = function(fn) {
var debug = require('debug')('express-interceptor');
return function(req, res, next) {
var methods = fn(req, res);
_validateParams(methods);
var originalEnd = res.end;
var originalWrite = res.write;
var invokeOriginalEnd = function(){
this.write = originalWrite;
originalEnd.apply(this, arguments);
};
var chunks = [];
var isIntercepting;
var isFirstWrite = true;
var isInterceptablePromise;
function intercept(rawChunk, encoding) {
if (isFirstWrite) {
isFirstWrite = false;
isInterceptablePromise = Promise.resolve(methods.isInterceptable())
.then(function(bool){
isIntercepting = bool;
return;
})
.catch(function(){
isIntercepting = false;
debug('function isInterceptable throws exception', arguments);
return;
});
}
// isFirstWrite should call first, so isInterceptablePromise will not by null
isInterceptablePromise.then(function(){
debug('isIntercepting? %s', isIntercepting);
if (isIntercepting) {
// collect all the parts of a response
if (rawChunk) {
var chunk = rawChunk
if (rawChunk !== null && !Buffer.isBuffer(chunk) && encoding !== 'buffer') {
if (!encoding) {
chunk = new Buffer(rawChunk)
} else {
chunk = new Buffer(rawChunk, encoding)
}
}
chunks.push(chunk);
}
if (typeof cb === 'function') {
cb();
}
}
});
return isInterceptablePromise;
}
res.write = function(chunk, encoding, cb) {
debug('write called');
var args = arguments;
intercept(chunk,encoding).then(function(){
!isIntercepting && originalWrite.apply(res, args);
});
};
function afterSend(oldBody, newBody) {
if (typeof methods.afterSend === 'function') {
process.nextTick(function() {
debug('methods.afterSend running now, body size: %s %s', oldBody.length, newBody.length);
methods.afterSend(oldBody, newBody);
});
}
}
function isTextContentType(contentType) {
return [
/text\//,
/application\/json/,
/application\/x\-javascript/,
/application\/html/,
/application\/xml/,
].some(function(regx){
return regx.test(contentType);
});
}
res.end = function(chunk, encoding, cb) {
debug('end called');
var args = Array.prototype.slice.call(arguments);
intercept(chunk,encoding).then(function(){
if (isIntercepting) {
isIntercepting = false;
var oldBody = Buffer.concat(chunks);
if (methods.intercept) {
if (typeof methods.intercept !== 'function') {
throw new Error('`send` must be a function with the body to be sent as the only param');
}
res.removeHeader('Content-Length');
if ( isTextContentType(res.get('content-type')) ) {
oldBody = oldBody.toString('utf-8');
}
// allow the user to re-write response
methods.intercept(oldBody, function(newBody) {
args[0] = newBody;
invokeOriginalEnd.apply(res,args);
afterSend(oldBody,newBody);
});
} else {
debug(' methods.send isnt defined');
afterSend(oldBody, oldBody);
invokeOriginalEnd.apply(res, args);
}
} else {
invokeOriginalEnd.apply(res, args);
}
});
};
next();
};
};
var VALID_PARAMS = ['isInterceptable', 'intercept', 'afterSend'];
function _validateParams(methods) {
for (var k in methods) {
if (VALID_PARAMS.indexOf(k) < 0) {
throw(new Error(k+' isn\'t a valid param (' + VALID_PARAMS.join(', ') + ')'));
}
}
if (!('isInterceptable' in methods)) {
throw('isInterceptable is a required param (function)');
}
}