fix(core): update

This commit is contained in:
2024-06-02 16:42:42 +02:00
parent e52ce7af61
commit 17e03e9790
16 changed files with 331 additions and 160 deletions

View File

@ -0,0 +1,8 @@
/**
* autocreated commitinfo by @pushrocks/commitinfo
*/
export const commitinfo = {
name: '@push.rocks/smartstream',
version: '3.0.39',
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.'
}

View File

@ -0,0 +1,156 @@
import * as plugins from './plugins.js';
// ========================================
// READ
// ========================================
export interface IStreamToolsRead<TInput, TOutput> {
done: () => void;
write: (writeArg: TInput) => void;
}
/**
* the read function is called anytime
* -> the WebDuplexStream is being read from
* and at the same time if nothing is enqueued
*/
export interface IStreamReadFunction<TInput, TOutput> {
(toolsArg: IStreamToolsRead<TInput, TOutput>): Promise<void>;
}
// ========================================
// WRITE
// ========================================
export interface IStreamToolsWrite<TInput, TOutput> {
truncate: () => void;
push: (pushArg: TOutput) => void;
}
/**
* the write function can return something.
* It is called anytime a chunk is written to the stream.
*/
export interface IStreamWriteFunction<TInput, TOutput> {
(chunkArg: TInput, toolsArg: IStreamToolsWrite<TInput, TOutput>): Promise<any>;
}
export interface IStreamFinalFunction<TInput, TOutput> {
(toolsArg: IStreamToolsWrite<TInput, TOutput>): Promise<TOutput>;
}
export interface WebDuplexStreamOptions<TInput, TOutput> {
readFunction?: IStreamReadFunction<TInput, TOutput>;
writeFunction?: IStreamWriteFunction<TInput, TOutput>;
finalFunction?: IStreamFinalFunction<TInput, TOutput>;
}
export class WebDuplexStream<TInput = any, TOutput = any> extends TransformStream<TInput, TOutput> {
static fromUInt8Array(uint8Array: Uint8Array): WebDuplexStream<Uint8Array, Uint8Array> {
const stream = new WebDuplexStream<Uint8Array, Uint8Array>({
writeFunction: async (chunk, { push }) => {
push(chunk); // Directly push the chunk as is
return null;
}
});
const writer = stream.writable.getWriter();
writer.write(uint8Array).then(() => writer.close());
return stream;
}
// INSTANCE
options: WebDuplexStreamOptions<TInput, TOutput>;
constructor(optionsArg: WebDuplexStreamOptions<TInput, TOutput>) {
super({
async transform(chunk, controller) {
// Transformation logic remains unchanged
if (optionsArg?.writeFunction) {
const tools: IStreamToolsWrite<TInput, TOutput> = {
truncate: () => controller.terminate(),
push: (pushArg: TOutput) => controller.enqueue(pushArg),
};
optionsArg.writeFunction(chunk, tools)
.then(writeReturnChunk => {
// the write return chunk is optional
// just in case the write function returns something other than void.
if (writeReturnChunk) {
controller.enqueue(writeReturnChunk);
}
})
.catch(err => controller.error(err));
} else {
controller.error(new Error('No write function provided'));
}
},
async flush(controller) {
// Flush logic remains unchanged
if (optionsArg?.finalFunction) {
const tools: IStreamToolsWrite<TInput, TOutput> = {
truncate: () => controller.terminate(),
push: (pipeObject) => controller.enqueue(pipeObject),
};
optionsArg.finalFunction(tools)
.then(finalChunk => {
if (finalChunk) {
controller.enqueue(finalChunk);
}
})
.catch(err => controller.error(err))
.finally(() => controller.terminate());
} else {
controller.terminate();
}
}
});
this.options = optionsArg;
}
// Method to create a custom readable stream that integrates the readFunction
// readFunction is executed whenever the stream is being read from and nothing is enqueued
getCustomReadableStream() {
const readableStream = this.readable;
const options = this.options;
const customReadable = new ReadableStream({
async pull(controller) {
const reader = readableStream.getReader();
// Check the current state of the original stream
const { value, done } = await reader.read();
reader.releaseLock();
if (done) {
// If the original stream is done, close the custom readable stream
controller.close();
} else {
if (value) {
// If there is data in the original stream, enqueue it and do not execute the readFunction
controller.enqueue(value);
} else if (options.readFunction) {
// If the original stream is empty, execute the readFunction and read again
await options.readFunction({
done: () => controller.close(),
write: (writeArg) => controller.enqueue(writeArg),
});
const newReader = readableStream.getReader();
const { value: newValue, done: newDone } = await newReader.read();
newReader.releaseLock();
if (newDone) {
controller.close();
} else {
controller.enqueue(newValue);
}
}
}
}
});
return customReadable;
}
}

64
ts_web/convert.ts Normal file
View File

@ -0,0 +1,64 @@
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 };
}

5
ts_web/index.ts Normal file
View File

@ -0,0 +1,5 @@
import './plugins.js';
export * from './classes.webduplexstream.js';
export {
convertDuplexToWebStream,
} from './convert.js';

15
ts_web/plugins.ts Normal file
View File

@ -0,0 +1,15 @@
// @push.rocks scope
import * as smartenv from '@push.rocks/smartenv';
export {
smartenv,
}
// lets setup dependencies
const smartenvInstance = new smartenv.Smartenv();
await smartenvInstance.getSafeNodeModule<typeof import('stream/web')>('stream/web', async (moduleArg) => {
globalThis.ReadableStream = moduleArg.ReadableStream;
globalThis.WritableStream = moduleArg.WritableStream;
globalThis.TransformStream = moduleArg.TransformStream;
})