diff --git a/lib/internal/modules/esm/get_format.js b/lib/internal/modules/esm/get_format.js index c029b6c614384f..5959d476e13bd9 100644 --- a/lib/internal/modules/esm/get_format.js +++ b/lib/internal/modules/esm/get_format.js @@ -19,7 +19,7 @@ const { const experimentalNetworkImports = getOptionValue('--experimental-network-imports'); const { containsModuleSyntax } = internalBinding('contextify'); -const { getPackageType } = require('internal/modules/esm/package_config'); +const { getPackageType } = require('internal/modules/package_json_reader'); const { fileURLToPath } = require('internal/url'); const { ERR_UNKNOWN_FILE_EXTENSION } = require('internal/errors').codes; diff --git a/lib/internal/modules/esm/module_job.js b/lib/internal/modules/esm/module_job.js index 2f04070327f10b..05db7f3867efe2 100644 --- a/lib/internal/modules/esm/module_job.js +++ b/lib/internal/modules/esm/module_job.js @@ -232,7 +232,7 @@ class ModuleJob { const packageConfig = StringPrototypeStartsWith(this.module.url, 'file://') && RegExpPrototypeExec(/\.js(\?[^#]*)?(#.*)?$/, this.module.url) !== null && - require('internal/modules/esm/package_config') + require('internal/modules/package_json_reader') .getPackageScopeConfig(this.module.url); if (packageConfig.type === 'module') { e.message += diff --git a/lib/internal/modules/esm/package_config.js b/lib/internal/modules/esm/package_config.js deleted file mode 100644 index 6b3847966cb1d3..00000000000000 --- a/lib/internal/modules/esm/package_config.js +++ /dev/null @@ -1,44 +0,0 @@ -'use strict'; - -const { ArrayIsArray } = primordials; -const modulesBinding = internalBinding('modules'); -const { deserializePackageJSON } = require('internal/modules/package_json_reader'); - -// TODO(@anonrig): Merge this file with internal/esm/package_json_reader.js - -/** - * Returns the package configuration for the given resolved URL. - * @param {URL | string} resolved - The resolved URL. - * @returns {import('typings/internalBinding/modules').PackageConfig} - The package configuration. - */ -function getPackageScopeConfig(resolved) { - const result = modulesBinding.getPackageScopeConfig(`${resolved}`); - - if (ArrayIsArray(result)) { - return deserializePackageJSON(`${resolved}`, result, false /* checkIntegrity */); - } - - // This means that the response is a string - // and it is the path to the package.json file - return { - __proto__: null, - pjsonPath: result, - exists: false, - type: 'none', - }; -} - -/** - * Returns the package type for a given URL. - * @param {URL} url - The URL to get the package type for. - */ -function getPackageType(url) { - // TODO(@anonrig): Write a C++ function that returns only "type". - return getPackageScopeConfig(url).type; -} - - -module.exports = { - getPackageScopeConfig, - getPackageType, -}; diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index f4485e6fb38b9c..bbbaed87479289 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -56,7 +56,6 @@ const { } = require('internal/errors').codes; const { Module: CJSModule } = require('internal/modules/cjs/loader'); -const { getPackageScopeConfig } = require('internal/modules/esm/package_config'); const { getConditionsSet } = require('internal/modules/esm/utils'); const packageJsonReader = require('internal/modules/package_json_reader'); const { internalModuleStat } = internalBinding('fs'); @@ -688,7 +687,7 @@ function packageImportsResolve(name, base, conditions) { throw new ERR_INVALID_MODULE_SPECIFIER(name, reason, fileURLToPath(base)); } let packageJSONUrl; - const packageConfig = getPackageScopeConfig(base); + const packageConfig = packageJsonReader.getPackageScopeConfig(base); if (packageConfig.exists) { packageJSONUrl = pathToFileURL(packageConfig.pjsonPath); const imports = packageConfig.imports; @@ -797,7 +796,7 @@ function packageResolve(specifier, base, conditions) { parsePackageName(specifier, base); // ResolveSelf - const packageConfig = getPackageScopeConfig(base); + const packageConfig = packageJsonReader.getPackageScopeConfig(base); if (packageConfig.exists) { const packageJSONUrl = pathToFileURL(packageConfig.pjsonPath); if (packageConfig.exports != null && packageConfig.name === packageName) { diff --git a/lib/internal/modules/package_json_reader.js b/lib/internal/modules/package_json_reader.js index 232ed35528c5c5..0842d543862c33 100644 --- a/lib/internal/modules/package_json_reader.js +++ b/lib/internal/modules/package_json_reader.js @@ -1,6 +1,7 @@ 'use strict'; const { + ArrayIsArray, JSONParse, StringPrototypeSlice, StringPrototypeLastIndexOf, @@ -149,11 +150,42 @@ function getNearestParentPackageJSON(checkPath) { return { data, path }; } +/** + * Returns the package configuration for the given resolved URL. + * @param {URL | string} resolved - The resolved URL. + * @returns {import('typings/internalBinding/modules').PackageConfig} - The package configuration. + */ +function getPackageScopeConfig(resolved) { + const result = modulesBinding.getPackageScopeConfig(`${resolved}`); + + if (ArrayIsArray(result)) { + return deserializePackageJSON(`${resolved}`, result, false /* checkIntegrity */); + } + + // This means that the response is a string + // and it is the path to the package.json file + return { + __proto__: null, + pjsonPath: result, + exists: false, + type: 'none', + }; +} + +/** + * Returns the package type for a given URL. + * @param {URL} url - The URL to get the package type for. + */ +function getPackageType(url) { + // TODO(@anonrig): Write a C++ function that returns only "type". + return getPackageScopeConfig(url).type; +} + module.exports = { checkPackageJSONIntegrity, read, readPackage, getNearestParentPackageJSON, - - deserializePackageJSON, + getPackageScopeConfig, + getPackageType, }; diff --git a/test/sequential/test-module-loading.js b/test/sequential/test-module-loading.js index ebbbcbb6c937ef..3bfd1090fe91fd 100644 --- a/test/sequential/test-module-loading.js +++ b/test/sequential/test-module-loading.js @@ -128,7 +128,7 @@ assert.throws( assert.throws( function() { require('../fixtures/packages/unparseable'); }, - /^SyntaxError: Error parsing/ + /^Error: Invalid package config/ ); {