@@ -158,6 +158,12 @@ let internalBinding;
158158// Create this WeakMap in js-land because V8 has no C++ API for WeakMap.
159159internalBinding ( 'module_wrap' ) . callbackMap = new WeakMap ( ) ;
160160
161+ // Think of this as module.exports in this file even though it is not
162+ // written in CommonJS style.
163+ const loaderExports = { internalBinding, NativeModule } ;
164+ const loaderId = 'internal/bootstrap/loaders' ;
165+ const config = internalBinding ( 'config' ) ;
166+
161167// Set up NativeModule.
162168function NativeModule ( id ) {
163169 this . filename = `${ id } .js` ;
@@ -167,34 +173,35 @@ function NativeModule(id) {
167173 this . exportKeys = undefined ;
168174 this . loaded = false ;
169175 this . loading = false ;
176+ if ( id === loaderId ) {
177+ // Do not expose this to user land even with --expose-internals.
178+ this . canBeRequiredByUsers = false ;
179+ } else if ( id . startsWith ( 'internal/' ) ) {
180+ this . canBeRequiredByUsers = config . exposeInternals ;
181+ } else {
182+ this . canBeRequiredByUsers = true ;
183+ }
170184}
171185
172186const {
173- source ,
187+ moduleIds ,
174188 compileFunction
175189} = internalBinding ( 'native_module' ) ;
176190
177- NativeModule . _source = source ;
178- NativeModule . _cache = { } ;
179-
180- const config = internalBinding ( 'config' ) ;
181-
182- // Think of this as module.exports in this file even though it is not
183- // written in CommonJS style.
184- const loaderExports = { internalBinding, NativeModule } ;
185- const loaderId = 'internal/bootstrap/loaders' ;
191+ NativeModule . map = new Map ( ) ;
192+ for ( var i = 0 ; i < moduleIds . length ; ++ i ) {
193+ const id = moduleIds [ i ] ;
194+ const mod = new NativeModule ( id ) ;
195+ NativeModule . map . set ( id , mod ) ;
196+ }
186197
187198NativeModule . require = function ( id ) {
188199 if ( id === loaderId ) {
189200 return loaderExports ;
190201 }
191202
192- const cached = NativeModule . getCached ( id ) ;
193- if ( cached && ( cached . loaded || cached . loading ) ) {
194- return cached . exports ;
195- }
196-
197- if ( ! NativeModule . exists ( id ) ) {
203+ const mod = NativeModule . map . get ( id ) ;
204+ if ( ! mod ) {
198205 // Model the error off the internal/errors.js model, but
199206 // do not use that module given that it could actually be
200207 // the one causing the error if there's a bug in Node.js.
@@ -205,60 +212,31 @@ NativeModule.require = function(id) {
205212 throw err ;
206213 }
207214
208- moduleLoadList . push ( `NativeModule ${ id } ` ) ;
209-
210- const nativeModule = new NativeModule ( id ) ;
211-
212- nativeModule . cache ( ) ;
213- nativeModule . compile ( ) ;
214-
215- return nativeModule . exports ;
216- } ;
217-
218- NativeModule . isDepsModule = function ( id ) {
219- return id . startsWith ( 'node-inspect/' ) || id . startsWith ( 'v8/' ) ;
220- } ;
221-
222- NativeModule . requireForDeps = function ( id ) {
223- if ( ! NativeModule . exists ( id ) ) {
224- id = `internal/deps/${ id } ` ;
215+ if ( mod . loaded || mod . loading ) {
216+ return mod . exports ;
225217 }
226- return NativeModule . require ( id ) ;
227- } ;
228218
229- NativeModule . getCached = function ( id ) {
230- return NativeModule . _cache [ id ] ;
219+ moduleLoadList . push ( `NativeModule ${ id } ` ) ;
220+ mod . compile ( ) ;
221+ return mod . exports ;
231222} ;
232223
233224NativeModule . exists = function ( id ) {
234- return NativeModule . _source . hasOwnProperty ( id ) ;
225+ return NativeModule . map . has ( id ) ;
235226} ;
236227
237- if ( config . exposeInternals ) {
238- NativeModule . nonInternalExists = function ( id ) {
239- // Do not expose this to user land even with --expose-internals.
240- if ( id === loaderId ) {
241- return false ;
242- }
243- return NativeModule . exists ( id ) ;
244- } ;
245-
246- NativeModule . isInternal = function ( id ) {
247- // Do not expose this to user land even with --expose-internals.
248- return id === loaderId ;
249- } ;
250- } else {
251- NativeModule . nonInternalExists = function ( id ) {
252- return NativeModule . exists ( id ) && ! NativeModule . isInternal ( id ) ;
253- } ;
254-
255- NativeModule . isInternal = function ( id ) {
256- return id . startsWith ( 'internal/' ) ;
257- } ;
258- }
228+ NativeModule . canBeRequiredByUsers = function ( id ) {
229+ const mod = NativeModule . map . get ( id ) ;
230+ return mod && mod . canBeRequiredByUsers ;
231+ } ;
259232
260- NativeModule . getSource = function ( id ) {
261- return NativeModule . _source [ id ] ;
233+ // Allow internal modules from dependencies to require
234+ // other modules from dependencies by providing fallbacks.
235+ NativeModule . requireWithFallbackInDeps = function ( request ) {
236+ if ( ! NativeModule . map . has ( request ) ) {
237+ request = `internal/deps/${ request } ` ;
238+ }
239+ return NativeModule . require ( request ) ;
262240} ;
263241
264242const getOwn = ( target , property , receiver ) => {
@@ -332,13 +310,13 @@ NativeModule.prototype.compile = function() {
332310
333311 try {
334312 const requireFn = this . id . startsWith ( 'internal/deps/' ) ?
335- NativeModule . requireForDeps :
313+ NativeModule . requireWithFallbackInDeps :
336314 NativeModule . require ;
337315
338316 const fn = compileFunction ( id ) ;
339317 fn ( this . exports , requireFn , this , process , internalBinding ) ;
340318
341- if ( config . experimentalModules && ! NativeModule . isInternal ( this . id ) ) {
319+ if ( config . experimentalModules && this . canBeRequiredByUsers ) {
342320 this . proxifyExports ( ) ;
343321 }
344322
@@ -348,10 +326,6 @@ NativeModule.prototype.compile = function() {
348326 }
349327} ;
350328
351- NativeModule . prototype . cache = function ( ) {
352- NativeModule . _cache [ this . id ] = this ;
353- } ;
354-
355329// Coverage must be turned on early, so that we can collect
356330// it for Node.js' own internal libraries.
357331if ( process . env . NODE_V8_COVERAGE ) {
0 commit comments