All files / src save_log.ts

97.62% Statements 41/42
87.5% Branches 14/16
100% Functions 5/5
97.37% Lines 37/38

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 771x 1x 1x 1x   1x   1x               1x 1x   35x 35x 35x     37x         37x     74x 37x 37x   37x     37x 39x 35x     35x 2x 2x 4x 2x 2x           2x     3x   37x 74x         1x 78x 4x   37x 1x   37x 74x      
import { LogEncryptMode, LogItem, ResultMsg } from './interface'
import Config from './global'
import LoganDB from './lib/logan_db'
import LogManager from './log_manager';
// @ts-ignore
const ENC_UTF8 = require('crypto-js/enc-utf8')
// @ts-ignore
const ENC_BASE64 = require('crypto-js/enc-base64');
interface LogStringOb {
    l: string,
    iv?: string,
    k?: string,
    v?: number 
}
let LoganDBInstance: LoganDB;
let logQueue: LogItem[] = [];
let logIsSaving: boolean = false;
function base64Encode(text: string) {
    var textUtf8 = ENC_UTF8.parse(text)
    var textBase64 = textUtf8.toString(ENC_BASE64)
    return textBase64
}
function stringifyLogItem(logItem: LogItem) {
    let logOb = {
        't': logItem.logType,
        'c': `${encodeURIComponent(logItem.content)}`,
        'd': `${Date.now()}`
    }
    return JSON.stringify(logOb)
}
async function saveRecursion() {
    while (logQueue.length > 0 && !logIsSaving) {
        logIsSaving = true;
        let logItem = logQueue.shift() as LogItem;
        try {
            Iif (!LogManager.canSave()) {
                throw new Error(ResultMsg.EXCEED_TRY_TIMES)
            }
            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) {
            throw e
        } finally {
            logIsSaving = false;
            await saveRecursion();
        }
    }
}
 
export default async function saveLog(logItem: LogItem) {
    if (!LoganDB.idbIsSupported()) {
        throw new Error(ResultMsg.DB_NOT_SUPPORT);
    } else {
        if (!LoganDBInstance) {
            LoganDBInstance = new LoganDB(Config.get('dbName') as string | undefined);
        }
        logQueue.push(logItem);
        await saveRecursion();
    }
}