Clean-room Rust rewrite (written without reference to useSend source). Crates: mail-types, mail-core, mail-server, mail-auth, mail-submit, mail-hooks, mail-deliverability; sdks/ts (@scribase/mail); docs/. Each verified by running locally (see plans/scribase-mail-rust-2026-11-12.md). mail-mta, mail-inbound, mail-oci are still in progress and not in this commit. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
49 lines
1.8 KiB
TypeScript
49 lines
1.8 KiB
TypeScript
/** Base64 helpers that work on Node 18+, Bun, Deno and edge runtimes. */
|
|
|
|
export function base64ToBytes(input: string): Uint8Array<ArrayBuffer> {
|
|
const normalized = input.replace(/-/g, "+").replace(/_/g, "/");
|
|
const padded = normalized + "=".repeat((4 - (normalized.length % 4)) % 4);
|
|
const binary = atob(padded);
|
|
const bytes = new Uint8Array(new ArrayBuffer(binary.length));
|
|
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
|
|
return bytes;
|
|
}
|
|
|
|
export function bytesToBase64(bytes: Uint8Array): string {
|
|
let binary = "";
|
|
for (let i = 0; i < bytes.length; i++) binary += String.fromCharCode(bytes[i] as number);
|
|
return btoa(binary);
|
|
}
|
|
|
|
export function base64UrlToBytes(input: string): Uint8Array<ArrayBuffer> {
|
|
return base64ToBytes(input);
|
|
}
|
|
|
|
export function base64UrlDecodeToString(input: string): string {
|
|
return new TextDecoder().decode(base64UrlToBytes(input));
|
|
}
|
|
|
|
export const utf8 = {
|
|
encode: (s: string): Uint8Array<ArrayBuffer> => {
|
|
const encoded = new TextEncoder().encode(s);
|
|
const copy = new Uint8Array(new ArrayBuffer(encoded.byteLength));
|
|
copy.set(encoded);
|
|
return copy;
|
|
},
|
|
};
|
|
|
|
/** Module specifier kept in a variable so bundlers and TypeScript leave it alone. */
|
|
const NODE_CRYPTO = "node:crypto";
|
|
|
|
/**
|
|
* WebCrypto on every supported runtime. Node 18 exposes it under
|
|
* `require("node:crypto").webcrypto` rather than `globalThis.crypto`.
|
|
*/
|
|
export async function subtle(): Promise<SubtleCrypto> {
|
|
const global = globalThis as { crypto?: Crypto };
|
|
if (global.crypto?.subtle) return global.crypto.subtle;
|
|
const nodeCrypto = (await import(/* @vite-ignore */ NODE_CRYPTO)) as { webcrypto?: Crypto };
|
|
if (nodeCrypto.webcrypto?.subtle) return nodeCrypto.webcrypto.subtle;
|
|
throw new Error("WebCrypto is not available in this runtime.");
|
|
}
|