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