Giter VIP home page Giter VIP logo

wreck's Introduction

@hapi/wreck

HTTP client utilities.

wreck is part of the hapi ecosystem and was designed to work seamlessly with the hapi web framework and its other components (but works great on its own or with other frameworks). If you are using a different web framework and find this module useful, check out hapi โ€“ they work even better together.

Visit the hapi.dev Developer Portal for tutorials, documentation, and support

Useful resources

wreck's People

Contributors

03byron avatar adrivanhoudt avatar arb avatar augnin avatar bodasia avatar cjihrig avatar delaguilaluis avatar devinivy avatar dkavassy avatar dominykas avatar emrealparslan93 avatar geek avatar ggoodman avatar hueniverse avatar jarrodyellets avatar kanongil avatar kidtronnix avatar kpdecker avatar lloydbenson avatar marsup avatar miksago avatar musinux avatar nargonath avatar nlf avatar sergioramos avatar spanditcaa avatar tlivings avatar torsph avatar totherik avatar westyler 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  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

wreck's Issues

Add logging to req/res details for debug mode

To make it easier to debug issues, we should add a log option for req/res details, along with response times. This should be logged to stdout or if the debug environment variable indicates, to a file.

How to handle HTTP_PROXY with Wreck

We need to pass through proxy for our http(s) calls.

request npm use by default envrionment variables HTTP_PROXY, HTTPS_PROXY, NO_PROXY
Is there a way to configure that with wreck ?

At the moment we use with success global-tunnel npm but he did'nt provide the no_proxy feature.

Payload problems when moving from Nipple to Wreck

We recently moved from Nipple to Wreck and now we're having problems when the function read returns the payload. It gives us a buffer, so when we try

Wreck.read(res,null,function(err,payload){
JSON.PARSE(payload);
})

The parse throws an error:

Debug: hapi, internal, implementation, error
SyntaxError: Unexpected token <
at Object.parse (native)
at setResponse (/opt/xoom/apps/gateway/current/lib/tools.js:74:52)
at /opt/xoom/apps/gateway/current/lib/tools.js:168:7
at finish (/opt/xoom/apps/gateway/current/node_modules/wreck/lib/index.js:218:20)
at wrapped (/opt/xoom/apps/gateway/current/node_modules/hoek/lib/index.js:560:20)
at onReaderFinish (/opt/xoom/apps/gateway/current/node_modules/wreck/lib/index.js:271:16)
at g (events.js:180:16)
at emit (events.js:117:20)
at finishMaybe (_stream_writable.js:359:12)
at endWritable (_stream_writable.js:366:3)

When we use toReadableStream console.log(Wreck.toReadableStream(payload, 'utf8').read()); we get:

<Buffer 3c 21 44 4f 43 54 59 50 45 20 68 74 6d 6c 3e 0a 3c 68 74 6d 6c 20 6c 61 6e 67 3d 22 65 6e 22 3e 0a 3c 68 65 61 64 3e 0a 20 20 3c 6d 65 74 61 20 63 68 61 ...>

Longpolling

Is there a way to do long polling with wreck?

Usage of post

Could you please add example of usage of Nipple.post? I tried to use it like this:

var uti = 'http://uri.com';
var options = {
  payload: JSON.stringify(data),
  json: true
};
Nipple.post(uri, options, function(err, res, payload) {

})

which seems to work in most cases, but if data contains some strange character it will throw an error:

{ [Error: Client request error: socket hang up]
  code: 'ECONNRESET',
  trace: [ { method: 'POST', url: 'http://luri.com' } ],
  isBoom: true,
  data: null,
  output: 
   { statusCode: 502,
     payload: 
      { statusCode: 502,
        error: 'Bad Gateway',
        message: 'socket hang up' },
     headers: {} },
  reformat: [Function] }

I figured out that the strange character is "ฤ‡".

How should the post method be called correctly?

payload from callback param is empty

When sending a POST with options.payload as a stringified JSON object, I get an empty string from the payload parameter of the callback function. If I add json: true or json: 'force' to the options object, I get null for the callback. I tried to track down where things are breaking in wreck/lib/index.js, and it looks like there might be something up with the Recorder instance. I can't see where its _write method is being called, and the buffer returned by reader.collect() always has a length of 0, even though in that same function I can still see the options.payload JSON.

Am I doing something wrong here? Or is there a bug? Let me know if you need any more information. I would love to be able to use this plugin with my new hapi project.

Thanks.

Fails on node v4

> lab -i 22

_http_server.js:515
  this._handle.readStart();
              ^

TypeError: Cannot read property 'readStart' of null
    at Socket.onSocketResume (_http_server.js:515:15)
    at emitNone (events.js:67:13)
    at Socket.emit (events.js:166:7)
    at resume_ (_stream_readable.js:712:10)
    at doNTCallback2 (node.js:429:9)
    at process._tickDomainCallback (node.js:384:17)

Also 84 and 85.

Missing Set-Cookie Headers

Wreck seems to be failing to include set-cookie in the response object passed to the callback of wreck.request

Wreck sez:

{ 'content-language': 'en',
  vary: 'Accept, Accept-Language, Cookie',
  allow: 'GET, POST, HEAD, OPTIONS',
  'access-control-allow-origin': '*',
  'content-type': 'application/json',
  'x-meridian-media-type': 'version=default',
  date: 'Tue, 11 Aug 2015 04:56:56 GMT',
  server: 'Google Frontend',
  'cache-control': 'private',
  'accept-ranges': 'none',
  'transfer-encoding': 'chunked' }

c.f. cURL:

< Content-Language: en
< Vary: Accept, Cookie, Accept-Language
< Allow: GET, POST, HEAD, OPTIONS
< Access-Control-Allow-Origin: *
< Content-Type: application/json
< X-Meridian-Media-Type: version=default
< Vary: Accept-Encoding
< Date: Tue, 11 Aug 2015 05:40:00 GMT
* Server Google Frontend is not blacklisted
< Server: Google Frontend
< Cache-Control: private
< Set-Cookie: csrftoken=lmwDrKZ2PcyCCFKL3XDe0m7G5yQKE1Mt; expires=Tue, 09-Aug-2016 05:40:00 GMT; Max-Age=31449600; Path=/
< Set-Cookie: sessionid=".eJyrVopPLC3JiC8tTi2KT0pMzk7NS1GyUiotyczRg3KL9UAq9PxSknzzU1JznKCKdJB1ZgI1mZobmxpamJqbmpkbmhuYmtUCABhHH0Y:1ZP2I4:IJXHa9qvqF6GBTewc9TD5vKwgJw"; expires=Tue, 25-Aug-2015 05:40:00 GMT; httponly; Max-Age=1209600; Path=/
< Expires: Tue, 11 Aug 2015 05:40:00 GMT
< Accept-Ranges: none
< Transfer-Encoding: chunked

Use wreck with views

How am I supposed to use wreck with views?
Should I use it to return data to a view?

And can I use mapUri (like a hapi proxy) to make the request?

Attempt to call `destroy()` on `Gunzip` object

To reproduce:

  1. Hapi server with parse: true or parse: gzip in a route handler.
  2. Issue a request with payload of application/json whose inflated size exceeds the server's maxBytes setting.
  3. Compress the request with Content-Encoding: gzip to a size that no longer exceeds the server's maxBytes setting.

Trace:

Debug: internal, implementation, error 
    TypeError: Object #<Gunzip> has no method 'destroy'
    at exports.read.onReaderError (/path_to_app/node_modules/hapi/node_modules/wreck/lib/index.js:263:13)
    at g (events.js:180:16)
    at emit (events.js:117:20)
    at internals.Recorder._write (/path_to_app/node_modules/hapi/node_modules/wreck/lib/recorder.js:31:21)
    at doWrite (_stream_writable.js:226:10)
    at writeOrBuffer (_stream_writable.js:216:5)
    at Writable.write (_stream_writable.js:183:11)
    at write (_stream_readable.js:602:24)
    at flow (_stream_readable.js:611:7)
    at Gunzip.pipeOnReadable (_stream_readable.js:643:5)
    at Gunzip.emit (events.js:92:17)
    at emitReadable_ (_stream_readable.js:427:10)
    at emitReadable (_stream_readable.js:423:5)
    at readableAddChunk (_stream_readable.js:166:9)
    at Gunzip.Readable.push (_stream_readable.js:128:10)
    at Gunzip.Transform.push (_stream_transform.js:140:32)

Change "response" Payload

I propose we change the payload we send to "response" handlers to be err, payload and just make payload a single large object rather than the five parameters we are currently passing. Much easier to maintain and won't introduce breaking changes when we need to add more to it.

Option to convert 4xx and 5xx to errors

I'm using Wreck with async to perform a series of network calls. In each function I have to write boilerplate code to check the response.statusCode and convert it to a Boom error. If Wreck could do that automatically it would allow me to rewrite something like this:

async.series([
  function(callback) {
    Wreck.get('https://google.com/first', function(err, response, payload) {
      if (err) {
        return callback(err)
      }

      if (response.statusCode >= 400) {
        err = Boom.create(response.statusCode, payload.message)
        return callback(err)
      }

      // do something with first payload
      callback()
    })
  },
  function(callback) {
    Wreck.get('https://google.com/second', function(err, response, payload) {
      if (err) {
        return callback(err)
      }

      if (response.statusCode >= 400) {
        err = Boom.create(response.statusCode, payload.message)
        return callback(err)
      }

      // do something with second payload
      callback()
    })
  },
  function(callback) {
    Wreck.get('https://google.com/third', function(err, response, payload) {
      if (err) {
        return callback(err)
      }

      if (response.statusCode >= 400) {
        err = Boom.create(response.statusCode, payload.message)
        return callback(err)
      }

      // do something  with third payload
      callback()
    })
  }
], function(err) {
  // done
})

to this:

var opts = { autoConvertErrors: true }
async.waterfall([
  function(callback) {
    Wreck.get('https://google.com/first', opts, callback)
  },
  function(response, payload, callback) {
    // do something with first payload
    Wreck.get('https://google.com/second', opts, callback)
  },
  function(response, payload, callback) {
    // do something with second payload
    Wreck.get('https://google.com/third', opts, callback)
  },
  function(response, payload, callback) {
    // do something with third payload
    callback()
  }
], function(err) {
  // done
})

Refactor `internals.shortcut` to have a fixed signature.

Per #28:

We don't support dynamic APIs for runtime functions.

The internals.shortcut method (via HTTP verb convenience APIs) breaks this convention by allowing any combination of optional options, and callback arguments, e.g. get(uri, [options], [callback]);. As a performance optimization the signature should always be additive, requiring arguments to be provided for each position: get(uri, [options [, callback]]);

For example, to omit the options argument on get, one should:

Nipple.get(uri, null, function (err, res, payload)) {
 //...
});

npm showing latest version as 0.0.0, is this correct? Missing tags?

Running npm info wreck version comes back with 0.0.0 for the version.

0.0.0 is not tagged in github, so I am wondering whether this was a typo?

npm info wreck also shows a 3.0.1 version but in github there is no 3.0.1 tag and package.json in master shows 3.0.0.

So I am confused by which version is the latest stable and am suggesting that all versions in npm would be tagged in github as well.

Automatically parse JSON

Is it possible to let the Nipple module automatically parse JSON content if we got the 'application/json' header set?

Error

I am trying to add api version number to all my responses from the API.
All responses from my API are JavaScript objects like,
reply({user:{name: 'foo',email: '[email protected]'}});

So i am doing this to get my desired result.

server.ext('onPreResponse', function (request, reply) {
    var response = request.response;
    if(!response.isBoom){
      Wreck.read(response, {json: true}, function (err, body) {
        body.version = 'api_version #0.1#';
        body.copyright = 'copyright';
        body.timestamp = Date.now();
        reply(body);
      });
    }else{
      reply.continue();
    }
  });

But it throws and error ,
Debug: internal, implementation, error
TypeError: Uncaught error: undefined is not a function
at internals.Client.read (/home/codeBase/forked_repo/gro-api/node_modules/wreck/lib/index.js:315:9)
at /home/codeBase/forked_repo/gro-api/server.js:61:13
at /home/codeBase/forked_repo/gro-api/node_modules/hapi/lib/handler.js:312:22
at iterate (/home/codeBase/forked_repo/gro-api/node_modules/hapi/node_modules/items/lib/index.js:35:13)
at done (/home/codeBase/forked_repo/gro-api/node_modules/hapi/node_modules/items/lib/index.js:27:25)
at Function.wrapped (/home/codeBase/forked_repo/gro-api/node_modules/hapi/node_modules/hoek/lib/index.js:813:20)
at Function.internals.continue (/home/codeBase/forked_repo/gro-api/node_modules/hapi/lib/reply.js:102:10)
at /home/codeBase/forked_repo/gro-api/node_modules/hapi-i18n/index.js:43:26
at /home/codeBase/forked_repo/gro-api/node_modules/hapi/lib/handler.js:312:22
at iterate (/home/codeBase/forked_repo/gro-api/node_modules/hapi/node_modules/items/lib/index.js:35:13)

Any idea where i am going wrong?

6.0.0 Release notes

Overview

All of the Wreck shortcuts for get, post, put, or delete now work as documented in regards to the value they pass for payload to the callback. Previously, buffer values for payload were converted to strings.

Upgrade Steps

Update any callback functions for get, post, put, or delete to convert the payload value to a string from a buffer if the value was being treated as a string previously.

For example:

Wreck.get('http://hapijs.com', function (err, res, payload) {

    // handle err if exists

    payload = Buffer.isBuffer(payload) ? payload.toString() : payload;
});

Possible EventEmitter memory leak detected

Hi
when I run some tests (lab) one after the other this warning is thrown.

  ..........(node) warning: possible EventEmitter memory leak detected. 11 response listeners added. Use emitter.setMaxListeners() to increase limit.
Trace
    at addListener (events.js:179:15)
    at E:\Vagrant\Ubuntu1404\testApp\api\node_modules\good\lib\monitor.js:172:15
    at done (E:\Vagrant\Ubuntu1404\testApp\api\node_modules\items\lib\index.js:30:25)
    at internals.GoodFile.init (E:\Vagrant\Ubuntu1404\testApp\api\node_modules\good-file\lib\index.js:157:5)
    at E:\Vagrant\Ubuntu1404\testApp\api\node_modules\good\lib\monitor.js:149:18
    at iterate (E:\Vagrant\Ubuntu1404\testApp\api\node_modules\items\lib\index.js:35:13)
    at Object.exports.serial (E:\Vagrant\Ubuntu1404\testApp\api\node_modules\items\lib\index.js:38:9)
    at internals.Monitor.start (E:\Vagrant\Ubuntu1404\testApp\api\node_modules\good\lib\monitor.js:124:11)
    at Object.exports.register (E:\Vagrant\Ubuntu1404\testApp\api\node_modules\good\lib\index.js:17:20)
    at E:\Vagrant\Ubuntu1404\testApp\api\node_modules\hapi\lib\plugin.js:242:14
    at iterate (E:\Vagrant\Ubuntu1404\testApp\api\node_modules\items\lib\index.js:35:13)
    at Object.exports.serial (E:\Vagrant\Ubuntu1404\testApp\api\node_modules\items\lib\index.js:38:9)
    at internals.Plugin.register (E:\Vagrant\Ubuntu1404\testApp\api\node_modules\hapi\lib\plugin.js:224:11)
    at Object.exports.register (E:\Vagrant\Ubuntu1404\testApp\api\lib\index.js:61:10)
    at E:\Vagrant\Ubuntu1404\testApp\api\node_modules\hapi\lib\plugin.js:242:14
    at iterate (E:\Vagrant\Ubuntu1404\testApp\api\node_modules\items\lib\index.js:35:13)
    at Object.exports.serial (E:\Vagrant\Ubuntu1404\testApp\api\node_modules\items\lib\index.js:38:9)
    at internals.Plugin.register (E:\Vagrant\Ubuntu1404\testApp\api\node_modules\hapi\lib\plugin.js:224:11)
    at Object.exports.initServer (E:\Vagrant\Ubuntu1404\testApp\api\test\_utils.js:199:10)
    at E:\Vagrant\Ubuntu1404\testApp\api\test\auth\local-reset-password.js:16:11
    at Immediate._onImmediate (C:\Users\dustin\AppData\Roaming\npm\node_modules\lab\lib\runner.js:445:17)
    at processImmediate [as _immediateCallback] (timers.js:358:17)

good: 6.1.2,
good-console: 5.0.0
hapi: 8.4.0
NodeJs: v0.12.2

thanks!

5.0.0 Release Notes

Breaking Change

Setting both agent and rejectUnauthorized on the options passed to Wreck.request throws an error. If you need to use a custom configured agent and rejectUnauthorized then construct the node Agent with rejectUnauthorized configured. Previously, this would silently ignore the rejectUnauthorized setting.

Enhancements

  • ClientRequest is returned from request and the shortcut methods so that requests can be aborted.
  • Requests to https that need to allow unauthorized certificates are now pooled.

Self Signed Certificates

I am trying to perform a request to an upstream API secured by ssl.

Currently i am having 'ECONNRESET', socket hang up errors. Any idea how to do this?

Nipple.request `options` are only optional if callback is omitted as well.

The code and tests indicate that the options argument is intended to be optional, but it's technically only optional if the callback argument is omitted as well.

Two possibilities to correct this are:

  1. Update docs to reflect the existing behavior: request(method, uri, [options, [callback]])
  2. Change the behavior to make options truly optional. This is slightly complicated by the private param _trace, so the change might be more involved than just checking the number of arguments.

Can't read redirected URI

The current API doesn't allow a user to know which URI that the request might have been redirected to. I need the redirected URI to resolve relative links from the redirected resource.

Another complication, is that I would like to be able to abort() the request at any time. The redirection logic means that the ClientRequest object returned from Wreck.request() will stop working this way once the first redirect is active.

The only simple solution, that can solve both of these issues, is to expose a redirect callback, which is called whenever a redirect request has been performed. A signature like this should do the trick: function (status, location, req) {}.

Let me know if you agree, and I will take a stab at implementing this.

4.0.0 Release Notes

Summary

The intent of v4.0.0 is to help mitigate bottlenecks when using Wreck and agent pooling. By default, on node v0.10, maxSockets is set to 5. This can cause slowness if there are a lot of requests that need to be made as only 5 sockets will get to be used. In node v0.11 this default value is changed to Infinity. Wreck v4.0.0 follows this lead and now defaults the maxSockets to Infinity.

Breaking Change

To avoid overwriting the maxSockets value in the node http and https globalAgent Wreck creates new agents. These agents are exported under the agents object in Wreck. If you need to change the maxSockets value from Infinity then you will need to change either Wreck.agents.http.maxSockets or Wreck.agents.https.maxSockets.

In other words, changing the globalAgent no longer has an impact on the default agent used by Wreck.

Enhancement

Agent pooling can now be disabled by passing false for the value of agent when calling a Wreck request function.

Refactor `read` API to have a fixed signature.

Per #28:

We don't support dynamic APIs for runtime functions.

The read API breaks this convention by allowing any combination of optional options, and callback arguments, e.g. read(req, [options], [callback]);. As a performance optimization the signature should always be additive, requiring arguments to be provided for each position: read(req, [options [, callback]]);

For example, to omit the options argument in read, one should:

Nipple.read(req, null, function (err, payload)) {
 //...
});

Caching wreck requests

I have a route that returns translations to the client. We have a API endpoint that gives me all the translations and then I just return the translations the client need.
I would like to cache the wreck request I make to the API so that I don't have to do it every time a user need new translations. Is this possible?

Ability for force JSON parsing

I am communicating with a service that ALWAYS returns mime-type = 'text/plain' could the ability to force JSON parsing be added? Something like:

options = {
  json: 'force'
}

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.