{ return packageType; } // The controlling `package.json` file has no `type` field. if (defaultType === 'module') { // An exception to the type flag making ESM the default everywhere is that package scopes under `node_modules` // should retain the assumption that a lack of a `type` field means CommonJS. return underNodeModules(url) ? 'commonjs' : 'module'; } return 'commonjs'; } if (ext === '') { const packageType = getPackageType(url); if (defaultType === 'commonjs') { // Legacy behavior if (packageType === 'none' || packageType === 'commonjs') { return 'commonjs'; } // Else packageType === 'module' return getFormatOfExtensionlessFile(url); } // Else defaultType === 'module' if (underNodeModules(url)) { // Exception for package scopes under `node_modules` return packageType === 'module' ? getFormatOfExtensionlessFile(url) : 'commonjs'; } if (packageType === 'none' || packageType === 'module') { return getFormatOfExtensionlessFile(url); } // Else packageType === 'commonjs' return 'commonjs'; } const format = extensionFormatMap[ext]; if (format) { return format; } if (experimentalSpecifierResolution !== 'node') { // Explicit undefined return indicates load hook should rerun format check if (ignoreErrors) { return undefined; } const filepath = fileURLToPath(url); throw new ERR_UNKNOWN_FILE_EXTENSION(ext, filepath); } return getLegacyExtensionFormat(ext) ?? null; } /** * @param {URL} url * @param {{parentURL: string}} context * @returns {Promise | undefined} only works when enabled */ function getHttpProtocolModuleFormat(url, context) { if (experimentalNetworkImports) { const { fetchModule } = require('internal/modules/esm/fetch_module'); return PromisePrototypeThen( PromiseResolve(fetchModule(url, context)), (entry) => { return mimeToFormat(entry.headers['content-type']); }, ); } } /** * @param {URL} url * @param {{parentURL: string}} context * @returns {Promise | string | undefined} only works when enabled */ function defaultGetFormatWithoutErrors(url, context) { const protocol = url.protocol; if (!ObjectPrototypeHasOwnProperty(protocolHandlers, protocol)) { return null; } return protocolHandlers[protocol](url, context, true); } /** * @param {URL} url * @param {{parentURL: string}} context * @returns {Promise | string | undefined} only works when enabled */ function defaultGetFormat(url, context) { const protocol = url.protocol; if (!ObjectPrototypeHasOwnProperty(protocolHandlers, protocol)) { return null; } return protocolHandlers[protocol](url, context, false); } module.exports = { defaultGetFormat, defaultGetFormatWithoutErrors, extensionFormatMap, extname, };