summaryrefslogtreecommitdiff
path: root/lib/api.js
blob: d8010a9f7f67df1919b202c918333d5643f6f283 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import * as fs from 'fs';

import { getToken } from '../database/config.js';
import { PriorityQueue } from './priority_queue.js';

let busy = false; // lets us know if we are already sending api requests or not.
let headers = undefined; // a file scope 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.

// 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',
//    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 function send(request, ctx) {
	return new Promise((resolve, reject) => {
		let data = {
			ctx: ctx,
			reject: reject,
			request: request,
			resolve: resolve,
		};
		if (!busy) {
			busy = true;
			send_this(data);
		} else {
			queue.enqueue(data, request.priority ? request.priority : 10);
		}
	});
}

function send_next() {
	if (queue.isEmpty()) {
		busy = false;
	} else {
		send_this(queue.dequeue().element);
	}
}

// send_this take a data object as argument built in the send function above
function send_this(data) {
	if (headers === undefined) {
		const token = getToken();
		if (token === null) {
			throw 'Could not get token from the database. Did you init or register yet?';
		}
		headers = {
			'Content-Type': 'application/json',
			'Authorization': `Bearer ${token}`
		};
	}
	let options = {
		headers: headers,
	};
	if (data.request.method !== undefined) {
		options['method'] = data.request.method;
	}
	if (data.request.payload !== undefined) {
		options['body'] = JSON.stringify(data.request.payload);
	}
	fs.writeFileSync('log', JSON.stringify({event: 'send', date: new Date(), data: data}) + '\n', {flag: 'a+'});
	fetch(`https://api.spacetraders.io/v2${data.request.endpoint}`, options)
		.then(response => response.json())
		.then(async response => {
			switch(response.error?.code) {
				//case 401: // 401 means a server reset happened
				// close database file
				// rm database file
				// logrotate
				// spawnSync
				// break;
			case 429:  // 429 means rate limited, let's hold back for 10 seconds
				await sleep(10000);
				queue.enqueue(data, 1);
				break;
			default:
				fs.writeFileSync('log', JSON.stringify({event: 'response', date: new Date(), data: response}) + '\n', {flag: 'a+'});
				return data.resolve(response);
			}})
		.catch(async err => {
			fs.writeFileSync('log', JSON.stringify({event: 'error', date: new Date(), data: err}) + '\n', {flag: 'a+'});
			switch(err.cause?.code) {
			case 503:  // 503 means maintenance mode, let's hold back for 1 minute
				await sleep(60000);
				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);
			}});
	setTimeout(send_next, 400); // 333 should work, but 400 will still allow manual requests to go through during development
}

export function debugLog(ctx) {
	console.log(`--- ${Date()} -----------------------------------------------------------------------------`);
	console.log(JSON.stringify(ctx, null, 2));
}

export function sleep(delay) {
	return new Promise((resolve) => setTimeout(resolve, delay))
}