• Stars
    star
    205
  • Rank 191,264 (Top 4 %)
  • Language
    C++
  • License
    ISC License
  • Created about 14 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

working with node.js buffers made easy

node-buffertools

Utilities for manipulating buffers.

**Deprecation warning: this module is no longer maintained and doesn't work with newer versions of Node.js.

Most functionality is now available through the native Buffer class.**

Installing the module

Easy! With npm:

npm install buffertools

From source:

node-gyp configure
node-gyp build

Now you can include the module in your project.

require('buffertools').extend();  // extend Buffer.prototype
var buf = new Buffer(42);         // create a 42 byte buffer
buf.clear();                      // clear it!

If you don't want to extend the Buffer class's prototype (recommended):

var buffertools = require('buffertools');
var buf = new Buffer(42);
buffertools.clear(buf);

Methods

Note that most methods that take a buffer as an argument, will also accept a string.

buffertools.extend([object], [object...])

Extend the arguments with the buffertools methods. If called without arguments, defaults to [Buffer.prototype, SlowBuffer.prototype]. Extending prototypes only makes sense for classes that derive from Buffer.

buffertools v1.x extended the Buffer prototype by default. In v2.x, it is opt-in. The reason for that is that buffertools was originally developed for node.js v0.3 (or maybe v0.2, I don't remember exactly when buffers were added) where the Buffer class was devoid of any useful methods. Over the years, it has grown a number of utility methods, some of which conflict with the buffertools methods of the same name, like Buffer#fill().

Buffer#clear()

buffertools.clear(buffer)

Clear the buffer. This is equivalent to Buffer#fill(0). Returns the buffer object so you can chain method calls.

Buffer#compare(buffer|string)

buffertools.compare(buffer, buffer|string)

Lexicographically compare two buffers. Returns a number less than zero if a < b, zero if a == b or greater than zero if a > b.

Buffers are considered equal when they are of the same length and contain the same binary data.

Smaller buffers are considered to be less than larger ones. Some buffers find this hurtful.

Buffer#concat(a, b, c, ...)

buffertools.concat(a, b, c, ...)

Concatenate two or more buffers/strings and return the result. Example:

// identical to new Buffer('foobarbaz')
a = new Buffer('foo');
b = new Buffer('bar');
c = a.concat(b, 'baz');
console.log(a, b, c); // "foo bar foobarbaz"

// static variant
buffertools.concat('foo', new Buffer('bar'), 'baz');

Buffer#equals(buffer|string)

buffertools.equals(buffer, buffer|string)

Returns true if this buffer equals the argument, false otherwise.

Buffers are considered equal when they are of the same length and contain the same binary data.

Caveat emptor: If your buffers contain strings with different character encodings, they will most likely not be equal.

Buffer#fill(integer|string|buffer)

buffertools.fill(buffer, integer|string|buffer)

Fill the buffer (repeatedly if necessary) with the argument. Returns the buffer object so you can chain method calls.

Buffer#fromHex()

buffertools.fromHex(buffer)

Assumes this buffer contains hexadecimal data (packed, no whitespace) and decodes it into binary data. Returns a new buffer with the decoded content. Throws an exception if non-hexadecimal data is encountered.

Buffer#indexOf(buffer|string, [start=0])

buffertools.indexOf(buffer, buffer|string, [start=0])

Search this buffer for the first occurrence of the argument, starting at offset start. Returns the zero-based index or -1 if there is no match.

Buffer#reverse()

buffertools.reverse(buffer)

Reverse the content of the buffer in place. Example:

b = new Buffer('live');
b.reverse();
console.log(b); // "evil"

Buffer#toHex()

buffertools.toHex(buffer)

Returns the contents of this buffer encoded as a hexadecimal string.

Classes

Singular, actually. To wit:

WritableBufferStream

This is a regular node.js writable stream that accumulates the data it receives into a buffer.

Example usage:

// slurp stdin into a buffer
process.stdin.resume();
ostream = new WritableBufferStream();
util.pump(process.stdin, ostream);
console.log(ostream.getBuffer());

The stream never emits 'error' or 'drain' events.

WritableBufferStream.getBuffer()

Return the data accumulated so far as a buffer.

TODO

  • Logical operations on buffers (AND, OR, XOR).
  • Add lastIndexOf() functions.

License

Copyright (c) 2010, Ben Noordhuis [email protected]

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

More Repositories

1

node-heapdump

Make a dump of the V8 heap for later inspection.
JavaScript
2,487
star
2

node-iconv

node.js iconv bindings - text recoding for fun and profit!
JavaScript
795
star
3

node-profiler

Access the V8 profiler from node.js
JavaScript
283
star
4

v8-cmake

The V8 JavaScript engine, but built with CMake instead of GN - WIP
C++
185
star
5

ragel

Ragel State Machine Compiler - http://www.complang.org/ragel/
C++
156
star
6

node-event-emitter

shows how to emit events from C++ land
JavaScript
80
star
7

node-unix-dgram

unix datagram support for node.js
C++
77
star
8

gyp

GYP can Generate Your Projects.
Python
72
star
9

quickjit

quickjs meets jit
C
60
star
10

node-mmap

mmap(2) bindings for node.js
C++
54
star
11

node-idle-gc

Run the V8 GC when node.js is idle.
C++
42
star
12

v8.rs

V8 bindings for Rust
C++
39
star
13

punycode

Punycode encoder/decoder
C
37
star
14

libuv-chat

a chat app done the hard way
JavaScript
32
star
15

phode

we put the async in php
C
30
star
16

uriparser2

Your one-stop C and C++ library for URI parsing.
C
30
star
17

bspc

Quake 3 BSP-to-AAS compiler
C
28
star
18

tkgate

A graphical editor and event-driven simulator for digital circuits
C
25
star
19

strace

http://sourceforge.net/projects/strace/
C
24
star
20

ngx_http_auth_cas_module

CAS client for nginx
C
24
star
21

node-weakref

weak references in node.js
C++
17
star
22

netif

List available network interfaces
Rust
13
star
23

python-ntlm

Python library that provides NTLM support, including an authentication handler for urllib2
Python
13
star
24

rust-epoll

low-level epoll bindings for rust
Rust
13
star
25

epoll-bench

C
12
star
26

node-wgpu

WebGPU for Node.js
Rust
12
star
27

libiconv

C
12
star
28

mongrel2

mongrel2 + dependencies = your one-stop mongrel2 build
C
12
star
29

termios

Pretty-print the contents of `struct termios`
C
10
star
30

lua-uv

lua + libuv = sweet async goodness
C
10
star
31

node-fastcgi

FastCGI support for node.js
C++
9
star
32

cracknode

stop gacking, start cracking
C
9
star
33

Infinite-Adaptive-Mario

Maven-ized version of Infinite Adaptive Mario.
Java
9
star
34

node-backtrace

Prints a C++ and JS backtrace on SIGABRT.
C++
8
star
35

vime

vime, a faster vm module
JavaScript
8
star
36

node-native-certs

Load TLS root certificates from the system trust store
JavaScript
7
star
37

node-curl

cURL bindings for node.js
C++
6
star
38

rust-errno

access errno from your rust code
Rust
6
star
39

marnix

MARNIX, a UNIX clone
C
6
star
40

smjs

gyp-ified spidermonkey, WIP
JavaScript
6
star
41

chicken-core

http://call-cc.org/
Scheme
5
star
42

amazing-graceful-fs

Like graceful-fs, but without eval() hacks and polyfills.
JavaScript
4
star
43

httpd-accfilt

kernel-mode http accept filter benchmark
C
4
star
44

mod_git

serve content straight from your git repositories
C
4
star
45

node-roughtime

Roughtime client for Node.js. Roughtime provides secure time synchronisation.
JavaScript
4
star
46

libsm

A fast string matcher library.
C
4
star
47

axis2-c

Apache Axis2/C is a Web services engine implemented in the C programming language.
C
4
star
48

suv

scheme + libuv - what's not to like?
Python
4
star
49

node-http-parser

C++
3
star
50

fth

a not-quite-Forth-to-LLVM-bitcode compiler
Python
3
star
51

entityplus

just another quake 3 mod
C
3
star
52

dyper

Rust
3
star
53

bnoordhuis.github.com

GitHub Pages
3
star
54

random-bigint

Generate cryptographically strong pseudo-random BigInts
JavaScript
3
star
55

chamfilter

block China and other South Asian countries at the firewall level
Shell
3
star
56

qo

quickjs + golang
C
2
star
57

faio

Fast asynchronous I/O
C
2
star
58

jove

JΓ–VE is a framework for making 2D games in JavaScript
Rust
2
star
59

gyp-bug

showcase gyp bug
C
2
star
60

mod_modlet

Hassle-free module authoring for Apache 2
C
2
star
61

node-bursar

Generate RSA keys as PKCS#1, PKCS#8 or BER
JavaScript
2
star
62

node-rusage

getrusage(2) bindings
C++
2
star
63

brr

Nothing special, just going fast
C
2
star
64

hpv

hyper + polloi + v8
Rust
2
star
65

node-permute

A tiny library to permutate a sequence.
JavaScript
2
star
66

json-schema-validator

Java
1
star
67

golang-quickjs-serde

Go
1
star