Library for decoding and encoding bencoded data.
Compliant with the BitTorrent bencoding specification.
Fast and easy to use.
Written in TypeScript.
Fully tested with 100% code coverage.
Without dependencies.
npm | yarn |
---|---|
npm install --save bencodec |
yarn add bencodec |
typescript | javascript |
---|---|
import bencodec from 'bencodec' |
const bencodec = require('bencodec') |
By default, all strings will be parsed as buffers.
const data = bencodec.decode( 'd3:bar4:spam3:fooi42ee' );
// or
const data = bencodec.decode( Buffer.from('d3:bar4:spam3:fooi42ee') );
// data
{ bar: <Buffer 73 70 61 6d>, foo: 42 }
To convert buffers to strings add stringify
option.
const data = bencodec.decode( 'd3:bar4:spam3:fooi42ee', { stringify: true } );
// data
{ bar: 'spam', foo: 42 }
By default method encode will return buffer.
const data = bencodec.encode({ bar: 'spam', foo: 42 });
// <Buffer 64 33 ... 65 65>
const data = bencodec.encode({ bar: 'spam', foo: 42 }, { stringify: true });
// 'd3:bar4:spam3:fooi42ee'
npm test
This project is licensed under the MIT License - see the LICENSE file for details.