Permalink
Browse files

Update the module to use node.js 0.8.x APIs.

Also fixes #38.
  • Loading branch information...
1 parent 56a1a30 commit 74f7c6d5a458d3ee3e6312e8680d93fff733e96e Dmitriy Khudorozhkov committed Aug 11, 2012
Showing with 52 additions and 29 deletions.
  1. +6 −0 README.md
  2. +40 −23 lib/couchdb.js
  3. +1 −1 test/test-attachment.js
  4. +1 −1 test/test-changes.js
  5. +1 −1 test/test-client.js
  6. +1 −1 test/test-db.js
  7. +2 −2 test/test-request.js
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!');
View
@@ -1,5 +1,6 @@
var
http = require('http'),
+ https = require('https'),
fs = require('fs'),
path = require('path'),
querystring = require('querystring'),
@@ -98,20 +99,18 @@ 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;
}
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,22 +142,33 @@ 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));
},
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);
});
});
View
@@ -60,6 +60,6 @@ db.saveAttachment(
})
});
-process.addListener('exit', function() {
+process.on('exit', function() {
checkCallbacks(callbacks);
});
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);
});
View
@@ -118,6 +118,6 @@ client2
db.remove();
db2.remove();
-process.addListener('exit', function() {
+process.on('exit', function() {
checkCallbacks(callbacks);
});
View
@@ -287,6 +287,6 @@ db
assert.equal(1, r.rows.length);
});
-process.addListener('exit', function() {
+process.on('exit', function() {
checkCallbacks(callbacks);
});
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);
});

0 comments on commit 74f7c6d

Please sign in to comment.