From efe9034bdefe8f3348710e99b055c30bd8b7d0e3 Mon Sep 17 00:00:00 2001 From: Ian Read Date: Wed, 5 May 2021 08:00:03 +0100 Subject: [PATCH 1/3] WIP --- buffers-and-streams/buffers.md | 2 +- buffers-and-streams/index.md | 12 +++++------- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/buffers-and-streams/buffers.md b/buffers-and-streams/buffers.md index 10099d5..b68951d 100644 --- a/buffers-and-streams/buffers.md +++ b/buffers-and-streams/buffers.md @@ -8,7 +8,7 @@ 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. +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. diff --git a/buffers-and-streams/index.md b/buffers-and-streams/index.md index 2a33f37..0d9d6be 100644 --- a/buffers-and-streams/index.md +++ b/buffers-and-streams/index.md @@ -6,9 +6,7 @@ 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. - -The topics that are covered include: +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 @@ -17,19 +15,19 @@ The topics that are covered include: ## 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. +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 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) +[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) +[Streams in depth](./streams) ## Working with Buffers and Streams From ff276082ac2b297cebba39af87ab09b3cd21d52c Mon Sep 17 00:00:00 2001 From: Ian Read Date: Wed, 5 May 2021 08:32:08 +0100 Subject: [PATCH 2/3] Suggestions for buffer --- buffers-and-streams/buffers.md | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/buffers-and-streams/buffers.md b/buffers-and-streams/buffers.md index b68951d..0580c2a 100644 --- a/buffers-and-streams/buffers.md +++ b/buffers-and-streams/buffers.md @@ -16,7 +16,12 @@ The Node `Buffer` class is a global class and therefore does not need to be impo ### 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"); @@ -30,9 +35,11 @@ console.log(myBuffer.toString()); // hello node buffers ``` +
+ ## Instantiating a Buffer -Buffers can be instantiated with: +The `Buffer.from()` methods allows instantiated `Buffers` with: - a string - an array @@ -40,17 +47,15 @@ Buffers can be instantiated with: - an arrayBuffer - an object -using `Buffer.from()`. +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_. -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? +### Example -> _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 +```javascript -``` const myBuffer1 = Buffer.from([1, 2, 3]); console.log(myBuffer1.length); // 3 @@ -63,26 +68,37 @@ console.log(myBuffer2); 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'. +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** From a31037e57a492205ff184ce1c42d44c289d27d9f Mon Sep 17 00:00:00 2001 From: Ian Read Date: Wed, 5 May 2021 08:38:28 +0100 Subject: [PATCH 3/3] Updated event links --- buffers-and-streams/index.md | 2 ++ buffers-and-streams/streams.md | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/buffers-and-streams/index.md b/buffers-and-streams/index.md index 0d9d6be..9d97ae4 100644 --- a/buffers-and-streams/index.md +++ b/buffers-and-streams/index.md @@ -6,6 +6,8 @@ author: john date: 2019-11-14 --- +_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 diff --git a/buffers-and-streams/streams.md b/buffers-and-streams/streams.md index 8ae6a1d..a557464 100644 --- a/buffers-and-streams/streams.md +++ b/buffers-and-streams/streams.md @@ -12,7 +12,7 @@ Node's `stream` module is an API for working with streaming data. Streams enable 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). +Streams are instances of [`EventEmitter`](/events). ## Working with streams