diff options
Diffstat (limited to '')
-rw-r--r-- | nodejs/lib/api.ts (renamed from nodejs/lib/api.js) | 125 |
1 files changed, 61 insertions, 64 deletions
diff --git a/nodejs/lib/api.js b/nodejs/lib/api.ts index ecb0282..08c6c71 100644 --- a/nodejs/lib/api.js +++ b/nodejs/lib/api.ts @@ -1,8 +1,9 @@ import * as fs from 'fs'; import * as events from 'events'; -import { getToken } from '../database/tokens.js'; -import { PriorityQueue } from './priority_queue.js'; +import { APIError, Request, RequestPromise, Response } from '../model/api.ts'; +import { getToken } from '../database/tokens.ts'; +import { PriorityQueue } from './priority_queue.ts'; // queue processor module variables const bus = new events.EventEmitter(); // a bus to notify the queue processor to start processing messages @@ -10,10 +11,9 @@ let busy = false; // true if we are already sending api requests. let backoffSeconds = 0; let running = false; // other module variables -let headers = undefined; // a file scoped variable so that we only evaluate these once. +let headers: {[key:string]:string}|null = null; // a file scoped variable so that we only evaluate these once. let queue = new PriorityQueue(); // a priority queue to hold api calls we want to send, allows for throttling. -// a single queue processor should be running at any time, otherwise there will be trouble! async function queue_processor() { if (running) { throw 'refusing to start a second queue processor'; @@ -30,9 +30,10 @@ async function queue_processor() { await new Promise(resolve => bus.once('send', resolve)); busy = true; } - const before = new Date(); - await send_this(queue.dequeue().element); - const duration = new Date() - before; + const before = new Date().getTime(); + const data = queue.dequeue() as RequestPromise<unknown>; + await send_this(data); + const duration = new Date().getTime() - before; if (duration < 400) { // 333 should work, but 400 should still allow some manual requests to go through during development await sleep(400 - duration); } @@ -44,21 +45,17 @@ async function queue_processor() { } queue_processor(); -// send takes a request object as argument and an optional context ctx -// example request: { -// endpoint: the path part of the url to call, -// method: HTTP method for `fetch` call, defaults to 'GET', -// page: run a paginated request starting from this page until all the following pages are fetched -// payload: optional json object that will be send along with the request, -// priority: optional priority value (defaults to 10, lower than 10 means the message will be sent faster) -// } -export async function send(request, ctx) { - if (request.page === undefined) { - return await send_one(request, ctx); - } - let ret = []; +export async function send<T>(request: Request): Promise<T|APIError> { + const response = await send_one<T>(request); + if (response.error) return response.error; + return response.data; +} + +export async function sendPaginated<T>(request: Request): Promise<Array<T>|APIError> { + if (request.page === undefined) request.page = 1; + let ret: Array<T> = []; while (true) { - const response = await send_one(request, ctx); + const response = await send_one<T>(request); if (response.meta === undefined) { throw {"message": "paginated request did not return a meta block", "request": request, "response": response}; } @@ -70,10 +67,9 @@ export async function send(request, ctx) { } } -function send_one(request, ctx) { +function send_one<T>(request: Request): Promise<Response<T>> { return new Promise((resolve, reject) => { - let data = { - ctx: ctx, + const data: RequestPromise<T> = { reject: reject, request: request, resolve: resolve, @@ -86,8 +82,8 @@ function send_one(request, ctx) { } // send_this take a data object as argument built in the send function above -async function send_this(data) { - if (headers === undefined) { +async function send_this(data: RequestPromise<unknown>) { + if (headers === null) { const token = getToken(); if (token === null) { throw 'Could not get token from the database. Did you init or register yet?'; @@ -97,7 +93,7 @@ async function send_this(data) { 'Authorization': `Bearer ${token}` }; } - let options = { + let options: {[key:string]:any} = { headers: headers, }; if (data.request.method !== undefined) { @@ -112,52 +108,53 @@ async function send_this(data) { } fs.writeFileSync('log', JSON.stringify({event: 'send', date: new Date(), data: data}) + '\n', {flag: 'a+'}); try { - let response = await fetch(`https://api.spacetraders.io/v2${data.request.endpoint}${pagination}`, options); - response = await response.json(); - switch(response.error?.code) { - case 401: // TODO 401 means a server reset happened - throw response; - // TODO reject all promises in queue - // reset database - // logrotate - // spawnSync? - // break; - case 429: // 429 means rate limited, let's hold back as instructed - backoffSeconds = response.error.data.retryAfter; - queue.enqueue(data, 1); - break; - case 503: // 503 means maintenance mode, let's hold back for 1 minute - backoffSeconds = 60; - queue.enqueue(data, 1); - break; - default: // no error! - fs.writeFileSync('log', JSON.stringify({event: 'response', date: new Date(), data: response}) + '\n', {flag: 'a+'}); - return data.resolve(response); + const response = await fetch(`https://api.spacetraders.io/v2${data.request.endpoint}${pagination}`, options); + const json = await response.json() as Response<unknown>; + switch(json.error?.code) { + case 401: // TODO 401 means a server reset happened + throw json; + // TODO reject all promises in queue + // reset database + // logrotate + // spawnSync? + // break; + case 429: // 429 means rate limited, let's hold back as instructed + backoffSeconds = json.error.data.retryAfter; + queue.enqueue(data, 1); + break; + case 503: // 503 means maintenance mode, let's hold back for 1 minute + backoffSeconds = 60; + queue.enqueue(data, 1); + break; + default: // no error! + fs.writeFileSync('log', JSON.stringify({event: 'response', date: new Date(), data: json}) + '\n', {flag: 'a+'}); + return data.resolve(json); } - } catch (err) { + } catch (_err) { + const err = _err as {cause?: {code: string}}; fs.writeFileSync('log', JSON.stringify({event: 'error', date: new Date(), data: err}) + '\n', {flag: 'a+'}); switch(err.cause?.code) { - case 'EAI_AGAIN': // DNS lookup timed out error, let's hold back for 5 seconds - backoffSeconds = 5; - queue.enqueue(data, 1); - break; - case 'ECONNRESET': - queue.enqueue(data, 1); - break; - case 'UND_ERR_CONNECT_TIMEOUT': - queue.enqueue(data, 1); - break; - default: - data.reject(err); + case 'EAI_AGAIN': // DNS lookup timed out error, let's hold back for 5 seconds + backoffSeconds = 5; + queue.enqueue(data, 1); + break; + case 'ECONNRESET': + queue.enqueue(data, 1); + break; + case 'UND_ERR_CONNECT_TIMEOUT': + queue.enqueue(data, 1); + break; + default: + data.reject(err); } } } -export function debugLog(ctx) { +export function debugLog(ctx: any) { console.log(`--- ${Date()} -----------------------------------------------------------------------------`); console.log(JSON.stringify(ctx, null, 2)); } -export function sleep(delay) { - return new Promise((resolve) => setTimeout(resolve, delay)) +export function sleep(delay: number): Promise<unknown> { + return new Promise((resolve) => setTimeout(resolve, delay)); } |