summaryrefslogtreecommitdiff
path: root/src/misc/fetch.ts
blob: 887aae1659637fa9421feb986e9cbcc2ce9a0d24 (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
import * as http from 'http';
import * as https from 'https';
import * as cache from 'lookup-dns-cache';
import fetch, { HeadersInit } from 'node-fetch';
import { HttpProxyAgent } from 'http-proxy-agent';
import { HttpsProxyAgent } from 'https-proxy-agent';
import config from '../config';

export async function getJson(url: string, accept = 'application/json, */*', timeout = 10000, headers?: HeadersInit) {
	const res = await fetch(url, {
		headers: Object.assign({
			'User-Agent': config.userAgent,
			Accept: accept
		}, headers || {}),
		timeout,
		agent: u => u.protocol == 'http:' ? httpAgent : httpsAgent,
	});

	if (!res.ok) {
		throw {
			name: `StatusError`,
			statusCode: res.status,
			message: `${res.status} ${res.statusText}`,
		};
	}

	return await res.json();
}

export const httpAgent = config.proxy
	? new HttpProxyAgent(config.proxy)
	: new http.Agent({
		keepAlive: true,
		keepAliveMsecs: 30 * 1000,
	});

export const httpsAgent = config.proxy
	? new HttpsProxyAgent(config.proxy)
	: new https.Agent({
		keepAlive: true,
		keepAliveMsecs: 30 * 1000,
		lookup: cache.lookup,
	});