json-seq-stream
Version:
application/json-seq support stream / async iterator library
327 lines (326 loc) • 8.27 kB
JavaScript
function E({ splitter: r, chunkEndSplit: e }) {
const { separator: t, checker: s } = R(r), n = [];
return {
args: [
{
transform(u, a) {
e || (u = [...n.splice(0, n.length), u].join(""));
for (const o of u.split(t))
if (!(o.length <= 0)) {
if (s.test(o)) {
g(n, a);
continue;
}
n.push(o);
}
e && g(n, a);
},
flush(u) {
g(n, u);
}
}
]
};
}
function R(r) {
typeof r == "string" && (r = [r]);
const e = r.map((t) => RegExp.escape(t)).join("|");
return {
separator: new RegExp(`(${e})`, "ug"),
checker: new RegExp(`^(${e})$`)
};
}
function g(r, e) {
if (r.length <= 0) return;
const t = r.splice(0, r.length).join("");
e.enqueue(t);
}
class J extends TransformStream {
constructor(e) {
const { args: t } = E(e);
super(...t);
}
}
function k({ delimiter: r, start: e, end: t, skip: s }) {
let n = !0;
return {
args: [
{
transform(u, a) {
s && u.length <= 0 || (n ? (e && a.enqueue(r), n = !1) : a.enqueue(r), a.enqueue(u));
},
flush(u) {
n || t && u.enqueue(r);
}
}
]
};
}
class O extends TransformStream {
constructor(e) {
const { args: t } = k(e);
super(...t);
}
}
function L({ parse: r, errorFallback: e }) {
return { args: [
{
transform(s, n) {
try {
const c = r(s);
c && n.enqueue(c);
} catch (c) {
if (!e) return;
const u = n.error.bind(n), a = n.enqueue.bind(n), o = e(c, { chunk: s, error: u, enqueue: a });
if (o?.then)
return o.catch(() => {
});
}
}
}
] };
}
class F extends TransformStream {
constructor(e) {
const { args: t } = L(e);
super(...t);
}
}
class m extends F {
constructor(e) {
let { errorFallback: t, parse: s } = e ?? {};
s ??= JSON.parse, t ??= !1, super({ parse: s, errorFallback: t });
}
}
const X = m, f = {
begin: "begin",
end: "end",
record: "record",
init: "init",
flush: "flush"
};
function y({ begin: r, end: e, chunkEndSplit: t, fallbackSkip: s }) {
const n = [], c = new RegExp(`(${RegExp.escape(r)}|${RegExp.escape(e)})`, "u"), u = s === !0 ? () => !0 : s === !1 ? () => !1 : s;
let a = f.init;
return {
args: [
{
transform(i, d) {
t || (i = [...n.splice(0, n.length), i].join(""));
for (const l of i.split(c)) {
if (l === r) {
const p = f.begin;
a === f.end || a === void 0 ? (S(n, d), a = p) : u(l, a, p) || (S(n, d), a = p);
continue;
}
if (l === e) {
const p = f.end;
a === f.record || a === f.begin ? (S(n, d), a = p) : u(l, a, p) || (S(n, d), a = p);
continue;
}
const h = f.record;
a === f.begin || a === f.record ? (n.push(l), a = h) : u(l, a, f.record) || (n.push(l), a = h);
}
t && (S(n, d), a = f.end);
},
flush(i) {
u("", a, f.flush) || S(n, i);
}
}
]
};
}
function S(r, e) {
if (r.length <= 0) return;
const t = r.splice(0, r.length).join("");
t.length <= 0 || e.enqueue(t);
}
class j extends TransformStream {
constructor(e) {
const { args: t } = y(e);
super(...t);
}
}
const q = "", w = `
`, N = q, D = w;
class b extends j {
constructor(e) {
let { begin: t, end: s, chunkEndSplit: n, fallbackSkip: c } = e ?? {};
t ??= N, s ??= D, n ??= !1, c ??= !1, super({ begin: t, end: s, chunkEndSplit: n, fallbackSkip: c });
}
}
const Y = b, Z = b;
function $(r) {
const { label: e, fatal: t, begin: s, end: n, parse: c, errorFallback: u } = r, a = new TextDecoderStream(e, { fatal: t }), o = new b({ begin: s, end: n }), i = new m({ parse: c, errorFallback: u }), { writable: d } = a, l = a.readable.pipeThrough(o).pipeThrough(i);
return { writable: d, readable: l };
}
class P {
#e;
#t;
get writable() {
return this.#e;
}
get readable() {
return this.#t;
}
constructor(e = {}) {
({
readable: this.#t,
writable: this.#e
} = $(e));
}
}
const ee = P;
function _({ stringify: r, errorFallback: e }) {
return { args: [
{
transform(s, n) {
try {
const c = r(s);
(c?.length ?? 0) > 0 && n.enqueue(c);
} catch (c) {
if (!e) return;
const u = n.error.bind(n), a = n.enqueue.bind(n), o = e(c, { chunk: s, error: u, enqueue: a });
if (o?.then)
return o.catch(() => {
});
}
}
}
] };
}
class C extends TransformStream {
constructor(e) {
const { args: t } = _(e);
super(...t);
}
}
class x extends C {
constructor(e) {
let { errorFallback: t, stringify: s } = e ?? {};
t ??= !1, s ??= JSON.stringify, super({ stringify: s, errorFallback: t });
}
}
function v({ begin: r, end: e, skip: t }) {
return {
args: [
{
transform(n, c) {
t && n.length <= 0 || (c.enqueue(r), c.enqueue(n), c.enqueue(e));
}
}
]
};
}
class A extends TransformStream {
constructor(e) {
const { args: t } = v(e);
super(...t);
}
}
const B = q, G = w, K = !1;
class T extends A {
constructor(e) {
let { begin: t, end: s, skip: n, ...c } = e ?? {};
t ??= B, s ??= G, n ??= K, super({ begin: t, end: s, skip: n, ...c });
}
}
const te = T;
function z(r) {
const { begin: e, end: t, stringify: s, errorFallback: n } = r, c = new TextEncoderStream(), u = new T({ begin: e, end: t }), a = new x({ stringify: s, errorFallback: n }), { writable: o } = a, i = a.readable.pipeThrough(u).pipeThrough(c);
return { writable: o, readable: i };
}
class ne {
#e;
#t;
get writable() {
return this.#e;
}
get readable() {
return this.#t;
}
constructor(e = {}) {
({
readable: this.#t,
writable: this.#e
} = z(e));
}
}
const I = `
`, H = `\r
`, M = [I, H];
class Q extends J {
constructor(e) {
let { splitter: t, chunkEndSplit: s } = e ?? {};
t ??= M, s ??= !1, super({ splitter: t, chunkEndSplit: s });
}
}
function U(r) {
const { label: e, fatal: t, splitter: s, chunkEndSplit: n, parse: c, errorFallback: u } = r ?? {}, a = new TextDecoderStream(e, { fatal: t }), o = new Q({ splitter: s, chunkEndSplit: n }), i = new m({ parse: c, errorFallback: u }), { writable: d } = a, l = a.readable.pipeThrough(o).pipeThrough(i);
return { writable: d, readable: l };
}
class re {
#e;
#t;
get writable() {
return this.#e;
}
get readable() {
return this.#t;
}
constructor(e) {
({
readable: this.#t,
writable: this.#e
} = U(e));
}
}
class V extends O {
constructor(e) {
let { delimiter: t, start: s, end: n, skip: c } = e ?? {};
t ??= I, s ??= !1, n ??= !1, c ??= !0, super({ delimiter: t, start: s, end: n, skip: c });
}
}
function W(r) {
const { delimiter: e, skip: t, start: s, end: n, stringify: c, errorFallback: u } = r, a = new TextEncoderStream(), o = new V({ delimiter: e, end: n, skip: t, start: s }), i = new x({ stringify: c, errorFallback: u }), { writable: d } = i, l = i.readable.pipeThrough(o).pipeThrough(a);
return { writable: d, readable: l };
}
class se {
#e;
#t;
get writable() {
return this.#e;
}
get readable() {
return this.#t;
}
constructor(e = {}) {
({
readable: this.#t,
writable: this.#e
} = W(e));
}
}
export {
re as InputJsonLinesStream,
m as InputJsonSequenceParseStream,
P as InputJsonSequenceStream,
Q as InputLineFeedSeparattedSequenceStream,
b as InputRecordSequenceStream,
Z as InputSequenceStream,
X as JsonSequenceFormatStream,
ee as JsonSequenceStream,
se as OutputJsonLinesStream,
ne as OutputJsonSequenceStream,
x as OutputJsonSequenceStringifyStream,
T as OutputRecordSequenceStream,
te as OutputSequenceStream,
V as OutputTextJoinLineFeedSequenceStream,
F as ParseStream,
j as RecordToSequenceStream,
Y as SequenceStream,
A as SequenceToRecordStream,
C as StringifyStream,
O as TextJoinStream,
J as TextSplitStream
};