Coder Social home page Coder Social logo

bcurl's Introduction

bcurl

A minimal web client.

Usage

const bcurl = require('bcurl');

const client = bcurl.client('http://localhost:8080');
const socket = await client.connect();

socket.send('hello', 'world');

// Rest
const json = await client.get('/foobar');
console.log(json);

// JSON-RPC
const json = await client.execute('/', 'method', {});

Contribution and License Agreement

If you contribute code to this project, you are implicitly allowing your code to be distributed under the MIT license. You are also implicitly verifying that all code is your original work. </legalese>

License

  • Copyright (c) 2017, Christopher Jeffrey (MIT License).

See LICENSE for more info.

bcurl's People

Contributors

blusyn avatar chjj avatar nodech avatar pinheadmz avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bcurl's Issues

better handling of json.error

This issue is similar to bcoin-org/brq#7 in that I'm getting unexpected behavior when using bcurl for APIs besides bcoin.

See here:
https://github.com/bcoin-org/bcurl/blob/master/lib/client.js#L211-L217

This gets broken by Kraken's API, which is already frustrating for a few other reasons, but one of them is this -- the API always returns an error property in the JSON:

https://api.kraken.com/0/public/Ticker?pair=XBTUSD
-->

{"error":[],"result":{"XXBTZUSD":{"a":["3536.10000","2","2.000"],"b":["3535.30000","1","1.000"],"c":["3531.80000","1.58333841"],"v":["8259.75520827","9590.27330937"],"p":["3510.71489","3507.83774"],"t":[12317,14569],"l":["3433.90000","3433.90000"],"h":["3579.80000","3579.80000"],"o":"3502.00000"}}}

Even though the error array is empty, bcurl detects that the property is there and throws without returning the rest of the object.

UnhandledPromiseRejectionWarning

If a request call in bcurl fails, the error is not properly caught, resulting in UnhandledPromiseRejectionWarning.

Simple test case:

const {Client} = require('bcurl');
(new Client()).get('/');

Result:

(node:13878) UnhandledPromiseRejectionWarning: Error: Bad response (wrong content-type).
    at Client.request (<node_modules>/bcurl/lib/client.js:194:13)
    at process._tickCallback (internal/process/next_tick.js:178:7)
(node:13878) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:13878) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Couldn't find a simple solution that didn't require a ton of try/catch blocks around every request. Perhaps @chjj has a simpler solution.

This was discovered while using bclient, which doesn't properly pass request failures back to the original caller. Possible bclient may also need to be updated to properly handle request errors.

request path

These two lines will concatenate this.path with endpoint.

Lines:

path: this.path + endpoint,

path: this.path + endpoint,

By default this.path is / and endpoint should always start with / (/wallet/...)
meaning actual request path will be POST //wallet/...

I noticed this when I tried to use bclient with Bitcoin Core for RPC calls. Because
bclient will create POST // HTTP instead of POST / HTTP bitcoin core will return 404.

We don't notice issue because bweb will actually normalize paths on the backend and correctly assume / path is request path.

Loosely related to: bcoin-org/brq#4

Merge with brq

This library is pretty small and could be merged with brq (also small) to reduce overhead and improve maintenance.

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.