-
Notifications
You must be signed in to change notification settings - Fork 731
Expand file tree
/
Copy pathnormalize.ts
More file actions
151 lines (133 loc) · 4.51 KB
/
Copy pathnormalize.ts
File metadata and controls
151 lines (133 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { canonicalizeRepoUrl } from '../utils/canonicalizeRepoUrl'
import type { CanonicalRepo } from '../utils/canonicalizeRepoUrl'
import type { Packument } from './types'
export function parseNpmName(raw: string): { namespace: string | null; name: string } {
if (raw.startsWith('@')) {
const slash = raw.indexOf('/')
if (slash !== -1) {
return { namespace: raw.slice(0, slash), name: raw.slice(slash + 1) }
}
}
return { namespace: null, name: raw }
}
// Postgres text columns cannot store NUL (U+0000); npm packuments occasionally
// carry them (e.g. mojibake descriptions). Strip them in place from every string
// in the packument before persisting — otherwise the inlined value breaks the
// PostgreSQL wire protocol ("invalid message format").
export function stripNullBytesDeep<T>(value: T): T {
if (typeof value === 'string') {
// eslint-disable-next-line no-control-regex
return value.replace(/\u0000/g, '') as T
}
if (Array.isArray(value)) {
for (let i = 0; i < value.length; i++) value[i] = stripNullBytesDeep(value[i])
return value
}
if (value !== null && typeof value === 'object') {
const obj = value as Record<string, unknown>
for (const k of Object.keys(obj)) obj[k] = stripNullBytesDeep(obj[k])
return value
}
return value
}
export function normalizeLicenses(packument: Packument): string[] {
const rawArr = packument.licenses
if (rawArr && Array.isArray(rawArr)) {
return dedup(rawArr.map((l) => clean(l.type)).filter(Boolean))
}
const raw = packument.license
if (!raw) return []
if (typeof raw === 'object') {
return raw.type ? [clean(raw.type)] : []
}
if (!raw || raw === 'UNLICENSED' || raw.startsWith('SEE LICENSE')) return []
return dedup(
raw
.split(/\s+(?:OR|AND)\s+/i)
.map((s) => clean(s))
.filter(Boolean),
)
}
// A version's `license` field comes in several shapes: a plain SPDX string ("MIT"),
// an object ({ type, url }), or the legacy array form ([{ type, file }, ...]). Passing those
// raw into a text column would persist objects/arrays, so collapse every shape to a single
// string (OR-joined for the array form) or null. Non-string `type` values are dropped.
export function versionLicense(raw: unknown): string | null {
if (raw == null) return null
if (typeof raw === 'string') return raw || null
if (Array.isArray(raw)) {
const types = raw
.map((l) => (typeof l === 'string' ? l : licenseType(l)))
.filter((t): t is string => Boolean(t))
return types.length ? types.join(' OR ') : null
}
return licenseType(raw)
}
// Extract a string `type` from a license object, or null if absent/non-string.
function licenseType(v: unknown): string | null {
if (typeof v !== 'object' || v === null) return null
const type = (v as { type?: unknown }).type
return typeof type === 'string' ? type : null
}
function clean(s: string): string {
return s.replace(/[()]/g, '').trim()
}
function dedup(arr: string[]): string[] {
return [...new Set(arr)]
}
export function extractRepo(packument: Packument): CanonicalRepo | null {
const repo = packument.repository
if (!repo) return null
const raw = typeof repo === 'string' ? repo : repo.url
if (!raw) return null
return canonicalizeRepoUrl(raw)
}
export function collectMaintainers(packument: Packument): Array<{
username: string
displayName: string | null
email: string | null
role: 'author' | 'maintainer'
}> {
const map = new Map<
string,
{
username: string
displayName: string | null
email: string | null
role: 'author' | 'maintainer'
}
>()
for (const m of packument.maintainers ?? []) {
if (!m.name) continue
map.set(m.name, {
username: m.name,
displayName: m.name,
email: m.email ?? null,
role: 'maintainer',
})
}
const author = packument.author
if (author) {
const parsed =
typeof author === 'string'
? parseAuthorString(author)
: { name: author.name, email: author.email ?? null }
if (parsed.name) {
map.set(parsed.name, {
username: parsed.name,
displayName: parsed.name,
email: parsed.email,
role: 'author',
})
}
}
return [...map.values()]
}
function parseAuthorString(s: string): { name: string; email: string | null } {
const emailMatch = s.match(/<([^>]+)>/)
const name = s.split(/[<(]/)[0].trim()
return { name, email: emailMatch ? emailMatch[1] : null }
}
export function isPrerelease(version: string): boolean {
return /^[0-9]+\.[0-9]+\.[0-9]+-.+/.test(version)
}