Skip to content

XMLHttpRequest Post times out #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
marcgrue opened this issue Dec 27, 2022 · 3 comments
Closed

XMLHttpRequest Post times out #74

marcgrue opened this issue Dec 27, 2022 · 3 comments

Comments

@marcgrue
Copy link

When importing

import org.scalajs.macrotaskexecutor.MacrotaskExecutor.Implicits._

I get the following timeout problem when doing a XMLHttpRequest Post:

/Users/mg/github/marcgrue/MacrotaskExecutor-post-test/node_modules/jsdom/lib/jsdom/living/helpers/events.js:15
  const event = createAnEvent(e, target._globalObject, eventInterface, attributes);
                                        ^

TypeError: Cannot read properties of undefined (reading '_globalObject')
    at fireAnEvent (/Users/mg/github/marcgrue/MacrotaskExecutor-post-test/node_modules/jsdom/lib/jsdom/living/helpers/events.js:15:41)
    at Timeout._onTimeout (/Users/mg/github/marcgrue/MacrotaskExecutor-post-test/node_modules/jsdom/lib/jsdom/living/post-message.js:36:7)
    at listOnTimeout (node:internal/timers:564:17)
    at process.processTimers (node:internal/timers:507:7)

Node.js v19.3.0

Client code:

// Fails
import org.scalajs.macrotaskexecutor.MacrotaskExecutor.Implicits._

// Works
//import scala.concurrent.ExecutionContext.Implicits.global

lazy val post: Future[String] = {
  val req     = new dom.XMLHttpRequest()
  val promise = Promise[dom.XMLHttpRequest]()
  req.onreadystatechange = { (_: dom.Event) =>
    if (req.readyState == 4) {
      if ((req.status >= 200 && req.status < 300) || req.status == 304) {
        promise.success(req)
      } else {
        promise.failure(new Exception(req.toString))
      }
    }
  }
  req.open("POST", "http://localhost:8080/post/test")
  req.timeout = 0
  req.setRequestHeader("Content-Type", "application/json")
  req.send()
  promise.future.map(_.response.asInstanceOf[String])
}

lazy val tests = Tests {
  "exception test" - {
    for {
      _ <- post.map(_ ==> "hello world")
    } yield ()
  }
}

Server code:

  implicit val system           = ActorSystem(Behaviors.empty, "actorSystem")
  implicit val executionContext = system.executionContext

  Http()
    .newServerAt("localhost", 8080)
    .bind {
      cors() {
        path("post" / "test").apply {
          post {
            extractRequest { _ =>
              complete("hello world")
            }
          }
        }
      }
    }
    .onComplete {
      case Success(b) => println(s"Ajax server is running ${b.localAddress} ")
      case Failure(e) => println(s"there was an error starting the server $e")
    }

Switching back to the standard import

import scala.concurrent.ExecutionContext.Implicits.global

solves the problem (but creates the other usual problems that is warned about).

Here's a minimal reproduction project with the above code:
https://github.com/marcgrue/MacrotaskExecutor-post-test

Shouldn't the macrotaskExecutor work for this scenario?

@armanbilge
Copy link
Member

Thanks for the report. This seems to be an issue specific to JSDOM when used with Node.js 19.

Perhaps try Node.js 18 (the current long-term-support version)?

@armanbilge
Copy link
Member

Ah, I was able to replicate this issue in CI with Node.js 18 as well :( sorry.

@marcgrue
Copy link
Author

No problem :-) I've been trying to install Node.js 18 for the project without success. Also, jsdom is at version 20.0.3. Could downgrade jsdom with npm install jsdom@18. But when I try npm install node@18 it still turns up as version 19 at runtime (also tried installing 18 with nvm, but still at 19). Anyway, maybe this doesn't matter so much, since the problem persists across multiple versions.

Thanks for looking into this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants