UNPKG

@adguard/agtree

Version:
147 lines (146 loc) 5.25 kB
/** * @file Input byte buffer for reading binary data. */ import { ByteBuffer } from './byte-buffer.js'; import { type Storage } from './storage-interface.js'; /** * Input byte buffer for reading binary data. * * @note Internally, this class uses a {@link ByteBuffer} instance, just providing a convenient API for reading data. */ export declare class InputByteBuffer extends ByteBuffer { /** * Current offset in the buffer for reading. */ private offset; /** * Shared native decoder for decoding strings. */ private readonly sharedNativeDecoder; /** * Flag indicating if the current environment is Chromium. * This is used for performance optimizations, because Chromium's TextEncoder/TextDecoder has a relatively * large marshalling overhead for small strings. */ private readonly isChromium; /** * Constructs a new `InputByteBuffer` instance. * * @param chunks Array of chunks to initialize the ByteBuffer with. * @param cloneChunks Flag indicating if the chunks should be cloned. For performance reasons, * its default value is `false`. If the original chunks are guaranteed not to change, * leave this flag as `false` to avoid unnecessary copying. * @param initialOffset Initial offset in the buffer for reading. * * @throws If the specified chunks array is empty. * @throws If the binary schema version in the buffer is not equal to the expected version. * @throws If the initial offset is out of bounds. */ constructor(chunks: Uint8Array[], cloneChunks?: boolean, initialOffset?: number); /** * Creates a new InputByteBuffer instance from a Storage instance by reading chunks from the storage. * * @param storage Storage instance. * @param key Key to read from the storage. * @returns New InputByteBuffer instance. * @note For performance reasons, chunks are passed by reference and not copied. */ static createFromStorage(storage: Storage, key: string): Promise<InputByteBuffer>; /** * Reads a 8-bit unsigned integer from the buffer. * * @returns 8-bit unsigned integer from the buffer. */ readUint8(): number; /** * Reads a 16-bit unsigned integer from the buffer. * * @returns 16-bit unsigned integer from the buffer. */ readUint16(): number; /** * Reads a 32-bit unsigned integer from the buffer at the specified index. * * @param index Index to read the 32-bit unsigned integer from. * * @returns 32-bit unsigned integer from the buffer. */ private readUint32FromIndex; /** * Reads a 32-bit unsigned integer from the buffer. * * @returns 32-bit unsigned integer from the buffer. */ readUint32(): number; /** * Reads schema version from the buffer. * * @returns 32-bit unsigned integer from the buffer. * @note Schema version is always stored at the beginning of the buffer. */ readSchemaVersion(): number; /** * Reads a 32-bit signed integer from the buffer. * * @returns 32-bit signed integer from the buffer. */ readInt32(): number; /** * Reads an optimized unsigned integer from the buffer. * 'Optimized' means that the integer is stored in a variable number of bytes, depending on its value, * so that smaller numbers occupy less space. * * @returns Decoded unsigned integer from the buffer. */ readOptimizedUint(): number; /** * Reads a string from the buffer. * * @returns Decoded string from the buffer. */ readString(): string; /** * Reads a 8-bit unsigned integer from the buffer without advancing the offset. * * @returns 8-bit unsigned integer from the buffer. */ peekUint8(): number; /** * Helper method for asserting the next 8-bit unsigned integer in the buffer. * * @param value Expected value. * @throws If the next value in the buffer is not equal to the expected value. */ assertUint8(value: number): void; /** * Creates a new `InputByteBuffer` instance with the given initial offset. * * @param initialOffset Initial offset for the new buffer. * @param cloneChunks Flag indicating if the chunks should be cloned. For performance reasons, * its default value is `false`. If the original chunks are guaranteed not to change, * leave this flag as `false` to avoid unnecessary copying. * * @returns New `InputByteBuffer` instance with the given initial offset. * * @note This method is useful if you want to read some data from a specific index. */ createCopyWithOffset(initialOffset: number, cloneChunks?: boolean): InputByteBuffer; /** * Gets the current offset in the buffer for reading. * * @returns Current offset in the buffer for reading. */ get currentOffset(): number; /** * Gets the capacity of the buffer. * * @returns Capacity of the buffer. */ get capacity(): number; /** * Gets the chunks of the buffer. * * @returns Chunks of the buffer. */ getChunks(): Uint8Array[]; }