/** * Buffer - Dynamic byte array wrapper for PDF data manipulation * * This module provides a flexible buffer implementation for working with binary PDF data. * Buffers are used throughout the library for reading, writing, and manipulating * PDF content, streams, and binary resources. * * This implementation mirrors the Rust `fitz::buffer::Buffer` for 100% API compatibility * and wraps Node.js Buffer for efficient memory management. * * @module buffer * @example * ```typescript * import { Buffer } from 'micropdf'; * * // Create from string * const buf = Buffer.fromString('Hello, PDF!'); * * // Append data * buf.append(Buffer.fromString(' More text.')); * * // Get as string * console.log(buf.toString()); // "Hello, PDF! More text." * * // Get raw bytes * const bytes = buf.toUint8Array(); * * // Get size * console.log(buf.length); // 24 * ``` */ import { type BufferLike, isBufferLike } from './types.js'; export { BufferLike, isBufferLike }; /** * A dynamic byte buffer for PDF data manipulation. * * Buffer provides efficient storage and manipulation of binary data. It's used * throughout the library for PDF streams, content, images, and other binary resources. * * **Key Features:** * - Dynamic resizing as data is appended * - Zero-copy conversion to/from Node.js Buffer * - String encoding/decoding support * - Slice and copy operations * - Compatible with standard Node.js Buffer operations * * Mirrors the Rust `Buffer` implementation with `bytes` crate semantics. * * @class Buffer * @example * ```typescript * // Create empty buffer * const buf1 = Buffer.create(); * * // Create from string * const buf2 = Buffer.fromString('Hello'); * * // Create from bytes * const bytes = new Uint8Array([72, 101, 108, 108, 111]); * const buf3 = Buffer.fromUint8Array(bytes); * * // Append data * buf1.append(buf2); * buf1.append(Buffer.fromString(' World!')); * * // Extract data * console.log(buf1.toString()); // "Hello World!" * console.log(buf1.length); // 12 * * // Slice * const hello = buf1.slice(0, 5); * console.log(hello.toString()); // "Hello" * * // Clear * buf1.clear(); * console.log(buf1.length); // 0 * ``` */ export declare class Buffer { /** * The underlying Node.js Buffer containing the data. * @private * @type {globalThis.Buffer} */ private _data; /** * Creates a new Buffer instance. * * **Note**: Use static factory methods instead of calling this constructor directly. * * @private * @param {globalThis.Buffer} data - The underlying Node.js Buffer */ private constructor(); /** * Creates a new empty buffer with optional initial capacity. * * The capacity parameter is a hint for initial memory allocation. The buffer * will automatically grow as needed when data is appended. * * @static * @param {number} [capacity=0] - Initial capacity in bytes (optional) * @returns {Buffer} A new empty buffer * @example * ```typescript * // Create empty buffer * const buf1 = Buffer.create(); * * // Create with initial capacity * const buf2 = Buffer.create(1024); // Reserve 1KB * ``` */ static create(capacity?: number): Buffer; /** * Creates a buffer from a Node.js Buffer (zero-copy). * * This operation wraps the existing Buffer without copying data, making it * very efficient. Modifications to the original Buffer will be visible in * the MicroPDF Buffer and vice versa. * * @static * @param {globalThis.Buffer} data - The Node.js Buffer to wrap * @returns {Buffer} A new Buffer wrapping the provided data * @example * ```typescript * const nodeBuffer = Buffer.from('Hello'); * const pdfBuffer = Buffer.fromBuffer(nodeBuffer); * console.log(pdfBuffer.toString()); // "Hello" * ``` */ static fromBuffer(data: globalThis.Buffer): Buffer; /** * Creates a buffer from a Uint8Array. * * The data is copied into a new Node.js Buffer. * * @static * @param {Uint8Array} data - The byte array to copy * @returns {Buffer} A new Buffer containing the data * @example * ```typescript * const bytes = new Uint8Array([72, 101, 108, 108, 111]); * const buf = Buffer.fromUint8Array(bytes); * console.log(buf.toString()); // "Hello" * ``` */ static fromUint8Array(data: Uint8Array): Buffer; /** * Creates a buffer from an ArrayBuffer. * * Useful for working with binary data from various Web APIs and file operations. * * @static * @param {ArrayBuffer} data - The ArrayBuffer to convert * @returns {Buffer} A new Buffer containing the data * @example * ```typescript * const arrayBuffer = new ArrayBuffer(5); * const view = new Uint8Array(arrayBuffer); * view.set([72, 101, 108, 108, 111]); * const buf = Buffer.fromArrayBuffer(arrayBuffer); * console.log(buf.toString()); // "Hello" * ``` */ static fromArrayBuffer(data: ArrayBuffer): Buffer; /** * Creates a buffer from a string with specified encoding. * * Supports all standard Node.js buffer encodings including UTF-8, ASCII, * Base64, Hex, and more. * * @static * @param {string} str - The string to encode * @param {BufferEncoding} [encoding='utf-8'] - The character encoding to use * @returns {Buffer} A new Buffer containing the encoded string * @example * ```typescript * // UTF-8 (default) * const buf1 = Buffer.fromString('Hello'); * * // ASCII * const buf2 = Buffer.fromString('Hello', 'ascii'); * * // Base64 * const buf3 = Buffer.fromString('SGVsbG8=', 'base64'); * console.log(buf3.toString()); // "Hello" * * // Hex * const buf4 = Buffer.fromString('48656c6c6f', 'hex'); * console.log(buf4.toString()); // "Hello" * ``` */ static fromString(str: string, encoding?: BufferEncoding): Buffer; /** * Create a buffer from base64-encoded data */ static fromBase64(data: string): Buffer; /** * Create a buffer from hex-encoded data */ static fromHex(data: string): Buffer; /** * Create a buffer from various input types */ static from(data: BufferLike): Buffer; /** * Get the length of the buffer in bytes */ get length(): number; /** * Check if the buffer is empty */ get isEmpty(): boolean; /** * Get the buffer capacity */ get capacity(): number; /** * Get the buffer data as a Node.js Buffer */ toNodeBuffer(): globalThis.Buffer; /** * Get the buffer data as a Uint8Array (copy) * * Creates a new Uint8Array with a copy of the buffer data. * Use `toUint8ArrayView()` for a zero-copy view if you don't need to modify it. */ toUint8Array(): Uint8Array; /** * Get a zero-copy Uint8Array view of the buffer data * * WARNING: This returns a view, not a copy. Modifying the view will * modify the original buffer. The view becomes invalid if the buffer * is modified (append, resize, etc.) or freed. * * Use this for read-only operations where performance matters. */ toUint8ArrayView(): Uint8Array; /** * Get the buffer data as an ArrayBuffer */ toArrayBuffer(): ArrayBuffer; /** * Get the buffer data as a string * * For UTF-8 encoding with buffers larger than 1KB, uses TextDecoder * which is more efficient than Node's toString() for large strings. */ toString(encoding?: BufferEncoding): string; /** * Get the buffer data as base64-encoded string */ toBase64(): string; /** * Get the buffer data as hex-encoded string */ toHex(): string; /** * Get the buffer data as a number array */ toArray(): number[]; /** * Resize the buffer to the specified size */ resize(newLength: number): this; /** * Clear all data from the buffer */ clear(): this; /** * Append data to the buffer */ append(data: BufferLike): this; /** * Append a single byte to the buffer */ appendByte(byte: number): this; /** * Append a string to the buffer (UTF-8 encoded) */ appendString(str: string, encoding?: BufferEncoding): this; /** * Append a 16-bit integer in little-endian format */ appendInt16LE(value: number): this; /** * Append a 32-bit integer in little-endian format */ appendInt32LE(value: number): this; /** * Append a 16-bit integer in big-endian format */ appendInt16BE(value: number): this; /** * Append a 32-bit integer in big-endian format */ appendInt32BE(value: number): this; /** * Append a 16-bit unsigned integer in little-endian format */ appendUInt16LE(value: number): this; /** * Append a 32-bit unsigned integer in little-endian format */ appendUInt32LE(value: number): this; /** * Append a 16-bit unsigned integer in big-endian format */ appendUInt16BE(value: number): this; /** * Append a 32-bit unsigned integer in big-endian format */ appendUInt32BE(value: number): this; /** * Append a float in little-endian format */ appendFloatLE(value: number): this; /** * Append a float in big-endian format */ appendFloatBE(value: number): this; /** * Append a double in little-endian format */ appendDoubleLE(value: number): this; /** * Append a double in big-endian format */ appendDoubleBE(value: number): this; /** * Get a slice of the buffer */ slice(start: number, end?: number): Buffer; /** * Split the buffer at the given index */ splitAt(mid: number): [Buffer, Buffer]; /** * Get a byte at the specified index */ at(index: number): number | undefined; /** * Set a byte at the specified index */ set(index: number, value: number): this; /** * Read a 16-bit unsigned integer at offset (big-endian) */ readUInt16BE(offset: number): number; /** * Read a 32-bit unsigned integer at offset (big-endian) */ readUInt32BE(offset: number): number; /** * Read a 16-bit unsigned integer at offset (little-endian) */ readUInt16LE(offset: number): number; /** * Read a 32-bit unsigned integer at offset (little-endian) */ readUInt32LE(offset: number): number; /** * Read a 16-bit signed integer at offset (big-endian) */ readInt16BE(offset: number): number; /** * Read a 32-bit signed integer at offset (big-endian) */ readInt32BE(offset: number): number; /** * Read a 16-bit signed integer at offset (little-endian) */ readInt16LE(offset: number): number; /** * Read a 32-bit signed integer at offset (little-endian) */ readInt32LE(offset: number): number; /** * Read a float at offset (big-endian) */ readFloatBE(offset: number): number; /** * Read a float at offset (little-endian) */ readFloatLE(offset: number): number; /** * Read a double at offset (big-endian) */ readDoubleBE(offset: number): number; /** * Read a double at offset (little-endian) */ readDoubleLE(offset: number): number; /** * Compute MD5 digest of buffer contents */ md5Digest(): Uint8Array; /** * Compute SHA-256 digest of buffer contents */ sha256Digest(): Uint8Array; /** * Check equality with another buffer */ equals(other: Buffer | BufferLike): boolean; /** * Compare with another buffer * Returns -1, 0, or 1 */ compare(other: Buffer | BufferLike): number; /** * Find index of a byte or pattern */ indexOf(value: number | BufferLike, start?: number): number; /** * Check if buffer includes a byte or pattern */ includes(value: number | BufferLike, start?: number): boolean; /** * Iterate over bytes */ [Symbol.iterator](): Iterator; /** * Get entries iterator */ entries(): IterableIterator<[number, number]>; /** * Get keys iterator */ keys(): IterableIterator; /** * Get values iterator */ values(): IterableIterator; /** * Create a copy of the buffer */ clone(): Buffer; } /** * A reader for consuming buffer contents */ export declare class BufferReader { private readonly data; private position; constructor(buffer: Buffer | BufferLike); /** * Get the current read position */ get pos(): number; /** * Get the number of bytes remaining */ get remaining(): number; /** * Check if we've reached the end */ get isEof(): boolean; /** * Peek at the next byte without consuming it */ peek(): number | null; /** * Read a single byte */ readByte(): number | null; /** * Read bytes into a buffer */ read(length: number): Uint8Array; /** * Read exactly n bytes, or throw if not enough data */ readExact(length: number): Uint8Array; /** * Read a 16-bit unsigned integer (big-endian) */ readUInt16BE(): number; /** * Read a 32-bit unsigned integer (big-endian) */ readUInt32BE(): number; /** * Read a 16-bit unsigned integer (little-endian) */ readUInt16LE(): number; /** * Read a 32-bit unsigned integer (little-endian) */ readUInt32LE(): number; /** * Read a 24-bit unsigned integer (big-endian) */ readUInt24BE(): number; /** * Seek to a position */ seek(pos: number): this; /** * Skip n bytes */ skip(n: number): this; /** * Read a line (up to and including newline) */ readLine(): Uint8Array | null; /** * Read a line as string */ readLineString(encoding?: BufferEncoding): string | null; } /** * A writer that accumulates data into a buffer */ export declare class BufferWriter { private data; private position; constructor(capacity?: number); /** * Get the current length */ get length(): number; /** * Check if empty */ get isEmpty(): boolean; /** * Ensure capacity */ private ensureCapacity; /** * Write bytes */ write(data: BufferLike): this; /** * Write a single byte */ writeByte(value: number): this; /** * Write a 16-bit unsigned integer (big-endian) */ writeUInt16BE(value: number): this; /** * Write a 32-bit unsigned integer (big-endian) */ writeUInt32BE(value: number): this; /** * Write a 16-bit unsigned integer (little-endian) */ writeUInt16LE(value: number): this; /** * Write a 32-bit unsigned integer (little-endian) */ writeUInt32LE(value: number): this; /** * Write a string */ writeString(str: string, encoding?: BufferEncoding): this; /** * Get the accumulated data as a slice */ toSlice(): Uint8Array; /** * Convert to Buffer */ toBuffer(): Buffer; /** * Clear the writer */ clear(): this; } //# sourceMappingURL=buffer.d.ts.map