com.vaadin.polymer.public.bower_components.pouchdb-find.www.pouchdb-3.2.2-prerelease.js Maven / Gradle / Ivy
The newest version!
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)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);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o 0) ? opts.keys.slice(opts.skip) : opts.keys;
if (opts.descending) {
keys.reverse();
}
if (!keys.length) {
return api._allDocs({limit: 0}, callback);
}
var finalResults = {
offset: opts.skip
};
return Promise.all(keys.map(function (key) {
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.total_rows = res.total_rows;
resolve(res.rows[0] || {key: key, error: 'not_found'});
});
});
})).then(function (results) {
finalResults.rows = results;
return finalResults;
});
}
utils.inherits(AbstractPouchDB, EventEmitter);
module.exports = AbstractPouchDB;
function AbstractPouchDB() {
var self = this;
EventEmitter.call(this);
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: 'now',
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.error(errors.NOT_AN_OBJECT));
}
this.bulkDocs({docs: [doc]}, opts, 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.error(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);
}
if (utils.isLocalId(doc._id) && typeof this._putLocal === 'function') {
if (doc._deleted) {
return this._removeLocal(doc, callback);
} else {
return this._putLocal(doc, callback);
}
}
this.bulkDocs({docs: [doc]}, opts, 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
};
return api.put(doc);
}
return api.get(docId).then(function (doc) {
if (doc._rev !== rev) {
throw errors.error(errors.REV_CONFLICT);
}
return createAttachment(doc);
}, function (err) {
// create new doc
if (err.reason === errors.MISSING_DOC.message) {
return createAttachment({_id: docId});
} else {
throw err;
}
});
});
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.error(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 (docOrId, optsOrRev, opts, callback) {
var doc;
if (typeof optsOrRev === 'string') {
// id, rev, opts, callback style
doc = {
_id: docOrId,
_rev: optsOrRev
};
if (typeof opts === 'function') {
callback = opts;
opts = {};
}
} else {
// doc, opts, callback style
doc = docOrId;
if (typeof optsOrRev === 'function') {
callback = optsOrRev;
opts = {};
} else {
callback = opts;
opts = optsOrRev;
}
}
opts = utils.clone(opts || {});
opts.was_delete = true;
var newDoc = {_id: doc._id, _rev: (doc._rev || opts.rev)};
newDoc._deleted = true;
if (utils.isLocalId(newDoc._id) && typeof this._removeLocal === 'function') {
return this._removeLocal(doc, callback);
}
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);
if (!ids.length) {
return callback(null, {});
}
var count = 0;
var missing = new utils.Map();
function addToMissing(id, revId) {
if (!missing.has(id)) {
missing.set(id, {missing: []});
}
missing.get(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.status === 404 && err.message === 'missing') {
missing.set(id, {missing: req[id]});
} else if (err) {
return callback(err);
} else {
processDoc(id, rev_tree);
}
if (++count === ids.length) {
// convert LazyMap to object
var missingObj = {};
missing.forEach(function (value, key) {
missingObj[key] = value;
});
return callback(null, missingObj);
}
});
}, 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 =
utils.adapterFun('compactDocument', function (docId, maxHeight, callback) {
var self = this;
this._getRevisionTree(docId, function (err, revTree) {
if (err) {
return callback(err);
}
var height = computeHeight(revTree);
var candidates = [];
var revs = [];
Object.keys(height).forEach(function (rev) {
if (height[rev] > maxHeight) {
candidates.push(rev);
}
});
merge.traverseRevTree(revTree, function (isLeaf, pos, revHash, ctx, opts) {
var rev = pos + '-' + revHash;
if (opts.status === 'available' && candidates.indexOf(rev) !== -1) {
revs.push(rev);
}
});
self._doCompaction(docId, 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;
opts = utils.clone(opts || {});
self.get('_local/compaction')["catch"](function () {
return false;
}).then(function (doc) {
if (typeof self._compact === 'function') {
if (doc && doc.last_seq) {
opts.last_seq = doc.last_seq;
}
return self._compact(opts, callback);
}
});
});
AbstractPouchDB.prototype._compact = function (opts, callback) {
var self = this;
var changesOpts = {
returnDocs: false,
last_seq: opts.last_seq || 0
};
var promises = [];
function onChange(row) {
promises.push(self.compactDocument(row.id, 0));
}
function onComplete(resp) {
var lastSeq = resp.last_seq;
Promise.all(promises).then(function () {
return upsert(self, '_local/compaction', function deltaFunc(doc) {
if (!doc.last_seq || doc.last_seq < lastSeq) {
doc.last_seq = lastSeq;
return doc;
}
return false; // somebody else got here first, don't update
});
}).then(function () {
callback(null, {ok: true});
})["catch"](callback);
}
self.changes(changesOpts)
.on('change', onChange)
.on('complete', onComplete)
.on('error', 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.error(errors.INVALID_ID));
}
if (utils.isLocalId(id) && typeof this._getLocal === 'function') {
return this._getLocal(id, callback);
}
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) {
return callback(err);
}
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.INVALID_REV));
}
}
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;
var metadata = result.metadata;
var ctx = result.ctx;
if (opts.conflicts) {
var conflicts = merge.collectConflicts(metadata);
if (conflicts.length) {
doc._conflicts = conflicts;
}
}
if (utils.isDeleted(metadata, doc._rev)) {
doc._deleted = true;
}
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;
});
var indexOfRev = path.ids.map(function (x) {return x.id; })
.indexOf(doc._rev.split('-')[1]) + 1;
var howMany = path.ids.length - indexOfRev;
path.ids.splice(indexOfRev, howMany);
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) {
utils.info('The "local_seq" option is deprecated and will be removed');
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) {
var att = doc._attachments[key];
att.data = data;
delete att.stub;
delete att.length;
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.error(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) {
this._closed = true;
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);
}
// assume we know better than the adapter, unless it informs us
info.db_name = info.db_name || self._db_name;
info.auto_compaction = !!(self.auto_compaction && self.type() !== 'http');
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 = {};
}
opts = utils.clone(opts);
if (Array.isArray(req)) {
req = {
docs: req
};
}
if (!req || !req.docs || !Array.isArray(req.docs)) {
return callback(errors.error(errors.MISSING_BULK_DOCS));
}
for (var i = 0; i < req.docs.length; ++i) {
if (typeof req.docs[i] !== 'object' || Array.isArray(req.docs[i])) {
return callback(errors.error(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;
}
}
if (!opts.new_edits && this.type() !== 'http') {
// ensure revisions of the same doc are sorted, so that
// the local adapter processes them correctly (#2935)
req.docs.sort(function (a, b) {
var idCompare = utils.compare(a._id, b._id);
if (idCompare !== 0) {
return idCompare;
}
var aStart = a._revisions ? a._revisions.start : 0;
var bStart = b._revisions ? b._revisions.start : 0;
return utils.compare(aStart, bStart);
});
}
req.docs.forEach(function (doc) {
if (doc._deleted) {
delete doc._attachments; // ignore atts for deleted docs
}
});
return this._bulkDocs(req, opts, function (err, res) {
if (err) {
return callback(err);
}
if (!opts.new_edits) {
// this is what couch does when new_edits is false
res = res.filter(function (x) {
return x.error;
});
}
callback(null, res);
});
});
AbstractPouchDB.prototype.registerDependentDatabase =
utils.adapterFun('registerDependentDatabase', function (dependentDb,
callback) {
var depDB = new this.constructor(dependentDb, this.__opts || {});
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});
});
});
},{}],2:[function(_dereq_,module,exports){
(function (process){
"use strict";
var CHANGES_BATCH_SIZE = 25;
// according to http://stackoverflow.com/a/417184/680742,
// the de factor URL length limit is 2000 characters.
// but since most of our measurements don't take the full
// URL into account, we fudge it a bit.
// TODO: we could measure the full URL to enforce exactly 2000 chars
var MAX_URL_LENGTH = 1800;
var utils = _dereq_(32);
var errors = _dereq_(19);
var log = _dereq_(38)('pouchdb:http');
var isBrowser = typeof process === 'undefined' || process.browser;
var buffer = _dereq_(18);
function encodeDocId(id) {
if (/^_(design|local)/.test(id)) {
return id;
}
return encodeURIComponent(id);
}
function preprocessAttachments(doc) {
if (!doc._attachments || !Object.keys(doc._attachments)) {
return utils.Promise.resolve();
}
return utils.Promise.all(Object.keys(doc._attachments).map(function (key) {
var attachment = doc._attachments[key];
if (attachment.data && typeof attachment.data !== 'string') {
if (isBrowser) {
return new utils.Promise(function (resolve) {
utils.readAsBinaryString(attachment.data, function (binary) {
attachment.data = utils.btoa(binary);
resolve();
});
});
} else {
attachment.data = attachment.data.toString('base64');
}
}
}));
}
// 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 = utils.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 || (opts.ajax && opts.ajax.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; };
api.getHeaders = function () {return utils.clone(host.headers); };
var ajaxOpts = opts.ajax || {};
opts = utils.clone(opts);
function ajax(options, callback) {
var reqOpts = utils.extend({}, ajaxOpts, options);
log(reqOpts.method + ' ' + reqOpts.url);
return utils.ajax(reqOpts, callback);
}
// Create a new CouchDB database based on the given opts
var createDB = function () {
ajax({headers: host.headers, method: 'PUT', url: dbUrl}, function (err) {
// 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) {
// 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) {
//check if the db exists
if (err) {
if (err.status === 404) {
utils.explain404(
'PouchDB is just detecting if the remote DB exists.');
//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) {
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(null, {ok: true});
} 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 (docOrId, optsOrRev, opts, callback) {
var doc;
if (typeof optsOrRev === 'string') {
// id, rev, opts, callback style
doc = {
_id: docOrId,
_rev: optsOrRev
};
if (typeof opts === 'function') {
callback = opts;
opts = {};
}
} else {
// doc, opts, callback style
doc = docOrId;
if (typeof optsOrRev === 'function') {
callback = optsOrRev;
opts = {};
} else {
callback = opts;
opts = optsOrRev;
}
}
var rev = (doc._rev || opts.rev);
// Delete the document
ajax({
headers: host.headers,
method: 'DELETE',
url: genDBUrl(host, encodeDocId(doc._id)) + '?rev=' + rev
}, callback);
});
function encodeAttachmentId(attachmentId) {
return attachmentId.split("/").map(encodeURIComponent).join("/");
}
// 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 + '/' + encodeAttachmentId(attachmentId), opts, callback);
});
// Remove the attachment given by the id and rev
api.removeAttachment =
utils.adapterFun('removeAttachment', function (docId, attachmentId, rev,
callback) {
var url = genDBUrl(host, encodeDocId(docId) + '/' +
encodeAttachmentId(attachmentId)) + '?rev=' + rev;
ajax({
headers: host.headers,
method: 'DELETE',
url: url
}, 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) + '/' + encodeAttachmentId(attachmentId);
var url = genDBUrl(host, id);
if (rev) {
url += '?rev=' + rev;
}
if (typeof blob === 'string') {
var binary;
try {
binary = utils.atob(blob);
} catch (err) {
// it's not base64-encoded, so throw error
return callback(errors.error(errors.BAD_ARG,
'Attachments need to be base64 encoded'));
}
if (isBrowser) {
blob = utils.createBlob([utils.fixBinary(binary)], {type: type});
} else {
blob = binary ? new buffer(binary, 'binary') : '';
}
}
var opts = {
headers: utils.clone(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;
var doc = args.shift();
var id = '_id' in doc;
var callback = args.pop();
if (typeof doc !== 'object' || Array.isArray(doc)) {
return callback(errors.error(errors.NOT_AN_OBJECT));
}
doc = utils.clone(doc);
preprocessAttachments(doc).then(function () {
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);
}
if (!args.length) {
break;
}
}
opts = opts || {};
var error = utils.invalidIdError(doc._id);
if (error) {
throw 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
}, function (err, res) {
if (err) {
return callback(err);
}
res.ok = true;
callback(null, res);
});
})["catch"](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.error(errors.NOT_AN_OBJECT));
}
if (! ("_id" in doc)) {
doc._id = utils.uuid();
}
api.put(doc, opts, function (err, res) {
if (err) {
return callback(err);
}
res.ok = true;
callback(null, res);
});
});
// Update/create multiple documents given by req in the database
// given by host.
api._bulkDocs = function (req, opts, callback) {
// 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;
}
utils.Promise.all(req.docs.map(preprocessAttachments)).then(function () {
// Update/create the documents
ajax({
headers: host.headers,
method: 'POST',
url: genDBUrl(host, '_bulk_docs'),
body: req
}, function (err, results) {
if (err) {
return callback(err);
}
results.forEach(function (result) {
result.ok = true; // smooths out cloudant not adding this
});
callback(null, results);
});
})["catch"](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';
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.attachments) {
// added in CouchDB 1.6.0
params.push('attachments=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 (typeof opts.inclusive_end !== 'undefined') {
params.push('inclusive_end=' + !!opts.inclusive_end);
}
// 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 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 batchSize = 'batch_size' in opts ? opts.batch_size : CHANGES_BATCH_SIZE;
opts = utils.clone(opts);
opts.timeout = opts.timeout || 30 * 1000;
// We give a 5 second buffer for CouchDB changes to respond with
// an ok timeout
var params = { timeout: opts.timeout - (5 * 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.attachments) {
params.attachments = 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 method = 'GET';
var body;
if (opts.doc_ids) {
// set this automagically for the user; it's annoying that couchdb
// requires both a "filter" and a "doc_ids" param.
params.filter = '_doc_ids';
var docIdsJson = JSON.stringify(opts.doc_ids);
if (docIdsJson.length < MAX_URL_LENGTH) {
params.doc_ids = docIdsJson;
} else {
// anything greater than ~2000 is unsafe for gets, so
// use POST instead
method = 'POST';
body = {doc_ids: opts.doc_ids };
}
}
if (opts.continuous && api._useSSE) {
return api.sse(opts, params, returnDocs);
}
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 (typeof params.since === "object") {
params.since = JSON.stringify(params.since);
}
if (opts.descending) {
if (limit) {
params.limit = leftToFetch;
}
} else {
params.limit = (!limit || leftToFetch > batchSize) ?
batchSize : 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: method,
url: genDBUrl(host, '_changes' + paramStr),
// _changes can take a long time to generate, especially when filtered
timeout: opts.timeout,
body: body
};
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 < batchSize) ||
(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.error(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();
}
}
};
};
api.sse = function (opts, params, returnDocs) {
params.feed = 'eventsource';
params.since = opts.since || 0;
params.limit = opts.limit;
delete params.timeout;
var paramStr = '?' + Object.keys(params).map(function (k) {
return k + '=' + params[k];
}).join('&');
var url = genDBUrl(host, '_changes' + paramStr);
var source = new EventSource(url);
var results = {
results: [],
last_seq: false
};
var dispatched = false;
var open = false;
source.addEventListener('message', msgHandler, false);
source.onopen = function () {
open = true;
};
source.onerror = errHandler;
return {
cancel: function () {
if (dispatched) {
return dispatched.cancel();
}
source.removeEventListener('message', msgHandler, false);
source.close();
}
};
function msgHandler(e) {
var data = JSON.parse(e.data);
if (returnDocs) {
results.results.push(data);
}
results.last_seq = data.seq;
utils.call(opts.onChange, data);
}
function errHandler(err) {
source.removeEventListener('message', msgHandler, false);
if (open === false) {
// errored before it opened
// likely doesn't support EventSource
api._useSSE = false;
dispatched = api._changes(opts);
return;
}
source.close();
utils.call(opts.complete, err);
}
};
api._useSSE = false;
// Currently disabled due to failing chrome tests in saucelabs
// api._useSSE = typeof global.EventSource === 'function';
// 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('revsDiff', 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: JSON.stringify(req)
}, callback);
});
api._close = function (callback) {
callback();
};
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;
}).call(this,_dereq_(37))
},{}],3:[function(_dereq_,module,exports){
'use strict';
var utils = _dereq_(32);
var idbConstants = _dereq_(5);
var DETECT_BLOB_SUPPORT_STORE = idbConstants.DETECT_BLOB_SUPPORT_STORE;
//
// Detect blob support. Chrome didn't support it until version 38.
// In version 37 they had a broken version where PNGs (and possibly
// other binary types) aren't stored correctly, because when you fetch
// them, the content type is always null.
//
// Furthermore, they have some outstanding bugs where blobs occasionally
// are read by FileReader as null, or by ajax as 404s.
//
// Sadly we use the 404 bug to detect the FileReader bug, so if they
// get fixed independently and released in different versions of Chrome,
// then the bug could come back. So it's worthwhile to watch these issues:
// 404 bug: https://code.google.com/p/chromium/issues/detail?id=447916
// FileReader bug: https://code.google.com/p/chromium/issues/detail?id=447836
//
function checkBlobSupport(txn, idb) {
return new utils.Promise(function (resolve, reject) {
var blob = utils.createBlob([''], {type: 'image/png'});
txn.objectStore(DETECT_BLOB_SUPPORT_STORE).put(blob, 'key');
txn.oncomplete = function () {
// have to do it in a separate transaction, else the correct
// content type is always returned
var blobTxn = idb.transaction([DETECT_BLOB_SUPPORT_STORE],
'readwrite');
var getBlobReq = blobTxn.objectStore(
DETECT_BLOB_SUPPORT_STORE).get('key');
getBlobReq.onerror = reject;
getBlobReq.onsuccess = function (e) {
var storedBlob = e.target.result;
var url = URL.createObjectURL(storedBlob);
utils.ajax({
url: url,
cache: true,
binary: true
}, function (err, res) {
if (err && err.status === 405) {
// firefox won't let us do that. but firefox doesn't
// have the blob type bug that Chrome does, so that's ok
resolve(true);
} else {
resolve(!!(res && res.type === 'image/png'));
if (err && err.status === 404) {
utils.explain404('PouchDB is just detecting blob URL support.');
}
}
URL.revokeObjectURL(url);
});
};
};
})["catch"](function () {
return false; // error, so assume unsupported
});
}
module.exports = checkBlobSupport;
},{}],4:[function(_dereq_,module,exports){
'use strict';
var utils = _dereq_(32);
var errors = _dereq_(19);
var idbUtils = _dereq_(6);
var idbConstants = _dereq_(5);
var ATTACH_AND_SEQ_STORE = idbConstants.ATTACH_AND_SEQ_STORE;
var ATTACH_STORE = idbConstants.ATTACH_STORE;
var BY_SEQ_STORE = idbConstants.BY_SEQ_STORE;
var DOC_STORE = idbConstants.DOC_STORE;
var LOCAL_STORE = idbConstants.LOCAL_STORE;
var META_STORE = idbConstants.META_STORE;
var compactRevs = idbUtils.compactRevs;
var decodeMetadata = idbUtils.decodeMetadata;
var encodeMetadata = idbUtils.encodeMetadata;
var idbError = idbUtils.idbError;
var openTransactionSafely = idbUtils.openTransactionSafely;
function idbBulkDocs(req, opts, api, idb, Changes, callback) {
var docInfos = req.docs;
var txn;
var docStore;
var bySeqStore;
var attachStore;
var attachAndSeqStore;
var docInfoError;
for (var i = 0, len = docInfos.length; i < len; i++) {
var doc = docInfos[i];
if (doc._id && utils.isLocalId(doc._id)) {
continue;
}
doc = docInfos[i] = utils.parseDoc(doc, opts.new_edits);
if (doc.error && !docInfoError) {
docInfoError = doc;
}
}
if (docInfoError) {
return callback(docInfoError);
}
var results = new Array(docInfos.length);
var fetchedDocs = new utils.Map();
var preconditionErrored = false;
var blobType = api._blobSupport ? 'blob' : 'base64';
utils.preprocessAttachments(docInfos, blobType, function (err) {
if (err) {
return callback(err);
}
startTransaction();
});
function startTransaction() {
var stores = [
DOC_STORE, BY_SEQ_STORE,
ATTACH_STORE, META_STORE,
LOCAL_STORE, ATTACH_AND_SEQ_STORE
];
var txnResult = openTransactionSafely(idb, stores, 'readwrite');
if (txnResult.error) {
return callback(txnResult.error);
}
txn = txnResult.txn;
txn.onerror = idbError(callback);
txn.ontimeout = idbError(callback);
txn.oncomplete = complete;
docStore = txn.objectStore(DOC_STORE);
bySeqStore = txn.objectStore(BY_SEQ_STORE);
attachStore = txn.objectStore(ATTACH_STORE);
attachAndSeqStore = txn.objectStore(ATTACH_AND_SEQ_STORE);
verifyAttachments(function (err) {
if (err) {
preconditionErrored = true;
return callback(err);
}
fetchExistingDocs();
});
}
function processDocs() {
utils.processDocs(docInfos, api, fetchedDocs, txn, results,
writeDoc, opts);
}
function fetchExistingDocs() {
if (!docInfos.length) {
return;
}
var numFetched = 0;
function checkDone() {
if (++numFetched === docInfos.length) {
processDocs();
}
}
function readMetadata(event) {
var metadata = decodeMetadata(event.target.result);
if (metadata) {
fetchedDocs.set(metadata.id, metadata);
}
checkDone();
}
for (var i = 0, len = docInfos.length; i < len; i++) {
var docInfo = docInfos[i];
if (docInfo._id && utils.isLocalId(docInfo._id)) {
checkDone(); // skip local docs
continue;
}
var req = docStore.get(docInfo.metadata.id);
req.onsuccess = readMetadata;
}
}
function complete() {
if (preconditionErrored) {
return;
}
Changes.notify(api._name);
api._docCount = -1; // invalidate
callback(null, results);
}
function verifyAttachment(digest, callback) {
var req = attachStore.get(digest);
req.onsuccess = function (e) {
if (!e.target.result) {
var err = errors.error(errors.MISSING_STUB,
'unknown stub attachment with digest ' +
digest);
err.status = 412;
callback(err);
} else {
callback();
}
};
}
function verifyAttachments(finish) {
var digests = [];
docInfos.forEach(function (docInfo) {
if (docInfo.data && docInfo.data._attachments) {
Object.keys(docInfo.data._attachments).forEach(function (filename) {
var att = docInfo.data._attachments[filename];
if (att.stub) {
digests.push(att.digest);
}
});
}
});
if (!digests.length) {
return finish();
}
var numDone = 0;
var err;
function checkDone() {
if (++numDone === digests.length) {
finish(err);
}
}
digests.forEach(function (digest) {
verifyAttachment(digest, function (attErr) {
if (attErr && !err) {
err = attErr;
}
checkDone();
});
});
}
function writeDoc(docInfo, winningRev, deleted, callback, isUpdate,
delta, resultsIdx) {
var doc = docInfo.data;
doc._id = docInfo.metadata.id;
doc._rev = docInfo.metadata.rev;
if (deleted) {
doc._deleted = true;
}
var hasAttachments = doc._attachments &&
Object.keys(doc._attachments).length;
if (hasAttachments) {
return writeAttachments(docInfo, winningRev, deleted,
callback, isUpdate, resultsIdx);
}
finishDoc(docInfo, winningRev, deleted,
callback, isUpdate, resultsIdx);
}
function autoCompact(docInfo) {
var revsToDelete = utils.compactTree(docInfo.metadata);
compactRevs(revsToDelete, docInfo.metadata.id, txn);
}
function finishDoc(docInfo, winningRev, deleted, callback, isUpdate,
resultsIdx) {
var doc = docInfo.data;
var metadata = docInfo.metadata;
doc._doc_id_rev = metadata.id + '::' + metadata.rev;
delete doc._id;
delete doc._rev;
function afterPutDoc(e) {
if (isUpdate && api.auto_compaction) {
autoCompact(docInfo);
}
metadata.seq = e.target.result;
// Current _rev is calculated from _rev_tree on read
delete metadata.rev;
var metadataToStore = encodeMetadata(metadata, winningRev, deleted);
var metaDataReq = docStore.put(metadataToStore);
metaDataReq.onsuccess = afterPutMetadata;
}
function afterPutDocError(e) {
// ConstraintError, need to update, not put (see #1638 for details)
e.preventDefault(); // avoid transaction abort
e.stopPropagation(); // avoid transaction onerror
var index = bySeqStore.index('_doc_id_rev');
var getKeyReq = index.getKey(doc._doc_id_rev);
getKeyReq.onsuccess = function (e) {
var putReq = bySeqStore.put(doc, e.target.result);
putReq.onsuccess = afterPutDoc;
};
}
function afterPutMetadata() {
results[resultsIdx] = {
ok: true,
id: metadata.id,
rev: winningRev
};
fetchedDocs.set(docInfo.metadata.id, docInfo.metadata);
insertAttachmentMappings(docInfo, metadata.seq, callback);
}
var putReq = bySeqStore.put(doc);
putReq.onsuccess = afterPutDoc;
putReq.onerror = afterPutDocError;
}
function writeAttachments(docInfo, winningRev, deleted, callback,
isUpdate, resultsIdx) {
var doc = docInfo.data;
var numDone = 0;
var attachments = Object.keys(doc._attachments);
function collectResults() {
if (numDone === attachments.length) {
finishDoc(docInfo, winningRev, deleted, callback, isUpdate,
resultsIdx);
}
}
function attachmentSaved() {
numDone++;
collectResults();
}
attachments.forEach(function (key) {
var att = docInfo.data._attachments[key];
if (!att.stub) {
var data = att.data;
delete att.data;
var digest = att.digest;
saveAttachment(digest, data, attachmentSaved);
} else {
numDone++;
collectResults();
}
});
}
// map seqs to attachment digests, which
// we will need later during compaction
function insertAttachmentMappings(docInfo, seq, callback) {
var attsAdded = 0;
var attsToAdd = Object.keys(docInfo.data._attachments || {});
if (!attsToAdd.length) {
return callback();
}
function checkDone() {
if (++attsAdded === attsToAdd.length) {
callback();
}
}
function add(att) {
var digest = docInfo.data._attachments[att].digest;
var req = attachAndSeqStore.put({
seq: seq,
digestSeq: digest + '::' + seq
});
req.onsuccess = checkDone;
req.onerror = function (e) {
// this callback is for a constaint error, which we ignore
// because this docid/rev has already been associated with
// the digest (e.g. when new_edits == false)
e.preventDefault(); // avoid transaction abort
e.stopPropagation(); // avoid transaction onerror
checkDone();
};
}
for (var i = 0; i < attsToAdd.length; i++) {
add(attsToAdd[i]); // do in parallel
}
}
function saveAttachment(digest, data, callback) {
var getKeyReq = attachStore.count(digest);
getKeyReq.onsuccess = function(e) {
var count = e.target.result;
if (count) {
return callback(); // already exists
}
var newAtt = {
digest: digest,
body: data
};
var putReq = attachStore.put(newAtt);
putReq.onsuccess = callback;
};
}
}
module.exports = idbBulkDocs;
},{}],5:[function(_dereq_,module,exports){
'use strict';
// IndexedDB requires a versioned database structure, so we use the
// version here to manage migrations.
exports.ADAPTER_VERSION = 5;
// The object stores created for each database
// DOC_STORE stores the document meta data, its revision history and state
// Keyed by document id
exports. DOC_STORE = 'document-store';
// BY_SEQ_STORE stores a particular version of a document, keyed by its
// sequence id
exports.BY_SEQ_STORE = 'by-sequence';
// Where we store attachments
exports.ATTACH_STORE = 'attach-store';
// Where we store many-to-many relations
// between attachment digests and seqs
exports.ATTACH_AND_SEQ_STORE = 'attach-seq-store';
// Where we store database-wide meta data in a single record
// keyed by id: META_STORE
exports.META_STORE = 'meta-store';
// Where we store local documents
exports.LOCAL_STORE = 'local-store';
// Where we detect blob support
exports.DETECT_BLOB_SUPPORT_STORE = 'detect-blob-support';
},{}],6:[function(_dereq_,module,exports){
(function (process){
'use strict';
var errors = _dereq_(19);
var utils = _dereq_(32);
var constants = _dereq_(5);
function tryCode(fun, that, args) {
try {
fun.apply(that, args);
} catch (err) { // shouldn't happen
if (typeof PouchDB !== 'undefined') {
PouchDB.emit('error', err);
}
}
}
exports.taskQueue = {
running: false,
queue: []
};
exports.applyNext = function () {
if (exports.taskQueue.running || !exports.taskQueue.queue.length) {
return;
}
exports.taskQueue.running = true;
var item = exports.taskQueue.queue.shift();
item.action(function (err, res) {
tryCode(item.callback, this, [err, res]);
exports.taskQueue.running = false;
process.nextTick(exports.applyNext);
});
};
exports.idbError = function (callback) {
return function (event) {
var message = (event.target && event.target.error &&
event.target.error.name) || event.target;
callback(errors.error(errors.IDB_ERROR, message, event.type));
};
};
// Unfortunately, the metadata has to be stringified
// when it is put into the database, because otherwise
// IndexedDB can throw errors for deeply-nested objects.
// Originally we just used JSON.parse/JSON.stringify; now
// we use this custom vuvuzela library that avoids recursion.
// If we could do it all over again, we'd probably use a
// format for the revision trees other than JSON.
exports.encodeMetadata = function (metadata, winningRev, deleted) {
return {
data: utils.safeJsonStringify(metadata),
winningRev: winningRev,
deletedOrLocal: deleted ? '1' : '0',
seq: metadata.seq, // highest seq for this doc
id: metadata.id
};
};
exports.decodeMetadata = function (storedObject) {
if (!storedObject) {
return null;
}
var metadata = utils.safeJsonParse(storedObject.data);
metadata.winningRev = storedObject.winningRev;
metadata.deletedOrLocal = storedObject.deletedOrLocal === '1';
metadata.seq = storedObject.seq;
return metadata;
};
// read the doc back out from the database. we don't store the
// _id or _rev because we already have _doc_id_rev.
exports.decodeDoc = function (doc) {
if (!doc) {
return doc;
}
var idx = utils.lastIndexOf(doc._doc_id_rev, ':');
doc._id = doc._doc_id_rev.substring(0, idx - 1);
doc._rev = doc._doc_id_rev.substring(idx + 1);
delete doc._doc_id_rev;
return doc;
};
// Read a blob from the database, encoding as necessary
// and translating from base64 if the IDB doesn't support
// native Blobs
exports.readBlobData = function (body, type, encode, callback) {
if (encode) {
if (!body) {
callback('');
} else if (typeof body !== 'string') { // we have blob support
utils.readAsBinaryString(body, function (binary) {
callback(utils.btoa(binary));
});
} else { // no blob support
callback(body);
}
} else {
if (!body) {
callback(utils.createBlob([''], {type: type}));
} else if (typeof body !== 'string') { // we have blob support
callback(body);
} else { // no blob support
body = utils.fixBinary(atob(body));
callback(utils.createBlob([body], {type: type}));
}
}
};
exports.fetchAttachmentsIfNecessary = function (doc, opts, txn, cb) {
var attachments = Object.keys(doc._attachments || {});
if (!attachments.length) {
return cb && cb();
}
var numDone = 0;
function checkDone() {
if (++numDone === attachments.length && cb) {
cb();
}
}
function fetchAttachment(doc, att) {
var attObj = doc._attachments[att];
var digest = attObj.digest;
var req = txn.objectStore(constants.ATTACH_STORE).get(digest);
req.onsuccess = function (e) {
attObj.body = e.target.result.body;
checkDone();
};
}
attachments.forEach(function (att) {
if (opts.attachments && opts.include_docs) {
fetchAttachment(doc, att);
} else {
doc._attachments[att].stub = true;
checkDone();
}
});
};
// IDB-specific postprocessing necessary because
// we don't know whether we stored a true Blob or
// a base64-encoded string, and if it's a Blob it
// needs to be read outside of the transaction context
exports.postProcessAttachments = function (results) {
return utils.Promise.all(results.map(function (row) {
if (row.doc && row.doc._attachments) {
var attNames = Object.keys(row.doc._attachments);
return utils.Promise.all(attNames.map(function (att) {
var attObj = row.doc._attachments[att];
if (!('body' in attObj)) { // already processed
return;
}
var body = attObj.body;
var type = attObj.content_type;
return new utils.Promise(function (resolve) {
exports.readBlobData(body, type, true, function (base64) {
row.doc._attachments[att] = utils.extend(
utils.pick(attObj, ['digest', 'content_type']),
{data: base64}
);
resolve();
});
});
}));
}
}));
};
exports.compactRevs = function (revs, docId, txn) {
var possiblyOrphanedDigests = [];
var seqStore = txn.objectStore(constants.BY_SEQ_STORE);
var attStore = txn.objectStore(constants.ATTACH_STORE);
var attAndSeqStore = txn.objectStore(constants.ATTACH_AND_SEQ_STORE);
var count = revs.length;
function checkDone() {
count--;
if (!count) { // done processing all revs
deleteOrphanedAttachments();
}
}
function deleteOrphanedAttachments() {
if (!possiblyOrphanedDigests.length) {
return;
}
possiblyOrphanedDigests.forEach(function (digest) {
var countReq = attAndSeqStore.index('digestSeq').count(
IDBKeyRange.bound(
digest + '::', digest + '::\uffff', false, false));
countReq.onsuccess = function (e) {
var count = e.target.result;
if (!count) {
// orphaned
attStore["delete"](digest);
}
};
});
}
revs.forEach(function (rev) {
var index = seqStore.index('_doc_id_rev');
var key = docId + "::" + rev;
index.getKey(key).onsuccess = function (e) {
var seq = e.target.result;
if (typeof seq !== 'number') {
return checkDone();
}
seqStore["delete"](seq);
var cursor = attAndSeqStore.index('seq')
.openCursor(IDBKeyRange.only(seq));
cursor.onsuccess = function (event) {
var cursor = event.target.result;
if (cursor) {
var digest = cursor.value.digestSeq.split('::')[0];
possiblyOrphanedDigests.push(digest);
attAndSeqStore["delete"](cursor.primaryKey);
cursor["continue"]();
} else { // done
checkDone();
}
};
};
});
};
exports.openTransactionSafely = function (idb, stores, mode) {
try {
return {
txn: idb.transaction(stores, mode)
};
} catch (err) {
return {
error: err
};
}
};
}).call(this,_dereq_(37))
},{}],7:[function(_dereq_,module,exports){
(function (process){
'use strict';
var utils = _dereq_(32);
var merge = _dereq_(27);
var errors = _dereq_(19);
var idbUtils = _dereq_(6);
var idbConstants = _dereq_(5);
var idbBulkDocs = _dereq_(4);
var checkBlobSupport = _dereq_(3);
var ADAPTER_VERSION = idbConstants.ADAPTER_VERSION;
var ATTACH_AND_SEQ_STORE = idbConstants.ATTACH_AND_SEQ_STORE;
var ATTACH_STORE = idbConstants.ATTACH_STORE;
var BY_SEQ_STORE = idbConstants.BY_SEQ_STORE;
var DETECT_BLOB_SUPPORT_STORE = idbConstants.DETECT_BLOB_SUPPORT_STORE;
var DOC_STORE = idbConstants.DOC_STORE;
var LOCAL_STORE = idbConstants.LOCAL_STORE;
var META_STORE = idbConstants.META_STORE;
var applyNext = idbUtils.applyNext;
var compactRevs = idbUtils.compactRevs;
var decodeDoc = idbUtils.decodeDoc;
var decodeMetadata = idbUtils.decodeMetadata;
var encodeMetadata = idbUtils.encodeMetadata;
var fetchAttachmentsIfNecessary = idbUtils.fetchAttachmentsIfNecessary;
var idbError = idbUtils.idbError;
var postProcessAttachments = idbUtils.postProcessAttachments;
var readBlobData = idbUtils.readBlobData;
var taskQueue = idbUtils.taskQueue;
var openTransactionSafely = idbUtils.openTransactionSafely;
var cachedDBs = {};
var blobSupportPromise;
function IdbPouch(opts, callback) {
var api = this;
taskQueue.queue.push({
action: function (thisCallback) {
init(api, opts, thisCallback);
},
callback: callback
});
applyNext();
}
function init(api, opts, callback) {
var name = opts.name;
var instanceId = null;
var idStored = false;
var idb = null;
api._docCount = -1;
api._blobSupport = null;
api._name = name;
// called when creating a fresh new database
function createSchema(db) {
var docStore = db.createObjectStore(DOC_STORE, {keyPath : 'id'});
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);
// added in v2
docStore.createIndex('deletedOrLocal', 'deletedOrLocal', {unique : false});
// added in v3
db.createObjectStore(LOCAL_STORE, {keyPath: '_id'});
// added in v4
var attAndSeqStore = db.createObjectStore(ATTACH_AND_SEQ_STORE,
{autoIncrement: true});
attAndSeqStore.createIndex('seq', 'seq');
attAndSeqStore.createIndex('digestSeq', 'digestSeq', {unique: true});
}
// migration to version 2
// unfortunately "deletedOrLocal" is a misnomer now that we no longer
// store local docs in the main doc-store, but whaddyagonnado
function addDeletedOrLocalIndex(txn, callback) {
var docStore = txn.objectStore(DOC_STORE);
docStore.createIndex('deletedOrLocal', 'deletedOrLocal', {unique : false});
docStore.openCursor().onsuccess = function (event) {
var cursor = event.target.result;
if (cursor) {
var metadata = cursor.value;
var deleted = utils.isDeleted(metadata);
metadata.deletedOrLocal = deleted ? "1" : "0";
docStore.put(metadata);
cursor["continue"]();
} else {
callback();
}
};
}
// migration to version 3 (part 1)
function createLocalStoreSchema(db) {
db.createObjectStore(LOCAL_STORE, {keyPath: '_id'})
.createIndex('_doc_id_rev', '_doc_id_rev', {unique: true});
}
// migration to version 3 (part 2)
function migrateLocalStore(txn, cb) {
var localStore = txn.objectStore(LOCAL_STORE);
var docStore = txn.objectStore(DOC_STORE);
var seqStore = txn.objectStore(BY_SEQ_STORE);
var cursor = docStore.openCursor();
cursor.onsuccess = function (event) {
var cursor = event.target.result;
if (cursor) {
var metadata = cursor.value;
var docId = metadata.id;
var local = utils.isLocalId(docId);
var rev = merge.winningRev(metadata);
if (local) {
var docIdRev = docId + "::" + rev;
// remove all seq entries
// associated with this docId
var start = docId + "::";
var end = docId + "::~";
var index = seqStore.index('_doc_id_rev');
var range = IDBKeyRange.bound(start, end, false, false);
var seqCursor = index.openCursor(range);
seqCursor.onsuccess = function (e) {
seqCursor = e.target.result;
if (!seqCursor) {
// done
docStore["delete"](cursor.primaryKey);
cursor["continue"]();
} else {
var data = seqCursor.value;
if (data._doc_id_rev === docIdRev) {
localStore.put(data);
}
seqStore["delete"](seqCursor.primaryKey);
seqCursor["continue"]();
}
};
} else {
cursor["continue"]();
}
} else if (cb) {
cb();
}
};
}
// migration to version 4 (part 1)
function addAttachAndSeqStore(db) {
var attAndSeqStore = db.createObjectStore(ATTACH_AND_SEQ_STORE,
{autoIncrement: true});
attAndSeqStore.createIndex('seq', 'seq');
attAndSeqStore.createIndex('digestSeq', 'digestSeq', {unique: true});
}
// migration to version 4 (part 2)
function migrateAttsAndSeqs(txn, callback) {
var seqStore = txn.objectStore(BY_SEQ_STORE);
var attStore = txn.objectStore(ATTACH_STORE);
var attAndSeqStore = txn.objectStore(ATTACH_AND_SEQ_STORE);
// need to actually populate the table. this is the expensive part,
// so as an optimization, check first that this database even
// contains attachments
var req = attStore.count();
req.onsuccess = function (e) {
var count = e.target.result;
if (!count) {
return callback(); // done
}
seqStore.openCursor().onsuccess = function (e) {
var cursor = e.target.result;
if (!cursor) {
return callback(); // done
}
var doc = cursor.value;
var seq = cursor.primaryKey;
var atts = Object.keys(doc._attachments || {});
var digestMap = {};
for (var j = 0; j < atts.length; j++) {
var att = doc._attachments[atts[j]];
digestMap[att.digest] = true; // uniq digests, just in case
}
var digests = Object.keys(digestMap);
for (j = 0; j < digests.length; j++) {
var digest = digests[j];
attAndSeqStore.put({
seq: seq,
digestSeq: digest + '::' + seq
});
}
cursor["continue"]();
};
};
}
// migration to version 5
// Instead of relying on on-the-fly migration of metadata,
// this brings the doc-store to its modern form:
// - metadata.winningrev
// - metadata.seq
// - stringify the metadata when storing it
function migrateMetadata(txn) {
function decodeMetadataCompat(storedObject) {
if (!storedObject.data) {
// old format, when we didn't store it stringified
storedObject.deletedOrLocal = storedObject.deletedOrLocal === '1';
return storedObject;
}
return decodeMetadata(storedObject);
}
// ensure that every metadata has a winningRev and seq,
// which was previously created on-the-fly but better to migrate
var bySeqStore = txn.objectStore(BY_SEQ_STORE);
var docStore = txn.objectStore(DOC_STORE);
var cursor = docStore.openCursor();
cursor.onsuccess = function (e) {
var cursor = e.target.result;
if (!cursor) {
return; // done
}
var metadata = decodeMetadataCompat(cursor.value);
metadata.winningRev = metadata.winningRev || merge.winningRev(metadata);
function fetchMetadataSeq() {
// metadata.seq was added post-3.2.0, so if it's missing,
// we need to fetch it manually
var start = metadata.id + '::';
var end = metadata.id + '::\uffff';
var req = bySeqStore.index('_doc_id_rev').openCursor(
IDBKeyRange.bound(start, end));
var metadataSeq = 0;
req.onsuccess = function (e) {
var cursor = e.target.result;
if (!cursor) {
metadata.seq = metadataSeq;
return onGetMetadataSeq();
}
var seq = cursor.primaryKey;
if (seq > metadataSeq) {
metadataSeq = seq;
}
cursor["continue"]();
};
}
function onGetMetadataSeq() {
var metadataToStore = encodeMetadata(metadata,
metadata.winningRev, metadata.deletedOrLocal);
var req = docStore.put(metadataToStore);
req.onsuccess = function () {
cursor["continue"]();
};
}
if (metadata.seq) {
return onGetMetadataSeq();
}
fetchMetadataSeq();
};
}
api.type = function () {
return 'idb';
};
api._id = utils.toPromise(function (callback) {
callback(null, instanceId);
});
api._bulkDocs = function idb_bulkDocs(req, opts, callback) {
idbBulkDocs(req, opts, api, idb, IdbPouch.Changes, callback);
};
// 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 {
var txnResult = openTransactionSafely(idb,
[DOC_STORE, BY_SEQ_STORE, ATTACH_STORE], 'readonly');
if (txnResult.error) {
return callback(txnResult.error);
}
txn = txnResult.txn;
}
function finish() {
callback(err, {doc: doc, metadata: metadata, ctx: txn});
}
txn.objectStore(DOC_STORE).get(id).onsuccess = function (e) {
metadata = decodeMetadata(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.error(errors.MISSING_DOC, 'missing');
return finish();
}
if (utils.isDeleted(metadata) && !opts.rev) {
err = errors.error(errors.MISSING_DOC, "deleted");
return finish();
}
var objectStore = txn.objectStore(BY_SEQ_STORE);
var rev = opts.rev || metadata.winningRev;
var key = metadata.id + '::' + rev;
objectStore.index('_doc_id_rev').get(key).onsuccess = function (e) {
doc = e.target.result;
if (doc) {
doc = decodeDoc(doc);
}
if (!doc) {
err = errors.error(errors.MISSING_DOC, 'missing');
return finish();
}
finish();
};
};
};
api._getAttachment = function (attachment, opts, callback) {
var txn;
opts = utils.clone(opts);
if (opts.ctx) {
txn = opts.ctx;
} else {
var txnResult = openTransactionSafely(idb,
[DOC_STORE, BY_SEQ_STORE, ATTACH_STORE], 'readonly');
if (txnResult.error) {
return callback(txnResult.error);
}
txn = txnResult.txn;
}
var digest = attachment.digest;
var type = attachment.content_type;
txn.objectStore(ATTACH_STORE).get(digest).onsuccess = function (e) {
var body = e.target.result.body;
readBlobData(body, type, opts.encode, function (blobData) {
callback(null, blobData);
});
};
};
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 inclusiveEnd = opts.inclusive_end !== false;
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 = null;
try {
if (start && end) {
keyRange = IDBKeyRange.bound(start, end, false, !inclusiveEnd);
} else if (start) {
if (descending) {
keyRange = IDBKeyRange.upperBound(start);
} else {
keyRange = IDBKeyRange.lowerBound(start);
}
} else if (end) {
if (descending) {
keyRange = IDBKeyRange.lowerBound(end, !inclusiveEnd);
} else {
keyRange = IDBKeyRange.upperBound(end, !inclusiveEnd);
}
} else if (key) {
keyRange = IDBKeyRange.only(key);
}
} 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 stores = [DOC_STORE, BY_SEQ_STORE];
if (opts.attachments) {
stores.push(ATTACH_STORE);
}
var txnResult = openTransactionSafely(idb, stores, 'readonly');
if (txnResult.error) {
return callback(txnResult.error);
}
var transaction = txnResult.txn;
function onResultsReady() {
callback(null, {
total_rows: totalRows,
offset: opts.skip,
rows: results
});
}
transaction.oncomplete = function () {
if (opts.attachments) {
postProcessAttachments(results).then(onResultsReady);
} else {
onResultsReady();
}
};
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 = decodeMetadata(cursor.value);
var winningRev = metadata.winningRev;
function allDocsInner(metadata, data) {
var doc = {
id: metadata.id,
key: metadata.id,
value: {
rev: winningRev
}
};
if (opts.include_docs) {
doc.doc = data;
if (opts.conflicts) {
doc.doc._conflicts = merge.collectConflicts(metadata);
}
fetchAttachmentsIfNecessary(doc.doc, opts, transaction);
}
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) {
if (inclusiveEnd && doc.key < manualDescEnd) {
return;
} else if (!inclusiveEnd && 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(decodeMetadata(cursor.value),
decodeDoc(event.target.result));
};
}
};
}
function countDocs(callback) {
if (api._docCount !== -1) {
return callback(null, api._docCount);
}
var count;
var txnResult = openTransactionSafely(idb, [DOC_STORE], 'readonly');
if (txnResult.error) {
return callback(txnResult.error);
}
var txn = txnResult.txn;
var index = txn.objectStore(DOC_STORE).index('deletedOrLocal');
index.count(IDBKeyRange.only("0")).onsuccess = function (e) {
count = e.target.result;
};
txn.onerror = idbError(callback);
txn.oncomplete = function () {
api._docCount = count;
callback(null, api._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 || !cachedDBs[api._name]) {
var error = new Error('db isn\'t open');
error.id = 'idbNull';
return callback(error);
}
var updateSeq = 0;
var txnResult = openTransactionSafely(idb, [BY_SEQ_STORE], 'readonly');
if (txnResult.error) {
return callback(txnResult.error);
}
var txn = txnResult.txn;
txn.objectStore(BY_SEQ_STORE).openCursor(null, "prev").onsuccess =
function (event) {
var cursor = event.target.result;
if (cursor) {
updateSeq = cursor.key;
} else {
updateSeq = 0;
}
};
txn.oncomplete = function () {
callback(null, {
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 docIds = opts.doc_ids && new utils.Set(opts.doc_ids);
var descending = opts.descending ? 'prev' : null;
opts.since = opts.since || 0;
var lastSeq = opts.since;
var limit = 'limit' in opts ? opts.limit : -1;
if (limit === 0) {
limit = 1; // per CouchDB _changes spec
}
var returnDocs;
if ('returnDocs' in opts) {
returnDocs = opts.returnDocs;
} else {
returnDocs = true;
}
var results = [];
var numResults = 0;
var filter = utils.filterChange(opts);
var docIdsToMetadata = new utils.Map();
var txn;
var bySeqStore;
var docStore;
function onGetCursor(cursor) {
var doc = decodeDoc(cursor.value);
var seq = cursor.key;
lastSeq = seq;
if (docIds && !docIds.has(doc._id)) {
return cursor["continue"]();
}
var metadata;
function onGetMetadata() {
if (metadata.seq !== seq) {
// some other seq is later
return cursor["continue"]();
}
if (metadata.winningRev === doc._rev) {
return onGetWinningDoc(doc);
}
fetchWinningDoc();
}
function fetchWinningDoc() {
var docIdRev = doc._id + '::' + metadata.winningRev;
var req = bySeqStore.index('_doc_id_rev').openCursor(
IDBKeyRange.bound(docIdRev, docIdRev + '\uffff'));
req.onsuccess = function (e) {
onGetWinningDoc(decodeDoc(e.target.result.value));
};
}
function onGetWinningDoc(winningDoc) {
var change = opts.processChange(winningDoc, metadata, opts);
change.seq = metadata.seq;
if (filter(change)) {
numResults++;
if (returnDocs) {
results.push(change);
}
// process the attachment immediately
// for the benefit of live listeners
if (opts.attachments && opts.include_docs) {
fetchAttachmentsIfNecessary(winningDoc, opts, txn, function () {
postProcessAttachments([change]).then(function () {
opts.onChange(change);
});
});
} else {
opts.onChange(change);
}
}
if (numResults !== limit) {
cursor["continue"]();
}
}
metadata = docIdsToMetadata.get(doc._id);
if (metadata) { // cached
return onGetMetadata();
}
// metadata not cached, have to go fetch it
docStore.get(doc._id).onsuccess = function (event) {
metadata = decodeMetadata(event.target.result);
docIdsToMetadata.set(doc._id, metadata);
onGetMetadata();
};
}
function onsuccess(event) {
var cursor = event.target.result;
if (!cursor) {
return;
}
onGetCursor(cursor);
}
function fetchChanges() {
var objectStores = [DOC_STORE, BY_SEQ_STORE];
if (opts.attachments) {
objectStores.push(ATTACH_STORE);
}
var txnResult = openTransactionSafely(idb, objectStores, 'readonly');
if (txnResult.error) {
return opts.complete(txnResult.error);
}
txn = txnResult.txn;
txn.onerror = idbError(opts.complete);
txn.oncomplete = onTxnComplete;
bySeqStore = txn.objectStore(BY_SEQ_STORE);
docStore = txn.objectStore(DOC_STORE);
var req;
if (descending) {
req = bySeqStore.openCursor(
null, descending);
} else {
req = bySeqStore.openCursor(
IDBKeyRange.lowerBound(opts.since, true));
}
req.onsuccess = onsuccess;
}
fetchChanges();
function onTxnComplete() {
function finish() {
opts.complete(null, {
results: results,
last_seq: lastSeq
});
}
if (!opts.continuous && opts.attachments) {
// cannot guarantee that postProcessing was already done,
// so do it again
postProcessAttachments(results).then(finish);
} else {
finish();
}
}
};
api._close = function (callback) {
if (idb === null) {
return callback(errors.error(errors.NOT_OPEN));
}
// https://developer.mozilla.org/en-US/docs/IndexedDB/IDBDatabase#close
// "Returns immediately and closes the connection in a separate thread..."
idb.close();
delete cachedDBs[name];
idb = null;
callback();
};
api._getRevisionTree = function (docId, callback) {
var txnResult = openTransactionSafely(idb, [DOC_STORE], 'readonly');
if (txnResult.error) {
return callback(txnResult.error);
}
var txn = txnResult.txn;
var req = txn.objectStore(DOC_STORE).get(docId);
req.onsuccess = function (event) {
var doc = decodeMetadata(event.target.result);
if (!doc) {
callback(errors.error(errors.MISSING_DOC));
} else {
callback(null, doc.rev_tree);
}
};
};
// This function removes revisions of document docId
// which are listed in revs and sets this document
// revision to to rev_tree
api._doCompaction = function (docId, revs, callback) {
var stores = [
DOC_STORE,
BY_SEQ_STORE,
ATTACH_STORE,
ATTACH_AND_SEQ_STORE
];
var txnResult = openTransactionSafely(idb, stores, 'readwrite');
if (txnResult.error) {
return callback(txnResult.error);
}
var txn = txnResult.txn;
var docStore = txn.objectStore(DOC_STORE);
docStore.get(docId).onsuccess = function (event) {
var metadata = decodeMetadata(event.target.result);
merge.traverseRevTree(metadata.rev_tree, function (isLeaf, pos,
revHash, ctx, opts) {
var rev = pos + '-' + revHash;
if (revs.indexOf(rev) !== -1) {
opts.status = 'missing';
}
});
compactRevs(revs, docId, txn);
var winningRev = metadata.winningRev;
var deleted = metadata.deletedOrLocal;
txn.objectStore(DOC_STORE).put(
encodeMetadata(metadata, winningRev, deleted));
};
txn.onerror = idbError(callback);
txn.oncomplete = function () {
utils.call(callback);
};
};
api._getLocal = function (id, callback) {
var txnResult = openTransactionSafely(idb, [LOCAL_STORE], 'readonly');
if (txnResult.error) {
return callback(txnResult.error);
}
var tx = txnResult.txn;
var req = tx.objectStore(LOCAL_STORE).get(id);
req.onerror = idbError(callback);
req.onsuccess = function (e) {
var doc = e.target.result;
if (!doc) {
callback(errors.error(errors.MISSING_DOC));
} else {
delete doc['_doc_id_rev']; // for backwards compat
callback(null, doc);
}
};
};
api._putLocal = function (doc, opts, callback) {
if (typeof opts === 'function') {
callback = opts;
opts = {};
}
delete doc._revisions; // ignore this, trust the rev
var oldRev = doc._rev;
var id = doc._id;
if (!oldRev) {
doc._rev = '0-1';
} else {
doc._rev = '0-' + (parseInt(oldRev.split('-')[1], 10) + 1);
}
var tx = opts.ctx;
var ret;
if (!tx) {
var txnResult = openTransactionSafely(idb, [LOCAL_STORE], 'readwrite');
if (txnResult.error) {
return callback(txnResult.error);
}
tx = txnResult.txn;
tx.onerror = idbError(callback);
tx.oncomplete = function () {
if (ret) {
callback(null, ret);
}
};
}
var oStore = tx.objectStore(LOCAL_STORE);
var req;
if (oldRev) {
req = oStore.get(id);
req.onsuccess = function (e) {
var oldDoc = e.target.result;
if (!oldDoc || oldDoc._rev !== oldRev) {
callback(errors.error(errors.REV_CONFLICT));
} else { // update
var req = oStore.put(doc);
req.onsuccess = function () {
ret = {ok: true, id: doc._id, rev: doc._rev};
if (opts.ctx) { // return immediately
callback(null, ret);
}
};
}
};
} else { // new doc
req = oStore.add(doc);
req.onerror = function (e) {
// constraint error, already exists
callback(errors.error(errors.REV_CONFLICT));
e.preventDefault(); // avoid transaction abort
e.stopPropagation(); // avoid transaction onerror
};
req.onsuccess = function () {
ret = {ok: true, id: doc._id, rev: doc._rev};
if (opts.ctx) { // return immediately
callback(null, ret);
}
};
}
};
api._removeLocal = function (doc, callback) {
var txnResult = openTransactionSafely(idb, [LOCAL_STORE], 'readwrite');
if (txnResult.error) {
return callback(txnResult.error);
}
var tx = txnResult.txn;
var ret;
tx.oncomplete = function () {
if (ret) {
callback(null, ret);
}
};
var id = doc._id;
var oStore = tx.objectStore(LOCAL_STORE);
var req = oStore.get(id);
req.onerror = idbError(callback);
req.onsuccess = function (e) {
var oldDoc = e.target.result;
if (!oldDoc || oldDoc._rev !== doc._rev) {
callback(errors.error(errors.MISSING_DOC));
} else {
oStore["delete"](id);
ret = {ok: true, id: id, rev: '0-0'};
}
};
};
var cached = cachedDBs[name];
if (cached) {
idb = cached.idb;
instanceId = cached.instanceId;
api._blobSupport = cached.blobSupport;
process.nextTick(function () {
callback(null, api);
});
return;
}
var req = indexedDB.open(name, ADAPTER_VERSION);
if (!('openReqList' in IdbPouch)) {
IdbPouch.openReqList = {};
}
IdbPouch.openReqList[name] = req;
req.onupgradeneeded = function (e) {
var db = e.target.result;
if (e.oldVersion < 1) {
return createSchema(db); // new db, initial schema
}
// do migrations
var txn = e.currentTarget.transaction;
// these migrations have to be done in this function, before
// control is returned to the event loop, because IndexedDB
if (e.oldVersion < 3) {
createLocalStoreSchema(db); // v2 -> v3
}
if (e.oldVersion < 4) {
addAttachAndSeqStore(db); // v3 -> v4
}
var migrations = [
addDeletedOrLocalIndex, // v1 -> v2
migrateLocalStore, // v2 -> v3
migrateAttsAndSeqs, // v3 -> v4
migrateMetadata // v4 -> v5
];
var i = e.oldVersion;
function next() {
var migration = migrations[i - 1];
i++;
if (migration) {
migration(txn, next);
}
}
next();
};
req.onsuccess = function (e) {
idb = e.target.result;
idb.onversionchange = function () {
idb.close();
delete cachedDBs[name];
};
idb.onabort = function () {
idb.close();
delete cachedDBs[name];
};
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 checkSetupComplete = function () {
if (api._blobSupport === null || !idStored) {
return;
} else {
cachedDBs[name] = {
idb: idb,
instanceId: instanceId,
blobSupport: api._blobSupport,
loaded: true
};
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();
};
}
if (!blobSupportPromise) {
// make sure blob support is only checked once
blobSupportPromise = checkBlobSupport(txn, idb);
}
blobSupportPromise.then(function (val) {
api._blobSupport = val;
checkSetupComplete();
});
};
};
req.onerror = idbError(callback);
}
IdbPouch.valid = function () {
// Issue #2533, we finally gave up on doing bug
// detection instead of browser sniffing. Safari brought us
// to our knees.
var isSafari = typeof openDatabase !== 'undefined' &&
/(Safari|iPhone|iPad|iPod)/.test(navigator.userAgent) &&
!/Chrome/.test(navigator.userAgent);
// some outdated implementations of IDB that appear on Samsung
// and HTC Android devices <4.4 are missing IDBKeyRange
return !isSafari && typeof indexedDB !== 'undefined' &&
typeof IDBKeyRange !== 'undefined';
};
function destroy(name, opts, callback) {
if (!('openReqList' in IdbPouch)) {
IdbPouch.openReqList = {};
}
IdbPouch.Changes.removeAllListeners(name);
//Close open request for "name" database to fix ie delay.
if (IdbPouch.openReqList[name] && IdbPouch.openReqList[name].result) {
IdbPouch.openReqList[name].result.close();
delete cachedDBs[name];
}
var req = indexedDB.deleteDatabase(name);
req.onsuccess = function () {
//Remove open request from the list.
if (IdbPouch.openReqList[name]) {
IdbPouch.openReqList[name] = null;
}
if (utils.hasLocalStorage() && (name in localStorage)) {
delete localStorage[name];
}
callback(null, { 'ok': true });
};
req.onerror = idbError(callback);
}
IdbPouch.destroy = utils.toPromise(function (name, opts, callback) {
taskQueue.queue.push({
action: function (thisCallback) {
destroy(name, opts, thisCallback);
},
callback: callback
});
applyNext();
});
IdbPouch.Changes = new utils.Changes();
module.exports = IdbPouch;
}).call(this,_dereq_(37))
},{}],8:[function(_dereq_,module,exports){
module.exports = ['idb', 'websql'];
},{}],9:[function(_dereq_,module,exports){
'use strict';
var utils = _dereq_(32);
var errors = _dereq_(19);
var websqlUtils = _dereq_(11);
var websqlConstants = _dereq_(10);
var DOC_STORE = websqlConstants.DOC_STORE;
var BY_SEQ_STORE = websqlConstants.BY_SEQ_STORE;
var ATTACH_STORE = websqlConstants.ATTACH_STORE;
var ATTACH_AND_SEQ_STORE = websqlConstants.ATTACH_AND_SEQ_STORE;
var select = websqlUtils.select;
var stringifyDoc = websqlUtils.stringifyDoc;
var compactRevs = websqlUtils.compactRevs;
var unknownError = websqlUtils.unknownError;
function websqlBulkDocs(req, opts, api, db, Changes, 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) {
if (doc._id && utils.isLocalId(doc._id)) {
return doc;
}
var newDoc = utils.parseDoc(doc, newEdits);
return newDoc;
});
var docInfoErrors = docInfos.filter(function (docInfo) {
return docInfo.error;
});
if (docInfoErrors.length) {
return callback(docInfoErrors[0]);
}
var tx;
var results = new Array(docInfos.length);
var fetchedDocs = new utils.Map();
var preconditionErrored;
function complete() {
if (preconditionErrored) {
return callback(preconditionErrored);
}
Changes.notify(api._name);
api._docCount = -1; // invalidate
callback(null, results);
}
function verifyAttachment(digest, callback) {
var sql = 'SELECT count(*) as cnt FROM ' + ATTACH_STORE +
' WHERE digest=?';
tx.executeSql(sql, [digest], function (tx, result) {
if (result.rows.item(0).cnt === 0) {
var err = errors.error(errors.MISSING_STUB,
'unknown stub attachment with digest ' +
digest);
callback(err);
} else {
callback();
}
});
}
function verifyAttachments(finish) {
var digests = [];
docInfos.forEach(function (docInfo) {
if (docInfo.data && docInfo.data._attachments) {
Object.keys(docInfo.data._attachments).forEach(function (filename) {
var att = docInfo.data._attachments[filename];
if (att.stub) {
digests.push(att.digest);
}
});
}
});
if (!digests.length) {
return finish();
}
var numDone = 0;
var err;
function checkDone() {
if (++numDone === digests.length) {
finish(err);
}
}
digests.forEach(function (digest) {
verifyAttachment(digest, function (attErr) {
if (attErr && !err) {
err = attErr;
}
checkDone();
});
});
}
function writeDoc(docInfo, winningRev, deleted, callback, isUpdate,
docCount, resultsIdx) {
function finish() {
var data = docInfo.data;
var deletedInt = deleted ? 1 : 0;
var id = data._id;
var rev = data._rev;
var json = stringifyDoc(data);
var sql = 'INSERT INTO ' + BY_SEQ_STORE +
' (doc_id, rev, json, deleted) VALUES (?, ?, ?, ?);';
var sqlArgs = [id, rev, json, deletedInt];
// map seqs to attachment digests, which
// we will need later during compaction
function insertAttachmentMappings(seq, callback) {
var attsAdded = 0;
var attsToAdd = Object.keys(data._attachments || {});
if (!attsToAdd.length) {
return callback();
}
function checkDone() {
if (++attsAdded === attsToAdd.length) {
callback();
}
return false; // ack handling a constraint error
}
function add(att) {
var sql = 'INSERT INTO ' + ATTACH_AND_SEQ_STORE +
' (digest, seq) VALUES (?,?)';
var sqlArgs = [data._attachments[att].digest, seq];
tx.executeSql(sql, sqlArgs, checkDone, checkDone);
// second callback is for a constaint error, which we ignore
// because this docid/rev has already been associated with
// the digest (e.g. when new_edits == false)
}
for (var i = 0; i < attsToAdd.length; i++) {
add(attsToAdd[i]); // do in parallel
}
}
tx.executeSql(sql, sqlArgs, function (tx, result) {
var seq = result.insertId;
insertAttachmentMappings(seq, function () {
dataWritten(tx, seq);
});
}, function () {
// constraint error, recover by updating instead (see #1638)
var fetchSql = select('seq', BY_SEQ_STORE, null,
'doc_id=? AND rev=?');
tx.executeSql(fetchSql, [id, rev], function (tx, res) {
var seq = res.rows.item(0).seq;
var sql = 'UPDATE ' + BY_SEQ_STORE +
' SET json=?, deleted=? WHERE doc_id=? AND rev=?;';
var sqlArgs = [json, deletedInt, id, rev];
tx.executeSql(sql, sqlArgs, function (tx) {
insertAttachmentMappings(seq, function () {
dataWritten(tx, seq);
});
});
});
return false; // ack that we've handled the error
});
}
function collectResults(attachmentErr) {
if (!err) {
if (attachmentErr) {
err = attachmentErr;
callback(err);
} else if (recv === attachments.length) {
finish();
}
}
}
var err = null;
var recv = 0;
docInfo.data._id = docInfo.metadata.id;
docInfo.data._rev = docInfo.metadata.rev;
var attachments = Object.keys(docInfo.data._attachments || {});
if (deleted) {
docInfo.data._deleted = true;
}
function attachmentSaved(err) {
recv++;
collectResults(err);
}
attachments.forEach(function (key) {
var att = docInfo.data._attachments[key];
if (!att.stub) {
var data = att.data;
delete att.data;
var digest = att.digest;
saveAttachment(digest, data, attachmentSaved);
} else {
recv++;
collectResults();
}
});
if (!attachments.length) {
finish();
}
function autoCompact() {
if (!isUpdate || !api.auto_compaction) {
return; // nothing to do
}
var id = docInfo.metadata.id;
var revsToDelete = utils.compactTree(docInfo.metadata);
compactRevs(revsToDelete, id, tx);
}
function dataWritten(tx, seq) {
autoCompact();
docInfo.metadata.seq = seq;
delete docInfo.metadata.rev;
var sql = isUpdate ?
'UPDATE ' + DOC_STORE +
' SET json=?, max_seq=?, winningseq=' +
'(SELECT seq FROM ' + BY_SEQ_STORE +
' WHERE doc_id=' + DOC_STORE + '.id AND rev=?) WHERE id=?'
: 'INSERT INTO ' + DOC_STORE +
' (id, winningseq, max_seq, json) VALUES (?,?,?,?);';
var metadataStr = utils.safeJsonStringify(docInfo.metadata);
var id = docInfo.metadata.id;
var params = isUpdate ?
[metadataStr, seq, winningRev, id] :
[id, seq, seq, metadataStr];
tx.executeSql(sql, params, function () {
results[resultsIdx] = {
ok: true,
id: docInfo.metadata.id,
rev: winningRev
};
fetchedDocs.set(id, docInfo.metadata);
callback();
});
}
}
function processDocs() {
utils.processDocs(docInfos, api, fetchedDocs,
tx, results, writeDoc, opts);
}
function fetchExistingDocs(callback) {
if (!docInfos.length) {
return callback();
}
var numFetched = 0;
function checkDone() {
if (++numFetched === docInfos.length) {
callback();
}
}
docInfos.forEach(function (docInfo) {
if (docInfo._id && utils.isLocalId(docInfo._id)) {
return checkDone(); // skip local docs
}
var id = docInfo.metadata.id;
tx.executeSql('SELECT json FROM ' + DOC_STORE +
' WHERE id = ?', [id], function (tx, result) {
if (result.rows.length) {
var metadata = utils.safeJsonParse(result.rows.item(0).json);
fetchedDocs.set(id, metadata);
}
checkDone();
});
});
}
function saveAttachment(digest, data, callback) {
var sql = 'SELECT digest FROM ' + ATTACH_STORE + ' WHERE digest=?';
tx.executeSql(sql, [digest], function (tx, result) {
if (result.rows.length) { // attachment already exists
return callback();
}
// we could just insert before selecting and catch the error,
// but my hunch is that it's cheaper not to serialize the blob
// from JS to C if we don't have to (TODO: confirm this)
sql = 'INSERT INTO ' + ATTACH_STORE +
' (digest, body, escaped) VALUES (?,?,1)';
tx.executeSql(sql, [digest, websqlUtils.escapeBlob(data)], function () {
callback();
}, function () {
// ignore constaint errors, means it already exists
callback();
return false; // ack we handled the error
});
});
}
utils.preprocessAttachments(docInfos, 'binary', function (err) {
if (err) {
return callback(err);
}
db.transaction(function (txn) {
tx = txn;
verifyAttachments(function (err) {
if (err) {
preconditionErrored = err;
} else {
fetchExistingDocs(processDocs);
}
});
}, unknownError(callback), complete);
});
}
module.exports = websqlBulkDocs;
},{}],10:[function(_dereq_,module,exports){
'use strict';
function quote(str) {
return "'" + str + "'";
}
exports.ADAPTER_VERSION = 7; // used to manage migrations
// The object stores created for each database
// DOC_STORE stores the document meta data, its revision history and state
exports.DOC_STORE = quote('document-store');
// BY_SEQ_STORE stores a particular version of a document, keyed by its
// sequence id
exports.BY_SEQ_STORE = quote('by-sequence');
// Where we store attachments
exports.ATTACH_STORE = quote('attach-store');
exports.LOCAL_STORE = quote('local-store');
exports.META_STORE = quote('metadata-store');
// where we store many-to-many relations between attachment
// digests and seqs
exports.ATTACH_AND_SEQ_STORE = quote('attach-seq-store');
},{}],11:[function(_dereq_,module,exports){
'use strict';
var utils = _dereq_(32);
var errors = _dereq_(19);
var websqlConstants = _dereq_(10);
var BY_SEQ_STORE = websqlConstants.BY_SEQ_STORE;
var ATTACH_STORE = websqlConstants.ATTACH_STORE;
var ATTACH_AND_SEQ_STORE = websqlConstants.ATTACH_AND_SEQ_STORE;
// escapeBlob and unescapeBlob are workarounds for a websql bug:
// https://code.google.com/p/chromium/issues/detail?id=422690
// https://bugs.webkit.org/show_bug.cgi?id=137637
// The goal is to never actually insert the \u0000 character
// in the database.
function escapeBlob(str) {
return str
.replace(/\u0002/g, '\u0002\u0002')
.replace(/\u0001/g, '\u0001\u0002')
.replace(/\u0000/g, '\u0001\u0001');
}
function unescapeBlob(str) {
return str
.replace(/\u0001\u0001/g, '\u0000')
.replace(/\u0001\u0002/g, '\u0001')
.replace(/\u0002\u0002/g, '\u0002');
}
function stringifyDoc(doc) {
// don't bother storing the id/rev. it uses lots of space,
// in persistent map/reduce especially
delete doc._id;
delete doc._rev;
return JSON.stringify(doc);
}
function unstringifyDoc(doc, id, rev) {
doc = JSON.parse(doc);
doc._id = id;
doc._rev = rev;
return doc;
}
// question mark groups IN queries, e.g. 3 -> '(?,?,?)'
function qMarks(num) {
var s = '(';
while (num--) {
s += '?';
if (num) {
s += ',';
}
}
return s + ')';
}
function select(selector, table, joiner, where, orderBy) {
return 'SELECT ' + selector + ' FROM ' +
(typeof table === 'string' ? table : table.join(' JOIN ')) +
(joiner ? (' ON ' + joiner) : '') +
(where ? (' WHERE ' +
(typeof where === 'string' ? where : where.join(' AND '))) : '') +
(orderBy ? (' ORDER BY ' + orderBy) : '');
}
function compactRevs(revs, docId, tx) {
if (!revs.length) {
return;
}
var numDone = 0;
var seqs = [];
function checkDone() {
if (++numDone === revs.length) { // done
deleteOrphans();
}
}
function deleteOrphans() {
// find orphaned attachment digests
if (!seqs.length) {
return;
}
var sql = 'SELECT DISTINCT digest AS digest FROM ' +
ATTACH_AND_SEQ_STORE + ' WHERE seq IN ' + qMarks(seqs.length);
tx.executeSql(sql, seqs, function (tx, res) {
var digestsToCheck = [];
for (var i = 0; i < res.rows.length; i++) {
digestsToCheck.push(res.rows.item(i).digest);
}
if (!digestsToCheck.length) {
return;
}
var sql = 'DELETE FROM ' + ATTACH_AND_SEQ_STORE +
' WHERE seq IN (' +
seqs.map(function () { return '?'; }).join(',') +
')';
tx.executeSql(sql, seqs, function (tx) {
var sql = 'SELECT digest FROM ' + ATTACH_AND_SEQ_STORE +
' WHERE digest IN (' +
digestsToCheck.map(function () { return '?'; }).join(',') +
')';
tx.executeSql(sql, digestsToCheck, function (tx, res) {
var nonOrphanedDigests = new utils.Set();
for (var i = 0; i < res.rows.length; i++) {
nonOrphanedDigests.add(res.rows.item(i).digest);
}
digestsToCheck.forEach(function (digest) {
if (nonOrphanedDigests.has(digest)) {
return;
}
tx.executeSql(
'DELETE FROM ' + ATTACH_AND_SEQ_STORE + ' WHERE digest=?',
[digest]);
tx.executeSql(
'DELETE FROM ' + ATTACH_STORE + ' WHERE digest=?', [digest]);
});
});
});
});
}
// update by-seq and attach stores in parallel
revs.forEach(function (rev) {
var sql = 'SELECT seq FROM ' + BY_SEQ_STORE +
' WHERE doc_id=? AND rev=?';
tx.executeSql(sql, [docId, rev], function (tx, res) {
if (!res.rows.length) { // already deleted
return checkDone();
}
var seq = res.rows.item(0).seq;
seqs.push(seq);
tx.executeSql(
'DELETE FROM ' + BY_SEQ_STORE + ' WHERE seq=?', [seq], checkDone);
});
});
}
function unknownError(callback) {
return function (event) {
// event may actually be a SQLError object, so report is as such
var errorNameMatch = event && event.constructor.toString()
.match(/function ([^\(]+)/);
var errorName = (errorNameMatch && errorNameMatch[1]) || event.type;
var errorReason = event.target || event.message;
callback(errors.error(errors.WSQ_ERROR, errorReason, errorName));
};
}
function getSize(opts) {
if ('size' in opts) {
// triggers immediate popup in iOS, fixes #2347
// e.g. 5000001 asks for 5 MB, 10000001 asks for 10 MB,
return opts.size * 1000000;
}
// In iOS, doesn't matter as long as it's <= 5000000.
// Except that if you request too much, our tests fail
// because of the native "do you accept?" popup.
// In Android <=4.3, this value is actually used as an
// honest-to-god ceiling for data, so we need to
// set it to a decently high number.
var isAndroid = /Android/.test(window.navigator.userAgent);
return isAndroid ? 5000000 : 1; // in PhantomJS, if you use 0 it will crash
}
module.exports = {
escapeBlob: escapeBlob,
unescapeBlob: unescapeBlob,
stringifyDoc: stringifyDoc,
unstringifyDoc: unstringifyDoc,
qMarks: qMarks,
select: select,
compactRevs: compactRevs,
unknownError: unknownError,
getSize: getSize
};
},{}],12:[function(_dereq_,module,exports){
'use strict';
var utils = _dereq_(32);
var merge = _dereq_(27);
var errors = _dereq_(19);
var parseHexString = _dereq_(21);
var websqlConstants = _dereq_(10);
var websqlUtils = _dereq_(11);
var websqlBulkDocs = _dereq_(9);
var ADAPTER_VERSION = websqlConstants.ADAPTER_VERSION;
var DOC_STORE = websqlConstants.DOC_STORE;
var BY_SEQ_STORE = websqlConstants.BY_SEQ_STORE;
var ATTACH_STORE = websqlConstants.ATTACH_STORE;
var LOCAL_STORE = websqlConstants.LOCAL_STORE;
var META_STORE = websqlConstants.META_STORE;
var ATTACH_AND_SEQ_STORE = websqlConstants.ATTACH_AND_SEQ_STORE;
var qMarks = websqlUtils.qMarks;
var stringifyDoc = websqlUtils.stringifyDoc;
var unstringifyDoc = websqlUtils.unstringifyDoc;
var select = websqlUtils.select;
var compactRevs = websqlUtils.compactRevs;
var unknownError = websqlUtils.unknownError;
var getSize = websqlUtils.getSize;
function fetchAttachmentsIfNecessary(doc, opts, api, txn, cb) {
var attachments = Object.keys(doc._attachments || {});
if (!attachments.length) {
return cb && cb();
}
var numDone = 0;
function checkDone() {
if (++numDone === attachments.length && cb) {
cb();
}
}
function fetchAttachment(doc, att) {
var attObj = doc._attachments[att];
var attOpts = {encode: true, ctx: txn};
api._getAttachment(attObj, attOpts, function (_, base64) {
doc._attachments[att] = utils.extend(
utils.pick(attObj, ['digest', 'content_type']),
{ data: base64 }
);
checkDone();
});
}
attachments.forEach(function (att) {
if (opts.attachments && opts.include_docs) {
fetchAttachment(doc, att);
} else {
doc._attachments[att].stub = true;
checkDone();
}
});
}
var cachedDatabases = {};
var openDBFunction = (typeof navigator !== 'undefined' &&
navigator.sqlitePlugin &&
navigator.sqlitePlugin.openDatabase) ?
navigator.sqlitePlugin.openDatabase.bind(navigator.sqlitePlugin) :
(typeof sqlitePlugin !== 'undefined' && sqlitePlugin.openDatabase) ?
sqlitePlugin.openDatabase.bind(sqlitePlugin) :
(typeof openDatabase !== 'undefined') ?
openDatabase :
null;
function openDB(name, version, desc, size) {
var db = cachedDatabases[name];
if (!db) {
db = cachedDatabases[name] = openDBFunction(name, version, desc, size);
}
return db;
}
var POUCH_VERSION = 1;
// these indexes cover the ground for most allDocs queries
var BY_SEQ_STORE_DELETED_INDEX_SQL =
'CREATE INDEX IF NOT EXISTS \'by-seq-deleted-idx\' ON ' +
BY_SEQ_STORE + ' (seq, deleted)';
var BY_SEQ_STORE_DOC_ID_REV_INDEX_SQL =
'CREATE UNIQUE INDEX IF NOT EXISTS \'by-seq-doc-id-rev\' ON ' +
BY_SEQ_STORE + ' (doc_id, rev)';
var DOC_STORE_WINNINGSEQ_INDEX_SQL =
'CREATE INDEX IF NOT EXISTS \'doc-winningseq-idx\' ON ' +
DOC_STORE + ' (winningseq)';
var ATTACH_AND_SEQ_STORE_SEQ_INDEX_SQL =
'CREATE INDEX IF NOT EXISTS \'attach-seq-seq-idx\' ON ' +
ATTACH_AND_SEQ_STORE + ' (seq)';
var ATTACH_AND_SEQ_STORE_ATTACH_INDEX_SQL =
'CREATE UNIQUE INDEX IF NOT EXISTS \'attach-seq-digest-idx\' ON ' +
ATTACH_AND_SEQ_STORE + ' (digest, seq)';
var DOC_STORE_AND_BY_SEQ_JOINER = BY_SEQ_STORE +
'.seq = ' + DOC_STORE + '.winningseq';
var SELECT_DOCS = BY_SEQ_STORE + '.seq AS seq, ' +
BY_SEQ_STORE + '.deleted AS deleted, ' +
BY_SEQ_STORE + '.json AS data, ' +
BY_SEQ_STORE + '.rev AS rev, ' +
DOC_STORE + '.json AS metadata';
function WebSqlPouch(opts, callback) {
var api = this;
var instanceId = null;
var size = getSize(opts);
var idRequests = [];
var encoding;
api._docCount = -1; // cache sqlite count(*) for performance
api._name = opts.name;
var db = openDB(api._name, POUCH_VERSION, api._name, size);
if (!db) {
return callback(errors.error(errors.UNKNOWN_ERROR));
} else if (typeof db.readTransaction !== 'function') {
// doesn't exist in sqlite plugin
db.readTransaction = db.transaction;
}
function dbCreated() {
// note the db name in case the browser upgrades to idb
if (utils.hasLocalStorage()) {
window.localStorage['_pouch__websqldb_' + api._name] = true;
}
callback(null, api);
}
// In this migration, we added the 'deleted' and 'local' columns to the
// by-seq and doc store tables.
// To preserve existing user data, we re-process all the existing JSON
// and add these values.
// Called migration2 because it corresponds to adapter version (db_version) #2
function runMigration2(tx, callback) {
// index used for the join in the allDocs query
tx.executeSql(DOC_STORE_WINNINGSEQ_INDEX_SQL);
tx.executeSql('ALTER TABLE ' + BY_SEQ_STORE +
' ADD COLUMN deleted TINYINT(1) DEFAULT 0', [], function () {
tx.executeSql(BY_SEQ_STORE_DELETED_INDEX_SQL);
tx.executeSql('ALTER TABLE ' + DOC_STORE +
' ADD COLUMN local TINYINT(1) DEFAULT 0', [], function () {
tx.executeSql('CREATE INDEX IF NOT EXISTS \'doc-store-local-idx\' ON ' +
DOC_STORE + ' (local, id)');
var sql = 'SELECT ' + DOC_STORE + '.winningseq AS seq, ' + DOC_STORE +
'.json AS metadata FROM ' + BY_SEQ_STORE + ' JOIN ' + DOC_STORE +
' ON ' + BY_SEQ_STORE + '.seq = ' + DOC_STORE + '.winningseq';
tx.executeSql(sql, [], function (tx, result) {
var deleted = [];
var local = [];
for (var i = 0; i < result.rows.length; i++) {
var item = result.rows.item(i);
var seq = item.seq;
var metadata = JSON.parse(item.metadata);
if (utils.isDeleted(metadata)) {
deleted.push(seq);
}
if (utils.isLocalId(metadata.id)) {
local.push(metadata.id);
}
}
tx.executeSql('UPDATE ' + DOC_STORE + 'SET local = 1 WHERE id IN ' +
qMarks(local.length), local, function () {
tx.executeSql('UPDATE ' + BY_SEQ_STORE +
' SET deleted = 1 WHERE seq IN ' +
qMarks(deleted.length), deleted, callback);
});
});
});
});
}
// in this migration, we make all the local docs unversioned
function runMigration3(tx, callback) {
var local = 'CREATE TABLE IF NOT EXISTS ' + LOCAL_STORE +
' (id UNIQUE, rev, json)';
tx.executeSql(local, [], function () {
var sql = 'SELECT ' + DOC_STORE + '.id AS id, ' +
BY_SEQ_STORE + '.json AS data ' +
'FROM ' + BY_SEQ_STORE + ' JOIN ' +
DOC_STORE + ' ON ' + BY_SEQ_STORE + '.seq = ' +
DOC_STORE + '.winningseq WHERE local = 1';
tx.executeSql(sql, [], function (tx, res) {
var rows = [];
for (var i = 0; i < res.rows.length; i++) {
rows.push(res.rows.item(i));
}
function doNext() {
if (!rows.length) {
return callback(tx);
}
var row = rows.shift();
var rev = JSON.parse(row.data)._rev;
tx.executeSql('INSERT INTO ' + LOCAL_STORE +
' (id, rev, json) VALUES (?,?,?)',
[row.id, rev, row.data], function (tx) {
tx.executeSql('DELETE FROM ' + DOC_STORE + ' WHERE id=?',
[row.id], function (tx) {
tx.executeSql('DELETE FROM ' + BY_SEQ_STORE + ' WHERE seq=?',
[row.seq], function () {
doNext();
});
});
});
}
doNext();
});
});
}
// in this migration, we remove doc_id_rev and just use rev
function runMigration4(tx, callback) {
function updateRows(rows) {
function doNext() {
if (!rows.length) {
return callback(tx);
}
var row = rows.shift();
var doc_id_rev = parseHexString(row.hex, encoding);
var idx = doc_id_rev.lastIndexOf('::');
var doc_id = doc_id_rev.substring(0, idx);
var rev = doc_id_rev.substring(idx + 2);
var sql = 'UPDATE ' + BY_SEQ_STORE +
' SET doc_id=?, rev=? WHERE doc_id_rev=?';
tx.executeSql(sql, [doc_id, rev, doc_id_rev], function () {
doNext();
});
}
doNext();
}
var sql = 'ALTER TABLE ' + BY_SEQ_STORE + ' ADD COLUMN doc_id';
tx.executeSql(sql, [], function (tx) {
var sql = 'ALTER TABLE ' + BY_SEQ_STORE + ' ADD COLUMN rev';
tx.executeSql(sql, [], function (tx) {
tx.executeSql(BY_SEQ_STORE_DOC_ID_REV_INDEX_SQL, [], function (tx) {
var sql = 'SELECT hex(doc_id_rev) as hex FROM ' + BY_SEQ_STORE;
tx.executeSql(sql, [], function (tx, res) {
var rows = [];
for (var i = 0; i < res.rows.length; i++) {
rows.push(res.rows.item(i));
}
updateRows(rows);
});
});
});
});
}
// in this migration, we add the attach_and_seq table
// for issue #2818
function runMigration5(tx, callback) {
function migrateAttsAndSeqs(tx) {
// need to actually populate the table. this is the expensive part,
// so as an optimization, check first that this database even
// contains attachments
var sql = 'SELECT COUNT(*) AS cnt FROM ' + ATTACH_STORE;
tx.executeSql(sql, [], function (tx, res) {
var count = res.rows.item(0).cnt;
if (!count) {
return callback(tx);
}
var offset = 0;
var pageSize = 10;
function nextPage() {
var sql = select(
SELECT_DOCS + ', ' + DOC_STORE + '.id AS id',
[DOC_STORE, BY_SEQ_STORE],
DOC_STORE_AND_BY_SEQ_JOINER,
null,
DOC_STORE + '.id '
);
sql += ' LIMIT ' + pageSize + ' OFFSET ' + offset;
offset += pageSize;
tx.executeSql(sql, [], function (tx, res) {
if (!res.rows.length) {
return callback(tx);
}
var digestSeqs = {};
function addDigestSeq(digest, seq) {
// uniq digest/seq pairs, just in case there are dups
var seqs = digestSeqs[digest] = (digestSeqs[digest] || []);
if (seqs.indexOf(seq) === -1) {
seqs.push(seq);
}
}
for (var i = 0; i < res.rows.length; i++) {
var row = res.rows.item(i);
var doc = unstringifyDoc(row.data, row.id, row.rev);
var atts = Object.keys(doc._attachments || {});
for (var j = 0; j < atts.length; j++) {
var att = doc._attachments[atts[j]];
addDigestSeq(att.digest, row.seq);
}
}
var digestSeqPairs = [];
Object.keys(digestSeqs).forEach(function (digest) {
var seqs = digestSeqs[digest];
seqs.forEach(function (seq) {
digestSeqPairs.push([digest, seq]);
});
});
if (!digestSeqPairs.length) {
return nextPage();
}
var numDone = 0;
digestSeqPairs.forEach(function (pair) {
var sql = 'INSERT INTO ' + ATTACH_AND_SEQ_STORE +
' (digest, seq) VALUES (?,?)';
tx.executeSql(sql, pair, function () {
if (++numDone === digestSeqPairs.length) {
nextPage();
}
});
});
});
}
nextPage();
});
}
var attachAndRev = 'CREATE TABLE IF NOT EXISTS ' +
ATTACH_AND_SEQ_STORE + ' (digest, seq INTEGER)';
tx.executeSql(attachAndRev, [], function (tx) {
tx.executeSql(
ATTACH_AND_SEQ_STORE_ATTACH_INDEX_SQL, [], function (tx) {
tx.executeSql(
ATTACH_AND_SEQ_STORE_SEQ_INDEX_SQL, [],
migrateAttsAndSeqs);
});
});
}
// in this migration, we use escapeBlob() and unescapeBlob()
// instead of reading out the binary as HEX, which is slow
function runMigration6(tx, callback) {
var sql = 'ALTER TABLE ' + ATTACH_STORE +
' ADD COLUMN escaped TINYINT(1) DEFAULT 0';
tx.executeSql(sql, [], callback);
}
// issue #3136, in this migration we need a "latest seq" as well
// as the "winning seq" in the doc store
function runMigration7(tx, callback) {
var sql = 'ALTER TABLE ' + DOC_STORE +
' ADD COLUMN max_seq INTEGER';
tx.executeSql(sql, [], function (tx) {
var sql = 'UPDATE ' + DOC_STORE + ' SET max_seq=(SELECT MAX(seq) FROM ' +
BY_SEQ_STORE + ' WHERE doc_id=id)';
tx.executeSql(sql, [], function (tx) {
// add unique index after filling, else we'll get a constraint
// error when we do the ALTER TABLE
var sql =
'CREATE UNIQUE INDEX IF NOT EXISTS \'doc-max-seq-idx\' ON ' +
DOC_STORE + ' (max_seq)';
tx.executeSql(sql, [], callback);
});
});
}
function checkEncoding(tx, cb) {
// UTF-8 on chrome/android, UTF-16 on safari < 7.1
tx.executeSql('SELECT HEX("a") AS hex', [], function (tx, res) {
var hex = res.rows.item(0).hex;
encoding = hex.length === 2 ? 'UTF-8' : 'UTF-16';
cb();
}
);
}
function onGetInstanceId() {
while (idRequests.length > 0) {
var idCallback = idRequests.pop();
idCallback(null, instanceId);
}
}
function onGetVersion(tx, dbVersion) {
if (dbVersion === 0) {
// initial schema
var meta = 'CREATE TABLE IF NOT EXISTS ' + META_STORE +
' (dbid, db_version INTEGER)';
var attach = 'CREATE TABLE IF NOT EXISTS ' + ATTACH_STORE +
' (digest UNIQUE, escaped TINYINT(1), body BLOB)';
var attachAndRev = 'CREATE TABLE IF NOT EXISTS ' +
ATTACH_AND_SEQ_STORE + ' (digest, seq INTEGER)';
// TODO: migrate winningseq to INTEGER
var doc = 'CREATE TABLE IF NOT EXISTS ' + DOC_STORE +
' (id unique, json, winningseq, max_seq INTEGER UNIQUE)';
var seq = 'CREATE TABLE IF NOT EXISTS ' + BY_SEQ_STORE +
' (seq INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, ' +
'json, deleted TINYINT(1), doc_id, rev)';
var local = 'CREATE TABLE IF NOT EXISTS ' + LOCAL_STORE +
' (id UNIQUE, rev, json)';
// creates
tx.executeSql(attach);
tx.executeSql(local);
tx.executeSql(attachAndRev, [], function () {
tx.executeSql(ATTACH_AND_SEQ_STORE_SEQ_INDEX_SQL);
tx.executeSql(ATTACH_AND_SEQ_STORE_ATTACH_INDEX_SQL);
});
tx.executeSql(doc, [], function () {
tx.executeSql(DOC_STORE_WINNINGSEQ_INDEX_SQL);
tx.executeSql(seq, [], function () {
tx.executeSql(BY_SEQ_STORE_DELETED_INDEX_SQL);
tx.executeSql(BY_SEQ_STORE_DOC_ID_REV_INDEX_SQL);
tx.executeSql(meta, [], function () {
// mark the db version, and new dbid
var initSeq = 'INSERT INTO ' + META_STORE +
' (db_version, dbid) VALUES (?,?)';
instanceId = utils.uuid();
var initSeqArgs = [ADAPTER_VERSION, instanceId];
tx.executeSql(initSeq, initSeqArgs, function () {
onGetInstanceId();
});
});
});
});
} else { // version > 0
var setupDone = function () {
var migrated = dbVersion < ADAPTER_VERSION;
if (migrated) {
// update the db version within this transaction
tx.executeSql('UPDATE ' + META_STORE + ' SET db_version = ' +
ADAPTER_VERSION);
}
// notify db.id() callers
var sql = 'SELECT dbid FROM ' + META_STORE;
tx.executeSql(sql, [], function (tx, result) {
instanceId = result.rows.item(0).dbid;
onGetInstanceId();
});
};
// would love to use promises here, but then websql
// ends the transaction early
var tasks = [
runMigration2,
runMigration3,
runMigration4,
runMigration5,
runMigration6,
runMigration7,
setupDone
];
// run each migration sequentially
var i = dbVersion;
var nextMigration = function (tx) {
tasks[i - 1](tx, nextMigration);
i++;
};
nextMigration(tx);
}
}
function setup() {
db.transaction(function (tx) {
// first check the encoding
checkEncoding(tx, function () {
// then get the version
fetchVersion(tx);
});
}, unknownError(callback), dbCreated);
}
function fetchVersion(tx) {
var sql = 'SELECT sql FROM sqlite_master WHERE tbl_name = ' + META_STORE;
tx.executeSql(sql, [], function (tx, result) {
if (!result.rows.length) {
// database hasn't even been created yet (version 0)
onGetVersion(tx, 0);
} else if (!/db_version/.test(result.rows.item(0).sql)) {
// table was created, but without the new db_version column,
// so add it.
tx.executeSql('ALTER TABLE ' + META_STORE +
' ADD COLUMN db_version INTEGER', [], function () {
// before version 2, this column didn't even exist
onGetVersion(tx, 1);
});
} else { // column exists, we can safely get it
tx.executeSql('SELECT db_version FROM ' + META_STORE,
[], function (tx, result) {
var dbVersion = result.rows.item(0).db_version;
onGetVersion(tx, dbVersion);
});
}
});
}
if (utils.isCordova()) {
//to wait until custom api is made in pouch.adapters before doing setup
window.addEventListener(api._name + '_pouch', function cordova_init() {
window.removeEventListener(api._name + '_pouch', cordova_init, false);
setup();
}, false);
} else {
setup();
}
api.type = function () {
return 'websql';
};
api._id = utils.toPromise(function (callback) {
callback(null, instanceId);
});
api._info = function (callback) {
db.readTransaction(function (tx) {
countDocs(tx, function (docCount) {
var sql = 'SELECT MAX(seq) AS seq FROM ' + BY_SEQ_STORE;
tx.executeSql(sql, [], function (tx, res) {
var updateSeq = res.rows.item(0).seq || 0;
callback(null, {
doc_count: docCount,
update_seq: updateSeq
});
});
});
}, unknownError(callback));
};
api._bulkDocs = function (req, opts, callback) {
websqlBulkDocs(req, opts, api, db, WebSqlPouch.Changes, callback);
};
api._get = function (id, opts, callback) {
opts = utils.clone(opts);
var doc;
var metadata;
var err;
if (!opts.ctx) {
db.readTransaction(function (txn) {
opts.ctx = txn;
api._get(id, opts, callback);
});
return;
}
var tx = opts.ctx;
function finish() {
callback(err, {doc: doc, metadata: metadata, ctx: tx});
}
var sql;
var sqlArgs;
if (opts.rev) {
sql = select(
SELECT_DOCS,
[DOC_STORE, BY_SEQ_STORE],
DOC_STORE + '.id=' + BY_SEQ_STORE + '.doc_id',
[BY_SEQ_STORE + '.doc_id=?', BY_SEQ_STORE + '.rev=?']);
sqlArgs = [id, opts.rev];
} else {
sql = select(
SELECT_DOCS,
[DOC_STORE, BY_SEQ_STORE],
DOC_STORE_AND_BY_SEQ_JOINER,
DOC_STORE + '.id=?');
sqlArgs = [id];
}
tx.executeSql(sql, sqlArgs, function (a, results) {
if (!results.rows.length) {
err = errors.error(errors.MISSING_DOC, 'missing');
return finish();
}
var item = results.rows.item(0);
metadata = utils.safeJsonParse(item.metadata);
if (item.deleted && !opts.rev) {
err = errors.error(errors.MISSING_DOC, 'deleted');
return finish();
}
doc = unstringifyDoc(item.data, metadata.id, item.rev);
finish();
});
};
function countDocs(tx, callback) {
if (api._docCount !== -1) {
return callback(api._docCount);
}
// count the total rows
var sql = select(
'COUNT(' + DOC_STORE + '.id) AS \'num\'',
[DOC_STORE, BY_SEQ_STORE],
DOC_STORE_AND_BY_SEQ_JOINER,
BY_SEQ_STORE + '.deleted=0');
tx.executeSql(sql, [], function (tx, result) {
api._docCount = result.rows.item(0).num;
callback(api._docCount);
});
}
api._allDocs = function (opts, callback) {
var results = [];
var totalRows;
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 descending = 'descending' in opts ? opts.descending : false;
var limit = 'limit' in opts ? opts.limit : -1;
var offset = 'skip' in opts ? opts.skip : 0;
var inclusiveEnd = opts.inclusive_end !== false;
var sqlArgs = [];
var criteria = [];
if (key !== false) {
criteria.push(DOC_STORE + '.id = ?');
sqlArgs.push(key);
} else if (start !== false || end !== false) {
if (start !== false) {
criteria.push(DOC_STORE + '.id ' + (descending ? '<=' : '>=') + ' ?');
sqlArgs.push(start);
}
if (end !== false) {
var comparator = descending ? '>' : '<';
if (inclusiveEnd) {
comparator += '=';
}
criteria.push(DOC_STORE + '.id ' + comparator + ' ?');
sqlArgs.push(end);
}
if (key !== false) {
criteria.push(DOC_STORE + '.id = ?');
sqlArgs.push(key);
}
}
if (opts.deleted !== 'ok') {
// report deleted if keys are specified
criteria.push(BY_SEQ_STORE + '.deleted = 0');
}
db.readTransaction(function (tx) {
// first count up the total rows
countDocs(tx, function (count) {
totalRows = count;
if (limit === 0) {
return;
}
// then actually fetch the documents
var sql = select(
SELECT_DOCS,
[DOC_STORE, BY_SEQ_STORE],
DOC_STORE_AND_BY_SEQ_JOINER,
criteria,
DOC_STORE + '.id ' + (descending ? 'DESC' : 'ASC')
);
sql += ' LIMIT ' + limit + ' OFFSET ' + offset;
tx.executeSql(sql, sqlArgs, function (tx, result) {
for (var i = 0, l = result.rows.length; i < l; i++) {
var item = result.rows.item(i);
var metadata = utils.safeJsonParse(item.metadata);
var id = metadata.id;
var data = unstringifyDoc(item.data, id, item.rev);
var winningRev = data._rev;
var doc = {
id: id,
key: id,
value: {rev: winningRev}
};
if (opts.include_docs) {
doc.doc = data;
doc.doc._rev = winningRev;
if (opts.conflicts) {
doc.doc._conflicts = merge.collectConflicts(metadata);
}
fetchAttachmentsIfNecessary(doc.doc, opts, api, tx);
}
if (item.deleted) {
if (opts.deleted === 'ok') {
doc.value.deleted = true;
doc.doc = null;
} else {
continue;
}
}
results.push(doc);
}
});
});
}, unknownError(callback), function () {
callback(null, {
total_rows: totalRows,
offset: opts.skip,
rows: results
});
});
};
api._changes = function (opts) {
opts = utils.clone(opts);
if (opts.continuous) {
var id = api._name + ':' + utils.uuid();
WebSqlPouch.Changes.addListener(api._name, id, api, opts);
WebSqlPouch.Changes.notify(api._name);
return {
cancel: function () {
WebSqlPouch.Changes.removeListener(api._name, id);
}
};
}
var descending = opts.descending;
// Ignore the `since` parameter when `descending` is true
opts.since = opts.since && !descending ? opts.since : 0;
var limit = 'limit' in opts ? opts.limit : -1;
if (limit === 0) {
limit = 1; // per CouchDB _changes spec
}
var returnDocs;
if ('returnDocs' in opts) {
returnDocs = opts.returnDocs;
} else {
returnDocs = true;
}
var results = [];
var numResults = 0;
function fetchChanges() {
var selectStmt =
DOC_STORE + '.json AS metadata, ' +
DOC_STORE + '.max_seq AS maxSeq, ' +
BY_SEQ_STORE + '.json AS winningDoc, ' +
BY_SEQ_STORE + '.rev AS winningRev ';
var from = DOC_STORE + ' JOIN ' + BY_SEQ_STORE;
var joiner = DOC_STORE + '.id=' + BY_SEQ_STORE + '.doc_id' +
' AND ' + DOC_STORE + '.winningseq=' + BY_SEQ_STORE + '.seq';
var criteria = ['maxSeq > ?'];
var sqlArgs = [opts.since];
if (opts.doc_ids) {
criteria.push(DOC_STORE + '.id IN ' + qMarks(opts.doc_ids.length));
sqlArgs = sqlArgs.concat(opts.doc_ids);
}
var orderBy = 'maxSeq ' + (descending ? 'DESC' : 'ASC');
var sql = select(selectStmt, from, joiner, criteria, orderBy);
var filter = utils.filterChange(opts);
if (!opts.view && !opts.filter) {
// we can just limit in the query
sql += ' LIMIT ' + limit;
}
var lastSeq = opts.since || 0;
db.readTransaction(function (tx) {
tx.executeSql(sql, sqlArgs, function (tx, result) {
function reportChange(change) {
return function () {
opts.onChange(change);
};
}
for (var i = 0, l = result.rows.length; i < l; i++) {
var item = result.rows.item(i);
var metadata = utils.safeJsonParse(item.metadata);
lastSeq = item.maxSeq;
var doc = unstringifyDoc(item.winningDoc, metadata.id,
item.winningRev);
var change = opts.processChange(doc, metadata, opts);
change.seq = item.maxSeq;
if (filter(change)) {
numResults++;
if (returnDocs) {
results.push(change);
}
// process the attachment immediately
// for the benefit of live listeners
if (opts.attachments && opts.include_docs) {
fetchAttachmentsIfNecessary(doc, opts, api, tx,
reportChange(change));
} else {
reportChange(change)();
}
}
if (numResults === limit) {
break;
}
}
});
}, unknownError(opts.complete), function () {
if (!opts.continuous) {
opts.complete(null, {
results: results,
last_seq: lastSeq
});
}
});
}
fetchChanges();
};
api._close = function (callback) {
//WebSQL databases do not need to be closed
callback();
};
api._getAttachment = function (attachment, opts, callback) {
var res;
var tx = opts.ctx;
var digest = attachment.digest;
var type = attachment.content_type;
var sql = 'SELECT escaped, ' +
'CASE WHEN escaped = 1 THEN body ELSE HEX(body) END AS body FROM ' +
ATTACH_STORE + ' WHERE digest=?';
tx.executeSql(sql, [digest], function (tx, result) {
// websql has a bug where \u0000 causes early truncation in strings
// and blobs. to work around this, we used to use the hex() function,
// but that's not performant. after migration 6, we remove \u0000
// and add it back in afterwards
var item = result.rows.item(0);
var data = item.escaped ? websqlUtils.unescapeBlob(item.body) :
parseHexString(item.body, encoding);
if (opts.encode) {
res = btoa(data);
} else {
data = utils.fixBinary(data);
res = utils.createBlob([data], {type: type});
}
callback(null, res);
});
};
api._getRevisionTree = function (docId, callback) {
db.readTransaction(function (tx) {
var sql = 'SELECT json AS metadata FROM ' + DOC_STORE + ' WHERE id = ?';
tx.executeSql(sql, [docId], function (tx, result) {
if (!result.rows.length) {
callback(errors.error(errors.MISSING_DOC));
} else {
var data = utils.safeJsonParse(result.rows.item(0).metadata);
callback(null, data.rev_tree);
}
});
});
};
api._doCompaction = function (docId, revs, callback) {
if (!revs.length) {
return callback();
}
db.transaction(function (tx) {
// update doc store
var sql = 'SELECT json AS metadata FROM ' + DOC_STORE + ' WHERE id = ?';
tx.executeSql(sql, [docId], function (tx, result) {
var metadata = utils.safeJsonParse(result.rows.item(0).metadata);
merge.traverseRevTree(metadata.rev_tree, function (isLeaf, pos,
revHash, ctx, opts) {
var rev = pos + '-' + revHash;
if (revs.indexOf(rev) !== -1) {
opts.status = 'missing';
}
});
var sql = 'UPDATE ' + DOC_STORE + ' SET json = ? WHERE id = ?';
tx.executeSql(sql, [utils.safeJsonStringify(metadata), docId]);
});
compactRevs(revs, docId, tx);
}, unknownError(callback), function () {
callback();
});
};
api._getLocal = function (id, callback) {
db.readTransaction(function (tx) {
var sql = 'SELECT json, rev FROM ' + LOCAL_STORE + ' WHERE id=?';
tx.executeSql(sql, [id], function (tx, res) {
if (res.rows.length) {
var item = res.rows.item(0);
var doc = unstringifyDoc(item.json, id, item.rev);
callback(null, doc);
} else {
callback(errors.error(errors.MISSING_DOC));
}
});
});
};
api._putLocal = function (doc, opts, callback) {
if (typeof opts === 'function') {
callback = opts;
opts = {};
}
delete doc._revisions; // ignore this, trust the rev
var oldRev = doc._rev;
var id = doc._id;
var newRev;
if (!oldRev) {
newRev = doc._rev = '0-1';
} else {
newRev = doc._rev = '0-' + (parseInt(oldRev.split('-')[1], 10) + 1);
}
var json = stringifyDoc(doc);
var ret;
function putLocal(tx) {
var sql;
var values;
if (oldRev) {
sql = 'UPDATE ' + LOCAL_STORE + ' SET rev=?, json=? ' +
'WHERE id=? AND rev=?';
values = [newRev, json, id, oldRev];
} else {
sql = 'INSERT INTO ' + LOCAL_STORE + ' (id, rev, json) VALUES (?,?,?)';
values = [id, newRev, json];
}
tx.executeSql(sql, values, function (tx, res) {
if (res.rowsAffected) {
ret = {ok: true, id: id, rev: newRev};
if (opts.ctx) { // return immediately
callback(null, ret);
}
} else {
callback(errors.error(errors.REV_CONFLICT));
}
}, function () {
callback(errors.error(errors.REV_CONFLICT));
return false; // ack that we handled the error
});
}
if (opts.ctx) {
putLocal(opts.ctx);
} else {
db.transaction(function (tx) {
putLocal(tx);
}, unknownError(callback), function () {
if (ret) {
callback(null, ret);
}
});
}
};
api._removeLocal = function (doc, callback) {
var ret;
db.transaction(function (tx) {
var sql = 'DELETE FROM ' + LOCAL_STORE + ' WHERE id=? AND rev=?';
var params = [doc._id, doc._rev];
tx.executeSql(sql, params, function (tx, res) {
if (!res.rowsAffected) {
return callback(errors.error(errors.MISSING_DOC));
}
ret = {ok: true, id: doc._id, rev: '0-0'};
});
}, unknownError(callback), function () {
if (ret) {
callback(null, ret);
}
});
};
}
WebSqlPouch.valid = function () {
return !!openDBFunction;
};
WebSqlPouch.destroy = utils.toPromise(function (name, opts, callback) {
WebSqlPouch.Changes.removeAllListeners(name);
var size = getSize(opts);
var db = openDB(name, POUCH_VERSION, name, size);
db.transaction(function (tx) {
var stores = [DOC_STORE, BY_SEQ_STORE, ATTACH_STORE, META_STORE,
LOCAL_STORE, ATTACH_AND_SEQ_STORE];
stores.forEach(function (store) {
tx.executeSql('DROP TABLE IF EXISTS ' + store, []);
});
}, unknownError(callback), function () {
if (utils.hasLocalStorage()) {
delete window.localStorage['_pouch__websqldb_' + name];
delete window.localStorage[name];
}
callback(null, {'ok': true});
});
});
WebSqlPouch.Changes = new utils.Changes();
module.exports = WebSqlPouch;
},{}],13:[function(_dereq_,module,exports){
'use strict';
var utils = _dereq_(32);
var merge = _dereq_(27);
var errors = _dereq_(19);
var EE = _dereq_(36).EventEmitter;
var evalFilter = _dereq_(25);
var evalView = _dereq_(26);
module.exports = Changes;
utils.inherits(Changes, EE);
function Changes(db, opts, callback) {
EE.call(this);
var self = this;
this.db = db;
opts = opts ? utils.clone(opts) : {};
var oldComplete = callback || opts.complete || function () {};
var complete = opts.complete = utils.once(function (err, resp) {
if (err) {
self.emit('error', err);
} else {
self.emit('complete', resp);
}
self.removeAllListeners();
db.removeListener('destroyed', onDestroy);
});
if (oldComplete) {
self.on('complete', function (resp) {
oldComplete(null, resp);
});
self.on('error', function (err) {
oldComplete(err);
});
}
var oldOnChange = opts.onChange;
if (oldOnChange) {
self.on('change', oldOnChange);
}
function onDestroy() {
self.cancel();
}
db.once('destroyed', onDestroy);
opts.onChange = function (change) {
if (opts.isCancelled) {
return;
}
self.emit('change', change);
if (self.startSeq && self.startSeq <= change.seq) {
self.emit('uptodate');
self.startSeq = false;
}
if (change.deleted) {
self.emit('delete', change);
} else if (change.changes.length === 1 &&
change.changes[0].rev.slice(0, 2) === '1-') {
self.emit('create', change);
} else {
self.emit('update', change);
}
};
var promise = new utils.Promise(function (fulfill, reject) {
opts.complete = function (err, res) {
if (err) {
reject(err);
} else {
fulfill(res);
}
};
});
self.once('cancel', function () {
if (oldOnChange) {
self.removeListener('change', oldOnChange);
}
opts.complete(null, {status: 'cancelled'});
});
this.then = promise.then.bind(promise);
this['catch'] = promise['catch'].bind(promise);
this.then(function (result) {
complete(null, result);
}, complete);
if (!db.taskqueue.isReady) {
db.taskqueue.addTask(function () {
if (self.isCancelled) {
self.emit('cancel');
} else {
self.doChanges(opts);
}
});
} else {
self.doChanges(opts);
}
}
Changes.prototype.cancel = function () {
this.isCancelled = true;
if (this.db.taskqueue.isReady) {
this.emit('cancel');
}
};
function processChange(doc, metadata, opts) {
var changeList = [{rev: doc._rev}];
if (opts.style === 'all_docs') {
changeList = merge.collectLeaves(metadata.rev_tree)
.map(function (x) { return {rev: x.rev}; });
}
var change = {
id: metadata.id,
changes: changeList,
doc: doc
};
if (utils.isDeleted(metadata, doc._rev)) {
change.deleted = true;
}
if (opts.conflicts) {
change.doc._conflicts = merge.collectConflicts(metadata);
if (!change.doc._conflicts.length) {
delete change.doc._conflicts;
}
}
return change;
}
Changes.prototype.doChanges = function (opts) {
var self = this;
var callback = opts.complete;
opts = utils.clone(opts);
if ('live' in opts && !('continuous' in opts)) {
opts.continuous = opts.live;
}
opts.processChange = processChange;
if (opts.since === 'latest') {
opts.since = 'now';
}
if (!opts.since) {
opts.since = 0;
}
if (opts.since === 'now') {
this.db.info().then(function (info) {
if (self.isCancelled) {
callback(null, {status: 'cancelled'});
return;
}
opts.since = info.update_seq;
self.doChanges(opts);
}, callback);
return;
}
if (opts.continuous && opts.since !== 'now') {
this.db.info().then(function (info) {
self.startSeq = info.update_seq;
}, function (err) {
if (err.id === 'idbNull') {
//db closed before this returned
//thats ok
return;
}
throw err;
});
}
if (this.db.type() !== 'http' &&
opts.filter && typeof opts.filter === 'string' &&
!opts.doc_ids) {
return this.filterChanges(opts);
}
if (!('descending' in opts)) {
opts.descending = false;
}
// 0 and 1 should return 1 document
opts.limit = opts.limit === 0 ? 1 : opts.limit;
opts.complete = callback;
var newPromise = this.db._changes(opts);
if (newPromise && typeof newPromise.cancel === 'function') {
var cancel = self.cancel;
self.cancel = utils.getArguments(function (args) {
newPromise.cancel();
cancel.apply(this, args);
});
}
};
Changes.prototype.filterChanges = function (opts) {
var self = this;
var callback = opts.complete;
if (opts.filter === '_view') {
if (!opts.view || typeof opts.view !== 'string') {
var err = errors.error(errors.BAD_REQUEST,
'`view` filter parameter is not provided.');
callback(err);
return;
}
// fetch a view from a design doc, make it behave like a filter
var viewName = opts.view.split('/');
this.db.get('_design/' + viewName[0], function (err, ddoc) {
if (self.isCancelled) {
callback(null, {status: 'cancelled'});
return;
}
if (err) {
callback(errors.generateErrorFromResponse(err));
return;
}
if (ddoc && ddoc.views && ddoc.views[viewName[1]]) {
var filter = evalView(ddoc.views[viewName[1]].map);
opts.filter = filter;
self.doChanges(opts);
return;
}
var msg = ddoc.views ? 'missing json key: ' + viewName[1] :
'missing json key: views';
if (!err) {
err = errors.error(errors.MISSING_DOC, msg);
}
callback(err);
return;
});
} else {
// fetch a filter from a design doc
var filterName = opts.filter.split('/');
this.db.get('_design/' + filterName[0], function (err, ddoc) {
if (self.isCancelled) {
callback(null, {status: 'cancelled'});
return;
}
if (err) {
callback(errors.generateErrorFromResponse(err));
return;
}
if (ddoc && ddoc.filters && ddoc.filters[filterName[1]]) {
var filter = evalFilter(ddoc.filters[filterName[1]]);
opts.filter = filter;
self.doChanges(opts);
return;
} else {
var msg = (ddoc && ddoc.filters) ? 'missing json key: ' + filterName[1]
: 'missing json key: filters';
if (!err) {
err = errors.error(errors.MISSING_DOC, msg);
}
callback(err);
return;
}
});
}
};
},{}],14:[function(_dereq_,module,exports){
'use strict';
var utils = _dereq_(32);
var pouchCollate = _dereq_(60);
var collate = pouchCollate.collate;
function updateCheckpoint(db, id, checkpoint, returnValue) {
return db.get(id)["catch"](function (err) {
if (err.status === 404) {
if (db.type() === 'http') {
utils.explain404(
'PouchDB is just checking if a remote checkpoint exists.');
}
return {_id: id};
}
throw err;
}).then(function (doc) {
if (returnValue.cancelled) {
return;
}
doc.last_seq = checkpoint;
return db.put(doc);
});
}
function Checkpointer(src, target, id, returnValue) {
this.src = src;
this.target = target;
this.id = id;
this.returnValue = returnValue;
}
Checkpointer.prototype.writeCheckpoint = function (checkpoint) {
var self = this;
return this.updateTarget(checkpoint).then(function () {
return self.updateSource(checkpoint);
});
};
Checkpointer.prototype.updateTarget = function (checkpoint) {
return updateCheckpoint(this.target, this.id, checkpoint, this.returnValue);
};
Checkpointer.prototype.updateSource = function (checkpoint) {
var self = this;
if (this.readOnlySource) {
return utils.Promise.resolve(true);
}
return updateCheckpoint(this.src, this.id, checkpoint, this.returnValue)[
"catch"](function (err) {
var isForbidden = typeof err.status === 'number' &&
Math.floor(err.status / 100) === 4;
if (isForbidden) {
self.readOnlySource = true;
return true;
}
throw err;
});
};
Checkpointer.prototype.getCheckpoint = function () {
var self = this;
return self.target.get(self.id).then(function (targetDoc) {
return self.src.get(self.id).then(function (sourceDoc) {
if (collate(targetDoc.last_seq, sourceDoc.last_seq) === 0) {
return sourceDoc.last_seq;
}
return 0;
}, function (err) {
if (err.status === 404 && targetDoc.last_seq) {
return self.src.put({
_id: self.id,
last_seq: 0
}).then(function () {
return 0;
}, function (err) {
if (err.status === 401) {
self.readOnlySource = true;
return targetDoc.last_seq;
}
return 0;
});
}
throw err;
});
})["catch"](function (err) {
if (err.status !== 404) {
throw err;
}
return 0;
});
};
module.exports = Checkpointer;
},{}],15:[function(_dereq_,module,exports){
(function (process,global){
/*globals cordova */
"use strict";
var Adapter = _dereq_(1);
var utils = _dereq_(32);
var TaskQueue = _dereq_(31);
var Promise = utils.Promise;
function defaultCallback(err) {
if (err && global.debug) {
console.error(err);
}
}
utils.inherits(PouchDB, Adapter);
function PouchDB(name, opts, callback) {
if (!(this instanceof PouchDB)) {
return new PouchDB(name, opts, callback);
}
var self = this;
if (typeof opts === 'function' || typeof opts === 'undefined') {
callback = opts;
opts = {};
}
if (name && typeof name === 'object') {
opts = name;
name = undefined;
}
if (typeof callback === 'undefined') {
callback = defaultCallback;
}
opts = opts || {};
this.__opts = opts;
var oldCB = callback;
self.auto_compaction = opts.auto_compaction;
self.prefix = PouchDB.prefix;
Adapter.call(self);
self.taskqueue = new TaskQueue();
var promise = new Promise(function (fulfill, reject) {
callback = function (err, resp) {
if (err) {
return reject(err);
}
delete resp.then;
fulfill(resp);
};
opts = utils.clone(opts);
var originalName = opts.name || name;
var backend, error;
(function () {
try {
if (typeof originalName !== 'string') {
error = new Error('Missing/invalid DB name');
error.code = 400;
throw error;
}
backend = PouchDB.parseAdapter(originalName, opts);
opts.originalName = originalName;
opts.name = backend.name;
if (opts.prefix && backend.adapter !== 'http' &&
backend.adapter !== 'https') {
opts.name = opts.prefix + opts.name;
}
opts.adapter = opts.adapter || backend.adapter;
self._adapter = opts.adapter;
self._db_name = originalName;
if (!PouchDB.adapters[opts.adapter]) {
error = new Error('Adapter is missing');
error.code = 404;
throw error;
}
if (!PouchDB.adapters[opts.adapter].valid()) {
error = new Error('Invalid Adapter');
error.code = 404;
throw error;
}
} catch (err) {
self.taskqueue.fail(err);
self.changes = utils.toPromise(function (opts) {
if (opts.complete) {
opts.complete(err);
}
});
}
}());
if (error) {
return reject(error); // constructor error, see above
}
self.adapter = opts.adapter;
// needs access to PouchDB;
self.replicate = {};
self.replicate.from = function (url, opts, callback) {
return self.constructor.replicate(url, self, opts, callback);
};
self.replicate.to = function (url, opts, callback) {
return self.constructor.replicate(self, url, opts, callback);
};
self.sync = function (dbName, opts, callback) {
return self.constructor.sync(self, dbName, opts, callback);
};
self.replicate.sync = self.sync;
self.destroy = utils.adapterFun('destroy', function (callback) {
var self = this;
var opts = this.__opts || {};
self.info(function (err, info) {
if (err) {
return callback(err);
}
opts.internal = true;
self.constructor.destroy(info.db_name, opts, callback);
});
});
PouchDB.adapters[opts.adapter].call(self, opts, function (err) {
if (err) {
if (callback) {
self.taskqueue.fail(err);
callback(err);
}
return;
}
function destructionListener(event) {
if (event === 'destroyed') {
self.emit('destroyed');
PouchDB.removeListener(originalName, destructionListener);
}
}
PouchDB.on(originalName, destructionListener);
self.emit('created', self);
PouchDB.emit('created', opts.originalName);
self.taskqueue.ready(self);
callback(null, self);
});
if (opts.skipSetup) {
self.taskqueue.ready(self);
process.nextTick(function () {
callback(null, self);
});
}
if (utils.isCordova()) {
//to inform websql adapter that we can use api
cordova.fireWindowEvent(opts.name + "_pouch", {});
}
});
promise.then(function (resp) {
oldCB(null, resp);
}, oldCB);
self.then = promise.then.bind(promise);
self["catch"] = promise["catch"].bind(promise);
}
PouchDB.debug = _dereq_(38);
module.exports = PouchDB;
}).call(this,_dereq_(37),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{}],16:[function(_dereq_,module,exports){
"use strict";
var createBlob = _dereq_(17);
var errors = _dereq_(19);
var utils = _dereq_(32);
var hasUpload;
function ajax(options, adapterCallback) {
var requestCompleted = false;
var callback = utils.getArguments(function (args) {
if (requestCompleted) {
return;
}
adapterCallback.apply(this, args);
requestCompleted = true;
});
if (typeof options === "function") {
callback = options;
options = {};
}
options = utils.clone(options);
var defaultOptions = {
method : "GET",
headers: {},
json: true,
processData: true,
timeout: 10000,
cache: false
};
options = utils.extend(true, defaultOptions, options);
// cache-buster, specifically designed to work around IE's aggressive caching
// see http://www.dashbay.com/2011/05/internet-explorer-caches-ajax/
if (options.method === 'GET' && !options.cache) {
var hasArgs = options.url.indexOf('?') !== -1;
options.url += (hasArgs ? '&' : '?') + '_nonce=' + utils.uuid(16);
}
function onSuccess(obj, resp, cb) {
if (!options.binary && !options.json && options.processData &&
typeof obj !== 'string') {
obj = JSON.stringify(obj);
} else if (!options.binary && options.json && typeof obj === 'string') {
try {
obj = JSON.parse(obj);
} catch (e) {
// Probably a malformed JSON from server
return cb(e);
}
}
if (Array.isArray(obj)) {
obj = obj.map(function (v) {
if (v.error || v.missing) {
return errors.generateErrorFromResponse(v);
} else {
return v;
}
});
}
cb(null, obj, resp);
}
function onError(err, cb) {
var errParsed, errObj;
try {
errParsed = JSON.parse(err.responseText);
//would prefer not to have a try/catch clause
errObj = errors.generateErrorFromResponse(errParsed);
} catch (e) {
errObj = errors.generateErrorFromResponse(err);
}
if (err.withCredentials && err.status === 0) {
// apparently this is what we get when the method
// is reported as not allowed by CORS. so fudge it
errObj.status = 405;
errObj.statusText = "Method Not Allowed";
}
cb(errObj);
}
var timer;
var xhr;
if (options.xhr) {
xhr = new options.xhr();
} else {
xhr = new XMLHttpRequest();
}
xhr.open(options.method, options.url);
xhr.withCredentials = true;
if (options.json) {
options.headers.Accept = 'application/json';
options.headers['Content-Type'] = options.headers['Content-Type'] ||
'application/json';
if (options.body &&
options.processData &&
typeof options.body !== "string") {
options.body = JSON.stringify(options.body);
}
}
if (options.binary) {
xhr.responseType = 'arraybuffer';
}
var createCookie = function (name, value, days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
}
document.cookie = name + "=" + value + expires + "; path=/";
};
for (var key in options.headers) {
if (options.headers.hasOwnProperty(key)) {
if (key === 'Cookie') {
var cookie = options.headers[key].split('=');
createCookie(cookie[0], cookie[1], 10);
} else {
xhr.setRequestHeader(key, options.headers[key]);
}
}
}
if (!("body" in options)) {
options.body = null;
}
var abortReq = function () {
if (requestCompleted) {
return;
}
xhr.abort();
onError(xhr, callback);
};
xhr.onreadystatechange = function () {
if (xhr.readyState !== 4 || requestCompleted) {
return;
}
clearTimeout(timer);
if (xhr.status >= 200 && xhr.status < 300) {
var data;
if (options.binary) {
data = createBlob([xhr.response || ''], {
type: xhr.getResponseHeader('Content-Type')
});
} else {
data = xhr.responseText;
}
onSuccess(data, xhr, callback);
} else {
onError(xhr, callback);
}
};
if (options.timeout > 0) {
timer = setTimeout(abortReq, options.timeout);
xhr.onprogress = function () {
clearTimeout(timer);
timer = setTimeout(abortReq, options.timeout);
};
if (typeof hasUpload === 'undefined') {
// IE throws an error if you try to access it directly
hasUpload = Object.keys(xhr).indexOf('upload') !== -1;
}
if (hasUpload) { // does not exist in ie9
xhr.upload.onprogress = xhr.onprogress;
}
}
if (options.body && (options.body instanceof Blob)) {
utils.readAsBinaryString(options.body, function (binary) {
xhr.send(utils.fixBinary(binary));
});
} else {
xhr.send(options.body);
}
return {abort: abortReq};
}
module.exports = ajax;
},{}],17:[function(_dereq_,module,exports){
(function (global){
"use strict";
//Abstracts constructing a Blob object, so it also works in older
//browsers that don't support the native Blob constructor. (i.e.
//old QtWebKit versions, at least).
function createBlob(parts, properties) {
parts = parts || [];
properties = properties || {};
try {
return new Blob(parts, properties);
} catch (e) {
if (e.name !== "TypeError") {
throw e;
}
var BlobBuilder = global.BlobBuilder ||
global.MSBlobBuilder ||
global.MozBlobBuilder ||
global.WebKitBlobBuilder;
var builder = new BlobBuilder();
for (var i = 0; i < parts.length; i += 1) {
builder.append(parts[i]);
}
return builder.getBlob(properties.type);
}
}
module.exports = createBlob;
}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{}],18:[function(_dereq_,module,exports){
// hey guess what, we don't need this in the browser
module.exports = {};
},{}],19:[function(_dereq_,module,exports){
"use strict";
var inherits = _dereq_(41);
inherits(PouchError, Error);
function PouchError(opts) {
Error.call(opts.reason);
this.status = opts.status;
this.name = opts.error;
this.message = opts.reason;
this.error = true;
}
PouchError.prototype.toString = function () {
return JSON.stringify({
status: this.status,
name: this.name,
message: this.message
});
};
exports.UNAUTHORIZED = new PouchError({
status: 401,
error: 'unauthorized',
reason: "Name or password is incorrect."
});
exports.MISSING_BULK_DOCS = new PouchError({
status: 400,
error: 'bad_request',
reason: "Missing JSON list of 'docs'"
});
exports.MISSING_DOC = new PouchError({
status: 404,
error: 'not_found',
reason: 'missing'
});
exports.REV_CONFLICT = new PouchError({
status: 409,
error: 'conflict',
reason: 'Document update conflict'
});
exports.INVALID_ID = new PouchError({
status: 400,
error: 'invalid_id',
reason: '_id field must contain a string'
});
exports.MISSING_ID = new PouchError({
status: 412,
error: 'missing_id',
reason: '_id is required for puts'
});
exports.RESERVED_ID = new PouchError({
status: 400,
error: 'bad_request',
reason: 'Only reserved document ids may start with underscore.'
});
exports.NOT_OPEN = new PouchError({
status: 412,
error: 'precondition_failed',
reason: 'Database not open'
});
exports.UNKNOWN_ERROR = new PouchError({
status: 500,
error: 'unknown_error',
reason: 'Database encountered an unknown error'
});
exports.BAD_ARG = new PouchError({
status: 500,
error: 'badarg',
reason: 'Some query argument is invalid'
});
exports.INVALID_REQUEST = new PouchError({
status: 400,
error: 'invalid_request',
reason: 'Request was invalid'
});
exports.QUERY_PARSE_ERROR = new PouchError({
status: 400,
error: 'query_parse_error',
reason: 'Some query parameter is invalid'
});
exports.DOC_VALIDATION = new PouchError({
status: 500,
error: 'doc_validation',
reason: 'Bad special document member'
});
exports.BAD_REQUEST = new PouchError({
status: 400,
error: 'bad_request',
reason: 'Something wrong with the request'
});
exports.NOT_AN_OBJECT = new PouchError({
status: 400,
error: 'bad_request',
reason: 'Document must be a JSON object'
});
exports.DB_MISSING = new PouchError({
status: 404,
error: 'not_found',
reason: 'Database not found'
});
exports.IDB_ERROR = new PouchError({
status: 500,
error: 'indexed_db_went_bad',
reason: 'unknown'
});
exports.WSQ_ERROR = new PouchError({
status: 500,
error: 'web_sql_went_bad',
reason: 'unknown'
});
exports.LDB_ERROR = new PouchError({
status: 500,
error: 'levelDB_went_went_bad',
reason: 'unknown'
});
exports.FORBIDDEN = new PouchError({
status: 403,
error: 'forbidden',
reason: 'Forbidden by design doc validate_doc_update function'
});
exports.INVALID_REV = new PouchError({
status: 400,
error: 'bad_request',
reason: 'Invalid rev format'
});
exports.FILE_EXISTS = new PouchError({
status: 412,
error: 'file_exists',
reason: 'The database could not be created, the file already exists.'
});
exports.MISSING_STUB = new PouchError({
status: 412,
error: 'missing_stub'
});
exports.error = function (error, reason, name) {
function CustomPouchError(reason) {
// inherit error properties from our parent error manually
// so as to allow proper JSON parsing.
/* jshint ignore:start */
for (var p in error) {
if (typeof error[p] !== 'function') {
this[p] = error[p];
}
}
/* jshint ignore:end */
if (name !== undefined) {
this.name = name;
}
if (reason !== undefined) {
this.reason = reason;
}
}
CustomPouchError.prototype = PouchError.prototype;
return new CustomPouchError(reason);
};
// Find one of the errors defined above based on the value
// of the specified property.
// If reason is provided prefer the error matching that reason.
// This is for differentiating between errors with the same name and status,
// eg, bad_request.
exports.getErrorTypeByProp = function (prop, value, reason) {
var errors = exports;
var keys = Object.keys(errors).filter(function (key) {
var error = errors[key];
return typeof error !== 'function' && error[prop] === value;
});
var key = reason && keys.filter(function (key) {
var error = errors[key];
return error.message === reason;
})[0] || keys[0];
return (key) ? errors[key] : null;
};
exports.generateErrorFromResponse = function (res) {
var error, errName, errType, errMsg, errReason;
var errors = exports;
errName = (res.error === true && typeof res.name === 'string') ?
res.name :
res.error;
errReason = res.reason;
errType = errors.getErrorTypeByProp('name', errName, errReason);
if (res.missing ||
errReason === 'missing' ||
errReason === 'deleted' ||
errName === 'not_found') {
errType = errors.MISSING_DOC;
} else if (errName === 'doc_validation') {
// doc validation needs special treatment since
// res.reason depends on the validation error.
// see utils.js
errType = errors.DOC_VALIDATION;
errMsg = errReason;
} else if (errName === 'bad_request' && errType.message !== errReason) {
// if bad_request error already found based on reason don't override.
// attachment errors.
if (errReason.indexOf('unknown stub attachment') === 0) {
errType = errors.MISSING_STUB;
errMsg = errReason;
} else {
errType = errors.BAD_REQUEST;
}
}
// fallback to error by statys or unknown error.
if (!errType) {
errType = errors.getErrorTypeByProp('status', res.status, errReason) ||
errors.UNKNOWN_ERROR;
}
error = errors.error(errType, errReason, errName);
// Keep custom message.
if (errMsg) {
error.message = errMsg;
}
// Keep helpful response data in our error messages.
if (res.id) {
error.id = res.id;
}
if (res.status) {
error.status = res.status;
}
if (res.statusText) {
error.name = res.statusText;
}
if (res.missing) {
error.missing = res.missing;
}
return error;
};
},{}],20:[function(_dereq_,module,exports){
(function (process,global){
'use strict';
var crypto = _dereq_(35);
var Md5 = _dereq_(96);
var setImmediateShim = global.setImmediate || global.setTimeout;
var MD5_CHUNK_SIZE = 32768;
function sliceShim(arrayBuffer, begin, end) {
if (typeof arrayBuffer.slice === 'function') {
if (!begin) {
return arrayBuffer.slice();
} else if (!end) {
return arrayBuffer.slice(begin);
} else {
return arrayBuffer.slice(begin, end);
}
}
//
// shim for IE courtesy of http://stackoverflow.com/a/21440217
//
//If `begin`/`end` is unspecified, Chrome assumes 0, so we do the same
//Chrome also converts the values to integers via flooring
begin = Math.floor(begin || 0);
end = Math.floor(end || 0);
var len = arrayBuffer.byteLength;
//If either `begin` or `end` is negative, it refers to an
//index from the end of the array, as opposed to from the beginning.
//The range specified by the `begin` and `end` values is clamped to the
//valid index range for the current array.
begin = begin < 0 ? Math.max(begin + len, 0) : Math.min(len, begin);
end = end < 0 ? Math.max(end + len, 0) : Math.min(len, end);
//If the computed length of the new ArrayBuffer would be negative, it
//is clamped to zero.
if (end - begin <= 0) {
return new ArrayBuffer(0);
}
var result = new ArrayBuffer(end - begin);
var resultBytes = new Uint8Array(result);
var sourceBytes = new Uint8Array(arrayBuffer, begin, end - begin);
resultBytes.set(sourceBytes);
return result;
}
// convert a 64-bit int to a binary string
function intToString(int) {
var bytes = [
(int & 0xff),
((int >>> 8) & 0xff),
((int >>> 16) & 0xff),
((int >>> 24) & 0xff)
];
return bytes.map(function (byte) {
return String.fromCharCode(byte);
}).join('');
}
// convert an array of 64-bit ints into
// a base64-encoded string
function rawToBase64(raw) {
var res = '';
for (var i = 0; i < raw.length; i++) {
res += intToString(raw[i]);
}
return btoa(res);
}
module.exports = function (data, callback) {
if (!process.browser) {
var base64 = crypto.createHash('md5').update(data).digest('base64');
callback(null, base64);
return;
}
var inputIsString = typeof data === 'string';
var len = inputIsString ? data.length : data.byteLength;
var chunkSize = Math.min(MD5_CHUNK_SIZE, len);
var chunks = Math.ceil(len / chunkSize);
var currentChunk = 0;
var buffer = inputIsString ? new Md5() : new Md5.ArrayBuffer();
function append(buffer, data, start, end) {
if (inputIsString) {
buffer.appendBinary(data.substring(start, end));
} else {
buffer.append(sliceShim(data, start, end));
}
}
function loadNextChunk() {
var start = currentChunk * chunkSize;
var end = start + chunkSize;
currentChunk++;
if (currentChunk < chunks) {
append(buffer, data, start, end);
setImmediateShim(loadNextChunk);
} else {
append(buffer, data, start, end);
var raw = buffer.end(true);
var base64 = rawToBase64(raw);
callback(null, base64);
buffer.destroy();
}
}
loadNextChunk();
};
}).call(this,_dereq_(37),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{}],21:[function(_dereq_,module,exports){
'use strict';
//
// Parsing hex strings. Yeah.
//
// So basically we need this because of a bug in WebSQL:
// https://code.google.com/p/chromium/issues/detail?id=422690
// https://bugs.webkit.org/show_bug.cgi?id=137637
//
// UTF-8 and UTF-16 are provided as separate functions
// for meager performance improvements
//
function decodeUtf8(str) {
return decodeURIComponent(window.escape(str));
}
function hexToInt(charCode) {
// '0'-'9' is 48-57
// 'A'-'F' is 65-70
// SQLite will only give us uppercase hex
return charCode < 65 ? (charCode - 48) : (charCode - 55);
}
// Example:
// pragma encoding=utf8;
// select hex('A');
// returns '41'
function parseHexUtf8(str, start, end) {
var result = '';
while (start < end) {
result += String.fromCharCode(
(hexToInt(str.charCodeAt(start++)) << 4) |
hexToInt(str.charCodeAt(start++)));
}
return result;
}
// Example:
// pragma encoding=utf16;
// select hex('A');
// returns '4100'
// notice that the 00 comes after the 41 (i.e. it's swizzled)
function parseHexUtf16(str, start, end) {
var result = '';
while (start < end) {
// UTF-16, so swizzle the bytes
result += String.fromCharCode(
(hexToInt(str.charCodeAt(start + 2)) << 12) |
(hexToInt(str.charCodeAt(start + 3)) << 8) |
(hexToInt(str.charCodeAt(start)) << 4) |
hexToInt(str.charCodeAt(start + 1)));
start += 4;
}
return result;
}
function parseHexString(str, encoding) {
if (encoding === 'UTF-8') {
return decodeUtf8(parseHexUtf8(str, 0, str.length));
} else {
return parseHexUtf16(str, 0, str.length);
}
}
module.exports = parseHexString;
},{}],22:[function(_dereq_,module,exports){
'use strict';
// originally parseUri 1.2.2, now patched by us
// (c) Steven Levithan
// MIT License
var 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*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
}
};
function parseUri(str) {
var o = options;
var m = o.parser[o.strictMode ? "strict" : "loose"].exec(str);
var uri = {};
var i = 14;
while (i--) {
var key = o.key[i];
var value = m[i] || "";
var encoded = ['user', 'password'].indexOf(key) !== -1;
uri[key] = encoded ? decodeURIComponent(value) : value;
}
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;
}
module.exports = parseUri;
},{}],23:[function(_dereq_,module,exports){
'use strict';
var upsert = _dereq_(95).upsert;
module.exports = function (db, doc, diffFun, cb) {
return upsert.call(db, doc, diffFun, cb);
};
},{}],24:[function(_dereq_,module,exports){
"use strict";
// BEGIN Math.uuid.js
/*!
Math.uuid.js (v1.4)
http://www.broofa.com
mailto:[email protected]
Copyright (c) 2010 Robert Kieffer
Dual licensed under the MIT and GPL licenses.
*/
/*
* Generate a random uuid.
*
* USAGE: Math.uuid(length, radix)
* length - the desired number of characters
* radix - the number of allowable values for each character.
*
* EXAMPLES:
* // No arguments - returns RFC4122, version 4 ID
* >>> Math.uuid()
* "92329D39-6F5C-4520-ABFC-AAB64544E172"
*
* // One argument - returns ID of the specified length
* >>> Math.uuid(15) // 15 character ID (default base=62)
* "VcydxgltxrVZSTV"
*
* // Two arguments - returns ID of the specified length, and radix.
* // (Radix must be <= 62)
* >>> Math.uuid(8, 2) // 8 character ID (base=2)
* "01001010"
* >>> Math.uuid(8, 10) // 8 character ID (base=10)
* "47473046"
* >>> Math.uuid(8, 16) // 8 character ID (base=16)
* "098F4D35"
*/
var chars = (
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' +
'abcdefghijklmnopqrstuvwxyz'
).split('');
function getValue(radix) {
return 0 | Math.random() * radix;
}
function uuid(len, radix) {
radix = radix || chars.length;
var out = '';
var i = -1;
if (len) {
// Compact form
while (++i < len) {
out += chars[getValue(radix)];
}
return out;
}
// rfc4122, version 4 form
// Fill in random data. At i==19 set the high bits of clock sequence as
// per rfc4122, sec. 4.1.5
while (++i < 36) {
switch (i) {
case 8:
case 13:
case 18:
case 23:
out += '-';
break;
case 19:
out += chars[(getValue(16) & 0x3) | 0x8];
break;
default:
out += chars[getValue(16)];
}
}
return out;
}
module.exports = uuid;
},{}],25:[function(_dereq_,module,exports){
'use strict';
module.exports = evalFilter;
function evalFilter(input) {
/*jshint evil: true */
return eval([
'(function () { return ',
input,
' })()'
].join(''));
}
},{}],26:[function(_dereq_,module,exports){
'use strict';
module.exports = evalView;
function evalView(input) {
/*jshint evil: true */
return eval([
'(function () {',
' return function (doc) {',
' var emitted = false;',
' var emit = function (a, b) {',
' emitted = true;',
' };',
' var view = ' + input + ';',
' view(doc);',
' if (emitted) {',
' return true;',
' }',
' }',
'})()'
].join('\n'));
}
},{}],27:[function(_dereq_,module,exports){
'use strict';
var extend = _dereq_(63);
// for a better overview of what this is doing, read:
// https://github.com/apache/couchdb/blob/master/src/couchdb/couch_key_tree.erl
//
// But for a quick intro, CouchDB uses a revision tree to store a documents
// history, A -> B -> C, when a document has conflicts, that is a branch in the
// tree, A -> (B1 | B2 -> C), We store these as a nested array in the format
//
// KeyTree = [Path ... ]
// Path = {pos: position_from_root, ids: Tree}
// Tree = [Key, Opts, [Tree, ...]], in particular single node: [Key, []]
// classic binary search
function binarySearch(arr, item, comparator) {
var low = 0;
var high = arr.length;
var mid;
while (low < high) {
mid = (low + high) >>> 1;
if (comparator(arr[mid], item) < 0) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
// assuming the arr is sorted, insert the item in the proper place
function insertSorted(arr, item, comparator) {
var idx = binarySearch(arr, item, comparator);
arr.splice(idx, 0, item);
}
// Turn a path as a flat array into a tree with a single branch
function pathToTree(path) {
var doc = path.shift();
var root = [doc.id, doc.opts, []];
var leaf = root;
var nleaf;
while (path.length) {
doc = path.shift();
nleaf = [doc.id, doc.opts, []];
leaf[2].push(nleaf);
leaf = nleaf;
}
return root;
}
// compare the IDs of two trees
function compareTree(a, b) {
return a[0] < b[0] ? -1 : 1;
}
// Merge two trees together
// The roots of tree1 and tree2 must be the same revision
function mergeTree(in_tree1, in_tree2) {
var queue = [{tree1: in_tree1, tree2: in_tree2}];
var conflicts = false;
while (queue.length > 0) {
var item = queue.pop();
var tree1 = item.tree1;
var tree2 = item.tree2;
if (tree1[1].status || tree2[1].status) {
tree1[1].status =
(tree1[1].status === 'available' ||
tree2[1].status === 'available') ? 'available' : 'missing';
}
for (var i = 0; i < tree2[2].length; i++) {
if (!tree1[2][0]) {
conflicts = 'new_leaf';
tree1[2][0] = tree2[2][i];
continue;
}
var merged = false;
for (var j = 0; j < tree1[2].length; j++) {
if (tree1[2][j][0] === tree2[2][i][0]) {
queue.push({tree1: tree1[2][j], tree2: tree2[2][i]});
merged = true;
}
}
if (!merged) {
conflicts = 'new_branch';
insertSorted(tree1[2], tree2[2][i], compareTree);
}
}
}
return {conflicts: conflicts, tree: in_tree1};
}
function doMerge(tree, path, dontExpand) {
var restree = [];
var conflicts = false;
var merged = false;
var res;
if (!tree.length) {
return {tree: [path], conflicts: 'new_leaf'};
}
tree.forEach(function (branch) {
if (branch.pos === path.pos && branch.ids[0] === path.ids[0]) {
// Paths start at the same position and have the same root, so they need
// merged
res = mergeTree(branch.ids, path.ids);
restree.push({pos: branch.pos, ids: res.tree});
conflicts = conflicts || res.conflicts;
merged = true;
} else if (dontExpand !== true) {
// The paths start at a different position, take the earliest path and
// traverse up until it as at the same point from root as the path we
// want to merge. If the keys match we return the longer path with the
// other merged After stemming we dont want to expand the trees
var t1 = branch.pos < path.pos ? branch : path;
var t2 = branch.pos < path.pos ? path : branch;
var diff = t2.pos - t1.pos;
var candidateParents = [];
var trees = [];
trees.push({ids: t1.ids, diff: diff, parent: null, parentIdx: null});
while (trees.length > 0) {
var item = trees.pop();
if (item.diff === 0) {
if (item.ids[0] === t2.ids[0]) {
candidateParents.push(item);
}
continue;
}
if (!item.ids) {
continue;
}
/*jshint loopfunc:true */
item.ids[2].forEach(function (el, idx) {
trees.push(
{ids: el, diff: item.diff - 1, parent: item.ids, parentIdx: idx});
});
}
var el = candidateParents[0];
if (!el) {
restree.push(branch);
} else {
res = mergeTree(el.ids, t2.ids);
el.parent[2][el.parentIdx] = res.tree;
restree.push({pos: t1.pos, ids: t1.ids});
conflicts = conflicts || res.conflicts;
merged = true;
}
} else {
restree.push(branch);
}
});
// We didnt find
if (!merged) {
restree.push(path);
}
restree.sort(function (a, b) {
return a.pos - b.pos;
});
return {
tree: restree,
conflicts: conflicts || 'internal_node'
};
}
// To ensure we dont grow the revision tree infinitely, we stem old revisions
function stem(tree, depth) {
// First we break out the tree into a complete list of root to leaf paths,
// we cut off the start of the path and generate a new set of flat trees
var stemmedPaths = PouchMerge.rootToLeaf(tree).map(function (path) {
var stemmed = path.ids.slice(-depth);
return {
pos: path.pos + (path.ids.length - stemmed.length),
ids: pathToTree(stemmed)
};
});
// Then we remerge all those flat trees together, ensuring that we dont
// connect trees that would go beyond the depth limit
return stemmedPaths.reduce(function (prev, current) {
return doMerge(prev, current, true).tree;
}, [stemmedPaths.shift()]);
}
var PouchMerge = {};
PouchMerge.merge = function (tree, path, depth) {
// Ugh, nicer way to not modify arguments in place?
tree = extend(true, [], tree);
path = extend(true, {}, path);
var newTree = doMerge(tree, path);
return {
tree: stem(newTree.tree, depth),
conflicts: newTree.conflicts
};
};
// We fetch all leafs of the revision tree, and sort them based on tree length
// and whether they were deleted, undeleted documents with the longest revision
// tree (most edits) win
// The final sort algorithm is slightly documented in a sidebar here:
// http://guide.couchdb.org/draft/conflicts.html
PouchMerge.winningRev = function (metadata) {
var leafs = [];
PouchMerge.traverseRevTree(metadata.rev_tree,
function (isLeaf, pos, id, something, opts) {
if (isLeaf) {
leafs.push({pos: pos, id: id, deleted: !!opts.deleted});
}
});
leafs.sort(function (a, b) {
if (a.deleted !== b.deleted) {
return a.deleted > b.deleted ? 1 : -1;
}
if (a.pos !== b.pos) {
return b.pos - a.pos;
}
return a.id < b.id ? 1 : -1;
});
return leafs[0].pos + '-' + leafs[0].id;
};
// Pretty much all below can be combined into a higher order function to
// traverse revisions
// The return value from the callback will be passed as context to all
// children of that node
PouchMerge.traverseRevTree = function (revs, callback) {
var toVisit = revs.slice();
var node;
while ((node = toVisit.pop())) {
var pos = node.pos;
var tree = node.ids;
var branches = tree[2];
var newCtx =
callback(branches.length === 0, pos, tree[0], node.ctx, tree[1]);
for (var i = 0, len = branches.length; i < len; i++) {
toVisit.push({pos: pos + 1, ids: branches[i], ctx: newCtx});
}
}
};
PouchMerge.collectLeaves = function (revs) {
var leaves = [];
PouchMerge.traverseRevTree(revs, function (isLeaf, pos, id, acc, opts) {
if (isLeaf) {
leaves.push({rev: pos + "-" + id, pos: pos, opts: opts});
}
});
leaves.sort(function (a, b) {
return b.pos - a.pos;
});
leaves.forEach(function (leaf) { delete leaf.pos; });
return leaves;
};
// returns revs of all conflicts that is leaves such that
// 1. are not deleted and
// 2. are different than winning revision
PouchMerge.collectConflicts = function (metadata) {
var win = PouchMerge.winningRev(metadata);
var leaves = PouchMerge.collectLeaves(metadata.rev_tree);
var conflicts = [];
leaves.forEach(function (leaf) {
if (leaf.rev !== win && !leaf.opts.deleted) {
conflicts.push(leaf.rev);
}
});
return conflicts;
};
PouchMerge.rootToLeaf = function (tree) {
var paths = [];
PouchMerge.traverseRevTree(tree, function (isLeaf, pos, id, history, opts) {
history = history ? history.slice(0) : [];
history.push({id: id, opts: opts});
if (isLeaf) {
var rootPos = pos + 1 - history.length;
paths.unshift({pos: rootPos, ids: history});
}
return history;
});
return paths;
};
module.exports = PouchMerge;
},{}],28:[function(_dereq_,module,exports){
'use strict';
var utils = _dereq_(32);
var EE = _dereq_(36).EventEmitter;
var Checkpointer = _dereq_(14);
var MAX_SIMULTANEOUS_REVS = 50;
var RETRY_DEFAULT = false;
function randomNumber(min, max) {
min = parseInt(min, 10);
max = parseInt(max, 10);
if (min !== min) {
min = 0;
}
if (max !== max || max <= min) {
max = (min || 1) << 1; //doubling
} else {
max = max + 1;
}
var ratio = Math.random();
var range = max - min;
return ~~(range * ratio + min); // ~~ coerces to an int, but fast.
}
function defaultBackOff(min) {
var max = 0;
if (!min) {
max = 2000;
}
return randomNumber(min, max);
}
function backOff(repId, src, target, opts, returnValue, result, error) {
if (opts.retry === false) {
returnValue.emit('error', error);
returnValue.removeAllListeners();
return;
}
opts.default_back_off = opts.default_back_off || 0;
opts.retries = opts.retries || 0;
if (typeof opts.back_off_function !== 'function') {
opts.back_off_function = defaultBackOff;
}
opts.retries++;
if (opts.max_retries && opts.retries > opts.max_retries) {
returnValue.emit('error', new Error('tried ' +
opts.retries + ' times but replication failed'));
returnValue.removeAllListeners();
return;
}
returnValue.emit('requestError', error);
if (returnValue.state === 'active') {
returnValue.emit('paused', error);
returnValue.state = 'stopped';
returnValue.once('active', function () {
opts.current_back_off = opts.default_back_off;
});
}
opts.current_back_off = opts.current_back_off || opts.default_back_off;
opts.current_back_off = opts.back_off_function(opts.current_back_off);
setTimeout(function () {
replicate(repId, src, target, opts, returnValue);
}, opts.current_back_off);
}
// We create a basic promise so the caller can cancel the replication possibly
// before we have actually started listening to changes etc
utils.inherits(Replication, EE);
function Replication() {
EE.call(this);
this.cancelled = false;
this.state = 'pending';
var self = this;
var promise = new utils.Promise(function (fulfill, reject) {
self.once('complete', fulfill);
self.once('error', reject);
});
self.then = function (resolve, reject) {
return promise.then(resolve, reject);
};
self["catch"] = function (reject) {
return promise["catch"](reject);
};
// As we allow error handling via "error" event as well,
// put a stub in here so that rejecting never throws UnhandledError.
self["catch"](function () {});
}
Replication.prototype.cancel = function () {
this.cancelled = true;
this.state = 'cancelled';
this.emit('cancel');
};
Replication.prototype.ready = function (src, target) {
var self = this;
this.once('change', function () {
if (this.state === 'pending' || this.state === 'stopped') {
self.state = 'active';
self.emit('active');
}
});
function onDestroy() {
self.cancel();
}
src.once('destroyed', onDestroy);
target.once('destroyed', onDestroy);
function cleanup() {
src.removeListener('destroyed', onDestroy);
target.removeListener('destroyed', onDestroy);
}
this.then(cleanup, cleanup);
};
// TODO: check CouchDB's replication id generation
// Generate a unique id particular to this replication
function genReplicationId(src, target, opts) {
var filterFun = opts.filter ? opts.filter.toString() : '';
return src.id().then(function (src_id) {
return target.id().then(function (target_id) {
var queryData = src_id + target_id + filterFun +
JSON.stringify(opts.query_params) + opts.doc_ids;
return utils.MD5(queryData).then(function (md5) {
// can't use straight-up md5 alphabet, because
// the char '/' is interpreted as being for attachments,
// and + is also not url-safe
md5 = md5.replace(/\//g, '.').replace(/\+/g, '_');
return '_local/' + md5;
});
});
});
}
function replicate(repId, src, target, opts, returnValue, result) {
var batches = []; // list of batches to be processed
var currentBatch; // the batch currently being processed
var pendingBatch = {
seq: 0,
changes: [],
docs: []
}; // next batch, not yet ready to be processed
var writingCheckpoint = false; // true while checkpoint is being written
var changesCompleted = false; // true when all changes received
var replicationCompleted = false; // true when replication has completed
var last_seq = 0;
var continuous = opts.continuous || opts.live || false;
var batch_size = opts.batch_size || 100;
var batches_limit = opts.batches_limit || 10;
var changesPending = false; // true while src.changes is running
var doc_ids = opts.doc_ids;
var state = {
cancelled: false
};
var checkpointer = new Checkpointer(src, target, repId, state);
var allErrors = [];
var changedDocs = [];
result = result || {
ok: true,
start_time: new Date(),
docs_read: 0,
docs_written: 0,
doc_write_failures: 0,
errors: []
};
var changesOpts = {};
returnValue.ready(src, target);
function writeDocs() {
if (currentBatch.docs.length === 0) {
return;
}
var docs = currentBatch.docs;
return target.bulkDocs({docs: docs, new_edits: false}).then(function (res) {
if (state.cancelled) {
completeReplication();
throw new Error('cancelled');
}
var errors = [];
var errorsById = {};
res.forEach(function (res) {
if (res.error) {
result.doc_write_failures++;
errors.push(res);
errorsById[res.id] = res;
}
});
result.errors = errors;
allErrors = allErrors.concat(errors);
result.docs_written += currentBatch.docs.length - errors.length;
var non403s = errors.filter(function (error) {
return error.name !== 'unauthorized' && error.name !== 'forbidden';
});
changedDocs = [];
docs.forEach(function(doc) {
var error = errorsById[doc._id];
if (error) {
returnValue.emit('denied', utils.clone(error));
} else {
changedDocs.push(doc);
}
});
if (non403s.length > 0) {
var error = new Error('bulkDocs error');
error.other_errors = errors;
abortReplication('target.bulkDocs failed to write docs', error);
throw new Error('bulkWrite partial failure');
}
}, function (err) {
result.doc_write_failures += docs.length;
throw err;
});
}
function processDiffDoc(id) {
var diffs = currentBatch.diffs;
var allMissing = diffs[id].missing;
// avoid url too long error by batching
var missingBatches = [];
for (var i = 0; i < allMissing.length; i += MAX_SIMULTANEOUS_REVS) {
missingBatches.push(allMissing.slice(i, Math.min(allMissing.length,
i + MAX_SIMULTANEOUS_REVS)));
}
return utils.Promise.all(missingBatches.map(function (missing) {
var opts = {
revs: true,
open_revs: missing,
attachments: true
};
return src.get(id, opts).then(function (docs) {
docs.forEach(function (doc) {
if (state.cancelled) {
return completeReplication();
}
if (doc.ok) {
result.docs_read++;
currentBatch.pendingRevs++;
currentBatch.docs.push(doc.ok);
}
});
delete diffs[id];
});
}));
}
function getAllDocs() {
var diffKeys = Object.keys(currentBatch.diffs);
return utils.Promise.all(diffKeys.map(processDiffDoc));
}
function getRevisionOneDocs() {
// filter out the generation 1 docs and get them
// leaving the non-generation one docs to be got otherwise
var ids = Object.keys(currentBatch.diffs).filter(function (id) {
var missing = currentBatch.diffs[id].missing;
return missing.length === 1 && missing[0].slice(0, 2) === '1-';
});
if (!ids.length) { // nothing to fetch
return utils.Promise.resolve();
}
return src.allDocs({
keys: ids,
include_docs: true
}).then(function (res) {
if (state.cancelled) {
completeReplication();
throw (new Error('cancelled'));
}
res.rows.forEach(function (row) {
if (row.doc && !row.deleted &&
row.value.rev.slice(0, 2) === '1-' && (
!row.doc._attachments ||
Object.keys(row.doc._attachments).length === 0
)
) {
result.docs_read++;
currentBatch.pendingRevs++;
currentBatch.docs.push(row.doc);
delete currentBatch.diffs[row.id];
}
});
});
}
function getDocs() {
return getRevisionOneDocs().then(getAllDocs);
}
function finishBatch() {
writingCheckpoint = true;
return checkpointer.writeCheckpoint(currentBatch.seq).then(function () {
writingCheckpoint = false;
if (state.cancelled) {
completeReplication();
throw new Error('cancelled');
}
result.last_seq = last_seq = currentBatch.seq;
var outResult = utils.clone(result);
outResult.docs = changedDocs;
returnValue.emit('change', outResult);
currentBatch = undefined;
getChanges();
})["catch"](function (err) {
writingCheckpoint = false;
abortReplication('writeCheckpoint completed with error', err);
throw err;
});
}
function getDiffs() {
var diff = {};
currentBatch.changes.forEach(function (change) {
diff[change.id] = change.changes.map(function (x) {
return x.rev;
});
});
return target.revsDiff(diff).then(function (diffs) {
if (state.cancelled) {
completeReplication();
throw new Error('cancelled');
}
// currentBatch.diffs elements are deleted as the documents are written
currentBatch.diffs = diffs;
currentBatch.pendingRevs = 0;
});
}
function startNextBatch() {
if (state.cancelled || currentBatch) {
return;
}
if (batches.length === 0) {
processPendingBatch(true);
return;
}
currentBatch = batches.shift();
getDiffs()
.then(getDocs)
.then(writeDocs)
.then(finishBatch)
.then(startNextBatch)[
"catch"](function (err) {
abortReplication('batch processing terminated with error', err);
});
}
function processPendingBatch(immediate) {
if (pendingBatch.changes.length === 0) {
if (batches.length === 0 && !currentBatch) {
if ((continuous && changesOpts.live) || changesCompleted) {
returnValue.emit('paused');
returnValue.emit('uptodate', result);
}
if (changesCompleted) {
completeReplication();
}
}
return;
}
if (
immediate ||
changesCompleted ||
pendingBatch.changes.length >= batch_size
) {
batches.push(pendingBatch);
pendingBatch = {
seq: 0,
changes: [],
docs: []
};
startNextBatch();
}
}
function abortReplication(reason, err) {
if (replicationCompleted) {
return;
}
if (!err.message) {
err.message = reason;
}
result.ok = false;
result.status = 'aborting';
result.errors.push(err);
allErrors = allErrors.concat(err);
batches = [];
pendingBatch = {
seq: 0,
changes: [],
docs: []
};
completeReplication();
}
function completeReplication() {
if (replicationCompleted) {
return;
}
if (state.cancelled) {
result.status = 'cancelled';
if (writingCheckpoint) {
return;
}
}
result.status = result.status || 'complete';
result.end_time = new Date();
result.last_seq = last_seq;
replicationCompleted = state.cancelled = true;
var non403s = allErrors.filter(function (error) {
return error.name !== 'unauthorized' && error.name !== 'forbidden';
});
if (non403s.length > 0) {
var error = allErrors.pop();
if (allErrors.length > 0) {
error.other_errors = allErrors;
}
error.result = result;
backOff(repId, src, target, opts, returnValue, result, error);
} else {
result.errors = allErrors;
returnValue.emit('complete', result);
returnValue.removeAllListeners();
}
}
function onChange(change) {
if (state.cancelled) {
return completeReplication();
}
var filter = utils.filterChange(opts)(change);
if (!filter) {
return;
}
if (
pendingBatch.changes.length === 0 &&
batches.length === 0 &&
!currentBatch
) {
returnValue.emit('outofdate', result);
}
pendingBatch.seq = change.seq;
pendingBatch.changes.push(change);
processPendingBatch(batches.length === 0);
}
function onChangesComplete(changes) {
changesPending = false;
if (state.cancelled) {
return completeReplication();
}
// if no results were returned then we're done,
// else fetch more
if (changes.results.length > 0) {
changesOpts.since = changes.last_seq;
getChanges();
} else {
if (continuous) {
changesOpts.live = true;
getChanges();
} else {
changesCompleted = true;
}
}
processPendingBatch(true);
}
function onChangesError(err) {
changesPending = false;
if (state.cancelled) {
return completeReplication();
}
abortReplication('changes rejected', err);
}
function getChanges() {
if (!(
!changesPending &&
!changesCompleted &&
batches.length < batches_limit
)) {
return;
}
changesPending = true;
function abortChanges() {
changes.cancel();
}
function removeListener() {
returnValue.removeListener('cancel', abortChanges);
}
returnValue.once('cancel', abortChanges);
var changes = src.changes(changesOpts)
.on('change', onChange);
changes.then(removeListener, removeListener);
changes.then(onChangesComplete)[
"catch"](onChangesError);
}
function startChanges() {
checkpointer.getCheckpoint().then(function (checkpoint) {
last_seq = checkpoint;
changesOpts = {
since: last_seq,
limit: batch_size,
batch_size: batch_size,
style: 'all_docs',
doc_ids: doc_ids,
returnDocs: true // required so we know when we're done
};
if (opts.filter) {
// required for the client-side filter in onChange
changesOpts.include_docs = true;
}
if (opts.query_params) {
changesOpts.query_params = opts.query_params;
}
getChanges();
})["catch"](function (err) {
abortReplication('getCheckpoint rejected with ', err);
});
}
returnValue.once('cancel', completeReplication);
if (typeof opts.onChange === 'function') {
returnValue.on('change', opts.onChange);
}
if (typeof opts.complete === 'function') {
returnValue.once('error', opts.complete);
returnValue.once('complete', function (result) {
opts.complete(null, result);
});
}
if (typeof opts.since === 'undefined') {
startChanges();
} else {
writingCheckpoint = true;
checkpointer.writeCheckpoint(opts.since).then(function () {
writingCheckpoint = false;
if (state.cancelled) {
completeReplication();
return;
}
last_seq = opts.since;
startChanges();
})["catch"](function (err) {
writingCheckpoint = false;
abortReplication('writeCheckpoint completed with error', err);
throw err;
});
}
}
exports.toPouch = toPouch;
function toPouch(db, opts) {
var PouchConstructor = opts.PouchConstructor;
if (typeof db === 'string') {
return new PouchConstructor(db);
} else if (db.then) {
return db;
} else {
return utils.Promise.resolve(db);
}
}
exports.replicate = replicateWrapper;
function replicateWrapper(src, target, opts, callback) {
if (typeof opts === 'function') {
callback = opts;
opts = {};
}
if (typeof opts === 'undefined') {
opts = {};
}
if (!opts.complete) {
opts.complete = callback || function () {};
}
opts = utils.clone(opts);
opts.continuous = opts.continuous || opts.live;
opts.retry = ('retry' in opts) ? opts.retry : RETRY_DEFAULT;
/*jshint validthis:true */
opts.PouchConstructor = opts.PouchConstructor || this;
var replicateRet = new Replication(opts);
toPouch(src, opts).then(function (src) {
return toPouch(target, opts).then(function (target) {
return genReplicationId(src, target, opts).then(function (repId) {
replicate(repId, src, target, opts, replicateRet);
});
});
})["catch"](function (err) {
replicateRet.emit('error', err);
opts.complete(err);
});
return replicateRet;
}
},{}],29:[function(_dereq_,module,exports){
"use strict";
var PouchDB = _dereq_(15);
var utils = _dereq_(32);
var Promise = utils.Promise;
var EventEmitter = _dereq_(36).EventEmitter;
PouchDB.adapters = {};
PouchDB.preferredAdapters = _dereq_(8);
PouchDB.prefix = '_pouch_';
var eventEmitter = new EventEmitter();
var eventEmitterMethods = [
'on',
'addListener',
'emit',
'listeners',
'once',
'removeAllListeners',
'removeListener',
'setMaxListeners'
];
eventEmitterMethods.forEach(function (method) {
PouchDB[method] = eventEmitter[method].bind(eventEmitter);
});
PouchDB.setMaxListeners(0);
PouchDB.parseAdapter = function (name, opts) {
var match = name.match(/([a-z\-]*):\/\/(.*)/);
var adapter, adapterName;
if (match) {
// the http adapter expects the fully qualified name
name = /http(s?)/.test(match[1]) ? match[1] + '://' + match[2] : match[2];
adapter = match[1];
if (!PouchDB.adapters[adapter].valid()) {
throw 'Invalid adapter';
}
return {name: name, adapter: match[1]};
}
// check for browsers that have been upgraded from websql-only to websql+idb
var skipIdb = 'idb' in PouchDB.adapters && 'websql' in PouchDB.adapters &&
utils.hasLocalStorage() &&
localStorage['_pouch__websqldb_' + PouchDB.prefix + name];
if (typeof opts !== 'undefined' && opts.db) {
adapterName = 'leveldb';
} else {
for (var i = 0; i < PouchDB.preferredAdapters.length; ++i) {
adapterName = PouchDB.preferredAdapters[i];
if (adapterName in PouchDB.adapters) {
if (skipIdb && adapterName === 'idb') {
continue; // keep using websql to avoid user data loss
}
break;
}
}
}
adapter = PouchDB.adapters[adapterName];
if (adapterName && adapter) {
var use_prefix = 'use_prefix' in adapter ? adapter.use_prefix : true;
return {
name: use_prefix ? PouchDB.prefix + name : name,
adapter: adapterName
};
}
throw 'No valid adapter found';
};
PouchDB.destroy = utils.toPromise(function (name, opts, callback) {
if (typeof opts === 'function' || typeof opts === 'undefined') {
callback = opts;
opts = {};
}
if (name && typeof name === 'object') {
opts = name;
name = undefined;
}
if (!opts.internal) {
console.log('PouchDB.destroy() is deprecated and will be removed. ' +
'Please use db.destroy() instead.');
}
var backend = PouchDB.parseAdapter(opts.name || name, opts);
var dbName = backend.name;
var adapter = PouchDB.adapters[backend.adapter];
var usePrefix = 'use_prefix' in adapter ? adapter.use_prefix : true;
var baseName = usePrefix ?
dbName.replace(new RegExp('^' + PouchDB.prefix), '') : dbName;
var fullName = (backend.adapter === 'http' || backend.adapter === 'https' ?
'' : (opts.prefix || '')) + dbName;
function destroyDb() {
// call destroy method of the particular adaptor
adapter.destroy(fullName, opts, function (err, resp) {
if (err) {
callback(err);
} else {
PouchDB.emit('destroyed', name);
//so we don't have to sift through all dbnames
PouchDB.emit(name, 'destroyed');
callback(null, resp || { 'ok': true });
}
});
}
var createOpts = utils.extend(true, {}, opts, {adapter : backend.adapter});
new PouchDB(baseName, createOpts, function (err, db) {
if (err) {
return callback(err);
}
db.get('_local/_pouch_dependentDbs', function (err, localDoc) {
if (err) {
if (err.status !== 404) {
return callback(err);
} else { // no dependencies
return destroyDb();
}
}
var dependentDbs = localDoc.dependentDbs;
var deletedMap = Object.keys(dependentDbs).map(function (name) {
var trueName = usePrefix ?
name.replace(new RegExp('^' + PouchDB.prefix), '') : name;
var subOpts = utils.extend(true, opts, db.__opts || {});
return db.constructor.destroy(trueName, subOpts);
});
Promise.all(deletedMap).then(destroyDb, function (error) {
callback(error);
});
});
});
});
PouchDB.adapter = function (id, obj) {
if (obj.valid()) {
PouchDB.adapters[id] = obj;
}
};
PouchDB.plugin = function (obj) {
Object.keys(obj).forEach(function (id) {
PouchDB.prototype[id] = obj[id];
});
};
PouchDB.defaults = function (defaultOpts) {
function PouchAlt(name, opts, callback) {
if (typeof opts === 'function' || typeof opts === 'undefined') {
callback = opts;
opts = {};
}
if (name && typeof name === 'object') {
opts = name;
name = undefined;
}
opts = utils.extend(true, {}, defaultOpts, opts);
PouchDB.call(this, name, opts, callback);
}
utils.inherits(PouchAlt, PouchDB);
PouchAlt.destroy = utils.toPromise(function (name, opts, callback) {
if (typeof opts === 'function' || typeof opts === 'undefined') {
callback = opts;
opts = {};
}
if (name && typeof name === 'object') {
opts = name;
name = undefined;
}
opts = utils.extend(true, {}, defaultOpts, opts);
return PouchDB.destroy(name, opts, callback);
});
eventEmitterMethods.forEach(function (method) {
PouchAlt[method] = eventEmitter[method].bind(eventEmitter);
});
PouchAlt.setMaxListeners(0);
PouchAlt.preferredAdapters = PouchDB.preferredAdapters.slice();
Object.keys(PouchDB).forEach(function (key) {
if (!(key in PouchAlt)) {
PouchAlt[key] = PouchDB[key];
}
});
return PouchAlt;
};
module.exports = PouchDB;
},{}],30:[function(_dereq_,module,exports){
'use strict';
var utils = _dereq_(32);
var replication = _dereq_(28);
var replicate = replication.replicate;
var EE = _dereq_(36).EventEmitter;
utils.inherits(Sync, EE);
module.exports = sync;
function sync(src, target, opts, callback) {
if (typeof opts === 'function') {
callback = opts;
opts = {};
}
if (typeof opts === 'undefined') {
opts = {};
}
opts = utils.clone(opts);
/*jshint validthis:true */
opts.PouchConstructor = opts.PouchConstructor || this;
src = replication.toPouch(src, opts);
target = replication.toPouch(target, opts);
return new Sync(src, target, opts, callback);
}
function Sync(src, target, opts, callback) {
var self = this;
this.canceled = false;
var onChange, complete;
if ('onChange' in opts) {
onChange = opts.onChange;
delete opts.onChange;
}
if (typeof callback === 'function' && !opts.complete) {
complete = callback;
} else if ('complete' in opts) {
complete = opts.complete;
delete opts.complete;
}
this.push = replicate(src, target, opts);
this.pull = replicate(target, src, opts);
var emittedCancel = false;
function onCancel(data) {
if (!emittedCancel) {
emittedCancel = true;
self.emit('cancel', data);
}
}
function pullChange(change) {
self.emit('change', {
direction: 'pull',
change: change
});
}
function pushChange(change) {
self.emit('change', {
direction: 'push',
change: change
});
}
function pushDenied(doc) {
self.emit('denied', {
direction: 'push',
doc: doc
});
}
function pullDenied(doc) {
self.emit('denied', {
direction: 'pull',
doc: doc
});
}
var listeners = {};
var removed = {};
function removeAll(type) { // type is 'push' or 'pull'
return function (event, func) {
var isChange = event === 'change' &&
(func === pullChange || func === pushChange);
var isCancel = event === 'cancel' && func === onCancel;
var isOtherEvent = event in listeners && func === listeners[event];
if (isChange || isCancel || isOtherEvent) {
if (!(event in removed)) {
removed[event] = {};
}
removed[event][type] = true;
if (Object.keys(removed[event]).length === 2) {
// both push and pull have asked to be removed
self.removeAllListeners(event);
}
}
};
}
if (opts.live) {
this.push.on('complete', self.pull.cancel.bind(self.pull));
this.pull.on('complete', self.push.cancel.bind(self.push));
}
this.on('newListener', function (event) {
if (event === 'change') {
self.pull.on('change', pullChange);
self.push.on('change', pushChange);
} else if (event === 'denied') {
self.pull.on('denied', pullDenied);
self.push.on('denied', pushDenied);
} else if (event === 'cancel') {
self.pull.on('cancel', onCancel);
self.push.on('cancel', onCancel);
} else if (event !== 'error' &&
event !== 'removeListener' &&
event !== 'complete' && !(event in listeners)) {
listeners[event] = function (e) {
self.emit(event, e);
};
self.pull.on(event, listeners[event]);
self.push.on(event, listeners[event]);
}
});
this.on('removeListener', function (event) {
if (event === 'change') {
self.pull.removeListener('change', pullChange);
self.push.removeListener('change', pushChange);
} else if (event === 'cancel') {
self.pull.removeListener('cancel', onCancel);
self.push.removeListener('cancel', onCancel);
} else if (event in listeners) {
if (typeof listeners[event] === 'function') {
self.pull.removeListener(event, listeners[event]);
self.push.removeListener(event, listeners[event]);
delete listeners[event];
}
}
});
this.pull.on('removeListener', removeAll('pull'));
this.push.on('removeListener', removeAll('push'));
var promise = utils.Promise.all([
this.push,
this.pull
]).then(function (resp) {
var out = {
push: resp[0],
pull: resp[1]
};
self.emit('complete', out);
if (complete) {
complete(null, out);
}
self.removeAllListeners();
return out;
}, function (err) {
self.cancel();
self.emit('error', err);
if (complete) {
complete(err);
}
self.removeAllListeners();
throw err;
});
this.then = function (success, err) {
return promise.then(success, err);
};
this["catch"] = function (err) {
return promise["catch"](err);
};
}
Sync.prototype.cancel = function () {
if (!this.canceled) {
this.canceled = true;
this.push.cancel();
this.pull.cancel();
}
};
},{}],31:[function(_dereq_,module,exports){
'use strict';
module.exports = TaskQueue;
function TaskQueue() {
this.isReady = false;
this.failed = false;
this.queue = [];
}
TaskQueue.prototype.execute = function () {
var d, func;
if (this.failed) {
while ((d = this.queue.shift())) {
if (typeof d === 'function') {
d(this.failed);
continue;
}
func = d.parameters[d.parameters.length - 1];
if (typeof func === 'function') {
func(this.failed);
} else if (d.name === 'changes' && typeof func.complete === 'function') {
func.complete(this.failed);
}
}
} else if (this.isReady) {
while ((d = this.queue.shift())) {
if (typeof d === 'function') {
d();
} else {
d.task = this.db[d.name].apply(this.db, d.parameters);
}
}
}
};
TaskQueue.prototype.fail = function (err) {
this.failed = err;
this.execute();
};
TaskQueue.prototype.ready = function (db) {
if (this.failed) {
return false;
} else if (arguments.length === 0) {
return this.isReady;
}
this.isReady = db ? true: false;
this.db = db;
this.execute();
};
TaskQueue.prototype.addTask = function (name, parameters) {
if (typeof name === 'function') {
this.queue.push(name);
if (this.failed) {
this.execute();
}
} else {
var task = { name: name, parameters: parameters };
this.queue.push(task);
if (this.failed) {
this.execute();
}
return task;
}
};
},{}],32:[function(_dereq_,module,exports){
(function (process,global){
/*jshint strict: false */
/*global chrome */
var merge = _dereq_(27);
exports.extend = _dereq_(63);
exports.ajax = _dereq_(16);
exports.createBlob = _dereq_(17);
exports.uuid = _dereq_(24);
exports.getArguments = _dereq_(34);
var buffer = _dereq_(18);
var errors = _dereq_(19);
var EventEmitter = _dereq_(36).EventEmitter;
var collections = _dereq_(62);
exports.Map = collections.Map;
exports.Set = collections.Set;
if (typeof global.Promise === 'function') {
exports.Promise = global.Promise;
} else {
exports.Promise = _dereq_(45);
}
var Promise = exports.Promise;
function toObject(array) {
return array.reduce(function (obj, item) {
obj[item] = true;
return obj;
}, {});
}
// List of top level reserved words for doc
var reservedWords = toObject([
'_id',
'_rev',
'_attachments',
'_deleted',
'_revisions',
'_revs_info',
'_conflicts',
'_deleted_conflicts',
'_local_seq',
'_rev_tree',
//replication documents
'_replication_id',
'_replication_state',
'_replication_state_time',
'_replication_state_reason',
'_replication_stats',
// Specific to Couchbase Sync Gateway
'_removed'
]);
// List of reserved words that should end up the document
var dataWords = toObject([
'_attachments',
//replication documents
'_replication_id',
'_replication_state',
'_replication_state_time',
'_replication_state_reason',
'_replication_stats'
]);
exports.lastIndexOf = function (str, char) {
for (var i = str.length - 1; i >= 0; i--) {
if (str.charAt(i) === char) {
return i;
}
}
return -1;
};
exports.clone = function (obj) {
return exports.extend(true, {}, obj);
};
// like underscore/lodash _.pick()
exports.pick = function (obj, arr) {
var res = {};
for (var i = 0, len = arr.length; i < len; i++) {
var prop = arr[i];
res[prop] = obj[prop];
}
return res;
};
exports.inherits = _dereq_(41);
// Determine id an ID is valid
// - invalid IDs begin with an underescore that does not begin '_design' or
// '_local'
// - any other string value is a valid id
// Returns the specific error object for each case
exports.invalidIdError = function (id) {
var err;
if (!id) {
err = errors.error(errors.MISSING_ID);
} else if (typeof id !== 'string') {
err = errors.error(errors.INVALID_ID);
} else if (/^_/.test(id) && !(/^_(design|local)/).test(id)) {
err = errors.error(errors.RESERVED_ID);
}
if (err) {
throw err;
}
};
function isChromeApp() {
return (typeof chrome !== "undefined" &&
typeof chrome.storage !== "undefined" &&
typeof chrome.storage.local !== "undefined");
}
// Pretty dumb name for a function, just wraps callback calls so we dont
// to if (callback) callback() everywhere
exports.call = exports.getArguments(function (args) {
if (!args.length) {
return;
}
var fun = args.shift();
if (typeof fun === 'function') {
fun.apply(this, args);
}
});
exports.isLocalId = function (id) {
return (/^_local/).test(id);
};
// check if a specific revision of a doc has been deleted
// - metadata: the metadata object from the doc store
// - rev: (optional) the revision to check. defaults to winning revision
exports.isDeleted = function (metadata, rev) {
if (!rev) {
rev = merge.winningRev(metadata);
}
var dashIndex = rev.indexOf('-');
if (dashIndex !== -1) {
rev = rev.substring(dashIndex + 1);
}
var deleted = false;
merge.traverseRevTree(metadata.rev_tree,
function (isLeaf, pos, id, acc, opts) {
if (id === rev) {
deleted = !!opts.deleted;
}
});
return deleted;
};
exports.revExists = function (metadata, rev) {
var found = false;
merge.traverseRevTree(metadata.rev_tree, function (leaf, pos, id) {
if ((pos + '-' + id) === rev) {
found = true;
}
});
return found;
};
exports.filterChange = function filterChange(opts) {
var req = {};
var hasFilter = opts.filter && typeof opts.filter === 'function';
req.query = opts.query_params;
return function filter(change) {
if (opts.filter && hasFilter && !opts.filter.call(this, change.doc, req)) {
return false;
}
if (!opts.include_docs) {
delete change.doc;
} else if (!opts.attachments) {
for (var att in change.doc._attachments) {
if (change.doc._attachments.hasOwnProperty(att)) {
change.doc._attachments[att].stub = true;
}
}
}
return true;
};
};
// Preprocess documents, parse their revisions, assign an id and a
// revision for new writes that are missing them, etc
exports.parseDoc = function (doc, newEdits) {
var nRevNum;
var newRevId;
var revInfo;
var error;
var opts = {status: 'available'};
if (doc._deleted) {
opts.deleted = true;
}
function parseRevisionInfo() {
revInfo = /^(\d+)-(.+)$/.exec(doc._rev);
if (!revInfo) {
error = errors.error(errors.INVALID_REV);
return error;
}
return {
prefix: parseInt(revInfo[1], 10),
id: revInfo[2]
};
}
if (newEdits) {
if (!doc._id) {
doc._id = exports.uuid();
}
newRevId = exports.uuid(32, 16).toLowerCase();
if (doc._rev) {
revInfo = parseRevisionInfo(doc._rev);
if (revInfo.error) {
return revInfo;
}
doc._rev_tree = [{
pos: revInfo.prefix,
ids: [revInfo.id, {status: 'missing'}, [[newRevId, opts, []]]]
}];
nRevNum = revInfo.prefix + 1;
} else {
doc._rev_tree = [{
pos: 1,
ids : [newRevId, opts, []]
}];
nRevNum = 1;
}
} else {
if (doc._revisions) {
doc._rev_tree = [{
pos: doc._revisions.start - doc._revisions.ids.length + 1,
ids: doc._revisions.ids.reduce(function (acc, x) {
if (acc === null) {
return [x, opts, []];
} else {
return [x, {status: 'missing'}, [acc]];
}
}, null)
}];
nRevNum = doc._revisions.start;
newRevId = doc._revisions.ids[0];
}
if (!doc._rev_tree) {
revInfo = parseRevisionInfo(doc._rev);
if (revInfo.error) {
return revInfo;
}
nRevNum = revInfo.prefix;
newRevId = revInfo.id;
doc._rev_tree = [{
pos: nRevNum,
ids: [newRevId, opts, []]
}];
}
}
exports.invalidIdError(doc._id);
doc._rev = [nRevNum, newRevId].join('-');
var result = {metadata : {}, data : {}};
for (var key in doc) {
if (doc.hasOwnProperty(key)) {
var specialKey = key[0] === '_';
if (specialKey && !reservedWords[key]) {
error = errors.error(errors.DOC_VALIDATION, key);
error.message = errors.DOC_VALIDATION.message + ': ' + key;
throw error;
} else if (specialKey && !dataWords[key]) {
result.metadata[key.slice(1)] = doc[key];
} else {
result.data[key] = doc[key];
}
}
}
return result;
};
exports.isCordova = function () {
return (typeof cordova !== "undefined" ||
typeof PhoneGap !== "undefined" ||
typeof phonegap !== "undefined");
};
exports.hasLocalStorage = function () {
if (isChromeApp()) {
return false;
}
try {
return localStorage;
} catch (e) {
return false;
}
};
exports.Changes = Changes;
exports.inherits(Changes, EventEmitter);
function Changes() {
if (!(this instanceof Changes)) {
return new Changes();
}
var self = this;
EventEmitter.call(this);
this.isChrome = isChromeApp();
this.listeners = {};
this.hasLocal = false;
if (!this.isChrome) {
this.hasLocal = exports.hasLocalStorage();
}
if (this.isChrome) {
chrome.storage.onChanged.addListener(function (e) {
// make sure it's event addressed to us
if (e.db_name != null) {
//object only has oldValue, newValue members
self.emit(e.dbName.newValue);
}
});
} else if (this.hasLocal) {
if (typeof addEventListener !== 'undefined') {
addEventListener("storage", function (e) {
self.emit(e.key);
});
} else { // old IE
window.attachEvent("storage", function (e) {
self.emit(e.key);
});
}
}
}
Changes.prototype.addListener = function (dbName, id, db, opts) {
if (this.listeners[id]) {
return;
}
var self = this;
var inprogress = false;
function eventFunction() {
if (!self.listeners[id]) {
return;
}
if (inprogress) {
inprogress = 'waiting';
return;
}
inprogress = true;
db.changes({
include_docs: opts.include_docs,
attachments: opts.attachments,
conflicts: opts.conflicts,
continuous: false,
descending: false,
filter: opts.filter,
doc_ids: opts.doc_ids,
view: opts.view,
since: opts.since,
query_params: opts.query_params
}).on('change', function (c) {
if (c.seq > opts.since && !opts.cancelled) {
opts.since = c.seq;
exports.call(opts.onChange, c);
}
}).on('complete', function () {
if (inprogress === 'waiting') {
process.nextTick(function () {
self.notify(dbName);
});
}
inprogress = false;
}).on('error', function () {
inprogress = false;
});
}
this.listeners[id] = eventFunction;
this.on(dbName, eventFunction);
};
Changes.prototype.removeListener = function (dbName, id) {
if (!(id in this.listeners)) {
return;
}
EventEmitter.prototype.removeListener.call(this, dbName,
this.listeners[id]);
};
Changes.prototype.notifyLocalWindows = function (dbName) {
//do a useless change on a storage thing
//in order to get other windows's listeners to activate
if (this.isChrome) {
chrome.storage.local.set({dbName: dbName});
} else if (this.hasLocal) {
localStorage[dbName] = (localStorage[dbName] === "a") ? "b" : "a";
}
};
Changes.prototype.notify = function (dbName) {
this.emit(dbName);
this.notifyLocalWindows(dbName);
};
if (typeof window === 'undefined' || typeof window.atob !== 'function') {
exports.atob = function (str) {
var base64 = new buffer(str, 'base64');
// Node.js will just skip the characters it can't encode instead of
// throwing and exception
if (base64.toString('base64') !== str) {
throw ("Cannot base64 encode full string");
}
return base64.toString('binary');
};
} else {
exports.atob = function (str) {
return atob(str);
};
}
if (typeof window === 'undefined' || typeof window.btoa !== 'function') {
exports.btoa = function (str) {
return new buffer(str, 'binary').toString('base64');
};
} else {
exports.btoa = function (str) {
return btoa(str);
};
}
// From http://stackoverflow.com/questions/14967647/ (continues on next line)
// encode-decode-image-with-base64-breaks-image (2013-04-21)
exports.fixBinary = function (bin) {
if (!process.browser) {
// don't need to do this in Node
return bin;
}
var length = bin.length;
var buf = new ArrayBuffer(length);
var arr = new Uint8Array(buf);
for (var i = 0; i < length; i++) {
arr[i] = bin.charCodeAt(i);
}
return buf;
};
// shim for browsers that don't support it
exports.readAsBinaryString = function (blob, callback) {
var reader = new FileReader();
var hasBinaryString = typeof reader.readAsBinaryString === 'function';
reader.onloadend = function (e) {
var result = e.target.result || '';
if (hasBinaryString) {
return callback(result);
}
callback(exports.arrayBufferToBinaryString(result));
};
if (hasBinaryString) {
reader.readAsBinaryString(blob);
} else {
reader.readAsArrayBuffer(blob);
}
};
exports.once = function (fun) {
var called = false;
return exports.getArguments(function (args) {
if (called) {
throw new Error('once called more than once');
} else {
called = true;
fun.apply(this, args);
}
});
};
exports.toPromise = function (func) {
//create the function we will be returning
return exports.getArguments(function (args) {
var self = this;
var tempCB =
(typeof args[args.length - 1] === 'function') ? args.pop() : false;
// if the last argument is a function, assume its a callback
var usedCB;
if (tempCB) {
// if it was a callback, create a new callback which calls it,
// but do so async so we don't trap any errors
usedCB = function (err, resp) {
process.nextTick(function () {
tempCB(err, resp);
});
};
}
var promise = new Promise(function (fulfill, reject) {
var resp;
try {
var callback = exports.once(function (err, mesg) {
if (err) {
reject(err);
} else {
fulfill(mesg);
}
});
// create a callback for this invocation
// apply the function in the orig context
args.push(callback);
resp = func.apply(self, args);
if (resp && typeof resp.then === 'function') {
fulfill(resp);
}
} catch (e) {
reject(e);
}
});
// if there is a callback, call it back
if (usedCB) {
promise.then(function (result) {
usedCB(null, result);
}, usedCB);
}
promise.cancel = function () {
return this;
};
return promise;
});
};
exports.adapterFun = function (name, callback) {
var log = _dereq_(38)('pouchdb:api');
function logApiCall(self, name, args) {
if (!log.enabled) {
return;
}
var logArgs = [self._db_name, name];
for (var i = 0; i < args.length - 1; i++) {
logArgs.push(args[i]);
}
log.apply(null, logArgs);
// override the callback itself to log the response
var origCallback = args[args.length - 1];
args[args.length - 1] = function (err, res) {
var responseArgs = [self._db_name, name];
responseArgs = responseArgs.concat(
err ? ['error', err] : ['success', res]
);
log.apply(null, responseArgs);
origCallback(err, res);
};
}
return exports.toPromise(exports.getArguments(function (args) {
if (this._closed) {
return Promise.reject(new Error('database is closed'));
}
var self = this;
logApiCall(self, name, args);
if (!this.taskqueue.isReady) {
return new exports.Promise(function (fulfill, reject) {
self.taskqueue.addTask(function (failed) {
if (failed) {
reject(failed);
} else {
fulfill(self[name].apply(self, args));
}
});
});
}
return callback.apply(this, args);
}));
};
//Can't find original post, but this is close
//http://stackoverflow.com/questions/6965107/ (continues on next line)
//converting-between-strings-and-arraybuffers
exports.arrayBufferToBinaryString = function (buffer) {
var binary = "";
var bytes = new Uint8Array(buffer);
var length = bytes.byteLength;
for (var i = 0; i < length; i++) {
binary += String.fromCharCode(bytes[i]);
}
return binary;
};
exports.cancellableFun = function (fun, self, opts) {
opts = opts ? exports.clone(true, {}, opts) : {};
var emitter = new EventEmitter();
var oldComplete = opts.complete || function () { };
var complete = opts.complete = exports.once(function (err, resp) {
if (err) {
oldComplete(err);
} else {
emitter.emit('end', resp);
oldComplete(null, resp);
}
emitter.removeAllListeners();
});
var oldOnChange = opts.onChange || function () {};
var lastChange = 0;
self.on('destroyed', function () {
emitter.removeAllListeners();
});
opts.onChange = function (change) {
oldOnChange(change);
if (change.seq <= lastChange) {
return;
}
lastChange = change.seq;
emitter.emit('change', change);
if (change.deleted) {
emitter.emit('delete', change);
} else if (change.changes.length === 1 &&
change.changes[0].rev.slice(0, 1) === '1-') {
emitter.emit('create', change);
} else {
emitter.emit('update', change);
}
};
var promise = new Promise(function (fulfill, reject) {
opts.complete = function (err, res) {
if (err) {
reject(err);
} else {
fulfill(res);
}
};
});
promise.then(function (result) {
complete(null, result);
}, complete);
// this needs to be overwridden by caller, dont fire complete until
// the task is ready
promise.cancel = function () {
promise.isCancelled = true;
if (self.taskqueue.isReady) {
opts.complete(null, {status: 'cancelled'});
}
};
if (!self.taskqueue.isReady) {
self.taskqueue.addTask(function () {
if (promise.isCancelled) {
opts.complete(null, {status: 'cancelled'});
} else {
fun(self, opts, promise);
}
});
} else {
fun(self, opts, promise);
}
promise.on = emitter.on.bind(emitter);
promise.once = emitter.once.bind(emitter);
promise.addListener = emitter.addListener.bind(emitter);
promise.removeListener = emitter.removeListener.bind(emitter);
promise.removeAllListeners = emitter.removeAllListeners.bind(emitter);
promise.setMaxListeners = emitter.setMaxListeners.bind(emitter);
promise.listeners = emitter.listeners.bind(emitter);
promise.emit = emitter.emit.bind(emitter);
return promise;
};
exports.MD5 = exports.toPromise(_dereq_(20));
// designed to give info to browser users, who are disturbed
// when they see 404s in the console
exports.explain404 = function (str) {
if (process.browser && 'console' in global && 'info' in console) {
console.info('The above 404 is totally normal. ' + str);
}
};
exports.info = function (str) {
if (typeof console !== 'undefined' && 'info' in console) {
console.info(str);
}
};
exports.parseUri = _dereq_(22);
exports.compare = function (left, right) {
return left < right ? -1 : left > right ? 1 : 0;
};
exports.updateDoc = function updateDoc(prev, docInfo, results,
i, cb, writeDoc, newEdits) {
if (exports.revExists(prev, docInfo.metadata.rev)) {
results[i] = docInfo;
return cb();
}
var previouslyDeleted = exports.isDeleted(prev);
var deleted = exports.isDeleted(docInfo.metadata);
var isRoot = /^1-/.test(docInfo.metadata.rev);
if (previouslyDeleted && !deleted && newEdits && isRoot) {
var newDoc = docInfo.data;
newDoc._rev = merge.winningRev(prev);
newDoc._id = docInfo.metadata.id;
docInfo = exports.parseDoc(newDoc, newEdits);
}
var merged = merge.merge(prev.rev_tree, docInfo.metadata.rev_tree[0], 1000);
var inConflict = newEdits && (((previouslyDeleted && deleted) ||
(!previouslyDeleted && merged.conflicts !== 'new_leaf') ||
(previouslyDeleted && !deleted && merged.conflicts === 'new_branch')));
if (inConflict) {
var err = errors.error(errors.REV_CONFLICT);
results[i] = err;
return cb();
}
var newRev = docInfo.metadata.rev;
docInfo.metadata.rev_tree = merged.tree;
if (prev.rev_map) {
docInfo.metadata.rev_map = prev.rev_map; // used by leveldb
}
// recalculate
var winningRev = merge.winningRev(docInfo.metadata);
deleted = exports.isDeleted(docInfo.metadata, winningRev);
var delta = 0;
if (newEdits || winningRev === newRev) {
// if newEdits==false and we're pushing existing revisions,
// then the only thing that matters is whether this revision
// is the winning one, and thus replaces an old one
delta = (previouslyDeleted === deleted) ? 0 :
previouslyDeleted < deleted ? -1 : 1;
}
writeDoc(docInfo, winningRev, deleted, cb, true, delta, i);
};
exports.processDocs = function processDocs(docInfos, api, fetchedDocs,
tx, results, writeDoc, opts,
overallCallback) {
if (!docInfos.length) {
return;
}
function insertDoc(docInfo, resultsIdx, callback) {
// Cant insert new deleted documents
var winningRev = merge.winningRev(docInfo.metadata);
var deleted = exports.isDeleted(docInfo.metadata, winningRev);
if ('was_delete' in opts && deleted) {
results[resultsIdx] = errors.error(errors.MISSING_DOC, 'deleted');
return callback();
}
var delta = deleted ? 0 : 1;
writeDoc(docInfo, winningRev, deleted, callback, false, delta, resultsIdx);
}
var newEdits = opts.new_edits;
var idsToDocs = new exports.Map();
var docsDone = 0;
var docsToDo = docInfos.length;
function checkAllDocsDone() {
if (++docsDone === docsToDo && overallCallback) {
overallCallback();
}
}
docInfos.forEach(function (currentDoc, resultsIdx) {
if (currentDoc._id && exports.isLocalId(currentDoc._id)) {
api[currentDoc._deleted ? '_removeLocal' : '_putLocal'](
currentDoc, {ctx: tx}, function (err) {
if (err) {
results[resultsIdx] = err;
} else {
results[resultsIdx] = {ok: true};
}
checkAllDocsDone();
});
return;
}
var id = currentDoc.metadata.id;
if (idsToDocs.has(id)) {
docsToDo--; // duplicate
idsToDocs.get(id).push([currentDoc, resultsIdx]);
} else {
idsToDocs.set(id, [[currentDoc, resultsIdx]]);
}
});
// in the case of new_edits, the user can provide multiple docs
// with the same id. these need to be processed sequentially
idsToDocs.forEach(function (docs, id) {
var numDone = 0;
function docWritten() {
if (++numDone < docs.length) {
nextDoc();
} else {
checkAllDocsDone();
}
}
function nextDoc() {
var value = docs[numDone];
var currentDoc = value[0];
var resultsIdx = value[1];
if (fetchedDocs.has(id)) {
exports.updateDoc(fetchedDocs.get(id), currentDoc, results,
resultsIdx, docWritten, writeDoc, newEdits);
} else {
insertDoc(currentDoc, resultsIdx, docWritten);
}
}
nextDoc();
});
};
exports.preprocessAttachments = function preprocessAttachments(
docInfos, blobType, callback) {
if (!docInfos.length) {
return callback();
}
var docv = 0;
function parseBase64(data) {
try {
return exports.atob(data);
} catch (e) {
var err = errors.error(errors.BAD_ARG,
'Attachments need to be base64 encoded');
return {error: err};
}
}
function preprocessAttachment(att, callback) {
if (att.stub) {
return callback();
}
if (typeof att.data === 'string') {
// input is a base64 string
var asBinary = parseBase64(att.data);
if (asBinary.error) {
return callback(asBinary.error);
}
att.length = asBinary.length;
if (blobType === 'blob') {
att.data = exports.createBlob([exports.fixBinary(asBinary)],
{type: att.content_type});
} else if (blobType === 'base64') {
att.data = exports.btoa(asBinary);
} else { // binary
att.data = asBinary;
}
exports.MD5(asBinary).then(function (result) {
att.digest = 'md5-' + result;
callback();
});
} else { // input is a blob
exports.readAsBinaryString(att.data, function (binary) {
if (blobType === 'binary') {
att.data = binary;
} else if (blobType === 'base64') {
att.data = exports.btoa(binary);
}
exports.MD5(binary).then(function (result) {
att.digest = 'md5-' + result;
att.length = binary.length;
callback();
});
});
}
}
var overallErr;
docInfos.forEach(function (docInfo) {
var attachments = docInfo.data && docInfo.data._attachments ?
Object.keys(docInfo.data._attachments) : [];
var recv = 0;
if (!attachments.length) {
return done();
}
function processedAttachment(err) {
overallErr = err;
recv++;
if (recv === attachments.length) {
done();
}
}
for (var key in docInfo.data._attachments) {
if (docInfo.data._attachments.hasOwnProperty(key)) {
preprocessAttachment(docInfo.data._attachments[key],
processedAttachment);
}
}
});
function done() {
docv++;
if (docInfos.length === docv) {
if (overallErr) {
callback(overallErr);
} else {
callback();
}
}
}
};
// compact a tree by marking its non-leafs as missing,
// and return a list of revs to delete
exports.compactTree = function compactTree(metadata) {
var revs = [];
merge.traverseRevTree(metadata.rev_tree, function (isLeaf, pos,
revHash, ctx, opts) {
if (opts.status === 'available' && !isLeaf) {
revs.push(pos + '-' + revHash);
opts.status = 'missing';
}
});
return revs;
};
var vuvuzela = _dereq_(97);
exports.safeJsonParse = function safeJsonParse(str) {
try {
return JSON.parse(str);
} catch (e) {
return vuvuzela.parse(str);
}
};
exports.safeJsonStringify = function safeJsonStringify(json) {
try {
return JSON.stringify(json);
} catch (e) {
return vuvuzela.stringify(json);
}
};
}).call(this,_dereq_(37),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{}],33:[function(_dereq_,module,exports){
module.exports = "3.2.2-prerelease";
},{}],34:[function(_dereq_,module,exports){
'use strict';
module.exports = argsArray;
function argsArray(fun) {
return function () {
var len = arguments.length;
if (len) {
var args = [];
var i = -1;
while (++i < len) {
args[i] = arguments[i];
}
return fun.call(this, args);
} else {
return fun.call(this, []);
}
};
}
},{}],35:[function(_dereq_,module,exports){
},{}],36:[function(_dereq_,module,exports){
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
function EventEmitter() {
this._events = this._events || {};
this._maxListeners = this._maxListeners || undefined;
}
module.exports = EventEmitter;
// Backwards-compat with node 0.10.x
EventEmitter.EventEmitter = EventEmitter;
EventEmitter.prototype._events = undefined;
EventEmitter.prototype._maxListeners = undefined;
// By default EventEmitters will print a warning if more than 10 listeners are
// added to it. This is a useful default which helps finding memory leaks.
EventEmitter.defaultMaxListeners = 10;
// Obviously not all Emitters should be limited to 10. This function allows
// that to be increased. Set to zero for unlimited.
EventEmitter.prototype.setMaxListeners = function(n) {
if (!isNumber(n) || n < 0 || isNaN(n))
throw TypeError('n must be a positive number');
this._maxListeners = n;
return this;
};
EventEmitter.prototype.emit = function(type) {
var er, handler, len, args, i, listeners;
if (!this._events)
this._events = {};
// If there is no 'error' event listener then throw.
if (type === 'error') {
if (!this._events.error ||
(isObject(this._events.error) && !this._events.error.length)) {
er = arguments[1];
if (er instanceof Error) {
throw er; // Unhandled 'error' event
}
throw TypeError('Uncaught, unspecified "error" event.');
}
}
handler = this._events[type];
if (isUndefined(handler))
return false;
if (isFunction(handler)) {
switch (arguments.length) {
// fast cases
case 1:
handler.call(this);
break;
case 2:
handler.call(this, arguments[1]);
break;
case 3:
handler.call(this, arguments[1], arguments[2]);
break;
// slower
default:
len = arguments.length;
args = new Array(len - 1);
for (i = 1; i < len; i++)
args[i - 1] = arguments[i];
handler.apply(this, args);
}
} else if (isObject(handler)) {
len = arguments.length;
args = new Array(len - 1);
for (i = 1; i < len; i++)
args[i - 1] = arguments[i];
listeners = handler.slice();
len = listeners.length;
for (i = 0; i < len; i++)
listeners[i].apply(this, args);
}
return true;
};
EventEmitter.prototype.addListener = function(type, listener) {
var m;
if (!isFunction(listener))
throw TypeError('listener must be a function');
if (!this._events)
this._events = {};
// To avoid recursion in the case that type === "newListener"! Before
// adding it to the listeners, first emit "newListener".
if (this._events.newListener)
this.emit('newListener', type,
isFunction(listener.listener) ?
listener.listener : listener);
if (!this._events[type])
// Optimize the case of one listener. Don't need the extra array object.
this._events[type] = listener;
else if (isObject(this._events[type]))
// If we've already got an array, just append.
this._events[type].push(listener);
else
// Adding the second element, need to change to array.
this._events[type] = [this._events[type], listener];
// Check for listener leak
if (isObject(this._events[type]) && !this._events[type].warned) {
var m;
if (!isUndefined(this._maxListeners)) {
m = this._maxListeners;
} else {
m = EventEmitter.defaultMaxListeners;
}
if (m && m > 0 && this._events[type].length > m) {
this._events[type].warned = true;
console.error('(node) warning: possible EventEmitter memory ' +
'leak detected. %d listeners added. ' +
'Use emitter.setMaxListeners() to increase limit.',
this._events[type].length);
if (typeof console.trace === 'function') {
// not supported in IE 10
console.trace();
}
}
}
return this;
};
EventEmitter.prototype.on = EventEmitter.prototype.addListener;
EventEmitter.prototype.once = function(type, listener) {
if (!isFunction(listener))
throw TypeError('listener must be a function');
var fired = false;
function g() {
this.removeListener(type, g);
if (!fired) {
fired = true;
listener.apply(this, arguments);
}
}
g.listener = listener;
this.on(type, g);
return this;
};
// emits a 'removeListener' event iff the listener was removed
EventEmitter.prototype.removeListener = function(type, listener) {
var list, position, length, i;
if (!isFunction(listener))
throw TypeError('listener must be a function');
if (!this._events || !this._events[type])
return this;
list = this._events[type];
length = list.length;
position = -1;
if (list === listener ||
(isFunction(list.listener) && list.listener === listener)) {
delete this._events[type];
if (this._events.removeListener)
this.emit('removeListener', type, listener);
} else if (isObject(list)) {
for (i = length; i-- > 0;) {
if (list[i] === listener ||
(list[i].listener && list[i].listener === listener)) {
position = i;
break;
}
}
if (position < 0)
return this;
if (list.length === 1) {
list.length = 0;
delete this._events[type];
} else {
list.splice(position, 1);
}
if (this._events.removeListener)
this.emit('removeListener', type, listener);
}
return this;
};
EventEmitter.prototype.removeAllListeners = function(type) {
var key, listeners;
if (!this._events)
return this;
// not listening for removeListener, no need to emit
if (!this._events.removeListener) {
if (arguments.length === 0)
this._events = {};
else if (this._events[type])
delete this._events[type];
return this;
}
// emit removeListener for all listeners on all events
if (arguments.length === 0) {
for (key in this._events) {
if (key === 'removeListener') continue;
this.removeAllListeners(key);
}
this.removeAllListeners('removeListener');
this._events = {};
return this;
}
listeners = this._events[type];
if (isFunction(listeners)) {
this.removeListener(type, listeners);
} else {
// LIFO order
while (listeners.length)
this.removeListener(type, listeners[listeners.length - 1]);
}
delete this._events[type];
return this;
};
EventEmitter.prototype.listeners = function(type) {
var ret;
if (!this._events || !this._events[type])
ret = [];
else if (isFunction(this._events[type]))
ret = [this._events[type]];
else
ret = this._events[type].slice();
return ret;
};
EventEmitter.listenerCount = function(emitter, type) {
var ret;
if (!emitter._events || !emitter._events[type])
ret = 0;
else if (isFunction(emitter._events[type]))
ret = 1;
else
ret = emitter._events[type].length;
return ret;
};
function isFunction(arg) {
return typeof arg === 'function';
}
function isNumber(arg) {
return typeof arg === 'number';
}
function isObject(arg) {
return typeof arg === 'object' && arg !== null;
}
function isUndefined(arg) {
return arg === void 0;
}
},{}],37:[function(_dereq_,module,exports){
// shim for using process in browser
var process = module.exports = {};
process.nextTick = (function () {
var canSetImmediate = typeof window !== 'undefined'
&& window.setImmediate;
var canMutationObserver = typeof window !== 'undefined'
&& window.MutationObserver;
var canPost = typeof window !== 'undefined'
&& window.postMessage && window.addEventListener
;
if (canSetImmediate) {
return function (f) { return window.setImmediate(f) };
}
var queue = [];
if (canMutationObserver) {
var hiddenDiv = document.createElement("div");
var observer = new MutationObserver(function () {
var queueList = queue.slice();
queue.length = 0;
queueList.forEach(function (fn) {
fn();
});
});
observer.observe(hiddenDiv, { attributes: true });
return function nextTick(fn) {
if (!queue.length) {
hiddenDiv.setAttribute('yes', 'no');
}
queue.push(fn);
};
}
if (canPost) {
window.addEventListener('message', function (ev) {
var source = ev.source;
if ((source === window || source === null) && ev.data === 'process-tick') {
ev.stopPropagation();
if (queue.length > 0) {
var fn = queue.shift();
fn();
}
}
}, true);
return function nextTick(fn) {
queue.push(fn);
window.postMessage('process-tick', '*');
};
}
return function nextTick(fn) {
setTimeout(fn, 0);
};
})();
process.title = 'browser';
process.browser = true;
process.env = {};
process.argv = [];
function noop() {}
process.on = noop;
process.addListener = noop;
process.once = noop;
process.off = noop;
process.removeListener = noop;
process.removeAllListeners = noop;
process.emit = noop;
process.binding = function (name) {
throw new Error('process.binding is not supported');
};
// TODO(shtylman)
process.cwd = function () { return '/' };
process.chdir = function (dir) {
throw new Error('process.chdir is not supported');
};
},{}],38:[function(_dereq_,module,exports){
/**
* This is the web browser implementation of `debug()`.
*
* Expose `debug()` as the module.
*/
exports = module.exports = _dereq_(39);
exports.log = log;
exports.formatArgs = formatArgs;
exports.save = save;
exports.load = load;
exports.useColors = useColors;
/**
* Use chrome.storage.local if we are in an app
*/
var storage;
if (typeof chrome !== 'undefined' && typeof chrome.storage !== 'undefined')
storage = chrome.storage.local;
else
storage = window.localStorage;
/**
* Colors.
*/
exports.colors = [
'lightseagreen',
'forestgreen',
'goldenrod',
'dodgerblue',
'darkorchid',
'crimson'
];
/**
* Currently only WebKit-based Web Inspectors, Firefox >= v31,
* and the Firebug extension (any Firefox version) are known
* to support "%c" CSS customizations.
*
* TODO: add a `localStorage` variable to explicitly enable/disable colors
*/
function useColors() {
// is webkit? http://stackoverflow.com/a/16459606/376773
return ('WebkitAppearance' in document.documentElement.style) ||
// is firebug? http://stackoverflow.com/a/398120/376773
(window.console && (console.firebug || (console.exception && console.table))) ||
// is firefox >= v31?
// https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages
(navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/) && parseInt(RegExp.$1, 10) >= 31);
}
/**
* Map %j to `JSON.stringify()`, since no Web Inspectors do that by default.
*/
exports.formatters.j = function(v) {
return JSON.stringify(v);
};
/**
* Colorize log arguments if enabled.
*
* @api public
*/
function formatArgs() {
var args = arguments;
var useColors = this.useColors;
args[0] = (useColors ? '%c' : '')
+ this.namespace
+ (useColors ? ' %c' : ' ')
+ args[0]
+ (useColors ? '%c ' : ' ')
+ '+' + exports.humanize(this.diff);
if (!useColors) return args;
var c = 'color: ' + this.color;
args = [args[0], c, 'color: inherit'].concat(Array.prototype.slice.call(args, 1));
// the final "%c" is somewhat tricky, because there could be other
// arguments passed either before or after the %c, so we need to
// figure out the correct index to insert the CSS into
var index = 0;
var lastC = 0;
args[0].replace(/%[a-z%]/g, function(match) {
if ('%' === match) return;
index++;
if ('%c' === match) {
// we only are interested in the *last* %c
// (the user may have provided their own)
lastC = index;
}
});
args.splice(lastC, 0, c);
return args;
}
/**
* Invokes `console.log()` when available.
* No-op when `console.log` is not a "function".
*
* @api public
*/
function log() {
// this hackery is required for IE8/9, where
// the `console.log` function doesn't have 'apply'
return 'object' === typeof console
&& console.log
&& Function.prototype.apply.call(console.log, console, arguments);
}
/**
* Save `namespaces`.
*
* @param {String} namespaces
* @api private
*/
function save(namespaces) {
try {
if (null == namespaces) {
storage.removeItem('debug');
} else {
storage.debug = namespaces;
}
} catch(e) {}
}
/**
* Load `namespaces`.
*
* @return {String} returns the previously persisted debug modes
* @api private
*/
function load() {
var r;
try {
r = storage.debug;
} catch(e) {}
return r;
}
/**
* Enable namespaces listed in `localStorage.debug` initially.
*/
exports.enable(load());
},{}],39:[function(_dereq_,module,exports){
/**
* This is the common logic for both the Node.js and web browser
* implementations of `debug()`.
*
* Expose `debug()` as the module.
*/
exports = module.exports = debug;
exports.coerce = coerce;
exports.disable = disable;
exports.enable = enable;
exports.enabled = enabled;
exports.humanize = _dereq_(40);
/**
* The currently active debug mode names, and names to skip.
*/
exports.names = [];
exports.skips = [];
/**
* Map of special "%n" handling functions, for the debug "format" argument.
*
* Valid key names are a single, lowercased letter, i.e. "n".
*/
exports.formatters = {};
/**
* Previously assigned color.
*/
var prevColor = 0;
/**
* Previous log timestamp.
*/
var prevTime;
/**
* Select a color.
*
* @return {Number}
* @api private
*/
function selectColor() {
return exports.colors[prevColor++ % exports.colors.length];
}
/**
* Create a debugger with the given `namespace`.
*
* @param {String} namespace
* @return {Function}
* @api public
*/
function debug(namespace) {
// define the `disabled` version
function disabled() {
}
disabled.enabled = false;
// define the `enabled` version
function enabled() {
var self = enabled;
// set `diff` timestamp
var curr = +new Date();
var ms = curr - (prevTime || curr);
self.diff = ms;
self.prev = prevTime;
self.curr = curr;
prevTime = curr;
// add the `color` if not set
if (null == self.useColors) self.useColors = exports.useColors();
if (null == self.color && self.useColors) self.color = selectColor();
var args = Array.prototype.slice.call(arguments);
args[0] = exports.coerce(args[0]);
if ('string' !== typeof args[0]) {
// anything else let's inspect with %o
args = ['%o'].concat(args);
}
// apply any `formatters` transformations
var index = 0;
args[0] = args[0].replace(/%([a-z%])/g, function(match, format) {
// if we encounter an escaped % then don't increase the array index
if (match === '%') return match;
index++;
var formatter = exports.formatters[format];
if ('function' === typeof formatter) {
var val = args[index];
match = formatter.call(self, val);
// now we need to remove `args[index]` since it's inlined in the `format`
args.splice(index, 1);
index--;
}
return match;
});
if ('function' === typeof exports.formatArgs) {
args = exports.formatArgs.apply(self, args);
}
var logFn = enabled.log || exports.log || console.log.bind(console);
logFn.apply(self, args);
}
enabled.enabled = true;
var fn = exports.enabled(namespace) ? enabled : disabled;
fn.namespace = namespace;
return fn;
}
/**
* Enables a debug mode by namespaces. This can include modes
* separated by a colon and wildcards.
*
* @param {String} namespaces
* @api public
*/
function enable(namespaces) {
exports.save(namespaces);
var split = (namespaces || '').split(/[\s,]+/);
var len = split.length;
for (var i = 0; i < len; i++) {
if (!split[i]) continue; // ignore empty strings
namespaces = split[i].replace(/\*/g, '.*?');
if (namespaces[0] === '-') {
exports.skips.push(new RegExp('^' + namespaces.substr(1) + '$'));
} else {
exports.names.push(new RegExp('^' + namespaces + '$'));
}
}
}
/**
* Disable debug output.
*
* @api public
*/
function disable() {
exports.enable('');
}
/**
* Returns true if the given mode name is enabled, false otherwise.
*
* @param {String} name
* @return {Boolean}
* @api public
*/
function enabled(name) {
var i, len;
for (i = 0, len = exports.skips.length; i < len; i++) {
if (exports.skips[i].test(name)) {
return false;
}
}
for (i = 0, len = exports.names.length; i < len; i++) {
if (exports.names[i].test(name)) {
return true;
}
}
return false;
}
/**
* Coerce `val`.
*
* @param {Mixed} val
* @return {Mixed}
* @api private
*/
function coerce(val) {
if (val instanceof Error) return val.stack || val.message;
return val;
}
},{}],40:[function(_dereq_,module,exports){
/**
* Helpers.
*/
var s = 1000;
var m = s * 60;
var h = m * 60;
var d = h * 24;
var y = d * 365.25;
/**
* Parse or format the given `val`.
*
* Options:
*
* - `long` verbose formatting [false]
*
* @param {String|Number} val
* @param {Object} options
* @return {String|Number}
* @api public
*/
module.exports = function(val, options){
options = options || {};
if ('string' == typeof val) return parse(val);
return options.long
? long(val)
: short(val);
};
/**
* Parse the given `str` and return milliseconds.
*
* @param {String} str
* @return {Number}
* @api private
*/
function parse(str) {
var match = /^((?:\d+)?\.?\d+) *(ms|seconds?|s|minutes?|m|hours?|h|days?|d|years?|y)?$/i.exec(str);
if (!match) return;
var n = parseFloat(match[1]);
var type = (match[2] || 'ms').toLowerCase();
switch (type) {
case 'years':
case 'year':
case 'y':
return n * y;
case 'days':
case 'day':
case 'd':
return n * d;
case 'hours':
case 'hour':
case 'h':
return n * h;
case 'minutes':
case 'minute':
case 'm':
return n * m;
case 'seconds':
case 'second':
case 's':
return n * s;
case 'ms':
return n;
}
}
/**
* Short format for `ms`.
*
* @param {Number} ms
* @return {String}
* @api private
*/
function short(ms) {
if (ms >= d) return Math.round(ms / d) + 'd';
if (ms >= h) return Math.round(ms / h) + 'h';
if (ms >= m) return Math.round(ms / m) + 'm';
if (ms >= s) return Math.round(ms / s) + 's';
return ms + 'ms';
}
/**
* Long format for `ms`.
*
* @param {Number} ms
* @return {String}
* @api private
*/
function long(ms) {
return plural(ms, d, 'day')
|| plural(ms, h, 'hour')
|| plural(ms, m, 'minute')
|| plural(ms, s, 'second')
|| ms + ' ms';
}
/**
* Pluralization helper.
*/
function plural(ms, n, name) {
if (ms < n) return;
if (ms < n * 1.5) return Math.floor(ms / n) + ' ' + name;
return Math.ceil(ms / n) + ' ' + name + 's';
}
},{}],41:[function(_dereq_,module,exports){
if (typeof Object.create === 'function') {
// implementation from standard node.js 'util' module
module.exports = function inherits(ctor, superCtor) {
ctor.super_ = superCtor
ctor.prototype = Object.create(superCtor.prototype, {
constructor: {
value: ctor,
enumerable: false,
writable: true,
configurable: true
}
});
};
} else {
// old school shim for old browsers
module.exports = function inherits(ctor, superCtor) {
ctor.super_ = superCtor
var TempCtor = function () {}
TempCtor.prototype = superCtor.prototype
ctor.prototype = new TempCtor()
ctor.prototype.constructor = ctor
}
}
},{}],42:[function(_dereq_,module,exports){
'use strict';
module.exports = INTERNAL;
function INTERNAL() {}
},{}],43:[function(_dereq_,module,exports){
'use strict';
var Promise = _dereq_(46);
var reject = _dereq_(49);
var resolve = _dereq_(50);
var INTERNAL = _dereq_(42);
var handlers = _dereq_(44);
module.exports = all;
function all(iterable) {
if (Object.prototype.toString.call(iterable) !== '[object Array]') {
return reject(new TypeError('must be an array'));
}
var len = iterable.length;
var called = false;
if (!len) {
return resolve([]);
}
var values = new Array(len);
var resolved = 0;
var i = -1;
var promise = new Promise(INTERNAL);
while (++i < len) {
allResolver(iterable[i], i);
}
return promise;
function allResolver(value, i) {
resolve(value).then(resolveFromAll, function (error) {
if (!called) {
called = true;
handlers.reject(promise, error);
}
});
function resolveFromAll(outValue) {
values[i] = outValue;
if (++resolved === len & !called) {
called = true;
handlers.resolve(promise, values);
}
}
}
}
},{}],44:[function(_dereq_,module,exports){
'use strict';
var tryCatch = _dereq_(53);
var resolveThenable = _dereq_(51);
var states = _dereq_(52);
exports.resolve = function (self, value) {
var result = tryCatch(getThen, value);
if (result.status === 'error') {
return exports.reject(self, result.value);
}
var thenable = result.value;
if (thenable) {
resolveThenable.safely(self, thenable);
} else {
self.state = states.FULFILLED;
self.outcome = value;
var i = -1;
var len = self.queue.length;
while (++i < len) {
self.queue[i].callFulfilled(value);
}
}
return self;
};
exports.reject = function (self, error) {
self.state = states.REJECTED;
self.outcome = error;
var i = -1;
var len = self.queue.length;
while (++i < len) {
self.queue[i].callRejected(error);
}
return self;
};
function getThen(obj) {
// Make sure we only access the accessor once as required by the spec
var then = obj && obj.then;
if (obj && typeof obj === 'object' && typeof then === 'function') {
return function appyThen() {
then.apply(obj, arguments);
};
}
}
},{}],45:[function(_dereq_,module,exports){
module.exports = exports = _dereq_(46);
exports.resolve = _dereq_(50);
exports.reject = _dereq_(49);
exports.all = _dereq_(43);
exports.race = _dereq_(48);
},{}],46:[function(_dereq_,module,exports){
'use strict';
var unwrap = _dereq_(54);
var INTERNAL = _dereq_(42);
var resolveThenable = _dereq_(51);
var states = _dereq_(52);
var QueueItem = _dereq_(47);
module.exports = Promise;
function Promise(resolver) {
if (!(this instanceof Promise)) {
return new Promise(resolver);
}
if (typeof resolver !== 'function') {
throw new TypeError('resolver must be a function');
}
this.state = states.PENDING;
this.queue = [];
this.outcome = void 0;
if (resolver !== INTERNAL) {
resolveThenable.safely(this, resolver);
}
}
Promise.prototype['catch'] = function (onRejected) {
return this.then(null, onRejected);
};
Promise.prototype.then = function (onFulfilled, onRejected) {
if (typeof onFulfilled !== 'function' && this.state === states.FULFILLED ||
typeof onRejected !== 'function' && this.state === states.REJECTED) {
return this;
}
var promise = new Promise(INTERNAL);
if (this.state !== states.PENDING) {
var resolver = this.state === states.FULFILLED ? onFulfilled: onRejected;
unwrap(promise, resolver, this.outcome);
} else {
this.queue.push(new QueueItem(promise, onFulfilled, onRejected));
}
return promise;
};
},{}],47:[function(_dereq_,module,exports){
'use strict';
var handlers = _dereq_(44);
var unwrap = _dereq_(54);
module.exports = QueueItem;
function QueueItem(promise, onFulfilled, onRejected) {
this.promise = promise;
if (typeof onFulfilled === 'function') {
this.onFulfilled = onFulfilled;
this.callFulfilled = this.otherCallFulfilled;
}
if (typeof onRejected === 'function') {
this.onRejected = onRejected;
this.callRejected = this.otherCallRejected;
}
}
QueueItem.prototype.callFulfilled = function (value) {
handlers.resolve(this.promise, value);
};
QueueItem.prototype.otherCallFulfilled = function (value) {
unwrap(this.promise, this.onFulfilled, value);
};
QueueItem.prototype.callRejected = function (value) {
handlers.reject(this.promise, value);
};
QueueItem.prototype.otherCallRejected = function (value) {
unwrap(this.promise, this.onRejected, value);
};
},{}],48:[function(_dereq_,module,exports){
'use strict';
var Promise = _dereq_(46);
var reject = _dereq_(49);
var resolve = _dereq_(50);
var INTERNAL = _dereq_(42);
var handlers = _dereq_(44);
module.exports = race;
function race(iterable) {
if (Object.prototype.toString.call(iterable) !== '[object Array]') {
return reject(new TypeError('must be an array'));
}
var len = iterable.length;
var called = false;
if (!len) {
return resolve([]);
}
var resolved = 0;
var i = -1;
var promise = new Promise(INTERNAL);
while (++i < len) {
resolver(iterable[i]);
}
return promise;
function resolver(value) {
resolve(value).then(function (response) {
if (!called) {
called = true;
handlers.resolve(promise, response);
}
}, function (error) {
if (!called) {
called = true;
handlers.reject(promise, error);
}
});
}
}
},{}],49:[function(_dereq_,module,exports){
'use strict';
var Promise = _dereq_(46);
var INTERNAL = _dereq_(42);
var handlers = _dereq_(44);
module.exports = reject;
function reject(reason) {
var promise = new Promise(INTERNAL);
return handlers.reject(promise, reason);
}
},{}],50:[function(_dereq_,module,exports){
'use strict';
var Promise = _dereq_(46);
var INTERNAL = _dereq_(42);
var handlers = _dereq_(44);
module.exports = resolve;
var FALSE = handlers.resolve(new Promise(INTERNAL), false);
var NULL = handlers.resolve(new Promise(INTERNAL), null);
var UNDEFINED = handlers.resolve(new Promise(INTERNAL), void 0);
var ZERO = handlers.resolve(new Promise(INTERNAL), 0);
var EMPTYSTRING = handlers.resolve(new Promise(INTERNAL), '');
function resolve(value) {
if (value) {
if (value instanceof Promise) {
return value;
}
return handlers.resolve(new Promise(INTERNAL), value);
}
var valueType = typeof value;
switch (valueType) {
case 'boolean':
return FALSE;
case 'undefined':
return UNDEFINED;
case 'object':
return NULL;
case 'number':
return ZERO;
case 'string':
return EMPTYSTRING;
}
}
},{}],51:[function(_dereq_,module,exports){
'use strict';
var handlers = _dereq_(44);
var tryCatch = _dereq_(53);
function safelyResolveThenable(self, thenable) {
// Either fulfill, reject or reject with error
var called = false;
function onError(value) {
if (called) {
return;
}
called = true;
handlers.reject(self, value);
}
function onSuccess(value) {
if (called) {
return;
}
called = true;
handlers.resolve(self, value);
}
function tryToUnwrap() {
thenable(onSuccess, onError);
}
var result = tryCatch(tryToUnwrap);
if (result.status === 'error') {
onError(result.value);
}
}
exports.safely = safelyResolveThenable;
},{}],52:[function(_dereq_,module,exports){
// Lazy man's symbols for states
exports.REJECTED = ['REJECTED'];
exports.FULFILLED = ['FULFILLED'];
exports.PENDING = ['PENDING'];
},{}],53:[function(_dereq_,module,exports){
'use strict';
module.exports = tryCatch;
function tryCatch(func, value) {
var out = {};
try {
out.value = func(value);
out.status = 'success';
} catch (e) {
out.status = 'error';
out.value = e;
}
return out;
}
},{}],54:[function(_dereq_,module,exports){
'use strict';
var immediate = _dereq_(55);
var handlers = _dereq_(44);
module.exports = unwrap;
function unwrap(promise, func, value) {
immediate(function () {
var returnValue;
try {
returnValue = func(value);
} catch (e) {
return handlers.reject(promise, e);
}
if (returnValue === promise) {
handlers.reject(promise, new TypeError('Cannot resolve promise with itself'));
} else {
handlers.resolve(promise, returnValue);
}
});
}
},{}],55:[function(_dereq_,module,exports){
'use strict';
var types = [
_dereq_(35),
_dereq_(57),
_dereq_(56),
_dereq_(58),
_dereq_(59)
];
var draining;
var queue = [];
//named nextTick for less confusing stack traces
function nextTick() {
draining = true;
var i, oldQueue;
var len = queue.length;
while (len) {
oldQueue = queue;
queue = [];
i = -1;
while (++i < len) {
oldQueue[i]();
}
len = queue.length;
}
draining = false;
}
var scheduleDrain;
var i = -1;
var len = types.length;
while (++ i < len) {
if (types[i] && types[i].test && types[i].test()) {
scheduleDrain = types[i].install(nextTick);
break;
}
}
module.exports = immediate;
function immediate(task) {
if (queue.push(task) === 1 && !draining) {
scheduleDrain();
}
}
},{}],56:[function(_dereq_,module,exports){
(function (global){
'use strict';
exports.test = function () {
if (global.setImmediate) {
// we can only get here in IE10
// which doesn't handel postMessage well
return false;
}
return typeof global.MessageChannel !== 'undefined';
};
exports.install = function (func) {
var channel = new global.MessageChannel();
channel.port1.onmessage = func;
return function () {
channel.port2.postMessage(0);
};
};
}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{}],57:[function(_dereq_,module,exports){
(function (global){
'use strict';
//based off rsvp https://github.com/tildeio/rsvp.js
//license https://github.com/tildeio/rsvp.js/blob/master/LICENSE
//https://github.com/tildeio/rsvp.js/blob/master/lib/rsvp/asap.js
var Mutation = global.MutationObserver || global.WebKitMutationObserver;
exports.test = function () {
return Mutation;
};
exports.install = function (handle) {
var called = 0;
var observer = new Mutation(handle);
var element = global.document.createTextNode('');
observer.observe(element, {
characterData: true
});
return function () {
element.data = (called = ++called % 2);
};
};
}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{}],58:[function(_dereq_,module,exports){
(function (global){
'use strict';
exports.test = function () {
return 'document' in global && 'onreadystatechange' in global.document.createElement('script');
};
exports.install = function (handle) {
return function () {
// Create a