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 🙏

© 2024 – Pkg Stats / Ryan Hefner

bncode

v0.5.3

Published

bittorrent bencoding and decoding.

Downloads

3,422

Readme

build status

bencoding for JS (node.js)

This is a small library to encode and decode bencoded (bittorrent) stuff. Bencoding is specified here.

Get & Install

github repository is here

Installable via npm (npm package name is bncode, note spelling!):

npm install bncode

Encoding

Encoding works as follows:

var benc  = require("bncode"),
    exmp = {}

exmp.bla = "blup"
exmp.foo = "bar"
exmp.one = 1
exmp.woah = {}
exmp.woah.arr = []
exmp.woah.arr.push(1)
exmp.woah.arr.push(2)
exmp.woah.arr.push(3)
exmp.str = new Buffer("Buffers work too")

var bencBuffer = benc.encode(exmp)

// d3:bla4:blup3:foo3:bar3:onei1e4:woahd3:arr \
// li1ei2ei3eee3:str16:Buffers work tooe

Decoding

Decoding will work progressively, e.g. if you're receiving partial bencoded strings on the network:

var benc = require("bncode"),
    buf  = null

decoder = new benc.decoder()
while (buf = receiveData()) {
  decoder.decode(buf)
}

log(decoder.result())

Or "all in one"

var benc = require("bncode"),
    buf  = getBuffer(),
    dec  = benc.decode(buf)

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 dicts. These are decoded as Javascript Strings, as they should always be strings of (ascii) characters and if they weren't decoded as JS Strings, dict's 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 suprises. Expect surprises (mainly not being able to round-trip encode/decode) if you encode fancy data-types.

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