1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | 'use strict';
|
15 |
|
16 | const pkg = require('../package.json');
|
17 | const http = require('http');
|
18 | const https = require('https');
|
19 | const cloudant = require('@cloudant/cloudant');
|
20 |
|
21 | const userAgent = 'couchbackup-cloudant/' + pkg.version + ' (Node.js ' +
|
22 | process.version + ')';
|
23 |
|
24 | module.exports = {
|
25 | client: function(url, opts) {
|
26 | var protocol = (url.match(/^https/)) ? https : http;
|
27 | const keepAliveAgent = new protocol.Agent({
|
28 | keepAlive: true,
|
29 | keepAliveMsecs: 30000,
|
30 | maxSockets: opts.parallelism
|
31 | });
|
32 |
|
33 | var actUrl = url.substr(0, url.lastIndexOf('/'));
|
34 | var dbName = url.substr(url.lastIndexOf('/') + 1);
|
35 |
|
36 | var pluginsToUse = ['retry'];
|
37 |
|
38 | if (opts.iamApiKey) {
|
39 | const iamPluginConfig = { iamApiKey: opts.iamApiKey };
|
40 | if (opts.iamTokenUrl) {
|
41 | iamPluginConfig.iamTokenUrl = opts.iamTokenUrl;
|
42 | }
|
43 | pluginsToUse.push({ iamauth: iamPluginConfig });
|
44 | } else {
|
45 | pluginsToUse.push({ cookieauth: { errorOnNoCreds: false } });
|
46 | }
|
47 | return cloudant({
|
48 | url: actUrl,
|
49 | plugins: pluginsToUse,
|
50 | requestDefaults: {
|
51 | agent: keepAliveAgent,
|
52 | headers: { 'User-Agent': userAgent },
|
53 | gzip: true,
|
54 | timeout: opts.requestTimeout
|
55 | }
|
56 | }).use(dbName);
|
57 | }
|
58 | };
|