Merge pull request #436 from Zagrios/bugfix/downloading-models-with-bsm-no-longer-works

[bugfix] fix downloading models not working
This commit is contained in:
MathieuG-P
2024-03-13 14:55:42 +01:00
committed by GitHub
2 changed files with 28 additions and 9 deletions
+2 -2
View File
@@ -1,12 +1,12 @@
{
"name": "bs-manager",
"version": "1.4.4",
"version": "1.4.6",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "bs-manager",
"version": "1.4.4",
"version": "1.4.6",
"hasInstallScript": true,
"license": "MIT",
"dependencies": {
+26 -7
View File
@@ -1,9 +1,11 @@
import { Agent, get } from "https";
import { Agent, RequestOptions, get } from "https";
import { createWriteStream, unlink } from "fs";
import { Progression } from "main/helpers/fs.helpers";
import { Observable, shareReplay, tap } from "rxjs";
import log from "electron-log";
import fetch, { RequestInfo, RequestInit } from "node-fetch";
import { app } from "electron";
import os from "os";
export class RequestService {
private static instance: RequestService;
@@ -15,16 +17,33 @@ export class RequestService {
return RequestService.instance;
}
private constructor() {}
private readonly defaultRequestInit: RequestInit;
private get ipv4Agent(){
return new Agent({ family: 4 });
private constructor() {
this.defaultRequestInit = {
headers: {
"User-Agent": `BSManager/${app.getVersion()} (${os.type()} ${os.release()})`
},
agent: new Agent({ family: 4 }),
};
}
private getInitWithOptions(options?: RequestInit): RequestInit {
return { ...this.defaultRequestInit, ...(options || {}) };
}
private requestOptionsFromDefaultInit(): RequestOptions {
return {
headers: this.defaultRequestInit.headers as Record<string, string>,
agent: this.defaultRequestInit.agent as Agent,
};
}
public async getJSON<T = unknown>(url: RequestInfo, options?: RequestInit): Promise<T> {
try {
const response = await fetch(url, {...options, agent: this.ipv4Agent});
const response = await fetch(url, this.getInitWithOptions(options));
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status} ${url}`);
@@ -50,7 +69,7 @@ export class RequestService {
});
file.on("error", err => unlink(dest, () => subscriber.error(err)));
const req = get(url, { agent: this.ipv4Agent }, res => {
const req = get(url, this.requestOptionsFromDefaultInit(), res => {
progress.total = parseInt(res.headers?.["content-length"] || "0", 10);
res.on("data", chunk => {
@@ -77,7 +96,7 @@ export class RequestService {
const allChunks: Buffer[] = [];
const req = get(url, { agent: this.ipv4Agent }, res => {
const req = get(url, this.requestOptionsFromDefaultInit(), res => {
progress.total = parseInt(res.headers?.["content-length"] || "0", 10);
res.on("data", chunk => {