From ebda268754e9d657ad60f459326b88bec9243c16 Mon Sep 17 00:00:00 2001 From: Myles Borins Date: Wed, 28 Nov 2018 00:39:16 -0500 Subject: [PATCH 01/12] modules: introduce nodejs namespace Up until now built-in modules shared a namepsace with all other modules on the file system. This commit introduces a url based namespace for the Node.js project to use for built-in modules. From this point on all future modules adding by Node.js core will be exposed in the `nodejs:` namespace. Legacy modules will exist on both the `nodejs:` and global namespaces sharing the same singleton. `require('nodejs:fs') === require('fs')` As `worker_threads` is still an experimental feature it has been moved into the `nodejs:` namespace. Why `nodejs:` and node `@nodejs/`? The `@nodejs/` namespace is already reserved on `npm` and there are plans to use this namespace for modules we publish to `npm`. Making a distinction between modules prepackaged in node and those being distributed externally seems like a prudent choice. --- doc/api/errors.md | 6 +++++ doc/api/worker_threads.md | 40 +++++++++++++++-------------- lib/internal/bootstrap/loaders.js | 2 +- lib/internal/errors.js | 1 + lib/internal/modules/cjs/helpers.js | 15 +++++++---- lib/internal/modules/cjs/loader.js | 14 +++++++++- tools/run-worker.js | 2 +- 7 files changed, 53 insertions(+), 27 deletions(-) diff --git a/doc/api/errors.md b/doc/api/errors.md index f0c118fd59d5e9..c6bbc74ee6886c 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -1234,6 +1234,12 @@ An invalid HTTP token was supplied. An IP address is not valid. + +### ERR_INVALID_NODE_BUILTIN + +An attempt was made to load a module from the Node.js namespace that doesn't +exist. Only built in modules can be require from 'nodejs:'. + ### ERR_INVALID_OPT_VALUE diff --git a/doc/api/worker_threads.md b/doc/api/worker_threads.md index ca3aed6140ca16..8b29eb3e446391 100644 --- a/doc/api/worker_threads.md +++ b/doc/api/worker_threads.md @@ -9,7 +9,7 @@ on independent threads, and to create message channels between them. It can be accessed using the `--experimental-worker` flag and: ```js -const worker = require('worker_threads'); +const worker = require('nodejs:worker_threads'); ``` Workers are useful for performing CPU-intensive JavaScript operations; do not @@ -23,7 +23,7 @@ share memory efficiently by transferring `ArrayBuffer` instances or sharing ```js const { Worker, isMainThread, parentPort, workerData -} = require('worker_threads'); +} = require('nodejs:worker_threads'); if (isMainThread) { module.exports = async function parseJSAsync(script) { @@ -104,7 +104,7 @@ yields an object with `port1` and `port2` properties, which refer to linked [`MessagePort`][] instances. ```js -const { MessageChannel } = require('worker_threads'); +const { MessageChannel } = require('nodejs:worker_threads'); const { port1, port2 } = new MessageChannel(); port1.on('message', (message) => console.log('received', message)); @@ -243,8 +243,10 @@ Notable differences inside a Worker environment are: - The [`process.stdin`][], [`process.stdout`][] and [`process.stderr`][] may be redirected by the parent thread. -- The [`require('worker_threads').isMainThread`][] property is set to `false`. -- The [`require('worker_threads').parentPort`][] message port is available, +- The [`require('nodejs:worker_threads').isMainThread`][] + property is set to `false`. +- The [`require('nodejs:worker_threads').parentPort`][] message port is + available, - [`process.exit()`][] does not stop the whole program, just the single thread, and [`process.abort()`][] is not available. - [`process.chdir()`][] and `process` methods that set group or user ids @@ -286,7 +288,7 @@ the thread barrier. const assert = require('assert'); const { Worker, MessageChannel, MessagePort, isMainThread, parentPort -} = require('worker_threads'); +} = require('nodejs:worker_threads'); if (isMainThread) { const worker = new Worker(__filename); const subChannel = new MessageChannel(); @@ -314,10 +316,10 @@ if (isMainThread) { * `eval` {boolean} If true, interpret the first argument to the constructor as a script that is executed once the worker is online. * `workerData` {any} Any JavaScript value that will be cloned and made - available as [`require('worker_threads').workerData`][]. The cloning will - occur as described in the [HTML structured clone algorithm][], and an error - will be thrown if the object cannot be cloned (e.g. because it contains - `function`s). + available as [`require('nodejs:worker_threads').workerData`][]. The + cloning will occur as described in the [HTML structured clone algorithm][], + and an error will be thrown if the object cannot be cloned (e.g. because it + contains `function`s). * stdin {boolean} If this is set to `true`, then `worker.stdin` will provide a writable stream whose contents will appear as `process.stdin` inside the Worker. By default, no data is provided. @@ -356,7 +358,7 @@ added: v10.5.0 * `value` {any} The transmitted value The `'message'` event is emitted when the worker thread has invoked -[`require('worker_threads').postMessage()`][]. See the [`port.on('message')`][] +[`require('nodejs:worker_threads').postMessage()`][]. See the [`port.on('message')`][] event for more details. ### Event: 'online' @@ -376,7 +378,7 @@ added: v10.5.0 * `transferList` {Object[]} Send a message to the worker that will be received via -[`require('worker_threads').parentPort.on('message')`][]. +[`require('nodejs:worker_threads').parentPort.on('message')`][]. See [`port.postMessage()`][] for more details. ### worker.ref() @@ -451,7 +453,7 @@ added: v10.5.0 * {integer} An integer identifier for the referenced thread. Inside the worker thread, -it is available as [`require('worker_threads').threadId`][]. +it is available as [`require('nodejs:worker_threads').threadId`][]. ### worker.unref()