A BitTorrent bencoding and decoding library for Node.js, Deno, and Bun.
Bencoding is the encoding format used by BitTorrent, specified in BEP 3.
- Works in Node.js, Deno, and Bun
- TypeScript definitions included
- Zero dependencies
- Single file implementation
npm install bncodeimport { encode, decode } from 'bncode'
const exmp = {
bla: 'blup',
foo: 'bar',
one: 1,
woah: {
arr: [1, 2, 3]
},
str: Buffer.from('Buffers work too')
}
const bencBuffer = encode(exmp)
// d3:bla4:blup3:foo3:bar3:onei1e4:woahd3:arr \
// li1ei2ei3eee3:str16:Buffers work tooeDecoding works progressively, e.g., if you're receiving partial bencoded strings on the network:
const bncode = require('bncode')
let buf = null
const decoder = new bncode.decoder()
while (buf = receiveData()) {
decoder.decode(buf)
}
console.log(decoder.result())Or "all in one":
const bncode = require('bncode')
const buf = getBuffer()
const dec = bncode.decode(buf)
console.log(dec.bla)There are some subtleties concerning bencoded strings. These are decoded as Buffer objects because they are just strings of raw bytes and as such would wreak havoc with multi-byte strings in JavaScript.
The exception to this is strings appearing as keys in bencoded dictionaries. These are decoded as JavaScript Strings, as they should always be strings of (ASCII) characters. If they weren't decoded as JS Strings, dictionaries couldn't be mapped to JavaScript objects.
+----------------------------------------------------+
| | |
| Bencoded | JavaScript |
|====================================================|
| Strings | Node Buffers, unless they are |
| | dictionary keys, in which case |
| | they become JavaScript Strings |
|----------------+-----------------------------------|
| Integers | Number |
|----------------+-----------------------------------|
| Lists | Array |
|----------------+-----------------------------------|
| Dictionaries | Object |
| | |
+----------------------------------------------------+
The code makes a best effort to encode JavaScript to bencoding. If you stick to basic types (Arrays, Objects with String keys and basic values, Strings, Buffers and Numbers) you shouldn't encounter surprises. Expect surprises (mainly not being able to round-trip encode/decode) if you encode fancy data types.
A transform stream is also available:
const bncode = require('bncode')
const fs = require('fs')
fs.createReadStream('file.torrent')
.pipe(new bncode.Stream())
.on('data', (data) => {
console.log(data)
})Encodes a JavaScript object into a bencoded Buffer.
Decodes a bencoded buffer into a JavaScript object.
Creates a progressive decoder that can handle partial data.
Creates a transform stream for decoding bencoded data.
bncode was written by Tim Becker (tim.becker@kuriositaet.de). I can be reached via email or (preferably) submit a bug to the GitHub repository.
- Roly Fentanes (fent) for bug reports
- Clark Fischer (clarkf)
- The fine folks at Travis
- Patrick Williams
- Feross Aboukhadijeh
MIT, see LICENSE
