1- /* eslint-disable no-restricted-globals, @typescript-eslint/no-require-imports */
1+ /* eslint-disable @typescript-eslint/no-require-imports */
22
33import * as fs from 'node:fs' ;
44import { isBuiltin } from 'node:module' ;
@@ -15,10 +15,42 @@ const allowedModules = new Set([
1515 'mongodb-client-encryption' ,
1616 'mongodb-connection-string-url' ,
1717 'path' ,
18- 'snappy'
18+ 'snappy' ,
19+ 'socks'
1920] ) ;
2021const blockedModules = new Set ( [ 'os' ] ) ;
2122
23+ // TODO: NODE-7460 - Remove Error and other unnecessary exports
24+ const exposedGlobals = new Set ( [
25+ 'AbortController' ,
26+ 'AbortSignal' ,
27+ 'BigInt' ,
28+ 'Buffer' ,
29+ 'Date' ,
30+ 'Error' ,
31+ 'Headers' ,
32+ 'Map' ,
33+ 'Math' ,
34+ 'Promise' ,
35+ 'TextDecoder' ,
36+ 'TextEncoder' ,
37+ 'URL' ,
38+ 'URLSearchParams' ,
39+
40+ 'console' ,
41+ 'crypto' ,
42+ 'performance' ,
43+ 'process' ,
44+
45+ 'clearImmediate' ,
46+ 'clearInterval' ,
47+ 'clearTimeout' ,
48+ 'setImmediate' ,
49+ 'setInterval' ,
50+ 'setTimeout' ,
51+ 'queueMicrotask'
52+ ] ) ;
53+
2254/**
2355 * Creates a require function that blocks access to specified core modules
2456 */
@@ -34,46 +66,12 @@ function createRestrictedRequire() {
3466 throw new Error ( `Access to core module '${ moduleName } ' is restricted in this context` ) ;
3567 }
3668 return require ( moduleName ) ;
37- } as NodeRequire ;
69+ } as NodeJS . Require ;
3870}
3971
40- // Create a sandbox context with necessary globals
41- const sandbox = vm . createContext ( {
72+ const context = {
4273 __proto__ : null ,
4374
44- // Console and timing
45- console : console ,
46- AbortController : AbortController ,
47- AbortSignal : AbortSignal ,
48- Date : global . Date ,
49- Error : global . Error ,
50- URL : global . URL ,
51- URLSearchParams : global . URLSearchParams ,
52- queueMicrotask : queueMicrotask ,
53- performance : global . performance ,
54- setTimeout : global . setTimeout ,
55- clearTimeout : global . clearTimeout ,
56- setInterval : global . setInterval ,
57- clearInterval : global . clearInterval ,
58- setImmediate : global . setImmediate ,
59- clearImmediate : global . clearImmediate ,
60-
61- // Process
62- process : process ,
63-
64- // TODO: NODE-7460 - Remove Error and other unnecessary exports
65-
66- // Global objects needed for runtime
67- Buffer : Buffer ,
68- Headers : global . Headers ,
69- Map : Map ,
70- Promise : Promise ,
71- Math : Math ,
72- TextEncoder : global . TextEncoder ,
73- TextDecoder : global . TextDecoder ,
74- BigInt : global . BigInt ,
75- crypto : global . crypto ,
76-
7775 // Custom require that blocks core modules
7876 require : createRestrictedRequire ( ) ,
7977
@@ -83,7 +81,17 @@ const sandbox = vm.createContext({
8381 // Needed for some modules
8482 global : undefined as any ,
8583 globalThis : undefined as any
86- } ) ;
84+ } ;
85+
86+ // Expose allowed globals in the context
87+ for ( const globalName of exposedGlobals ) {
88+ if ( globalName in global ) {
89+ context [ globalName ] = ( global as any ) [ globalName ] ;
90+ }
91+ }
92+
93+ // Create a sandbox context with necessary globals
94+ const sandbox = vm . createContext ( context ) ;
8795
8896// Make global and globalThis point to the sandbox
8997sandbox . global = sandbox ;
@@ -93,7 +101,7 @@ sandbox.globalThis = sandbox;
93101 * Load the bundled MongoDB driver module in a VM context
94102 * This allows us to control the globals that the driver has access to
95103 */
96- export function loadContextifiedMongoDBModule ( ) {
104+ export function loadContextifiedMongoDBModule ( ) : typeof import ( '../../mongodb_all' ) {
97105 const bundlePath = path . join ( __dirname , 'bundle/driver-bundle.js' ) ;
98106
99107 if ( ! fs . existsSync ( bundlePath ) ) {
@@ -114,5 +122,5 @@ export function loadContextifiedMongoDBModule() {
114122 // Execute the bundle with the restricted require from the sandbox
115123 fn ( moduleContainer . exports , moduleContainer , sandbox . require ) ;
116124
117- return moduleContainer . exports ;
125+ return moduleContainer . exports as unknown as typeof import ( '../../mongodb_all' ) ;
118126}
0 commit comments