From 5a81c714536b67fbddd2ab8c1f327ca618e3d55f Mon Sep 17 00:00:00 2001 From: John Behan Date: Sat, 9 Nov 2019 23:46:53 +0000 Subject: [PATCH 01/16] start of buffers and streams docs --- buffers-and-streams/buffers.md | 7 ++++ buffers-and-streams/connecting-streams.md | 1 - buffers-and-streams/incremental-processing.md | 1 - buffers-and-streams/index.md | 33 +++++++++++++++++-- buffers-and-streams/nodejs-buffer-apis.md | 1 - buffers-and-streams/streams.md | 7 ++++ buffers-and-streams/transforming-data.md | 1 - 7 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 buffers-and-streams/buffers.md delete mode 100644 buffers-and-streams/connecting-streams.md delete mode 100644 buffers-and-streams/incremental-processing.md delete mode 100644 buffers-and-streams/nodejs-buffer-apis.md create mode 100644 buffers-and-streams/streams.md delete mode 100644 buffers-and-streams/transforming-data.md diff --git a/buffers-and-streams/buffers.md b/buffers-and-streams/buffers.md new file mode 100644 index 0000000..912897a --- /dev/null +++ b/buffers-and-streams/buffers.md @@ -0,0 +1,7 @@ +--- +layout: default +title: Buffers in depth +url: buffers +--- + +## Buffers 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 f7ddfdc..8a6a0ae 100644 --- a/buffers-and-streams/index.md +++ b/buffers-and-streams/index.md @@ -1,6 +1,35 @@ --- -layout: default.njk +layout: default title: Buffers and Streams +url: buffers-and-streams --- -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 + +[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 + +A buffer is a... + +[Buffers in depth](./buffers.md) + +### Stream + +A Stream is a.... + +[Streams in depth](./streams.md) + +## Further Reading +MPJ intro to Streams - YouTube + +Net Ninja intro to Buffers and Streams - YouTube 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..2afcc9c --- /dev/null +++ b/buffers-and-streams/streams.md @@ -0,0 +1,7 @@ +--- +layout: default +title: Streams in depth +url: streams +--- + +## Streams 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 From 55f47877a8814d8a5ff9cbee86c8ba25b330bf00 Mon Sep 17 00:00:00 2001 From: John Behan Date: Sun, 10 Nov 2019 12:35:59 +0000 Subject: [PATCH 02/16] placeholders for content --- buffers-and-streams/index.md | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/buffers-and-streams/index.md b/buffers-and-streams/index.md index 8a6a0ae..f6165dc 100644 --- a/buffers-and-streams/index.md +++ b/buffers-and-streams/index.md @@ -8,14 +8,14 @@ The Node Application Developer Certification exam section for Buffers and Stream The topics that are covered include: - - Node.js Buffer API’s - - Incremental Processing - - Transforming Data - - Connecting Streams +- Node.js Buffer API’s +- Incremental Processing +- Transforming Data +- Connecting Streams ## Buffers and Streams Overview - -[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. + +The NodeJS 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 @@ -29,7 +29,18 @@ A Stream is a.... [Streams in depth](./streams.md) +## Working with Buffers and Streams + +### Examples + +### Details + +## Summary + +## Exercise + ## Further Reading + MPJ intro to Streams - YouTube Net Ninja intro to Buffers and Streams - YouTube From 2140ef6057908e34bb6b887a530c5b3453f31771 Mon Sep 17 00:00:00 2001 From: John Behan Date: Sun, 10 Nov 2019 13:19:30 +0000 Subject: [PATCH 03/16] overview of stream and buffer --- buffers-and-streams/buffers.md | 6 +++++- buffers-and-streams/index.md | 14 ++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/buffers-and-streams/buffers.md b/buffers-and-streams/buffers.md index 912897a..8b6317e 100644 --- a/buffers-and-streams/buffers.md +++ b/buffers-and-streams/buffers.md @@ -4,4 +4,8 @@ title: Buffers in depth url: buffers --- -## Buffers +## Buffers + +### Usage + +The NodeJS Buffer class is a global class and therefore does not need to be imported into a module. diff --git a/buffers-and-streams/index.md b/buffers-and-streams/index.md index f6165dc..79386de 100644 --- a/buffers-and-streams/index.md +++ b/buffers-and-streams/index.md @@ -15,17 +15,17 @@ The topics that are covered include: ## Buffers and Streams Overview -The NodeJS 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. +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 -A buffer is a... +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.md) ### Stream -A Stream is a.... +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.md) @@ -39,8 +39,10 @@ A Stream is a.... ## Exercise -## Further Reading +## Terminology -MPJ intro to Streams - YouTube +**Buffer** -Net Ninja intro to Buffers and Streams - YouTube +> 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) From d4b0abfff4f77afe7e405a065528c884a149386b Mon Sep 17 00:00:00 2001 From: John Behan Date: Wed, 13 Nov 2019 18:07:25 +0000 Subject: [PATCH 04/16] Update buffers.md --- buffers-and-streams/buffers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/buffers-and-streams/buffers.md b/buffers-and-streams/buffers.md index 8b6317e..0c1fcda 100644 --- a/buffers-and-streams/buffers.md +++ b/buffers-and-streams/buffers.md @@ -8,4 +8,4 @@ url: buffers ### Usage -The NodeJS Buffer class is a global class and therefore does not need to be imported into a module. +The Node `Buffer` class is a global class and therefore does not need to be imported into a module. From 7602b1c1daaa1c31f09086391e7a07438ea9b558 Mon Sep 17 00:00:00 2001 From: John Behan Date: Wed, 13 Nov 2019 21:09:03 +0000 Subject: [PATCH 05/16] add content for buffers --- buffers-and-streams/buffers.md | 51 +++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/buffers-and-streams/buffers.md b/buffers-and-streams/buffers.md index 0c1fcda..146ea20 100644 --- a/buffers-and-streams/buffers.md +++ b/buffers-and-streams/buffers.md @@ -6,6 +6,55 @@ url: buffers ## Buffers -### Usage +Node's `Buffer` class enables working with binary data in Javascript. A buffer is 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. + +### A simple 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()` + +> _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. + +``` +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); +// +``` \ No newline at end of file From 7c91fbf7bbe48fe117f726a599a545f63fdafaad Mon Sep 17 00:00:00 2001 From: John Behan Date: Thu, 14 Nov 2019 07:36:04 +0000 Subject: [PATCH 06/16] info on memory allocation for buffers --- buffers-and-streams/buffers.md | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/buffers-and-streams/buffers.md b/buffers-and-streams/buffers.md index 146ea20..6826fbc 100644 --- a/buffers-and-streams/buffers.md +++ b/buffers-and-streams/buffers.md @@ -42,6 +42,8 @@ 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. ``` @@ -57,4 +59,28 @@ console.log(myBuffer2); const myBuffer3 = Buffer.alloc(3, 'a'); console.log(myBuffer3); // -``` \ No newline at end of file +``` + +### 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. From 49cb957fd558411b16d5d1ca9f0d21c07b446703 Mon Sep 17 00:00:00 2001 From: John Behan Date: Fri, 15 Nov 2019 19:16:29 +0000 Subject: [PATCH 07/16] start on streams, add buffer to cheatsheet --- buffers-and-streams/buffers.md | 8 ++++++-- buffers-and-streams/index.md | 2 ++ buffers-and-streams/streams.md | 16 +++++++++++++++- cheatsheet/index.md | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 3 deletions(-) diff --git a/buffers-and-streams/buffers.md b/buffers-and-streams/buffers.md index 6826fbc..10099d5 100644 --- a/buffers-and-streams/buffers.md +++ b/buffers-and-streams/buffers.md @@ -2,17 +2,19 @@ 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 is allows a programme to store data temporarily outside the V8 engine's stack. +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. -### A simple example +### Example ```javascript // create a buffer from a string @@ -46,6 +48,8 @@ Buffers can also be instantiated by size using `Buffer.alloc()`, `Buffer.allocUn > _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); diff --git a/buffers-and-streams/index.md b/buffers-and-streams/index.md index 79386de..b989337 100644 --- a/buffers-and-streams/index.md +++ b/buffers-and-streams/index.md @@ -2,6 +2,8 @@ layout: default title: Buffers and Streams url: buffers-and-streams +author: john +date: 2019-11-14 --- The Node Application Developer Certification exam section for Buffers and Streams is worth 11% of the total and is broken into the following sections. diff --git a/buffers-and-streams/streams.md b/buffers-and-streams/streams.md index 2afcc9c..8ae6a1d 100644 --- a/buffers-and-streams/streams.md +++ b/buffers-and-streams/streams.md @@ -2,6 +2,20 @@ layout: default title: Streams in depth url: streams +author: john +date: 2019-11-15 --- -## Streams +## 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/cheatsheet/index.md b/cheatsheet/index.md index a3abf0f..ffd9c2f 100644 --- a/cheatsheet/index.md +++ b/cheatsheet/index.md @@ -3,6 +3,40 @@ layout: default.njk 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]) +``` + ## [Events](#events) Class: events.EventEmitter From af2e777df63eed7ccd7992c1e196dd3933eca8ad Mon Sep 17 00:00:00 2001 From: John Behan Date: Fri, 15 Nov 2019 19:25:13 +0000 Subject: [PATCH 08/16] fix link --- buffers-and-streams/index.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/buffers-and-streams/index.md b/buffers-and-streams/index.md index b989337..2a33f37 100644 --- a/buffers-and-streams/index.md +++ b/buffers-and-streams/index.md @@ -23,13 +23,13 @@ The Node.js APIS [Buffer](https://nodejs.org/docs/latest-v10.x/api/buffer.html) 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.md) +[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.md) +[Streams in depth](/streams) ## Working with Buffers and Streams From 115e294210051ca40f1e09389199fb9850885514 Mon Sep 17 00:00:00 2001 From: John Behan Date: Sat, 9 Nov 2019 23:46:53 +0000 Subject: [PATCH 09/16] start of buffers and streams docs --- buffers-and-streams/buffers.md | 7 +++++ buffers-and-streams/connecting-streams.md | 1 - buffers-and-streams/incremental-processing.md | 1 - buffers-and-streams/index.md | 31 ++++++++++++++++++- buffers-and-streams/nodejs-buffer-apis.md | 1 - buffers-and-streams/streams.md | 7 +++++ buffers-and-streams/transforming-data.md | 1 - 7 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 buffers-and-streams/buffers.md delete mode 100644 buffers-and-streams/connecting-streams.md delete mode 100644 buffers-and-streams/incremental-processing.md delete mode 100644 buffers-and-streams/nodejs-buffer-apis.md create mode 100644 buffers-and-streams/streams.md delete mode 100644 buffers-and-streams/transforming-data.md diff --git a/buffers-and-streams/buffers.md b/buffers-and-streams/buffers.md new file mode 100644 index 0000000..912897a --- /dev/null +++ b/buffers-and-streams/buffers.md @@ -0,0 +1,7 @@ +--- +layout: default +title: Buffers in depth +url: buffers +--- + +## Buffers 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..8a6a0ae 100644 --- a/buffers-and-streams/index.md +++ b/buffers-and-streams/index.md @@ -1,6 +1,35 @@ --- layout: default title: Buffers and Streams +url: buffers-and-streams --- -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 + +[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 + +A buffer is a... + +[Buffers in depth](./buffers.md) + +### Stream + +A Stream is a.... + +[Streams in depth](./streams.md) + +## Further Reading +MPJ intro to Streams - YouTube + +Net Ninja intro to Buffers and Streams - YouTube 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..2afcc9c --- /dev/null +++ b/buffers-and-streams/streams.md @@ -0,0 +1,7 @@ +--- +layout: default +title: Streams in depth +url: streams +--- + +## Streams 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 From 17ad1bea4e2e1db1dee41af727941aac2249e104 Mon Sep 17 00:00:00 2001 From: John Behan Date: Sun, 10 Nov 2019 12:35:59 +0000 Subject: [PATCH 10/16] placeholders for content --- buffers-and-streams/index.md | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/buffers-and-streams/index.md b/buffers-and-streams/index.md index 8a6a0ae..f6165dc 100644 --- a/buffers-and-streams/index.md +++ b/buffers-and-streams/index.md @@ -8,14 +8,14 @@ The Node Application Developer Certification exam section for Buffers and Stream The topics that are covered include: - - Node.js Buffer API’s - - Incremental Processing - - Transforming Data - - Connecting Streams +- Node.js Buffer API’s +- Incremental Processing +- Transforming Data +- Connecting Streams ## Buffers and Streams Overview - -[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. + +The NodeJS 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 @@ -29,7 +29,18 @@ A Stream is a.... [Streams in depth](./streams.md) +## Working with Buffers and Streams + +### Examples + +### Details + +## Summary + +## Exercise + ## Further Reading + MPJ intro to Streams - YouTube Net Ninja intro to Buffers and Streams - YouTube From 3cca7d0e2108bdfc80af9f57ce191592316552d6 Mon Sep 17 00:00:00 2001 From: John Behan Date: Sun, 10 Nov 2019 13:19:30 +0000 Subject: [PATCH 11/16] overview of stream and buffer --- buffers-and-streams/buffers.md | 6 +++++- buffers-and-streams/index.md | 14 ++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/buffers-and-streams/buffers.md b/buffers-and-streams/buffers.md index 912897a..8b6317e 100644 --- a/buffers-and-streams/buffers.md +++ b/buffers-and-streams/buffers.md @@ -4,4 +4,8 @@ title: Buffers in depth url: buffers --- -## Buffers +## Buffers + +### Usage + +The NodeJS Buffer class is a global class and therefore does not need to be imported into a module. diff --git a/buffers-and-streams/index.md b/buffers-and-streams/index.md index f6165dc..79386de 100644 --- a/buffers-and-streams/index.md +++ b/buffers-and-streams/index.md @@ -15,17 +15,17 @@ The topics that are covered include: ## Buffers and Streams Overview -The NodeJS 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. +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 -A buffer is a... +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.md) ### Stream -A Stream is a.... +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.md) @@ -39,8 +39,10 @@ A Stream is a.... ## Exercise -## Further Reading +## Terminology -MPJ intro to Streams - YouTube +**Buffer** -Net Ninja intro to Buffers and Streams - YouTube +> 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) From b64643fb4c028d5b09396501df31c7e3547bb700 Mon Sep 17 00:00:00 2001 From: John Behan Date: Wed, 13 Nov 2019 18:07:25 +0000 Subject: [PATCH 12/16] Update buffers.md --- buffers-and-streams/buffers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/buffers-and-streams/buffers.md b/buffers-and-streams/buffers.md index 8b6317e..0c1fcda 100644 --- a/buffers-and-streams/buffers.md +++ b/buffers-and-streams/buffers.md @@ -8,4 +8,4 @@ url: buffers ### Usage -The NodeJS Buffer class is a global class and therefore does not need to be imported into a module. +The Node `Buffer` class is a global class and therefore does not need to be imported into a module. From 2602b7d17d4e1f85dc81617fe399562f92a10884 Mon Sep 17 00:00:00 2001 From: John Behan Date: Wed, 13 Nov 2019 21:09:03 +0000 Subject: [PATCH 13/16] add content for buffers --- buffers-and-streams/buffers.md | 51 +++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/buffers-and-streams/buffers.md b/buffers-and-streams/buffers.md index 0c1fcda..146ea20 100644 --- a/buffers-and-streams/buffers.md +++ b/buffers-and-streams/buffers.md @@ -6,6 +6,55 @@ url: buffers ## Buffers -### Usage +Node's `Buffer` class enables working with binary data in Javascript. A buffer is 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. + +### A simple 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()` + +> _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. + +``` +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); +// +``` \ No newline at end of file From a3888ebde79e737997d87544cb6980068e67547f Mon Sep 17 00:00:00 2001 From: John Behan Date: Thu, 14 Nov 2019 07:36:04 +0000 Subject: [PATCH 14/16] info on memory allocation for buffers --- buffers-and-streams/buffers.md | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/buffers-and-streams/buffers.md b/buffers-and-streams/buffers.md index 146ea20..6826fbc 100644 --- a/buffers-and-streams/buffers.md +++ b/buffers-and-streams/buffers.md @@ -42,6 +42,8 @@ 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. ``` @@ -57,4 +59,28 @@ console.log(myBuffer2); const myBuffer3 = Buffer.alloc(3, 'a'); console.log(myBuffer3); // -``` \ No newline at end of file +``` + +### 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. From 3b36d1fedad1718e327febd64645da8d6069d337 Mon Sep 17 00:00:00 2001 From: John Behan Date: Fri, 15 Nov 2019 19:16:29 +0000 Subject: [PATCH 15/16] start on streams, add buffer to cheatsheet --- buffers-and-streams/buffers.md | 8 ++++++-- buffers-and-streams/index.md | 2 ++ buffers-and-streams/streams.md | 16 +++++++++++++++- cheatsheet/index.md | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 3 deletions(-) diff --git a/buffers-and-streams/buffers.md b/buffers-and-streams/buffers.md index 6826fbc..10099d5 100644 --- a/buffers-and-streams/buffers.md +++ b/buffers-and-streams/buffers.md @@ -2,17 +2,19 @@ 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 is allows a programme to store data temporarily outside the V8 engine's stack. +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. -### A simple example +### Example ```javascript // create a buffer from a string @@ -46,6 +48,8 @@ Buffers can also be instantiated by size using `Buffer.alloc()`, `Buffer.allocUn > _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); diff --git a/buffers-and-streams/index.md b/buffers-and-streams/index.md index 79386de..b989337 100644 --- a/buffers-and-streams/index.md +++ b/buffers-and-streams/index.md @@ -2,6 +2,8 @@ layout: default title: Buffers and Streams url: buffers-and-streams +author: john +date: 2019-11-14 --- The Node Application Developer Certification exam section for Buffers and Streams is worth 11% of the total and is broken into the following sections. diff --git a/buffers-and-streams/streams.md b/buffers-and-streams/streams.md index 2afcc9c..8ae6a1d 100644 --- a/buffers-and-streams/streams.md +++ b/buffers-and-streams/streams.md @@ -2,6 +2,20 @@ layout: default title: Streams in depth url: streams +author: john +date: 2019-11-15 --- -## Streams +## 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/cheatsheet/index.md b/cheatsheet/index.md index 86d45f7..a13dd58 100644 --- a/cheatsheet/index.md +++ b/cheatsheet/index.md @@ -48,6 +48,40 @@ async function callFoo() { } ``` +## [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) Class: events.EventEmitter From 2a57b37e2d23022a2c36097138527b7a1fce89b2 Mon Sep 17 00:00:00 2001 From: John Behan Date: Fri, 15 Nov 2019 19:25:13 +0000 Subject: [PATCH 16/16] fix link --- buffers-and-streams/index.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/buffers-and-streams/index.md b/buffers-and-streams/index.md index b989337..2a33f37 100644 --- a/buffers-and-streams/index.md +++ b/buffers-and-streams/index.md @@ -23,13 +23,13 @@ The Node.js APIS [Buffer](https://nodejs.org/docs/latest-v10.x/api/buffer.html) 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.md) +[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.md) +[Streams in depth](/streams) ## Working with Buffers and Streams