UNPKG

harperdb

Version:

HarperDB is a distributed database, caching service, streaming broker, and application development platform focused on performance and ease of use.

126 lines (125 loc) 5.34 kB
/** * This module provides a Blob class that can be used to store binary data in the database, and can be used to store large binary data in a file * on the server. The Blob class is a subclass of the global Blob class, and can be used in the same way. * The Blob-backed files begin with an 8-byte header: * - The first 2 bytes indicate the type of storage: * - 0: Uncompressed * - 1: Compressed with deflate * - 0xff: Error state (followed by error message). A record can be saved prior to an error in saving a blob, so we must be capable of tracking and even replicating that state * - The next 6 bytes are the size of the content * - While the file is being written, 0xffffffffffff is used as a placeholder to indicate that the file is not finished being written (this nicely matches the logic that if the written content size is less than the indicated content size, it is not finished) * - Note that for compressed data, the size is the uncompressed size, and the compressed size in the file */ import type { LMDBStore } from 'lmdb'; export declare const Blob: { new (blobParts?: BlobPart[], options?: BlobPropertyBag): Blob; prototype: Blob; } | { new (contents: Buffer[]): { content: Buffer; stream(): ReadableStream<any>; text(): Promise<string>; arrayBuffer(): Promise<ArrayBufferLike>; get size(): number; slice(): /*elided*/ any; bytes(): Promise<Buffer>; get type(): string; }; }; export declare let blobsWereEncoded: boolean; declare function InstanceOfBlobWithNoConstructor(): void; declare namespace InstanceOfBlobWithNoConstructor { var prototype: Blob | { content: Buffer; stream(): ReadableStream<any>; text(): Promise<string>; arrayBuffer(): Promise<ArrayBufferLike>; get size(): number; slice(): /*elided*/ any; bytes(): Promise<Buffer>; get type(): string; }; } /** * A blob that is backed by a file, and can be saved to the database as a reference * Note that this is used instead of the native Blob class for a few reasons: * 1. This has the built-in functionality for reading from the file-based storage * 2. This support for streams and asynchronous access to data that may not have a known size ahead of time * 3. This also avoids the Blob constructor which is expensive due to the transferred setup * Harper still supports saving native Blobs, but when they blobs are retrieved from storage, they always use this class. */ declare class FileBackedBlob extends InstanceOfBlobWithNoConstructor { #private; type: string; size: number; finished: Promise<void>; constructor(options?: BlobCreationOptions); on(type: string, callback: (error: Error) => void): void; toJSON(): string | { description: string; }; text(): Promise<string>; bytes(): Promise<Buffer>; arrayBuffer(): Promise<ArrayBuffer>; stream(): ReadableStream; slice(start: number, end: number, type?: string): Blob; save(targetTable: any): Promise<void>; } /** * Delete the file for the blob * @param blob */ export declare function deleteBlob(blob: Blob): void; export declare function setDeletionDelay(delay: number): void; export type BlobCreationOptions = { type?: string; compress?: boolean; flush?: boolean; size?: number; }; export declare function getFileId(blob: Blob): string; export declare function getFilePathForBlob(blob: FileBackedBlob): string; export declare const databasePaths: Map<LMDBStore, string[]>; export declare function getRootBlobPathsForDB(store: LMDBStore): string[]; export declare function deleteRootBlobPathsForDB(store: LMDBStore): Promise<any[]>; /** * Encode blobs with file paths, so that they can be saved to the database * @param callback * @param encodingId * @param objectToClear */ export declare function encodeBlobsWithFilePath<T>(callback: () => T, encodingId: number, store: LMDBStore): T; /** * Encode blobs as buffers, so they can be transferred remotely * @param callback * @param encodingId * @param objectToClear */ export declare function encodeBlobsAsBuffers<T>(callback: () => T): Promise<T>; /** * Decode blobs, creating local storage to hold the blogs and returning a promise that resolves when all the blobs are written to disk * @param callback */ export declare function decodeBlobsWithWrites(callback: () => void, blobCallback?: (blob: Blob) => void): Promise<void> | Promise<void[]>; /** * Decode with a callback for when blobs are encountered, allowing for detecting of blobs * @param callback */ export declare function decodeWithBlobCallback(callback: () => void, blobCallback: (blob: Blob) => void, rootStore?: LMDBStore): void; /** * Decode with a callback for when blobs are encountered, allowing for detecting of blobs * @param callback */ export declare function decodeFromDatabase(callback: () => void, rootStore: LMDBStore): void; /** * Delete blobs in an object, recursively searching for blobs * @param object */ export declare function deleteBlobsInObject(object: any): void; /** * Find all blobs in an object, recursively searching for Blob instances * @param object * @param callback */ export declare function findBlobsInObject(object: any, callback: (blob: Blob) => void): void; export {};