@m4trix/core 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,716 @@
1
+ 'use strict';
2
+
3
+ // src/stream/Pump.ts
4
+ var Pump = class _Pump {
5
+ constructor(src) {
6
+ this.src = src;
7
+ }
8
+ /**
9
+ * Wrap an existing AsyncIterable or Readable stream into a Pump
10
+ *
11
+ * @template U The type of data in the source stream
12
+ * @param source The source stream to convert to a Pump (AsyncIterable, ReadableStream, or NodeJS.ReadableStream)
13
+ * @returns A new Pump instance that wraps the source
14
+ */
15
+ static from(source) {
16
+ async function* gen() {
17
+ let seq = 0;
18
+ function isAsyncIterable(obj) {
19
+ return Symbol.asyncIterator in obj;
20
+ }
21
+ function isWebReadableStream(obj) {
22
+ return "getReader" in obj && typeof obj.getReader === "function";
23
+ }
24
+ function isNodeReadableStream(obj) {
25
+ return "pipe" in obj && "on" in obj && typeof obj.pipe === "function" && typeof obj.on === "function";
26
+ }
27
+ if (isAsyncIterable(source)) {
28
+ const iterator = source[Symbol.asyncIterator]();
29
+ try {
30
+ while (true) {
31
+ const result = await iterator.next();
32
+ if (result.done)
33
+ break;
34
+ yield {
35
+ sequence: seq++,
36
+ data: result.value,
37
+ done: false
38
+ };
39
+ }
40
+ } finally {
41
+ }
42
+ } else if (isWebReadableStream(source)) {
43
+ const reader = source.getReader();
44
+ try {
45
+ while (true) {
46
+ const result = await reader.read();
47
+ if (result.done)
48
+ break;
49
+ yield {
50
+ sequence: seq++,
51
+ data: result.value,
52
+ done: false
53
+ };
54
+ }
55
+ } finally {
56
+ reader.releaseLock();
57
+ }
58
+ } else if (isNodeReadableStream(source)) {
59
+ try {
60
+ for await (const chunk of source) {
61
+ yield {
62
+ sequence: seq++,
63
+ data: chunk,
64
+ done: false
65
+ };
66
+ }
67
+ } catch (error) {
68
+ console.error("Error reading from Node.js stream:", error);
69
+ throw error;
70
+ }
71
+ }
72
+ yield { sequence: seq, data: void 0, done: true };
73
+ }
74
+ return new _Pump(gen());
75
+ }
76
+ /**
77
+ * Sync or async map over the data portion of each chunk
78
+ *
79
+ * @template U The output type after transformation
80
+ * @param fn The mapping function that transforms each chunk
81
+ * @returns A new Pump instance with the transformed data
82
+ */
83
+ map(fn) {
84
+ async function* gen() {
85
+ for await (const { sequence, data, done } of this.src) {
86
+ if (done) {
87
+ const out2 = data !== void 0 ? await fn(data) : void 0;
88
+ yield { sequence, data: out2, done };
89
+ break;
90
+ }
91
+ const out = await fn(data);
92
+ yield { sequence, data: out, done };
93
+ }
94
+ }
95
+ return new _Pump(gen.call(this));
96
+ }
97
+ /**
98
+ * Stateful map allows processing stream chunks with a persistent context object.
99
+ *
100
+ * The context is initialized when the first chunk arrives and can be updated with each chunk.
101
+ * This is useful for maintaining state across the stream processing.
102
+ *
103
+ * If you plan to use sockets you should rather opt for asyncStatefulMap.
104
+ *
105
+ * The pipe closes only after all processing is complete, including any final operations in onClose.
106
+ *
107
+ * TODO: Un-tested
108
+ *
109
+ * @param handlers Object containing callback functions for stream processing
110
+ * @param handlers.onFirstChunk Function called when the first chunk arrives, initializes the context
111
+ * @param handlers.onChunk Function called for each subsequent chunk, updates the context
112
+ * @param handlers.onClose Optional function called when the stream closes, allows final processing
113
+ * @returns A new Pump instance with transformed data
114
+ */
115
+ statefulMap(handlers) {
116
+ const { src } = this;
117
+ const gen = async function* () {
118
+ let context;
119
+ let initialized = false;
120
+ let lastChunk;
121
+ let seq = 0;
122
+ const queue = [];
123
+ const yieldData = (data) => {
124
+ queue.push(data);
125
+ };
126
+ for await (const { data, done } of src) {
127
+ if (done) {
128
+ if (context && handlers.onClose) {
129
+ await handlers.onClose(lastChunk, context, yieldData);
130
+ }
131
+ while (queue.length > 0) {
132
+ yield { sequence: seq++, data: queue.shift(), done: false };
133
+ }
134
+ yield {
135
+ sequence: seq++,
136
+ data: void 0,
137
+ done: true
138
+ };
139
+ break;
140
+ }
141
+ if (!initialized) {
142
+ context = await handlers.onFirstChunk(data, yieldData);
143
+ initialized = true;
144
+ } else if (context) {
145
+ context = await handlers.onChunk(data, context, yieldData);
146
+ }
147
+ lastChunk = data;
148
+ while (queue.length > 0) {
149
+ yield { sequence: seq++, data: queue.shift(), done: false };
150
+ }
151
+ }
152
+ };
153
+ return new _Pump(gen());
154
+ }
155
+ /**
156
+ * Async map means that each incoming chunk is causing an async operation that when it completes
157
+ * should yield a new chunk.
158
+ * The pipe closes only after you unlock the pipe by using the unlockCloseEvent callback.
159
+ *
160
+ * Stateful refers to the fact that you can create your own small context object that is passed in the subsequent callbacks.
161
+ * This allows you to keep track of things like a socket connection.
162
+ *
163
+ * Why is this nice? Well if you use things like a socket the pipe might have received the close event,
164
+ * before you got any or all of your socket responses. Sockets don't fit into the standard promise pattern,
165
+ * which makes it harder to wait for them.
166
+ *
167
+ * TODO: Un-tested
168
+ *
169
+ * @param handlers Object containing callback functions for stream processing
170
+ * @param handlers.onFirstChunk Function called when the first chunk arrives, initializes the context
171
+ * @param handlers.onChunk Function called for each subsequent chunk, updates the context
172
+ * @param handlers.onClose Optional function called when the stream closes, allows final processing
173
+ * @returns A new Pump instance with transformed data
174
+ */
175
+ asyncStatefulMap(handlers) {
176
+ const { src } = this;
177
+ const gen = async function* () {
178
+ let context;
179
+ let initialized = false;
180
+ let lastChunk;
181
+ let seq = 0;
182
+ let lockedCloseEvent = true;
183
+ const queue = [];
184
+ const yieldData = (data) => {
185
+ queue.push(data);
186
+ };
187
+ const unlockCloseEvent = () => {
188
+ lockedCloseEvent = false;
189
+ };
190
+ for await (const { data, done } of src) {
191
+ if (done) {
192
+ if (context && handlers.onClose) {
193
+ await handlers.onClose(
194
+ lastChunk,
195
+ context,
196
+ yieldData,
197
+ unlockCloseEvent
198
+ );
199
+ }
200
+ const timestamp = Date.now();
201
+ while (lockedCloseEvent && Date.now() - timestamp < 1e4) {
202
+ while (queue.length > 0) {
203
+ yield { sequence: seq++, data: queue.shift(), done: false };
204
+ }
205
+ await new Promise((resolve) => setTimeout(resolve, 5));
206
+ }
207
+ while (queue.length > 0) {
208
+ yield { sequence: seq++, data: queue.shift(), done: false };
209
+ }
210
+ yield {
211
+ sequence: seq++,
212
+ data: void 0,
213
+ done: true
214
+ };
215
+ break;
216
+ }
217
+ if (!initialized) {
218
+ context = await handlers.onFirstChunk(
219
+ data,
220
+ yieldData,
221
+ unlockCloseEvent
222
+ );
223
+ initialized = true;
224
+ } else if (context) {
225
+ context = await handlers.onChunk(
226
+ data,
227
+ context,
228
+ yieldData,
229
+ unlockCloseEvent
230
+ );
231
+ }
232
+ lastChunk = data;
233
+ while (queue.length > 0) {
234
+ yield { sequence: seq++, data: queue.shift(), done: false };
235
+ }
236
+ }
237
+ };
238
+ return new _Pump(gen());
239
+ }
240
+ /**
241
+ * Filter items based on a predicate
242
+ *
243
+ * @param predicate A function that determines whether to keep each chunk
244
+ * @returns A new Pump instance containing only chunks that passed the predicate
245
+ */
246
+ filter(predicate) {
247
+ async function* gen() {
248
+ for await (const { sequence, data, done } of this.src) {
249
+ if (done) {
250
+ yield { sequence, data, done: true };
251
+ break;
252
+ }
253
+ const keep = await predicate(data);
254
+ if (keep) {
255
+ yield { sequence, data, done: false };
256
+ }
257
+ }
258
+ }
259
+ return new _Pump(gen.call(this));
260
+ }
261
+ /**
262
+ * Bundles (accumulates) chunks together based on a condition rather than a fixed size.
263
+ *
264
+ * This is useful when you need to group chunks dynamically based on their content or other criteria.
265
+ *
266
+ * Example: Bundling text chunks with a maximum character limit
267
+ *
268
+ * Input chunks: ["Hello", " this", " is", " a few", " chunks", " of text"]
269
+ * With max size of 10 characters:
270
+ * - First bundle: ["Hello", " this"] (10 chars)
271
+ * - Second bundle: [" is", " a few"] (8 chars)
272
+ * - Third bundle: [" chunks", " of text"] (13 chars)
273
+ *
274
+ * @param closeBundleCondition - Function that determines when to close the current bundle
275
+ * Returns true when the current bundle should be emitted
276
+ * Parameters:
277
+ * - chunk: The current chunk being processed
278
+ * - accumulatedChunks: Array of chunks in the current bundle
279
+ *
280
+ * @returns A pump that emits arrays of bundled items
281
+ */
282
+ bundle(closeBundleCondition) {
283
+ async function* gen() {
284
+ let buffer = [];
285
+ let lastSequence = 0;
286
+ for await (const { sequence, data, done } of this.src) {
287
+ lastSequence = sequence;
288
+ if (done) {
289
+ if (buffer.length > 0) {
290
+ yield { sequence, data: [...buffer], done: false };
291
+ }
292
+ yield {
293
+ sequence: lastSequence,
294
+ data: void 0,
295
+ done: true
296
+ };
297
+ break;
298
+ }
299
+ const shouldClose = await closeBundleCondition(data, buffer);
300
+ buffer.push(data);
301
+ if (shouldClose) {
302
+ yield {
303
+ sequence: lastSequence,
304
+ data: [...buffer],
305
+ done: false
306
+ };
307
+ buffer = [];
308
+ }
309
+ }
310
+ }
311
+ return new _Pump(gen.call(this));
312
+ }
313
+ /**
314
+ * Tap into each chunk without altering it
315
+ *
316
+ * @param fn A function that receives each chunk but doesn't affect the stream
317
+ * @returns The same pump instance with unmodified data
318
+ */
319
+ onChunk(fn) {
320
+ async function* gen() {
321
+ for await (const chunk of this.src) {
322
+ if (chunk.data === void 0 && chunk.done) {
323
+ yield chunk;
324
+ }
325
+ await fn(chunk.data);
326
+ yield chunk;
327
+ }
328
+ }
329
+ return new _Pump(gen.call(this));
330
+ }
331
+ /**
332
+ * Collect all chunks in the stream and run a callback when the stream is done.
333
+ * The callback receives an array of all chunks that passed through.
334
+ *
335
+ * This is useful for analytics, logging, or processing the complete stream history
336
+ * after all chunks have been received.
337
+ *
338
+ * @param fn - Callback function that receives the array of all chunks when the stream is complete
339
+ * @returns The same pump, for chaining
340
+ */
341
+ onClose(fn) {
342
+ async function* gen() {
343
+ const history = [];
344
+ for await (const chunk of this.src) {
345
+ if (chunk.data !== void 0) {
346
+ history.push(chunk.data);
347
+ }
348
+ if (chunk.done) {
349
+ await fn(history);
350
+ }
351
+ yield chunk;
352
+ }
353
+ }
354
+ return new _Pump(gen.call(this));
355
+ }
356
+ /**
357
+ * Batch `n` chunks into arrays before emitting
358
+ *
359
+ * @param n The number of chunks to batch together
360
+ * @returns A new Pump instance that emits arrays of batched chunks
361
+ */
362
+ batch(n) {
363
+ async function* gen() {
364
+ let buffer = [];
365
+ for await (const chunk of this.src) {
366
+ if (chunk.done) {
367
+ if (chunk.data === void 0) {
368
+ yield {
369
+ sequence: buffer[0].sequence,
370
+ data: buffer.map((c) => c.data),
371
+ done: false
372
+ };
373
+ yield {
374
+ sequence: chunk.sequence,
375
+ data: void 0,
376
+ done: true
377
+ };
378
+ buffer = [];
379
+ } else {
380
+ buffer.push(chunk);
381
+ yield {
382
+ sequence: buffer[0].sequence,
383
+ data: buffer.map((c) => c.data),
384
+ done: true
385
+ };
386
+ }
387
+ break;
388
+ }
389
+ buffer.push(chunk);
390
+ if (buffer.length === n) {
391
+ yield {
392
+ sequence: buffer[0].sequence,
393
+ data: buffer.map((c) => c.data),
394
+ done: chunk.done
395
+ };
396
+ buffer = [];
397
+ }
398
+ }
399
+ }
400
+ return new _Pump(gen.call(this));
401
+ }
402
+ /**
403
+ * If you want to prevent chunk starvation, you can buffer the chunks.
404
+ * Chunks will not be bundled into arrays or object but kept as is,
405
+ * but the pipeline will not progress at that segment until the buffer is filled up.
406
+ * Once a buffer is filled up it will drain and never buffer again.
407
+ *
408
+ * @param n The number of chunks to buffer before processing continues
409
+ * @returns A new Pump instance with buffering behavior
410
+ */
411
+ buffer(n) {
412
+ async function* gen() {
413
+ let buffer = [];
414
+ let bufferFilled = false;
415
+ for await (const chunk of this.src) {
416
+ if (!bufferFilled) {
417
+ if (!chunk.done) {
418
+ buffer.push(chunk);
419
+ }
420
+ if (buffer.length >= n || chunk.done) {
421
+ bufferFilled = true;
422
+ for (const bufferedChunk of buffer) {
423
+ yield bufferedChunk;
424
+ }
425
+ if (chunk.done) {
426
+ yield {
427
+ sequence: chunk.sequence,
428
+ data: void 0,
429
+ done: true
430
+ };
431
+ break;
432
+ }
433
+ buffer = [];
434
+ }
435
+ } else {
436
+ yield chunk;
437
+ }
438
+ }
439
+ for (const bufferedChunk of buffer) {
440
+ yield bufferedChunk;
441
+ }
442
+ }
443
+ return new _Pump(gen.call(this));
444
+ }
445
+ /**
446
+ * Rechunk the stream: transform one chunk into zero, one, or many output chunks.
447
+ * The handler function receives the current buffer of chunks, a push function to emit new chunks,
448
+ * and a flag indicating if this is the last chunk in the stream.
449
+ *
450
+ * @param handler Function that transforms chunks and pushes new ones
451
+ * @returns A new Pump instance with rechunked data
452
+ */
453
+ rechunk(handler) {
454
+ async function* gen() {
455
+ let buffer = [];
456
+ let seq = 0;
457
+ const pending = [];
458
+ const push = (chunk) => {
459
+ pending.push(chunk);
460
+ };
461
+ for await (const { data, done } of this.src) {
462
+ if (!done) {
463
+ if (data !== void 0) {
464
+ buffer.push(data);
465
+ }
466
+ await handler({
467
+ buffer,
468
+ push,
469
+ lastChunk: false,
470
+ setBuffer: (b) => {
471
+ buffer = b;
472
+ }
473
+ });
474
+ } else {
475
+ await handler({
476
+ buffer,
477
+ push,
478
+ lastChunk: true,
479
+ setBuffer: (b) => {
480
+ buffer = b;
481
+ }
482
+ });
483
+ }
484
+ while (pending.length > 0) {
485
+ const out = pending.shift();
486
+ yield { sequence: seq++, data: out, done: false };
487
+ }
488
+ if (done) {
489
+ break;
490
+ }
491
+ }
492
+ yield { sequence: seq, data: void 0, done: true };
493
+ }
494
+ return new _Pump(gen.call(this));
495
+ }
496
+ slidingWindow(size, step, fn) {
497
+ async function* gen() {
498
+ const history = [];
499
+ let offset = 0;
500
+ let lastSeq = 0;
501
+ function buildWindow(_offset, _size, _history) {
502
+ const window = Array(_size).fill(void 0);
503
+ let windowIndex = 0;
504
+ for (let i = _offset; i > _offset - _size; i -= step) {
505
+ if (i >= history.length) {
506
+ windowIndex++;
507
+ continue;
508
+ }
509
+ if (i < 0) {
510
+ break;
511
+ }
512
+ window[windowIndex] = _history[i];
513
+ windowIndex++;
514
+ }
515
+ return window;
516
+ }
517
+ for await (const { sequence, data, done } of this.src) {
518
+ if (done) {
519
+ for (let i = 0; i < size - 1; i++) {
520
+ const window2 = buildWindow(offset + i, size, history);
521
+ yield { sequence: lastSeq, data: window2, done: false };
522
+ }
523
+ if (data === void 0) {
524
+ yield {
525
+ sequence: lastSeq,
526
+ data: void 0,
527
+ done: true
528
+ };
529
+ } else {
530
+ yield {
531
+ sequence: lastSeq,
532
+ data: [
533
+ history[history.length - 2] ?? void 0,
534
+ history[history.length - 3] ?? void 0,
535
+ history[history.length - 1]
536
+ ],
537
+ done: true
538
+ };
539
+ }
540
+ break;
541
+ }
542
+ lastSeq = sequence;
543
+ history.push(data);
544
+ const window = buildWindow(offset, size, history);
545
+ yield { sequence, data: window, done: false };
546
+ offset++;
547
+ }
548
+ }
549
+ const base = new _Pump(gen.call(this));
550
+ return fn ? base.map(fn) : base;
551
+ }
552
+ /**
553
+ * Sequentially flatten inner stream sources emitted by the pipeline.
554
+ * Works with any Source type (AsyncIterable or ReadableStream).
555
+ * This method is only available when the current Pump contains Source elements.
556
+ *
557
+ * @template U The type of data in the inner streams
558
+ * @template F The type of inner stream source (extends Source<U>)
559
+ * @returns A Pump instance with flattened stream data
560
+ */
561
+ sequenceStreams() {
562
+ async function* gen() {
563
+ let seq = 0;
564
+ for await (const { data: innerSource, done: outerDone } of this.src) {
565
+ if (outerDone)
566
+ break;
567
+ const innerPump = _Pump.from(innerSource);
568
+ for await (const { data, done } of innerPump.src) {
569
+ if (done)
570
+ break;
571
+ yield { sequence: seq++, data, done: false };
572
+ }
573
+ }
574
+ yield { sequence: seq, data: void 0, done: true };
575
+ }
576
+ return new _Pump(gen.call(this));
577
+ }
578
+ /**
579
+ * Fork the stream: two independent Pump<T> consumers
580
+ * Both resulting Pumps will receive the same data, allowing for divergent processing paths.
581
+ *
582
+ * @returns An array containing two independent Pump instances with the same source data
583
+ */
584
+ fork() {
585
+ const buffers = [[], []];
586
+ let done = false;
587
+ const srcIter = this.src[Symbol.asyncIterator]();
588
+ async function fill() {
589
+ const { value, done: streamDone } = await srcIter.next();
590
+ if (streamDone) {
591
+ done = true;
592
+ return;
593
+ }
594
+ buffers.forEach((q) => q.push(value));
595
+ if (value.done)
596
+ done = true;
597
+ }
598
+ function makeStream(buf) {
599
+ return {
600
+ [Symbol.asyncIterator]() {
601
+ return {
602
+ async next() {
603
+ while (buf.length === 0 && !done) {
604
+ await fill();
605
+ }
606
+ if (buf.length === 0)
607
+ return {
608
+ done: true,
609
+ value: void 0
610
+ };
611
+ return { done: false, value: buf.shift() };
612
+ }
613
+ };
614
+ }
615
+ };
616
+ }
617
+ return [new _Pump(makeStream(buffers[0])), new _Pump(makeStream(buffers[1]))];
618
+ }
619
+ /**
620
+ * Drain the pipeline, consuming all chunks.
621
+ * Returns a Promise that resolves when all chunks have been consumed.
622
+ *
623
+ * @returns A Promise that resolves when all chunks have been consumed
624
+ */
625
+ drain() {
626
+ return (async () => {
627
+ for await (const { done } of this.src) {
628
+ if (done)
629
+ break;
630
+ }
631
+ })();
632
+ }
633
+ /**
634
+ * Drain the pipeline to a StreamTransformer.
635
+ * Applies transform() to each data chunk, then closes the transformer,
636
+ * and returns its response (which can be of any type defined by the transformer).
637
+ *
638
+ * Example with httpStreamResponse:
639
+ * ```
640
+ * const { transform, response, close } = httpStreamResponse(options);
641
+ * return Pump.from(messageStream).drainTo({ transform, close, response });
642
+ * ```
643
+ *
644
+ * @template U The type of data expected by the transformer (extends T)
645
+ * @template R The response type produced by the transformer
646
+ * @param transformer The StreamTransformer to drain to
647
+ * @returns The response from the transformer
648
+ */
649
+ drainTo(transformer) {
650
+ (async () => {
651
+ for await (const { data, done } of this.src) {
652
+ if (done)
653
+ break;
654
+ transformer.transform(data);
655
+ }
656
+ transformer.close();
657
+ })();
658
+ return transformer.response;
659
+ }
660
+ };
661
+
662
+ // src/stream/utility/pipe-transformers/response.ts
663
+ function httpStreamResponse(options = {}) {
664
+ const { init, encoder } = options;
665
+ const encodeFn = encoder ?? ((d) => {
666
+ if (d instanceof Uint8Array)
667
+ return d;
668
+ if (typeof d === "string")
669
+ return d;
670
+ return JSON.stringify(d);
671
+ });
672
+ const { readable, writable } = new TransformStream();
673
+ const writer = writable.getWriter();
674
+ const response = new Response(readable, init);
675
+ const transform = (chunk) => {
676
+ const encoded = encodeFn(chunk);
677
+ const bytes = typeof encoded === "string" ? new TextEncoder().encode(encoded) : encoded;
678
+ writer.write(bytes);
679
+ return chunk;
680
+ };
681
+ const close = () => {
682
+ writer.close();
683
+ };
684
+ return { transform, response, close };
685
+ }
686
+
687
+ // src/stream/utility/rechunker/ensure-full-words.ts
688
+ async function ensureFullWords({
689
+ buffer,
690
+ push,
691
+ lastChunk
692
+ }) {
693
+ const combined = buffer.join("");
694
+ const lastBoundary = Math.max(
695
+ combined.lastIndexOf(" "),
696
+ combined.lastIndexOf("\n"),
697
+ combined.lastIndexOf(" ")
698
+ );
699
+ if (lastBoundary !== -1 || lastChunk) {
700
+ const emitPart = lastBoundary !== -1 ? combined.slice(0, lastBoundary + 1) : combined;
701
+ const leftoverPart = lastBoundary !== -1 ? combined.slice(lastBoundary + 1) : "";
702
+ if (emitPart.trim().length > 0) {
703
+ push(emitPart);
704
+ }
705
+ buffer.length = 0;
706
+ if (leftoverPart.length > 0) {
707
+ buffer.push(leftoverPart);
708
+ }
709
+ }
710
+ }
711
+
712
+ exports.Pump = Pump;
713
+ exports.ensureFullWords = ensureFullWords;
714
+ exports.httpStreamResponse = httpStreamResponse;
715
+ //# sourceMappingURL=out.js.map
716
+ //# sourceMappingURL=index.cjs.map