diff --git a/buffers-and-streams/buffers.md b/buffers-and-streams/buffers.md new file mode 100644 index 0000000..10099d5 --- /dev/null +++ b/buffers-and-streams/buffers.md @@ -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. + +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 + +```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 + +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); +// + +// 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 Octets which is a more accurate way of saying 'an 8-bit byte'. + +``` +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..2a33f37 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. +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. +> +> -- [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..8ae6a1d --- /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..5adae70 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,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)