Skip to content

Commit 992d500

Browse files
committed
TODO: Axios uses xhr instead of http backend. Very unstable hacky solution here as backup.
1 parent fafb6fa commit 992d500

6 files changed

Lines changed: 53 additions & 163 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"lint": "eslint .",
3838
"napi-build": "cd napi && npm run build && node ../scripts/moveNapi.js",
3939
"nsfw-build": "cd node_modules/nsfw && node-gyp build && cd ../.. && node scripts/moveNsfw.js",
40-
"start-electron:dev": "cross-env-shell NODE_ENV=development APP_TYPE=electron BROWSER=none wait-on http://localhost:8080 -i 2000 && electron .",
40+
"start-electron:dev": "cross-env-shell NODE_ENV=development APP_TYPE=electron wait-on http://localhost:8080 -i 2000 && electron .",
4141
"start-electron:prod": "electron .",
4242
"start-web:dev": "cross-env-shell REACT_APP_RELEASE_TYPE=setup webpack serve --hot --mode development",
4343
"build-web:setup": "cross-env-shell REACT_APP_RELEASE_TYPE=setup webpack --mode production",

public/electron.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ let tray;
3535
let watcher;
3636

3737
const discordRPC = require('./discordRPC');
38+
const { downloadInstanceFiles, downloadFile } = require('./downloadFile.js');
3839

3940
const gotTheLock = app.requestSingleInstanceLock();
4041

@@ -901,6 +902,24 @@ ipcMain.handle('download-optedout-mods', async (e, { mods, instancePath }) => {
901902
}
902903
});
903904

905+
ipcMain.handle(
906+
'download-instance-files',
907+
async (e, arr, updatePercentage, threads) => {
908+
const back = downloadInstanceFiles(
909+
mainWindow,
910+
arr,
911+
updatePercentage,
912+
threads
913+
);
914+
log.log('Back:', back);
915+
return back;
916+
}
917+
);
918+
919+
ipcMain.handle('download-file', async (e, fileName, url, onProgress) => {
920+
return downloadFile(mainWindow, fileName, url, onProgress);
921+
});
922+
904923
ipcMain.handle('start-listener', async (e, dirPath) => {
905924
try {
906925
log.log('Trying to start listener');

src/app/desktop/DesktopRoot.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { ipcRenderer } from 'electron';
66
import { useSelector, useDispatch } from 'react-redux';
77
import { push } from 'connected-react-router';
88
import { message } from 'antd';
9+
import axios from 'axios';
10+
import adapter from 'axios/lib/adapters/http';
911
import RouteWithSubRoutes from '../../common/components/RouteWithSubRoutes';
1012
import {
1113
loginWithAccessToken,
@@ -72,6 +74,11 @@ function DesktopRoot({ store }) {
7274
});
7375

7476
const init = async () => {
77+
console.log(axios.defaults.adapter);
78+
axios.defaults.adapter = adapter;
79+
console.log(axios.defaults.adapter);
80+
console.log(adapter);
81+
7582
dispatch(requesting(features.mcAuthentication));
7683
const userDataStatic = await ipcRenderer.invoke('getUserData');
7784
const userData = dispatch(updateUserData(userDataStatic));

src/app/desktop/utils/computeFileHash.js

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 21 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -1,161 +1,33 @@
1-
import makeDir from 'make-dir';
2-
import fss from 'fs';
3-
import axios from 'axios';
4-
import pMap from 'p-map';
5-
import path from 'path';
6-
import adapter from 'axios/lib/adapters/http';
7-
import computeFileHash from './computeFileHash';
8-
9-
const fs = fss.promises;
10-
11-
function getUri(url) {
12-
return new URL(url).href;
13-
}
1+
import { ipcRenderer } from 'electron';
142

153
export const downloadInstanceFiles = async (
164
arr,
175
updatePercentage,
186
threads = 4
197
) => {
20-
let downloaded = 0;
21-
await pMap(
8+
ipcRenderer.on('download-instance-files-progress', (e, p) =>
9+
updatePercentage(p)
10+
);
11+
const res = await ipcRenderer.invoke(
12+
'download-instance-files',
2213
arr,
23-
async item => {
24-
let counter = 0;
25-
let res = false;
26-
if (!item.path || !item.url) {
27-
console.warn('Skipping', item);
28-
return;
29-
}
30-
do {
31-
counter += 1;
32-
if (counter !== 1) {
33-
await new Promise(resolve => setTimeout(resolve, 5000));
34-
}
35-
36-
try {
37-
res = await downloadFileInstance(
38-
item.path,
39-
item.url,
40-
item.sha1,
41-
item.legacyPath
42-
);
43-
} catch {
44-
// Do nothing
45-
}
46-
} while (!res && counter < 3);
47-
downloaded += 1;
48-
if (
49-
updatePercentage &&
50-
(downloaded % 5 === 0 || downloaded === arr.length)
51-
) {
52-
updatePercentage(downloaded);
53-
}
54-
},
55-
{ concurrency: threads }
14+
updatePercentage !== undefined,
15+
threads
5616
);
57-
};
58-
59-
const downloadFileInstance = async (fileName, url, sha1, legacyPath) => {
60-
let encodedUrl;
61-
try {
62-
const filePath = path.dirname(fileName);
63-
try {
64-
await fs.access(fileName);
65-
if (legacyPath) await fs.access(legacyPath);
66-
const checksum = await computeFileHash(fileName);
67-
const legacyChecksum = legacyPath && (await computeFileHash(legacyPath));
68-
if (checksum === sha1 && (!legacyPath || legacyChecksum === sha1)) {
69-
return true;
70-
}
71-
} catch {
72-
await makeDir(filePath);
73-
if (legacyPath) await makeDir(path.dirname(legacyPath));
74-
}
75-
76-
encodedUrl = getUri(url);
77-
78-
const { data } = await axios.get(encodedUrl, {
79-
responseType: 'stream',
80-
responseEncoding: null,
81-
adapter,
82-
timeout: 60000 * 20
83-
});
84-
85-
const wStream = fss.createWriteStream(fileName, {
86-
encoding: null
87-
});
88-
89-
data.pipe(wStream);
90-
let wStreamLegacy;
91-
if (legacyPath) {
92-
wStreamLegacy = fss.createWriteStream(legacyPath, {
93-
encoding: null
94-
});
95-
data.pipe(wStreamLegacy);
96-
}
97-
98-
await new Promise((resolve, reject) => {
99-
data.on('error', err => {
100-
console.error(err);
101-
reject(err);
102-
});
103-
104-
data.on('end', () => {
105-
wStream.end();
106-
wStream.close();
107-
if (legacyPath) {
108-
wStreamLegacy.end();
109-
wStreamLegacy.close();
110-
}
111-
resolve();
112-
});
113-
});
114-
return true;
115-
} catch (e) {
116-
console.error(
117-
`Error while downloading <${url} | ${encodedUrl}> to <${fileName}> --> ${e.message}`
118-
);
119-
return false;
120-
}
17+
console.log('Back:', res);
18+
ipcRenderer.removeAllListeners('download-instance-files-progress');
19+
return res;
12120
};
12221

12322
export const downloadFile = async (fileName, url, onProgress) => {
124-
await makeDir(path.dirname(fileName));
125-
126-
const encodedUrl = getUri(url);
127-
128-
const { data, headers } = await axios.get(encodedUrl, {
129-
responseType: 'stream',
130-
responseEncoding: null,
131-
adapter,
132-
timeout: 60000 * 20
133-
});
134-
135-
const out = fss.createWriteStream(fileName, { encoding: null });
136-
data.pipe(out);
137-
138-
// Save variable to know progress
139-
let receivedBytes = 0;
140-
const totalBytes = parseInt(headers['content-length'], 10);
141-
142-
data.on('data', chunk => {
143-
// Update the received bytes
144-
receivedBytes += chunk.length;
145-
if (onProgress) {
146-
onProgress(parseInt(((receivedBytes * 100) / totalBytes).toFixed(1), 10));
147-
}
148-
});
149-
150-
return new Promise((resolve, reject) => {
151-
data.on('end', () => {
152-
out.end();
153-
out.close();
154-
resolve();
155-
});
156-
157-
data.on('error', () => {
158-
reject();
159-
});
160-
});
23+
ipcRenderer.on('download-file-progress', (e, p) => onProgress(p));
24+
const res = await ipcRenderer.invoke(
25+
'download-file',
26+
fileName,
27+
url,
28+
onProgress !== undefined
29+
);
30+
console.log('Back-single:', res);
31+
ipcRenderer.removeAllListeners('download-file-progress');
32+
return res;
16133
};

webpack.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ const config = {
140140
parallel: true
141141
})
142142
]
143+
},
144+
resolve: {
145+
alias: {
146+
'axios/lib/adapters/xhr': 'axios/lib/adapters/http'
147+
}
143148
}
144149
};
145150

0 commit comments

Comments
 (0)