Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var start = document.getElementById('start');
var stuff = document.getElementById('stuff');
var remote = new PouchDB('https://skimdb.npmjs.com/registry');
var local = new PouchDB('npm');
start.addEventListener('click', function (){
remote.info().then(function (a) {
return a.doc_count;
}).then(function (count) {
remote.replicate.to(local, {
batch_size: 100
}).on('change', function (change) {
stuff.innerText = change.docs_written + ' out of ' + count;
}).on('complete', function () {
stuff.innerText = change.docs_written + ' done'
});
});
});
<div id="stuff"></div>
<button id="start">start</button>
<script src="pouchdb.js"></script>
<script src="ascript.js"></script>
!function(e){if("object"==typeof exports)module.exports=e();else if("function"==typeof define&&define.amd)define(e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.PouchDB=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){
module.exports = "2.2.0-alpha";
},{}],2:[function(_dereq_,module,exports){
"use strict";
var utils = _dereq_('./utils');
var merge = _dereq_('./merge');
var errors = _dereq_('./deps/errors');
var EventEmitter = _dereq_('events').EventEmitter;
var upsert = _dereq_('./deps/upsert');
var Changes = _dereq_('./changes');
var Promise = utils.Promise;
/*
* A generic pouch adapter
*/
// returns first element of arr satisfying callback predicate
function arrayFirst(arr, callback) {
for (var i = 0; i < arr.length; i++) {
if (callback(arr[i], i) === true) {
return arr[i];
}
}
return false;
}
// Wrapper for functions that call the bulkdocs api with a single doc,
// if the first result is an error, return an error
function yankError(callback) {
return function (err, results) {
if (err || results[0].error) {
callback(err || results[0]);
} else {
callback(null, results[0]);
}
};
}
// for every node in a revision tree computes its distance from the closest
// leaf
function computeHeight(revs) {
var height = {};
var edges = [];
merge.traverseRevTree(revs, function (isLeaf, pos, id, prnt) {
var rev = pos + "-" + id;
if (isLeaf) {
height[rev] = 0;
}
if (prnt !== undefined) {
edges.push({from: prnt, to: rev});
}
return rev;
});
edges.reverse();
edges.forEach(function (edge) {
if (height[edge.from] === undefined) {
height[edge.from] = 1 + height[edge.to];
} else {
height[edge.from] = Math.min(height[edge.from], 1 + height[edge.to]);
}
});
return height;
}
function allDocsKeysQuery(api, opts, callback) {
var keys = ('limit' in opts) ?
opts.keys.slice(opts.skip, opts.limit + opts.skip) :
(opts.skip > 0) ? opts.keys.slice(opts.skip) : opts.keys;
if (opts.descending) {
keys.reverse();
}
if (!keys.length) {
return api._allDocs({limit: 0}, callback);
}
var finalResults = {
rows: new Array(keys.length),
offset: opts.skip
};
Promise.all(keys.map(function (key, i) {
var subOpts = utils.extend(true, {key: key, deleted: 'ok'}, opts);
['limit', 'skip', 'keys'].forEach(function (optKey) {
delete subOpts[optKey];
});
return new Promise(function (resolve, reject) {
api._allDocs(subOpts, function (err, res) {
if (err) {
return reject(err);
}
finalResults.rows[i] = res.rows[0] || {key: key, error: 'not_found'};
finalResults.total_rows = res.total_rows;
resolve();
});
});
})).then(function () {
callback(null, finalResults);
}).catch(function (err) {
callback(err);
});
}
utils.inherits(AbstractPouchDB, EventEmitter);
module.exports = AbstractPouchDB;
function AbstractPouchDB() {
var self = this;
EventEmitter.call(this);
self.autoCompact = function (callback) {
if (!self.auto_compaction) {
return callback;
}
return function (err, res) {
if (err) {
callback(err);
} else {
var count = res.length;
var decCount = function () {
count--;
if (!count) {
callback(null, res);
}
};
res.forEach(function (doc) {
if (doc.ok) {
// TODO: we need better error handling
self.compactDocument(doc.id, 1, decCount);
} else {
decCount();
}
});
}
};
};
var listeners = 0, changes;
var eventNames = ['change', 'delete', 'create', 'update'];
this.on('newListener', function (eventName) {
if (~eventNames.indexOf(eventName)) {
if (listeners) {
listeners++;
return;
} else {
listeners++;
}
} else {
return;
}
var lastChange = 0;
changes = this.changes({
conflicts: true,
include_docs: true,
continuous: true,
since: 'latest',
onChange: function (change) {
if (change.seq <= lastChange) {
return;
}
lastChange = change.seq;
self.emit('change', change);
if (change.doc._deleted) {
self.emit('delete', change);
} else if (change.doc._rev.split('-')[0] === '1') {
self.emit('create', change);
} else {
self.emit('update', change);
}
}
});
});
this.on('removeListener', function (eventName) {
if (~eventNames.indexOf(eventName)) {
listeners--;
if (listeners) {
return;
}
} else {
return;
}
changes.cancel();
});
}
AbstractPouchDB.prototype.post =
utils.adapterFun('post', function (doc, opts, callback) {
if (typeof opts === 'function') {
callback = opts;
opts = {};
}
if (typeof doc !== 'object' || Array.isArray(doc)) {
return callback(errors.NOT_AN_OBJECT);
}
return this.bulkDocs({docs: [doc]}, opts,
this.autoCompact(yankError(callback)));
});
AbstractPouchDB.prototype.put =
utils.adapterFun('put', utils.getArguments(function (args) {
var temp, temptype, opts, callback;
var doc = args.shift();
var id = '_id' in doc;
if (typeof doc !== 'object' || Array.isArray(doc)) {
callback = args.pop();
return callback(errors.NOT_AN_OBJECT);
}
doc = utils.clone(doc);
while (true) {
temp = args.shift();
temptype = typeof temp;
if (temptype === "string" && !id) {
doc._id = temp;
id = true;
} else if (temptype === "string" && id && !('_rev' in doc)) {
doc._rev = temp;
} else if (temptype === "object") {
opts = temp;
} else if (temptype === "function") {
callback = temp;
}
if (!args.length) {
break;
}
}
opts = opts || {};
var error = utils.invalidIdError(doc._id);
if (error) {
return callback(error);
}
return this.bulkDocs({docs: [doc]}, opts,
this.autoCompact(yankError(callback)));
}));
AbstractPouchDB.prototype.putAttachment =
utils.adapterFun('putAttachment', function (docId, attachmentId, rev,
blob, type, callback) {
var api = this;
if (typeof type === 'function') {
callback = type;
type = blob;
blob = rev;
rev = null;
}
if (typeof type === 'undefined') {
type = blob;
blob = rev;
rev = null;
}
function createAttachment(doc) {
doc._attachments = doc._attachments || {};
doc._attachments[attachmentId] = {
content_type: type,
data: blob
};
api.put(doc, callback);
}
api.get(docId, function (err, doc) {
// create new doc
if (err && err.error === errors.MISSING_DOC.error) {
createAttachment({_id: docId});
return;
}
if (err) {
callback(err);
return;
}
if (doc._rev !== rev) {
callback(errors.REV_CONFLICT);
return;
}
createAttachment(doc);
});
});
AbstractPouchDB.prototype.removeAttachment =
utils.adapterFun('removeAttachment', function (docId, attachmentId, rev,
callback) {
var self = this;
self.get(docId, function (err, obj) {
if (err) {
callback(err);
return;
}
if (obj._rev !== rev) {
callback(errors.REV_CONFLICT);
return;
}
if (!obj._attachments) {
return callback();
}
delete obj._attachments[attachmentId];
if (Object.keys(obj._attachments).length === 0) {
delete obj._attachments;
}
self.put(obj, callback);
});
});
AbstractPouchDB.prototype.remove =
utils.adapterFun('remove', function (doc, opts, callback) {
if (typeof opts === 'function') {
callback = opts;
opts = {};
}
if (opts === undefined) {
opts = {};
}
opts = utils.clone(opts);
opts.was_delete = true;
var newDoc = {_id: doc._id, _rev: doc._rev};
newDoc._deleted = true;
return this.bulkDocs({docs: [newDoc]}, opts, yankError(callback));
});
AbstractPouchDB.prototype.revsDiff =
utils.adapterFun('revsDiff', function (req, opts, callback) {
if (typeof opts === 'function') {
callback = opts;
opts = {};
}
opts = utils.clone(opts);
var ids = Object.keys(req);
var count = 0;
var missing = {};
function addToMissing(id, revId) {
if (!missing[id]) {
missing[id] = {missing: []};
}
missing[id].missing.push(revId);
}
function processDoc(id, rev_tree) {
// Is this fast enough? Maybe we should switch to a set simulated by a map
var missingForId = req[id].slice(0);
merge.traverseRevTree(rev_tree, function (isLeaf, pos, revHash, ctx,
opts) {
var rev = pos + '-' + revHash;
var idx = missingForId.indexOf(rev);
if (idx === -1) {
return;
}
missingForId.splice(idx, 1);
if (opts.status !== 'available') {
addToMissing(id, rev);
}
});
// Traversing the tree is synchronous, so now `missingForId` contains
// revisions that were not found in the tree
missingForId.forEach(function (rev) {
addToMissing(id, rev);
});
}
ids.map(function (id) {
this._getRevisionTree(id, function (err, rev_tree) {
if (err && err.name === 'not_found' && err.message === 'missing') {
missing[id] = {missing: req[id]};
} else if (err) {
return callback(err);
} else {
processDoc(id, rev_tree);
}
if (++count === ids.length) {
return callback(null, missing);
}
});
}, this);
});
// compact one document and fire callback
// by compacting we mean removing all revisions which
// are further from the leaf in revision tree than max_height
AbstractPouchDB.prototype.compactDocument =
function (docId, max_height, callback) {
var self = this;
this._getRevisionTree(docId, function (err, rev_tree) {
if (err) {
return callback(err);
}
var height = computeHeight(rev_tree);
var candidates = [];
var revs = [];
Object.keys(height).forEach(function (rev) {
if (height[rev] > max_height) {
candidates.push(rev);
}
});
merge.traverseRevTree(rev_tree, function (isLeaf, pos, revHash, ctx, opts) {
var rev = pos + '-' + revHash;
if (opts.status === 'available' && candidates.indexOf(rev) !== -1) {
opts.status = 'missing';
revs.push(rev);
}
});
self._doCompaction(docId, rev_tree, revs, callback);
});
};
// compact the whole database using single document
// compaction
AbstractPouchDB.prototype.compact =
utils.adapterFun('compact', function (opts, callback) {
if (typeof opts === 'function') {
callback = opts;
opts = {};
}
var self = this;
this.changes({complete: function (err, res) {
if (err) {
callback(); // TODO: silently fail
return;
}
var count = res.results.length;
if (!count) {
callback();
return;
}
res.results.forEach(function (row) {
self.compactDocument(row.id, 0, function () {
count--;
if (!count) {
callback();
}
});
});
}});
});
/* Begin api wrappers. Specific functionality to storage belongs in the
_[method] */
AbstractPouchDB.prototype.get =
utils.adapterFun('get', function (id, opts, callback) {
if (typeof opts === 'function') {
callback = opts;
opts = {};
}
if (typeof id !== 'string') {
return callback(errors.INVALID_ID);
}
var leaves = [], self = this;
function finishOpenRevs() {
var result = [];
var count = leaves.length;
if (!count) {
return callback(null, result);
}
// order with open_revs is unspecified
leaves.forEach(function (leaf) {
self.get(id,
{rev: leaf, revs: opts.revs, attachments: opts.attachments},
function (err, doc) {
if (!err) {
result.push({ok: doc});
} else {
result.push({missing: leaf});
}
count--;
if (!count) {
callback(null, result);
}
});
});
}
if (opts.open_revs) {
if (opts.open_revs === "all") {
this._getRevisionTree(id, function (err, rev_tree) {
if (err) {
// if there's no such document we should treat this
// situation the same way as if revision tree was empty
rev_tree = [];
}
leaves = merge.collectLeaves(rev_tree).map(function (leaf) {
return leaf.rev;
});
finishOpenRevs();
});
} else {
if (Array.isArray(opts.open_revs)) {
leaves = opts.open_revs;
for (var i = 0; i < leaves.length; i++) {
var l = leaves[i];
// looks like it's the only thing couchdb checks
if (!(typeof(l) === "string" && /^\d+-/.test(l))) {
return callback(errors.error(errors.BAD_REQUEST,
"Invalid rev format"));
}
}
finishOpenRevs();
} else {
return callback(errors.error(errors.UNKNOWN_ERROR,
'function_clause'));
}
}
return; // open_revs does not like other options
}
return this._get(id, opts, function (err, result) {
opts = utils.clone(opts);
if (err) {
return callback(err);
}
var doc = result.doc;
if (!doc) {
// a smoke test for something being very wrong
return callback(new Error('no doc!'));
}
var metadata = result.metadata;
var ctx = result.ctx;
if (opts.conflicts) {
var conflicts = merge.collectConflicts(metadata);
if (conflicts.length) {
doc._conflicts = conflicts;
}
}
if (opts.revs || opts.revs_info) {
var paths = merge.rootToLeaf(metadata.rev_tree);
var path = arrayFirst(paths, function (arr) {
return arr.ids.map(function (x) { return x.id; })
.indexOf(doc._rev.split('-')[1]) !== -1;
});
path.ids.splice(path.ids.map(function (x) {return x.id; })
.indexOf(doc._rev.split('-')[1]) + 1);
path.ids.reverse();
if (opts.revs) {
doc._revisions = {
start: (path.pos + path.ids.length) - 1,
ids: path.ids.map(function (rev) {
return rev.id;
})
};
}
if (opts.revs_info) {
var pos = path.pos + path.ids.length;
doc._revs_info = path.ids.map(function (rev) {
pos--;
return {
rev: pos + '-' + rev.id,
status: rev.opts.status
};
});
}
}
if (opts.local_seq) {
doc._local_seq = result.metadata.seq;
}
if (opts.attachments && doc._attachments) {
var attachments = doc._attachments;
var count = Object.keys(attachments).length;
if (count === 0) {
return callback(null, doc);
}
Object.keys(attachments).forEach(function (key) {
this._getAttachment(attachments[key],
{encode: true, ctx: ctx}, function (err, data) {
doc._attachments[key].data = data;
if (!--count) {
callback(null, doc);
}
});
}, self);
} else {
if (doc._attachments) {
for (var key in doc._attachments) {
if (doc._attachments.hasOwnProperty(key)) {
doc._attachments[key].stub = true;
}
}
}
callback(null, doc);
}
});
});
AbstractPouchDB.prototype.getAttachment =
utils.adapterFun('getAttachment', function (docId, attachmentId, opts,
callback) {
var self = this;
if (opts instanceof Function) {
callback = opts;
opts = {};
}
opts = utils.clone(opts);
this._get(docId, opts, function (err, res) {
if (err) {
return callback(err);
}
if (res.doc._attachments && res.doc._attachments[attachmentId]) {
opts.ctx = res.ctx;
self._getAttachment(res.doc._attachments[attachmentId], opts, callback);
} else {
return callback(errors.MISSING_DOC);
}
});
});
AbstractPouchDB.prototype.allDocs =
utils.adapterFun('allDocs', function (opts, callback) {
if (typeof opts === 'function') {
callback = opts;
opts = {};
}
opts = utils.clone(opts);
opts.skip = typeof opts.skip !== 'undefined' ? opts.skip : 0;
if ('keys' in opts) {
if (!Array.isArray(opts.keys)) {
return callback(new TypeError('options.keys must be an array'));
}
var incompatibleOpt =
['startkey', 'endkey', 'key'].filter(function (incompatibleOpt) {
return incompatibleOpt in opts;
})[0];
if (incompatibleOpt) {
callback(errors.error(errors.QUERY_PARSE_ERROR,
'Query parameter `' + incompatibleOpt +
'` is not compatible with multi-get'
));
return;
}
if (this.type() !== 'http') {
return allDocsKeysQuery(this, opts, callback);
}
}
return this._allDocs(opts, callback);
});
AbstractPouchDB.prototype.changes = function (opts, callback) {
if (typeof opts === 'function') {
callback = opts;
opts = {};
}
return new Changes(this, opts, callback);
};
AbstractPouchDB.prototype.close =
utils.adapterFun('close', function (callback) {
return this._close(callback);
});
AbstractPouchDB.prototype.info = utils.adapterFun('info', function (callback) {
var self = this;
this._info(function (err, info) {
if (err) {
return callback(err);
}
var len = self.prefix.length;
if (info.db_name.length > len &&
info.db_name.slice(0, len) === self.prefix) {
info.db_name = info.db_name.slice(len);
}
callback(null, info);
});
});
AbstractPouchDB.prototype.id = utils.adapterFun('id', function (callback) {
return this._id(callback);
});
AbstractPouchDB.prototype.type = function () {
return (typeof this._type === 'function') ? this._type() : this.adapter;
};
AbstractPouchDB.prototype.bulkDocs =
utils.adapterFun('bulkDocs', function (req, opts, callback) {
if (typeof opts === 'function') {
callback = opts;
opts = {};
}
if (!opts) {
opts = {};
} else {
opts = utils.clone(opts);
}
if (!req || !req.docs) {
return callback(errors.MISSING_BULK_DOCS);
}
if (!Array.isArray(req.docs)) {
return callback(errors.QUERY_PARSE_ERROR);
}
for (var i = 0; i < req.docs.length; ++i) {
if (typeof req.docs[i] !== 'object' || Array.isArray(req.docs[i])) {
return callback(errors.NOT_AN_OBJECT);
}
}
req = utils.clone(req);
if (!('new_edits' in opts)) {
if ('new_edits' in req) {
opts.new_edits = req.new_edits;
} else {
opts.new_edits = true;
}
}
return this._bulkDocs(req, opts, this.autoCompact(callback));
});
AbstractPouchDB.prototype.registerDependentDatabase =
utils.adapterFun('registerDependentDatabase', function (dependentDb,
callback) {
var depDB = new this.constructor(dependentDb, {adapter: this._adapter});
function diffFun(doc) {
doc.dependentDbs = doc.dependentDbs || {};
if (doc.dependentDbs[dependentDb]) {
return false; // no update required
}
doc.dependentDbs[dependentDb] = true;
return doc;
}
upsert(this, '_local/_pouch_dependentDbs', diffFun, function (err) {
if (err) {
return callback(err);
}
return callback(null, {db: depDB});
});
});
},{"./changes":6,"./deps/errors":10,"./deps/upsert":11,"./merge":16,"./utils":21,"events":24}],3:[function(_dereq_,module,exports){
"use strict";
var utils = _dereq_('../utils');
var errors = _dereq_('../deps/errors');
// parseUri 1.2.2
// (c) Steven Levithan <stevenlevithan.com>
// MIT License
function parseUri(str) {
var o = parseUri.options;
var m = o.parser[o.strictMode ? "strict" : "loose"].exec(str);
var uri = {};
var i = 14;
while (i--) {
uri[o.key[i]] = m[i] || "";
}
uri[o.q.name] = {};
uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
if ($1) {
uri[o.q.name][$1] = $2;
}
});
return uri;
}
function encodeDocId(id) {
if (/^_(design|local)/.test(id)) {
return id;
}
return encodeURIComponent(id);
}
parseUri.options = {
strictMode: false,
key: ["source", "protocol", "authority", "userInfo", "user", "password",
"host", "port", "relative", "path", "directory", "file", "query",
"anchor"],
q: {
name: "queryKey",
parser: /(?:^|&)([^&=]*)=?([^&]*)/g
},
parser: {
/* jshint maxlen: false */
strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
}
};
// Get all the information you possibly can about the URI given by name and
// return it as a suitable object.
function getHost(name, opts) {
// If the given name contains "http:"
if (/http(s?):/.test(name)) {
// Prase the URI into all its little bits
var uri = parseUri(name);
// Store the fact that it is a remote URI
uri.remote = true;
// Store the user and password as a separate auth object
if (uri.user || uri.password) {
uri.auth = {username: uri.user, password: uri.password};
}
// Split the path part of the URI into parts using '/' as the delimiter
// after removing any leading '/' and any trailing '/'
var parts = uri.path.replace(/(^\/|\/$)/g, '').split('/');
// Store the first part as the database name and remove it from the parts
// array
uri.db = parts.pop();
// Restore the path by joining all the remaining parts (all the parts
// except for the database name) with '/'s
uri.path = parts.join('/');
opts = opts || {};
opts = utils.clone(opts);
uri.headers = opts.headers || {};
if (opts.auth || uri.auth) {
var nAuth = opts.auth || uri.auth;
var token = utils.btoa(nAuth.username + ':' + nAuth.password);
uri.headers.Authorization = 'Basic ' + token;
}
if (opts.headers) {
uri.headers = opts.headers;
}
return uri;
}
// If the given name does not contain 'http:' then return a very basic object
// with no host, the current path, the given name as the database name and no
// username/password
return {host: '', path: '/', db: name, auth: false};
}
// Generate a URL with the host data given by opts and the given path
function genDBUrl(opts, path) {
return genUrl(opts, opts.db + '/' + path);
}
// Generate a URL with the host data given by opts and the given path
function genUrl(opts, path) {
if (opts.remote) {
// If the host already has a path, then we need to have a path delimiter
// Otherwise, the path delimiter is the empty string
var pathDel = !opts.path ? '' : '/';
// If the host already has a path, then we need to have a path delimiter
// Otherwise, the path delimiter is the empty string
return opts.protocol + '://' + opts.host + ':' + opts.port + '/' +
opts.path + pathDel + path;
}
return '/' + path;
}
// Implements the PouchDB API for dealing with CouchDB instances over HTTP
function HttpPouch(opts, callback) {
// The functions that will be publicly available for HttpPouch
var api = this;
api.getHost = opts.getHost ? opts.getHost : getHost;
// Parse the URI given by opts.name into an easy-to-use object
var host = api.getHost(opts.name, opts);
// Generate the database URL based on the host
var dbUrl = genDBUrl(host, '');
api.getUrl = function () {return dbUrl; };
var ajaxOpts = opts.ajax || {};
opts = utils.clone(opts);
function ajax(options, callback) {
return utils.ajax(utils.extend({}, ajaxOpts, options), callback);
}
var uuids = {
list: [],
get: function (opts, callback) {
if (typeof opts === 'function') {
callback = opts;
opts = {count: 10};
}
var cb = function (err, body) {
if (err || !('uuids' in body)) {
callback(err || errors.UNKNOWN_ERROR);
} else {
uuids.list = uuids.list.concat(body.uuids);
callback(null, "OK");
}
};
var params = '?count=' + opts.count;
ajax({
headers: host.headers,
method: 'GET',
url: genUrl(host, '_uuids') + params
}, cb);
}
};
// Create a new CouchDB database based on the given opts
var createDB = function () {
ajax({headers: host.headers, method: 'PUT', url: dbUrl},
function (err, ret) {
// If we get an "Unauthorized" error
if (err && err.status === 401) {
// Test if the database already exists
ajax({headers: host.headers, method: 'HEAD', url: dbUrl},
function (err, ret) {
// If there is still an error
if (err) {
// Give the error to the callback to deal with
callback(err);
} else {
// Continue as if there had been no errors
callback(null, api);
}
});
// If there were no errros or if the only error is "Precondition Failed"
// (note: "Precondition Failed" occurs when we try to create a database
// that already exists)
} else if (!err || err.status === 412) {
// Continue as if there had been no errors
callback(null, api);
} else {
callback(err);
}
});
};
if (!opts.skipSetup) {
ajax({headers: host.headers, method: 'GET', url: dbUrl},
function (err, ret) {
//check if the db exists
if (err) {
if (err.status === 404) {
//if it doesn't, create it
createDB();
} else {
callback(err);
}
} else {
//go do stuff with the db
callback(null, api);
}
});
}
api.type = function () {
return 'http';
};
api.id = utils.adapterFun('id', function (callback) {
ajax({
headers: host.headers,
method: 'GET',
url: genUrl(host, '')
}, function (err, result) {
if (err) {
callback(err);
} else {
var uuid = (result && result.uuid) ?
result.uuid + host.db : genDBUrl(host, '');
callback(null, uuid);
}
});
});
api.request = utils.adapterFun('request', function (options, callback) {
options.headers = host.headers;
options.url = genDBUrl(host, options.url);
ajax(options, callback);
});
// Sends a POST request to the host calling the couchdb _compact function
// version: The version of CouchDB it is running
api.compact = utils.adapterFun('compact', function (opts, callback) {
if (typeof opts === 'function') {
callback = opts;
opts = {};
}
opts = utils.clone(opts);
ajax({
headers: host.headers,
url: genDBUrl(host, '_compact'),
method: 'POST'
}, function () {
function ping() {
api.info(function (err, res) {
if (!res.compact_running) {
callback();
} else {
setTimeout(ping, opts.interval || 200);
}
});
}
// Ping the http if it's finished compaction
if (typeof callback === "function") {
ping();
}
});
});
// Calls GET on the host, which gets back a JSON string containing
// couchdb: A welcome string
// version: The version of CouchDB it is running
api._info = function (callback) {
ajax({
headers: host.headers,
method: 'GET',
url: genDBUrl(host, '')
}, function (err, res) {
if (err) {
callback(err);
} else {
res.host = genDBUrl(host, '');
callback(null, res);
}
});
};
// Get the document with the given id from the database given by host.
// The id could be solely the _id in the database, or it may be a
// _design/ID or _local/ID path
api.get = utils.adapterFun('get', function (id, opts, callback) {
// If no options were given, set the callback to the second parameter
if (typeof opts === 'function') {
callback = opts;
opts = {};
}
opts = utils.clone(opts);
if (opts.auto_encode === undefined) {
opts.auto_encode = true;
}
// List of parameters to add to the GET request
var params = [];
// If it exists, add the opts.revs value to the list of parameters.
// If revs=true then the resulting JSON will include a field
// _revisions containing an array of the revision IDs.
if (opts.revs) {
params.push('revs=true');
}
// If it exists, add the opts.revs_info value to the list of parameters.
// If revs_info=true then the resulting JSON will include the field
// _revs_info containing an array of objects in which each object
// representing an available revision.
if (opts.revs_info) {
params.push('revs_info=true');
}
if (opts.local_seq) {
params.push('local_seq=true');
}
// If it exists, add the opts.open_revs value to the list of parameters.
// If open_revs=all then the resulting JSON will include all the leaf
// revisions. If open_revs=["rev1", "rev2",...] then the resulting JSON
// will contain an array of objects containing data of all revisions
if (opts.open_revs) {
if (opts.open_revs !== "all") {
opts.open_revs = JSON.stringify(opts.open_revs);
}
params.push('open_revs=' + opts.open_revs);
}
// If it exists, add the opts.attachments value to the list of parameters.
// If attachments=true the resulting JSON will include the base64-encoded
// contents in the "data" property of each attachment.
if (opts.attachments) {
params.push('attachments=true');
}
// If it exists, add the opts.rev value to the list of parameters.
// If rev is given a revision number then get the specified revision.
if (opts.rev) {
params.push('rev=' + opts.rev);
}
// If it exists, add the opts.conflicts value to the list of parameters.
// If conflicts=true then the resulting JSON will include the field
// _conflicts containing all the conflicting revisions.
if (opts.conflicts) {
params.push('conflicts=' + opts.conflicts);
}
// Format the list of parameters into a valid URI query string
params = params.join('&');
params = params === '' ? '' : '?' + params;
if (opts.auto_encode) {
id = encodeDocId(id);
}
// Set the options for the ajax call
var options = {
headers: host.headers,
method: 'GET',
url: genDBUrl(host, id + params)
};
// If the given id contains at least one '/' and the part before the '/'
// is NOT "_design" and is NOT "_local"
// OR
// If the given id contains at least two '/' and the part before the first
// '/' is "_design".
// TODO This second condition seems strange since if parts[0] === '_design'
// then we already know that parts[0] !== '_local'.
var parts = id.split('/');
if ((parts.length > 1 && parts[0] !== '_design' && parts[0] !== '_local') ||
(parts.length > 2 && parts[0] === '_design' && parts[0] !== '_local')) {
// Binary is expected back from the server
options.binary = true;
}
// Get the document
ajax(options, function (err, doc, xhr) {
// If the document does not exist, send an error to the callback
if (err) {
return callback(err);
}
// Send the document to the callback
callback(null, doc, xhr);
});
});
// Delete the document given by doc from the database given by host.
api.remove = utils.adapterFun('remove', function (doc, opts, callback) {
// If no options were given, set the callback to be the second parameter
if (typeof opts === 'function') {
callback = opts;
opts = {};
}
// Delete the document
ajax({
headers: host.headers,
method: 'DELETE',
url: genDBUrl(host, encodeDocId(doc._id)) + '?rev=' + doc._rev
}, callback);
});
// Get the attachment
api.getAttachment =
utils.adapterFun('getAttachment', function (docId, attachmentId, opts,
callback) {
if (typeof opts === 'function') {
callback = opts;
opts = {};
}
opts = utils.clone(opts);
if (opts.auto_encode === undefined) {
opts.auto_encode = true;
}
if (opts.auto_encode) {
docId = encodeDocId(docId);
}
opts.auto_encode = false;
api.get(docId + '/' + attachmentId, opts, callback);
});
// Remove the attachment given by the id and rev
api.removeAttachment =
utils.adapterFun('removeAttachment', function (docId, attachmentId, rev,
callback) {
ajax({
headers: host.headers,
method: 'DELETE',
url: genDBUrl(host, encodeDocId(docId) + '/' + attachmentId) + '?rev=' +
rev
}, callback);
});
// Add the attachment given by blob and its contentType property
// to the document with the given id, the revision given by rev, and
// add it to the database given by host.
api.putAttachment =
utils.adapterFun('putAttachment', function (docId, attachmentId, rev, blob,
type, callback) {
if (typeof type === 'function') {
callback = type;
type = blob;
blob = rev;
rev = null;
}
if (typeof type === 'undefined') {
type = blob;
blob = rev;
rev = null;
}
var id = encodeDocId(docId) + '/' + attachmentId;
var url = genDBUrl(host, id);
if (rev) {
url += '?rev=' + rev;
}
var opts = {
headers: host.headers,
method: 'PUT',
url: url,
processData: false,
body: blob,
timeout: 60000
};
opts.headers['Content-Type'] = type;
// Add the attachment
ajax(opts, callback);
});
// Add the document given by doc (in JSON string format) to the database
// given by host. This fails if the doc has no _id field.
api.put = utils.adapterFun('put', utils.getArguments(function (args) {
var temp, temptype, opts, callback;
var doc = args.shift();
var id = '_id' in doc;
if (typeof doc !== 'object' || Array.isArray(doc)) {
callback = args.pop();
return callback(errors.NOT_AN_OBJECT);
}
doc = utils.clone(doc);
while (true) {
temp = args.shift();
temptype = typeof temp;
if (temptype === "string" && !id) {
doc._id = temp;
id = true;
} else if (temptype === "string" && id && !('_rev' in doc)) {
doc._rev = temp;
} else if (temptype === "object") {
opts = utils.clone(temp);
} else if (temptype === "function") {
callback = temp;
}
if (!args.length) {
break;
}
}
opts = opts || {};
var error = utils.invalidIdError(doc._id);
if (error) {
return callback(error);
}
// List of parameter to add to the PUT request
var params = [];
// If it exists, add the opts.new_edits value to the list of parameters.
// If new_edits = false then the database will NOT assign this document a
// new revision number
if (opts && typeof opts.new_edits !== 'undefined') {
params.push('new_edits=' + opts.new_edits);
}
// Format the list of parameters into a valid URI query string
params = params.join('&');
if (params !== '') {
params = '?' + params;
}
// Add the document
ajax({
headers: host.headers,
method: 'PUT',
url: genDBUrl(host, encodeDocId(doc._id)) + params,
body: doc
}, callback);
}));
// Add the document given by doc (in JSON string format) to the database
// given by host. This does not assume that doc is a new document
// (i.e. does not have a _id or a _rev field.)
api.post = utils.adapterFun('post', function (doc, opts, callback) {
// If no options were given, set the callback to be the second parameter
if (typeof opts === 'function') {
callback = opts;
opts = {};
}
opts = utils.clone(opts);
if (typeof doc !== 'object') {
return callback(errors.NOT_AN_OBJECT);
}
if (! ("_id" in doc)) {
if (uuids.list.length > 0) {
doc._id = uuids.list.pop();
api.put(doc, opts, callback);
} else {
uuids.get(function (err, resp) {
if (err) {
return callback(errors.UNKNOWN_ERROR);
}
doc._id = uuids.list.pop();
api.put(doc, opts, callback);
});
}
} else {
api.put(doc, opts, callback);
}
});
// Update/create multiple documents given by req in the database
// given by host.
api.bulkDocs = utils.adapterFun('bulkDocs', function (req, opts, callback) {
// If no options were given, set the callback to be the second parameter
if (typeof opts === 'function') {
callback = opts;
opts = {};
}
if (!opts) {
opts = {};
}
if (!Array.isArray(req.docs)) {
return callback(
errors.error(
errors.NOT_AN_OBJECT, "Missing JSON list of 'docs'"));
}
var bad = req.docs.filter(function (doc) {
return typeof doc !== 'object' || Array.isArray(doc);
});
if (bad.length) {
return callback(errors.NOT_AN_OBJECT);
}
req = utils.clone(req);
opts = utils.clone(opts);
// If opts.new_edits exists add it to the document data to be
// send to the database.
// If new_edits=false then it prevents the database from creating
// new revision numbers for the documents. Instead it just uses
// the old ones. This is used in database replication.
if (typeof opts.new_edits !== 'undefined') {
req.new_edits = opts.new_edits;
}
// Update/create the documents
ajax({
headers: host.headers,
method: 'POST',
url: genDBUrl(host, '_bulk_docs'),
body: req
}, callback);
});
// Get a listing of the documents in the database given
// by host and ordered by increasing id.
api.allDocs = utils.adapterFun('allDocs', function (opts, callback) {
if (typeof opts === 'function') {
callback = opts;
opts = {};
}
opts = utils.clone(opts);
// List of parameters to add to the GET request
var params = [];
var body;
var method = 'GET';
// TODO I don't see conflicts as a valid parameter for a
// _all_docs request
// (see http://wiki.apache.org/couchdb/HTTP_Document_API#all_docs)
if (opts.conflicts) {
params.push('conflicts=true');
}
// If opts.descending is truthy add it to params
if (opts.descending) {
params.push('descending=true');
}
// If opts.include_docs exists, add the include_docs value to the
// list of parameters.
// If include_docs=true then include the associated document with each
// result.
if (opts.include_docs) {
params.push('include_docs=true');
}
if (opts.key) {
params.push('key=' + encodeURIComponent(JSON.stringify(opts.key)));
}
// If opts.startkey exists, add the startkey value to the list of
// parameters.
// If startkey is given then the returned list of documents will
// start with the document whose id is startkey.
if (opts.startkey) {
params.push('startkey=' +
encodeURIComponent(JSON.stringify(opts.startkey)));
}
// If opts.endkey exists, add the endkey value to the list of parameters.
// If endkey is given then the returned list of docuemnts will
// end with the document whose id is endkey.
if (opts.endkey) {
params.push('endkey=' + encodeURIComponent(JSON.stringify(opts.endkey)));
}
// If opts.limit exists, add the limit value to the parameter list.
if (typeof opts.limit !== 'undefined') {
params.push('limit=' + opts.limit);
}
if (typeof opts.skip !== 'undefined') {
params.push('skip=' + opts.skip);
}
// Format the list of parameters into a valid URI query string
params = params.join('&');
if (params !== '') {
params = '?' + params;
}
if (typeof opts.keys !== 'undefined') {
var MAX_URL_LENGTH = 2000;
// according to http://stackoverflow.com/a/417184/680742,
// the de factor URL length limit is 2000 characters
var keysAsString =
'keys=' + encodeURIComponent(JSON.stringify(opts.keys));
if (keysAsString.length + params.length + 1 <= MAX_URL_LENGTH) {
// If the keys are short enough, do a GET. we do this to work around
// Safari not understanding 304s on POSTs (see issue #1239)
params += (params.indexOf('?') !== -1 ? '&' : '?') + keysAsString;
} else {
// If keys are too long, issue a POST request to circumvent GET
// query string limits
// see http://wiki.apache.org/couchdb/HTTP_view_API#Querying_Options
method = 'POST';
body = JSON.stringify({keys: opts.keys});
}
}
// Get the document listing
ajax({
headers: host.headers,
method: method,
url: genDBUrl(host, '_all_docs' + params),
body: body
}, callback);
});
// Get a list of changes made to documents in the database given by host.
// TODO According to the README, there should be two other methods here,
// api.changes.addListener and api.changes.removeListener.
api._changes = function (opts) {
// We internally page the results of a changes request, this means
// if there is a large set of changes to be returned we can start
// processing them quicker instead of waiting on the entire
// set of changes to return and attempting to process them at once
var CHANGES_LIMIT = 25;
opts = utils.clone(opts);
opts.timeout = opts.timeout || 0;
// set timeout to 20s to prevent aborting via Ajax timeout
var params = { timeout: 20 * 1000 };
var limit = (typeof opts.limit !== 'undefined') ? opts.limit : false;
if (limit === 0) {
limit = 1;
}
var returnDocs;
if ('returnDocs' in opts) {
returnDocs = opts.returnDocs;
} else {
returnDocs = true;
}
//
var leftToFetch = limit;
if (opts.style) {
params.style = opts.style;
}
if (opts.include_docs || opts.filter && typeof opts.filter === 'function') {
params.include_docs = true;
}
if (opts.continuous) {
params.feed = 'longpoll';
}
if (opts.conflicts) {
params.conflicts = true;
}
if (opts.descending) {
params.descending = true;
}
if (opts.filter && typeof opts.filter === 'string') {
params.filter = opts.filter;
if (opts.filter === '_view' &&
opts.view &&
typeof opts.view === 'string') {
params.view = opts.view;
}
}
// If opts.query_params exists, pass it through to the changes request.
// These parameters may be used by the filter on the source database.
if (opts.query_params && typeof opts.query_params === 'object') {
for (var param_name in opts.query_params) {
if (opts.query_params.hasOwnProperty(param_name)) {
params[param_name] = opts.query_params[param_name];
}
}
}
var xhr;
var lastFetchedSeq;
// Get all the changes starting wtih the one immediately after the
// sequence number given by since.
var fetch = function (since, callback) {
if (opts.aborted) {
return;
}
params.since = since;
if (opts.descending) {
if (limit) {
params.limit = leftToFetch;
}
} else {
params.limit = (!limit || leftToFetch > CHANGES_LIMIT) ?
CHANGES_LIMIT : leftToFetch;
}
var paramStr = '?' + Object.keys(params).map(function (k) {
return k + '=' + params[k];
}).join('&');
// Set the options for the ajax call
var xhrOpts = {
headers: host.headers,
method: 'GET',
url: genDBUrl(host, '_changes' + paramStr),
// _changes can take a long time to generate, especially when filtered
timeout: opts.timeout
};
lastFetchedSeq = since;
if (opts.aborted) {
return;
}
// Get the changes
xhr = ajax(xhrOpts, callback);
};
// If opts.since exists, get all the changes from the sequence
// number given by opts.since. Otherwise, get all the changes
// from the sequence number 0.
var fetchTimeout = 10;
var fetchRetryCount = 0;
var results = {results: []};
var fetched = function (err, res) {
if (opts.aborted) {
return;
}
var raw_results_length = 0;
// If the result of the ajax call (res) contains changes (res.results)
if (res && res.results) {
raw_results_length = res.results.length;
results.last_seq = res.last_seq;
// For each change
var req = {};
req.query = opts.query_params;
res.results = res.results.filter(function (c) {
leftToFetch--;
var ret = utils.filterChange(opts)(c);
if (ret) {
if (returnDocs) {
results.results.push(c);
}
utils.call(opts.onChange, c);
}
return ret;
});
} else if (err) {
// In case of an error, stop listening for changes and call
// opts.complete
opts.aborted = true;
utils.call(opts.complete, err);
return;
}
// The changes feed may have timed out with no results
// if so reuse last update sequence
if (res && res.last_seq) {
lastFetchedSeq = res.last_seq;
}
var finished = (limit && leftToFetch <= 0) ||
(res && raw_results_length < CHANGES_LIMIT) ||
(opts.descending);
if ((opts.continuous && !(limit && leftToFetch <= 0)) || !finished) {
// Increase retry delay exponentially as long as errors persist
if (err) {
fetchRetryCount += 1;
} else {
fetchRetryCount = 0;
}
var timeoutMultiplier = 1 << fetchRetryCount;
var retryWait = fetchTimeout * timeoutMultiplier;
var maximumWait = opts.maximumWait || 30000;
if (retryWait > maximumWait) {
utils.call(opts.complete, err || errors.UNKNOWN_ERROR);
return;
}
// Queue a call to fetch again with the newest sequence number
setTimeout(function () { fetch(lastFetchedSeq, fetched); }, retryWait);
} else {
// We're done, call the callback
utils.call(opts.complete, null, results);
}
};
fetch(opts.since || 0, fetched);
// Return a method to cancel this method from processing any more
return {
cancel: function () {
opts.aborted = true;
if (xhr) {
xhr.abort();
}
}
};
};
// Given a set of document/revision IDs (given by req), tets the subset of
// those that do NOT correspond to revisions stored in the database.
// See http://wiki.apache.org/couchdb/HttpPostRevsDiff
api.revsDiff = utils.adapterFun('revsDif', function (req, opts, callback) {
// If no options were given, set the callback to be the second parameter
if (typeof opts === 'function') {
callback = opts;
opts = {};
}
// Get the missing document/revision IDs
ajax({
headers: host.headers,
method: 'POST',
url: genDBUrl(host, '_revs_diff'),
body: req
}, function (err, res) {
callback(err, res);
});
});
api.close = utils.adapterFun('close', function (callback) {
callback();
});
function replicateOnServer(target, opts, promise, targetHostUrl) {
opts = utils.clone(opts);
var targetHost = api.getHost(targetHostUrl);
var params = {
source: host.db,
target: targetHost.protocol === host.protocol &&
targetHost.authority ===
host.authority ? targetHost.db : targetHost.source
};
if (opts.continuous) {
params.continuous = true;
}
if (opts.create_target) {
params.create_target = true;
}
if (opts.doc_ids) {
params.doc_ids = opts.doc_ids;
}
if (opts.filter && typeof opts.filter === 'string') {
params.filter = opts.filter;
}
if (opts.query_params) {
params.query_params = opts.query_params;
}
var result = {};
var repOpts = {
headers: host.headers,
method: 'POST',
url: genUrl(host, '_replicate'),
body: params
};
var xhr;
promise.cancel = function () {
this.cancelled = true;
if (xhr && !result.ok) {
xhr.abort();
}
if (result._local_id) {
repOpts.body = {
replication_id: result._local_id
};
}
repOpts.body.cancel = true;
ajax(repOpts, function (err, resp, xhr) {
// If the replication cancel request fails, send an error to the
// callback
if (err) {
return callback(err);
}
// Send the replication cancel result to the complete callback
utils.call(opts.complete, null, result, xhr);
});
};
if (promise.cancelled) {
return;
}
xhr = ajax(repOpts, function (err, resp, xhr) {
// If the replication fails, send an error to the callback
if (err) {
return callback(err);
}
result.ok = true;
// Provided by CouchDB from 1.2.0 onward to cancel replication
if (resp._local_id) {
result._local_id = resp._local_id;
}
// Send the replication result to the complete callback
utils.call(opts.complete, null, resp, xhr);
});
}
api.replicateOnServer = function (target, opts, promise) {
if (!api.taskqueue.isReady) {
api.taskqueue.addTask('replicateOnServer', [target, opts, promise]);
return promise;
}
target.info(function (err, info) {
replicateOnServer(target, opts, promise, info.host);
});
};
api.destroy = utils.adapterFun('destroy', function (callback) {
ajax({
url: genDBUrl(host, ''),
method: 'DELETE',
headers: host.headers
}, function (err, resp) {
if (err) {
api.emit('error', err);
callback(err);
} else {
api.emit('destroyed');
callback(null, resp);
}
});
});
}
// Delete the HttpPouch specified by the given name.
HttpPouch.destroy = utils.toPromise(function (name, opts, callback) {
var host = getHost(name, opts);
opts = opts || {};
if (typeof opts === 'function') {
callback = opts;
opts = {};
}
opts = utils.clone(opts);
opts.headers = host.headers;
opts.method = 'DELETE';
opts.url = genDBUrl(host, '');
var ajaxOpts = opts.ajax || {};
opts = utils.extend({}, opts, ajaxOpts);
utils.ajax(opts, callback);
});
// HttpPouch is a valid adapter.
HttpPouch.valid = function () {
return true;
};
module.exports = HttpPouch;
},{"../deps/errors":10,"../utils":21}],4:[function(_dereq_,module,exports){
(function (global){
'use strict';
var utils = _dereq_('../utils');
var merge = _dereq_('../merge');
var errors = _dereq_('../deps/errors');
function idbError(callback) {
return function (event) {
callback(errors.error(errors.IDB_ERROR, event.target, event.type));
};
}
function isModernIdb() {
// check for outdated implementations of IDB
// that rely on the setVersion method instead of onupgradeneeded (issue #1207)
// cache based on appVersion, in case the browser is updated
var cacheKey = "_pouch__checkModernIdb_" +
(global.navigator && global.navigator.appVersion);
var cached = utils.hasLocalStorage() && global.localStorage[cacheKey];
if (cached) {
return JSON.parse(cached);
}
var dbName = '_pouch__checkModernIdb';
var result = global.indexedDB.open(dbName, 1).onupgradeneeded === null;
if (global.indexedDB.deleteDatabase) {
global.indexedDB.deleteDatabase(dbName); // db no longer needed
}
if (utils.hasLocalStorage()) {
global.localStorage[cacheKey] = JSON.stringify(result); // cache
}
return result;
}
function IdbPouch(opts, callback) {
// IndexedDB requires a versioned database structure, so we use the
// version here to manage migrations.
var ADAPTER_VERSION = 2;
// The object stores created for each database
// DOC_STORE stores the document meta data, its revision history and state
var DOC_STORE = 'document-store';
// BY_SEQ_STORE stores a particular version of a document, keyed by its
// sequence id
var BY_SEQ_STORE = 'by-sequence';
// Where we store attachments
var ATTACH_STORE = 'attach-store';
// Where we store meta data
var META_STORE = 'meta-store';
// Where we detect blob support
var DETECT_BLOB_SUPPORT_STORE = 'detect-blob-support';
var name = opts.name;
var req = global.indexedDB.open(name, ADAPTER_VERSION);
var docCount = -1;
if (!('openReqList' in IdbPouch)) {
IdbPouch.openReqList = {};
}
IdbPouch.openReqList[name] = req;
var blobSupport = null;
var instanceId = null;
var api = this;
var idb = null;
req.onupgradeneeded = function (e) {
var db = e.target.result;
if (e.oldVersion < 1) {
// initial schema
createSchema(db);
}
if (e.oldVersion < 2) {
// version 2 adds the deletedOrLocal index
addDeletedOrLocalIndex(e);
}
};
function createSchema(db) {
db.createObjectStore(DOC_STORE, {keyPath : 'id'})
.createIndex('seq', 'seq', {unique: true});
db.createObjectStore(BY_SEQ_STORE, {autoIncrement: true})
.createIndex('_doc_id_rev', '_doc_id_rev', {unique: true});
db.createObjectStore(ATTACH_STORE, {keyPath: 'digest'});
db.createObjectStore(META_STORE, {keyPath: 'id', autoIncrement: false});
db.createObjectStore(DETECT_BLOB_SUPPORT_STORE);
}
function addDeletedOrLocalIndex(e) {
var docStore = e.currentTarget.transaction.objectStore(DOC_STORE);
docStore.openCursor().onsuccess = function (event) {
var cursor = event.target.result;
if (cursor) {
var metadata = cursor.value;
var deleted = utils.isDeleted(metadata);
var local = utils.isLocalId(metadata.id);
metadata.deletedOrLocal = (deleted || local) ? "1" : "0";
docStore.put(metadata);
cursor['continue']();
} else {
docStore.createIndex('deletedOrLocal',
'deletedOrLocal', {unique : false});
}
};
}
req.onsuccess = function (e) {
idb = e.target.result;
idb.onversionchange = function () {
idb.close();
};
var txn = idb.transaction([META_STORE, DETECT_BLOB_SUPPORT_STORE],
'readwrite');
var req = txn.objectStore(META_STORE).get(META_STORE);
req.onsuccess = function (e) {
var idStored = false;
var checkSetupComplete = function () {
if (blobSupport === null || !idStored) {
return;
} else {
callback(null, api);
}
};
var meta = e.target.result || {id: META_STORE};
if (name + '_id' in meta) {
instanceId = meta[name + '_id'];
idStored = true;
checkSetupComplete();
} else {
instanceId = utils.uuid();
meta[name + '_id'] = instanceId;
txn.objectStore(META_STORE).put(meta).onsuccess = function () {
idStored = true;
checkSetupComplete();
};
}
// detect blob support
try {
txn.objectStore(DETECT_BLOB_SUPPORT_STORE).put(utils.createBlob(),
"key");
blobSupport = true;
} catch (err) {
blobSupport = false;
} finally {
checkSetupComplete();
}
};
};
req.onerror = idbError(callback);
api.type = function () {
return 'idb';
};
api._id = utils.toPromise(function (callback) {
callback(null, instanceId);
});
api._bulkDocs = function idb_bulkDocs(req, opts, callback) {
var newEdits = opts.new_edits;
var userDocs = req.docs;
// Parse the docs, give them a sequence number for the result
var docInfos = userDocs.map(function (doc, i) {
var newDoc = utils.parseDoc(doc, newEdits);
newDoc._bulk_seq = i;
return newDoc;
});
var docInfoErrors = docInfos.filter(function (docInfo) {
return docInfo.error;
});
if (docInfoErrors.length) {
return callback(docInfoErrors[0]);
}
var results = [];
var docsWritten = 0;
function writeMetaData(e) {
var meta = e.target.result;
meta.updateSeq = (meta.updateSeq || 0) + docsWritten;
txn.objectStore(META_STORE).put(meta);
}
function processDocs() {
if (!docInfos.length) {
txn.objectStore(META_STORE).get(META_STORE).onsuccess = writeMetaData;
return;
}
var currentDoc = docInfos.shift();
var req = txn.objectStore(DOC_STORE).get(currentDoc.metadata.id);
req.onsuccess = function process_docRead(event) {
var oldDoc = event.target.result;
if (!oldDoc) {
insertDoc(currentDoc);
} else {
updateDoc(oldDoc, currentDoc);
}
};
}
function complete(event) {
var aresults = [];
results.sort(sortByBulkSeq);
results.forEach(function (result) {
delete result._bulk_seq;
if (result.error) {
aresults.push(result);
return;
}
var metadata = result.metadata;
var rev = merge.winningRev(metadata);
aresults.push({
ok: true,
id: metadata.id,
rev: rev
});
if (utils.isLocalId(metadata.id)) {
return;
}
IdbPouch.Changes.notify(name);
IdbPouch.Changes.notifyLocalWindows(name);
});
docCount = -1; // invalidate
callback(null, aresults);
}
function preprocessAttachment(att, finish) {
if (att.stub) {
return finish();
}
if (typeof att.data === 'string') {
var data;
try {
data = atob(att.data);
} catch (e) {
var err = errors.error(errors.BAD_ARG,
"Attachments need to be base64 encoded");
return callback(err);
}
att.digest = 'md5-' + utils.MD5(data);
if (blobSupport) {
var type = att.content_type;
data = utils.fixBinary(data);
att.data = utils.createBlob([data], {type: type});
}
return finish();
}
var reader = new FileReader();
reader.onloadend = function (e) {
var binary = utils.arrayBufferToBinaryString(this.result);
att.digest = 'md5-' + utils.MD5(binary);
if (!blobSupport) {
att.data = btoa(binary);
}
finish();
};
reader.readAsArrayBuffer(att.data);
}
function preprocessAttachments(callback) {
if (!docInfos.length) {
return callback();
}
var docv = 0;
docInfos.forEach(function (docInfo) {
var attachments = docInfo.data && docInfo.data._attachments ?
Object.keys(docInfo.data._attachments) : [];
if (!attachments.length) {
return done();
}
var recv = 0;
function attachmentProcessed() {
recv++;
if (recv === attachments.length) {
done();
}
}
for (var key in docInfo.data._attachments) {
if (docInfo.data._attachments.hasOwnProperty(key)) {
preprocessAttachment(docInfo.data._attachments[key],
attachmentProcessed);
}
}
});
function done() {
docv++;
if (docInfos.length === docv) {
callback();
}
}
}
function writeDoc(docInfo, winningRev, deleted, callback) {
var err = null;
var recv = 0;
docInfo.data._id = docInfo.metadata.id;
docInfo.data._rev = docInfo.metadata.rev;
docsWritten++;
if (deleted) {
docInfo.data._deleted = true;
}
var attachments = docInfo.data._attachments ?
Object.keys(docInfo.data._attachments) : [];
function collectResults(attachmentErr) {
if (!err) {
if (attachmentErr) {
err = attachmentErr;
callback(err);
} else if (recv === attachments.length) {
finish();
}
}
}
function attachmentSaved(err) {
recv++;
collectResults(err);
}
for (var key in docInfo.data._attachments) {
if (!docInfo.data._attachments[key].stub) {
var data = docInfo.data._attachments[key].data;
delete docInfo.data._attachments[key].data;
var digest = docInfo.data._attachments[key].digest;
saveAttachment(docInfo, digest, data, attachmentSaved);
} else {
recv++;
collectResults();
}
}
function finish() {
docInfo.data._doc_id_rev = docInfo.data._id + "::" + docInfo.data._rev;
var index = txn.objectStore(BY_SEQ_STORE).index('_doc_id_rev');
index.getKey(docInfo.data._doc_id_rev).onsuccess = function (e) {
var dataReq = e.target.result ?
txn.objectStore(BY_SEQ_STORE).put(docInfo.data, e.target.result) :
txn.objectStore(BY_SEQ_STORE).put(docInfo.data);
dataReq.onsuccess = function (e) {
var metadata = docInfo.metadata;
metadata.seq = e.target.result;
// Current _rev is calculated from _rev_tree on read
delete metadata.rev;
var local = utils.isLocalId(metadata.id);
metadata.deletedOrLocal = (deleted || local) ? "1" : "0";
metadata.winningRev = merge.winningRev(metadata);
var metaDataReq = txn.objectStore(DOC_STORE).put(metadata);
metaDataReq.onsuccess = function () {
delete metadata.deletedOrLocal;
delete metadata.winningRev;
results.push(docInfo);
utils.call(callback);
};
};
};
}
if (!attachments.length) {
finish();
}
}
function updateDoc(oldDoc, docInfo) {
var winningRev = merge.winningRev(docInfo.metadata);
var deleted = utils.isDeleted(docInfo.metadata, winningRev);
var merged =
merge.merge(oldDoc.rev_tree, docInfo.metadata.rev_tree[0], 1000);
var wasPreviouslyDeleted = utils.isDeleted(oldDoc);
var inConflict = (wasPreviouslyDeleted && deleted) ||
(!wasPreviouslyDeleted && newEdits && merged.conflicts !== 'new_leaf');
if (inConflict) {
results.push(makeErr(errors.REV_CONFLICT, docInfo._bulk_seq));
return processDocs();
}
docInfo.metadata.rev_tree = merged.tree;
writeDoc(docInfo, winningRev, deleted, processDocs);
}
function insertDoc(docInfo) {
var winningRev = merge.winningRev(docInfo.metadata);
var deleted = utils.isDeleted(docInfo.metadata, winningRev);
// Cant insert new deleted documents
if ('was_delete' in opts && deleted) {
results.push(errors.MISSING_DOC);
return processDocs();
}
writeDoc(docInfo, winningRev, deleted, processDocs);
}
// Insert sequence number into the error so we can sort later
function makeErr(err, seq) {
err._bulk_seq = seq;
return err;
}
function saveAttachment(docInfo, digest, data, callback) {
var objectStore = txn.objectStore(ATTACH_STORE);
objectStore.get(digest).onsuccess = function (e) {
var originalRefs = e.target.result && e.target.result.refs || {};
var ref = [docInfo.metadata.id, docInfo.metadata.rev].join('@');
var newAtt = {
digest: digest,
body: data,
refs: originalRefs
};
newAtt.refs[ref] = true;
objectStore.put(newAtt).onsuccess = function (e) {
utils.call(callback);
};
};
}
var txn;
preprocessAttachments(function () {
txn = idb.transaction([DOC_STORE, BY_SEQ_STORE, ATTACH_STORE, META_STORE],
'readwrite');
txn.onerror = idbError(callback);
txn.ontimeout = idbError(callback);
txn.oncomplete = complete;
processDocs();
});
};
function sortByBulkSeq(a, b) {
return a._bulk_seq - b._bulk_seq;
}
// First we look up the metadata in the ids database, then we fetch the
// current revision(s) from the by sequence store
api._get = function idb_get(id, opts, callback) {
var doc;
var metadata;
var err;
var txn;
opts = utils.clone(opts);
if (opts.ctx) {
txn = opts.ctx;
} else {
txn =
idb.transaction([DOC_STORE, BY_SEQ_STORE, ATTACH_STORE], 'readonly');
}
function finish() {
callback(err, {doc: doc, metadata: metadata, ctx: txn});
}
txn.objectStore(DOC_STORE).get(id).onsuccess = function (e) {
metadata = e.target.result;
// we can determine the result here if:
// 1. there is no such document
// 2. the document is deleted and we don't ask about specific rev
// When we ask with opts.rev we expect the answer to be either
// doc (possibly with _deleted=true) or missing error
if (!metadata) {
err = errors.MISSING_DOC;
return finish();
}
if (utils.isDeleted(metadata) && !opts.rev) {
err = errors.error(errors.MISSING_DOC, "deleted");
return finish();
}
var objectStore = txn.objectStore(BY_SEQ_STORE);
// metadata.winningRev was added later, so older DBs might not have it
var rev = opts.rev || metadata.winningRev || merge.winningRev(metadata);
var key = metadata.id + '::' + rev;
objectStore.index('_doc_id_rev').get(key).onsuccess = function (e) {
doc = e.target.result;
if (doc && doc._doc_id_rev) {
delete(doc._doc_id_rev);
}
if (!doc) {
err = errors.MISSING_DOC;
return finish();
}
finish();
};
};
};
api._getAttachment = function (attachment, opts, callback) {
var result;
var txn;
opts = utils.clone(opts);
if (opts.ctx) {
txn = opts.ctx;
} else {
txn =
idb.transaction([DOC_STORE, BY_SEQ_STORE, ATTACH_STORE], 'readonly');
}
var digest = attachment.digest;
var type = attachment.content_type;
txn.objectStore(ATTACH_STORE).get(digest).onsuccess = function (e) {
var data = e.target.result.body;
if (opts.encode) {
if (blobSupport) {
var reader = new FileReader();
reader.onloadend = function (e) {
var binary = utils.arrayBufferToBinaryString(this.result);
result = btoa(binary);
callback(null, result);
};
reader.readAsArrayBuffer(data);
} else {
result = data;
callback(null, result);
}
} else {
if (blobSupport) {
result = data;
} else {
data = utils.fixBinary(atob(data));
result = utils.createBlob([data], {type: type});
}
callback(null, result);
}
};
};
function allDocsQuery(totalRows, opts, callback) {
var start = 'startkey' in opts ? opts.startkey : false;
var end = 'endkey' in opts ? opts.endkey : false;
var key = 'key' in opts ? opts.key : false;
var skip = opts.skip || 0;
var limit = typeof opts.limit === 'number' ? opts.limit : -1;
var descending = 'descending' in opts && opts.descending ? 'prev' : null;
var manualDescEnd = false;
if (descending && start && end) {
// unfortunately IDB has a quirk where IDBKeyRange.bound is invalid if the
// start is less than the end, even in descending mode. Best bet
// is just to handle it manually in that case.
manualDescEnd = end;
end = false;
}
var keyRange;
try {
keyRange = start && end ? global.IDBKeyRange.bound(start, end)
: start ? (descending ? global.IDBKeyRange.upperBound(start)
: global.IDBKeyRange.lowerBound(start))
: end ? (descending ? global.IDBKeyRange.lowerBound(end)
: global.IDBKeyRange.upperBound(end))
: key ? global.IDBKeyRange.only(key) : null;
} catch (e) {
if (e.name === "DataError" && e.code === 0) {
// data error, start is less than end
return callback(null, {
total_rows : totalRows,
offset : opts.skip,
rows : []
});
} else {
return callback(errors.error(errors.IDB_ERROR, e.name, e.message));
}
}
var transaction = idb.transaction([DOC_STORE, BY_SEQ_STORE], 'readonly');
transaction.oncomplete = function () {
callback(null, {
total_rows: totalRows,
offset: opts.skip,
rows: results
});
};
var oStore = transaction.objectStore(DOC_STORE);
var oCursor = descending ? oStore.openCursor(keyRange, descending)
: oStore.openCursor(keyRange);
var results = [];
oCursor.onsuccess = function (e) {
if (!e.target.result) {
return;
}
var cursor = e.target.result;
var metadata = cursor.value;
// metadata.winningRev added later, some dbs might be missing it
var winningRev = metadata.winningRev || merge.winningRev(metadata);
function allDocsInner(metadata, data) {
if (utils.isLocalId(metadata.id)) {
return cursor['continue']();
}
var doc = {
id: metadata.id,
key: metadata.id,
value: {
rev: winningRev
}
};
if (opts.include_docs) {
doc.doc = data;
doc.doc._rev = winningRev;
if (doc.doc._doc_id_rev) {
delete(doc.doc._doc_id_rev);
}
if (opts.conflicts) {
doc.doc._conflicts = merge.collectConflicts(metadata);
}
for (var att in doc.doc._attachments) {
if (doc.doc._attachments.hasOwnProperty(att)) {
doc.doc._attachments[att].stub = true;
}
}
}
var deleted = utils.isDeleted(metadata, winningRev);
if (opts.deleted === 'ok') {
// deleted docs are okay with keys_requests
if (deleted) {
doc.value.deleted = true;
doc.doc = null;
}
results.push(doc);
} else if (!deleted && skip-- <= 0) {
if (manualDescEnd && doc.key < manualDescEnd) {
return;
}
results.push(doc);
if (--limit === 0) {
return;
}
}
cursor['continue']();
}
if (!opts.include_docs) {
allDocsInner(metadata);
} else {
var index = transaction.objectStore(BY_SEQ_STORE).index('_doc_id_rev');
var key = metadata.id + "::" + winningRev;
index.get(key).onsuccess = function (event) {
allDocsInner(cursor.value, event.target.result);
};
}
};
}
function countDocs(callback) {
if (docCount !== -1) {
return callback(null, docCount);
}
var count;
var txn = idb.transaction([DOC_STORE], 'readonly');
var index = txn.objectStore(DOC_STORE).index('deletedOrLocal');
index.count(global.IDBKeyRange.only("0")).onsuccess = function (e) {
count = e.target.result;
};
txn.onerror = idbError(callback);
txn.oncomplete = function () {
docCount = count;
callback(null, docCount);
};
}
api._allDocs = function idb_allDocs(opts, callback) {
// first count the total_rows
countDocs(function (err, totalRows) {
if (err) {
return callback(err);
}
if (opts.limit === 0) {
return callback(null, {
total_rows : totalRows,
offset : opts.skip,
rows : []
});
}
allDocsQuery(totalRows, opts, callback);
});
};
api._info = function idb_info(callback) {
countDocs(function (err, count) {
if (err) {
return callback(err);
}
if (idb === null) {
var error = new Error('db isn\'t open');
error.id = 'idbNull';
return callback(error);
}
var updateSeq = 0;
var txn = idb.transaction([META_STORE], 'readonly');
txn.objectStore(META_STORE).get(META_STORE).onsuccess = function (e) {
updateSeq = e.target.result && e.target.result.updateSeq || 0;
};
txn.oncomplete = function () {
callback(null, {
db_name: name,
doc_count: count,
update_seq: updateSeq
});
};
});
};
api._changes = function (opts) {
opts = utils.clone(opts);
if (opts.continuous) {
var id = name + ':' + utils.uuid();
IdbPouch.Changes.addListener(name, id, api, opts);
IdbPouch.Changes.notify(name);
return {
cancel: function () {
IdbPouch.Changes.removeListener(name, id);
}
};
}
var descending = opts.descending ? 'prev' : null;
var lastSeq = 0;
// Ignore the `since` parameter when `descending` is true
opts.since = opts.since && !descending ? opts.since : 0;
var limit = 'limit' in opts ?