Skip to content

Commit

Permalink
remove unnecessary request handling
Browse files Browse the repository at this point in the history
  • Loading branch information
thewahome committed Feb 5, 2025
1 parent 1300386 commit 475ba2b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 26 deletions.
30 changes: 17 additions & 13 deletions vscode/microsoft-kiota/src/kiotaInterop/install.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import AdmZip from 'adm-zip';
import appdataPath from 'appdata-path';
import { https } from "follow-redirects";
import * as fs from 'fs';
import * as https from 'https';
import * as path from 'path';
import appdataPath from 'appdata-path';

import runtimeJson from './runtime.json';

Expand Down Expand Up @@ -104,28 +104,32 @@ function unzipFile(zipFilePath: string, destinationPath: string) {
zip.extractAllTo(destinationPath, true);
}

function downloadFileFromUrl(url: string, destinationPath: string) {
function downloadFileFromUrl(url: string, destinationPath: string): Promise<void> {
return new Promise((resolve, reject) => {
const file = fs.createWriteStream(destinationPath);
const request = https.get(url, (response) => {
if (response.statusCode && response.statusCode >= 300 && response.statusCode < 400 && response.headers.location) {
resolve(downloadFileFromUrl(response.headers.location, destinationPath));
} else if (response.statusCode && response.statusCode >= 200 && response.statusCode < 300) {
const filePath = fs.createWriteStream(destinationPath);
response.pipe(filePath);
filePath.on('finish', () => {
filePath.close();
resolve(undefined);
});
filePath.on('error', (err) => {
fs.unlink(destinationPath, () => reject(err));
response.pipe(file);
file.on("finish", () => {
file.close((err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
} else {
reject(new Error(`Failed to download file: ${response.statusCode}`));
}
});
request.on('error', (err) => {
reject(err);

request.on("error", (err) => {
fs.unlink(destinationPath, () => reject(err));
});

request.end();
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
import { getKiotaVersion } from "../getKiotaVersion";
import { https } from "follow-redirects";

jest.setTimeout(60000);

describe("getKiotaVersion", () => {
let request: ReturnType<typeof https.get> | undefined;

beforeEach(() => {
request = undefined;
});

afterEach(() => {
if (request) {
request.destroy();
}
});

test("should return version when successful", async () => {
const version = await getKiotaVersion();
expect(version).toBeDefined();
Expand Down

0 comments on commit 475ba2b

Please sign in to comment.