All Projects → nrepl → bencode

nrepl / bencode

Licence: EPL-1.0 license
A netstring and bencode implementation for Clojure.

Programming Languages

clojure
4091 projects

Projects that are alternatives of or similar to bencode

bencode online
Bencode & bdecode in your browser
Stars: ✭ 24 (-45.45%)
Mutual labels:  bencode
lein-nrepl
A simple Leininingen plugin to start nREPL 0.4+
Stars: ✭ 19 (-56.82%)
Mutual labels:  nrepl
bencode
PHP Bencode (BitTorrent) Encoder/Decoder
Stars: ✭ 19 (-56.82%)
Mutual labels:  bencode
spring-boost
Introduce Clojure and live-coding power to your Spring Boot application!
Stars: ✭ 23 (-47.73%)
Mutual labels:  nrepl
BencodeNET
.NET library for encoding/decoding bencode and reading/writing torrent files
Stars: ✭ 133 (+202.27%)
Mutual labels:  bencode
bencode
Bencode Input/Output Streams for Java
Stars: ✭ 49 (+11.36%)
Mutual labels:  bencode
iced-nrepl
nREPL middleware to support vim-iced.
Stars: ✭ 13 (-70.45%)
Mutual labels:  nrepl
php-bencode
C++ PHP extension which can boost the process of encoding and decoding of Bencode.
Stars: ✭ 16 (-63.64%)
Mutual labels:  bencode
torrentool
The tool to work with torrent files.
Stars: ✭ 96 (+118.18%)
Mutual labels:  bencode
bento
🍱 A fast, correct, pure-Elixir library for reading and writing Bencoded metainfo (.torrent) files.
Stars: ✭ 71 (+61.36%)
Mutual labels:  bencode
bencode.py
Simple bencode parser (for Python 2, Python 3 and PyPy)
Stars: ✭ 28 (-36.36%)
Mutual labels:  bencode
bencoder.pyx
A fast bencode implementation in Cython
Stars: ✭ 26 (-40.91%)
Mutual labels:  bencode
roll
Roll — backend for Clojure
Stars: ✭ 73 (+65.91%)
Mutual labels:  nrepl
Clojure-Sublimed
Clojure support for Sublime Text 4
Stars: ✭ 268 (+509.09%)
Mutual labels:  nrepl
nrepl-cljs
nrepl server implemented for bootstrap clojurescript
Stars: ✭ 37 (-15.91%)
Mutual labels:  nrepl

CircleCI Clojars Project cljdoc badge downloads badge

bencode

A netstring and bencode implementation for Clojure.

This particular implementation was extracted from nREPL, so it could be reused by other applications and be made ClojureScript-compatible as well.

There are other netstring/bencode libraries for Clojure out there, but this one has the distinct advantage that it's certainly going to work well with nREPL. ;-)

Motivation

In each and every application, which contacts peer processes via some communication channel, the handling of the communication channel is obviously a central part of the application. Unfortunately introduces handling of buffers of varying sizes often bugs in form of buffer overflows and similar.

A strong factor in this situation is of course the protocol which goes over the wire. Depending on its design it might be difficult to estimate the size of the input up front. This introduces more handling of message buffers to accomodate for inputs of varying sizes. This is particularly difficult in languages like C, where there is no bounds checking of array accesses and where errors might go unnoticed for considerable amount of time.

To address these issues D. Bernstein developed the so called netstrings. They are especially designed to allow easy construction of the message buffers, easy and robust parsing.

BitTorrent extended this to the bencode protocol which also includes ways to encode numbers and collections like lists or maps.

wire is based on these ideas.

Usage

Just add bencode as a dependency to your project and start hacking.

[nrepl/bencode "1.1.0"]

The API is documented in great detail here.

The main functions in the API are:

  • read-netsring
  • write-nestring
  • read-bencode
  • write-bencode

Here are some usage examples for each of the functions available:

  • read-netstring
(-> (.getBytes "13:Hello, World!," "UTF-8")
    ByteArrayInputStream.
    read-netstring
    (String. "UTF-8"))

=> Hello, World!
  • write-netstring
(-> (doto (ByteArrayOutputStream.)
       (write-netstring (.getBytes "Hello, World!" "UTF-8")))
    .toString)

=> "13:Hello, World!,"
  • read-bencode
(vec
  (map
    #(String. % "UTF-8")
    (-> (.getBytes "5:nrepl2:is7:awesomee" "UTF-8")
        ByteArrayInputStream.
        PushbackInputStream.
        read-bencode)))

=> ["nrepl" "is" "awesome"]
  • write-bencode
(-> (doto (ByteArrayOutputStream.)
     (write-bencode {:foo "bar"}))
   .toString)

=> "d3:foo3:bare"

Additionally, you can check this document to learn more about it's usage.

Use Cases

Obviously you can use this library whenever you need to deal with netstrings or bencode, but I assume that in practice most people will end up using it for building alternative Clojure nREPL clients or servers. babashka.nrepl is one notable user of the library.

There's also the potential to have the library support ClojureScript and ClojureCLR down the road, so it could be leveraged in even more contexts. Sky is the limit!

License

Copyright © 2018-2021 Meikel Brandmeyer, Bozhidar Batsov and nREPL contributors

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].