Skip to content

Commit

Permalink
net: Handle isAsync in done so data url can be sync
Browse files Browse the repository at this point in the history
  • Loading branch information
lauriro committed Jul 10, 2024
1 parent 8b91d7b commit b5dcf8e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
6 changes: 4 additions & 2 deletions net.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ XMLHttpRequest.prototype = {
abort: function() {
throw Error("XMLHttpRequest abort/reuse not implemented")
},
open: function (method, url, async) {
open: function (method, url, isAsync) {
var xhr = this
if (async === false) throw Error("XMLHttpRequest sync not implemented")
xhr._sync = isAsync === false

if (xhr.readyState > xhr.UNSENT) {
xhr.abort()
Expand All @@ -82,6 +82,7 @@ XMLHttpRequest.prototype = {
, 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]
Expand Down Expand Up @@ -137,6 +138,7 @@ XMLHttpRequest.prototype = {
if (xhr.onerror) xhr.onerror(err)
else throw err
}
else if (xhr._sync) setState(xhr, xhr.DONE)
else process.nextTick(setState, xhr, xhr.DONE)
}
}
Expand Down
41 changes: 28 additions & 13 deletions test/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe("XMLHttpRequest", function() {
assert.throws(function() {
var xhr = new XMLHttpRequest()
xhr.open("GET", "https://litejs.com", false)
xhr.send()
})
assert.end()
})
Expand Down Expand Up @@ -93,26 +94,40 @@ describe("XMLHttpRequest", function() {
[ "data:text/javascript,console.log('a')", "text/javascript", "console.log('a')" ]
]

it("throws on invalid Data URL", function(assert) {
it("handles invalid Data URL", function(assert) {
assert.plan(2)
assert.throws(function() {
var xhr = new XMLHttpRequest()
xhr.open("GET", "data:Hello")
xhr.send()
})
assert.end()
var xhr = new XMLHttpRequest()
xhr.onerror = function(err) {
assert.ok(err)
}
xhr.open("GET", "data:Hello")
xhr.send()
})

table.forEach(function(data, i) {
it("handles data " + i, function(assert) {
var xhr = new XMLHttpRequest()
xhr.open("GET", data[0])
xhr.onload = function() {
assert.equal(xhr.getResponseHeader("content-type"), data[1])
assert.equal(xhr.responseText, data[2])
assert.end()
}
xhr.send()
})
it("handles async request {i}", table, function(url, mime, body, assert) {
var xhr = new XMLHttpRequest()
xhr.open("GET", url)
assert.equal(xhr.responseText, "")
xhr.onload = function() {
assert.equal(xhr.getResponseHeader("content-type"), mime)
assert.equal(xhr.responseText, body)
assert.end()
}
xhr.send()
})
it("handles sync request {i}", table, function(url, mime, body, assert) {
var xhr = new XMLHttpRequest()
xhr.open("GET", url, false)
xhr.send()
assert.equal(xhr.getResponseHeader("content-type"), mime)
assert.equal(xhr.responseText, body)
assert.equal(xhr.readyState, 4)
assert.end()
})
})
describe("File URLs", function() {
Expand Down

0 comments on commit b5dcf8e

Please sign in to comment.