Skip to content

litert/bencode.js

Repository files navigation

LiteRT/Bencode

npm version License node GitHub issues GitHub Releases

A bencode decode/encode library based on Node.js.

Requirement

  • TypeScript v3.1.x (or newer)
  • Node.js v8.0.0 (or newer)

Installation

npm i @litert/bencode --save

Usage

Encoding:

import * as libBencode from "@litert/bencode";

const enc = new libBEncode.BencodeEncoder();

const beData = enc.encode({ // Encode data into BEncode.
    "name": "Angus",
    "age": 18,
    "friends": [
        "Edith",
        "Alex"
    ],
    "scores": {
        "math": 87
    },
    "randomBytes": Buffer.from('vsm/GvyGjZqUeuPa7ZP8h9ot8VRCe/6arpboI46EIlg=', 'base64')
});

/**
 * Don't print as a UTF-8 string, because it's binary.
 */
console.log(beData);

Decoding:

import * as libBencode from "@litert/bencode";

const dec = new LibBencode.BencodeDecoder();

const beData = dec.decode('d4:name5:Angus7:friendsld4:name5:Editheee');

console.log(beData); // Output: { name: 'Angus', friends: [ { name: 'Edith' } ] }

License

This library is published under Apache-2.0 license.