UNPKG

@webbuf/webbuf

Version:

Rust/WASM optimized buffers for the web, node.js, deno, and bun.

124 lines (123 loc) 4.72 kB
/** * Base32 alphabet types matching the Rust base32 crate */ export type Base32Alphabet = "Crockford" | "Rfc4648" | "Rfc4648Lower" | "Rfc4648Hex" | "Rfc4648HexLower" | "Z"; /** * Options for base32 encoding/decoding */ export interface Base32Options { /** * The alphabet to use for encoding/decoding. * @default "Crockford" */ alphabet?: Base32Alphabet; /** * Whether to use padding (only applies to Rfc4648* alphabets). * @default true */ padding?: boolean; } export declare class WebBuf extends Uint8Array { static concat(list: Uint8Array[]): WebBuf; static alloc(size: number, fill?: number): WebBuf; fill(value: number, start?: number, end?: number): this; slice(start?: number, end?: number): WebBuf; subarray(start?: number, end?: number): WebBuf; /** * Reverse the buffer in place * @returns webbuf */ reverse(): this; clone(): WebBuf; toReverse(): WebBuf; copy(target: WebBuf, targetStart?: number, sourceStart?: number, sourceEnd?: number): number; /** * Return a WebBuf that is a view of the same data as the input Uint8Array * * @param buffer * @returns WebBuf */ static view(buffer: Uint8Array): WebBuf; /** * Create a new WebBuf from a Uint8Array (copy) * @param buffer * @returns webbuf */ static fromUint8Array(buffer: Uint8Array): WebBuf; static fromArray(array: number[]): WebBuf; static fromUtf8(str: string): WebBuf; static fromString(str: string, encoding?: "utf8" | "hex" | "base64"): WebBuf; static FROM_BASE64_ALGO_THRESHOLD: number; static TO_BASE64_ALGO_THRESHOLD: number; static FROM_HEX_ALGO_THRESHOLD: number; static TO_HEX_ALGO_THRESHOLD: number; static fromHexPureJs(hex: string): WebBuf; static fromHexWasm(hex: string): WebBuf; static fromHex(hex: string): WebBuf; toHexPureJs(): string; toHexWasm(): string; toHex(): string; static fromBase64PureJs(b64: string, stripWhitespace?: boolean): WebBuf; static fromBase64Wasm(b64: string, stripWhitespace?: boolean): WebBuf; /** * Convert a base64 string to a Uint8Array. Tolerant of whitespace, but * throws if the string has invalid characters. * * @param b64 * @returns Uint8Array * @throws {Error} if the input string is not valid base64 */ static fromBase64(b64: string, stripWhitespace?: boolean): WebBuf; toBase64PureJs(): string; toBase64Wasm(): string; toBase64(): string; /** * Encode this buffer as a base32 string. * * @param options - Options for encoding * @param options.alphabet - The alphabet to use (default: "Crockford") * @param options.padding - Whether to use padding for Rfc4648* alphabets (default: true) * @returns The base32 encoded string */ toBase32(options?: Base32Options): string; /** * Create a WebBuf from a base32 encoded string. * * @param str - The base32 encoded string * @param options - Options for decoding * @param options.alphabet - The alphabet to use (default: "Crockford") * @param options.padding - Whether the string uses padding for Rfc4648* alphabets (default: true) * @returns The decoded WebBuf */ static fromBase32(str: string, options?: Base32Options): WebBuf; /** * Override Uint8Array.from to return a WebBuf * * @param source An array-like or iterable object to convert to WebBuf * @param mapFn Optional map function to call on every element of the array * @param thisArg Optional value to use as `this` when executing `mapFn` * @returns WebBuf */ static from(source: ArrayLike<number> | Iterable<number> | string, mapFn?: ((v: number, k: number) => number) | string, thisArg?: unknown): WebBuf; toUtf8(): string; toString(encoding?: "utf8" | "hex" | "base64"): string; inspect(): string; toArray(): number[]; compare(other: WebBuf): number; static compare(buf1: WebBuf, buf2: WebBuf): number; equals(other: WebBuf): boolean; write(buf: WebBuf, offset?: number): number; read(offset: number, ext: number): WebBuf; /** * Securely wipe the buffer by filling it with zeros. * * Call this method before releasing references to buffers containing * sensitive data (keys, passwords, etc.) to minimize the window where * sensitive data remains in memory. * * Note: This is a best-effort security measure. JavaScript's JIT compiler * may optimize away the write if it detects the buffer isn't read afterward, * and copies of the data may exist elsewhere in memory. */ wipe(): void; }