Package detail

bencode

webtorrent123.8kMIT4.0.0

Bencode de/encoder

bdecode, bencode, bencoding, bittorrent

readme

Bencode

npm npm downloads ci 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 bencode@0.8.0, 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

changelog

4.0.0 (2023-08-09)

chore

BREAKING CHANGES

  • update uint8-util

3.1.0 (2023-07-31)

Features

3.0.3 (2023-01-31)

Bug Fixes

3.0.2 (2023-01-31)

Bug Fixes

3.0.1 (2023-01-31)

Bug Fixes

3.0.0 (2022-11-28)

Features

BREAKING CHANGES

  • ESM only

2.0.3 (2022-05-13)

2.0.2 (2021-07-28)

Bug Fixes

  • Patch release to drop a dependecy to safe-buffer (#99) (a661715)

2.0.1

  • fix deprecation warning on Buffer() constructor (@jhermsmeier)
  • update dev depedencies (@jhermsmeier)

2.0.0

  • Drop support for Node 0.10, 0.12., add support for Node 8 & 9 (@jhermsmeier)
  • Support for typed arrays (@jhermsmeier, @nazar-pc)

1.0.0

  • Support Node 0.10, 0.12, and early Node 4 (@feross)

0.12.0

  • Add btparse to benchmarks (@themasch)
  • Use Buffer.from() & Buffer.allocUnsafe() (@slang800)
  • Use constants for character codes (@slang800)
  • Fix Makefile (@zunsthy)

0.11.0

  • Ignore null-values when encoding (@jhermsmeier)
  • Add test/BEP-0023: Test correct handling of compacted peer lists (@jhermsmeier)
  • Implement a faster way to parse intergers from buffers (@themasch)
  • Fix string to be decoded in README (@ngotchac)

0.10.0

  • Add standard code style (@slang800)
  • Update benchmarks (@slang800)
  • Remove lib/dict.js (@slang800)
  • Move main entrypoint into ./lib (@slang800)
  • Clean up package.json (@slang800)
  • Remove extra files from being published to npm (@slang800)

0.9.0

  • Implement the abstract-encoding API (@jhermsmeier)

0.8.0

  • Add support for encoding Boolean values (@kaelar)

0.7.0

  • Add binary key support (@deoxxa)
  • Improve test output format (@jhermsmeier)
  • Removed node v0.8 from CI tests

0.6.0

  • Fixed invalid test data (@themasch)
  • Added Makefile for browser tests (@themasch)
  • Fixed Browserify compatibility (@themasch)

0.5.2

  • Thorough fix for 64 bit and 53 bit numbers (@pwmckenna)

0.5.1

  • Added warning on float conversion during encoding (@jhermsmeier)

0.5.0

  • Added support for 64 bit number values (@pwmckenna)
  • Switched benchmark lib to matcha (@themasch)
  • Fixed npm scripts to work on Windows (@jhermsmeier)

0.4.3

  • improved performance a lot
  • dropped support for de- and encoding floats to respect the spec

    note: node-bencode will still decodes stuff like "i42.23e" but will cast the result to an interger

0.4.2

  • bugfix: sort dictionary keys to follow the spec

0.4.1

  • bugfix: number decoding was kinda broken

0.4.0

  • fixed problems with multibyte strings
  • some performance improvements
  • improved code quality

0.3.0

  • decode() accepts a encoding as its second paramtere

0.2.0

  • complete rewrite, @jhermsmeier joins the team

0.1.0

  • added encoding

0.0.1

First version, decoding only