summaryrefslogtreecommitdiff
path: root/src/routers/vault/utils/flatfile.js
blob: e9d6feec7bf8e5b190c163a1b8d53ab74c9f6a98 (plain) (blame)
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
const execFile = require("child_process").execFile;
const ripgrep = require("ripgrep-bin");

const execAsync = (cmd, par) =>
    new Promise((resolve, reject) =>
        execFile(cmd, par, (error, stdout, stderr) =>
            resolve(error ? "" : (stdout ? stdout : stderr))));

const tmwa_account_regex = new RegExp("^(?<id>[0-9]+)\t(?<name>[^\t]+)\t(?<password>[^\t]+)\t");

const parseAccountLine = (line) => {
    const { groups: account } = tmwa_account_regex.exec(line);
    return {
        id: +account.id,
        name: account.name,
        password: account.password,
    };
}

const findAccount = async (account_id, name) => {
    const regex = `^${account_id}\t${name}\t`;
    const stdout = await execAsync(ripgrep, ["--case-sensitive", `--max-count=1`, regex, "account.txt"]);
    let account = null;
    if (stdout.length)
        account = parseAccountLine(stdout.slice(0, -1).split("\n")[0]);
    return account;
};

module.exports = {
    findAccount,
};