Coder Social home page Coder Social logo

bencode.js's Introduction

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("bencode"),
    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("bencode"),
    buf  = null

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

log(decoder.result())

Or "all in one"

var benc = require("bencode"),
    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

bencode.js 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.

License

MIT, see LICENSE

bencode.js's People

Contributors

a2800276 avatar fent avatar clarkf avatar ivshti avatar

Watchers

James Cloos avatar  avatar  avatar

Forkers

grunya404

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.