UNPKG

tupleson

Version:

A hackable JSON serializer/deserializer

48 lines 1.41 kB
import { TsonPromiseRejectionError, TsonStreamInterruptedError } from "../asyncErrors.js"; function isPromise(value) { return !!value && typeof value === "object" && "then" in value && typeof value.catch === "function"; } const PROMISE_RESOLVED = 0; const PROMISE_REJECTED = 1; const tsonPromise = { async: true, deserialize: (opts) => { const promise = new Promise((resolve, reject) => { async function _handle() { const next = await opts.reader.read(); opts.close(); if (next.done) { throw new TsonPromiseRejectionError( "Expected promise value, got done" ); } const { value } = next; if (value instanceof TsonStreamInterruptedError) { reject(TsonPromiseRejectionError.from(value)); return; } const [status, result] = value; status === PROMISE_RESOLVED ? resolve(result) : reject(TsonPromiseRejectionError.from(result)); } void _handle().catch(reject); }); promise.catch(() => { }); return promise; }, key: "Promise", serializeIterator(opts) { const value = opts.value.then((value2) => [PROMISE_RESOLVED, value2]).catch((err) => [PROMISE_REJECTED, err]); return async function* generator() { yield await value; }(); }, test: isPromise }; export { tsonPromise }; //# sourceMappingURL=tsonPromise.mjs.map