The future of globalPreload
#124
-
In working on nodejs/node#44710, moving loaders off-thread, I’ve run into various issues related to the Starting with just function loadQuibble () {
global.__quibble = { stubModuleGeneration: 1 }
}
export function globalPreload () {
return `(${loadQuibble.toString()})()`
} It doesn’t use More broadly, I’m wondering why we need Looking at the original PR that created
The security concerns are already much better handled by a few other in-progress APIs under https://nodejs.org/api/permissions.html. The “define things in global scope” like polyfills or helpers can be done via Of course, I’m inviting everyone to now go ahead and prove me wrong, so feel free. 😄 I think if we do need to keep cc @nodejs/loaders @JakobJingleheimer @jkrems @bmeck @giltayar |
Beta Was this translation helpful? Give feedback.
Replies: 9 comments 27 replies
-
I currently don't use But I already changed what needs to change to support "off thread" work, and you can see the So, yes: |
Beta Was this translation helpful? Give feedback.
-
In other words, and if I was not clear: if you move loaders off thread, and don't give |
Beta Was this translation helpful? Give feedback.
-
To an extent yes userland loaders could use this, but things like multi-thread coordination and head of line blocking would be pretty rough.
There is a test for this exact kind of mocking in core already at https://github.com/nodejs/node/blob/main/test/fixtures/es-module-loaders/mock-loader.mjs that relies on the message channel. It was undocumented when implemented as it isn’t something that gives any benefits unless loaders are moved to their own thread.
|
Beta Was this translation helpful? Give feedback.
-
Sloppy mode was as @ljharb stated. Polyfills. It is a hook to modify the
runtime not for module loading
…On Wed, Dec 28, 2022, 7:26 PM Geoffrey Booth ***@***.***> wrote:
Access to a bidirectional port to communicate with the worker thread.
There’s one more thing: globalPreload needs to run *for every worker
thread spawned by the user*. Because the loader thread needs to
communicate with all of them.
Let’s step back for a second, though. When globalPreload was introduced
in nodejs/node#32068 <nodejs/node#32068> in March
2020, it didn’t have a communications channel. The port helper was added
in nodejs/node#39240 <nodejs/node#39240> in July
2021. So is it fair to say that the *original* globalPreload didn’t do
anything that --import can do instead? (Leaving aside the sloppy mode
consideration, since we can’t seem to pin down why that was desired; I also
feel like it should be doable within --import, either through vm like
@jkrems <https://github.com/jkrems> suggests or maybe by ESM code
importing a CommonJS file.)
Because if --import can do everything that the injected sloppy mode
script can do, it begs the question whether an injected script is the best
way to provide a hook to a communications channel. Maybe we could leave
--import for the “inject code before user application code”
responsibility and create a new API specifically to provide a
communications channel between the loaders thread and the main thread. What
would be the downside of doing so?
As for any of this running for each worker thread spawned by the user,
that’s a whole other ball of complexity that I’m not sure we’ve ever
tested. Arguably we might want a way to run --import for each worker
thread, for example to add instrumentation or polyfills to each worker
thread, beyond just the use case of loader thread communication.
—
Reply to this email directly, view it on GitHub
<#124 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABZJI26WWUWW6MABRCOVI3WPTSD7ANCNFSM6AAAAAATLAAVJU>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
ServiceWorker is closer to loaders than that. They do not do broadcast. 1
to 1 communication is the norm there. Multi response is an issue
…On Thu, Dec 29, 2022, 7:25 AM Maël Nison ***@***.***> wrote:
There's some precedent for having a single message channel that all
consumers share and filter as needed (that's how iframes and workers
behave, through postmessage)
—
Reply to this email directly, view it on GitHub
<#124 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABZJIZCD2RYA34YUU75JA3WPWGKZANCNFSM6AAAAAATLAAVJU>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Sloppy mode and guarantee of execution on first tick.
…On Thu, Dec 29, 2022, 4:03 PM Geoffrey Booth ***@***.***> wrote:
--import would work (assuming it should run per thread, and I think it
should), but:
1. I think it is not good DX to now require *two* options to make this
kind of thing work, and.
2. You still need to find some way to communicate via a port.
Just so we don’t skip over the question, though, *does* globalPreload
provide anything that --import doesn’t other than the communication
channel? Regardless of whether we think whatever non-globalPreload option
is better UX, what would be lost if we removed the globalPreload hook
entirely and instead provided some other way of establishing a
communication channel? I’m not saying that --import plus who-knows-what
*should* be the way that that’s done; it’s just a hypothetical to tease
out what value we’re getting out of globalPreload.
—
Reply to this email directly, view it on GitHub
<#124 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABZJI3J4XYPTDSTB2GCIYTWPYDD5ANCNFSM6AAAAAATLAAVJU>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Yep but requires disk unlike import due to using paths
…On Thu, Dec 29, 2022, 4:58 PM Geoffrey Booth ***@***.***> wrote:
I think --require provides both of those?
—
Reply to this email directly, view it on GitHub
<#124 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABZJIYVHXRVF3JPEH4MNO3WPYJQ5ANCNFSM6AAAAAATLAAVJU>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
We could add a possibility to register loaders programmatically in a |
Beta Was this translation helpful? Give feedback.
-
Once we finish shaking out the bugs from moving loaders off the main thread, I think the |
Beta Was this translation helpful? Give feedback.
I currently don't use
port
because the loader works "on thread", and so I can communicate via global variables.But I already changed what needs to change to support "off thread" work, and you can see the
globalPreload
code which usesport
here: https://github.com/testdouble/quibble/blob/support-off-thread-loaders/lib/quibble.mjs#L128So, yes:
globalPreload
withport
is necessary exactly for when loaders become "off thread".