-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
net.js
153 lines (137 loc) · 3.75 KB
/
net.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
/* global unescape */
/*! litejs.com/MIT-LICENSE.txt */
var DOM = require(".")
, URL = require("url").URL
, parser = new DOM.DOMParser()
, dataUrlRe = /^([^;,]*?)(;[^,]+?|),(.*)$/
exports.XMLHttpRequest = XMLHttpRequest
exports.defaultHeaders = {
accept: ["Accept", "*/*"]
}
exports.protocolHandler = {}
function XMLHttpRequest() {
this._reqHeaders = Object.assign({}, exports.defaultHeaders)
}
function setState(xhr, state) {
if (xhr.readyState !== state) {
xhr.readyState = state
if (xhr.onreadystatechange) xhr.onreadystatechange()
if (state === xhr.DONE && xhr.onload) xhr.onload()
}
}
XMLHttpRequest.prototype = {
UNSENT: 0,
OPENED: 1,
HEADERS_RECEIVED: 2,
LOADING: 3,
DONE: 4,
readyState: 0,
status: 0,
statusText: "",
response: "",
responseText: "",
responseType: "",
responseURL: "",
get responseXML() {
var xhr = this
, mime = (xhr.getResponseHeader("Content-Type") || "").split(";")[0]
return (
xhr.readyState !== xhr.DONE ? null :
// XMLHttpRequest spec originally supported only XML
mime !== "application/xml" && xhr.responseType !== "document" ? null :
parser.parseFromString(xhr.responseText, mime)
)
},
getAllResponseHeaders: function () {
var xhr = this
return xhr.readyState >= xhr.HEADERS_RECEIVED && Object.keys(xhr._headers).map(function(name) {
return name + ": " + xhr._headers[name] + "\r\n"
}).join("") || null
},
getResponseHeader: function (name) {
var xhr = this
return xhr.readyState >= xhr.HEADERS_RECEIVED && xhr._headers[name.toLowerCase()] || null
},
setRequestHeader: function(name, value) {
this._reqHeaders[name.toLowerCase()] = [name, value]
},
abort: function() {
throw Error("XMLHttpRequest abort/reuse not implemented")
},
open: function (method, url, isAsync) {
var xhr = this
xhr._sync = isAsync === false
if (xhr.readyState > xhr.UNSENT) {
xhr.abort()
}
xhr.method = method
xhr.responseURL = url
setState(xhr, xhr.OPENED)
},
send: function (data) {
var xhr = this
, url = new URL(xhr.responseURL, XMLHttpRequest.base)
, proto = url.protocol.slice(0, -1)
if (proto === "http" || proto === "https") {
if (xhr._sync) throw Error("XMLHttpRequest sync not implemented")
url.method = xhr.method
url.headers = Object.keys(xhr._reqHeaders).reduce(function(result, key) {
var entrie = xhr._reqHeaders[key]
result[entrie[0]] = entrie[1]
return result
}, {})
var req = require(proto).request(url, function(res) {
head(res.statusCode, res.statusMessage, res.headers)
res.on("data", fillBody)
res.on("end", done)
})
req.on("error", done)
req.end(data)
return
}
if (proto === "data") {
var match = dataUrlRe.exec(url.pathname)
if (match) {
head(200, "OK", { "content-type": match[1] || "text/plain" })
fillBody(match[2] ? Buffer.from(match[3], "base64") : unescape(match[3]))
done()
} else done(Error("Invalid URL: " + url))
return
}
if (proto === "file") {
require("fs").readFile(url, function(err, chunk) {
if (err) {
head(404, "Not Found", {})
} else {
head(200, "OK", { "content-type": "text/plain" })
fillBody(chunk)
}
done()
})
return
}
if (exports.protocolHandler[proto]) {
exports.protocolHandler[proto](url, head, fillBody, done)
return
}
throw Error("Unsuported protocol in: " + url)
function head(code, text, headers) {
xhr.status = code
xhr.statusText = text
xhr._headers = headers
setState(xhr, xhr.HEADERS_RECEIVED)
}
function fillBody(chunk) {
xhr.responseText += chunk.toString("utf8")
setState(xhr, xhr.LOADING)
}
function done(err) {
if (err) {
if (xhr.onerror) xhr.onerror(err)
else throw err
}
else if (xhr._sync) setState(xhr, xhr.DONE)
else process.nextTick(setState, xhr, xhr.DONE)
}
}
}