Skip to content

Instantly share code, notes, and snippets.

@TianyiLi
Created December 8, 2016 05:35
Show Gist options
  • Save TianyiLi/595ca110fe61ec28425874a0a891fb2a to your computer and use it in GitHub Desktop.
Save TianyiLi/595ca110fe61ec28425874a0a891fb2a to your computer and use it in GitHub Desktop.
Net Speed with speedtest.net
import * as fs from 'fs';
import { EventEmitter } from 'events';
const speedTest = require('speedtest-net');
var wrap = (callback: Function) => {
try {
callback();
} catch (error) {
console.log(error);
}
};
(() => {
wrap(async ()=> {
let data = await startTest();
console.log(`We got the data is \n ${data} \n END!`);
});
})();
function startTest() {
let config = {
maxTime: 2500,
serverId: 2133
}
let a: EventEmitter = speedTest(config)
return new Promise((res, rej) => {
a.on('downloadprogress', progress => {
console.log(progress);
});
a.on('config', config => {
fs.writeFile('./netConfig', JSON.stringify(config));
})
a.on('done', dataOverLoad => {
fs.writeFile('./config', JSON.stringify(dataOverLoad));
res(dataOverLoad);
});
a.on('error', err=>{
rej(err);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment