All files / src save-log.ts

100% Statements 43/43
93.75% Branches 15/16
100% Functions 5/5
100% Lines 39/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 861x 1x 1x 1x 1x 1x               1x 1x   35x 35x 35x     37x         37x     125x 53x 53x   53x 12x   41x 4x   37x 1x       37x 39x 35x     35x     2x 2x 4x     2x       2x           2x         19x 19x   53x 53x         1x 53x 53x    
import { LogEncryptMode, LogItem, ResultMsg } from './interface';
import Config from './global';
import LoganDB from './lib/logan-db';
import LogManager from './log-manager';
const ENC_UTF8 = require('crypto-js/enc-utf8');
const ENC_BASE64 = require('crypto-js/enc-base64');
interface LogStringOb {
    l: string;
    iv?: string;
    k?: string;
    v?: number;
}
let LoganDBInstance: LoganDB;
const logQueue: LogItem[] = [];
let logIsSaving: boolean = false;
function base64Encode (text: string): string {
    const textUtf8 = ENC_UTF8.parse(text);
    const textBase64 = textUtf8.toString(ENC_BASE64);
    return textBase64;
}
function stringifyLogItem (logItem: LogItem): string {
    const logOb = {
        t: logItem.logType,
        c: `${encodeURIComponent(logItem.content)}`,
        d: `${Date.now()}`
    };
    return JSON.stringify(logOb);
}
async function saveRecursion (): Promise<void> {
    while (logQueue.length > 0 && !logIsSaving) {
        logIsSaving = true;
        const logItem = logQueue.shift() as LogItem;
        try {
            if (!LogManager.canSave()) {
                throw new Error(ResultMsg.EXCEED_TRY_TIMES);
            }
            if (!LoganDB.idbIsSupported()) {
                throw new Error(ResultMsg.DB_NOT_SUPPORT);
            }
            if (!LoganDBInstance) {
                LoganDBInstance = new LoganDB(Config.get('dbName') as
                    | string
                    | undefined);
            }
            const plainLog = stringifyLogItem(logItem);
            if (logItem.encryptVersion === LogEncryptMode.PLAIN) {
                const logStringOb: LogStringOb = {
                    l: base64Encode(plainLog)
                };
                return await LoganDBInstance.addLog(
                    JSON.stringify(logStringOb)
                );
            } else if (IlogItem.encryptVersion === LogEncryptMode.RSA) {
                const publicKey = Config.get('publicKey');
                const encryptionModule = await import(
                    /* webpackChunkName: "encryption" */ './lib/encryption'
                );
                const cipherOb = encryptionModule.encryptByRSA(
                    plainLog,
                    `${publicKey}`
                );
                const logStringOb: LogStringOb = {
                    l: cipherOb.cipherText,
                    iv: cipherOb.iv,
                    k: cipherOb.secretKey,
                    v: LogEncryptMode.RSA
                };
                return await LoganDBInstance.addLog(
                    JSON.stringify(logStringOb)
                );
            }
        } catch (e) {
            LogManager.errorTrigger();
            (Config.get('errorHandler') as Function)(e);
        } finally {
            logIsSaving = false; //eslint-disable-line require-atomic-updates
            saveRecursion();
        }
    }
}
 
export default function saveLog (logItem: LogItem): void {
    logQueue.push(logItem);
    saveRecursion();
}