Skip to content

VrajVed/Bencode-Parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Bencode Parser

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.

Features

  • ✅ Full Bencode support (integers, strings, lists, dictionaries)
  • ✅ Simple and intuitive API
  • ✅ Zero dependencies
  • ✅ ES6 module support
  • ✅ Bidirectional conversion (encode & decode)

Installation

Clone or download this repository:

git clone https://github.com/VrajVed/Bencode-Parser.git
cd Bencode-Parser

Usage

Importing

import BencodeParser from './bencoder.js';

Encoding

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'

Decoding

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' }

Alternative API (Instance-based)

You can also create a parser instance for decoding:

const parser = new BencodeParser('i42e');
const result = parser.parse();
console.log(result); // 42

Bencode Format

Bencode 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"}

API Reference

BencodeParser.encode(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

BencodeParser.decode(bencodedString)

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

new BencodeParser(data)

Creates a parser instance (for manual parsing).

Parameters:

  • data (string) - The Bencode string to parse

parser.parse()

Parses the Bencode string provided to the constructor.

Returns: number|string|array|object - The parsed JavaScript value

Examples

Round-trip Conversion

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'] }

Working with Torrent Files

// 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 file

Running Tests

node index.js

Contributing

Contributions are welcome! Feel free to open issues or submit pull requests.

Author

VrajVed

Resources


Created for educational purposes to learn about parsing and encoding.

About

A basic bencode parser written in javascript

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published