UNPKG

async-streamify

Version:

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

183 lines (182 loc) 6.7 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.AsyncObjectSerializer = void 0; const bufferedAsyncIterable_js_1 = __importDefault(require("../util/bufferedAsyncIterable.js")); const isAsyncIteratable_js_1 = __importDefault(require("../util/isAsyncIteratable.js")); const errorKind = __importStar(require("../kinds/error.js")); /** * Serializes objects containing promises and async iterables into a stream of updates. * Handles nested promises, async iterables, and regular object properties. * * @template TSource - The type of the source object being serialized * * @example * ```typescript * const obj = { * name: "test", * promise: Promise.resolve(42), * async *numbers() { * yield 1; * yield 2; * } * }; * * const serializer = new AsyncObjectSerializer(obj); * * for await (const update of serializer) { * console.log(update); * // Initial: { name: "test", promise: { $promise: 1 }, numbers: { $asyncIterator: 2 } } * // Promise resolved: [1, 42] * // Iterator values: [2, { done: false, value: 1 }], [2, { done: false, value: 2 }] * // Iterator done: [2, { done: true }] * } * ``` */ class AsyncObjectSerializer extends bufferedAsyncIterable_js_1.default { /** * Schedules updates for all active async iterators * @returns void */ scheduleIteratorUpdates() { for (let i = 0; i < this.activeIterators.length; i++) { const thisIter = this.activeIterators[i]; const { idx, iter, nextPromise, done } = thisIter; if (!done && !nextPromise) { thisIter.nextPromise = iter[Symbol.asyncIterator]().next().then((result) => { this.push([idx, { done: result.done, value: this.serializeValue(result.value), }]); if (result.done) { thisIter.done = true; this.decrementActiveCount(); } thisIter.nextPromise = undefined; }); } } } /** * Creates a new AsyncObjectSerializer instance * @param object - The source object to serialize */ constructor(object) { super(); Object.defineProperty(this, "sourceObject", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "serializationIdCounter", { enumerable: true, configurable: true, writable: true, value: 1 }); Object.defineProperty(this, "activeAsyncOperations", { enumerable: true, configurable: true, writable: true, value: 0 }); Object.defineProperty(this, "activeIterators", { enumerable: true, configurable: true, writable: true, value: [] }); this.sourceObject = object; // Option 1) queue all next()'s only when buffer is empty // this.onWait = this.scheduleIteratorUpdates; // Option 2) queue all next()'s on every next call = faster streaming this.onNext = this.scheduleIteratorUpdates; this.push(this.serializeValue(object)); if (this.activeAsyncOperations === 0) { this.close(); } } /** * Gets the next unique serialization ID and increments the active operation count * @returns number */ getNextSerializationId() { this.activeAsyncOperations++; return this.serializationIdCounter++; } /** * Decrements the active operation count and marks as done if no operations remain * @returns void */ decrementActiveCount() { this.activeAsyncOperations--; if (this.activeAsyncOperations === 0) { this.close(); } } /** * Recursively serializes a value, handling promises, async iterables, and nested objects * @param value - The value to serialize * @returns The serialized value */ serializeValue(value) { // Return primitives as-is: null, undefined, numbers, strings, etc. if (typeof value !== "object" || value === null) { return value; } if (Array.isArray(value)) { return value.map((val) => this.serializeValue(val)); } if (value instanceof Promise) { const idx = this.getNextSerializationId(); value.then((resolved) => { this.push([idx, { $resolve: this.serializeValue(resolved) }]); }).catch((error) => { this.push([idx, { $reject: errorKind.maybeSerialize(error) }]); }).finally(() => { this.decrementActiveCount(); }); return { $promise: idx }; } if ((0, isAsyncIteratable_js_1.default)(value)) { const idx = this.getNextSerializationId(); this.activeIterators.push({ idx, iter: value, }); return { $asyncIterator: idx }; } if (value.constructor === Object) { return Object.fromEntries(Object.entries(value).map(([key, val]) => [key, this.serializeValue(val)])); } return value; } } exports.AsyncObjectSerializer = AsyncObjectSerializer; exports.default = AsyncObjectSerializer;