'use strict' import { freeze } from '../freeze/index' import { Sandbox } from '../sandbox/index' import windowOwnPropertyNames from '../sandbox/windowOwnPropertyNames.config' type log = { object: object name: string action: string value?: any } const sandbox = new Sandbox() const randPassword = function (): string { let shadowString = sandbox.shadowWindow.String let shadowMath = sandbox.shadowWindow.Math let shadowArray = sandbox.shadowWindow.Array return (new shadowArray(8 + shadowMath.round(shadowMath.random() * 8))).join(',').split(',').map(() => { return shadowString['fromCharCode'](33 + shadowMath.round(shadowMath.random() * 89)) }).join('') } class Strongbox { public password = randPassword() private log: Function | undefined private tracker = (e: object) => { if (typeof (this.log) === 'function') { this.log(e) } else { console.log('Event Log:', e) } } constructor (insurance: [object], log?: Function) { let that = this let secretKey = this.password // tracker log this.log = log for (let insured of insurance) { let object = insured[0] let props = insured[1] let value = object[props] Object.defineProperty(object, props, { get () { that.password = secretKey = randPassword() return (password: string, setValue: any) => { if (password === secretKey) { if (setValue) { value = setValue } else { return value } } else { let log: log = { object, name: props, action: 'read' } if (setValue) { log.value = setValue log.action = 'write' } that.tracker(log) return } } }, set () { return }, enumerable: true, configurable: false }) freeze(object, true) } return this } lock () { windowOwnPropertyNames.forEach((key: string) => { let type = typeof (window[key]) if (type === 'object' || type === 'function') { try { switch (key) { case 'window': break default: freeze(window[key], true) break } } catch (_e) { // } } }) } } export { Strongbox }