feat(core): Add support for creating Web ReadableStream from a file
This commit is contained in:
parent
9c30e5bab1
commit
c8dc791c83
@ -1,5 +1,11 @@
|
|||||||
# Changelog
|
# 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)
|
## 2024-10-13 - 3.0.46 - fix(WebDuplexStream)
|
||||||
Fix errors in WebDuplexStream transformation and test logic
|
Fix errors in WebDuplexStream transformation and test logic
|
||||||
|
|
||||||
|
@ -3,6 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@push.rocks/smartstream',
|
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.'
|
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.'
|
||||||
}
|
}
|
||||||
|
@ -11,3 +11,7 @@ export * from './smartstream.functions.js';
|
|||||||
|
|
||||||
import * as plugins from './smartstream.plugins.js';
|
import * as plugins from './smartstream.plugins.js';
|
||||||
export const webstream = plugins.webstream;
|
export const webstream = plugins.webstream;
|
||||||
|
import * as nodewebhelpers from './smartstream.nodewebhelpers.js';
|
||||||
|
export {
|
||||||
|
nodewebhelpers,
|
||||||
|
}
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
@ -3,6 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@push.rocks/smartstream',
|
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.'
|
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.'
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user