From 917d52b9928b1447fd1148adb262e4f224f2945e Mon Sep 17 00:00:00 2001 From: YarnSaw Date: Thu, 31 Oct 2024 21:24:49 -0400 Subject: [PATCH] Fix sync requests for old node versions, fix server switch returns --- lib/XMLHttpRequest.js | 10 ++++++---- tests/server.js | 8 ++++++-- tests/test-sync-response.js | 5 +++-- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/XMLHttpRequest.js b/lib/XMLHttpRequest.js index 729f45f..372d3c5 100644 --- a/lib/XMLHttpRequest.js +++ b/lib/XMLHttpRequest.js @@ -615,15 +615,17 @@ function XMLHttpRequest(opts) { var syncFile = ".node-xmlhttprequest-sync-" + process.pid; fs.writeFileSync(syncFile, "", "utf8"); // The async request the other Node process executes - var execString = "var http = require('http'), https = require('https'), fs = require('fs');" + var execString = "'use strict';" + + "var http = require('http'), https = require('https'), fs = require('fs');" + "function concat(bufferArray) {" + " let length = 0, offset = 0;" + " for (let k = 0; k < bufferArray.length; k++)" + " length += bufferArray[k].length;" + " const result = Buffer.alloc(length);" - + " for (let k = 0; k < bufferArray.length; k++)" - + " {" - + " result.set(bufferArray[k], offset);" + + " for (let k = 0; k < bufferArray.length; k++) {" + + " for (let i = 0; i < bufferArray[k].length; i++) {" + + " result[offset+i] = bufferArray[k][i]" + + " }" + " offset += bufferArray[k].length;" + " }" + " return result;" diff --git a/tests/server.js b/tests/server.js index ac8bd55..4649091 100644 --- a/tests/server.js +++ b/tests/server.js @@ -6,22 +6,26 @@ var server = http.createServer(function (req, res) { case "/text": res.writeHead(200, {"Content-Type": "text/plain"}) res.end("Hello world!"); + return; case "/xml": res.writeHead(200, {"Content-Type": "application/xml"}) res.end("Foobar"); + return; case "/json": res.writeHead(200, {"Content-Type": "application/json"}) res.end(JSON.stringify({ foo: "bar" })); + return; case "/binary1": res.writeHead(200, {"Content-Type": "application/octet-stream"}) res.end(Buffer.from("Hello world!")); - case "/binary2": { + return; + case "/binary2": const ta = new Float32Array([1, 5, 6, 7]); const buf = Buffer.from(ta.buffer); const str = buf.toString('binary'); res.writeHead(200, {"Content-Type": "application/octet-stream"}) res.end(str); - } + return; default: res.writeHead(404, {"Content-Type": "text/plain"}) res.end("Not found"); diff --git a/tests/test-sync-response.js b/tests/test-sync-response.js index 82b0a67..316027e 100644 --- a/tests/test-sync-response.js +++ b/tests/test-sync-response.js @@ -2,6 +2,7 @@ * Test GET http URL with both async and sync mode. * Use xhr.responseType = "" and "arraybuffer". */ +'use strict'; var assert = require("assert") , spawn = require('child_process').spawn @@ -9,9 +10,9 @@ var assert = require("assert") , serverProcess; const supressConsoleOutput = true; -function log (...args) { +function log (_) { if ( !supressConsoleOutput) - console.debug(...args); + console.log(arguments); } // Running a sync XHR and a webserver within the same process will cause a deadlock