@@ -53,8 +53,9 @@ const {
5353 resolveWithHooks,
5454 loadHooks,
5555 loadWithHooks,
56+ validateLoadSloppy,
5657} = require ( 'internal/modules/customization_hooks' ) ;
57- let defaultResolve , defaultLoad , defaultLoadSync , importMetaInitializer ;
58+ let defaultResolve , defaultLoadSync , importMetaInitializer ;
5859
5960const { tracingChannel } = require ( 'diagnostics_channel' ) ;
6061const onImport = tracingChannel ( 'module.import' ) ;
@@ -144,6 +145,10 @@ let hooksProxy;
144145 * @typedef {ArrayBuffer|TypedArray|string } ModuleSource
145146 */
146147
148+ /**
149+ * @typedef {{ format: ModuleFormat, source: ModuleSource, translatorKey: string } } TranslateContext
150+ */
151+
147152/**
148153 * This class covers the base machinery of module loading. To add custom
149154 * behavior you can pass a customizations object and this object will be
@@ -492,18 +497,19 @@ class ModuleLoader {
492497
493498 const loadResult = this . #loadSync( url , { format, importAttributes } ) ;
494499
500+ const formatFromLoad = loadResult . format ;
495501 // Use the synchronous commonjs translator which can deal with cycles.
496- const finalFormat =
497- loadResult . format === 'commonjs' ||
498- loadResult . format === 'commonjs-typescript' ? 'commonjs-sync' : loadResult . format ;
502+ const translatorKey = ( formatFromLoad === 'commonjs' || formatFromLoad === 'commonjs-typescript' ) ?
503+ 'commonjs-sync' : formatFromLoad ;
499504
500- if ( finalFormat === 'wasm' ) {
505+ if ( translatorKey === 'wasm' ) {
501506 assert . fail ( 'WASM is currently unsupported by require(esm)' ) ;
502507 }
503508
504509 const { source } = loadResult ;
505510 const isMain = ( parentURL === undefined ) ;
506- const wrap = this . #translate( url , finalFormat , source , parentURL ) ;
511+ const translateContext = { format : formatFromLoad , source, translatorKey, __proto__ : null } ;
512+ const wrap = this . #translate( url , translateContext , parentURL ) ;
507513 assert ( wrap instanceof ModuleWrap , `Translator used for require(${ url } ) should not be async` ) ;
508514
509515 if ( process . env . WATCH_REPORT_DEPENDENCIES && process . send ) {
@@ -512,7 +518,7 @@ class ModuleLoader {
512518
513519 const cjsModule = wrap [ imported_cjs_symbol ] ;
514520 if ( cjsModule ) {
515- assert ( finalFormat === 'commonjs-sync' ) ;
521+ assert ( translatorKey === 'commonjs-sync' ) ;
516522 // Check if the ESM initiating import CJS is being required by the same CJS module.
517523 if ( cjsModule ?. [ kIsExecuting ] ) {
518524 const parentFilename = urlToFilename ( parentURL ) ;
@@ -536,22 +542,22 @@ class ModuleLoader {
536542 * Translate a loaded module source into a ModuleWrap. This is run synchronously,
537543 * but the translator may return the ModuleWrap in a Promise.
538544 * @param {string } url URL of the module to be translated.
539- * @param {string } format Format of the module to be translated. This is used to find
540- * matching translators.
541- * @param {ModuleSource } source Source of the module to be translated.
542- * @param {string|undefined } parentURL URL of the parent module. Undefined if it's the entry point.
545+ * @param {TranslateContext } translateContext Context for the translator
546+ * @param {string|undefined } parentURL URL of the module initiating the module loading for the first time.
547+ * Undefined if it's the entry point.
543548 * @returns {ModuleWrap }
544549 */
545- #translate( url , format , source , parentURL ) {
550+ #translate( url , translateContext , parentURL ) {
551+ const { translatorKey, format } = translateContext ;
546552 this . validateLoadResult ( url , format ) ;
547- const translator = getTranslators ( ) . get ( format ) ;
553+ const translator = getTranslators ( ) . get ( translatorKey ) ;
548554
549555 if ( ! translator ) {
550- throw new ERR_UNKNOWN_MODULE_FORMAT ( format , url ) ;
556+ throw new ERR_UNKNOWN_MODULE_FORMAT ( translatorKey , url ) ;
551557 }
552558
553- const result = FunctionPrototypeCall ( translator , this , url , source , parentURL === undefined ) ;
554- assert ( result instanceof ModuleWrap ) ;
559+ const result = FunctionPrototypeCall ( translator , this , url , translateContext , parentURL ) ;
560+ assert ( result instanceof ModuleWrap , `The ${ format } module returned is not a ModuleWrap` ) ;
555561 return result ;
556562 }
557563
@@ -564,7 +570,8 @@ class ModuleLoader {
564570 * @returns {ModuleWrap }
565571 */
566572 loadAndTranslateForRequireInImportedCJS ( url , loadContext , parentURL ) {
567- const { format : formatFromLoad , source } = this . #loadSync( url , loadContext ) ;
573+ const loadResult = this . #loadSync( url , loadContext ) ;
574+ const formatFromLoad = loadResult . format ;
568575
569576 if ( formatFromLoad === 'wasm' ) { // require(wasm) is not supported.
570577 throw new ERR_UNKNOWN_MODULE_FORMAT ( formatFromLoad , url ) ;
@@ -576,15 +583,16 @@ class ModuleLoader {
576583 }
577584 }
578585
579- let finalFormat = formatFromLoad ;
586+ let translatorKey = formatFromLoad ;
580587 if ( formatFromLoad === 'commonjs' ) {
581- finalFormat = 'require-commonjs' ;
588+ translatorKey = 'require-commonjs' ;
582589 }
583590 if ( formatFromLoad === 'commonjs-typescript' ) {
584- finalFormat = 'require-commonjs-typescript' ;
591+ translatorKey = 'require-commonjs-typescript' ;
585592 }
586593
587- const wrap = this . #translate( url , finalFormat , source , parentURL ) ;
594+ const translateContext = { ...loadResult , translatorKey, __proto__ : null } ;
595+ const wrap = this . #translate( url , translateContext , parentURL ) ;
588596 assert ( wrap instanceof ModuleWrap , `Translator used for require(${ url } ) should not be async` ) ;
589597 return wrap ;
590598 }
@@ -599,8 +607,9 @@ class ModuleLoader {
599607 */
600608 loadAndTranslate ( url , loadContext , parentURL ) {
601609 const maybePromise = this . load ( url , loadContext ) ;
602- const afterLoad = ( { format, source } ) => {
603- return this . #translate( url , format , source , parentURL ) ;
610+ const afterLoad = ( loadResult ) => {
611+ const translateContext = { ...loadResult , translatorKey : loadResult . format , __proto__ : null } ;
612+ return this . #translate( url , translateContext , parentURL ) ;
604613 } ;
605614 if ( isPromise ( maybePromise ) ) {
606615 return maybePromise . then ( afterLoad ) ;
@@ -818,8 +827,8 @@ class ModuleLoader {
818827 return this . #customizations. load ( url , context ) ;
819828 }
820829
821- defaultLoad ??= require ( 'internal/modules/esm/load' ) . defaultLoad ;
822- return defaultLoad ( url , context ) ;
830+ defaultLoadSync ??= require ( 'internal/modules/esm/load' ) . defaultLoadSync ;
831+ return defaultLoadSync ( url , context ) ;
823832 }
824833
825834 /**
@@ -854,7 +863,7 @@ class ModuleLoader {
854863 // TODO(joyeecheung): construct the ModuleLoadContext in the loaders directly instead
855864 // of converting them from plain objects in the hooks.
856865 return loadWithHooks ( url , context . format , context . importAttributes , this . #defaultConditions,
857- this . #loadAndMaybeBlockOnLoaderThread. bind ( this ) ) ;
866+ this . #loadAndMaybeBlockOnLoaderThread. bind ( this ) , validateLoadSloppy ) ;
858867 }
859868 return this . #loadAndMaybeBlockOnLoaderThread( url , context ) ;
860869 }
0 commit comments