fix(WebDuplexStream): Fix errors in WebDuplexStream transformation and test logic
This commit is contained in:
parent
f9b8bf33b0
commit
5f2c5f9380
@ -1,5 +1,11 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## 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)
|
## 2024-10-13 - 3.0.45 - fix(ts)
|
||||||
Fixed formatting issues in SmartDuplex class
|
Fixed formatting issues in SmartDuplex class
|
||||||
|
|
||||||
|
@ -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';
|
import * as webstream from '../ts_web/index.js';
|
||||||
|
|
||||||
tap.test('WebDuplexStream', async (toolsArg) => {
|
tap.test('WebDuplexStream fromUInt8Array should read back the same Uint8Array', async () => {
|
||||||
const testDone = toolsArg.defer(); // Create a deferred object to control test completion.
|
|
||||||
const inputUint8Array = new Uint8Array([1, 2, 3, 4, 5]);
|
const inputUint8Array = new Uint8Array([1, 2, 3, 4, 5]);
|
||||||
const stream = webstream.WebDuplexStream.fromUInt8Array(inputUint8Array);
|
const stream = webstream.WebDuplexStream.fromUInt8Array(inputUint8Array);
|
||||||
|
|
||||||
const reader = stream.readable.getReader();
|
const reader = stream.readable.getReader();
|
||||||
let readUint8Array = new Uint8Array();
|
let readUint8Array = new Uint8Array();
|
||||||
|
|
||||||
reader.read().then(function processText({ done, value }) {
|
// Read from the stream
|
||||||
if (done) {
|
while (true) {
|
||||||
expect(readUint8Array).toEqual(inputUint8Array);
|
const { value, done } = await reader.read();
|
||||||
testDone.resolve(); // Correctly signal that the test is done.
|
if (done) break;
|
||||||
return;
|
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);
|
|
||||||
});
|
|
||||||
|
|
||||||
return testDone.promise; // Return the promise to properly wait for the test to complete.
|
expect(readUint8Array).toEqual(inputUint8Array);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
tap.test('WebDuplexStream should handle transform with a write function', async () => {
|
||||||
tap.test('should handle transform with a write function', async (toolsArg) => {
|
|
||||||
const testDone = toolsArg.defer();
|
|
||||||
const input = [1, 2, 3, 4, 5];
|
const input = [1, 2, 3, 4, 5];
|
||||||
const expectedOutput = [2, 4, 6, 8, 10];
|
const expectedOutput = [2, 4, 6, 8, 10];
|
||||||
|
|
||||||
const transformStream = new webstream.WebDuplexStream<number, number>({
|
const transformStream = new webstream.WebDuplexStream<number, number>({
|
||||||
writeFunction: (chunk, { push }) => {
|
writeFunction: async (chunk, { push }) => {
|
||||||
push(chunk * 2); // Push the doubled number into the stream
|
// Push the doubled number into the stream
|
||||||
return Promise.resolve(); // Resolve the promise immediately
|
push(chunk * 2);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -40,31 +40,28 @@ tap.test('should handle transform with a write function', async (toolsArg) => {
|
|||||||
|
|
||||||
const output: number[] = [];
|
const output: number[] = [];
|
||||||
|
|
||||||
// Process the text and resolve the test once done.
|
// Read from the stream asynchronously
|
||||||
const processText = async ({ done, value }) => {
|
const readPromise = (async () => {
|
||||||
if (done) {
|
while (true) {
|
||||||
expect(output).toEqual(expectedOutput);
|
const { value, done } = await readableStream.read();
|
||||||
testDone.resolve(); // Resolve the deferred test once all values have been read.
|
if (done) break;
|
||||||
return;
|
if (value !== undefined) {
|
||||||
|
output.push(value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (value !== undefined) {
|
})();
|
||||||
output.push(value);
|
|
||||||
}
|
|
||||||
// Continue reading and processing.
|
|
||||||
await readableStream.read().then(processText);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Start the read process before writing to the stream.
|
// Write to the stream
|
||||||
readableStream.read().then(processText);
|
|
||||||
|
|
||||||
// Sequentially write to the stream and close when done.
|
|
||||||
for (const num of input) {
|
for (const num of input) {
|
||||||
await writableStream.write(num);
|
await writableStream.write(num);
|
||||||
}
|
}
|
||||||
await writableStream.close();
|
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();
|
tap.start();
|
@ -3,6 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@push.rocks/smartstream',
|
name: '@push.rocks/smartstream',
|
||||||
version: '3.0.45',
|
version: '3.0.46',
|
||||||
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.'
|
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.'
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@push.rocks/smartstream',
|
name: '@push.rocks/smartstream',
|
||||||
version: '3.0.45',
|
version: '3.0.46',
|
||||||
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.'
|
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.'
|
||||||
}
|
}
|
||||||
|
@ -63,6 +63,7 @@ export class WebDuplexStream<TInput = any, TOutput = any> extends TransformStrea
|
|||||||
options: WebDuplexStreamOptions<TInput, TOutput>;
|
options: WebDuplexStreamOptions<TInput, TOutput>;
|
||||||
|
|
||||||
constructor(optionsArg: WebDuplexStreamOptions<TInput, TOutput>) {
|
constructor(optionsArg: WebDuplexStreamOptions<TInput, TOutput>) {
|
||||||
|
// here we call into the official web stream api
|
||||||
super({
|
super({
|
||||||
async transform(chunk, controller) {
|
async transform(chunk, controller) {
|
||||||
// Transformation logic remains unchanged
|
// Transformation logic remains unchanged
|
||||||
@ -72,15 +73,14 @@ export class WebDuplexStream<TInput = any, TOutput = any> extends TransformStrea
|
|||||||
push: (pushArg: TOutput) => controller.enqueue(pushArg),
|
push: (pushArg: TOutput) => controller.enqueue(pushArg),
|
||||||
};
|
};
|
||||||
|
|
||||||
optionsArg.writeFunction(chunk, tools)
|
try {
|
||||||
.then(writeReturnChunk => {
|
const writeReturnChunk = await optionsArg.writeFunction(chunk, tools);
|
||||||
// the write return chunk is optional
|
if (writeReturnChunk) { // return chunk is optional
|
||||||
// just in case the write function returns something other than void.
|
controller.enqueue(writeReturnChunk);
|
||||||
if (writeReturnChunk) {
|
}
|
||||||
controller.enqueue(writeReturnChunk);
|
} catch (err) {
|
||||||
}
|
controller.error(err);
|
||||||
})
|
}
|
||||||
.catch(err => controller.error(err));
|
|
||||||
} else {
|
} else {
|
||||||
controller.error(new Error('No write function provided'));
|
controller.error(new Error('No write function provided'));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user