|
1 | | -### Usage |
2 | 1 |
|
3 | | - var bencode = require("bencode"); |
4 | | - var data = bencode.decode(benstring); |
| 2 | +# node-bencode |
| 3 | + |
| 4 | +A node library for encoding and decoding bencoded data, |
| 5 | +according to the [BitTorrent specification](http://www.bittorrent.org/beps/bep_0003.html). |
| 6 | + |
| 7 | +## Index |
| 8 | + |
| 9 | +- [About BEncoding](#about-bencoding) |
| 10 | +- [Installation](#install-with-npm) |
| 11 | +- [Usage](#usage) |
| 12 | +- [API](#api) |
| 13 | + |
| 14 | +## About BEncoding |
| 15 | + |
| 16 | +from [Wikipedia](https://en.wikipedia.org/wiki/Bencoding): |
| 17 | + |
| 18 | +Bencode (pronounced like B encode) is the encoding used by the peer-to-peer |
| 19 | +file sharing system BitTorrent for storing and transmitting loosely structured data. |
| 20 | + |
| 21 | +It supports four different types of values: |
| 22 | +- byte strings |
| 23 | +- integers |
| 24 | +- lists |
| 25 | +- dictionaries |
| 26 | + |
| 27 | +Bencoding is most commonly used in torrent files. |
| 28 | +These metadata files are simply bencoded dictionaries. |
| 29 | + |
| 30 | +## Install with [npm](http://npmjs.org) |
| 31 | + |
| 32 | +``` |
| 33 | +npm install bencode |
| 34 | +``` |
| 35 | + |
| 36 | +## Usage |
| 37 | + |
| 38 | +```javascript |
| 39 | +var bencode = require( 'bencode' ) |
| 40 | +``` |
| 41 | + |
| 42 | +### Encoding |
| 43 | + |
| 44 | +```javascript |
| 45 | + |
| 46 | +var data = { |
| 47 | + string: 'Hello World', |
| 48 | + integer: 12345, |
| 49 | + dict: { |
| 50 | + key: 'This is a string within a dictionary' |
| 51 | + }, |
| 52 | + list: [ 1, 2, 3, 4, 'string', 5, {} ] |
| 53 | +} |
| 54 | + |
| 55 | +var result = bencode.encode( data ) |
| 56 | + |
| 57 | +``` |
| 58 | + |
| 59 | +#### Output |
| 60 | + |
| 61 | +``` |
| 62 | +d6:string11:Hello World7:integeri12345e4:dictd3:key36:This is a string within a dictionarye4:litli1ei2ei3ei4e6:stringi5edeee |
| 63 | +``` |
| 64 | + |
| 65 | +### Decoding |
| 66 | + |
| 67 | +```javascript |
| 68 | +var data = new Buffer( 'd6:string11:Hello World7:integeri12345e4:dictd3:key36:This is a string within a dictionarye4:litli1ei2ei3ei4e6:stringi5edeee' ) |
| 69 | +var result = bencode.decode( data ) |
| 70 | +``` |
| 71 | + |
| 72 | +#### Output |
| 73 | + |
| 74 | +```javascript |
| 75 | +{ |
| 76 | + string: <Buffer 48 65 6c 6c 6f 20 57 6f 72 6c 64>, |
| 77 | + integer: 12345, |
| 78 | + dict: { |
| 79 | + key: <Buffer 54 68 69 73 20 69 73 20 61 20 73 74 72 69 6e 67 20 77 69 74 68 69 6e 20 61 20 64 69 63 74 69 6f 6e 61 72 79> |
| 80 | + }, |
| 81 | + list: [ 1, 2, 3, 4, <Buffer 73 74 72 69 6e 67>, 5, {} ] |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +Automagically convert bytestrings to strings: |
| 86 | + |
| 87 | +```javascript |
| 88 | +var result = bencode.decode( data, true ) |
| 89 | +``` |
| 90 | + |
| 91 | +#### Output |
| 92 | + |
| 93 | +```javascript |
| 94 | +{ |
| 95 | + string: 'Hello World', |
| 96 | + integer: 12345, |
| 97 | + dict: { |
| 98 | + key: 'This is a string within a dictionary' |
| 99 | + }, |
| 100 | + list: [ 1, 2, 3, 4, 'string', 5, {} ] |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +## API |
| 105 | + |
| 106 | +### bencode.encode( *data* ) |
| 107 | + |
| 108 | +> `Buffer` | `Array` | `String` | `Object` | `Number` __data__ |
| 109 | +
|
| 110 | +Returns `String` |
| 111 | + |
| 112 | +### bencode.decode( *data*, *toString* ) |
| 113 | + |
| 114 | +> `Buffer` __data__ |
| 115 | +> `Boolean` __toString__ |
| 116 | +
|
| 117 | +If `toString` is set to something truthy, |
| 118 | +bytestrings are automatically converted to strings. |
| 119 | + |
| 120 | +Returns `Object` | `Array` | `Buffer` | `String` | `Number` |
0 commit comments