add-cors-to-couchdb
Advanced tools
Comparing version 0.0.4 to 0.0.5
15
bin.js
#!/usr/bin/env node | ||
'use strict'; | ||
var cbc = require('./'); | ||
var addCorsToCouch = require('./'); | ||
var yargs = require('yargs') | ||
@@ -15,3 +15,3 @@ .alias('p', 'password') | ||
.example('$0', 'update couch at http://127.0.0.1:5984') | ||
.example('$0 http://me.iriscouch.com -u me -p pw', 'update with auth'); | ||
.example('$0 http://example.com -u username -p password', 'update with auth'); | ||
var argv = yargs.argv; | ||
@@ -28,9 +28,8 @@ if (argv.h) { | ||
var url = argv._[0] || 'http://127.0.0.1:5984'; | ||
cbc(url, auth, function (err) { | ||
if (err) { | ||
console.log(err); | ||
process.exit(1); | ||
} | ||
addCorsToCouch(url, auth).then(function () { | ||
console.log('success'); | ||
process.exit(0); | ||
}); | ||
}).catch(function (err) { | ||
console.log(err); | ||
process.exit(1); | ||
}); |
115
index.js
'use strict'; | ||
var fetch = require('node-fetch'); | ||
var url = require('url'); | ||
var transports = {}; | ||
transports.https = require('https'); | ||
transports.http = require('http'); | ||
module.exports = addCors; | ||
function addCors(inurl, auth, callback) { | ||
if (typeof auth === 'function') { | ||
callback = auth; | ||
auth = void 0; | ||
} | ||
var Promise = require('lie'); | ||
var len = todo.length; | ||
var errored = false; | ||
function cb(err, resp) { | ||
if (errored) { | ||
return; | ||
} | ||
if (err) { | ||
errored = true; | ||
return callback(err); | ||
} | ||
len--; | ||
if (!len) { | ||
callback(); | ||
} | ||
} | ||
todo.forEach(function (item) { | ||
var opts = url.parse(inurl + item.path); | ||
if (auth) { | ||
opts.auth = auth; | ||
} | ||
send(opts, item.value, cb); | ||
}); | ||
} | ||
function formatProtocol(protocol) { | ||
if (protocol.slice(-1) === ':') { | ||
protocol = protocol.slice(0, -1); | ||
} | ||
return protocol.toLowerCase(); | ||
} | ||
function send(opts, data, callback) { | ||
opts.method = 'PUT'; | ||
var req = transports[formatProtocol(opts.protocol)].request(opts, function(res) { | ||
var output = ''; | ||
if (res.statusCode !== 200) { | ||
callback(new Error('got a ' + res.statusCode + ' code')); | ||
res.on('data', function () {}); | ||
return; | ||
} | ||
res.on('data', function (chunk) { | ||
output += chunk.toString(); | ||
}).on('error', callback).on('end', function () { | ||
callback(null, output); | ||
}); | ||
}); | ||
req.write(data); | ||
req.end(); | ||
} | ||
var todo = [ | ||
var requests = [ | ||
{ | ||
@@ -82,1 +28,56 @@ path: '/_config/httpd/enable_cors', | ||
]; | ||
function formatUrl(baseUrl, auth, path) { | ||
var urlObject = url.parse(baseUrl + path); | ||
if (auth) { | ||
urlObject.auth = auth; | ||
} | ||
return url.format(urlObject); | ||
} | ||
function updateConfig(urlString, value) { | ||
return fetch(urlString, {method: 'PUT', body: value}).then(function (resp) { | ||
if (resp.status === 200) { | ||
return; | ||
} | ||
return resp.text().then(function (text) { | ||
throw new Error('status ' + resp.status + ' ' + text); | ||
}); | ||
}); | ||
} | ||
function doCouch1(baseUrl, auth) { | ||
return Promise.all(requests.map(function (req) { | ||
var urlString = formatUrl(baseUrl, auth, req.path); | ||
return updateConfig(urlString, req.value); | ||
})); | ||
} | ||
function doCouch2(baseUrl, auth, membershipResp) { | ||
// do the Couch1 logic for all cluster_nodes | ||
// see https://github.com/klaemo/docker-couchdb/issues/42#issuecomment-169610897 | ||
return membershipResp.json().then(function (members) { | ||
return Promise.all(members.cluster_nodes.map(function (node) { | ||
return Promise.all(requests.map(function (req) { | ||
var path = '/_node/' + node + '/' + req.path; | ||
var urlString = formatUrl(baseUrl, auth, path); | ||
return updateConfig(urlString, req.value); | ||
})); | ||
})); | ||
}); | ||
} | ||
function addCors(baseUrl, auth) { | ||
// check if we're dealing with couch 1 or couch 2 | ||
var urlString = formatUrl(baseUrl, auth, '/_membership'); | ||
return fetch(urlString).then(function (resp) { | ||
if (resp.status !== 200) { | ||
return doCouch1(baseUrl, auth); | ||
} else { | ||
return doCouch2(baseUrl, auth, resp); | ||
} | ||
}); | ||
} | ||
module.exports = addCors; |
{ | ||
"name": "add-cors-to-couchdb", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "add CORS to couchdb", | ||
@@ -16,3 +16,3 @@ "main": "index.js", | ||
"author": "Calvin W. Metcalf", | ||
"license": "Apache", | ||
"license": "Apache-2.0", | ||
"bugs": { | ||
@@ -23,5 +23,11 @@ "url": "https://github.com/pouchdb/add-cors-to-couchdb/issues" | ||
"dependencies": { | ||
"lie": "^3.0.2", | ||
"node-fetch": "^1.4.0", | ||
"yargs": "^1.3.1" | ||
}, | ||
"bin": "./bin.js" | ||
"bin": "./bin.js", | ||
"files": [ | ||
"bin.js", | ||
"index.js" | ||
] | ||
} |
@@ -1,2 +0,2 @@ | ||
add cors to couchdb | ||
Add CORS to CouchDB [![Build Status](https://travis-ci.org/pouchdb/add-cors-to-couchdb.svg?branch=master)](https://travis-ci.org/pouchdb/add-cors-to-couchdb) | ||
==== | ||
@@ -6,8 +6,8 @@ | ||
CouchDB doesn't come with [CORS](https://en.wikipedia.org/wiki/Cross-Origin_Resource_Sharing) enabled by default. This is a problem for libraries like [PouchDB](http://pouchdb.com), which depend on being able to access CouchDB no matter what URL it's being served from. | ||
CouchDB doesn't come with [CORS](https://en.wikipedia.org/wiki/Cross-Origin_Resource_Sharing) enabled by default. This is a problem for libraries like [PouchDB](http://pouchdb.com), which depend on being able to access CouchDB no matter what URL it's being served from. This script fixes that. | ||
This script fixes that. | ||
#### Usage | ||
You need to have [Node.js and NPM](https://nodejs.org) installed. Then do: | ||
``` | ||
@@ -18,3 +18,3 @@ npm install -g add-cors-to-couchdb | ||
or if it is a remote database: | ||
Or if it is a remote database: | ||
@@ -48,1 +48,20 @@ ``` | ||
You can always change the configuration later by simply going to [http://localhost:5984/_utils/config.html](http://localhost:5984/_utils/config.html) and updating the values. However, these default options are good for getting up and running. | ||
#### CouchDB 2.0+ instructions | ||
This modules automatically detects CouchDB 2.0 and should do the right thing. But in case | ||
you need to configure CORS on a per-node basis, do: | ||
```bash | ||
curl -X GET $HOST/_membership | ||
``` | ||
to see the list of available nodes, then do e.g.: | ||
```bash | ||
curl -X PUT $HOST/_node/node1@127.0.0.1/_config/httpd/enable_cors -d '"true"' | ||
curl -X PUT $HOST/_node/node1@127.0.0.1/_config/cors/origins -d '"*"' | ||
curl -X PUT $HOST/_node/node1@127.0.0.1/_config/cors/credentials -d '"true"' | ||
curl -X PUT $HOST/_node/node1@127.0.0.1/_config/cors/methods -d '"GET, PUT, POST, HEAD, DELETE"' | ||
curl -X PUT $HOST/_node/node1@127.0.0.1/_config/cors/headers -d '"accept, authorization, content-type, origin, referer, x-csrf-token"' | ||
``` |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Misc. License Issues
License(Experimental) A package's licensing information has fine-grained problems.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
17368
0
65
3
104
+ Addedlie@^3.0.2
+ Addednode-fetch@^1.4.0
+ Addedencoding@0.1.13(transitive)
+ Addediconv-lite@0.6.3(transitive)
+ Addedimmediate@3.0.6(transitive)
+ Addedis-stream@1.1.0(transitive)
+ Addedlie@3.3.0(transitive)
+ Addednode-fetch@1.7.3(transitive)
+ Addedsafer-buffer@2.1.2(transitive)