@push.rocks/smartstream 3.3.0 → 3.4.2
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.
- package/.smartconfig.json +27 -0
- package/dist_ts/00_commitinfo_data.js +1 -1
- package/dist_ts/smartstream.classes.smartduplex.d.ts +12 -6
- package/dist_ts/smartstream.classes.smartduplex.js +182 -72
- package/dist_ts/smartstream.classes.streamintake.js +6 -3
- package/dist_ts/smartstream.classes.streamwrapper.js +16 -13
- package/dist_ts/smartstream.functions.d.ts +2 -2
- package/dist_ts/smartstream.functions.js +1 -1
- package/dist_ts/smartstream.nodewebhelpers.d.ts +2 -2
- package/dist_ts/smartstream.nodewebhelpers.js +97 -36
- package/dist_ts_web/00_commitinfo_data.js +1 -1
- package/dist_ts_web/classes.webduplexstream.js +24 -16
- package/license +21 -0
- package/package.json +12 -10
- package/readme.md +145 -15
- package/ts/00_commitinfo_data.ts +1 -1
- package/ts/smartstream.classes.smartduplex.ts +214 -81
- package/ts/smartstream.classes.streamintake.ts +5 -2
- package/ts/smartstream.classes.streamwrapper.ts +18 -14
- package/ts/smartstream.functions.ts +2 -2
- package/ts/smartstream.nodewebhelpers.ts +105 -37
- package/ts_web/00_commitinfo_data.ts +1 -1
- package/ts_web/classes.webduplexstream.ts +24 -14
package/readme.md
CHANGED
|
@@ -12,7 +12,7 @@ For reporting bugs, issues, or security vulnerabilities, please visit [community
|
|
|
12
12
|
pnpm install @push.rocks/smartstream
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
-
The package ships with two entry points:
|
|
15
|
+
The package ships with **two entry points** so you can pick exactly what you need:
|
|
16
16
|
|
|
17
17
|
| Entry Point | Import Path | Environment |
|
|
18
18
|
|---|---|---|
|
|
@@ -83,6 +83,8 @@ const splitter = new SmartDuplex<string, string>({
|
|
|
83
83
|
Run cleanup or emit final data when the writable side ends:
|
|
84
84
|
|
|
85
85
|
```typescript
|
|
86
|
+
let runningTotal = 0;
|
|
87
|
+
|
|
86
88
|
const aggregator = new SmartDuplex<number, number>({
|
|
87
89
|
objectMode: true,
|
|
88
90
|
writeFunction: async (chunk, tools) => {
|
|
@@ -95,6 +97,8 @@ const aggregator = new SmartDuplex<number, number>({
|
|
|
95
97
|
});
|
|
96
98
|
```
|
|
97
99
|
|
|
100
|
+
The `finalFunction` can also push multiple chunks via `tools.push()`, just like `writeFunction`.
|
|
101
|
+
|
|
98
102
|
#### Truncating a Stream Early
|
|
99
103
|
|
|
100
104
|
Call `tools.truncate()` inside `writeFunction` to signal that no more data should be read:
|
|
@@ -132,16 +136,36 @@ nodeDuplex.pipe(processTransform).pipe(outputStream);
|
|
|
132
136
|
|
|
133
137
|
#### Getting Web Streams from SmartDuplex
|
|
134
138
|
|
|
135
|
-
Convert a `SmartDuplex` into Web `ReadableStream` + `WritableStream` pair:
|
|
139
|
+
Convert a `SmartDuplex` into a Web `ReadableStream` + `WritableStream` pair:
|
|
136
140
|
|
|
137
141
|
```typescript
|
|
138
142
|
const duplex = new SmartDuplex({
|
|
143
|
+
objectMode: true,
|
|
139
144
|
writeFunction: async (chunk, tools) => {
|
|
140
145
|
return transform(chunk);
|
|
141
146
|
},
|
|
142
147
|
});
|
|
143
148
|
|
|
144
149
|
const { readable, writable } = await duplex.getWebStreams();
|
|
150
|
+
|
|
151
|
+
const writer = writable.getWriter();
|
|
152
|
+
const reader = readable.getReader();
|
|
153
|
+
|
|
154
|
+
// Read and write concurrently to avoid TransformStream backpressure
|
|
155
|
+
const readAll = async () => {
|
|
156
|
+
const results = [];
|
|
157
|
+
while (true) {
|
|
158
|
+
const { value, done } = await reader.read();
|
|
159
|
+
if (done) break;
|
|
160
|
+
results.push(value);
|
|
161
|
+
}
|
|
162
|
+
return results;
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
const readPromise = readAll();
|
|
166
|
+
await writer.write('hello');
|
|
167
|
+
await writer.close();
|
|
168
|
+
const results = await readPromise;
|
|
145
169
|
```
|
|
146
170
|
|
|
147
171
|
#### Debug Mode
|
|
@@ -186,7 +210,7 @@ pipeline.run()
|
|
|
186
210
|
.catch((err) => console.error('Pipeline failed:', err));
|
|
187
211
|
```
|
|
188
212
|
|
|
189
|
-
You can
|
|
213
|
+
You can listen for custom events across all streams in the pipeline:
|
|
190
214
|
|
|
191
215
|
```typescript
|
|
192
216
|
pipeline.onCustomEvent('progress', () => {
|
|
@@ -194,6 +218,14 @@ pipeline.onCustomEvent('progress', () => {
|
|
|
194
218
|
});
|
|
195
219
|
```
|
|
196
220
|
|
|
221
|
+
You can also await `streamStarted()` to know when the pipeline has been wired up:
|
|
222
|
+
|
|
223
|
+
```typescript
|
|
224
|
+
const runPromise = pipeline.run();
|
|
225
|
+
await pipeline.streamStarted(); // Resolves once pipes are connected
|
|
226
|
+
await runPromise;
|
|
227
|
+
```
|
|
228
|
+
|
|
197
229
|
---
|
|
198
230
|
|
|
199
231
|
### 📥 StreamIntake — Dynamic Data Injection
|
|
@@ -222,7 +254,7 @@ intake.pushData('World');
|
|
|
222
254
|
intake.signalEnd(); // Signal end-of-stream
|
|
223
255
|
```
|
|
224
256
|
|
|
225
|
-
#### Demand-
|
|
257
|
+
#### Demand-Driven Production with Observable
|
|
226
258
|
|
|
227
259
|
`pushNextObservable` emits whenever the stream is ready for more data — perfect for throttled or event-driven producers:
|
|
228
260
|
|
|
@@ -265,7 +297,10 @@ Quickly create a `SmartDuplex` from a simple async mapping function:
|
|
|
265
297
|
```typescript
|
|
266
298
|
import { createTransformFunction } from '@push.rocks/smartstream';
|
|
267
299
|
|
|
268
|
-
const doubler = createTransformFunction<number, number>(
|
|
300
|
+
const doubler = createTransformFunction<number, number>(
|
|
301
|
+
async (n) => n * 2,
|
|
302
|
+
{ objectMode: true }
|
|
303
|
+
);
|
|
269
304
|
|
|
270
305
|
intakeStream.pipe(doubler).pipe(outputStream);
|
|
271
306
|
```
|
|
@@ -299,16 +334,26 @@ const stream = new WebDuplexStream<number, number>({
|
|
|
299
334
|
const writer = stream.writable.getWriter();
|
|
300
335
|
const reader = stream.readable.getReader();
|
|
301
336
|
|
|
302
|
-
//
|
|
337
|
+
// Always read concurrently with writes — TransformStream applies backpressure
|
|
338
|
+
const readPromise = (async () => {
|
|
339
|
+
const results = [];
|
|
340
|
+
while (true) {
|
|
341
|
+
const { value, done } = await reader.read();
|
|
342
|
+
if (done) break;
|
|
343
|
+
results.push(value);
|
|
344
|
+
}
|
|
345
|
+
return results;
|
|
346
|
+
})();
|
|
347
|
+
|
|
303
348
|
await writer.write(5);
|
|
304
349
|
await writer.write(10);
|
|
305
350
|
await writer.close();
|
|
306
351
|
|
|
307
|
-
//
|
|
308
|
-
const { value } = await reader.read(); // 10
|
|
309
|
-
const { value: v2 } = await reader.read(); // 20
|
|
352
|
+
const results = await readPromise; // [10, 20]
|
|
310
353
|
```
|
|
311
354
|
|
|
355
|
+
> 💡 **Tip:** Because `TransformStream` enforces backpressure between its writable and readable sides, you must start reading *before* or *concurrently with* writes. If you await all writes first and then read, the writes will block waiting for the readable side to drain.
|
|
356
|
+
|
|
312
357
|
#### From a Uint8Array
|
|
313
358
|
|
|
314
359
|
```typescript
|
|
@@ -337,11 +382,24 @@ const reader = stream.readable.getReader();
|
|
|
337
382
|
// reads "CHUNK 1", "CHUNK 2"
|
|
338
383
|
```
|
|
339
384
|
|
|
385
|
+
#### Final Function
|
|
386
|
+
|
|
387
|
+
Emit trailing data when the writable side is closed:
|
|
388
|
+
|
|
389
|
+
```typescript
|
|
390
|
+
const stream = new WebDuplexStream<string, string>({
|
|
391
|
+
writeFunction: async (chunk) => chunk,
|
|
392
|
+
finalFunction: async (tools) => {
|
|
393
|
+
tools.push('footer');
|
|
394
|
+
},
|
|
395
|
+
});
|
|
396
|
+
```
|
|
397
|
+
|
|
340
398
|
---
|
|
341
399
|
|
|
342
400
|
### 🔀 Node ↔ Web Stream Converters
|
|
343
401
|
|
|
344
|
-
The `nodewebhelpers` namespace provides bidirectional converters between Node.js and Web Streams:
|
|
402
|
+
The `nodewebhelpers` namespace provides bidirectional converters between Node.js and Web Streams with proper backpressure handling:
|
|
345
403
|
|
|
346
404
|
```typescript
|
|
347
405
|
import { nodewebhelpers } from '@push.rocks/smartstream';
|
|
@@ -381,6 +439,16 @@ const nodeReadable2 = nodewebhelpers.convertWebReadableToNodeReadable(webReadabl
|
|
|
381
439
|
nodeReadable2.pipe(fs.createWriteStream('./copy.bin'));
|
|
382
440
|
```
|
|
383
441
|
|
|
442
|
+
#### Example: Round-Trip Conversion
|
|
443
|
+
|
|
444
|
+
```typescript
|
|
445
|
+
// Node → Web → Node (lossless round-trip)
|
|
446
|
+
const original = fs.createReadStream('./photo.jpg');
|
|
447
|
+
const webStream = nodewebhelpers.convertNodeReadableToWebReadable(original);
|
|
448
|
+
const backToNode = nodewebhelpers.convertWebReadableToNodeReadable(webStream);
|
|
449
|
+
backToNode.pipe(fs.createWriteStream('./photo-copy.jpg'));
|
|
450
|
+
```
|
|
451
|
+
|
|
384
452
|
---
|
|
385
453
|
|
|
386
454
|
### 🏗️ Backpressure Handling
|
|
@@ -417,25 +485,23 @@ fast.end();
|
|
|
417
485
|
|
|
418
486
|
---
|
|
419
487
|
|
|
420
|
-
### 🎯 Real-World Example: Processing Pipeline
|
|
488
|
+
### 🎯 Real-World Example: Log Processing Pipeline
|
|
421
489
|
|
|
422
490
|
```typescript
|
|
423
491
|
import fs from 'fs';
|
|
424
492
|
import { SmartDuplex, StreamWrapper } from '@push.rocks/smartstream';
|
|
425
493
|
|
|
426
|
-
// Read →
|
|
494
|
+
// Read → Parse → Filter → Write
|
|
427
495
|
const pipeline = new StreamWrapper([
|
|
428
496
|
fs.createReadStream('./access.log'),
|
|
429
497
|
new SmartDuplex({
|
|
430
498
|
writeFunction: async (chunk) => {
|
|
431
|
-
// Parse each line
|
|
432
499
|
return chunk.toString().split('\n');
|
|
433
500
|
},
|
|
434
501
|
}),
|
|
435
502
|
new SmartDuplex({
|
|
436
503
|
objectMode: true,
|
|
437
504
|
writeFunction: async (lines: string[], tools) => {
|
|
438
|
-
// Filter and push matching lines
|
|
439
505
|
for (const line of lines) {
|
|
440
506
|
if (line.includes('ERROR')) {
|
|
441
507
|
await tools.push(line + '\n');
|
|
@@ -450,6 +516,70 @@ await pipeline.run();
|
|
|
450
516
|
console.log('Error extraction complete');
|
|
451
517
|
```
|
|
452
518
|
|
|
519
|
+
---
|
|
520
|
+
|
|
521
|
+
### 📋 API Reference
|
|
522
|
+
|
|
523
|
+
#### SmartDuplex
|
|
524
|
+
|
|
525
|
+
| Member | Type | Description |
|
|
526
|
+
|---|---|---|
|
|
527
|
+
| `new SmartDuplex(options?)` | Constructor | Create a new duplex stream |
|
|
528
|
+
| `options.writeFunction` | `(chunk, tools) => Promise<T>` | Transform each chunk; return to push, or use `tools.push()` |
|
|
529
|
+
| `options.finalFunction` | `(tools) => Promise<T>` | Emit final data when writable ends |
|
|
530
|
+
| `options.readFunction` | `() => Promise<void>` | Supply data to the readable side |
|
|
531
|
+
| `options.debug` | `boolean` | Enable internal logging |
|
|
532
|
+
| `options.name` | `string` | Stream name for debug logs |
|
|
533
|
+
| `SmartDuplex.fromBuffer(buf)` | Static | Create a readable stream from a Buffer |
|
|
534
|
+
| `SmartDuplex.fromWebReadableStream(rs)` | Static | Bridge a Web ReadableStream to Node.js Duplex |
|
|
535
|
+
| `duplex.getWebStreams()` | Method | Get `{ readable, writable }` Web Streams pair |
|
|
536
|
+
|
|
537
|
+
#### WebDuplexStream
|
|
538
|
+
|
|
539
|
+
| Member | Type | Description |
|
|
540
|
+
|---|---|---|
|
|
541
|
+
| `new WebDuplexStream(options)` | Constructor | Create a new web transform stream |
|
|
542
|
+
| `options.writeFunction` | `(chunk, tools) => Promise<T>` | Transform each chunk; use `tools.push()` or return |
|
|
543
|
+
| `options.finalFunction` | `(tools) => Promise<T>` | Emit data on flush |
|
|
544
|
+
| `options.readFunction` | `(tools) => Promise<void>` | Supply data via `tools.write()`, signal `tools.done()` |
|
|
545
|
+
| `WebDuplexStream.fromUInt8Array(arr)` | Static | Create a stream from a Uint8Array |
|
|
546
|
+
|
|
547
|
+
#### StreamWrapper
|
|
548
|
+
|
|
549
|
+
| Member | Type | Description |
|
|
550
|
+
|---|---|---|
|
|
551
|
+
| `new StreamWrapper(streams[])` | Constructor | Create a pipeline from an array of streams |
|
|
552
|
+
| `wrapper.run()` | Method | Pipe all streams and return a Promise |
|
|
553
|
+
| `wrapper.streamStarted()` | Method | Promise that resolves when pipes are connected |
|
|
554
|
+
| `wrapper.onCustomEvent(name, fn)` | Method | Listen for custom events across all streams |
|
|
555
|
+
|
|
556
|
+
#### StreamIntake
|
|
557
|
+
|
|
558
|
+
| Member | Type | Description |
|
|
559
|
+
|---|---|---|
|
|
560
|
+
| `new StreamIntake<T>()` | Constructor | Create a new intake stream (object mode) |
|
|
561
|
+
| `intake.pushData(data)` | Method | Push data into the stream |
|
|
562
|
+
| `intake.signalEnd()` | Method | Signal end of stream |
|
|
563
|
+
| `intake.pushNextObservable` | Property | Observable that emits when the stream wants more data |
|
|
564
|
+
| `StreamIntake.fromStream(stream)` | Static | Wrap a Node.js Readable or Web ReadableStream |
|
|
565
|
+
|
|
566
|
+
#### nodewebhelpers
|
|
567
|
+
|
|
568
|
+
| Function | Description |
|
|
569
|
+
|---|---|
|
|
570
|
+
| `createWebReadableStreamFromFile(path)` | File → Web ReadableStream (pull-based backpressure) |
|
|
571
|
+
| `convertWebReadableToNodeReadable(rs)` | Web ReadableStream → Node.js Readable |
|
|
572
|
+
| `convertNodeReadableToWebReadable(ns)` | Node.js Readable → Web ReadableStream (pull-based backpressure) |
|
|
573
|
+
| `convertWebWritableToNodeWritable(ws)` | Web WritableStream → Node.js Writable |
|
|
574
|
+
| `convertNodeWritableToWebWritable(nw)` | Node.js Writable → Web WritableStream |
|
|
575
|
+
|
|
576
|
+
#### Utility Functions
|
|
577
|
+
|
|
578
|
+
| Function | Description |
|
|
579
|
+
|---|---|
|
|
580
|
+
| `createTransformFunction(fn, opts?)` | Create a SmartDuplex from an async mapping function |
|
|
581
|
+
| `createPassThrough()` | Create an object-mode passthrough stream |
|
|
582
|
+
|
|
453
583
|
## License and Legal Information
|
|
454
584
|
|
|
455
585
|
This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the [LICENSE](./LICENSE) file.
|
|
@@ -464,7 +594,7 @@ Use of these trademarks must comply with Task Venture Capital GmbH's Trademark G
|
|
|
464
594
|
|
|
465
595
|
### Company Information
|
|
466
596
|
|
|
467
|
-
Task Venture Capital GmbH
|
|
597
|
+
Task Venture Capital GmbH
|
|
468
598
|
Registered at District Court Bremen HRB 35230 HB, Germany
|
|
469
599
|
|
|
470
600
|
For any legal inquiries or further information, please contact us via email at hello@task.vc.
|
package/ts/00_commitinfo_data.ts
CHANGED
|
@@ -3,6 +3,6 @@
|
|
|
3
3
|
*/
|
|
4
4
|
export const commitinfo = {
|
|
5
5
|
name: '@push.rocks/smartstream',
|
|
6
|
-
version: '3.
|
|
6
|
+
version: '3.4.2',
|
|
7
7
|
description: 'A library to simplify the creation and manipulation of Node.js streams, providing utilities for handling transform, duplex, and readable/writable streams effectively in TypeScript.'
|
|
8
8
|
}
|