"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get base64ToJson () {
        return base64ToJson;
    },
    get base64UrlDecode () {
        return base64UrlDecode;
    },
    get base64UrlEncode () {
        return base64UrlEncode;
    },
    get convertToBase64 () {
        return convertToBase64;
    },
    get depem () {
        return depem;
    },
    get jsonToBase64 () {
        return jsonToBase64;
    },
    get pem () {
        return pem;
    }
});
const pem = (data, banner)=>{
    return `-----BEGIN ${banner}-----\n${data.toString('base64').match(/.{1,64}/g)?.join('\n')}\n-----END ${banner}-----`;
};
const depem = (pemString, expectedBanner)=>{
    const text = pemString.trim();
    const beginRx = /^-{5}\s*BEGIN ?([^-]+)?-{5}\r?\n/;
    const endRx = /\r?\n-{5}\s*END ?([^-]+)?-{5}$/;
    const beginMatch = text.match(beginRx);
    if (!beginMatch || beginMatch[1] !== expectedBanner) {
        throw new Error(`Missing start banner on PEM, expected '-----BEGIN ${expectedBanner}-----'`);
    }
    const endMatch = text.match(endRx);
    if (!endMatch || endMatch[1] !== expectedBanner) {
        throw new Error(`Missing end banner on PEM, expected '-----END ${expectedBanner}-----'`);
    }
    return Buffer.from(text.replace(/^--.+/gm, ''), 'base64');
};
const base64UrlEncode = (input)=>{
    return Buffer.from(input instanceof ArrayBuffer ? Buffer.from(input) : input).toString('base64url');
};
const base64UrlDecode = (input)=>{
    return Buffer.from(input, 'base64url');
};
const base64ToJson = (base64str)=>{
    return JSON.parse(Buffer.from(base64str, 'base64').toString('binary'));
};
const jsonToBase64 = (obj)=>{
    return Buffer.from(JSON.stringify(obj), 'binary').toString('base64');
};
const convertToBase64 = (file)=>new Promise((resolve, reject)=>{
        const reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = ()=>{
            if (typeof reader.result !== 'string') {
                throw new Error('Failed to read file');
            }
            // Extract the base64 data from the data url for saving
            // See note for more details https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL
            const base64Data = reader.result.split('base64,').pop();
            return resolve(base64Data);
        };
        reader.onerror = (conversionError)=>reject(conversionError);
    });

//# sourceMappingURL=encodings.js.map