• Stars
    star
    165
  • Rank 228,906 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 14 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

bencode de/encoder for nodejs

Bencode

npm npm downloads tests FOSSA Status

A node library for encoding and decoding bencoded data, according to the BitTorrent specification.

Index

About BEncoding

from Wikipedia:

Bencode (pronounced like B encode) is the encoding used by the peer-to-peer file sharing system BitTorrent for storing and transmitting loosely structured data.

It supports four different types of values:

  • byte strings
  • integers
  • lists
  • dictionaries

Bencoding is most commonly used in torrent files. These metadata files are simply bencoded dictionaries.

Install with npm

npm install bencode

Usage

import bencode from 'bencode'

You can also use node-bencode with browserify to be able to use it in a lot of modern browsers.

Encoding

var data = {
  string: 'Hello World',
  integer: 12345,
  dict: {
    key: 'This is a string within a dictionary'
  },
  list: [ 1, 2, 3, 4, 'string', 5, {} ]
}

var result = bencode.encode( data )

NOTE As of [email protected], boolean values will be cast to integers (false -> 0, true -> 1).

Output

d4:dictd3:key36:This is a string within a dictionarye7:integeri12345e4:listli1ei2ei3ei4e6:stringi5edee6:string11:Hello Worlde

Decoding

var data = Buffer.from('d6:string11:Hello World7:integeri12345e4:dictd3:key36:This is a string within a dictionarye4:listli1ei2ei3ei4e6:stringi5edeee')
var result = bencode.decode( data )

Output

{
  string: <Buffer 48 65 6c 6c 6f 20 57 6f 72 6c 64>,
  integer: 12345,
  dict: {
    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>
  },
  list: [ 1, 2, 3, 4, <Buffer 73 74 72 69 6e 67>, 5, {} ]
}

Automagically convert bytestrings to strings:

var result = bencode.decode( data, 'utf8' )

Output

{
  string: 'Hello World',
  integer: 12345,
  dict: {
    key: 'This is a string within a dictionary'
  },
  list: [ 1, 2, 3, 4, 'string', 5, {} ]
}

API

The API is compatible with the abstract-encoding specification.

bencode.encode( data, [buffer], [offset] )

Buffer | Array | String | Object | Number | Boolean data Buffer buffer Number offset

Returns Buffer

bencode.decode( data, [start], [end], [encoding] )

Buffer data Number start Number end String encoding

If encoding is set, bytestrings are automatically converted to strings.

Returns Object | Array | Buffer | String | Number

bencode.byteLength( value ) or bencode.encodingLength( value )

Buffer | Array | String | Object | Number | Boolean value

More Repositories

1

webtorrent

⚡️ Streaming torrent client for the web
JavaScript
29,385
star
2

webtorrent-desktop

❤️ Streaming torrent app for Mac, Windows, and Linux
JavaScript
9,659
star
3

instant.io

🚀 Streaming file transfer over WebTorrent (torrents on the web)
JavaScript
3,423
star
4

bittorrent-tracker

🌊 Simple, robust, BitTorrent tracker (client & server) implementation
JavaScript
1,744
star
5

bittorrent-dht

🕸 Simple, robust, BitTorrent DHT implementation
JavaScript
1,216
star
6

webtorrent-cli

WebTorrent, the streaming torrent client. For the command line.
JavaScript
1,154
star
7

webtorrent-hybrid

WebTorrent (with WebRTC support in Node.js)
JavaScript
518
star
8

parse-torrent

Parse a torrent identifier (magnet uri, .torrent file, info hash)
JavaScript
438
star
9

create-torrent

Create .torrent files
JavaScript
342
star
10

bittorrent-protocol

Simple, robust, BitTorrent peer wire protocol implementation
JavaScript
342
star
11

magnet-uri

Parse a magnet URI and return an object of keys/values
JavaScript
221
star
12

torrent-discovery

Discover BitTorrent and WebTorrent peers
JavaScript
216
star
13

webtorrent.io

The code that runs the WebTorrent website
JavaScript
125
star
14

ut_metadata

BitTorrent Extension for Peers to Send Metadata Files (BEP 9)
JavaScript
110
star
15

bittorrent-peerid

Map a BitTorrent peer ID to a human-readable client name and version
JavaScript
65
star
16

parse-torrent-file

DEPRECATED: Parse a .torrent file and return an object of keys/values
JavaScript
63
star
17

ut_pex

Implementation of ut_pex bittorrent protocol (PEX) for webtorrent
JavaScript
58
star
18

workshop

Learn WebTorrent and WebRTC in a guided workshop!
48
star
19

bittorrent-lsd

Local Service Discovery (BEP14) implementation
JavaScript
35
star
20

fs-chunk-store

Filesystem (fs) chunk store that is abstract-chunk-store compliant
JavaScript
33
star
21

torrent-piece

Torrent piece abstraction
JavaScript
23
star
22

webtorrent-fixtures

Sample torrent files for the WebTorrent test suite
JavaScript
22
star
23

load-ip-set

download and parse ip-set (blocklist) files
JavaScript
22
star
24

lt_donthave

The BitTorrent lt_donthave extension (BEP 54)
JavaScript
11
star
25

addr-to-ip-port

Convert an "address:port" string to an array [address:string, port:number]
JavaScript
10
star
26

bep53-range

Parse and compose Magnet URI extension (BEP53) ranges
JavaScript
9
star
27

string2compact

Convert 'hostname:port' strings to BitTorrent's compact ip/host binary returned by Trackers
JavaScript
9
star
28

renovate-config

Renovate's config
1
star
29

semantic-release-config

JavaScript
1
star
30

.github

1
star