@@ -55,8 +55,9 @@ const {
5555 resolveWithHooks,
5656 loadHooks,
5757 loadWithHooks,
58+ validateLoadSloppy,
5859} = require ( 'internal/modules/customization_hooks' ) ;
59- let defaultResolve , defaultLoad , defaultLoadSync , importMetaInitializer ;
60+ let defaultResolve , defaultLoadSync , importMetaInitializer ;
6061
6162const { tracingChannel } = require ( 'diagnostics_channel' ) ;
6263const onImport = tracingChannel ( 'module.import' ) ;
@@ -146,6 +147,10 @@ let hooksProxy;
146147 * @typedef {ArrayBuffer|TypedArray|string } ModuleSource
147148 */
148149
150+ /**
151+ * @typedef {{ format: ModuleFormat, source: ModuleSource, translatorKey: string } } TranslateContext
152+ */
153+
149154/**
150155 * This class covers the base machinery of module loading. To add custom
151156 * behavior you can pass a customizations object and this object will be
@@ -503,18 +508,19 @@ class ModuleLoader {
503508
504509 const loadResult = this . #loadSync( url , { format, importAttributes } ) ;
505510
511+ const formatFromLoad = loadResult . format ;
506512 // Use the synchronous commonjs translator which can deal with cycles.
507- const finalFormat =
508- loadResult . format === 'commonjs' ||
509- loadResult . format === 'commonjs-typescript' ? 'commonjs-sync' : loadResult . format ;
513+ const translatorKey = ( formatFromLoad === 'commonjs' || formatFromLoad === 'commonjs-typescript' ) ?
514+ 'commonjs-sync' : formatFromLoad ;
510515
511- if ( finalFormat === 'wasm' ) {
516+ if ( translatorKey === 'wasm' ) {
512517 assert . fail ( 'WASM is currently unsupported by require(esm)' ) ;
513518 }
514519
515520 const { source } = loadResult ;
516521 const isMain = ( parentURL === undefined ) ;
517- const wrap = this . #translate( url , finalFormat , source , parentURL ) ;
522+ const translateContext = { format : formatFromLoad , source, translatorKey, __proto__ : null } ;
523+ const wrap = this . #translate( url , translateContext , parentURL ) ;
518524 assert ( wrap instanceof ModuleWrap , `Translator used for require(${ url } ) should not be async` ) ;
519525
520526 if ( process . env . WATCH_REPORT_DEPENDENCIES && process . send ) {
@@ -523,7 +529,7 @@ class ModuleLoader {
523529
524530 const cjsModule = wrap [ imported_cjs_symbol ] ;
525531 if ( cjsModule ) {
526- assert ( finalFormat === 'commonjs-sync' ) ;
532+ assert ( translatorKey === 'commonjs-sync' ) ;
527533 // Check if the ESM initiating import CJS is being required by the same CJS module.
528534 if ( cjsModule ?. [ kIsExecuting ] ) {
529535 const parentFilename = urlToFilename ( parentURL ) ;
@@ -547,22 +553,22 @@ class ModuleLoader {
547553 * Translate a loaded module source into a ModuleWrap. This is run synchronously,
548554 * but the translator may return the ModuleWrap in a Promise.
549555 * @param {string } url URL of the module to be translated.
550- * @param {string } format Format of the module to be translated. This is used to find
551- * matching translators.
552- * @param {ModuleSource } source Source of the module to be translated.
553- * @param {string|undefined } parentURL URL of the parent module. Undefined if it's the entry point.
556+ * @param {TranslateContext } translateContext Context for the translator
557+ * @param {string|undefined } parentURL URL of the module initiating the module loading for the first time.
558+ * Undefined if it's the entry point.
554559 * @returns {ModuleWrap }
555560 */
556- #translate( url , format , source , parentURL ) {
561+ #translate( url , translateContext , parentURL ) {
562+ const { translatorKey, format } = translateContext ;
557563 this . validateLoadResult ( url , format ) ;
558- const translator = getTranslators ( ) . get ( format ) ;
564+ const translator = getTranslators ( ) . get ( translatorKey ) ;
559565
560566 if ( ! translator ) {
561- throw new ERR_UNKNOWN_MODULE_FORMAT ( format , url ) ;
567+ throw new ERR_UNKNOWN_MODULE_FORMAT ( translatorKey , url ) ;
562568 }
563569
564- const result = FunctionPrototypeCall ( translator , this , url , source , parentURL === undefined ) ;
565- assert ( result instanceof ModuleWrap ) ;
570+ const result = FunctionPrototypeCall ( translator , this , url , translateContext , parentURL ) ;
571+ assert ( result instanceof ModuleWrap , `The ${ format } module returned is not a ModuleWrap` ) ;
566572 return result ;
567573 }
568574
@@ -575,7 +581,8 @@ class ModuleLoader {
575581 * @returns {ModuleWrap }
576582 */
577583 loadAndTranslateForRequireInImportedCJS ( url , loadContext , parentURL ) {
578- const { format : formatFromLoad , source } = this . #loadSync( url , loadContext ) ;
584+ const loadResult = this . #loadSync( url , loadContext ) ;
585+ const formatFromLoad = loadResult . format ;
579586
580587 if ( formatFromLoad === 'wasm' ) { // require(wasm) is not supported.
581588 throw new ERR_UNKNOWN_MODULE_FORMAT ( formatFromLoad , url ) ;
@@ -587,15 +594,16 @@ class ModuleLoader {
587594 }
588595 }
589596
590- let finalFormat = formatFromLoad ;
597+ let translatorKey = formatFromLoad ;
591598 if ( formatFromLoad === 'commonjs' ) {
592- finalFormat = 'require-commonjs' ;
599+ translatorKey = 'require-commonjs' ;
593600 }
594601 if ( formatFromLoad === 'commonjs-typescript' ) {
595- finalFormat = 'require-commonjs-typescript' ;
602+ translatorKey = 'require-commonjs-typescript' ;
596603 }
597604
598- const wrap = this . #translate( url , finalFormat , source , parentURL ) ;
605+ const translateContext = { ...loadResult , translatorKey, __proto__ : null } ;
606+ const wrap = this . #translate( url , translateContext , parentURL ) ;
599607 assert ( wrap instanceof ModuleWrap , `Translator used for require(${ url } ) should not be async` ) ;
600608 return wrap ;
601609 }
@@ -610,8 +618,9 @@ class ModuleLoader {
610618 */
611619 loadAndTranslate ( url , loadContext , parentURL ) {
612620 const maybePromise = this . load ( url , loadContext ) ;
613- const afterLoad = ( { format, source } ) => {
614- return this . #translate( url , format , source , parentURL ) ;
621+ const afterLoad = ( loadResult ) => {
622+ const translateContext = { ...loadResult , translatorKey : loadResult . format , __proto__ : null } ;
623+ return this . #translate( url , translateContext , parentURL ) ;
615624 } ;
616625 if ( isPromise ( maybePromise ) ) {
617626 return maybePromise . then ( afterLoad ) ;
@@ -837,8 +846,8 @@ class ModuleLoader {
837846 return this . #customizations. load ( url , context ) ;
838847 }
839848
840- defaultLoad ??= require ( 'internal/modules/esm/load' ) . defaultLoad ;
841- return defaultLoad ( url , context ) ;
849+ defaultLoadSync ??= require ( 'internal/modules/esm/load' ) . defaultLoadSync ;
850+ return defaultLoadSync ( url , context ) ;
842851 }
843852
844853 /**
@@ -873,7 +882,7 @@ class ModuleLoader {
873882 // TODO(joyeecheung): construct the ModuleLoadContext in the loaders directly instead
874883 // of converting them from plain objects in the hooks.
875884 return loadWithHooks ( url , context . format , context . importAttributes , this . #defaultConditions,
876- this . #loadAndMaybeBlockOnLoaderThread. bind ( this ) ) ;
885+ this . #loadAndMaybeBlockOnLoaderThread. bind ( this ) , validateLoadSloppy ) ;
877886 }
878887 return this . #loadAndMaybeBlockOnLoaderThread( url , context ) ;
879888 }
0 commit comments