UNPKG

filefive

Version:

SFTP/FTP/Amazon S3 client and dual-panel file manager for macOS and Linux

49 lines (48 loc) 1.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const Local_1 = require("./Local"); const posix_1 = require("node:path/posix"); class Passwords { static async load(dir, onMiss) { this.saveFile = (0, posix_1.join)(dir, 'credentials.json'); this.resolve = onMiss; this.store = new Map(JSON.parse((await (0, Local_1.read)(this.saveFile)).toString()).map(([id, password]) => [id, [password, true]])); } static set(id, password, remember, save) { if (password === false) { this.pending.get(id)?.[1](); } else { this.pending.get(id)?.[0](password); remember && this.store.set(id, [password, save]); save && this.dump(); } } static async get(id, skipMissing = false) { if (this.store.has(id)) { return this.store.get(id)[0]; } if (skipMissing) { return ''; } const p = new Promise((resolve, reject) => this.pending.set(id, [resolve, reject])); this.resolve(id); return p; } static delete(id, save) { const found = this.store.get(id); if (found && (save || found[1] === save)) { this.store.delete(id); found[1] === true && this.dump(); } this.pending.delete(id); } static dump() { (0, Local_1.write)(this.saveFile, JSON.stringify(Array.from(this.store.entries()) .filter(([, [, save]]) => save) .map(([id, [password,]]) => [id, password]))); } } Passwords.saveFile = ''; Passwords.pending = new Map(); exports.default = Passwords;