Optimized the code in the speedtest.js

This commit is contained in:
Mathias Wagner
2023-11-30 11:40:01 +01:00
parent 85cc8613db
commit 96769fa6df

View File

@@ -4,20 +4,11 @@ module.exports = async (serverId, binary_path = './bin/speedtest' + (process.pla
const args = ['--accept-license', '--accept-gdpr', '--format=jsonl'];
if (serverId) args.push(`--server-id=${serverId}`);
const process = spawn(binary_path, args, {windowsHide: true});
process.stdout.on('data', onLine);
let result = {};
await new Promise((resolve, reject) => {
process.on('error', e => reject({message: e}));
process.on('exit', resolve);
});
const process = spawn(binary_path, args, {windowsHide: true});
if (result.error) throw new Error(result.error);
return result;
function onLine(buffer) {
process.stdout.on('data', (buffer) => {
const line = buffer.toString().replace("\n", "");
if (!line.startsWith("{")) return;
@@ -31,5 +22,13 @@ module.exports = async (serverId, binary_path = './bin/speedtest' + (process.pla
if (data.error) result.error = data.error;
if (data.type === "result") result = data;
}
});
await new Promise((resolve, reject) => {
process.on('error', e => reject({message: e}));
process.on('exit', resolve);
});
if (result.error) throw new Error(result.error);
return result;
}