Pure JavaScript tools for buffer (such as concat)
Latest commit 10117c2 Nov 25, 2016 @coolaj86 committed on GitHub Update README.md


Daplie is Taking Back the Internet!

Stop serving the empire and join the rebel alliance!


Pure JavaScript utils which extend the global Buffer object in Node.JS.

npm install bufferjs


  • buffer.addChunk(chunk)
  • Buffer.indexOf(haystack, needle, startIndex)
  • buffer.indexOf(needle, startIndex)


Adds a buffer chunk to buffer.

Returns true when full

Returns new Buffer if buffer overflows


var buffer = new Buffer(9),

buffer.addChunk(new Buffer("abc"));
buffer.addChunk(new Buffer("def"));
buffer.toString(); // "abcdef???"
// Note: "???" is whatever random garbage was internally `malloc`d.    

console.log(true === buffer.addChunk(new Buffer("ghi"))); // true, buffer is full
// true when buffer is full

// adding past the index will throw an error
overflow = buffer.addChunk(new Buffer("jkl"));
buffer.toString(); // abcdefghi
overflow.toString(); // jkl

Buffer.indexOf(haystack, needle, startIndex)

Searches the given haystack Buffer for the given needle Buffer. Returns the index of the first occurence of needle, or -1 if the pattern was not found. needle may also be a regular String, for convenience. startIndex tells at which index to begin the search.


var haystack = new Buffer(10);
var needle = '\r\n';
haystack.write(needle, 5);

Buffer.indexOf(haystack, needle); // 5
Buffer.indexOf(haystack, 'NOT_IN_HAYSTACK'); // -1

buffer.indexOf(needle, startIndex)

OO-style variant of Buffer.indexOf. this is the haystack, and only needle needs to be supplied when called.


var haystack = new Buffer(10);
var needle = '\r\n';
haystack.write(needle, 5);

haystack.indexOf(needle); // 5
haystack.indexOf('NOT_IN_HAYSTACK'); // -1

Buffer.concat(list, [totalLength])

Deprecated: Node.JS 0.8.x has Buffer.concat built-in (compatible). See http://nodejs.org/api/buffer.html#buffer_class_method_buffer_concat_list_totallength

WARNING: v1.2.1 uses NON-NATIVE concat. v2.0.0 uses NATIVE concat ONLY.

Returns a new Buffer with the contents of all buffers in the Array.


var buffers = [
        new Buffer("abc")
      , new Buffer("def")
      , new Buffer("ghi")
  , buffer

buffer = Buffer.concat(buffers);
// abcdefghi