-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv-xhr.js
55 lines (48 loc) · 1.26 KB
/
env-xhr.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
var fs = require("fs");
XMLHttpRequest = function() {
var self = this,
info = self._info = {},
headers = {},
url;
// TODO handle file system errors?
self.open = function(m, u, a) {
info.url = u;
info.async = a;
self.send = a ? read : readSync;
};
self.setRequestHeader = function(n, v) {
if (/^Accept$/i.test(n)) info.mimeType = v;
};
function read() {
fs.readFile(info.url, "binary", function(e, d) {
if (e) {
self.status = 404; // assumed
} else {
self.status = 200;
self.responseText = d;
self.responseXML = {_xml: d};
headers["Content-Length"] = d.length;
}
self.readyState = 4;
XMLHttpRequest._last = self;
if (self.onreadystatechange) self.onreadystatechange();
});
}
function readSync() {
try {
var d = fs.readFileSync(info.url, "binary");
self.status = 200;
self.responseText = d;
self.responseXML = {_xml: d};
headers["Content-Length"] = d.length;
} catch (e) {
self.status = 404; // assumed
}
self.readyState = 4;
XMLHttpRequest._last = self;
if (self.onreadystatechange) self.onreadystatechange();
}
self.getResponseHeader = function(n) {
return headers[n];
};
};