feat(core): Add support for creating Web ReadableStream from a file

This commit is contained in:
Philipp Kunz 2024-10-13 13:49:13 +02:00
parent 9c30e5bab1
commit c8dc791c83
5 changed files with 46 additions and 2 deletions

View File

@ -1,5 +1,11 @@
# Changelog
## 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

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@push.rocks/smartstream',
version: '3.0.46',
version: '3.1.0',
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

@ -11,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,
}

View 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();
}
});
}

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@push.rocks/smartstream',
version: '3.0.46',
version: '3.1.0',
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.'
}