Skip to content

start of buffers and streams docs #16

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

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
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
90 changes: 90 additions & 0 deletions buffers-and-streams/buffers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
---
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 programme to store data temporarily outside the V8 engine's stack.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?
This needs to be a lot simpler


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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what are buffer's used for - a real world example

answer the so what? question

### Example

```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
```

## Instantiating a Buffer

Buffers can be instantiated with:

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

using `Buffer.from()`.

Buffers can also be instantiated by size using `Buffer.alloc()`, `Buffer.allocUnsafe()` and `Buffer.allocUnsafeSlow()`

// The next line should be an 'aside', how to do this in markdown and the rendered site?

> _Unsafe_ as the memory containing the buffer is not initialised - i.e. not zeroed-out, so the potential exists for sensitive data to be leaked.

### Example

```
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>
```

### 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 Octets which is a more accurate way of saying 'an 8-bit byte'.

```
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
```

## 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.
The Node Application Developer Certification exam section for Buffers and Streams is worth 11% of the total and is broken into the following sections.

The topics that are covered include:

- 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

The Node.js Buffer class implements buffers which are fixed size chunks of memory outside the V8 heap. Buffers are used to deal with binary data when dealing with TCP streams and reading/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.

68 changes: 68 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,41 @@ async function callFoo() {
throw err;
}
}

```

## [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])
```

## [Events](#events)
Expand Down