npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

bncode

v0.6.0

Published

BitTorrent bencoding and decoding library for Node.js

Downloads

7,669

Readme

build status JSR JSR Score

bncode

A BitTorrent bencoding and decoding library for Node.js, Deno, and Bun.

Bencoding is the encoding format used by BitTorrent, specified in BEP 3.

Features

  • Works in Node.js, Deno, and Bun
  • TypeScript definitions included
  • Zero dependencies
  • Single file implementation

Installation

npm install bncode

## Usage

import { 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 tooe

Decoding

Decoding 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)

String Handling

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.

Mapping bencoding to JavaScript

 +----------------------------------------------------+
 |                |                                   |
 |  Bencoded      |    JavaScript                     |
 |====================================================|
 |  Strings       |    Node Buffers, unless they are  |
 |                |    dictionary keys, in which case |
 |                |    they become JavaScript Strings |
 |----------------+-----------------------------------|
 |  Integers      |    Number                         |
 |----------------+-----------------------------------|
 |  Lists         |    Array                          |
 |----------------+-----------------------------------|
 |  Dictionaries  |    Object                         |
 |                |                                   |
 +----------------------------------------------------+

Mapping JavaScript to bencoding

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.

Stream API

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

API

bncode.encode(obj)

Encodes a JavaScript object into a bencoded Buffer.

bncode.decode(buffer, [encoding])

Decodes a bencoded buffer into a JavaScript object.

new bncode.decoder()

Creates a progressive decoder that can handle partial data.

new bncode.Stream([options])

Creates a transform stream for decoding bencoded data.

Author

bncode was written by Tim Becker ([email protected]). I can be reached via email or (preferably) submit a bug to the GitHub repository.

Thanks

  • Roly Fentanes (fent) for bug reports
  • Clark Fischer (clarkf)
  • The fine folks at Travis
  • Patrick Williams
  • Feross Aboukhadijeh

License

MIT, see LICENSE