@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.
@@ -1,7 +1,7 @@
1
1
  import * as plugins from './smartstream.plugins.js';
2
2
 
3
3
  /**
4
- * Creates a Web ReadableStream from a file.
4
+ * Creates a Web ReadableStream from a file using pull-based backpressure.
5
5
  *
6
6
  * @param filePath - The path to the file to be read
7
7
  * @returns A Web ReadableStream that reads the file in chunks
@@ -11,23 +11,53 @@ export function createWebReadableStreamFromFile(filePath: string): ReadableStrea
11
11
 
12
12
  return new ReadableStream({
13
13
  start(controller) {
14
- // When data is available, enqueue it into the Web ReadableStream
15
- fileStream.on('data', (chunk) => {
16
- controller.enqueue(chunk as Uint8Array);
14
+ fileStream.on('error', (err) => {
15
+ controller.error(err);
17
16
  });
18
17
 
19
- // When the file stream ends, close the Web ReadableStream
20
18
  fileStream.on('end', () => {
21
19
  controller.close();
22
20
  });
23
21
 
24
- // If there's an error, error the Web ReadableStream
25
- fileStream.on('error', (err) => {
26
- controller.error(err);
22
+ // Pause immediately — pull() will drive reads
23
+ fileStream.pause();
24
+ },
25
+ pull(controller) {
26
+ return new Promise<void>((resolve, reject) => {
27
+ const chunk = fileStream.read();
28
+ if (chunk !== null) {
29
+ controller.enqueue(chunk as Uint8Array);
30
+ resolve();
31
+ return;
32
+ }
33
+ // No data available yet — wait for 'readable' or 'end'
34
+ const onReadable = () => {
35
+ cleanup();
36
+ const data = fileStream.read();
37
+ if (data !== null) {
38
+ controller.enqueue(data as Uint8Array);
39
+ }
40
+ resolve();
41
+ };
42
+ const onEnd = () => {
43
+ cleanup();
44
+ resolve();
45
+ };
46
+ const onError = (err: Error) => {
47
+ cleanup();
48
+ reject(err);
49
+ };
50
+ const cleanup = () => {
51
+ fileStream.removeListener('readable', onReadable);
52
+ fileStream.removeListener('end', onEnd);
53
+ fileStream.removeListener('error', onError);
54
+ };
55
+ fileStream.once('readable', onReadable);
56
+ fileStream.once('end', onEnd);
57
+ fileStream.once('error', onError);
27
58
  });
28
59
  },
29
60
  cancel() {
30
- // If the Web ReadableStream is canceled, destroy the file stream
31
61
  fileStream.destroy();
32
62
  }
33
63
  });
@@ -43,23 +73,25 @@ export function convertWebReadableToNodeReadable(webStream: ReadableStream<Uint8
43
73
  const reader = webStream.getReader();
44
74
 
45
75
  return new plugins.stream.Readable({
46
- async read() {
47
- try {
48
- const { value, done } = await reader.read();
49
- if (done) {
50
- this.push(null); // Signal end of stream
51
- } else {
52
- this.push(Buffer.from(value)); // Convert Uint8Array to Buffer for Node.js Readable
76
+ read() {
77
+ reader.read().then(
78
+ ({ value, done }) => {
79
+ if (done) {
80
+ this.push(null);
81
+ } else {
82
+ this.push(Buffer.from(value));
83
+ }
84
+ },
85
+ (err) => {
86
+ this.destroy(err);
53
87
  }
54
- } catch (err) {
55
- this.destroy(err); // Handle errors by destroying the stream
56
- }
88
+ );
57
89
  }
58
90
  });
59
91
  }
60
92
 
61
93
  /**
62
- * Converts a Node.js Readable stream to a Web ReadableStream.
94
+ * Converts a Node.js Readable stream to a Web ReadableStream using pull-based backpressure.
63
95
  *
64
96
  * @param nodeStream - The Node.js Readable stream to convert
65
97
  * @returns A Web ReadableStream that reads data from the Node.js Readable stream
@@ -67,16 +99,50 @@ export function convertWebReadableToNodeReadable(webStream: ReadableStream<Uint8
67
99
  export function convertNodeReadableToWebReadable(nodeStream: plugins.stream.Readable): ReadableStream<Uint8Array> {
68
100
  return new ReadableStream({
69
101
  start(controller) {
70
- nodeStream.on('data', (chunk) => {
71
- controller.enqueue(new Uint8Array(chunk));
102
+ nodeStream.on('error', (err) => {
103
+ controller.error(err);
72
104
  });
73
105
 
74
106
  nodeStream.on('end', () => {
75
107
  controller.close();
76
108
  });
77
109
 
78
- nodeStream.on('error', (err) => {
79
- controller.error(err);
110
+ // Pause immediately — pull() will drive reads
111
+ nodeStream.pause();
112
+ },
113
+ pull(controller) {
114
+ return new Promise<void>((resolve, reject) => {
115
+ const chunk = nodeStream.read();
116
+ if (chunk !== null) {
117
+ controller.enqueue(new Uint8Array(chunk));
118
+ resolve();
119
+ return;
120
+ }
121
+ // No data available yet — wait for 'readable' or 'end'
122
+ const onReadable = () => {
123
+ cleanup();
124
+ const data = nodeStream.read();
125
+ if (data !== null) {
126
+ controller.enqueue(new Uint8Array(data));
127
+ }
128
+ resolve();
129
+ };
130
+ const onEnd = () => {
131
+ cleanup();
132
+ resolve();
133
+ };
134
+ const onError = (err: Error) => {
135
+ cleanup();
136
+ reject(err);
137
+ };
138
+ const cleanup = () => {
139
+ nodeStream.removeListener('readable', onReadable);
140
+ nodeStream.removeListener('end', onEnd);
141
+ nodeStream.removeListener('error', onError);
142
+ };
143
+ nodeStream.once('readable', onReadable);
144
+ nodeStream.once('end', onEnd);
145
+ nodeStream.once('error', onError);
80
146
  });
81
147
  },
82
148
  cancel() {
@@ -95,19 +161,23 @@ export function convertWebWritableToNodeWritable(webWritable: WritableStream<Uin
95
161
  const writer = webWritable.getWriter();
96
162
 
97
163
  return new plugins.stream.Writable({
98
- async write(chunk, encoding, callback) {
99
- try {
100
- await writer.write(new Uint8Array(chunk));
101
- callback();
102
- } catch (err) {
103
- callback(err);
104
- }
164
+ write(chunk, encoding, callback) {
165
+ writer.write(new Uint8Array(chunk)).then(
166
+ () => callback(),
167
+ (err) => callback(err)
168
+ );
105
169
  },
106
170
  final(callback) {
107
171
  writer.close().then(() => callback()).catch(callback);
108
172
  },
109
173
  destroy(err, callback) {
110
- writer.abort(err).then(() => callback(err)).catch(callback);
174
+ if (err) {
175
+ writer.abort(err).then(() => callback(err)).catch(() => callback(err));
176
+ } else {
177
+ // Clean destroy — just release the lock
178
+ writer.releaseLock();
179
+ callback(null);
180
+ }
111
181
  }
112
182
  });
113
183
  }
@@ -133,7 +203,7 @@ export function convertNodeWritableToWebWritable(nodeWritable: plugins.stream.Wr
133
203
  },
134
204
  close() {
135
205
  return new Promise((resolve, reject) => {
136
- nodeWritable.end((err) => {
206
+ nodeWritable.end((err: Error | null) => {
137
207
  if (err) {
138
208
  reject(err);
139
209
  } else {
@@ -143,9 +213,7 @@ export function convertNodeWritableToWebWritable(nodeWritable: plugins.stream.Wr
143
213
  });
144
214
  },
145
215
  abort(reason) {
146
- return new Promise((resolve, reject) => {
147
- nodeWritable.destroy(reason);
148
- });
216
+ nodeWritable.destroy(reason instanceof Error ? reason : new Error(String(reason)));
149
217
  }
150
218
  });
151
- }
219
+ }
@@ -3,6 +3,6 @@
3
3
  */
4
4
  export const commitinfo = {
5
5
  name: '@push.rocks/smartstream',
6
- version: '3.3.0',
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
  }
@@ -78,17 +78,14 @@ export class WebDuplexStream<TInput = any, TOutput = any> extends TransformStrea
78
78
 
79
79
  try {
80
80
  const finalChunk = await optionsArg.finalFunction(tools);
81
- if (finalChunk) {
82
- controller.enqueue(finalChunk);
81
+ if (finalChunk !== undefined && finalChunk !== null) {
82
+ controller.enqueue(finalChunk as TOutput);
83
83
  }
84
84
  } catch (err) {
85
85
  controller.error(err);
86
- } finally {
87
- controller.terminate();
88
86
  }
89
- } else {
90
- controller.terminate();
91
87
  }
88
+ // TransformStream auto-closes readable after flush resolves — no terminate() needed
92
89
  },
93
90
  });
94
91
 
@@ -96,7 +93,9 @@ export class WebDuplexStream<TInput = any, TOutput = any> extends TransformStrea
96
93
 
97
94
  // Start producing data if readFunction is provided
98
95
  if (this.options.readFunction) {
99
- this._startReading();
96
+ this._startReading().catch((err) => {
97
+ // Prevent unhandled rejection — the error is propagated through the writable side
98
+ });
100
99
  }
101
100
  }
102
101
 
@@ -104,17 +103,28 @@ export class WebDuplexStream<TInput = any, TOutput = any> extends TransformStrea
104
103
  const writable = this.writable;
105
104
  const writer = writable.getWriter();
106
105
 
106
+ let doneSignaled = false;
107
107
  const tools: IStreamToolsRead<TInput, TOutput> = {
108
- done: () => writer.close(),
108
+ done: () => {
109
+ doneSignaled = true;
110
+ },
109
111
  write: async (writeArg) => await writer.write(writeArg),
110
112
  };
111
113
 
112
114
  try {
113
- await this.options.readFunction(tools);
115
+ const readFunction = this.options.readFunction;
116
+ if (readFunction) {
117
+ await readFunction(tools);
118
+ }
119
+ if (doneSignaled) {
120
+ await writer.close();
121
+ }
114
122
  } catch (err) {
115
- writer.abort(err);
116
- } finally {
117
- writer.releaseLock();
123
+ try {
124
+ await writer.abort(err);
125
+ } catch (_) {
126
+ // Writer may already be in error state
127
+ }
118
128
  }
119
129
  }
120
130
 
@@ -128,8 +138,8 @@ export class WebDuplexStream<TInput = any, TOutput = any> extends TransformStrea
128
138
  });
129
139
 
130
140
  const writer = stream.writable.getWriter();
131
- writer.write(uint8Array).then(() => writer.close());
141
+ writer.write(uint8Array).then(() => writer.close()).catch(() => {});
132
142
 
133
143
  return stream;
134
144
  }
135
- }
145
+ }