@litejs/dom 24.7.0 → 24.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +1 -1
  2. package/net.js +24 -12
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
 
2
2
  [1]: https://badgen.net/coveralls/c/github/litejs/dom
3
3
  [2]: https://coveralls.io/r/litejs/dom
4
- [3]: https://badgen.net/packagephobia/install/@litejs/dom
4
+ [3]: https://packagephobia.now.sh/badge?p=@litejs/dom
5
5
  [4]: https://packagephobia.now.sh/result?p=@litejs/dom
6
6
  [5]: https://badgen.net/badge/icon/Buy%20Me%20A%20Tea/orange?icon=kofi&label
7
7
  [6]: https://www.buymeacoffee.com/lauriro
package/net.js CHANGED
@@ -12,6 +12,7 @@ exports.XMLHttpRequest = XMLHttpRequest
12
12
  exports.defaultHeaders = {
13
13
  accept: ["Accept", "*/*"]
14
14
  }
15
+ exports.protocolHandler = {}
15
16
 
16
17
  function XMLHttpRequest() {
17
18
  this._reqHeaders = Object.assign({}, exports.defaultHeaders)
@@ -64,9 +65,9 @@ XMLHttpRequest.prototype = {
64
65
  abort: function() {
65
66
  throw Error("XMLHttpRequest abort/reuse not implemented")
66
67
  },
67
- open: function (method, url, async) {
68
+ open: function (method, url, isAsync) {
68
69
  var xhr = this
69
- if (async === false) throw Error("XMLHttpRequest sync not implemented")
70
+ xhr._sync = isAsync === false
70
71
 
71
72
  if (xhr.readyState > xhr.UNSENT) {
72
73
  xhr.abort()
@@ -79,35 +80,36 @@ XMLHttpRequest.prototype = {
79
80
  send: function (data) {
80
81
  var xhr = this
81
82
  , url = new URL(xhr.responseURL, XMLHttpRequest.base)
83
+ , proto = url.protocol.slice(0, -1)
82
84
 
83
- if (url.protocol === "http:" || url.protocol === "https:") {
85
+ if (proto === "http" || proto === "https") {
86
+ if (xhr._sync) throw Error("XMLHttpRequest sync not implemented")
84
87
  url.method = xhr.method
85
88
  url.headers = Object.keys(xhr._reqHeaders).reduce(function(result, key) {
86
89
  var entrie = xhr._reqHeaders[key]
87
90
  result[entrie[0]] = entrie[1]
88
91
  return result
89
92
  }, {})
90
- var req = require(url.protocol.slice(0, -1)).request(url, function(res) {
93
+ var req = require(proto).request(url, function(res) {
91
94
  head(res.statusCode, res.statusMessage, res.headers)
92
95
  res.on("data", fillBody)
93
96
  res.on("end", done)
94
97
  })
95
- if (xhr.onerror) req.on("error", xhr.onerror)
98
+ req.on("error", done)
96
99
  req.end(data)
97
100
  return
98
101
  }
99
- if (url.protocol === "data:") {
102
+ if (proto === "data") {
100
103
  var match = dataUrlRe.exec(url.pathname)
101
- if (!match) throw Error("Invalid URL: " + url)
102
- process.nextTick(function() {
104
+ if (match) {
103
105
  head(200, "OK", { "content-type": match[1] || "text/plain" })
104
106
  fillBody(match[2] ? Buffer.from(match[3], "base64") : unescape(match[3]))
105
107
  done()
106
- })
108
+ } else done(Error("Invalid URL: " + url))
107
109
  return
108
110
  }
109
111
 
110
- if (url.protocol === "file:") {
112
+ if (proto === "file") {
111
113
  require("fs").readFile(url, function(err, chunk) {
112
114
  if (err) {
113
115
  head(404, "Not Found", {})
@@ -120,6 +122,11 @@ XMLHttpRequest.prototype = {
120
122
  return
121
123
  }
122
124
 
125
+ if (exports.protocolHandler[proto]) {
126
+ exports.protocolHandler[proto](url, head, fillBody, done)
127
+ return
128
+ }
129
+
123
130
  throw Error("Unsuported protocol in: " + url)
124
131
 
125
132
  function head(code, text, headers) {
@@ -132,8 +139,13 @@ XMLHttpRequest.prototype = {
132
139
  xhr.responseText += chunk.toString("utf8")
133
140
  setState(xhr, xhr.LOADING)
134
141
  }
135
- function done() {
136
- setState(xhr, xhr.DONE)
142
+ function done(err) {
143
+ if (err) {
144
+ if (xhr.onerror) xhr.onerror(err)
145
+ else throw err
146
+ }
147
+ else if (xhr._sync) setState(xhr, xhr.DONE)
148
+ else process.nextTick(setState, xhr, xhr.DONE)
137
149
  }
138
150
  }
139
151
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@litejs/dom",
3
- "version": "24.7.0",
3
+ "version": "24.8.0",
4
4
  "description": "A small DOM library for server-side testing, rendering, and handling of HTML files",
5
5
  "license": "MIT",
6
6
  "author": "Lauri Rooden <lauri@rooden.ee>",