Skip to content

Commit

Permalink
Use Node 18, drop node-fetch (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
bwateratmsft authored Nov 14, 2023
1 parent 5a05e1e commit 24e2d35
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 241 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
16.17
18.15
236 changes: 29 additions & 207 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "@microsoft/vscode-docker-extensibility",
"author": "Microsoft Corporation",
"version": "0.0.0",
"workspaces": [
"packages/vscode-container-client",
"packages/vscode-docker-registries"
Expand Down
4 changes: 2 additions & 2 deletions packages/vscode-container-client/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@microsoft/vscode-container-client",
"author": "Microsoft Corporation",
"version": "0.1.1",
"version": "0.1.2",
"description": "Extensibility model for implementing container runtime providers (shared by VS and VS Code)",
"license": "See LICENSE in the project root for license information.",
"repository": {
Expand Down Expand Up @@ -29,7 +29,7 @@
"devDependencies": {
"@types/chai": "^4.3.0",
"@types/mocha": "^9.1.0",
"@types/node": "16.x",
"@types/node": "18.x",
"@types/vscode": "1.63.1",
"@typescript-eslint/eslint-plugin": "^5.10.2",
"@typescript-eslint/parser": "^5.10.2",
Expand Down
6 changes: 2 additions & 4 deletions packages/vscode-docker-registries/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@microsoft/vscode-docker-registries",
"author": "Microsoft Corporation",
"version": "0.1.7",
"version": "0.1.8",
"description": "Extensibility model for contributing registry providers to the Docker extension for Visual Studio Code",
"license": "See LICENSE in the project root for license information.",
"repository": {
Expand Down Expand Up @@ -30,8 +30,7 @@
"@types/chai": "^4.2.14",
"@types/chai-as-promised": "^7.1.3",
"@types/mocha": "^10.0.0",
"@types/node": "16.x",
"@types/node-fetch": "^2.5.12",
"@types/node": "18.x",
"@types/vscode": "1.75.0",
"@typescript-eslint/eslint-plugin": "^5.3.1",
"@typescript-eslint/parser": "^5.3.1",
Expand All @@ -42,7 +41,6 @@
"typescript": "^5.0.4"
},
"dependencies": {
"node-fetch": "^2.6.11",
"dayjs": "^1.11.7"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export abstract class RegistryV2DataProvider extends CommonRegistryDataProvider
public async getImageDigest(item: CommonTag): Promise<string> {
const response = await this.getManifestV2(item);

const digest = response.headers['docker-content-digest'];
const digest = response.headers.get('docker-content-digest');
if (!digest) {
throw new Error('Could not find digest');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import * as vscode from 'vscode';
import { AuthenticationProvider } from '../../contracts/AuthenticationProvider';
import { isBasicOAuthProvider } from '../../auth/BasicOAuthProvider';
import { RequestLike, httpRequest } from '../../utils/httpRequest';
import { HeadersLike, RequestLike, httpRequest } from '../../utils/httpRequest';
import { HttpErrorResponse } from '../../utils/errors';

export interface RegistryV2RequestOptions {
Expand All @@ -25,7 +25,7 @@ export interface RegistryV2Response<T> {
statusText: string;
succeeded: boolean;
uri: vscode.Uri;
headers: Record<string, string>;
headers: HeadersLike;
body: T | undefined;
}

Expand All @@ -34,8 +34,8 @@ export async function registryV2Request<T>(options: RegistryV2RequestOptions): P
const result = await registryV2RequestInternal<T>({ ...options, throwOnFailure: false });
if (result.succeeded) {
return result;
} else if (result.status === 401 && result.headers['www-authenticate']) {
options.authenticationProvider.fallback(result.headers['www-authenticate']);
} else if (result.status === 401 && result.headers.get('www-authenticate')) {
options.authenticationProvider.fallback(result.headers.get('www-authenticate') as string);
} else {
throw new HttpErrorResponse(options.requestUri.toString(), result.status, result.statusText);
}
Expand All @@ -48,17 +48,17 @@ async function registryV2RequestInternal<T>(options: RegistryV2RequestOptions):
const query = new URLSearchParams(options.query);
const uri = options.requestUri.with({ query: query.toString() });

const auth = await options.authenticationProvider.getSession(options.scopes, options.sessionOptions);

const request: RequestLike = {
headers: {
accept: 'application/json',
Authorization: `${auth.type} ${auth.accessToken}`,
...options.headers
},
method: options.method,
};

const auth = await options.authenticationProvider.getSession(options.scopes, options.sessionOptions);
request.headers['Authorization'] = `${auth.type} ${auth.accessToken}`;

const response = await httpRequest(uri.toString(true), request, options.throwOnFailure);

return {
Expand All @@ -67,6 +67,6 @@ async function registryV2RequestInternal<T>(options: RegistryV2RequestOptions):
succeeded: response.succeeded,
uri: uri,
headers: response.headers,
body: response.succeeded && (parseInt(response.headers['content-length']) || response.headers['transfer-encoding'] === 'chunked') ? await response.json() as T : undefined,
body: response.succeeded && (parseInt(response.headers.get('content-length') ?? '0') || response.headers.get('transfer-encoding') === 'chunked') ? await response.json() as T : undefined,
};
}
28 changes: 10 additions & 18 deletions packages/vscode-docker-registries/src/utils/httpRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
*--------------------------------------------------------------------------------------------*/

import * as vscode from 'vscode';
import { Request, RequestInit, Response, ResponseInit, default as fetch } from 'node-fetch';
import { HttpErrorResponse, UnauthorizedError } from './errors';

export function getNextLinkFromHeaders(headers: HeadersLike, baseUrl: vscode.Uri): vscode.Uri | undefined {
const linkHeader: string | undefined = headers['link'];
export function getNextLinkFromHeaders(headers: Headers, baseUrl: vscode.Uri): vscode.Uri | undefined {
const linkHeader = headers.get('link');
if (!linkHeader) {
return undefined;
}
Expand All @@ -23,43 +22,36 @@ export function getNextLinkFromHeaders(headers: HeadersLike, baseUrl: vscode.Uri
return nextLinkUri;
}

export type HeadersLike = Record<string, string>;
export type HeadersLike = Headers;

export type RequestLike = RequestInit & {
headers: HeadersLike;
headers: Record<string, string>;
};

export type ResponseLike<T> = ResponseInit & {
export interface ResponseLike<T> extends Response {
headers: HeadersLike;
status: number;
statusText: string;
succeeded: boolean;
json: () => Promise<T>;
};
}

export async function httpRequest<T>(url: string, request: RequestLike, throwOnFailure: boolean = true): Promise<ResponseLike<T>> {
const fetchRequest = new Request(url, request);
const response: Response = await fetch(fetchRequest);

const headers: HeadersLike = {};
for (const [header, value] of response.headers.entries()) {
headers[header] = value;
}

const succeeded = response.status >= 200 && response.status < 300;

if (throwOnFailure && response.status === 401) {
throw new UnauthorizedError(vscode.l10n.t('Request to \'{0}\' failed with response 401: Unauthorized', url));
} else if (throwOnFailure && !succeeded) {
} else if (throwOnFailure && !response.ok) {
throw new HttpErrorResponse(url, response.status, response.statusText);
}

return {
...response,
headers: headers,
headers: response.headers, // These are getters so we need to call them to get the values
status: response.status,
statusText: response.statusText,
succeeded: succeeded,
json: response.json.bind(response),
succeeded: response.ok,
json: response.json.bind(response) as () => Promise<T>,
};
}

0 comments on commit 24e2d35

Please sign in to comment.