-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathnet.js
More file actions
150 lines (136 loc) · 3.61 KB
/
net.js
File metadata and controls
150 lines (136 loc) · 3.61 KB
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
/*! litejs.com/MIT-LICENSE.txt */
"use strict"
var DOM = require(".")
, URL = require("url").URL
, parser = new DOM.DOMParser()
, setState = (xhr, state) => {
if (xhr.readyState !== state) {
xhr.readyState = state
if (xhr.onreadystatechange) xhr.onreadystatechange()
if (state === xhr.DONE && xhr.onload) xhr.onload()
}
}
exports.XMLHttpRequest = XMLHttpRequest
exports.defaultHeaders = {
accept: ["Accept", "*/*"]
}
exports.protocolHandler = {}
function XMLHttpRequest() {
this._reqHeaders = Object.assign({}, exports.defaultHeaders)
}
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() {
var xhr = this
return xhr.readyState >= xhr.HEADERS_RECEIVED && Object.keys(xhr._headers).map(
name => name + ": " + xhr._headers[name] + "\r\n"
).join("") || null
},
getResponseHeader(name) {
var xhr = this
return xhr.readyState >= xhr.HEADERS_RECEIVED && xhr._headers[name.toLowerCase()] || null
},
setRequestHeader(name, value) {
this._reqHeaders[name.toLowerCase()] = [name, value]
},
abort() {
throw Error("XMLHttpRequest abort/reuse not implemented")
},
open(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(data) {
var xhr = this
, url = new URL(xhr.responseURL, XMLHttpRequest.base)
, proto = url.protocol.slice(0, -1)
, head = (code, text, headers) => {
xhr.status = code
xhr.statusText = text
xhr._headers = headers
setState(xhr, xhr.HEADERS_RECEIVED)
}
, fillBody = chunk => {
xhr.responseText += chunk.toString("utf8")
setState(xhr, xhr.LOADING)
}
, 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)
}
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((result, key) => {
var entrie = xhr._reqHeaders[key]
result[entrie[0]] = entrie[1]
return result
}, {})
var req = require(proto).request(url, 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 = /^([^;,]*?)(;[^,]+?|),(.*)$/.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, (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)
}
}