diff --git a/buffers-and-streams/buffers.md b/buffers-and-streams/buffers.md new file mode 100644 index 0000000..0580c2a --- /dev/null +++ b/buffers-and-streams/buffers.md @@ -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. + +
+ +```javascript + +// create a buffer from a string +const myBuffer = Buffer.from("hello node buffers"); + +console.log(myBuffer); +// + +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 + +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 + +
+ +```javascript + +const myBuffer1 = Buffer.from([1, 2, 3]); +console.log(myBuffer1.length); +// 3 + +const myBuffer2 = Buffer.alloc(3); +console.log(myBuffer2); +// + +// allocate a size for the buffer and give each byte an initial value 'a' +const myBuffer3 = Buffer.alloc(3, 'a'); +console.log(myBuffer3); +// + +``` + +
+ +### 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")'. + +
+ +```javascript + +const myBuffer4 = Buffer.alloc(4); +console.log(myBuffer4); +// + +myBuffer4.write('card'); +console.log(myBuffer4); +// + +myBuffer4.write('cards'); +console.log(myBuffer4); +// - 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. diff --git a/buffers-and-streams/connecting-streams.md b/buffers-and-streams/connecting-streams.md deleted file mode 100644 index 05b4321..0000000 --- a/buffers-and-streams/connecting-streams.md +++ /dev/null @@ -1 +0,0 @@ -# Connecting Streams \ No newline at end of file diff --git a/buffers-and-streams/incremental-processing.md b/buffers-and-streams/incremental-processing.md deleted file mode 100644 index d245239..0000000 --- a/buffers-and-streams/incremental-processing.md +++ /dev/null @@ -1 +0,0 @@ -# Incremental Processing \ No newline at end of file diff --git a/buffers-and-streams/index.md b/buffers-and-streams/index.md index 87653f3..9d97ae4 100644 --- a/buffers-and-streams/index.md +++ b/buffers-and-streams/index.md @@ -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. +> +> -- [Wikipedia](https://en.wikipedia.org/wiki/Data_buffer) diff --git a/buffers-and-streams/nodejs-buffer-apis.md b/buffers-and-streams/nodejs-buffer-apis.md deleted file mode 100644 index e0f1295..0000000 --- a/buffers-and-streams/nodejs-buffer-apis.md +++ /dev/null @@ -1 +0,0 @@ -# Node.js Buffer APIs \ No newline at end of file diff --git a/buffers-and-streams/streams.md b/buffers-and-streams/streams.md new file mode 100644 index 0000000..a557464 --- /dev/null +++ b/buffers-and-streams/streams.md @@ -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');` diff --git a/buffers-and-streams/transforming-data.md b/buffers-and-streams/transforming-data.md deleted file mode 100644 index 47cffeb..0000000 --- a/buffers-and-streams/transforming-data.md +++ /dev/null @@ -1 +0,0 @@ -# Transforming Data \ No newline at end of file diff --git a/cheatsheet/index.md b/cheatsheet/index.md index 86d45f7..51f7fad 100644 --- a/cheatsheet/index.md +++ b/cheatsheet/index.md @@ -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 @@ -46,6 +79,7 @@ async function callFoo() { throw err; } } + ``` ## [Events](#events)