11[ ![ build status] ( https://secure.travis-ci.org/a2800276/bncode.png )] ( http://travis-ci.org/a2800276/bncode )
2- # bencoding for JS (node.js)
2+ [ ![ JSR] ( https://jsr.io/badges/@a2800276/bncode )] ( https://jsr.io/@a2800276/bncode )
3+ [ ![ JSR Score] ( https://jsr.io/badges/@a2800276/bncode/score )] ( https://jsr.io/@a2800276/bncode )
34
5+ # bncode
46
5- This is a small library to encode and decode bencoded (bittorrent) stuff.
6- Bencoding is specified [ here] ( http://www.bittorrent.org/beps/bep_0003.html ) .
7+ A BitTorrent bencoding and decoding library for Node.js, Deno, and Bun.
78
9+ Bencoding is the encoding format used by BitTorrent, specified in [ BEP
10+ 3] ( http://www.bittorrent.org/beps/bep_0003.html ) .
811
9- ## Get & Install
12+ ## Features
1013
11- github repository is [ here] ( https://github.com/a2800276/bncode )
14+ - Works in Node.js, Deno, and Bun
15+ - TypeScript definitions included
16+ - Zero dependencies
17+ - Single file implementation
1218
13- Installable via npm (npm package name is ** bncode ** , note spelling!):
19+ ### Installation
1420
15- npm install bncode
21+ ``` bash
22+ npm install bncode
23+ ```
1624
25+ ### ## Usage
1726
18- ## Encoding
27+ ``` javascript
28+ import { encode , decode } from ' bncode'
1929
20- Encoding works as follows:
21-
22- var benc = require("bncode"),
23- exmp = {}
24-
25- exmp.bla = "blup"
26- exmp.foo = "bar"
27- exmp.one = 1
28- exmp.woah = {}
29- exmp.woah.arr = []
30- exmp.woah.arr.push(1)
31- exmp.woah.arr.push(2)
32- exmp.woah.arr.push(3)
33- exmp.str = new Buffer("Buffers work too")
34-
35- var bencBuffer = benc.encode(exmp)
36-
37- // d3:bla4:blup3:foo3:bar3:onei1e4:woahd3:arr \
38- // li1ei2ei3eee3:str16:Buffers work tooe
30+ const exmp = {
31+ bla: ' blup' ,
32+ foo: ' bar' ,
33+ one: 1 ,
34+ woah: {
35+ arr: [1 , 2 , 3 ]
36+ },
37+ str: Buffer .from (' Buffers work too' )
38+ }
3939
40+ const bencBuffer = encode (exmp)
4041
42+ // d3:bla4:blup3:foo3:bar3:onei1e4:woahd3:arr \
43+ // li1ei2ei3eee3:str16:Buffers work tooe
44+ ```
4145
4246## Decoding
4347
44- Decoding will work progressively, e.g. if you're receiving partial
48+ Decoding works progressively, e.g., if you're receiving partial
4549bencoded strings on the network:
4650
47- var benc = require("bncode"),
48- buf = null
49-
50- decoder = new benc.decoder()
51- while (buf = receiveData()) {
52- decoder.decode(buf)
53- }
54-
55- log(decoder.result())
51+ ``` javascript
52+ const bncode = require (' bncode' )
53+ let buf = null
54+
55+ const decoder = new bncode.decoder ()
56+ while (buf = receiveData ()) {
57+ decoder .decode (buf)
58+ }
59+
60+ console .log (decoder .result ())
61+ ```
5662
63+ Or "all in one":
5764
58- Or "all in one"
65+ ``` javascript
66+ const bncode = require (' bncode' )
67+ const buf = getBuffer ()
68+ const dec = bncode .decode (buf)
5969
60- var benc = require("bncode"),
61- buf = getBuffer(),
62- dec = benc.decode(buf)
63-
64- log(dec.bla)
70+ console .log (dec .bla )
71+ ```
6572
73+ ### String Handling
6674
6775There are some subtleties concerning bencoded strings. These are
6876decoded as Buffer objects because they are just strings of raw bytes
69- and as such would wreak havoc with multi byte strings in javascript .
77+ and as such would wreak havoc with multi- byte strings in JavaScript .
7078
7179The exception to this is strings appearing as keys in bencoded
72- dicts . These are decoded as Javascript Strings, as they should always
73- be strings of (ascii ) characters and if they weren't decoded as JS
74- Strings, dict's couldn't be mapped to Javascript objects.
80+ dictionaries . These are decoded as JavaScript Strings, as they should always
81+ be strings of (ASCII ) characters. If they weren't decoded as JS
82+ Strings, dictionaries couldn't be mapped to JavaScript objects.
7583
84+ ## Mapping bencoding to JavaScript
7685
77- ## Mapping bencoding to Javascript
78-
79-
8086 +----------------------------------------------------+
8187 | | |
82- | Bencoded | Javascript |
88+ | Bencoded | JavaScript |
8389 |====================================================|
84- | Strings | node Buffers, unless they are |
85- | | Dictionary keys, in which case |
86- | | they become Javascript Strings |
90+ | Strings | Node Buffers, unless they are |
91+ | | dictionary keys, in which case |
92+ | | they become JavaScript Strings |
8793 |----------------+-----------------------------------|
8894 | Integers | Number |
8995 |----------------+-----------------------------------|
@@ -93,30 +99,59 @@ Strings, dict's couldn't be mapped to Javascript objects.
9399 | | |
94100 +----------------------------------------------------+
95101
102+ ## Mapping JavaScript to bencoding
96103
97- ## Mapping Javascript to bencoding
98-
99- The code makes a best effort to encode Javascript to bencoding. If you stick to basic
104+ The code makes a best effort to encode JavaScript to bencoding. If you stick to basic
100105types (Arrays, Objects with String keys and basic values, Strings, Buffers and Numbers)
101- you shouldn't encounter suprises . Expect surprises (mainly not being able to round-trip
102- encode/decode) if you encode fancy data- types.
106+ you shouldn't encounter surprises . Expect surprises (mainly not being able to round-trip
107+ encode/decode) if you encode fancy data types.
103108
109+ ## Stream API
104110
105- ## Author
111+ A transform stream is also available:
112+
113+ ``` javascript
114+ const bncode = require (' bncode' )
115+ const fs = require (' fs' )
116+
117+ fs .createReadStream (' file.torrent' )
118+ .pipe (new bncode.Stream ())
119+ .on (' data' , (data ) => {
120+ console .log (data)
121+ })
122+ ```
123+
124+ ## API
125+
126+ ### ` bncode.encode(obj) `
106127
107- bncode was written by Tim Becker (tim.becker@kuriositaet.de ) I can be reached via
108- email or (preferably) submit a bug to the github repository.
128+ Encodes a JavaScript object into a bencoded Buffer.
109129
130+ ### ` bncode.decode(buffer, [encoding]) `
131+
132+ Decodes a bencoded buffer into a JavaScript object.
133+
134+ ### ` new bncode.decoder() `
135+
136+ Creates a progressive decoder that can handle partial data.
137+
138+ ### ` new bncode.Stream([options]) `
139+
140+ Creates a transform stream for decoding bencoded data.
141+
142+ ## Author
143+
144+ bncode was written by Tim Becker (tim.becker@kuriositaet.de ). I can be reached via
145+ email or (preferably) submit a bug to the GitHub repository.
110146
111147## Thanks
112148
113- * Roly Fentanes (fent) for bug reports.
149+ * Roly Fentanes (fent) for bug reports
114150* Clark Fischer (clarkf)
115- * The fine folks at Travis.
151+ * The fine folks at Travis
116152* Patrick Williams
117153* Feross Aboukhadijeh
118154
119-
120155## License
121156
122157MIT, see ` LICENSE `
0 commit comments