-
Notifications
You must be signed in to change notification settings - Fork 20
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
jjmax75
wants to merge
19
commits into
master
Choose a base branch
from
buffersAndStreams
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
5a81c71
start of buffers and streams docs
jjmax75 55f4787
placeholders for content
jjmax75 2140ef6
overview of stream and buffer
jjmax75 d4b0abf
Update buffers.md
jjmax75 7602b1c
add content for buffers
jjmax75 7c91fbf
info on memory allocation for buffers
jjmax75 49cb957
start on streams, add buffer to cheatsheet
jjmax75 da32273
Merge branch 'master' into buffersAndStreams
jjmax75 af2e777
fix link
jjmax75 115e294
start of buffers and streams docs
jjmax75 17ad1be
placeholders for content
jjmax75 3cca7d0
overview of stream and buffer
jjmax75 b64643f
Update buffers.md
jjmax75 2602b7d
add content for buffers
jjmax75 a3888eb
info on memory allocation for buffers
jjmax75 3b36d1f
start on streams, add buffer to cheatsheet
jjmax75 2a57b37
fix link
jjmax75 2012ac5
Merge branch 'master' into buffersAndStreams
readikus 9e04eba
Merge branch 'buffersAndStreams' of https://github.com/jjmax75/openjs…
readikus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
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. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what are buffer's used for - a real world example answer the |
||
### 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. |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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');` |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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