Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 109 lines (79 sloc) 2.393 kb
11b5c60 James Halliday rebranded as binary
authored
1 Binary
2 ======
9ebc04e James Halliday readme woo
authored
3
4 Unpack multibyte binary values from buffers and streams.
5 You can specify the endianness and signedness of the fields to be unpacked too.
6
1e33146 James Halliday buf.js example
authored
7 This module is a cleaner, faster, and more complete version of
8 [bufferlist](https://github.com/substack/node-bufferlist)'s binary module that
9 runs on pre-allocated buffers instead of a linked list.
10
11 Examples
12 ========
13
14 buf.js
15 ------
16 var buf = new Buffer([ 97, 98, 99, 100, 101, 102, 0 ]);
17
18 var Binary = require('binary');
19 Binary(buf)
20 .word16ls('ab')
21 .word32bu('cf')
22 .word8('x')
23 .tap(function (vars) {
24 console.dir(vars);
25 })
26 ;
27 -
28 $ node buf.js
29 { ab: 25185, cf: 1667523942, x: 0 }
30
d783aa4 James Halliday stream example
authored
31 stream.js
32 ---------
33
34 var Binary = require('binary');
35 var stdin = process.openStdin();
36
37 Binary(stdin)
38 .word32lu('x')
39 .word16bs('y')
40 .word16bu('z')
41 .tap(function (vars) {
42 console.dir(vars);
43 })
44 ;
45 -
46 $ node examples/stream.js
47 abcdefgh
48 { x: 1684234849, y: 25958, z: 26472 }
49 ^D
50
8464aae James Halliday readme updates for the methods
authored
51 Methods
52 =======
53
54 Binary(buf)
55 -----------
56
57 Start a new chain parser for a `Buffer`.
58
59 Binary(emitter)
60 ---------------
61 Binary(emitter, eventName='data')
62 ---------------------------------
63
64 Start a new chain parser for an `EventEmitter` for an event name `eventName`,
65 which defaults to `'data'`.
66
67 word{8,16,32,64}{l,b}{e,u,s}(name)
68 ----------------------------------
69
70 Parse bytes in the buffer or stream given:
71
01c32c0 James Halliday silly markdown
authored
72 * number of bits
73 * endianness ( l : little, b : big ),
74 * signedness ( u and e : unsigned, s : signed )
8464aae James Halliday readme updates for the methods
authored
75
76 These functions won't start parsing until all previous parser functions have run
77 and the data is available.
78
79 The result of the parse goes into the variable stash at `name`.
80
81 tap(cb)
82 -------
83
84 The callback `cb` is provided with the variable stash from all the previous
85 actions once they've all finished.
86
9ebc04e James Halliday readme woo
authored
87 Installation
88 ============
89
90 To install with [npm](http://github.com/isaacs/npm):
91
11b5c60 James Halliday rebranded as binary
authored
92 npm install binary
9ebc04e James Halliday readme woo
authored
93
94 To run the tests with [expresso](http://github.com/visionmedia/expresso):
95
96 expresso
1e33146 James Halliday buf.js example
authored
97
98 Notes
99 =====
100
101 The word64 functions will only return approximations since javascript uses ieee
102 floating point for all number types. Mind the loss of precision.
103
104 Todo
105 ====
106
107 * Actually verify that this approach is faster than bufferlist/binary.
108 * Add all the nifty nested parser functions without sacrificing performance.
Something went wrong with that request. Please try again.