Comparing version 1.4.3 to 1.5.0
module.exports = Cloudant; | ||
/** | ||
* Copyright (c) 2015 IBM Cloudant, Inc. All rights reserved. | ||
* Copyright (c) 2016 IBM Cloudant, Inc. All rights reserved. | ||
* | ||
@@ -17,5 +17,5 @@ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file | ||
var Nano = require('nano'), | ||
debug = require('debug')('cloudant'), | ||
nanodebug = require('debug')('nano'); | ||
var Nano = require('nano'); | ||
var debug = require('debug')('cloudant'); | ||
var nanodebug = require('debug')('nano'); | ||
@@ -29,7 +29,7 @@ | ||
// This IS the Cloudant API. It is mostly nano, with a few functions. | ||
function Cloudant(credentials, callback) { | ||
debug('Initialize', credentials); | ||
function Cloudant(options, callback) { | ||
debug('Initialize', options); | ||
// Save the username and password for potential conversion to cookie auth. | ||
var login = reconfigure.getCredentials(credentials); | ||
var login = reconfigure.getOptions(options); | ||
@@ -39,3 +39,3 @@ // Convert the credentials into a URL that will work for cloudant. The | ||
// except for the .cookie option. | ||
var cookie = credentials.cookie; | ||
var cookie = options.cookie; | ||
@@ -45,15 +45,36 @@ var pkg = require('./package.json'); | ||
var requestDefaults = { headers: { "User-agent": useragent}, gzip:true }; | ||
if (typeof credentials == "object") { | ||
if (credentials.requestDefaults) { | ||
requestDefaults = credentials.requestDefaults; | ||
delete credentials.requestDefaults; | ||
var theurl = null; | ||
if (typeof options == "object") { | ||
if (options.requestDefaults) { | ||
requestDefaults = options.requestDefaults; | ||
delete options.requestDefaults; | ||
} | ||
credentials = reconfigure(credentials); | ||
theurl = reconfigure(options); | ||
} else { | ||
credentials = reconfigure({ url: credentials}) | ||
theurl = reconfigure({ url: options}) | ||
} | ||
debug('Create underlying Nano instance, credentials=%j requestDefaults=%j', credentials, requestDefaults); | ||
var nano = Nano({url:credentials, requestDefaults: requestDefaults, cookie: cookie, log: nanodebug}); | ||
// keep connections alive by default | ||
if (requestDefaults && !requestDefaults.agent) { | ||
var protocol = (theurl.match(/^https/))? require('https') : require('http'); | ||
var agent = new protocol.Agent({ keepAlive:true }); | ||
requestDefaults.agent = agent; | ||
} | ||
// plugin a request library | ||
var plugin = null; | ||
if (options.plugin) { | ||
if(typeof options.plugin === 'string') { | ||
var plugintype = options.plugin || 'default'; | ||
debug('Using the "' + plugintype + '" plugin'); | ||
plugin = require('./plugins/' + plugintype)(options); | ||
} else if (typeof options.plugin === 'function') { | ||
debug('Using a custom plugin'); | ||
plugin = options.plugin; | ||
} | ||
} | ||
debug('Create underlying Nano instance, options=%j requestDefaults=%j', options, requestDefaults); | ||
var nano = Nano({url:theurl, request: plugin, requestDefaults: requestDefaults, cookie: cookie, log: nanodebug}); | ||
// our own implementation of 'use' e.g. nano.use or nano.db.use | ||
@@ -60,0 +81,0 @@ // it includes all db-level functions |
@@ -50,5 +50,5 @@ // reconfigure deals with the various ways the credentials can be passed in | ||
var credentials = getCredentials(config); | ||
var username = credentials.username; | ||
var password = credentials.password; | ||
var options = getOptions(config); | ||
var username = options.username; | ||
var password = options.password; | ||
@@ -80,4 +80,4 @@ // Configure for Cloudant, either authenticated or anonymous. | ||
module.exports.getCredentials = getCredentials; | ||
function getCredentials(config) { | ||
module.exports.getOptions = getOptions; | ||
function getOptions(config) { | ||
// The username is the account ("foo" for "foo.cloudant.com") | ||
@@ -84,0 +84,0 @@ // or the third-party API key. |
@@ -7,3 +7,3 @@ { | ||
"repository": "git://github.com/cloudant/nodejs-cloudant", | ||
"version": "1.4.3", | ||
"version": "1.5.0", | ||
"author": "Jason Smith <jason@cloudant.com>", | ||
@@ -21,4 +21,6 @@ "contributors": [ | ||
"dependencies": { | ||
"async": "^2.0.1", | ||
"debug": "2.2.0", | ||
"nano": "6.2.0" | ||
"nano": "6.2.0", | ||
"request": "^2.53.0" | ||
}, | ||
@@ -25,0 +27,0 @@ "devDependencies": { |
@@ -11,2 +11,3 @@ # Cloudant Node.js Client | ||
* [Cloudant Local](#cloudant-local) | ||
* [Request Plugins](#request-plugins) | ||
* [API Reference](#api-reference) | ||
@@ -210,3 +211,66 @@ * [Authorization and API Keys](#authorization-and-api-keys) | ||
### Request Plugins | ||
This library can be used with one of three `request` plugins: | ||
1. `default` - the default [request](https://www.npmjs.com/package/request) library plugin. This uses Node.js's callbacks to communicate Cloudant's replies | ||
back to your app and can be used to stream data using the Node.js [Stream API](https://nodejs.org/api/stream.html). | ||
2. `promises` - if you'd prefer to write code in the Promises style then the "promises" plugin turns each request into a Promise. This plugin cannot be used | ||
stream data because instead of returning the HTTP request, we are simply returning a Promise instead. | ||
3. `retry` - on occasion, Cloudant's multi-tenant offerring may reply with an HTTP 429 response because you've exceed the number of API requests in a given amount of time. | ||
The "retry" plugin will automatically retry your request with exponential back-off. | ||
4. custom plugin - you may also supply your own function which will be called to make API calls. | ||
#### The 'promises' Plugins | ||
When initialising the Cloudant library, you can opt to use the 'promises' plugin: | ||
```js | ||
var cloudant = Cloudant({url: myurl, plugin:'promises'}); | ||
var mydb = cloudant.db.use('mydb'); | ||
``` | ||
Then the library will return a Promise for every asynchronous call: | ||
```js | ||
mydb.list().then(function(data) { | ||
console.log(data); | ||
}).catch(function(err) { | ||
console.log('something went wrong', err); | ||
}); | ||
``` | ||
#### The 'retry' plugin | ||
When initialising the Cloudant library, you can opt to use the 'retry' plugin: | ||
```js | ||
var cloudant = Cloudant({url: myurl, plugin:'retry'}); | ||
var mydb = cloudant.db.use('mydb'); | ||
``` | ||
Then use the Cloudant library normally. You may also opt to configure the retry parameters: | ||
- retryAttempts - the maximum number of times the request will be attempted (default 3) | ||
- retryTimeout - the number of milliseconds after the first attempt that the second request will be tried; the timeout doubling with each subsequent attempt (default 500) | ||
```js | ||
var cloudant = Cloudant({url: myurl, plugin:'retry', retryAttempts:5, retryTimeout:1000 }); | ||
var mydb = cloudant.db.use('mydb'); | ||
``` | ||
#### Custom plugin | ||
When initialising the Cloudant library, you can supply your own plugin function: | ||
```js | ||
var doNothingPlugin = function(opts, callback) { | ||
// don't do anything, just pretend that everything's ok. | ||
callback(null, { statusCode:200 }, { ok: true}); | ||
}; | ||
var cloudant = Cloudant({url: myurl, plugin: doNothingPlugin}); | ||
``` | ||
Whenever the Cloudant library wishes to make an outgoing HTTP request, it will call your function instead of `request`. | ||
## API Reference | ||
@@ -213,0 +277,0 @@ |
@@ -30,2 +30,5 @@ /** | ||
var nock = require('./nock.js'); | ||
var ME = process.env.cloudant_username || 'nodejs'; | ||
var PASSWORD = process.env.cloudant_password || null; | ||
var SERVER = 'https://' + ME + '.cloudant.com'; | ||
@@ -48,3 +51,3 @@ var real_require = require; | ||
before(function() { | ||
mocks = nock('https://nodejs.cloudant.com') | ||
mocks = nock(SERVER) | ||
.get('/_all_dbs').reply(200, ['database_changes', 'third_party_db']) | ||
@@ -64,3 +67,3 @@ .delete('/alice').reply(404, {error:'not_found', reason:'Database does not exist.'}) | ||
var me = 'nodejs'; // Set this to your own account | ||
var me = ME; // Set this to your own account | ||
var password = process.env.cloudant_password; | ||
@@ -79,4 +82,4 @@ | ||
it('Example 2', function(done) { | ||
require('dotenv').load(); | ||
// Load the Cloudant library. | ||
@@ -86,3 +89,3 @@ var Cloudant = require('cloudant'); | ||
// Initialize Cloudant with settings from .env | ||
var username = process.env.cloudant_username || "nodejs"; | ||
var username = ME; | ||
var password = process.env.cloudant_password; | ||
@@ -124,4 +127,4 @@ var cloudant = Cloudant({account:username, password:password}); | ||
before(function() { | ||
mocks = nock('https://nodejs.cloudant.com') | ||
.get('/_session').reply(200, {ok:true, userCtx:{name:'nodejs', roles:[]}}) | ||
mocks = nock(SERVER) | ||
.get('/_session').reply(200, {ok:true, userCtx:{name:ME, roles:[]}}) | ||
.post('/_session').reply(200, {XXXXX:'YYYYYYYYYY', ok:true, userCtx:{name:'jhs', roles:[]}}) | ||
@@ -134,3 +137,3 @@ .get('/').reply(200, {couchdb:'Welcome', version:'1.0.2', cloudant_build:'2488'}) | ||
var Cloudant = require('cloudant'); | ||
var me = 'nodejs'; // Replace with your account. | ||
var me = ME; // Replace with your account. | ||
var password = process.env.cloudant_password; | ||
@@ -157,5 +160,5 @@ | ||
before(function() { | ||
mocks = nock('https://nodejs.cloudant.com') | ||
mocks = nock(SERVER) | ||
.get('/_session').reply(200, {XXXXX:'YYYYYYYYYY', ok:true, userCtx:{name:'jhs', roles:[]}}) | ||
.post('/_session').reply(200, {XXXXX:'YYYYYYYYYY', ok:true, userCtx:{name:'jhs', roles:[]}}) | ||
//.post('/_session').reply(200, {XXXXX:'YYYYYYYYYY', ok:true, userCtx:{name:'jhs', roles:[]}}) | ||
.get('/').reply(200, {couchdb:'Welcome!!!!!', version:'1.0.2', cloudant_build:'2488'}); | ||
@@ -166,3 +169,3 @@ }); | ||
var Cloudant = require('cloudant'); | ||
var me = "nodejs"; // Substitute with your Cloudant user account. | ||
var me = ME; // Substitute with your Cloudant user account. | ||
var otherUsername = "jhs"; // Substitute with some other Cloudant user account. | ||
@@ -192,3 +195,3 @@ var otherPassword = process.env.other_cloudant_password; | ||
before(function() { | ||
mocks = nock('https://nodejs.cloudant.com') | ||
mocks = nock(SERVER) | ||
.post('/_api/v2/api_keys') | ||
@@ -204,3 +207,3 @@ .reply(200, { "password": "Eivln4jPiLS8BoTxjXjVukDT", "ok": true, "key": "thandoodstrenterprourete" }) | ||
var Cloudant = require('cloudant'); | ||
var me = 'nodejs'; // Replace with your account. | ||
var me = ME; // Replace with your account. | ||
var password = process.env.cloudant_password; | ||
@@ -260,3 +263,3 @@ var cloudant = Cloudant({account:me, password:password}); | ||
before(function() { | ||
mocks = nock('https://nodejs.cloudant.com') | ||
mocks = nock(SERVER) | ||
.put('/_api/v2/user/config/cors') | ||
@@ -275,3 +278,3 @@ .reply(200, {ok:true}) | ||
var Cloudant = require('cloudant'); | ||
cloudant = Cloudant({account:'nodejs', password:process.env.cloudant_password}); | ||
cloudant = Cloudant({account:ME, password:process.env.cloudant_password}); | ||
}); | ||
@@ -321,3 +324,3 @@ | ||
before(function() { | ||
mocks = nock('https://nodejs.cloudant.com') | ||
mocks = nock(SERVER) | ||
.post('/_api/v2/user/virtual_hosts') | ||
@@ -334,3 +337,3 @@ .reply(200, {ok:true}) | ||
var Cloudant = require('cloudant'); | ||
cloudant = Cloudant({account:'nodejs', password:process.env.cloudant_password}); | ||
cloudant = Cloudant({account:ME, password:process.env.cloudant_password}); | ||
}); | ||
@@ -368,3 +371,3 @@ | ||
before(function() { | ||
mocks = nock('https://nodejs.cloudant.com') | ||
mocks = nock(SERVER) | ||
.put('/my_db') | ||
@@ -385,3 +388,3 @@ .reply(200, {ok:true}) | ||
var Cloudant = require('cloudant'); | ||
cloudant = Cloudant({account:'nodejs', password:process.env.cloudant_password}); | ||
cloudant = Cloudant({account:ME, password:process.env.cloudant_password}); | ||
cloudant.db.create('my_db', function(er) { | ||
@@ -452,3 +455,3 @@ if (er) throw er; | ||
before(function() { | ||
mocks = nock('https://nodejs.cloudant.com') | ||
mocks = nock(SERVER) | ||
.put('/my_db') | ||
@@ -471,3 +474,3 @@ .reply(200, {ok:true}) | ||
var Cloudant = require('cloudant'); | ||
cloudant = Cloudant({account:'nodejs', password:process.env.cloudant_password}); | ||
cloudant = Cloudant({account:ME, password:process.env.cloudant_password}); | ||
cloudant.db.create('my_db', function(er) { | ||
@@ -559,5 +562,5 @@ if (er) throw er; | ||
before(function() { | ||
mocks = nock('https://nodejs.cloudant.com') | ||
mocks = nock(SERVER) | ||
.post('/_session') | ||
.reply(200, { ok: true, name: 'nodejs', roles: [] }, | ||
.reply(200, { ok: true, name: ME, roles: [] }, | ||
{'set-cookie': ['AuthSession=bm9kZWpzOjU1RTA1NDdEOsUsoq9lykQCEBhwTpIyEbgmYpvX; Version=1; Expires=Sat, 29 Aug 2015 12:30:53 GMT; Max-Age=86400; Path=/; HttpOnly; Secure']}) | ||
@@ -567,6 +570,6 @@ .post('/alice') | ||
.get('/_session') | ||
.reply(200, {ok:true, userCtx:{name:'nodejs',roles:['_admin','_reader','_writer']}}); | ||
.reply(200, {ok:true, userCtx:{name:ME,roles:['_admin','_reader','_writer']}}); | ||
var Cloudant = require('cloudant'); | ||
cloudant = Cloudant({account:'nodejs', password:process.env.cloudant_password}); | ||
cloudant = Cloudant({account:ME, password:process.env.cloudant_password}); | ||
}); | ||
@@ -582,3 +585,3 @@ | ||
var Cloudant = require('cloudant'); | ||
var username = 'nodejs'; // Set this to your own account | ||
var username = ME; // Set this to your own account | ||
var password = process.env.cloudant_password; | ||
@@ -614,3 +617,3 @@ var cloudant = Cloudant({account:username, password:password}); | ||
var Cloudant = require('cloudant'); | ||
var username = 'nodejs'; // Set this to your own account | ||
var username = ME; // Set this to your own account | ||
var other_cloudant = Cloudant({account:username, cookie:cookies[username]}); | ||
@@ -640,3 +643,3 @@ | ||
var Cloudant = require('cloudant'); | ||
var username = 'nodejs'; // Set this to your own account | ||
var username = ME; // Set this to your own account | ||
var cloudant = Cloudant({account:username, cookie:cookies[username]}); | ||
@@ -643,0 +646,0 @@ |
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
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
133354
21
2166
1005
4
26
6
+ Addedasync@^2.0.1
+ Addedrequest@^2.53.0
+ Addedasync@2.6.4(transitive)
+ Addedlodash@4.17.21(transitive)