UNPKG

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