Skip to content

Commit cf6ded5

Browse files
nodejs-github-bottargos
authored andcommittedOct 2, 2024
deps: update cjs-module-lexer to 1.4.0
PR-URL: #54713 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michael Dawson <midawson@redhat.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 77c702c commit cf6ded5

12 files changed

+735
-564
lines changed
 

‎deps/cjs-module-lexer/.gitignore

-11
This file was deleted.

‎deps/cjs-module-lexer/CHANGELOG.md

-40
This file was deleted.

‎deps/cjs-module-lexer/LICENSE

100755100644
+10-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
MIT License
2-
-----------
3-
4-
Copyright (C) 2018-2020 Guy Bedford
5-
6-
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:
7-
8-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9-
10-
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.
1+
MIT License
2+
-----------
3+
4+
Copyright (C) 2018-2020 Guy Bedford
5+
6+
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:
7+
8+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9+
10+
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.

‎deps/cjs-module-lexer/README.md

100755100644
+462-453
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
"use strict";
2+
3+
exports.init = init;
4+
exports.parse = parse;
5+
let wasm;
6+
const isLE = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
7+
function parse(source, name = '@') {
8+
if (!wasm) throw new Error('Not initialized');
9+
const len = source.length + 1;
10+
11+
// need 2 bytes per code point plus analysis space so we double again
12+
const extraMem = (wasm.__heap_base.value || wasm.__heap_base) + len * 4 - wasm.memory.buffer.byteLength;
13+
if (extraMem > 0) wasm.memory.grow(Math.ceil(extraMem / 65536));
14+
const addr = wasm.sa(len);
15+
(isLE ? copyLE : copyBE)(source, new Uint16Array(wasm.memory.buffer, addr, len));
16+
const err_code = wasm.parseCJS(addr, source.length, 0, 0, 0);
17+
if (err_code) {
18+
const err = new Error(`Parse error ${name}${wasm.e()}:${source.slice(0, wasm.e()).split('\n').length}:${wasm.e() - source.lastIndexOf('\n', wasm.e() - 1)}`);
19+
Object.assign(err, {
20+
idx: wasm.e()
21+
});
22+
if (err_code === 5 || err_code === 6 || err_code === 7) Object.assign(err, {
23+
code: 'ERR_LEXER_ESM_SYNTAX'
24+
});
25+
throw err;
26+
}
27+
let exports = new Set(),
28+
reexports = new Set(),
29+
unsafeGetters = new Set();
30+
while (wasm.rre()) {
31+
const reexptStr = decode(source.slice(wasm.res(), wasm.ree()));
32+
if (reexptStr) reexports.add(reexptStr);
33+
}
34+
while (wasm.ru()) unsafeGetters.add(decode(source.slice(wasm.us(), wasm.ue())));
35+
while (wasm.re()) {
36+
let exptStr = decode(source.slice(wasm.es(), wasm.ee()));
37+
if (exptStr !== undefined && !unsafeGetters.has(exptStr)) exports.add(exptStr);
38+
}
39+
return {
40+
exports: [...exports],
41+
reexports: [...reexports]
42+
};
43+
}
44+
function decode(str) {
45+
if (str[0] === '"' || str[0] === '\'') {
46+
try {
47+
const decoded = (0, eval)(str);
48+
// Filter to exclude non-matching UTF-16 surrogate strings
49+
for (let i = 0; i < decoded.length; i++) {
50+
const surrogatePrefix = decoded.charCodeAt(i) & 0xFC00;
51+
if (surrogatePrefix < 0xD800) {
52+
// Not a surrogate
53+
continue;
54+
} else if (surrogatePrefix === 0xD800) {
55+
// Validate surrogate pair
56+
if ((decoded.charCodeAt(++i) & 0xFC00) !== 0xDC00) return;
57+
} else {
58+
// Out-of-range surrogate code (above 0xD800)
59+
return;
60+
}
61+
}
62+
return decoded;
63+
} catch {}
64+
} else {
65+
return str;
66+
}
67+
}
68+
function copyBE(src, outBuf16) {
69+
const len = src.length;
70+
let i = 0;
71+
while (i < len) {
72+
const ch = src.charCodeAt(i);
73+
outBuf16[i++] = (ch & 0xff) << 8 | ch >>> 8;
74+
}
75+
}
76+
function copyLE(src, outBuf16) {
77+
const len = src.length;
78+
let i = 0;
79+
while (i < len) outBuf16[i] = src.charCodeAt(i++);
80+
}
81+
let initPromise;
82+
function init() {
83+
if (initPromise) return initPromise;
84+
return initPromise = (async () => {
85+
const compiled = await WebAssembly.compile((await import('node:fs')).readFileSync(new URL(import.meta.resolve('../lib/lexer.wasm'))));
86+
const {
87+
exports
88+
} = await WebAssembly.instantiate(compiled);
89+
wasm = exports;
90+
})();
91+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
let wasm;
2+
3+
const isLE = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
4+
5+
export function parse (source, name = '@') {
6+
if (!wasm)
7+
throw new Error('Not initialized');
8+
9+
const len = source.length + 1;
10+
11+
// need 2 bytes per code point plus analysis space so we double again
12+
const extraMem = (wasm.__heap_base.value || wasm.__heap_base) + len * 4 - wasm.memory.buffer.byteLength;
13+
if (extraMem > 0)
14+
wasm.memory.grow(Math.ceil(extraMem / 65536));
15+
16+
const addr = wasm.sa(len);
17+
(isLE ? copyLE : copyBE)(source, new Uint16Array(wasm.memory.buffer, addr, len));
18+
19+
const err_code = wasm.parseCJS(addr, source.length, 0, 0, 0);
20+
21+
if (err_code) {
22+
const err = new Error(`Parse error ${name}${wasm.e()}:${source.slice(0, wasm.e()).split('\n').length}:${wasm.e() - source.lastIndexOf('\n', wasm.e() - 1)}`);
23+
Object.assign(err, { idx: wasm.e() });
24+
if (err_code === 5 || err_code === 6 || err_code === 7)
25+
Object.assign(err, { code: 'ERR_LEXER_ESM_SYNTAX' });
26+
throw err;
27+
}
28+
29+
let exports = new Set(), reexports = new Set(), unsafeGetters = new Set();
30+
31+
while (wasm.rre()) {
32+
const reexptStr = decode(source.slice(wasm.res(), wasm.ree()));
33+
if (reexptStr)
34+
reexports.add(reexptStr);
35+
}
36+
while (wasm.ru())
37+
unsafeGetters.add(decode(source.slice(wasm.us(), wasm.ue())));
38+
while (wasm.re()) {
39+
let exptStr = decode(source.slice(wasm.es(), wasm.ee()));
40+
if (exptStr !== undefined && !unsafeGetters.has(exptStr))
41+
exports.add(exptStr);
42+
}
43+
44+
return { exports: [...exports], reexports: [...reexports] };
45+
}
46+
47+
function decode (str) {
48+
if (str[0] === '"' || str[0] === '\'') {
49+
try {
50+
const decoded = (0, eval)(str);
51+
// Filter to exclude non-matching UTF-16 surrogate strings
52+
for (let i = 0; i < decoded.length; i++) {
53+
const surrogatePrefix = decoded.charCodeAt(i) & 0xFC00;
54+
if (surrogatePrefix < 0xD800) {
55+
// Not a surrogate
56+
continue;
57+
}
58+
else if (surrogatePrefix === 0xD800) {
59+
// Validate surrogate pair
60+
if ((decoded.charCodeAt(++i) & 0xFC00) !== 0xDC00)
61+
return;
62+
}
63+
else {
64+
// Out-of-range surrogate code (above 0xD800)
65+
return;
66+
}
67+
}
68+
return decoded;
69+
}
70+
catch {}
71+
}
72+
else {
73+
return str;
74+
}
75+
}
76+
77+
function copyBE (src, outBuf16) {
78+
const len = src.length;
79+
let i = 0;
80+
while (i < len) {
81+
const ch = src.charCodeAt(i);
82+
outBuf16[i++] = (ch & 0xff) << 8 | ch >>> 8;
83+
}
84+
}
85+
86+
function copyLE (src, outBuf16) {
87+
const len = src.length;
88+
let i = 0;
89+
while (i < len)
90+
outBuf16[i] = src.charCodeAt(i++);
91+
}
92+
93+
let initPromise;
94+
export function init () {
95+
if (initPromise)
96+
return initPromise;
97+
return initPromise = (async () => {
98+
const compiled = await WebAssembly.compile(
99+
(await import('node:fs')).readFileSync(new URL(import.meta.resolve('../lib/lexer.wasm')))
100+
);
101+
const { exports } = await WebAssembly.instantiate(compiled);
102+
wasm = exports;
103+
})();
104+
}

‎deps/cjs-module-lexer/dist/lexer.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎deps/cjs-module-lexer/dist/lexer.mjs

+2-2
Large diffs are not rendered by default.

‎deps/cjs-module-lexer/lexer.d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export interface Exports {
2+
exports: string[];
3+
reexports: string[];
4+
}
5+
6+
export declare function parse(source: string, name?: string): Exports;
7+
export declare function init(): Promise<void>;
8+
export declare function initSync(): void;

‎deps/cjs-module-lexer/lexer.js

100755100644
+8-3
Original file line numberDiff line numberDiff line change
@@ -1149,6 +1149,10 @@ function codePointAtLast (bPos) {
11491149
return ch;
11501150
}
11511151

1152+
function esmSyntaxErr (msg) {
1153+
return Object.assign(new Error(msg), { code: 'ERR_LEXER_ESM_SYNTAX' });
1154+
}
1155+
11521156
function throwIfImportStatement () {
11531157
const startPos = pos;
11541158
pos += 6;
@@ -1160,7 +1164,7 @@ function throwIfImportStatement () {
11601164
return;
11611165
// import.meta
11621166
case 46/*.*/:
1163-
throw new Error('Unexpected import.meta in CJS module.');
1167+
throw esmSyntaxErr('Unexpected import.meta in CJS module.');
11641168

11651169
default:
11661170
// no space after "import" -> not an import keyword
@@ -1176,7 +1180,7 @@ function throwIfImportStatement () {
11761180
return;
11771181
}
11781182
// import statements are a syntax error in CommonJS
1179-
throw new Error('Unexpected import statement in CJS module.');
1183+
throw esmSyntaxErr('Unexpected import statement in CJS module.');
11801184
}
11811185
}
11821186

@@ -1186,7 +1190,7 @@ function throwIfExportStatement () {
11861190
const ch = commentWhitespace();
11871191
if (pos === curPos && !isPunctuator(ch))
11881192
return;
1189-
throw new Error('Unexpected export statement in CJS module.');
1193+
throw esmSyntaxErr('Unexpected export statement in CJS module.');
11901194
}
11911195

11921196
function commentWhitespace () {
@@ -1435,4 +1439,5 @@ function isExpressionTerminator (curPos) {
14351439
const initPromise = Promise.resolve();
14361440

14371441
module.exports.init = () => initPromise;
1442+
module.exports.initSync = () => {};
14381443
module.exports.parse = parseCJS;

‎deps/cjs-module-lexer/package.json

100755100644
+48-43
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,48 @@
1-
{
2-
"name": "cjs-module-lexer",
3-
"version": "1.2.2",
4-
"description": "Lexes CommonJS modules, returning their named exports metadata",
5-
"main": "lexer.js",
6-
"exports": {
7-
"import": "./dist/lexer.mjs",
8-
"default": "./lexer.js"
9-
},
10-
"types": "lexer.d.ts",
11-
"scripts": {
12-
"test-js": "mocha -b -u tdd test/*.js",
13-
"test-wasm": "WASM=1 mocha -b -u tdd test/*.js",
14-
"test": "npm run test-wasm && npm run test-js",
15-
"bench": "node --expose-gc bench/index.mjs",
16-
"build": "node build.js && babel dist/lexer.mjs | terser -o dist/lexer.js",
17-
"build-wasm": "make lib/lexer.wasm && node build.js",
18-
"prepublishOnly": "make && npm run build",
19-
"footprint": "npm run build && cat dist/lexer.js | gzip -9f | wc -c"
20-
},
21-
"author": "Guy Bedford",
22-
"license": "MIT",
23-
"devDependencies": {
24-
"@babel/cli": "^7.5.5",
25-
"@babel/core": "^7.5.5",
26-
"@babel/plugin-transform-modules-commonjs": "^7.5.0",
27-
"kleur": "^2.0.2",
28-
"mocha": "^5.2.0",
29-
"terser": "^4.1.4"
30-
},
31-
"files": [
32-
"dist",
33-
"lexer.d.ts"
34-
],
35-
"repository": {
36-
"type": "git",
37-
"url": "git+https://github.com/guybedford/cjs-module-lexer.git"
38-
},
39-
"bugs": {
40-
"url": "https://github.com/guybedford/cjs-module-lexer/issues"
41-
},
42-
"homepage": "https://github.com/guybedford/cjs-module-lexer#readme"
43-
}
1+
{
2+
"name": "cjs-module-lexer",
3+
"version": "1.4.0",
4+
"description": "Lexes CommonJS modules, returning their named exports metadata",
5+
"main": "lexer.js",
6+
"exports": {
7+
"import": {
8+
"types": "./lexer.d.mts",
9+
"default": "./dist/lexer.mjs"
10+
},
11+
"default": "./lexer.js"
12+
},
13+
"types": "lexer.d.ts",
14+
"scripts": {
15+
"test-js": "mocha -b -u tdd test/*.js",
16+
"test-wasm": "cross-env WASM=1 mocha -b -u tdd test/*.js",
17+
"test-wasm-sync": "cross-env WASM_SYNC=1 mocha -b -u tdd test/*.js",
18+
"test": "npm run test-wasm ; npm run test-wasm-sync ; npm run test-js",
19+
"bench": "node --expose-gc bench/index.mjs",
20+
"build": "node build.js ; babel dist/lexer.mjs -o dist/lexer.js ; terser dist/lexer.js -o dist/lexer.js",
21+
"build-wasm": "make lib/lexer.wasm ; node build.js",
22+
"prepublishOnly": "make && npm run build",
23+
"footprint": "npm run build && cat dist/lexer.js | gzip -9f | wc -c"
24+
},
25+
"author": "Guy Bedford",
26+
"license": "MIT",
27+
"devDependencies": {
28+
"@babel/cli": "^7.5.5",
29+
"@babel/core": "^7.5.5",
30+
"@babel/plugin-transform-modules-commonjs": "^7.5.0",
31+
"cross-env": "^7.0.3",
32+
"kleur": "^2.0.2",
33+
"mocha": "^9.1.3",
34+
"terser": "^4.1.4"
35+
},
36+
"files": [
37+
"dist",
38+
"lexer.d.ts"
39+
],
40+
"repository": {
41+
"type": "git",
42+
"url": "git+https://github.com/nodejs/cjs-module-lexer.git"
43+
},
44+
"bugs": {
45+
"url": "https://github.com/nodejs/cjs-module-lexer/issues"
46+
},
47+
"homepage": "https://github.com/nodejs/cjs-module-lexer#readme"
48+
}

‎src/cjs_module_lexer_version.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
// Refer to tools/dep_updaters/update-cjs-module-lexer.sh
33
#ifndef SRC_CJS_MODULE_LEXER_VERSION_H_
44
#define SRC_CJS_MODULE_LEXER_VERSION_H_
5-
#define CJS_MODULE_LEXER_VERSION "1.2.2"
5+
#define CJS_MODULE_LEXER_VERSION "1.4.0"
66
#endif // SRC_CJS_MODULE_LEXER_VERSION_H_

0 commit comments

Comments
 (0)
Please sign in to comment.