2
0
mirror of https://github.com/frappe/books.git synced 2024-09-20 11:29:00 +00:00
books/main/contactMothership.ts
18alantom 56e5a7fc74 incr: shift contactMothership
- ship lang files too cause sometimes errors out.
2022-05-23 16:18:23 +05:30

88 lines
2.1 KiB
TypeScript

import { app } from 'electron';
import fs from 'fs';
import http from 'http';
import https from 'https';
import path from 'path';
export function getUrlAndTokenString() {
const inProduction = app.isPackaged;
const empty = { url: '', telemetryUrl: '', tokenString: '' };
let errLogCredsPath = path.join(
process.resourcesPath,
'../creds/log_creds.txt'
);
if (!fs.existsSync(errLogCredsPath)) {
errLogCredsPath = path.join(__dirname, '../log_creds.txt');
}
if (!fs.existsSync(errLogCredsPath)) {
!inProduction && console.log(`${errLogCredsPath} doesn't exist, can't log`);
return empty;
}
let apiKey, apiSecret, url, telemetryUrl;
try {
[apiKey, apiSecret, url, telemetryUrl] = fs
.readFileSync(errLogCredsPath, 'utf-8')
.split('\n')
.filter((f) => f.length);
} catch (err) {
if (!inProduction) {
console.log(`logging error using creds at: ${errLogCredsPath} failed`);
console.log(err);
}
return empty;
}
return {
url: encodeURI(url),
telemetryUrl: encodeURI(telemetryUrl),
tokenString: `token ${apiKey}:${apiSecret}`,
};
}
function post(bodyJson: string) {
const inProduction = app.isPackaged;
const { url, tokenString } = getUrlAndTokenString();
const isHttps = url.split(':')[0].toLowerCase() === 'https';
const headers = {
Authorization: tokenString,
Accept: 'application/json',
'Content-Type': 'application/json',
};
const req = (isHttps ? https : http).request(
url,
{
method: 'POST',
headers,
},
(res) => {
if (inProduction) {
return;
}
console.log(`STATUS: ${res.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
res.setEncoding('utf8');
res.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
});
}
);
req.on('error', (e) => {
console.log(`ERROR: ${e.message}`);
});
req.write(bodyJson);
req.end();
}
export function sendError(bodyJson: string) {
post(bodyJson);
}
// Nothing nefarious going on here.
// Just regular old user mandated error logging.