fix(api): fix int overflow
This commit is contained in:
parent
12f73d42ca
commit
3f96cc43a0
1 changed files with 16 additions and 9 deletions
|
@ -9,6 +9,19 @@ import type { ApiManager } from './apiManager';
|
||||||
import type { Client } from '../client/client';
|
import type { Client } from '../client/client';
|
||||||
import type { IMakeRequestOptions, IRouteIdentifier } from '../utils/types';
|
import type { IMakeRequestOptions, IRouteIdentifier } from '../utils/types';
|
||||||
|
|
||||||
|
function calculateReset(reset: number, resetAfter: number, serverDate: number): number {
|
||||||
|
// Use direct reset time when available, server date becomes irrelevant in this case
|
||||||
|
if (resetAfter) {
|
||||||
|
return Date.now() + Number(resetAfter) * 1000;
|
||||||
|
}
|
||||||
|
return new Date(Number(reset) * 1000).getTime() - getAPIOffset(serverDate);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getAPIOffset(serverDate: number): number {
|
||||||
|
return new Date(serverDate).getTime() - Date.now();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
export class ApiHandler {
|
export class ApiHandler {
|
||||||
public client: Client;
|
public client: Client;
|
||||||
public hash: string;
|
public hash: string;
|
||||||
|
@ -32,10 +45,6 @@ export class ApiHandler {
|
||||||
this.majorParameter = majorParameter;
|
this.majorParameter = majorParameter;
|
||||||
this.manager = manager;
|
this.manager = manager;
|
||||||
this.queue = new AsyncQueue();
|
this.queue = new AsyncQueue();
|
||||||
// this.queue = queue(async (task: { routeId: IRouteIdentifier, requestOptions: IMakeRequestOptions; }, callback) => {
|
|
||||||
// if (this.limited) await sleep(this.timeToReset);
|
|
||||||
// callback(await this.makeRequest(task.routeId, task.requestOptions));
|
|
||||||
// });
|
|
||||||
this.remaining = -1;
|
this.remaining = -1;
|
||||||
this.reset = -1;
|
this.reset = -1;
|
||||||
}
|
}
|
||||||
|
@ -77,16 +86,14 @@ export class ApiHandler {
|
||||||
const serverDate = res.headers.get('date');
|
const serverDate = res.headers.get('date');
|
||||||
|
|
||||||
if (!resetAfter && options.path.includes('reactions')) {
|
if (!resetAfter && options.path.includes('reactions')) {
|
||||||
this.reset = new Date(Number(serverDate)).getTime() - (new Date(Number(serverDate)).getTime() - Date.now()) + 250;
|
this.reset = new Date(Number(serverDate)).getTime() - getAPIOffset(Number(serverDate)) + 250;
|
||||||
}
|
}
|
||||||
|
|
||||||
let retryAfter = 0;
|
let retryAfter = 0;
|
||||||
|
|
||||||
this.limit = limit !== null ? Number(limit) : Number.MAX_VALUE;
|
this.limit = limit !== null ? Number(limit) : Number.MAX_VALUE;
|
||||||
this.remaining = remaining !== null ? Number(remaining) : Number.MAX_VALUE;
|
this.remaining = remaining !== null ? Number(remaining) : Number.MAX_VALUE;
|
||||||
this.reset = reset || resetAfter
|
this.reset = reset || resetAfter ? calculateReset(Number(reset), Number(resetAfter), Number(serverDate)) : Date.now();
|
||||||
? Number(reset) * 1000 + Date.now() + this.client.options.api.offset
|
|
||||||
: Date.now();
|
|
||||||
|
|
||||||
if (retry !== null)
|
if (retry !== null)
|
||||||
retryAfter = retry ? Number(retry) * 1000 : -1;
|
retryAfter = retry ? Number(retry) * 1000 : -1;
|
||||||
|
@ -105,7 +112,7 @@ export class ApiHandler {
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
return res.json() as unknown as T;
|
return res.json() as unknown as T;
|
||||||
} else if (res.status === 429) {
|
} else if (res.status === 429) {
|
||||||
await sleep(retryAfter);
|
await sleep(this.timeToReset);
|
||||||
return this.makeRequest(routeId, options, retries++);
|
return this.makeRequest(routeId, options, retries++);
|
||||||
} else {
|
} else {
|
||||||
throw new Error('An unknown status code was returned!');
|
throw new Error('An unknown status code was returned!');
|
||||||
|
|
Reference in a new issue