|
| 1 | +# Streams & Buffers |
| 2 | + |
| 3 | +### 1) Streams |
| 4 | + |
| 5 | +#### Data Access in Node |
| 6 | +- Fully buffered access vs. partially buffered access - these differ in terms of: |
| 7 | + - How the data is exposed |
| 8 | + - The amount of memory used to store the data |
| 9 | + |
| 10 | +#### Fully Buffered Access |
| 11 | +- Asynchronous `readFile()` |
| 12 | +- Synchronous `readFileSync()` |
| 13 | +- The data is returned in **one big chunk** after all of it has been read |
| 14 | +- Node needs to allocate enough memory to store all of that data |
| 15 | + |
| 16 | +#### Partially Buffered Access |
| 17 | +- Asynchronous `read()` `createReadStream()` |
| 18 | +- Synchronous `readSync()` |
| 19 | +- Data input is treated as a **series of events**, occurring as the data is being read or written - this allows us to **access data as it is being read** |
| 20 | +- Partially-buffered methods (listed above) allow us to specify the size of the buffer and read the data in small chunks |
| 21 | + |
| 22 | +#### Streams |
| 23 | +- Streams are a form of **partially buffered access** |
| 24 | +- Streams return small pieces of data (using a buffer) and trigger an event when each new piece of data is available for processing |
| 25 | +- Since streams are partially buffered, we can process each piece of data **as soon as it is read** - this means we don't necessarily need to buffer / save the whole file into memory |
| 26 | +- The Node stream interface consists of two parts: |
| 27 | + - Readable streams |
| 28 | + - Writeable streams |
| 29 | + |
| 30 | +#### Readable Streams |
| 31 | +- Readable streams emit the following events: |
| 32 | + - `data` emits a buffer / string |
| 33 | + - `end` is emitted when the stream receives an EOF (End of Function) - this indicates that there will be no more `data` events |
| 34 | + - `error` is emitted if there's an error receiving data |
| 35 | +- To bind a callback to an event, use `stream.on(eventname, callback)` - the HTTP request object is one example of a readable stream that we've already covered: |
| 36 | + - `request.on('error', function(err) { ... });` |
| 37 | + - `request.on('data', function(data) { ... });` |
| 38 | + - `request.on('end', function() { ... });` |
| 39 | +- Readable streams also have the following methods: |
| 40 | + - `request.pause()` pauses incoming data events |
| 41 | + - `request.resume()` resumes after a `pause()` |
| 42 | + - `request.destroy()` stops the stream emitting any more events |
| 43 | + |
| 44 | +### 2) Buffers |
| 45 | +- In Node, buffers are a **high-performance alternative** to strings, representing raw C memory allocation |
| 46 | +- Buffers act like fixed-size arrays of integers, each represented as a **hexidecimal number** when run through `console.log()` |
| 47 | + |
| 48 | +```js |
| 49 | +var buffer = new Buffer(10); |
| 50 | +buffer[0] = 255; |
| 51 | +console.log(buffer); // <Buffer ff 00 00 00 00 4a 00 00 00 00> |
| 52 | +``` |
| 53 | +- Since buffers represent raw memory, any buffer content you haven't assigned (anything other than `buffer[0]` in the above example) will be whatever happens to sit in that position in memory (e.g. the `4a` seen above) |
| 54 | +- Compared to strings, buffers are fixed in size, and have very limited predefined functions (missing convenient ones like `String.replace()` for example) |
| 55 | +- If you want to use string functions on buffers, you can convert them to strings with `buffer.toString()` first |
| 56 | + |
| 57 | +## Resources |
| 58 | +- http://book.mixu.net/node/ch9.html |
| 59 | +- https://www.sitepoint.com/basics-node-js-streams/ |
| 60 | +- https://www.tutorialspoint.com/nodejs/nodejs_streams.htm |
0 commit comments