Skip to content

Commit a4b1a77

Browse files
aduh95RafaelGSS
authored andcommitted
esm: skip file: URL conversion to path when possible
PR-URL: #46305 Reviewed-By: Geoffrey Booth <[email protected]> Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: Jacob Smith <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent c340de6 commit a4b1a77

File tree

3 files changed

+58
-8
lines changed

3 files changed

+58
-8
lines changed

lib/internal/modules/esm/get_format.js

+29-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ const {
44
ObjectPrototypeHasOwnProperty,
55
PromisePrototypeThen,
66
PromiseResolve,
7+
StringPrototypeCharCodeAt,
78
StringPrototypeSlice,
89
} = primordials;
9-
const { basename, extname, relative } = require('path');
10+
const { basename, relative } = require('path');
1011
const { getOptionValue } = require('internal/options');
1112
const {
1213
extensionFormatMap,
@@ -41,15 +42,38 @@ function getDataProtocolModuleFormat(parsed) {
4142
return mimeToFormat(mime);
4243
}
4344

45+
const DOT_CODE = 46;
46+
const SLASH_CODE = 47;
47+
48+
/**
49+
* Returns the file extension from a URL. Should give similar result to
50+
* `require('node:path').extname(require('node:url').fileURLToPath(url))`
51+
* when used with a `file:` URL.
52+
* @param {URL} url
53+
* @returns {string}
54+
*/
55+
function extname(url) {
56+
const { pathname } = url;
57+
for (let i = pathname.length - 1; i > 0; i--) {
58+
switch (StringPrototypeCharCodeAt(pathname, i)) {
59+
case SLASH_CODE:
60+
return '';
61+
62+
case DOT_CODE:
63+
return StringPrototypeCharCodeAt(pathname, i - 1) === SLASH_CODE ? '' : StringPrototypeSlice(pathname, i);
64+
}
65+
}
66+
return '';
67+
}
68+
4469
/**
4570
* @param {URL} url
4671
* @param {{parentURL: string}} context
4772
* @param {boolean} ignoreErrors
4873
* @returns {string}
4974
*/
5075
function getFileProtocolModuleFormat(url, context, ignoreErrors) {
51-
const filepath = fileURLToPath(url);
52-
const ext = extname(filepath);
76+
const ext = extname(url);
5377
if (ext === '.js') {
5478
return getPackageType(url) === 'module' ? 'module' : 'commonjs';
5579
}
@@ -59,6 +83,7 @@ function getFileProtocolModuleFormat(url, context, ignoreErrors) {
5983

6084
// Explicit undefined return indicates load hook should rerun format check
6185
if (ignoreErrors) { return undefined; }
86+
const filepath = fileURLToPath(url);
6287
let suggestion = '';
6388
if (getPackageType(url) === 'module' && ext === '') {
6489
const config = getPackageScopeConfig(url);
@@ -119,4 +144,5 @@ module.exports = {
119144
defaultGetFormat,
120145
defaultGetFormatWithoutErrors,
121146
extensionFormatMap,
147+
extname,
122148
};

lib/internal/modules/esm/translators.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ const {
3535
Module: CJSModule,
3636
cjsParseCache,
3737
} = require('internal/modules/cjs/loader');
38-
const internalURLModule = require('internal/url');
39-
const { fileURLToPath, URL } = require('url');
38+
const { fileURLToPath, URL } = require('internal/url');
4039
let debug = require('internal/util/debuglog').debuglog('esm', (fn) => {
4140
debug = fn;
4241
});
@@ -147,9 +146,7 @@ translators.set('commonjs', async function commonjsStrategy(url, source,
147146
isMain) {
148147
debug(`Translating CJSModule ${url}`);
149148

150-
let filename = internalURLModule.fileURLToPath(new URL(url));
151-
if (isWindows)
152-
filename = StringPrototypeReplaceAll(filename, '/', '\\');
149+
const filename = fileURLToPath(new URL(url));
153150

154151
if (!cjsParse) await initCJSParse();
155152
const { module, exportNames } = cjsPreparseModuleExports(filename);

test/parallel/test-esm-url-extname.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Flags: --expose-internals
2+
'use strict';
3+
require('../common');
4+
const assert = require('node:assert');
5+
const path = require('node:path');
6+
const { extname } = require('node:internal/modules/esm/get_format');
7+
const { fileURLToPath } = require('node:url');
8+
9+
[
10+
'file:///c:/path/to/file',
11+
'file:///c:/path/to/file.ext',
12+
'file:///c:/path.to/file.ext',
13+
'file:///c:/path.to/file',
14+
'file:///c:/path.to/.file',
15+
'file:///c:/path.to/.file.ext',
16+
'file:///c:/path/to/f.ext',
17+
'file:///c:/path/to/..ext',
18+
'file:///c:/path/to/..',
19+
'file:///c:/file',
20+
'file:///c:/file.ext',
21+
'file:///c:/.file',
22+
'file:///c:/.file.ext',
23+
].forEach((input) => {
24+
const inputAsURL = new URL(input);
25+
const inputAsPath = fileURLToPath(inputAsURL);
26+
assert.strictEqual(extname(inputAsURL), path.extname(inputAsPath));
27+
});

0 commit comments

Comments
 (0)