Swap from request to got

This commit is contained in:
Michael Morrison 2020-07-04 23:28:03 -05:00
parent 67563c0f1e
commit 487b17dd73
9 changed files with 215 additions and 340 deletions

View file

@ -3,12 +3,12 @@ const Core = require('./core');
class AssettoCorsa extends Core {
async run(state) {
const serverInfo = await this.request({
json: true,
uri: `http://${this.options.address}:${this.options.port}/INFO`
url: `http://${this.options.address}:${this.options.port}/INFO`,
responseType: 'json'
});
const carInfo = await this.request({
json: true,
uri: `http://${this.options.address}:${this.options.port}/JSON|${parseInt(Math.random() * 999999999999999, 10)}`
url: `http://${this.options.address}:${this.options.port}/JSON|${parseInt(Math.random() * 999999999999999, 10)}`,
responseType: 'json'
});
if (!serverInfo || !carInfo || !carInfo.Cars) {

View file

@ -4,7 +4,7 @@ const Core = require('./core'),
class BuildAndShoot extends Core {
async run(state) {
const body = await this.request({
uri: 'http://'+this.options.address+':'+this.options.port+'/',
url: 'http://'+this.options.address+':'+this.options.port+'/',
});
let m;

View file

@ -2,7 +2,7 @@ const EventEmitter = require('events').EventEmitter,
net = require('net'),
Reader = require('../lib/reader'),
HexUtil = require('../lib/HexUtil'),
requestAsync = require('request-promise'),
got = require('got'),
Promises = require('../lib/Promises'),
Logger = require('../lib/Logger'),
DnsResolver = require('../lib/DnsResolver');
@ -40,11 +40,10 @@ class Core extends EventEmitter {
let abortCall = null;
this.abortedPromise = new Promise((resolve,reject) => {
abortCall = () => reject(new Error("Query is finished -- cancelling outstanding promises"));
}).catch(() => {
// Make sure that if this promise isn't attached to, it doesn't throw a unhandled promise rejection
});
// Make sure that if this promise isn't attached to, it doesn't throw a unhandled promise rejection
this.abortedPromise.catch(() => {});
let timeout;
try {
const promise = this.runOnce();
@ -342,24 +341,26 @@ class Core extends EventEmitter {
}
}
async request(params) {
// If we haven't opened a raw tcp socket yet during this query, just open one and then immediately close it.
// This will give us a much more accurate RTT than using the rtt of the http request.
async tcpPing() {
// This will give a much more accurate RTT than using the rtt of an http request.
if (!this.usedTcp) {
await this.withTcp(() => {});
}
}
async request(params) {
await this.tcpPing();
let requestPromise;
try {
requestPromise = requestAsync({
requestPromise = got({
...params,
timeout: this.options.socketTimeout,
resolveWithFullResponse: true
timeout: this.options.socketTimeout
});
this.debugLog(log => {
log(() => params.uri + " HTTP-->");
log(() => params.url + " HTTP-->");
requestPromise
.then((response) => log(params.uri + " <--HTTP " + response.statusCode))
.then((response) => log(params.url + " <--HTTP " + response.statusCode))
.catch(() => {});
});
const wrappedPromise = requestPromise.then(response => {

View file

@ -12,18 +12,18 @@ class FiveM extends Quake2 {
await super.run(state);
{
const raw = await this.request({
uri: 'http://' + this.options.address + ':' + this.options.port + '/info.json'
const json = await this.request({
url: 'http://' + this.options.address + ':' + this.options.port + '/info.json',
responseType: 'json'
});
const json = JSON.parse(raw);
state.raw.info = json;
}
{
const raw = await this.request({
uri: 'http://' + this.options.address + ':' + this.options.port + '/players.json'
const json = await this.request({
url: 'http://' + this.options.address + ':' + this.options.port + '/players.json',
responseType: 'json'
});
const json = JSON.parse(raw);
state.raw.players = json;
state.players = [];
for (const player of json) {

View file

@ -2,8 +2,10 @@ const Core = require('./core');
class GeneShift extends Core {
async run(state) {
await this.tcpPing();
const body = await this.request({
uri: 'http://geneshift.net/game/receiveLobby.php'
url: 'http://geneshift.net/game/receiveLobby.php'
});
const split = body.split('<br/>');

View file

@ -2,11 +2,11 @@ const Core = require('./core');
class Kspdmp extends Core {
async run(state) {
const body = await this.request({
uri: 'http://'+this.options.address+':'+this.options.port
const json = await this.request({
url: 'http://'+this.options.address+':'+this.options.port,
responseType: 'json'
});
const json = JSON.parse(body);
for (const one of json.players) {
state.players.push({name:one.nickname,team:one.team});
}

View file

@ -2,15 +2,15 @@ const Core = require('./core');
class Terraria extends Core {
async run(state) {
const body = await this.request({
uri: 'http://'+this.options.address+':'+this.options.port+'/v2/server/status',
qs: {
const json = await this.request({
url: 'http://'+this.options.address+':'+this.options.port+'/v2/server/status',
searchParams: {
players: 'true',
token: this.options.token
}
},
responseType: 'json'
});
const json = JSON.parse(body);
if(json.status !== '200') throw new Error('Invalid status');
for (const one of json.players) {