Skip to content

Instantly share code, notes, and snippets.

@bmeck
Last active February 6, 2018 17:20
Show Gist options
  • Save bmeck/5b43052cb2e28bdae29c161f16f12801 to your computer and use it in GitHub Desktop.
Save bmeck/5b43052cb2e28bdae29c161f16f12801 to your computer and use it in GitHub Desktop.
Removal of path searching / defining a hook for migration.

Problem

There has been no progress in working towards a single cohesive story for path resolution between Servers and Web. Notable discussion points relevant to this are:

  1. Node has a path searching algorithm.
  2. Web has not been able to gather support for any of the following:
    1. Build tooling as part of UX expectations (lack of interest)
    2. Smarter static web servers (lack of interest). PoC example at https://github.com/bmeck/esm-http-server
    3. A resolve based hook. (interest shown with desire for ~6 months of userland experimentation)
      1. This will be assumed to exist under the existence of whatwg/html#2640 , URL.createObjectURL, and Service Workers.
      2. Rudimentary PoC (without actual integration) at https://github.com/bmeck/browser-hooking without a ServiceWorker. SW example at https://github.com/bmeck/node-sw-compat-loader-test.
  3. Node now has hooks for import
    1. Node has existing requirements for per-package hooks.
    2. Per-package hooks can be used to give a migration process towards people using features that the web is deficient in.

This proposal would seek to remove searching for index files and file extensions.

This proposal would seek to remove searching for package.json#main when importing resolves to a directory.

This proposal seeks to define a loader hook that can be encourages to be used per-package that adds the behaviors it wishes to remove.

This proposal does not seek to remove .mjs from being the canonical authorship of ESM.

Per-package loader hooks

The underpinning assumption of this proposal is a strong support for per-package loader hooks. This is a definition of the capabilities and a bikeshed for how to achieve them.

Scope of hooks

Hooks must be confined to a well defined subsection of the URL space (fs) used by import.

This proposal will define the boundaries of subsections to be:

  • A directory containing package.json will have a termination when crossing the directory.

Given the fs of:

/path-searching-hook
/foo
  /package.json
  /bar/example.mjs
/a
  /package.json
  /a.mjs
// /foo/bar/example.mjs
import '../' // does not cross boundary by resolving to `/foo`
import '../..' // does cross boundary by resolving outside of `/foo` to `/`
// /a/a.mjs
import '../foo' // does cross boundary by resolving out of `/a`
import '../foo' // does cross boundary by resolving to `/foo`

Consumer and Author negotiation

  • It must be possible as a consumer to affect the path resolved within another package's scope.
  • It must be possible as a author to affect the path resolved within the author's package scope.

In order to avoid recursive boundary crossing in one step, all paths will be resolved in two phases.

  1. External resolution that is resolved by consumers from a different package scope.
  2. Self resolution that is resolved by the package scope containing the resolved path.
// /a/a.mjs
import('/foo');

// 1. fires /a 's package scope loader hooks, seeing `/a/a.mjs` as source and `/foo` as specifier
// lets assume it resolves to /foo
// 2. fires /foos 's package scope loader hooks, seeing `/foo` as source and `./` as specifier

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.

{
  "name": "foo",
  "hooks": "../path-searching-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

  • only a resolve hook. use URL.createObjectURL or alternatives like Service Workers if you need to modify source.

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.

By only having a single resolve hook, paths can be rewritten and observed to do in-source replacement.

This is problematic however, since URL.createObjectURL 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 URL.createObjectURL reservations to a location on disk.

Problem, multiple boundary crossing

/root
  /package.json
  /entry
    /package.json
  /dep
    /package.json

If entry were to import('../dep'). It would be handled in the typical entry hooks then dep hooks manner. This does not give root a chance to intercept the imports.

This is seen as a suitable limitation since root is presumed to have ownership of entry and dep's source code by them existing within its directory. Edit the entry and dep packages as needed in order to achieve hooking that goes through root'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:

#! node --loader
module.exports = class LogImports {
  constructor(parent) {
    this.parent = parent;
  }
  async resolve(url) {
    debugger;
    const ret = await this.parent.resolve(url);
    console.log(url, 'became', ret);
    return ret;
  }
}

Example use cases for composition

  • Code Coverage
  • Instrumentation such as APM
  • Mocks/Spies in testing frameworks
  • Logging/Debugging
  • Compilation
  • Linting
  • Isolation (such as with code signing)

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 or primitives.

The parent passed to the constructor of a loader will be a limited facade that only shows white listed proprties 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.

npm start
# => node hasErrors.mjs
# aborts
export NODE_OPTIONS='--loader DebugImports'
npm start
# will log imports if HasErrors defers to the parent loader

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 or NODE_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.

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.

@medikoo
Copy link

medikoo commented Feb 4, 2018

Are you saying assets doesn't seem good, or that per package hooks do not seem good?

That using two different buckets for front-end and back-end code does not seem good. Both are different programs, but it's one language and best if for both path resolution of dependencies follows same rules.

Also, this does not work for runtime based redirection such as on process.env or navigator.lang.

Why? If those tokens needs to be resolved out of environment, then it simply can be done that way:

resolveUrl(sourcePath, dependencySpecifier, { route: [navigator.lang, 'browser']);

They are inherently linked since any source text generation output needs to be assigned a URL.

Never in our ecosystem path resolution was linked with transpilation. There was never a native support for transpilation hooks. Then why suddenly you state that both are inherently linked. I don't understand (?)

@bmeck
Copy link
Author

bmeck commented Feb 4, 2018

Why? If those tokens needs to be resolved out of environment, then it simply can be done that way

This would require that import syntax in JS be able to provide that { route: [navigator.lang, 'browser']}.

Then why suddenly you state that both are inherently linked. I don't understand (?)

It isn't sudden, all forms of compilation generate a source text that is assigned a URL if they want to be usable from ESM. The means by which these URLs have their body populated (disk, text URL, URL.createObjectURL, etc.) isn't important; but this method of being loadable by ESM is always used by any code generation or instrumentation system does this by either putting things in memory or on disk generally.

@bmeck
Copy link
Author

bmeck commented Feb 4, 2018

I think it might be easier to not think of resolution as purely a path searching thing. It can point to URLs there were generated during the resolution process. That feature cannot be removed unless all ways to reserve URLs that could be resolved is removed from the runtime and left only to the host environment. Even if it is left for the host environment, it would be very hard to prevent all ways to do things like dump a file on disk that points to new URLs using something like export * from ....

@medikoo
Copy link

medikoo commented Feb 5, 2018

This would require that import syntax in JS be able to provide that { route: [navigator.lang, 'browser']}

In my understanding we were talking about custom path resolver, that plays role in resolving paths which were put to import (a feature that may allow e.g. node-like paths resolution), and it doesn't influence in anyway what values we put to import.

e.g. for import _ from './locale.js' custom resolver may resolve ./locale.en.js (on basis of { route: [navigator.lang]} option passed to path resolver function).

It isn't sudden, all forms of compilation generate a source text that is assigned a URL if they want to be usable from ESM

Ok, and here we're talking purely about URL resolution, not about what's at source text (whether it implies transpilation step or not etc). It's two independent things. How can format of source text that's being addressed by given URL have an impact on value of given URL?

I think it might be easier to not think of resolution as purely a path searching thing.

For simplicity I believe it's what we should do. Isn't it just about mapping string token to complete URL? What about KISS and YAGNI?

That feature cannot be removed unless all ways to reserve URLs that could be resolved is removed from the runtime and left only to the host environment. Even if it is left for the host environment

Sorry I have problems understanding that statement. What exactly feature? Why it cannot be removed, can you provide some example, so I understand better?

@bmeck
Copy link
Author

bmeck commented Feb 5, 2018

e.g. for import _ from './locale.js' custom resolver may resolve ./locale.en.js (on basis of { route: [navigator.lang]} option passed to path resolver function).

Correct, which is not available at runtime since ESM resolves prior to any evaluation.

Ok, and here we're talking purely about URL resolution, not about what's at source text (whether it implies transpilation step or not etc). It's two independent things. How can format of source text that's being addressed by given URL have an impact on value of given URL?

I don't understand the question.

For simplicity I believe it's what we should do. Isn't it just about mapping string token to complete URL? What about KISS and YAGNI?

It isn't purely path searching by its very nature.

Isn't it just about mapping string token to complete URL.

No, since it also has to do a variety of other things during resolution like how to form the shape of the target Abstract Module Record which has to be declared with something out of band like MIME or file extension.

Sorry I have problems understanding that statement. What exactly feature? Why it cannot be removed, can you provide some example, so I understand better?

We can't remove URL.createObjectURL which can generate blob: URLs from the web standards. Other things that do work in the web like data: URLs are explicitly blocked in Node due to this idea of generating ESM records being really weird. In node, ensuring that a path lookup never changes over time by people writing to the file system while in path resolution. Node also has things like vm.Module that are landing which also can generate new records at runtime.

We could try to remove some of these, but you can't remove all of them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment