This repository has been archived by the owner on Feb 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
217 lines (173 loc) · 5.13 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
var EventEmitter = require('events').EventEmitter;
var inherits = require('util').inherits;
var zlib = require('zlib');
var forEach = require('lodash').forEach;
var Mitm = require('mitm');
function FauxJax() {
this._installed = false;
}
inherits(FauxJax, EventEmitter);
FauxJax.prototype.install = function(opts) {
if (opts && opts.gzip) {
this._gzip = opts.gzip;
}
if (this._installed) {
this.emit('error', new Error('faux-jax: Cannot call `install()` twice. Did you forgot to call `restore()`?'));
return;
}
this._installed = true;
this._mitm = new Mitm();
this._mitm.enable();
this._mitm.on('request', this._newRequest.bind(this));
this._mitm.on('connect', this._newSocket.bind(this));
};
FauxJax.prototype.restore = function() {
if (!this._installed) {
this.emit('error', new Error('faux-jax: Cannot call `restore()` when not installed'));
return;
}
this._stopWaiting();
this._installed = false;
this._mitm.disable();
this.removeAllListeners('request');
this.emit('restore');
};
FauxJax.prototype.waitFor = function(n, callback) {
var fj = this;
var fakeRequests = [];
this.on('request', waitFor);
function waitFor(fakeRequest) {
fakeRequests.push(fakeRequest);
if (fakeRequests.length === n) {
fj.removeListener('request', waitFor);
callback(null, fakeRequests);
}
}
};
// specific Node.JS implementation, can be used to
// socket.emit('error') which will then be
FauxJax.prototype._newSocket = function(socket) {
this.emit('socket', socket);
};
FauxJax.prototype._newRequest = function(req, res) {
if (this.listeners('request').length === 0) {
this.emit('error', new Error('faux-jax: received an unexpected request: ' + req.headers.host + req.url));
return;
}
var fj = this;
var chunks = [];
var protocol = req.socket.encrypted === true ? 'https:' : 'http:';
var fakeRequest = new FakeRequest({
requestMethod: req.method,
// cannot detect http from https for now,
// https://github.com/moll/node-mitm/issues/10
// so we default to http
requestURL: protocol + '//' + req.headers.host + req.url,
requestHeaders: req.headers,
requestBody: null,
res: res,
fj: fj,
gzip: this._gzip
});
res.once('finish', function() {
fj._stopWaiting();
fakeRequest._clearTimeout();
});
req.on('end', function() {
if (chunks.length > 0) {
fakeRequest.requestBody = Buffer.concat(chunks).toString();
}
// wait for a response, this simulates a timer waiting for a server response
// not needed in the browser but in nodejs, we need to have it otherwise the process
// will exits
fj._wait();
// this is used to mimick nodejs default READ timeout (2 minutes on Linux)
fakeRequest._setTimeout(2 * 60 * 1000);
fj.emit('request', fakeRequest);
});
req.on('data', function(chunk) {
chunks.push(chunk);
});
};
FauxJax.prototype._wait = function() {
clearInterval(this._interval);
this._interval = setInterval(function noop() {}, 20000);
};
FauxJax.prototype._stopWaiting = function() {
clearInterval(this._interval);
};
function FakeRequest(opts) {
this._fj = opts.fj;
this._res = opts.res;
this.requestMethod = opts.requestMethod;
this.requestURL = opts.requestURL;
this.requestHeaders = opts.requestHeaders;
this.requestBody = opts.requestBody;
this._gzip = opts.gzip;
}
FakeRequest.prototype.setResponseHeaders = function(headers) {
var fk = this;
forEach(headers, function(headerValue, headerName) {
fk._res.setHeader(headerName, headerValue);
});
};
FakeRequest.prototype.setResponseBody = function(body, cb) {
var res = this._res;
if (this._gzip === true) {
zlib.gzip(body, write);
return;
}
process.nextTick(function() {
write(null, body);
});
function write(err, bodyToWrite) {
if (err) {
throw err;
}
if (bodyToWrite !== undefined) {
res.write(bodyToWrite, cb);
return;
}
cb();
}
};
FakeRequest.prototype.respond = function(statusCode, headers, body) {
var res = this._res;
var fj = this._fj;
res.statusCode = statusCode;
if (this._gzip === true) {
this.setResponseHeaders({'content-encoding': 'gzip'});
}
if (headers) {
this.setResponseHeaders(headers);
}
if (body !== undefined) {
this.setResponseBody(body, end);
return;
}
process.nextTick(end);
function end() {
res.end();
fj.emit('response-end');
try {
res.socket.emit('end');
} catch (e) {
// empty on purpose, can break in old node.js versions
}
}
};
FakeRequest.prototype._setTimeout = function(ms) {
this._timeout = setTimeout(this._timedout.bind(this), ms);
this._timeoutListener = this._clearTimeout.bind(this);
this._fj.once('restore', this._timeoutListener);
this._fj.once('response-end', this._timeoutListener);
};
FakeRequest.prototype._timedout = function() {
this.emit('error', new Error('faux-jax: socket hang up'));
};
FakeRequest.prototype._clearTimeout = function() {
clearTimeout(this._timeout);
this._fj.removeListener('restore', this._timeoutListener);
this._fj.removeListener('response-end', this._timeoutListener);
};
module.exports = new FauxJax();