@push.rocks/smartstream 3.3.0 → 3.4.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.
@@ -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,25 @@ 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
115
  await this.options.readFunction(tools);
116
+ if (doneSignaled) {
117
+ await writer.close();
118
+ }
114
119
  } catch (err) {
115
- writer.abort(err);
116
- } finally {
117
- writer.releaseLock();
120
+ try {
121
+ await writer.abort(err);
122
+ } catch (_) {
123
+ // Writer may already be in error state
124
+ }
118
125
  }
119
126
  }
120
127
 
@@ -128,8 +135,8 @@ export class WebDuplexStream<TInput = any, TOutput = any> extends TransformStrea
128
135
  });
129
136
 
130
137
  const writer = stream.writable.getWriter();
131
- writer.write(uint8Array).then(() => writer.close());
138
+ writer.write(uint8Array).then(() => writer.close()).catch(() => {});
132
139
 
133
140
  return stream;
134
141
  }
135
- }
142
+ }