// (c) Vitaliy Filippov, 2024
// License: Mozilla Public License 2.0 or Vitastor Network Public License 1.1

class RequestError extends Error
{
    constructor(code, text, details)
    {
        super();
        this.code = code;
        this.message = text;
        this.details = details;
    }
}

function de64(k)
{
    if (k == null) // null or undefined
        return k;
    return Buffer.from(k, 'base64').toString();
}

function b64(k)
{
    if (k == null) // null or undefined
        return k;
    return Buffer.from(k).toString('base64');
}

function runCallbacks(obj, key, new_value)
{
    const cbs = obj[key];
    obj[key] = new_value;
    if (cbs)
    {
        for (const cb of cbs)
        {
            cb();
        }
    }
}

module.exports = {
    RequestError,
    de64,
    b64,
    runCallbacks,
};
