Debian Codegraph @

node/varint/README.md

raw
1 # varint
2
3 encode whole numbers to an array of [protobuf-style varint bytes](https://developers.google.com/protocol-buffers/docs/encoding#varints) and also decode them.
4
5 ```javascript
6 var varint = require('varint')
7
8 var bytes = varint.encode(300) // === [0xAC, 0x02]
9 varint.decode(bytes) // 300
10 varint.decode.bytes // 2 (the last decode() call required 2 bytes)
11 ```
12
13 ## api
14
15 ### varint = require('varint')
16
17 ### varint.encode(num[, buffer=[], offset=0]) -> buffer
18
19 Encodes `num` into `buffer` starting at `offset`. returns `buffer`, with the encoded varint written into it. If `buffer` is not provided, it will default to a new array.
20
21 `varint.encode.bytes` will now be set to the number of bytes
22 modified.
23
24 ### varint.decode(data[, offset=0]) -> number
25
26 decodes `data`, which can be either a buffer or array of integers, from position `offset` or default 0 and returns the decoded original integer.
27
28 Throws a `RangeError` when `data` does not represent a valid encoding.
29
30 ### varint.decode.bytes
31
32 if you also require the length (number of bytes) that were required to decode the integer you can access it via `varint.decode.bytes`. this is an integer property that will tell you the number of bytes that the last .decode() call had to use to decode.
33
34 ### varint.encode.bytes
35
36 similar to `decode.bytes` when encoding a number it can be useful to know how many bytes where written (especially if you pass an output array). you can access this via `varint.encode.bytes` which holds the number of bytes written in the last encode.
37
38
39 ### varint.encodingLength(num)
40
41 returns the number of bytes this number will be encoded as, up to a maximum of 8.
42
43 ## usage notes
44
45 If varint is passed a buffer that does not contain a valid end
46 byte, then `decode` will throw `RangeError`, and `decode.bytes`
47 will be set to 0. If you are reading from a streaming source,
48 it's okay to pass an incomplete buffer into `decode`, detect this
49 case, and then concatenate the next buffer.
50
51 # License
52
53 MIT