Skip to content

Commit 27588ad

Browse files
committed
Simplifying? Kinda?
1 parent 8e1c095 commit 27588ad

File tree

3 files changed

+14
-20
lines changed

3 files changed

+14
-20
lines changed

src/tree/images/imageChecker/OutdatedImageChecker.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,8 @@ export class OutdatedImageChecker {
107107
private async getLatestImageDigest(registry: ImageRegistry, repo: string, tag: string): Promise<string> {
108108
const manifestResponse = await httpRequest(`${registry.baseUrl}/${repo}/manifests/${tag}`, this.defaultRequestOptions, async (request) => {
109109
if (registry.signRequest) {
110-
return registry.signRequest(request, `repository:library/${repo}:pull`);
110+
await registry.signRequest(request, `repository:library/${repo}:pull`);
111111
}
112-
113-
return request;
114112
});
115113

116114
return manifestResponse.headers.get('docker-content-digest') as string;

src/tree/images/imageChecker/registries.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
import { ImageNameInfo } from '@microsoft/vscode-container-client';
77
import { URL } from 'url';
88
import { ociClientId } from '../../../constants';
9-
import { HttpErrorResponse, IOAuthContext, RequestLike, RequestOptionsLike, bearerAuthHeader, getWwwAuthenticateContext, httpRequest } from '../../../utils/httpRequest';
9+
import { HttpErrorResponse, IOAuthContext, RequestOptionsLike, bearerAuthHeader, getWwwAuthenticateContext, httpRequest } from '../../../utils/httpRequest';
1010
import { NormalizedImageNameInfo } from '../NormalizedImageNameInfo';
1111

1212
export interface ImageRegistry {
1313
isMatch: (imageNameInfo: ImageNameInfo) => boolean;
1414
baseUrl: string;
15-
signRequest?(request: RequestLike, scope: string): Promise<RequestLike>;
15+
signRequest?(request: RequestOptionsLike, scope: string): Promise<void>;
1616
}
1717

1818
let dockerHubAuthContext: IOAuthContext | undefined;
@@ -24,7 +24,7 @@ export const registries: ImageRegistry[] = [
2424
return !!imageNameInfo.originalName && normalizedImageNameInfo.normalizedRegistry === 'docker.io' && normalizedImageNameInfo.normalizedNamespace === 'library';
2525
},
2626
baseUrl: 'https://registry-1.docker.io/v2/library',
27-
signRequest: async (request: RequestLike, scope: string): Promise<RequestLike> => {
27+
signRequest: async (request: RequestOptionsLike, scope: string): Promise<void> => {
2828
if (!dockerHubAuthContext) {
2929
try {
3030
const options: RequestOptionsLike = {
@@ -59,8 +59,7 @@ export const registries: ImageRegistry[] = [
5959
const tokenResponse = await httpRequest<{ token: string }>(url.toString(), authRequestOptions);
6060
const token = (await tokenResponse.json()).token;
6161

62-
request.headers.set('Authorization', bearerAuthHeader(token));
63-
return request;
62+
request.headers['Authorization'] = bearerAuthHeader(token);
6463
}
6564
},
6665
{

src/utils/httpRequest.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,25 @@ export const enum ErrorHandling {
1616
export async function httpRequest<T>(
1717
url: string,
1818
options?: RequestOptionsLike,
19-
signRequest?: (request: RequestLike) => Promise<RequestLike>,
19+
signRequest?: (request: RequestOptionsLike) => Promise<void>,
2020
errorHandling: ErrorHandling = ErrorHandling.ThrowOnError
2121
): Promise<HttpResponse<T>> {
22-
const requestOptions: RequestInit = options;
22+
const requestOptions: RequestInit = { ...options };
2323
if (options?.form) {
2424
// URLSearchParams is a silly way to say "it's form data"
2525
requestOptions.body = new URLSearchParams(options.form);
2626
}
2727

28-
let request = new Request(url, requestOptions ?? {});
28+
if (!!requestOptions.body && requestOptions.duplex === undefined) {
29+
// node-fetch requires the "duplex" option to be set to "half" in order to send a body
30+
requestOptions.duplex = 'half';
31+
}
2932

3033
if (signRequest) {
31-
request = await signRequest(request) as Request;
34+
await signRequest(requestOptions as RequestOptionsLike);
3235
}
3336

34-
const response = await fetch(request);
37+
const response = await fetch(url, requestOptions);
3538

3639
if (errorHandling === ErrorHandling.ReturnErrorResponse || response.ok) {
3740
return new HttpResponse(response, url);
@@ -105,18 +108,12 @@ export const enum HttpStatusCode {
105108
}
106109

107110
export interface RequestOptionsLike {
108-
headers?: { [key: string]: string };
111+
headers?: { [key: string]: readonly string[] | string };
109112
method?: RequestMethod; // This is an enum type because it enforces the above valid options on callers (which do not directly use node-fetch's Request object)
110113
form?: { [key: string]: string };
111114
body?: string;
112115
}
113116

114-
export interface RequestLike {
115-
url: string;
116-
headers: HeadersLike;
117-
method: string; // This is a string because node-fetch's Request defines it as such
118-
}
119-
120117
export interface HeadersLike {
121118
get(header: string): string | string[];
122119
set(header: string, value: string): void;

0 commit comments

Comments
 (0)