Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions doc/api/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -3502,6 +3502,9 @@ npx codemod@latest @nodejs/crypto-rsa-pss-update

<!-- YAML
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/67488
description: End-of-Life.
- version: v17.0.0
pr-url: https://github.com/nodejs/node/pull/40117
description: Runtime deprecation.
Expand All @@ -3511,10 +3514,11 @@ changes:
with `--pending-deprecation` support.
-->

Type: Runtime
Type: End-of-Life

The remapping of specifiers ending in `"/"` like `import 'pkg/x/'` is deprecated
for package `"exports"` and `"imports"` pattern resolutions.
The remapping of specifiers ending in `"/"` like `import 'pkg/x/'` is no longer
supported for package `"exports"` and `"imports"` pattern resolutions. Such
specifiers throw an [`ERR_INVALID_MODULE_SPECIFIER`][] error.

### DEP0156: `.aborted` property and `'abort'`, `'aborted'` event in `http`

Expand Down Expand Up @@ -3758,6 +3762,9 @@ that will be removed in a future V8 release.

<!-- YAML
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/67488
description: End-of-Life.
- version: v19.0.0
pr-url: https://github.com/nodejs/node/pull/44495
description: Runtime deprecation.
Expand All @@ -3767,12 +3774,12 @@ changes:
with `--pending-deprecation` support.
-->

Type: Runtime
Type: End-of-Life

Package imports and exports targets mapping into paths including a double slash
(of _"/"_ or _"\\"_) are deprecated and will fail with a resolution validation
error in a future release. This same deprecation also applies to pattern matches
starting or ending in a slash.
(of _"/"_ or _"\\"_) throw an [`ERR_INVALID_PACKAGE_TARGET`][] error. Pattern
matches starting or ending in a slash, or containing a double slash, throw an
[`ERR_INVALID_MODULE_SPECIFIER`][] error.

### DEP0167: Weak `DiffieHellmanGroup` instances (`modp1`, `modp2`, `modp5`)

Expand Down Expand Up @@ -4868,6 +4875,8 @@ Type: Documentation-only
[`CryptoKey`]: webcrypto.md#class-cryptokey
[`Decipheriv`]: crypto.md#class-decipheriv
[`Duplex.toWeb()`]: stream.md#streamduplextowebstreamduplex-options
[`ERR_INVALID_MODULE_SPECIFIER`]: errors.md#err_invalid_module_specifier
[`ERR_INVALID_PACKAGE_TARGET`]: errors.md#err_invalid_package_target
[`Error.isError`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/isError
[`KeyObject.from()`]: crypto.md#static-method-keyobjectfromkey
[`REPLServer.clearBufferedCommand()`]: repl.md#replserverclearbufferedcommand
Expand Down
120 changes: 17 additions & 103 deletions lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const {
ObjectPrototypeHasOwnProperty,
RegExpPrototypeExec,
RegExpPrototypeSymbolReplace,
SafeSet,
String,
StringPrototypeEndsWith,
StringPrototypeIncludes,
Expand Down Expand Up @@ -57,61 +56,6 @@ const {

const { hasPackageMap, packageMapResolve } = require('internal/modules/package_map');

const emittedPackageWarnings = new SafeSet();

/**
* Emits a deprecation warning for the use of a deprecated trailing slash pattern mapping in the "exports" field
* module resolution of a package.
* @param {string} match - The deprecated trailing slash pattern mapping.
* @param {string} pjsonUrl - The URL of the package.json file.
* @param {string} base - The URL of the module that imported the package.
*/
function emitTrailingSlashPatternDeprecation(match, pjsonUrl, base) {
if (process.noDeprecation) {
return;
}
const pjsonPath = fileURLToPath(pjsonUrl);
if (emittedPackageWarnings.has(pjsonPath + '|' + match)) { return; }
emittedPackageWarnings.add(pjsonPath + '|' + match);
process.emitWarning(
`Use of deprecated trailing slash pattern mapping "${match}" in the ` +
`"exports" field module resolution of the package at ${pjsonPath}${
base ? ` imported from ${fileURLToPath(base)}` :
''}. Mapping specifiers ending in "/" is no longer supported.`,
'DeprecationWarning',
'DEP0155',
);
}

const doubleSlashRegEx = /[/\\][/\\]/;

/**
* Emits a deprecation warning for invalid segment in module resolution.
* @param {string} target - The target module.
* @param {string} request - The requested module.
* @param {string} match - The matched module.
* @param {string} pjsonUrl - The package.json URL.
* @param {boolean} internal - Whether the module is in the "imports" or "exports" field.
* @param {string} base - The base URL.
* @param {boolean} isTarget - Whether the target is a module.
*/
function emitInvalidSegmentDeprecation(target, request, match, pjsonUrl, internal, base, isTarget) {
if (process.noDeprecation) {
return;
}
const pjsonPath = fileURLToPath(pjsonUrl);
const double = RegExpPrototypeExec(doubleSlashRegEx, isTarget ? target : request) !== null;
process.emitWarning(
`Use of deprecated ${double ? 'double slash' :
'leading or trailing slash matching'} resolving "${target}" for module ` +
`request "${request}" ${request !== match ? `matched to "${match}" ` : ''
}in the "${internal ? 'imports' : 'exports'}" field module resolution of the package at ${
pjsonPath}${base ? ` imported from ${fileURLToPath(base)}` : ''}.`,
'DeprecationWarning',
'DEP0166',
);
}

/**
* Emits a deprecation warning if the given URL is a module and
* the package.json file does not define a "main" or "exports" field.
Expand Down Expand Up @@ -323,7 +267,6 @@ function invalidPackageTarget(
}

const invalidSegmentRegEx = /(^|\\|\/)((\.|%2e)(\.|%2e)?|(n|%6e|%4e)(o|%6f|%4f)(d|%64|%44)(e|%65|%45)(_|%5f)(m|%6d|%4d)(o|%6f|%4f)(d|%64|%44)(u|%75|%55)(l|%6c|%4c)(e|%65|%45)(s|%73|%53))?(\\|\/|$)/i;
const deprecatedInvalidSegmentRegEx = /(^|\\|\/)((\.|%2e)(\.|%2e)?|(n|%6e|%4e)(o|%6f|%4f)(d|%64|%44)(e|%65|%45)(_|%5f)(m|%6d|%4d)(o|%6f|%4f)(d|%64|%44)(u|%75|%55)(l|%6c|%4c)(e|%65|%45)(s|%73|%53))(\\|\/|$)/i;
const patternRegEx = /\*/g;

/**
Expand All @@ -335,7 +278,6 @@ const patternRegEx = /\*/g;
* @param {string} base - The base URL to resolve the target against.
* @param {RegExp} pattern - The pattern to replace in the target string.
* @param {boolean} internal - Whether the target is internal to the package.
* @param {boolean} isPathMap - Whether the target is a path map.
* @param {string[]} conditions - The import conditions.
* @returns {URL} - The resolved URL object.
* @throws {ERR_INVALID_PACKAGE_TARGET} - If the target is invalid.
Expand All @@ -349,7 +291,6 @@ function resolvePackageTargetString(
base,
pattern,
internal,
isPathMap,
conditions,
) {

Expand All @@ -374,19 +315,7 @@ function resolvePackageTargetString(
}

if (RegExpPrototypeExec(invalidSegmentRegEx, StringPrototypeSlice(target, 2)) !== null) {
if (RegExpPrototypeExec(deprecatedInvalidSegmentRegEx, StringPrototypeSlice(target, 2)) === null) {
if (!isPathMap) {
const request = pattern ?
StringPrototypeReplace(match, '*', () => subpath) :
match + subpath;
const resolvedTarget = pattern ?
RegExpPrototypeSymbolReplace(patternRegEx, target, () => subpath) :
target;
emitInvalidSegmentDeprecation(resolvedTarget, request, match, packageJSONUrl, internal, base, true);
}
} else {
throw invalidPackageTarget(match, target, packageJSONUrl, internal, base);
}
throw invalidPackageTarget(match, target, packageJSONUrl, internal, base);
}

const resolved = new URL(target, packageJSONUrl);
Expand All @@ -401,16 +330,7 @@ function resolvePackageTargetString(

if (RegExpPrototypeExec(invalidSegmentRegEx, subpath) !== null) {
const request = pattern ? StringPrototypeReplace(match, '*', () => subpath) : match + subpath;
if (RegExpPrototypeExec(deprecatedInvalidSegmentRegEx, subpath) === null) {
if (!isPathMap) {
const resolvedTarget = pattern ?
RegExpPrototypeSymbolReplace(patternRegEx, target, () => subpath) :
target;
emitInvalidSegmentDeprecation(resolvedTarget, request, match, packageJSONUrl, internal, base, false);
}
} else {
throwInvalidSubpath(request, match, packageJSONUrl, internal, base);
}
throwInvalidSubpath(request, match, packageJSONUrl, internal, base);
}

if (pattern) {
Expand Down Expand Up @@ -442,16 +362,15 @@ function isArrayIndex(key) {
* @param {string} base - The base path to resolve.
* @param {RegExp} pattern - The pattern to match.
* @param {boolean} internal - Whether the package is internal.
* @param {boolean} isPathMap - Whether the package is a path map.
* @param {Set<string>} conditions - The conditions to match.
* @returns {URL | null | undefined} - The resolved target, or null if not found, or undefined if not resolvable.
*/
function resolvePackageTarget(packageJSONUrl, target, subpath, packageSubpath,
base, pattern, internal, isPathMap, conditions) {
base, pattern, internal, conditions) {
if (typeof target === 'string') {
return resolvePackageTargetString(
target, subpath, packageSubpath, packageJSONUrl, base, pattern, internal,
isPathMap, conditions);
conditions);
} else if (ArrayIsArray(target)) {
if (target.length === 0) {
return null;
Expand All @@ -464,7 +383,7 @@ function resolvePackageTarget(packageJSONUrl, target, subpath, packageSubpath,
try {
resolveResult = resolvePackageTarget(
packageJSONUrl, targetItem, subpath, packageSubpath, base, pattern,
internal, isPathMap, conditions);
internal, conditions);
} catch (e) {
lastException = e;
if (e.code === 'ERR_INVALID_PACKAGE_TARGET') {
Expand Down Expand Up @@ -501,7 +420,7 @@ function resolvePackageTarget(packageJSONUrl, target, subpath, packageSubpath,
const conditionalTarget = target[key];
const resolveResult = resolvePackageTarget(
packageJSONUrl, conditionalTarget, subpath, packageSubpath, base,
pattern, internal, isPathMap, conditions);
pattern, internal, conditions);
if (resolveResult === undefined) { continue; }
return resolveResult;
}
Expand Down Expand Up @@ -560,12 +479,18 @@ function packageExportsResolve(
exports = { '.': exports };
}

if (StringPrototypeEndsWith(packageSubpath, '/')) {
const reason = `is not a valid subpath for the "exports" resolution of ${
fileURLToPath(packageJSONUrl)}`;
throw new ERR_INVALID_MODULE_SPECIFIER(packageSubpath, reason,
base && fileURLToPath(base));
}

if (ObjectPrototypeHasOwnProperty(exports, packageSubpath) &&
!StringPrototypeIncludes(packageSubpath, '*') &&
!StringPrototypeEndsWith(packageSubpath, '/')) {
!StringPrototypeIncludes(packageSubpath, '*')) {
const target = exports[packageSubpath];
const resolveResult = resolvePackageTarget(
packageJSONUrl, target, '', packageSubpath, base, false, false, false,
packageJSONUrl, target, '', packageSubpath, base, false, false,
conditions,
);

Expand All @@ -585,16 +510,6 @@ function packageExportsResolve(
if (patternIndex !== -1 &&
StringPrototypeStartsWith(packageSubpath,
StringPrototypeSlice(key, 0, patternIndex))) {
// When this reaches EOL, this can throw at the top of the whole function:
//
// if (StringPrototypeEndsWith(packageSubpath, '/'))
// throwInvalidSubpath(packageSubpath)
//
// To match "imports" and the spec.
if (StringPrototypeEndsWith(packageSubpath, '/')) {
emitTrailingSlashPatternDeprecation(packageSubpath, packageJSONUrl,
base);
}
const patternTrailer = StringPrototypeSlice(key, patternIndex + 1);
if (packageSubpath.length >= key.length &&
StringPrototypeEndsWith(packageSubpath, patternTrailer) &&
Expand All @@ -618,7 +533,6 @@ function packageExportsResolve(
base,
true,
false,
StringPrototypeEndsWith(packageSubpath, '/'),
conditions);

if (resolveResult == null) {
Expand Down Expand Up @@ -674,7 +588,7 @@ function packageImportsResolve(name, base, conditions) {
if (ObjectPrototypeHasOwnProperty(imports, name) &&
!StringPrototypeIncludes(name, '*')) {
const resolveResult = resolvePackageTarget(
packageJSONUrl, imports[name], '', name, base, false, true, false,
packageJSONUrl, imports[name], '', name, base, false, true,
conditions,
);
if (resolveResult != null) {
Expand Down Expand Up @@ -708,7 +622,7 @@ function packageImportsResolve(name, base, conditions) {
const resolveResult = resolvePackageTarget(packageJSONUrl, target,
bestMatchSubpath,
bestMatch, base, true,
true, false, conditions);
true, conditions);
if (resolveResult != null) {
return resolveResult;
}
Expand Down
13 changes: 0 additions & 13 deletions test/es-module/test-esm-exports-deprecations.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,6 @@ import assert from 'assert';

let curWarning = 0;
const expectedWarnings = [
'Use of deprecated leading or trailing slash',
'Use of deprecated double slash',
'.//asdf.js',
'".//internal/test.js"',
'".//internal//test.js"',
'"./////internal/////test.js"',
'"./trailing-pattern-slash/"',
'"./subpath/dir1/dir1.js"',
'"./subpath//dir1/dir1.js"',
'.//asdf.js',
'".//internal/test.js"',
'".//internal//test.js"',
'"./////internal/////test.js"',
'no_exports',
'default_index',
];
Expand Down
29 changes: 15 additions & 14 deletions test/es-module/test-esm-exports.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,6 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js';
['pkgexports/dir1/dir1/trailer', maybeWrapped({ default: 'main' })],
['pkgexports/dir2/dir2/trailer', maybeWrapped({ default: 'index' })],
['pkgexports/a/dir1/dir1', maybeWrapped({ default: 'main' })],
['pkgexports/a/b/dir1/dir1', maybeWrapped({ default: 'main' })],

// Deprecated:
// Double slashes:
['pkgexports/a//dir1/dir1', maybeWrapped({ default: 'main' })],
// double slash target
['pkgexports/doubleslash', maybeWrapped({ default: 'asdf' })],
// Null target with several slashes
['pkgexports/sub//internal/test.js', maybeWrapped({ default: 'internal only' })],
['pkgexports/sub//internal//test.js', maybeWrapped({ default: 'internal only' })],
['pkgexports/sub/////internal/////test.js', maybeWrapped({ default: 'internal only' })],
// trailing slash
['pkgexports/trailing-pattern-slash/',
maybeWrapped({ default: 'trailing-pattern-slash' })],
]);

if (!isRequire) {
Expand Down Expand Up @@ -112,6 +98,8 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js';
['pkgexports/nofallback2', './nofallback2'],
// Reaching into nested node_modules
['pkgexports/nodemodules', './nodemodules'],
// Double slash target
['pkgexports/doubleslash', './doubleslash'],
// Self resolve invalid
['pkgexports/resolve-self-invalid', './invalid2'],
]);
Expand All @@ -124,6 +112,12 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js';
['pkgexports/sub/no%64e_modules', './sub/no%64e_modules'],
// Cannot backtrack below exposed path, even with percent encoded "."
['pkgexports/sub/%2e./asdf', './asdf'],
// Pattern matches containing empty segments (double slashes)
['pkgexports/a/b/dir1/dir1', './a/b/dir1/dir1'],
['pkgexports/a//dir1/dir1', './a//dir1/dir1'],
['pkgexports/sub//internal/test.js', './sub//internal/test.js'],
['pkgexports/sub//internal//test.js', './sub//internal//test.js'],
['pkgexports/sub/////internal/////test.js', './sub/////internal/////test.js'],
]);

for (const [specifier, subpath] of undefinedExports) {
Expand Down Expand Up @@ -154,6 +148,13 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js';
}));
}

// Subpaths ending in "/" are invalid, even when matching a pattern
loadFixture('pkgexports/trailing-pattern-slash/').catch(mustCall((err) => {
assert.strictEqual(err.code, 'ERR_INVALID_MODULE_SPECIFIER');
assertStartsWith(err.message, 'Invalid module "./trailing-pattern-slash/"');
assertIncludes(err.message, 'is not a valid subpath for the "exports"');
}));

// Conditional export, even with no match, should still be used instead
// of falling back to main
if (isRequire) {
Expand Down
Loading
Loading