mirror of
https://github.com/gnmyt/myspeed.git
synced 2025-12-31 01:59:37 -06:00
Created the speedtest util
This commit is contained in:
29
server/util/speedtest.js
Normal file
29
server/util/speedtest.js
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user