@push.rocks/smartstream 3.1.0 → 3.1.1
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/dist_ts/00_commitinfo_data.js +1 -1
- package/dist_ts_web/00_commitinfo_data.js +1 -1
- package/dist_ts_web/classes.webduplexstream.d.ts +6 -9
- package/dist_ts_web/classes.webduplexstream.js +49 -63
- package/package.json +1 -1
- package/ts/00_commitinfo_data.ts +1 -1
- package/ts_web/00_commitinfo_data.ts +1 -1
- package/ts_web/classes.webduplexstream.ts +61 -82
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*/
|
|
4
4
|
export const commitinfo = {
|
|
5
5
|
name: '@push.rocks/smartstream',
|
|
6
|
-
version: '3.1.
|
|
6
|
+
version: '3.1.1',
|
|
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
|
};
|
|
9
9
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiMDBfY29tbWl0aW5mb19kYXRhLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvMDBfY29tbWl0aW5mb19kYXRhLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOztHQUVHO0FBQ0gsTUFBTSxDQUFDLE1BQU0sVUFBVSxHQUFHO0lBQ3hCLElBQUksRUFBRSx5QkFBeUI7SUFDL0IsT0FBTyxFQUFFLE9BQU87SUFDaEIsV0FBVyxFQUFFLHNMQUFzTDtDQUNwTSxDQUFBIn0=
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*/
|
|
4
4
|
export const commitinfo = {
|
|
5
5
|
name: '@push.rocks/smartstream',
|
|
6
|
-
version: '3.1.
|
|
6
|
+
version: '3.1.1',
|
|
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
|
};
|
|
9
9
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiMDBfY29tbWl0aW5mb19kYXRhLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHNfd2ViLzAwX2NvbW1pdGluZm9fZGF0YS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTs7R0FFRztBQUNILE1BQU0sQ0FBQyxNQUFNLFVBQVUsR0FBRztJQUN4QixJQUFJLEVBQUUseUJBQXlCO0lBQy9CLE9BQU8sRUFBRSxPQUFPO0lBQ2hCLFdBQVcsRUFBRSxzTEFBc0w7Q0FDcE0sQ0FBQSJ9
|
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
export interface IStreamToolsRead<TInput, TOutput> {
|
|
2
2
|
done: () => void;
|
|
3
|
-
write: (writeArg: TInput) => void
|
|
3
|
+
write: (writeArg: TInput) => Promise<void>;
|
|
4
4
|
}
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
7
|
-
* -> the WebDuplexStream is being read from
|
|
8
|
-
* and at the same time if nothing is enqueued
|
|
6
|
+
* The read function is called when data needs to be read into the stream.
|
|
9
7
|
*/
|
|
10
8
|
export interface IStreamReadFunction<TInput, TOutput> {
|
|
11
9
|
(toolsArg: IStreamToolsRead<TInput, TOutput>): Promise<void>;
|
|
@@ -15,14 +13,13 @@ export interface IStreamToolsWrite<TInput, TOutput> {
|
|
|
15
13
|
push: (pushArg: TOutput) => void;
|
|
16
14
|
}
|
|
17
15
|
/**
|
|
18
|
-
*
|
|
19
|
-
* It is called anytime a chunk is written to the stream.
|
|
16
|
+
* The write function is called whenever a chunk is written to the stream.
|
|
20
17
|
*/
|
|
21
18
|
export interface IStreamWriteFunction<TInput, TOutput> {
|
|
22
19
|
(chunkArg: TInput, toolsArg: IStreamToolsWrite<TInput, TOutput>): Promise<any>;
|
|
23
20
|
}
|
|
24
21
|
export interface IStreamFinalFunction<TInput, TOutput> {
|
|
25
|
-
(toolsArg: IStreamToolsWrite<TInput, TOutput>): Promise<TOutput>;
|
|
22
|
+
(toolsArg: IStreamToolsWrite<TInput, TOutput>): Promise<TOutput | void>;
|
|
26
23
|
}
|
|
27
24
|
export interface WebDuplexStreamOptions<TInput, TOutput> {
|
|
28
25
|
readFunction?: IStreamReadFunction<TInput, TOutput>;
|
|
@@ -30,8 +27,8 @@ export interface WebDuplexStreamOptions<TInput, TOutput> {
|
|
|
30
27
|
finalFunction?: IStreamFinalFunction<TInput, TOutput>;
|
|
31
28
|
}
|
|
32
29
|
export declare class WebDuplexStream<TInput = any, TOutput = any> extends TransformStream<TInput, TOutput> {
|
|
33
|
-
static fromUInt8Array(uint8Array: Uint8Array): WebDuplexStream<Uint8Array, Uint8Array>;
|
|
34
30
|
options: WebDuplexStreamOptions<TInput, TOutput>;
|
|
35
31
|
constructor(optionsArg: WebDuplexStreamOptions<TInput, TOutput>);
|
|
36
|
-
|
|
32
|
+
private _startReading;
|
|
33
|
+
static fromUInt8Array(uint8Array: Uint8Array): WebDuplexStream<Uint8Array, Uint8Array>;
|
|
37
34
|
}
|
|
@@ -1,21 +1,11 @@
|
|
|
1
1
|
import * as plugins from './plugins.js';
|
|
2
2
|
export class WebDuplexStream extends TransformStream {
|
|
3
|
-
static fromUInt8Array(uint8Array) {
|
|
4
|
-
const stream = new WebDuplexStream({
|
|
5
|
-
writeFunction: async (chunk, { push }) => {
|
|
6
|
-
push(chunk); // Directly push the chunk as is
|
|
7
|
-
return null;
|
|
8
|
-
}
|
|
9
|
-
});
|
|
10
|
-
const writer = stream.writable.getWriter();
|
|
11
|
-
writer.write(uint8Array).then(() => writer.close());
|
|
12
|
-
return stream;
|
|
13
|
-
}
|
|
14
3
|
constructor(optionsArg) {
|
|
15
|
-
// here we call into the official web stream api
|
|
16
4
|
super({
|
|
5
|
+
async start(controller) {
|
|
6
|
+
// Optionally initialize any state here
|
|
7
|
+
},
|
|
17
8
|
async transform(chunk, controller) {
|
|
18
|
-
// Transformation logic remains unchanged
|
|
19
9
|
if (optionsArg?.writeFunction) {
|
|
20
10
|
const tools = {
|
|
21
11
|
truncate: () => controller.terminate(),
|
|
@@ -23,7 +13,7 @@ export class WebDuplexStream extends TransformStream {
|
|
|
23
13
|
};
|
|
24
14
|
try {
|
|
25
15
|
const writeReturnChunk = await optionsArg.writeFunction(chunk, tools);
|
|
26
|
-
if (writeReturnChunk
|
|
16
|
+
if (writeReturnChunk !== undefined && writeReturnChunk !== null) {
|
|
27
17
|
controller.enqueue(writeReturnChunk);
|
|
28
18
|
}
|
|
29
19
|
}
|
|
@@ -32,72 +22,68 @@ export class WebDuplexStream extends TransformStream {
|
|
|
32
22
|
}
|
|
33
23
|
}
|
|
34
24
|
else {
|
|
35
|
-
|
|
25
|
+
// If no writeFunction is provided, pass the chunk through
|
|
26
|
+
controller.enqueue(chunk);
|
|
36
27
|
}
|
|
37
28
|
},
|
|
38
29
|
async flush(controller) {
|
|
39
|
-
// Flush logic remains unchanged
|
|
40
30
|
if (optionsArg?.finalFunction) {
|
|
41
31
|
const tools = {
|
|
42
32
|
truncate: () => controller.terminate(),
|
|
43
|
-
push: (
|
|
33
|
+
push: (pushArg) => controller.enqueue(pushArg),
|
|
44
34
|
};
|
|
45
|
-
|
|
46
|
-
|
|
35
|
+
try {
|
|
36
|
+
const finalChunk = await optionsArg.finalFunction(tools);
|
|
47
37
|
if (finalChunk) {
|
|
48
38
|
controller.enqueue(finalChunk);
|
|
49
39
|
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
|
|
40
|
+
}
|
|
41
|
+
catch (err) {
|
|
42
|
+
controller.error(err);
|
|
43
|
+
}
|
|
44
|
+
finally {
|
|
45
|
+
controller.terminate();
|
|
46
|
+
}
|
|
53
47
|
}
|
|
54
48
|
else {
|
|
55
49
|
controller.terminate();
|
|
56
50
|
}
|
|
57
|
-
}
|
|
51
|
+
},
|
|
58
52
|
});
|
|
59
53
|
this.options = optionsArg;
|
|
54
|
+
// Start producing data if readFunction is provided
|
|
55
|
+
if (this.options.readFunction) {
|
|
56
|
+
this._startReading();
|
|
57
|
+
}
|
|
60
58
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
write: (writeArg) => controller.enqueue(writeArg),
|
|
86
|
-
});
|
|
87
|
-
const newReader = readableStream.getReader();
|
|
88
|
-
const { value: newValue, done: newDone } = await newReader.read();
|
|
89
|
-
newReader.releaseLock();
|
|
90
|
-
if (newDone) {
|
|
91
|
-
controller.close();
|
|
92
|
-
}
|
|
93
|
-
else {
|
|
94
|
-
controller.enqueue(newValue);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
59
|
+
async _startReading() {
|
|
60
|
+
const writable = this.writable;
|
|
61
|
+
const writer = writable.getWriter();
|
|
62
|
+
const tools = {
|
|
63
|
+
done: () => writer.close(),
|
|
64
|
+
write: async (writeArg) => await writer.write(writeArg),
|
|
65
|
+
};
|
|
66
|
+
try {
|
|
67
|
+
await this.options.readFunction(tools);
|
|
68
|
+
}
|
|
69
|
+
catch (err) {
|
|
70
|
+
writer.abort(err);
|
|
71
|
+
}
|
|
72
|
+
finally {
|
|
73
|
+
writer.releaseLock();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
// Static method example (adjust as needed)
|
|
77
|
+
static fromUInt8Array(uint8Array) {
|
|
78
|
+
const stream = new WebDuplexStream({
|
|
79
|
+
writeFunction: async (chunk, { push }) => {
|
|
80
|
+
push(chunk); // Directly push the chunk as is
|
|
81
|
+
return null;
|
|
82
|
+
},
|
|
99
83
|
});
|
|
100
|
-
|
|
84
|
+
const writer = stream.writable.getWriter();
|
|
85
|
+
writer.write(uint8Array).then(() => writer.close());
|
|
86
|
+
return stream;
|
|
101
87
|
}
|
|
102
88
|
}
|
|
103
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
89
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY2xhc3Nlcy53ZWJkdXBsZXhzdHJlYW0uanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90c193ZWIvY2xhc3Nlcy53ZWJkdXBsZXhzdHJlYW0udHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxLQUFLLE9BQU8sTUFBTSxjQUFjLENBQUM7QUEwQ3hDLE1BQU0sT0FBTyxlQUE2QyxTQUFRLGVBQWdDO0lBSWhHLFlBQVksVUFBbUQ7UUFDN0QsS0FBSyxDQUFDO1lBQ0osS0FBSyxDQUFDLEtBQUssQ0FBQyxVQUFVO2dCQUNwQix1Q0FBdUM7WUFDekMsQ0FBQztZQUNELEtBQUssQ0FBQyxTQUFTLENBQUMsS0FBSyxFQUFFLFVBQVU7Z0JBQy9CLElBQUksVUFBVSxFQUFFLGFBQWEsRUFBRSxDQUFDO29CQUM5QixNQUFNLEtBQUssR0FBdUM7d0JBQ2hELFFBQVEsRUFBRSxHQUFHLEVBQUUsQ0FBQyxVQUFVLENBQUMsU0FBUyxFQUFFO3dCQUN0QyxJQUFJLEVBQUUsQ0FBQyxPQUFnQixFQUFFLEVBQUUsQ0FBQyxVQUFVLENBQUMsT0FBTyxDQUFDLE9BQU8sQ0FBQztxQkFDeEQsQ0FBQztvQkFFRixJQUFJLENBQUM7d0JBQ0gsTUFBTSxnQkFBZ0IsR0FBRyxNQUFNLFVBQVUsQ0FBQyxhQUFhLENBQUMsS0FBSyxFQUFFLEtBQUssQ0FBQyxDQUFDO3dCQUN0RSxJQ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QztJQUMzQyxNQUFNLENBQUMsY0FBYyxDQUFDLFVBQXNCO1FBQzFDLE1BQU0sTUFBTSxHQUFHLElBQUksZUFBZSxDQUF5QjtZQUN6RCxhQUFhLEVBQUUsS0FBSyxFQUFFLEtBQUssRUFBRSxFQUFFLElBQUksRUFBRSxFQUFFLEVBQUU7Z0JBQ3ZDLElBQUksQ0FBQyxLQUFLLENBQUMsQ0FBQyxDQUFDLGdDQUFnQztnQkFDN0MsT0FBTyxJQUFJLENBQUM7WUFDZCxDQUFDO1NBQ0YsQ0FBQyxDQUFDO1FBRUgsTUFBTSxNQUFNLEdBQUcsTUFBTSxDQUFDLFFBQVEsQ0FBQyxTQUFTLEVBQUUsQ0FBQztRQUMzQyxNQUFNLENBQUMsS0FBSyxDQUFDLFVBQVUsQ0FBQyxDQUFDLElBQUksQ0FBQyxHQUFHLEVBQUUsQ0FBQyxNQUFNLENBQUMsS0FBSyxFQUFFLENBQUMsQ0FBQztRQUVwRCxPQUFPLE1BQU0sQ0FBQztJQUNoQixDQUFDO0NBQ0YifQ==
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@push.rocks/smartstream",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"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.",
|
|
6
6
|
"type": "module",
|
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.1.
|
|
6
|
+
version: '3.1.1',
|
|
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
|
}
|
|
@@ -3,6 +3,6 @@
|
|
|
3
3
|
*/
|
|
4
4
|
export const commitinfo = {
|
|
5
5
|
name: '@push.rocks/smartstream',
|
|
6
|
-
version: '3.1.
|
|
6
|
+
version: '3.1.1',
|
|
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
|
}
|
|
@@ -1,25 +1,22 @@
|
|
|
1
1
|
import * as plugins from './plugins.js';
|
|
2
2
|
|
|
3
|
-
|
|
4
3
|
// ========================================
|
|
5
|
-
//
|
|
4
|
+
// Interfaces for Read functionality
|
|
6
5
|
// ========================================
|
|
7
6
|
export interface IStreamToolsRead<TInput, TOutput> {
|
|
8
7
|
done: () => void;
|
|
9
|
-
write: (writeArg: TInput) => void
|
|
8
|
+
write: (writeArg: TInput) => Promise<void>;
|
|
10
9
|
}
|
|
11
10
|
|
|
12
11
|
/**
|
|
13
|
-
*
|
|
14
|
-
* -> the WebDuplexStream is being read from
|
|
15
|
-
* and at the same time if nothing is enqueued
|
|
12
|
+
* The read function is called when data needs to be read into the stream.
|
|
16
13
|
*/
|
|
17
14
|
export interface IStreamReadFunction<TInput, TOutput> {
|
|
18
15
|
(toolsArg: IStreamToolsRead<TInput, TOutput>): Promise<void>;
|
|
19
16
|
}
|
|
20
17
|
|
|
21
18
|
// ========================================
|
|
22
|
-
//
|
|
19
|
+
// Interfaces for Write functionality
|
|
23
20
|
// ========================================
|
|
24
21
|
export interface IStreamToolsWrite<TInput, TOutput> {
|
|
25
22
|
truncate: () => void;
|
|
@@ -27,15 +24,14 @@ export interface IStreamToolsWrite<TInput, TOutput> {
|
|
|
27
24
|
}
|
|
28
25
|
|
|
29
26
|
/**
|
|
30
|
-
*
|
|
31
|
-
* It is called anytime a chunk is written to the stream.
|
|
27
|
+
* The write function is called whenever a chunk is written to the stream.
|
|
32
28
|
*/
|
|
33
29
|
export interface IStreamWriteFunction<TInput, TOutput> {
|
|
34
30
|
(chunkArg: TInput, toolsArg: IStreamToolsWrite<TInput, TOutput>): Promise<any>;
|
|
35
31
|
}
|
|
36
32
|
|
|
37
33
|
export interface IStreamFinalFunction<TInput, TOutput> {
|
|
38
|
-
(toolsArg: IStreamToolsWrite<TInput, TOutput>): Promise<TOutput>;
|
|
34
|
+
(toolsArg: IStreamToolsWrite<TInput, TOutput>): Promise<TOutput | void>;
|
|
39
35
|
}
|
|
40
36
|
|
|
41
37
|
export interface WebDuplexStreamOptions<TInput, TOutput> {
|
|
@@ -45,112 +41,95 @@ export interface WebDuplexStreamOptions<TInput, TOutput> {
|
|
|
45
41
|
}
|
|
46
42
|
|
|
47
43
|
export class WebDuplexStream<TInput = any, TOutput = any> extends TransformStream<TInput, TOutput> {
|
|
48
|
-
static fromUInt8Array(uint8Array: Uint8Array): WebDuplexStream<Uint8Array, Uint8Array> {
|
|
49
|
-
const stream = new WebDuplexStream<Uint8Array, Uint8Array>({
|
|
50
|
-
writeFunction: async (chunk, { push }) => {
|
|
51
|
-
push(chunk); // Directly push the chunk as is
|
|
52
|
-
return null;
|
|
53
|
-
}
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
const writer = stream.writable.getWriter();
|
|
57
|
-
writer.write(uint8Array).then(() => writer.close());
|
|
58
|
-
|
|
59
|
-
return stream;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
44
|
// INSTANCE
|
|
63
45
|
options: WebDuplexStreamOptions<TInput, TOutput>;
|
|
64
46
|
|
|
65
47
|
constructor(optionsArg: WebDuplexStreamOptions<TInput, TOutput>) {
|
|
66
|
-
// here we call into the official web stream api
|
|
67
48
|
super({
|
|
49
|
+
async start(controller) {
|
|
50
|
+
// Optionally initialize any state here
|
|
51
|
+
},
|
|
68
52
|
async transform(chunk, controller) {
|
|
69
|
-
// Transformation logic remains unchanged
|
|
70
53
|
if (optionsArg?.writeFunction) {
|
|
71
54
|
const tools: IStreamToolsWrite<TInput, TOutput> = {
|
|
72
55
|
truncate: () => controller.terminate(),
|
|
73
56
|
push: (pushArg: TOutput) => controller.enqueue(pushArg),
|
|
74
57
|
};
|
|
75
|
-
|
|
58
|
+
|
|
76
59
|
try {
|
|
77
60
|
const writeReturnChunk = await optionsArg.writeFunction(chunk, tools);
|
|
78
|
-
if (writeReturnChunk
|
|
61
|
+
if (writeReturnChunk !== undefined && writeReturnChunk !== null) {
|
|
79
62
|
controller.enqueue(writeReturnChunk);
|
|
80
63
|
}
|
|
81
64
|
} catch (err) {
|
|
82
65
|
controller.error(err);
|
|
83
66
|
}
|
|
84
67
|
} else {
|
|
85
|
-
|
|
68
|
+
// If no writeFunction is provided, pass the chunk through
|
|
69
|
+
controller.enqueue(chunk as unknown as TOutput);
|
|
86
70
|
}
|
|
87
71
|
},
|
|
88
72
|
async flush(controller) {
|
|
89
|
-
// Flush logic remains unchanged
|
|
90
73
|
if (optionsArg?.finalFunction) {
|
|
91
74
|
const tools: IStreamToolsWrite<TInput, TOutput> = {
|
|
92
75
|
truncate: () => controller.terminate(),
|
|
93
|
-
push: (
|
|
76
|
+
push: (pushArg) => controller.enqueue(pushArg),
|
|
94
77
|
};
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
78
|
+
|
|
79
|
+
try {
|
|
80
|
+
const finalChunk = await optionsArg.finalFunction(tools);
|
|
81
|
+
if (finalChunk) {
|
|
82
|
+
controller.enqueue(finalChunk);
|
|
83
|
+
}
|
|
84
|
+
} catch (err) {
|
|
85
|
+
controller.error(err);
|
|
86
|
+
} finally {
|
|
87
|
+
controller.terminate();
|
|
88
|
+
}
|
|
104
89
|
} else {
|
|
105
90
|
controller.terminate();
|
|
106
91
|
}
|
|
107
|
-
}
|
|
92
|
+
},
|
|
108
93
|
});
|
|
109
94
|
|
|
110
95
|
this.options = optionsArg;
|
|
96
|
+
|
|
97
|
+
// Start producing data if readFunction is provided
|
|
98
|
+
if (this.options.readFunction) {
|
|
99
|
+
this._startReading();
|
|
100
|
+
}
|
|
111
101
|
}
|
|
112
102
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
const
|
|
118
|
-
|
|
119
|
-
async
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
const newReader = readableStream.getReader();
|
|
141
|
-
const { value: newValue, done: newDone } = await newReader.read();
|
|
142
|
-
newReader.releaseLock();
|
|
143
|
-
|
|
144
|
-
if (newDone) {
|
|
145
|
-
controller.close();
|
|
146
|
-
} else {
|
|
147
|
-
controller.enqueue(newValue);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
}
|
|
103
|
+
private async _startReading() {
|
|
104
|
+
const writable = this.writable;
|
|
105
|
+
const writer = writable.getWriter();
|
|
106
|
+
|
|
107
|
+
const tools: IStreamToolsRead<TInput, TOutput> = {
|
|
108
|
+
done: () => writer.close(),
|
|
109
|
+
write: async (writeArg) => await writer.write(writeArg),
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
try {
|
|
113
|
+
await this.options.readFunction(tools);
|
|
114
|
+
} catch (err) {
|
|
115
|
+
writer.abort(err);
|
|
116
|
+
} finally {
|
|
117
|
+
writer.releaseLock();
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Static method example (adjust as needed)
|
|
122
|
+
static fromUInt8Array(uint8Array: Uint8Array): WebDuplexStream<Uint8Array, Uint8Array> {
|
|
123
|
+
const stream = new WebDuplexStream<Uint8Array, Uint8Array>({
|
|
124
|
+
writeFunction: async (chunk, { push }) => {
|
|
125
|
+
push(chunk); // Directly push the chunk as is
|
|
126
|
+
return null;
|
|
127
|
+
},
|
|
152
128
|
});
|
|
153
129
|
|
|
154
|
-
|
|
130
|
+
const writer = stream.writable.getWriter();
|
|
131
|
+
writer.write(uint8Array).then(() => writer.close());
|
|
132
|
+
|
|
133
|
+
return stream;
|
|
155
134
|
}
|
|
156
|
-
}
|
|
135
|
+
}
|