From 9fc46deafce3e9147948ef918229b47b0fa0675f Mon Sep 17 00:00:00 2001 From: Pieter van der Eems Date: Wed, 22 Apr 2015 14:31:37 +0200 Subject: [PATCH 01/13] Add support for a path in host --- lib/cradle.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/cradle.js b/lib/cradle.js index 269f1ab..a15d46b 100644 --- a/lib/cradle.js +++ b/lib/cradle.js @@ -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))) { @@ -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) { @@ -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; } From 917715ffd3a509174a048185957c28e2e4e1ff91 Mon Sep 17 00:00:00 2001 From: Pieter van der Eems Date: Wed, 22 Apr 2015 15:03:49 +0200 Subject: [PATCH 02/13] Changed package.json to allow our server to install this from npm --- package.json | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 6eae65f..639c880 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,14 @@ { - "name": "cradle", - "version": "0.6.9", - "description": "the high-level, caching, CouchDB library", + "name": "cradle-proxy", + "version": "0.0.1", + "description": "Cradle 0.6.9 with support for a path in host, see ihttps://github.com/flatiron/cradle/pull/284, High-level, caching, CouchDB library", "url": "http://cloudhead.io/cradle", - "keywords": ["couchdb", "database", "couch"], + "keywords": ["couchdb", "database", "couch", "path"], "author": "Alexis Sellier ", "contributors": [ { "name": "Charlie Robbins", "email": "charlie@nodejitsu.com" }, - { "name": "Maciej Malecki", "email": "maciej@nodejitsu.com" } + { "name": "Maciej Malecki", "email": "maciej@nodejitsu.com" }, + { "name": "Pieter van der Eems", "email": "github@eemco.nl"} ], "main": "./lib/cradle", "dependencies": { @@ -25,5 +26,5 @@ "engines": { "node": ">=0.8.0" }, - "repository": "flatiron/cradle" + "repository": "pietervandereems/cradle" } From a3601696a095f85c825bd7e3b233423c370c2541 Mon Sep 17 00:00:00 2001 From: Pieter van der Eems Date: Thu, 23 Apr 2015 16:27:37 +0200 Subject: [PATCH 03/13] Update README.md with warning --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 4560d79..f03e042 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,13 @@ 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. + introduction ------------ From ef55fe0fe28fdb13b92d0c77d3845c3f3bbbb27c Mon Sep 17 00:00:00 2001 From: Pieter van der Eems Date: Thu, 23 Apr 2015 16:49:07 +0200 Subject: [PATCH 04/13] Use path_prefix for the changes functionality as well --- lib/cradle/database/changes.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cradle/database/changes.js b/lib/cradle/database/changes.js index fe0ba8f..b8bcd7a 100644 --- a/lib/cradle/database/changes.js +++ b/lib/cradle/database/changes.js @@ -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); From 6e1db51ef055fd2515d13797660820107ba5b36e Mon Sep 17 00:00:00 2001 From: Pieter van der Eems Date: Tue, 28 Apr 2015 10:20:52 +0200 Subject: [PATCH 05/13] Update package.json version to 0.0.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 639c880..d14df06 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cradle-proxy", - "version": "0.0.1", + "version": "0.0.2", "description": "Cradle 0.6.9 with support for a path in host, see ihttps://github.com/flatiron/cradle/pull/284, High-level, caching, CouchDB library", "url": "http://cloudhead.io/cradle", "keywords": ["couchdb", "database", "couch", "path"], From abac1ec179b2212868ba4fa1a018787efe59a41a Mon Sep 17 00:00:00 2001 From: Pieter van der Eems Date: Tue, 28 Apr 2015 13:53:41 +0200 Subject: [PATCH 06/13] In query, replace // with / --- lib/cradle/database/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/cradle/database/index.js b/lib/cradle/database/index.js index 9effb77..6fa087b 100644 --- a/lib/cradle/database/index.js +++ b/lib/cradle/database/index.js @@ -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); }; @@ -62,4 +63,4 @@ Database.prototype.destroy = function (callback) { require('./attachments'); require('./changes'); require('./documents'); -require('./views'); \ No newline at end of file +require('./views'); From 943292171fbc69986ef6ab2c455804ba1323623c Mon Sep 17 00:00:00 2001 From: Pieter van der Eems Date: Tue, 28 Apr 2015 13:54:38 +0200 Subject: [PATCH 07/13] Updated version in package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d14df06..a0cc2d0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cradle-proxy", - "version": "0.0.2", + "version": "0.0.3", "description": "Cradle 0.6.9 with support for a path in host, see ihttps://github.com/flatiron/cradle/pull/284, High-level, caching, CouchDB library", "url": "http://cloudhead.io/cradle", "keywords": ["couchdb", "database", "couch", "path"], From db7c668159353ee61a9d153072b3af9ec23573e3 Mon Sep 17 00:00:00 2001 From: Pieter van der Eems Date: Thu, 30 Jun 2016 17:00:49 +0200 Subject: [PATCH 08/13] Added a try/catch on JSON.parse at rawRequest --- lib/cradle/database/attachments.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/cradle/database/attachments.js b/lib/cradle/database/attachments.js index e029281..ab93ff0 100644 --- a/lib/cradle/database/attachments.js +++ b/lib/cradle/database/attachments.js @@ -103,7 +103,17 @@ Database.prototype.saveAttachment = function (doc, attachment, callback) { return callback(err); } - var result = JSON.parse(body); + try { + var result = JSON.parse(body); + } catch (e) { + console.error('Cradle-proxy, rawRequest error, received a body that is not Json formatted', { + body: body, + options: options, + err: e + }); + console.trace('rawRequest Trace'); + throw 'JSON parse error in rawRequest Trace'; + } result.headers = res.headers; result.headers.status = res.statusCode; From 90d1a3efb77665dd6db4b11466874f2a575986b7 Mon Sep 17 00:00:00 2001 From: Pieter van der Eems Date: Thu, 30 Jun 2016 17:02:39 +0200 Subject: [PATCH 09/13] Update version to 0.0.4 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a0cc2d0..6e22e60 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cradle-proxy", - "version": "0.0.3", + "version": "0.0.4", "description": "Cradle 0.6.9 with support for a path in host, see ihttps://github.com/flatiron/cradle/pull/284, High-level, caching, CouchDB library", "url": "http://cloudhead.io/cradle", "keywords": ["couchdb", "database", "couch", "path"], From 08f94da7e8d30009bdf229362a2567ea70f3a421 Mon Sep 17 00:00:00 2001 From: Pieter van der Eems Date: Fri, 1 Jul 2016 14:29:22 +0200 Subject: [PATCH 10/13] Added my default eslintrc --- .eslintrc.js | 250 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 250 insertions(+) create mode 100644 .eslintrc.js diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 0000000..cdb8d23 --- /dev/null +++ b/.eslintrc.js @@ -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" + ] + } +}; \ No newline at end of file From 0d23a082062195faaf6652c46474a9a6bb401b5b Mon Sep 17 00:00:00 2001 From: Pieter van der Eems Date: Fri, 1 Jul 2016 14:30:06 +0200 Subject: [PATCH 11/13] If saveAttachment returns nonJson, callback with error. --- lib/cradle/database/attachments.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/cradle/database/attachments.js b/lib/cradle/database/attachments.js index ab93ff0..c43523c 100644 --- a/lib/cradle/database/attachments.js +++ b/lib/cradle/database/attachments.js @@ -1,3 +1,4 @@ + var querystring = require('querystring'), Args = require('vargs').Constructor, cradle = require('../../cradle'), @@ -99,20 +100,19 @@ Database.prototype.saveAttachment = function (doc, attachment, callback) { } return this.connection.rawRequest(options, function (err, res, body) { + var result; if (err) { return callback(err); } try { - var result = JSON.parse(body); + result = JSON.parse(body); } catch (e) { - console.error('Cradle-proxy, rawRequest error, received a body that is not Json formatted', { + return callback({ + message: 'saveAttachment returned no-JSON', body: body, - options: options, - err: e + errorCode: 'nonJSON' }); - console.trace('rawRequest Trace'); - throw 'JSON parse error in rawRequest Trace'; } result.headers = res.headers; result.headers.status = res.statusCode; @@ -128,7 +128,7 @@ Database.prototype.saveAttachment = function (doc, attachment, callback) { return callback(null, result); } - callback(result); + return callback(result); }); }; From 31069f5673c8819519fd4b8dcf5712a29d6c47ad Mon Sep 17 00:00:00 2001 From: Pieter van der Eems Date: Fri, 1 Jul 2016 14:30:47 +0200 Subject: [PATCH 12/13] Upped version to 0.0.5 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 6e22e60..25920e4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cradle-proxy", - "version": "0.0.4", + "version": "0.0.5", "description": "Cradle 0.6.9 with support for a path in host, see ihttps://github.com/flatiron/cradle/pull/284, High-level, caching, CouchDB library", "url": "http://cloudhead.io/cradle", "keywords": ["couchdb", "database", "couch", "path"], From 593117574dd006b98b5c8827919a198a187bb1c4 Mon Sep 17 00:00:00 2001 From: Pieter van der Eems Date: Tue, 26 Jul 2016 09:24:31 +0200 Subject: [PATCH 13/13] Use newer follow lib, document changes --- README.md | 7 +++++++ package.json | 4 ++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f03e042..5f5db8d 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,13 @@ 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 ------------ diff --git a/package.json b/package.json index 25920e4..c6dcf6e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cradle-proxy", - "version": "0.0.5", + "version": "0.0.6", "description": "Cradle 0.6.9 with support for a path in host, see ihttps://github.com/flatiron/cradle/pull/284, High-level, caching, CouchDB library", "url": "http://cloudhead.io/cradle", "keywords": ["couchdb", "database", "couch", "path"], @@ -12,7 +12,7 @@ ], "main": "./lib/cradle", "dependencies": { - "follow": "0.11.x", + "follow": "0.12.x", "request": "2.x.x", "vargs": "0.1.0" },