@vltpkg/pick-manifest 1.0.0-rc.22 → 1.0.0-rc.24

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.
@@ -0,0 +1,58 @@
1
+ import { Range, Version } from '@vltpkg/semver';
2
+ import { Spec } from '@vltpkg/spec';
3
+ import type { Manifest, Packument, RevDoc, RevDocEntry } from '@vltpkg/types';
4
+ export type PickManifestOptions = {
5
+ tag?: string;
6
+ before?: Date | number | string;
7
+ 'node-version'?: string;
8
+ os?: string;
9
+ arch?: string;
10
+ libc?: string;
11
+ };
12
+ export type Manifestish = Pick<Manifest, 'engines' | 'os' | 'cpu' | 'libc'> | Pick<RevDocEntry, 'engines' | 'os' | 'cpu' | 'libc'>;
13
+ export type Packumentish = Packument | RevDoc;
14
+ export type PickManifestish<T extends Packumentish> = T extends RevDoc ? RevDocEntry : Manifest;
15
+ export type ManiCheck<T extends Packumentish> = {
16
+ version: Version;
17
+ deprecated: boolean;
18
+ platform: boolean;
19
+ prerelease: boolean;
20
+ mani: PickManifestish<T>;
21
+ };
22
+ /**
23
+ * Path to the ldd binary used for filesystem-based libc detection.
24
+ */
25
+ export declare const LDD_PATH = "/usr/bin/ldd";
26
+ /**
27
+ * Detect libc family by reading the /usr/bin/ldd file.
28
+ * Returns 'glibc', 'musl', null (detection ran but unknown)
29
+ * or undefined (file not readable).
30
+ */
31
+ export declare const getFamilyFromFilesystem: () => string | null | undefined;
32
+ /**
33
+ * Detect libc family by using process.report.getReport().
34
+ * Returns 'glibc', 'musl', or null if detection fails.
35
+ */
36
+ export declare const getFamilyFromReport: () => string | null;
37
+ /** Reset the cached libc detection result. For testing only. */
38
+ export declare const resetLibcCache: () => void;
39
+ /**
40
+ * Detect the libc family (glibc or musl) on Linux systems.
41
+ * First tries filesystem-based detection (/usr/bin/ldd),
42
+ * then falls back to process.report.
43
+ * Returns undefined on non-Linux platforms or if detection fails.
44
+ */
45
+ export declare const detectLibc: () => string | undefined;
46
+ /**
47
+ * Call with a manifest and the node version and process platform/arch/libc
48
+ * to check whether a version is suitable for the current platform.
49
+ */
50
+ export declare const platformCheck: (mani: Manifestish, nodeVersion: Version | string, wantOs?: string, wantArch?: string, wantLibc?: string) => boolean;
51
+ /**
52
+ * Choose the most appropriate manifest from a packument.
53
+ *
54
+ * If `before` is set in the options, then the packument MUST
55
+ * be a full non-minified Packument object. Otherwise, a minified packument
56
+ * is fine.
57
+ */
58
+ export declare function pickManifest<T extends Packumentish>(packument: T, wanted: Range | Spec | string, opts?: PickManifestOptions): PickManifestish<T> | undefined;
package/dist/index.js ADDED
@@ -0,0 +1,306 @@
1
+ import { error } from '@vltpkg/error-cause';
2
+ import { parse, Range, satisfies, Version, isRange, } from '@vltpkg/semver';
3
+ import { Spec } from '@vltpkg/spec';
4
+ import { isSpec } from '@vltpkg/spec/browser';
5
+ import { readFileSync } from 'node:fs';
6
+ const parsedNodeVersion = Version.parse(process.version);
7
+ const isBefore = (version, before, verTimes) => {
8
+ if (!verTimes || !version || !before)
9
+ return true;
10
+ const time = version && verTimes[version];
11
+ return !!time && Date.parse(time) <= before;
12
+ };
13
+ const checkList = (value, list) => {
14
+ if (typeof list === 'string') {
15
+ list = [list];
16
+ }
17
+ // invalid list is equivalent to 'any'
18
+ if (!Array.isArray(list))
19
+ return true;
20
+ if (list.length === 1 && list[0] === 'any') {
21
+ return true;
22
+ }
23
+ // match none of the negated values, and at least one of the
24
+ // non-negated values, if any are present.
25
+ let negated = 0;
26
+ let match = false;
27
+ for (const entry of list) {
28
+ const negate = entry.startsWith('!');
29
+ const test = negate ? entry.slice(1) : entry;
30
+ if (negate) {
31
+ negated++;
32
+ if (value === test) {
33
+ return false;
34
+ }
35
+ }
36
+ else {
37
+ match = match || value === test;
38
+ }
39
+ }
40
+ return match || negated === list.length;
41
+ };
42
+ /* c8 ignore start - Linux-only helper */
43
+ const isMusl = (file) => file.includes('libc.musl-') || file.includes('ld-musl-');
44
+ /* c8 ignore stop */
45
+ /**
46
+ * Path to the ldd binary used for filesystem-based libc detection.
47
+ */
48
+ export const LDD_PATH = '/usr/bin/ldd';
49
+ /**
50
+ * Detect libc family by reading the /usr/bin/ldd file.
51
+ * Returns 'glibc', 'musl', null (detection ran but unknown)
52
+ * or undefined (file not readable).
53
+ */
54
+ export const getFamilyFromFilesystem = () => {
55
+ /* c8 ignore start - Linux-only detection */
56
+ try {
57
+ const content = readFileSync(LDD_PATH, 'utf-8');
58
+ if (content.includes('musl')) {
59
+ return 'musl';
60
+ }
61
+ if (content.includes('GNU C Library')) {
62
+ return 'glibc';
63
+ }
64
+ // File exists but content unrecognized
65
+ return null;
66
+ }
67
+ catch {
68
+ // File not readable, return undefined to signal fallback needed
69
+ return undefined;
70
+ }
71
+ /* c8 ignore stop */
72
+ };
73
+ /**
74
+ * Detect libc family by using process.report.getReport().
75
+ * Returns 'glibc', 'musl', or null if detection fails.
76
+ */
77
+ export const getFamilyFromReport = () => {
78
+ /* c8 ignore start - Linux-only detection */
79
+ try {
80
+ const processReport = process.report;
81
+ const originalExclude = processReport.excludeNetwork === true;
82
+ processReport.excludeNetwork = true;
83
+ const report = processReport.getReport();
84
+ processReport.excludeNetwork = originalExclude;
85
+ if (report?.header?.glibcRuntimeVersion) {
86
+ return 'glibc';
87
+ }
88
+ if (Array.isArray(report?.sharedObjects) &&
89
+ report.sharedObjects.some(isMusl)) {
90
+ return 'musl';
91
+ }
92
+ }
93
+ catch {
94
+ // detection failed
95
+ }
96
+ return null;
97
+ /* c8 ignore stop */
98
+ };
99
+ /**
100
+ * Cached libc family detection result.
101
+ * null means detection ran but couldn't determine, undefined means not cached yet.
102
+ */
103
+ let cachedLibcFamily = undefined;
104
+ /* c8 ignore start - test helper */
105
+ /** Reset the cached libc detection result. For testing only. */
106
+ export const resetLibcCache = () => {
107
+ cachedLibcFamily = undefined;
108
+ };
109
+ /* c8 ignore stop */
110
+ /**
111
+ * Detect the libc family (glibc or musl) on Linux systems.
112
+ * First tries filesystem-based detection (/usr/bin/ldd),
113
+ * then falls back to process.report.
114
+ * Returns undefined on non-Linux platforms or if detection fails.
115
+ */
116
+ export const detectLibc = () => {
117
+ /* c8 ignore start - Linux-only detection */
118
+ // Return cached result if available (null means detection ran but failed)
119
+ if (cachedLibcFamily !== undefined) {
120
+ return cachedLibcFamily ?? undefined;
121
+ }
122
+ // libc checks only work on linux
123
+ if (process.platform !== 'linux') {
124
+ cachedLibcFamily = null;
125
+ return undefined;
126
+ }
127
+ // Try filesystem detection first
128
+ let family = getFamilyFromFilesystem();
129
+ // If filesystem detection didn't work, fall back to process.report
130
+ if (!family) {
131
+ family = getFamilyFromReport();
132
+ }
133
+ cachedLibcFamily = family;
134
+ return family ?? undefined;
135
+ /* c8 ignore stop */
136
+ };
137
+ /**
138
+ * Call with a manifest and the node version and process platform/arch/libc
139
+ * to check whether a version is suitable for the current platform.
140
+ */
141
+ export const platformCheck = (mani, nodeVersion, wantOs, wantArch, wantLibc) => {
142
+ const { engines, os, cpu, libc } = mani;
143
+ if (engines) {
144
+ const { node } = engines;
145
+ if (node && !satisfies(nodeVersion, node, true)) {
146
+ return false;
147
+ }
148
+ }
149
+ if (wantOs && !checkList(wantOs, os))
150
+ return false;
151
+ if (wantArch && !checkList(wantArch, cpu))
152
+ return false;
153
+ // libc checks only apply when the package specifies libc requirements
154
+ if (libc) {
155
+ /* c8 ignore start - system specific */
156
+ // if wantLibc is not provided, detect it
157
+ const libcFamily = wantLibc ?? detectLibc();
158
+ // if libc can't be determined, fail the check (can't install libc-specific packages)
159
+ if (!libcFamily)
160
+ return false;
161
+ if (!checkList(libcFamily, libc))
162
+ return false;
163
+ /* c8 ignore stop */
164
+ }
165
+ return true;
166
+ };
167
+ const versionOk = (packument, version, nodeVersion, os, arch, libc, before) => {
168
+ const mani = packument.versions[version];
169
+ /* c8 ignore next */
170
+ if (!mani)
171
+ return false;
172
+ const { time } = packument;
173
+ return (isBefore(version, before, time) &&
174
+ platformCheck(mani, nodeVersion, os, arch, libc));
175
+ };
176
+ /**
177
+ * Choose the most appropriate manifest from a packument.
178
+ *
179
+ * If `before` is set in the options, then the packument MUST
180
+ * be a full non-minified Packument object. Otherwise, a minified packument
181
+ * is fine.
182
+ */
183
+ export function pickManifest(packument, wanted, opts = {}) {
184
+ const { tag = 'latest', before, 'node-version': nodeVersion, os = process.platform, arch = process.arch, libc, } = opts;
185
+ const nv = !nodeVersion ? parsedNodeVersion : Version.parse(nodeVersion);
186
+ // detect libc if not provided
187
+ const libcFamily = libc ?? detectLibc();
188
+ // cast since 'time' might not be present on minified packuments
189
+ const { name, time: verTimes, versions, 'dist-tags': distTags, } = packument;
190
+ const time = before && verTimes ? +new Date(before) : Infinity;
191
+ let range = undefined;
192
+ let spec = undefined;
193
+ if (typeof wanted === 'object') {
194
+ if (isSpec(wanted)) {
195
+ const f = wanted.final;
196
+ range = f.range;
197
+ spec = f;
198
+ }
199
+ else if (isRange(wanted)) {
200
+ range = wanted;
201
+ }
202
+ }
203
+ else {
204
+ spec = Spec.parse(`${name}@${wanted}`).final;
205
+ range = spec.range;
206
+ }
207
+ if (!range) {
208
+ if (!spec?.distTag) {
209
+ throw error('Only dist-tag or semver range specs are supported', { spec });
210
+ }
211
+ // if there is an explicit dist tag, we must get that version.
212
+ const ver = distTags[spec.distTag];
213
+ if (!ver)
214
+ return undefined;
215
+ // if the version in the dist-tags is before the before date, then
216
+ // we use that. Otherwise, we get the highest precedence version
217
+ // prior to the dist-tag.
218
+ const mani = versions[ver];
219
+ if (mani &&
220
+ versionOk(packument, ver, nv, os, arch, libcFamily, time)) {
221
+ return mani;
222
+ }
223
+ else {
224
+ range = new Range(`<=${ver}`);
225
+ }
226
+ }
227
+ if (range.isAny)
228
+ range = new Range('*', true);
229
+ // if the range is *, then we prefer the 'latest' if available
230
+ // but skip this if it should be avoided, in that case we have
231
+ // to try a little harder.
232
+ const defaultVer = distTags[tag];
233
+ const defTagVersion = defaultVer ? Version.parse(defaultVer) : undefined;
234
+ if (defaultVer &&
235
+ (range.isAny || defTagVersion?.satisfies(range)) &&
236
+ versionOk(packument, defaultVer, nv, os, arch, libcFamily, time)) {
237
+ return versions[defaultVer];
238
+ }
239
+ // ok, actually have to scan the list
240
+ const entries = Object.entries(versions);
241
+ if (!entries.length) {
242
+ return undefined;
243
+ }
244
+ let found = undefined;
245
+ let foundIsDefTag = false;
246
+ for (const [ver, mani] of entries) {
247
+ if (time && verTimes && !isBefore(ver, time, verTimes)) {
248
+ continue;
249
+ }
250
+ const version = parse(ver);
251
+ if (!version?.satisfies(range)) {
252
+ continue;
253
+ }
254
+ const mc = {
255
+ version,
256
+ deprecated: !!mani.deprecated,
257
+ platform: platformCheck(mani, nv, os, arch, libcFamily),
258
+ prerelease: !!version.prerelease?.length,
259
+ mani,
260
+ };
261
+ if (!found) {
262
+ found = mc;
263
+ if (defTagVersion?.equals(found.version)) {
264
+ foundIsDefTag = true;
265
+ }
266
+ continue;
267
+ }
268
+ const mok = !mc.deprecated && mc.platform;
269
+ const fok = !found.deprecated && found.platform;
270
+ if (mok !== fok) {
271
+ if (mok) {
272
+ found = mc;
273
+ foundIsDefTag = !!defTagVersion?.equals(mc.version);
274
+ }
275
+ }
276
+ else if (mc.platform !== found.platform) {
277
+ if (mc.platform) {
278
+ found = mc;
279
+ foundIsDefTag = !!defTagVersion?.equals(mc.version);
280
+ }
281
+ }
282
+ else if (mc.deprecated !== found.deprecated) {
283
+ if (!mc.deprecated) {
284
+ found = mc;
285
+ /* c8 ignore next */
286
+ foundIsDefTag = !!defTagVersion?.equals(mc.version);
287
+ }
288
+ }
289
+ else if (found.prerelease !== mc.prerelease) {
290
+ if (!mc.prerelease) {
291
+ found = mc;
292
+ /* c8 ignore next */
293
+ foundIsDefTag = !!defTagVersion?.equals(mc.version);
294
+ }
295
+ }
296
+ else if (defTagVersion?.equals(mc.version)) {
297
+ found = mc;
298
+ foundIsDefTag = true;
299
+ }
300
+ else if (mc.version.greaterThan(found.version) &&
301
+ !foundIsDefTag) {
302
+ found = mc;
303
+ }
304
+ }
305
+ return found?.mani;
306
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vltpkg/pick-manifest",
3
3
  "description": "Choose a manifest from a packument",
4
- "version": "1.0.0-rc.22",
4
+ "version": "1.0.0-rc.24",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/vltpkg/vltpkg.git",
@@ -12,10 +12,10 @@
12
12
  "email": "support@vlt.sh"
13
13
  },
14
14
  "dependencies": {
15
- "@vltpkg/error-cause": "1.0.0-rc.22",
16
- "@vltpkg/semver": "1.0.0-rc.22",
17
- "@vltpkg/spec": "1.0.0-rc.22",
18
- "@vltpkg/types": "1.0.0-rc.22"
15
+ "@vltpkg/error-cause": "1.0.0-rc.24",
16
+ "@vltpkg/semver": "1.0.0-rc.24",
17
+ "@vltpkg/spec": "1.0.0-rc.24",
18
+ "@vltpkg/types": "1.0.0-rc.24"
19
19
  },
20
20
  "devDependencies": {
21
21
  "@eslint/js": "^9.39.1",
@@ -49,13 +49,13 @@
49
49
  "extends": "../../tap-config.yaml"
50
50
  },
51
51
  "prettier": "../../.prettierrc.js",
52
- "module": "./src/index.ts",
52
+ "module": "./dist/index.js",
53
53
  "type": "module",
54
54
  "exports": {
55
55
  "./package.json": "./package.json",
56
56
  ".": {
57
57
  "import": {
58
- "default": "./src/index.ts"
58
+ "default": "./dist/index.js"
59
59
  }
60
60
  }
61
61
  },