@pnpm/deps.graph-hasher 1002.0.8

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 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,17 @@
1
+ # @pnpm/calc-dep-state
2
+
3
+ > Calculates the state of a dependency
4
+
5
+ <!--@shields('npm')-->
6
+ [![npm version](https://img.shields.io/npm/v/@pnpm/calc-dep-state.svg)](https://www.npmjs.com/package/@pnpm/calc-dep-state)
7
+ <!--/@-->
8
+
9
+ ## Installation
10
+
11
+ ```sh
12
+ pnpm add @pnpm/calc-dep-state
13
+ ```
14
+
15
+ ## License
16
+
17
+ MIT
package/lib/index.d.ts ADDED
@@ -0,0 +1,42 @@
1
+ import type { LockfileObject, LockfileResolution, PackageSnapshot } from '@pnpm/lockfile.types';
2
+ import type { AllowBuild, DepPath, PkgIdWithPatchHash } from '@pnpm/types';
3
+ export type DepsGraph<T extends string> = Record<T, DepsGraphNode<T>>;
4
+ export interface DepsGraphNode<T extends string> {
5
+ children: {
6
+ [alias: string]: T;
7
+ };
8
+ pkgIdWithPatchHash?: PkgIdWithPatchHash;
9
+ resolution?: LockfileResolution;
10
+ fullPkgId?: string;
11
+ }
12
+ export interface DepsStateCache {
13
+ [depPath: string]: string;
14
+ }
15
+ export declare function calcDepState<T extends string>(depsGraph: DepsGraph<T>, cache: DepsStateCache, depPath: string, opts: {
16
+ patchFileHash?: string;
17
+ includeDepGraphHash: boolean;
18
+ }): string;
19
+ export interface PkgMeta {
20
+ depPath: DepPath;
21
+ name: string;
22
+ version: string;
23
+ }
24
+ export type PkgMetaIterator<T extends PkgMeta> = IterableIterator<T>;
25
+ export interface HashedDepPath<T extends PkgMeta> {
26
+ pkgMeta: T;
27
+ hash: string;
28
+ }
29
+ export declare function iterateHashedGraphNodes<T extends PkgMeta>(graph: DepsGraph<DepPath>, pkgMetaIterator: PkgMetaIterator<T>, allowBuild?: AllowBuild): IterableIterator<HashedDepPath<T>>;
30
+ export declare function calcGraphNodeHash<T extends PkgMeta>({ graph, cache, builtDepPaths, buildRequiredCache }: {
31
+ graph: DepsGraph<DepPath>;
32
+ cache: DepsStateCache;
33
+ builtDepPaths?: Set<DepPath>;
34
+ buildRequiredCache?: Record<string, boolean>;
35
+ }, pkgMeta: T): string;
36
+ export declare function calcLeafGlobalVirtualStorePath(fullPkgId: string, name: string, version: string): string;
37
+ export interface PkgMetaAndSnapshot extends PkgMeta {
38
+ pkgSnapshot: PackageSnapshot;
39
+ pkgIdWithPatchHash: PkgIdWithPatchHash;
40
+ }
41
+ export declare function iteratePkgMeta(lockfile: LockfileObject, graph: DepsGraph<DepPath>): PkgMetaIterator<PkgMetaAndSnapshot>;
42
+ export declare function lockfileToDepGraph(lockfile: LockfileObject): DepsGraph<DepPath>;
package/lib/index.js ADDED
@@ -0,0 +1,181 @@
1
+ import { ENGINE_NAME } from '@pnpm/constants';
2
+ import { hashObject, hashObjectWithoutSorting } from '@pnpm/crypto.object-hasher';
3
+ import { getPkgIdWithPatchHash, refToRelative } from '@pnpm/deps.path';
4
+ import { nameVerFromPkgSnapshot } from '@pnpm/lockfile.utils';
5
+ export function calcDepState(depsGraph, cache, depPath, opts) {
6
+ let result = ENGINE_NAME;
7
+ if (opts.includeDepGraphHash) {
8
+ const depGraphHash = calcDepGraphHash(depsGraph, cache, new Set(), depPath);
9
+ result += `;deps=${depGraphHash}`;
10
+ }
11
+ if (opts.patchFileHash) {
12
+ result += `;patch=${opts.patchFileHash}`;
13
+ }
14
+ return result;
15
+ }
16
+ function calcDepGraphHash(depsGraph, cache, parents, depPath) {
17
+ if (cache[depPath])
18
+ return cache[depPath];
19
+ const node = depsGraph[depPath];
20
+ if (!node)
21
+ return '';
22
+ if (!node.fullPkgId) {
23
+ if (!node.pkgIdWithPatchHash) {
24
+ throw new Error(`pkgIdWithPatchHash is not defined for ${depPath} in depsGraph`);
25
+ }
26
+ if (!node.resolution) {
27
+ throw new Error(`resolution is not defined for ${depPath} in depsGraph`);
28
+ }
29
+ node.fullPkgId = createFullPkgId(node.pkgIdWithPatchHash, node.resolution);
30
+ }
31
+ const deps = {};
32
+ if (Object.keys(node.children).length && !parents.has(node.fullPkgId)) {
33
+ const nextParents = new Set([...Array.from(parents), node.fullPkgId]);
34
+ const _calcDepGraphHash = calcDepGraphHash.bind(null, depsGraph, cache, nextParents);
35
+ for (const alias in node.children) {
36
+ if (Object.hasOwn(node.children, alias)) {
37
+ const childId = node.children[alias];
38
+ deps[alias] = _calcDepGraphHash(childId);
39
+ }
40
+ }
41
+ }
42
+ cache[depPath] = hashObject({
43
+ id: node.fullPkgId,
44
+ deps,
45
+ });
46
+ return cache[depPath];
47
+ }
48
+ export function* iterateHashedGraphNodes(graph, pkgMetaIterator, allowBuild) {
49
+ let builtDepPaths;
50
+ let entries;
51
+ if (allowBuild != null) {
52
+ const pkgMetaList = Array.from(pkgMetaIterator);
53
+ builtDepPaths = computeBuiltDepPaths(pkgMetaList, allowBuild);
54
+ entries = pkgMetaList;
55
+ }
56
+ else {
57
+ entries = pkgMetaIterator;
58
+ }
59
+ const _calcGraphNodeHash = calcGraphNodeHash.bind(null, {
60
+ graph,
61
+ cache: {},
62
+ builtDepPaths,
63
+ buildRequiredCache: builtDepPaths !== undefined ? {} : undefined,
64
+ });
65
+ for (const pkgMeta of entries) {
66
+ yield {
67
+ hash: _calcGraphNodeHash(pkgMeta),
68
+ pkgMeta,
69
+ };
70
+ }
71
+ }
72
+ export function calcGraphNodeHash({ graph, cache, builtDepPaths, buildRequiredCache }, pkgMeta) {
73
+ const { name, version, depPath } = pkgMeta;
74
+ // When builtDepPaths is provided (derived from the allowBuilds config),
75
+ // we only include the engine name for packages that are allowed to build
76
+ // or transitively depend on a package that is allowed to build.
77
+ // This makes GVS hashes engine-agnostic for pure-JS packages,
78
+ // so they survive Node.js upgrades and architecture changes.
79
+ const includeEngine = builtDepPaths === undefined ||
80
+ transitivelyRequiresBuild(graph, builtDepPaths, buildRequiredCache ??= {}, depPath, new Set());
81
+ const engine = includeEngine ? ENGINE_NAME : null;
82
+ const deps = calcDepGraphHash(graph, cache, new Set(), depPath);
83
+ const hexDigest = hashObjectWithoutSorting({ engine, deps }, { encoding: 'hex' });
84
+ return formatGlobalVirtualStorePath(name, version, hexDigest);
85
+ }
86
+ export function calcLeafGlobalVirtualStorePath(fullPkgId, name, version) {
87
+ const depsHash = hashObject({ id: fullPkgId, deps: {} });
88
+ const hexDigest = hashObjectWithoutSorting({ engine: null, deps: depsHash }, { encoding: 'hex' });
89
+ return formatGlobalVirtualStorePath(name, version, hexDigest);
90
+ }
91
+ // Use @/ prefix for unscoped packages to maintain uniform 4-level directory depth
92
+ // Scoped: @scope/pkg/version/hash
93
+ // Unscoped: @/pkg/version/hash
94
+ function formatGlobalVirtualStorePath(name, version, hexDigest) {
95
+ const prefix = name.startsWith('@') ? '' : '@/';
96
+ return `${prefix}${name}/${version}/${hexDigest}`;
97
+ }
98
+ export function* iteratePkgMeta(lockfile, graph) {
99
+ if (lockfile.packages == null) {
100
+ return;
101
+ }
102
+ for (const depPath in lockfile.packages) {
103
+ if (!Object.hasOwn(lockfile.packages, depPath)) {
104
+ continue;
105
+ }
106
+ const pkgSnapshot = lockfile.packages[depPath];
107
+ const { name, version } = nameVerFromPkgSnapshot(depPath, pkgSnapshot);
108
+ yield {
109
+ name,
110
+ version,
111
+ depPath: depPath,
112
+ pkgIdWithPatchHash: graph[depPath]?.pkgIdWithPatchHash ?? getPkgIdWithPatchHash(depPath),
113
+ pkgSnapshot,
114
+ };
115
+ }
116
+ }
117
+ export function lockfileToDepGraph(lockfile) {
118
+ const graph = {};
119
+ if (lockfile.packages != null) {
120
+ for (const [depPath, pkgSnapshot] of Object.entries(lockfile.packages)) {
121
+ const children = lockfileDepsToGraphChildren({
122
+ ...pkgSnapshot.dependencies,
123
+ ...pkgSnapshot.optionalDependencies,
124
+ });
125
+ graph[depPath] = {
126
+ children,
127
+ fullPkgId: createFullPkgId(getPkgIdWithPatchHash(depPath), pkgSnapshot.resolution),
128
+ };
129
+ }
130
+ }
131
+ return graph;
132
+ }
133
+ function computeBuiltDepPaths(entries, allowBuild) {
134
+ const builtDepPaths = new Set();
135
+ for (const { depPath, name, version } of entries) {
136
+ if (allowBuild(name, version) === true) {
137
+ builtDepPaths.add(depPath);
138
+ }
139
+ }
140
+ return builtDepPaths;
141
+ }
142
+ function transitivelyRequiresBuild(graph, builtDepPaths, cache, depPath, parents) {
143
+ if (depPath in cache)
144
+ return cache[depPath];
145
+ if (builtDepPaths.has(depPath)) {
146
+ cache[depPath] = true;
147
+ return true;
148
+ }
149
+ const node = graph[depPath];
150
+ if (!node) {
151
+ cache[depPath] = false;
152
+ return false;
153
+ }
154
+ if (parents.has(depPath)) {
155
+ return false;
156
+ }
157
+ const nextParents = new Set([...parents, depPath]);
158
+ for (const childDepPath of Object.values(node.children)) {
159
+ if (transitivelyRequiresBuild(graph, builtDepPaths, cache, childDepPath, nextParents)) {
160
+ cache[depPath] = true;
161
+ return true;
162
+ }
163
+ }
164
+ cache[depPath] = false;
165
+ return false;
166
+ }
167
+ function lockfileDepsToGraphChildren(deps) {
168
+ const children = {};
169
+ for (const [alias, reference] of Object.entries(deps)) {
170
+ const depPath = refToRelative(reference, alias);
171
+ if (depPath) {
172
+ children[alias] = depPath;
173
+ }
174
+ }
175
+ return children;
176
+ }
177
+ function createFullPkgId(pkgIdWithPatchHash, resolution) {
178
+ const res = 'integrity' in resolution ? String(resolution.integrity) : hashObject(resolution);
179
+ return `${pkgIdWithPatchHash}:${res}`;
180
+ }
181
+ //# sourceMappingURL=index.js.map
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@pnpm/deps.graph-hasher",
3
+ "version": "1002.0.8",
4
+ "description": "Calculates the state of a dependency",
5
+ "keywords": [
6
+ "pnpm",
7
+ "pnpm11"
8
+ ],
9
+ "license": "MIT",
10
+ "funding": "https://opencollective.com/pnpm",
11
+ "repository": "https://github.com/pnpm/pnpm/tree/main/deps/graph-hasher",
12
+ "homepage": "https://github.com/pnpm/pnpm/tree/main/deps/graph-hasher#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/pnpm/pnpm/issues"
15
+ },
16
+ "type": "module",
17
+ "main": "lib/index.js",
18
+ "types": "lib/index.d.ts",
19
+ "exports": {
20
+ ".": "./lib/index.js"
21
+ },
22
+ "files": [
23
+ "lib",
24
+ "!*.map"
25
+ ],
26
+ "dependencies": {
27
+ "@pnpm/constants": "1001.3.1",
28
+ "@pnpm/crypto.object-hasher": "1000.1.0",
29
+ "@pnpm/deps.path": "1001.1.3",
30
+ "@pnpm/types": "1000.9.0",
31
+ "@pnpm/lockfile.utils": "1003.0.3",
32
+ "@pnpm/lockfile.types": "1002.0.2"
33
+ },
34
+ "devDependencies": {
35
+ "@pnpm/deps.graph-hasher": "1002.0.8"
36
+ },
37
+ "engines": {
38
+ "node": ">=22.13"
39
+ },
40
+ "jest": {
41
+ "preset": "@pnpm/jest-config"
42
+ },
43
+ "scripts": {
44
+ "lint": "eslint \"src/**/*.ts\" \"test/**/*.ts\"",
45
+ "_test": "cross-env NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules --disable-warning=ExperimentalWarning --disable-warning=DEP0169\" jest",
46
+ "test": "pnpm run compile && pnpm run _test",
47
+ "compile": "tsgo --build && pnpm run lint --fix"
48
+ }
49
+ }