Skip to content

Commit 6025590

Browse files
authored
Merge branch 'nodejs:main' into fix-possible-dereference-of-null
2 parents ce20ac3 + c969649 commit 6025590

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+405
-110
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,6 @@ For information about the governance of the Node.js project, see
401401
**Xuguang Mei** <<[email protected]>> (he/him)
402402
* [mhdawson](https://github.com/mhdawson) -
403403
**Michael Dawson** <<[email protected]>> (he/him)
404-
* [mildsunrise](https://github.com/mildsunrise) -
405-
**Alba Mendez** <<[email protected]>> (she/her)
406404
* [MoLow](https://github.com/MoLow) -
407405
**Moshe Atlow** <<[email protected]>> (he/him)
408406
* [MrJithil](https://github.com/MrJithil) -
@@ -611,6 +609,8 @@ For information about the governance of the Node.js project, see
611609
**Mikeal Rogers** <<[email protected]>>
612610
* [miladfarca](https://github.com/miladfarca) -
613611
**Milad Fa** <<[email protected]>> (he/him)
612+
* [mildsunrise](https://github.com/mildsunrise) -
613+
**Alba Mendez** <<[email protected]>> (she/her)
614614
* [misterdjules](https://github.com/misterdjules) -
615615
**Julien Gilli** <<[email protected]>>
616616
* [mmarchini](https://github.com/mmarchini) -

doc/api/esm.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,35 @@ import { readFileSync } from 'node:fs';
400400
const buffer = readFileSync(new URL('./data.proto', import.meta.url));
401401
```
402402
403+
### `import.meta.main`
404+
405+
<!-- YAML
406+
added:
407+
- REPLACEME
408+
-->
409+
410+
> Stability: 1.0 - Early development
411+
412+
* {boolean} `true` when the current module is the entry point of the current process; `false` otherwise.
413+
414+
Equivalent to `require.main === module` in CommonJS.
415+
416+
Analogous to Python's `__name__ == "__main__"`.
417+
418+
```js
419+
export function foo() {
420+
return 'Hello, world';
421+
}
422+
423+
function main() {
424+
const message = foo();
425+
console.log(message);
426+
}
427+
428+
if (import.meta.main) main();
429+
// `foo` can be imported from another module without possible side-effects from `main`
430+
```
431+
403432
### `import.meta.resolve(specifier)`
404433
405434
<!-- YAML
@@ -616,6 +645,10 @@ These CommonJS variables are not available in ES modules.
616645
They can instead be loaded with [`module.createRequire()`][] or
617646
[`process.dlopen`][].
618647
648+
#### No `require.main`
649+
650+
To replace `require.main === module`, there is the [`import.meta.main`][] API.
651+
619652
#### No `require.resolve`
620653
621654
Relative resolution can be handled via `new URL('./local', import.meta.url)`.
@@ -1181,6 +1214,7 @@ resolution for ESM specifiers is [commonjs-extension-resolution-loader][].
11811214
[`import()`]: #import-expressions
11821215
[`import.meta.dirname`]: #importmetadirname
11831216
[`import.meta.filename`]: #importmetafilename
1217+
[`import.meta.main`]: #importmetamain
11841218
[`import.meta.resolve`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import.meta/resolve
11851219
[`import.meta.url`]: #importmetaurl
11861220
[`import`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import

doc/api/zlib.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,6 @@ Allowed flush values.
615615
* `zlib.constants.Z_FULL_FLUSH`
616616
* `zlib.constants.Z_FINISH`
617617
* `zlib.constants.Z_BLOCK`
618-
* `zlib.constants.Z_TREES`
619618

620619
Return codes for the compression/decompression functions. Negative
621620
values are errors, positive values are used for special but normal

lib/_stream_duplex.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
module.exports = require('stream').Duplex;
44

5-
process.emitWarning('The _stream_duplex module is deprecated.',
5+
process.emitWarning('The _stream_duplex module is deprecated. Use `node:stream` instead.',
66
'DeprecationWarning', 'DEP0193');

lib/_stream_passthrough.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
module.exports = require('stream').PassThrough;
44

5-
process.emitWarning('The _stream_passthrough module is deprecated.',
5+
process.emitWarning('The _stream_passthrough module is deprecated. Use `node:stream` instead.',
66
'DeprecationWarning', 'DEP0193');

lib/_stream_readable.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
module.exports = require('stream').Readable;
44

5-
process.emitWarning('The _stream_readable module is deprecated.',
5+
process.emitWarning('The _stream_readable module is deprecated. Use `node:stream` instead.',
66
'DeprecationWarning', 'DEP0193');

lib/_stream_transform.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
module.exports = require('stream').Transform;
44

5-
process.emitWarning('The _stream_transform module is deprecated.',
5+
process.emitWarning('The _stream_transform module is deprecated. Use `node:stream` instead.',
66
'DeprecationWarning', 'DEP0193');

lib/_stream_wrap.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
module.exports = require('internal/js_stream_socket');
44

5-
process.emitWarning('The _stream_wrap module is deprecated.',
5+
process.emitWarning('The _stream_wrap module is deprecated. Use `node:stream` instead.',
66
'DeprecationWarning', 'DEP0125');

lib/_stream_writable.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
module.exports = require('stream').Writable;
44

5-
process.emitWarning('The _stream_writable module is deprecated.',
5+
process.emitWarning('The _stream_writable module is deprecated. Use `node:stream` instead.',
66
'DeprecationWarning', 'DEP0193');

lib/_tls_common.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ module.exports = {
66
createSecureContext,
77
translatePeerCertificate,
88
};
9-
process.emitWarning('The _tls_common module is deprecated.',
9+
process.emitWarning('The _tls_common module is deprecated. Use `node:tls` instead.',
1010
'DeprecationWarning', 'DEP0192');

lib/_tls_wrap.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ module.exports = {
77
createServer,
88
connect,
99
};
10-
process.emitWarning('The _tls_wrap module is deprecated.',
10+
process.emitWarning('The _tls_wrap module is deprecated. Use `node:tls` instead.',
1111
'DeprecationWarning', 'DEP0192');

lib/internal/errors.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1338,7 +1338,12 @@ E('ERR_IMPORT_ATTRIBUTE_MISSING',
13381338
E('ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE',
13391339
'Module "%s" is not of type "%s"', TypeError);
13401340
E('ERR_IMPORT_ATTRIBUTE_UNSUPPORTED',
1341-
'Import attribute "%s" with value "%s" is not supported', TypeError);
1341+
function error(attribute, value, url = undefined) {
1342+
if (url === undefined) {
1343+
return `Import attribute "${attribute}" with value "${value}" is not supported`;
1344+
}
1345+
return `Import attribute "${attribute}" with value "${value}" is not supported in ${url}`;
1346+
}, TypeError);
13421347
E('ERR_INCOMPATIBLE_OPTION_PAIR',
13431348
'Option "%s" cannot be used in combination with option "%s"', TypeError, HideStackFramesError);
13441349
E('ERR_INPUT_TYPE_NOT_ALLOWED', '--input-type can only be used with string ' +

lib/internal/main/worker_thread.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,20 @@ port.on('message', (message) => {
203203
break;
204204
}
205205

206+
case 'data-url': {
207+
const { runEntryPointWithESMLoader } = require('internal/modules/run_main');
208+
209+
RegExpPrototypeExec(/^/, ''); // Necessary to reset RegExp statics before user code runs.
210+
const promise = runEntryPointWithESMLoader((cascadedLoader) => {
211+
return cascadedLoader.import(filename, undefined, { __proto__: null }, undefined, true);
212+
});
213+
214+
PromisePrototypeThen(promise, undefined, (e) => {
215+
workerOnGlobalUncaughtException(e, true);
216+
});
217+
break;
218+
}
219+
206220
default: {
207221
// script filename
208222
// runMain here might be monkey-patched by users in --require.

lib/internal/modules/esm/assert.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function validateAttributes(url, format,
5757
const keys = ObjectKeys(importAttributes);
5858
for (let i = 0; i < keys.length; i++) {
5959
if (keys[i] !== 'type') {
60-
throw new ERR_IMPORT_ATTRIBUTE_UNSUPPORTED(keys[i], importAttributes[keys[i]]);
60+
throw new ERR_IMPORT_ATTRIBUTE_UNSUPPORTED(keys[i], importAttributes[keys[i]], url);
6161
}
6262
}
6363
const validType = formatTypeMap[format];
@@ -102,7 +102,7 @@ function handleInvalidType(url, type) {
102102

103103
// `type` might not have been one of the types we understand.
104104
if (!ArrayPrototypeIncludes(supportedTypeAttributes, type)) {
105-
throw new ERR_IMPORT_ATTRIBUTE_UNSUPPORTED('type', type);
105+
throw new ERR_IMPORT_ATTRIBUTE_UNSUPPORTED('type', type, url);
106106
}
107107

108108
// `type` was the wrong value for this format.

lib/internal/modules/esm/initialize_import_meta.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ function createImportMetaResolve(defaultParentURL, loader, allowParentURL) {
5151
/**
5252
* Create the `import.meta` object for a module.
5353
* @param {object} meta
54-
* @param {{url: string}} context
54+
* @param {{url: string, isMain?: boolean}} context
5555
* @param {typeof import('./loader.js').ModuleLoader} loader Reference to the current module loader
5656
* @returns {{dirname?: string, filename?: string, url: string, resolve?: Function}}
5757
*/
5858
function initializeImportMeta(meta, context, loader) {
59-
const { url } = context;
59+
const { url, isMain } = context;
6060

6161
// Alphabetical
6262
if (StringPrototypeStartsWith(url, 'file:') === true) {
@@ -65,6 +65,8 @@ function initializeImportMeta(meta, context, loader) {
6565
setLazyPathHelpers(meta, url);
6666
}
6767

68+
meta.main = !!isMain;
69+
6870
if (!loader || loader.allowImportMetaResolve) {
6971
meta.resolve = createImportMetaResolve(url, loader, experimentalImportMetaResolve);
7072
}

lib/internal/modules/esm/loader.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,11 @@ class ModuleLoader {
248248
*
249249
* @param {string} source Source code of the module.
250250
* @param {string} url URL of the module.
251+
* @param {{ isMain?: boolean }|undefined} context - context object containing module metadata.
251252
* @returns {object} The module wrap object.
252253
*/
253-
createModuleWrap(source, url) {
254-
return compileSourceTextModule(url, source, this);
254+
createModuleWrap(source, url, context = kEmptyObject) {
255+
return compileSourceTextModule(url, source, this, context);
255256
}
256257

257258
/**
@@ -289,7 +290,8 @@ class ModuleLoader {
289290
* @returns {Promise<object>} The module object.
290291
*/
291292
eval(source, url, isEntryPoint = false) {
292-
const wrap = this.createModuleWrap(source, url);
293+
const context = isEntryPoint ? { isMain: true } : undefined;
294+
const wrap = this.createModuleWrap(source, url, context);
293295
return this.executeModuleJob(url, wrap, isEntryPoint);
294296
}
295297

lib/internal/modules/esm/translators.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ translators.set('module', function moduleStrategy(url, source, isMain) {
103103
source = stringify(source);
104104
debug(`Translating StandardModule ${url}`);
105105
const { compileSourceTextModule } = require('internal/modules/esm/utils');
106-
const module = compileSourceTextModule(url, source, this);
106+
const context = isMain ? { isMain } : undefined;
107+
const module = compileSourceTextModule(url, source, this, context);
107108
return module;
108109
});
109110

lib/internal/modules/esm/utils.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const {
4242
const {
4343
emitExperimentalWarning,
4444
getCWDURL,
45+
kEmptyObject,
4546
} = require('internal/util');
4647
const assert = require('internal/assert');
4748
const {
@@ -188,7 +189,7 @@ function registerModule(referrer, registry) {
188189
*/
189190
function defaultInitializeImportMetaForModule(meta, wrap) {
190191
const cascadedLoader = require('internal/modules/esm/loader').getOrInitializeCascadedLoader();
191-
return cascadedLoader.importMetaInitialize(meta, { url: wrap.url });
192+
return cascadedLoader.importMetaInitialize(meta, { url: wrap.url, isMain: wrap.isMain });
192193
}
193194

194195
/**
@@ -342,15 +343,22 @@ async function initializeHooks() {
342343
* @param {string} source Source code of the module.
343344
* @param {typeof import('./loader.js').ModuleLoader|undefined} cascadedLoader If provided,
344345
* register the module for default handling.
346+
* @param {{ isMain?: boolean }|undefined} context - context object containing module metadata.
345347
* @returns {ModuleWrap}
346348
*/
347-
function compileSourceTextModule(url, source, cascadedLoader) {
349+
function compileSourceTextModule(url, source, cascadedLoader, context = kEmptyObject) {
348350
const hostDefinedOption = cascadedLoader ? source_text_module_default_hdo : undefined;
349351
const wrap = new ModuleWrap(url, undefined, source, 0, 0, hostDefinedOption);
350352

351353
if (!cascadedLoader) {
352354
return wrap;
353355
}
356+
357+
const { isMain } = context;
358+
if (isMain) {
359+
wrap.isMain = true;
360+
}
361+
354362
// Cache the source map for the module if present.
355363
if (wrap.sourceMapURL) {
356364
maybeCacheSourceMap(url, source, wrap, false, undefined, wrap.sourceMapURL);

lib/internal/worker.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const {
77
AtomicsAdd,
88
Float64Array,
99
FunctionPrototypeBind,
10-
JSONStringify,
1110
MathMax,
1211
ObjectEntries,
1312
Promise,
@@ -167,8 +166,8 @@ class Worker extends EventEmitter {
167166
doEval = 'classic';
168167
} else if (isURL(filename) && filename.protocol === 'data:') {
169168
url = null;
170-
doEval = 'module';
171-
filename = `import ${JSONStringify(`${filename}`)}`;
169+
doEval = 'data-url';
170+
filename = `${filename}`;
172171
} else {
173172
doEval = false;
174173
if (isURL(filename)) {

lib/sys.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@
2727
// Note to maintainers: Although this module has been deprecated for a while
2828
// we do not plan to remove it. See: https://github.com/nodejs/node/pull/35407#issuecomment-700693439
2929
module.exports = require('util');
30-
process.emitWarning('sys is deprecated. Use util instead.',
30+
process.emitWarning('sys is deprecated. Use `node:util` instead.',
3131
'DeprecationWarning', 'DEP0025');

lib/zlib.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ ZlibBase.prototype._final = function(callback) {
307307
// This is currently only used to figure out which flush flag to use for the
308308
// last chunk.
309309
// Roughly, the following holds:
310-
// Z_NO_FLUSH (< Z_TREES) < Z_BLOCK < Z_PARTIAL_FLUSH <
310+
// Z_NO_FLUSH < Z_BLOCK < Z_PARTIAL_FLUSH <
311311
// Z_SYNC_FLUSH < Z_FULL_FLUSH < Z_FINISH
312312
const flushiness = [];
313313
const kFlushFlagList = [Z_NO_FLUSH, Z_BLOCK, Z_PARTIAL_FLUSH,

src/crypto/crypto_dh.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ using ncrypto::DHPointer;
2222
using ncrypto::EVPKeyCtxPointer;
2323
using ncrypto::EVPKeyPointer;
2424
using v8::ArrayBuffer;
25+
using v8::BackingStoreInitializationMode;
26+
using v8::BackingStoreOnFailureMode;
2527
using v8::ConstructorBehavior;
2628
using v8::Context;
2729
using v8::DontDelete;
@@ -58,6 +60,20 @@ MaybeLocal<Value> DataPointerToBuffer(Environment* env, DataPointer&& data) {
5860
struct Flag {
5961
bool secure;
6062
};
63+
#ifdef V8_ENABLE_SANDBOX
64+
auto backing = ArrayBuffer::NewBackingStore(
65+
env->isolate(),
66+
data.size(),
67+
BackingStoreInitializationMode::kUninitialized,
68+
BackingStoreOnFailureMode::kReturnNull);
69+
if (!backing) {
70+
THROW_ERR_MEMORY_ALLOCATION_FAILED(env);
71+
return MaybeLocal<Value>();
72+
}
73+
if (data.size() > 0) {
74+
memcpy(backing->Data(), data.get(), data.size());
75+
}
76+
#else
6177
auto backing = ArrayBuffer::NewBackingStore(
6278
data.get(),
6379
data.size(),
@@ -67,6 +83,7 @@ MaybeLocal<Value> DataPointerToBuffer(Environment* env, DataPointer&& data) {
6783
},
6884
new Flag{data.isSecure()});
6985
data.release();
86+
#endif // V8_ENABLE_SANDBOX
7087

7188
auto ab = ArrayBuffer::New(env->isolate(), std::move(backing));
7289
return Buffer::New(env, ab, 0, ab->ByteLength()).FromMaybe(Local<Value>());

0 commit comments

Comments
 (0)