1 | # bufio
|
2 |
|
3 | Buffer and serialization utilities for javascript.
|
4 |
|
5 | ## Usage
|
6 |
|
7 | ``` js
|
8 | const assert = require('assert');
|
9 | const bio = require('bufio');
|
10 |
|
11 | const bw = bio.write();
|
12 | bw.writeU64(100);
|
13 | bw.writeString('foo', 'ascii');
|
14 | const data = bw.render();
|
15 |
|
16 | const br = bio.read(data);
|
17 | assert(br.readU64() === 100);
|
18 | assert(br.readString('ascii') === 'foo');
|
19 | ```
|
20 |
|
21 | ## Struct Usage
|
22 |
|
23 | ``` js
|
24 | const bio = require('bufio');
|
25 |
|
26 | class MyStruct extends bio.Struct {
|
27 | constructor() {
|
28 | super();
|
29 | this.str = 'hello';
|
30 | this.value = 0;
|
31 | }
|
32 |
|
33 | write(bw) {
|
34 | bw.writeVarString(this.str, 'ascii');
|
35 | bw.writeU64(this.value);
|
36 | return this;
|
37 | }
|
38 |
|
39 | read(br) {
|
40 | this.str = br.readVarString('ascii');
|
41 | this.value = br.readU64();
|
42 | return this;
|
43 | }
|
44 | }
|
45 |
|
46 | const obj = new MyStruct();
|
47 |
|
48 | console.log('Buffer:');
|
49 | console.log(obj.encode());
|
50 |
|
51 | console.log('Decoded:');
|
52 | console.log(MyStruct.decode(obj.encode()));
|
53 |
|
54 | console.log('Hex:');
|
55 | console.log(obj.toHex());
|
56 |
|
57 | console.log('Decoded:');
|
58 | console.log(MyStruct.fromHex(obj.toHex()));
|
59 |
|
60 | console.log('Base64:');
|
61 | console.log(obj.toBase64());
|
62 | ```
|
63 |
|
64 | ## Contribution and License Agreement
|
65 |
|
66 | If you contribute code to this project, you are implicitly allowing your code
|
67 | to be distributed under the MIT license. You are also implicitly verifying that
|
68 | all code is your original work. `</legalese>`
|
69 |
|
70 | ## License
|
71 |
|
72 | - Copyright (c) 2017, Christopher Jeffrey (MIT License).
|
73 |
|
74 | See LICENSE for more info.
|