Commit Graph

404 Commits

Author SHA1 Message Date
Joyee Cheung
2863c54257 module: cache synchronous module jobs before linking
So that if there are circular dependencies in the synchronous
module graph, they could be resolved using the cached jobs.
In case linking fails and the error gets caught, reset the
cache right after linking. If it succeeds, the caller will
cache it again. Otherwise the error bubbles up to users,
and since we unset the cache for the unlinkable module
the next attempt would still fail.

PR-URL: https://github.com/nodejs/node/pull/52868
Fixes: https://github.com/nodejs/node/issues/52864
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
2024-05-09 00:25:14 +00:00
Gabriel Bota
22cb99d073
module: have a single hooks thread for all workers
PR-URL: https://github.com/nodejs/node/pull/52706
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
2024-05-08 02:20:48 +00:00
Joyee Cheung
4d59a9deda
module: support ESM detection in the CJS loader
This patch:

1. Adds ESM syntax detection to compileFunctionForCJSLoader()
  for --experimental-detect-module and allow it to emit the
  warning for how to load ESM when it's used to parse ESM as
  CJS but detection is not enabled.
2. Moves the ESM detection of --experimental-detect-module for
  the entrypoint from executeUserEntryPoint() into
  Module.prototype._compile() and handle it directly in the
  CJS loader so that the errors thrown during compilation *and
  execution* during the loading of the entrypoint does not
  need to be bubbled all the way up. If the entrypoint doesn't
  parse as CJS, and detection is enabled, the CJS loader will
  re-load the entrypoint as ESM on the spot asynchronously using
  runEntryPointWithESMLoader() and cascadedLoader.import(). This
  is fine for the entrypoint because unlike require(ESM) we don't
  the namespace of the entrypoint synchronously, and can just
  ignore the returned value. In this case process.mainModule is
  reset to undefined as they are not available for ESM entrypoints.
3. Supports --experimental-detect-module for require(esm).

PR-URL: https://github.com/nodejs/node/pull/52047
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2024-04-29 20:21:53 +00:00
Nicolò Ribaudo
07f481cfcf
esm: drop support for import assertions
This patch removes support for the `assert` keyword
for import attributes. It was an old variant of the
proposal that was only shipped in V8 and no other
engine, and that has then been replaced by the `with`
keyword.

Chrome is planning to remove support for `assert`
in version 126, which will be released in June.

Node.js already supports the `with` keyword for
import attributes, and this patch does not change that.

PR-URL: https://github.com/nodejs/node/pull/52104
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io>
Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
2024-04-19 00:01:24 +00:00
Joyee Cheung
468fe9eb08
module: fix submodules loaded by require() and import()
Previously there is an edge case where submodules loaded by require()
may not be loaded by import() again from different intermediate
edges in the graph. This patch fixes that, added tests, and added
debug logs.

Drive-by: make loader a private field so it doesn't show up in logs.
PR-URL: https://github.com/nodejs/node/pull/52487
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
2024-04-16 14:07:44 +00:00
Joyee Cheung
db1746182b
module: disallow CJS <-> ESM edges in a cycle from require(esm)
This patch disallows CJS <-> ESM edges when they come from
require(esm) requested in ESM evalaution.

Drive-by: don't reuse the cache for imported CJS modules to stash
source code of required ESM because the former is also used for
cycle detection.

PR-URL: https://github.com/nodejs/node/pull/52264
Fixes: https://github.com/nodejs/node/issues/52145
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2024-04-08 14:45:55 +00:00
Stefan Stojanovic
45f0dd0192
module,win: fix long path resolve
Fixes: https://github.com/nodejs/node/issues/50753
PR-URL: https://github.com/nodejs/node/pull/51097
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
2024-04-08 14:37:46 +00:00
Antoine du Hamel
bae14b7914
test: do not set concurrency on parallelized runs
Our CI already run test files in parallel, having `node:test` spawns
child processes concurrently could lead to oversubscribing the CI
machine. This commit sets the `concurrency` depending
on the presence of `TEST_PARALLEL` in the env, so running the test
file individually still spawns child processes concurrently, and
running the whole test suite does not oversubscribe the machine.

PR-URL: https://github.com/nodejs/node/pull/52177
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
2024-03-23 21:11:28 +00:00
Geoffrey Booth
f4a0a3b04b
module: warn on detection in typeless package
PR-URL: https://github.com/nodejs/node/pull/52168
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2024-03-23 00:38:31 +00:00
Joyee Cheung
5f7fad2605
module: support require()ing synchronous ESM graphs
This patch adds `require()` support for synchronous ESM graphs under
the flag `--experimental-require-module`

This is based on the the following design aspect of ESM:

- The resolution can be synchronous (up to the host)
- The evaluation of a synchronous graph (without top-level await) is
  also synchronous, and, by the time the module graph is instantiated
  (before evaluation starts), this is is already known.

If `--experimental-require-module` is enabled, and the ECMAScript
module being loaded by `require()` meets the following requirements:

- Explicitly marked as an ES module with a `"type": "module"` field in
  the closest package.json or a `.mjs` extension.
- Fully synchronous (contains no top-level `await`).

`require()` will load the requested module as an ES Module, and return
the module name space object. In this case it is similar to dynamic
`import()` but is run synchronously and returns the name space object
directly.

```mjs
// point.mjs
export function distance(a, b) {
  return (b.x - a.x) ** 2 + (b.y - a.y) ** 2;
}
class Point {
  constructor(x, y) { this.x = x; this.y = y; }
}
export default Point;
```

```cjs
const required = require('./point.mjs');
// [Module: null prototype] {
//   default: [class Point],
//   distance: [Function: distance]
// }
console.log(required);

(async () => {
  const imported = await import('./point.mjs');
  console.log(imported === required);  // true
})();
```

If the module being `require()`'d contains top-level `await`, or the
module graph it `import`s contains top-level `await`,
[`ERR_REQUIRE_ASYNC_MODULE`][] will be thrown. In this case, users
should load the asynchronous module using `import()`.

If `--experimental-print-required-tla` is enabled, instead of throwing
`ERR_REQUIRE_ASYNC_MODULE` before evaluation, Node.js will evaluate the
module, try to locate the top-level awaits, and print their location to
help users fix them.

PR-URL: https://github.com/nodejs/node/pull/51977
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
2024-03-18 18:10:06 +08:00
Geoffrey Booth
63d04d4d80
module: fix detect-module not retrying as esm for cjs-only errors
PR-URL: https://github.com/nodejs/node/pull/52024
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
2024-03-12 13:37:51 +00:00
Joyee Cheung
575ced8139
module: print location of unsettled top-level await in entry points
When the entry point is a module and the graph it imports still
contains unsettled top-level await when the Node.js instance
finishes the event loop, search from the entry point module
for unsettled top-level await and print their location.

To avoid unnecessary overhead, we register a promise that only
gets settled when the entry point graph evaluation returns
from await, and only search the module graph if it's still
unsettled by the time the instance is exiting.

This patch only handles this for entry point modules. Other kinds of
modules are more complicated so will be left for the future.

Drive-by: update the terminology "unfinished promise" to the
more correct one "unsettled promise" in the codebase.

PR-URL: https://github.com/nodejs/node/pull/51999
Fixes: https://github.com/nodejs/node/issues/42868
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
2024-03-10 08:21:22 +08:00
Joyee Cheung
7f2d61f82a
v8: implement v8.queryObjects() for memory leak regression testing
This is similar to the `queryObjects()` console API provided by the
Chromium DevTools console. It can be used to search for objects that
have the matching constructor on its prototype chain in the entire
heap, which can be useful for memory leak regression tests. To avoid
surprising results, users should avoid using this API on constructors
whose implementation they don't control, or on constructors that can
be invoked by other parties in the application.

To avoid accidental leaks, this API does not return raw references to
the objects found. By default, it returns the count of the objects
found. If `options.format` is `'summary'`, it returns an array
containing brief string representations for each object. The visibility
provided in this API is similar to what the heap snapshot provides,
while users can save the cost of serialization and parsing and directly
filer the target objects during the search.

We have been using this API internally for the test suite, which
has been more stable than any other leak regression testing
strategies in the CI. With a public implementation we can now
use the public API instead.

PR-URL: https://github.com/nodejs/node/pull/51927
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
2024-03-02 22:11:30 +00:00
unbyte
fc0f2cf475
lib,src: extract sourceMappingURL from module
PR-URL: https://github.com/nodejs/node/pull/51690
Refs: https://github.com/nodejs/node/issues/51522
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
2024-02-26 11:55:30 +08:00
Joyee Cheung
ad0bcb9c02
vm: support using the default loader to handle dynamic import()
This patch adds support for using
`vm.constants.USE_MAIN_CONTEXT_DEFAULT_LOADER` as
`importModuleDynamically` in all APIs that take the option
except `vm.SourceTextModule`. This allows users to have a shortcut
to support dynamic import() in the compiled code without missing
the compilation cache if they don't need customization of the
loading process. We emit an experimental warning when the
`import()` is actually handled by the default loader through
this option instead of requiring `--experimental-vm-modules`.

In addition this refactors the documentation for
`importModuleDynamically` and adds a dedicated section for it
with examples.

`vm.SourceTextModule` is not supported in this patch because
it needs additional refactoring to handle `initializeImportMeta`,
which can be done in a follow-up.

PR-URL: https://github.com/nodejs/node/pull/51244
Fixes: https://github.com/nodejs/node/issues/51154
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
2024-02-01 11:45:42 +00:00
Antoine du Hamel
3fbe1579ce
esm: improve error when calling import.meta.resolve from data: URL
PR-URL: https://github.com/nodejs/node/pull/49516
Reviewed-By: James M Snell <jasnell@gmail.com>
2024-01-31 23:22:18 +01:00
Joyee Cheung
aef9d4ea7b
test: use checkIfCollectableByCounting in SourceTextModule leak test
...which may be more reliable than than checkIfCollectable().

PR-URL: https://github.com/nodejs/node/pull/51512
Refs: https://github.com/nodejs/node/pull/51362
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
2024-01-22 11:53:11 +00:00
per4uk
d0e455044e
module: fix --preserve-symlinks-main
Fixes resolving main module when the `argv[1]` was pointing to a symlink
without its file extension.

PR-URL: https://github.com/nodejs/node/pull/51312
Fixes: https://github.com/nodejs/node/issues/41000
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2024-01-10 23:26:07 +01:00
Michaël Zasso
c64e6035a6
src: do not treat all paths ending with node_modules as such
Fixes: https://github.com/nodejs/node/issues/51245
PR-URL: https://github.com/nodejs/node/pull/51269
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io>
2023-12-25 11:44:26 +00:00
Antoine du Hamel
a717fa2111
esm: fix hint on invalid module specifier
PR-URL: https://github.com/nodejs/node/pull/51223
Fixes: https://github.com/nodejs/node/issues/51216
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2023-12-23 20:38:08 +00:00
Luigi Pinca
40715481bf
test: use tmpdir.refresh() in test-esm-loader-resolve-type.mjs
Use `tmpdir.refresh()` in
`test/es-module/test-esm-loader-resolve-type.mjs` so that the temporary
directory is removed when the test exits.

PR-URL: https://github.com/nodejs/node/pull/51206
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
2023-12-20 18:17:31 +00:00
Luigi Pinca
0390a1de66
test: use tmpdir.refresh() in test-esm-json.mjs
Use `tmpdir.refresh()` in `test/es-module/test-esm-json.mjs` so that
the temporary directory is removed when the test exits.

PR-URL: https://github.com/nodejs/node/pull/51205
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
Reviewed-By: Debadree Chatterjee <debadree333@gmail.com>
2023-12-20 17:47:39 +00:00
Hiroki Osame
228bc5c457
module: load source maps in commonjs translator
PR-URL: https://github.com/nodejs/node/pull/51033
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2023-12-13 05:53:19 +00:00
Bruce MacNaughton
ab857e138f
esm: fix hook name in error message
PR-URL: https://github.com/nodejs/node/pull/50466
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2023-12-05 19:54:01 +01:00
Yagiz Nizipli
951d00d36b
src: disable uncaught exception abortion for ESM syntax detection
PR-URL: https://github.com/nodejs/node/pull/50987
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
2023-12-03 23:45:57 +00:00
Francesco Trotta
2602c4642b
esm: do not call getSource when format is commonjs
Ensure that `defaultLoad` does not uselessly access the file system to
get the source of modules that are known to be in CommonJS format.

This allows CommonJS imports to resolve in the current phase of the
event loop.

Refs: https://github.com/eslint/eslint/pull/17683
PR-URL: https://github.com/nodejs/node/pull/50465
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2023-11-29 09:21:54 +00:00
Antoine du Hamel
4eafcf8ff5
esm: fallback to getSource when load returns nullish source
When using the Modules Customization Hooks API to load CommonJS modules,
we want to support the returned value of `defaultLoad` which must be
nullish to preserve backward compatibility. This can be achieved by
fetching the source from the translator.

PR-URL: https://github.com/nodejs/node/pull/50825
Fixes: https://github.com/nodejs/node/issues/50435
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
2023-11-29 10:12:05 +01:00
Michaël Zasso
4db94c5023
src: iterate on import attributes array correctly
The array's length is supposed to be a multiple of two for dynamic
import callbacks.

Fixes: https://github.com/nodejs/node/issues/50700
PR-URL: https://github.com/nodejs/node/pull/50703
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Shelley Vohr <shelley.vohr@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2023-11-19 21:22:49 +00:00
Yagiz Nizipli
f13dbfd43a src: move package resolver to c++
Co-authored-by: Daniel Lemire <daniel@lemire.me>
PR-URL: https://github.com/nodejs/node/pull/50322
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
2023-11-17 00:16:38 +00:00
Antoine du Hamel
0dfc59e4fc
esm: bypass CJS loader in default load under --default-type=module
This allows user to opt-out from using the monkey-patchable CJS loader,
even to load CJS modules.

PR-URL: https://github.com/nodejs/node/pull/50004
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
2023-11-10 09:20:46 +01:00
Antoine du Hamel
a3e09b3fdd
module: execute --import sequentially
PR-URL: https://github.com/nodejs/node/pull/50474
Fixes: https://github.com/nodejs/node/issues/50427
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
2023-11-01 12:38:01 +00:00
Chengzhong Wu
86afefafda
vm: allow dynamic import with a referrer realm
A referrer can be a Script Record, a Cyclic Module Record, or a Realm
Record as defined in https://tc39.es/ecma262/#sec-HostLoadImportedModule.

Add support for dynamic import calls with a realm as the referrer and
allow specifying an `importModuleDynamically` callback in
`vm.createContext`.

PR-URL: https://github.com/nodejs/node/pull/50360
Refs: https://github.com/nodejs/node/issues/49726
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2023-11-01 06:05:13 +00:00
James Sumners
3daa0a6c04
esm: add import.meta.dirname and import.meta.filename
PR-URL: https://github.com/nodejs/node/pull/48740
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Jiawen Geng <technicalcute@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
2023-10-31 21:11:15 +00:00
Marco Ippolito
8d40818d7a
module: add application/json in accept header when fetching json module
PR-URL: https://github.com/nodejs/node/pull/50119
Refs: https://github.com/nodejs/node/issues/50116
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2023-10-30 16:51:52 +00:00
Antoine du Hamel
8f742bb13f
test: ensure never settling promises are detected
PR-URL: https://github.com/nodejs/node/pull/50318
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
2023-10-23 17:55:50 +00:00
Antoine du Hamel
08ae8401f1
esm: do not give wrong hints when detecting file format
PR-URL: https://github.com/nodejs/node/pull/50314
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
2023-10-23 16:01:59 +00:00
Antoine du Hamel
745979cf39
Revert "test: set test-esm-loader-resolve-type as flaky"
This reverts commit 2e4bb3c017.

PR-URL: https://github.com/nodejs/node/pull/50315
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
2023-10-21 15:54:28 +00:00
Geoffrey Booth
02926d3c6a
esm: detect ESM syntax in ambiguous JavaScript
PR-URL: https://github.com/nodejs/node/pull/50096
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
2023-10-20 15:44:56 +00:00
Antoine du Hamel
765339a32c
test: deflake test-loaders-workers-spawned
PR-URL: https://github.com/nodejs/node/pull/50251
Fixes: https://github.com/nodejs/node/issues/50247
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
2023-10-19 10:44:46 +02:00
Yagiz Nizipli
2e4bb3c017
test: set test-esm-loader-resolve-type as flaky
Ref: https://github.com/nodejs/node/issues/50040
PR-URL: https://github.com/nodejs/node/pull/50226
Refs: https://github.com/nodejs/node/issues/50040
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
2023-10-19 00:15:28 +00:00
Antoine du Hamel
37d4f08cbd
esm: rename error code related to import attributes
PR-URL: https://github.com/nodejs/node/pull/50181
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Zeyu "Alex" Yang <himself65@outlook.com>
2023-10-18 14:27:55 +00:00
Antoine du Hamel
d1ef6aa2db
esm: use import attributes instead of import assertions
The old import assertions proposal has been
renamed to "import attributes" with the follwing major changes:

1. The keyword is now `with` instead of `assert`.
2. Unknown assertions cause an error rather than being ignored,

This commit updates the documentation to encourage folks to use the new
syntax, and add aliases for module customization hooks.

PR-URL: https://github.com/nodejs/node/pull/50140
Fixes: https://github.com/nodejs/node/issues/50134
Refs: 159c82c5e6
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
2023-10-14 03:52:38 +00:00
Tobias Nießen
3b23b2ee53 module: fix code injection through export names
createDynamicModule() properly escapes import names, but not export
names. In WebAssembly, any string is a valid export name. Importing a
WebAssembly module that uses a non-identifier export name leads to
either a syntax error in createDynamicModule() or to code injection,
that is, to the evaluation of almost arbitrary JavaScript code outside
of the WebAssembly module.

To address this issue, adopt the same mechanism in createExport() that
createImport() already uses. Add tests for both exports and imports.

PR-URL: https://github.com/nodejs-private/node-private/pull/461
Backport-PR-URL: https://github.com/nodejs-private/node-private/pull/489
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
CVE-ID: CVE-2023-39333
2023-10-13 18:03:52 -03:00
Jithil P Ponnan
d4c5fe488e
lib: fix compileFunction throws range error for negative numbers
PR-URL: https://github.com/nodejs/node/pull/49855
Fixes: https://github.com/nodejs/node/issues/49848
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
2023-10-06 14:33:46 +00:00
Geoffrey Booth
19b470f866
esm: bypass CommonJS loader under --default-type
PR-URL: https://github.com/nodejs/node/pull/49986
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2023-10-05 17:57:32 +00:00
Node.js GitHub Bot
581434e54f
tools: update eslint to 8.50.0
PR-URL: https://github.com/nodejs/node/pull/49989
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
2023-10-03 15:32:41 +00:00
Geoffrey Booth
9c683204db
esm: unflag extensionless javascript and wasm in module scope
PR-URL: https://github.com/nodejs/node/pull/49974
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
2023-10-03 12:31:21 +08:00
Richard Lau
6c9625dca4
test: update skip for moved test-wasm-web-api
`test-wasm-web-api` was moved from `test/parallel` to `test/es-modules`.
Update the status files for parallel and es-modules accordingly.

Refs: https://github.com/nodejs/node/pull/49869
Refs: https://github.com/nodejs/node/pull/47299
Refs: https://github.com/nodejs/node/issues/47297
PR-URL: https://github.com/nodejs/node/pull/49958
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
2023-09-30 18:10:14 +00:00
Antoine du Hamel
7f0e36af52
esm: fix cache collision on JSON files using file: URL
PR-URL: https://github.com/nodejs/node/pull/49887
Fixes: https://github.com/nodejs/node/issues/49724
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: LiviaMedeiros <livia@cirno.name>
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
2023-09-29 08:46:32 +00:00
Geoffrey Booth
85301803e1
esm: --experimental-default-type flag to flip module defaults
PR-URL: https://github.com/nodejs/node/pull/49869
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2023-09-29 06:18:44 +00:00
Jacob Smith
645b788bea
esm: identify parent importing a url with invalid host
PR-URL: https://github.com/nodejs/node/pull/49736
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
2023-09-23 05:41:26 +00:00
Joyee Cheung
017998971b
test: deflake test-vm-contextified-script-leak
Similar to the test-vm-source-text-module-leak fix, use a snapshot
to force a thorough GC in order to prevent false positives.

PR-URL: https://github.com/nodejs/node/pull/49710
Refs: https://github.com/nodejs/reliability/issues/669
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
2023-09-23 03:46:22 +00:00
Antoine du Hamel
20b996dcf1
esm: fix return type of import.meta.resolve
PR-URL: https://github.com/nodejs/node/pull/49698
Fixes: https://github.com/nodejs/node/issues/49695
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
2023-09-20 10:09:15 +00:00
Geoffrey Booth
f91b4e2bf0
esm: update loaders warning
PR-URL: https://github.com/nodejs/node/pull/49633
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
2023-09-19 03:59:50 +00:00
Joyee Cheung
9322bcd683
test: use checkIfCollectable in vm leak tests
Previously we simply create a lot of the target objects and check
if the process crash due to OOM. Due to how we use emphemeron GC
to handle memory management, which is inefficient but necessary
for correctness, the tests can produce false positives as
the GC isn't efficient enough to catch up with a very fast
heap growth.

This patch uses a new checkIfCollectable() utility to terminate the
test early once we detect that any of the target object can actually
be garbage collected. This should lower the chance of false positives.
As a drive-by this also allows us to use setImmediate() to grow the
heap even faster to make the tests run faster.

PR-URL: https://github.com/nodejs/node/pull/49671
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
2023-09-18 23:00:53 +02:00
Antoine du Hamel
56ecf29283
esm: fix support for URL instances in register
PR-URL: https://github.com/nodejs/node/pull/49655
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2023-09-17 00:48:48 +00:00
Daniel Compton
6a489df73b
esm: clarify ERR_REQUIRE_ESM errors
In #39175, better ESM errors were introduced. This commit tweaks the
language in the error slightly to make it clear that there are three
different options to resolve the error.

Refs: https://github.com/nodejs/node/pull/39175
PR-URL: https://github.com/nodejs/node/pull/49521
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
2023-09-15 05:13:54 +00:00
Joyee Cheung
2dfaee4bc8 module: fix the leak in SourceTextModule and ContextifySript
Replace the persistent handles to v8::Module and
v8::UnboundScript with an internal reference that V8's GC is
aware of to fix the leaks.

PR-URL: https://github.com/nodejs/node/pull/48510
Refs: https://github.com/nodejs/node/issues/44211
Refs: https://github.com/nodejs/node/issues/42080
Refs: https://github.com/nodejs/node/issues/47096
Refs: https://github.com/nodejs/node/issues/43205
Refs: https://github.com/nodejs/node/issues/38695
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
2023-09-14 15:42:26 +00:00
Joyee Cheung
4aa6b8c2e4 module: fix leak of vm.SyntheticModule
Previously we maintain a strong persistent reference to the
ModuleWrap to retrieve the ID-to-ModuleWrap mapping from
the HostImportModuleDynamicallyCallback using the number ID
stored in the host-defined options. As a result the ModuleWrap
would be kept alive until the Environment is shut down, which
would be a leak for user code. With the new symbol-based
host-defined option we can just get the ModuleWrap from the
JS-land WeakMap so there's now no need to maintain this
strong reference. This would at least fix the leak for
vm.SyntheticModule. vm.SourceTextModule is still leaking
due to the strong persistent reference to the v8::Module.

PR-URL: https://github.com/nodejs/node/pull/48510
Refs: https://github.com/nodejs/node/issues/44211
Refs: https://github.com/nodejs/node/issues/42080
Refs: https://github.com/nodejs/node/issues/47096
Refs: https://github.com/nodejs/node/issues/43205
Refs: https://github.com/nodejs/node/issues/38695
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
2023-09-14 15:42:25 +00:00
Joyee Cheung
bc9a875c99 module: use symbol in WeakMap to manage host defined options
Previously when managing the importModuleDynamically callback of
vm.compileFunction(), we use an ID number as the host defined option
and maintain a per-Environment ID -> CompiledFnEntry map to retain
the top-level referrer function returned by vm.compileFunction() in
order to pass it back to the callback, but it would leak because with
how we used v8::Persistent to maintain this reference, V8 would not
be able to understand the cycle and would just think that the
CompiledFnEntry was supposed to live forever. We made an attempt
to make that reference known to V8 by making the CompiledFnEntry weak
and using a private symbol to make CompiledFnEntry strongly
references the top-level referrer function in
https://github.com/nodejs/node/pull/46785, but that turned out to be
unsound, because the there's no guarantee that the top-level function
must be alive while import() can still be initiated from that
function, since V8 could discard the top-level function and only keep
inner functions alive, so relying on the top-level function to keep
the CompiledFnEntry alive could result in use-after-free which caused
a revert of that fix.

With this patch we use a symbol in the host defined options instead of
a number, because with the stage-3 symbol-as-weakmap-keys proposal
we could directly use that symbol to keep the referrer alive using a
WeakMap. As a bonus this also keeps the other kinds of referrers
alive as long as import() can still be initiated from that
Script/Module, so this also fixes the long-standing crash caused by
vm.Script being GC'ed too early when its importModuleDynamically
callback still needs it.

PR-URL: https://github.com/nodejs/node/pull/48510
Refs: https://github.com/nodejs/node/issues/44211
Refs: https://github.com/nodejs/node/issues/42080
Refs: https://github.com/nodejs/node/issues/47096
Refs: https://github.com/nodejs/node/issues/43205
Refs: https://github.com/nodejs/node/issues/38695
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
2023-09-14 15:42:24 +00:00
Jacob Smith
705e623ac4
esm: remove globalPreload hook (superseded by initialize)
PR-URL: https://github.com/nodejs/node/pull/49144
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2023-09-10 04:32:36 +00:00
Antoine du Hamel
c159b90bb1
test: increase coverage of Module.register and initialize hook
PR-URL: https://github.com/nodejs/node/pull/49532
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
2023-09-10 01:29:33 +00:00
Geoffrey Booth
479a50c7e9
test: isolate globalPreload tests
PR-URL: https://github.com/nodejs/node/pull/49545
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
2023-09-09 23:02:21 +00:00
Antoine du Hamel
b64c3ee6cb
esm: remove return value for Module.register
The current API shape si not great because it's too limited and
redundant with the use of `MessagePort`.

PR-URL: https://github.com/nodejs/node/pull/49529
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
2023-09-09 12:44:28 +00:00
Geoffrey Booth
63c3846d0a esm: refactor test-esm-loader-resolve-type
PR-URL: https://github.com/nodejs/node/pull/49493
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2023-09-06 22:30:09 +00:00
Geoffrey Booth
a927adef9e esm: refactor test-esm-named-exports
PR-URL: https://github.com/nodejs/node/pull/49493
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2023-09-06 22:30:08 +00:00
Antoine du Hamel
fdc65d9769
esm: fix loading of CJS modules from ESM
PR-URL: https://github.com/nodejs/node/pull/49500
Fixes: https://github.com/nodejs/node/issues/49497
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
2023-09-06 10:12:14 +00:00
Geoffrey Booth
6a5394ea7d
esm: refactor mocking test
PR-URL: https://github.com/nodejs/node/pull/49465
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2023-09-05 01:13:40 +00:00
Chengzhong Wu
04a3567dba
test: verify dynamic import call with absolute path strings
PR-URL: https://github.com/nodejs/node/pull/49275
Refs: https://github.com/nodejs/node/pull/48655
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2023-08-31 17:15:27 +00:00
Livia Medeiros
eeddbfae6c
test,benchmark: use tmpdir.fileURL()
PR-URL: https://github.com/nodejs/node/pull/49138
Refs: https://github.com/nodejs/node/pull/49040
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
2023-08-21 16:42:03 +00:00
Livia Medeiros
052434a0c1
test: use tmpdir.resolve()
PR-URL: https://github.com/nodejs/node/pull/49136
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
2023-08-21 16:41:53 +00:00
Antoine du Hamel
0daea967ea
test: reduce flakiness of test-esm-loader-hooks
PR-URL: https://github.com/nodejs/node/pull/49248
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
2023-08-20 04:50:06 +00:00
Carlos Espa
413c16e490
src,permission: add multiple allow-fs-* flags
Support for a single comma separates list for allow-fs-* flags is
removed. Instead now multiple flags can be passed to allow multiple
paths.

Fixes: https://github.com/nodejs/security-wg/issues/1039
PR-URL: https://github.com/nodejs/node/pull/49047
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
2023-08-17 18:39:04 +00:00
Antoine du Hamel
1aeb48c975
test: refactor test-esm-loader-hooks for easier debugging
- Always check stderr before stdout as the former would contain error
  information.
- Always match the full stdout to avoid surprises.
- Use `deepStrictEqual` when appropriate to get more informative test
  failures.
- Remove leading slashes from relative paths/URLs to not confuse them
  with absolute paths.
- Remove unnecessary `--no-warnings` flag.

PR-URL: https://github.com/nodejs/node/pull/49131
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
2023-08-15 09:32:38 +00:00
André Alves
71391987f7
lib: fix MIME overmatch in data URLs
This commit adds the delimiters ^ and $ to the regex that matches the
MIME types for `data:` URLs.

PR-URL: https://github.com/nodejs/node/pull/49104
Fixes: https://github.com/nodejs/node/issues/48957
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2023-08-13 23:24:22 +02:00
Antoine du Hamel
b5da2f4dad
esm: fix globalPreload warning
PR-URL: https://github.com/nodejs/node/pull/49069
Fixes: https://github.com/nodejs/node/issues/49026
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Jacob Smith <jacob@frende.me>
2023-08-13 14:37:32 +00:00
Guy Bedford
ee8b7f1f18
esm: unflag import.meta.resolve
PR-URL: https://github.com/nodejs/node/pull/49028
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
2023-08-13 05:42:25 +00:00
Antoine du Hamel
3d20582c0f
module: make CJS load from ESM loader
PR-URL: https://github.com/nodejs/node/pull/47999
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
2023-08-13 04:23:05 +00:00
Guy Bedford
569267d048
esm: import.meta.resolve exact module not found errors should return
PR-URL: https://github.com/nodejs/node/pull/49038
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Jan Krems <jan.krems@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
2023-08-13 00:02:03 +00:00
Antoine du Hamel
e68e359aae
test: reduce flakiness of test-esm-loader-hooks
PR-URL: https://github.com/nodejs/node/pull/49105
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
2023-08-12 10:20:12 +00:00
Antoine du Hamel
6aafb6fbbb
esm: protect ERR_UNSUPPORTED_DIR_IMPORT against prototype pollution
PR-URL: https://github.com/nodejs/node/pull/49060
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
2023-08-11 06:29:14 +00:00
Livia Medeiros
7bbcb292b2
test: add tmpdir.fileURL()
PR-URL: https://github.com/nodejs/node/pull/49040
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
2023-08-10 23:07:30 +00:00
Izaak Schroeder
6ad8318373
esm: add initialize hook, integrate with register
Follows @giltayar's proposed API:

> `register` can pass any data it wants to the loader, which will be
passed to the exported `initialize` function of the loader.
Additionally, if the user of `register` wants to communicate with the
loader, it can just create a `MessageChannel` and pass the port to the
loader as data.

The `register` API is now:

```ts
interface Options {
  parentUrl?: string;
  data?: any;
  transferList?: any[];
}

function register(loader: string, parentUrl?: string): any;
function register(loader: string, options?: Options): any;
```

This API is backwards compatible with the old one (new arguments are
optional and at the end) and allows for passing data into the new
`initialize` hook. If this hook returns data it is passed back to
`register`:

```ts
function initialize(data: any): Promise<any>;
```

**NOTE**: Currently there is no mechanism for a loader to exchange
ownership of something back to the caller.

Refs: https://github.com/nodejs/loaders/issues/147
PR-URL: https://github.com/nodejs/node/pull/48842
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
2023-08-03 05:10:59 +00:00
Antoine du Hamel
af4cdcde15
src: do not pass user input to format string
PR-URL: https://github.com/nodejs/node/pull/48973
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
2023-08-01 20:05:51 +00:00
Antoine du Hamel
ce958d2d78
test: validate error code rather than message
PR-URL: https://github.com/nodejs/node/pull/48972
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
2023-08-01 19:40:06 +00:00
Antoine du Hamel
384f5d44a8
test: fix some assumptions in tests
Some tests are assuming they will be run from a directory that do not
contain any quote or special character in its path. That assumption is
not necessary, using `JSON.stringify` or `pathToFileURL` ensures the
test can be run whatever the path looks like.

PR-URL: https://github.com/nodejs/node/pull/48958
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
2023-07-31 13:09:07 +00:00
Antoine du Hamel
42b5711d0b
test: fix es-module/test-esm-initialization
PR-URL: https://github.com/nodejs/node/pull/48880
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
2023-07-29 09:51:10 +00:00
Antoine du Hamel
053511f7ec
module: ensure successful import returns the same result
PR-URL: https://github.com/nodejs/node/pull/46662
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
2023-07-26 20:34:20 +00:00
Izaak Schroeder
a2fc4a383e
esm: unflag Module.register and allow nested loader import()
Major functional changes:

- Allow `import()` to work within loaders that require other loaders,
- Unflag the use of `Module.register`.

A new interface `Customizations` has been created in order to unify
`ModuleLoader` (previously `DefaultModuleLoader`), `Hooks` and
`CustomizedModuleLoader` all of which now implement it:

```ts
interface LoadResult {
  format: ModuleFormat;
  source: ModuleSource;
}

interface ResolveResult {
  format: string;
  url: URL['href'];
}

interface Customizations {
  allowImportMetaResolve: boolean;
  load(url: string, context: object): Promise<LoadResult>
  resolve(
    originalSpecifier:
    string, parentURL: string,
    importAssertions: Record<string, string>
  ): Promise<ResolveResult>
  resolveSync(
    originalSpecifier:
    string, parentURL: string,
    importAssertions: Record<string, string>
  ) ResolveResult;
  register(specifier: string, parentUrl: string): any;
  forceLoadHooks(): void;
  importMetaInitialize(meta, context, loader): void;
}
```

The `ModuleLoader` class now has `setCustomizations` which takes an
object of this shape and delegates its responsibilities to this object
if present.

Note that two properties `allowImportMetaResolve` and `resolveSync`
exist now as a mechanism for `import.meta.resolve` – since `Hooks`
does not implement `resolveSync` other loaders cannot use
`import.meta.resolve`; `allowImportMetaResolve` is a way of checking
for that case instead of invoking `resolveSync` and erroring.

Fixes https://github.com/nodejs/node/issues/48515
Closes https://github.com/nodejs/node/pull/48439

PR-URL: https://github.com/nodejs/node/pull/48559
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
2023-07-19 10:33:47 +00:00
Antoine du Hamel
16b8c71a32
esm: add back globalPreload tests and fix failing ones
PR-URL: https://github.com/nodejs/node/pull/48779
Fixes: https://github.com/nodejs/node/issues/48778
Fixes: https://github.com/nodejs/node/issues/48516
Refs: https://github.com/nodejs/node/pull/46402
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
2023-07-17 17:26:15 +02:00
Antoine du Hamel
ffb1929b6a
esm: fix emit deprecation on legacy main resolve
PR-URL: https://github.com/nodejs/node/pull/48664
Refs: https://github.com/nodejs/node/pull/48325
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
2023-07-11 16:49:06 +00:00
Vinicius Lourenço
a2a8e31cbc
src,lib: reducing C++ calls of esm legacy main resolve
Instead of many C++ calls, now we make only one C++ call
to return a enum number that represents the selected state.

PR-URL: https://github.com/nodejs/node/pull/48325
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
2023-07-03 18:42:47 +00:00
João Lenon
a40a6c890a
module: implement register utility
PR-URL: https://github.com/nodejs/node/pull/46826
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2023-06-12 00:00:46 +00:00
Joyee Cheung
e1fa85133f
src: implement natives binding without special casing
This patch removes special case in the internal binding loader
for natives, and implements it using the builtins internal
binding. Internally we do not actually need the natives binding,
so implement it as a legacy wrapper instead.

PR-URL: https://github.com/nodejs/node/pull/48186
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
2023-06-09 15:32:55 +02:00
Antoine du Hamel
fa1c73213f
esm: handle globalPreload hook returning a nullish value
PR-URL: https://github.com/nodejs/node/pull/48249
Fixes: https://github.com/nodejs/node/issues/48240
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
2023-06-01 12:10:45 +02:00
Antoine du Hamel
d28f1f1193
esm: handle more error types thrown from the loader thread
PR-URL: https://github.com/nodejs/node/pull/48247
Refs: https://github.com/nodejs/node/issues/48240
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Jacob Smith <jacob@frende.me>
2023-06-01 09:21:01 +00:00
Gil Tayar
ccada8bccc
module: change default resolver to not throw on unknown scheme
Fixes https://github.com/nodejs/loaders/issues/138

PR-URL: https://github.com/nodejs/node/pull/47824
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2023-05-19 15:50:50 +00:00
Tobias Nießen
92d3611fca
test: mark test-esm-loader-http-imports as flaky
This test has been failing occasionally for weeks.

Refs: https://github.com/nodejs/node/issues/47836
Refs: https://github.com/nodejs/reliability/issues/564
PR-URL: https://github.com/nodejs/node/pull/47987
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2023-05-15 10:54:20 +00:00
Antoine du Hamel
cdd20cfd71
esm: do not use 'beforeExit' on the main thread
PR-URL: https://github.com/nodejs/node/pull/47964
Fixes: https://github.com/nodejs/node/issues/47929
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
2023-05-14 05:32:08 +00:00
Joyee Cheung
f14d2e5a0c
loader: use default loader as cascaded loader in the in loader worker
Use the default loader as the cascaded loader in the loader worker.
Otherwise we spawn loader workers in the loader workers indefinitely.

PR-URL: https://github.com/nodejs/node/pull/47620
Fixes: https://github.com/nodejs/node/issues/47566
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
2023-04-21 12:27:54 +00:00