tupleson
Version:
A hackable JSON serializer/deserializer
79 lines • 2.56 kB
JavaScript
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 tsonAsyncIterable_exports = {};
__export(tsonAsyncIterable_exports, {
tsonAsyncIterable: () => tsonAsyncIterable
});
module.exports = __toCommonJS(tsonAsyncIterable_exports);
var import_asyncErrors = require("../asyncErrors.js");
const ITERATOR_VALUE = 0;
const ITERATOR_ERROR = 1;
const ITERATOR_DONE = 2;
function isAsyncIterable(value) {
return !!value && typeof value === "object" && typeof value[Symbol.asyncIterator] === "function";
}
const tsonAsyncIterable = {
async: true,
deserialize: (opts) => {
return async function* generator() {
let next;
while (next = await opts.reader.read(), !next.done) {
const { value } = next;
if (value instanceof import_asyncErrors.TsonStreamInterruptedError) {
if (value.cause instanceof import_asyncErrors.TsonAbortError) {
opts.close();
return;
}
throw value;
}
switch (value[0]) {
case ITERATOR_DONE: {
opts.close();
return;
}
case ITERATOR_ERROR: {
opts.close();
throw import_asyncErrors.TsonPromiseRejectionError.from(value[1]);
}
case ITERATOR_VALUE: {
yield value[1];
break;
}
}
}
}();
},
key: "AsyncIterable",
serializeIterator: async function* serialize(opts) {
try {
for await (const value of opts.value) {
yield [ITERATOR_VALUE, value];
}
yield [ITERATOR_DONE];
} catch (err) {
yield [ITERATOR_ERROR, err];
}
},
test: isAsyncIterable
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
tsonAsyncIterable
});
//# sourceMappingURL=tsonAsyncIterable.js.map
;