Comparing version 1.6.0 to 2.0.0
@@ -7,3 +7,3 @@ var fs = require('fs'), | ||
var resp = needle.get(url, { compressed: true, follow: true }); | ||
var resp = needle.get(url, { compressed: true, follow_max: 10 }); | ||
console.log('Downloading...'); | ||
@@ -21,4 +21,4 @@ | ||
resp.on('end', function(data) { | ||
resp.on('done', function(data) { | ||
console.log('Done'); | ||
}) |
@@ -9,3 +9,3 @@ var needle = require('./..'); | ||
needle.get('http://test.webdav.org/auth-digest/', opts, function(err, resp, body){ | ||
needle.get('http://test.webdav.org/auth-digest/', opts, function(err, resp, body) { | ||
console.log(resp.headers); | ||
@@ -12,0 +12,0 @@ |
@@ -23,4 +23,4 @@ var needle = require('./../'); | ||
resp.on('end', function(data) { | ||
resp.on('done', function(data) { | ||
console.log('Done.'); | ||
}) |
@@ -21,4 +21,4 @@ ////////////////////////////////////////// | ||
resp.on('end', function() { | ||
resp.on('done', function() { | ||
console.log('Done.'); | ||
}); |
@@ -12,4 +12,4 @@ var fs = require('fs'), | ||
.pipe(fs.createWriteStream(file)) | ||
.on('finish', function() { | ||
.on('done', function() { | ||
console.log('Done!') | ||
}) |
@@ -149,2 +149,7 @@ ////////////////////////////////////////// | ||
function Needle(method, uri, data, options, callback) { | ||
// if (!(this instanceof Needle)) { | ||
// return new Needle(method, uri, data, options, callback); | ||
// } | ||
if (typeof uri !== 'string') | ||
@@ -452,2 +457,4 @@ throw new TypeError('URL must be a string, not ' + uri); | ||
request.abort(); | ||
// also invoke done() to terminate job on read_timeout | ||
if (type == 'read') done(new Error(type + ' timeout')); | ||
}, milisecs); | ||
@@ -486,3 +493,5 @@ } | ||
if (redirect_codes.indexOf(resp.statusCode) !== -1 && self.should_follow(headers.location, config, uri)) { | ||
// clear timer before following redirects to prevent unexpected setTimeout consequence | ||
clearTimeout(timer); | ||
if (count <= config.follow_max) { | ||
@@ -505,3 +514,3 @@ out.emit('redirect', headers.location); | ||
if (config.follow_set_referer) | ||
config.headers['referer'] = uri; // the original, not the destination URL. | ||
config.headers['referer'] = encodeURI(uri); // the original, not the destination URL. | ||
@@ -642,5 +651,9 @@ config.headers['host'] = null; // clear previous Host header to avoid conflicts. | ||
} | ||
} | ||
// And set the .body property once all data is in. | ||
out.on('end', function() { | ||
// And set the .body property once all data is in. | ||
out.on('end', function() { | ||
if (resp.body) { // callback mode | ||
// we want to be able to access to the raw data later, so keep a reference. | ||
@@ -667,18 +680,16 @@ resp.raw = Buffer.concat(resp.raw); | ||
} | ||
} | ||
// if an output file is being written to, make sure the callback | ||
// is triggered after all data has been written to it. | ||
if (out.file) { | ||
out.file.on('close', function() { | ||
done(null, resp, resp.body); | ||
}) | ||
} else { | ||
// elvis has left the building. | ||
// if an output file is being written to, make sure the callback | ||
// is triggered after all data has been written to it. | ||
if (out.file) { | ||
out.file.on('close', function() { | ||
done(null, resp, resp.body); | ||
} | ||
}) | ||
} else { // elvis has left the building. | ||
done(null, resp, resp.body); | ||
} | ||
}); | ||
}); | ||
} | ||
}); // end request call | ||
@@ -718,5 +729,25 @@ | ||
exports.version = version; | ||
if (typeof Promise !== 'undefined') { | ||
module.exports = function() { | ||
var verb, args = [].slice.call(arguments); | ||
exports.defaults = function(obj) { | ||
if (args[0].match(/\.|\//)) // first argument looks like a URL | ||
verb = (args.length > 2) ? 'post' : 'get'; | ||
else | ||
verb = args.shift(); | ||
if (verb.match(/get|head/) && args.length == 1) | ||
args.splice(1, 0, null); // assume no data if head/get with one argument (options) | ||
return new Promise(function(resolve, reject) { | ||
module.exports.request(verb, args[0], args[1], args[2], function(err, resp) { | ||
return err ? reject(err) : resolve(resp); | ||
}); | ||
}) | ||
} | ||
} | ||
module.exports.version = version; | ||
module.exports.defaults = function(obj) { | ||
for (var key in obj) { | ||
@@ -741,3 +772,3 @@ var target_key = aliased.options[key] || key; | ||
'head get'.split(' ').forEach(function(method) { | ||
exports[method] = function(uri, options, callback) { | ||
module.exports[method] = function(uri, options, callback) { | ||
return new Needle(method, uri, null, options, callback).start(); | ||
@@ -748,3 +779,3 @@ } | ||
'post put patch delete'.split(' ').forEach(function(method) { | ||
exports[method] = function(uri, data, options, callback) { | ||
module.exports[method] = function(uri, data, options, callback) { | ||
return new Needle(method, uri, data, options, callback).start(); | ||
@@ -754,4 +785,4 @@ } | ||
exports.request = function(method, uri, data, opts, callback) { | ||
module.exports.request = function(method, uri, data, opts, callback) { | ||
return new Needle(method, uri, data, opts, callback).start(); | ||
}; |
@@ -29,3 +29,3 @@ // based on the qs module, but handles null objects as expected | ||
if (prefix) | ||
ret.push(stringify(arr[i], prefix + '[' + i + ']')); | ||
ret.push(stringify(arr[i], prefix + '[]')); | ||
else | ||
@@ -32,0 +32,0 @@ ret.push(stringify(arr[i])); |
{ | ||
"name": "needle", | ||
"version": "1.6.0", | ||
"version": "2.0.0", | ||
"description": "The leanest and most handsome HTTP client in the Nodelands.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -238,3 +238,3 @@ Needle | ||
### Event: `'done'` | ||
### Event: `'done'` (previously 'end') | ||
@@ -246,4 +246,4 @@ - `exception <Error>` (optional) | ||
```js | ||
var resp = needle.get('something.worthy/of/being/streamed/by/needle') | ||
.pipe(somewhereElse); | ||
var resp = needle.get('something.worthy/of/being/streamed/by/needle'); | ||
resp.pipe(someWritableStream); | ||
@@ -250,0 +250,0 @@ resp.on('done', function(err) { |
@@ -136,3 +136,3 @@ var needle = require('../'), | ||
describe('when request timeouts', function(){ | ||
describe('when request times out', function(){ | ||
@@ -203,3 +203,3 @@ var server, | ||
done(); | ||
}, 100); | ||
}, 350); | ||
}) | ||
@@ -206,0 +206,0 @@ |
@@ -627,3 +627,3 @@ var needle = require('..'), | ||
spy.called.should.be.true; | ||
spy.args[0][0].should.be.a.Buffer; | ||
spy.args[0][0].constructor.name.should.eql('Buffer'); | ||
spy.args[0][0].toString().should.equal('foo=bar&test=%E6%B5%8B%E8%AF%95'); | ||
@@ -630,0 +630,0 @@ resp.body.body.should.eql('foo=bar&test=%E6%B5%8B%E8%AF%95'); |
@@ -108,4 +108,22 @@ var should = require('should'), | ||
describe('with object where val is an array', function() { | ||
it('works', function() { | ||
var res = stringify({ foo: ['bar', 'baz'] }); | ||
res.should.eql('foo[]=bar&foo[]=baz'); | ||
}) | ||
}) | ||
describe('with object where val is an array of key val objects', function() { | ||
it('works', function() { | ||
var res = stringify({ foo: [{'1': 'bar'}, {'2': 'baz'}] }); | ||
res.should.eql('foo[][1]=bar&foo[][2]=baz'); | ||
}) | ||
}) | ||
}) | ||
}) |
@@ -13,3 +13,3 @@ var should = require('should'), | ||
before(function(){ | ||
before(function() { | ||
server = http.createServer(function(req, res) { | ||
@@ -21,3 +21,3 @@ res.setHeader('Content-Type', 'application/json') | ||
after(function(){ | ||
after(function() { | ||
server.close(); | ||
@@ -36,7 +36,7 @@ }) | ||
var readableCalled = false; | ||
stream.on('readable', function () { | ||
stream.on('readable', function() { | ||
readableCalled = true; | ||
}) | ||
stream.on('done', function () { | ||
stream.on('done', function() { | ||
readableCalled.should.be.true; | ||
@@ -46,3 +46,4 @@ done(); | ||
stream.resume(); | ||
stream.resume() | ||
}) | ||
@@ -69,3 +70,3 @@ | ||
it('emits a raw buffer if we do not want to parse JSON', function(done) { | ||
var stream = needle.get('localhost:' + port, {parse: false}) | ||
var stream = needle.get('localhost:' + port, { parse: false }) | ||
@@ -72,0 +73,0 @@ var chunks = []; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
196697
43
4509