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
/
Copy pathbrowser.js
114 lines (88 loc) · 2.56 KB
/
browser.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
var EventEmitter = require('events').EventEmitter;
var inherits = require('util').inherits;
var XMLHttpRequestMock = require('./lib/XMLHttpRequest/');
var XDomainRequestMock = require('./lib/XDomainRequest/');
var native = require('./lib/native');
var support = require('./lib/support');
function FauxJax() {
this._installed = false;
}
inherits(FauxJax, EventEmitter);
FauxJax.prototype.install = function() {
if (this._installed) {
this.emit('error', new Error('faux-jax: Cannot call `install()` twice. Did you forgot to call `restore()`?'));
return;
}
this._installed = true;
// only modify the writable state of XMLHttpRequest in old ies when installing
// it will be done only once
require('./lib/make-native-implementations-writable')();
if (this.support.xhr) {
global.XMLHttpRequest = FakeXHR;
}
if (this.support.xdr) {
global.XDomainRequest = FakeXDR;
}
};
FauxJax.prototype.restore = function() {
if (!this._installed) {
this.emit('error', new Error('faux-jax: Cannot call `restore()` when not installed'));
return;
}
if (support.xhr) {
global.XMLHttpRequest = native.XMLHttpRequest;
}
if (support.xdr) {
global.XDomainRequest = native.XDomainRequest;
}
this.removeAllListeners('request');
this._installed = false;
};
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);
}
}
};
FauxJax.prototype._newRequest = function(fakeRequest) {
if (this.listeners('request').length === 0) {
this.emit('error', new Error('faux-jax: received an unexpected request: ' + fakeRequest.requestURL));
return;
}
this.emit('request', fakeRequest);
};
FauxJax.prototype.support = support;
var fauxJax = new FauxJax();
function FakeXHR() {
XMLHttpRequestMock.call(this);
}
inherits(FakeXHR, XMLHttpRequestMock);
FakeXHR.prototype.send = function() {
var req = this;
XMLHttpRequestMock.prototype.send.apply(req, arguments);
if (req.async) {
setTimeout(function() {
fauxJax._newRequest(req);
});
} else {
fauxJax._newRequest(req);
}
};
function FakeXDR() {
XDomainRequestMock.call(this);
}
inherits(FakeXDR, XDomainRequestMock);
FakeXDR.prototype.send = function() {
var req = this;
XDomainRequestMock.prototype.send.apply(req, arguments);
setTimeout(function() {
fauxJax._newRequest(req);
});
};
module.exports = fauxJax;