diff --git a/lib/cradle.js b/lib/cradle.js index 275fa84..e85c383 100644 --- a/lib/cradle.js +++ b/lib/cradle.js @@ -47,8 +47,8 @@ cradle.Connection = function Connection(/* variable args */) { options = {}, remote, match, - host, - port, + host, + port, ca, agentOptions = {}, auth; @@ -64,7 +64,7 @@ cradle.Connection = function Connection(/* variable args */) { ca = options.ca; } else { host = a; - + if (match = host.match(/^(.+)\:(\d{2,5})$/)) { host = match[1]; port = parseInt(match[2]); @@ -115,7 +115,7 @@ cradle.Connection = function Connection(/* variable args */) { this.transport = http; } this.agent = new (this.transport.Agent)(agentOptions); - + this.agent.maxSockets = this.options.maxSockets; }; @@ -128,9 +128,9 @@ cradle.Connection = function Connection(/* variable args */) { // content // // OLDAPI: function (method, path, options, data, headers) -// +// cradle.Connection.prototype.rawRequest = function (options, callback) { - var promise = new(events.EventEmitter), + var promise = new(events.EventEmitter), self = this; // HTTP Headers @@ -145,7 +145,7 @@ cradle.Connection.prototype.rawRequest = function (options, callback) { Object.keys(this.options.headers).forEach(function (header) { options.headers[header] = self.options.headers[header]; }); - + if (options.query && Object.keys(options.query).length) { for (var k in options.query) { if (typeof(options.query[k]) === 'boolean') { @@ -159,8 +159,8 @@ cradle.Connection.prototype.rawRequest = function (options, callback) { options.agent = this.agent; options.uri = this._url(options.path); delete options.path; - options = cradle.merge(this.options.request, options); - + options = cradle.merge(this.options.request || {}, options); + return request(options, callback || function () { }); }; @@ -189,7 +189,7 @@ cradle.Connection.prototype.request = function (options, callback) { // HTTP Headers options.headers = options.headers || {}; - + // // Handle POST/PUT data. We also convert functions to strings, // so they can be used in _design documents. @@ -231,16 +231,16 @@ cradle.Connection.prototype.request = function (options, callback) { body.headers.status = res.statusCode; return callback(new cradle.CouchError(body)); } - + try { body = JSON.parse(body) } catch (err) { } - + if (body && body.error) { cradle.extend(body, { headers: res.headers }); body.headers.status = res.statusCode; return callback(new cradle.CouchError(body)); } - + callback(null, self.options.raw ? body : new cradle.Response(body, res)); }); }; @@ -274,21 +274,21 @@ cradle.Connection.prototype.activeTasks = function (callback) { this.request({ path: '/_active_tasks' }, callback); }; cradle.Connection.prototype.uuids = function (count, callback) { - if (typeof(count) === 'function') { - callback = count; + if (typeof(count) === 'function') { + callback = count; count = null; } - - this.request({ - method: 'GET', - path: '/_uuids', + + this.request({ + method: 'GET', + path: '/_uuids', query: count ? { count: count } : {} }, callback); }; cradle.Connection.prototype.replicate = function (options, callback) { this.request({ - method: 'POST', - path: '/_replicate', + method: 'POST', + path: '/_replicate', body: options }, callback); }; @@ -298,7 +298,7 @@ cradle.Connection.prototype._url = function (path) { if (this.port !== 443 && this.port !== 80) { url += ':' + this.port; } - + url += path[0] === '/' ? path : ('/' + path); return url; } diff --git a/lib/cradle/cache.js b/lib/cradle/cache.js index a5b1fa5..fac568a 100644 --- a/lib/cradle/cache.js +++ b/lib/cradle/cache.js @@ -1,5 +1,4 @@ /*jshint node:true */ -"use strict"; var Response = require('./response').Response; // diff --git a/lib/cradle/database/attachments.js b/lib/cradle/database/attachments.js index afacf13..4aaf0c5 100644 --- a/lib/cradle/database/attachments.js +++ b/lib/cradle/database/attachments.js @@ -1,5 +1,4 @@ /*jshint node:true */ -"use strict"; var querystring = require('querystring'), Args = require('vargs').Constructor, @@ -11,7 +10,7 @@ Database.prototype.getAttachment = function (id, attachmentName, callback) { // TODO: Update cache? // return this.connection.rawRequest({ - method: 'GET', + method: 'GET', path: '/' + [this.name, querystring.escape(id), attachmentName].join('/'), encoding: null }, callback); @@ -29,7 +28,7 @@ Database.prototype.removeAttachment = function (doc, attachmentName, callback) { id = doc.id || doc._id; rev = doc.rev || doc._rev; } - + if (!id) { error = new(TypeError)("first argument must be a document id"); if (!callback) { @@ -37,7 +36,7 @@ Database.prototype.removeAttachment = function (doc, attachmentName, callback) { } return callback(error); } - + if (!rev && this.cache.has(id)) { rev = this.cache.get(id)._rev; } else if (rev) { @@ -59,7 +58,7 @@ Database.prototype.saveAttachment = function (doc, attachment, callback) { error, rev, id; - + if (typeof doc === 'string') { id = doc; } else { @@ -74,15 +73,15 @@ Database.prototype.saveAttachment = function (doc, attachment, callback) { } return callback(error); } - + attachmentName = typeof attachment !== 'string' ? attachment.name : attachment; - + if (!rev && this.cache.has(id)) { params = { rev: this.cache.get(id)._rev }; } else if (rev) { params = { rev: rev.replace(/\"/g, '') }; } - + options.method = 'PUT'; options.path = '/' + [this.name, querystring.escape(id), attachmentName].join('/'); options.headers = { @@ -95,15 +94,15 @@ Database.prototype.saveAttachment = function (doc, attachment, callback) { if (attachment.contentLength) { options.headers['Content-Length'] = attachment.contentLength; } - + if (attachment.body) { options.body = attachment.body; } - + if (params) { options.path += ('?' + querystring.stringify(params)); } - + return this.connection.rawRequest(options, function (err, res, body) { if (err) { return callback(err); @@ -121,10 +120,10 @@ Database.prototype.saveAttachment = function (doc, attachment, callback) { cached._attachments = cached._attachments || {}; cached._attachments[attachmentName] = { stub: true }; } - + return callback(null, result); } - + callback(result); }); }; diff --git a/lib/cradle/database/changes.js b/lib/cradle/database/changes.js index b3ae670..8197167 100644 --- a/lib/cradle/database/changes.js +++ b/lib/cradle/database/changes.js @@ -1,5 +1,4 @@ /*jshint node:true */ -"use strict"; var events = require('events'), querystring = require('querystring'), @@ -9,16 +8,16 @@ var events = require('events'), Database = require('./index').Database; Database.prototype.changes = function (options, callback) { - if (typeof(options) === 'function') { + if (typeof(options) === 'function') { callback = options; - options = {}; + options = {}; } - + options = options || {}; - + if (callback) { return this.query({ - method: 'GET', + method: 'GET', path: '_changes', query: options }, callback); @@ -32,30 +31,30 @@ Database.prototype.changes = function (options, callback) { if (!options.db) { protocol = this.connection.protocol || 'http'; - + if (this.connection.auth && this.connection.auth.username && this.connection.auth.password) { - auth = this.connection.auth.username + ':' + this.connection.auth.password + '@'; + auth = this.connection.auth.username + ':' + this.connection.auth.password + '@'; } - + options.db = protocol + '://' + auth + this.connection.host + ':' + this.connection.port + '/' + this.name; } - + feed = new follow.Feed(options); feed.on('change', function () { // - // Remark: Support the legacy `data` events. + // Remark: Support the legacy `data` events. // if (!responded) { responded = true; feed.emit('response', response); } - + response.emit.apply(response, ['data'].concat(Array.prototype.slice.call(arguments))); }); - + if (options.follow !== false) { feed.follow(); } - + return feed; }; diff --git a/lib/cradle/database/documents.js b/lib/cradle/database/documents.js index 08424d9..6083745 100644 --- a/lib/cradle/database/documents.js +++ b/lib/cradle/database/documents.js @@ -1,5 +1,4 @@ /*jshint node:true */ -"use strict"; var querystring = require('querystring'), Args = require('vargs').Constructor, @@ -37,7 +36,7 @@ Database.prototype.get = function (id, rev) { if (rev && args.length === 2) { if (typeof(rev) === 'string') { options = { - rev: rev + rev: rev }; } else if (typeof(rev) === 'object') { options = rev; diff --git a/lib/cradle/database/index.js b/lib/cradle/database/index.js index 86dc5dd..b2b1ee6 100644 --- a/lib/cradle/database/index.js +++ b/lib/cradle/database/index.js @@ -1,5 +1,4 @@ /*jshint node:true */ -"use strict"; var querystring = require('querystring'), Args = require('vargs').Constructor, @@ -54,12 +53,12 @@ Database.prototype.create = function (callback) { Database.prototype.destroy = function (callback) { if (arguments.length > 1) { throw new(Error)("destroy() doesn't take any additional arguments"); - } - + } + this.query({ - method: 'DELETE', - path: '/', - }, callback); + method: 'DELETE', + path: '/', + }, callback); }; // @@ -68,4 +67,4 @@ Database.prototype.destroy = function (callback) { require('./attachments'); require('./changes'); require('./documents'); -require('./views'); \ No newline at end of file +require('./views'); diff --git a/lib/cradle/database/views.js b/lib/cradle/database/views.js index 2dba5b5..e40f983 100644 --- a/lib/cradle/database/views.js +++ b/lib/cradle/database/views.js @@ -1,5 +1,4 @@ /*jshint node:true */ -"use strict"; var querystring = require('querystring'), Args = require('vargs').Constructor, diff --git a/lib/cradle/errors.js b/lib/cradle/errors.js index 0a5d55d..2541cfe 100644 --- a/lib/cradle/errors.js +++ b/lib/cradle/errors.js @@ -1,5 +1,4 @@ /*jshint node:true */ -"use strict"; var util = require('util'); diff --git a/lib/cradle/response.js b/lib/cradle/response.js index e8d9f74..0ce1c33 100644 --- a/lib/cradle/response.js +++ b/lib/cradle/response.js @@ -1,5 +1,4 @@ /*jshint node:true */ -"use strict"; // // HTTP response wrapper diff --git a/package.json b/package.json index 44525d2..2209ed2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cradle", - "version": "0.7.0", + "version": "0.7.1", "description": "the high-level, caching, CouchDB library", "url": "http://cloudhead.io/cradle", "keywords": [ diff --git a/test/database-attachment-test.js b/test/database-attachment-test.js index 5d5ba3e..cf8b5f6 100644 --- a/test/database-attachment-test.js +++ b/test/database-attachment-test.js @@ -23,14 +23,20 @@ vows.describe('cradle/database/attachments').addBatch( topic: function (db) { var that = this; db.save({ _id: 'attachment-cacher' }, function (e, res) { + if (e) { + console.error(e); + } db.saveAttachment({ - id: res.id, + id: res.id, rev: res.rev }, { - name: 'cached/foo.txt', - 'Content-Type': 'text/plain', + name: 'cached/foo.txt', + 'Content-Type': 'text/plain', body: 'Foo!' - }, function () { + }, function (e, res) { + if (e) { + console.error(e); + } that.callback(null, db.cache.get(res.id)); }); }); @@ -68,8 +74,8 @@ vows.describe('cradle/database/attachments').addBatch( var callback = this.callback; db.save({ _id: 'attachment-saving-pulls-rev-from-cache' }, function (e, res) { db.saveAttachment(res.id, { - name: 'foo.txt', - contentType: 'text/plain', + name: 'foo.txt', + contentType: 'text/plain', body: 'Foo!' }, callback); }); @@ -87,11 +93,11 @@ vows.describe('cradle/database/attachments').addBatch( var that = this; db.save({_id: 'complete-attachment'}, function (e, res) { db.saveAttachment({ - id: res.id, + id: res.id, rev: res.rev }, { - name: 'foo.txt', - 'content-type': 'text/plain', + name: 'foo.txt', + 'content-type': 'text/plain', body: 'Foo!' }, that.callback); }); @@ -107,13 +113,13 @@ vows.describe('cradle/database/attachments').addBatch( var callback = this.callback, filestream; db.save({ _id: 'piped-attachment' }, function (e, res) { var stream = db.saveAttachment({ - id: res.id, + id: res.id, rev: res.rev }, { - name: 'foo.txt', + name: 'foo.txt', contentType: 'text/plain' }, callback); - + fs.createReadStream(__dirname + "/../README.md").pipe(stream); }); }, @@ -130,11 +136,11 @@ vows.describe('cradle/database/attachments').addBatch( oldRev = res.rev; db.save({_id: 'attachment-incorrect-revision', _rev:res.rev}, function (e, res) { db.saveAttachment({ - id: res.id, + id: res.id, rev: oldRev }, { - name: 'foo.txt', - contentType: 'text/plain', + name: 'foo.txt', + contentType: 'text/plain', body: 'Foo!' }, callback); }); @@ -146,8 +152,8 @@ vows.describe('cradle/database/attachments').addBatch( "to a non-existing document": { topic: function (db) { db.saveAttachment('standalone-attachment', { - name: 'foo.txt', - contentType: 'text/plain', + name: 'foo.txt', + contentType: 'text/plain', body: 'Foo!' }, this.callback); }, @@ -162,15 +168,15 @@ vows.describe('cradle/database/attachments').addBatch( "when it exists": { topic: function (db) { var that = this, doc = { - _id: 'attachment-getter', - _attachments: { + _id: 'attachment-getter', + _attachments: { "foo.txt": { - content_type: "text/plain", + content_type: "text/plain", data: "aGVsbG8gd29ybGQ=" } } }; - + db.save(doc, function (e, res) { db.getAttachment('attachment-getter', 'foo.txt', that.callback); }); @@ -200,15 +206,15 @@ vows.describe('cradle/database/attachments').addBatch( topic: function (db) { var id = 'attachment-incorrect-revision', that = this; - + db.head('attachment-incorrect-revision', function (err, _doc) { db.saveAttachment({ - id: id, + id: id, rev: _doc.etag, }, { name: 'etag-foo.txt', contentType: 'text/plain', - body: 'FOOO!!' + body: 'FOOO!!' }, that.callback); }); }, @@ -233,7 +239,7 @@ vows.describe('cradle/database/attachments').addBatch( }, "should write the correct attachment to disk": function (err, res, body) { assert.isNull(err); - + assert.equal( fs.readFileSync(path.join(__dirname, '..', 'README.md'), 'utf8'), fs.readFileSync(path.join(__dirname, 'fixtures', 'README.md'), 'utf8') @@ -244,14 +250,14 @@ vows.describe('cradle/database/attachments').addBatch( topic: function (db) { var stream = db.getAttachment('attachment-not-found', 'foo.txt'); stream.pipe(fs.createWriteStream(path.join(__dirname, 'fixtures', 'not-found.txt'))); - + stream.on('end', this.callback); }, "should write the error to disk": function () { var result = JSON.parse( fs.readFileSync(path.join(__dirname, 'fixtures', 'not-found.txt'), 'utf8') ); - + assert.equal(result.reason, 'Document is missing attachment'); } }