Skip to content

Commit

Permalink
Fix sync requests for old node versions, fix server switch returns
Browse files Browse the repository at this point in the history
  • Loading branch information
YarnSaw committed Nov 1, 2024
1 parent 03b3f95 commit 917d52b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
10 changes: 6 additions & 4 deletions lib/XMLHttpRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;"
Expand Down
8 changes: 6 additions & 2 deletions tests/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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("<element><child>Foobar</child></element>");
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");
Expand Down
5 changes: 3 additions & 2 deletions tests/test-sync-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
* 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
, XMLHttpRequest = require("../lib/XMLHttpRequest").XMLHttpRequest
, 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
Expand Down

0 comments on commit 917d52b

Please sign in to comment.