Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
6a2ef1b152 | |||
7b1d2199e9 | |||
04c22f73df | |||
c8dc791c83 | |||
9c30e5bab1 | |||
5f2c5f9380 | |||
f9b8bf33b0 | |||
a55b2548d7 | |||
c8465b82be | |||
b593e3a32c |
55
changelog.md
Normal file
55
changelog.md
Normal file
@ -0,0 +1,55 @@
|
||||
# Changelog
|
||||
|
||||
## 2024-10-13 - 3.1.1 - fix(WebDuplexStream)
|
||||
Improved read/write interface and error handling in WebDuplexStream
|
||||
|
||||
- Enhanced the IStreamToolsRead and IStreamToolsWrite interfaces for better Promise handling
|
||||
- Refined readFunction and writeFunction handling to accommodate asynchronous data processing and error propagation
|
||||
- Added internal _startReading method to facilitate initial data handling if readFunction is present
|
||||
- Maintained backward compatibility while ensuring data continuity when no writeFunction is specified
|
||||
|
||||
## 2024-10-13 - 3.1.0 - feat(core)
|
||||
Add support for creating Web ReadableStream from a file
|
||||
|
||||
- Introduced a new helper function `createWebReadableStreamFromFile` that allows for creating a Web ReadableStream from a file path.
|
||||
- Updated exports in `ts/index.ts` to include `nodewebhelpers` which provides the new web stream feature.
|
||||
|
||||
## 2024-10-13 - 3.0.46 - fix(WebDuplexStream)
|
||||
Fix errors in WebDuplexStream transformation and test logic
|
||||
|
||||
- Corrected async handling in WebDuplexStream write function
|
||||
- Fixed `WebDuplexStream` tests to properly handle asynchronous reading and writing
|
||||
|
||||
## 2024-10-13 - 3.0.45 - fix(ts)
|
||||
Fixed formatting issues in SmartDuplex class
|
||||
|
||||
- Resolved inconsistent spacing in SmartDuplex class methods and constructor.
|
||||
- Ensured consistent formatting in the getWebStreams method.
|
||||
|
||||
## 2024-06-02 - 3.0.39 - smartduplex
|
||||
Add .getWebStreams method
|
||||
|
||||
- Introduced a new `.getWebStreams` method in the smartduplex module, providing compatibility with the web streams API.
|
||||
|
||||
## 2024-03-16 - 3.0.34 - configuration
|
||||
Update project configuration files
|
||||
|
||||
- Updated `tsconfig` for optimization.
|
||||
- Modified `npmextra.json` to set the `githost` attribute.
|
||||
|
||||
## 2023-11-03 - 3.0.0 to 3.0.8 - core
|
||||
Transition to major version 3.x
|
||||
|
||||
- Implemented breaking changes in the core system for better performance and feature set.
|
||||
- Continuous core updates to improve stability and performance across minor version increments.
|
||||
|
||||
## 2023-11-02 - 2.0.4 to 2.0.8 - core
|
||||
Core updates and a major fix
|
||||
|
||||
- Implemented core updates addressing minor bugs and enhancements.
|
||||
- A significant breaking change update transitioning from 2.0.x to 3.0.0.
|
||||
|
||||
## 2022-03-31 - 2.0.0 - core
|
||||
Major esm transition
|
||||
|
||||
- Implemented a breaking change by switching the core to ESM (ECMAScript Module) format for modernized module handling.
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@push.rocks/smartstream",
|
||||
"version": "3.0.43",
|
||||
"version": "3.1.1",
|
||||
"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",
|
||||
|
@ -1,37 +1,37 @@
|
||||
import { expect, expectAsync, tap } from '@push.rocks/tapbundle';
|
||||
import { expect, tap } from '@push.rocks/tapbundle';
|
||||
import * as webstream from '../ts_web/index.js';
|
||||
|
||||
tap.test('WebDuplexStream', async (toolsArg) => {
|
||||
const testDone = toolsArg.defer(); // Create a deferred object to control test completion.
|
||||
tap.test('WebDuplexStream fromUInt8Array should read back the same Uint8Array', async () => {
|
||||
const inputUint8Array = new Uint8Array([1, 2, 3, 4, 5]);
|
||||
const stream = webstream.WebDuplexStream.fromUInt8Array(inputUint8Array);
|
||||
|
||||
const reader = stream.readable.getReader();
|
||||
let readUint8Array = new Uint8Array();
|
||||
|
||||
reader.read().then(function processText({ done, value }) {
|
||||
if (done) {
|
||||
expect(readUint8Array).toEqual(inputUint8Array);
|
||||
testDone.resolve(); // Correctly signal that the test is done.
|
||||
return;
|
||||
// Read from the stream
|
||||
while (true) {
|
||||
const { value, done } = await reader.read();
|
||||
if (done) break;
|
||||
if (value) {
|
||||
// Concatenate value to readUint8Array
|
||||
const tempArray = new Uint8Array(readUint8Array.length + value.length);
|
||||
tempArray.set(readUint8Array, 0);
|
||||
tempArray.set(value, readUint8Array.length);
|
||||
readUint8Array = tempArray;
|
||||
}
|
||||
readUint8Array = new Uint8Array([...readUint8Array, ...value]);
|
||||
return reader.read().then(processText);
|
||||
}
|
||||
|
||||
expect(readUint8Array).toEqual(inputUint8Array);
|
||||
});
|
||||
|
||||
return testDone.promise; // Return the promise to properly wait for the test to complete.
|
||||
});
|
||||
|
||||
|
||||
tap.test('should handle transform with a write function', async (toolsArg) => {
|
||||
const testDone = toolsArg.defer();
|
||||
tap.test('WebDuplexStream should handle transform with a write function', async () => {
|
||||
const input = [1, 2, 3, 4, 5];
|
||||
const expectedOutput = [2, 4, 6, 8, 10];
|
||||
|
||||
const transformStream = new webstream.WebDuplexStream<number, number>({
|
||||
writeFunction: (chunk, { push }) => {
|
||||
push(chunk * 2); // Push the doubled number into the stream
|
||||
return Promise.resolve(); // Resolve the promise immediately
|
||||
writeFunction: async (chunk, { push }) => {
|
||||
// Push the doubled number into the stream
|
||||
push(chunk * 2);
|
||||
},
|
||||
});
|
||||
|
||||
@ -40,31 +40,28 @@ tap.test('should handle transform with a write function', async (toolsArg) => {
|
||||
|
||||
const output: number[] = [];
|
||||
|
||||
// Process the text and resolve the test once done.
|
||||
const processText = async ({ done, value }) => {
|
||||
if (done) {
|
||||
expect(output).toEqual(expectedOutput);
|
||||
testDone.resolve(); // Resolve the deferred test once all values have been read.
|
||||
return;
|
||||
}
|
||||
// Read from the stream asynchronously
|
||||
const readPromise = (async () => {
|
||||
while (true) {
|
||||
const { value, done } = await readableStream.read();
|
||||
if (done) break;
|
||||
if (value !== undefined) {
|
||||
output.push(value);
|
||||
}
|
||||
// Continue reading and processing.
|
||||
await readableStream.read().then(processText);
|
||||
};
|
||||
}
|
||||
})();
|
||||
|
||||
// Start the read process before writing to the stream.
|
||||
readableStream.read().then(processText);
|
||||
|
||||
// Sequentially write to the stream and close when done.
|
||||
// Write to the stream
|
||||
for (const num of input) {
|
||||
await writableStream.write(num);
|
||||
}
|
||||
await writableStream.close();
|
||||
|
||||
return testDone.promise; // This will wait until the testDone is resolved before completing the test.
|
||||
// Wait for the reading to complete
|
||||
await readPromise;
|
||||
|
||||
// Assert that the output matches the expected transformed data
|
||||
expect(output).toEqual(expectedOutput);
|
||||
});
|
||||
|
||||
|
||||
tap.start();
|
@ -1,8 +1,8 @@
|
||||
/**
|
||||
* autocreated commitinfo by @pushrocks/commitinfo
|
||||
* autocreated commitinfo by @push.rocks/commitinfo
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartstream',
|
||||
version: '3.0.43',
|
||||
version: '3.1.1',
|
||||
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';
|
||||
@ -6,3 +11,7 @@ export * from './smartstream.functions.js';
|
||||
|
||||
import * as plugins from './smartstream.plugins.js';
|
||||
export const webstream = plugins.webstream;
|
||||
import * as nodewebhelpers from './smartstream.nodewebhelpers.js';
|
||||
export {
|
||||
nodewebhelpers,
|
||||
}
|
||||
|
@ -53,21 +53,29 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
|
||||
}
|
||||
|
||||
// INSTANCE
|
||||
private backpressuredArray: plugins.lik.BackpressuredArray<TOutput>;
|
||||
private backpressuredArray: plugins.lik.BackpressuredArray<TOutput>; // an array that only takes a defined amount of items
|
||||
public options: ISmartDuplexOptions<TInput, TOutput>;
|
||||
private observableSubscription?: plugins.smartrx.rxjs.Subscription;
|
||||
private debugLog(messageArg: string) {
|
||||
// optional debug log
|
||||
if (this.options.debug) {
|
||||
console.log(messageArg);
|
||||
}
|
||||
}
|
||||
|
||||
constructor(optionsArg?: ISmartDuplexOptions<TInput, TOutput>) {
|
||||
super(Object.assign({
|
||||
super(
|
||||
Object.assign(
|
||||
{
|
||||
highWaterMark: 1,
|
||||
}, optionsArg));
|
||||
},
|
||||
optionsArg
|
||||
)
|
||||
);
|
||||
this.options = optionsArg;
|
||||
this.backpressuredArray = new plugins.lik.BackpressuredArray<TOutput>(this.options.highWaterMark || 1)
|
||||
this.backpressuredArray = new plugins.lik.BackpressuredArray<TOutput>(
|
||||
this.options.highWaterMark || 1
|
||||
);
|
||||
}
|
||||
|
||||
public async _read(size: number): Promise<void> {
|
||||
@ -92,7 +100,7 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
|
||||
this.debugLog(`${this.options.name}: can push more again`);
|
||||
}
|
||||
return canPushMore;
|
||||
};
|
||||
}
|
||||
|
||||
private asyncWritePromiseObjectmap = new plugins.lik.ObjectMap<Promise<any>>();
|
||||
// Ensure the _write method types the chunk as TInput and encodes TOutput
|
||||
@ -110,7 +118,7 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
|
||||
},
|
||||
push: async (pushArg: TOutput) => {
|
||||
return await this.backpressuredPush(pushArg);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
try {
|
||||
@ -158,7 +166,7 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
|
||||
callback();
|
||||
}
|
||||
|
||||
public async getWebStreams(): Promise<{ readable: ReadableStream, writable: WritableStream }> {
|
||||
public async getWebStreams(): Promise<{ readable: ReadableStream; writable: WritableStream }> {
|
||||
const duplex = this;
|
||||
const readable = new ReadableStream({
|
||||
start(controller) {
|
||||
@ -175,7 +183,7 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
|
||||
},
|
||||
cancel(reason) {
|
||||
duplex.destroy(new Error(reason));
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
const writable = new WritableStream({
|
||||
@ -201,7 +209,7 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
|
||||
},
|
||||
abort(reason) {
|
||||
duplex.destroy(new Error(reason));
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
return { readable, writable };
|
||||
|
@ -2,7 +2,7 @@ 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): StreamIntake<U> {
|
||||
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) {
|
||||
|
34
ts/smartstream.nodewebhelpers.ts
Normal file
34
ts/smartstream.nodewebhelpers.ts
Normal file
@ -0,0 +1,34 @@
|
||||
import { createReadStream } from 'fs';
|
||||
|
||||
/**
|
||||
* Creates a Web ReadableStream from a file.
|
||||
*
|
||||
* @param filePath - The path to the file to be read
|
||||
* @returns A Web ReadableStream that reads the file in chunks
|
||||
*/
|
||||
export function createWebReadableStreamFromFile(filePath: string): ReadableStream<Uint8Array> {
|
||||
const fileStream = createReadStream(filePath);
|
||||
|
||||
return new ReadableStream({
|
||||
start(controller) {
|
||||
// When data is available, enqueue it into the Web ReadableStream
|
||||
fileStream.on('data', (chunk) => {
|
||||
controller.enqueue(chunk as Uint8Array);
|
||||
});
|
||||
|
||||
// When the file stream ends, close the Web ReadableStream
|
||||
fileStream.on('end', () => {
|
||||
controller.close();
|
||||
});
|
||||
|
||||
// If there's an error, error the Web ReadableStream
|
||||
fileStream.on('error', (err) => {
|
||||
controller.error(err);
|
||||
});
|
||||
},
|
||||
cancel() {
|
||||
// If the Web ReadableStream is canceled, destroy the file stream
|
||||
fileStream.destroy();
|
||||
}
|
||||
});
|
||||
}
|
@ -1,8 +1,8 @@
|
||||
/**
|
||||
* autocreated commitinfo by @pushrocks/commitinfo
|
||||
* autocreated commitinfo by @push.rocks/commitinfo
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartstream',
|
||||
version: '3.0.43',
|
||||
version: '3.1.1',
|
||||
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,25 +1,22 @@
|
||||
import * as plugins from './plugins.js';
|
||||
|
||||
|
||||
// ========================================
|
||||
// READ
|
||||
// Interfaces for Read functionality
|
||||
// ========================================
|
||||
export interface IStreamToolsRead<TInput, TOutput> {
|
||||
done: () => void;
|
||||
write: (writeArg: TInput) => void;
|
||||
write: (writeArg: TInput) => Promise<void>;
|
||||
}
|
||||
|
||||
/**
|
||||
* the read function is called anytime
|
||||
* -> the WebDuplexStream is being read from
|
||||
* and at the same time if nothing is enqueued
|
||||
* The read function is called when data needs to be read into the stream.
|
||||
*/
|
||||
export interface IStreamReadFunction<TInput, TOutput> {
|
||||
(toolsArg: IStreamToolsRead<TInput, TOutput>): Promise<void>;
|
||||
}
|
||||
|
||||
// ========================================
|
||||
// WRITE
|
||||
// Interfaces for Write functionality
|
||||
// ========================================
|
||||
export interface IStreamToolsWrite<TInput, TOutput> {
|
||||
truncate: () => void;
|
||||
@ -27,15 +24,14 @@ export interface IStreamToolsWrite<TInput, TOutput> {
|
||||
}
|
||||
|
||||
/**
|
||||
* the write function can return something.
|
||||
* It is called anytime a chunk is written to the stream.
|
||||
* The write function is called whenever 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>;
|
||||
(toolsArg: IStreamToolsWrite<TInput, TOutput>): Promise<TOutput | void>;
|
||||
}
|
||||
|
||||
export interface WebDuplexStreamOptions<TInput, TOutput> {
|
||||
@ -45,12 +41,90 @@ export interface WebDuplexStreamOptions<TInput, TOutput> {
|
||||
}
|
||||
|
||||
export class WebDuplexStream<TInput = any, TOutput = any> extends TransformStream<TInput, TOutput> {
|
||||
// INSTANCE
|
||||
options: WebDuplexStreamOptions<TInput, TOutput>;
|
||||
|
||||
constructor(optionsArg: WebDuplexStreamOptions<TInput, TOutput>) {
|
||||
super({
|
||||
async start(controller) {
|
||||
// Optionally initialize any state here
|
||||
},
|
||||
async transform(chunk, controller) {
|
||||
if (optionsArg?.writeFunction) {
|
||||
const tools: IStreamToolsWrite<TInput, TOutput> = {
|
||||
truncate: () => controller.terminate(),
|
||||
push: (pushArg: TOutput) => controller.enqueue(pushArg),
|
||||
};
|
||||
|
||||
try {
|
||||
const writeReturnChunk = await optionsArg.writeFunction(chunk, tools);
|
||||
if (writeReturnChunk !== undefined && writeReturnChunk !== null) {
|
||||
controller.enqueue(writeReturnChunk);
|
||||
}
|
||||
} catch (err) {
|
||||
controller.error(err);
|
||||
}
|
||||
} else {
|
||||
// If no writeFunction is provided, pass the chunk through
|
||||
controller.enqueue(chunk as unknown as TOutput);
|
||||
}
|
||||
},
|
||||
async flush(controller) {
|
||||
if (optionsArg?.finalFunction) {
|
||||
const tools: IStreamToolsWrite<TInput, TOutput> = {
|
||||
truncate: () => controller.terminate(),
|
||||
push: (pushArg) => controller.enqueue(pushArg),
|
||||
};
|
||||
|
||||
try {
|
||||
const finalChunk = await optionsArg.finalFunction(tools);
|
||||
if (finalChunk) {
|
||||
controller.enqueue(finalChunk);
|
||||
}
|
||||
} catch (err) {
|
||||
controller.error(err);
|
||||
} finally {
|
||||
controller.terminate();
|
||||
}
|
||||
} else {
|
||||
controller.terminate();
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
this.options = optionsArg;
|
||||
|
||||
// Start producing data if readFunction is provided
|
||||
if (this.options.readFunction) {
|
||||
this._startReading();
|
||||
}
|
||||
}
|
||||
|
||||
private async _startReading() {
|
||||
const writable = this.writable;
|
||||
const writer = writable.getWriter();
|
||||
|
||||
const tools: IStreamToolsRead<TInput, TOutput> = {
|
||||
done: () => writer.close(),
|
||||
write: async (writeArg) => await writer.write(writeArg),
|
||||
};
|
||||
|
||||
try {
|
||||
await this.options.readFunction(tools);
|
||||
} catch (err) {
|
||||
writer.abort(err);
|
||||
} finally {
|
||||
writer.releaseLock();
|
||||
}
|
||||
}
|
||||
|
||||
// Static method example (adjust as needed)
|
||||
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();
|
||||
@ -58,99 +132,4 @@ export class WebDuplexStream<TInput = any, TOutput = any> extends TransformStrea
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user