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

89 lines
8.1 KiB
JavaScript

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.save = exports.load = exports.FILE_TYPE = exports.CRYPTO_DIR = exports.STORAGE_DIR = void 0;
const crypto_1 = __importDefault(require("crypto"));
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
const os_1 = __importDefault(require("os"));
const util_1 = require("util");
const errors_1 = require("./errors");
const has_error_code_1 = __importDefault(require("./utils/has-error-code"));
const readFile = (0, util_1.promisify)(fs_1.default.readFile);
const writeFile = (0, util_1.promisify)(fs_1.default.writeFile);
const mkdir = (0, util_1.promisify)(fs_1.default.mkdir);
const readdir = (0, util_1.promisify)(fs_1.default.readdir);
const rm = (0, util_1.promisify)(fs_1.default.rm);
exports.STORAGE_DIR = '.tcss';
exports.CRYPTO_DIR = '.tcpk';
var FILE_TYPE;
(function (FILE_TYPE) {
FILE_TYPE[FILE_TYPE["STORAGE"] = 0] = "STORAGE";
FILE_TYPE[FILE_TYPE["NONCE"] = 1] = "NONCE";
FILE_TYPE[FILE_TYPE["AUTH_TAG"] = 2] = "AUTH_TAG";
FILE_TYPE[FILE_TYPE["PRIVATE_KEY"] = 3] = "PRIVATE_KEY";
FILE_TYPE[FILE_TYPE["PASSPHRASE"] = 4] = "PASSPHRASE";
})(FILE_TYPE = exports.FILE_TYPE || (exports.FILE_TYPE = {}));
// TODO: Use OS APIs to secretely store PASSPHRASE, AUTH_TAG, NONCE
const FILENAME_TEMPLATES = {
[FILE_TYPE.STORAGE]: (seed) => path_1.default.join(os_1.default.homedir(), exports.STORAGE_DIR, `storage-${seed}`),
[FILE_TYPE.NONCE]: (seed) => path_1.default.join(os_1.default.homedir(), exports.CRYPTO_DIR, `n-${seed}`),
[FILE_TYPE.AUTH_TAG]: (seed) => path_1.default.join(os_1.default.homedir(), exports.CRYPTO_DIR, `at-${seed}`),
[FILE_TYPE.PRIVATE_KEY]: (seed) => path_1.default.join(os_1.default.homedir(), exports.CRYPTO_DIR, `pk-${seed}`),
[FILE_TYPE.PASSPHRASE]: (seed) => path_1.default.join(os_1.default.homedir(), `.tcpp-${seed}`),
};
async function ensureDir(filename) {
await mkdir(path_1.default.dirname(filename), { recursive: true });
}
async function readNamesDir(path) {
try {
return await readdir(path);
}
catch (error) {
if ((0, has_error_code_1.default)(error) && error.code === 'ENOENT')
throw new errors_1.SavedDataNotDetected();
throw error;
}
}
async function detectNames(type) {
const template = FILENAME_TEMPLATES[type]('');
const dirname = path_1.default.dirname(template);
const basenames = await readNamesDir(dirname);
const filenames = basenames.filter(name => name.includes(path_1.default.basename(template)));
return filenames.map(name => path_1.default.join(dirname, name));
}
async function detectName(type) {
const names = await detectNames(type);
if (!names.length)
throw new errors_1.SavedDataNotDetected();
if (names.length > 1)
throw new errors_1.MultipleSavedDataDetected();
return names[0];
}
async function generateName(type) {
return FILENAME_TEMPLATES[type](crypto_1.default.randomUUID());
}
async function load(type) {
const name = await detectName(type);
const data = await readFile(name);
await remove(name);
return data;
}
exports.load = load;
async function save(type, data) {
const name = await generateName(type);
await ensureDir(name);
await removeAll(type);
await writeFile(name, data);
}
exports.save = save;
async function remove(name) {
await rm(name, { force: true });
}
async function removeAll(type) {
const names = await detectNames(type);
await Promise.all(names.map(name => remove(name)));
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZmlsZS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3NyYy9maWxlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7OztBQUFBLG9EQUE0QjtBQUM1QixnREFBd0I7QUFDeEIsNENBQW9CO0FBQ3BCLDRDQUFvQjtBQUNwQiwrQkFBaUM7QUFDakMscUNBQTJFO0FBQzNFLDRFQUFrRDtBQUdsRCxNQUFNLFFBQVE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