Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

needle

Package Overview
Dependencies
Maintainers
1
Versions
112
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

needle - npm Package Compare versions

Comparing version 1.5.0 to 1.5.1

15

lib/needle.js

@@ -221,2 +221,5 @@ //////////////////////////////////////////

if (options.content_type)
config.headers['content-type'] = options.content_type;
// set connection header if opts.connection was passed, or if node < 0.11.4 (close)

@@ -286,3 +289,3 @@ if (options.connection || close_by_default)

// assume boss wants JSON either if json is true, or if content-type was set and JSON ain't false (weird).
// unless options.json was set to false, assume boss also wants JSON if content-type matches.
var json = options.json || (options.json !== false && config.headers['content-type'] == 'application/json');

@@ -356,4 +359,4 @@

// unless a specific accept header is set, assume json:true wants json back.
if (options.json && config.headers['accept'] === defaults.accept)
// unless a specific accept header was set, assume json: true wants JSON back.
if (options.json && (!options.accept && !(options.headers || {}).accept))
config.headers['accept'] = 'application/json';

@@ -579,3 +582,3 @@

// for some reason, simply piping resp to the writable streams doesn't
// for some reason, simply piping resp to the writable stream doesn't
// work all the time (stream gets cut in the middle with no warning).

@@ -596,3 +599,3 @@ // so we'll manually need to do the readable/write(chunk) trick.

var chunk;
while ((chunk = this.read()) != null) {
while ((chunk = this.read()) !== null) {
if (file.writable) file.write(chunk);

@@ -672,3 +675,3 @@

} else {
// time to call back, junior.
// elvis has left the building.
done(null, resp, resp.body);

@@ -675,0 +678,0 @@ }

2

package.json
{
"name": "needle",
"version": "1.5.0",
"version": "1.5.1",
"description": "The leanest and most handsome HTTP client in the Nodelands.",

@@ -5,0 +5,0 @@ "keywords": [

@@ -221,12 +221,12 @@ Needle

- `open_timeout`: (or `timeout`) Returns error if connection takes longer than X milisecs to establish. Defaults to `10000` (10 secs). `0` means no timeout.
- `agent` : Uses an [http.Agent](https://nodejs.org/api/http.html#http_class_http_agent) of your choice, instead of the global, default one. Useful for tweaking the behaviour at the connection level, such as when doing tunneling (see below for an example).
- `json` : When `true`, sets content type to `application/json` and sends request body as JSON string, instead of a query string.
- `open_timeout`: (or `timeout`) Returns error if connection takes longer than X milisecs to establish. Defaults to `10000` (10 secs). `0` means no timeout.
- `read_timeout`: Returns error if data transfer takes longer than X milisecs, after connection is established. Defaults to `0` (no timeout).
- `follow_max` : (or `follow`) Number of redirects to follow. Defaults to `0`. See below for more redirect options.
- `multipart` : Enables multipart/form-data encoding. Defaults to `false`. Use it when uploading files.
- `agent` : Uses an http.Agent of your choice, instead of the global, default one.
- `proxy` : Forwards request through HTTP(s) proxy. Eg. `proxy: 'http://user:pass@proxy.server.com:3128'`. For more advanced proxying/tunneling use a custom `agent`, as described below.
- `headers` : Object containing custom HTTP headers for request. Overrides defaults described below.
- `headers` : Object containing custom HTTP headers for request. Overrides defaults described below.
- `auth` : Determines what to do with provided username/password. Options are `auto`, `digest` or `basic` (default). `auto` will detect the type of authentication depending on the response headers.
- `json` : When `true`, sets content type to `application/json` and sends request body as JSON string, instead of a query string.
- `stream_length`: When sending streams, this lets you manually set the Content-Length header --if the stream's bytecount is known beforehand--, preventing ECONNRESET (socket hang up) errors on some servers that misbehave when receiving payloads of unknown size. Set it to `0` and Needle will get and set the stream's length for you, or leave empty for the default behaviour, which is no Content-Length header for stream payloads.
- `stream_length`: When sending streams, this lets you manually set the Content-Length header --if the stream's bytecount is known beforehand--, preventing ECONNRESET (socket hang up) errors on some servers that misbehave when receiving payloads of unknown size. Set it to `0` and Needle will get and set the stream's length for you, or leave unset for the default behaviour, which is no Content-Length header for stream payloads.

@@ -248,3 +248,3 @@ Response options

- `cookies` : Sets a {key: 'val'} object as a 'Cookie' header.
- `cookies` : Builds and sets a Cookie header from a `{ key: 'value' }` object.
- `compressed`: If `true`, sets 'Accept-Encoding' header to 'gzip,deflate', and inflates content if zipped. Defaults to `false`.

@@ -256,2 +256,3 @@ - `username` : For HTTP basic auth.

- `user_agent`: Sets the 'User-Agent' HTTP header. Defaults to `Needle/{version} (Node.js {node_version})`.
- `content_type`: Sets the 'Content-Type' header. Unset by default, unless you're sending data in which case it's set accordingly to whatever is being sent (`application/x-www-form-urlencoded`, `application/json` or `multipart/form-data`). That is, of course, unless the option is passed, either here or through the `options.headers`. You're the boss.

@@ -258,0 +259,0 @@ Node.js TLS Options

@@ -596,2 +596,42 @@ var needle = require('..'),

describe('with json: false and content_type = "application/json"', function() {
var opts = { json: false, content_type: 'application/json' };
it('sends request', function(done) {
spystub_request();
post({ foo: 'bar', test: '测试' }, opts, function(err, resp) {
check_request('post');
done();
})
})
it('sets Content-Type header to application/json', function(done) {
post({ foo: 'bar', test: '测试' }, opts, function(err, resp) {
resp.body.headers['content-type'].should.equal('application/json');
done();
})
})
it('doesnt change default Accept header', function(done) {
post({ foo: 'bar', test: '测试' }, opts, function(err, resp) {
// resp.body contains 'header' and 'body', mirroring what we sent
resp.body.headers['accept'].should.equal('*/*');
done();
})
})
it('writes as buffer', function(done) {
post({ foo: 'bar', test: '测试' }, opts, function(err, resp) {
spy.called.should.be.true;
spy.args[0][0].should.be.a.Buffer;
spy.args[0][0].toString().should.equal('foo=bar&test=%E6%B5%8B%E8%AF%95');
resp.body.body.should.eql('foo=bar&test=%E6%B5%8B%E8%AF%95');
done();
})
})
})
describe('with json: undefined but content-type = application/json', function() {

@@ -672,2 +712,42 @@

describe('with json: true and content_type: */* (passed, not default)', function() {
var opts = { json: true, accept: '*/*' };
it('sends request', function(done) {
spystub_request();
post({ foo: 'bar', test: '测试' }, opts, function(err, resp) {
check_request('post');
done();
})
})
it('sets Content-Type header to application/json', function(done) {
post({ foo: 'bar', test: '测试' }, opts, function(err, resp) {
resp.body.headers['content-type'].should.equal('application/json; charset=utf-8');
done();
})
})
it('respects Accept header set by user', function(done) {
post({ foo: 'bar', test: '测试' }, opts, function(err, resp) {
resp.body.headers['accept'].should.equal('*/*');
done();
})
})
it('writes JSON.stringified object', function(done) {
post({ foo: 'bar', test: '测试' }, opts, function(err, resp) {
spy.called.should.be.true;
var json = JSON.stringify({ foo: 'bar', test: '测试' })
spy.args[0][0].toString().should.eql(json)
resp.body.body.should.eql(json);
done();
})
})
})
})

@@ -674,0 +754,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc