import type { Refkey } from "@alloy-js/core";
import { code, refkey } from "@alloy-js/core";
import type { Children } from "@alloy-js/core/jsx-runtime";
import * as ts from "@alloy-js/typescript";
export function getEncodeUint8ArrayRef(): Refkey {
return refkey("encodeUint8Array");
}
export function EncodeUint8Array(): Children {
const valueRef = refkey();
const encodingRef = refkey();
const key = getEncodeUint8ArrayRef();
return (
{code`
if (!${valueRef}) {
return ${valueRef} as any;
}
return Buffer.from(${valueRef}).toString(${encodingRef});
`}
);
}
export function getDecodeUint8ArrayRef(): Refkey {
return refkey("decodeUint8Array");
}
export function DecodeBase64(): Children {
const key = getDecodeUint8ArrayRef();
const valueRef = refkey();
return (
{code`
if(!${valueRef}) {
return ${valueRef} as any;
}
// Normalize Base64URL to Base64
const base64 = ${valueRef}.replace(/-/g, '+').replace(/_/g, '/')
.padEnd(${valueRef}.length + (4 - (${valueRef}.length % 4)) % 4, '=');
return new Uint8Array(Buffer.from(base64, 'base64'));
`}
);
}