@vltpkg/types 0.0.0-9 → 1.0.0-rc.2

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/dist/esm/index.js CHANGED
@@ -1,5 +1,443 @@
1
1
  import { error } from '@vltpkg/error-cause';
2
- const integrityRE = /^sha512-[a-zA-Z0-9/+]{86}==$/;
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
+ * Type guard to check if a value is a {@link NormalizedEngines}.
395
+ */
396
+ export const isNormalizedEngines = (o) => {
397
+ return isRecordStringString(o);
398
+ };
399
+ /**
400
+ * Type guard to check if a value is a {@link NormalizedOs}.
401
+ */
402
+ export const isNormalizedOs = (o) => {
403
+ return (Array.isArray(o) &&
404
+ o.length > 0 &&
405
+ o.every(item => typeof item === 'string' &&
406
+ !!item &&
407
+ !item.startsWith(' ') &&
408
+ !item.endsWith(' ')));
409
+ };
410
+ /**
411
+ * Type guard to check if a value is a {@link NormalizedCpu}.
412
+ */
413
+ export const isNormalizedCpu = (o) => {
414
+ return (Array.isArray(o) &&
415
+ o.length > 0 &&
416
+ o.every(item => typeof item === 'string' &&
417
+ !!item &&
418
+ !item.startsWith(' ') &&
419
+ !item.endsWith(' ')));
420
+ };
421
+ /**
422
+ * Normalizes the bin paths.
423
+ */
424
+ export const normalizeBinPaths = (manifest) => {
425
+ const { name, bin } = manifest;
426
+ if (bin) {
427
+ if (name && typeof bin === 'string') {
428
+ const [_scope, pkg] = parseScope(name);
429
+ return { [pkg]: bin };
430
+ }
431
+ else if (typeof bin === 'object') {
432
+ return bin;
433
+ }
434
+ }
435
+ };
436
+ /**
437
+ * A type guard to check if a value is a boolean.
438
+ */
439
+ export const isBoolean = (value) => typeof value === 'boolean';
440
+ export const integrityRE = /^sha512-[a-zA-Z0-9/+]{86}==$/;
3
441
  export const isIntegrity = (i) => typeof i === 'string' && integrityRE.test(i);
4
442
  export const asIntegrity = (i) => {
5
443
  if (!isIntegrity(i)) {
@@ -13,7 +451,7 @@ export const asIntegrity = (i) => {
13
451
  export const assertIntegrity = i => {
14
452
  asIntegrity(i);
15
453
  };
16
- const keyIDRE = /^SHA256:[a-zA-Z0-9/+]{43}$/;
454
+ export const keyIDRE = /^SHA256:[a-zA-Z0-9/+]{43}$/;
17
455
  export const isKeyID = (k) => typeof k === 'string' && keyIDRE.test(k);
18
456
  export const asKeyID = (k) => {
19
457
  if (!isKeyID(k)) {
@@ -27,19 +465,52 @@ export const asKeyID = (k) => {
27
465
  export const assertKeyID = k => {
28
466
  asKeyID(k);
29
467
  };
30
- const isObj = (o) => !!o && typeof o === 'object';
31
- const maybeRecordStringString = (o) => o === undefined || isRecordStringString(o);
32
- const isRecordStringString = (o) => isRecordStringT(o, s => typeof s === 'string');
33
- const isRecordStringT = (o, check) => isObj(o) &&
468
+ /**
469
+ * Convert an unknown value to an error.
470
+ */
471
+ export const asError = (er, fallbackMessage = 'Unknown error') => er instanceof Error ? er : new Error(String(er) || fallbackMessage);
472
+ /**
473
+ * Check if a value is an error.
474
+ */
475
+ export const isError = (er) => er instanceof Error;
476
+ /**
477
+ * Check if an error has a cause property.
478
+ */
479
+ export const isErrorWithCause = (er) => isError(er) && 'cause' in er;
480
+ /**
481
+ * Check if an unknown value is a plain object.
482
+ */
483
+ export const isObject = (v) => !!v &&
484
+ typeof v === 'object' &&
485
+ (v.constructor === Object ||
486
+ v.constructor === undefined);
487
+ export const maybeRecordStringString = (o) => o === undefined || isRecordStringString(o);
488
+ export const isRecordStringString = (o) => isRecordStringT(o, s => typeof s === 'string');
489
+ export const assertRecordStringString = (o) => assertRecordStringT(o, s => typeof s === 'string', 'Record<string, string>');
490
+ export const isRecordStringT = (o, check) => isObject(o) &&
34
491
  Object.entries(o).every(([k, v]) => typeof k === 'string' && check(v));
35
- const isRecordStringManifest = (o) => isRecordStringT(o, v => isManifest(v));
36
- const maybePeerDependenciesMetaSet = (o) => o === undefined ||
492
+ export const assertRecordStringT = (o, check,
493
+ /** a type description, like 'Record<string, Record<string, string>>' */
494
+ wanted) => {
495
+ if (!isRecordStringT(o, check)) {
496
+ throw error('Invalid record', {
497
+ found: o,
498
+ wanted,
499
+ });
500
+ }
501
+ };
502
+ export const isRecordStringManifest = (o) => isRecordStringT(o, v => isManifest(v));
503
+ export const maybePeerDependenciesMetaSet = (o) => o === undefined ||
37
504
  isRecordStringT(o, v => isPeerDependenciesMetaValue(v));
38
- const maybeBoolean = (o) => o === undefined || typeof o === 'boolean';
39
- const isPeerDependenciesMetaValue = (o) => isObj(o) && maybeBoolean(o.optional);
40
- const maybeString = (a) => a === undefined || typeof a === 'string';
41
- const maybeDist = (a) => a === undefined || (isObj(a) && maybeString(a.tarball));
42
- export const isManifest = (m) => isObj(m) &&
505
+ export const maybeBoolean = (o) => o === undefined || typeof o === 'boolean';
506
+ export const isPeerDependenciesMetaValue = (o) => isObject(o) && maybeBoolean(o.optional);
507
+ export const maybeString = (a) => a === undefined || typeof a === 'string';
508
+ export const maybeDist = (a) => a === undefined || (isObject(a) && maybeString(a.tarball));
509
+ /**
510
+ * Is a given unknown value a valid {@link Manifest} object?
511
+ * Returns `true` if so.
512
+ */
513
+ export const isManifest = (m) => isObject(m) &&
43
514
  !Array.isArray(m) &&
44
515
  maybeString(m.name) &&
45
516
  maybeString(m.version) &&
@@ -50,19 +521,179 @@ export const isManifest = (m) => isObj(m) &&
50
521
  maybeRecordStringString(m.acceptDependencies) &&
51
522
  maybePeerDependenciesMetaSet(m.peerDependenciesMeta) &&
52
523
  maybeDist(m.dist);
524
+ /**
525
+ * A specific {@link Manifest} that is retrieved uniquely from reading
526
+ * registry packument and manifest endpoints, it has `dist`, `name` and
527
+ * `version` fields defined.
528
+ */
53
529
  export const isManifestRegistry = (m) => isManifest(m) && !!m.dist && !!m.name && !!m.version;
530
+ /**
531
+ * Given an unknown value, convert it to a {@link Manifest}.
532
+ */
54
533
  export const asManifest = (m, from) => {
55
534
  if (!isManifest(m)) {
56
535
  throw error('invalid manifest', { found: m }, from ?? asManifest);
57
536
  }
58
537
  return m;
59
538
  };
539
+ /**
540
+ * Given a {@link Manifest} returns a {@link NormalizedManifest} that
541
+ * contains normalized author, bugs, funding, contributors, keywords and
542
+ * version fields.
543
+ */
544
+ export const normalizeManifest = (manifest) => {
545
+ manifest = fixManifestVersion(manifest);
546
+ const normalizedAuthor = parsePerson(manifest.author);
547
+ const normalizedFunding = normalizeFunding(manifest.funding);
548
+ const normalizedContributors = normalizeContributors(manifest.contributors, manifest.maintainers);
549
+ const normalizedBugs = normalizeBugs(manifest.bugs);
550
+ const normalizedKeywords = normalizeKeywords(manifest.keywords);
551
+ const normalizedEngines = normalizeEngines(manifest.engines);
552
+ const normalizedOs = normalizeOs(manifest.os);
553
+ const normalizedCpu = normalizeCpu(manifest.cpu);
554
+ const normalizedBin = normalizeBinPaths(manifest);
555
+ // holds the same object reference but renames the variable here
556
+ // so that it's simpler to cast it to the normalized type
557
+ const normalizedManifest = manifest;
558
+ if (normalizedAuthor) {
559
+ normalizedManifest.author = normalizedAuthor;
560
+ }
561
+ else {
562
+ delete normalizedManifest.author;
563
+ }
564
+ if (normalizedFunding) {
565
+ normalizedManifest.funding = normalizedFunding;
566
+ }
567
+ else {
568
+ delete normalizedManifest.funding;
569
+ }
570
+ if (normalizedContributors) {
571
+ normalizedManifest.contributors = normalizedContributors;
572
+ }
573
+ else {
574
+ delete normalizedManifest.contributors;
575
+ }
576
+ if (normalizedBugs) {
577
+ normalizedManifest.bugs = normalizedBugs;
578
+ }
579
+ else {
580
+ delete normalizedManifest.bugs;
581
+ }
582
+ if (normalizedKeywords) {
583
+ normalizedManifest.keywords = normalizedKeywords;
584
+ }
585
+ else {
586
+ delete normalizedManifest.keywords;
587
+ }
588
+ if (normalizedEngines) {
589
+ normalizedManifest.engines = normalizedEngines;
590
+ }
591
+ else {
592
+ delete normalizedManifest.engines;
593
+ }
594
+ if (normalizedOs) {
595
+ normalizedManifest.os = normalizedOs;
596
+ }
597
+ else {
598
+ delete normalizedManifest.os;
599
+ }
600
+ if (normalizedCpu) {
601
+ normalizedManifest.cpu = normalizedCpu;
602
+ }
603
+ else {
604
+ delete normalizedManifest.cpu;
605
+ }
606
+ if (normalizedBin) {
607
+ normalizedManifest.bin = normalizedBin;
608
+ }
609
+ else {
610
+ delete normalizedManifest.bin;
611
+ }
612
+ // Remove maintainers field if it exists in the raw manifest
613
+ // this can only happen if the manifest is of ManifestRegistry type
614
+ if ('maintainers' in normalizedManifest &&
615
+ normalizedManifest.maintainers) {
616
+ delete normalizedManifest.maintainers;
617
+ return normalizedManifest;
618
+ }
619
+ return normalizedManifest;
620
+ };
621
+ /**
622
+ * Type guard to check if a value is a {@link NormalizedManifest}.
623
+ */
624
+ export const isNormalizedManifest = (o) => {
625
+ return (isManifest(o) &&
626
+ // given that all these values are optional and potentially undefined
627
+ // we only check their value content if they are present
628
+ ('author' in o ? isNormalizedContributorEntry(o.author) : true) &&
629
+ ('contributors' in o ?
630
+ isNormalizedContributors(o.contributors)
631
+ : true) &&
632
+ ('funding' in o ? isNormalizedFunding(o.funding) : true) &&
633
+ ('bugs' in o ? isNormalizedBugs(o.bugs) : true) &&
634
+ ('keywords' in o ? isNormalizedKeywords(o.keywords) : true) &&
635
+ ('engines' in o ? isNormalizedEngines(o.engines) : true) &&
636
+ ('os' in o ? isNormalizedOs(o.os) : true) &&
637
+ ('cpu' in o ? isNormalizedCpu(o.cpu) : true));
638
+ };
639
+ /**
640
+ * Given an unknown value, convert it to a {@link NormalizedManifest}.
641
+ */
642
+ export const asNormalizedManifest = (m, from) => {
643
+ if (!isNormalizedManifest(m)) {
644
+ throw error('invalid normalized manifest', { found: m }, from ?? asNormalizedManifest);
645
+ }
646
+ return m;
647
+ };
648
+ /**
649
+ * Given an unknown value, convert it to a {@link ManifestRegistry}.
650
+ */
60
651
  export const asManifestRegistry = (m, from) => {
61
652
  if (!isManifestRegistry(m)) {
62
653
  throw error('invalid registry manifest', { found: m }, from ?? asManifestRegistry);
63
654
  }
64
655
  return m;
65
656
  };
657
+ /**
658
+ * Type guard to check if a value is a {@link NormalizedManifestRegistry}.
659
+ */
660
+ export const isNormalizedManifestRegistry = (o) => {
661
+ return isNormalizedManifest(o) && isManifestRegistry(o);
662
+ };
663
+ /**
664
+ * Given an unknown value, convert it to a {@link NormalizedManifestRegistry}.
665
+ */
666
+ export const asNormalizedManifestRegistry = (m, from) => {
667
+ if (!isNormalizedManifestRegistry(m)) {
668
+ throw error('invalid normalized manifest registry', { found: m }, from ?? asNormalizedManifestRegistry);
669
+ }
670
+ return m;
671
+ };
672
+ /**
673
+ * Expands a normalized contributor entry by converting the
674
+ * in-memory symbols to their plain values.
675
+ */
676
+ const expandNormalizedContributorEntrySymbols = (c) => {
677
+ return {
678
+ ...c,
679
+ writeAccess: c[kWriteAccess],
680
+ isPublisher: c[kIsPublisher],
681
+ };
682
+ };
683
+ /**
684
+ * Walks a normalized manifest and expands any symbols found
685
+ * in the `author` and `contributors` fields.
686
+ */
687
+ export const expandNormalizedManifestSymbols = (m) => {
688
+ const res = { ...m };
689
+ if (isNormalizedContributorEntry(m.author)) {
690
+ res.author = expandNormalizedContributorEntrySymbols(m.author);
691
+ }
692
+ if (isNormalizedContributors(m.contributors)) {
693
+ res.contributors = m.contributors.map(expandNormalizedContributorEntrySymbols);
694
+ }
695
+ return res;
696
+ };
66
697
  export const assertManifest = m => {
67
698
  asManifest(m, assertManifest);
68
699
  };
@@ -70,7 +701,7 @@ export const assertManifestRegistry = m => {
70
701
  asManifestRegistry(m, assertManifestRegistry);
71
702
  };
72
703
  export const isPackument = (p) => {
73
- if (!isObj(p) || typeof p.name !== 'string')
704
+ if (!isObject(p) || typeof p.name !== 'string')
74
705
  return false;
75
706
  const { versions, 'dist-tags': distTags, time } = p;
76
707
  return (isRecordStringString(distTags) &&
@@ -117,4 +748,15 @@ export const dependencyTypes = new Map([
117
748
  ['peerDependencies', 'peer'],
118
749
  ['optionalDependencies', 'optional'],
119
750
  ]);
751
+ /**
752
+ * Parse a scoped package name into its scope and name components.
753
+ */
754
+ export const parseScope = (scoped) => {
755
+ if (scoped.startsWith('@')) {
756
+ const [scope, name, ...rest] = scoped.split('/');
757
+ if (scope && name && rest.length === 0)
758
+ return [scope, name];
759
+ }
760
+ return [undefined, scoped];
761
+ };
120
762
  //# sourceMappingURL=index.js.map