mirror of
https://github.com/gnmyt/myspeed.git
synced 2026-01-19 11:39:42 -06:00
Optimized the code in the speedtest.js
This commit is contained in:
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user