A lightweight JavaScript library for encoding and decoding Bencode format. Bencode is the encoding used by the BitTorrent protocol for storing and transmitting loosely structured data.
- ✅ Full Bencode support (integers, strings, lists, dictionaries)
- ✅ Simple and intuitive API
- ✅ Zero dependencies
- ✅ ES6 module support
- ✅ Bidirectional conversion (encode & decode)
Clone or download this repository:
git clone https://github.com/VrajVed/Bencode-Parser.git
cd Bencode-Parserimport BencodeParser from './bencoder.js';Convert JavaScript values to Bencode format:
// Encode an integer
BencodeParser.encode(42);
// Output: 'i42e'
// Encode a string
BencodeParser.encode('spam');
// Output: '4:spam'
// Encode an array
BencodeParser.encode([1, 2, 3]);
// Output: 'li1ei2ei3ee'
// Encode an object
BencodeParser.encode({ key: 'value' });
// Output: 'd3:key5:valuee'
// Encode nested structures
BencodeParser.encode({
name: 'example',
size: 1024,
files: ['file1.txt', 'file2.txt']
});
// Output: 'd5:filesli9:file1.txti9:file2.txtee4:name7:example4:sizei1024ee'Convert Bencode strings back to JavaScript:
// Decode an integer
BencodeParser.decode('i42e');
// Output: 42
// Decode a string
BencodeParser.decode('4:spam');
// Output: 'spam'
// Decode a list
BencodeParser.decode('li1ei2ei3ee');
// Output: [1, 2, 3]
// Decode a dictionary
BencodeParser.decode('d3:key5:valuee');
// Output: { key: 'value' }You can also create a parser instance for decoding:
const parser = new BencodeParser('i42e');
const result = parser.parse();
console.log(result); // 42Bencode supports four data types:
| Type | Format | Example | JavaScript Equivalent |
|---|---|---|---|
| Integer | i<number>e |
i42e |
42 |
| String | <length>:<string> |
4:spam |
"spam" |
| List | l<items>e |
li1ei2ee |
[1, 2] |
| Dictionary | d<key><value>...e |
d3:key5:valuee |
{key: "value"} |
Encodes a JavaScript value into Bencode format.
Parameters:
value(number|string|array|object) - The value to encode
Returns: String - The Bencode encoded string
Throws: Error if the value type is not supported
Decodes a Bencode string into a JavaScript value.
Parameters:
bencodedString(string) - The Bencode string to decode
Returns: number|string|array|object - The decoded JavaScript value
Throws: Error if the Bencode format is invalid
Creates a parser instance (for manual parsing).
Parameters:
data(string) - The Bencode string to parse
Parses the Bencode string provided to the constructor.
Returns: number|string|array|object - The parsed JavaScript value
const original = {
name: 'MyTorrent',
size: 2048,
files: ['video.mp4', 'subtitle.srt']
};
const encoded = BencodeParser.encode(original);
console.log(encoded);
// 'd5:filesli9:video.mp4i12:subtitle.srtee4:name9:MyTorrent4:sizei2048ee'
const decoded = BencodeParser.decode(encoded);
console.log(decoded);
// { name: 'MyTorrent', size: 2048, files: ['video.mp4', 'subtitle.srt'] }// Typical torrent metadata structure
const torrentInfo = {
announce: 'http://tracker.example.com:8080/announce',
info: {
name: 'example.iso',
length: 1073741824,
'piece length': 262144
}
};
const encoded = BencodeParser.encode(torrentInfo);
// This can now be written to a .torrent filenode index.jsContributions are welcome! Feel free to open issues or submit pull requests.
VrajVed
Created for educational purposes to learn about parsing and encoding.