mirror of
https://github.com/tribufu/node-gamedig
synced 2026-05-18 09:35:50 +00:00
Upgrade syntax of everything to more modern javascript
This commit is contained in:
parent
f8d903b982
commit
69288baebc
43 changed files with 1499 additions and 1521 deletions
56
lib/index.js
56
lib/index.js
|
|
@ -1,46 +1,48 @@
|
|||
var dgram = require('dgram'),
|
||||
EventEmitter = require('events').EventEmitter,
|
||||
util = require('util'),
|
||||
dns = require('dns'),
|
||||
TypeResolver = require('./typeresolver');
|
||||
const dgram = require('dgram'),
|
||||
TypeResolver = require('./typeresolver');
|
||||
|
||||
var activeQueries = [];
|
||||
const activeQueries = [];
|
||||
|
||||
var udpSocket = dgram.createSocket('udp4');
|
||||
const udpSocket = dgram.createSocket('udp4');
|
||||
udpSocket.unref();
|
||||
udpSocket.bind(21943);
|
||||
udpSocket.on('message', function(buffer, rinfo) {
|
||||
udpSocket.on('message', (buffer, rinfo) => {
|
||||
if(Gamedig.debug) console.log(rinfo.address+':'+rinfo.port+" <--UDP "+buffer.toString('hex'));
|
||||
for(var i = 0; i < activeQueries.length; i++) {
|
||||
var query = activeQueries[i];
|
||||
for(const query of activeQueries) {
|
||||
if(
|
||||
query.options.address != rinfo.address
|
||||
&& query.options.altaddress != rinfo.address
|
||||
query.options.address !== rinfo.address
|
||||
&& query.options.altaddress !== rinfo.address
|
||||
) continue;
|
||||
if(query.options.port_query != rinfo.port) continue;
|
||||
if(query.options.port_query !== rinfo.port) continue;
|
||||
query._udpResponse(buffer);
|
||||
break;
|
||||
}
|
||||
});
|
||||
udpSocket.on('error', function(e) {
|
||||
udpSocket.on('error', (e) => {
|
||||
if(Gamedig.debug) console.log("UDP ERROR: "+e);
|
||||
});
|
||||
|
||||
Gamedig = {
|
||||
class Gamedig {
|
||||
|
||||
query: function(options,callback) {
|
||||
static query(options,callback) {
|
||||
const promise = new Promise((resolve,reject) => {
|
||||
for (const key of Object.keys(options)) {
|
||||
if (['port_query', 'port'].includes(key)) {
|
||||
options[key] = parseInt(options[key]);
|
||||
}
|
||||
}
|
||||
|
||||
options.callback = (state) => {
|
||||
if (state.error) reject(state.error);
|
||||
else resolve(state);
|
||||
};
|
||||
|
||||
var query;
|
||||
let query;
|
||||
try {
|
||||
query = TypeResolver.lookup(options.type);
|
||||
} catch(e) {
|
||||
process.nextTick(function() {
|
||||
options.callback({error:e.message});
|
||||
process.nextTick(() => {
|
||||
options.callback({error:e});
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
|
@ -63,26 +65,28 @@ Gamedig = {
|
|||
}
|
||||
|
||||
// copy over options
|
||||
for(var i in options) query.options[i] = options[i];
|
||||
for(const key of Object.keys(options)) {
|
||||
query.options[key] = options[key];
|
||||
}
|
||||
|
||||
activeQueries.push(query);
|
||||
|
||||
query.on('finished',function(state) {
|
||||
var i = activeQueries.indexOf(query);
|
||||
query.on('finished',() => {
|
||||
const i = activeQueries.indexOf(query);
|
||||
if(i >= 0) activeQueries.splice(i, 1);
|
||||
});
|
||||
|
||||
process.nextTick(function() {
|
||||
process.nextTick(() => {
|
||||
query.start();
|
||||
});
|
||||
});
|
||||
|
||||
if (callback && callback instanceof Function) {
|
||||
if(callback.length == 2) {
|
||||
if(callback.length === 2) {
|
||||
promise
|
||||
.then((state) => callback(null,state))
|
||||
.catch((error) => callback(error));
|
||||
} else if (callback.length == 1) {
|
||||
} else if (callback.length === 1) {
|
||||
promise
|
||||
.then((state) => callback(state))
|
||||
.catch((error) => callback({error:error}));
|
||||
|
|
@ -92,6 +96,6 @@ Gamedig = {
|
|||
return promise;
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = Gamedig;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue