Created the speedtest util

This commit is contained in:
Mathias
2022-08-09 00:48:46 +02:00
parent 9d12a872a6
commit bd0c16fe50

29
server/util/speedtest.js Normal file
View File

@@ -0,0 +1,29 @@
const {spawn} = require('child_process');
module.exports = async (serverId, binary_path = './bin/speedtest') => {
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);
});
if (result.error) throw new Error(result.error);
return result;
function onLine(buffer) {
const line = buffer.toString().replace("\n", "");
if (!line.startsWith("{")) return;
let data = JSON.parse(line);
if (data.error) result.error = data.error;
if (data.type === "result") result = data;
}
}