import { lib as KeetaNetLib } from '@keetanetwork/keetanet-client';
export const Buffer = KeetaNetLib.Utils.Buffer.Buffer;
/*
 * Because our public interfaces are ArrayBuffers we often need to convert
 * Buffers to ArrayBuffers -- an alias to the Node function to do that
 */
export const bufferToArrayBuffer = KeetaNetLib.Utils.Helper.bufferToArrayBuffer.bind(KeetaNetLib.Utils.Helper);
function toBuffer(src) {
    if (ArrayBuffer.isView(src)) {
        // Zero-copy: Buffer will reference the same ArrayBuffer range.
        // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
        return Buffer.from(src.buffer, src.byteOffset, src.byteLength);
    }
    // src is ArrayBuffer
    // Zero-copy: shares memory with the ArrayBuffer
    // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
    return Buffer.from(src);
}
/*
 * Helper to convert ArrayBuffer back to Buffer
 */
export function arrayBufferToBuffer(arrayBuffer) {
    return (toBuffer(arrayBuffer));
}
/*
 * Converts a Buffer backed by ArrayBufferLike storage into one backed by an ArrayBuffer.
 */
export function arrayBufferLikeToBuffer(buffer) {
    if (ArrayBuffer.isView(buffer)) {
        return (toBuffer(buffer));
    }
    // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
    return (toBuffer(buffer));
}
/**
 * Regular expression to match base64 input.
 */
const BASE64_INPUT_REGEX = /^[-_A-Za-z0-9+/=\s]*$/;
/**
 * Decode a base64 string into a {@link Buffer}.
 */
export function decodeBase64Strict(input) {
    if (!BASE64_INPUT_REGEX.test(input)) {
        return (undefined);
    }
    const decoded = Buffer.from(input, 'base64');
    if (decoded.length === 0) {
        return (undefined);
    }
    return (decoded);
}
//# sourceMappingURL=buffer.js.map