UNPKG

tupleson

Version:

A hackable JSON serializer/deserializer

69 lines 2.47 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var tsonPromise_exports = {}; __export(tsonPromise_exports, { tsonPromise: () => tsonPromise }); module.exports = __toCommonJS(tsonPromise_exports); var import_asyncErrors = require("../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 import_asyncErrors.TsonPromiseRejectionError( "Expected promise value, got done" ); } const { value } = next; if (value instanceof import_asyncErrors.TsonStreamInterruptedError) { reject(import_asyncErrors.TsonPromiseRejectionError.from(value)); return; } const [status, result] = value; status === PROMISE_RESOLVED ? resolve(result) : reject(import_asyncErrors.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 }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { tsonPromise }); //# sourceMappingURL=tsonPromise.js.map