Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Module update for Node.js 0.8.x. #43

Merged
merged 1 commit into from

2 participants

Dmitriy Khudorozhkov Alexey
Dmitriy Khudorozhkov

This update:

  • makes use of APIs of Node 0.8.x;
  • fixes issue #38;
  • fixes the start-up sample, as previous one couldn't be run on clean Node/CouchDB setup.

This update is very likely to have performance problems due to socket reset on every API call; please see:

joyent/node#1958

I'm currently working on another patch to rectify this.

Dmitriy Khudorozhkov Update the module to use node.js 0.8.x APIs.
Also fixes #38.
74f7c6d
Dmitriy Khudorozhkov

P.S. update also introduces the ability to use https as a data transport - I'll probably look into CouchDB authentication a bit later.

Alexey norlin merged commit 892f591 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 11, 2012
  1. Update the module to use node.js 0.8.x APIs.

    Dmitriy Khudorozhkov authored
    Also fixes #38.
This page is out of date. Refresh to see the latest.
6 README.md
View
@@ -22,6 +22,12 @@ To use the library, create a new file called `my-couch-adventure.js`:
db = client.db('my-db');
db
+ .create(function(er){
+ if (er) throw new Error(JSON.stringify(er));
+ util.puts('Created new db.');
+ });
+
+ db
.saveDoc('my-doc', {awesome: 'couch fun'}, function(er, ok) {
if (er) throw new Error(JSON.stringify(er));
util.puts('Saved my first doc to the couch!');
63 lib/couchdb.js
View
@@ -1,5 +1,6 @@
var
http = require('http'),
+ https = require('https'),
fs = require('fs'),
path = require('path'),
querystring = require('querystring'),
@@ -98,7 +99,7 @@ exports.toAttachment = function(file, cb) {
})
};
-exports.createClient = function(port, host, user, pass, maxListeners) {
+exports.createClient = function(port, host, user, pass, maxListeners, secure) {
if (isNaN(port)) {
host = port;
port = 5984;
@@ -106,12 +107,10 @@ exports.createClient = function(port, host, user, pass, maxListeners) {
host = host || "localhost";
var
- httpClient = http.createClient(port, host),
+ httpClient = secure ? https : http,
+ httpAgent = new httpClient.Agent({host: host, port: port, maxSockets: 1}),
+ nlisteners = 0, listeners = [],
couchClient = new Client();
-
- if (typeof maxListeners !== "undefined"){
- httpClient.setMaxListeners(maxListeners);
- }
couchClient.__defineGetter__('host', function() {
return host;
@@ -129,7 +128,7 @@ exports.createClient = function(port, host, user, pass, maxListeners) {
};
couchClient._addClientListener = function() {
- httpClient.addListener.apply(httpClient, arguments);
+ listeners.push({args: arguments});
};
couchClient._queueRequest = function(options, cb) {
@@ -143,12 +142,17 @@ exports.createClient = function(port, host, user, pass, maxListeners) {
// CouchDB 1.0 requires application/json for _bulk_docs and other POST APIs.
options.headers['Content-Type'] = options.headers['Content-Type'] || 'application/json';
+ options.headers['Connection'] = 'close';
+
var
- request = httpClient.request(
- options.method.toUpperCase(),
- options.path,
- options.headers
- ),
+ request = httpClient.request({
+ host: host,
+ port: port,
+ agent: httpAgent,
+ method: options.method.toUpperCase(),
+ path: options.path,
+ headers: options.headers
+ }),
cbFired = false,
onError = function(reason) {
if (!cbFired) cb && cb(new Error(reason));
@@ -156,9 +160,15 @@ exports.createClient = function(port, host, user, pass, maxListeners) {
onClose = function() {
if (!cbFired) cb && cb();
};
-
- request.addListener('error', onError);
- request.addListener('close', onClose);
+
+ request.setMaxListeners(maxListeners);
+
+ request.on('error', onError);
+ request.on('close', onClose);
+
+ for(var i = 0, n = listeners.length; i < n; i++) {
+ request.on.apply(request, listeners[i].args);
+ }
if (options.data && typeof options.data != 'string') {
options.data = exports.toJSON(options.data);
@@ -167,14 +177,17 @@ exports.createClient = function(port, host, user, pass, maxListeners) {
if (options.data) {
request.write(options.data, options.requestEncoding || 'utf8');
}
- request.addListener("response", function(res) {
+
+ request.on("response", function(res) {
var buffer = '';
res.setEncoding(options.responseEncoding || 'utf8');
res
- .addListener('data', function(chunk) {
+ .on('data', function(chunk) {
buffer += (chunk || '');
})
- .addListener('end', function() {
+ .on('end', function() {
+ httpAgent.sockets[host + ':' + port][0].emit("agentRemove");
+
if(options.responseEncoding == 'binary') {
cbFired = true;
return cb && cb(null, buffer);
@@ -204,8 +217,12 @@ exports.createClient = function(port, host, user, pass, maxListeners) {
json: json,
});
});
-
+
+ for(i = 0, n = listeners.length; i < n; i++) {
+ res.on.apply(res, listeners[i].args);
+ }
});
+
request.end();
};
@@ -257,7 +274,7 @@ Client.prototype.addListener = function(type, fn) {
Client.prototype.request = function(method, path, data, cb) {
var
defaults = {
- method: 'get',
+ method: 'GET',
path: '/',
headers: {},
data: null,
@@ -621,8 +638,8 @@ Db.prototype.changesStream = function(query, options) {
buffer = '';
request.setTimeout(options.timeout);
- request.addListener("response", function(res) {
- res.addListener('data', function(chunk) {
+ request.on("response", function(res) {
+ res.on('data', function(chunk) {
buffer += (chunk || '');
var offset, change;
@@ -644,7 +661,7 @@ Db.prototype.changesStream = function(query, options) {
stream.emit('data', change);
}
})
- res.addListener('close', function(hadError) {
+ res.on('close', function(hadError) {
stream.emit('end', hadError);
});
});
2  test/test-attachment.js
View
@@ -60,6 +60,6 @@ db.saveAttachment(
})
});
-process.addListener('exit', function() {
+process.on('exit', function() {
checkCallbacks(callbacks);
});
2  test/test-changes.js
View
@@ -41,6 +41,6 @@ db.changes({since: 1}, function(er, r) {
assert.equal(1, r.results.length);
});
-process.addListener('exit', function() {
+process.on('exit', function() {
checkCallbacks(callbacks);
});
2  test/test-client.js
View
@@ -118,6 +118,6 @@ client2
db.remove();
db2.remove();
-process.addListener('exit', function() {
+process.on('exit', function() {
checkCallbacks(callbacks);
});
2  test/test-db.js
View
@@ -287,6 +287,6 @@ db
assert.equal(1, r.rows.length);
});
-process.addListener('exit', function() {
+process.on('exit', function() {
checkCallbacks(callbacks);
});
4 test/test-request.js
View
@@ -25,7 +25,7 @@ client
});
client
- .request('get', '/_uuids', {count: 3}, function(er, r) {
+ .request('GET', '/_uuids', {count: 3}, function(er, r) {
if (er) throw new Error(JSON.stringify(er));
callbacks.C = true;
assert.ok(3, r.uuids.length);
@@ -59,6 +59,6 @@ client
assert.equal('method_not_allowed', r.error);
});
-process.addListener('exit', function() {
+process.on('exit', function() {
checkCallbacks(callbacks);
});
Something went wrong with that request. Please try again.