Skip to content

Commit

Permalink
fix(pnp): ensure URL is imported on Node.js < 20
Browse files Browse the repository at this point in the history
  • Loading branch information
merceyz committed Jan 4, 2024
1 parent c68c716 commit 99b8c34
Show file tree
Hide file tree
Showing 17 changed files with 105 additions and 64 deletions.
12 changes: 12 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,17 @@ module.exports = {
message: `Use 'httpUtils' instead`,
},
],
'no-restricted-imports': [
`error`,
{
patterns: [
{
group: [`url`, `node:url`],
importNames: [`URL`],
message: `URL is a global, no need to import it`,
},
],
},
],
},
};
28 changes: 4 additions & 24 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 3 additions & 23 deletions .pnp.loader.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions .yarn/versions/ae9dd917.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
releases:
"@yarnpkg/cli": patch
"@yarnpkg/core": patch
"@yarnpkg/fslib": patch
"@yarnpkg/plugin-essentials": patch
"@yarnpkg/plugin-npm": patch
"@yarnpkg/pnp": patch

declined:
- "@yarnpkg/plugin-compat"
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-dlx"
- "@yarnpkg/plugin-exec"
- "@yarnpkg/plugin-file"
- "@yarnpkg/plugin-git"
- "@yarnpkg/plugin-github"
- "@yarnpkg/plugin-http"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-link"
- "@yarnpkg/plugin-nm"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-patch"
- "@yarnpkg/plugin-pnp"
- "@yarnpkg/plugin-pnpm"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/plugin-workspace-tools"
- vscode-zipfs
- "@yarnpkg/builder"
- "@yarnpkg/doctor"
- "@yarnpkg/extensions"
- "@yarnpkg/libzip"
- "@yarnpkg/nm"
- "@yarnpkg/pnpify"
- "@yarnpkg/sdks"
- "@yarnpkg/shell"
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {npath} from '@yarnpkg/fslib';
import {delimiter} from 'path';
import {URL} from 'url';

import * as exec from './exec';
import * as tests from './tests';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {YarnVersion, formatUtils, httpUtils, structUtils, hashUtils}
import {PortablePath, npath, ppath, xfs} from '@yarnpkg/fslib';
import {Command, Option, Usage} from 'clipanion';
import semver from 'semver';
import {URL} from 'url';
import {runInNewContext} from 'vm';

import {getAvailablePlugins} from './list';
Expand Down
1 change: 0 additions & 1 deletion packages/plugin-npm/sources/NpmSemverFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {Configuration, Fetcher, FetchOptions, MinimalFetchOptions} from '@yarnpk
import {structUtils, tgzUtils, semverUtils} from '@yarnpkg/core';
import {Locator, MessageName, ReportError} from '@yarnpkg/core';
import semver from 'semver';
import {URL} from 'url';

import {PROTOCOL} from './constants';
import * as npmConfigUtils from './npmConfigUtils';
Expand Down
1 change: 0 additions & 1 deletion packages/plugin-npm/sources/npmHttpUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {Filename, PortablePath, ppath, xfs}
import {prompt} from 'enquirer';
import pick from 'lodash/pick';
import semver from 'semver';
import {URL} from 'url';

import {Hooks} from './index';
import * as npmConfigUtils from './npmConfigUtils';
Expand Down
1 change: 0 additions & 1 deletion packages/plugin-npm/sources/npmPublishUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {PortablePath, xfs, npath} from '@yarnpkg/fslib';
import {packUtils} from '@yarnpkg/plugin-pack';
import {createHash} from 'crypto';
import ssri from 'ssri';
import {URL} from 'url';

import {normalizeRegistry} from './npmConfigUtils';

Expand Down
1 change: 0 additions & 1 deletion packages/yarnpkg-core/sources/Plugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {PortablePath} from '@yarnpkg/fslib';
import {CommandClass} from 'clipanion';
import {Writable, Readable} from 'stream';
import {URL} from 'url';

import {PluginConfiguration, Configuration, ConfigurationDefinitionMap, PackageExtensionData} from './Configuration';
import {Fetcher} from './Fetcher';
Expand Down
1 change: 0 additions & 1 deletion packages/yarnpkg-core/sources/httpUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {Agent as HttpsAgent} from 'https';
import {Agent as HttpAgent, IncomingHttpHeaders} from 'http';
import micromatch from 'micromatch';
import tunnel, {ProxyOptions} from 'tunnel';
import {URL} from 'url';

import {ConfigurationValueMap, Configuration} from './Configuration';
import {MessageName} from './MessageName';
Expand Down
12 changes: 6 additions & 6 deletions packages/yarnpkg-fslib/sources/NodePathFS.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import buffer from 'buffer';
import {URL, fileURLToPath} from 'url';
import {inspect} from 'util';
import buffer from 'buffer';
import {fileURLToPath} from 'url';
import {inspect} from 'util';

import {FakeFS} from './FakeFS';
import {ProxiedFS} from './ProxiedFS';
import {npath, NativePath} from './path';
import {FakeFS} from './FakeFS';
import {ProxiedFS} from './ProxiedFS';
import {npath, NativePath} from './path';

/**
* Adds support for file URLs and Buffers to the wrapped `baseFs`, but *not* inside the typings.
Expand Down
38 changes: 38 additions & 0 deletions packages/yarnpkg-pnp/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import {brotliCompressSync} from 'zlib';

import pkg from './package.json';

/**
* @returns {import('rollup').Plugin}
*/
function wrapOutput() {
return {
name: `wrap-output`,
Expand All @@ -25,6 +28,39 @@ function wrapOutput() {
};
}

/**
* Before https://github.com/nodejs/node/pull/46904 using a custom global URL class
* wasn't supported by `fileURLToPath` so this plugin ensures that for Node.js < 20
* we always use the builtin URL class.
* TODO: Remove this plugin when dropping support for Node.js < 20
* @returns {import('rollup').Plugin}
*/
function importURL() {
return {
name: `import-url`,
resolveId(id) {
if (id === `virtual:url`) return `\0virtual:url`;

return undefined;
},
load(id) {
if (id === `\0virtual:url`) {
return `
import { URL as nodeURL } from 'url';
export const URL = Number(process.versions.node.split('.', 1)[0]) < 20 ? nodeURL : globalThis.URL;
`;
}
return undefined;
},
transform(code, id) {
if (code.includes(`new URL`) || code.includes(`instanceof URL`))
return `import {URL} from 'virtual:url';\n${code}`;

return undefined;
},
};
}

// eslint-disable-next-line arca/no-default-export
export default defineConfig([
{
Expand Down Expand Up @@ -53,6 +89,7 @@ export default defineConfig([
},
}),
cjs({transformMixedEsModules: true, extensions: [`.js`, `.ts`]}),
importURL(),
wrapOutput(),
],
},
Expand Down Expand Up @@ -80,6 +117,7 @@ export default defineConfig([
},
}),
cjs({requireReturnsDefault: `preferred`}),
importURL(),
wrapOutput(),
],
},
Expand Down
2 changes: 1 addition & 1 deletion packages/yarnpkg-pnp/sources/esm-loader/built-loader.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion packages/yarnpkg-pnp/sources/esm-loader/loaderUtils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {NativePath} from '@yarnpkg/fslib';
import fs from 'fs';
import path from 'path';
import {URL} from 'url';

import * as nodeUtils from '../loader/nodeUtils';

Expand Down
2 changes: 1 addition & 1 deletion packages/yarnpkg-pnp/sources/hook.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/yarnpkg-pnp/sources/loader/applyPatch.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {FakeFS, PosixFS, npath, patchFs, PortablePath, NativePath, VirtualFS} from '@yarnpkg/fslib';
import fs from 'fs';
import {Module, isBuiltin} from 'module';
import {URL, fileURLToPath} from 'url';
import {fileURLToPath} from 'url';

import {PnpApi} from '../types';

Expand Down

0 comments on commit 99b8c34

Please sign in to comment.