Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 7 commits
  • 8 files changed
  • 0 commit comments
  • 1 contributor
View
172 README.markdown
@@ -1,4 +1,4 @@
-Binary
+binary
======
Unpack multibyte binary values from buffers and streams.
@@ -10,50 +10,70 @@ runs on pre-allocated buffers instead of a linked list.
[![build status](https://secure.travis-ci.org/substack/node-binary.png)](http://travis-ci.org/substack/node-binary)
-Examples
+examples
========
+stream.js
+---------
+
+``` js
+var binary = require('binary');
+
+var ws = binary()
+ .word32lu('x')
+ .word16bs('y')
+ .word16bu('z')
+ .tap(function (vars) {
+ console.dir(vars);
+ })
+;
+process.stdin.pipe(ws);
+process.stdin.resume();
+```
+
+output:
+
+```
+$ node examples/stream.js
+abcdefgh
+{ x: 1684234849, y: 25958, z: 26472 }
+^D
+```
+
parse.js
--------
- var buf = new Buffer([ 97, 98, 99, 100, 101, 102, 0 ]);
-
- var Binary = require('binary');
- var vars = Binary.parse(buf)
- .word16ls('ab')
- .word32bu('cf')
- .word8('x')
- .vars
- ;
- console.dir(vars);
--
- $ node parse.js
- { ab: 25185, cf: 1667523942, x: 0 }
+``` js
+var buf = new Buffer([ 97, 98, 99, 100, 101, 102, 0 ]);
-stream.js
----------
+var binary = require('binary');
+var vars = binary.parse(buf)
+ .word16ls('ab')
+ .word32bu('cf')
+ .word8('x')
+ .vars
+;
+console.dir(vars);
+```
+
+output:
- var Binary = require('binary');
- var stdin = process.openStdin();
-
- Binary.stream(stdin)
- .word32lu('x')
- .word16bs('y')
- .word16bu('z')
- .tap(function (vars) {
- console.dir(vars);
- })
- ;
--
- $ node examples/stream.js
- abcdefgh
- { x: 1684234849, y: 25958, z: 26472 }
- ^D
-
-Methods
+```
+{ ab: 25185, cf: 1667523942, x: 0 }
+```
+
+methods
=======
-Binary.parse(buf)
+`var binary = require('binary')`
+
+var b = binary()
+----------------
+
+Return a new writable stream `b` that has the chainable methods documented below
+for buffering binary input.
+
+binary.parse(buf)
-----------------
Parse a static buffer in one pass. Returns a chainable interface with the
@@ -64,25 +84,8 @@ In parse mode, methods will set their keys to `null` if the buffer isn't big
enough except `buffer()` and `scan()` which read up up to the end of the buffer
and stop.
-Binary.stream(emitter, eventName='data')
-----------------------------------------
-
-Parse a stream of buffer events from `eventName`. Each action will only execute
-once enough data has arrived from the event emitter.
-
-Binary(bufOrEm, eventName)
---------------------------
-
-If `bufOrEm` is a Buffer, returns `Binary.parse(bufOrEm)`, else returns
-`Binary.stream(bufOrEm, eventName)`
-
-Binary.put()
-------------
-
-Returns a new [put object](http://github.com/substack/node-put).
-
-word{8,16,32,64}{l,b}{e,u,s}(key)
-----------------------------------
+b.word{8,16,32,64}{l,b}{e,u,s}(key)
+-----------------------------------
Parse bytes in the buffer or stream given:
@@ -99,16 +102,16 @@ values don't exist they will be created automatically, so for instance you can
assign into `dst.addr` and `dst.port` and the `dst` key in the variable stash
will be `{ addr : x, port : y }` afterwards.
-buffer(key, size)
------------------
+b.buffer(key, size)
+-------------------
Take `size` bytes directly off the buffer stream, putting the resulting buffer
slice in the variable stash at `key`. If `size` is a string, use the value at
`vars[size]`. The key follows the same dotted address rules as the word
functions.
-scan(key, buffer)
------------------
+b.scan(key, buffer)
+-------------------
Search for `buffer` in the stream and store all the intervening data in the
stash at at `key`, excluding the search buffer. If `buffer` passed as a string,
@@ -116,52 +119,59 @@ it will be converted into a Buffer internally.
For example, to read in a line you can just do:
- Binary(em)
- .scan('line', new Buffer('\r\n'))
- .tap(function (vars) {
- console.log(vars.line)
- })
- ;
-
-tap(cb)
--------
+``` js
+var b = binary()
+ .scan('line', new Buffer('\r\n'))
+ .tap(function (vars) {
+ console.log(vars.line)
+ })
+;
+stream.pipe(b);
+```
+
+b.tap(cb)
+---------
The callback `cb` is provided with the variable stash from all the previous
actions once they've all finished.
You can nest additional actions onto `this` inside the callback.
-into(key, cb)
--------------
+b.into(key, cb)
+---------------
Like `.tap()`, except all nested actions will assign into a `key` in the `vars`
stash.
-loop(cb)
---------
+b.loop(cb)
+----------
Loop, each time calling `cb(end, vars)` for function `end` and the variable
stash with `this` set to a new chain for nested parsing. The loop terminates
once `end` is called.
-flush()
--------
+b.flush()
+---------
Clear the variable stash entirely.
-Installation
+installation
============
To install with [npm](http://github.com/isaacs/npm):
-
- npm install binary
-To run the tests with [expresso](http://github.com/visionmedia/expresso):
+```
+npm install binary
+```
- expresso
-
-Notes
+notes
=====
The word64 functions will only return approximations since javascript uses ieee
floating point for all number types. Mind the loss of precision.
+
+license
+=======
+
+MIT
+
View
4 examples/buf.js → example/buf.js
@@ -1,7 +1,7 @@
var buf = new Buffer([ 97, 98, 99, 100, 101, 102, 0 ]);
-var Binary = require('binary');
-Binary(buf)
+var binary = require('binary');
+binary(buf)
.word16ls('ab')
.word32bu('cf')
.word8('x')
View
4 examples/parse.js → example/parse.js
@@ -1,7 +1,7 @@
var buf = new Buffer([ 97, 98, 99, 100, 101, 102, 0 ]);
-var Binary = require('binary');
-var vars = Binary.parse(buf)
+var binary = require('binary');
+var vars = binary.parse(buf)
.word16ls('ab')
.word32bu('cf')
.word8('x')
View
7 examples/stream.js → example/stream.js
@@ -1,7 +1,6 @@
-var Binary = require('binary');
-var stdin = process.openStdin();
+var binary = require('binary');
-Binary.stream(stdin)
+var ws = binary()
.word32lu('x')
.word16bs('y')
.word16bu('z')
@@ -9,3 +8,5 @@ Binary.stream(stdin)
console.dir(vars);
})
;
+process.stdin.pipe(ws);
+process.stdin.resume();
View
62 index.js
@@ -2,20 +2,31 @@ var Chainsaw = require('chainsaw');
var EventEmitter = require('events').EventEmitter;
var Buffers = require('buffers');
var Vars = require('./lib/vars.js');
+var Stream = require('stream').Stream;
exports = module.exports = function (bufOrEm, eventName) {
if (Buffer.isBuffer(bufOrEm)) {
return exports.parse(bufOrEm);
}
- else {
- return exports.stream(bufOrEm, eventName);
+
+ var s = exports.stream();
+ if (bufOrEm && bufOrEm.pipe) {
+ bufOrEm.pipe(s);
+ }
+ else if (bufOrEm) {
+ bufOrEm.on(eventName || 'data', function (buf) {
+ s.write(buf);
+ });
+
+ bufOrEm.on('end', function () {
+ s.end();
+ });
}
+ return s;
};
-exports.put = require('put');
-
-exports.stream = function (em, eventName) {
- if (eventName === undefined) eventName = 'data';
+exports.stream = function (input) {
+ if (input) return exports.apply(null, arguments);
var pending = null;
function getBytes (bytes, cb, skip) {
@@ -30,12 +41,6 @@ exports.stream = function (em, eventName) {
dispatch();
}
- var buffers = Buffers();
- em.on(eventName, function (buf) {
- buffers.push(buf);
- dispatch();
- });
-
var offset = null;
function dispatch () {
if (!pending) {
@@ -73,14 +78,7 @@ exports.stream = function (em, eventName) {
}
}
- var vars = Vars();
-
- var done = false, caughtEnd = false;
- em.on('end', function () {
- caughtEnd = true
- });
-
- return Chainsaw.light(function builder (saw) {
+ function builder (saw) {
function next () { if (!done) saw.next() }
var self = words(function (bytes, cb) {
@@ -200,7 +198,31 @@ exports.stream = function (em, eventName) {
};
return self;
+ };
+
+ var stream = Chainsaw.light(builder);
+ stream.writable = true;
+
+ var buffers = Buffers();
+
+ stream.write = function (buf) {
+ buffers.push(buf);
+ dispatch();
+ };
+
+ var vars = Vars();
+
+ var done = false, caughtEnd = false;
+ stream.end = function () {
+ caughtEnd = true;
+ };
+
+ stream.pipe = Stream.prototype.pipe;
+ Object.getOwnPropertyNames(EventEmitter.prototype).forEach(function (name) {
+ stream[name] = EventEmitter.prototype[name];
});
+
+ return stream;
};
exports.parse = function parse (buffer) {
View
9 package.json
@@ -1,6 +1,6 @@
{
"name" : "binary",
- "version" : "0.2.6",
+ "version" : "0.3.0",
"description" : "Unpack multibyte binary values from buffers",
"main" : "./index.js",
"repository" : {
@@ -21,9 +21,8 @@
"url" : "http://substack.net"
},
"dependencies" : {
- "chainsaw" : "0.1.x",
- "put" : ">=0.0.5 <0.1",
- "buffers" : "0.1.x"
+ "chainsaw" : "~0.1.0",
+ "buffers" : "~0.1.1"
},
"devDependencies" : {
"seq" : "~0.2.5",
@@ -32,7 +31,7 @@
"scripts" : {
"test" : "tap test/*.js"
},
- "license" : "MIT/X11",
+ "license" : "MIT",
"engine" : {
"node" : ">=0.4.0"
}
View
49 test/pipe.js
@@ -0,0 +1,49 @@
+var binary = require('../');
+var test = require('tap').test;
+var Stream = require('stream').Stream;
+
+test('loop', function (t) {
+ t.plan(3 * 2 + 1);
+
+ var rs = new Stream;
+ rs.readable = true;
+
+ var ws = binary()
+ .loop(function (end, vars) {
+ t.strictEqual(vars, this.vars);
+ this
+ .word16lu('a')
+ .word8u('b')
+ .word8s('c')
+ .tap(function (vars_) {
+ t.strictEqual(vars, vars_);
+ if (vars.c < 0) end();
+ })
+ ;
+ })
+ .tap(function (vars) {
+ t.same(vars, { a : 1337, b : 55, c : -5 });
+ })
+ ;
+ rs.pipe(ws);
+
+ setTimeout(function () {
+ rs.emit('data', new Buffer([ 2, 10, 88 ]));
+ }, 10);
+ setTimeout(function () {
+ rs.emit('data', new Buffer([ 100, 3, 6, 242, 30 ]));
+ }, 20);
+ setTimeout(function () {
+ rs.emit('data', new Buffer([ 60, 60, 199, 44 ]));
+ }, 30);
+
+ setTimeout(function () {
+ rs.emit('data', new Buffer([ 57, 5 ]));
+ }, 80);
+ setTimeout(function () {
+ rs.emit('data', new Buffer([ 55, 251 ]));
+ }, 90);
+ setTimeout(function () {
+ rs.emit('end');
+ }, 100);
+});
View
15 test/put.js
@@ -1,15 +0,0 @@
-var Binary = require('../');
-var test = require('tap').test;
-
-test('builder', function (t) {
- t.plan(1);
- var buf = Binary.put()
- .word16be(1337)
- .put(new Buffer([ 7, 8, 9 ]))
- .buffer()
- ;
- t.same(
- [].slice.call(buf),
- [].slice.call(new Buffer([ 0x05, 0x39, 0x07, 0x08, 0x09 ]))
- );
-});

No commit comments for this range

Something went wrong with that request. Please try again.