The Stream class extends the TransformStream interface, providing additional convenience methods for handling streams, such as writing, closing, and aborting reducing the amount of code required for those operations.
If you're interested in a simple and effective way to read streams, take a look at @alessiofrittoli/stream-reader package.
Run the following command to start using stream-writer in your projects:
npm i @alessiofrittoli/stream-writeror using pnpm
pnpm i @alessiofrittoli/stream-writer- Inherits all the functionality of the
TransformStreamAPI, allowing for custom transformations of input to output streams. - Provides seamless integration with modern streaming APIs.
writeMethod: A high-level abstraction for writing chunks of data into the stream. It handles readiness and ensures proper sequencing of write operations.closeMethod: Safely closes the stream while preventing multiple or concurrent close operations.abortMethod: Gracefully aborts the stream with an optional reason, making it easier to handle errors or interruptions.
- The
headersproperty contains default headers commonly used in server responses, such as:Connection: keep-aliveTransfer-Encoding: chunkedCache-Control: no-cache, no-transformX-Accel-Buffering: noContent-Encoding: none
- The
writerproperty provides direct access to the underlyingWritableStreamDefaultWriter, enabling fine-grained control over the writable stream. - Ensures proper handling of stream readiness, errors, and resource management (e.g., releasing locks).
- Generic parameters (
Ifor input,Ofor output) make the class type-safe and adaptable to various use cases, such as processing specific data types.
- Internal mechanisms, ensure the class behaves predictably and avoids race conditions.
- Methods like
write,close, andabortreturn the currentStreaminstance, enabling method chaining for more concise and readable code.
- The
Streamclass can be used in both client-side and server-side applications where theTransformStreamAPI is supported. - Its modular design makes it easy to extend or customize further for specific application needs.
- Clear and concise API with thoughtful defaults.
- Built-in documentation and examples make it easy to understand and integrate into existing projects.
These features make the Stream class a versatile and developer-friendly abstraction for working with streams in modern JavaScript and TypeScript environments.
import { Stream } from '@alessiofrittoli/stream-writer'| Property | Type | Description |
|---|---|---|
writable |
WritableStream<I> |
The writable stream instance. |
readable |
ReadableStream<O> |
The readable stream instance. |
writer |
WritableStreamDefaultWriter<I> |
The writer instance for the writable stream. |
closed |
boolean |
Indicates whether the stream is closed. |
headers |
Headers |
Common headers to return in a server response. |
Constructs a new instance of the Stream class.
| Parameter | Type | Description |
|---|---|---|
transformer |
Transformer<I, O> |
(Optional) A custom transformer for the stream. |
writableStrategy |
QueuingStrategy<I> |
(Optional) A custom strategy for the writable stream. |
readableStrategy |
QueuingStrategy<O> |
(Optional) A custom strategy for the readable stream. |
Writes data into the stream.
| Parameter | Type | Description |
|---|---|---|
chunk |
I |
The data chunk to write to the stream. |
Type: Promise<Stream>
- A Promise that resolves to the current
Streaminstance.
Closes the stream.
- Closes the writer if it is not already closed or in the process of closing.
- Prevents multiple close operations when .close() is not awaited.
- Releases the lock on the writer.
Type: Promise<Stream>
- A Promise that resolves to the current
Streaminstance.
Aborts the stream with an optional reason.
| Parameter | Type | Description |
|---|---|---|
reason |
string |
(Optional) The reason for aborting the stream. |
Type: Promise<Stream>
- A Promise that resolves to the current
Streaminstance.
const routeHandler = () => {
const stream = new Stream()
const streamTask = async () => {
await stream.write( 'data' )
await stream.write( 'data 2' )
await stream.write( 'data 3' )
await stream.write( 'data 4' )
}
streamTask()
.then( () => stream.close() )
return new Response( stream.readable, { headers: stream.headers } )
}const routeHandler = () => {
const encoder = new TextEncoder()
const stream = (
new Stream<string, Uint8Array>( {
transform( chunk, controller )
{
controller.enqueue( encoder.encode( chunk ) )
}
} )
)
const streamTask = async () => {
await stream.write( 'data' )
await stream.write( 'data 2' )
await stream.write( 'data 3' )
await stream.write( 'data 4' )
}
streamTask()
.then( () => stream.close() )
return new Response( stream.readable, { headers: stream.headers } )
}const routeHandler = request => {
request.signal.addEventListener( 'abort', () => {
stream.abort( 'The user aborted the request.' )
} )
const stream = new Stream()
const streamTask = async () => {
await stream.write( 'data' )
await stream.write( 'data 2' )
await stream.write( 'data 3' )
await stream.write( 'data 4' )
}
streamTask()
.catch( error => {
if ( error.name === 'AbortError' ) {
return console.log( 'AbortError:', error.message )
}
await stream.write( error.message )
} )
.finally( () => stream.close() )
return new Response( stream.readable, { headers: stream.headers } )
}If you're interested in a simple and effective way to read streams, take a look at @alessiofrittoli/stream-reader package.
npm installor using pnpm
pnpm iRun the following command to test and build code for distribution.
pnpm buildwarnings / errors check.
pnpm lintRun all the defined test suites by running the following:
# Run tests and watch file changes.
pnpm test:watch
# Run tests in a CI environment.
pnpm test:ci- See
package.jsonfile scripts for more info.
Run tests with coverage.
An HTTP server is then started to serve coverage files from ./coverage folder.
test:coverage:serveContributions are truly welcome!
Please refer to the Contributing Doc for more information on how to start contributing to this project.
Help keep this project up to date with GitHub Sponsor.
If you believe you have found a security vulnerability, we encourage you to responsibly disclose this and NOT open a public issue. We will investigate all legitimate reports. Email security@alessiofrittoli.it to disclose any security vulnerabilities.
|
|
|