diff --git a/.github/workflows/tools.yml b/.github/workflows/tools.yml index 258772934def71..4136af15cd0771 100644 --- a/.github/workflows/tools.yml +++ b/.github/workflows/tools.yml @@ -19,7 +19,7 @@ on: - amaro - brotli - c-ares - - cjs-module-lexer + - merve - corepack - googletest - gyp-next @@ -105,11 +105,11 @@ jobs: cat temp-output tail -n1 temp-output | grep "NEW_VERSION=" >> "$GITHUB_ENV" || true rm temp-output - - id: cjs-module-lexer + - id: merve subsystem: deps label: dependencies run: | - ./tools/dep_updaters/update-cjs-module-lexer.sh > temp-output + ./tools/dep_updaters/update-merve.sh > temp-output cat temp-output tail -n1 temp-output | grep "NEW_VERSION=" >> "$GITHUB_ENV" || true rm temp-output diff --git a/LICENSE b/LICENSE index df0947155c7e57..9c4014789483b4 100644 --- a/LICENSE +++ b/LICENSE @@ -104,18 +104,26 @@ The externally maintained libraries used by Node.js are: SOFTWARE. """ -- cjs-module-lexer, located at deps/cjs-module-lexer, is licensed as follows: +- merve, located at deps/merve, is licensed as follows: """ - MIT License - ----------- - - Copyright (C) 2018-2020 Guy Bedford + Copyright 2026 Yagiz Nizipli - Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + Permission is hereby granted, free of charge, to any person obtaining a copy of + this software and associated documentation files (the "Software"), to deal in + the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: - The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ - ittapi, located at deps/v8/third_party/ittapi, is licensed as follows: diff --git a/configure.py b/configure.py index 4fb061aae7a955..35b87f26ddbee3 100755 --- a/configure.py +++ b/configure.py @@ -58,9 +58,7 @@ maglev_enabled_architectures = ('x64', 'arm', 'arm64', 's390x') # builtins may be removed later if they have been disabled by options -shareable_builtins = {'cjs_module_lexer/lexer': 'deps/cjs-module-lexer/lexer.js', - 'cjs_module_lexer/dist/lexer': 'deps/cjs-module-lexer/dist/lexer.js', - 'undici/undici': 'deps/undici/undici.js', +shareable_builtins = {'undici/undici': 'deps/undici/undici.js', 'amaro/dist/index': 'deps/amaro/dist/index.js' } @@ -556,7 +554,6 @@ dest='shared_simdutf_libpath', help='a directory to search for the shared simdutf DLL') - shared_optgroup.add_argument('--shared-ada', action='store_true', dest='shared_ada', @@ -579,6 +576,28 @@ dest='shared_ada_libpath', help='a directory to search for the shared ada DLL') +shared_optgroup.add_argument('--shared-merve', + action='store_true', + dest='shared_merve', + default=None, + help='link to a shared merve DLL instead of static linking') + +shared_optgroup.add_argument('--shared-merve-includes', + action='store', + dest='shared_merve_includes', + help='directory containing merve header files') + +shared_optgroup.add_argument('--shared-merve-libname', + action='store', + dest='shared_merve_libname', + default='merve', + help='alternative lib name to link to [default: %(default)s]') + +shared_optgroup.add_argument('--shared-merve-libpath', + action='store', + dest='shared_merve_libpath', + help='a directory to search for the shared merve DLL') + shared_optgroup.add_argument('--shared-brotli', action='store_true', dest='shared_brotli', @@ -2482,6 +2501,7 @@ def make_bin_override(): configure_library('cares', output, pkgname='libcares') configure_library('gtest', output) configure_library('hdr_histogram', output) +configure_library('merve', output) configure_library('nbytes', output) configure_library('nghttp2', output, pkgname='libnghttp2') configure_library('nghttp3', output, pkgname='libnghttp3') diff --git a/deps/cjs-module-lexer/LICENSE b/deps/cjs-module-lexer/LICENSE deleted file mode 100644 index 935b357962d08b..00000000000000 --- a/deps/cjs-module-lexer/LICENSE +++ /dev/null @@ -1,10 +0,0 @@ -MIT License ------------ - -Copyright (C) 2018-2020 Guy Bedford - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/deps/cjs-module-lexer/README.md b/deps/cjs-module-lexer/README.md deleted file mode 100644 index addf5edc6b1193..00000000000000 --- a/deps/cjs-module-lexer/README.md +++ /dev/null @@ -1,483 +0,0 @@ -# CJS Module Lexer - -[![Build Status][travis-image]][travis-url] - -A [very fast](#benchmarks) JS CommonJS module syntax lexer used to detect the most likely list of named exports of a CommonJS module. - -Outputs the list of named exports (`exports.name = ...`) and possible module reexports (`module.exports = require('...')`), including the common transpiler variations of these cases. - -Forked from https://github.com/guybedford/es-module-lexer. - -_Comprehensively handles the JS language grammar while remaining small and fast. - ~90ms per MB of JS cold and ~15ms per MB of JS warm, [see benchmarks](#benchmarks) for more info._ - -### Project Status - -This project is used in Node.js core for detecting the named exports available when importing a CJS module into ESM, and is maintained for this purpose. - -PRs will be accepted and upstreamed for parser bugs, performance improvements or new syntax support only. - -_Detection patterns for this project are **frozen**_. This is because adding any new export detection patterns would result in fragmented backwards-compatibility. Specifically, it would be very difficult to figure out why an ES module named export for CommonJS might work in newer Node.js versions but not older versions. This problem would only be discovered downstream of module authors, with the fix for module authors being to then have to understand which patterns in this project provide full backwards-compatibily. Rather, by fully freezing the detected patterns, if it works in any Node.js version it will work in any other. Build tools can also reliably treat the supported syntax for this project as a part of their output target for ensuring syntax support. - -### Usage - -``` -npm install cjs-module-lexer -``` - -For use in CommonJS: - -```js -const { parse } = require('cjs-module-lexer'); - -// `init` return a promise for parity with the ESM API, but you do not have to call it - -const { exports, reexports } = parse(` - // named exports detection - module.exports.a = 'a'; - (function () { - exports.b = 'b'; - })(); - Object.defineProperty(exports, 'c', { value: 'c' }); - /* exports.d = 'not detected'; */ - - // reexports detection - if (maybe) module.exports = require('./dep1.js'); - if (another) module.exports = require('./dep2.js'); - - // literal exports assignments - module.exports = { a, b: c, d, 'e': f } - - // __esModule detection - Object.defineProperty(module.exports, '__esModule', { value: true }) -`); - -// exports === ['a', 'b', 'c', '__esModule'] -// reexports === ['./dep1.js', './dep2.js'] -``` - -When using the ESM version, Wasm is supported instead: - -```js -import { parse, init } from 'cjs-module-lexer'; -// init() needs to be called and waited upon, or use initSync() to compile -// Wasm blockingly and synchronously. -await init(); -const { exports, reexports } = parse(source); -``` - -The Wasm build is around 1.5x faster and without a cold start. - -### Grammar - -CommonJS exports matches are run against the source token stream. - -The token grammar is: - -``` -IDENTIFIER: As defined by ECMA-262, without support for identifier `\` escapes, filtered to remove strict reserved words: - "implements", "interface", "let", "package", "private", "protected", "public", "static", "yield", "enum" - -STRING_LITERAL: A `"` or `'` bounded ECMA-262 string literal. - -MODULE_EXPORTS: `module` `.` `exports` - -EXPORTS_IDENTIFIER: MODULE_EXPORTS_IDENTIFIER | `exports` - -EXPORTS_DOT_ASSIGN: EXPORTS_IDENTIFIER `.` IDENTIFIER `=` - -EXPORTS_LITERAL_COMPUTED_ASSIGN: EXPORTS_IDENTIFIER `[` STRING_LITERAL `]` `=` - -EXPORTS_LITERAL_PROP: (IDENTIFIER (`:` IDENTIFIER)?) | (STRING_LITERAL `:` IDENTIFIER) - -EXPORTS_SPREAD: `...` (IDENTIFIER | REQUIRE) - -EXPORTS_MEMBER: EXPORTS_DOT_ASSIGN | EXPORTS_LITERAL_COMPUTED_ASSIGN - -EXPORTS_DEFINE: `Object` `.` `defineProperty `(` EXPORTS_IDENFITIER `,` STRING_LITERAL - -EXPORTS_DEFINE_VALUE: EXPORTS_DEFINE `, {` - (`enumerable: true,`)? - ( - `value:` | - `get` (`: function` IDENTIFIER? )? `() {` return IDENTIFIER (`.` IDENTIFIER | `[` STRING_LITERAL `]`)? `;`? `}` `,`? - ) - `})` - -EXPORTS_LITERAL: MODULE_EXPORTS `=` `{` (EXPORTS_LITERAL_PROP | EXPORTS_SPREAD) `,`)+ `}` - -REQUIRE: `require` `(` STRING_LITERAL `)` - -EXPORTS_ASSIGN: (`var` | `const` | `let`) IDENTIFIER `=` (`_interopRequireWildcard (`)? REQUIRE - -MODULE_EXPORTS_ASSIGN: MODULE_EXPORTS `=` REQUIRE - -EXPORT_STAR: (`__export` | `__exportStar`) `(` REQUIRE - -EXPORT_STAR_LIB: `Object.keys(` IDENTIFIER$1 `).forEach(function (` IDENTIFIER$2 `) {` - ( - ( - `if (` IDENTIFIER$2 `===` ( `'default'` | `"default"` ) `||` IDENTIFIER$2 `===` ( '__esModule' | `"__esModule"` ) `) return` `;`? - ( - (`if (Object` `.prototype`? `.hasOwnProperty.call(` IDENTIFIER `, ` IDENTIFIER$2 `)) return` `;`?)? - (`if (` IDENTIFIER$2 `in` EXPORTS_IDENTIFIER `&&` EXPORTS_IDENTIFIER `[` IDENTIFIER$2 `] ===` IDENTIFIER$1 `[` IDENTIFIER$2 `]) return` `;`)? - )? - ) | - `if (` IDENTIFIER$2 `!==` ( `'default'` | `"default"` ) (`&& !` (`Object` `.prototype`? `.hasOwnProperty.call(` IDENTIFIER `, ` IDENTIFIER$2 `)` | IDENTIFIER `.hasOwnProperty(` IDENTIFIER$2 `)`))? `)` - ) - ( - EXPORTS_IDENTIFIER `[` IDENTIFIER$2 `] =` IDENTIFIER$1 `[` IDENTIFIER$2 `]` `;`? | - `Object.defineProperty(` EXPORTS_IDENTIFIER `, ` IDENTIFIER$2 `, { enumerable: true, get` (`: function` IDENTIFIER? )? `() { return ` IDENTIFIER$1 `[` IDENTIFIER$2 `]` `;`? `}` `,`? `})` `;`? - ) - `})` -``` - -Spacing between tokens is taken to be any ECMA-262 whitespace, ECMA-262 block comment or ECMA-262 line comment. - -* The returned export names are taken to be the combination of: - 1. All `IDENTIFIER` and `STRING_LITERAL` slots for `EXPORTS_MEMBER` and `EXPORTS_LITERAL` matches. - 2. The first `STRING_LITERAL` slot for all `EXPORTS_DEFINE_VALUE` matches where that same string is not an `EXPORTS_DEFINE` match that is not also an `EXPORTS_DEFINE_VALUE` match. -* The reexport specifiers are taken to be the combination of: - 1. The `REQUIRE` matches of the last matched of either `MODULE_EXPORTS_ASSIGN` or `EXPORTS_LITERAL`. - 2. All _top-level_ `EXPORT_STAR` `REQUIRE` matches and `EXPORTS_ASSIGN` matches whose `IDENTIFIER` also matches the first `IDENTIFIER` in `EXPORT_STAR_LIB`. - -### Parsing Examples - -#### Named Exports Parsing - -The basic matching rules for named exports are `exports.name`, `exports['name']` or `Object.defineProperty(exports, 'name', ...)`. This matching is done without scope analysis and regardless of the expression position: - -```js -// DETECTS EXPORTS: a, b -(function (exports) { - exports.a = 'a'; - exports['b'] = 'b'; -})(exports); -``` - -Because there is no scope analysis, the above detection may overclassify: - -```js -// DETECTS EXPORTS: a, b, c -(function (exports, Object) { - exports.a = 'a'; - exports['b'] = 'b'; - if (false) - exports.c = 'c'; -})(NOT_EXPORTS, NOT_OBJECT); -``` - -It will in turn underclassify in cases where the identifiers are renamed: - -```js -// DETECTS: NO EXPORTS -(function (e) { - e.a = 'a'; - e['b'] = 'b'; -})(exports); -``` - -#### Getter Exports Parsing - -`Object.defineProperty` is detected for specifically value and getter forms returning an identifier or member expression: - -```js -// DETECTS: a, b, c, d, __esModule -Object.defineProperty(exports, 'a', { - enumerable: true, - get: function () { - return q.p; - } -}); -Object.defineProperty(exports, 'b', { - enumerable: true, - get: function () { - return q['p']; - } -}); -Object.defineProperty(exports, 'c', { - enumerable: true, - get () { - return b; - } -}); -Object.defineProperty(exports, 'd', { value: 'd' }); -Object.defineProperty(exports, '__esModule', { value: true }); -``` - -Value properties are also detected specifically: - -```js -Object.defineProperty(exports, 'a', { - value: 'no problem' -}); -``` - -To avoid matching getters that have side effects, any getter for an export name that does not support the forms above will -opt-out of the getter matching: - -```js -// DETECTS: NO EXPORTS -Object.defineProperty(exports, 'a', { - get () { - return 'nope'; - } -}); - -if (false) { - Object.defineProperty(module.exports, 'a', { - get () { - return dynamic(); - } - }) -} -``` - -Alternative object definition structures or getter function bodies are not detected: - -```js -// DETECTS: NO EXPORTS -Object.defineProperty(exports, 'a', { - enumerable: false, - get () { - return p; - } -}); -Object.defineProperty(exports, 'b', { - configurable: true, - get () { - return p; - } -}); -Object.defineProperty(exports, 'c', { - get: () => p -}); -Object.defineProperty(exports, 'd', { - enumerable: true, - get: function () { - return dynamic(); - } -}); -Object.defineProperty(exports, 'e', { - enumerable: true, - get () { - return 'str'; - } -}); -``` - -`Object.defineProperties` is also not supported. - -#### Exports Object Assignment - -A best-effort is made to detect `module.exports` object assignments, but because this is not a full parser, arbitrary expressions are not handled in the -object parsing process. - -Simple object definitions are supported: - -```js -// DETECTS EXPORTS: a, b, c -module.exports = { - a, - 'b': b, - c: c, - ...d -}; -``` - -Object properties that are not identifiers or string expressions will bail out of the object detection, while spreads are ignored: - -```js -// DETECTS EXPORTS: a, b -module.exports = { - a, - ...d, - b: require('c'), - c: "not detected since require('c') above bails the object detection" -} -``` - -`Object.defineProperties` is not currently supported either. - -#### module.exports reexport assignment - -Any `module.exports = require('mod')` assignment is detected as a reexport, but only the last one is returned: - -```js -// DETECTS REEXPORTS: c -module.exports = require('a'); -(module => module.exports = require('b'))(NOT_MODULE); -if (false) module.exports = require('c'); -``` - -This is to avoid over-classification in Webpack bundles with externals which include `module.exports = require('external')` in their source for every external dependency. - -In exports object assignment, any spread of `require()` are detected as multiple separate reexports: - -```js -// DETECTS REEXPORTS: a, b -module.exports = require('ignored'); -module.exports = { - ...require('a'), - ...require('b') -}; -``` - -#### Transpiler Re-exports - -For named exports, transpiler output works well with the rules described above. - -But for star re-exports, special care is taken to support common patterns of transpiler outputs from Babel and TypeScript as well as bundlers like RollupJS. -These reexport and star reexport patterns are restricted to only be detected at the top-level as provided by the direct output of these tools. - -For example, `export * from 'external'` is output by Babel as: - -```js -"use strict"; - -exports.__esModule = true; - -var _external = require("external"); - -Object.keys(_external).forEach(function (key) { - if (key === "default" || key === "__esModule") return; - exports[key] = _external[key]; -}); -``` - -Where the `var _external = require("external")` is specifically detected as well as the `Object.keys(_external)` statement, down to the exact -for of that entire expression including minor variations of the output. The `_external` and `key` identifiers are carefully matched in this -detection. - -Similarly for TypeScript, `export * from 'external'` is output as: - -```js -"use strict"; -function __export(m) { - for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; -} -Object.defineProperty(exports, "__esModule", { value: true }); -__export(require("external")); -``` - -Where the `__export(require("external"))` statement is explicitly detected as a reexport, including variations `tslib.__export` and `__exportStar`. - -### Environment Support - -Node.js 10+, and [all browsers with Web Assembly support](https://caniuse.com/#feat=wasm). - -### JS Grammar Support - -* Token state parses all line comments, block comments, strings, template strings, blocks, parens and punctuators. -* Division operator / regex token ambiguity is handled via backtracking checks against punctuator prefixes, including closing brace or paren backtracking. -* Always correctly parses valid JS source, but may parse invalid JS source without errors. - -### Benchmarks - -Benchmarks can be run with `npm run bench`. - -Current results: - -JS Build: - -``` -Module load time -> 4ms -Cold Run, All Samples -test/samples/*.js (3635 KiB) -> 299ms - -Warm Runs (average of 25 runs) -test/samples/angular.js (1410 KiB) -> 13.96ms -test/samples/angular.min.js (303 KiB) -> 4.72ms -test/samples/d3.js (553 KiB) -> 6.76ms -test/samples/d3.min.js (250 KiB) -> 4ms -test/samples/magic-string.js (34 KiB) -> 0.64ms -test/samples/magic-string.min.js (20 KiB) -> 0ms -test/samples/rollup.js (698 KiB) -> 8.48ms -test/samples/rollup.min.js (367 KiB) -> 5.36ms - -Warm Runs, All Samples (average of 25 runs) -test/samples/*.js (3635 KiB) -> 40.28ms -``` - -Wasm Build: -``` -Module load time -> 10ms -Cold Run, All Samples -test/samples/*.js (3635 KiB) -> 43ms - -Warm Runs (average of 25 runs) -test/samples/angular.js (1410 KiB) -> 9.32ms -test/samples/angular.min.js (303 KiB) -> 3.16ms -test/samples/d3.js (553 KiB) -> 5ms -test/samples/d3.min.js (250 KiB) -> 2.32ms -test/samples/magic-string.js (34 KiB) -> 0.16ms -test/samples/magic-string.min.js (20 KiB) -> 0ms -test/samples/rollup.js (698 KiB) -> 6.28ms -test/samples/rollup.min.js (367 KiB) -> 3.6ms - -Warm Runs, All Samples (average of 25 runs) -test/samples/*.js (3635 KiB) -> 27.76ms -``` - -### Wasm Build Steps - -The build uses docker and make, they must be installed first. - -To build the lexer wasm run `npm run build-wasm`. - -Optimization passes are run with [Binaryen](https://github.com/WebAssembly/binaryen) -prior to publish to reduce the Web Assembly footprint. - -After building the lexer wasm, build the final distribution components -(lexer.js and lexer.mjs) by running `npm run build`. - -If you need to build lib/lexer.wat (optional) you must first install -[wabt](https://github.com/WebAssembly/wabt) as a sibling folder to this -project. The wat file is then build by running `make lib/lexer.wat` - -### Creating a Release -These are the steps to create and publish a release. You will need docker -installed as well as having installed [wabt](https://github.com/WebAssembly/wabt) -as outlined above: - -- [ ] Figure out if the release should be semver patch, minor or major based on the changes since - the last release and determine the new version. -- [ ] Update the package.json version, and run a full build and test - - npm install - - npm run build - - npm run test -- [ ] Commit and tag the changes, pushing up to main and the tag - - For example - - `git tag -a 1.4.2 -m "1.4.2"` - - `git push origin tag 1.4.2` -- [ ] Create the GitHub release -- [ ] Run npm publish from an account with access (asking somebody with access - the nodejs-foundation account is an option if you don't have access. - -### License - -MIT - -[travis-url]: https://travis-ci.org/guybedford/es-module-lexer -[travis-image]: https://travis-ci.org/guybedford/es-module-lexer.svg?branch=master diff --git a/deps/cjs-module-lexer/dist/lexer.js b/deps/cjs-module-lexer/dist/lexer.js deleted file mode 100644 index e1a05b5b260684..00000000000000 --- a/deps/cjs-module-lexer/dist/lexer.js +++ /dev/null @@ -1 +0,0 @@ -"use strict";exports.init=init;exports.initSync=initSync;exports.parse=parse;let A;const B=1===new Uint8Array(new Uint16Array([1]).buffer)[0];function parse(E,g="@"){if(!A)throw new Error("Not initialized");const w=E.length+1,D=(A.__heap_base.value||A.__heap_base)+4*w-A.memory.buffer.byteLength;D>0&&A.memory.grow(Math.ceil(D/65536));const G=A.sa(w);(B?C:I)(E,new Uint16Array(A.memory.buffer,G,w));const S=A.parseCJS(G,E.length,0,0,0);if(S){const B=new Error(`Parse error ${g}${A.e()}:${E.slice(0,A.e()).split("\n").length}:${A.e()-E.lastIndexOf("\n",A.e()-1)}`);throw Object.assign(B,{idx:A.e()}),5!==S&&6!==S&&7!==S||Object.assign(B,{code:"ERR_LEXER_ESM_SYNTAX"}),B}let o=new Set,R=new Set,y=new Set;for(;A.rre();){const B=Q(E.slice(A.res(),A.ree()));B&&R.add(B)}for(;A.ru();)y.add(Q(E.slice(A.us(),A.ue())));for(;A.re();){let B=Q(E.slice(A.es(),A.ee()));void 0===B||y.has(B)||o.add(B)}return{exports:[...o],reexports:[...R]}}function Q(A){if('"'!==A[0]&&"'"!==A[0])return A;try{const B=function(A){const B=A[0];if('"'===B)try{return JSON.parse(A)}catch{}else if("'"===B&&A.length>1&&"'"===A[A.length-1]&&-1===A.indexOf('"'))try{return JSON.parse('"'+A.slice(1,-1)+'"')}catch{}let Q="",g={v:1};for(;g.v1114111)throw new SyntaxError;++B.v}while("}"!==A[B.v]);++B.v}else for(let E=0;E<4;++E)Q=16*Q+g(A[B.v]),++B.v;return String.fromCodePoint(Q)}(A,B);case"0":case"1":case"2":case"3":case"4":case"5":case"6":case"7":return function(A,B,Q){let E=A<="3"?2:1,g=+A;do{if((A=B[Q.v])<"0"||A>"7")break;g=8*g+ +A,++Q.v,--E}while(E>0);return String.fromCodePoint(g)}(Q,A,B);default:return Q}}function g(A){if(A>="0"&&A<="9")return+A;if(A>="a"&&A<="f")return A.charCodeAt(0)-87;if(A>="A"&&A<="F")return A.charCodeAt(0)-55;throw new SyntaxError}function I(A,B){const Q=A.length;let E=0;for(;E>>8}}function C(A,B){const Q=A.length;let E=0;for(;EA.charCodeAt(0))}let D;function init(){return D||(D=(async()=>{const B=await WebAssembly.compile(w()),{exports:Q}=await WebAssembly.instantiate(B);A=Q})())}function initSync(){if(A)return;const B=new WebAssembly.Module(w()),{exports:Q}=new WebAssembly.Instance(B);A=Q} \ No newline at end of file diff --git a/deps/cjs-module-lexer/dist/lexer.mjs b/deps/cjs-module-lexer/dist/lexer.mjs deleted file mode 100644 index 44a2f3eb0d1c35..00000000000000 --- a/deps/cjs-module-lexer/dist/lexer.mjs +++ /dev/null @@ -1,2 +0,0 @@ -/* cjs-module-lexer 2.2.0 */ -let A;const B=1===new Uint8Array(new Uint16Array([1]).buffer)[0];export function parse(E,g="@"){if(!A)throw new Error("Not initialized");const w=E.length+1,D=(A.__heap_base.value||A.__heap_base)+4*w-A.memory.buffer.byteLength;D>0&&A.memory.grow(Math.ceil(D/65536));const G=A.sa(w);(B?C:I)(E,new Uint16Array(A.memory.buffer,G,w));const S=A.parseCJS(G,E.length,0,0,0);if(S){const B=new Error(`Parse error ${g}${A.e()}:${E.slice(0,A.e()).split("\n").length}:${A.e()-E.lastIndexOf("\n",A.e()-1)}`);throw Object.assign(B,{idx:A.e()}),5!==S&&6!==S&&7!==S||Object.assign(B,{code:"ERR_LEXER_ESM_SYNTAX"}),B}let o=new Set,R=new Set,y=new Set;for(;A.rre();){const B=Q(E.slice(A.res(),A.ree()));B&&R.add(B)}for(;A.ru();)y.add(Q(E.slice(A.us(),A.ue())));for(;A.re();){let B=Q(E.slice(A.es(),A.ee()));void 0===B||y.has(B)||o.add(B)}return{exports:[...o],reexports:[...R]}}function Q(A){if('"'!==A[0]&&"'"!==A[0])return A;try{const B=function(A){const B=A[0];if('"'===B)try{return JSON.parse(A)}catch{}else if("'"===B&&A.length>1&&"'"===A[A.length-1]&&-1===A.indexOf('"'))try{return JSON.parse('"'+A.slice(1,-1)+'"')}catch{}let Q="",g={v:1};for(;g.v1114111)throw new SyntaxError;++B.v}while("}"!==A[B.v]);++B.v}else for(let E=0;E<4;++E)Q=16*Q+g(A[B.v]),++B.v;return String.fromCodePoint(Q)}(A,B);case"0":case"1":case"2":case"3":case"4":case"5":case"6":case"7":return function(A,B,Q){let E=A<="3"?2:1,g=+A;do{if((A=B[Q.v])<"0"||A>"7")break;g=8*g+ +A,++Q.v,--E}while(E>0);return String.fromCodePoint(g)}(Q,A,B);default:return Q}}function g(A){if(A>="0"&&A<="9")return+A;if(A>="a"&&A<="f")return A.charCodeAt(0)-87;if(A>="A"&&A<="F")return A.charCodeAt(0)-55;throw new SyntaxError}function I(A,B){const Q=A.length;let E=0;for(;E>>8}}function C(A,B){const Q=A.length;let E=0;for(;EA.charCodeAt(0))}let D;export function init(){return D||(D=(async()=>{const B=await WebAssembly.compile(w()),{exports:Q}=await WebAssembly.instantiate(B);A=Q})())}export function initSync(){if(A)return;const B=new WebAssembly.Module(w()),{exports:Q}=new WebAssembly.Instance(B);A=Q} \ No newline at end of file diff --git a/deps/cjs-module-lexer/lexer.js b/deps/cjs-module-lexer/lexer.js deleted file mode 100755 index 1614fd9e62d88f..00000000000000 --- a/deps/cjs-module-lexer/lexer.js +++ /dev/null @@ -1,1607 +0,0 @@ -let source, pos, end; -let openTokenDepth, - templateDepth, - lastTokenPos, - lastSlashWasDivision, - templateStack, - templateStackDepth, - openTokenPosStack, - openClassPosStack, - nextBraceIsClass, - starExportMap, - lastStarExportSpecifier, - _exports, - unsafeGetters, - reexports; - -function resetState () { - openTokenDepth = 0; - templateDepth = -1; - lastTokenPos = -1; - lastSlashWasDivision = false; - templateStack = new Array(1024); - templateStackDepth = 0; - openTokenPosStack = new Array(1024); - openClassPosStack = new Array(1024); - nextBraceIsClass = false; - starExportMap = Object.create(null); - lastStarExportSpecifier = null; - - _exports = new Set(); - unsafeGetters = new Set(); - reexports = new Set(); -} - -// RequireType -const Import = 0; -const ExportAssign = 1; -const ExportStar = 2; - -function parseCJS (source, name = '@') { - resetState(); - try { - parseSource(source); - } - catch (e) { - e.message += `\n at ${name}:${source.slice(0, pos).split('\n').length}:${pos - source.lastIndexOf('\n', pos - 1)}`; - e.loc = pos; - throw e; - } - const result = { exports: [..._exports].filter(expt => expt !== undefined && !unsafeGetters.has(expt)), reexports: [...reexports].filter(reexpt => reexpt !== undefined) }; - resetState(); - return result; -} - -function decode (str) { - if (str[0] === '"' || str[0] === '\'') { - try { - const decoded = scanStringLiteral(str); - // Filter to exclude non-matching UTF-16 surrogate strings - for (let i = 0; i < decoded.length; i++) { - const surrogatePrefix = decoded.charCodeAt(i) & 0xFC00; - if (surrogatePrefix < 0xD800) { - // Not a surrogate - continue; - } - else if (surrogatePrefix === 0xD800) { - // Validate surrogate pair - if ((decoded.charCodeAt(++i) & 0xFC00) !== 0xDC00) - return; - } - else { - // Out-of-range surrogate code (above 0xD800) - return; - } - } - return decoded; - } - catch {} - } - else { - return str; - } -} - -function parseSource (cjsSource) { - source = cjsSource; - pos = -1; - end = source.length - 1; - let ch = 0; - - // Handle #! - if (source.charCodeAt(0) === 35/*#*/ && source.charCodeAt(1) === 33/*!*/) { - if (source.length === 2) - return true; - pos += 2; - while (pos++ < end) { - ch = source.charCodeAt(pos); - if (ch === 10/*\n*/ || ch === 13/*\r*/) - break; - } - } - - while (pos++ < end) { - ch = source.charCodeAt(pos); - - if (ch === 32 || ch < 14 && ch > 8) - continue; - - if (openTokenDepth === 0) { - switch (ch) { - case 105/*i*/: - if (source.startsWith('mport', pos + 1) && keywordStart(pos)) - throwIfImportStatement(); - lastTokenPos = pos; - continue; - case 114/*r*/: - const startPos = pos; - if (tryParseRequire(Import) && keywordStart(startPos)) - tryBacktrackAddStarExportBinding(startPos - 1); - lastTokenPos = pos; - continue; - case 95/*_*/: - if (source.startsWith('interopRequireWildcard', pos + 1) && (keywordStart(pos) || source.charCodeAt(pos - 1) === 46/*.*/)) { - const startPos = pos; - pos += 23; - if (source.charCodeAt(pos) === 40/*(*/) { - pos++; - openTokenPosStack[openTokenDepth++] = lastTokenPos; - if (tryParseRequire(Import) && keywordStart(startPos)) { - tryBacktrackAddStarExportBinding(startPos - 1); - } - } - } - else if (source.startsWith('_export', pos + 1) && (keywordStart(pos) || source.charCodeAt(pos - 1) === 46/*.*/)) { - pos += 8; - if (source.startsWith('Star', pos)) - pos += 4; - if (source.charCodeAt(pos) === 40/*(*/) { - openTokenPosStack[openTokenDepth++] = lastTokenPos; - if (source.charCodeAt(pos + 1) === 114/*r*/) { - pos++; - tryParseRequire(ExportStar); - } - } - } - lastTokenPos = pos; - continue; - } - } - - switch (ch) { - case 101/*e*/: - if (source.startsWith('xport', pos + 1) && keywordStart(pos)) { - if (source.charCodeAt(pos + 6) === 115/*s*/) - tryParseExportsDotAssign(false); - else if (openTokenDepth === 0) - throwIfExportStatement(); - } - break; - case 99/*c*/: - if (keywordStart(pos) && source.startsWith('lass', pos + 1) && isBrOrWs(source.charCodeAt(pos + 5))) - nextBraceIsClass = true; - break; - case 109/*m*/: - if (source.startsWith('odule', pos + 1) && keywordStart(pos)) - tryParseModuleExportsDotAssign(); - break; - case 79/*O*/: - if (source.startsWith('bject', pos + 1) && keywordStart(pos)) - tryParseObjectDefineOrKeys(openTokenDepth === 0); - break; - case 40/*(*/: - openTokenPosStack[openTokenDepth++] = lastTokenPos; - break; - case 41/*)*/: - if (openTokenDepth === 0) - throw new Error('Unexpected closing bracket.'); - openTokenDepth--; - break; - case 123/*{*/: - openClassPosStack[openTokenDepth] = nextBraceIsClass; - nextBraceIsClass = false; - openTokenPosStack[openTokenDepth++] = lastTokenPos; - break; - case 125/*}*/: - if (openTokenDepth === 0) - throw new Error('Unexpected closing brace.'); - if (openTokenDepth-- === templateDepth) { - templateDepth = templateStack[--templateStackDepth]; - templateString(); - } - else { - if (templateDepth !== -1 && openTokenDepth < templateDepth) - throw new Error('Unexpected closing brace.'); - } - break; - case 60/*>*/: - // TODO: + [6.1.7 Array Index]: https://tc39.es/ecma262/#integer-index [Addons]: addons.md @@ -1320,10 +1320,10 @@ resolution for ESM specifiers is [commonjs-extension-resolution-loader][]. [`process.dlopen`]: process.md#processdlopenmodule-filename-flags [`require(esm)`]: modules.md#loading-ecmascript-modules-using-require [`url.fileURLToPath()`]: url.md#urlfileurltopathurl-options -[cjs-module-lexer]: https://github.com/nodejs/cjs-module-lexer/tree/2.0.0 [commonjs-extension-resolution-loader]: https://github.com/nodejs/loaders-test/tree/main/commonjs-extension-resolution-loader [custom https loader]: module.md#import-from-https [import.meta.resolve]: #importmetaresolvespecifier +[merve]: https://github.com/anonrig/merve/tree/v1.0.0 [percent-encoded]: url.md#percent-encoding-in-urls [special scheme]: https://url.spec.whatwg.org/#special-scheme [status code]: process.md#exit-codes diff --git a/doc/api/packages.md b/doc/api/packages.md index bc28dd5f839557..02bf9664dae0b1 100644 --- a/doc/api/packages.md +++ b/doc/api/packages.md @@ -187,7 +187,7 @@ or `import()` expressions (available in both CommonJS and ES Modules): * Any other file extensions will result in a [`ERR_UNKNOWN_FILE_EXTENSION`][] error. Additional file extensions can be facilitated via [customization hooks][]. * `import`/`import()` can be used to load JavaScript [CommonJS modules][commonjs]. - Such modules are passed through the `cjs-module-lexer` to try to identify named + Such modules are passed through [merve][] to try to identify named exports, which are available if they can be determined through static analysis. Regardless of how a module is requested, the resolution and loading process can be customized @@ -1184,6 +1184,7 @@ This field defines [subpath imports][] for the current package. [folders as modules]: modules.md#folders-as-modules [import maps]: https://github.com/WICG/import-maps [load ECMAScript modules from CommonJS modules]: modules.md#loading-ecmascript-modules-using-require +[merve]: https://github.com/anonrig/merve [packages folder mapping]: https://github.com/WICG/import-maps#packages-via-trailing-slashes [self-reference]: #self-referencing-a-package-using-its-name [subpath exports]: #subpath-exports diff --git a/doc/api/process.md b/doc/api/process.md index e9d9ac543e1584..2dc9b1c13f35a9 100644 --- a/doc/api/process.md +++ b/doc/api/process.md @@ -4449,32 +4449,35 @@ console.log(versions); Will generate an object similar to: ```console -{ node: '23.0.0', - acorn: '8.11.3', - ada: '2.7.8', - ares: '1.28.1', - base64: '0.5.2', - brotli: '1.1.0', - cjs_module_lexer: '1.2.2', - cldr: '45.0', - icu: '75.1', - llhttp: '9.2.1', - modules: '127', - napi: '9', - nghttp2: '1.61.0', - nghttp3: '0.7.0', - ngtcp2: '1.3.0', - openssl: '3.0.13+quic', - simdjson: '3.8.0', - simdutf: '5.2.4', - sqlite: '3.46.0', - tz: '2024a', - undici: '6.13.0', - unicode: '15.1', - uv: '1.48.0', - uvwasi: '0.0.20', - v8: '12.4.254.14-node.11', - zlib: '1.3.0.1-motley-7d77fb7' } +{ node: '26.0.0-pre', + acorn: '8.15.0', + ada: '3.4.1', + amaro: '1.1.5', + ares: '1.34.6', + brotli: '1.2.0', + merve: '1.0.0', + cldr: '48.0', + icu: '78.2', + llhttp: '9.3.0', + modules: '144', + napi: '10', + nbytes: '0.1.1', + ncrypto: '0.0.1', + nghttp2: '1.68.0', + nghttp3: '', + ngtcp2: '', + openssl: '3.5.4', + simdjson: '4.2.4', + simdutf: '7.3.3', + sqlite: '3.51.2', + tz: '2025c', + undici: '7.18.2', + unicode: '17.0', + uv: '1.51.0', + uvwasi: '0.0.23', + v8: '14.3.127.18-node.10', + zlib: '1.3.1-e00f703', + zstd: '1.5.7' } ``` ## Exit codes diff --git a/doc/contributing/maintaining/maintaining-cjs-module-lexer.md b/doc/contributing/maintaining/maintaining-cjs-module-lexer.md deleted file mode 100644 index a657f6b190d7ea..00000000000000 --- a/doc/contributing/maintaining/maintaining-cjs-module-lexer.md +++ /dev/null @@ -1,59 +0,0 @@ -# Maintaining cjs-module-lexer - -The [cjs-module-lexer](https://github.com/nodejs/node/tree/HEAD/deps/cjs-module-lexer) -dependency is used within the Node.js ESM implementation to detect the -named exports of a CommonJS module. - -It is used within -[`node:internal/modules/esm/translators`](https://github.com/nodejs/node/blob/HEAD/lib/internal/modules/esm/translators.js) -in which both `internal/deps/cjs-module-lexer/lexer` and -`internal/deps/cjs-module-lexer/dist/lexer` are required and used. - -`internal/deps/cjs-module-lexer/lexer` -is a regular JavaScript implementation that is -used when WebAssembly is not available on a platform. -`internal/deps/cjs-module-lexer/dist/lexer` is a faster -implementation using WebAssembly which is generated from a -C based implementation. These two paths -resolve to the files in `deps/cjs-module-lexer` due to their -inclusion in the `deps_files` entry in -[node.gyp](https://github.com/nodejs/node/blob/main/node.gyp). - -The two different versions of lexer.js are maintained in the -[nodejs/cjs-module-lexer][] project. - -In order to update the Node.js dependencies to use to a newer version -of cjs-module-lexer, complete the following steps: - -* Clone [nodejs/cjs-module-lexer][] - and check out the version that you want Node.js to use. -* Follow the WASM build steps outlined in - [wasm-build-steps](https://github.com/nodejs/cjs-module-lexer#wasm-build-steps). - This will generate the WASM based dist/lexer.js file. -* Preserving the same directory structure, copy the following files over - to `deps/cjs-module-lexer` directory where you have checked out Node.js. - -```text -├── CHANGELOG.md -├── dist -│ ├── lexer.js -│ └── lexer.mjs -├── lexer.js -├── LICENSE -├── package.json -└── README.md -``` - -* Update the link to the cjs-module-lexer in the list at the end of - [doc/api/esm.md](../../api/esm.md) - to point to the updated version. - -* Create a PR, adding the files in the deps/cjs-module-lexer that - were modified. - -If updates are needed to cjs-module-lexer for Node.js, first PR -those updates into -[nodejs/cjs-module-lexer][], -request a release and then pull in the updated version once available. - -[nodejs/cjs-module-lexer]: https://github.com/nodejs/cjs-module-lexer diff --git a/doc/contributing/maintaining/maintaining-dependencies.md b/doc/contributing/maintaining/maintaining-dependencies.md index 7cc3e483d268a8..83fa827e1443b5 100644 --- a/doc/contributing/maintaining/maintaining-dependencies.md +++ b/doc/contributing/maintaining/maintaining-dependencies.md @@ -14,7 +14,7 @@ This a list of all the dependencies: * [base64][] * [brotli][] * [c-ares][] -* [cjs-module-lexer][] +* [merve][] * [corepack][] * [googletest][] * [histogram][] @@ -238,12 +238,12 @@ used for the homonym generic-purpose lossless compression algorithm. The [c-ares](https://github.com/c-ares/c-ares) is a C library for asynchronous DNS requests. -### cjs-module-lexer +### merve -The [cjs-module-lexer](https://github.com/nodejs/node/tree/HEAD/deps/cjs-module-lexer) +The [merve](https://github.com/nodejs/node/tree/HEAD/deps/merve) dependency is used within the Node.js ESM implementation to detect the named exports of a CommonJS module. -See [maintaining-cjs-module-lexer][] for more information. +See [maintaining-merve][] for more information. ### corepack @@ -406,7 +406,6 @@ according to [RFC 8878](https://datatracker.ietf.org/doc/html/rfc8878). [base64]: #base64 [brotli]: #brotli [c-ares]: #c-ares -[cjs-module-lexer]: #cjs-module-lexer [corepack]: #corepack [dependency-update-action]: ../../../.github/workflows/tools.yml [googletest]: #googletest @@ -416,11 +415,12 @@ according to [RFC 8878](https://datatracker.ietf.org/doc/html/rfc8878). [libuv]: #libuv [llhttp]: #llhttp [maintaining-V8]: ./maintaining-V8.md -[maintaining-cjs-module-lexer]: ./maintaining-cjs-module-lexer.md [maintaining-http]: ./maintaining-http.md [maintaining-icu]: ./maintaining-icu.md +[maintaining-merve]: ./maintaining-merve.md [maintaining-openssl]: ./maintaining-openssl.md [maintaining-web-assembly]: ./maintaining-web-assembly.md +[merve]: #merve [minimatch]: #minimatch [nghttp2]: #nghttp2 [nghttp3]: #nghttp3 diff --git a/doc/contributing/maintaining/maintaining-merve.md b/doc/contributing/maintaining/maintaining-merve.md new file mode 100644 index 00000000000000..66b9d61e5b1311 --- /dev/null +++ b/doc/contributing/maintaining/maintaining-merve.md @@ -0,0 +1,32 @@ +# Maintaining merve + +The [merve](https://github.com/nodejs/node/tree/HEAD/deps/merve) +dependency is used within the Node.js ESM implementation to detect the +named exports of a CommonJS module. + +It is used within +[`node:internal/modules/esm/translators`](https://github.com/nodejs/node/blob/HEAD/lib/internal/modules/esm/translators.js) +where it is exposed via an internal binding. + +## Updating merve + +The `tools/dep_updaters/update-merve.sh` script automates the update of the +merve dependency. It fetches the latest release from GitHub and updates the +files in the `deps/merve` directory. + +To update merve manually: + +* Check the [merve releases][] for a new version. +* Download the latest single-header release. +* Replace the files in `deps/merve` (preserving `merve.gyp`). +* Update the link to merve in the list at the end of + [doc/api/esm.md](../../api/esm.md) + to point to the updated version. +* Create a PR adding the files in the deps/merve that were modified. + +If updates are needed to merve for Node.js, first PR those updates into +[anonrig/merve][], +request a release and then pull in the updated version once available. + +[anonrig/merve]: https://github.com/anonrig/merve +[merve releases]: https://github.com/anonrig/merve/releases diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index 87f271adcd3e50..6658b302c19bd4 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -6,7 +6,6 @@ const { JSONParse, ObjectAssign, ObjectPrototypeHasOwnProperty, - SafeArrayIterator, SafeMap, SafeSet, SafeWeakMap, @@ -69,23 +68,7 @@ function getSource(url) { return getSourceSync(url); } -/** @type {import('deps/cjs-module-lexer/lexer.js').parse} */ -let cjsParse; -/** - * Initializes the CommonJS module lexer parser using the JavaScript version. - */ -function initCJSParseSync() { - if (cjsParse === undefined) { - if (typeof WebAssembly !== 'undefined') { - const { parse, initSync } = require('internal/deps/cjs-module-lexer/dist/lexer'); - initSync(); - cjsParse = parse; - } else { - const { parse } = require('internal/deps/cjs-module-lexer/lexer'); - cjsParse = parse; - } - } -} +const { parse: cjsParse } = internalBinding('cjs_lexer'); const translators = new SafeMap(); exports.translators = translators; @@ -315,24 +298,18 @@ function createCJSNoSourceModuleWrap(url, parentURL) { } translators.set('commonjs-sync', function requireCommonJS(url, translateContext, parentURL) { - initCJSParseSync(); - return createCJSModuleWrap(url, translateContext, parentURL, loadCJSModuleWithModuleLoad); }); // Handle CommonJS modules referenced by `require` calls. // This translator function must be sync, as `require` is sync. translators.set('require-commonjs', (url, translateContext, parentURL) => { - initCJSParseSync(); - assert(cjsParse); - return createCJSModuleWrap(url, translateContext, parentURL); }); // Handle CommonJS modules referenced by `require` calls. // This translator function must be sync, as `require` is sync. translators.set('require-commonjs-typescript', (url, translateContext, parentURL) => { - assert(cjsParse); translateContext.source = stripTypeScriptModuleTypes(stringify(translateContext.source), url); return createCJSModuleWrap(url, translateContext, parentURL); }); @@ -346,10 +323,6 @@ function loadCJSModuleWithModuleLoad(module, source, url, filename, isMain) { // Handle CommonJS modules referenced by `import` statements or expressions, // or as the initial entry point when the ESM loader handles a CommonJS entry. translators.set('commonjs', function commonjsStrategy(url, translateContext, parentURL) { - if (!cjsParse) { - initCJSParseSync(); - } - // For backward-compatibility, it's possible to return a nullish value for // CJS source associated with a `file:` URL - that usually means the source is not // customized (is loaded by default load) or the hook author wants it to be reloaded @@ -411,15 +384,7 @@ function cjsPreparseModuleExports(filename, source, format) { module[kModuleSource] = source; debug(`Preparsing exports of ${filename}`); - let exports, reexports; - try { - ({ exports, reexports } = cjsParse(source || '')); - } catch { - exports = []; - reexports = []; - } - - const exportNames = new SafeSet(new SafeArrayIterator(exports)); + const { 0: exportNames, 1: reexports } = cjsParse(source); // Set first for cycles. module[kModuleExportNames] = exportNames; diff --git a/node.gyp b/node.gyp index 55adfc7bb15a93..e1cd8d23c20845 100644 --- a/node.gyp +++ b/node.gyp @@ -112,6 +112,7 @@ 'src/node_config.cc', 'src/node_config_file.cc', 'src/node_constants.cc', + 'src/node_cjs_lexer.cc', 'src/node_contextify.cc', 'src/node_credentials.cc', 'src/node_debug.cc', diff --git a/node.gypi b/node.gypi index 32889a9c774353..ecf118efc2cad2 100644 --- a/node.gypi +++ b/node.gypi @@ -226,6 +226,10 @@ 'dependencies': [ 'deps/ada/ada.gyp:ada' ], }], + [ 'node_shared_merve=="false"', { + 'dependencies': [ 'deps/merve/merve.gyp:merve' ], + }], + [ 'node_shared_simdjson=="false"', { 'dependencies': [ 'deps/simdjson/simdjson.gyp:simdjson' ], }], diff --git a/src/cjs_module_lexer_version.h b/src/cjs_module_lexer_version.h deleted file mode 100644 index c734bdcc9ba184..00000000000000 --- a/src/cjs_module_lexer_version.h +++ /dev/null @@ -1,6 +0,0 @@ -// This is an auto generated file, please do not edit. -// Refer to tools/dep_updaters/update-cjs-module-lexer.sh -#ifndef SRC_CJS_MODULE_LEXER_VERSION_H_ -#define SRC_CJS_MODULE_LEXER_VERSION_H_ -#define CJS_MODULE_LEXER_VERSION "2.2.0" -#endif // SRC_CJS_MODULE_LEXER_VERSION_H_ diff --git a/src/node_binding.cc b/src/node_binding.cc index 3b284583d6ccc9..740706e917b7d2 100644 --- a/src/node_binding.cc +++ b/src/node_binding.cc @@ -43,6 +43,7 @@ V(buffer) \ V(builtins) \ V(cares_wrap) \ + V(cjs_lexer) \ V(config) \ V(constants) \ V(contextify) \ diff --git a/src/node_builtins.cc b/src/node_builtins.cc index db83c46c81f767..f6e0e81c2ed16d 100644 --- a/src/node_builtins.cc +++ b/src/node_builtins.cc @@ -46,18 +46,6 @@ using v8::Value; BuiltinLoader::BuiltinLoader() : config_(GetConfig()), code_cache_(std::make_shared()) { LoadJavaScriptSource(); -#ifdef NODE_SHARED_BUILTIN_CJS_MODULE_LEXER_LEXER_PATH - AddExternalizedBuiltin( - "internal/deps/cjs-module-lexer/lexer", - STRINGIFY(NODE_SHARED_BUILTIN_CJS_MODULE_LEXER_LEXER_PATH)); -#endif // NODE_SHARED_BUILTIN_CJS_MODULE_LEXER_LEXER_PATH - -#ifdef NODE_SHARED_BUILTIN_CJS_MODULE_LEXER_DIST_LEXER_PATH - AddExternalizedBuiltin( - "internal/deps/cjs-module-lexer/dist/lexer", - STRINGIFY(NODE_SHARED_BUILTIN_CJS_MODULE_LEXER_DIST_LEXER_PATH)); -#endif // NODE_SHARED_BUILTIN_CJS_MODULE_LEXER_DIST_LEXER_PATH - #ifdef NODE_SHARED_BUILTIN_UNDICI_UNDICI_PATH AddExternalizedBuiltin("internal/deps/undici/undici", STRINGIFY(NODE_SHARED_BUILTIN_UNDICI_UNDICI_PATH)); @@ -126,9 +114,6 @@ BuiltinLoader::BuiltinCategories BuiltinLoader::GetBuiltinCategories() const { "internal/main/" }; - builtin_categories.can_be_required.emplace( - "internal/deps/cjs-module-lexer/lexer"); - builtin_categories.cannot_be_required = std::set { #if !HAVE_INSPECTOR "inspector", "inspector/promises", "internal/util/inspector", diff --git a/src/node_cjs_lexer.cc b/src/node_cjs_lexer.cc new file mode 100644 index 00000000000000..1b44dec9174841 --- /dev/null +++ b/src/node_cjs_lexer.cc @@ -0,0 +1,107 @@ +#include "env-inl.h" +#include "node_external_reference.h" +#include "simdutf.h" +#include "util-inl.h" +#include "v8.h" + +#include "merve.h" + +namespace node { +namespace cjs_lexer { + +using v8::Array; +using v8::Context; +using v8::FunctionCallbackInfo; +using v8::Isolate; +using v8::Local; +using v8::LocalVector; +using v8::NewStringType; +using v8::Object; +using v8::Set; +using v8::String; +using v8::Value; + +// Create a V8 string from an export_string variant, using fast path for ASCII +inline Local CreateString(Isolate* isolate, + const lexer::export_string& str) { + std::string_view sv = lexer::get_string_view(str); + + if (simdutf::validate_ascii(sv.data(), sv.size())) { + return String::NewFromOneByte(isolate, + reinterpret_cast(sv.data()), + NewStringType::kInternalized, + static_cast(sv.size())) + .ToLocalChecked(); + } + + return String::NewFromUtf8(isolate, + sv.data(), + NewStringType::kInternalized, + static_cast(sv.size())) + .ToLocalChecked(); +} + +void Parse(const FunctionCallbackInfo& args) { + Isolate* isolate = args.GetIsolate(); + Local context = isolate->GetCurrentContext(); + + // Handle null, undefined, or missing argument by defaulting to empty string + if (args.Length() < 1 || args[0]->IsNullOrUndefined()) { + Local result_elements[] = {Set::New(isolate), + Array::New(isolate, 0)}; + args.GetReturnValue().Set(Array::New(isolate, result_elements, 2)); + return; + } + + CHECK(args[0]->IsString()); + + Utf8Value source(isolate, args[0]); + + auto result = lexer::parse_commonjs(source.ToStringView()); + + if (!result.has_value()) { + Local result_elements[] = {Set::New(isolate), + Array::New(isolate, 0)}; + args.GetReturnValue().Set(Array::New(isolate, result_elements, 2)); + return; + } + + const auto& analysis = result.value(); + + // Convert exports to JS Set + Local exports_set = Set::New(isolate); + for (const auto& exp : analysis.exports) { + exports_set->Add(context, CreateString(isolate, exp)).ToLocalChecked(); + } + + // Convert reexports to JS array using batch creation + LocalVector reexports_vec(isolate); + reexports_vec.reserve(analysis.re_exports.size()); + for (const auto& reexp : analysis.re_exports) { + reexports_vec.push_back(CreateString(isolate, reexp)); + } + + // Create result array [exports (Set), reexports (Array)] + Local result_elements[] = { + exports_set, + Array::New(isolate, reexports_vec.data(), reexports_vec.size())}; + args.GetReturnValue().Set(Array::New(isolate, result_elements, 2)); +} + +void Initialize(Local target, + Local unused, + Local context, + void* priv) { + SetMethodNoSideEffect(context, target, "parse", Parse); +} + +void RegisterExternalReferences(ExternalReferenceRegistry* registry) { + registry->Register(Parse); +} + +} // namespace cjs_lexer +} // namespace node + +NODE_BINDING_CONTEXT_AWARE_INTERNAL(cjs_lexer, node::cjs_lexer::Initialize) +NODE_BINDING_EXTERNAL_REFERENCE(cjs_lexer, + node::cjs_lexer::RegisterExternalReferences) diff --git a/src/node_metadata.cc b/src/node_metadata.cc index 0b7a2a306a4d0b..1458bf48d6b7a3 100644 --- a/src/node_metadata.cc +++ b/src/node_metadata.cc @@ -4,8 +4,8 @@ #include "amaro_version.h" #include "ares.h" #include "brotli/encode.h" -#include "cjs_module_lexer_version.h" #include "llhttp.h" +#include "merve.h" #include "nbytes.h" #include "nghttp2/nghttp2ver.h" #include "node.h" @@ -128,7 +128,7 @@ Metadata::Versions::Versions() { #endif acorn = ACORN_VERSION; - cjs_module_lexer = CJS_MODULE_LEXER_VERSION; + merve = MERVE_VERSION; uvwasi = UVWASI_VERSION_STRING; zstd = ZSTD_VERSION_STRING; diff --git a/src/node_metadata.h b/src/node_metadata.h index 4c0ef17685b868..8703e4315a999d 100644 --- a/src/node_metadata.h +++ b/src/node_metadata.h @@ -64,7 +64,7 @@ namespace node { V(nghttp3) \ NODE_VERSIONS_KEY_AMARO(V) \ NODE_VERSIONS_KEY_UNDICI(V) \ - V(cjs_module_lexer) + V(merve) #if HAVE_OPENSSL #define NODE_VERSIONS_KEY_CRYPTO(V) V(openssl) V(ncrypto) diff --git a/test/parallel/test-process-versions.js b/test/parallel/test-process-versions.js index a5f23fb94de3fc..b670ff37ac6f38 100644 --- a/test/parallel/test-process-versions.js +++ b/test/parallel/test-process-versions.js @@ -4,7 +4,6 @@ const assert = require('assert'); // Import of pure js (non-shared) deps for comparison const acorn = require('../../deps/acorn/acorn/package.json'); -const cjs_module_lexer = require('../../deps/cjs-module-lexer/src/package.json'); const expected_keys = [ 'ares', @@ -24,8 +23,8 @@ const expected_keys = [ 'simdjson', 'simdutf', 'ada', - 'cjs_module_lexer', 'nbytes', + 'merve', ]; @@ -70,6 +69,7 @@ assert.match(process.versions.acorn, commonTemplate); assert.match(process.versions.ares, commonTemplate); assert.match(process.versions.brotli, commonTemplate); assert.match(process.versions.llhttp, commonTemplate); +assert.match(process.versions.merve, commonTemplate); assert.match(process.versions.node, commonTemplate); assert.match(process.versions.uv, commonTemplate); assert.match(process.versions.nbytes, commonTemplate); @@ -85,7 +85,6 @@ assert.match( /^\d+\.\d+\.\d+(?:\.\d+)?-node\.\d+(?: \(candidate\))?$/ ); assert.match(process.versions.modules, /^\d+$/); -assert.match(process.versions.cjs_module_lexer, commonTemplate); if (common.hasCrypto) { const { hasOpenSSL3 } = require('../common/crypto'); @@ -120,5 +119,3 @@ if (hasUndici) { const expectedAcornVersion = acorn.version; assert.strictEqual(process.versions.acorn, expectedAcornVersion); -const expectedCjsModuleLexerVersion = cjs_module_lexer.version; -assert.strictEqual(process.versions.cjs_module_lexer, expectedCjsModuleLexerVersion); diff --git a/tools/dep_updaters/update-cjs-module-lexer.sh b/tools/dep_updaters/update-cjs-module-lexer.sh deleted file mode 100755 index 1d12546ab45deb..00000000000000 --- a/tools/dep_updaters/update-cjs-module-lexer.sh +++ /dev/null @@ -1,109 +0,0 @@ -#!/bin/sh -set -e -# Shell script to update cjs-module-lexer in the source tree to a specific version - -BASE_DIR=$(cd "$(dirname "$0")/../.." && pwd) - -DEPS_DIR="$BASE_DIR/deps" -[ -z "$NODE" ] && NODE="$BASE_DIR/out/Release/node" -[ -x "$NODE" ] || NODE=$(command -v node) - -NPM="$DEPS_DIR/npm/bin/npm-cli.js" - -# shellcheck disable=SC1091 -. "$BASE_DIR/tools/dep_updaters/utils.sh" - -NEW_VERSION="$("$NODE" --input-type=module <<'EOF' -const res = await fetch('https://api.github.com/repos/nodejs/cjs-module-lexer/tags', - process.env.GITHUB_TOKEN && { - headers: { - "Authorization": `Bearer ${process.env.GITHUB_TOKEN}` - }, - }); -if (!res.ok) throw new Error(`FetchError: ${res.status} ${res.statusText}`, { cause: res }); -const tags = await res.json(); -const { name } = tags.at(0) -console.log(name); -EOF -)" - - -CURRENT_VERSION=$("$NODE" -p "require('./deps/cjs-module-lexer/src/package.json').version") - -# This function exit with 0 if new version and current version are the same -compare_dependency_version "cjs-module-lexer" "$NEW_VERSION" "$CURRENT_VERSION" - -echo "Making temporary workspace" - -WORKSPACE=$(mktemp -d 2> /dev/null || mktemp -d -t 'tmp') - -cleanup () { - EXIT_CODE=$? - [ -d "$WORKSPACE" ] && rm -rf "$WORKSPACE" - exit $EXIT_CODE -} - -trap cleanup INT TERM EXIT - -CJS_MODULE_LEXER_ZIP="cjs-module-lexer-$NEW_VERSION" - -# remove existing source and built files -rm -rf "$DEPS_DIR/cjs-module-lexer/src" -rm -rf "$DEPS_DIR/cjs-module-lexer/dist" -rm "$DEPS_DIR/cjs-module-lexer/lexer.js" - -cd "$WORKSPACE" - -curl -sL -o "$CJS_MODULE_LEXER_ZIP.zip" "https://github.com/nodejs/cjs-module-lexer/archive/refs/tags/$NEW_VERSION.zip" - -log_and_verify_sha256sum "cjs-module-lexer" "$CJS_MODULE_LEXER_ZIP.zip" - -echo "Unzipping..." -unzip "$CJS_MODULE_LEXER_ZIP.zip" -d "src" -mv "src/$CJS_MODULE_LEXER_ZIP" "$DEPS_DIR/cjs-module-lexer/src" -rm "$CJS_MODULE_LEXER_ZIP.zip" -cd "$ROOT" - -( - # remove components we don't need to keep in nodejs/deps - # these are files that we don't need to build from source - cd "$DEPS_DIR/cjs-module-lexer/src" - rm -rf bench - rm -rf .github || true - rm -rf test - rm .gitignore - rm .travis.yml - rm CODE_OF_CONDUCT.md - rm CONTRIBUTING.md - - # Rebuild components from source - rm lib/*.* - "$NODE" "$NPM" install --ignore-scripts - "$NODE" "$NPM" run build-wasm - "$NODE" "$NPM" run build - "$NODE" "$NPM" prune --production - - # remove files we don't need in Node.js - rm -rf node_modules - - # copy over the built files to the expected locations - mv dist .. - cp lexer.js .. - cp LICENSE .. - cp README.md .. -) - -# update cjs_module_lexer_version.h -cat > "$BASE_DIR/src/cjs_module_lexer_version.h" << EOL -// This is an auto generated file, please do not edit. -// Refer to tools/dep_updaters/update-cjs-module-lexer.sh -#ifndef SRC_CJS_MODULE_LEXER_VERSION_H_ -#define SRC_CJS_MODULE_LEXER_VERSION_H_ -#define CJS_MODULE_LEXER_VERSION "$NEW_VERSION" -#endif // SRC_CJS_MODULE_LEXER_VERSION_H_ -EOL - -# Update the version number on maintaining-dependencies.md -# and print the new version as the last line of the script as we need -# to add it to $GITHUB_ENV variable -finalize_version_update "cjs-module-lexer" "$NEW_VERSION" "src/cjs_module_lexer_version.h" diff --git a/tools/dep_updaters/update-merve.sh b/tools/dep_updaters/update-merve.sh new file mode 100644 index 00000000000000..ec0525aadb48b6 --- /dev/null +++ b/tools/dep_updaters/update-merve.sh @@ -0,0 +1,65 @@ +#!/bin/sh +set -e +# Shell script to update merve in the source tree to a specific version + +BASE_DIR=$(cd "$(dirname "$0")/../.." && pwd) +DEPS_DIR="$BASE_DIR/deps" +[ -z "$NODE" ] && NODE="$BASE_DIR/out/Release/node" +[ -x "$NODE" ] || NODE=$(command -v node) + +# shellcheck disable=SC1091 +. "$BASE_DIR/tools/dep_updaters/utils.sh" + +NEW_VERSION="$("$NODE" --input-type=module <<'EOF' +const res = await fetch('https://api.github.com/repos/anonrig/merve/releases/latest', + process.env.GITHUB_TOKEN && { + headers: { + "Authorization": `Bearer ${process.env.GITHUB_TOKEN}` + }, + }); +if (!res.ok) throw new Error(`FetchError: ${res.status} ${res.statusText}`, { cause: res }); +const { tag_name } = await res.json(); +console.log(tag_name.replace('v', '')); +EOF +)" + +CURRENT_VERSION=$(grep "#define MERVE_VERSION" "$DEPS_DIR/merve/merve.h" | sed -n "s/^.*VERSION \"\(.*\)\"/\1/p") + +# This function exit with 0 if new version and current version are the same +compare_dependency_version "merve" "$NEW_VERSION" "$CURRENT_VERSION" + +echo "Making temporary workspace..." + +WORKSPACE=$(mktemp -d 2> /dev/null || mktemp -d -t 'tmp') + +cleanup () { + EXIT_CODE=$? + [ -d "$WORKSPACE" ] && rm -rf "$WORKSPACE" + exit $EXIT_CODE +} + +trap cleanup INT TERM EXIT + +MERVE_REF="v$NEW_VERSION" +MERVE_ZIP="singleheader.zip" +MERVE_LICENSE="LICENSE-MIT" + +cd "$WORKSPACE" + +echo "Fetching merve source archive..." +curl -sL -o "$MERVE_ZIP" "https://github.com/anonrig/merve/releases/download/$MERVE_REF/singleheader.zip" +log_and_verify_sha256sum "merve" "$MERVE_ZIP" +unzip "$MERVE_ZIP" +rm "$MERVE_ZIP" + +curl -sL -o "$MERVE_LICENSE" "https://raw.githubusercontent.com/anonrig/merve/HEAD/LICENSE-MIT" + +echo "Replacing existing merve (except GYP build files)" +mv "$DEPS_DIR/merve/merve.gyp" "$WORKSPACE/" +rm -rf "$DEPS_DIR/merve" +mv "$WORKSPACE" "$DEPS_DIR/merve" + +# Update the version number on maintaining-dependencies.md +# and print the new version as the last line of the script as we need +# to add it to $GITHUB_ENV variable +finalize_version_update "merve" "$NEW_VERSION" diff --git a/tools/license-builder.sh b/tools/license-builder.sh index c54e4a9197739a..7137708699ec86 100755 --- a/tools/license-builder.sh +++ b/tools/license-builder.sh @@ -34,8 +34,8 @@ licenseText="$(cat "${rootdir}/deps/acorn/acorn/LICENSE")" addlicense "Acorn" "deps/acorn" "$licenseText" licenseText="$(cat "${rootdir}/deps/cares/LICENSE.md")" addlicense "c-ares" "deps/cares" "$licenseText" -licenseText="$(cat "${rootdir}/deps/cjs-module-lexer/LICENSE")" -addlicense "cjs-module-lexer" "deps/cjs-module-lexer" "$licenseText" +licenseText="$(cat "${rootdir}/deps/merve/LICENSE-MIT")" +addlicense "merve" "deps/merve" "$licenseText" licenseText="$(cat "${rootdir}/deps/v8/third_party/ittapi/LICENSES/BSD-3-Clause.txt")" addlicense "ittapi" "deps/v8/third_party/ittapi" "$licenseText" licenseText="$(cat "${rootdir}/deps/amaro/LICENSE.md")" diff --git a/typings/globals.d.ts b/typings/globals.d.ts index 060494300c1caa..739f3d9a534026 100644 --- a/typings/globals.d.ts +++ b/typings/globals.d.ts @@ -2,6 +2,7 @@ import { AsyncContextFrameBinding } from './internalBinding/async_context_frame' import { AsyncWrapBinding } from './internalBinding/async_wrap'; import { BlobBinding } from './internalBinding/blob'; import { BufferBinding } from './internalBinding/buffer'; +import { CJSLexerBinding } from './internalBinding/cjs_lexer'; import { ConfigBinding } from './internalBinding/config'; import { ConstantsBinding } from './internalBinding/constants'; import { DebugBinding } from './internalBinding/debug'; @@ -36,6 +37,7 @@ interface InternalBindingMap { async_wrap: AsyncWrapBinding; blob: BlobBinding; buffer: BufferBinding; + cjs_lexer: CJSLexerBinding; config: ConfigBinding; constants: ConstantsBinding; debug: DebugBinding; diff --git a/typings/internalBinding/cjs_lexer.d.ts b/typings/internalBinding/cjs_lexer.d.ts new file mode 100644 index 00000000000000..70f44a4c5c64f4 --- /dev/null +++ b/typings/internalBinding/cjs_lexer.d.ts @@ -0,0 +1,5 @@ +export type CJSLexerParseResult = [exports: Set, reexports: string[]]; + +export interface CJSLexerBinding { + parse(source?: string | null): CJSLexerParseResult; +}