Skip to content

Commit

Permalink
esm: improve typings and code coverage
Browse files Browse the repository at this point in the history
PR-URL: #42305
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Mestery <mestery@protonmail.com>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
  • Loading branch information
bmeck authored and danielleadams committed Apr 24, 2022
1 parent 5eaa591 commit 2e42ccf
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/internal/modules/esm/formats.js
Expand Up @@ -29,6 +29,10 @@ if (experimentalWasmModules) {
extensionFormatMap['.wasm'] = legacyExtensionFormatMap['.wasm'] = 'wasm';
}

/**
* @param {string} mime
* @returns {string | null}
*/
function mimeToFormat(mime) {
if (
RegExpPrototypeTest(
Expand Down
21 changes: 21 additions & 0 deletions lib/internal/modules/esm/get_format.js
Expand Up @@ -41,6 +41,12 @@ function getDataProtocolModuleFormat(parsed) {
return mimeToFormat(mime);
}

/**
* @param {URL} url
* @param {{parentURL: string}} context
* @param {boolean} ignoreErrors
* @returns {string}
*/
function getFileProtocolModuleFormat(url, context, ignoreErrors) {
const ext = extname(url.pathname);
if (ext === '.js') {
Expand All @@ -59,6 +65,11 @@ function getFileProtocolModuleFormat(url, context, ignoreErrors) {
return getLegacyExtensionFormat(ext) ?? null;
}

/**
* @param {URL} url
* @param {{parentURL: string}} context
* @returns {Promise<string> | undefined} only works when enabled
*/
function getHttpProtocolModuleFormat(url, context) {
if (experimentalNetworkImports) {
return PromisePrototypeThen(
Expand All @@ -70,13 +81,23 @@ function getHttpProtocolModuleFormat(url, context) {
}
}

/**
* @param {URL | URL['href']} url
* @param {{parentURL: string}} context
* @returns {Promise<string> | string | undefined} only works when enabled
*/
function defaultGetFormatWithoutErrors(url, context) {
const parsed = new URL(url);
if (!ObjectPrototypeHasOwnProperty(protocolHandlers, parsed.protocol))
return null;
return protocolHandlers[parsed.protocol](parsed, context, true);
}

/**
* @param {URL | URL['href']} url
* @param {{parentURL: string}} context
* @returns {Promise<string> | string | undefined} only works when enabled
*/
function defaultGetFormat(url, context) {
const parsed = new URL(url);
return ObjectPrototypeHasOwnProperty(protocolHandlers, parsed.protocol) ?
Expand Down
1 change: 1 addition & 0 deletions lib/internal/modules/esm/resolve.js
Expand Up @@ -79,6 +79,7 @@ const DEFAULT_CONDITIONS_SET = new SafeSet(DEFAULT_CONDITIONS);
* @typedef {string | string[] | Record<string, unknown>} Exports
* @typedef {'module' | 'commonjs'} PackageType
* @typedef {{
* pjsonPath: string,
* exports?: ExportConfig;
* name?: string;
* main?: string;
Expand Down
7 changes: 5 additions & 2 deletions test/es-module/test-esm-basic-imports.mjs
@@ -1,7 +1,10 @@
import '../common/index.mjs';
import assert from 'assert';
import ok from '../fixtures/es-modules/test-esm-ok.mjs';
import okShebang from './test-esm-shebang.mjs';
import * as okShebangNs from './test-esm-shebang.mjs';
// encode the '.'
import * as okShebangPercentNs from './test-esm-shebang%2emjs';

assert(ok);
assert(okShebang);
assert(okShebangNs.default);
assert.strict.equal(okShebangNs, okShebangPercentNs);

0 comments on commit 2e42ccf

Please sign in to comment.