diff --git a/changelog.md b/changelog.md index b21184f..1f0e2f2 100644 --- a/changelog.md +++ b/changelog.md @@ -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 diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 562916a..2853522 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -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.' } diff --git a/ts/index.ts b/ts/index.ts index a740308..534fadb 100644 --- a/ts/index.ts +++ b/ts/index.ts @@ -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, +} diff --git a/ts/smartstream.nodewebhelpers.ts b/ts/smartstream.nodewebhelpers.ts new file mode 100644 index 0000000..ff60737 --- /dev/null +++ b/ts/smartstream.nodewebhelpers.ts @@ -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 { + 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(); + } + }); +} \ No newline at end of file diff --git a/ts_web/00_commitinfo_data.ts b/ts_web/00_commitinfo_data.ts index 562916a..2853522 100644 --- a/ts_web/00_commitinfo_data.ts +++ b/ts_web/00_commitinfo_data.ts @@ -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.' }