Skip to content

Commit 179d869

Browse files
GabrielCousinsevbch
authored andcommitted
refactor: replace request-sync with axios
1 parent 5d81c3f commit 179d869

File tree

7 files changed

+78
-243
lines changed

7 files changed

+78
-243
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@
156156
"@types/node": "20.2.5",
157157
"@types/simple-mock": "^0.8.6",
158158
"@types/tar": "^6.1.13",
159-
"@types/unzipper": "^0.10.10",
160159
"@types/vscode": "^1.81.0",
161160
"@typescript-eslint/eslint-plugin": "^5.59.8",
162161
"@typescript-eslint/parser": "^5.59.8",
@@ -169,7 +168,7 @@
169168
},
170169
"dependencies": {
171170
"adm-zip": "^0.5.16",
172-
"sync-request": "^6.1.0",
171+
"axios": "^1.9.0",
173172
"tar": "^7.4.3"
174173
}
175174
}

src/extension.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ function registerOpenViewsCommands(
6969
);
7070
}
7171

72-
export function activate(context: ExtensionContext) {
72+
export async function activate(context: ExtensionContext) {
7373
const outputChannel = window.createOutputChannel("GitGuardian");
74-
let configuration = getConfiguration(context, outputChannel);
74+
let configuration = await getConfiguration(context, outputChannel);
7575

7676
const ggshieldResolver = new GGShieldResolver(
7777
outputChannel,

src/lib/ggshield-configuration-utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ import { getGGShield } from "./ggshield-resolver-utils";
77
* Retrieve configuration from settings
88
* @returns {GGShieldConfiguration} from the extension settings
99
*/
10-
export function getConfiguration(
10+
export async function getConfiguration(
1111
context: ExtensionContext,
1212
outputChannel: OutputChannel
13-
): GGShieldConfiguration {
13+
): Promise<GGShieldConfiguration> {
1414
const config = workspace.getConfiguration("gitguardian");
1515

1616
const ggshieldPath: string | undefined = config.get("GGShieldPath");
1717
const apiUrl: string | undefined = config.get("apiUrl");
1818
const allowSelfSigned: boolean = config.get("allowSelfSigned", false);
1919

20-
const pathToGGShield: string = getGGShield(
20+
const pathToGGShield: string = await getGGShield(
2121
os.platform(),
2222
os.arch(),
2323
context,

src/lib/ggshield-resolver-utils.ts

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
import * as path from "path";
22
import * as fs from "fs";
33
import * as tar from "tar";
4+
import axios, { AxiosRequestConfig} from "axios";
5+
46
const AdmZip = require("adm-zip");
57
import { ExtensionContext, OutputChannel } from "vscode";
68

9+
const defaultRequestConfig = {
10+
headers: { "User-Agent": "GitGuardian-VSCode-Extension" },
11+
timeout: 30_000
12+
} satisfies AxiosRequestConfig;
13+
714
/**
815
* Get the absolute path to GGShield binary. If it doesn't exist, it will be installed.
916
* @param platform The platform of the user
@@ -12,12 +19,12 @@ import { ExtensionContext, OutputChannel } from "vscode";
1219
* @param outputChannel The output channel to use
1320
* @returns The absolute path to the GGShield binary
1421
*/
15-
export function getGGShield(
22+
export async function getGGShield(
1623
platform: NodeJS.Platform,
1724
arch: string,
1825
context: ExtensionContext,
1926
outputChannel: OutputChannel
20-
): string {
27+
): Promise<string> {
2128
const version = fs
2229
.readFileSync(path.join(context.extensionPath, "ggshield_version"), "utf8")
2330
.trim();
@@ -47,7 +54,7 @@ export function getGGShield(
4754
}
4855
fs.mkdirSync(ggshieldFolder);
4956
// install GGShield
50-
installGGShield(platform, arch, ggshieldFolder, version);
57+
await installGGShield(platform, arch, ggshieldFolder, version);
5158
outputChannel.appendLine(
5259
`Updated to GGShield v${version}. Checkout https://github.com/GitGuardian/ggshield for more info.`
5360
);
@@ -59,14 +66,16 @@ export function getGGShield(
5966
* Get the latest version of GGShield
6067
* @returns The latest version of GGShield
6168
*/
62-
export function getGGShieldLatestVersion(): string {
63-
const response = require("sync-request")(
64-
"GET",
69+
export async function getGGShieldLatestVersion(): Promise<string> {
70+
const { data } = await axios.get<{tag_name?: string}>(
6571
"https://api.github.com/repos/GitGuardian/ggshield/releases/latest",
66-
{ headers: { "User-Agent": "GitGuardian-VSCode-Extension" } }
72+
{
73+
...defaultRequestConfig,
74+
responseEncoding: 'utf8'
75+
}
6776
);
68-
const data = JSON.parse(response.getBody("utf8"));
69-
return data.tag_name?.replace(/^v/, "");
77+
78+
return data.tag_name?.replace(/^v/, "") ?? "";
7079
}
7180

7281
/**
@@ -119,12 +128,12 @@ export function computeGGShieldFolderName(
119128
* @param ggshieldFolder The folder of the GGShield binary
120129
* @param version The version of GGShield
121130
*/
122-
export function installGGShield(
131+
export async function installGGShield(
123132
platform: NodeJS.Platform,
124133
arch: string,
125134
ggshieldFolder: string,
126135
version: string
127-
): void {
136+
): Promise<void> {
128137
let extension: string = "";
129138
switch (platform) {
130139
case "win32":
@@ -144,7 +153,7 @@ export function installGGShield(
144153
version
145154
)}.${extension}`;
146155
const downloadUrl: string = `https://github.com/GitGuardian/ggshield/releases/download/v${version}/${fileName}`;
147-
downloadGGShieldFromGitHub(fileName, downloadUrl, ggshieldFolder);
156+
await downloadGGShieldFromGitHub(fileName, downloadUrl, ggshieldFolder);
148157
extractGGShieldBinary(path.join(ggshieldFolder, fileName), ggshieldFolder);
149158
}
150159

@@ -177,16 +186,18 @@ export function extractGGShieldBinary(
177186
* @param downloadUrl The URL of the GGShield binary
178187
* @param ggshieldFolder The folder of the GGShield binary
179188
*/
180-
function downloadGGShieldFromGitHub(
189+
async function downloadGGShieldFromGitHub(
181190
fileName: string,
182191
downloadUrl: string,
183192
ggshieldFolder: string
184-
): void {
193+
): Promise<void> {
185194
console.log(`Downloading GGShield from ${downloadUrl}`);
186-
const response = require("sync-request")("GET", downloadUrl, {
187-
headers: { "User-Agent": "GitGuardian-VSCode-Extension" },
195+
const { data } = await axios.get(downloadUrl, {
196+
...defaultRequestConfig,
197+
responseType: 'arraybuffer'
188198
});
189-
fs.writeFileSync(path.join(ggshieldFolder, fileName), response.getBody());
199+
200+
fs.writeFileSync(path.join(ggshieldFolder, fileName), data);
190201
console.log(
191202
`GGShield archive downloaded to ${path.join(ggshieldFolder, fileName)}`
192203
);

src/test/suite/lib/ggshield-configuration-utils.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ suite("getConfiguration", () => {
2727
simple.restore();
2828
});
2929

30-
test("Vscode settings are correctly read", () => {
30+
test("Vscode settings are correctly read", async () => {
3131
const context = {} as ExtensionContext;
3232
const outputChannel = window.createOutputChannel("GitGuardian");
3333
simple.mock(context, "asAbsolutePath").returnWith("");
@@ -42,7 +42,7 @@ suite("getConfiguration", () => {
4242
}
4343
},
4444
});
45-
const configuration = getConfiguration(context, outputChannel);
45+
const configuration = await getConfiguration(context, outputChannel);
4646

4747
// Assert both workspace.getConfiguration and GGShieldConfiguration constructor were called
4848
assert(

src/test/suite/lib/ggshield-resolver-utils.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ const AdmZip = require("adm-zip");
77
import * as getGGShieldUtils from "../../../lib/ggshield-resolver-utils";
88
import { ExtensionContext, window, OutputChannel } from "vscode";
99

10-
suite("getGGShield integration tests", () => {
10+
suite("getGGShield integration tests", async () => {
1111
let tempDir: string;
1212
let mockContext: ExtensionContext;
1313
let outputChannel: OutputChannel = window.createOutputChannel("GitGuardian");
1414
const platform = process.platform;
1515
const arch = process.arch;
16-
const latestVersion = getGGShieldUtils.getGGShieldLatestVersion();
16+
const latestVersion = await getGGShieldUtils.getGGShieldLatestVersion();
1717
let originalLog: (message?: any, ...optionalParams: any[]) => void;
1818
let output: string;
1919

@@ -66,7 +66,7 @@ suite("getGGShield integration tests", () => {
6666
);
6767
});
6868

69-
test("installs binary when it doesn't exist", () => {
69+
test("installs binary when it doesn't exist", async () => {
7070
const expectedBinaryPath: string = getGGShieldUtils.computeGGShieldPath(
7171
platform,
7272
arch,
@@ -75,7 +75,7 @@ suite("getGGShield integration tests", () => {
7575
);
7676
assert(!fs.existsSync(expectedBinaryPath));
7777

78-
const result = getGGShieldUtils.getGGShield(
78+
const result = await getGGShieldUtils.getGGShield(
7979
platform,
8080
arch,
8181
mockContext,
@@ -91,14 +91,14 @@ suite("getGGShield integration tests", () => {
9191
);
9292
});
9393

94-
test("updates binary when newer version set by ggshield_version file", () => {
94+
test("updates binary when newer version set by ggshield_version file", async () => {
9595
const oldBinaryPath: string = createFakeBinary(
9696
tempDir,
9797
platform,
9898
arch,
9999
"1.0.0"
100100
);
101-
const result = getGGShieldUtils.getGGShield(
101+
const result = await getGGShieldUtils.getGGShield(
102102
platform,
103103
arch,
104104
mockContext,

0 commit comments

Comments
 (0)