import { Decoder } from "./Decoder"; import { ensureAsyncIterable } from "./utils/stream"; import { defaultDecodeOptions } from "./decode"; import type { ReadableStreamLike } from "./utils/stream"; import type { DecodeOptions } from "./decode"; import type { SplitUndefined } from "./context"; /** * @throws {@link RangeError} if the buffer is incomplete, including the case where the buffer is empty. * @throws {@link DecodeError} if the buffer contains invalid data. */ export async function decodeAsync( streamLike: ReadableStreamLike | BufferSource>, options: DecodeOptions> = defaultDecodeOptions as any, ): Promise { const stream = ensureAsyncIterable(streamLike); const decoder = new Decoder( options.extensionCodec, (options as typeof options & { context: any }).context, options.maxStrLength, options.maxBinLength, options.maxArrayLength, options.maxMapLength, options.maxExtLength, ); return decoder.decodeAsync(stream); } /** * @throws {@link RangeError} if the buffer is incomplete, including the case where the buffer is empty. * @throws {@link DecodeError} if the buffer contains invalid data. */ export function decodeArrayStream( streamLike: ReadableStreamLike | BufferSource>, options: DecodeOptions> = defaultDecodeOptions as any, ): AsyncGenerator { const stream = ensureAsyncIterable(streamLike); const decoder = new Decoder( options.extensionCodec, (options as typeof options & { context: any }).context, options.maxStrLength, options.maxBinLength, options.maxArrayLength, options.maxMapLength, options.maxExtLength, ); return decoder.decodeArrayStream(stream); } /** * @throws {@link RangeError} if the buffer is incomplete, including the case where the buffer is empty. * @throws {@link DecodeError} if the buffer contains invalid data. */ export function decodeMultiStream( streamLike: ReadableStreamLike | BufferSource>, options: DecodeOptions> = defaultDecodeOptions as any, ): AsyncGenerator { const stream = ensureAsyncIterable(streamLike); const decoder = new Decoder( options.extensionCodec, (options as typeof options & { context: any }).context, options.maxStrLength, options.maxBinLength, options.maxArrayLength, options.maxMapLength, options.maxExtLength, ); return decoder.decodeStream(stream); } /** * @deprecated Use {@link decodeMultiStream()} instead. */ export function decodeStream( streamLike: ReadableStreamLike | BufferSource>, options: DecodeOptions> = defaultDecodeOptions as any, ): AsyncGenerator { return decodeMultiStream(streamLike, options); }