core/src/common/http.ts

142 lines
3.2 KiB
TypeScript
Raw Normal View History

2020-11-28 19:08:23 +00:00
import http from 'http';
import https from 'https';
import qs from 'querystring';
import fs from 'fs-extra';
import url from 'url';
export function httpGET(
link: string,
headers: any = {},
restrictToText = true,
saveTo?: string,
lback?: number): Promise<any> {
const parsed = url.parse(link);
const opts = {
headers: {
Accept: '*/*',
'Accept-Language': 'en-US',
'User-Agent': 'Squeebot/Commons-3.0.0',
},
host: parsed.hostname,
path: parsed.path,
port: parsed.port,
};
if (headers) {
opts.headers = Object.assign(opts.headers, headers);
}
let reqTimeOut: any;
let data: string | null = '';
const httpModule = parsed.protocol === 'https:' ? https : http;
return new Promise((resolve, reject) => {
2021-01-18 13:21:53 +00:00
if (lback && lback >= 5) {
return reject(new Error('infinite loop!'));
}
2020-11-28 19:08:23 +00:00
const req = httpModule.get(opts, (res) => {
if (res.statusCode === 302 || res.statusCode === 301) {
if (!lback) {
lback = 1;
} else {
lback += 1;
}
return httpGET(res.headers.location as string,
headers,
restrictToText,
saveTo,
lback,
).then(resolve, reject);
}
if (saveTo) {
const file = fs.createWriteStream(saveTo);
res.pipe(file);
file.on('close', () => resolve(saveTo));
file.on('error', (e) => reject(e));
return;
}
if (restrictToText) {
const cType = res.headers['content-type'];
if (cType && cType.indexOf('text') === -1 && cType.indexOf('json') === -1) {
req.abort();
return reject(new Error('Response type is not supported by httpGET!'));
}
}
reqTimeOut = setTimeout(() => {
req.abort();
data = null;
reject(new Error('Request took too long!'));
}, 5000);
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
clearTimeout(reqTimeOut);
resolve(data || saveTo);
});
}).on('error', (e) => {
reject(e);
});
req.setTimeout(10000);
});
}
export function httpPOST(
link: string,
headers: any = {},
data: any): Promise<any> {
const parsed = url.parse(link);
let postData = qs.stringify(data);
const opts = {
headers: {
'Content-Length': Buffer.byteLength(postData),
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': 'Squeebot/Commons-3.0.0',
},
host: parsed.host,
method: 'POST',
path: parsed.path,
port: parsed.port,
};
if (headers) {
opts.headers = Object.assign(opts.headers, headers);
}
if (opts.headers['Content-Type'] === 'application/json') {
postData = JSON.stringify(data);
}
return new Promise((resolve, reject) => {
const httpModule = parsed.protocol === 'https:' ? https : http;
const req = httpModule.request(opts, (res) => {
res.setEncoding('utf8');
let resp = '';
res.on('data', (chunk) => {
resp += chunk;
});
res.on('end', () => {
resolve(resp);
});
}).on('error', (e) => {
reject(e);
});
req.write(postData);
req.end();
});
}