Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Various Updates #2

Merged
merged 7 commits into from

2 participants

@DamonOehlman

Hi Pedro,

A few updates in this one (not sure exactly what you will choose to include), but there are probably some that are worthwhile:

  • Added Content-Length header to the Utils.request method which is required if you are working with CouchDB through an nginx proxy (which is something we are doing).

  • Added the ability to pass a path into the options when you initialise PJsonCouch. Once again this is useful if you are working with couch through an nginx proxy (http://wiki.apache.org/couchdb/Nginx_As_a_Reverse_Proxy)

  • I did some tweaking on the way options were being passed around. Essentially, because of the lexical scope of JS there really was no need to pass the options around between the various methods (they already had access to the options variable). As a result initialization tests on the options object can be run earlier (see https://github.com/DamonOehlman/PJsonCouch/blob/master/lib/PJsonCouch.js#L202) and also I've renamed options to reqOpts in the buildRequest function.

Completely up-to-you which (if any) of the changes I've made get integrated back into your codebase :)

Cheers,
Damon.

@landeiro
Owner

Hi Damon,

It seems nice!

I will look at it more carefully as soon as possible.

Thanks for your help!

@landeiro
Owner

Hi again Damon,

In this update I'm having some trouble with special chars like ã,á,õ etc...
This issue is cause by the determination of the content length :

// add the content length header
reqOptions.headers['Content-Length'] = data.length;

with the modification to : reqOptions.headers['Content-Length'] = unescape(encodeURIComponent(data)).length;

it seems to work, can you please confirm that?

A simple test : saveBulkDocs({db:db, "docs":[{string:"não"}]},function(r){console.log(r)} );

cheers!

@DamonOehlman

Just tried it and it works fine for me here with your modification :)

@landeiro
Owner

So, when/if you insert this on your branch I can merge them :)

@landeiro landeiro merged commit 62ff49a into landeiro:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 8, 2011
  1. @DamonOehlman
Commits on Aug 10, 2011
  1. @DamonOehlman
  2. @DamonOehlman
Commits on Aug 11, 2011
  1. @DamonOehlman

    Added path option

    DamonOehlman authored
  2. @DamonOehlman

    Added path option

    DamonOehlman authored
  3. @DamonOehlman

    Added ability to talk to Couch on a subpath, added content-length for…

    DamonOehlman authored
    … routing through nginx proxy
Commits on Aug 15, 2011
  1. @DamonOehlman
This page is out of date. Refresh to see the latest.
Showing with 60 additions and 52 deletions.
  1. +60 −52 lib/PJsonCouch.js
View
112 lib/PJsonCouch.js
@@ -63,7 +63,7 @@ var Utils = {
smartQueryString: function (query) {
if (query === "") return "";
- var qstring = ""
+ var qstring = "";
for (key in query) {
if (qstring !== "") {
qstring += "&";
@@ -126,7 +126,7 @@ var Utils = {
try {
result = JSON.parse(result);
} catch (err) {
- result = Utils.setJSONError(option, result)
+ result = Utils.setJSONError(option, result);
};
if (typeof result !== "object") result = Utils.setJSONError(option, result);
@@ -156,6 +156,10 @@ var Utils = {
request: function (reqOptions, data, reqConfig, callBackFunction) {
var result = "";
data = data || "";
+
+ // add the content length header
+ reqOptions.headers['Content-Length'] = unescape(encodeURIComponent(data)).length;
+
var req = http.request(reqOptions, function (res) {
(reqOptions.resultEncoding) ? res.setEncoding(reqOptions.resultEncoding) : res.setEncoding('utf8');
res.on('data', function (chunk) {
@@ -189,26 +193,32 @@ var Utils = {
}
req.end();
}
-}
+};
var PJsonCouch = function (options) {
+
+ // initialise options defaults
+ options.headers = options.headers || {};
+ options.path = options.path || '';
if (this instanceof PJsonCouch) {
this.setDB = function (dbObj) {
myDB = dbObj.db;
- }
+ };
+
this.getDB = function () {
return {
db: myDB
};
- }
+ };
+
this.getSession = function () {
return {
AuthSession: mySession
};
- }
+ };
var myDB = options.db;
@@ -219,13 +229,13 @@ var PJsonCouch = function (options) {
debugWithHeaders: false,
debugOnError: false,
throwExceptionsOnError: false
- }
+ };
this.setDebugConfig = function (argCfg) {
for (var argKey in argCfg) {
if (config.hasOwnProperty(argKey)) config[argKey] = argCfg[argKey];
}
- }
+ };
this.getDebugConfig = function () {
return config;
@@ -234,7 +244,7 @@ var PJsonCouch = function (options) {
this.login = function (credentials, callBackFunction) {
var postOptions = this.buildRequest("POST", {
serverAction: "_session"
- }, options);
+ });
postOptions.headers = {};
postOptions.headers["Content-Type"] = "application/x-www-form-urlencoded";
// postOptions.headers["Referer"] = "http://127.0.0.1";
@@ -249,13 +259,12 @@ var PJsonCouch = function (options) {
callBackFunction(res);
}
});
-
- }
+ };
this.logout = function (callBackFunction) {
var reqOptions = this.buildRequest("DELETE", {
serverAction: "_session"
- }, options);
+ });
Utils.request(reqOptions, "", config, function (logoutResult) {
if (logoutResult.ok) {
mySession = "";
@@ -264,11 +273,10 @@ var PJsonCouch = function (options) {
callBackFunction(logoutResult);
}
});
-
- }
+ };
this.createDB = function (url, callBackFunction) {
- var reqOptions = this.buildRequest("PUT", url, options);
+ var reqOptions = this.buildRequest("PUT", url);
Utils.request(reqOptions, "", config, function (dbInfo) {
if (callBackFunction) {
callBackFunction(dbInfo);
@@ -277,7 +285,7 @@ var PJsonCouch = function (options) {
};
this.deleteDB = function (url, callBackFunction) {
- var reqOptions = this.buildRequest("DELETE", url, options);
+ var reqOptions = this.buildRequest("DELETE", url);
Utils.request(reqOptions, "", config, function (dbInfo) {
if (callBackFunction) {
callBackFunction(dbInfo);
@@ -287,7 +295,7 @@ var PJsonCouch = function (options) {
this.queryDB = function (url, callBackFunction) {
var method = (url.request) ? methodsWithRequest[url.action] : methods[url.action];
- var reqOptions = this.buildRequest(method, url, options);
+ var reqOptions = this.buildRequest(method, url);
Utils.request(reqOptions, JSON.stringify(url.request), config, function (dbInfo) {
if (callBackFunction) {
callBackFunction(dbInfo);
@@ -296,13 +304,16 @@ var PJsonCouch = function (options) {
};
this.saveDoc = function (url, callBackFunction) {
+ var putOptions;
+
if (typeof url.doc === "string") {
url.doc = JSON.parse(url.doc);
}
if (url.local) {
url.doc._id = url.doc._id.replace(/_local\//, "");
url.action = encodeURIComponent(url.doc._id);
- var putOptions = this.buildRequest("PUT", url, options);
+
+ putOptions = this.buildRequest("PUT", url);
Utils.request(putOptions, JSON.stringify(url.doc), config, function (result) {
if (callBackFunction) {
callBackFunction(result);
@@ -310,7 +321,8 @@ var PJsonCouch = function (options) {
});
} else if (url.doc._id) {
url.action = encodeURIComponent(url.doc._id);
- var putOptions = this.buildRequest("PUT", url, options);
+
+ putOptions = this.buildRequest("PUT", url);
Utils.request(putOptions, JSON.stringify(url.doc), config, function (result) {
if (callBackFunction) {
callBackFunction(result);
@@ -322,7 +334,7 @@ var PJsonCouch = function (options) {
action: "_uuids"
}, function (u) {
url.action = encodeURIComponent(u.uuids[0]);
- var putOptions = that.buildRequest("PUT", url, options);
+ var putOptions = that.buildRequest("PUT", url);
Utils.request(putOptions, JSON.stringify(url.doc), config, function (result) {
if (callBackFunction) {
callBackFunction(result);
@@ -345,7 +357,7 @@ var PJsonCouch = function (options) {
var content = url.attachment.content;
var encoding = url.attachment.content_encoding;
url.attachment = url.attachment.file;
- var putOptions = this.buildRequest("PUT", url, options);
+ var putOptions = this.buildRequest("PUT", url);
putOptions.requestEncoding = encoding;
putOptions.headers["Content-Type"] = type;
if (length) {
@@ -360,7 +372,7 @@ var PJsonCouch = function (options) {
}
});
- }
+ };
this.deleteDocAttachment = function (url, callBackFunction) {
if (url.rev) {
@@ -371,14 +383,14 @@ var PJsonCouch = function (options) {
url.id = encodeURIComponent(url.id);
}
url.attachment = url.attachment.file;
- var deleteOptions = this.buildRequest("DELETE", url, options);
+ var deleteOptions = this.buildRequest("DELETE", url);
Utils.request(deleteOptions, "", config, function (result) {
if (callBackFunction) {
callBackFunction(result);
}
});
- }
+ };
this.deleteDoc = function (url, callBackFunction) {
if (url.local) {
@@ -388,7 +400,7 @@ var PJsonCouch = function (options) {
} else {
url.doc._deleted = true;
}
- var postdeleteOptions = this.buildRequest((url.local) ? "DELETE" : "POST", url, options);
+ var postdeleteOptions = this.buildRequest((url.local) ? "DELETE" : "POST", url);
Utils.request(postdeleteOptions, JSON.stringify(url.doc), config, function (result) {
if (callBackFunction) {
callBackFunction(result);
@@ -400,6 +412,7 @@ var PJsonCouch = function (options) {
if (url.id) {
url.id = encodeURIComponent(url.id);
}
+
var getOptions = this.buildRequest("GET", url, options);
Utils.request(getOptions, "", config, function (doc) {
if (callBackFunction) {
@@ -414,7 +427,7 @@ var PJsonCouch = function (options) {
}
var encoding = url.attachment.content_encoding;
url.attachment = url.attachment.file;
- var getOptions = this.buildRequest("GET", url, options);
+ var getOptions = this.buildRequest("GET", url);
getOptions.resultEncoding = encoding;
Utils.request(getOptions, "", config, function (doc) {
if (callBackFunction) callBackFunction(doc);
@@ -422,7 +435,7 @@ var PJsonCouch = function (options) {
};
this.infoDoc = function (url, callBackFunction) {
- var headOptions = this.buildRequest("HEAD", url, options);
+ var headOptions = this.buildRequest("HEAD", url);
Utils.request(headOptions, "", config, function (result) {
if (callBackFunction) {
callBackFunction(result);
@@ -444,7 +457,7 @@ var PJsonCouch = function (options) {
url.args.rev = url.source.rev;
}
}
- var copyOptions = this.buildRequest("COPY", url, options);
+ var copyOptions = this.buildRequest("COPY", url);
copyOptions.headers.destination = url.destination.id + ((url.destination.rev) ? "?rev=" + url.destination.rev : "");
Utils.request(copyOptions, "", config, function (result) {
if (callBackFunction) {
@@ -456,7 +469,7 @@ var PJsonCouch = function (options) {
this.saveBulkDocs = function (url, callBackFunction) {
var result = "";
url.action = '_bulk_docs';
- var postOptions = this.buildRequest("POST", url, options);
+ var postOptions = this.buildRequest("POST", url);
var extraParams = url.args;
var docs = {};
@@ -483,7 +496,7 @@ var PJsonCouch = function (options) {
data[idx] = url[idx];
}
}
- var serverOptions = this.buildRequest(methods[url.action], url, options);
+ var serverOptions = this.buildRequest(methods[url.action], url);
if (serverOptions.method !== "PUT" && serverOptions.method !== "POST") {
data = "";
}
@@ -497,7 +510,7 @@ var PJsonCouch = function (options) {
this.infoDesign = function (url, callBackFunction) {
url.action = "_info";
- var designOptions = this.buildRequest(methods[url.action], url, options);
+ var designOptions = this.buildRequest(methods[url.action], url);
Utils.request(designOptions, "", config, function (result) {
if (callBackFunction) {
callBackFunction(result);
@@ -507,7 +520,7 @@ var PJsonCouch = function (options) {
this.queryDesign = function (url, callBackFunction) {
url.stringifyRequest = true;
- var designOptions = this.buildRequest((url.request) ? "POST" : methods[url.action], url, options);
+ var designOptions = this.buildRequest((url.request) ? "POST" : methods[url.action], url);
Utils.request(designOptions, JSON.stringify(url.request), config, function (result) {
if (callBackFunction) {
callBackFunction(result);
@@ -515,36 +528,31 @@ var PJsonCouch = function (options) {
});
};
- this.buildRequest = function (method, url, options) {
+ this.buildRequest = function (method, url) {
if (!url.db && !url.serverAction) url.db = myDB;
- var qsparams = Utils.smartQueryString(url.args);
- options.path = Utils.decodeGetData(url);
- options.method = method;
- options.path = (qsparams === "") ? options.path : options.path + "?" + qsparams;
- if (options.headers === undefined) {
- options.headers = {};
- }
+
+ var qsparams = Utils.smartQueryString(url.args),
+ reqOpts = Utils.clone(options);
+
+ reqOpts.path += Utils.decodeGetData(url);
+ reqOpts.method = method;
+ reqOpts.path = (qsparams === "") ? reqOpts.path : reqOpts.path + "?" + qsparams;
+
if (method !== "GET") {
- options.headers["Content-Type"] = "application/json";
+ reqOpts.headers["Content-Type"] = "application/json";
}
-
if (mySession !== "") {
-
- options.headers["Cookie"] = "AuthSession=" + mySession;
+ reqOpts.headers["Cookie"] = "AuthSession=" + mySession;
}
- options.headers["User-Agent"] = "PJsonCouch (node.js client for CouchDB) https://github.com/landeiro/PJsonCouch <landeiro@gmail.com>"
+ reqOpts.headers["User-Agent"] = "PJsonCouch (node.js client for CouchDB) https://github.com/landeiro/PJsonCouch <landeiro@gmail.com>";
- return Utils.clone(options);
- }
+ return reqOpts;
+ };
} else {
return new PJsonCouch(options);
}
-}
-
-
-
-
+};
module.exports = PJsonCouch;
Something went wrong with that request. Please try again.