UNPKG

askexperts

Version:

AskExperts SDK: build and use AI experts - ask them questions and pay with bitcoin on an open protocol

104 lines (103 loc) 3.28 kB
/** * StreamWriter implementation for NIP-173 (Streaming Over Nostr) */ import { SimplePool } from "nostr-tools"; import { Compression } from "./compression.js"; import { Encryption } from "./encryption.js"; import { StreamMetadata, StreamWriterConfig, StreamStatus } from "./types.js"; /** * StreamWriter for NIP-173 streams * Handles writing data to a Nostr stream, with support for batching, compression, and encryption */ export declare class StreamWriter { private metadata; private pool; private config; private compression; private encryption; private compressor; private senderPrivkey; private chunkIndex; private batchTimer; private lastFlushTime; private isDone; private isError; private currentChunkSize; private publishPromises; private lastChunkId; /** * Creates a new StreamWriter * * @param metadata - Stream metadata * @param pool - SimplePool instance for relay communication * @param config - Configuration options * @param compression - Optional custom compression implementation */ constructor(metadata: StreamMetadata, pool: SimplePool, senderPrivkey: Uint8Array, config?: StreamWriterConfig, compression?: Compression, encryption?: Encryption); get status(): StreamStatus; private isBinary; private ensureCompressor; /** * Compresses data and updates the current chunk size * * @param data - Data to compress * @throws CompressionSizeLimitExceeded if the data would exceed the max chunk size */ private compress; /** * Writes data to the stream * * @param data - Data to write (string or Uint8Array) * @param done - Whether this is the last chunk */ /** * Splits data into manageable chunks while preserving data type * * @param data - The data to split (string or Uint8Array) * @param maxPartSize - Maximum size of each part * @returns Array of parts with the same type as the input */ private splitData; maxChunkSize(): Promise<number>; maxPartSize(): Promise<number>; write(data: string | Uint8Array, done?: boolean): Promise<void>; /** * Sends an error status and closes the stream * * @param code - Error code * @param message - Error message */ error(code: string, message: string): Promise<void>; private waitPublished; /** * Flushes the current compressor and sends the compressed data as a chunk * * @returns Promise resolving to the event ID of the sent chunk */ private flushCompressor; /** * Sends a compressed chunk of data as a kind:20173 event * * @param compressedContent - Compressed content to send * @param status - Chunk status */ private sendCompressedChunk; /** * Sends an error chunk * * @param errorContent - Error content as JSON string */ private sendErrorChunk; /** * Creates and publishes an event * * @param content - Event content * @param status - Chunk status */ private createAndPublishEvent; /** * Disposes of the stream and releases resources * Implements the disposable pattern */ [Symbol.dispose](): void; }