@@ -55,6 +55,7 @@ const {
5555 resolveWithHooks,
5656 loadHooks,
5757 loadWithHooks,
58+ validateLoadSloppy,
5859} = require ( 'internal/modules/customization_hooks' ) ;
5960let defaultResolve , defaultLoad , defaultLoadSync , importMetaInitializer ;
6061
@@ -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,16 +508,18 @@ 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 = loadResult . format === 'commonjs' ? 'commonjs-sync' : loadResult . format ;
513+ const translatorKey = formatFromLoad === 'commonjs' ? 'commonjs-sync' : formatFromLoad ;
508514
509- if ( finalFormat === 'wasm' ) {
515+ if ( translatorKey === 'wasm' ) {
510516 assert . fail ( 'WASM is currently unsupported by require(esm)' ) ;
511517 }
512518
513519 const { source } = loadResult ;
514520 const isMain = ( parentURL === undefined ) ;
515- const wrap = this . #translate( url , finalFormat , source , parentURL ) ;
521+ const translateContext = { format : formatFromLoad , source, translatorKey, __proto__ : null } ;
522+ const wrap = this . #translate( url , translateContext , parentURL ) ;
516523 assert ( wrap instanceof ModuleWrap , `Translator used for require(${ url } ) should not be async` ) ;
517524
518525 if ( process . env . WATCH_REPORT_DEPENDENCIES && process . send ) {
@@ -521,7 +528,7 @@ class ModuleLoader {
521528
522529 const cjsModule = wrap [ imported_cjs_symbol ] ;
523530 if ( cjsModule ) {
524- assert ( finalFormat === 'commonjs-sync' ) ;
531+ assert ( translatorKey === 'commonjs-sync' ) ;
525532 // Check if the ESM initiating import CJS is being required by the same CJS module.
526533 if ( cjsModule ?. [ kIsExecuting ] ) {
527534 const parentFilename = urlToFilename ( parentURL ) ;
@@ -545,22 +552,22 @@ class ModuleLoader {
545552 * Translate a loaded module source into a ModuleWrap. This is run synchronously,
546553 * but the translator may return the ModuleWrap in a Promise.
547554 * @param {string } url URL of the module to be translated.
548- * @param {string } format Format of the module to be translated. This is used to find
549- * matching translators.
550- * @param {ModuleSource } source Source of the module to be translated.
551- * @param {string|undefined } parentURL URL of the parent module. Undefined if it's the entry point.
555+ * @param {TranslateContext } translateContext Context for the translator
556+ * @param {string|undefined } parentURL URL of the module initiating the module loading for the first time.
557+ * Undefined if it's the entry point.
552558 * @returns {ModuleWrap }
553559 */
554- #translate( url , format , source , parentURL ) {
560+ #translate( url , translateContext , parentURL ) {
561+ const { translatorKey, format } = translateContext ;
555562 this . validateLoadResult ( url , format ) ;
556- const translator = getTranslators ( ) . get ( format ) ;
563+ const translator = getTranslators ( ) . get ( translatorKey ) ;
557564
558565 if ( ! translator ) {
559- throw new ERR_UNKNOWN_MODULE_FORMAT ( format , url ) ;
566+ throw new ERR_UNKNOWN_MODULE_FORMAT ( translatorKey , url ) ;
560567 }
561568
562- const result = FunctionPrototypeCall ( translator , this , url , source , parentURL === undefined ) ;
563- assert ( result instanceof ModuleWrap ) ;
569+ const result = FunctionPrototypeCall ( translator , this , url , translateContext , parentURL ) ;
570+ assert ( result instanceof ModuleWrap , `The ${ format } module returned is not a ModuleWrap` ) ;
564571 return result ;
565572 }
566573
@@ -573,7 +580,8 @@ class ModuleLoader {
573580 * @returns {ModuleWrap }
574581 */
575582 loadAndTranslateForRequireInImportedCJS ( url , loadContext , parentURL ) {
576- const { format : formatFromLoad , source } = this . #loadSync( url , loadContext ) ;
583+ const loadResult = this . #loadSync( url , loadContext ) ;
584+ const formatFromLoad = loadResult . format ;
577585
578586 if ( formatFromLoad === 'wasm' ) { // require(wasm) is not supported.
579587 throw new ERR_UNKNOWN_MODULE_FORMAT ( formatFromLoad , url ) ;
@@ -585,15 +593,16 @@ class ModuleLoader {
585593 }
586594 }
587595
588- let finalFormat = formatFromLoad ;
596+ let translatorKey = formatFromLoad ;
589597 if ( formatFromLoad === 'commonjs' ) {
590- finalFormat = 'require-commonjs' ;
598+ translatorKey = 'require-commonjs' ;
591599 }
592600 if ( formatFromLoad === 'commonjs-typescript' ) {
593- finalFormat = 'require-commonjs-typescript' ;
601+ translatorKey = 'require-commonjs-typescript' ;
594602 }
595603
596- const wrap = this . #translate( url , finalFormat , source , parentURL ) ;
604+ const translateContext = { ...loadResult , translatorKey, __proto__ : null } ;
605+ const wrap = this . #translate( url , translateContext , parentURL ) ;
597606 assert ( wrap instanceof ModuleWrap , `Translator used for require(${ url } ) should not be async` ) ;
598607 return wrap ;
599608 }
@@ -608,8 +617,9 @@ class ModuleLoader {
608617 */
609618 loadAndTranslate ( url , loadContext , parentURL ) {
610619 const maybePromise = this . load ( url , loadContext ) ;
611- const afterLoad = ( { format, source } ) => {
612- return this . #translate( url , format , source , parentURL ) ;
620+ const afterLoad = ( loadResult ) => {
621+ const translateContext = { ...loadResult , translatorKey : loadResult . format , __proto__ : null } ;
622+ return this . #translate( url , translateContext , parentURL ) ;
613623 } ;
614624 if ( isPromise ( maybePromise ) ) {
615625 return maybePromise . then ( afterLoad ) ;
@@ -835,8 +845,8 @@ class ModuleLoader {
835845 return this . #customizations. load ( url , context ) ;
836846 }
837847
838- defaultLoad ??= require ( 'internal/modules/esm/load' ) . defaultLoad ;
839- return defaultLoad ( url , context ) ;
848+ defaultLoadSync ??= require ( 'internal/modules/esm/load' ) . defaultLoadSync ;
849+ return defaultLoadSync ( url , context ) ;
840850 }
841851
842852 /**
@@ -871,7 +881,7 @@ class ModuleLoader {
871881 // TODO(joyeecheung): construct the ModuleLoadContext in the loaders directly instead
872882 // of converting them from plain objects in the hooks.
873883 return loadWithHooks ( url , context . format , context . importAttributes , this . #defaultConditions,
874- this . #loadAndMaybeBlockOnLoaderThread. bind ( this ) ) ;
884+ this . #loadAndMaybeBlockOnLoaderThread. bind ( this ) , validateLoadSloppy ) ;
875885 }
876886 return this . #loadAndMaybeBlockOnLoaderThread( url , context ) ;
877887 }
0 commit comments