diff --git a/dummy/index.ts b/dummy/index.ts index 90d19084..eb513a8e 100644 --- a/dummy/index.ts +++ b/dummy/index.ts @@ -83,7 +83,6 @@ async function setOtherSettings(fyo: Fyo) { await acc.setAndSync({ gstin: '27LIN180000A1Z5', }); - console.log(acc.gstin, await fyo.db.getSingleValues('gstin')); } /** diff --git a/fyo/core/authHandler.ts b/fyo/core/authHandler.ts index 8f8c2b10..f104198b 100644 --- a/fyo/core/authHandler.ts +++ b/fyo/core/authHandler.ts @@ -1,6 +1,7 @@ import { Fyo } from 'fyo'; import { AuthDemux } from 'fyo/demux/auth'; -import { AuthDemuxBase, TelemetryCreds } from 'utils/auth/types'; +import { AuthDemuxBase } from 'utils/auth/types'; +import { Creds } from 'utils/types'; import { AuthDemuxConstructor } from './types'; interface AuthConfig { @@ -19,6 +20,7 @@ export class AuthHandler { #session: Session; fyo: Fyo; #demux: AuthDemuxBase; + #creds?: Creds; constructor(fyo: Fyo, Demux?: AuthDemuxConstructor) { this.fyo = fyo; @@ -111,7 +113,11 @@ export class AuthHandler { return this.#config.serverURL || ''; } - async getTelemetryCreds(): Promise { - return await this.#demux.getTelemetryCreds(); + async getCreds(): Promise { + if (!this.#creds) { + this.#creds = await this.#demux.getCreds(); + } + + return this.#creds; } } diff --git a/fyo/demux/auth.ts b/fyo/demux/auth.ts index e9ea5136..e22607d1 100644 --- a/fyo/demux/auth.ts +++ b/fyo/demux/auth.ts @@ -1,6 +1,7 @@ import { ipcRenderer } from 'electron'; -import { AuthDemuxBase, TelemetryCreds } from 'utils/auth/types'; +import { AuthDemuxBase } from 'utils/auth/types'; import { IPC_ACTIONS } from 'utils/messages'; +import { Creds } from 'utils/types'; export class AuthDemux extends AuthDemuxBase { #isElectron: boolean = false; @@ -9,14 +10,11 @@ export class AuthDemux extends AuthDemuxBase { this.#isElectron = isElectron; } - async getTelemetryCreds(): Promise { + async getCreds(): Promise { if (this.#isElectron) { - const creds = await ipcRenderer.invoke(IPC_ACTIONS.GET_CREDS); - const url: string = creds?.telemetryUrl ?? ''; - const token: string = creds?.tokenString ?? ''; - return { url, token }; + return (await ipcRenderer.invoke(IPC_ACTIONS.GET_CREDS)) as Creds; } else { - return { url: '', token: '' }; + return { errorLogUrl: '', tokenString: '', telemetryUrl: '' }; } } } diff --git a/fyo/telemetry/telemetry.ts b/fyo/telemetry/telemetry.ts index 0d8b913a..09f76ef3 100644 --- a/fyo/telemetry/telemetry.ts +++ b/fyo/telemetry/telemetry.ts @@ -118,9 +118,9 @@ export class TelemetryManager { return; } - const { url, token } = await this.fyo.auth.getTelemetryCreds(); - this.#url = url; - this.#token = token; + const { telemetryUrl, tokenString } = await this.fyo.auth.getCreds(); + this.#url = telemetryUrl; + this.#token = tokenString; } #getTelemtryData( diff --git a/fyo/tests/helpers.ts b/fyo/tests/helpers.ts index d91f9fbd..9754d8c4 100644 --- a/fyo/tests/helpers.ts +++ b/fyo/tests/helpers.ts @@ -1,7 +1,8 @@ -import { AuthDemuxBase, TelemetryCreds } from 'utils/auth/types'; +import { AuthDemuxBase } from 'utils/auth/types'; +import { Creds } from 'utils/types'; export class DummyAuthDemux extends AuthDemuxBase { - async getTelemetryCreds(): Promise { - return { url: '', token: '' }; + async getCreds(): Promise { + return { errorLogUrl: '', tokenString: '', telemetryUrl: '' }; } } diff --git a/main/contactMothership.ts b/main/contactMothership.ts index 6f84fc77..24ecace3 100644 --- a/main/contactMothership.ts +++ b/main/contactMothership.ts @@ -1,12 +1,13 @@ import { app } from 'electron'; import fs from 'fs'; -import http from 'http'; -import https from 'https'; +import fetch from 'node-fetch'; import path from 'path'; +import { Creds } from 'utils/types'; +import { rendererLog } from './helpers'; -export function getUrlAndTokenString() { +export function getUrlAndTokenString(): Creds { const inProduction = app.isPackaged; - const empty = { url: '', telemetryUrl: '', tokenString: '' }; + const empty: Creds = { errorLogUrl: '', telemetryUrl: '', tokenString: '' }; let errLogCredsPath = path.join( process.resourcesPath, '../creds/log_creds.txt' @@ -20,9 +21,9 @@ export function getUrlAndTokenString() { return empty; } - let apiKey, apiSecret, url, telemetryUrl; + let apiKey, apiSecret, errorLogUrl, telemetryUrl; try { - [apiKey, apiSecret, url, telemetryUrl] = fs + [apiKey, apiSecret, errorLogUrl, telemetryUrl] = fs .readFileSync(errLogCredsPath, 'utf-8') .split('\n') .filter((f) => f.length); @@ -35,53 +36,21 @@ export function getUrlAndTokenString() { } return { - url: encodeURI(url), + errorLogUrl: encodeURI(errorLogUrl), 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'; +export async function sendError(body: string) { + const { errorLogUrl, tokenString } = getUrlAndTokenString(); 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}`); + await fetch(errorLogUrl, { method: 'POST', headers, body }).catch((err) => { + rendererLog(err); }); - req.write(bodyJson); - req.end(); } - -export function sendError(bodyJson: string) { - post(bodyJson); -} - -// Nothing nefarious going on here. -// Just regular old user mandated error logging. diff --git a/package.json b/package.json index ae90e813..9ea571b3 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "@types/luxon": "^2.3.1", "@types/mocha": "^9.1.0", "@types/node": "^17.0.23", + "@types/node-fetch": "^2.6.1", "@typescript-eslint/eslint-plugin": "^4.15.1", "@typescript-eslint/parser": "^4.15.1", "@vue/cli-plugin-babel": "^4.5.0", diff --git a/src/renderer/registerIpcRendererListeners.ts b/src/renderer/registerIpcRendererListeners.ts index cf915006..38532e62 100644 --- a/src/renderer/registerIpcRendererListeners.ts +++ b/src/renderer/registerIpcRendererListeners.ts @@ -55,7 +55,13 @@ export default function registerIpcRendererListeners() { await handleError(true, error as Error); }); - ipcRenderer.on(IPC_CHANNELS.CONSOLE_LOG, console.log); + ipcRenderer.on(IPC_CHANNELS.CONSOLE_LOG, (_, ...stuff: unknown[]) => { + if (!fyo.store.isDevelopment) { + return; + } + + console.log(...stuff); + }); document.addEventListener('visibilitychange', function () { const { visibilityState } = document; diff --git a/utils/auth/types.ts b/utils/auth/types.ts index d730aaea..043a9d4a 100644 --- a/utils/auth/types.ts +++ b/utils/auth/types.ts @@ -1,5 +1,5 @@ -export type TelemetryCreds = { url: string; token: string }; +import { Creds } from 'utils/types'; export abstract class AuthDemuxBase { - abstract getTelemetryCreds(): Promise + abstract getCreds(): Promise; } diff --git a/utils/types.ts b/utils/types.ts index efc203e8..bf5b21b9 100644 --- a/utils/types.ts +++ b/utils/types.ts @@ -16,10 +16,11 @@ export interface CountryInfo { locale: string; } - export interface VersionParts { major: number; minor: number; patch: number; beta?: number; -}; \ No newline at end of file +} + +export type Creds = { errorLogUrl: string; telemetryUrl: string; tokenString: string }; diff --git a/yarn.lock b/yarn.lock index d36353e1..f8982b4f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1447,6 +1447,14 @@ resolved "https://registry.yarnpkg.com/@types/ms/-/ms-0.7.31.tgz#31b7ca6407128a3d2bbc27fe2d21b345397f6197" integrity sha512-iiUgKzV9AuaEkZqkOLDIvlQiL6ltuZd9tGcW3gwpnX8JbuiuhFlEGmmFXEXkN50Cvq7Os88IY2v0dkDqXYWVgA== +"@types/node-fetch@^2.6.1": + version "2.6.1" + resolved "https://registry.yarnpkg.com/@types/node-fetch/-/node-fetch-2.6.1.tgz#8f127c50481db65886800ef496f20bbf15518975" + integrity sha512-oMqjURCaxoSIsHSr1E47QHzbmzNR5rK8McHuNb11BOM9cHcIK3Avy0s/b2JlXHoQGTYS3NsvWzV1M0iK7l0wbA== + dependencies: + "@types/node" "*" + form-data "^3.0.0" + "@types/node@*": version "17.0.8" resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.8.tgz#50d680c8a8a78fe30abe6906453b21ad8ab0ad7b" @@ -6058,6 +6066,15 @@ fork-ts-checker-webpack-plugin@^3.1.1: tapable "^1.0.0" worker-rpc "^0.1.0" +form-data@^3.0.0: + version "3.0.1" + resolved "https://registry.yarnpkg.com/form-data/-/form-data-3.0.1.tgz#ebd53791b78356a99af9a300d4282c4d5eb9755f" + integrity sha512-RHkBKtLWUVwd7SqRIvCZMEvAMoGUp0XU+seQiZejj0COz3RI3hWP4sCv3gZWWLjJTd7rGwcsF5eKZGii0r/hbg== + dependencies: + asynckit "^0.4.0" + combined-stream "^1.0.8" + mime-types "^2.1.12" + form-data@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452"