Skip to content

Commit 4d0bec1

Browse files
committed
Rename types
1 parent 0387f55 commit 4d0bec1

File tree

6 files changed

+55
-35
lines changed

6 files changed

+55
-35
lines changed

lib/start-proxy-action.js

Lines changed: 7 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/start-proxy-action.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import {
1919
ProxyInfo,
2020
sendFailedStatusReport,
2121
sendSuccessStatusReport,
22-
ValidCredential,
23-
ValidRegistry,
22+
Credential,
23+
Registry,
2424
} from "./start-proxy";
2525
import { checkConnections } from "./start-proxy/reachability";
2626
import { ActionName, sendUnhandledErrorStatusReport } from "./status-report";
@@ -40,7 +40,8 @@ type BasicAuthCredentials = {
4040
};
4141

4242
type ProxyConfig = {
43-
all_credentials: ValidCredential[];
43+
/** The validated configurations for the proxy. */
44+
all_credentials: Credential[];
4445
ca: CertificateAuthority;
4546
proxy_auth?: BasicAuthCredentials;
4647
};
@@ -243,7 +244,7 @@ async function startProxy(
243244
core.setOutput("proxy_port", port.toString());
244245
core.setOutput("proxy_ca_certificate", config.ca.cert);
245246

246-
const registry_urls: ValidRegistry[] = config.all_credentials
247+
const registry_urls: Registry[] = config.all_credentials
247248
.filter((credential) => credential.url !== undefined)
248249
.map((credential) => ({
249250
type: credential.type,

src/start-proxy.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ import { KnownLanguage } from "./languages";
1515
import { Logger } from "./logging";
1616
import {
1717
Address,
18-
Credential,
18+
RawCredential,
1919
Registry,
20-
ValidCredential,
20+
Credential,
2121
} from "./start-proxy/types";
2222
import {
2323
ActionName,
@@ -234,7 +234,7 @@ const LANGUAGE_TO_REGISTRY_TYPE: Partial<Record<KnownLanguage, string[]>> = {
234234
*
235235
* @throws A `ConfigurationError` if the `Registry` value contains neither a `url` or `host` field.
236236
*/
237-
function getRegistryAddress(registry: Registry): Address {
237+
function getRegistryAddress(registry: Partial<Registry>): Address {
238238
if (isDefined(registry.url)) {
239239
return {
240240
url: registry.url,
@@ -261,7 +261,7 @@ export function getCredentials(
261261
registrySecrets: string | undefined,
262262
registriesCredentials: string | undefined,
263263
language: KnownLanguage | undefined,
264-
): ValidCredential[] {
264+
): Credential[] {
265265
const registryTypeForLanguage = language
266266
? LANGUAGE_TO_REGISTRY_TYPE[language]
267267
: undefined;
@@ -279,9 +279,9 @@ export function getCredentials(
279279
}
280280

281281
// Parse and validate the credentials
282-
let parsed: Credential[];
282+
let parsed: RawCredential[];
283283
try {
284-
parsed = JSON.parse(credentialsStr) as Credential[];
284+
parsed = JSON.parse(credentialsStr) as RawCredential[];
285285
} catch {
286286
// Don't log the error since it might contain sensitive information.
287287
logger.error("Failed to parse the credentials data.");
@@ -295,12 +295,17 @@ export function getCredentials(
295295
);
296296
}
297297

298-
const out: ValidCredential[] = [];
298+
const out: Credential[] = [];
299299
for (const e of parsed) {
300300
if (e === null || typeof e !== "object") {
301301
throw new ConfigurationError("Invalid credentials - must be an object");
302302
}
303303

304+
// The configuration must have a type.
305+
if (!isDefined(e.type)) {
306+
throw new ConfigurationError("Invalid credentials - must have a type");
307+
}
308+
304309
// Mask credentials to reduce chance of accidental leakage in logs.
305310
if (isDefined(e.password)) {
306311
core.setSecret(e.password);
@@ -450,7 +455,7 @@ export async function getDownloadUrl(
450455
*
451456
* @param c The credential to convert to a string.
452457
*/
453-
export function credentialToStr(c: Credential): string {
458+
export function credentialToStr(c: RawCredential): string {
454459
return `Type: ${c.type}; Host: ${c.host}; Url: ${c.url} Username: ${
455460
c.username
456461
}; Password: ${c.password !== undefined}; Token: ${c.token !== undefined}`;

src/start-proxy/reachability.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,22 @@ import {
1111
ReachabilityBackend,
1212
ReachabilityError,
1313
} from "./reachability";
14-
import { ProxyInfo, ValidRegistry } from "./types";
14+
import { ProxyInfo, Registry } from "./types";
1515

1616
setupTests(test);
1717

1818
class MockReachabilityBackend implements ReachabilityBackend {
19-
public async checkConnection(_registry: ValidRegistry): Promise<number> {
19+
public async checkConnection(_registry: Registry): Promise<number> {
2020
return 200;
2121
}
2222
}
2323

24-
const mavenRegistry: ValidRegistry = {
24+
const mavenRegistry: Registry = {
2525
type: "maven_registry",
2626
url: "https://repo.maven.apache.org/maven2/",
2727
};
2828

29-
const nugetFeed: ValidRegistry = {
29+
const nugetFeed: Registry = {
3030
type: "nuget_feed",
3131
url: "https://api.nuget.org/v3/index.json",
3232
};

src/start-proxy/reachability.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { HttpsProxyAgent } from "https-proxy-agent";
55
import { Logger } from "../logging";
66
import { getErrorMessage } from "../util";
77

8-
import { getAddressString, ProxyInfo, ValidRegistry } from "./types";
8+
import { getAddressString, ProxyInfo, Registry } from "./types";
99

1010
export class ReachabilityError extends Error {
1111
constructor(public readonly statusCode?: number | undefined) {
@@ -25,7 +25,7 @@ export interface ReachabilityBackend {
2525
* @param registry The registry to try and reach.
2626
* @returns The successful status code (in the `<400` range).
2727
*/
28-
checkConnection: (registry: ValidRegistry) => Promise<number>;
28+
checkConnection: (registry: Registry) => Promise<number>;
2929
}
3030

3131
class NetworkReachabilityBackend implements ReachabilityBackend {
@@ -38,7 +38,7 @@ class NetworkReachabilityBackend implements ReachabilityBackend {
3838
this.agent = new HttpsProxyAgent(`http://${proxy.host}:${proxy.port}`);
3939
}
4040

41-
public async checkConnection(registry: ValidRegistry): Promise<number> {
41+
public async checkConnection(registry: Registry): Promise<number> {
4242
return new Promise((resolve, reject) => {
4343
const req = https.request(
4444
getAddressString(registry),
@@ -78,8 +78,8 @@ export async function checkConnections(
7878
logger: Logger,
7979
proxy: ProxyInfo,
8080
backend?: ReachabilityBackend,
81-
): Promise<Set<ValidRegistry>> {
82-
const result: Set<ValidRegistry> = new Set();
81+
): Promise<Set<Registry>> {
82+
const result: Set<Registry> = new Set();
8383

8484
// Don't do anything if there are no registries.
8585
if (proxy.registries.length === 0) return result;

src/start-proxy/types.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
1-
export interface Credential extends Registry {
1+
/**
2+
* After parsing configurations from JSON, we don't know whether all the keys we expect are
3+
* present or not. This type is used to represent such values, which we expect to be
4+
* `Credential` values, but haven't validated yet.
5+
*/
6+
export type RawCredential = Partial<Credential>;
7+
8+
/**
9+
* A package registry configuration includes identifying information as well as
10+
* authentication credentials.
11+
*/
12+
export type Credential = {
13+
/** The username needed to authenticate to the package registry, if any. */
214
username?: string;
15+
/** The password needed to authenticate to the package registry, if any. */
316
password?: string;
17+
/** The token needed to authenticate to the package registry, if any. */
418
token?: string;
5-
}
19+
} & Registry;
620

7-
export interface Registry {
21+
/** A package registry is identified by its type and address. */
22+
export type Registry = {
23+
/** The type of the package registry. */
824
type: string;
9-
host?: string;
10-
url?: string;
11-
}
25+
} & Address;
1226

1327
// If a registry has an `url`, then that takes precedence over the `host` which may or may
1428
// not be defined.
@@ -39,12 +53,9 @@ export function getAddressString(address: Address): string {
3953
}
4054
}
4155

42-
export type ValidRegistry<T extends Registry = Registry> = T & Address;
43-
export type ValidCredential = ValidRegistry<Credential>;
44-
4556
export interface ProxyInfo {
4657
host: string;
4758
port: number;
4859
cert: string;
49-
registries: ValidRegistry[];
60+
registries: Registry[];
5061
}

0 commit comments

Comments
 (0)