Skip to content

Buffers and streams ihr #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: buffersAndStreams
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions buffers-and-streams/buffers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
---
layout: default
title: Buffers in depth
url: buffers
author: john
date: 2019-11-14
---

## Buffers

Node's `Buffer` class enables working with binary data in JavaScript. A buffer allows a program to store data temporarily outside the V8 engine's stack.

Though not entirely accurate we can think of a buffer as an array of bytes, each byte is represented by a hexadecimal digit.

The Node `Buffer` class is a global class and therefore does not need to be imported into a module.

### Example

In this first example, it creates a buffer from a string and shows some of the basic manipulation this provides.

<div class="repl-code">

```javascript

// create a buffer from a string
const myBuffer = Buffer.from("hello node buffers");

console.log(myBuffer);
// <Buffer 68 65 6c 6c 6f 20 6e 6f 64 65 20 62 75 66 66 65 72 73>

console.log(myBuffer[0]);
// 104 - unicode char code for h (decimal representation of hex number 68)

console.log(myBuffer.toString());
// hello node buffers
```

</div>

## Instantiating a Buffer

The `Buffer.from()` methods allows instantiated `Buffers` with:

- a string
- an array
- another Buffer
- an arrayBuffer
- an object

Buffers can also be instantiated by size using `Buffer.alloc()`, `Buffer.allocUnsafe()` and `Buffer.allocUnsafeSlow()`. `alloc` allocates a new buffer of a given `size`, and fills it with zeros (or whatever is specified in the `fill` parameter. The `allocUnsafe` method allocates a given space in memory but does not initialise the values. The contents of the newly created block of memory are unknown, and they potentially _may contain sensitive data_.


### Example

<div class="repl-code">

```javascript

const myBuffer1 = Buffer.from([1, 2, 3]);
console.log(myBuffer1.length);
// 3

const myBuffer2 = Buffer.alloc(3);
console.log(myBuffer2);
// <Buffer 00 00 00>

// allocate a size for the buffer and give each byte an initial value 'a'
const myBuffer3 = Buffer.alloc(3, 'a');
console.log(myBuffer3);
// <Buffer 61 61 61>

```

</div>

### Caveat: Buffer size

Once instantiated, using either `from()` or one of the `alloc()` methods a Buffer cannot be resized.

A `Buffer's` size is measured in bytes (common 8-bit bytes, or "Octets")'.

<div class="repl-code">

```javascript

const myBuffer4 = Buffer.alloc(4);
console.log(myBuffer4);
// <Buffer 00 00 00 00>

myBuffer4.write('card');
console.log(myBuffer4);
// <Buffer 63 61 72 64>

myBuffer4.write('cards');
console.log(myBuffer4);
// <Buffer 63 61 72 64> - last character is lost

```

</div>

## Terminology

**Octet**

An Octet is a more accurate way to describe a byte consisting of 8-bits. In some systems a byte can have more or less bits.
1 change: 0 additions & 1 deletion buffers-and-streams/connecting-streams.md

This file was deleted.

1 change: 0 additions & 1 deletion buffers-and-streams/incremental-processing.md

This file was deleted.

46 changes: 45 additions & 1 deletion buffers-and-streams/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,50 @@
---
layout: default
title: Buffers and Streams
url: buffers-and-streams
author: john
date: 2019-11-14
---

I've seen things you people wouldn't believe. Attack ships on fire off the shoulder of Orion. I watched C-beams glitter in the dark near the Tannhäuser Gate. All those moments will be lost in time, like buffers and streams.
_Prerequisites: [events](../events)_

The Node Application Developer Certification exam section for Buffers and Streams is worth 11% of the total and is broken into the following sections:

- Node.js Buffer API’s
- Incremental Processing
- Transforming Data
- Connecting Streams

## Buffers and Streams Overview

The Node.js APIs [Buffer](https://nodejs.org/docs/latest-v10.x/api/buffer.html) and [Stream](https://nodejs.org/docs/latest-v10.x/api/stream.html) are fundamentally seperate topics yet are intrinsically linked. This section will cover how these APIs work individually and with each other.

### Buffer

Buffers are fixed size chunks of memory outside the V8 heap, managed by the Node.js `Buffer` class. These allow applications to work with binary data across networked streams (TCP) and reading or writing to the file system.

[Buffers in depth](./buffers)

### Stream

The Node.js Stream module provides an API for working with streaming data. Streams are a fundamental concept in, and are prevalent throughout applications built on, Node. Streams are an efficient way to handle sending and receiving data particularly for large or indeterminate amounts of data.

[Streams in depth](./streams)

## Working with Buffers and Streams

### Examples

### Details

## Summary

## Exercise

## Terminology

**Buffer**

> In computer science, a data buffer (or just buffer) is a region of a physical memory storage used to temporarily store data while it is being moved from one place to another.
>
> -- <cite>[Wikipedia](https://en.wikipedia.org/wiki/Data_buffer)</cite>
1 change: 0 additions & 1 deletion buffers-and-streams/nodejs-buffer-apis.md

This file was deleted.

21 changes: 21 additions & 0 deletions buffers-and-streams/streams.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
layout: default
title: Streams in depth
url: streams
author: john
date: 2019-11-15
---

## Streams

Node's `stream` module is an API for working with streaming data. Streams enable working with data as it is received rather than waiting for the data to be fully loaded into memory.

An example of streaming data is watching an online video broadcast. As the data is received the video can be displayed.

Streams are instances of [`EventEmitter`](/events).

## Working with streams

To use the `stream` module it must be imported -

`const stream = require('stream');`
1 change: 0 additions & 1 deletion buffers-and-streams/transforming-data.md

This file was deleted.

34 changes: 34 additions & 0 deletions cheatsheet/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,39 @@ layout: default
title: Node Cheat Sheet
---

## [Buffers](#buffers)

Class: Buffer

```
Buffer.from(array)
Buffer.from(arrayBuffer[, byteOffset[, length]])
Buffer.from(buffer)
Buffer.from(object[, offsetOrEncoding[, length]])
Buffer.from(string[, encoding])
Buffer.alloc(size[, fill[, encoding]])
Buffer.allocUnsafe(size)
Buffer.allocUnsafeSlow(size)
```

Instance: buf

```
buf.slice([start[, end]])
buf.compare(target[, targetStart[, targetEnd[, sourceStart[, sourceEnd]]]])
buf.copy(target[, targetStart[, sourceStart[, sourceEnd]]])
buf.entries()
buf.equals(otherBuffer)
buf.fill(value[, offset[, end]][, encoding])
buf.includes(value[, byteOffset][, encoding])
buf.indexOf(value[, byteOffset][, encoding])
buf.keys()
buf.lastIndexOf(value[, byteOffset][, encoding])
buf.length
buf.toString([encoding[, start[, end]]])
buf.values()
buf.write(string[, offset[, length]][, encoding])

## [Control Flow](#control-flow)

### Callback pattern
Expand Down Expand Up @@ -46,6 +79,7 @@ async function callFoo() {
throw err;
}
}

```

## [Events](#events)
Expand Down