/
index.ts
187 lines (160 loc) · 4.98 KB
/
index.ts
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
import crypto from 'crypto';
import { logger } from '../../logger';
import * as globalCache from '../../util/cache/global';
import { Http } from '../../util/http';
import { GithubHttp } from '../../util/http/github';
import { GetReleasesConfig, ReleaseResult } from '../common';
export const id = 'pod';
export const defaultRegistryUrls = ['https://cdn.cocoapods.org'];
const cacheNamespace = `datasource-${id}`;
const cacheMinutes = 30;
const githubHttp = new GithubHttp();
const http = new Http(id);
function shardParts(lookupName: string): string[] {
return crypto
.createHash('md5')
.update(lookupName)
.digest('hex')
.slice(0, 3)
.split('');
}
function releasesGithubUrl(
lookupName: string,
opts: { account: string; repo: string; useShard: boolean }
): string {
const { useShard, account, repo } = opts;
const prefix = 'https://api.github.com/repos';
const shard = shardParts(lookupName).join('/');
const suffix = useShard ? `${shard}/${lookupName}` : lookupName;
return `${prefix}/${account}/${repo}/contents/Specs/${suffix}`;
}
function handleError(lookupName: string, err: Error): void {
const errorData = { lookupName, err };
if (
err.statusCode === 429 ||
(err.statusCode >= 500 && err.statusCode < 600)
) {
logger.warn({ lookupName, err }, `CocoaPods registry failure`);
throw new Error('registry-failure');
}
if (err.statusCode === 401) {
logger.debug(errorData, 'Authorization error');
} else if (err.statusCode === 404) {
logger.debug(errorData, 'Package lookup error');
} else {
logger.warn(errorData, 'CocoaPods lookup failure: Unknown error');
}
}
async function requestCDN(
url: string,
lookupName: string
): Promise<string | null> {
try {
const resp = await http.get(url);
if (resp && resp.body) {
return resp.body;
}
} catch (err) {
handleError(lookupName, err);
}
return null;
}
async function requestGithub<T = unknown>(
url: string,
lookupName: string
): Promise<T | null> {
try {
const resp = await githubHttp.getJson<T>(url);
if (resp && resp.body) {
return resp.body;
}
} catch (err) {
handleError(lookupName, err);
}
return null;
}
const githubRegex = /^https:\/\/github\.com\/(?<account>[^/]+)\/(?<repo>[^/]+?)(\.git|\/.*)?$/;
async function getReleasesFromGithub(
lookupName: string,
registryUrl: string,
useShard = false
): Promise<ReleaseResult | null> {
const match = githubRegex.exec(registryUrl);
const { account, repo } = (match && match.groups) || {};
const opts = { account, repo, useShard };
const url = releasesGithubUrl(lookupName, opts);
const resp = await requestGithub<{ name: string }[]>(url, lookupName);
if (resp) {
const releases = resp.map(({ name }) => ({ version: name }));
return { releases };
}
if (!useShard) {
return getReleasesFromGithub(lookupName, registryUrl, true);
}
return null;
}
function releasesCDNUrl(lookupName: string, registryUrl: string): string {
const shard = shardParts(lookupName).join('_');
return `${registryUrl}/all_pods_versions_${shard}.txt`;
}
async function getReleasesFromCDN(
lookupName: string,
registryUrl: string
): Promise<ReleaseResult | null> {
const url = releasesCDNUrl(lookupName, registryUrl);
const resp = await requestCDN(url, lookupName);
if (resp) {
const lines = resp.split('\n');
for (let idx = 0; idx < lines.length; idx += 1) {
const line = lines[idx];
const [name, ...versions] = line.split('/');
if (name === lookupName.replace(/\/.*$/, '')) {
const releases = versions.map((version) => ({ version }));
return { releases };
}
}
}
return null;
}
function isDefaultRepo(url: string): boolean {
const match = githubRegex.exec(url);
if (match) {
const { account, repo } = match.groups || {};
return (
account.toLowerCase() === 'cocoapods' && repo.toLowerCase() === 'specs'
); // https://github.com/CocoaPods/Specs.git
}
return false;
}
export async function getReleases({
lookupName,
registryUrls,
}: GetReleasesConfig): Promise<ReleaseResult | null> {
const podName = lookupName.replace(/\/.*$/, '');
const cachedResult = await globalCache.get<ReleaseResult>(
cacheNamespace,
podName
);
/* istanbul ignore next line */
if (cachedResult) {
logger.debug(`CocoaPods: Return cached result for ${podName}`);
return cachedResult;
}
let result: ReleaseResult | null = null;
for (let idx = 0; !result && idx < registryUrls.length; idx += 1) {
let registryUrl = registryUrls[idx].replace(/\/+$/, '');
// In order to not abuse github API limits, query CDN instead
if (isDefaultRepo(registryUrl)) {
[registryUrl] = defaultRegistryUrls;
}
if (githubRegex.exec(registryUrl)) {
result = await getReleasesFromGithub(podName, registryUrl);
} else {
result = await getReleasesFromCDN(podName, registryUrl);
}
}
if (result) {
await globalCache.set(cacheNamespace, podName, result, cacheMinutes);
}
return result;
}