feat(smartduplex): improve backpressure handling and web/node stream interoperability
This commit is contained in:
@@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartstream',
|
||||
version: '3.3.0',
|
||||
version: '3.4.0',
|
||||
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.'
|
||||
}
|
||||
|
||||
@@ -78,17 +78,14 @@ export class WebDuplexStream<TInput = any, TOutput = any> extends TransformStrea
|
||||
|
||||
try {
|
||||
const finalChunk = await optionsArg.finalFunction(tools);
|
||||
if (finalChunk) {
|
||||
controller.enqueue(finalChunk);
|
||||
if (finalChunk !== undefined && finalChunk !== null) {
|
||||
controller.enqueue(finalChunk as TOutput);
|
||||
}
|
||||
} catch (err) {
|
||||
controller.error(err);
|
||||
} finally {
|
||||
controller.terminate();
|
||||
}
|
||||
} else {
|
||||
controller.terminate();
|
||||
}
|
||||
// TransformStream auto-closes readable after flush resolves — no terminate() needed
|
||||
},
|
||||
});
|
||||
|
||||
@@ -96,7 +93,9 @@ export class WebDuplexStream<TInput = any, TOutput = any> extends TransformStrea
|
||||
|
||||
// Start producing data if readFunction is provided
|
||||
if (this.options.readFunction) {
|
||||
this._startReading();
|
||||
this._startReading().catch((err) => {
|
||||
// Prevent unhandled rejection — the error is propagated through the writable side
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,17 +103,25 @@ export class WebDuplexStream<TInput = any, TOutput = any> extends TransformStrea
|
||||
const writable = this.writable;
|
||||
const writer = writable.getWriter();
|
||||
|
||||
let doneSignaled = false;
|
||||
const tools: IStreamToolsRead<TInput, TOutput> = {
|
||||
done: () => writer.close(),
|
||||
done: () => {
|
||||
doneSignaled = true;
|
||||
},
|
||||
write: async (writeArg) => await writer.write(writeArg),
|
||||
};
|
||||
|
||||
try {
|
||||
await this.options.readFunction(tools);
|
||||
if (doneSignaled) {
|
||||
await writer.close();
|
||||
}
|
||||
} catch (err) {
|
||||
writer.abort(err);
|
||||
} finally {
|
||||
writer.releaseLock();
|
||||
try {
|
||||
await writer.abort(err);
|
||||
} catch (_) {
|
||||
// Writer may already be in error state
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,8 +135,8 @@ export class WebDuplexStream<TInput = any, TOutput = any> extends TransformStrea
|
||||
});
|
||||
|
||||
const writer = stream.writable.getWriter();
|
||||
writer.write(uint8Array).then(() => writer.close());
|
||||
writer.write(uint8Array).then(() => writer.close()).catch(() => {});
|
||||
|
||||
return stream;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user