-
Notifications
You must be signed in to change notification settings - Fork 30k
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
RFC: Per Package Loader Hooks #18233
Comments
It would be better to store all ESM info in: a single |
@mcollina I've looked at allowing |
Is the release of official ES module support certain to include this functionality? The particular mechanism used to interact with the module loading isn't important, just the ability to interact in the loading. I'm looking into how one might load APM instrumentation into ES modules, and this level of support would be awesome (it is way better than the work arounds I've been able to come up with). |
@lykkin APMs would probably use the existing APM example workflow and a global loader ( |
We spent some time experimenting with using loader hooks for instrumenting external modules, but weren't able to find a reasonable solution. Our test case involved a bare-bones express app (with express'
|
@lykkin as stated in the JS spec, the module namespace object should not be mutable. So, I'm not sure your idea with |
@lykkin we should setup a call probably before discussing adding hooks, since that might apply to both per package and global hooks, which are separate things. |
@bmeck The A call sounds super helpful, I've been trying to read up on the discussion going on around this, though it sounds like there are a bunch of points I'm missing. |
@lykkin very interesting feedback. Would you be interested in proposing a postLoad hook further here for Node? |
@guybedford It sounds like there would have to be a case made upstream to v8 about exposing modules as they are being loaded, though there are also other interfaces that could be implemented to get the same functionality (e.g. an instrumentation API in v8 where you can register hooks on a module's methods and it will export the relevant data to a listener). Having a hook in v8 for doing these kinds of things would be helpful for sure, but it sounds like I need to know more about v8's goals in this area to make a proper proposal. Would you know where I can read more about that? |
@lykkin I wonder how far we might get with a translation approach something like: instrument (moduleName: string) {
return {
exports: ['x', 'y', 'p'],
instrument (exportName, value) {
return wrap(exportName, value);
}
}
} export var x = 5;
export const y= 10;
export function p () {
return ++x;
}
// this source is added by the instrumentation hook
x = doInstrument(x);
p = doInstrument(p);
y = doInstrument(y); The only problem with the above would be At least using these hacks we could possibly explore the space somewhat. v8 API hooks would be the ideal certainly though, we'd just need to have a pretty good idea of what we'd want for Node and why before pushing I think? |
(sorry @bmeck for going off-topic here, perhaps we should start a new thread) |
Is this issue still relevant? There was a lot ongoing since this issue was opened. |
@BridgeAR deferred while other things still are being looked into, but yes this is still being discussed though this proposal likely needs some updating |
@guybedford I want to re-re-revisit this. |
@bmeck I'm still quite strongly against arbitrary hooks, so would prefer specific per-package features that are justified by direct use cases over arbitrary resolution hooking, and only arbitrary hooks where the limits of a more specific method are really being hit. The reason being that package invariants are much easier to handle with generic resolution rules that can be applied. |
The general problem with using loaders due to it being a CLI flag and also being global and the persistent leaning on loaders to solve various use cases is recurring throughout multiple years. I think claiming there isn't a justification is a bit much at this point. |
Per-package loader hooks
This proposal seeks strong support for per-package loader hooks.
This is a definition of how to achieve them.
Problem
Individual application and packages have loading considerations that vary. The ability to globally mutate the Node module system is problematic and causes packages to alter each other's behavior implicitly.
CommonJS had various abilities to mutate the CJS loader with
NODE_PATH
,require.extentsion
, etc. These have all been deprecated.Various workarounds to these use cases do exist but do not apply to all code using these patterns.
Example Use Cases
Proposal
Scope of hooks
Hooks must be confined to a well defined subsection of the URL space (
fs
) used byimport
.This proposal will define the boundaries of subsections to be:
Given the
fs
of:Consumer and Author negotiation
In order to avoid recursive boundary crossing in one step, all paths will be resolved in two phases. This is similar to
Declaration of hooks
Per package loader hooks can be declared in a
package.json
file as a specifier to find using the globally defined resolution algorithm.Global hooks may affect this resolution, but package hooks may not.
This allows code coverage, instrumentation, etc. to access package hooks.
This also allows the hooks to exist outside of package boundaries. This file when loaded as a loader will be in a separate Module Map space from userland and only has the globally defined resolution algorithm.
Types of hooks
vm.Module
to obtain a new URL if you need to create Module records dynamically.On the nature of static resolution
ESM is able to link statically and there should be a path to allow static / ahead of time usage of per package hooks ideally.
By only having a single
resolve
hook, paths can be rewritten and observed to do in-source replacement.This is problematic however, since
vm.Module
lives in memory.Usage of such APIs on platforms without writable
fs
like Heroku should have a path forward for these hooks.I recommend a combination of V8's SnapshotCreator when possible, and a flag to allow rewriting
vm.Module
reservations to a location on disk.Problem, multiple boundary crossing
If
entry
were toimport('../dep')
. It would be handled in the typicalentry
hooks thendep
hooks manner. This does not giveroot
a chance to intercept the imports.This is seen as a suitable limitation since
root
is presumed to have ownership ofentry
anddep
's source code by them existing within its directory. Edit theentry
anddep
packages as needed in order to achieve hooking that goes throughroot
's use cases.Composition
Hooks should have a means by which to achieve composition. This is needed for cases of multiple transformations. A package might seek to call a
super
of sorts to get the result of a parent loader, and it may seek to do the exact opposite as a guard to ensure expected behavior.Loaders therefore need to have a concept of a parent loader hooks to defer to, or to ignore.
Changing hook allocation to be done using
new
and providing the parent as a paremeter is sufficient for this:Example use cases for composition
Isolation
Hooks that are composed still are isolated by per-package boundaries. Nested packages will not fire the
parent
loader hooks unless they cross into a package boundary with those hooks.Passing arbitrary data between instances can be problematic for both isolation and threading. Therefore the only data passed between instances of loaders will be transferables (including structured clone algorithm) or primitives.
The
parent
passed to the constructor of a loader will be a limited facade that only shows white listed properties and calls the relevant method on the true parent instance. It will ensure errors are thrown if given improper arguments length and/or non-transferable data.Per-package composition
Can be achieved by manually constructing the chain inside their per-package hook code.
Global composition
Can be achieved by providing multiple
--loader
flags. This allows for better debugging when development loaders need to be added. The full design of this is left to another RFC.Ignoring parents
In certain scenarios a package may need to ignore the parent loader. In those situations the hooks will be unable to defer to the default global behavior of the process, which may provide debugging behavior such as logging/code coverage/linting/etc.
For now escape hatches are punted on this design space to userland, but it is recommended that when using
NODE_ENV=development
orNODE_ENV=test
all loaders defer to the parent loader.Code signing invariant implications
Mutating the code loaded in a code signed bundle is problematic. Integrity checks of unexpectedly mutated imports should fail. This area needs more research. Use of any sort of in-source translation should be avoided.
Future research
Given the problems of ignoring scripts and code signing being unable to easily defer to parent loaders more design needs to be done around development workflows. Inspector tooling is the recommended approach. This may mean adding special hooks to inject loader hooks during development via a flag such as
--inspector-loader-hooks=LogImport
that may fire before per package hooks but ensures the inspector is running. Such hooks would not be suitable for production environments.This design also does not instrument CJS as loaders currently are not able to instrument CJS. It is not a design goal of this specification to add CJS support to ESM loaders; however, any design for CJS loaders that is presented should and will be considered for compatibility reasons.
The text was updated successfully, but these errors were encountered: