UNPKG

async-streamify

Version:

Stream and serialize nested promises and async iterables over HTTP, workers, etc

75 lines 2.61 kB
/** * Deserializes objects that were serialized using AsyncObjectSerializer. * Reconstructs promises and async iterables from their serialized representations. * * @template TTarget - The expected type of the deserialized object * * @example * ```typescript * const serializedStream = new AsyncObjectSerializer({ * value: Promise.resolve(42), * stream: (async function*() { yield 1; yield 2; })() * }); * * const deserializer = new AsyncObjectDeserializer(serializedStream); * const result = await deserializer.deserialize(); * * console.log(await result.value); // 42 * for await (const num of result.stream) { * console.log(num); // 1, 2 * } * ``` */ export declare class AsyncObjectDeserializer<TTarget extends object> { private iter; private deserializedRoot?; private promises; private activeIterators; /** * Optional callback that is called when the root object is first deserialized */ onSetRoot?: (root: TTarget) => void; /** * Creates a new AsyncObjectDeserializer instance * @param iter - The async iterable containing serialized object chunks */ constructor(iter: AsyncIterable<object>); /** * Processes the serialized stream and reconstructs the original object * with its promises and async iterables * * @returns Promise<TTarget | undefined> The deserialized object */ deserialize(): Promise<TTarget | undefined>; /** * Recursively deserializes a value, reconstructing promises, async iterables, * and nested objects from their serialized form * * @param serializedValue - The serialized value to deserialize * @returns The deserialized value */ private deserializeValue; } /** * Helper function to deserialize an object stream and return a promise that * resolves with the root object as soon as it's available * * @template T - The expected type of the deserialized object * @param iter - The async iterable containing serialized object chunks * @returns Promise<T> A promise that resolves with the deserialized root object * * @example * ```typescript * const serializedStream = new AsyncObjectSerializer({ * name: "test", * value: Promise.resolve(42) * }); * * const obj = await deserialize<{ name: string, value: Promise<number> }>(serializedStream); * console.log(obj.name); // "test" * console.log(await obj.value); // 42 * ``` */ export declare function deserialize<T extends object>(iter: AsyncIterable<object>): Promise<T>; export default deserialize; //# sourceMappingURL=asyncObjectDeserializer.d.ts.map