https://github.com/guilospanck/node-js-streams
Basic example using Node.js streams with TypeScript.
https://github.com/guilospanck/node-js-streams
nodejs nodejs-streams streams typescript
Last synced: 2 months ago
JSON representation
Basic example using Node.js streams with TypeScript.
- Host: GitHub
- URL: https://github.com/guilospanck/node-js-streams
- Owner: Guilospanck
- Created: 2022-03-12T13:24:34.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-03-01T09:21:09.000Z (over 2 years ago)
- Last Synced: 2025-02-12T12:55:48.542Z (4 months ago)
- Topics: nodejs, nodejs-streams, streams, typescript
- Language: TypeScript
- Homepage:
- Size: 106 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# NodeJS Streams
[](https://codecov.io/gh/Guilospanck/node-js-streams)Basic example using NodeJS Streams.
## Installation
Git clone this repository:
```bash
git clone https://github.com/Guilospanck/node-js-streams.git
```
### "Slow" test
Change directory into the repository:
```bash
cd node-js-streams/
```
Then run:
```bash
yarn
```
To install dependencies and then run:
```bash
yarn start:dev
```### Fast test
Be sure to have [Docker] and [Docker Compose] installed.Change directory into the repository:
```bash
cd node-js-streams/
```
Then run:
```bash
sudo docker-compose -f docker-compose.yml up -d --build
```
In order to view the logs once it's built, run:
```bash
sudo docker ps # get the id of the container
sudo docker logs [id]
```----
## Streams
Basically we have three main structures: `Readable`, `Transform` and `Writable`.### Readable
The Readable is the `source` of data. So, let's say we have a `client-server` architecture and we, the client, want to retrieve some information from the server.The Readable will be kept in the server and it will read and send the stream of data.
### Transform
The Transform is the `man in the middle`. It's responsible for making changes on the data. Everything you want to do with the stream of data, you can use the Transform to do that.### Writable
The Writable usually lies in the client and is responsive to finally do something with the data received.## Chunk and callbacks
- `Chunk` is the data coming from the stream. It is a buffer and if you want to get the data, just parse it using `JSON.parse(chunk);` or use `toString();`.
- `Callback` is used to inform the server (the readable stream) that the client received the data and it can send the other. If we don't pass it, it won't get the next data.--------
## TypeScript
Install TS dev dependencies:
```bash
yarn add -D typescript ts-node
```
Then generate a new `tsconfig.json` file:
```bash
npx tsc --init
```
Use the `tsconfig.json` that exists in this repository for a more complete version.Also, add a new file called `nodemon.json` in order to run Nodemon with TypeScript without getting an error.
--------
## Jest with TS
Install the Jest dev dependencies:
```bash
yarn add -D jest ts-jest @types/jest
```
Then generate a new `jest.config.js` (or `jest.config.cjs`) file:
```bash
npx ts-jest config:init
```[Docker]: https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-20-04
[Docker Compose]: https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-compose-on-ubuntu-20-04