Innovenergy_trunk/frontend/node_modules/testcafe-safe-storage/lib/index.js

85 lines
5.6 KiB
JavaScript
Raw Normal View History

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.errors = exports.SafeStorage = void 0;
const file_js_1 = require("./file.js");
const crypto_js_1 = __importDefault(require("./crypto.js"));
const errors = __importStar(require("./errors.js"));
exports.errors = errors;
const has_error_code_js_1 = __importDefault(require("./utils/has-error-code.js"));
function defaultValidator(a) {
return true;
}
function shouldSuppressError(error, suppressor) {
if (typeof suppressor === 'function')
return error instanceof suppressor;
if ((0, has_error_code_js_1.default)(error))
return error.code === suppressor;
return false;
}
class SafeStorage {
constructor(validator = defaultValidator, cryptoContext = new crypto_js_1.default()) {
this.validator = validator;
this.cryptoContext = cryptoContext;
}
async _encrypt(data) {
return this.cryptoContext.encrypt(data);
}
async _decrypt(data) {
return this.cryptoContext.decrypt(data);
}
async _safeLoad() {
const buffer = await (0, file_js_1.load)(file_js_1.FILE_TYPE.STORAGE);
const data = await this._decrypt(buffer);
return JSON.parse(data.toString());
}
async load() {
const data = await this._safeLoad();
if (!this.validator(data))
throw new errors.LoadedDataInvalid();
await this.save(data);
return data;
}
async tryLoad(options = { suppress: [errors.SavedDataNotDetected] }) {
try {
return await this.load();
}
catch (error) {
if (options.suppress && options.suppress.some(suppressor => shouldSuppressError(error, suppressor)))
return options.default;
throw error;
}
}
async save(data) {
const buffer = await this._encrypt(Buffer.from(JSON.stringify(data)));
await (0, file_js_1.save)(file_js_1.FILE_TYPE.STORAGE, buffer);
}
}
exports.SafeStorage = SafeStorage;
exports.default = SafeStorage;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7QUFBQSx1Q0FFbUI7QUFFbkIsNERBQXdDO0FBRXhDLG9EQUFzQztBQWtGN0Isd0JBQU07QUFqRmYsa0ZBQXFEO0FBY3JELFNBQVMsZ0JBQWdCLENBQUssQ0FBVTtJQUNwQyxPQUFPLElBQUksQ0FBQztBQUNoQixDQUFDO0FBRUQsU0FBUyxtQkFBbUIsQ0FBRSxLQUFjLEVBQUUsVUFBMkI7SUFDckUsSUFBSSxPQUFPLFVBQVUsS0FBSyxVQUFVO1FBQ2hDLE9BQU8sS0FBSyxZQUFZLFVBQVUsQ0FBQztJQUV2QyxJQUFJLElBQUEsMkJBQVksRUFBQyxLQUFLLENBQUM7UUFDbkIsT0FBTyxLQUFLLENBQUMsSUFBSSxLQUFLLFVBQVUsQ0FBQztJQUVyQyxPQUFPLEtBQUssQ0FBQztBQUNqQixDQUFDO0FBRUQsTUFBYSxXQUFXO0lBQ3BCLFlBQ1ksWUFBMEIsZ0JBQWdCLEVBQzFDLGdCQUErQixJQUFJLG1CQUFhLEVBQUU7UUFEbEQsY0FBUyxHQ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