feat(SmartDuplex): Added method to create SmartDuplex from a WebReadableStream.
This commit is contained in:
@@ -52,6 +52,31 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
|
||||
return smartDuplex;
|
||||
}
|
||||
|
||||
public static fromWebReadableStream<T = any>(
|
||||
readableStream: ReadableStream<T>
|
||||
): SmartDuplex<T, T> {
|
||||
const smartDuplex = new SmartDuplex<T, T>({
|
||||
readFunction: async () => {
|
||||
const reader = readableStream.getReader();
|
||||
try {
|
||||
while (true) {
|
||||
const { value, done } = await reader.read();
|
||||
if (value !== undefined) {
|
||||
smartDuplex.push(value);
|
||||
}
|
||||
if (done) {
|
||||
smartDuplex.end();
|
||||
break;
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
reader.releaseLock();
|
||||
}
|
||||
},
|
||||
});
|
||||
return smartDuplex;
|
||||
}
|
||||
|
||||
// INSTANCE
|
||||
private backpressuredArray: plugins.lik.BackpressuredArray<TOutput>; // an array that only takes a defined amount of items
|
||||
public options: ISmartDuplexOptions<TInput, TOutput>;
|
||||
|
||||
Reference in New Issue
Block a user