Skip to content

Commit

Permalink
net: Use older api for Node 6
Browse files Browse the repository at this point in the history
  • Loading branch information
lauriro committed Jul 4, 2024
1 parent 6c173ce commit c38e473
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
strategy:
matrix:
include:
- { node: 6 , os: ubuntu-latest, arch: x64 }
- { node: 8, os: windows-latest, arch: x86 }
- { node: 12, os: macos-latest, arch: x64 }
- { node: 16, os: ubuntu-latest, arch: x64 }
Expand Down
28 changes: 16 additions & 12 deletions net.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,27 @@ XMLHttpRequest.prototype = {
send: function (data) {
var xhr = this
, url = new URL(xhr.responseURL, XMLHttpRequest.base)
, proto = url.protocol.slice(0, -1)

if (url.protocol === "http:" || url.protocol === "https:") {
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
}, {})
require(url.protocol.slice(0, -1)).request(url, function(res) {
if (proto === "http" || proto === "https") {
require(proto).request({
method: xhr.method,
host: url.host,
port: url.port,
path: url.path,
headers: Object.keys(xhr._reqHeaders).reduce(function(result, key) {
var entrie = xhr._reqHeaders[key]
result[entrie[0]] = entrie[1]
return result
}, {})
}, function(res) {
head(res.statusCode, res.statusMessage, res.headers)
res.on("data", fillBody)
res.on("end", done)
}).end(data)
return
}
if (url.protocol === "data:") {
if (proto === "data") {
var match = dataUrlRe.exec(url.pathname)
if (!match) throw Error("Invalid URL: " + url)
process.nextTick(function() {
Expand All @@ -104,9 +109,8 @@ XMLHttpRequest.prototype = {
})
return
}

if (url.protocol === "file:") {
require("fs").readFile(url, function(err, chunk) {
if (proto === "file") {
require("fs").readFile(url.pathname, function(err, chunk) {
if (err) {
head(404, "Not Found", {})
} else {
Expand Down

0 comments on commit c38e473

Please sign in to comment.