New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

add-cors-to-couchdb

Package Overview
Dependencies
Maintainers
2
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

add-cors-to-couchdb - npm Package Compare versions

Comparing version 0.0.4 to 0.0.5

LICENSE

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);
});
'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"'
```
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