# Streams

Pour simplifier l'implémentation asynchrone du traitement d'un flux de données, Node.js propose l'utilisation de la notion de **stream**.

NodeJS définit des interfaces d'implémentation de **stream** : Readable, Writable, Duplex et Transform.

<https://nodejs.org/api/stream.html#stream_api_for_stream_implementors>

{% embed url="<https://nodejs.org/api/stream.html#stream_api_for_stream_implementors>" %}

## NodeJS implémente quelques streams et fonctionnalités de transformation

```javascript
const crypto = require('crypto');
const fs = require('fs');
const zlib = require('zlib');

const password = new Buffer(process.env.PASS || 'password');
const encryptStream = crypto.createCipher('aes-256-cbc', password);

const gzip = zlib.createGzip();
const readStream = fs.createReadStream('secret-data.txt');
const writeStream = fs.createWriteStream('encrypted-secret-data.gz');

/* Read current file. */
readStream

    /* Encrypt data. */
    .pipe(encryptStream)

    /* Compress data. */
    .pipe(gzip)

    /* Write data to output file. */
    .pipe(writeStream)

    /* We are done. */
    .on('finish', function () {
        console.log('done');
    });
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://guide-node-js.wishtack.io/node-js/streams.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
