Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
c8465b82be | |||
b593e3a32c | |||
a490f521ab | |||
59027782dc | |||
8c7dd7970c | |||
22d18dc21f | |||
1cb6f727af | |||
824c44d165 | |||
3e062103f8 | |||
6451e93c12 |
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@push.rocks/smartstream",
|
||||
"version": "3.0.39",
|
||||
"version": "3.0.44",
|
||||
"private": false,
|
||||
"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.",
|
||||
"type": "module",
|
||||
|
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartstream',
|
||||
version: '3.0.39',
|
||||
version: '3.0.44',
|
||||
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.'
|
||||
}
|
||||
|
@ -1,3 +1,8 @@
|
||||
import { stream } from './smartstream.plugins.js';
|
||||
export {
|
||||
stream,
|
||||
}
|
||||
|
||||
export * from './smartstream.classes.smartduplex.js';
|
||||
export * from './smartstream.classes.streamwrapper.js';
|
||||
export * from './smartstream.classes.streamintake.js';
|
||||
|
@ -157,4 +157,53 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
|
||||
this.backpressuredArray.push(null);
|
||||
callback();
|
||||
}
|
||||
|
||||
public async getWebStreams(): Promise<{ readable: ReadableStream, writable: WritableStream }> {
|
||||
const duplex = this;
|
||||
const readable = new ReadableStream({
|
||||
start(controller) {
|
||||
duplex.on('readable', () => {
|
||||
let chunk;
|
||||
while (null !== (chunk = duplex.read())) {
|
||||
controller.enqueue(chunk);
|
||||
}
|
||||
});
|
||||
|
||||
duplex.on('end', () => {
|
||||
controller.close();
|
||||
});
|
||||
},
|
||||
cancel(reason) {
|
||||
duplex.destroy(new Error(reason));
|
||||
}
|
||||
});
|
||||
|
||||
const writable = new WritableStream({
|
||||
write(chunk) {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
const isBackpressured = !duplex.write(chunk, (error) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
|
||||
if (isBackpressured) {
|
||||
duplex.once('drain', resolve);
|
||||
}
|
||||
});
|
||||
},
|
||||
close() {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
duplex.end(resolve);
|
||||
});
|
||||
},
|
||||
abort(reason) {
|
||||
duplex.destroy(new Error(reason));
|
||||
}
|
||||
});
|
||||
|
||||
return { readable, writable };
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,45 @@
|
||||
import * as plugins from './smartstream.plugins.js';
|
||||
|
||||
export class StreamIntake<T> extends plugins.stream.Readable {
|
||||
// STATIC
|
||||
public static async fromStream<U>(inputStream: plugins.stream.Readable | ReadableStream, options?: plugins.stream.ReadableOptions): Promise<StreamIntake<U>> {
|
||||
const intakeStream = new StreamIntake<U>(options);
|
||||
|
||||
if (inputStream instanceof plugins.stream.Readable) {
|
||||
inputStream.on('data', (chunk: U) => {
|
||||
intakeStream.pushData(chunk);
|
||||
});
|
||||
|
||||
inputStream.on('end', () => {
|
||||
intakeStream.signalEnd();
|
||||
});
|
||||
|
||||
inputStream.on('error', (err: Error) => {
|
||||
intakeStream.destroy(err);
|
||||
});
|
||||
} else {
|
||||
const reader = (inputStream as ReadableStream).getReader();
|
||||
|
||||
const readChunk = () => {
|
||||
reader.read().then(({ done, value }) => {
|
||||
if (done) {
|
||||
intakeStream.signalEnd();
|
||||
} else {
|
||||
intakeStream.pushData(value);
|
||||
readChunk();
|
||||
}
|
||||
}).catch((err) => {
|
||||
intakeStream.destroy(err);
|
||||
});
|
||||
};
|
||||
|
||||
readChunk();
|
||||
}
|
||||
|
||||
return intakeStream;
|
||||
}
|
||||
|
||||
// INSTANCE
|
||||
private signalEndBoolean = false;
|
||||
private chunkStore: T[] = [];
|
||||
public pushNextObservable = new plugins.smartrx.ObservableIntake<any>();
|
||||
|
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartstream',
|
||||
version: '3.0.39',
|
||||
version: '3.0.44',
|
||||
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.'
|
||||
}
|
||||
|
@ -1,64 +0,0 @@
|
||||
export interface IDuplexStream {
|
||||
read(): any;
|
||||
write(chunk: any, callback?: (error?: Error | null) => void): boolean;
|
||||
on(event: string, listener: (...args: any[]) => void): this;
|
||||
once(event: string, listener: (...args: any[]) => void): this;
|
||||
end(callback?: () => void): void;
|
||||
destroy(error?: Error): void;
|
||||
}
|
||||
|
||||
export interface IReadableStreamOptions {
|
||||
highWaterMark?: number;
|
||||
}
|
||||
|
||||
export interface IWritableStreamOptions {
|
||||
highWaterMark?: number;
|
||||
}
|
||||
|
||||
export function convertDuplexToWebStream(duplex: IDuplexStream): { readable: ReadableStream, writable: WritableStream } {
|
||||
const readable = new ReadableStream({
|
||||
start(controller) {
|
||||
duplex.on('readable', () => {
|
||||
let chunk;
|
||||
while (null !== (chunk = duplex.read())) {
|
||||
controller.enqueue(chunk);
|
||||
}
|
||||
});
|
||||
|
||||
duplex.on('end', () => {
|
||||
controller.close();
|
||||
});
|
||||
},
|
||||
cancel(reason) {
|
||||
duplex.destroy(new Error(reason));
|
||||
}
|
||||
});
|
||||
|
||||
const writable = new WritableStream({
|
||||
write(chunk) {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
const isBackpressured = !duplex.write(chunk, (error) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
|
||||
if (isBackpressured) {
|
||||
duplex.once('drain', resolve);
|
||||
}
|
||||
});
|
||||
},
|
||||
close() {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
duplex.end(resolve);
|
||||
});
|
||||
},
|
||||
abort(reason) {
|
||||
duplex.destroy(new Error(reason));
|
||||
}
|
||||
});
|
||||
|
||||
return { readable, writable };
|
||||
}
|
@ -1,5 +1,2 @@
|
||||
import './plugins.js';
|
||||
export * from './classes.webduplexstream.js';
|
||||
export {
|
||||
convertDuplexToWebStream,
|
||||
} from './convert.js';
|
Reference in New Issue
Block a user