A bencode decode/encode library based on Node.js.
- TypeScript v3.1.x (or newer)
- Node.js v8.0.0 (or newer)
npm i @litert/bencode --save
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' } ] }
This library is published under Apache-2.0 license.