Skip to content

Commit 7d9cf78

Browse files
committed
added better message to restricted require error, including the source location
1 parent 875174b commit 7d9cf78

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@
142142
"check:search-indexes": "nyc mocha --config test/mocha_mongodb.js test/manual/search-index-management.prose.test.ts",
143143
"check:test": "mocha --config test/mocha_mongodb.js test/integration",
144144
"check:unit": "nyc mocha test/unit",
145-
"check:unit-bundled": "npm run switch:to-bundled && npm run check:unit || npm run switch:to-unbundled",
145+
"check:unit-bundled": "npm run build:bundle && npm run switch:to-bundled && npm run check:unit ; npm run switch:to-unbundled",
146146
"check:ts": "node ./node_modules/typescript/bin/tsc -v && node ./node_modules/typescript/bin/tsc --noEmit",
147147
"check:atlas": "nyc mocha --config test/manual/mocharc.js test/manual/atlas_connectivity.test.ts",
148148
"check:drivers-atlas-testing": "nyc mocha --config test/mocha_mongodb.js test/atlas/drivers_atlas_testing.test.ts",
@@ -157,8 +157,8 @@
157157
"check:snappy": "nyc mocha test/unit/assorted/snappy.test.js",
158158
"check:x509": "nyc mocha test/manual/x509_auth.test.ts",
159159
"check:runtime-independence": "ts-node test/tools/runner/vm_context_helper.ts test/integration/change-streams/change_stream.test.ts",
160-
"check:test-bundled": "npm run switch:to-bundled && npm run check:test || npm run switch:to-unbundled",
161-
"build:bundle": "npm run bundle:driver && npm run bundle:types && npm run build:runtime-barrel",
160+
"check:test-bundled": "npm run build:bundle && npm run switch:to-bundled&& npm run check:test ; npm run switch:to-unbundled",
161+
"build:bundle": "npm run bundle:driver && npm run bundle:types",
162162
"build:runtime-barrel": "node etc/build-runtime-barrel.mjs",
163163
"bundle:driver": "node etc/bundle-driver.mjs",
164164
"bundle:types": "npx tsc --project ./tsconfig.json --declaration --emitDeclarationOnly --declarationDir test/tools/runner/bundle/types",

test/tools/runner/vm_context_helper.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ function createRestrictedRequire() {
1414
return function restrictedRequire(moduleName: string) {
1515
// Block core modules
1616
if (isBuiltin(moduleName) && blockedModules.has(moduleName)) {
17-
throw new Error(`Access to core module '${moduleName}' is restricted in this context`);
17+
const sourceFile = new Error().stack.split('\n')[2]?.replace('at', '').trim();
18+
const source = sourceFile ? `from ${sourceFile}` : 'from an unknown source';
19+
throw new Error(
20+
`Access to core module '${moduleName}' (${source}) is restricted in this context`
21+
);
1822
}
1923

2024
return require(moduleName);

0 commit comments

Comments
 (0)