Skip to content

Import assertions with loaders #39

Open
@aduh95

Description

@aduh95

Here's a draft of what solution I can think of regarding relations between loaders and import assertions.

Relation of ESMLoader class with import assertions:

Before resolve() resolve() Between resolve() and load() load() After load()
Check if importAssertions.type value is supported nothing use importAssertions.type to check for race conditions nothing check if finalFormat is compatible with importAssertions.type
nothing pass importAssertions to resolve() check if importAssertions.type value is supported, and use it to check for race conditions nothing check if finalFormat is compatible with importAssertions.type
nothing pass importAssertions to resolve() disallow race conditions?1 nothing check if finalFormat is compatible with importAssertions.type
nothing pass importAssertions to resolve() one of the above pass importAssertions to load() check if importAssertions.type value is supported and compatible with finalFormat
nothing pass importAssertions to resolve() one of the above pass importAssertions to load() and let load() do the checks nothing
nothing nothing use importAssertions.type to check for race conditions pass importAssertions to load() and let load() do the checks nothing
nothing nothing nothing pass importAssertions to load() and let load() do the checks nothing
  1. The first one is the approach on my first Unflag import assertions PR, drawbacks of this approach have been discussed.
  2. The second seems really appealing to me, it would allow folks to write hooks to support assertion-less JSON, and also support import assertions that are not supported by Node.js.
  3. Same thing except the loader throws if a URL is loaded with different assertions. It puts the burden of handling race conditions to the resolve hook. Not sure this is actually doable and/or desirable.
  4. pass the output importAssertions of resolve() to load() context. This adds complexity, not sure it's worth it.
  5. Same but now the loader hook is in charge of doing the verification, so users can opt-out. That seems... dangerous? but maybe it's actually OK?
  6. Same but import assertions are not passed to resolve().

My (new) favourite is 2.: it seems to me like the perfect balance between simplicity and letting users do powerful things (e.g.: it would be quite easy to implement a loader that supports assertionless imports). I have a PR almost ready implementing this.

Anyway, we can discuss that on next week meeting since Goeffrey invited me, but happy to hear thoughts before that if people want to share them.

Footnotes

  1. if the resolver returns an url that's already in the cache and the returned importAssertion.type has a value different from the one in the cache, throw a TypeError.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions