Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for a path in host #284

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
250 changes: 250 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
module.exports = {
"env": {
"es6": true,
"node": true
},
"extends": "eslint:recommended",
"rules": {
"accessor-pairs": "error",
"array-bracket-spacing": [
"error",
"never"
],
"array-callback-return": "error",
"arrow-body-style": "error",
"arrow-parens": "error",
"arrow-spacing": "error",
"block-scoped-var": "error",
"block-spacing": "error",
"brace-style": [
"error",
"1tbs"
],
"callback-return": "error",
"camelcase": [
"error",
{
"properties": "never"
}
],
"comma-spacing": [
"error",
{
"after": true,
"before": false
}
],
"comma-style": [
"error",
"last"
],
"complexity": "error",
"computed-property-spacing": [
"error",
"never"
],
"consistent-return": "error",
"consistent-this": "off",
"curly": "error",
"default-case": "error",
"dot-location": "error",
"dot-notation": "error",
"eol-last": "error",
"eqeqeq": "error",
"func-names": "off",
"func-style": [
"error",
"declaration"
],
"generator-star-spacing": "error",
"global-require": "error",
"guard-for-in": "error",
"handle-callback-err": "off",
"id-blacklist": "error",
"id-length": "off",
"id-match": "error",
"indent": "error",
"init-declarations": "off",
"jsx-quotes": "error",
"key-spacing": "off",
"keyword-spacing": [
"error",
{
"after": true,
"before": true
}
],
"linebreak-style": [
"error",
"unix"
],
"lines-around-comment": "off",
"max-depth": "error",
"max-len": "off",
"max-nested-callbacks": "error",
"max-params": "error",
"max-statements": "off",
"max-statements-per-line": "error",
"new-cap": "error",
"new-parens": "error",
"newline-after-var": "off",
"newline-before-return": "off",
"newline-per-chained-call": "error",
"no-alert": "error",
"no-array-constructor": "error",
"no-bitwise": "error",
"no-caller": "error",
"no-catch-shadow": "off",
"no-confusing-arrow": "error",
"no-console": [
"warn",
{
allow: [
"warn",
"error"
]
}
],
"no-continue": "error",
"no-div-regex": "error",
"no-duplicate-imports": "error",
"no-else-return": "error",
"no-empty-function": "error",
"no-eq-null": "error",
"no-eval": "error",
"no-extend-native": "error",
"no-extra-bind": "error",
"no-extra-label": "error",
"no-extra-parens": "off",
"no-floating-decimal": "error",
"no-implicit-coercion": "error",
"no-implicit-globals": "error",
"no-implied-eval": "error",
"no-inline-comments": "off",
"no-invalid-this": "error",
"no-iterator": "error",
"no-label-var": "error",
"no-labels": "error",
"no-lone-blocks": "error",
"no-lonely-if": "error",
"no-loop-func": "error",
"no-magic-numbers": "off",
"no-mixed-requires": "off",
"no-multi-spaces": "off",
"no-multi-str": "error",
"no-multiple-empty-lines": "error",
"no-native-reassign": "error",
"no-negated-condition": "off",
"no-nested-ternary": "error",
"no-new": "error",
"no-new-func": "error",
"no-new-object": "error",
"no-new-require": "error",
"no-new-wrappers": "error",
"no-octal-escape": "error",
"no-param-reassign": [
"error",
{
"props": false
}
],
"no-path-concat": "error",
"no-plusplus": "error",
"no-process-env": "warn",
"no-process-exit": "error",
"no-proto": "error",
"no-restricted-globals": "error",
"no-restricted-imports": "error",
"no-restricted-modules": "error",
"no-restricted-syntax": "error",
"no-return-assign": "error",
"no-script-url": "error",
"no-self-compare": "error",
"no-sequences": "error",
"no-shadow": "off",
"no-shadow-restricted-names": "error",
"no-spaced-func": "error",
"no-sync": "off",
"no-ternary": "error",
"no-throw-literal": "error",
"no-trailing-spaces": "off",
"no-undef-init": "error",
"no-undefined": "error",
"no-underscore-dangle": "off",
"no-unmodified-loop-condition": "error",
"no-unneeded-ternary": "error",
"no-unsafe-finally": "error",
"no-unused-expressions": "error",
"no-use-before-define": "error",
"no-useless-call": "error",
"no-useless-computed-key": "error",
"no-useless-concat": "error",
"no-useless-constructor": "error",
"no-useless-escape": "error",
"no-var": "off",
"no-void": "error",
"no-warning-comments": "error",
"no-whitespace-before-property": "error",
"no-with": "error",
"object-curly-spacing": "off",
"object-property-newline": [
"error",
{
"allowMultiplePropertiesPerLine": true
}
],
"object-shorthand": "off",
"one-var": "error",
"one-var-declaration-per-line": [
"error",
"initializations"
],
"operator-assignment": [
"error",
"always"
],
"operator-linebreak": "error",
"padded-blocks": "off",
"prefer-arrow-callback": "off",
"prefer-const": "error",
"prefer-reflect": "off",
"prefer-rest-params": "error",
"prefer-spread": "error",
"prefer-template": "off",
"quote-props": "off",
"quotes": "off",
"radix": [
"error",
"always"
],
"require-jsdoc": "off",
"require-yield": "error",
"semi": "error",
"semi-spacing": "error",
"sort-imports": "error",
"sort-vars": "off",
"space-before-blocks": "error",
"space-before-function-paren": "off",
"space-in-parens": [
"error",
"never"
],
"space-infix-ops": "error",
"space-unary-ops": "error",
"spaced-comment": "off",
"strict": [
"error",
"function"
],
"template-curly-spacing": "error",
"valid-jsdoc": "error",
"vars-on-top": "error",
"wrap-iife": "error",
"wrap-regex": "error",
"yield-star-spacing": "error",
"yoda": [
"error",
"never"
]
}
};
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@ cradle

A high-level, caching, CouchDB client for Node.js


warning
-------
This is based on cradle 0.6.9 and could be out of date when you read this.
The difference between this and cradle 0.6.9 (https://github.com/flatiron/cradle/) is the ability to have a host like "127.0.0.1/api/to/couchdb"
See https://github.com/flatiron/cradle/pull/284 for the pull request on cradle that is included inhere.

changes
-------
Added proxy capability
Try/catch around JSON.parse in this.connection.rawRequest
Update follow library to version 0.12.x


introduction
------------

Expand Down
18 changes: 16 additions & 2 deletions lib/cradle.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ cradle.Connection = function Connection(/* variable args */) {
port,
ca,
agentOptions = {},
auth;
auth,
hostSplit;

args.forEach(function (a) {
if (typeof(a) === 'number' || (typeof(a) === 'string' && /^\d{2,5}$/.test(a))) {
Expand Down Expand Up @@ -107,6 +108,15 @@ cradle.Connection = function Connection(/* variable args */) {
console.log('Warning: "ssl" option is deprecated. Use "secure" instead.');
}

if (this.host.indexOf('/') !== -1) {
hostSplit = this.host.split('/');
this.host = hostSplit.shift();
this.path_prefix = '/' + hostSplit.join('/');
if (this.path_prefix.substr(-1) === '/') {
this.path_prefix = this.path_prefix.slice(0, -1);
}
}

agentOptions.host = this.host;
agentOptions.port = this.port;
if (this.options.secure) {
Expand Down Expand Up @@ -300,7 +310,11 @@ cradle.Connection.prototype._url = function (path) {
if (this.port !== 443 && this.port !== 80) {
url += ':' + this.port;
}


if (this.path_prefix) {
url += this.path_prefix;
}

url += path[0] === '/' ? path : ('/' + path);
return url;
}
Expand Down
14 changes: 12 additions & 2 deletions lib/cradle/database/attachments.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

var querystring = require('querystring'),
Args = require('vargs').Constructor,
cradle = require('../../cradle'),
Expand Down Expand Up @@ -99,11 +100,20 @@ Database.prototype.saveAttachment = function (doc, attachment, callback) {
}

return this.connection.rawRequest(options, function (err, res, body) {
var result;
if (err) {
return callback(err);
}

var result = JSON.parse(body);
try {
result = JSON.parse(body);
} catch (e) {
return callback({
message: 'saveAttachment returned no-JSON',
body: body,
errorCode: 'nonJSON'
});
}
result.headers = res.headers;
result.headers.status = res.statusCode;

Expand All @@ -118,7 +128,7 @@ Database.prototype.saveAttachment = function (doc, attachment, callback) {
return callback(null, result);
}

callback(result);
return callback(result);
});
};

Expand Down
2 changes: 1 addition & 1 deletion lib/cradle/database/changes.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Database.prototype.changes = function (options, callback) {
auth = this.connection.auth.username + ':' + this.connection.auth.password + '@';
}

options.db = protocol + '://' + auth + this.connection.host + ':' + this.connection.port + '/' + this.name;
options.db = protocol + '://' + auth + this.connection.host + ':' + this.connection.port + (this.connection.path_prefix || '') + '/' + this.name;
}

feed = new follow.Feed(options);
Expand Down
3 changes: 2 additions & 1 deletion lib/cradle/database/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var Database = exports.Database = function (name, connection) {
// which prepends the database name.
Database.prototype.query = function (options, callback) {
options.path = [this.name, options.path].filter(Boolean).join('/');
options.path = options.path.replace(/\/+/g,'/');
return this.connection.request(options, callback);
};

Expand Down Expand Up @@ -62,4 +63,4 @@ Database.prototype.destroy = function (callback) {
require('./attachments');
require('./changes');
require('./documents');
require('./views');
require('./views');
Loading