-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspecs.ts
More file actions
231 lines (220 loc) · 6.64 KB
/
specs.ts
File metadata and controls
231 lines (220 loc) · 6.64 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**
* @file Package spec parsing, name resolution, and GitHub URL utilities.
*/
import { REGISTRY_SCOPE_DELIMITER } from '../constants/socket'
// @ts-expect-error - external vendored module
import { PackageURL } from '../external/@socketregistry/packageurl-js'
import npmPackageArg from '../external/npm-package-arg'
import { isPlainObject } from '../objects/predicates'
import { getSmolPurl } from '../smol/purl'
import { isNonEmptyString } from '../strings/predicates'
import {
StringPrototypeCharCodeAt,
StringPrototypeEndsWith,
StringPrototypeIndexOf,
StringPrototypeSlice,
StringPrototypeStartsWith,
} from '../primordials/string'
/**
* Get the release tag for a version.
*
* @example
* ;```typescript
* getReleaseTag('lodash@latest') // 'latest'
* getReleaseTag('@scope/pkg@beta') // 'beta'
* getReleaseTag('lodash') // ''
* ```
*/
export function getReleaseTag(spec: string): string {
if (!spec) {
return ''
}
// Handle scoped packages like @scope/package vs @scope/package@tag.
let atIndex = -1
if (StringPrototypeStartsWith(spec, '@')) {
// Find the second @ for scoped packages.
atIndex = StringPrototypeIndexOf(spec, '@', 1)
} else {
// Find the first @ for unscoped packages.
atIndex = StringPrototypeIndexOf(spec, '@')
}
if (atIndex !== -1) {
return StringPrototypeSlice(spec, atIndex + 1)
}
return ''
}
/**
* Extract user and project from GitHub repository URL.
*
* @example
* ;```typescript
* getRepoUrlDetails('https://github.com/lodash/lodash.git')
* // { user: 'lodash', project: 'lodash' }
* ```
*/
export function getRepoUrlDetails(repoUrl: string = ''): {
user: string
project: string
} {
// Anchor the host to exactly `github.com` (optionally preceded by
// userinfo like `user@`). Escaping the `.` blocks lookalikes like
// `githubXcom`; pinning the host to the full final label blocks
// `fake-github.com` or `github.com.attacker.tld` shenanigans. The
// scheme class allows `+` so npm's canonical `git+https://…` and
// `git+ssh://…` forms from package.json `repository.url` match.
// Callers passing scp-style git@github.com:… need to normalize
// upstream; we require `://` here so the host is unambiguous.
const match =
/^(?:[a-z][a-z+]*:\/\/)(?:[^/@]+@)?github\.com\/([^?#]+)(?:$|[?#])/i.exec(
repoUrl,
)
if (!match || !match[1]) {
return { user: '', project: '' }
}
const userAndRepo = match[1].split('/')
const user = userAndRepo[0] || ''
const rawProject = userAndRepo[1] ?? ''
const project = StringPrototypeEndsWith(rawProject, '.git')
? rawProject.slice(0, -4)
: rawProject
return { user, project }
}
/**
* Generate GitHub API URL for a tag reference.
*
* @example
* ;```typescript
* gitHubTagRefUrl('lodash', 'lodash', 'v4.17.21')
* // 'https://api.github.com/repos/lodash/lodash/git/ref/tags/v4.17.21'
* ```
*/
export function gitHubTagRefUrl(
user: string,
project: string,
tag: string,
): string {
return `https://api.github.com/repos/${user}/${project}/git/ref/tags/${tag}`
}
/**
* Generate GitHub tarball download URL for a commit SHA.
*
* @example
* ;```typescript
* gitHubTgzUrl('lodash', 'lodash', 'abc123')
* // 'https://github.com/lodash/lodash/archive/abc123.tar.gz'
* ```
*/
export function gitHubTgzUrl(
user: string,
project: string,
sha: string,
): string {
return `https://github.com/${user}/${project}/archive/${sha}.tar.gz`
}
/**
* Check if a package specifier is a GitHub tarball URL.
*
* @example
* ;```typescript
* isGitHubTgzSpec('https://github.com/user/repo/archive/abc123.tar.gz') // true
* isGitHubTgzSpec('lodash@4.17.21') // false
* ```
*/
export function isGitHubTgzSpec(spec: unknown, where?: string): boolean {
let parsedSpec: unknown
if (isPlainObject(spec)) {
parsedSpec = spec
} else {
// module is imported at the top
parsedSpec = npmPackageArg(spec as string, where)
}
const typedSpec = parsedSpec as {
type?: string | undefined
saveSpec?: string | undefined
}
return (
typedSpec.type === 'remote' && !!typedSpec.saveSpec?.endsWith('.tar.gz')
)
}
/**
* Check if a package specifier is a GitHub URL with committish.
*
* @example
* ;```typescript
* isGitHubUrlSpec('github:user/repo#v1.0.0') // true
* isGitHubUrlSpec('lodash@4.17.21') // false
* ```
*/
export function isGitHubUrlSpec(spec: unknown, where?: string): boolean {
let parsedSpec: unknown
if (isPlainObject(spec)) {
parsedSpec = spec
} else {
// module is imported at the top
parsedSpec = npmPackageArg(spec as string, where)
}
const typedSpec = parsedSpec as {
gitCommittish?: string | undefined
hosted?: { domain?: string | undefined } | undefined
type?: string | undefined
}
return (
typedSpec.type === 'git' &&
typedSpec.hosted?.domain === 'github.com' &&
isNonEmptyString(typedSpec.gitCommittish)
)
}
/**
* Slugify an npm package name into a hyphenated identifier suitable for
* User-Agent tokens, log namespaces, file paths, and other contexts where `@`
* and `/` are not welcome.
*
* @example
* ;```typescript
* pkgNameToSlug('@socketsecurity/lib') // 'socketsecurity-lib'
* pkgNameToSlug('@cyclonedx/cdxgen') // 'cyclonedx-cdxgen'
* pkgNameToSlug('lodash') // 'lodash'
* ```
*/
export function pkgNameToSlug(pkgName: string): string {
return StringPrototypeCharCodeAt(pkgName, 0) === 64 /* '@' */
? `${pkgName.slice(1).replace('/', '-')}`
: pkgName
}
/**
* Resolve full package name from a PURL object with custom delimiter.
*
* @example
* ;```typescript
* resolvePackageName({ name: 'core', namespace: '@babel' }) // '@babel/core'
* resolvePackageName({ name: 'lodash' }) // 'lodash'
* ```
*/
export function resolvePackageName(
purlObj: { name: string; namespace?: string | undefined },
delimiter: string = '/',
): string {
const { name, namespace } = purlObj
return `${namespace ? `${namespace}${delimiter}` : ''}${name}`
}
/**
* Convert npm package name to Socket registry format with delimiter.
*
* @example
* ;```typescript
* resolveRegistryPackageName('@babel/core') // 'babel__core'
* resolveRegistryPackageName('lodash') // 'lodash'
* ```
*/
export function resolveRegistryPackageName(pkgName: string): string {
const input = `pkg:npm/${pkgName}`
// Prefer node:smol-purl on the smol binary (C++-accelerated, with
// a 10 000-entry result cache); fall back to packageurl-js JS impl.
const smolPurl = getSmolPurl()
const purlObj = smolPurl
? smolPurl.parse(input)
: PackageURL.fromString(input)
return purlObj.namespace
? `${purlObj.namespace.slice(1)}${REGISTRY_SCOPE_DELIMITER}${purlObj.name}`
: pkgName
}