@adguard/agtree
Version:
Tool set for working with adblock filter lists
133 lines (132 loc) • 4.74 kB
TypeScript
import { ByteBuffer } from './byte-buffer.js';
import { type Storage } from './storage-interface.js';
/**
* Output byte buffer for writing binary data.
*
* @note Internally, this class uses a {@link ByteBuffer} instance, just providing a convenient API for reading data.
*/
export declare class OutputByteBuffer extends ByteBuffer {
/**
* Current offset in the buffer for writing.
*/
private offset;
/**
* Size of the shared buffer for encoding strings in bytes.
* This is a divisor of ByteBuffer.CHUNK_SIZE and experience shows that this value works optimally.
* This is sufficient for most strings that occur in filter lists (we checked average string length in popular
* filter lists).
*/
private static readonly ENCODER_BUFFER_SIZE;
/**
* Length threshold for using a shared buffer for encoding strings.
* This temp buffer is needed because we write the short strings in it
* (so there is no need to constantly allocate a new buffer).
* The reason for dividing ENCODER_BUFFER_SIZE by 4 is to ensure that the encoded string fits in the buffer,
* if we also take into account the worst possible case (each character is encoded with 4 bytes).
*/
private static readonly SHORT_STRING_THRESHOLD;
/**
* Represents the maximum value that can be written as a 'storage optimized' unsigned integer.
* 0x1FFFFFFF means 29 bits — 32 bits minus 3 bits — because the last bit in each byte is a flag indicating
* if there are more bytes (except for the last byte).
*/
static MAX_OPTIMIZED_UINT: number;
/**
* Shared buffer for encoding strings.
*/
private readonly sharedBuffer;
/**
* Shared native encoder for encoding strings.
*/
private readonly sharedNativeEncoder;
/**
* 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 OutputByteBuffer instance.
*/
constructor();
/**
* Writes a 8-bit unsigned integer to the buffer.
*
* @param value Value to write.
* @returns Number of bytes written to the buffer.
*/
writeUint8(value: number): number;
/**
* Writes a 16-bit unsigned integer to the buffer.
*
* @param value Value to write.
* @returns Number of bytes written to the buffer.
*/
writeUint16(value: number): number;
/**
* Writes a 32-bit unsigned integer to the buffer at a specific index.
*
* @param value Value to write.
* @param index Index to write the value to.
* @returns Number of bytes written to the buffer.
*/
private writeUint32ToIndex;
/**
* Writes a 32-bit unsigned integer to the buffer.
*
* @param value Value to write.
* @returns Number of bytes written to the buffer.
*/
writeUint32(value: number): number;
/**
* Writes a 32-bit signed integer to the buffer.
*
* @param value Value to write.
* @returns Number of bytes written to the buffer.
*/
writeInt32(value: number): number;
/**
* Writes a Uint8Array to the byte buffer.
*
* @param buffer Buffer to write.
*/
private writeBuffer;
/**
* Writes a string to the buffer.
*
* @param value Value to write.
* @returns Number of bytes written to the buffer.
*/
writeString(value: string): number;
/**
* Writes chunks to the storage.
*
* @param storage Storage to write the chunks to.
* @param key Key to write the chunks to.
* @note For performance reasons, chunks are passed by reference and not copied.
* @throws If the storage write operation throws.
*/
writeChunksToStorage(storage: Storage, key: string): Promise<void>;
/**
* Writes an 'optimized' unsigned integer to the buffer.
* 'Optimized' means smaller storage usage for smaller numbers.
* Except for the last byte, each byte's most significant bit is a flag indicating if there are more bytes.
*
* @param value Value to write.
* @returns Number of bytes written to the buffer.
* @throws If the value exceeds the 29-bit limit.
*/
writeOptimizedUint(value: number): number;
/**
* Gets the current offset in the buffer for writing.
*
* @returns Current offset in the buffer for writing.
*/
get currentOffset(): number;
/**
* Gets the chunks of the buffer.
*
* @returns Chunks of the buffer.
*/
getChunks(): Uint8Array[];
}