Coder Social home page Coder Social logo

node-bencode's Introduction


WebTorrent
WebTorrent

The streaming torrent client. For node.js and the web.

discord ci npm version npm downloads Standard - JavaScript Style Guide

Sponsored by    Socket - JavaScript open source supply chain security    Wormhole

WebTorrent is a streaming torrent client for node.js and the browser. YEP, THAT'S RIGHT. THE BROWSER. It's written completely in JavaScript – the language of the web – so the same code works in both runtimes.

In node.js, this module is a simple torrent client, using TCP and UDP to talk to other torrent clients.

In the browser, WebTorrent uses WebRTC (data channels) for peer-to-peer transport. It can be used without browser plugins, extensions, or installations. It's Just JavaScript™. Note: WebTorrent does not support UDP/TCP peers in browser.

Simply include the webtorrent.min.js script on your page to start fetching files over WebRTC using the BitTorrent protocol, or import WebTorrent from 'webtorrent' with browserify or webpack. See demo apps and code examples below.

jsdelivr download count

To make BitTorrent work over WebRTC (which is the only P2P transport that works on the web) we made some protocol changes. Therefore, a browser-based WebTorrent client or "web peer" can only connect to other clients that support WebTorrent/WebRTC.

To seed files to web peers, use a client that supports WebTorrent, e.g. WebTorrent Desktop, a desktop client with a familiar UI that can connect to web peers, webtorrent-hybrid, a command line program, or Instant.io, a website. Established torrent clients like Vuze have already added WebTorrent support so they can connect to both normal and web peers. We hope other clients will follow.

Network

Features

  • Torrent client for node.js & the browser (same npm package!)
  • Insanely fast
  • Download multiple torrents simultaneously, efficiently
  • Pure Javascript (no native dependencies)
  • Exposes files as streams
    • Fetches pieces from the network on-demand so seeking is supported (even before torrent is finished)
    • Seamlessly switches between sequential and rarest-first piece selection strategy
  • Supports advanced torrent client features
  • Comprehensive test suite (runs completely offline, so it's reliable and fast)
  • Check all the supported BEPs here

Browser/WebRTC environment features

  • WebRTC data channels for lightweight peer-to-peer communication with no plugins
  • No silos. WebTorrent is a P2P network for the entire web. WebTorrent clients running on one domain can connect to clients on any other domain.
  • Stream video torrents into a <video> tag (webm, mkv, mp4, ogv, mov, etc (AV1, H264, HEVC*, VP8, VP9, AAC, FLAC, MP3, OPUS, Vorbis, etc))
  • Supports Chrome, Firefox, Opera and Safari.

Install

To install WebTorrent for use in node or the browser with import WebTorrent from 'webtorrent', run:

npm install webtorrent

To install a webtorrent command line program, run:

npm install webtorrent-cli -g

To install a WebTorrent desktop application for Mac, Windows, or Linux, see WebTorrent Desktop.

Ways to help

Who is using WebTorrent today?

Lots of folks!

WebTorrent API Documentation

Read the full API Documentation.

Usage

WebTorrent is the first BitTorrent client that works in the browser, using open web standards (no plugins, just HTML5 and WebRTC)! It's easy to get started!

In the browser

Downloading a file is simple:
import WebTorrent from 'webtorrent'

const client = new WebTorrent()
const magnetURI = '...'

client.add(magnetURI, torrent => {
  // Got torrent metadata!
  console.log('Client is downloading:', torrent.infoHash)

  for (const file of torrent.files) {
    document.body.append(file.name)
  }
})
Seeding a file is simple, too:
import dragDrop from 'drag-drop'
import WebTorrent from 'webtorrent'

const client = new WebTorrent()

// When user drops files on the browser, create a new torrent and start seeding it!
dragDrop('body', files => {
  client.seed(files, torrent => {
    console.log('Client is seeding:', torrent.infoHash)
  })
})

There are more examples in docs/get-started.md.

Browserify

WebTorrent works great with browserify, an npm package that lets you use node-style require() to organize your browser code and load modules installed by npm (as seen in the previous examples).

Webpack

WebTorrent also works with webpack, another module bundler. However, webpack requires extra configuration which you can find in the webpack bundle config used by webtorrent.

Or, you can just use the pre-built version via import WebTorrent from 'webtorrent/dist/webtorrent.min.js' and skip the webpack configuration.

Script tag

WebTorrent is also available as a standalone script (webtorrent.min.js) which exposes WebTorrent on the window object, so it can be used with just a script tag:

<script type='module'>
  import WebTorrent from 'webtorrent.min.js'
</script>

The WebTorrent script is also hosted on fast, reliable CDN infrastructure (Cloudflare and MaxCDN) for easy inclusion on your site:

<script type='module'>
  import WebTorrent from 'https://esm.sh/webtorrent'
</script>
Chrome App

If you want to use WebTorrent in a Chrome App, you can include the following script:

<script type='module'>
  import WebTorrent from 'webtorrent.chromeapp.js'
</script>

Be sure to enable the chrome.sockets.udp and chrome.sockets.tcp permissions!

In Node.js

WebTorrent also works in node.js, using the same npm package! It's mad science!

NOTE: To connect to "web peers" (browsers) in addition to normal BitTorrent peers, use webtorrent-hybrid which includes WebRTC support for node.

As a command line app

WebTorrent is also available as a command line app. Here's how to use it:

$ npm install webtorrent-cli -g
$ webtorrent --help

To download a torrent:

$ webtorrent magnet_uri

To stream a torrent to a device like AirPlay or Chromecast, just pass a flag:

$ webtorrent magnet_uri --airplay

There are many supported streaming options:

--airplay               Apple TV
--chromecast            Chromecast
--mplayer               MPlayer
--mpv                   MPV
--omx [jack]            omx [default: hdmi]
--vlc                   VLC
--xbmc                  XBMC
--stdout                standard out [implies --quiet]

In addition to magnet uris, WebTorrent supports many ways to specify a torrent.

Talks about WebTorrent

Modules

Most of the active development is happening inside of small npm packages which are used by WebTorrent.

The Node Way™

"When applications are done well, they are just the really application-specific, brackish residue that can't be so easily abstracted away. All the nice, reusable components sublimate away onto github and npm where everybody can collaborate to advance the commons." — substack from "how I write modules"

node.js is shiny

Modules

These are the main modules that make up WebTorrent:

module tests version description
webtorrent torrent client (this module)
bittorrent-dht distributed hash table client
bittorrent-peerid identify client name/version
bittorrent-protocol bittorrent protocol stream
bittorrent-tracker bittorrent tracker server/client
bittorrent-lsd bittorrent local service discovery
create-torrent create .torrent files
magnet-uri parse magnet uris
parse-torrent parse torrent identifiers
torrent-discovery find peers via dht, tracker, and lsd
ut_metadata metadata for magnet uris (protocol extension)
ut_pex peer discovery (protocol extension)

Enable debug logs

In node, enable debug logs by setting the DEBUG environment variable to the name of the module you want to debug (e.g. bittorrent-protocol, or * to print all logs).

DEBUG=* webtorrent

In the browser, enable debug logs by running this in the developer console:

localStorage.setItem('debug', '*')

Disable by running this:

localStorage.removeItem('debug')

License

MIT. Copyright (c) Feross Aboukhadijeh and WebTorrent, LLC.

node-bencode's People

Contributors

alxhotel avatar ckcr4lyf avatar deoxxa avatar diegorbaquero avatar feross avatar fossabot avatar greenkeeperio-bot avatar jhermsmeier avatar jimmywarting avatar kaelar avatar leask avatar nazar-pc avatar ngotchac avatar notslang avatar pwmckenna avatar renovate[bot] avatar semantic-release-bot avatar thaunknown avatar themasch avatar zunsthy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

node-bencode's Issues

Discussion: de-/encoding floats

Hi everybody,

node-bencode seems to be the only library that allows decoding floats from bencode.
Thats the only point (i know) where we moved apart form the specs. Other librarys encode floats like we do but don't decode them. Thats a no-go.
So what do we do?
a) keep decoding/encoding floats even when the specs only covers integers.
b) drop support for floats. This would include dropping encode & decode support.

If we choose (b), how should we handle floats while encoding? Cast them to ints?

i'm a fan of a..

change test framework from mocha to tape

I'd suggest, in the progress to support browserify, that we switch to tape as a test framework to be able to use the same test suite in browsers & node. This allows us to use something like testling-ci & travis with the same test suite.

Are there any concerns against this step? If there aren't I'll rework the tests to work with tape & setup testling-ci.

Error: not a number: buffer[42910] = 18

Reference: https://github.com/caryyu/bteye/blob/bencode-experiment/test/bencode.test.js

Exception

/Users/caryyu/Desktop/bteye/node_modules/bencode/lib/decode.js:44
    throw new Error('not a number: buffer[' + i + '] = ' + num)
    ^

Error: not a number: buffer[42910] = 18
    at getIntFromBuffer (/Users/caryyu/Desktop/bteye/node_modules/bencode/lib/decode.js:44:11)
    at Function.decode.buffer (/Users/caryyu/Desktop/bteye/node_modules/bencode/lib/decode.js:160:16)
    at Function.decode.dictionary (/Users/caryyu/Desktop/bteye/node_modules/bencode/lib/decode.js:127:17)
    at Function.decode.next (/Users/caryyu/Desktop/bteye/node_modules/bencode/lib/decode.js:94:21)
    at Function.decode.dictionary (/Users/caryyu/Desktop/bteye/node_modules/bencode/lib/decode.js:127:36)
    at Function.decode.next (/Users/caryyu/Desktop/bteye/node_modules/bencode/lib/decode.js:94:21)
    at Object.decode (/Users/caryyu/Desktop/bteye/node_modules/bencode/lib/decode.js:83:17)
    at Request._callback (/Users/caryyu/Desktop/bteye/test/bencode.test.js:10:24)
    at Request.self.callback (/Users/caryyu/Desktop/bteye/node_modules/request/request.js:185:22)
    at Request.emit (events.js:314:20)

Reproduce

Git-check out the referred the repo above, and then execute the following commands

npm i
node test/bencode.test.js

Consider support typescript?

What version of this package are you using?
2.0.2

What problem do you want to solve?
support ide completion

What do you think is the correct solution to this problem?
add d d.ts file

Are you willing to submit a pull request to implement this change?

No capable

Improve decoding performance

There are still at least 6,000 ops/sec to be gained:

bencode->decode( Buffer )     x  12,468 ops/sec  ±0.78% (64 runs sampled)
bencoding->decode( Buffer )   x  18,556 ops/sec  ±0.57% (62 runs sampled)
dht-bencode->decode( Buffer ) x  18,344 ops/sec  ±0.35% (62 runs sampled)
bncode->decode( Buffer )      x  770 ops/sec     ±1.39% (58 runs sampled)

`node-bencode` can produce dictionary entries with duplicate keys.

Bug

node-bencode can produce dictionary entries with duplicate keys.


node-bencode assumes that binary string keys made out of unique Javascript string keys are unique as well, which is false.

encode.string = function (buffers, data) {
buffers.push(text2arr(text2arr(data).byteLength + ':' + data))
}

https://github.com/ThaUnknown/uint8-util/blob/149c44c010b3ad17a7904c4266545bbca1fd4403/_node.js#L13

 encode.string = function (buffers, data) { 
   buffers.push(text2arr(text2arr(data).byteLength + ':' + data)) 
 } 
export const text2arr = str => new Uint8Array(Buffer.from(str, 'utf8'))

Proof-of-concept

For example, let node-bencode try encoding {"\uD800": 1, "\uDFFF": 2}. It’ll produce dictionary entries with the duplicate key, "3:\xEF\xBF\xBD".

const lone_surrogates = "\uD800\uDFFF";
// Lone (“unmatched”) UTF-16 surrogates. Invalid in UTF-16.

const a = Buffer.from(lone_surrogates[0], "UTF-8");
const b = Buffer.from(lone_surrogates[1], "UTF-8");
// Decoding the Javascript strings in UTF-16 and encoding them into UTF-8.

console.log(a, a.toString(), b, b.toString());
//  Since those Javascript strings are invalid in UTF-16,
// those lone surrogates are decoded
// into `REPLACEMENT CHARACTER`s (U+FFFD)
// and subsequently encoded into `<Buffer ef bf bd>`.
// Meaning,

console.log(a.equals(b));
// is true, when (lone_surrogates[0] === lone_surrogates[1]) is false.

Implement stream decoding

  • Implement Encoder
  • Implement Decoder
  • switch from mocha to tape on streams branch
  • write tests for encoder/decoder streams

browserify not work

Because of browser's polyfill, Buffer is object and will be considered as dictionary whenbencode.encode.

example:

const data = { error: Uint8Array.from([101, 114, 114, 111, 114]) }
const result = bencode.encode(data)
console.log(result)

The expected result is d5:error5:errore, but the real result is d5:errordi0e:i101ei1e:i114e....

  switch (typeof data) {
    case 'string':
      encode.buffer(buffers, data)
      break
    case 'number':
      encode.number(buffers, data)
      break
    case 'object':
      data.constructor === Array
        ? encode.list(buffers, data)
        : encode.dict(buffers, data)
      break
    case 'boolean':
      encode.number(buffers, data ? 1 : 0)
      break
  }

typeof will return 'object' for Buffer, if browser's polyfill works.

decode integer NaN

The current code fails to decode integers.
Test case:

var bencode = require( 'bencode' )
var data = {
string: 'Hello World',
integer: 12345,
dict: {
key: 'This is a string within a dictionary'
},
list: [ 1, 2, 3, 4, 'string', 5, {} ]
}
var result = bencode.encode( data )
bencode.decode ( result , 'utf8')
{ string: 'Hello World',
integer: NaN,
dict: { key: 'This is a string within a dictionary' },
list:
[ NaN,
NaN,
NaN,
NaN,
'string',
NaN,
{} ] }

The function decode.integer beginning on line 146 should be:

decode.integer = function() {

var end = decode.find( 0x65 )
var number = decode.data.toString (decode.encoding, decode.position + 1, end )

decode.position += end + 1 - decode.position

return Number (number)
}

dictionary not sorted

According to the BitTorrent specification dictionaries need to be sorted:

"Dictionaries are encoded as a 'd' followed by a list of alternating keys and their corresponding values followed by an 'e'. For example, d3:cow3:moo4:spam4:eggse corresponds to {'cow': 'moo', 'spam': 'eggs'} and d4:spaml1:a1:bee corresponds to {'spam': ['a', 'b']}. Keys must be strings and appear in sorted order (sorted as raw strings, not alphanumerics)."

However, node-bencode simply converts object properties as found. Here is the test case:

var data = {
string: 'Hello World',
integer: 12345,
}
var result = bencode.encode( data )

produces:
d6:string11:Hello World7:integeri12345ee

should be:
d7:integeri12345e6:string11:Hello Worlde

Release v1.0.0

This library is stable and depended-upon by lots of packages. Want to just call it 1.0.0?

Sidenote: semver is weird for versions below 1.0.0, so the new recommendation is to start packages off at 1.0.0 instead of 0.0.0.

add example code to decode torrent file

the readme has examples only to decode strings, but not files

import fs from 'fs'
import bencode from 'bencode'

var buffer = fs.readFileSync('input.torrent')
var objectUtf8 = bencode.decode(buffer, 'utf8')
console.log(objectUtf8)

decode without using the entire input

Not sure if this is a bug or not but I'd like to discuss how decode should behave when we feed multiple parts.

Example:

bencode.decode('i123ei123e', 'utf8'); // 123

The input has more data than that. When we use the decode function, it silently returns just the first decoded part. Is this intended?

I saw some cases where a single buffer they wanted to decode had multiple bencoded parts (it was some data received from the network they had no control over). And when they used this lib, it was silently missing parts. So they had to implement some logic to check if the decoded result was everything the input had or not. They managed to get everything working, but I'm wondering if the lib should give a heads up (exception or whatever) when this happens.

Incorrect encoding of Buffer

This code produces invald result on encoding binary data (like info/pieces section of torrent file) as default type casting to string is assuming utf-8 encoding
if( data instanceof Buffer )
return data.length + ':' + data
The correct way is to specify 'binary' encoding explicitly. I.e.
if( data instanceof Buffer )
return data.length + ':' + data.toString('binary')

Can't get exact binary key values

This is the content of http://open.nyaatorrents.info:6544/scrape?info_hash=%91%ee%82%12%97%98%57%a5%f4%06%9e%c7%b2%8b%eb%47%cf%c1%db%f1 right now. It's the scrape output from nyaatorrents for the torrent with the hash 91ee8212979857a5f4069ec7b28beb47cfc1dbf1.

ZDU6ZmlsZXNkMjA6ke6CEpeYV6X0Bp7HsovrR8/B2/FkODpjb21wbGV0ZWkyZTEwOmRvd25sb2FkZWRpMTBlMTA6aW5jb21wbGV0ZWkyNzVlZWVl

Here it is in a script:

var bencode = require("./");

var data = Buffer("ZDU6ZmlsZXNkMjA6ke6CEpeYV6X0Bp7HsovrR8/B2/FkODpjb21wbGV0ZWkyZTEwOmRvd25sb2FkZWRpMTBlMTA6aW5jb21wbGV0ZWkyNzVlZWVl", "base64");

var obj = bencode.decode(data);

console.log(obj);

console.log(Object.keys(obj.files)[0]);

console.log(Buffer(object.keys(obj.files)[0]).toString("hex"));

As you'd expect, the last console.log outputs gibberish. This is due to JavaScript strings not playing too nicely with binary data. The only way around this is to avoid storing objects as objects (and thus keys as strings), or to encode the keys differently, as far as I can see.

Usage instructions are incorrect for v3.0.0

Since the package is now ESM-only, we need to import bencode. However currently the docs incorrectly state the way to use it is:

var bencode = require( 'bencode' )

What version of this package are you using?

v3.0.0

What operating system, Node.js, and npm version?

OS: Arch Linux x86_64, Kernel: 6.0.10-arch2-1
Node: v16.10.0
NPM: 7.24.0

What happened?

file:///tmp/bcode/main.js:1
const bencode = require('bencode');
                ^

ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and '/tmp/bcode/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at file:///tmp/bcode/main.js:1:17
    at ModuleJob.run (node:internal/modules/esm/module_job:183:25)
    at async Loader.import (node:internal/modules/esm/loader:178:24)
    at async Object.loadESM (node:internal/process/esm_loader:68:5)
    at async handleMainPromise (node:internal/modules/run_main:63:12)

What did you expect to happen?

Import would work

Are you willing to submit a pull request to fix this bug?

Yes

Error: `Buffer.from` is not a function

What version of this package are you using?
2.0.2

What operating system, Node.js, and npm version?
OS: KDE neon 5.23 (Ubuntu 20.04)
Node version: v17.6.0
npm version: 8.5.1

What happened?
When running npm start, the following happened.

A JavaScript error occurred in the main process
Uncaught Exception:
TypeError: Buffer.from is not a function
    at Object.<anonymous> (/home/zeankundev/nuTorrent/node_modules/bencode/lib/encode.js:49:22)
    at Module._compile (module.js:428:26)
    at Object.Module._extensions..js (module.js:446:10)
    at Module.load (module.js:353:32)
    at Function.Module._load (module.js:308:12)
    at Module.require (module.js:363:17)
    at require (module.js:382:17)
    at Object.<anonymous> (/home/zeankundev/nuTorrent/node_modules/bencode/lib/index.js:3:18)
    at Module._compile (module.js:428:26)
    at Object.Module._extensions..js (module.js:446:10)

What did you expect to happen?
Launching the application to debug

Are you willing to submit a pull request to fix this bug?
No

Fix encoding of non-ascii Strings

have a look on the failing testcase.

it('should be able to encode a unicode string', function() {
    assert.equal(bencode.encode("ö±sdf"), '7:ö±sdf');
    assert.equal(bencode.encode(new Buffer("ö±sdf")), '7:ö±sdf');
 })

The first encode creates '5:ö±sdf' instead of '7:ö±sdf', multibytes are counted incorrectly

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/ci.yml
  • actions/checkout v3
  • actions/setup-node v3
.github/workflows/release.yml
  • actions/checkout v3
  • actions/setup-node v3
  • actions/cache v3
npm
package.json
  • uint8-util ^2.2.2
  • @webtorrent/semantic-release-config 1.0.10
  • browserify ^17.0.0
  • nanobench 3.0.0
  • semantic-release 21.1.2
  • standard 17.1.0
  • tap-spec 5.0.0
  • tape 5.8.1

  • Check this box to trigger a request for Renovate to run again on this repository

Integers should have no size limitation.

Bug: it fails to satisfy the specification.

BEP 52 — The BitTorrent protocol specification version 2

Integers have no size limitation.

Currently, node-bencode allows decoded integer data to be corrupted. It should either use bigints or throw on a long integer input.

function getIntFromBuffer (buffer, start, end) {
let sum = 0
let sign = 1
for (let i = start; i < end; i++) {
const num = buffer[i]
if (num < 58 && num >= 48) {
sum = sum * 10 + (num - 48)
continue
}

yourdecode("i9007199254740991e") === yourdecode("i9007199254740992e")

MAX_INT64 issue

According to the bep_0044, it should be possible to set an integer to MAX_INT64, (i.e. 0x7fffffffffffffff). libtorrent successfully bencode this number, although node-bencode doesn't allow this and I receive this error :

WARNING: Possible data corruption detected with value 9223372036854776000: Bencoding only defines support for integers, value was converted to 0

Which is fairly normal as my number is too big.

At the moment I hacked the number encoding case to push a hand crafted buffer when I detect an overflow, but it's not pretty at all... I don't see any easy solution, apart from using a library dedicated to handling large numbers, like node-int64.

Any thoughts ?
Cheers !

decode.bytes does not adhere to abstract-encoding spec

What version of this package are you using?
Latest

What operating system, Node.js, and npm version?
Arch Linux, latest Node LTS (v20)

What happened?
decode.bytes returns the length of the entire input data after decoding

What did you expect to happen?
It should've returned only the number of bytes consumed.

Are you willing to submit a pull request to fix this bug?
Yes

It sorts dictionary entries incorrectly.

Bug

// fix for issue #13 - sorted dicts
const keys = Object.keys(data).sort()

 // fix for issue #13 - sorted dicts 
 const keys = Object.keys(data).sort() 

This is not the correct way of sorting dictionary entries.


When you say “strings” in the context of Bencoding, you mean “binary strings,” or more specifically, “8-bit byte sequences.”

BEP 52 — The BitTorrent protocol specification version 2

Note that, in the context of bencoding, strings, including dictionary keys, are arbitrary byte sequences (uint8_t[]).

And Array.prototype.sort compares 16-bit units by default.

If compareFn is not supplied, all non-undefined array elements are sorted by converting them to strings and comparing strings in UTF-16 code units order.

The simple .sort() results in a different order (sorted_in_utf16) than the correct one (sorted_in_utf8). Observe:

const A = String.fromCodePoint(0xFF61);
const B = String.fromCodePoint(0x10002);
const sorted_in_utf8 = [A, B].sort((a, b) => Buffer.compare(Buffer.from(a), Buffer.from(b))); // [A, B]
const sorted_in_utf16 = [A, B].sort(); // [B, A]

Related issues

Decode breaks for compact format

Hi Guys,

When a tracker response contains peer list in the compact format (BEP23), decode breaks giving the following error : Invalid data: Missing delimiter ":" [0x3a]

For example decode fails on the response of this request .

Is this supported, or am I doing something wrong ?

Version : 0.10.0
OS : Ubuntu 14.04.3 LTS

Buffer.isBuffer is not a function erro in angular6 while using bencode

I am using bencode in aAngular6 and it is throwing an error Buffer.isBuffer is not a function

Here is my funciton in component.ts and is giving an error at this line const decodedData = _.decode(e.data, null, null, 'utf8');

M2M(url, callbacks) { const self = this; self.url = url; self.ws = null; if (!callbacks) { callbacks = {}; } self.listeners = {}; const identity = null; const packets = { 'null': 0, 'request_join': 1, 'request_identify': 2, 'welcome': 3, 'log': 4, 'request_send': 5, 'route': 6, 'ping': 7, 'pong': 8, 'set_identity': 9, 'request_open': 10, 'request_close': 11, 'request_close_all': 12, 'keep_alive': 13, 'notify_open': 14, 'request_login': 15, 'instruction': 16, 'notify_close': 19, 'request_leave': 20, 'route_control': 21, 'request_send_control': 22 }; const packets_lookup = {}; for (const key in packets) { packets_lookup[packets[key]] = key; } self.callback = function (name, data) { if (callbacks[name] !== undefined) { callbacks[name](data); } } ; self.connect = function () { self.identity = null; const ws = new WebSocket(self.url); ws.binaryType = 'arraybuffer'; self.ws = ws; ws.onmessage = function (e) { // console.log(e) const decodedData = _.decode(e.data, null, null, 'utf8'); self.onPacket(decodedData); } ; ws.onopen = function (e) { self.callback('connect', e); // self.sendPacket('request_join'); setInterval(self.sendKeepAlive, 15 * 1000); } ; ws.onclose = function () { self.callback('close', {}); } ; ws.onerror = function (error) { self.callback('error', { error: error }); } ; } ; self.sendKeepAlive = function () { if (self.ws && self.identity) { self.sendPacket('keep_alive'); } } ; self.close = function () { if (self.ws) { // console.log('leaving') self.sendPacket('request_leave'); self.ws.close(); } self.ws = null; } ; const handlers = { 'ping': function (packet_type, packet_body) { self.sendPacket('pong', [self.decode_utf8(packet_body[0])]); }, 'route': function (packet_type, packet_body) { const channel_no = packet_body[0]; const packet_data = packet_body[1]; const listeners = self.listeners[channel_no]; if (listeners) { for (let i = 0; i < listeners.length; i++) { try { listeners[i](channel_no, packet_data); } catch (err) { if (console) { // console.log(err); } } } } self.callback('route', { 'channel': channel_no, 'payload': packet_data }); }, 'welcome': function () { self.callback('onready', {}); }, 'set_identity': function (packet_type, packet_body) { const identity = self.decode_utf8(packet_body[0]); self.identity = identity; self.callback('identity', { 'identity': identity }); }, 'log': function (packet_type, packet_body) { // if (console) { // console.log(self.decode_utf8(packet_body[0])); // } }, 'notify_open': function (packet_type, packet_body) { self.callback('channel_open', { port: packet_body[0] }); }, 'notify_close': function (packet_type, packet_body) { self.callback('channel_close', { port: packet_body[0] }); }, 'request_send': function (packet_type, packet_body) { self.callback('request_send', { body: packet_body }); }, 'request_close': function (packet_type, packet_body) { self.callback('request_close', { body: packet_body }); }, 'request_identify': function (packet_type, packet_body) { self.callback('request_identity', { body: packet_body }); } }; function makePacket(packet_type_number, packet_body) { let packet; if (packet_body !== undefined) { packet = [packet_type_number]; packet.push.apply(packet, packet_body); } else { packet = [packet_type_number]; } const packet_data = _.encode(packet); return packet_data; } self.on = function (name, callback) { callbacks[name] = callback; return this; } ; self.sendPacket = function (packet_type, packet_body) { const packet_type_number = packets[packet_type]; const packet = makePacket(packet_type_number, packet_body); if (self.ws.readyState === 1) { self.ws.send(packet); } } ; self.writeChannel = function (channel_no, payload, key) { self.sendPacket('route', [channel_no, payload, key]); } ; self.writeControl = function (channel_no, payload) { self.sendPacket('request_send_control', [channel_no, payload]); } ; self.onPacket = function (packet) { if (packet) { const packet_type = packet[0]; const packet_body = packet.slice(1); const handler = handlers[packets_lookup[packet_type]]; if (handler === undefined) { return; } handler(packet_type, packet_body); } } ; self.addListener = function (channel_no, callback) { if (self.listeners[channel_no] === undefined) { self.listeners[channel_no] = []; } self.listeners[channel_no].push(callback); } ; return self; }

Error: Invalid data: Missing delimiter ":" [0x3a]

Hi,

In the following code:

const crypto = require('crypto');
const bencode = require('bencode');

console.log(bencode.decode(bencode.encode({a: 'false'})));
console.log(bencode.decode(bencode.encode({a: false})))

I'm getting:

Error: Invalid data: Missing delimiter ":" [0x3a]
    at Function.decode.find (/home/tomasz/dev/git/payment-processor/node_modules/bencode/lib/decode.js:50:9)
    at Function.decode.bytes (/home/tomasz/dev/git/payment-processor/node_modules/bencode/lib/decode.js:103:23)
    at Function.decode.next (/home/tomasz/dev/git/payment-processor/node_modules/bencode/lib/decode.js:33:30)
    at Function.decode.dictionary (/home/tomasz/dev/git/payment-processor/node_modules/bencode/lib/decode.js:65:43)
    at Function.decode.next (/home/tomasz/dev/git/payment-processor/node_modules/bencode/lib/decode.js:30:30)
    at Object.decode (/home/tomasz/dev/git/payment-processor/node_modules/bencode/lib/decode.js:19:17)
    at Object.<anonymous> (/home/tomasz/dev/git/payment-processor/test/sample.js:5:21)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)

Any object containing a boolean as a value on any key causes decode to fail.

Any plan for ESM support?

Currently trying to use in typescript with ESM first focus, it is very messy. My import looks like:

import * as bencode from 'bencode';

And I need to use ts-ignore since the types (admittedly from @types/bencode) seem wrong:

    //@ts-ignore
    const parsed = bencode.default.decode(torrentFile, 'utf8');

I am currently in process of forking this lib and rewriting exports to make it cleaner to use. Wondering if you guys would be into CJS+ESM , and types as part of the lib as well.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.