-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathintercept.js
139 lines (105 loc) · 3.52 KB
/
intercept.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
var HTTP = require("http");
var Utility = require("lodash");
HTTP.IncomingMessage.prototype.intercept = function(callback) {
var self = this;
if(self.intercepted) {
throw new Error("Intercepted already!");
}
// Buffer to contain original data:
var buffer = new Buffer(0);
var interception = {
buffer: null
};
self.emit = Utility.wrap(self.emit, function(original, event) {
if(event === "data") {
var chunk = arguments[2];
buffer = Buffer.concat([buffer, Buffer.isBuffer(chunk) ? new Buffer(chunk) : chunk]);
}
else if(event === "end") {
interception.buffer = new Buffer(buffer.length);
buffer.copy(interception.buffer);
try {
// Give observers the opportunity to change things:
original.call(self, "interception", interception);
// Adjust "content-length" header accordingly to buffer's contents:
self.headers["content-length"] = (interception.buffer || "").length;
original.call(self, "data", interception.buffer);
}
catch(exception) {
console.error(exception.stack);
// If something went wrong during the interception let's send the original data:
original.call(self, "data", buffer);
}
original.apply(self, Array.prototype.slice.call(arguments, 1));
}
else {
// Any other call to "emit" is simply passed through:
original.apply(self, Array.prototype.slice.call(arguments, 1));
}
});
self.intercepted = true;
if(callback) {
self.on("interception", callback);
}
return self;
};
HTTP.ServerResponse.prototype.intercept = function(callback) {
var self = this;
if(self.intercepted) {
throw new Error("Intercepted already!");
}
// Buffer to contain original data:
var buffer = new Buffer(0);
var interception = {
buffer: null,
reasonPhrase: ""
};
var original = Utility.pick(self, "writeHead", "write", "end");
// TODO: there's probably something to do with "addTrailers" also but we won't bother for now...
self.writeHead = function(statusCode/*, reasonPhrase, headers*/) {
self.statusCode = statusCode;
var reasonIsPresent = (typeof arguments[1] === "string");
interception.reasonPhrase = reasonIsPresent ? arguments[1] : "";
var headers = reasonIsPresent ? arguments[2] : arguments[1];
if(headers) {
for(var name in headers) {
if(headers.hasOwnProperty(name)) {
self.setHeader(name, headers[name]);
}
}
}
};
self.write = function(chunk) {
if(chunk && chunk.length) {
buffer = Buffer.concat([buffer, Buffer.isBuffer(chunk) ? chunk : new Buffer(chunk)]);
}
return true;
};
self.end = function(chunk, encoding) {
self.write(chunk, encoding);
Utility.merge(self, original);
interception.buffer = new Buffer(buffer.length);
buffer.copy(interception.buffer);
try {
// Give observers the opportunity to change things:
self.emit("interception", interception);
}
catch(exception) {
console.error(exception.stack);
self.statusCode = 500;
interception.reasonPhrase = (process.env.NODE_ENV != "production") ? "Interception Failed" : HTTP.STATUS_CODES[500];
// TODO: not sure if this is the best behavior. Maybe we could keep track of the original headers also and send the response as it should have been sent without interception?
interception.buffer = buffer;
}
if(!self.finished) {
self.setHeader("Content-Length", interception.buffer ? interception.buffer.length : 0);
self.writeHead(self.statusCode, interception.reasonPhrase || "");
self.end(interception.buffer || "");
}
};
self.intercepted = true;
if(callback) {
self.on("interception", callback);
}
return self;
};