@pnpm/resolving.npm-resolver 1004.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +65 -0
- package/lib/fetch.d.ts +23 -0
- package/lib/fetch.js +92 -0
- package/lib/index.d.ts +99 -0
- package/lib/index.js +547 -0
- package/lib/normalizeRegistryUrl.d.ts +4 -0
- package/lib/normalizeRegistryUrl.js +12 -0
- package/lib/parseBareSpecifier.d.ts +11 -0
- package/lib/parseBareSpecifier.js +55 -0
- package/lib/pickPackage.d.ts +34 -0
- package/lib/pickPackage.js +278 -0
- package/lib/pickPackageFromMeta.d.ts +20 -0
- package/lib/pickPackageFromMeta.js +202 -0
- package/lib/toRaw.d.ts +2 -0
- package/lib/toRaw.js +4 -0
- package/lib/trustChecks.d.ts +9 -0
- package/lib/trustChecks.js +89 -0
- package/lib/whichVersionIsPinned.d.ts +2 -0
- package/lib/whichVersionIsPinned.js +30 -0
- package/lib/workspacePrefToNpm.d.ts +1 -0
- package/lib/workspacePrefToNpm.js +13 -0
- package/package.json +90 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2015-2016 Rico Sta. Cruz and other contributors
|
|
4
|
+
Copyright (c) 2016-2026 Zoltan Kochan and other contributors
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# @pnpm/npm-resolver
|
|
2
|
+
|
|
3
|
+
> Resolver for npm-hosted packages
|
|
4
|
+
|
|
5
|
+
<!--@shields('npm')-->
|
|
6
|
+
[](https://www.npmjs.com/package/@pnpm/npm-resolver)
|
|
7
|
+
<!--/@-->
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
pnpm add @pnpm/npm-resolver
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
<!--@example('./example.js')-->
|
|
18
|
+
```js
|
|
19
|
+
'use strict'
|
|
20
|
+
const createResolveFromNpm = require('@pnpm/npm-resolver').default
|
|
21
|
+
|
|
22
|
+
const resolveFromNpm = createResolveFromNpm({
|
|
23
|
+
store: '.store',
|
|
24
|
+
offline: false,
|
|
25
|
+
rawConfig: {
|
|
26
|
+
registry: 'https://registry.npmjs.org/',
|
|
27
|
+
},
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
resolveFromNpm({alias: 'is-positive', bareSpecifier: '1.0.0'}, {
|
|
31
|
+
registry: 'https://registry.npmjs.org/',
|
|
32
|
+
})
|
|
33
|
+
.then(resolveResult => console.log(JSON.stringify(resolveResult, null, 2)))
|
|
34
|
+
//> {
|
|
35
|
+
// "id": "registry.npmjs.org/is-positive/1.0.0",
|
|
36
|
+
// "latest": "3.1.0",
|
|
37
|
+
// "package": {
|
|
38
|
+
// "name": "is-positive",
|
|
39
|
+
// "version": "1.0.0",
|
|
40
|
+
// "devDependencies": {
|
|
41
|
+
// "ava": "^0.0.4"
|
|
42
|
+
// },
|
|
43
|
+
// "_hasShrinkwrap": false,
|
|
44
|
+
// "directories": {},
|
|
45
|
+
// "dist": {
|
|
46
|
+
// "shasum": "88009856b64a2f1eb7d8bb0179418424ae0452cb",
|
|
47
|
+
// "tarball": "https://registry.npmjs.org/is-positive/-/is-positive-1.0.0.tgz"
|
|
48
|
+
// },
|
|
49
|
+
// "engines": {
|
|
50
|
+
// "node": ">=0.10.0"
|
|
51
|
+
// }
|
|
52
|
+
// },
|
|
53
|
+
// "resolution": {
|
|
54
|
+
// "integrity": "sha1-iACYVrZKLx632LsBeUGEJK4EUss=",
|
|
55
|
+
// "registry": "https://registry.npmjs.org/",
|
|
56
|
+
// "tarball": "https://registry.npmjs.org/is-positive/-/is-positive-1.0.0.tgz"
|
|
57
|
+
// },
|
|
58
|
+
// "resolvedVia": "npm-registry"
|
|
59
|
+
// }
|
|
60
|
+
```
|
|
61
|
+
<!--/@-->
|
|
62
|
+
|
|
63
|
+
## License
|
|
64
|
+
|
|
65
|
+
MIT
|
package/lib/fetch.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { FetchError, type FetchErrorRequest, type FetchErrorResponse } from '@pnpm/error';
|
|
2
|
+
import type { FetchFromRegistry, RetryTimeoutOptions } from '@pnpm/fetching.types';
|
|
3
|
+
import type { PackageMeta } from '@pnpm/resolving.registry.types';
|
|
4
|
+
export interface FetchMetadataResult {
|
|
5
|
+
meta: PackageMeta;
|
|
6
|
+
jsonText: string;
|
|
7
|
+
}
|
|
8
|
+
export declare class RegistryResponseError extends FetchError {
|
|
9
|
+
readonly pkgName: string;
|
|
10
|
+
constructor(request: FetchErrorRequest, response: FetchErrorResponse, pkgName: string);
|
|
11
|
+
}
|
|
12
|
+
export interface FetchMetadataFromFromRegistryOptions {
|
|
13
|
+
fetch: FetchFromRegistry;
|
|
14
|
+
retry: RetryTimeoutOptions;
|
|
15
|
+
timeout: number;
|
|
16
|
+
fetchWarnTimeoutMs: number;
|
|
17
|
+
}
|
|
18
|
+
export interface FetchMetadataOptions {
|
|
19
|
+
registry: string;
|
|
20
|
+
authHeaderValue?: string;
|
|
21
|
+
fullMetadata?: boolean;
|
|
22
|
+
}
|
|
23
|
+
export declare function fetchMetadataFromFromRegistry(fetchOpts: FetchMetadataFromFromRegistryOptions, pkgName: string, { authHeaderValue, fullMetadata, registry }: FetchMetadataOptions): Promise<FetchMetadataResult>;
|
package/lib/fetch.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import url from 'node:url';
|
|
2
|
+
import { requestRetryLogger } from '@pnpm/core-loggers';
|
|
3
|
+
import { FetchError, PnpmError, } from '@pnpm/error';
|
|
4
|
+
import { globalWarn } from '@pnpm/logger';
|
|
5
|
+
import * as retry from '@zkochan/retry';
|
|
6
|
+
// https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
|
|
7
|
+
// eslint-disable-next-line regexp/no-super-linear-backtracking, regexp/use-ignore-case
|
|
8
|
+
const semverRegex = /(.*)(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/;
|
|
9
|
+
export class RegistryResponseError extends FetchError {
|
|
10
|
+
pkgName;
|
|
11
|
+
constructor(request, response, pkgName) {
|
|
12
|
+
let hint;
|
|
13
|
+
if (response.status === 404) {
|
|
14
|
+
hint = `${pkgName} is not in the npm registry, or you have no permission to fetch it.`;
|
|
15
|
+
const matched = pkgName.match(semverRegex);
|
|
16
|
+
if (matched != null) {
|
|
17
|
+
hint += ` Did you mean ${matched[1]}?`;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
super(request, response, hint);
|
|
21
|
+
this.pkgName = pkgName;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
export async function fetchMetadataFromFromRegistry(fetchOpts, pkgName, { authHeaderValue, fullMetadata, registry, }) {
|
|
25
|
+
const uri = toUri(pkgName, registry);
|
|
26
|
+
const op = retry.operation(fetchOpts.retry);
|
|
27
|
+
return new Promise((resolve, reject) => {
|
|
28
|
+
op.attempt(async (attempt) => {
|
|
29
|
+
let response;
|
|
30
|
+
const startTime = Date.now();
|
|
31
|
+
try {
|
|
32
|
+
response = await fetchOpts.fetch(uri, {
|
|
33
|
+
authHeaderValue,
|
|
34
|
+
compress: true,
|
|
35
|
+
fullMetadata,
|
|
36
|
+
retry: fetchOpts.retry,
|
|
37
|
+
timeout: fetchOpts.timeout,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
catch (error) { // eslint-disable-line
|
|
41
|
+
reject(new PnpmError('META_FETCH_FAIL', `GET ${uri}: ${error.message}`, { attempts: attempt }));
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
if (response.status >= 400) {
|
|
45
|
+
const request = {
|
|
46
|
+
authHeaderValue,
|
|
47
|
+
url: uri,
|
|
48
|
+
};
|
|
49
|
+
reject(new RegistryResponseError(request, response, pkgName));
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
// Here we only retry broken JSON responses.
|
|
53
|
+
// Other HTTP issues are retried by the @pnpm/network.fetch library
|
|
54
|
+
try {
|
|
55
|
+
const jsonText = await response.text();
|
|
56
|
+
const meta = JSON.parse(jsonText);
|
|
57
|
+
// Check if request took longer than expected
|
|
58
|
+
const elapsedMs = Date.now() - startTime;
|
|
59
|
+
if (elapsedMs > fetchOpts.fetchWarnTimeoutMs) {
|
|
60
|
+
globalWarn(`Request took ${elapsedMs}ms: ${uri}`);
|
|
61
|
+
}
|
|
62
|
+
resolve({ meta, jsonText });
|
|
63
|
+
}
|
|
64
|
+
catch (error) { // eslint-disable-line
|
|
65
|
+
const timeout = op.retry(new PnpmError('BROKEN_METADATA_JSON', error.message));
|
|
66
|
+
if (timeout === false) {
|
|
67
|
+
reject(op.mainError());
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
requestRetryLogger.debug({
|
|
71
|
+
attempt,
|
|
72
|
+
error,
|
|
73
|
+
maxRetries: fetchOpts.retry.retries,
|
|
74
|
+
method: 'GET',
|
|
75
|
+
timeout,
|
|
76
|
+
url: uri,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
function toUri(pkgName, registry) {
|
|
83
|
+
let encodedName;
|
|
84
|
+
if (pkgName[0] === '@') {
|
|
85
|
+
encodedName = `@${encodeURIComponent(pkgName.slice(1))}`;
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
encodedName = encodeURIComponent(pkgName);
|
|
89
|
+
}
|
|
90
|
+
return new url.URL(encodedName, registry.endsWith('/') ? registry : `${registry}/`).toString();
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=fetch.js.map
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { PnpmError } from '@pnpm/error';
|
|
2
|
+
import type { FetchFromRegistry, GetAuthHeader, RetryTimeoutOptions } from '@pnpm/fetching.types';
|
|
3
|
+
import type { PackageMeta } from '@pnpm/resolving.registry.types';
|
|
4
|
+
import type { DirectoryResolution, PkgResolutionId, PreferredVersions, ResolveResult, TarballResolution, WantedDependency, WorkspacePackages } from '@pnpm/resolving.resolver-base';
|
|
5
|
+
import type { DependencyManifest, PackageVersionPolicy, PinnedVersion, Registries, TrustPolicy } from '@pnpm/types';
|
|
6
|
+
import { RegistryResponseError } from './fetch.js';
|
|
7
|
+
import { parseBareSpecifier, type RegistryPackageSpec } from './parseBareSpecifier.js';
|
|
8
|
+
import { type PackageMetaCache, pickPackage, type PickPackageOptions } from './pickPackage.js';
|
|
9
|
+
import { workspacePrefToNpm } from './workspacePrefToNpm.js';
|
|
10
|
+
export interface NoMatchingVersionErrorOptions {
|
|
11
|
+
wantedDependency: WantedDependency;
|
|
12
|
+
packageMeta: PackageMeta;
|
|
13
|
+
registry: string;
|
|
14
|
+
immatureVersion?: string;
|
|
15
|
+
publishedBy?: Date;
|
|
16
|
+
}
|
|
17
|
+
export declare class NoMatchingVersionError extends PnpmError {
|
|
18
|
+
readonly packageMeta: PackageMeta;
|
|
19
|
+
readonly immatureVersion?: string;
|
|
20
|
+
constructor(opts: NoMatchingVersionErrorOptions);
|
|
21
|
+
}
|
|
22
|
+
export { type PackageMeta, type PackageMetaCache, parseBareSpecifier, type RegistryPackageSpec, RegistryResponseError, workspacePrefToNpm, };
|
|
23
|
+
export { whichVersionIsPinned } from './whichVersionIsPinned.js';
|
|
24
|
+
export interface ResolverFactoryOptions {
|
|
25
|
+
cacheDir: string;
|
|
26
|
+
storeDir?: string;
|
|
27
|
+
fullMetadata?: boolean;
|
|
28
|
+
filterMetadata?: boolean;
|
|
29
|
+
offline?: boolean;
|
|
30
|
+
preferOffline?: boolean;
|
|
31
|
+
retry?: RetryTimeoutOptions;
|
|
32
|
+
timeout?: number;
|
|
33
|
+
registries: Registries;
|
|
34
|
+
saveWorkspaceProtocol?: boolean | 'rolling';
|
|
35
|
+
preserveAbsolutePaths?: boolean;
|
|
36
|
+
strictPublishedByCheck?: boolean;
|
|
37
|
+
fetchWarnTimeoutMs?: number;
|
|
38
|
+
}
|
|
39
|
+
export interface NpmResolveResult extends ResolveResult {
|
|
40
|
+
latest?: string;
|
|
41
|
+
manifest: DependencyManifest;
|
|
42
|
+
resolution: TarballResolution;
|
|
43
|
+
resolvedVia: 'npm-registry';
|
|
44
|
+
}
|
|
45
|
+
export interface JsrResolveResult extends ResolveResult {
|
|
46
|
+
alias: string;
|
|
47
|
+
manifest: DependencyManifest;
|
|
48
|
+
resolution: TarballResolution;
|
|
49
|
+
resolvedVia: 'jsr-registry';
|
|
50
|
+
}
|
|
51
|
+
export interface WorkspaceResolveResult extends ResolveResult {
|
|
52
|
+
manifest: DependencyManifest;
|
|
53
|
+
resolution: DirectoryResolution;
|
|
54
|
+
resolvedVia: 'workspace';
|
|
55
|
+
}
|
|
56
|
+
export type NpmResolver = (wantedDependency: WantedDependency & {
|
|
57
|
+
optional?: boolean;
|
|
58
|
+
}, opts: ResolveFromNpmOptions) => Promise<NpmResolveResult | JsrResolveResult | WorkspaceResolveResult | null>;
|
|
59
|
+
export declare function createNpmResolver(fetchFromRegistry: FetchFromRegistry, getAuthHeader: GetAuthHeader, opts: ResolverFactoryOptions): {
|
|
60
|
+
resolveFromNpm: NpmResolver;
|
|
61
|
+
resolveFromJsr: NpmResolver;
|
|
62
|
+
clearCache: () => void;
|
|
63
|
+
};
|
|
64
|
+
export interface ResolveFromNpmContext {
|
|
65
|
+
pickPackage: (spec: RegistryPackageSpec, opts: PickPackageOptions) => ReturnType<typeof pickPackage>;
|
|
66
|
+
getAuthHeaderValueByURI: (registry: string) => string | undefined;
|
|
67
|
+
registries: Registries;
|
|
68
|
+
saveWorkspaceProtocol?: boolean | 'rolling';
|
|
69
|
+
peekManifestFromStore?: (opts: {
|
|
70
|
+
id: PkgResolutionId;
|
|
71
|
+
integrity: string;
|
|
72
|
+
name?: string;
|
|
73
|
+
version?: string;
|
|
74
|
+
}) => Promise<DependencyManifest | undefined>;
|
|
75
|
+
}
|
|
76
|
+
export type ResolveFromNpmOptions = {
|
|
77
|
+
alwaysTryWorkspacePackages?: boolean;
|
|
78
|
+
defaultTag?: string;
|
|
79
|
+
publishedBy?: Date;
|
|
80
|
+
publishedByExclude?: PackageVersionPolicy;
|
|
81
|
+
pickLowestVersion?: boolean;
|
|
82
|
+
trustPolicy?: TrustPolicy;
|
|
83
|
+
trustPolicyExclude?: PackageVersionPolicy;
|
|
84
|
+
trustPolicyIgnoreAfter?: number;
|
|
85
|
+
dryRun?: boolean;
|
|
86
|
+
lockfileDir?: string;
|
|
87
|
+
preferredVersions?: PreferredVersions;
|
|
88
|
+
preferWorkspacePackages?: boolean;
|
|
89
|
+
update?: false | 'compatible' | 'latest';
|
|
90
|
+
injectWorkspacePackages?: boolean;
|
|
91
|
+
calcSpecifier?: boolean;
|
|
92
|
+
pinnedVersion?: PinnedVersion;
|
|
93
|
+
} & ({
|
|
94
|
+
projectDir?: string;
|
|
95
|
+
workspacePackages?: undefined;
|
|
96
|
+
} | {
|
|
97
|
+
projectDir: string;
|
|
98
|
+
workspacePackages: WorkspacePackages;
|
|
99
|
+
});
|