@push.rocks/webstream
Version:
A cross-platform streaming package designed for node and web environments.
101 lines (65 loc) • 4.76 kB
Markdown
# /webstream
a cross platform streaming package
## Install
To install ` .rocks/webstream`, use npm (or yarn, or pnpm):
```bash
npm install .rocks/webstream
# or
yarn add .rocks/webstream
# or
pnpm add .rocks/webstream
```
## Usage
The ` .rocks/webstream` package provides a cross-platform streaming solution leveraging Readable and Writable streams found in modern web and server environments. This package takes advantage of TypeScript for type safety and enhanced development experience.
### Basic Setup
First, ensure you have the package installed. Then you can import and use it within your project:
```typescript
import * as webstream from '@push.rocks/webstream';
```
### Converting Node.js Duplex Streams to Web Streams
One of the core functionalities of ` .rocks/webstream` is the ability to convert Node.js Duplex streams into the Web Streams API's ReadableStream and WritableStream. This allows you to use Node.js streams in environments that support the Web Streams API, such as modern browsers.
```typescript
import { convertDuplexToWebStream } from '@push.rocks/webstream';
// Assuming 'someDuplexStream' is an existing instance of a Node.js Duplex stream
const { readable, writable } = convertDuplexToWebStream(someDuplexStream);
// 'readable' is a Web Streams API ReadableStream
// 'writable' is a Web Streams API WritableStream
```
### Creating a WebDuplexStream
The `WebDuplexStream<TInput, TOutput>` class allows you to create a custom duplex stream that follows the Web Streams standard. It supports transformation operations, making it useful for creating pipelines.
```typescript
import { WebDuplexStream } from '@push.rocks/webstream';
const transformStream = new WebDuplexStream<number, string>({
writeFunction: async (chunk, { push }) => {
push(`Number: ${chunk}`);
},
});
const writer = transformStream.writable.getWriter();
writer.write(1);
writer.write(2);
writer.close();
const reader = transformStream.readable.getReader();
(async () => {
while (true) {
const { done, value } = await reader.read();
if (done) break;
console.log(value); // Outputs: 'Number: 1', 'Number: 2'
}
})();
```
This example creates a `WebDuplexStream` that converts incoming numbers into strings prefixed with "Number: ". The stream's `writeFunction` is called for each chunk of the stream's input, allowing transformation logic to be applied.
### Conclusion
` .rocks/webstream` makes cross-platform streaming consistent and straightforward, bridging the gap between Node.js streams and Web Streams. It offers a seamless way to work with streaming data, ensuring compatibility and performance across different environments.
With TypeScript support, developing with ` .rocks/webstream` becomes more manageable, offering compile-time type checks and intelligent code completion, aiding in quicker development cycles and reducing runtime errors.
The conversion utilities and customizable `WebDuplexStream` class offer a flexible solution to deal with streaming data, allowing for a wide range of applications such as data transformation, file processing, or communication channels within your applications.
For more advanced use cases and detailed documentation, refer to the package's [TypeDoc documentation](https://push.rocks.gitlab.io/webstream/).
## License and Legal Information
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
### Trademarks
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
### Company Information
Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany
For any legal inquiries or if you require further information, please contact us via email at hello .vc.
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
.rocks