-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
index.ts
122 lines (106 loc) · 3.82 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
import { Marshal } from '@qnighy/marshal';
import type { ZodError } from 'zod';
import { logger } from '../../../logger';
import { HttpError } from '../../../util/http';
import { AsyncResult, Result } from '../../../util/result';
import { getQueryString, joinUrlParts, parseUrl } from '../../../util/url';
import * as rubyVersioning from '../../versioning/ruby';
import { Datasource } from '../datasource';
import type { GetReleasesConfig, ReleaseResult } from '../types';
import { getV1Releases } from './common';
import { RubygemsHttp } from './http';
import { MetadataCache } from './metadata-cache';
import { GemInfo, MarshalledVersionInfo } from './schema';
import { VersionsEndpointCache } from './versions-endpoint-cache';
function unlessServerSide<T, E>(
err: E,
cb: () => AsyncResult<T, E>
): AsyncResult<T, E> {
if (err instanceof HttpError && err.response?.statusCode) {
const code = err.response.statusCode;
if (code >= 500 && code <= 599) {
return AsyncResult.err(err);
}
}
return cb();
}
export class RubyGemsDatasource extends Datasource {
static readonly id = 'rubygems';
private metadataCache: MetadataCache;
constructor() {
super(RubyGemsDatasource.id);
this.http = new RubygemsHttp(RubyGemsDatasource.id);
this.versionsEndpointCache = new VersionsEndpointCache(this.http);
this.metadataCache = new MetadataCache(this.http);
}
override readonly defaultRegistryUrls = ['https://rubygems.org'];
override readonly defaultVersioning = rubyVersioning.id;
override readonly registryStrategy = 'hunt';
private readonly versionsEndpointCache: VersionsEndpointCache;
async getReleases({
packageName,
registryUrl,
}: GetReleasesConfig): Promise<ReleaseResult | null> {
// istanbul ignore if
if (!registryUrl) {
return null;
}
const registryHostname = parseUrl(registryUrl)?.hostname;
let result: AsyncResult<ReleaseResult, Error | string>;
if (registryHostname === 'rubygems.org') {
result = Result.wrap(
this.versionsEndpointCache.getVersions(registryUrl, packageName)
).transform((versions) =>
this.metadataCache.getRelease(registryUrl, packageName, versions)
);
} else if (
registryHostname === 'rubygems.pkg.github.com' ||
registryHostname === 'gitlab.com'
) {
result = this.getReleasesViaDeprecatedAPI(registryUrl, packageName);
} else {
result = getV1Releases(this.http, registryUrl, packageName)
.catch((err) =>
unlessServerSide(err, () =>
this.getReleasesViaInfoEndpoint(registryUrl, packageName)
)
)
.catch((err) =>
unlessServerSide(err, () =>
this.getReleasesViaDeprecatedAPI(registryUrl, packageName)
)
);
}
const { val, err } = await result.unwrap();
if (val) {
return val;
}
if (err instanceof Error) {
this.handleGenericErrors(err);
}
logger.debug({ packageName, registryUrl }, `Rubygems fetch error: ${err}`);
return null;
}
private getReleasesViaInfoEndpoint(
registryUrl: string,
packageName: string
): AsyncResult<ReleaseResult, Error | ZodError> {
const url = joinUrlParts(registryUrl, '/info', packageName);
return Result.wrap(this.http.get(url)).transform(({ body }) =>
GemInfo.safeParse(body)
);
}
private getReleasesViaDeprecatedAPI(
registryUrl: string,
packageName: string
): AsyncResult<ReleaseResult, Error | ZodError> {
const path = joinUrlParts(registryUrl, `/api/v1/dependencies`);
const query = getQueryString({ gems: packageName });
const url = `${path}?${query}`;
const bufPromise = this.http.getBuffer(url);
return Result.wrap(bufPromise).transform(({ body }) => {
const data = Marshal.parse(body);
return MarshalledVersionInfo.safeParse(data);
});
}
}