From fc17211b55037276626d57a8096782ecca4b125d Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Sun, 11 Oct 2015 10:57:15 -0400 Subject: [PATCH] (#41) - fix pouchdb 5.0.0 support --- index.js => lib/index.js | 2 +- pouch-utils.js => lib/pouch-utils.js | 13 ++--- .../to-buffer-stream.js | 0 lib/version.js | 1 + writable-stream.js => lib/writable-stream.js | 54 +++++++++++-------- package.json | 19 ++++--- version.js | 1 - 7 files changed, 47 insertions(+), 43 deletions(-) rename index.js => lib/index.js (98%) rename pouch-utils.js => lib/pouch-utils.js (84%) rename to-buffer-stream.js => lib/to-buffer-stream.js (100%) create mode 100644 lib/version.js rename writable-stream.js => lib/writable-stream.js (80%) delete mode 100644 version.js diff --git a/index.js b/lib/index.js similarity index 98% rename from index.js rename to lib/index.js index 6ffa561..ef30b36 100644 --- a/index.js +++ b/lib/index.js @@ -35,9 +35,9 @@ exports.plugin.dump = utils.toPromise(function (writableStream, opts, callback) var PouchDB = self.constructor; var output = new PouchDB(self._db_name, { - stream: writableStream, adapter: 'writableStream' }); + output.setupStream(writableStream); var chain = self.info().then(function (info) { var header = { diff --git a/pouch-utils.js b/lib/pouch-utils.js similarity index 84% rename from pouch-utils.js rename to lib/pouch-utils.js index 5495f0e..196e7b1 100644 --- a/pouch-utils.js +++ b/lib/pouch-utils.js @@ -1,12 +1,7 @@ 'use strict'; -var Promise; -/* istanbul ignore next */ -if (typeof window !== 'undefined' && window.PouchDB) { - Promise = window.PouchDB.utils.Promise; -} else { - Promise = typeof global.Promise === 'function' ? global.Promise : require('lie'); -} +var Promise = require('pouchdb/extras/promise'); + /* istanbul ignore next */ exports.once = function (fun) { var called = false; @@ -79,6 +74,4 @@ exports.toPromise = function (func) { }); }; -exports.extend = require('pouchdb-extend'); -exports.inherits = require('inherits'); -exports.Promise = Promise; +exports.inherits = require('inherits'); \ No newline at end of file diff --git a/to-buffer-stream.js b/lib/to-buffer-stream.js similarity index 100% rename from to-buffer-stream.js rename to lib/to-buffer-stream.js diff --git a/lib/version.js b/lib/version.js new file mode 100644 index 0000000..bf6661b --- /dev/null +++ b/lib/version.js @@ -0,0 +1 @@ +module.exports = require('./../package.json').version; \ No newline at end of file diff --git a/writable-stream.js b/lib/writable-stream.js similarity index 80% rename from writable-stream.js rename to lib/writable-stream.js index 6ba0e5b..10c2f86 100644 --- a/writable-stream.js +++ b/lib/writable-stream.js @@ -1,6 +1,7 @@ 'use strict'; var utils = require('./pouch-utils'); +var Promise = require('pouchdb/extras/promise'); var ERROR_REV_CONFLICT = { status: 409, name: 'conflict', @@ -13,23 +14,25 @@ var ERROR_MISSING_DOC = { message: 'missing' }; function WritableStreamPouch(opts, callback) { - - this.instanceId = Math.random().toString(); - this.stream = opts.stream; - this.ldj = ldj.serialize(); - this.ldj.pipe(this.stream); - this.localStore = {}; var api = this; - process.nextTick(function () { - callback(null, api); - }); + api.instanceId = Math.random().toString(); + api.ldj = ldj.serialize(); + api.localStore = {}; + api.originalName = opts.name; + + // TODO: I would pass this in as a constructor opt, but + // PouchDB changed how it clones in 5.0.0 so this broke + api.setupStream = function (stream) { + api.ldj.pipe(stream); + }; + /* istanbul ignore next */ api.type = function () { - return 'readable-stream'; + return 'writableStream'; }; api._id = utils.toPromise(function (callback) { - callback(null, this.instanceId); + callback(null, api.instanceId); }); api._bulkDocs = function (req, opts, callback) { @@ -50,7 +53,7 @@ function WritableStreamPouch(opts, callback) { }); } else { // writing local docs for replication - utils.Promise.all(docs.map(function (doc) { + Promise.all(docs.map(function (doc) { self.localStore[doc._id] = doc; })).then(function (res) { callback(null, res); @@ -59,6 +62,7 @@ function WritableStreamPouch(opts, callback) { }); } }; + api._getRevisionTree = function (docId, callback) { process.nextTick(function () { callback(ERROR_MISSING_DOC); @@ -118,6 +122,7 @@ function WritableStreamPouch(opts, callback) { } }); }; + /* istanbul ignore next */ api._removeLocal = function (doc, callback) { var self = this; @@ -131,19 +136,26 @@ function WritableStreamPouch(opts, callback) { } }); }; + + /* istanbul ignore next */ + api._destroy = function (opts, callback) { + if (typeof opts === 'function') { + callback = opts; + opts = {}; + } + WritableStreamPouch.Changes.removeAllListeners(api.originalName); + process.nextTick(function () { + callback(null, {'ok': true}); + }); + }; + + process.nextTick(function () { + callback(null, api); + }); } WritableStreamPouch.valid = function () { return true; }; -/* istanbul ignore next */ -WritableStreamPouch.destroy = utils.toPromise(function (name, opts, callback) { - WritableStreamPouch.Changes.removeAllListeners(name); - process.nextTick(function () { - callback(null, {'ok': true}); - }); -}); - -//WritableStreamPouch.Changes = new utils.Changes(); module.exports = WritableStreamPouch; diff --git a/package.json b/package.json index fa33f36..f73e7b6 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "pouchdb-replication-stream", "version": "1.2.4", "description": "PouchDB/CouchDB replication as a stream", - "main": "index.js", + "main": "lib/index.js", "repository": { "type": "git", "url": "git://github.com/nolanlawson/pouchdb-replication-stream.git" @@ -24,10 +24,10 @@ "scripts": { "test-node": "TEST_DB=testdb,http://localhost:5984/testdb istanbul test ./node_modules/mocha/bin/_mocha test/test.js -- -R spec", "test-browser": "./bin/test-browser.js", - "jshint": "jshint -c .jshintrc *.js test/test.js", + "jshint": "jshint -c .jshintrc lib test/test.js", "test": "npm run jshint && ./bin/run-test.sh", "build": "mkdirp dist && npm run browserify && npm run min", - "browserify": "browserify index.js | ./bin/es3ify.js | derequire > dist/pouchdb.replication-stream.js", + "browserify": "browserify . | ./bin/es3ify.js | derequire > dist/pouchdb.replication-stream.js", "min": "uglifyjs dist/pouchdb.replication-stream.js -mc > dist/pouchdb.replication-stream.min.js", "dev": "browserify test/test.js > test/test-bundle.js && npm run dev-server", "dev-server": "./bin/dev-server.js", @@ -40,25 +40,24 @@ "lie": "^2.6.0", "lodash": "^3.3.0", "pouch-stream": "^0.4.0", - "pouchdb-extend": "^0.1.2", "through2": "^0.6.1" }, "devDependencies": { "bluebird": "^1.0.7", - "browserify": "^9.0.8", - "chai": "~1.8.1", - "chai-as-promised": "~4.1.0", + "browserify": "^11.2.0", + "chai": "^3.3.0", + "chai-as-promised": "^5.1.0", "derequire": "^2.0.0", "es3ify": "^0.1.3", - "http-server": "~0.5.5", + "http-server": "~0.8.5", "istanbul": "^0.2.7", - "jshint": "~2.3.0", + "jshint": "^2.3.0", "memorystream": "^0.3.0", "mkdirp": "^0.5.0", "mocha": "~1.18", "noms": "0.0.0", "phantomjs": "^1.9.7-5", - "pouchdb": "pouchdb/pouchdb", + "pouchdb": "^5.0.0", "random-document-stream": "0.0.0", "request": "^2.36.0", "sauce-connect-launcher": "^0.4.2", diff --git a/version.js b/version.js deleted file mode 100644 index 64335ee..0000000 --- a/version.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = require('./package.json').version; \ No newline at end of file