@vltpkg/types 1.0.0-rc.23 → 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,558 @@
1
+ import type { DepID } from '@vltpkg/dep-id';
2
+ import type { Spec, SpecLikeBase, SpecOptions } from '@vltpkg/spec';
3
+ /**
4
+ * Utility type that overrides specific properties of type T with new types
5
+ * from R. Constrains override values to exclude undefined, ensuring that
6
+ * normalization cannot introduce undefined to fields that shouldn't have it.
7
+ */
8
+ export type Override<T, R extends {
9
+ [K in keyof R]: R[K] extends undefined ? never : R[K];
10
+ }> = {
11
+ [K in keyof T]: K extends keyof R ? R[K] : T[K];
12
+ };
13
+ /** anything that can be encoded in JSON */
14
+ export type JSONField = JSONField[] | boolean | number | string | {
15
+ [k: string]: JSONField;
16
+ } | null | undefined;
17
+ /** sha512 SRI string */
18
+ export type Integrity = `sha512-${string}`;
19
+ /** SHA256 key identifier */
20
+ export type KeyID = `SHA256:${string}`;
21
+ /** The Manifest['dist'] field present in registry manifests */
22
+ export type Dist = {
23
+ integrity?: Integrity;
24
+ shasum?: string;
25
+ tarball?: string;
26
+ fileCount?: number;
27
+ unpackedSize?: number;
28
+ signatures?: {
29
+ keyid: KeyID;
30
+ sig: string;
31
+ }[];
32
+ };
33
+ /** An object used to mark some peerDeps as optional */
34
+ export type PeerDependenciesMetaValue = {
35
+ optional?: boolean;
36
+ };
37
+ export type ConditionalValueObject = {
38
+ [k: string]: ConditionalValue;
39
+ };
40
+ export type ConditionalValue = ConditionalValue[] | ConditionalValueObject | string | null;
41
+ export type ExportsSubpaths = {
42
+ [path in '.' | `./${string}`]?: ConditionalValue;
43
+ };
44
+ export type Exports = Exclude<ConditionalValue, null> | ExportsSubpaths;
45
+ export type Imports = Record<`#${string}`, ConditionalValue>;
46
+ export type FundingEntry = string | {
47
+ url: string;
48
+ type?: string;
49
+ [key: string]: JSONField;
50
+ };
51
+ export type Funding = FundingEntry | FundingEntry[];
52
+ /**
53
+ * An object with url and optional additional properties
54
+ */
55
+ export type NormalizedFundingEntry = {
56
+ url: string;
57
+ type?: string;
58
+ [key: string]: JSONField;
59
+ };
60
+ /**
61
+ * Normalized funding information, an array of {@link NormalizedFundingEntry}.
62
+ */
63
+ export type NormalizedFunding = NormalizedFundingEntry[];
64
+ /**
65
+ * Normalize funding information to a consistent format.
66
+ */
67
+ export declare const normalizeFunding: (funding: unknown) => NormalizedFunding | undefined;
68
+ /**
69
+ * Type guard to check if a value is a {@link NormalizedFundingEntry}.
70
+ */
71
+ export declare const isNormalizedFundingEntry: (o: unknown) => o is NormalizedFundingEntry;
72
+ /**
73
+ * Type guard to check if a value is a {@link NormalizedFunding}.
74
+ */
75
+ export declare const isNormalizedFunding: (o: unknown) => o is NormalizedFunding;
76
+ /**
77
+ * Given a version Normalize the version field in a manifest.
78
+ */
79
+ export declare const fixManifestVersion: <T extends Manifest | ManifestRegistry>(manifest: T) => T;
80
+ declare const kWriteAccess: unique symbol;
81
+ declare const kIsPublisher: unique symbol;
82
+ /**
83
+ * Parse a string or object into a normalized contributor.
84
+ */
85
+ export declare const parsePerson: (person: unknown, writeAccess?: boolean, isPublisher?: boolean) => NormalizedContributorEntry | undefined;
86
+ /**
87
+ * Normalized contributors - always an array of {@link NormalizedContributorEntry}.
88
+ */
89
+ export type NormalizedContributors = NormalizedContributorEntry[];
90
+ /**
91
+ * Represents a normalized contributor object. This is the type that is
92
+ * used in the {@link NormalizedManifest} and {@link NormalizedManifestRegistry}
93
+ * objects.
94
+ */
95
+ export type NormalizedContributorEntry = {
96
+ email?: string;
97
+ name?: string;
98
+ [kWriteAccess]?: boolean;
99
+ [kIsPublisher]?: boolean;
100
+ writeAccess?: boolean;
101
+ isPublisher?: boolean;
102
+ };
103
+ /**
104
+ * Type guard to check if a value is a normalized contributor entry.
105
+ */
106
+ export declare const isNormalizedContributorEntry: (o: unknown) => o is NormalizedContributorEntry;
107
+ /**
108
+ * Type guard to check if a value is a {@link NormalizedContributors}.
109
+ */
110
+ export declare const isNormalizedContributors: (o: unknown) => o is NormalizedContributors;
111
+ /**
112
+ * Normalize contributors and maintainers from various formats
113
+ */
114
+ export declare const normalizeContributors: (contributors: unknown, maintainers?: unknown) => NormalizedContributorEntry[] | undefined;
115
+ export type Person = string | {
116
+ name: string;
117
+ url?: string;
118
+ email?: string;
119
+ };
120
+ export type Repository = string | {
121
+ type: string;
122
+ url: string;
123
+ };
124
+ export type Bugs = string | {
125
+ url?: string;
126
+ email?: string;
127
+ };
128
+ export type Keywords = string[] | string;
129
+ /**
130
+ * Normalized bugs entry - always an object with type and url/email
131
+ */
132
+ export type NormalizedBugsEntry = {
133
+ type?: 'email' | 'link';
134
+ url?: string;
135
+ email?: string;
136
+ };
137
+ /**
138
+ * Normalized keywords - always an array of strings
139
+ */
140
+ export type NormalizedKeywords = string[];
141
+ /**
142
+ * Normalized engines - always a record of string to string
143
+ */
144
+ export type NormalizedEngines = Record<string, string>;
145
+ /**
146
+ * Normalized OS list - always an array of strings
147
+ */
148
+ export type NormalizedOs = string[];
149
+ /**
150
+ * Normalized CPU list - always an array of strings
151
+ */
152
+ export type NormalizedCpu = string[];
153
+ /**
154
+ * Normalized libc list - always an array of strings
155
+ */
156
+ export type NormalizedLibc = string[];
157
+ /**
158
+ * Normalized bugs - always an array of {@link NormalizedBugsEntry}
159
+ */
160
+ export type NormalizedBugs = NormalizedBugsEntry[];
161
+ /**
162
+ * Normalized bin - always a record of string to string
163
+ */
164
+ export type NormalizedBin = Record<string, string>;
165
+ /**
166
+ * Normalize bugs information to a {@link NormalizedBugs} consistent format.
167
+ */
168
+ export declare const normalizeBugs: (bugs: unknown) => NormalizedBugs | undefined;
169
+ /**
170
+ * Type guard to check if a value is a {@link NormalizedBugsEntry}.
171
+ */
172
+ export declare const isNormalizedBugsEntry: (o: unknown) => o is NormalizedBugsEntry;
173
+ /**
174
+ * Type guard to check if a value is a {@link NormalizedBugs}.
175
+ */
176
+ export declare const isNormalizedBugs: (o: unknown) => o is NormalizedBugs;
177
+ /**
178
+ * Normalize keywords information to a {@link NormalizedKeywords} consistent format.
179
+ */
180
+ export declare const normalizeKeywords: (keywords: unknown) => NormalizedKeywords | undefined;
181
+ /**
182
+ * Type guard to check if a value is a {@link NormalizedKeywords}.
183
+ */
184
+ export declare const isNormalizedKeywords: (o: unknown) => o is NormalizedKeywords;
185
+ /**
186
+ * Normalize engines information to a {@link NormalizedEngines} consistent format.
187
+ */
188
+ export declare const normalizeEngines: (engines: unknown) => NormalizedEngines | undefined;
189
+ /**
190
+ * Normalize OS information to a {@link NormalizedOs} consistent format.
191
+ */
192
+ export declare const normalizeOs: (os: unknown) => NormalizedOs | undefined;
193
+ /**
194
+ * Normalize CPU information to a {@link NormalizedCpu} consistent format.
195
+ */
196
+ export declare const normalizeCpu: (cpu: unknown) => NormalizedCpu | undefined;
197
+ /**
198
+ * Normalize libc information to a {@link NormalizedLibc} consistent format.
199
+ */
200
+ export declare const normalizeLibc: (libc: unknown) => NormalizedLibc | undefined;
201
+ /**
202
+ * Type guard to check if a value is a {@link NormalizedEngines}.
203
+ */
204
+ export declare const isNormalizedEngines: (o: unknown) => o is NormalizedEngines;
205
+ /**
206
+ * Type guard to check if a value is a {@link NormalizedOs}.
207
+ */
208
+ export declare const isNormalizedOs: (o: unknown) => o is NormalizedOs;
209
+ /**
210
+ * Type guard to check if a value is a {@link NormalizedCpu}.
211
+ */
212
+ export declare const isNormalizedCpu: (o: unknown) => o is NormalizedCpu;
213
+ /**
214
+ * Type guard to check if a value is a {@link NormalizedLibc}.
215
+ */
216
+ export declare const isNormalizedLibc: (o: unknown) => o is NormalizedLibc;
217
+ /**
218
+ * Normalizes the bin paths.
219
+ */
220
+ export declare const normalizeBinPaths: (manifest: Pick<Manifest, "bin" | "name">) => Record<string, string> | undefined;
221
+ export type Manifest = {
222
+ /** The name of the package. optional because {} is a valid package.json */
223
+ name?: string;
224
+ /** The version of the package. optional because {} is a valid package.json */
225
+ version?: string;
226
+ /** production dependencies, name:specifier */
227
+ dependencies?: Record<string, string>;
228
+ /** development dependencies, name:specifier */
229
+ devDependencies?: Record<string, string>;
230
+ /** optional dependencies, name:specifier */
231
+ optionalDependencies?: Record<string, string>;
232
+ /** peer dependencies, name:specifier */
233
+ peerDependencies?: Record<string, string>;
234
+ /** peer dependencies marked as optional */
235
+ peerDependenciesMeta?: Record<string, PeerDependenciesMetaValue>;
236
+ /** dependency ranges that are acceptable, but not forced */
237
+ acceptDependencies?: Record<string, string>;
238
+ /** names of dependencies included in the package tarball */
239
+ bundleDependencies?: string[];
240
+ /** a message indicating that this is not to be used */
241
+ deprecated?: string;
242
+ /** executable built and linked by this package */
243
+ bin?: Record<string, string> | string;
244
+ /** run-script actions for this package */
245
+ scripts?: Record<string, string>;
246
+ /** supported run-time platforms this package can run on */
247
+ engines?: Record<string, string>;
248
+ /** supported operating systems this package can run on */
249
+ os?: string[] | string;
250
+ /** supported CPU architectures this package can run on */
251
+ cpu?: string[] | string;
252
+ /** supported libc implementations this package can run on (e.g. glibc, musl) */
253
+ libc?: string[] | string;
254
+ /** URLs that can be visited to fund this project */
255
+ funding?: Funding;
256
+ /** The homepage of the repository */
257
+ homepage?: string;
258
+ /**
259
+ * Only present in Manifests served by a registry. Contains information
260
+ * about the artifact served for this package release.
261
+ */
262
+ dist?: Dist;
263
+ /** a short description of the package */
264
+ description?: string;
265
+ /** search keywords */
266
+ keywords?: Keywords;
267
+ /** where to go to file issues */
268
+ bugs?: Bugs;
269
+ /** where the development happens */
270
+ repository?: Repository;
271
+ /** the main module, if exports['.'] is not set */
272
+ main?: string;
273
+ /** named subpath exports */
274
+ exports?: Exports;
275
+ /** named #identifier imports */
276
+ imports?: Imports;
277
+ /**
278
+ * the HEAD of the git repo this was published from
279
+ * only present in published packages
280
+ */
281
+ gitHead?: string;
282
+ /** whether the package is private */
283
+ private?: boolean;
284
+ /** whether this is ESM or CommonJS by default */
285
+ type?: 'commonjs' | 'module';
286
+ /** npm puts this on published manifests */
287
+ gypfile?: boolean;
288
+ /** the author of a package */
289
+ author?: Person;
290
+ /** contributors to the package */
291
+ contributors?: Person[];
292
+ /** the license of the package */
293
+ license?: string;
294
+ };
295
+ export type NormalizedFields = {
296
+ bugs: NormalizedBugs | undefined;
297
+ author: NormalizedContributorEntry | undefined;
298
+ contributors: NormalizedContributors | undefined;
299
+ funding: NormalizedFunding | undefined;
300
+ keywords: NormalizedKeywords | undefined;
301
+ engines: NormalizedEngines | undefined;
302
+ os: NormalizedOs | undefined;
303
+ cpu: NormalizedCpu | undefined;
304
+ libc: NormalizedLibc | undefined;
305
+ bin: NormalizedBin | undefined;
306
+ };
307
+ /**
308
+ * A {@link Manifest} object that contains normalized fields.
309
+ */
310
+ export type NormalizedManifest = Override<Manifest, NormalizedFields>;
311
+ /**
312
+ * A {@link ManifestRegistry} object that contains normalized fields.
313
+ */
314
+ export type NormalizedManifestRegistry = Override<ManifestRegistry, NormalizedFields>;
315
+ /**
316
+ * A specific type of {@link Manifest} that represents manifests that were
317
+ * retrieved from a registry, these will always have `name`, `version`
318
+ * and `dist` information along with an optional `maintainers` field.
319
+ */
320
+ export type ManifestRegistry = Manifest & Required<Pick<Manifest, 'name' | 'version' | 'dist'>> & {
321
+ maintainers?: unknown;
322
+ };
323
+ /**
324
+ * Maps the manifest type to the equivalent normalized manifest type.
325
+ */
326
+ export type SomeNormalizedManifest<T> = T extends ManifestRegistry ? NormalizedManifestRegistry : NormalizedManifest;
327
+ /**
328
+ * A document that represents available package versions in a given registry
329
+ * along with extra information, such as `dist-tags` and `maintainers` info.
330
+ * The `versions` field is key-value structure in which keys are the
331
+ * available versions of a given package and values are
332
+ * {@link ManifestRegistry} objects.
333
+ */
334
+ export type Packument = {
335
+ name: string;
336
+ 'dist-tags': Record<string, string>;
337
+ versions: Record<string, Manifest>;
338
+ modified?: string;
339
+ time?: Record<string, string>;
340
+ readme?: string;
341
+ contributors?: Person[];
342
+ maintainers?: Person[];
343
+ };
344
+ export type RefType = 'branch' | 'head' | 'other' | 'pull' | 'tag';
345
+ /**
346
+ * A representation of a given remote ref in a {@link RevDoc} object.
347
+ */
348
+ export type RevDocEntry = Omit<Manifest, 'type'> & Required<Pick<Manifest, 'version'>> & {
349
+ /** sha this references */
350
+ sha: string;
351
+ /** ref as passed git locally */
352
+ ref: string;
353
+ /** canonical full ref, like `refs/tags/blahblah` */
354
+ rawRef: string;
355
+ /** what type of ref this is: 'branch', 'tag', etc. */
356
+ type: RefType;
357
+ };
358
+ /**
359
+ * An object kind of resembling a packument, but about a git repo.
360
+ */
361
+ export type RevDoc = Omit<Packument, 'versions'> & {
362
+ /** all semver-looking tags go in this record */
363
+ versions: Record<string, RevDocEntry>;
364
+ /** all named things that can be cloned down remotely */
365
+ refs: Record<string, RevDocEntry>;
366
+ /** all named shas referenced above */
367
+ shas: Record<string, string[]>;
368
+ };
369
+ /**
370
+ * A type guard to check if a value is a boolean.
371
+ */
372
+ export declare const isBoolean: (value: unknown) => value is boolean;
373
+ export declare const integrityRE: RegExp;
374
+ export declare const isIntegrity: (i: unknown) => i is Integrity;
375
+ export declare const asIntegrity: (i: unknown) => Integrity;
376
+ export declare const assertIntegrity: (i: unknown) => asserts i is Integrity;
377
+ export declare const keyIDRE: RegExp;
378
+ export declare const isKeyID: (k: unknown) => k is KeyID;
379
+ export declare const asKeyID: (k: unknown) => KeyID;
380
+ export declare const assertKeyID: (k: unknown) => asserts k is KeyID;
381
+ /**
382
+ * Convert an unknown value to an error.
383
+ */
384
+ export declare const asError: (er: unknown, fallbackMessage?: string) => Error;
385
+ /**
386
+ * Check if a value is an error.
387
+ */
388
+ export declare const isError: (er: unknown) => er is Error;
389
+ /**
390
+ * Check if an error has a cause property.
391
+ */
392
+ export declare const isErrorWithCause: (er: unknown) => er is Error & {
393
+ cause: unknown;
394
+ };
395
+ /**
396
+ * Check if an unknown value is a plain object.
397
+ */
398
+ export declare const isObject: (v: unknown) => v is Record<string, unknown>;
399
+ export declare const maybeRecordStringString: (o: unknown) => o is Record<string, string> | undefined;
400
+ export declare const isRecordStringString: (o: unknown) => o is Record<string, string>;
401
+ export declare const assertRecordStringString: (o: unknown) => void;
402
+ export declare const isRecordStringT: <T>(o: unknown, check: (o: unknown) => o is T) => o is Record<string, T>;
403
+ export declare const assertRecordStringT: <T>(o: unknown, check: (o: unknown) => o is T, wanted: string) => asserts o is Record<string, T>;
404
+ export declare const isRecordStringManifest: (o: unknown) => o is Record<string, Manifest>;
405
+ export declare const maybePeerDependenciesMetaSet: (o: unknown) => o is Record<string, PeerDependenciesMetaValue> | undefined;
406
+ export declare const maybeBoolean: (o: unknown) => o is boolean;
407
+ export declare const isPeerDependenciesMetaValue: (o: unknown) => o is PeerDependenciesMetaValue;
408
+ export declare const maybeString: (a: unknown) => a is string | undefined;
409
+ export declare const maybeDist: (a: unknown) => a is Manifest["dist"];
410
+ /**
411
+ * Is a given unknown value a valid {@link Manifest} object?
412
+ * Returns `true` if so.
413
+ */
414
+ export declare const isManifest: (m: unknown) => m is Manifest;
415
+ /**
416
+ * A specific {@link Manifest} that is retrieved uniquely from reading
417
+ * registry packument and manifest endpoints, it has `dist`, `name` and
418
+ * `version` fields defined.
419
+ */
420
+ export declare const isManifestRegistry: (m: unknown) => m is ManifestRegistry;
421
+ /**
422
+ * Given an unknown value, convert it to a {@link Manifest}.
423
+ */
424
+ export declare const asManifest: (m: unknown, from?: (...a: unknown[]) => any) => Manifest;
425
+ /**
426
+ * Given a {@link Manifest} returns a {@link NormalizedManifest} that
427
+ * contains normalized author, bugs, funding, contributors, keywords and
428
+ * version fields.
429
+ */
430
+ export declare const normalizeManifest: <T extends Manifest | ManifestRegistry>(manifest: T) => SomeNormalizedManifest<T>;
431
+ /**
432
+ * Type guard to check if a value is a {@link NormalizedManifest}.
433
+ */
434
+ export declare const isNormalizedManifest: (o: unknown) => o is NormalizedManifest;
435
+ /**
436
+ * Given an unknown value, convert it to a {@link NormalizedManifest}.
437
+ */
438
+ export declare const asNormalizedManifest: (m: unknown, from?: (...a: unknown[]) => any) => NormalizedManifest;
439
+ /**
440
+ * Given an unknown value, convert it to a {@link ManifestRegistry}.
441
+ */
442
+ export declare const asManifestRegistry: (m: unknown, from?: (...a: unknown[]) => any) => ManifestRegistry;
443
+ /**
444
+ * Type guard to check if a value is a {@link NormalizedManifestRegistry}.
445
+ */
446
+ export declare const isNormalizedManifestRegistry: (o: unknown) => o is NormalizedManifestRegistry;
447
+ /**
448
+ * Given an unknown value, convert it to a {@link NormalizedManifestRegistry}.
449
+ */
450
+ export declare const asNormalizedManifestRegistry: (m: unknown, from?: (...a: unknown[]) => any) => NormalizedManifestRegistry;
451
+ /**
452
+ * Walks a normalized manifest and expands any symbols found
453
+ * in the `author` and `contributors` fields.
454
+ */
455
+ export declare const expandNormalizedManifestSymbols: (m: NormalizedManifest) => NormalizedManifest;
456
+ export declare const assertManifest: (m: unknown) => asserts m is Manifest;
457
+ export declare const assertManifestRegistry: (m: unknown) => asserts m is ManifestRegistry;
458
+ export declare const isPackument: (p: unknown) => p is Packument;
459
+ export declare const asPackument: (p: unknown, from?: (...a: unknown[]) => any) => Packument;
460
+ export declare const assertPackument: (m: unknown) => asserts m is Packument;
461
+ /**
462
+ * Name of the package.json keys used to define different types of dependencies.
463
+ */
464
+ export type DependencyTypeLong = 'dependencies' | 'devDependencies' | 'optionalDependencies' | 'peerDependencies';
465
+ /**
466
+ * Unique keys that define different types of dependencies relationship.
467
+ */
468
+ export type DependencyTypeShort = 'dev' | 'optional' | 'peer' | 'peerOptional' | 'prod';
469
+ /**
470
+ * Unique keys that indicate how a new or updated dependency should be saved
471
+ * back to a manifest.
472
+ *
473
+ * `'implicit'` is used to indicate that a dependency should be saved as
474
+ * whatever type it already exists as. If the dependency does not exist,
475
+ * then `'implicit'` is equivalent to `'prod'`, as that is the default
476
+ * save type.
477
+ */
478
+ export type DependencySaveType = DependencyTypeShort | 'implicit';
479
+ /**
480
+ * A set of the possible long dependency type names,
481
+ * as used in `package.json` files.
482
+ */
483
+ export declare const longDependencyTypes: Set<DependencyTypeLong>;
484
+ /**
485
+ * A set of the short type keys used to represent dependency relationships.
486
+ */
487
+ export declare const shortDependencyTypes: Set<DependencyTypeShort>;
488
+ /**
489
+ * Maps between long form names usually used in `package.json` files
490
+ * to a corresponding short form name, used in lockfiles.
491
+ */
492
+ export declare const dependencyTypes: Map<DependencyTypeLong, DependencyTypeShort>;
493
+ export type EdgeLike = {
494
+ name: string;
495
+ from: NodeLike;
496
+ spec: SpecLikeBase;
497
+ to?: NodeLike;
498
+ type: DependencyTypeShort;
499
+ optional?: boolean;
500
+ peer?: boolean;
501
+ };
502
+ export type GraphLike = {
503
+ importers: Set<NodeLike>;
504
+ mainImporter: NodeLike;
505
+ projectRoot: string;
506
+ nodes: Map<DepID, NodeLike>;
507
+ nodesByName: Map<string, Set<NodeLike>>;
508
+ edges: Set<EdgeLike>;
509
+ addEdge: (type: DependencyTypeShort, spec: Spec, from: NodeLike, to?: NodeLike) => EdgeLike;
510
+ addNode: (id?: DepID, manifest?: NormalizedManifest, spec?: Spec, name?: string, version?: string) => NodeLike;
511
+ removeNode(node: NodeLike, replacement?: NodeLike, keepEdges?: boolean): void;
512
+ };
513
+ export type NodeLike = {
514
+ id: DepID;
515
+ confused: boolean;
516
+ edgesIn: Set<EdgeLike>;
517
+ edgesOut: Map<string, EdgeLike>;
518
+ workspaces: Map<string, EdgeLike> | undefined;
519
+ location?: string;
520
+ manifest?: NormalizedManifest | null;
521
+ rawManifest?: NormalizedManifest | null;
522
+ name?: string | null;
523
+ version?: string | null;
524
+ integrity?: string | null;
525
+ resolved?: string | null;
526
+ importer: boolean;
527
+ graph: GraphLike;
528
+ mainImporter: boolean;
529
+ projectRoot: string;
530
+ dev: boolean;
531
+ optional: boolean;
532
+ modifier?: string | undefined;
533
+ peerSetHash?: string | undefined;
534
+ registry?: string;
535
+ platform?: {
536
+ engines?: Record<string, string>;
537
+ os?: string[] | string;
538
+ cpu?: string[] | string;
539
+ libc?: string[] | string;
540
+ };
541
+ bins?: Record<string, string>;
542
+ buildState?: 'none' | 'needed' | 'built' | 'failed';
543
+ buildAllowed?: boolean;
544
+ buildBlocked?: boolean;
545
+ options: SpecOptions;
546
+ toJSON: () => Pick<NodeLike, 'id' | 'name' | 'version' | 'location' | 'importer' | 'manifest' | 'projectRoot' | 'integrity' | 'resolved' | 'dev' | 'optional' | 'confused' | 'platform' | 'buildState' | 'buildAllowed' | 'buildBlocked'> & {
547
+ rawManifest?: NodeLike['manifest'];
548
+ };
549
+ toString(): string;
550
+ setResolved(): void;
551
+ setConfusedManifest(fixed: NormalizedManifest, confused?: NormalizedManifest): void;
552
+ maybeSetConfusedManifest(spec: Spec, confused?: NormalizedManifest): void;
553
+ };
554
+ /**
555
+ * Parse a scoped package name into its scope and name components.
556
+ */
557
+ export declare const parseScope: (scoped: string) => [string | undefined, string];
558
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,815 @@
1
+ import { error } from '@vltpkg/error-cause';
2
+ import { Version } from '@vltpkg/semver';
3
+ /**
4
+ * Normalize a single funding entry to a consistent format.
5
+ */
6
+ const normalizeFundingEntry = (item) => {
7
+ const getTypeFromUrl = (url) => {
8
+ try {
9
+ const { hostname } = new URL(url);
10
+ const domain = hostname.startsWith('www.') ? hostname.slice(4) : hostname;
11
+ if (domain === 'github.com')
12
+ return 'github';
13
+ if (domain === 'patreon.com')
14
+ return 'patreon';
15
+ if (domain === 'opencollective.com')
16
+ return 'opencollective';
17
+ return 'individual';
18
+ }
19
+ catch {
20
+ return 'invalid';
21
+ }
22
+ };
23
+ const validateType = (url, type) => {
24
+ const urlType = getTypeFromUrl(url);
25
+ if (!type ||
26
+ ['github', 'patreon', 'opencollective'].includes(urlType))
27
+ return urlType;
28
+ if (urlType === 'invalid')
29
+ return undefined;
30
+ return type;
31
+ };
32
+ if (typeof item === 'string') {
33
+ return { url: item, type: getTypeFromUrl(item) };
34
+ }
35
+ if (isObject(item) &&
36
+ 'url' in item &&
37
+ typeof item.url === 'string') {
38
+ // If the item is already normalized, return it directly
39
+ if (isNormalizedFundingEntry(item)) {
40
+ return item;
41
+ }
42
+ const obj = item;
43
+ const url = obj.url;
44
+ const validatedType = validateType(url, obj.type);
45
+ const result = { ...obj, url };
46
+ if (validatedType) {
47
+ result.type = validatedType;
48
+ }
49
+ else {
50
+ delete result.type;
51
+ }
52
+ return result;
53
+ }
54
+ return { url: '', type: 'individual' };
55
+ };
56
+ /**
57
+ * Normalize funding information to a consistent format.
58
+ */
59
+ export const normalizeFunding = (funding) => {
60
+ if (!funding)
61
+ return;
62
+ const fundingArray = Array.isArray(funding) ? funding : [funding];
63
+ const sources = fundingArray.map(normalizeFundingEntry);
64
+ return sources.length > 0 ? sources : undefined;
65
+ };
66
+ /**
67
+ * Type guard to check if a value is a {@link NormalizedFundingEntry}.
68
+ */
69
+ export const isNormalizedFundingEntry = (o) => {
70
+ return (isObject(o) &&
71
+ 'url' in o &&
72
+ typeof o.url === 'string' &&
73
+ !!o.url &&
74
+ 'type' in o &&
75
+ typeof o.type === 'string' &&
76
+ ['github', 'patreon', 'opencollective', 'individual'].includes(o.type));
77
+ };
78
+ /**
79
+ * Type guard to check if a value is a {@link NormalizedFunding}.
80
+ */
81
+ export const isNormalizedFunding = (o) => {
82
+ return (Array.isArray(o) &&
83
+ o.length > 0 &&
84
+ o.every(isNormalizedFundingEntry));
85
+ };
86
+ /**
87
+ * Given a version Normalize the version field in a manifest.
88
+ */
89
+ export const fixManifestVersion = (manifest) => {
90
+ if (!Object.hasOwn(manifest, 'version')) {
91
+ return manifest;
92
+ }
93
+ if (!manifest.version) {
94
+ throw error('version is empty', {
95
+ manifest,
96
+ });
97
+ }
98
+ const version = Version.parse(manifest.version);
99
+ manifest.version = version.toString();
100
+ return manifest;
101
+ };
102
+ const kWriteAccess = Symbol.for('writeAccess');
103
+ const kIsPublisher = Symbol.for('isPublisher');
104
+ /**
105
+ * Parse a string or object into a normalized contributor.
106
+ */
107
+ export const parsePerson = (person, writeAccess, isPublisher) => {
108
+ if (!person)
109
+ return;
110
+ if (isObject(person)) {
111
+ // this is an already parsed object person, just return its value
112
+ if (isNormalizedContributorEntry(person)) {
113
+ return person;
114
+ }
115
+ const name = typeof person.name === 'string' ? person.name : undefined;
116
+ const email = typeof person.email === 'string' ? person.email
117
+ : typeof person.mail === 'string' ? person.mail
118
+ : undefined;
119
+ if (!name && !email)
120
+ return undefined;
121
+ return {
122
+ name,
123
+ email,
124
+ [kWriteAccess]: writeAccess ?? false,
125
+ [kIsPublisher]: isPublisher ?? false,
126
+ };
127
+ }
128
+ else if (typeof person === 'string') {
129
+ const NAME_PATTERN = /^([^(<]+)/;
130
+ const EMAIL_PATTERN = /<([^<>]+)>/;
131
+ const name = NAME_PATTERN.exec(person)?.[0].trim() || '';
132
+ const email = EMAIL_PATTERN.exec(person)?.[1] || '';
133
+ if (!name && !email)
134
+ return undefined;
135
+ return {
136
+ name: name || undefined,
137
+ email: email || undefined,
138
+ [kWriteAccess]: writeAccess ?? false,
139
+ [kIsPublisher]: isPublisher ?? false,
140
+ };
141
+ }
142
+ return;
143
+ };
144
+ /**
145
+ * Type guard to check if a value is a normalized contributor entry.
146
+ */
147
+ export const isNormalizedContributorEntry = (o) => {
148
+ return (isObject(o) &&
149
+ typeof o.name === 'string' &&
150
+ !!o.name &&
151
+ typeof o.email === 'string' &&
152
+ !!o.email &&
153
+ (isBoolean(o[kWriteAccess]) ||
154
+ isBoolean(o.writeAccess)) &&
155
+ (isBoolean(o[kIsPublisher]) ||
156
+ isBoolean(o.isPublisher)));
157
+ };
158
+ /**
159
+ * Type guard to check if a value is a {@link NormalizedContributors}.
160
+ */
161
+ export const isNormalizedContributors = (o) => {
162
+ return (Array.isArray(o) &&
163
+ o.length > 0 &&
164
+ o.every(isNormalizedContributorEntry));
165
+ };
166
+ /**
167
+ * Normalize contributors and maintainers from various formats
168
+ */
169
+ export const normalizeContributors = (contributors, maintainers) => {
170
+ if (!contributors && !maintainers)
171
+ return;
172
+ const result = [];
173
+ // Parse regular contributors (if any)
174
+ if (contributors) {
175
+ const contributorsArray = Array.isArray(contributors) ? contributors : [contributors];
176
+ const normalizedArray = contributorsArray.every(isNormalizedContributorEntry);
177
+ const noMaintainers = !maintainers ||
178
+ (Array.isArray(maintainers) && maintainers.length === 0);
179
+ // If all contributors are already normalized, and there are
180
+ // no maintainers, return the contributors directly
181
+ if (normalizedArray) {
182
+ if (noMaintainers) {
183
+ return contributorsArray.length > 0 ?
184
+ contributorsArray
185
+ : undefined;
186
+ }
187
+ else {
188
+ result.push(...contributorsArray);
189
+ }
190
+ }
191
+ // Parse each contributor and filter out undefined values
192
+ const parsedContributors = contributorsArray
193
+ .map(person => parsePerson(person))
194
+ .filter((c) => c !== undefined);
195
+ result.push(...parsedContributors);
196
+ }
197
+ // Parse maintainers with special flags
198
+ if (maintainers) {
199
+ const maintainersArray = Array.isArray(maintainers) ? maintainers : [maintainers];
200
+ const parsedMaintainers = maintainersArray
201
+ .map(person => parsePerson(person, true, true))
202
+ .filter((c) => c !== undefined);
203
+ result.push(...parsedMaintainers);
204
+ }
205
+ return result.length > 0 ? result : undefined;
206
+ };
207
+ /**
208
+ * Helper function to normalize a single {@link Bugs} entry.
209
+ */
210
+ const normalizeSingleBug = (bug) => {
211
+ const res = [];
212
+ if (typeof bug === 'string') {
213
+ // Try to parse as URL first - if it succeeds, treat as link
214
+ try {
215
+ new URL(bug);
216
+ res.push({ type: 'link', url: bug });
217
+ }
218
+ catch {
219
+ // TODO: need a more robust email validation, likely
220
+ // to be replaced with valibot / zod
221
+ // If URL parsing fails, check if it's a valid email
222
+ const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
223
+ if (emailRegex.test(bug)) {
224
+ res.push({ type: 'email', email: bug });
225
+ }
226
+ else {
227
+ // Default to link for plain strings like 'example.com'
228
+ res.push({ type: 'link', url: bug });
229
+ }
230
+ }
231
+ }
232
+ else if (isObject(bug)) {
233
+ if (isNormalizedBugsEntry(bug)) {
234
+ res.push(bug);
235
+ }
236
+ const obj = bug;
237
+ if (obj.url) {
238
+ res.push({ type: 'link', url: obj.url });
239
+ }
240
+ if (obj.email) {
241
+ res.push({ type: 'email', email: obj.email });
242
+ }
243
+ }
244
+ return res.length > 0 ? res : [];
245
+ };
246
+ /**
247
+ * Normalize bugs information to a {@link NormalizedBugs} consistent format.
248
+ */
249
+ export const normalizeBugs = (bugs) => {
250
+ if (!bugs)
251
+ return;
252
+ const result = [];
253
+ // Handle array of bugs entries
254
+ if (Array.isArray(bugs)) {
255
+ for (const bug of bugs) {
256
+ result.push(...normalizeSingleBug(bug));
257
+ }
258
+ }
259
+ else {
260
+ // Handle single bugs entry
261
+ result.push(...normalizeSingleBug(bugs));
262
+ }
263
+ return result.length > 0 ? result : undefined;
264
+ };
265
+ /**
266
+ * Type guard to check if a value is a {@link NormalizedBugsEntry}.
267
+ */
268
+ export const isNormalizedBugsEntry = (o) => {
269
+ return (isObject(o) &&
270
+ 'type' in o &&
271
+ ((o.type === 'email' &&
272
+ typeof o.email === 'string' &&
273
+ !!o.email) ||
274
+ (o.type === 'link' && typeof o.url === 'string' && !!o.url)));
275
+ };
276
+ /**
277
+ * Type guard to check if a value is a {@link NormalizedBugs}.
278
+ */
279
+ export const isNormalizedBugs = (o) => {
280
+ return (Array.isArray(o) && o.length > 0 && o.every(isNormalizedBugsEntry));
281
+ };
282
+ /**
283
+ * Normalize keywords information to a {@link NormalizedKeywords} consistent format.
284
+ */
285
+ export const normalizeKeywords = (keywords) => {
286
+ if (!keywords)
287
+ return;
288
+ let keywordArray = [];
289
+ if (typeof keywords === 'string') {
290
+ // Handle comma-separated string values
291
+ keywordArray = keywords
292
+ .split(',')
293
+ .map(keyword => keyword.trim())
294
+ .filter(keyword => keyword.length > 0);
295
+ }
296
+ else if (Array.isArray(keywords)) {
297
+ // If all keywords are already normalized, return them directly
298
+ if (isNormalizedKeywords(keywords)) {
299
+ return keywords;
300
+ }
301
+ // Handle array of strings, filter out empty/invalid entries
302
+ keywordArray = keywords
303
+ .filter((keyword) => typeof keyword === 'string')
304
+ .map(keyword => keyword.trim())
305
+ .filter(keyword => keyword.length > 0);
306
+ }
307
+ else {
308
+ // Invalid format
309
+ return;
310
+ }
311
+ return keywordArray.length > 0 ? keywordArray : undefined;
312
+ };
313
+ /**
314
+ * Type guard to check if a value is a {@link NormalizedKeywords}.
315
+ */
316
+ export const isNormalizedKeywords = (o) => {
317
+ return (Array.isArray(o) &&
318
+ o.length > 0 &&
319
+ o.every(keyword => typeof keyword === 'string' &&
320
+ !!keyword &&
321
+ !keyword.startsWith(' ') &&
322
+ !keyword.endsWith(' ')));
323
+ };
324
+ /**
325
+ * Normalize engines information to a {@link NormalizedEngines} consistent format.
326
+ */
327
+ export const normalizeEngines = (engines) => {
328
+ if (!engines)
329
+ return;
330
+ if (isNormalizedEngines(engines)) {
331
+ // Return undefined if empty object
332
+ return Object.keys(engines).length === 0 ? undefined : engines;
333
+ }
334
+ // Invalid format
335
+ return;
336
+ };
337
+ /**
338
+ * Normalize OS information to a {@link NormalizedOs} consistent format.
339
+ */
340
+ export const normalizeOs = (os) => {
341
+ if (!os)
342
+ return;
343
+ let osArray = [];
344
+ if (typeof os === 'string') {
345
+ // Handle single OS string
346
+ osArray = [os.trim()].filter(item => item.length > 0);
347
+ }
348
+ else if (Array.isArray(os)) {
349
+ // If all OS entries are already normalized, return them directly
350
+ if (isNormalizedOs(os)) {
351
+ return os;
352
+ }
353
+ // Handle array of strings, filter out empty/invalid entries
354
+ osArray = os
355
+ .filter((item) => typeof item === 'string')
356
+ .map(item => item.trim())
357
+ .filter(item => item.length > 0);
358
+ }
359
+ else {
360
+ // Invalid format
361
+ return;
362
+ }
363
+ return osArray.length > 0 ? osArray : undefined;
364
+ };
365
+ /**
366
+ * Normalize CPU information to a {@link NormalizedCpu} consistent format.
367
+ */
368
+ export const normalizeCpu = (cpu) => {
369
+ if (!cpu)
370
+ return;
371
+ let cpuArray = [];
372
+ if (typeof cpu === 'string') {
373
+ // Handle single CPU string
374
+ cpuArray = [cpu.trim()].filter(item => item.length > 0);
375
+ }
376
+ else if (Array.isArray(cpu)) {
377
+ // If all CPU entries are already normalized, return them directly
378
+ if (isNormalizedCpu(cpu)) {
379
+ return cpu;
380
+ }
381
+ // Handle array of strings, filter out empty/invalid entries
382
+ cpuArray = cpu
383
+ .filter((item) => typeof item === 'string')
384
+ .map(item => item.trim())
385
+ .filter(item => item.length > 0);
386
+ }
387
+ else {
388
+ // Invalid format
389
+ return;
390
+ }
391
+ return cpuArray.length > 0 ? cpuArray : undefined;
392
+ };
393
+ /**
394
+ * Normalize libc information to a {@link NormalizedLibc} consistent format.
395
+ */
396
+ export const normalizeLibc = (libc) => {
397
+ if (!libc)
398
+ return;
399
+ let libcArray = [];
400
+ if (typeof libc === 'string') {
401
+ // Handle single libc string
402
+ libcArray = [libc.trim()].filter(item => item.length > 0);
403
+ }
404
+ else if (Array.isArray(libc)) {
405
+ // If all libc entries are already normalized, return them directly
406
+ if (isNormalizedLibc(libc)) {
407
+ return libc;
408
+ }
409
+ // Handle array of strings, filter out empty/invalid entries
410
+ libcArray = libc
411
+ .filter((item) => typeof item === 'string')
412
+ .map(item => item.trim())
413
+ .filter(item => item.length > 0);
414
+ }
415
+ else {
416
+ // Invalid format
417
+ return;
418
+ }
419
+ return libcArray.length > 0 ? libcArray : undefined;
420
+ };
421
+ /**
422
+ * Type guard to check if a value is a {@link NormalizedEngines}.
423
+ */
424
+ export const isNormalizedEngines = (o) => {
425
+ return isRecordStringString(o);
426
+ };
427
+ /**
428
+ * Type guard to check if a value is a {@link NormalizedOs}.
429
+ */
430
+ export const isNormalizedOs = (o) => {
431
+ return (Array.isArray(o) &&
432
+ o.length > 0 &&
433
+ o.every(item => typeof item === 'string' &&
434
+ !!item &&
435
+ !item.startsWith(' ') &&
436
+ !item.endsWith(' ')));
437
+ };
438
+ /**
439
+ * Type guard to check if a value is a {@link NormalizedCpu}.
440
+ */
441
+ export const isNormalizedCpu = (o) => {
442
+ return (Array.isArray(o) &&
443
+ o.length > 0 &&
444
+ o.every(item => typeof item === 'string' &&
445
+ !!item &&
446
+ !item.startsWith(' ') &&
447
+ !item.endsWith(' ')));
448
+ };
449
+ /**
450
+ * Type guard to check if a value is a {@link NormalizedLibc}.
451
+ */
452
+ export const isNormalizedLibc = (o) => {
453
+ return (Array.isArray(o) &&
454
+ o.length > 0 &&
455
+ o.every(item => typeof item === 'string' &&
456
+ !!item &&
457
+ !item.startsWith(' ') &&
458
+ !item.endsWith(' ')));
459
+ };
460
+ /**
461
+ * Normalizes the bin paths.
462
+ */
463
+ export const normalizeBinPaths = (manifest) => {
464
+ const { name, bin } = manifest;
465
+ if (bin) {
466
+ if (name && typeof bin === 'string') {
467
+ const [_scope, pkg] = parseScope(name);
468
+ return { [pkg]: bin };
469
+ }
470
+ else if (typeof bin === 'object') {
471
+ return bin;
472
+ }
473
+ }
474
+ };
475
+ /**
476
+ * A type guard to check if a value is a boolean.
477
+ */
478
+ export const isBoolean = (value) => typeof value === 'boolean';
479
+ export const integrityRE = /^sha512-[a-zA-Z0-9/+]{86}==$/;
480
+ export const isIntegrity = (i) => typeof i === 'string' && integrityRE.test(i);
481
+ export const asIntegrity = (i) => {
482
+ if (!isIntegrity(i)) {
483
+ throw error('invalid integrity', {
484
+ found: i,
485
+ wanted: integrityRE,
486
+ }, asIntegrity);
487
+ }
488
+ return i;
489
+ };
490
+ export const assertIntegrity = i => {
491
+ asIntegrity(i);
492
+ };
493
+ export const keyIDRE = /^SHA256:[a-zA-Z0-9/+]{43}$/;
494
+ export const isKeyID = (k) => typeof k === 'string' && keyIDRE.test(k);
495
+ export const asKeyID = (k) => {
496
+ if (!isKeyID(k)) {
497
+ throw error('invalid key ID', {
498
+ found: k,
499
+ wanted: keyIDRE,
500
+ }, asKeyID);
501
+ }
502
+ return k;
503
+ };
504
+ export const assertKeyID = k => {
505
+ asKeyID(k);
506
+ };
507
+ /**
508
+ * Convert an unknown value to an error.
509
+ */
510
+ export const asError = (er, fallbackMessage = 'Unknown error') => er instanceof Error ? er : new Error(String(er) || fallbackMessage);
511
+ /**
512
+ * Check if a value is an error.
513
+ */
514
+ export const isError = (er) => er instanceof Error;
515
+ /**
516
+ * Check if an error has a cause property.
517
+ */
518
+ export const isErrorWithCause = (er) => isError(er) && 'cause' in er;
519
+ /**
520
+ * Check if an unknown value is a plain object.
521
+ */
522
+ export const isObject = (v) => !!v &&
523
+ typeof v === 'object' &&
524
+ (v.constructor === Object ||
525
+ v.constructor === undefined);
526
+ export const maybeRecordStringString = (o) => o === undefined || isRecordStringString(o);
527
+ export const isRecordStringString = (o) => isRecordStringT(o, s => typeof s === 'string');
528
+ export const assertRecordStringString = (o) => assertRecordStringT(o, s => typeof s === 'string', 'Record<string, string>');
529
+ export const isRecordStringT = (o, check) => isObject(o) &&
530
+ Object.entries(o).every(([k, v]) => typeof k === 'string' && check(v));
531
+ export const assertRecordStringT = (o, check,
532
+ /** a type description, like 'Record<string, Record<string, string>>' */
533
+ wanted) => {
534
+ if (!isRecordStringT(o, check)) {
535
+ throw error('Invalid record', {
536
+ found: o,
537
+ wanted,
538
+ });
539
+ }
540
+ };
541
+ export const isRecordStringManifest = (o) => isRecordStringT(o, v => isManifest(v));
542
+ export const maybePeerDependenciesMetaSet = (o) => o === undefined ||
543
+ isRecordStringT(o, v => isPeerDependenciesMetaValue(v));
544
+ export const maybeBoolean = (o) => o === undefined || typeof o === 'boolean';
545
+ export const isPeerDependenciesMetaValue = (o) => isObject(o) && maybeBoolean(o.optional);
546
+ export const maybeString = (a) => a === undefined || typeof a === 'string';
547
+ export const maybeDist = (a) => a === undefined || (isObject(a) && maybeString(a.tarball));
548
+ /**
549
+ * Is a given unknown value a valid {@link Manifest} object?
550
+ * Returns `true` if so.
551
+ */
552
+ export const isManifest = (m) => isObject(m) &&
553
+ !Array.isArray(m) &&
554
+ maybeString(m.name) &&
555
+ maybeString(m.version) &&
556
+ maybeRecordStringString(m.dependencies) &&
557
+ maybeRecordStringString(m.devDependencies) &&
558
+ maybeRecordStringString(m.optionalDependencies) &&
559
+ maybeRecordStringString(m.peerDependencies) &&
560
+ maybeRecordStringString(m.acceptDependencies) &&
561
+ maybePeerDependenciesMetaSet(m.peerDependenciesMeta) &&
562
+ maybeDist(m.dist);
563
+ /**
564
+ * A specific {@link Manifest} that is retrieved uniquely from reading
565
+ * registry packument and manifest endpoints, it has `dist`, `name` and
566
+ * `version` fields defined.
567
+ */
568
+ export const isManifestRegistry = (m) => isManifest(m) && !!m.dist && !!m.name && !!m.version;
569
+ /**
570
+ * Given an unknown value, convert it to a {@link Manifest}.
571
+ */
572
+ export const asManifest = (m, from) => {
573
+ if (!isManifest(m)) {
574
+ throw error('invalid manifest', { found: m }, from ?? asManifest);
575
+ }
576
+ return m;
577
+ };
578
+ const normalizeManifestCache = new WeakMap();
579
+ /**
580
+ * Given a {@link Manifest} returns a {@link NormalizedManifest} that
581
+ * contains normalized author, bugs, funding, contributors, keywords and
582
+ * version fields.
583
+ */
584
+ export const normalizeManifest = (manifest) => {
585
+ // Check cache first using manifest object reference
586
+ const cached = normalizeManifestCache.get(manifest);
587
+ if (cached) {
588
+ return cached;
589
+ }
590
+ manifest = fixManifestVersion(manifest);
591
+ const normalizedAuthor = parsePerson(manifest.author);
592
+ const normalizedFunding = normalizeFunding(manifest.funding);
593
+ const normalizedContributors = normalizeContributors(manifest.contributors, manifest.maintainers);
594
+ const normalizedBugs = normalizeBugs(manifest.bugs);
595
+ const normalizedKeywords = normalizeKeywords(manifest.keywords);
596
+ const normalizedEngines = normalizeEngines(manifest.engines);
597
+ const normalizedOs = normalizeOs(manifest.os);
598
+ const normalizedCpu = normalizeCpu(manifest.cpu);
599
+ const normalizedLibc = normalizeLibc(manifest.libc);
600
+ const normalizedBin = normalizeBinPaths(manifest);
601
+ // holds the same object reference but renames the variable here
602
+ // so that it's simpler to cast it to the normalized type
603
+ const normalizedManifest = manifest;
604
+ if (normalizedAuthor) {
605
+ normalizedManifest.author = normalizedAuthor;
606
+ }
607
+ else {
608
+ delete normalizedManifest.author;
609
+ }
610
+ if (normalizedFunding) {
611
+ normalizedManifest.funding = normalizedFunding;
612
+ }
613
+ else {
614
+ delete normalizedManifest.funding;
615
+ }
616
+ if (normalizedContributors) {
617
+ normalizedManifest.contributors = normalizedContributors;
618
+ }
619
+ else {
620
+ delete normalizedManifest.contributors;
621
+ }
622
+ if (normalizedBugs) {
623
+ normalizedManifest.bugs = normalizedBugs;
624
+ }
625
+ else {
626
+ delete normalizedManifest.bugs;
627
+ }
628
+ if (normalizedKeywords) {
629
+ normalizedManifest.keywords = normalizedKeywords;
630
+ }
631
+ else {
632
+ delete normalizedManifest.keywords;
633
+ }
634
+ if (normalizedEngines) {
635
+ normalizedManifest.engines = normalizedEngines;
636
+ }
637
+ else {
638
+ delete normalizedManifest.engines;
639
+ }
640
+ if (normalizedOs) {
641
+ normalizedManifest.os = normalizedOs;
642
+ }
643
+ else {
644
+ delete normalizedManifest.os;
645
+ }
646
+ if (normalizedCpu) {
647
+ normalizedManifest.cpu = normalizedCpu;
648
+ }
649
+ else {
650
+ delete normalizedManifest.cpu;
651
+ }
652
+ if (normalizedLibc) {
653
+ normalizedManifest.libc = normalizedLibc;
654
+ }
655
+ else {
656
+ delete normalizedManifest.libc;
657
+ }
658
+ if (normalizedBin) {
659
+ normalizedManifest.bin = normalizedBin;
660
+ }
661
+ else {
662
+ delete normalizedManifest.bin;
663
+ }
664
+ // Remove maintainers field if it exists in the raw manifest
665
+ // this can only happen if the manifest is of ManifestRegistry type
666
+ if ('maintainers' in normalizedManifest &&
667
+ normalizedManifest.maintainers) {
668
+ delete normalizedManifest.maintainers;
669
+ }
670
+ // Cache the result using the manifest object reference
671
+ normalizeManifestCache.set(manifest, normalizedManifest);
672
+ return normalizedManifest;
673
+ };
674
+ /**
675
+ * Type guard to check if a value is a {@link NormalizedManifest}.
676
+ */
677
+ export const isNormalizedManifest = (o) => {
678
+ return (isManifest(o) &&
679
+ // given that all these values are optional and potentially undefined
680
+ // we only check their value content if they are present
681
+ ('author' in o ? isNormalizedContributorEntry(o.author) : true) &&
682
+ ('contributors' in o ?
683
+ isNormalizedContributors(o.contributors)
684
+ : true) &&
685
+ ('funding' in o ? isNormalizedFunding(o.funding) : true) &&
686
+ ('bugs' in o ? isNormalizedBugs(o.bugs) : true) &&
687
+ ('keywords' in o ? isNormalizedKeywords(o.keywords) : true) &&
688
+ ('engines' in o ? isNormalizedEngines(o.engines) : true) &&
689
+ ('os' in o ? isNormalizedOs(o.os) : true) &&
690
+ ('cpu' in o ? isNormalizedCpu(o.cpu) : true) &&
691
+ ('libc' in o ? isNormalizedLibc(o.libc) : true));
692
+ };
693
+ /**
694
+ * Given an unknown value, convert it to a {@link NormalizedManifest}.
695
+ */
696
+ export const asNormalizedManifest = (m, from) => {
697
+ if (!isNormalizedManifest(m)) {
698
+ throw error('invalid normalized manifest', { found: m }, from ?? asNormalizedManifest);
699
+ }
700
+ return m;
701
+ };
702
+ /**
703
+ * Given an unknown value, convert it to a {@link ManifestRegistry}.
704
+ */
705
+ export const asManifestRegistry = (m, from) => {
706
+ if (!isManifestRegistry(m)) {
707
+ throw error('invalid registry manifest', { found: m }, from ?? asManifestRegistry);
708
+ }
709
+ return m;
710
+ };
711
+ /**
712
+ * Type guard to check if a value is a {@link NormalizedManifestRegistry}.
713
+ */
714
+ export const isNormalizedManifestRegistry = (o) => {
715
+ return isNormalizedManifest(o) && isManifestRegistry(o);
716
+ };
717
+ /**
718
+ * Given an unknown value, convert it to a {@link NormalizedManifestRegistry}.
719
+ */
720
+ export const asNormalizedManifestRegistry = (m, from) => {
721
+ if (!isNormalizedManifestRegistry(m)) {
722
+ throw error('invalid normalized manifest registry', { found: m }, from ?? asNormalizedManifestRegistry);
723
+ }
724
+ return m;
725
+ };
726
+ /**
727
+ * Expands a normalized contributor entry by converting the
728
+ * in-memory symbols to their plain values.
729
+ */
730
+ const expandNormalizedContributorEntrySymbols = (c) => {
731
+ return {
732
+ ...c,
733
+ writeAccess: c[kWriteAccess],
734
+ isPublisher: c[kIsPublisher],
735
+ };
736
+ };
737
+ /**
738
+ * Walks a normalized manifest and expands any symbols found
739
+ * in the `author` and `contributors` fields.
740
+ */
741
+ export const expandNormalizedManifestSymbols = (m) => {
742
+ const res = { ...m };
743
+ if (isNormalizedContributorEntry(m.author)) {
744
+ res.author = expandNormalizedContributorEntrySymbols(m.author);
745
+ }
746
+ if (isNormalizedContributors(m.contributors)) {
747
+ res.contributors = m.contributors.map(expandNormalizedContributorEntrySymbols);
748
+ }
749
+ return res;
750
+ };
751
+ export const assertManifest = m => {
752
+ asManifest(m, assertManifest);
753
+ };
754
+ export const assertManifestRegistry = m => {
755
+ asManifestRegistry(m, assertManifestRegistry);
756
+ };
757
+ export const isPackument = (p) => {
758
+ if (!isObject(p) || typeof p.name !== 'string')
759
+ return false;
760
+ const { versions, 'dist-tags': distTags, time } = p;
761
+ return (isRecordStringString(distTags) &&
762
+ isRecordStringManifest(versions) &&
763
+ maybeRecordStringString(time) &&
764
+ Object.values(distTags).every(v => versions[v]?.name == p.name));
765
+ };
766
+ export const asPackument = (p, from) => {
767
+ if (!isPackument(p)) {
768
+ throw error('invalid packument', { found: p }, from ?? asPackument);
769
+ }
770
+ return p;
771
+ };
772
+ export const assertPackument = m => {
773
+ asPackument(m);
774
+ };
775
+ /**
776
+ * A set of the possible long dependency type names,
777
+ * as used in `package.json` files.
778
+ */
779
+ export const longDependencyTypes = new Set([
780
+ 'dependencies',
781
+ 'devDependencies',
782
+ 'peerDependencies',
783
+ 'optionalDependencies',
784
+ ]);
785
+ /**
786
+ * A set of the short type keys used to represent dependency relationships.
787
+ */
788
+ export const shortDependencyTypes = new Set([
789
+ 'prod',
790
+ 'dev',
791
+ 'optional',
792
+ 'peer',
793
+ 'peerOptional',
794
+ ]);
795
+ /**
796
+ * Maps between long form names usually used in `package.json` files
797
+ * to a corresponding short form name, used in lockfiles.
798
+ */
799
+ export const dependencyTypes = new Map([
800
+ ['dependencies', 'prod'],
801
+ ['devDependencies', 'dev'],
802
+ ['peerDependencies', 'peer'],
803
+ ['optionalDependencies', 'optional'],
804
+ ]);
805
+ /**
806
+ * Parse a scoped package name into its scope and name components.
807
+ */
808
+ export const parseScope = (scoped) => {
809
+ if (scoped.startsWith('@')) {
810
+ const [scope, name, ...rest] = scoped.split('/');
811
+ if (scope && name && rest.length === 0)
812
+ return [scope, name];
813
+ }
814
+ return [undefined, scoped];
815
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vltpkg/types",
3
3
  "description": "definitions for some of vlt's core types",
4
- "version": "1.0.0-rc.23",
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/dep-id": "1.0.0-rc.23",
16
- "@vltpkg/error-cause": "1.0.0-rc.23",
17
- "@vltpkg/semver": "1.0.0-rc.23",
18
- "@vltpkg/spec": "1.0.0-rc.23"
15
+ "@vltpkg/dep-id": "1.0.0-rc.24",
16
+ "@vltpkg/error-cause": "1.0.0-rc.24",
17
+ "@vltpkg/semver": "1.0.0-rc.24",
18
+ "@vltpkg/spec": "1.0.0-rc.24"
19
19
  },
20
20
  "devDependencies": {
21
21
  "@eslint/js": "^9.39.1",