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

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

Merged
merged 1 commit into from Sep 5, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -21,6 +21,12 @@ To use the library, create a new file called `my-couch-adventure.js`:
client = couchdb.createClient(5984, 'localhost'),
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));
Expand Down
63 changes: 40 additions & 23 deletions lib/couchdb.js
@@ -1,5 +1,6 @@
var
http = require('http'),
https = require('https'),
fs = require('fs'),
path = require('path'),
querystring = require('querystring'),
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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();
};

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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);
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/test-attachment.js
Expand Up @@ -60,6 +60,6 @@ db.saveAttachment(
})
});

process.addListener('exit', function() {
process.on('exit', function() {
checkCallbacks(callbacks);
});
2 changes: 1 addition & 1 deletion test/test-changes.js
Expand Up @@ -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 changes: 1 addition & 1 deletion test/test-client.js
Expand Up @@ -118,6 +118,6 @@ client2
db.remove();
db2.remove();

process.addListener('exit', function() {
process.on('exit', function() {
checkCallbacks(callbacks);
});
2 changes: 1 addition & 1 deletion test/test-db.js
Expand Up @@ -287,6 +287,6 @@ db
assert.equal(1, r.rows.length);
});

process.addListener('exit', function() {
process.on('exit', function() {
checkCallbacks(callbacks);
});
4 changes: 2 additions & 2 deletions test/test-request.js
Expand Up @@ -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);
Expand Down Expand Up @@ -59,6 +59,6 @@ client
assert.equal('method_not_allowed', r.error);
});

process.addListener('exit', function() {
process.on('exit', function() {
checkCallbacks(callbacks);
});