Home

Buffers

Buffers are included with node. Buffers are a spot in memory where data can live.

// using a node repl
// Buffer is a globally available reserved word!
Object.keys(Buffer)[
  ("poolSize",
  "from",
  "of",
  "alloc",
  "allocUnsafe",
  "allocUnsafeSlow",
  "isBuffer",
  "compare",
  "isEncoding",
  "concat",
  "byteLength")
]

Buffers Allocate Memory

Allocate is a fancy term for distribute.
Buffers get distributed.
A buffer is a piece of memory - ram.
Buffers are distributed with either "safe" or "unsafe" methods.
"unsafe" allocation runs the risk of being filled with memory "fragments" from data that had been used previously. These can contain bytes of data.
"safe" allocation uses "new" memory that has not been used previously.

Create A Buffer Using a Safe Allocation

const HOW_MANY_BYTES = 10
const a = Buffer.alloc(HOW_MANY_BYTES)
a
// <Buffer 00 00 00 00 00 00 00 00 00 00>

Create A Buffer Using an UnSafe Allocation

const HOW_MANY_BYTES = 12
const a = Buffer.allocUnsafe(HOW_MANY_BYTES)
a
// <Buffer 00 00 00 00 00 00 00 00 00 00 00 00>

Create A Buffer From A String

> const STR = "This is a six word string."
// undefined
> Buffer.from(STR)
<Buffer 54 68 69 73 20 69 73 20 61 20 73 69 78 20 77 6f 72 64 20 73 74 72 69 6e 67 >


// interesting views here...
"abcdefghijklmnop".split("").forEach(c => console.log(Buffer.from(c)))
<Buffer 61>
<Buffer 62>
<Buffer 63>
<Buffer 64>
<Buffer 65>
<Buffer 66>
<Buffer 67>
<Buffer 68>
<Buffer 69>
<Buffer 6a>
<Buffer 6b>
<Buffer 6c>
<Buffer 6d>
<Buffer 6e>
<Buffer 6f>
<Buffer 70>

"ABCDEFGHIJKLMNOP".split("").forEach(c => console.log(Buffer.from(c)))
<Buffer 41>
<Buffer 42>
<Buffer 43>
<Buffer 44>
<Buffer 45>
<Buffer 46>
<Buffer 47>
<Buffer 48>
<Buffer 49>
<Buffer 4a>
<Buffer 4b>
<Buffer 4c>
<Buffer 4d>
<Buffer 4e>
<Buffer 4f>
<Buffer 50>

Create Strings From Buffers

const str = "this is a string"
// 'this is a string'

const strBuffer = Buffer.from(str)
// undefined

strBuffer
// <Buffer 74 68 69 73 20 69 73 20 61 20 73 74 72 69 6e 67>

strBuffer.toString()
// 'this is a string'

const baseString = "this will be base64 encoded."
const bufferSixtyFour = Buffer.from(baseString, "base64")

bufferSixtyFour.toString("base64")

strBuffer.toString("hex")
// '74686973206973206120737472696e67'

strBuffer.toString("utf8")
// 'this is a string'

Above, note that buffers can take encodings via strings. The default encoding is utf8. This won't cover the other encodings in detail.

Buffers And JSON

Buffers and json can work together. Here a "full circle" takes a string into a buffer into a stringified object, BACK to a json object, BACK to a buffer, BACK to the string.

Buffer a string:

const s = "this is a string"
// 'this is a string'

const bufferedS = Buffer.from(s)
// <Buffer 74 68 69 73 20 69 73 20 61 20 73 74 72 69 6e 67>

Passing that buffer into JSON.stringify & using JSON.parse on the stringified buffer:

// stringifying a buffer
const jsonStringifiedBuffer = JSON.stringify(bufferedS)
// '{"type":"Buffer","data":[116,104,105,115,32,105,115,32,97,32,115,116,114,105,110,103]}'

// parsing a stringified buffer INTO an object
// the object has 2 keys: type, and data
const parsedJsonString = JSON.parse(jsonStringifiedBuffer)
// {
//   type: 'Buffer',
//   data: [
//     116, 104, 105, 115,  32,
//     105, 115,  32,  97,  32,
//     115, 116, 114, 105, 110,
//     103
//   ]
// }

Re-Building the buffer from the parsed stringified buffer

const almostThere = Buffer.from(parsedJsonString.data)
// <Buffer 74 68 69 73 20 69 73 20 61 20 73 74 72 69 6e 67>

Buffer.toString(almostThere)
// 'this is a string'

Encoding And Decoding Strings

Converting a string to a base46-encoded string and back to a string

const THE_STRING = "this is a string!"
// 'this is a string!'

// in two commands
const b64Buffd = Buffer.from(THE_STRING).toString("base64")
// 'dGhpcyBpcyBhIHN0cmluZyE='
const redone = Buffer.from(b64Buffd, "base64").toString()
// 'this is a string!'

// same thing, across several lines
let starting = "we've begun at the end"
// we've begun at the end
let bufferedStart = Buffer.from(starting)
// <Buffer 77 65 27 76 65 20 62 65 67 75 6e 20 61 74 20 74 68 65 20 65 6e 64>

let startInSixtyfour = bufferedStart.toString("base64")
// 'd2UndmUgYmVndW4gYXQgdGhlIGVuZA=='
let backToBuffer = Buffer.from(startInSixtyfour, "base64")
// <Buffer 77 65 27 76 65 20 62 65 67 75 6e 20 61 74 20 74 68 65 20 65 6e 64>
let backToStart = backToBuffer.toString()
// "we've begun at the end"
Tags: