Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'basic-auth' of https://github.com/steelThread/couch-client

 into steelThread
  • Loading branch information...
commit 2debbc72291be97601f2005c5688cb0e3e827581 2 parents 2a4c2d5 + 0b47dc7
@candland candland authored
Showing with 35 additions and 46 deletions.
  1. +35 −46 lib/couch-client.js
View
81 lib/couch-client.js
@@ -1,37 +1,18 @@
/*global Buffer */
-var Http = require('http'),
- Url = require('url'),
+var http = require('http'),
+ https = require('https'),
+ Url = require('url'),
EventEmitter = require('events').EventEmitter,
querystring = require('querystring');
-var POOL_SIZE = 200; // Maximum number of concurrent connections allowed.
-var MAX_DOCS = 1000; // The maximum number of docs to send in a single batch
-
// Handles changes made in node v0.3.0
var NOT_FOUND_ERR_NO = process.ENOENT ? process.ENOENT : require('constants').ENOENT;
+var MAX_DOCS = 1000; // The maximum number of docs to send in a single batch
function noOp(err) { if (err) { throw err; } }
-var pool = new Array(POOL_SIZE);
-var poolIndex = 0;
-function getServerFromPool(uri, callback) {
- try {
- poolIndex = (poolIndex + 1) % POOL_SIZE;
- if (!pool[poolIndex]) {
- pool[poolIndex] = Http.createClient(uri.port, uri.hostname);
- }
- process.nextTick(function () {
- callback(null, pool[poolIndex]);
- });
- } catch (err) {
- process.nextTick(function () {
- callback(err);
- });
- }
-}
-
var CONNECTION_DEFAULTS = {
host: '127.0.0.1:5984',
port: 5984,
@@ -39,14 +20,14 @@ var CONNECTION_DEFAULTS = {
pathname: "/"
};
-
-
function CouchClient(url) {
var uri = Url.parse(url);
+ uri.secure = uri.protocol == 'https:';
+ uri.protocolHandler = uri.secure ? https : http;
uri.__proto__ = CONNECTION_DEFAULTS;
var revCache = {};
- // A simple wrapper around node's http client.
+ // A simple wrapper around node's http(s) request.
function request(method, path, body, callback) {
var stream;
// Body is optional
@@ -68,35 +49,43 @@ function CouchClient(url) {
}
var headers = {
- "Host": uri.host
+ "Host": uri.hostname
};
+ // add the authorization header if provided and using https
+ if (uri.auth && uri.secure) {
+ headers["Authorization"] = "Basic " + new Buffer(uri.auth, "ascii").toString("base64");
+ }
+
if (body) {
body = JSON.stringify(body);
headers["Content-Length"] = Buffer.byteLength(body);
headers["Content-Type"] = "application/json";
}
- getServerFromPool(uri, function (err, server) {
- var request = server.request(method, path, headers);
- if (body) {
- request.write(body, 'utf8');
- }
- request.end();
- request.on('response', function (response) {
- response.setEncoding('utf8');
- var body = "";
- response.on('data', function (chunk) {
- if (callback) { body += chunk; }
- if (stream) { stream.emit('data', chunk); }
- });
- response.on('end', function () {
- if (callback) { callback(null, JSON.parse(body)); }
- if (stream) { stream.emit('end'); }
- });
- response.on('error', errorHandler);
+ var options = {
+ host: uri.hostname,
+ method: method,
+ path: path,
+ port: uri.port,
+ headers: headers
+ };
+ var request = uri.protocolHandler.request(options, function (response) {
+ response.setEncoding('utf8');
+ var body = "";
+ response.on('data', function (chunk) {
+ if (callback) { body += chunk; }
+ if (stream) { stream.emit('data', chunk); }
});
- request.on('error', errorHandler);
+ response.on('end', function () {
+ if (callback) { callback(null, JSON.parse(body)); }
+ if (stream) { stream.emit('end'); }
+ });
+ response.on('error', errorHandler);
});
+ request.on('error', errorHandler);
+
+ if (body) { request.write(body, 'utf8'); }
+ request.end();
return stream;
}
Please sign in to comment.
Something went wrong with that request. Please try again.