From 9c75a6934fc042ccaedd13600066924f506d1135 Mon Sep 17 00:00:00 2001 From: Lauri Rooden Date: Thu, 4 Jul 2024 10:47:49 +0300 Subject: [PATCH] net: Use older api for Node 6 --- .github/workflows/test.yml | 1 + net.js | 29 +++++++++++++++++------------ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a603c24..8ae8e28 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 } diff --git a/net.js b/net.js index 6135f4a..663b0da 100644 --- a/net.js +++ b/net.js @@ -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() { @@ -104,9 +109,9 @@ XMLHttpRequest.prototype = { }) return } - - if (url.protocol === "file:") { - require("fs").readFile(url, function(err, chunk) { + if (proto === "file") { + console.log("READ", xhr.responseURL, XMLHttpRequest.base, url) + require("fs").readFile(url.pathname.slice(1), function(err, chunk) { if (err) { head(404, "Not Found", {}) } else {