testdouble / quibble

Makes it easy to replace require'd dependencies.
94 stars 25 forks source link

Support node 20 #96

Closed giltayar closed 1 year ago

giltayar commented 1 year ago

Fixes #95

Node.js 20 moved the loaders off the main thread, to their own thread. Usually this is not a problem for loaders, but quibble has an API that communicates with the loader itself. In versions prior to 20, this communication was done using global variables, but since the loaders are in a separate worker/thread, then this is not possible (workers/threads do not share global variables)

The only option for communication is to use messages between the main thread and the worker threads. Luckily, the loader team gave loaders the ability to do just that.

This PR enables the loader API to communicate with the loader using messages, if the loaders are off thread, and reverts to the previous method (global variables) if the loaders are in the main thread.

I took the liberty of removing crud related to ESM loaders in past versions. Also, the Github action now checks the current versions of Node, which are 16, 18, and 20.

I have also cleaned up the code and made Windows support easier by making all ESM code use URLs instead of file paths, because 1) that is the native language ESM speaks, 2) It is compatible with Windows, and 3) now that we can have HTTP (and other) urls as import specifiers, then we are not allowed to assume that the files are on disk!

giltayar commented 1 year ago

This also fixes https://github.com/testdouble/testdouble.js/issues/513

giltayar commented 1 year ago

Hmm... Windows not working. Luckily, I have a Window machine now, so checking...

giltayar commented 1 year ago

@searls sorry about the mess. The work on the Windows side (and a good night's sleep 😊 made me realize the code's design is problematic. I will probably spend today on some level of refactoring and get it to work correctly. So I'm guessing the above comments won't really have any meaning once I finish refactoring and making it work on both platforms, but I promise I will go over them once its done.

BTW, there are two main problems I see on the windows side:

  1. Windows/Unix paths vs file: URLs.
  2. It seems that during initialization, the order things happen is different between Windows and Linux.

Luckily I have windows machine with both WSL and Windows, so I can navigate back and forth between them easily.

giltayar commented 1 year ago

Major refactoring after it failed (spectacularly!) in Windows. The code is MUCH improved, and Windows compatibility should be much better going forward, because now all ESM code deals with URLs and not file paths (see more information about this in the PR description)

Ready for re-review!

searls commented 1 year ago

Released as quibble@0.7.0 and testdouble@3.18.0

DenyVeyten commented 12 months ago

It looks like this PR drops node 14 support intentionally. I was trying to use quibble as a loader to mock esm dependencies for node 14-20, but now see that testdouble itself supports node 16+. Any chance you can bring back node 14 support at least for quibble? :)

searls commented 12 months ago

Node 14 is EOL since this April, and I can tell you from watching @giltayar work that getting ESM to function across 14, 16, 18, and 20 has required an incredible amount of patching and branching. Please expect this library to aggressively drop support for EOL'd versions so that we can keep the codebase simple and maintainable