@@ -158,6 +158,12 @@ let internalBinding;
158
158
// Create this WeakMap in js-land because V8 has no C++ API for WeakMap.
159
159
internalBinding ( 'module_wrap' ) . callbackMap = new WeakMap ( ) ;
160
160
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
+
161
167
// Set up NativeModule.
162
168
function NativeModule ( id ) {
163
169
this . filename = `${ id } .js` ;
@@ -167,34 +173,35 @@ function NativeModule(id) {
167
173
this . exportKeys = undefined ;
168
174
this . loaded = false ;
169
175
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
+ }
170
184
}
171
185
172
186
const {
173
- source ,
187
+ moduleIds ,
174
188
compileFunction
175
189
} = internalBinding ( 'native_module' ) ;
176
190
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
+ }
186
197
187
198
NativeModule . require = function ( id ) {
188
199
if ( id === loaderId ) {
189
200
return loaderExports ;
190
201
}
191
202
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 ) {
198
205
// Model the error off the internal/errors.js model, but
199
206
// do not use that module given that it could actually be
200
207
// the one causing the error if there's a bug in Node.js.
@@ -205,62 +212,31 @@ NativeModule.require = function(id) {
205
212
throw err ;
206
213
}
207
214
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
- // TODO(TimothyGu): remove when DEP0084 reaches end of life.
225
- NativeModule . isDepsModule ( id ) ) {
226
- id = `internal/deps/${ id } ` ;
215
+ if ( mod . loaded || mod . loading ) {
216
+ return mod . exports ;
227
217
}
228
- return NativeModule . require ( id ) ;
229
- } ;
230
218
231
- NativeModule . getCached = function ( id ) {
232
- return NativeModule . _cache [ id ] ;
219
+ moduleLoadList . push ( `NativeModule ${ id } ` ) ;
220
+ mod . compile ( ) ;
221
+ return mod . exports ;
233
222
} ;
234
223
235
224
NativeModule . exists = function ( id ) {
236
- return NativeModule . _source . hasOwnProperty ( id ) ;
225
+ return NativeModule . map . has ( id ) ;
237
226
} ;
238
227
239
- if ( config . exposeInternals ) {
240
- NativeModule . nonInternalExists = function ( id ) {
241
- // Do not expose this to user land even with --expose-internals.
242
- if ( id === loaderId ) {
243
- return false ;
244
- }
245
- return NativeModule . exists ( id ) ;
246
- } ;
247
-
248
- NativeModule . isInternal = function ( id ) {
249
- // Do not expose this to user land even with --expose-internals.
250
- return id === loaderId ;
251
- } ;
252
- } else {
253
- NativeModule . nonInternalExists = function ( id ) {
254
- return NativeModule . exists ( id ) && ! NativeModule . isInternal ( id ) ;
255
- } ;
256
-
257
- NativeModule . isInternal = function ( id ) {
258
- return id . startsWith ( 'internal/' ) ;
259
- } ;
260
- }
228
+ NativeModule . canBeRequiredByUsers = function ( id ) {
229
+ const mod = NativeModule . map . get ( id ) ;
230
+ return mod && mod . canBeRequiredByUsers ;
231
+ } ;
261
232
262
- NativeModule . getSource = function ( id ) {
263
- 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 ) ;
264
240
} ;
265
241
266
242
const getOwn = ( target , property , receiver ) => {
@@ -334,13 +310,13 @@ NativeModule.prototype.compile = function() {
334
310
335
311
try {
336
312
const requireFn = this . id . startsWith ( 'internal/deps/' ) ?
337
- NativeModule . requireForDeps :
313
+ NativeModule . requireWithFallbackInDeps :
338
314
NativeModule . require ;
339
315
340
316
const fn = compileFunction ( id ) ;
341
317
fn ( this . exports , requireFn , this , process , internalBinding ) ;
342
318
343
- if ( config . experimentalModules && ! NativeModule . isInternal ( this . id ) ) {
319
+ if ( config . experimentalModules && this . canBeRequiredByUsers ) {
344
320
this . proxifyExports ( ) ;
345
321
}
346
322
@@ -350,10 +326,6 @@ NativeModule.prototype.compile = function() {
350
326
}
351
327
} ;
352
328
353
- NativeModule . prototype . cache = function ( ) {
354
- NativeModule . _cache [ this . id ] = this ;
355
- } ;
356
-
357
329
// Coverage must be turned on early, so that we can collect
358
330
// it for Node.js' own internal libraries.
359
331
if ( process . env . NODE_V8_COVERAGE ) {
0 commit comments