From ac20ee347f379ad4ab8b3eb5bbcc9605bcf14703 Mon Sep 17 00:00:00 2001 From: Cyrille Perois Date: Fri, 22 Jun 2018 16:12:24 +0200 Subject: [PATCH] chore: release v0.11.0 --- CHANGELOG.md | 14 +- dist/cozy-client.js | 28035 ++++++++++++++++----------------- dist/cozy-client.js.map | 2 +- dist/cozy-client.min.js | 18 +- dist/cozy-client.min.js.map | 2 +- dist/cozy-client.node.js | 15 +- dist/cozy-client.node.js.map | 2 +- package.json | 2 +- 8 files changed, 13320 insertions(+), 14770 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5da9319e..029f0d59 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - none yet +## [v0.11.0] - 2018-06-22 + +### Added +- intents: add an redirectFn parameter to redirect method + +### Removed +- none yet + + ## [v0.10.0] - 2018-06-22 ### Changed @@ -27,7 +36,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Fixed -- isomorphic-fetch has been added as a direct dependency, fixing a problem on node on macOS. +- isomorphic-fetch has been added as a direct dependency, fixing a problem on node on macOS. ## [v0.9.0] - 2018-04-27 ### Added @@ -336,7 +345,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Refactoring on offline to return Promise -[Unreleased]: https://github.com/cozy/cozy-client-js/compare/v0.10.0...HEAD +[Unreleased]: https://github.com/cozy/cozy-client-js/compare/v0.11.0...HEAD +[v0.11.0]: https://github.com/cozy/cozy-client-js/compare/v0.10.0...v0.11.0 [v0.10.0]: https://github.com/cozy/cozy-client-js/compare/v0.9.0...v0.10.0 [v0.9.0]: https://github.com/cozy/cozy-client-js/compare/v0.8.3...v0.9.0 [v0.8.3]: https://github.com/cozy/cozy-client-js/compare/v0.8.2...v0.8.3 diff --git a/dist/cozy-client.js b/dist/cozy-client.js index e97077aa..2f1971cd 100644 --- a/dist/cozy-client.js +++ b/dist/cozy-client.js @@ -434,10 +434,7 @@ return /******/ (function(modules) { // webpackBootstrap function parseHeaders(rawHeaders) { var headers = new Headers() - // Replace instances of \r\n and \n followed by at least one space or horizontal tab with a space - // https://tools.ietf.org/html/rfc7230#section-3.2 - var preProcessedHeaders = rawHeaders.replace(/\r?\n[\t ]+/g, ' ') - preProcessedHeaders.split(/\r?\n/).forEach(function(line) { + rawHeaders.split(/\r?\n/).forEach(function(line) { var parts = line.split(':') var key = parts.shift().trim() if (key) { @@ -456,7 +453,7 @@ return /******/ (function(modules) { // webpackBootstrap } this.type = 'default' - this.status = options.status === undefined ? 200 : options.status + this.status = 'status' in options ? options.status : 200 this.ok = this.status >= 200 && this.status < 300 this.statusText = 'statusText' in options ? options.statusText : 'OK' this.headers = new Headers(options.headers) @@ -523,8 +520,6 @@ return /******/ (function(modules) { // webpackBootstrap if (request.credentials === 'include') { xhr.withCredentials = true - } else if (request.credentials === 'omit') { - xhr.withCredentials = false } if ('responseType' in xhr && support.blob) { @@ -553,33 +548,33 @@ return /******/ (function(modules) { // webpackBootstrap __webpack_require__(4); - var _utils = __webpack_require__(41); + var _utils = __webpack_require__(40); - var _auth_storage = __webpack_require__(42); + var _auth_storage = __webpack_require__(41); - var _auth_v = __webpack_require__(43); + var _auth_v = __webpack_require__(42); - var _auth_v2 = __webpack_require__(44); + var _auth_v2 = __webpack_require__(43); var auth = _interopRequireWildcard(_auth_v2); - var _data = __webpack_require__(47); + var _data = __webpack_require__(46); var data = _interopRequireWildcard(_data); - var _fetch = __webpack_require__(45); + var _fetch = __webpack_require__(44); var cozyFetch = _interopRequireWildcard(_fetch); - var _mango = __webpack_require__(49); + var _mango = __webpack_require__(48); var mango = _interopRequireWildcard(_mango); - var _files = __webpack_require__(50); + var _files = __webpack_require__(49); var files = _interopRequireWildcard(_files); - var _intents = __webpack_require__(51); + var _intents = __webpack_require__(50); var intents = _interopRequireWildcard(_intents); @@ -591,11 +586,11 @@ return /******/ (function(modules) { // webpackBootstrap var offline = _interopRequireWildcard(_offline); - var _settings = __webpack_require__(88); + var _settings = __webpack_require__(101); var settings = _interopRequireWildcard(_settings); - var _relations = __webpack_require__(89); + var _relations = __webpack_require__(102); var relations = _interopRequireWildcard(_relations); @@ -1007,7 +1002,7 @@ return /******/ (function(modules) { // webpackBootstrap /* 7 */ /***/ function(module, exports) { - var core = module.exports = { version: '2.5.7' }; + var core = module.exports = { version: '2.5.3' }; if (typeof __e == 'number') __e = core; // eslint-disable-line no-undef @@ -1245,9 +1240,9 @@ return /******/ (function(modules) { // webpackBootstrap 'use strict'; // 19.1.2.1 Object.assign(target, source, ...) var getKeys = __webpack_require__(24); - var gOPS = __webpack_require__(38); - var pIE = __webpack_require__(39); - var toObject = __webpack_require__(40); + var gOPS = __webpack_require__(37); + var pIE = __webpack_require__(38); + var toObject = __webpack_require__(39); var IObject = __webpack_require__(27); var $assign = Object.assign; @@ -1284,7 +1279,7 @@ return /******/ (function(modules) { // webpackBootstrap // 19.1.2.14 / 15.2.3.14 Object.keys(O) var $keys = __webpack_require__(25); - var enumBugKeys = __webpack_require__(37); + var enumBugKeys = __webpack_require__(36); module.exports = Object.keys || function keys(O) { return $keys(O, enumBugKeys); @@ -1441,29 +1436,16 @@ return /******/ (function(modules) { // webpackBootstrap /* 35 */ /***/ function(module, exports, __webpack_require__) { - var core = __webpack_require__(7); var global = __webpack_require__(6); var SHARED = '__core-js_shared__'; var store = global[SHARED] || (global[SHARED] = {}); - - (module.exports = function (key, value) { - return store[key] || (store[key] = value !== undefined ? value : {}); - })('versions', []).push({ - version: core.version, - mode: __webpack_require__(36) ? 'pure' : 'global', - copyright: '© 2018 Denis Pushkarev (zloirock.ru)' - }); + module.exports = function (key) { + return store[key] || (store[key] = {}); + }; /***/ }, /* 36 */ -/***/ function(module, exports) { - - module.exports = false; - - -/***/ }, -/* 37 */ /***/ function(module, exports) { // IE 8- don't enum bug keys @@ -1473,21 +1455,21 @@ return /******/ (function(modules) { // webpackBootstrap /***/ }, -/* 38 */ +/* 37 */ /***/ function(module, exports) { exports.f = Object.getOwnPropertySymbols; /***/ }, -/* 39 */ +/* 38 */ /***/ function(module, exports) { exports.f = {}.propertyIsEnumerable; /***/ }, -/* 40 */ +/* 39 */ /***/ function(module, exports, __webpack_require__) { // 7.1.13 ToObject(argument) @@ -1498,7 +1480,7 @@ return /******/ (function(modules) { // webpackBootstrap /***/ }, -/* 41 */ +/* 40 */ /***/ function(module, exports) { 'use strict'; @@ -1671,7 +1653,7 @@ return /******/ (function(modules) { // webpackBootstrap } /***/ }, -/* 42 */ +/* 41 */ /***/ function(module, exports) { 'use strict'; @@ -1785,7 +1767,7 @@ return /******/ (function(modules) { // webpackBootstrap }(); /***/ }, -/* 43 */ +/* 42 */ /***/ function(module, exports) { 'use strict'; @@ -1857,7 +1839,7 @@ return /******/ (function(modules) { // webpackBootstrap }(); /***/ }, -/* 44 */ +/* 43 */ /***/ function(module, exports, __webpack_require__) { 'use strict'; @@ -1882,9 +1864,9 @@ return /******/ (function(modules) { // webpackBootstrap exports.refreshToken = refreshToken; exports.oauthFlow = oauthFlow; - var _utils = __webpack_require__(41); + var _utils = __webpack_require__(40); - var _fetch = __webpack_require__(45); + var _fetch = __webpack_require__(44); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } @@ -2342,7 +2324,7 @@ return /******/ (function(modules) { // webpackBootstrap } /***/ }, -/* 45 */ +/* 44 */ /***/ function(module, exports, __webpack_require__) { 'use strict'; @@ -2360,11 +2342,11 @@ return /******/ (function(modules) { // webpackBootstrap exports.cozyFetchRawJSON = cozyFetchRawJSON; exports.handleInvalidTokenError = handleInvalidTokenError; - var _auth_v = __webpack_require__(44); + var _auth_v = __webpack_require__(43); - var _utils = __webpack_require__(41); + var _utils = __webpack_require__(40); - var _jsonapi = __webpack_require__(46); + var _jsonapi = __webpack_require__(45); var _jsonapi2 = _interopRequireDefault(_jsonapi); @@ -2566,7 +2548,7 @@ return /******/ (function(modules) { // webpackBootstrap }; /***/ }, -/* 46 */ +/* 45 */ /***/ function(module, exports) { 'use strict'; @@ -2632,7 +2614,7 @@ return /******/ (function(modules) { // webpackBootstrap exports.default = handleTopLevel; /***/ }, -/* 47 */ +/* 46 */ /***/ function(module, exports, __webpack_require__) { 'use strict'; @@ -2649,11 +2631,11 @@ return /******/ (function(modules) { // webpackBootstrap exports.updateAttributes = updateAttributes; exports._delete = _delete; - var _utils = __webpack_require__(41); + var _utils = __webpack_require__(40); - var _doctypes = __webpack_require__(48); + var _doctypes = __webpack_require__(47); - var _fetch = __webpack_require__(45); + var _fetch = __webpack_require__(44); var NOREV = 'stack-v2-no-rev'; @@ -2919,7 +2901,7 @@ return /******/ (function(modules) { // webpackBootstrap } /***/ }, -/* 48 */ +/* 47 */ /***/ function(module, exports, __webpack_require__) { 'use strict'; @@ -2930,7 +2912,7 @@ return /******/ (function(modules) { // webpackBootstrap exports.DOCTYPE_FILES = undefined; exports.normalizeDoctype = normalizeDoctype; - var _utils = __webpack_require__(41); + var _utils = __webpack_require__(40); var DOCTYPE_FILES = exports.DOCTYPE_FILES = 'io.cozy.files'; @@ -2967,7 +2949,7 @@ return /******/ (function(modules) { // webpackBootstrap } /***/ }, -/* 49 */ +/* 48 */ /***/ function(module, exports, __webpack_require__) { 'use strict'; @@ -2987,11 +2969,11 @@ return /******/ (function(modules) { // webpackBootstrap exports.normalizeSelector = normalizeSelector; exports.makeMapReduceQuery = makeMapReduceQuery; - var _utils = __webpack_require__(41); + var _utils = __webpack_require__(40); - var _doctypes = __webpack_require__(48); + var _doctypes = __webpack_require__(47); - var _fetch = __webpack_require__(45); + var _fetch = __webpack_require__(44); function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } @@ -3305,7 +3287,7 @@ return /******/ (function(modules) { // webpackBootstrap } /***/ }, -/* 50 */ +/* 49 */ /***/ function(module, exports, __webpack_require__) { 'use strict'; @@ -3342,13 +3324,13 @@ return /******/ (function(modules) { // webpackBootstrap exports.restoreById = restoreById; exports.destroyById = destroyById; - var _fetch = __webpack_require__(45); + var _fetch = __webpack_require__(44); - var _jsonapi = __webpack_require__(46); + var _jsonapi = __webpack_require__(45); var _jsonapi2 = _interopRequireDefault(_jsonapi); - var _doctypes = __webpack_require__(48); + var _doctypes = __webpack_require__(47); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } @@ -3776,7 +3758,7 @@ return /******/ (function(modules) { // webpackBootstrap } /***/ }, -/* 51 */ +/* 50 */ /***/ function(module, exports, __webpack_require__) { 'use strict'; @@ -3786,7 +3768,7 @@ return /******/ (function(modules) { // webpackBootstrap }); exports.redirect = exports.getRedirectionURL = undefined; - var _regenerator = __webpack_require__(52); + var _regenerator = __webpack_require__(51); var _regenerator2 = _interopRequireDefault(_regenerator); @@ -3851,7 +3833,7 @@ return /******/ (function(modules) { // webpackBootstrap }(); var redirect = exports.redirect = function () { - var _ref2 = _asyncToGenerator( /*#__PURE__*/_regenerator2.default.mark(function _callee2(cozy, type, doc) { + var _ref2 = _asyncToGenerator( /*#__PURE__*/_regenerator2.default.mark(function _callee2(cozy, type, doc, redirectFn) { var redirectionURL; return _regenerator2.default.wrap(function _callee2$(_context2) { while (1) { @@ -3871,9 +3853,18 @@ return /******/ (function(modules) { // webpackBootstrap case 4: redirectionURL = _context2.sent; + if (!(redirectFn && typeof redirectFn === 'function')) { + _context2.next = 7; + break; + } + + return _context2.abrupt('return', redirectFn(redirectionURL)); + + case 7: + window.location.href = redirectionURL; - case 6: + case 8: case 'end': return _context2.stop(); } @@ -3881,7 +3872,7 @@ return /******/ (function(modules) { // webpackBootstrap }, _callee2, this); })); - return function redirect(_x6, _x7, _x8) { + return function redirect(_x6, _x7, _x8, _x9) { return _ref2.apply(this, arguments); }; }(); @@ -3889,7 +3880,7 @@ return /******/ (function(modules) { // webpackBootstrap exports.create = create; exports.createService = createService; - var _fetch = __webpack_require__(45); + var _fetch = __webpack_require__(44); var _helpers = __webpack_require__(55); @@ -3962,26 +3953,22 @@ return /******/ (function(modules) { // webpackBootstrap } /***/ }, -/* 52 */ +/* 51 */ /***/ function(module, exports, __webpack_require__) { - module.exports = __webpack_require__(53); + module.exports = __webpack_require__(52); /***/ }, -/* 53 */ +/* 52 */ /***/ function(module, exports, __webpack_require__) { - /** - * Copyright (c) 2014-present, Facebook, Inc. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - - // This method of obtaining a reference to the global object needs to be + /* WEBPACK VAR INJECTION */(function(global) {// This method of obtaining a reference to the global object needs to be // kept identical to the way it is obtained in runtime.js - var g = (function() { return this })() || Function("return this")(); + var g = + typeof global === "object" ? global : + typeof window === "object" ? window : + typeof self === "object" ? self : this; // Use `getOwnPropertyNames` because not all browsers support calling // `hasOwnProperty` on the global `self` object in a worker. See #183. @@ -3994,7 +3981,7 @@ return /******/ (function(modules) { // webpackBootstrap // Force reevalutation of runtime.js. g.regeneratorRuntime = undefined; - module.exports = __webpack_require__(54); + module.exports = __webpack_require__(53); if (hadRuntime) { // Restore the original runtime. @@ -4007,17 +3994,21 @@ return /******/ (function(modules) { // webpackBootstrap g.regeneratorRuntime = undefined; } } - + + /* WEBPACK VAR INJECTION */}.call(exports, (function() { return this; }()))) /***/ }, -/* 54 */ -/***/ function(module, exports) { +/* 53 */ +/***/ function(module, exports, __webpack_require__) { - /** - * Copyright (c) 2014-present, Facebook, Inc. + /* WEBPACK VAR INJECTION */(function(global, process) {/** + * Copyright (c) 2014, Facebook, Inc. + * All rights reserved. * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. + * This source code is licensed under the BSD-style license found in the + * https://raw.github.com/facebook/regenerator/master/LICENSE file. An + * additional grant of patent rights can be found in the PATENTS file in + * the same directory. */ !(function(global) { @@ -4028,7 +4019,6 @@ return /******/ (function(modules) { // webpackBootstrap var undefined; // More compressible than void 0. var $Symbol = typeof Symbol === "function" ? Symbol : {}; var iteratorSymbol = $Symbol.iterator || "@@iterator"; - var asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator"; var toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag"; var inModule = typeof module === "object"; @@ -4202,6 +4192,10 @@ return /******/ (function(modules) { // webpackBootstrap } } + if (typeof process === "object" && process.domain) { + invoke = process.domain.bind(invoke); + } + var previousPromise; function enqueue(method, arg) { @@ -4238,9 +4232,6 @@ return /******/ (function(modules) { // webpackBootstrap } defineIteratorMethods(AsyncIterator.prototype); - AsyncIterator.prototype[asyncIteratorSymbol] = function () { - return this; - }; runtime.AsyncIterator = AsyncIterator; // Note that simple async functions are implemented on top of @@ -4424,15 +4415,6 @@ return /******/ (function(modules) { // webpackBootstrap Gp[toStringTagSymbol] = "Generator"; - // A Generator should always return itself as the iterator object when the - // @@iterator function is called on it. Some browsers' implementations of the - // iterator prototype chain incorrectly implement this, causing the Generator - // object to not be returned from this call. This ensures that doesn't happen. - // See https://github.com/facebook/regenerator/issues/274 for more details. - Gp[iteratorSymbol] = function() { - return this; - }; - Gp.toString = function() { return "[object Generator]"; }; @@ -4735,114 +4717,303 @@ return /******/ (function(modules) { // webpackBootstrap } }; })( - // In sloppy mode, unbound `this` refers to the global object, fallback to - // Function constructor if we're in global strict mode. That is sadly a form - // of indirect eval which violates Content Security Policy. - (function() { return this })() || Function("return this")() + // Among the various tricks for obtaining a reference to the global + // object, this seems to be the most reliable technique that does not + // use indirect eval (which violates Content Security Policy). + typeof global === "object" ? global : + typeof window === "object" ? window : + typeof self === "object" ? self : this ); - - -/***/ }, -/* 55 */ -/***/ function(module, exports) { - - 'use strict'; - - Object.defineProperty(exports, "__esModule", { - value: true - }); - exports.pickService = pickService; - // helper to serialize/deserialize an error for/from postMessage - var errorSerializer = exports.errorSerializer = function () { - function mapErrorProperties(from, to) { - var result = Object.assign(to, from); - var nativeProperties = ['name', 'message']; - return nativeProperties.reduce(function (result, property) { - if (from[property]) { - to[property] = from[property]; - } - return result; - }, result); - } - return { - serialize: function serialize(error) { - return mapErrorProperties(error, {}); - }, - deserialize: function deserialize(data) { - return mapErrorProperties(data, new Error(data.message)); - } - }; - }(); - var first = function first(arr) { - return arr && arr[0]; - }; - // In a far future, the user will have to pick the desired service from a list. - // For now it's our job, an easy job as we arbitrary pick the first service of - // the list. - function pickService(intent, filterServices) { - var services = intent.attributes.services; - var filteredServices = filterServices ? (services || []).filter(filterServices) : services; - return first(filteredServices); - } + /* WEBPACK VAR INJECTION */}.call(exports, (function() { return this; }()), __webpack_require__(54))) /***/ }, -/* 56 */ -/***/ function(module, exports, __webpack_require__) { +/* 54 */ +/***/ function(module, exports) { - 'use strict'; - - Object.defineProperty(exports, "__esModule", { - value: true - }); - - var _regenerator = __webpack_require__(52); - - var _regenerator2 = _interopRequireDefault(_regenerator); - - var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; - - exports.start = start; - - var _helpers = __webpack_require__(55); + // shim for using process in browser + var process = module.exports = {}; - var _ = __webpack_require__(51); + // cached from whatever global is present so that test runners that stub it + // don't break things. But we need to wrap it in a try catch in case it is + // wrapped in strict mode code which doesn't define any globals. It's inside a + // function because try/catches deoptimize in certain engines. - function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + var cachedSetTimeout; + var cachedClearTimeout; - function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } + function defaultSetTimout() { + throw new Error('setTimeout has not been defined'); + } + function defaultClearTimeout () { + throw new Error('clearTimeout has not been defined'); + } + (function () { + try { + if (typeof setTimeout === 'function') { + cachedSetTimeout = setTimeout; + } else { + cachedSetTimeout = defaultSetTimout; + } + } catch (e) { + cachedSetTimeout = defaultSetTimout; + } + try { + if (typeof clearTimeout === 'function') { + cachedClearTimeout = clearTimeout; + } else { + cachedClearTimeout = defaultClearTimeout; + } + } catch (e) { + cachedClearTimeout = defaultClearTimeout; + } + } ()) + function runTimeout(fun) { + if (cachedSetTimeout === setTimeout) { + //normal enviroments in sane situations + return setTimeout(fun, 0); + } + // if setTimeout wasn't available but was latter defined + if ((cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) && setTimeout) { + cachedSetTimeout = setTimeout; + return setTimeout(fun, 0); + } + try { + // when when somebody has screwed with setTimeout but no I.E. maddness + return cachedSetTimeout(fun, 0); + } catch(e){ + try { + // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally + return cachedSetTimeout.call(null, fun, 0); + } catch(e){ + // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error + return cachedSetTimeout.call(this, fun, 0); + } + } - var intentClass = 'coz-intent'; - function hideIntentIframe(iframe) { - iframe.style.display = 'none'; } + function runClearTimeout(marker) { + if (cachedClearTimeout === clearTimeout) { + //normal enviroments in sane situations + return clearTimeout(marker); + } + // if clearTimeout wasn't available but was latter defined + if ((cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) && clearTimeout) { + cachedClearTimeout = clearTimeout; + return clearTimeout(marker); + } + try { + // when when somebody has screwed with setTimeout but no I.E. maddness + return cachedClearTimeout(marker); + } catch (e){ + try { + // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally + return cachedClearTimeout.call(null, marker); + } catch (e){ + // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error. + // Some versions of I.E. have different rules for clearTimeout vs setTimeout + return cachedClearTimeout.call(this, marker); + } + } - function showIntentFrame(iframe) { - iframe.style.display = 'block'; - } - function buildIntentIframe(intent, element, url) { - var document = element.ownerDocument; - if (!document) return Promise.reject(new Error('Cannot retrieve document object from given element')); - var iframe = document.createElement('iframe'); - // TODO: implement 'title' attribute - iframe.setAttribute('id', 'intent-' + intent._id); - iframe.setAttribute('src', url); - iframe.classList.add(intentClass); - return iframe; } + var queue = []; + var draining = false; + var currentQueue; + var queueIndex = -1; - function injectIntentIframe(intent, element, url, options) { - var onReadyCallback = options.onReadyCallback; - - var iframe = buildIntentIframe(intent, element, url, options.onReadyCallback); - // if callback provided for when iframe is loaded - if (typeof onReadyCallback === 'function') iframe.onload = onReadyCallback; - element.appendChild(iframe); - iframe.focus(); - return iframe; + function cleanUpNextTick() { + if (!draining || !currentQueue) { + return; + } + draining = false; + if (currentQueue.length) { + queue = currentQueue.concat(queue); + } else { + queueIndex = -1; + } + if (queue.length) { + drainQueue(); + } + } + + function drainQueue() { + if (draining) { + return; + } + var timeout = runTimeout(cleanUpNextTick); + draining = true; + + var len = queue.length; + while(len) { + currentQueue = queue; + queue = []; + while (++queueIndex < len) { + if (currentQueue) { + currentQueue[queueIndex].run(); + } + } + queueIndex = -1; + len = queue.length; + } + currentQueue = null; + draining = false; + runClearTimeout(timeout); + } + + process.nextTick = function (fun) { + var args = new Array(arguments.length - 1); + if (arguments.length > 1) { + for (var i = 1; i < arguments.length; i++) { + args[i - 1] = arguments[i]; + } + } + queue.push(new Item(fun, args)); + if (queue.length === 1 && !draining) { + runTimeout(drainQueue); + } + }; + + // v8 likes predictible objects + function Item(fun, array) { + this.fun = fun; + this.array = array; + } + Item.prototype.run = function () { + this.fun.apply(null, this.array); + }; + process.title = 'browser'; + process.browser = true; + process.env = {}; + process.argv = []; + process.version = ''; // empty string to avoid regexp issues + process.versions = {}; + + 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'); + }; + + process.cwd = function () { return '/' }; + process.chdir = function (dir) { + throw new Error('process.chdir is not supported'); + }; + process.umask = function() { return 0; }; + + +/***/ }, +/* 55 */ +/***/ function(module, exports) { + + 'use strict'; + + Object.defineProperty(exports, "__esModule", { + value: true + }); + exports.pickService = pickService; + // helper to serialize/deserialize an error for/from postMessage + var errorSerializer = exports.errorSerializer = function () { + function mapErrorProperties(from, to) { + var result = Object.assign(to, from); + var nativeProperties = ['name', 'message']; + return nativeProperties.reduce(function (result, property) { + if (from[property]) { + to[property] = from[property]; + } + return result; + }, result); + } + return { + serialize: function serialize(error) { + return mapErrorProperties(error, {}); + }, + deserialize: function deserialize(data) { + return mapErrorProperties(data, new Error(data.message)); + } + }; + }(); + + var first = function first(arr) { + return arr && arr[0]; + }; + // In a far future, the user will have to pick the desired service from a list. + // For now it's our job, an easy job as we arbitrary pick the first service of + // the list. + function pickService(intent, filterServices) { + var services = intent.attributes.services; + var filteredServices = filterServices ? (services || []).filter(filterServices) : services; + return first(filteredServices); + } + +/***/ }, +/* 56 */ +/***/ function(module, exports, __webpack_require__) { + + 'use strict'; + + Object.defineProperty(exports, "__esModule", { + value: true + }); + + var _regenerator = __webpack_require__(51); + + var _regenerator2 = _interopRequireDefault(_regenerator); + + var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; + + exports.start = start; + + var _helpers = __webpack_require__(55); + + var _ = __webpack_require__(50); + + function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + + function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } + + var intentClass = 'coz-intent'; + + function hideIntentIframe(iframe) { + iframe.style.display = 'none'; + } + + function showIntentFrame(iframe) { + iframe.style.display = 'block'; + } + + function buildIntentIframe(intent, element, url) { + var document = element.ownerDocument; + if (!document) return Promise.reject(new Error('Cannot retrieve document object from given element')); + + var iframe = document.createElement('iframe'); + // TODO: implement 'title' attribute + iframe.setAttribute('id', 'intent-' + intent._id); + iframe.setAttribute('src', url); + iframe.classList.add(intentClass); + return iframe; + } + + function injectIntentIframe(intent, element, url, options) { + var onReadyCallback = options.onReadyCallback; + + var iframe = buildIntentIframe(intent, element, url, options.onReadyCallback); + // if callback provided for when iframe is loaded + if (typeof onReadyCallback === 'function') iframe.onload = onReadyCallback; + element.appendChild(iframe); + iframe.focus(); + return iframe; } // inject iframe for service in given element @@ -5057,7 +5228,7 @@ return /******/ (function(modules) { // webpackBootstrap }); exports.start = start; - var _fetch = __webpack_require__(45); + var _fetch = __webpack_require__(44); var _helpers = __webpack_require__(55); @@ -5199,7 +5370,7 @@ return /******/ (function(modules) { // webpackBootstrap exports.queued = queued; exports.create = create; - var _fetch = __webpack_require__(45); + var _fetch = __webpack_require__(44); function count(cozy, workerType) { return (0, _fetch.cozyFetchJSON)(cozy, 'GET', '/jobs/queue/' + workerType).then(function (data) { @@ -5250,17 +5421,17 @@ return /******/ (function(modules) { // webpackBootstrap exports.stopRepeatedReplication = stopRepeatedReplication; exports.stopAllRepeatedReplication = stopAllRepeatedReplication; - var _doctypes = __webpack_require__(48); + var _doctypes = __webpack_require__(47); - var _auth_v = __webpack_require__(44); + var _auth_v = __webpack_require__(43); - var _utils = __webpack_require__(41); + var _utils = __webpack_require__(40); var _pouchdb = __webpack_require__(60); var _pouchdb2 = _interopRequireDefault(_pouchdb); - var _pouchdbFind = __webpack_require__(77); + var _pouchdbFind = __webpack_require__(72); var _pouchdbFind2 = _interopRequireDefault(_pouchdbFind); @@ -5565,16 +5736,16 @@ return /******/ (function(modules) { // webpackBootstrap var lie = _interopDefault(__webpack_require__(61)); var getArguments = _interopDefault(__webpack_require__(63)); + var debug = _interopDefault(__webpack_require__(64)); + var events = __webpack_require__(67); + var inherits = _interopDefault(__webpack_require__(68)); var nextTick = _interopDefault(__webpack_require__(62)); - var events = __webpack_require__(64); - var inherits = _interopDefault(__webpack_require__(65)); - var uuidV4 = _interopDefault(__webpack_require__(66)); - var debug = _interopDefault(__webpack_require__(71)); - var Md5 = _interopDefault(__webpack_require__(75)); - var vuvuzela = _interopDefault(__webpack_require__(76)); + var scopedEval = _interopDefault(__webpack_require__(69)); + var Md5 = _interopDefault(__webpack_require__(70)); + var vuvuzela = _interopDefault(__webpack_require__(71)); /* istanbul ignore next */ - var PouchPromise = typeof Promise === 'function' ? Promise : lie; + var PouchPromise$1 = typeof Promise === 'function' ? Promise : lie; function isBinaryObject(object) { return (typeof ArrayBuffer !== 'undefined' && object instanceof ArrayBuffer) || @@ -5691,7 +5862,7 @@ return /******/ (function(modules) { // webpackBootstrap var self = this; // if the last argument is a function, assume its a callback var usedCB = (typeof args[args.length - 1] === 'function') ? args.pop() : false; - var promise = new PouchPromise(function (fulfill, reject) { + var promise = new PouchPromise$1(function (fulfill, reject) { var resp; try { var callback = once(function (err, mesg) { @@ -5722,40 +5893,42 @@ return /******/ (function(modules) { // webpackBootstrap }); } - function logApiCall(self, name, args) { - /* istanbul ignore if */ - if (self.constructor.listeners('debug').length) { - var logArgs = ['api', self.name, name]; - for (var i = 0; i < args.length - 1; i++) { - logArgs.push(args[i]); - } - self.constructor.emit('debug', logArgs); - - // override the callback itself to log the response - var origCallback = args[args.length - 1]; - args[args.length - 1] = function (err, res) { - var responseArgs = ['api', self.name, name]; - responseArgs = responseArgs.concat( - err ? ['error', err] : ['success', res] - ); - self.constructor.emit('debug', responseArgs); - origCallback(err, res); - }; - } - } + var log = debug('pouchdb:api'); function adapterFun(name, callback) { + function logApiCall(self, name, args) { + /* istanbul ignore if */ + if (log.enabled) { + var logArgs = [self.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.name, name]; + responseArgs = responseArgs.concat( + err ? ['error', err] : ['success', res] + ); + log.apply(null, responseArgs); + origCallback(err, res); + }; + } + } + return toPromise(getArguments(function (args) { if (this._closed) { - return PouchPromise.reject(new Error('database is closed')); + return PouchPromise$1.reject(new Error('database is closed')); } if (this._destroyed) { - return PouchPromise.reject(new Error('database is destroyed')); + return PouchPromise$1.reject(new Error('database is destroyed')); } var self = this; logApiCall(self, name, args); if (!this.taskqueue.isReady) { - return new PouchPromise(function (fulfill, reject) { + return new PouchPromise$1(function (fulfill, reject) { self.taskqueue.addTask(function (failed) { if (failed) { reject(failed); @@ -5769,6 +5942,18 @@ return /******/ (function(modules) { // webpackBootstrap })); } + // like underscore/lodash _.pick() + function pick(obj, arr) { + var res = {}; + for (var i = 0, len = arr.length; i < len; i++) { + var prop = arr[i]; + if (prop in obj) { + res[prop] = obj[prop]; + } + } + return res; + } + function mangle(key) { return '$' + key; } @@ -5867,18 +6052,6 @@ return /******/ (function(modules) { // webpackBootstrap } } - // like underscore/lodash _.pick() - function pick(obj, arr) { - var res = {}; - for (var i = 0, len = arr.length; i < len; i++) { - var prop = arr[i]; - if (prop in obj) { - res[prop] = obj[prop]; - } - } - return res; - } - // Most browsers throttle concurrent requests at 6, so it's silly // to shim _bulk_get by trying to launch potentially hundreds of requests // and then letting the majority time out. We can handle this ourselves. @@ -6033,18 +6206,6 @@ return /******/ (function(modules) { // webpackBootstrap return hasLocal; } - // Custom nextTick() shim for browsers. In node, this will just be process.nextTick(). We - // avoid using process.nextTick() directly because the polyfill is very large and we don't - // need all of it (see: https://github.com/defunctzombie/node-process). - // "immediate" 3.0.8 is used by lie, and it's a smaller version of the latest "immediate" - // package, so it's the one we use. - // When we use nextTick() in our codebase, we only care about not releasing Zalgo - // (see: http://blog.izs.me/post/59142742143/designing-apis-for-asynchrony). - // Microtask vs macrotask doesn't matter to us. So we're free to use the fastest - // (least latency) option, which is "immediate" due to use of microtasks. - // All of our nextTicks are isolated to this one function so we can easily swap out one - // implementation for another. - inherits(Changes, events.EventEmitter); /* istanbul ignore next */ @@ -6148,7 +6309,7 @@ return /******/ (function(modules) { // webpackBootstrap function guardedConsole(method) { /* istanbul ignore else */ - if (typeof console !== 'undefined' && typeof console[method] === 'function') { + if (console !== 'undefined' && method in console) { var args = Array.prototype.slice.call(arguments, 1); console[method].apply(console, args); } @@ -6164,7 +6325,7 @@ return /******/ (function(modules) { // webpackBootstrap max = max + 1; } // In order to not exceed maxTimeout, pick a random value between half of maxTimeout and maxTimeout - if (max > maxTimeout) { + if(max > maxTimeout) { min = maxTimeout >> 1; // divide by two max = maxTimeout; } @@ -6215,7 +6376,7 @@ return /******/ (function(modules) { // webpackBootstrap } } - var $inject_Object_assign = assign; + var assign$1 = assign; inherits(PouchError, Error); @@ -6365,6 +6526,22 @@ return /******/ (function(modules) { // webpackBootstrap // for browsers that don't support it like IE /* istanbul ignore next */ + function f() {} + + var hasName = f.name; + var res; + + // We dont run coverage in IE + /* istanbul ignore else */ + if (hasName) { + res = function (fun) { + return fun.name; + }; + } else { + res = function (fun) { + return fun.toString().match(/^\s*function\s*(\S*)\s*\(/)[1]; + }; + } // Determine id an ID is valid // - invalid IDs begin with an underescore that does not begin '_design' or @@ -6385,44 +6562,31 @@ return /******/ (function(modules) { // webpackBootstrap } } - // Checks if a PouchDB object is "remote" or not. This is - // designed to opt-in to certain optimizations, such as - // avoiding checks for "dependentDbs" and other things that - // we know only apply to local databases. In general, "remote" - // should be true for the http adapter, and for third-party - // adapters with similar expensive boundaries to cross for - // every API call, such as socket-pouch and worker-pouch. - // Previously, this was handled via db.type() === 'http' - // which is now deprecated. - - function isRemote(db) { - if (typeof db._remote === 'boolean') { - return db._remote; - } - /* istanbul ignore next */ - if (typeof db.type === 'function') { - guardedConsole('warn', - 'db.type() is deprecated and will be removed in ' + - 'a future version of PouchDB'); - return db.type() === 'http'; - } - /* istanbul ignore next */ - return false; - } - function listenerCount(ee, type) { return 'listenerCount' in ee ? ee.listenerCount(type) : events.EventEmitter.listenerCount(ee, type); } - function parseDesignDocFunctionName(s) { - if (!s) { - return null; - } - var parts = s.split('/'); - if (parts.length === 2) { - return parts; - } + // Custom nextTick() shim for browsers. In node, this will just be process.nextTick(). We + // avoid using process.nextTick() directly because the polyfill is very large and we don't + // need all of it (see: https://github.com/defunctzombie/node-process). + // "immediate" 3.0.8 is used by lie, and it's a smaller version of the latest "immediate" + // package, so it's the one we use. + // When we use nextTick() in our codebase, we only care about not releasing Zalgo + // (see: http://blog.izs.me/post/59142742143/designing-apis-for-asynchrony). + // Microtask vs macrotask doesn't matter to us. So we're free to use the fastest + // (least latency) option, which is "immediate" due to use of microtasks. + // All of our nextTicks are isolated to this one function so we can easily swap out one + // implementation for another. + + function parseDesignDocFunctionName(s) { + if (!s) { + return null; + } + var parts = s.split('/'); + if (parts.length === 2) { + return parts; + } if (parts.length === 1) { return [s, s]; } @@ -6443,7 +6607,7 @@ return /******/ (function(modules) { // webpackBootstrap var qParser = /(?:^|&)([^&=]*)=?([^&]*)/g; // use the "loose" parser - /* eslint maxlen: 0, no-useless-escape: 0 */ + /* jshint maxlen: false */ var parser = /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/; function parseUri(str) { @@ -6468,28 +6632,11 @@ return /******/ (function(modules) { // webpackBootstrap return uri; } - // Based on https://github.com/alexdavid/scope-eval v0.0.3 - // (source: https://unpkg.com/scope-eval@0.0.3/scope_eval.js) - // This is basically just a wrapper around new Function() - - function scopeEval(source, scope) { - var keys = []; - var values = []; - for (var key in scope) { - if (scope.hasOwnProperty(key)) { - keys.push(key); - values.push(scope[key]); - } - } - keys.push(source); - return Function.apply(null, keys).apply(null, values); - } - // this is essentially the "update sugar" function from daleharvey/pouchdb#1388 // the diffFun tells us what delta to apply to the doc. it either returns // the doc, or false if it doesn't need to do an update after all function upsert(db, docId, diffFun) { - return new PouchPromise(function (fulfill, reject) { + return new PouchPromise$1(function (fulfill, reject) { db.get(docId, function (err, doc) { if (err) { /* istanbul ignore next */ @@ -6533,11 +6680,82 @@ return /******/ (function(modules) { // webpackBootstrap }); } - function rev() { - return uuidV4.v4().replace(/-/g, '').toLowerCase(); - } + // BEGIN Math.uuid.js + + /*! + Math.uuid.js (v1.4) + http://www.broofa.com + mailto:robert@broofa.com + + 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)]; + } + } - var uuid = uuidV4.v4; + return out; + } // 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 @@ -6852,7 +7070,7 @@ return /******/ (function(modules) { // webpackBootstrap function stem(tree, depth) { // First we break out the tree into a complete list of root to leaf paths var paths = rootToLeaf(tree); - var stemmedRevs; + var maybeStem = {}; var result; for (var i = 0, len = paths.length; i < len; i++) { @@ -6860,49 +7078,34 @@ return /******/ (function(modules) { // webpackBootstrap // `depth` to stem to, and generate a new set of flat trees var path = paths[i]; var stemmed = path.ids; - var node; - if (stemmed.length > depth) { - // only do the stemming work if we actually need to stem - if (!stemmedRevs) { - stemmedRevs = {}; // avoid allocating this object unnecessarily - } - var numStemmed = stemmed.length - depth; - node = { - pos: path.pos + numStemmed, - ids: pathToTree(stemmed, numStemmed) - }; + var numStemmed = Math.max(0, stemmed.length - depth); + var stemmedNode = { + pos: path.pos + numStemmed, + ids: pathToTree(stemmed, numStemmed) + }; - for (var s = 0; s < numStemmed; s++) { - var rev = (path.pos + s) + '-' + stemmed[s].id; - stemmedRevs[rev] = true; - } - } else { // no need to actually stem - node = { - pos: path.pos, - ids: pathToTree(stemmed, 0) - }; + for (var s = 0; s < numStemmed; s++) { + var rev = (path.pos + s) + '-' + stemmed[s].id; + maybeStem[rev] = true; } // Then we remerge all those flat trees together, ensuring that we dont // connect trees that would go beyond the depth limit if (result) { - result = doMerge(result, node, true).tree; + result = doMerge(result, stemmedNode, true).tree; } else { - result = [node]; + result = [stemmedNode]; } } - // this is memory-heavy per Chrome profiler, avoid unless we actually stemmed - if (stemmedRevs) { - traverseRevTree(result, function (isLeaf, pos, revHash) { - // some revisions may have been removed in a branch but not in another - delete stemmedRevs[pos + '-' + revHash]; - }); - } + traverseRevTree(result, function (isLeaf, pos, revHash) { + // some revisions may have been removed in a branch but not in another + delete maybeStem[pos + '-' + revHash]; + }); return { tree: result, - revs: stemmedRevs ? Object.keys(stemmedRevs) : [] + revs: Object.keys(maybeStem) }; } @@ -6999,12 +7202,35 @@ return /******/ (function(modules) { // webpackBootstrap throw new Error('Unable to resolve latest revision for id ' + metadata.id + ', rev ' + rev); } + function evalFilter(input) { + return scopedEval('"use strict";\nreturn ' + input + ';', {}); + } + + function evalView(input) { + var code = [ + 'return function(doc) {', + ' "use strict";', + ' var emitted = false;', + ' var emit = function (a, b) {', + ' emitted = true;', + ' };', + ' var view = ' + input + ';', + ' view(doc);', + ' if (emitted) {', + ' return true;', + ' }', + '};' + ].join('\n'); + + return scopedEval(code, {}); + } + inherits(Changes$2, events.EventEmitter); - function tryCatchInChangeListener(self, change, pending, lastSeq) { + function tryCatchInChangeListener(self, change) { // isolate try/catches to avoid V8 deoptimizations try { - self.emit('change', change, pending, lastSeq); + self.emit('change', change); } catch (e) { guardedConsole('error', 'Error in .on("change", function):', e); } @@ -7037,15 +7263,15 @@ return /******/ (function(modules) { // webpackBootstrap } db.once('destroyed', onDestroy); - opts.onChange = function (change, pending, lastSeq) { + opts.onChange = function (change) { /* istanbul ignore if */ if (self.isCancelled) { return; } - tryCatchInChangeListener(self, change, pending, lastSeq); + tryCatchInChangeListener(self, change); }; - var promise = new PouchPromise(function (fulfill, reject) { + var promise = new PouchPromise$1(function (fulfill, reject) { opts.complete = function (err, res) { if (err) { reject(err); @@ -7073,11 +7299,11 @@ return /******/ (function(modules) { // webpackBootstrap } else if (self.isCancelled) { self.emit('cancel'); } else { - self.validateChanges(opts); + self.doChanges(opts); } }); } else { - self.validateChanges(opts); + self.doChanges(opts); } } Changes$2.prototype.cancel = function () { @@ -7110,23 +7336,6 @@ return /******/ (function(modules) { // webpackBootstrap return change; } - Changes$2.prototype.validateChanges = function (opts) { - var callback = opts.complete; - var self = this; - - /* istanbul ignore else */ - if (PouchDB._changesFilterPlugin) { - PouchDB._changesFilterPlugin.validate(opts, function (err) { - if (err) { - return callback(err); - } - self.doChanges(opts); - }); - } else { - self.doChanges(opts); - } - }; - Changes$2.prototype.doChanges = function (opts) { var self = this; var callback = opts.complete; @@ -7156,22 +7365,21 @@ return /******/ (function(modules) { // webpackBootstrap return; } - /* istanbul ignore else */ - if (PouchDB._changesFilterPlugin) { - PouchDB._changesFilterPlugin.normalize(opts); - if (PouchDB._changesFilterPlugin.shouldFilter(this, opts)) { - return PouchDB._changesFilterPlugin.filter(this, opts); + + if (opts.view && !opts.filter) { + opts.filter = '_view'; + } + + if (opts.filter && typeof opts.filter === 'string') { + if (opts.filter === '_view') { + opts.view = normalizeDesignDocFunctionName(opts.view); + } else { + opts.filter = normalizeDesignDocFunctionName(opts.filter); + } + + if (this.db.type() !== 'http' && !opts.doc_ids) { + return this.filterChanges(opts); } - } else { - ['doc_ids', 'filter', 'selector', 'view'].forEach(function (key) { - if (key in opts) { - guardedConsole('warn', - 'The "' + key + '" option was passed in to changes/replicate, ' + - 'but pouchdb-changes-filter plugin is not installed, so it ' + - 'was ignored. Please install the plugin to enable filtering.' - ); - } - }); } if (!('descending' in opts)) { @@ -7192,6 +7400,63 @@ return /******/ (function(modules) { // webpackBootstrap } }; + Changes$2.prototype.filterChanges = function (opts) { + var self = this; + var callback = opts.complete; + if (opts.filter === '_view') { + if (!opts.view || typeof opts.view !== 'string') { + var err = createError(BAD_REQUEST, + '`view` filter parameter not found or invalid.'); + return callback(err); + } + // fetch a view from a design doc, make it behave like a filter + var viewName = parseDesignDocFunctionName(opts.view); + this.db.get('_design/' + viewName[0], function (err, ddoc) { + /* istanbul ignore if */ + if (self.isCancelled) { + return callback(null, {status: 'cancelled'}); + } + /* istanbul ignore next */ + if (err) { + return callback(generateErrorFromResponse(err)); + } + var mapFun = ddoc && ddoc.views && ddoc.views[viewName[1]] && + ddoc.views[viewName[1]].map; + if (!mapFun) { + return callback(createError(MISSING_DOC, + (ddoc.views ? 'missing json key: ' + viewName[1] : + 'missing json key: views'))); + } + opts.filter = evalView(mapFun); + self.doChanges(opts); + }); + } else { + // fetch a filter from a design doc + var filterName = parseDesignDocFunctionName(opts.filter); + if (!filterName) { + return self.doChanges(opts); + } + this.db.get('_design/' + filterName[0], function (err, ddoc) { + /* istanbul ignore if */ + if (self.isCancelled) { + return callback(null, {status: 'cancelled'}); + } + /* istanbul ignore next */ + if (err) { + return callback(generateErrorFromResponse(err)); + } + var filterFun = ddoc && ddoc.filters && ddoc.filters[filterName[1]]; + if (!filterFun) { + return callback(createError(MISSING_DOC, + ((ddoc && ddoc.filters) ? 'missing json key: ' + filterName[1] + : 'missing json key: filters'))); + } + opts.filter = evalFilter(filterFun); + self.doChanges(opts); + }); + } + }; + /* * A generic pouch adapter */ @@ -7202,12 +7467,10 @@ return /******/ (function(modules) { // webpackBootstrap // Wrapper for functions that call the bulkdocs api with a single doc, // if the first result is an error, return an error - function yankError(callback, docId) { + function yankError(callback) { return function (err, results) { if (err || (results[0] && results[0].error)) { - err = err || results[0]; - err.docId = docId; - callback(err); + callback(err || results[0]); } else { callback(null, results.length ? results[0] : results); } @@ -7249,14 +7512,14 @@ return /******/ (function(modules) { // webpackBootstrap var height = {}; var edges = []; traverseRevTree(revs, function (isLeaf, pos, id, prnt) { - var rev$$1 = pos + "-" + id; + var rev = pos + "-" + id; if (isLeaf) { - height[rev$$1] = 0; + height[rev] = 0; } if (prnt !== undefined) { - edges.push({from: prnt, to: rev$$1}); + edges.push({from: prnt, to: rev}); } - return rev$$1; + return rev; }); edges.reverse(); @@ -7270,17 +7533,38 @@ return /******/ (function(modules) { // webpackBootstrap return height; } - function allDocsKeysParse(opts) { + function allDocsKeysQuery(api, opts, callback) { var keys = ('limit' in opts) ? - opts.keys.slice(opts.skip, opts.limit + opts.skip) : - (opts.skip > 0) ? opts.keys.slice(opts.skip) : opts.keys; - opts.keys = keys; - opts.skip = 0; - delete opts.limit; + opts.keys.slice(opts.skip, opts.limit + opts.skip) : + (opts.skip > 0) ? opts.keys.slice(opts.skip) : opts.keys; if (opts.descending) { keys.reverse(); - opts.descending = false; } + if (!keys.length) { + return api._allDocs({limit: 0}, callback); + } + var finalResults = { + offset: opts.skip + }; + return PouchPromise$1.all(keys.map(function (key) { + var subOpts = assign$1({key: key, deleted: 'ok'}, opts); + ['limit', 'skip', 'keys'].forEach(function (optKey) { + delete subOpts[optKey]; + }); + return new PouchPromise$1(function (resolve, reject) { + api._allDocs(subOpts, function (err, res) { + /* istanbul ignore if */ + 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; + }); } // all compaction is done in a queue, to avoid attaching @@ -7314,7 +7598,7 @@ return /******/ (function(modules) { // webpackBootstrap function attachmentNameError(name) { if (name.charAt(0) === '_') { - return name + ' is not a valid attachment name, attachment ' + + return name + 'is not a valid attachment name, attachment ' + 'names cannot start with \'_\''; } return false; @@ -7335,7 +7619,7 @@ return /******/ (function(modules) { // webpackBootstrap if (typeof doc !== 'object' || Array.isArray(doc)) { return callback(createError(NOT_AN_OBJECT)); } - this.bulkDocs({docs: [doc]}, opts, yankError(callback, doc._id)); + this.bulkDocs({docs: [doc]}, opts, yankError(callback)); }); AbstractPouchDB.prototype.put = adapterFun('put', function (doc, opts, cb) { @@ -7354,56 +7638,28 @@ return /******/ (function(modules) { // webpackBootstrap return this._putLocal(doc, cb); } } - var self = this; - if (opts.force && doc._rev) { - transformForceOptionToNewEditsOption(); - putDoc(function (err) { - var result = err ? null : {ok: true, id: doc._id, rev: doc._rev}; - cb(err, result); - }); + if (typeof this._put === 'function' && opts.new_edits !== false) { + this._put(doc, opts, cb); } else { - putDoc(cb); - } - - function transformForceOptionToNewEditsOption() { - var parts = doc._rev.split('-'); - var oldRevId = parts[1]; - var oldRevNum = parseInt(parts[0], 10); - - var newRevNum = oldRevNum + 1; - var newRevId = rev(); - - doc._revisions = { - start: newRevNum, - ids: [newRevId, oldRevId] - }; - doc._rev = newRevNum + '-' + newRevId; - opts.new_edits = false; - } - function putDoc(next) { - if (typeof self._put === 'function' && opts.new_edits !== false) { - self._put(doc, opts, next); - } else { - self.bulkDocs({docs: [doc]}, opts, yankError(next, doc._id)); - } + this.bulkDocs({docs: [doc]}, opts, yankError(cb)); } }); AbstractPouchDB.prototype.putAttachment = - adapterFun('putAttachment', function (docId, attachmentId, rev$$1, + adapterFun('putAttachment', function (docId, attachmentId, rev, blob, type) { var api = this; if (typeof type === 'function') { type = blob; - blob = rev$$1; - rev$$1 = null; + blob = rev; + rev = null; } // Lets fix in https://github.com/pouchdb/pouchdb/issues/3267 /* istanbul ignore if */ if (typeof type === 'undefined') { type = blob; - blob = rev$$1; - rev$$1 = null; + blob = rev; + rev = null; } if (!type) { guardedConsole('warn', 'Attachment', attachmentId, 'on document', docId, 'is missing content_type'); @@ -7421,7 +7677,7 @@ return /******/ (function(modules) { // webpackBootstrap } return api.get(docId).then(function (doc) { - if (doc._rev !== rev$$1) { + if (doc._rev !== rev) { throw createError(REV_CONFLICT); } @@ -7438,7 +7694,7 @@ return /******/ (function(modules) { // webpackBootstrap }); AbstractPouchDB.prototype.removeAttachment = - adapterFun('removeAttachment', function (docId, attachmentId, rev$$1, + adapterFun('removeAttachment', function (docId, attachmentId, rev, callback) { var self = this; self.get(docId, function (err, obj) { @@ -7447,7 +7703,7 @@ return /******/ (function(modules) { // webpackBootstrap callback(err); return; } - if (obj._rev !== rev$$1) { + if (obj._rev !== rev) { callback(createError(REV_CONFLICT)); return; } @@ -7494,7 +7750,7 @@ return /******/ (function(modules) { // webpackBootstrap if (isLocalId(newDoc._id) && typeof this._removeLocal === 'function') { return this._removeLocal(doc, callback); } - this.bulkDocs({docs: [newDoc]}, opts, yankError(callback, newDoc._id)); + this.bulkDocs({docs: [newDoc]}, opts, yankError(callback)); }); AbstractPouchDB.prototype.revsDiff = @@ -7524,8 +7780,8 @@ return /******/ (function(modules) { // webpackBootstrap var missingForId = req[id].slice(0); traverseRevTree(rev_tree, function (isLeaf, pos, revHash, ctx, opts) { - var rev$$1 = pos + '-' + revHash; - var idx = missingForId.indexOf(rev$$1); + var rev = pos + '-' + revHash; + var idx = missingForId.indexOf(rev); if (idx === -1) { return; } @@ -7533,14 +7789,14 @@ return /******/ (function(modules) { // webpackBootstrap missingForId.splice(idx, 1); /* istanbul ignore if */ if (opts.status !== 'available') { - addToMissing(id, rev$$1); + addToMissing(id, rev); } }); // Traversing the tree is synchronous, so now `missingForId` contains // revisions that were not found in the tree - missingForId.forEach(function (rev$$1) { - addToMissing(id, rev$$1); + missingForId.forEach(function (rev) { + addToMissing(id, rev); }); } @@ -7593,16 +7849,16 @@ return /******/ (function(modules) { // webpackBootstrap var height = computeHeight(revTree); var candidates = []; var revs = []; - Object.keys(height).forEach(function (rev$$1) { - if (height[rev$$1] > maxHeight) { - candidates.push(rev$$1); + Object.keys(height).forEach(function (rev) { + if (height[rev] > maxHeight) { + candidates.push(rev); } }); traverseRevTree(revTree, function (isLeaf, pos, revHash, ctx, opts) { - var rev$$1 = pos + '-' + revHash; - if (opts.status === 'available' && candidates.indexOf(rev$$1) !== -1) { - revs.push(rev$$1); + var rev = pos + '-' + revHash; + if (opts.status === 'available' && candidates.indexOf(rev) !== -1) { + revs.push(rev); } }); self._doCompaction(docId, revs, callback); @@ -7640,7 +7896,7 @@ return /******/ (function(modules) { // webpackBootstrap } function onComplete(resp) { var lastSeq = resp.last_seq; - PouchPromise.all(promises).then(function () { + PouchPromise$1.all(promises).then(function () { return upsert(self, '_local/compaction', function deltaFunc(doc) { if (!doc.last_seq || doc.last_seq < lastSeq) { doc.last_seq = lastSeq; @@ -7687,8 +7943,7 @@ return /******/ (function(modules) { // webpackBootstrap rev: leaf, revs: opts.revs, latest: opts.latest, - attachments: opts.attachments, - binary: opts.binary + attachments: opts.attachments }, function (err, doc) { if (!err) { // using latest=true can produce duplicates @@ -7744,7 +7999,6 @@ return /******/ (function(modules) { // webpackBootstrap return this._get(id, opts, function (err, result) { if (err) { - err.docId = id; return cb(err); } @@ -7791,18 +8045,18 @@ return /******/ (function(modules) { // webpackBootstrap if (opts.revs) { doc._revisions = { start: (path.pos + path.ids.length) - 1, - ids: path.ids.map(function (rev$$1) { - return rev$$1.id; + 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$$1) { + doc._revs_info = path.ids.map(function (rev) { pos--; return { - rev: pos + '-' + rev$$1.id, - status: rev$$1.opts.status + rev: pos + '-' + rev.id, + status: rev.opts.status }; }); } @@ -7899,11 +8153,8 @@ return /******/ (function(modules) { // webpackBootstrap )); return; } - if (!isRemote(this)) { - allDocsKeysParse(opts); - if (opts.keys.length === 0) { - return this._allDocs({limit: 0}, callback); - } + if (this.type() !== 'http') { + return allDocsKeysQuery(this, opts, callback); } } @@ -7932,8 +8183,8 @@ return /******/ (function(modules) { // webpackBootstrap } // assume we know better than the adapter, unless it informs us info.db_name = info.db_name || self.name; - info.auto_compaction = !!(self.auto_compaction && !isRemote(self)); - info.adapter = self.adapter; + info.auto_compaction = !!(self.auto_compaction && self.type() !== 'http'); + info.adapter = self.type(); callback(null, info); }); }); @@ -7997,7 +8248,7 @@ return /******/ (function(modules) { // webpackBootstrap } var adapter = this; - if (!opts.new_edits && !isRemote(adapter)) { + if (!opts.new_edits && adapter.type() !== 'http') { // ensure revisions of the same doc are sorted, so that // the local adapter processes them correctly (#2935) req.docs.sort(compareByIdThenRev); @@ -8023,7 +8274,7 @@ return /******/ (function(modules) { // webpackBootstrap }); } // add ids for error/conflict responses (not required for CouchDB) - if (!isRemote(adapter)) { + if (adapter.type() !== 'http') { for (var i = 0, l = res.length; i < l; i++) { res[i].id = res[i].id || ids[i]; } @@ -8075,7 +8326,7 @@ return /******/ (function(modules) { // webpackBootstrap }); } - if (isRemote(self)) { + if (self.type() === 'http') { // no need to check for dependent DBs if it's a remote DB return destroyDb(); } @@ -8098,7 +8349,7 @@ return /******/ (function(modules) { // webpackBootstrap name.replace(new RegExp('^' + PouchDB.prefix), '') : name; return new PouchDB(trueName, self.__opts).destroy(); }); - PouchPromise.all(deletedMap).then(destroyDb, callback); + PouchPromise$1.all(deletedMap).then(destroyDb, callback); }); }); @@ -8140,7 +8391,7 @@ return /******/ (function(modules) { // webpackBootstrap }; function parseAdapter(name, opts) { - var match = name.match(/([a-z-]*):\/\/(.*)/); + var match = name.match(/([a-z\-]*):\/\/(.*)/); if (match) { // the http adapter expects the fully qualified name return { @@ -8149,9 +8400,9 @@ return /******/ (function(modules) { // webpackBootstrap }; } - var adapters = PouchDB.adapters; - var preferredAdapters = PouchDB.preferredAdapters; - var prefix = PouchDB.prefix; + var adapters = PouchDB$5.adapters; + var preferredAdapters = PouchDB$5.preferredAdapters; + var prefix = PouchDB$5.prefix; var adapterName = opts.adapter; if (!adapterName) { // automatically determine adapter @@ -8194,29 +8445,40 @@ return /******/ (function(modules) { // webpackBootstrap // that may have been created with the same name. function prepareForDestruction(self) { - function onDestroyed(from_constructor) { + var destructionListeners = self.constructor._destructionListeners; + + function onDestroyed() { self.removeListener('closed', onClosed); - if (!from_constructor) { - self.constructor.emit('destroyed', self.name); - } + self.constructor.emit('destroyed', self.name); + } + + function onConstructorDestroyed() { + self.removeListener('destroyed', onDestroyed); + self.removeListener('closed', onClosed); + self.emit('destroyed'); } function onClosed() { self.removeListener('destroyed', onDestroyed); - self.constructor.emit('unref', self); + destructionListeners.delete(self.name); } self.once('destroyed', onDestroyed); self.once('closed', onClosed); - self.constructor.emit('ref', self); + + // in setup.js, the constructor is primed to listen for destroy events + if (!destructionListeners.has(self.name)) { + destructionListeners.set(self.name, []); + } + destructionListeners.get(self.name).push(onConstructorDestroyed); } - inherits(PouchDB, AbstractPouchDB); - function PouchDB(name, opts) { + inherits(PouchDB$5, AbstractPouchDB); + function PouchDB$5(name, opts) { // In Node our test suite only tests this for PouchAlt unfortunately /* istanbul ignore if */ - if (!(this instanceof PouchDB)) { - return new PouchDB(name, opts); + if (!(this instanceof PouchDB$5)) { + return new PouchDB$5(name, opts); } var self = this; @@ -8231,7 +8493,7 @@ return /******/ (function(modules) { // webpackBootstrap this.__opts = opts = clone(opts); self.auto_compaction = opts.auto_compaction; - self.prefix = PouchDB.prefix; + self.prefix = PouchDB$5.prefix; if (typeof name !== 'string') { throw new Error('Missing/invalid DB name'); @@ -8245,10 +8507,10 @@ return /******/ (function(modules) { // webpackBootstrap self.name = name; self._adapter = opts.adapter; - PouchDB.emit('debug', ['adapter', 'Picked adapter: ', opts.adapter]); + debug('pouchdb:adapter')('Picked adapter: ' + opts.adapter); - if (!PouchDB.adapters[opts.adapter] || - !PouchDB.adapters[opts.adapter].valid()) { + if (!PouchDB$5.adapters[opts.adapter] || + !PouchDB$5.adapters[opts.adapter].valid()) { throw new Error('Invalid Adapter: ' + opts.adapter); } @@ -8257,23 +8519,25 @@ return /******/ (function(modules) { // webpackBootstrap self.adapter = opts.adapter; - PouchDB.adapters[opts.adapter].call(self, opts, function (err) { + PouchDB$5.adapters[opts.adapter].call(self, opts, function (err) { if (err) { return self.taskqueue.fail(err); } prepareForDestruction(self); self.emit('created', self); - PouchDB.emit('created', self.name); + PouchDB$5.emit('created', self.name); self.taskqueue.ready(self); }); } - PouchDB.adapters = {}; - PouchDB.preferredAdapters = []; + PouchDB$5.debug = debug; - PouchDB.prefix = '_pouch_'; + PouchDB$5.adapters = {}; + PouchDB$5.preferredAdapters = []; + + PouchDB$5.prefix = '_pouch_'; var eventEmitter = new events.EventEmitter(); @@ -8287,74 +8551,40 @@ return /******/ (function(modules) { // webpackBootstrap // these are created in constructor.js, and allow us to notify each DB with // the same name that it was destroyed, via the constructor object var destructListeners = Pouch._destructionListeners = new ExportedMap(); - - Pouch.on('ref', function onConstructorRef(db) { - if (!destructListeners.has(db.name)) { - destructListeners.set(db.name, []); - } - destructListeners.get(db.name).push(db); - }); - - Pouch.on('unref', function onConstructorUnref(db) { - if (!destructListeners.has(db.name)) { - return; - } - var dbList = destructListeners.get(db.name); - var pos = dbList.indexOf(db); - if (pos < 0) { - /* istanbul ignore next */ - return; - } - dbList.splice(pos, 1); - if (dbList.length > 1) { - /* istanbul ignore next */ - destructListeners.set(db.name, dbList); - } else { - destructListeners.delete(db.name); - } - }); - Pouch.on('destroyed', function onConstructorDestroyed(name) { - if (!destructListeners.has(name)) { - return; - } - var dbList = destructListeners.get(name); - destructListeners.delete(name); - dbList.forEach(function (db) { - db.emit('destroyed',true); + destructListeners.get(name).forEach(function (callback) { + callback(); }); + destructListeners.delete(name); }); } - setUpEventEmitter(PouchDB); + setUpEventEmitter(PouchDB$5); - PouchDB.adapter = function (id, obj, addToPreferredAdapters) { + PouchDB$5.adapter = function (id, obj, addToPreferredAdapters) { /* istanbul ignore else */ if (obj.valid()) { - PouchDB.adapters[id] = obj; + PouchDB$5.adapters[id] = obj; if (addToPreferredAdapters) { - PouchDB.preferredAdapters.push(id); + PouchDB$5.preferredAdapters.push(id); } } }; - PouchDB.plugin = function (obj) { + PouchDB$5.plugin = function (obj) { if (typeof obj === 'function') { // function style for plugins - obj(PouchDB); - } else if (typeof obj !== 'object' || Object.keys(obj).length === 0) { - throw new Error('Invalid plugin: got "' + obj + '", expected an object or a function'); + obj(PouchDB$5); + } else if (typeof obj !== 'object' || Object.keys(obj).length === 0){ + throw new Error('Invalid plugin: got \"' + obj + '\", expected an object or a function'); } else { Object.keys(obj).forEach(function (id) { // object style for plugins - PouchDB.prototype[id] = obj[id]; + PouchDB$5.prototype[id] = obj[id]; }); } - if (this.__defaults) { - PouchDB.__defaults = $inject_Object_assign({}, this.__defaults); - } - return PouchDB; + return PouchDB$5; }; - PouchDB.defaults = function (defaultOpts) { + PouchDB$5.defaults = function (defaultOpts) { function PouchAlt(name, opts) { if (!(this instanceof PouchAlt)) { return new PouchAlt(name, opts); @@ -8368,10489 +8598,8836 @@ return /******/ (function(modules) { // webpackBootstrap delete opts.name; } - opts = $inject_Object_assign({}, PouchAlt.__defaults, opts); - PouchDB.call(this, name, opts); + opts = assign$1({}, PouchAlt.__defaults, opts); + PouchDB$5.call(this, name, opts); } - inherits(PouchAlt, PouchDB); + inherits(PouchAlt, PouchDB$5); - PouchAlt.preferredAdapters = PouchDB.preferredAdapters.slice(); - Object.keys(PouchDB).forEach(function (key) { + PouchAlt.preferredAdapters = PouchDB$5.preferredAdapters.slice(); + Object.keys(PouchDB$5).forEach(function (key) { if (!(key in PouchAlt)) { - PouchAlt[key] = PouchDB[key]; + PouchAlt[key] = PouchDB$5[key]; } }); // make default options transitive // https://github.com/pouchdb/pouchdb/issues/5922 - PouchAlt.__defaults = $inject_Object_assign({}, this.__defaults, defaultOpts); + PouchAlt.__defaults = assign$1({}, this.__defaults, defaultOpts); return PouchAlt; }; // managed automatically by set-version.js - var version = "6.4.3"; - - function debugPouch(PouchDB) { - PouchDB.debug = debug; - var logs = {}; - /* istanbul ignore next */ - PouchDB.on('debug', function (args) { - // first argument is log identifier - var logId = args[0]; - // rest should be passed verbatim to debug module - var logArgs = args.slice(1); - if (!logs[logId]) { - logs[logId] = debug('pouchdb:' + logId); - } - logs[logId].apply(null, logArgs); - }); - } + var version = "6.1.1"; - // this would just be "return doc[field]", but fields - // can be "deep" due to dot notation - function getFieldFromDoc(doc, parsedField) { - var value = doc; - for (var i = 0, len = parsedField.length; i < len; i++) { - var key = parsedField[i]; - value = value[key]; - if (!value) { - break; - } - } - return value; - } + PouchDB$5.version = version; - function compare$1(left, right) { - return left < right ? -1 : left > right ? 1 : 0; - } - - // Converts a string in dot notation to an array of its components, with backslash escaping - function parseField(fieldName) { - // fields may be deep (e.g. "foo.bar.baz"), so parse - var fields = []; - var current = ''; - for (var i = 0, len = fieldName.length; i < len; i++) { - var ch = fieldName[i]; - if (ch === '.') { - if (i > 0 && fieldName[i - 1] === '\\') { // escaped delimiter - current = current.substring(0, current.length - 1) + '.'; - } else { // not escaped, so delimiter - fields.push(current); - current = ''; - } - } else { // normal character - current += ch; - } - } - fields.push(current); - return fields; - } - - var combinationFields = ['$or', '$nor', '$not']; - function isCombinationalField(field) { - return combinationFields.indexOf(field) > -1; + 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' + ]); - function getKey(obj) { - return Object.keys(obj)[0]; - } + // 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' + ]); - function getValue(obj) { - return obj[getKey(obj)]; + function parseRevisionInfo(rev) { + if (!/^\d+\-./.test(rev)) { + return createError(INVALID_REV); + } + var idx = rev.indexOf('-'); + var left = rev.substring(0, idx); + var right = rev.substring(idx + 1); + return { + prefix: parseInt(left, 10), + id: right + }; } + function makeRevTreeFromRevisions(revisions, opts) { + var pos = revisions.start - revisions.ids.length + 1; - // flatten an array of selectors joined by an $and operator - function mergeAndedSelectors(selectors) { - - // sort to ensure that e.g. if the user specified - // $and: [{$gt: 'a'}, {$gt: 'b'}], then it's collapsed into - // just {$gt: 'b'} - var res = {}; - - selectors.forEach(function (selector) { - Object.keys(selector).forEach(function (field) { - var matcher = selector[field]; - if (typeof matcher !== 'object') { - matcher = {$eq: matcher}; - } - - if (isCombinationalField(field)) { - if (matcher instanceof Array) { - res[field] = matcher.map(function (m) { - return mergeAndedSelectors([m]); - }); - } else { - res[field] = mergeAndedSelectors([matcher]); - } - } else { - var fieldMatchers = res[field] = res[field] || {}; - Object.keys(matcher).forEach(function (operator) { - var value = matcher[operator]; + var revisionIds = revisions.ids; + var ids = [revisionIds[0], opts, []]; - if (operator === '$gt' || operator === '$gte') { - return mergeGtGte(operator, value, fieldMatchers); - } else if (operator === '$lt' || operator === '$lte') { - return mergeLtLte(operator, value, fieldMatchers); - } else if (operator === '$ne') { - return mergeNe(value, fieldMatchers); - } else if (operator === '$eq') { - return mergeEq(value, fieldMatchers); - } - fieldMatchers[operator] = value; - }); - } - }); - }); + for (var i = 1, len = revisionIds.length; i < len; i++) { + ids = [revisionIds[i], {status: 'missing'}, [ids]]; + } - return res; + return [{ + pos: pos, + ids: ids + }]; } + // Preprocess documents, parse their revisions, assign an id and a + // revision for new writes that are missing them, etc + function parseDoc(doc, newEdits) { - - // collapse logically equivalent gt/gte values - function mergeGtGte(operator, value, fieldMatchers) { - if (typeof fieldMatchers.$eq !== 'undefined') { - return; // do nothing + var nRevNum; + var newRevId; + var revInfo; + var opts = {status: 'available'}; + if (doc._deleted) { + opts.deleted = true; } - if (typeof fieldMatchers.$gte !== 'undefined') { - if (operator === '$gte') { - if (value > fieldMatchers.$gte) { // more specificity - fieldMatchers.$gte = value; - } - } else { // operator === '$gt' - if (value >= fieldMatchers.$gte) { // more specificity - delete fieldMatchers.$gte; - fieldMatchers.$gt = value; - } + + if (newEdits) { + if (!doc._id) { + doc._id = uuid(); } - } else if (typeof fieldMatchers.$gt !== 'undefined') { - if (operator === '$gte') { - if (value > fieldMatchers.$gt) { // more specificity - delete fieldMatchers.$gt; - fieldMatchers.$gte = value; - } - } else { // operator === '$gt' - if (value > fieldMatchers.$gt) { // more specificity - fieldMatchers.$gt = value; + newRevId = 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 { - fieldMatchers[operator] = value; - } - } - - // collapse logically equivalent lt/lte values - function mergeLtLte(operator, value, fieldMatchers) { - if (typeof fieldMatchers.$eq !== 'undefined') { - return; // do nothing - } - if (typeof fieldMatchers.$lte !== 'undefined') { - if (operator === '$lte') { - if (value < fieldMatchers.$lte) { // more specificity - fieldMatchers.$lte = value; - } - } else { // operator === '$gt' - if (value <= fieldMatchers.$lte) { // more specificity - delete fieldMatchers.$lte; - fieldMatchers.$lt = value; - } + if (doc._revisions) { + doc._rev_tree = makeRevTreeFromRevisions(doc._revisions, opts); + nRevNum = doc._revisions.start; + newRevId = doc._revisions.ids[0]; } - } else if (typeof fieldMatchers.$lt !== 'undefined') { - if (operator === '$lte') { - if (value < fieldMatchers.$lt) { // more specificity - delete fieldMatchers.$lt; - fieldMatchers.$lte = value; - } - } else { // operator === '$gt' - if (value < fieldMatchers.$lt) { // more specificity - fieldMatchers.$lt = value; + 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, []] + }]; } - } else { - fieldMatchers[operator] = value; - } - } - - // combine $ne values into one array - function mergeNe(value, fieldMatchers) { - if ('$ne' in fieldMatchers) { - // there are many things this could "not" be - fieldMatchers.$ne.push(value); - } else { // doesn't exist yet - fieldMatchers.$ne = [value]; } - } - // add $eq into the mix - function mergeEq(value, fieldMatchers) { - // these all have less specificity than the $eq - // TODO: check for user errors here - delete fieldMatchers.$gt; - delete fieldMatchers.$gte; - delete fieldMatchers.$lt; - delete fieldMatchers.$lte; - delete fieldMatchers.$ne; - fieldMatchers.$eq = value; - } + invalidIdError(doc._id); + doc._rev = nRevNum + '-' + newRevId; - // - // normalize the selector - // - function massageSelector(input) { - var result = clone(input); - var wasAnded = false; - if ('$and' in result) { - result = mergeAndedSelectors(result['$and']); - wasAnded = true; + var result = {metadata : {}, data : {}}; + for (var key in doc) { + /* istanbul ignore else */ + if (Object.prototype.hasOwnProperty.call(doc, key)) { + var specialKey = key[0] === '_'; + if (specialKey && !reservedWords[key]) { + var error = createError(DOC_VALIDATION, key); + error.message = 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; + } - ['$or', '$nor'].forEach(function (orOrNor) { - if (orOrNor in result) { - // message each individual selector - // e.g. {foo: 'bar'} becomes {foo: {$eq: 'bar'}} - result[orOrNor].forEach(function (subSelector) { - var fields = Object.keys(subSelector); - for (var i = 0; i < fields.length; i++) { - var field = fields[i]; - var matcher = subSelector[field]; - if (typeof matcher !== 'object' || matcher === null) { - subSelector[field] = {$eq: matcher}; - } - } - }); - } - }); + var thisAtob = function (str) { + return atob(str); + }; - if ('$not' in result) { - //This feels a little like forcing, but it will work for now, - //I would like to come back to this and make the merging of selectors a little more generic - result['$not'] = mergeAndedSelectors([result['$not']]); - } - - var fields = Object.keys(result); - - for (var i = 0; i < fields.length; i++) { - var field = fields[i]; - var matcher = result[field]; + var thisBtoa = function (str) { + return btoa(str); + }; - if (typeof matcher !== 'object' || matcher === null) { - matcher = {$eq: matcher}; - } else if ('$ne' in matcher && !wasAnded) { - // I put these in an array, since there may be more than one - // but in the "mergeAnded" operation, I already take care of that - matcher.$ne = [matcher.$ne]; + // Abstracts constructing a Blob object, so it also works in older + // browsers that don't support the native Blob constructor (e.g. + // old QtWebKit versions, Android < 4.4). + function createBlob(parts, properties) { + /* global BlobBuilder,MSBlobBuilder,MozBlobBuilder,WebKitBlobBuilder */ + parts = parts || []; + properties = properties || {}; + try { + return new Blob(parts, properties); + } catch (e) { + if (e.name !== "TypeError") { + throw e; } - result[field] = matcher; + var Builder = typeof BlobBuilder !== 'undefined' ? BlobBuilder : + typeof MSBlobBuilder !== 'undefined' ? MSBlobBuilder : + typeof MozBlobBuilder !== 'undefined' ? MozBlobBuilder : + WebKitBlobBuilder; + var builder = new Builder(); + for (var i = 0; i < parts.length; i += 1) { + builder.append(parts[i]); + } + return builder.getBlob(properties.type); } - - return result; } - function pad(str, padWith, upToLength) { - var padding = ''; - var targetLength = upToLength - str.length; - /* istanbul ignore next */ - while (padding.length < targetLength) { - padding += padWith; + // From http://stackoverflow.com/questions/14967647/ (continues on next line) + // encode-decode-image-with-base64-breaks-image (2013-04-21) + function binaryStringToArrayBuffer(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 padding; + return buf; } - function padLeft(str, padWith, upToLength) { - var padding = pad(str, padWith, upToLength); - return padding + str; + function binStringToBluffer(binString, type) { + return createBlob([binaryStringToArrayBuffer(binString)], {type: type}); } - var MIN_MAGNITUDE = -324; // verified by -Number.MIN_VALUE - var MAGNITUDE_DIGITS = 3; // ditto - var SEP = ''; // set to '_' for easier debugging - - function collate(a, b) { + function b64ToBluffer(b64, type) { + return binStringToBluffer(thisAtob(b64), type); + } - if (a === b) { - return 0; + //Can't find original post, but this is close + //http://stackoverflow.com/questions/6965107/ (continues on next line) + //converting-between-strings-and-arraybuffers + function arrayBufferToBinaryString(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; + } - a = normalizeKey(a); - b = normalizeKey(b); + // shim for browsers that don't support it + function readAsBinaryString(blob, callback) { + if (typeof FileReader === 'undefined') { + // fix for Firefox in a web worker + // https://bugzilla.mozilla.org/show_bug.cgi?id=901097 + return callback(arrayBufferToBinaryString( + new FileReaderSync().readAsArrayBuffer(blob))); + } - var ai = collationIndex(a); - var bi = collationIndex(b); - if ((ai - bi) !== 0) { - return ai - bi; + 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(arrayBufferToBinaryString(result)); + }; + if (hasBinaryString) { + reader.readAsBinaryString(blob); + } else { + reader.readAsArrayBuffer(blob); } - switch (typeof a) { - case 'number': - return a - b; - case 'boolean': - return a < b ? -1 : 1; - case 'string': - return stringCollate(a, b); + } + + function blobToBinaryString(blobOrBuffer, callback) { + readAsBinaryString(blobOrBuffer, function (bin) { + callback(bin); + }); + } + + function blobToBase64(blobOrBuffer, callback) { + blobToBinaryString(blobOrBuffer, function (base64) { + callback(thisBtoa(base64)); + }); + } + + // simplified API. universal browser support is assumed + function readAsArrayBuffer(blob, callback) { + if (typeof FileReader === 'undefined') { + // fix for Firefox in a web worker: + // https://bugzilla.mozilla.org/show_bug.cgi?id=901097 + return callback(new FileReaderSync().readAsArrayBuffer(blob)); } - return Array.isArray(a) ? arrayCollate(a, b) : objectCollate(a, b); + + var reader = new FileReader(); + reader.onloadend = function (e) { + var result = e.target.result || new ArrayBuffer(0); + callback(result); + }; + reader.readAsArrayBuffer(blob); } - // couch considers null/NaN/Infinity/-Infinity === undefined, - // for the purposes of mapreduce indexes. also, dates get stringified. - function normalizeKey(key) { - switch (typeof key) { - case 'undefined': - return null; - case 'number': - if (key === Infinity || key === -Infinity || isNaN(key)) { - return null; - } - return key; - case 'object': - var origKey = key; - if (Array.isArray(key)) { - var len = key.length; - key = new Array(len); - for (var i = 0; i < len; i++) { - key[i] = normalizeKey(origKey[i]); - } - /* istanbul ignore next */ - } else if (key instanceof Date) { - return key.toJSON(); - } else if (key !== null) { // generic object - key = {}; - for (var k in origKey) { - if (origKey.hasOwnProperty(k)) { - var val = origKey[k]; - if (typeof val !== 'undefined') { - key[k] = normalizeKey(val); - } - } - } - } + // this is not used in the browser + + var setImmediateShim = global.setImmediate || global.setTimeout; + var MD5_CHUNK_SIZE = 32768; + + function rawToBase64(raw) { + return thisBtoa(raw); + } + + function sliceBlob(blob$$1, start, end) { + if (blob$$1.webkitSlice) { + return blob$$1.webkitSlice(start, end); } - return key; + return blob$$1.slice(start, end); } - function indexify(key) { - if (key !== null) { - switch (typeof key) { - case 'boolean': - return key ? 1 : 0; - case 'number': - return numToIndexableString(key); - case 'string': - // We've to be sure that key does not contain \u0000 - // Do order-preserving replacements: - // 0 -> 1, 1 - // 1 -> 1, 2 - // 2 -> 2, 2 - return key - .replace(/\u0002/g, '\u0002\u0002') - .replace(/\u0001/g, '\u0001\u0002') - .replace(/\u0000/g, '\u0001\u0001'); - case 'object': - var isArray = Array.isArray(key); - var arr = isArray ? key : Object.keys(key); - var i = -1; - var len = arr.length; - var result = ''; - if (isArray) { - while (++i < len) { - result += toIndexableString(arr[i]); - } - } else { - while (++i < len) { - var objKey = arr[i]; - result += toIndexableString(objKey) + - toIndexableString(key[objKey]); - } - } - return result; - } + function appendBlob(buffer, blob$$1, start, end, callback) { + if (start > 0 || end < blob$$1.size) { + // only slice blob if we really need to + blob$$1 = sliceBlob(blob$$1, start, end); } - return ''; + readAsArrayBuffer(blob$$1, function (arrayBuffer) { + buffer.append(arrayBuffer); + callback(); + }); } - // convert the given key to a string that would be appropriate - // for lexical sorting, e.g. within a database, where the - // sorting is the same given by the collate() function. - function toIndexableString(key) { - var zero = '\u0000'; - key = normalizeKey(key); - return collationIndex(key) + SEP + indexify(key) + zero; + function appendString(buffer, string, start, end, callback) { + if (start > 0 || end < string.length) { + // only create a substring if we really need to + string = string.substring(start, end); + } + buffer.appendBinary(string); + callback(); } - function parseNumber(str, i) { - var originalIdx = i; - var num; - var zero = str[i] === '1'; - if (zero) { - num = 0; - i++; - } else { - var neg = str[i] === '0'; - i++; - var numAsString = ''; - var magAsString = str.substring(i, i + MAGNITUDE_DIGITS); - var magnitude = parseInt(magAsString, 10) + MIN_MAGNITUDE; - /* istanbul ignore next */ - if (neg) { - magnitude = -magnitude; - } - i += MAGNITUDE_DIGITS; - while (true) { - var ch = str[i]; - if (ch === '\u0000') { - break; - } else { - numAsString += ch; - } - i++; - } - numAsString = numAsString.split('.'); - if (numAsString.length === 1) { - num = parseInt(numAsString, 10); - } else { - /* istanbul ignore next */ - num = parseFloat(numAsString[0] + '.' + numAsString[1]); - } - /* istanbul ignore next */ - if (neg) { - num = num - 10; - } - /* istanbul ignore next */ - if (magnitude !== 0) { - // parseFloat is more reliable than pow due to rounding errors - // e.g. Number.MAX_VALUE would return Infinity if we did - // num * Math.pow(10, magnitude); - num = parseFloat(num + 'e' + magnitude); - } + function binaryMd5(data, callback) { + var inputIsString = typeof data === 'string'; + var len = inputIsString ? data.length : data.size; + 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(); + + var append = inputIsString ? appendString : appendBlob; + + function next() { + setImmediateShim(loadNextChunk); } - return {num: num, length : i - originalIdx}; - } - // move up the stack while parsing - // this function moved outside of parseIndexableString for performance - function pop(stack, metaStack) { - var obj = stack.pop(); + function done() { + var raw = buffer.end(true); + var base64 = rawToBase64(raw); + callback(base64); + buffer.destroy(); + } - if (metaStack.length) { - var lastMetaElement = metaStack[metaStack.length - 1]; - if (obj === lastMetaElement.element) { - // popping a meta-element, e.g. an object whose value is another object - metaStack.pop(); - lastMetaElement = metaStack[metaStack.length - 1]; - } - var element = lastMetaElement.element; - var lastElementIndex = lastMetaElement.index; - if (Array.isArray(element)) { - element.push(obj); - } else if (lastElementIndex === stack.length - 2) { // obj with key+value - var key = stack.pop(); - element[key] = obj; + function loadNextChunk() { + var start = currentChunk * chunkSize; + var end = start + chunkSize; + currentChunk++; + if (currentChunk < chunks) { + append(buffer, data, start, end, next); } else { - stack.push(obj); // obj with key only + append(buffer, data, start, end, done); } } + loadNextChunk(); } - function parseIndexableString(str) { - var stack = []; - var metaStack = []; // stack for arrays and objects - var i = 0; + function stringMd5(string) { + return Md5.hash(string); + } - /*eslint no-constant-condition: ["error", { "checkLoops": false }]*/ - while (true) { - var collationIndex = str[i++]; - if (collationIndex === '\u0000') { - if (stack.length === 1) { - return stack.pop(); - } else { - pop(stack, metaStack); - continue; - } - } - switch (collationIndex) { - case '1': - stack.push(null); - break; - case '2': - stack.push(str[i] === '1'); - i++; - break; - case '3': - var parsedNum = parseNumber(str, i); - stack.push(parsedNum.num); - i += parsedNum.length; - break; - case '4': - var parsedStr = ''; - /*eslint no-constant-condition: ["error", { "checkLoops": false }]*/ - while (true) { - var ch = str[i]; - if (ch === '\u0000') { - break; - } - parsedStr += ch; - i++; - } - // perform the reverse of the order-preserving replacement - // algorithm (see above) - parsedStr = parsedStr.replace(/\u0001\u0001/g, '\u0000') - .replace(/\u0001\u0002/g, '\u0001') - .replace(/\u0002\u0002/g, '\u0002'); - stack.push(parsedStr); - break; - case '5': - var arrayElement = { element: [], index: stack.length }; - stack.push(arrayElement.element); - metaStack.push(arrayElement); - break; - case '6': - var objElement = { element: {}, index: stack.length }; - stack.push(objElement.element); - metaStack.push(objElement); - break; - /* istanbul ignore next */ - default: - throw new Error( - 'bad collationIndex or unexpectedly reached end of input: ' + - collationIndex); - } + function parseBase64(data) { + try { + return thisAtob(data); + } catch (e) { + var err = createError(BAD_ARG, + 'Attachment is not a valid base64 string'); + return {error: err}; } } - function arrayCollate(a, b) { - var len = Math.min(a.length, b.length); - for (var i = 0; i < len; i++) { - var sort = collate(a[i], b[i]); - if (sort !== 0) { - return sort; - } + function preprocessString(att, blobType, callback) { + var asBinary = parseBase64(att.data); + if (asBinary.error) { + return callback(asBinary.error); } - return (a.length === b.length) ? 0 : - (a.length > b.length) ? 1 : -1; - } - function stringCollate(a, b) { - // See: https://github.com/daleharvey/pouchdb/issues/40 - // This is incompatible with the CouchDB implementation, but its the - // best we can do for now - return (a === b) ? 0 : ((a > b) ? 1 : -1); - } - function objectCollate(a, b) { - var ak = Object.keys(a), bk = Object.keys(b); - var len = Math.min(ak.length, bk.length); - for (var i = 0; i < len; i++) { - // First sort the keys - var sort = collate(ak[i], bk[i]); - if (sort !== 0) { - return sort; - } - // if the keys are equal sort the values - sort = collate(a[ak[i]], b[bk[i]]); - if (sort !== 0) { - return sort; - } + att.length = asBinary.length; + if (blobType === 'blob') { + att.data = binStringToBluffer(asBinary, att.content_type); + } else if (blobType === 'base64') { + att.data = thisBtoa(asBinary); + } else { // binary + att.data = asBinary; } - return (ak.length === bk.length) ? 0 : - (ak.length > bk.length) ? 1 : -1; + binaryMd5(asBinary, function (result) { + att.digest = 'md5-' + result; + callback(); + }); } - // The collation is defined by erlangs ordered terms - // the atoms null, true, false come first, then numbers, strings, - // arrays, then objects - // null/undefined/NaN/Infinity/-Infinity are all considered null - function collationIndex(x) { - var id = ['boolean', 'number', 'string', 'object']; - var idx = id.indexOf(typeof x); - //false if -1 otherwise true, but fast!!!!1 - if (~idx) { - if (x === null) { - return 1; - } - if (Array.isArray(x)) { - return 5; + + function preprocessBlob(att, blobType, callback) { + binaryMd5(att.data, function (md5) { + att.digest = 'md5-' + md5; + // size is for blobs (browser), length is for buffers (node) + att.length = att.data.size || att.data.length || 0; + if (blobType === 'binary') { + blobToBinaryString(att.data, function (binString) { + att.data = binString; + callback(); + }); + } else if (blobType === 'base64') { + blobToBase64(att.data, function (b64) { + att.data = b64; + callback(); + }); + } else { + callback(); } - return idx < 3 ? (idx + 2) : (idx + 3); + }); + } + + function preprocessAttachment(att, blobType, callback) { + if (att.stub) { + return callback(); } - /* istanbul ignore next */ - if (Array.isArray(x)) { - return 5; + if (typeof att.data === 'string') { // input is a base64 string + preprocessString(att, blobType, callback); + } else { // input is a blob + preprocessBlob(att, blobType, callback); } } - // conversion: - // x yyy zz...zz - // x = 0 for negative, 1 for 0, 2 for positive - // y = exponent (for negative numbers negated) moved so that it's >= 0 - // z = mantisse - function numToIndexableString(num) { + function preprocessAttachments(docInfos, blobType, callback) { - if (num === 0) { - return '1'; + if (!docInfos.length) { + return callback(); } - // convert number to exponential format for easier and - // more succinct string sorting - var expFormat = num.toExponential().split(/e\+?/); - var magnitude = parseInt(expFormat[1], 10); + var docv = 0; + var overallErr; - var neg = num < 0; + docInfos.forEach(function (docInfo) { + var attachments = docInfo.data && docInfo.data._attachments ? + Object.keys(docInfo.data._attachments) : []; + var recv = 0; - var result = neg ? '0' : '2'; + if (!attachments.length) { + return done(); + } - // first sort by magnitude - // it's easier if all magnitudes are positive - var magForComparison = ((neg ? -magnitude : magnitude) - MIN_MAGNITUDE); - var magString = padLeft((magForComparison).toString(), '0', MAGNITUDE_DIGITS); + function processedAttachment(err) { + overallErr = err; + recv++; + if (recv === attachments.length) { + done(); + } + } - result += SEP + magString; + for (var key in docInfo.data._attachments) { + if (docInfo.data._attachments.hasOwnProperty(key)) { + preprocessAttachment(docInfo.data._attachments[key], + blobType, processedAttachment); + } + } + }); - // then sort by the factor - var factor = Math.abs(parseFloat(expFormat[0])); // [1..10) - /* istanbul ignore next */ - if (neg) { // for negative reverse ordering - factor = 10 - factor; + function done() { + docv++; + if (docInfos.length === docv) { + if (overallErr) { + callback(overallErr); + } else { + callback(); + } + } } + } - var factorStr = factor.toFixed(20); - - // strip zeros from the end - factorStr = factorStr.replace(/\.?0+$/, ''); - - result += SEP + factorStr; + function updateDoc(revLimit, prev, docInfo, results, + i, cb, writeDoc, newEdits) { - return result; - } + if (revExists(prev.rev_tree, docInfo.metadata.rev)) { + results[i] = docInfo; + return cb(); + } - // create a comparator based on the sort object - function createFieldSorter(sort) { + // sometimes this is pre-calculated. historically not always + var previousWinningRev = prev.winningRev || winningRev(prev); + var previouslyDeleted = 'deleted' in prev ? prev.deleted : + isDeleted(prev, previousWinningRev); + var deleted = 'deleted' in docInfo.metadata ? docInfo.metadata.deleted : + isDeleted(docInfo.metadata); + var isRoot = /^1-/.test(docInfo.metadata.rev); - function getFieldValuesAsArray(doc) { - return sort.map(function (sorting) { - var fieldName = getKey(sorting); - var parsedField = parseField(fieldName); - var docFieldValue = getFieldFromDoc(doc, parsedField); - return docFieldValue; - }); + if (previouslyDeleted && !deleted && newEdits && isRoot) { + var newDoc = docInfo.data; + newDoc._rev = previousWinningRev; + newDoc._id = docInfo.metadata.id; + docInfo = parseDoc(newDoc, newEdits); } - return function (aRow, bRow) { - var aFieldValues = getFieldValuesAsArray(aRow.doc); - var bFieldValues = getFieldValuesAsArray(bRow.doc); - var collation = collate(aFieldValues, bFieldValues); - if (collation !== 0) { - return collation; - } - // this is what mango seems to do - return compare$1(aRow.doc._id, bRow.doc._id); - }; - } + var merged = merge(prev.rev_tree, docInfo.metadata.rev_tree[0], revLimit); - function filterInMemoryFields(rows, requestDef, inMemoryFields) { - rows = rows.filter(function (row) { - return rowFilter(row.doc, requestDef.selector, inMemoryFields); - }); + var inConflict = newEdits && (((previouslyDeleted && deleted) || + (!previouslyDeleted && merged.conflicts !== 'new_leaf') || + (previouslyDeleted && !deleted && merged.conflicts === 'new_branch'))); - if (requestDef.sort) { - // in-memory sort - var fieldSorter = createFieldSorter(requestDef.sort); - rows = rows.sort(fieldSorter); - if (typeof requestDef.sort[0] !== 'string' && - getValue(requestDef.sort[0]) === 'desc') { - rows = rows.reverse(); - } + if (inConflict) { + var err = createError(REV_CONFLICT); + results[i] = err; + return cb(); } - if ('limit' in requestDef || 'skip' in requestDef) { - // have to do the limit in-memory - var skip = requestDef.skip || 0; - var limit = ('limit' in requestDef ? requestDef.limit : rows.length) + skip; - rows = rows.slice(skip, limit); + var newRev = docInfo.metadata.rev; + docInfo.metadata.rev_tree = merged.tree; + docInfo.stemmedRevs = merged.stemmedRevs || []; + /* istanbul ignore else */ + if (prev.rev_map) { + docInfo.metadata.rev_map = prev.rev_map; // used only by leveldb } - return rows; - } - function rowFilter(doc, selector, inMemoryFields) { - return inMemoryFields.every(function (field) { - var matcher = selector[field]; - var parsedField = parseField(field); - var docFieldValue = getFieldFromDoc(doc, parsedField); - if (isCombinationalField(field)) { - return matchCominationalSelector(field, matcher, doc); - } + // recalculate + var winningRev$$1 = winningRev(docInfo.metadata); + var winningRevIsDeleted = isDeleted(docInfo.metadata, winningRev$$1); - return matchSelector(matcher, doc, parsedField, docFieldValue); - }); - } + // calculate the total number of documents that were added/removed, + // from the perspective of total_rows/doc_count + var delta = (previouslyDeleted === winningRevIsDeleted) ? 0 : + previouslyDeleted < winningRevIsDeleted ? -1 : 1; - function matchSelector(matcher, doc, parsedField, docFieldValue) { - if (!matcher) { - // no filtering necessary; this field is just needed for sorting - return true; + var newRevIsDeleted; + if (newRev === winningRev$$1) { + // if the new rev is the same as the winning rev, we can reuse that value + newRevIsDeleted = winningRevIsDeleted; + } else { + // if they're not the same, then we need to recalculate + newRevIsDeleted = isDeleted(docInfo.metadata, newRev); } - return Object.keys(matcher).every(function (userOperator) { - var userValue = matcher[userOperator]; - return match(userOperator, doc, userValue, parsedField, docFieldValue); - }); + writeDoc(docInfo, winningRev$$1, winningRevIsDeleted, newRevIsDeleted, + true, delta, i, cb); } - function matchCominationalSelector(field, matcher, doc) { - - if (field === '$or') { - return matcher.some(function (orMatchers) { - return rowFilter(doc, orMatchers, Object.keys(orMatchers)); - }); - } + function rootIsMissing(docInfo) { + return docInfo.metadata.rev_tree[0].ids[1].status === 'missing'; + } - if (field === '$not') { - return !rowFilter(doc, matcher, Object.keys(matcher)); - } + function processDocs(revLimit, docInfos, api, fetchedDocs, tx, results, + writeDoc, opts, overallCallback) { - //`$nor` - return !matcher.find(function (orMatchers) { - return rowFilter(doc, orMatchers, Object.keys(orMatchers)); - }); + // Default to 1000 locally + revLimit = revLimit || 1000; - } + function insertDoc(docInfo, resultsIdx, callback) { + // Cant insert new deleted documents + var winningRev$$1 = winningRev(docInfo.metadata); + var deleted = isDeleted(docInfo.metadata, winningRev$$1); + if ('was_delete' in opts && deleted) { + results[resultsIdx] = createError(MISSING_DOC, 'deleted'); + return callback(); + } - function match(userOperator, doc, userValue, parsedField, docFieldValue) { - if (!matchers[userOperator]) { - throw new Error('unknown operator "' + userOperator + - '" - should be one of $eq, $lte, $lt, $gt, $gte, $exists, $ne, $in, ' + - '$nin, $size, $mod, $regex, $elemMatch, $type, $allMatch or $all'); - } - return matchers[userOperator](doc, userValue, parsedField, docFieldValue); - } + // 4712 - detect whether a new document was inserted with a _rev + var inConflict = newEdits && rootIsMissing(docInfo); - function fieldExists(docFieldValue) { - return typeof docFieldValue !== 'undefined' && docFieldValue !== null; - } + if (inConflict) { + var err = createError(REV_CONFLICT); + results[resultsIdx] = err; + return callback(); + } - function fieldIsNotUndefined(docFieldValue) { - return typeof docFieldValue !== 'undefined'; - } + var delta = deleted ? 0 : 1; - function modField(docFieldValue, userValue) { - var divisor = userValue[0]; - var mod = userValue[1]; - if (divisor === 0) { - throw new Error('Bad divisor, cannot divide by zero'); + writeDoc(docInfo, winningRev$$1, deleted, deleted, false, + delta, resultsIdx, callback); } - if (parseInt(divisor, 10) !== divisor ) { - throw new Error('Divisor is not an integer'); - } + var newEdits = opts.new_edits; + var idsToDocs = new ExportedMap(); - if (parseInt(mod, 10) !== mod ) { - throw new Error('Modulus is not an integer'); - } + var docsDone = 0; + var docsToDo = docInfos.length; - if (parseInt(docFieldValue, 10) !== docFieldValue) { - return false; + function checkAllDocsDone() { + if (++docsDone === docsToDo && overallCallback) { + overallCallback(); + } } - return docFieldValue % divisor === mod; - } + docInfos.forEach(function (currentDoc, resultsIdx) { - function arrayContainsValue(docFieldValue, userValue) { - return userValue.some(function (val) { - if (docFieldValue instanceof Array) { - return docFieldValue.indexOf(val) > -1; + if (currentDoc._id && isLocalId(currentDoc._id)) { + var fun = currentDoc._deleted ? '_removeLocal' : '_putLocal'; + api[fun](currentDoc, {ctx: tx}, function (err, res) { + results[resultsIdx] = err || res; + checkAllDocsDone(); + }); + return; } - return docFieldValue === val; + var id = currentDoc.metadata.id; + if (idsToDocs.has(id)) { + docsToDo--; // duplicate + idsToDocs.get(id).push([currentDoc, resultsIdx]); + } else { + idsToDocs.set(id, [[currentDoc, resultsIdx]]); + } }); - } - function arrayContainsAllValues(docFieldValue, userValue) { - return userValue.every(function (val) { - return docFieldValue.indexOf(val) > -1; + // 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)) { + updateDoc(revLimit, fetchedDocs.get(id), currentDoc, results, + resultsIdx, docWritten, writeDoc, newEdits); + } else { + // Ensure stemming applies to new writes as well + var merged = merge([], currentDoc.metadata.rev_tree[0], revLimit); + currentDoc.metadata.rev_tree = merged.tree; + currentDoc.stemmedRevs = merged.stemmedRevs || []; + insertDoc(currentDoc, resultsIdx, docWritten); + } + } + nextDoc(); }); } - function arraySize(docFieldValue, userValue) { - return docFieldValue.length === userValue; - } + // IndexedDB requires a versioned database structure, so we use the + // version here to manage migrations. + var ADAPTER_VERSION = 5; - function regexMatch(docFieldValue, userValue) { - var re = new RegExp(userValue); + // The object stores created for each database + // DOC_STORE stores the document meta data, its revision history and state + // Keyed by document id + var DOC_STORE = 'document-store'; + // BY_SEQ_STORE stores a particular version of a document, keyed by its + // sequence id + var BY_SEQ_STORE = 'by-sequence'; + // Where we store attachments + var ATTACH_STORE = 'attach-store'; + // Where we store many-to-many relations + // between attachment digests and seqs + var ATTACH_AND_SEQ_STORE = 'attach-seq-store'; - return re.test(docFieldValue); - } + // Where we store database-wide meta data in a single record + // keyed by id: META_STORE + var META_STORE = 'meta-store'; + // Where we store local documents + var LOCAL_STORE = 'local-store'; + // Where we detect blob support + var DETECT_BLOB_SUPPORT_STORE = 'detect-blob-support'; - function typeMatch(docFieldValue, userValue) { + function safeJsonParse(str) { + // This try/catch guards against stack overflow errors. + // JSON.parse() is faster than vuvuzela.parse() but vuvuzela + // cannot overflow. + try { + return JSON.parse(str); + } catch (e) { + /* istanbul ignore next */ + return vuvuzela.parse(str); + } + } - switch (userValue) { - case 'null': - return docFieldValue === null; - case 'boolean': - return typeof (docFieldValue) === 'boolean'; - case 'number': - return typeof (docFieldValue) === 'number'; - case 'string': - return typeof (docFieldValue) === 'string'; - case 'array': - return docFieldValue instanceof Array; - case 'object': - return ({}).toString.call(docFieldValue) === '[object Object]'; + function safeJsonStringify(json) { + try { + return JSON.stringify(json); + } catch (e) { + /* istanbul ignore next */ + return vuvuzela.stringify(json); } + } - throw new Error(userValue + ' not supported as a type.' + - 'Please use one of object, string, array, number, boolean or null.'); + function idbError(callback) { + return function (evt) { + var message = 'unknown_error'; + if (evt.target && evt.target.error) { + message = evt.target.error.name || evt.target.error.message; + } + callback(createError(IDB_ERROR, message, evt.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. + function encodeMetadata(metadata, winningRev, deleted) { + return { + data: safeJsonStringify(metadata), + winningRev: winningRev, + deletedOrLocal: deleted ? '1' : '0', + seq: metadata.seq, // highest seq for this doc + id: metadata.id + }; } - var matchers = { + function decodeMetadata(storedObject) { + if (!storedObject) { + return null; + } + var metadata = safeJsonParse(storedObject.data); + metadata.winningRev = storedObject.winningRev; + metadata.deleted = storedObject.deletedOrLocal === '1'; + metadata.seq = storedObject.seq; + return metadata; + } - '$elemMatch': function (doc, userValue, parsedField, docFieldValue) { - if (!Array.isArray(docFieldValue)) { - return false; - } + // read the doc back out from the database. we don't store the + // _id or _rev because we already have _doc_id_rev. + function decodeDoc(doc) { + if (!doc) { + return doc; + } + var idx = doc._doc_id_rev.lastIndexOf(':'); + 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; + } - if (docFieldValue.length === 0) { - return false; + // Read a blob from the database, encoding as necessary + // and translating from base64 if the IDB doesn't support + // native Blobs + function readBlobData(body, type, asBlob, callback) { + if (asBlob) { + if (!body) { + callback(createBlob([''], {type: type})); + } else if (typeof body !== 'string') { // we have blob support + callback(body); + } else { // no blob support + callback(b64ToBluffer(body, type)); } - - if (typeof docFieldValue[0] === 'object') { - return docFieldValue.some(function (val) { - return rowFilter(val, userValue, Object.keys(userValue)); + } else { // as base64 string + if (!body) { + callback(''); + } else if (typeof body !== 'string') { // we have blob support + readAsBinaryString(body, function (binary) { + callback(thisBtoa(binary)); }); + } else { // no blob support + callback(body); } + } + } - return docFieldValue.some(function (val) { - return matchSelector(userValue, doc, parsedField, val); - }); - }, + function fetchAttachmentsIfNecessary(doc, opts, txn, cb) { + var attachments = Object.keys(doc._attachments || {}); + if (!attachments.length) { + return cb && cb(); + } + var numDone = 0; - '$allMatch': function (doc, userValue, parsedField, docFieldValue) { - if (!Array.isArray(docFieldValue)) { - return false; + function checkDone() { + if (++numDone === attachments.length && cb) { + cb(); } + } - /* istanbul ignore next */ - if (docFieldValue.length === 0) { - return false; - } + function fetchAttachment(doc, att) { + var attObj = doc._attachments[att]; + var digest = attObj.digest; + var req = txn.objectStore(ATTACH_STORE).get(digest); + req.onsuccess = function (e) { + attObj.body = e.target.result.body; + checkDone(); + }; + } - if (typeof docFieldValue[0] === 'object') { - return docFieldValue.every(function (val) { - return rowFilter(val, userValue, Object.keys(userValue)); - }); + attachments.forEach(function (att) { + if (opts.attachments && opts.include_docs) { + fetchAttachment(doc, att); + } else { + doc._attachments[att].stub = true; + checkDone(); } + }); + } - return docFieldValue.every(function (val) { - return matchSelector(userValue, doc, parsedField, val); - }); - }, - - '$eq': function (doc, userValue, parsedField, docFieldValue) { - return fieldIsNotUndefined(docFieldValue) && collate(docFieldValue, userValue) === 0; - }, + // 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 + function postProcessAttachments(results, asBlob) { + return PouchPromise$1.all(results.map(function (row) { + if (row.doc && row.doc._attachments) { + var attNames = Object.keys(row.doc._attachments); + return PouchPromise$1.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 PouchPromise$1(function (resolve) { + readBlobData(body, type, asBlob, function (data) { + row.doc._attachments[att] = assign$1( + pick(attObj, ['digest', 'content_type']), + {data: data} + ); + resolve(); + }); + }); + })); + } + })); + } - '$gte': function (doc, userValue, parsedField, docFieldValue) { - return fieldIsNotUndefined(docFieldValue) && collate(docFieldValue, userValue) >= 0; - }, + function compactRevs(revs, docId, txn) { - '$gt': function (doc, userValue, parsedField, docFieldValue) { - return fieldIsNotUndefined(docFieldValue) && collate(docFieldValue, userValue) > 0; - }, + var possiblyOrphanedDigests = []; + var seqStore = txn.objectStore(BY_SEQ_STORE); + var attStore = txn.objectStore(ATTACH_STORE); + var attAndSeqStore = txn.objectStore(ATTACH_AND_SEQ_STORE); + var count = revs.length; - '$lte': function (doc, userValue, parsedField, docFieldValue) { - return fieldIsNotUndefined(docFieldValue) && collate(docFieldValue, userValue) <= 0; - }, + function checkDone() { + count--; + if (!count) { // done processing all revs + deleteOrphanedAttachments(); + } + } - '$lt': function (doc, userValue, parsedField, docFieldValue) { - return fieldIsNotUndefined(docFieldValue) && collate(docFieldValue, userValue) < 0; - }, - - '$exists': function (doc, userValue, parsedField, docFieldValue) { - //a field that is null is still considered to exist - if (userValue) { - return fieldIsNotUndefined(docFieldValue); + function deleteOrphanedAttachments() { + if (!possiblyOrphanedDigests.length) { + return; } - - return !fieldIsNotUndefined(docFieldValue); - }, - - '$mod': function (doc, userValue, parsedField, docFieldValue) { - return fieldExists(docFieldValue) && modField(docFieldValue, userValue); - }, - - '$ne': function (doc, userValue, parsedField, docFieldValue) { - return userValue.every(function (neValue) { - return collate(docFieldValue, neValue) !== 0; + 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); + } + }; }); - }, - '$in': function (doc, userValue, parsedField, docFieldValue) { - return fieldExists(docFieldValue) && arrayContainsValue(docFieldValue, userValue); - }, - - '$nin': function (doc, userValue, parsedField, docFieldValue) { - return fieldExists(docFieldValue) && !arrayContainsValue(docFieldValue, userValue); - }, - - '$size': function (doc, userValue, parsedField, docFieldValue) { - return fieldExists(docFieldValue) && arraySize(docFieldValue, userValue); - }, - - '$all': function (doc, userValue, parsedField, docFieldValue) { - return Array.isArray(docFieldValue) && arrayContainsAllValues(docFieldValue, userValue); - }, - - '$regex': function (doc, userValue, parsedField, docFieldValue) { - return fieldExists(docFieldValue) && regexMatch(docFieldValue, userValue); - }, - - '$type': function (doc, userValue, parsedField, docFieldValue) { - return typeMatch(docFieldValue, userValue); } - }; - // return true if the given doc matches the supplied selector - function matchesSelector(doc, selector) { - /* istanbul ignore if */ - if (typeof selector !== 'object') { - // match the CouchDB error message - throw new Error('Selector error: expected a JSON object'); - } + 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); - selector = massageSelector(selector); - var row = { - 'doc': doc - }; + var cursor = attAndSeqStore.index('seq') + .openCursor(IDBKeyRange.only(seq)); - var rowsMatched = filterInMemoryFields([row], { 'selector': selector }, Object.keys(selector)); - return rowsMatched && rowsMatched.length === 1; + 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(); + } + }; + }; + }); } - function evalFilter(input) { - return scopeEval('"use strict";\nreturn ' + input + ';', {}); + function openTransactionSafely(idb, stores, mode) { + try { + return { + txn: idb.transaction(stores, mode) + }; + } catch (err) { + return { + error: err + }; + } } - function evalView(input) { - var code = [ - 'return function(doc) {', - ' "use strict";', - ' var emitted = false;', - ' var emit = function (a, b) {', - ' emitted = true;', - ' };', - ' var view = ' + input + ';', - ' view(doc);', - ' if (emitted) {', - ' return true;', - ' }', - '};' - ].join('\n'); + var changesHandler$$1 = new Changes(); - return scopeEval(code, {}); - } + function idbBulkDocs(dbOpts, req, opts, api, idb, callback) { + var docInfos = req.docs; + var txn; + var docStore; + var bySeqStore; + var attachStore; + var attachAndSeqStore; + var metaStore; + var docInfoError; + var metaDoc; - function validate(opts, callback) { - if (opts.selector) { - if (opts.filter && opts.filter !== '_selector') { - var filterName = typeof opts.filter === 'string' ? - opts.filter : 'function'; - return callback(new Error('selector invalid for filter "' + filterName + '"')); + for (var i = 0, len = docInfos.length; i < len; i++) { + var doc = docInfos[i]; + if (doc._id && isLocalId(doc._id)) { + continue; + } + doc = docInfos[i] = parseDoc(doc, opts.new_edits); + if (doc.error && !docInfoError) { + docInfoError = doc; } } - callback(); - } - function normalize(opts) { - if (opts.view && !opts.filter) { - opts.filter = '_view'; + if (docInfoError) { + return callback(docInfoError); } - if (opts.selector && !opts.filter) { - opts.filter = '_selector'; - } + var allDocsProcessed = false; + var docCountDelta = 0; + var results = new Array(docInfos.length); + var fetchedDocs = new ExportedMap(); + var preconditionErrored = false; + var blobType = api._meta.blobSupport ? 'blob' : 'base64'; - if (opts.filter && typeof opts.filter === 'string') { - if (opts.filter === '_view') { - opts.view = normalizeDesignDocFunctionName(opts.view); - } else { - opts.filter = normalizeDesignDocFunctionName(opts.filter); + preprocessAttachments(docInfos, blobType, function (err) { + if (err) { + return callback(err); } - } - } + startTransaction(); + }); - function shouldFilter(changesHandler, opts) { - return opts.filter && typeof opts.filter === 'string' && - !opts.doc_ids && !isRemote(changesHandler.db); - } + function startTransaction() { - function filter(changesHandler, opts) { - var callback = opts.complete; - if (opts.filter === '_view') { - if (!opts.view || typeof opts.view !== 'string') { - var err = createError(BAD_REQUEST, - '`view` filter parameter not found or invalid.'); - return callback(err); + var stores = [ + DOC_STORE, BY_SEQ_STORE, + ATTACH_STORE, + LOCAL_STORE, ATTACH_AND_SEQ_STORE, + META_STORE + ]; + var txnResult = openTransactionSafely(idb, stores, 'readwrite'); + if (txnResult.error) { + return callback(txnResult.error); } - // fetch a view from a design doc, make it behave like a filter - var viewName = parseDesignDocFunctionName(opts.view); - changesHandler.db.get('_design/' + viewName[0], function (err, ddoc) { - /* istanbul ignore if */ - if (changesHandler.isCancelled) { - return callback(null, {status: 'cancelled'}); - } - /* istanbul ignore next */ - if (err) { - return callback(generateErrorFromResponse(err)); - } - var mapFun = ddoc && ddoc.views && ddoc.views[viewName[1]] && - ddoc.views[viewName[1]].map; - if (!mapFun) { - return callback(createError(MISSING_DOC, - (ddoc.views ? 'missing json key: ' + viewName[1] : - 'missing json key: views'))); - } - opts.filter = evalView(mapFun); - changesHandler.doChanges(opts); - }); - } else if (opts.selector) { - opts.filter = function (doc) { - return matchesSelector(doc, opts.selector); + txn = txnResult.txn; + txn.onabort = 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); + metaStore = txn.objectStore(META_STORE); + + metaStore.get(META_STORE).onsuccess = function (e) { + metaDoc = e.target.result; + updateDocCountIfReady(); }; - changesHandler.doChanges(opts); - } else { - // fetch a filter from a design doc - var filterName = parseDesignDocFunctionName(opts.filter); - changesHandler.db.get('_design/' + filterName[0], function (err, ddoc) { - /* istanbul ignore if */ - if (changesHandler.isCancelled) { - return callback(null, {status: 'cancelled'}); - } - /* istanbul ignore next */ + + verifyAttachments(function (err) { if (err) { - return callback(generateErrorFromResponse(err)); - } - var filterFun = ddoc && ddoc.filters && ddoc.filters[filterName[1]]; - if (!filterFun) { - return callback(createError(MISSING_DOC, - ((ddoc && ddoc.filters) ? 'missing json key: ' + filterName[1] - : 'missing json key: filters'))); + preconditionErrored = true; + return callback(err); } - opts.filter = evalFilter(filterFun); - changesHandler.doChanges(opts); + fetchExistingDocs(); }); } - } - function applyChangesFilterPlugin(PouchDB) { - PouchDB._changesFilterPlugin = { - validate: validate, - normalize: normalize, - shouldFilter: shouldFilter, - filter: filter - }; - } + function onAllDocsProcessed() { + allDocsProcessed = true; + updateDocCountIfReady(); + } - // TODO: remove from pouchdb-core (breaking) - PouchDB.plugin(debugPouch); + function idbProcessDocs() { + processDocs(dbOpts.revs_limit, docInfos, api, fetchedDocs, + txn, results, writeDoc, opts, onAllDocsProcessed); + } - // TODO: remove from pouchdb-core (breaking) - PouchDB.plugin(applyChangesFilterPlugin); + function updateDocCountIfReady() { + if (!metaDoc || !allDocsProcessed) { + return; + } + // caching the docCount saves a lot of time in allDocs() and + // info(), which is why we go to all the trouble of doing this + metaDoc.docCount += docCountDelta; + metaStore.put(metaDoc); + } - PouchDB.version = version; + function fetchExistingDocs() { - 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' - ]); + if (!docInfos.length) { + return; + } - // 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' - ]); + var numFetched = 0; - function parseRevisionInfo(rev$$1) { - if (!/^\d+-./.test(rev$$1)) { - return createError(INVALID_REV); - } - var idx = rev$$1.indexOf('-'); - var left = rev$$1.substring(0, idx); - var right = rev$$1.substring(idx + 1); - return { - prefix: parseInt(left, 10), - id: right - }; - } + function checkDone() { + if (++numFetched === docInfos.length) { + idbProcessDocs(); + } + } - function makeRevTreeFromRevisions(revisions, opts) { - var pos = revisions.start - revisions.ids.length + 1; + function readMetadata(event) { + var metadata = decodeMetadata(event.target.result); - var revisionIds = revisions.ids; - var ids = [revisionIds[0], opts, []]; + if (metadata) { + fetchedDocs.set(metadata.id, metadata); + } + checkDone(); + } - for (var i = 1, len = revisionIds.length; i < len; i++) { - ids = [revisionIds[i], {status: 'missing'}, [ids]]; + for (var i = 0, len = docInfos.length; i < len; i++) { + var docInfo = docInfos[i]; + if (docInfo._id && isLocalId(docInfo._id)) { + checkDone(); // skip local docs + continue; + } + var req = docStore.get(docInfo.metadata.id); + req.onsuccess = readMetadata; + } } - return [{ - pos: pos, - ids: ids - }]; - } - - // Preprocess documents, parse their revisions, assign an id and a - // revision for new writes that are missing them, etc - function parseDoc(doc, newEdits) { + function complete() { + if (preconditionErrored) { + return; + } - var nRevNum; - var newRevId; - var revInfo; - var opts = {status: 'available'}; - if (doc._deleted) { - opts.deleted = true; + changesHandler$$1.notify(api._meta.name); + callback(null, results); } - if (newEdits) { - if (!doc._id) { - doc._id = uuid(); - } - newRevId = rev(); - 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 = makeRevTreeFromRevisions(doc._revisions, opts); - nRevNum = doc._revisions.start; - newRevId = doc._revisions.ids[0]; - } - if (!doc._rev_tree) { - revInfo = parseRevisionInfo(doc._rev); - if (revInfo.error) { - return revInfo; + function verifyAttachment(digest, callback) { + + var req = attachStore.get(digest); + req.onsuccess = function (e) { + if (!e.target.result) { + var err = createError(MISSING_STUB, + 'unknown stub attachment with digest ' + + digest); + err.status = 412; + callback(err); + } else { + callback(); } - nRevNum = revInfo.prefix; - newRevId = revInfo.id; - doc._rev_tree = [{ - pos: nRevNum, - ids: [newRevId, opts, []] - }]; - } + }; } - invalidIdError(doc._id); + function verifyAttachments(finish) { - doc._rev = nRevNum + '-' + newRevId; - var result = {metadata : {}, data : {}}; - for (var key in doc) { - /* istanbul ignore else */ - if (Object.prototype.hasOwnProperty.call(doc, key)) { - var specialKey = key[0] === '_'; - if (specialKey && !reservedWords[key]) { - var error = createError(DOC_VALIDATION, key); - error.message = DOC_VALIDATION.message + ': ' + key; - throw error; - } else if (specialKey && !dataWords[key]) { - result.metadata[key.slice(1)] = doc[key]; - } else { - result.data[key] = doc[key]; + 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(); + }); + }); } - return result; - } - var thisAtob = function (str) { - return atob(str); - }; + function writeDoc(docInfo, winningRev$$1, winningRevIsDeleted, newRevIsDeleted, + isUpdate, delta, resultsIdx, callback) { - var thisBtoa = function (str) { - return btoa(str); - }; + docInfo.metadata.winningRev = winningRev$$1; + docInfo.metadata.deleted = winningRevIsDeleted; - // Abstracts constructing a Blob object, so it also works in older - // browsers that don't support the native Blob constructor (e.g. - // old QtWebKit versions, Android < 4.4). - function createBlob(parts, properties) { - /* global BlobBuilder,MSBlobBuilder,MozBlobBuilder,WebKitBlobBuilder */ - parts = parts || []; - properties = properties || {}; - try { - return new Blob(parts, properties); - } catch (e) { - if (e.name !== "TypeError") { - throw e; + var doc = docInfo.data; + doc._id = docInfo.metadata.id; + doc._rev = docInfo.metadata.rev; + + if (newRevIsDeleted) { + doc._deleted = true; } - var Builder = typeof BlobBuilder !== 'undefined' ? BlobBuilder : - typeof MSBlobBuilder !== 'undefined' ? MSBlobBuilder : - typeof MozBlobBuilder !== 'undefined' ? MozBlobBuilder : - WebKitBlobBuilder; - var builder = new Builder(); - for (var i = 0; i < parts.length; i += 1) { - builder.append(parts[i]); + + var hasAttachments = doc._attachments && + Object.keys(doc._attachments).length; + if (hasAttachments) { + return writeAttachments(docInfo, winningRev$$1, winningRevIsDeleted, + isUpdate, resultsIdx, callback); } - return builder.getBlob(properties.type); - } - } - // From http://stackoverflow.com/questions/14967647/ (continues on next line) - // encode-decode-image-with-base64-breaks-image (2013-04-21) - function binaryStringToArrayBuffer(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); + docCountDelta += delta; + updateDocCountIfReady(); + + finishDoc(docInfo, winningRev$$1, winningRevIsDeleted, + isUpdate, resultsIdx, callback); } - return buf; - } - function binStringToBluffer(binString, type) { - return createBlob([binaryStringToArrayBuffer(binString)], {type: type}); - } + function finishDoc(docInfo, winningRev$$1, winningRevIsDeleted, + isUpdate, resultsIdx, callback) { - function b64ToBluffer(b64, type) { - return binStringToBluffer(thisAtob(b64), type); - } + var doc = docInfo.data; + var metadata = docInfo.metadata; - //Can't find original post, but this is close - //http://stackoverflow.com/questions/6965107/ (continues on next line) - //converting-between-strings-and-arraybuffers - function arrayBufferToBinaryString(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; - } + doc._doc_id_rev = metadata.id + '::' + metadata.rev; + delete doc._id; + delete doc._rev; - // shim for browsers that don't support it - function readAsBinaryString(blob, callback) { - if (typeof FileReader === 'undefined') { - // fix for Firefox in a web worker - // https://bugzilla.mozilla.org/show_bug.cgi?id=901097 - return callback(arrayBufferToBinaryString( - new FileReaderSync().readAsArrayBuffer(blob))); - } + function afterPutDoc(e) { + var revsToDelete = docInfo.stemmedRevs || []; - var reader = new FileReader(); - var hasBinaryString = typeof reader.readAsBinaryString === 'function'; - reader.onloadend = function (e) { - var result = e.target.result || ''; - if (hasBinaryString) { - return callback(result); + if (isUpdate && api.auto_compaction) { + revsToDelete = revsToDelete.concat(compactTree(docInfo.metadata)); + } + + if (revsToDelete && revsToDelete.length) { + compactRevs(revsToDelete, docInfo.metadata.id, txn); + } + + metadata.seq = e.target.result; + // Current _rev is calculated from _rev_tree on read + // delete metadata.rev; + var metadataToStore = encodeMetadata(metadata, winningRev$$1, + winningRevIsDeleted); + var metaDataReq = docStore.put(metadataToStore); + metaDataReq.onsuccess = afterPutMetadata; } - callback(arrayBufferToBinaryString(result)); - }; - if (hasBinaryString) { - reader.readAsBinaryString(blob); - } else { - reader.readAsArrayBuffer(blob); - } - } - function blobToBinaryString(blobOrBuffer, callback) { - readAsBinaryString(blobOrBuffer, function (bin) { - callback(bin); - }); - } + 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 blobToBase64(blobOrBuffer, callback) { - blobToBinaryString(blobOrBuffer, function (base64) { - callback(thisBtoa(base64)); - }); - } + function afterPutMetadata() { + results[resultsIdx] = { + ok: true, + id: metadata.id, + rev: metadata.rev + }; + fetchedDocs.set(docInfo.metadata.id, docInfo.metadata); + insertAttachmentMappings(docInfo, metadata.seq, callback); + } - // simplified API. universal browser support is assumed - function readAsArrayBuffer(blob, callback) { - if (typeof FileReader === 'undefined') { - // fix for Firefox in a web worker: - // https://bugzilla.mozilla.org/show_bug.cgi?id=901097 - return callback(new FileReaderSync().readAsArrayBuffer(blob)); + var putReq = bySeqStore.put(doc); + + putReq.onsuccess = afterPutDoc; + putReq.onerror = afterPutDocError; } - var reader = new FileReader(); - reader.onloadend = function (e) { - var result = e.target.result || new ArrayBuffer(0); - callback(result); - }; - reader.readAsArrayBuffer(blob); - } + function writeAttachments(docInfo, winningRev$$1, winningRevIsDeleted, + isUpdate, resultsIdx, callback) { - // this is not used in the browser - var setImmediateShim = global.setImmediate || global.setTimeout; - var MD5_CHUNK_SIZE = 32768; + var doc = docInfo.data; - function rawToBase64(raw) { - return thisBtoa(raw); - } + var numDone = 0; + var attachments = Object.keys(doc._attachments); - function sliceBlob(blob, start, end) { - if (blob.webkitSlice) { - return blob.webkitSlice(start, end); - } - return blob.slice(start, end); - } + function collectResults() { + if (numDone === attachments.length) { + finishDoc(docInfo, winningRev$$1, winningRevIsDeleted, + isUpdate, resultsIdx, callback); + } + } - function appendBlob(buffer, blob, start, end, callback) { - if (start > 0 || end < blob.size) { - // only slice blob if we really need to - blob = sliceBlob(blob, start, end); - } - readAsArrayBuffer(blob, function (arrayBuffer) { - buffer.append(arrayBuffer); - callback(); - }); - } + function attachmentSaved() { + numDone++; + collectResults(); + } - function appendString(buffer, string, start, end, callback) { - if (start > 0 || end < string.length) { - // only create a substring if we really need to - string = string.substring(start, end); + attachments.forEach(function (key) { + var att = docInfo.data._attachments[key]; + if (!att.stub) { + var data = att.data; + delete att.data; + att.revpos = parseInt(winningRev$$1, 10); + var digest = att.digest; + saveAttachment(digest, data, attachmentSaved); + } else { + numDone++; + collectResults(); + } + }); } - buffer.appendBinary(string); - callback(); - } - function binaryMd5(data, callback) { - var inputIsString = typeof data === 'string'; - var len = inputIsString ? data.length : data.size; - 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(); + // map seqs to attachment digests, which + // we will need later during compaction + function insertAttachmentMappings(docInfo, seq, callback) { - var append = inputIsString ? appendString : appendBlob; + var attsAdded = 0; + var attsToAdd = Object.keys(docInfo.data._attachments || {}); - function next() { - setImmediateShim(loadNextChunk); - } + if (!attsToAdd.length) { + return callback(); + } - function done() { - var raw = buffer.end(true); - var base64 = rawToBase64(raw); - callback(base64); - buffer.destroy(); - } + function checkDone() { + if (++attsAdded === attsToAdd.length) { + callback(); + } + } - function loadNextChunk() { - var start = currentChunk * chunkSize; - var end = start + chunkSize; - currentChunk++; - if (currentChunk < chunks) { - append(buffer, data, start, end, next); - } else { - append(buffer, data, start, end, done); + 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 } } - loadNextChunk(); - } - function stringMd5(string) { - return Md5.hash(string); - } + function saveAttachment(digest, data, callback) { - function parseBase64(data) { - try { - return thisAtob(data); - } catch (e) { - var err = createError(BAD_ARG, - 'Attachment is not a valid base64 string'); - return {error: err}; + + 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; + }; } } - function preprocessString(att, blobType, callback) { - var asBinary = parseBase64(att.data); - if (asBinary.error) { - return callback(asBinary.error); - } + // Abstraction over IDBCursor and getAll()/getAllKeys() that allows us to batch our operations + // while falling back to a normal IDBCursor operation on browsers that don't support getAll() or + // getAllKeys(). This allows for a much faster implementation than just straight-up cursors, because + // we're not processing each document one-at-a-time. + function runBatchedCursor(objectStore, keyRange, descending, batchSize, onBatch) { - att.length = asBinary.length; - if (blobType === 'blob') { - att.data = binStringToBluffer(asBinary, att.content_type); - } else if (blobType === 'base64') { - att.data = thisBtoa(asBinary); - } else { // binary - att.data = asBinary; - } - binaryMd5(asBinary, function (result) { - att.digest = 'md5-' + result; - callback(); - }); - } + // Bail out of getAll()/getAllKeys() in the following cases: + // 1) either method is unsupported - we need both + // 2) batchSize is 1 (might as well use IDBCursor), or batchSize is -1 (i.e. batchSize unlimited, + // not really clear the user wants a batched approach where the entire DB is read into memory, + // perhaps they are filtering on a per-doc basis) + // 3) descending – no real way to do this via getAll()/getAllKeys() - function preprocessBlob(att, blobType, callback) { - binaryMd5(att.data, function (md5) { - att.digest = 'md5-' + md5; - // size is for blobs (browser), length is for buffers (node) - att.length = att.data.size || att.data.length || 0; - if (blobType === 'binary') { - blobToBinaryString(att.data, function (binString) { - att.data = binString; - callback(); - }); - } else if (blobType === 'base64') { - blobToBase64(att.data, function (b64) { - att.data = b64; - callback(); - }); - } else { - callback(); - } - }); - } + var useGetAll = typeof objectStore.getAll === 'function' && + typeof objectStore.getAllKeys === 'function' && + batchSize > 1 && !descending; - function preprocessAttachment(att, blobType, callback) { - if (att.stub) { - return callback(); + var keysBatch; + var valuesBatch; + var pseudoCursor; + + function onGetAll(e) { + valuesBatch = e.target.result; + if (keysBatch) { + onBatch(keysBatch, valuesBatch, pseudoCursor); + } } - if (typeof att.data === 'string') { // input is a base64 string - preprocessString(att, blobType, callback); - } else { // input is a blob - preprocessBlob(att, blobType, callback); + + function onGetAllKeys(e) { + keysBatch = e.target.result; + if (valuesBatch) { + onBatch(keysBatch, valuesBatch, pseudoCursor); + } } - } - function preprocessAttachments(docInfos, blobType, callback) { + function continuePseudoCursor() { + if (!keysBatch.length) { // no more results + return onBatch(); + } + // fetch next batch, exclusive start + var lastKey = keysBatch[keysBatch.length - 1]; + var newKeyRange; + if (keyRange && keyRange.upper) { + try { + newKeyRange = IDBKeyRange.bound(lastKey, keyRange.upper, + true, keyRange.upperOpen); + } catch (e) { + if (e.name === "DataError" && e.code === 0) { + return onBatch(); // we're done, startkey and endkey are equal + } + } + } else { + newKeyRange = IDBKeyRange.lowerBound(lastKey, true); + } + keyRange = newKeyRange; + keysBatch = null; + valuesBatch = null; + objectStore.getAll(keyRange, batchSize).onsuccess = onGetAll; + objectStore.getAllKeys(keyRange, batchSize).onsuccess = onGetAllKeys; + } - if (!docInfos.length) { - return callback(); + function onCursor(e) { + var cursor = e.target.result; + if (!cursor) { // done + return onBatch(); + } + // regular IDBCursor acts like a batch where batch size is always 1 + onBatch([cursor.key], [cursor.value], cursor); } - var docv = 0; - var overallErr; + if (useGetAll) { + pseudoCursor = {"continue": continuePseudoCursor}; + objectStore.getAll(keyRange, batchSize).onsuccess = onGetAll; + objectStore.getAllKeys(keyRange, batchSize).onsuccess = onGetAllKeys; + } else if (descending) { + objectStore.openCursor(keyRange, 'prev').onsuccess = onCursor; + } else { + objectStore.openCursor(keyRange).onsuccess = onCursor; + } + } - docInfos.forEach(function (docInfo) { - var attachments = docInfo.data && docInfo.data._attachments ? - Object.keys(docInfo.data._attachments) : []; - var recv = 0; + // simple shim for objectStore.getAll(), falling back to IDBCursor + function getAll(objectStore, keyRange, onSuccess) { + if (typeof objectStore.getAll === 'function') { + // use native getAll + objectStore.getAll(keyRange).onsuccess = onSuccess; + return; + } + // fall back to cursors + var values = []; - if (!attachments.length) { - return done(); + function onCursor(e) { + var cursor = e.target.result; + if (cursor) { + values.push(cursor.value); + cursor.continue(); + } else { + onSuccess({ + target: { + result: values + } + }); } + } - function processedAttachment(err) { - overallErr = err; - recv++; - if (recv === attachments.length) { - done(); - } - } + objectStore.openCursor(keyRange).onsuccess = onCursor; + } - for (var key in docInfo.data._attachments) { - if (docInfo.data._attachments.hasOwnProperty(key)) { - preprocessAttachment(docInfo.data._attachments[key], - blobType, processedAttachment); + function createKeyRange(start, end, inclusiveEnd, key, descending) { + try { + if (start && end) { + if (descending) { + return IDBKeyRange.bound(end, start, !inclusiveEnd, false); + } else { + return IDBKeyRange.bound(start, end, false, !inclusiveEnd); } - } - }); - - function done() { - docv++; - if (docInfos.length === docv) { - if (overallErr) { - callback(overallErr); + } else if (start) { + if (descending) { + return IDBKeyRange.upperBound(start); } else { - callback(); + return IDBKeyRange.lowerBound(start); + } + } else if (end) { + if (descending) { + return IDBKeyRange.lowerBound(end, !inclusiveEnd); + } else { + return IDBKeyRange.upperBound(end, !inclusiveEnd); } + } else if (key) { + return IDBKeyRange.only(key); } + } catch (e) { + return {error: e}; } + return null; } - function updateDoc(revLimit, prev, docInfo, results, - i, cb, writeDoc, newEdits) { + function idbAllDocs(opts, idb, 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; - if (revExists(prev.rev_tree, docInfo.metadata.rev)) { - results[i] = docInfo; - return cb(); + var keyRange = createKeyRange(start, end, inclusiveEnd, key, opts.descending); + var keyRangeError = keyRange && keyRange.error; + if (keyRangeError && !(keyRangeError.name === "DataError" && + keyRangeError.code === 0)) { + // DataError with error code 0 indicates start is less than end, so + // can just do an empty query. Else need to throw + return callback(createError(IDB_ERROR, + keyRangeError.name, keyRangeError.message)); } - // sometimes this is pre-calculated. historically not always - var previousWinningRev = prev.winningRev || winningRev(prev); - var previouslyDeleted = 'deleted' in prev ? prev.deleted : - isDeleted(prev, previousWinningRev); - var deleted = 'deleted' in docInfo.metadata ? docInfo.metadata.deleted : - isDeleted(docInfo.metadata); - var isRoot = /^1-/.test(docInfo.metadata.rev); + var stores = [DOC_STORE, BY_SEQ_STORE, META_STORE]; - if (previouslyDeleted && !deleted && newEdits && isRoot) { - var newDoc = docInfo.data; - newDoc._rev = previousWinningRev; - newDoc._id = docInfo.metadata.id; - docInfo = parseDoc(newDoc, newEdits); + if (opts.attachments) { + stores.push(ATTACH_STORE); } + var txnResult = openTransactionSafely(idb, stores, 'readonly'); + if (txnResult.error) { + return callback(txnResult.error); + } + var txn = txnResult.txn; + txn.oncomplete = onTxnComplete; + txn.onabort = idbError(callback); + var docStore = txn.objectStore(DOC_STORE); + var seqStore = txn.objectStore(BY_SEQ_STORE); + var metaStore = txn.objectStore(META_STORE); + var docIdRevIndex = seqStore.index('_doc_id_rev'); + var results = []; + var docCount; - var merged = merge(prev.rev_tree, docInfo.metadata.rev_tree[0], revLimit); - - var inConflict = newEdits && (( - (previouslyDeleted && deleted && merged.conflicts !== 'new_leaf') || - (!previouslyDeleted && merged.conflicts !== 'new_leaf') || - (previouslyDeleted && !deleted && merged.conflicts === 'new_branch'))); + metaStore.get(META_STORE).onsuccess = function (e) { + docCount = e.target.result.docCount; + }; - if (inConflict) { - var err = createError(REV_CONFLICT); - results[i] = err; - return cb(); + // if the user specifies include_docs=true, then we don't + // want to block the main cursor while we're fetching the doc + function fetchDocAsynchronously(metadata, row, winningRev$$1) { + var key = metadata.id + "::" + winningRev$$1; + docIdRevIndex.get(key).onsuccess = function onGetDoc(e) { + row.doc = decodeDoc(e.target.result); + if (opts.conflicts) { + var conflicts = collectConflicts(metadata); + if (conflicts.length) { + row.doc._conflicts = conflicts; + } + } + fetchAttachmentsIfNecessary(row.doc, opts, txn); + }; } - var newRev = docInfo.metadata.rev; - docInfo.metadata.rev_tree = merged.tree; - docInfo.stemmedRevs = merged.stemmedRevs || []; - /* istanbul ignore else */ - if (prev.rev_map) { - docInfo.metadata.rev_map = prev.rev_map; // used only by leveldb + function allDocsInner(winningRev$$1, metadata) { + var row = { + id: metadata.id, + key: metadata.id, + value: { + rev: winningRev$$1 + } + }; + var deleted = metadata.deleted; + if (opts.deleted === 'ok') { + results.push(row); + // deleted docs are okay with "keys" requests + if (deleted) { + row.value.deleted = true; + row.doc = null; + } else if (opts.include_docs) { + fetchDocAsynchronously(metadata, row, winningRev$$1); + } + } else if (!deleted && skip-- <= 0) { + results.push(row); + if (opts.include_docs) { + fetchDocAsynchronously(metadata, row, winningRev$$1); + } + } } - // recalculate - var winningRev$$1 = winningRev(docInfo.metadata); - var winningRevIsDeleted = isDeleted(docInfo.metadata, winningRev$$1); - - // calculate the total number of documents that were added/removed, - // from the perspective of total_rows/doc_count - var delta = (previouslyDeleted === winningRevIsDeleted) ? 0 : - previouslyDeleted < winningRevIsDeleted ? -1 : 1; - - var newRevIsDeleted; - if (newRev === winningRev$$1) { - // if the new rev is the same as the winning rev, we can reuse that value - newRevIsDeleted = winningRevIsDeleted; - } else { - // if they're not the same, then we need to recalculate - newRevIsDeleted = isDeleted(docInfo.metadata, newRev); + function processBatch(batchValues) { + for (var i = 0, len = batchValues.length; i < len; i++) { + if (results.length === limit) { + break; + } + var batchValue = batchValues[i]; + var metadata = decodeMetadata(batchValue); + var winningRev$$1 = metadata.winningRev; + allDocsInner(winningRev$$1, metadata); + } } - writeDoc(docInfo, winningRev$$1, winningRevIsDeleted, newRevIsDeleted, - true, delta, i, cb); - } - - function rootIsMissing(docInfo) { - return docInfo.metadata.rev_tree[0].ids[1].status === 'missing'; - } - - function processDocs(revLimit, docInfos, api, fetchedDocs, tx, results, - writeDoc, opts, overallCallback) { - - // Default to 1000 locally - revLimit = revLimit || 1000; - - function insertDoc(docInfo, resultsIdx, callback) { - // Cant insert new deleted documents - var winningRev$$1 = winningRev(docInfo.metadata); - var deleted = isDeleted(docInfo.metadata, winningRev$$1); - if ('was_delete' in opts && deleted) { - results[resultsIdx] = createError(MISSING_DOC, 'deleted'); - return callback(); + function onBatch(batchKeys, batchValues, cursor) { + if (!cursor) { + return; } - - // 4712 - detect whether a new document was inserted with a _rev - var inConflict = newEdits && rootIsMissing(docInfo); - - if (inConflict) { - var err = createError(REV_CONFLICT); - results[resultsIdx] = err; - return callback(); + processBatch(batchValues); + if (results.length < limit) { + cursor.continue(); } - - var delta = deleted ? 0 : 1; - - writeDoc(docInfo, winningRev$$1, deleted, deleted, false, - delta, resultsIdx, callback); } - var newEdits = opts.new_edits; - var idsToDocs = new ExportedMap(); - - var docsDone = 0; - var docsToDo = docInfos.length; - - function checkAllDocsDone() { - if (++docsDone === docsToDo && overallCallback) { - overallCallback(); + function onGetAll(e) { + var values = e.target.result; + if (opts.descending) { + values = values.reverse(); } + processBatch(values); } - docInfos.forEach(function (currentDoc, resultsIdx) { - - if (currentDoc._id && isLocalId(currentDoc._id)) { - var fun = currentDoc._deleted ? '_removeLocal' : '_putLocal'; - api[fun](currentDoc, {ctx: tx}, function (err, res) { - results[resultsIdx] = err || res; - checkAllDocsDone(); - }); - return; - } + function onResultsReady() { + callback(null, { + total_rows: docCount, + offset: opts.skip, + rows: results + }); + } - var id = currentDoc.metadata.id; - if (idsToDocs.has(id)) { - docsToDo--; // duplicate - idsToDocs.get(id).push([currentDoc, resultsIdx]); + function onTxnComplete() { + if (opts.attachments) { + postProcessAttachments(results, opts.binary).then(onResultsReady); } else { - idsToDocs.set(id, [[currentDoc, resultsIdx]]); + onResultsReady(); } - }); + } - // 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; + // don't bother doing any requests if start > end or limit === 0 + if (keyRangeError || limit === 0) { + return; + } + if (limit === -1) { // just fetch everything + return getAll(docStore, keyRange, onGetAll); + } + // else do a cursor + // choose a batch size based on the skip, since we'll need to skip that many + runBatchedCursor(docStore, keyRange, opts.descending, limit + skip, onBatch); + } - function docWritten() { - if (++numDone < docs.length) { - nextDoc(); - } else { - checkAllDocsDone(); - } - } - function nextDoc() { - var value = docs[numDone]; - var currentDoc = value[0]; - var resultsIdx = value[1]; + // + // Blobs are not supported in all versions of IndexedDB, notably + // Chrome <37 and Android <5. In those versions, storing a blob will throw. + // + // Various other blob bugs exist in Chrome v37-42 (inclusive). + // Detecting them is expensive and confusing to users, and Chrome 37-42 + // is at very low usage worldwide, so we do a hacky userAgent check instead. + // + // content-type bug: https://code.google.com/p/chromium/issues/detail?id=408120 + // 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) { + return new PouchPromise$1(function (resolve) { + var blob$$1 = createBlob(['']); + var req = txn.objectStore(DETECT_BLOB_SUPPORT_STORE).put(blob$$1, 'key'); - if (fetchedDocs.has(id)) { - updateDoc(revLimit, fetchedDocs.get(id), currentDoc, results, - resultsIdx, docWritten, writeDoc, newEdits); - } else { - // Ensure stemming applies to new writes as well - var merged = merge([], currentDoc.metadata.rev_tree[0], revLimit); - currentDoc.metadata.rev_tree = merged.tree; - currentDoc.stemmedRevs = merged.stemmedRevs || []; - insertDoc(currentDoc, resultsIdx, docWritten); - } - } - nextDoc(); + req.onsuccess = function () { + var matchedChrome = navigator.userAgent.match(/Chrome\/(\d+)/); + var matchedEdge = navigator.userAgent.match(/Edge\//); + // MS Edge pretends to be Chrome 42: + // https://msdn.microsoft.com/en-us/library/hh869301%28v=vs.85%29.aspx + resolve(matchedEdge || !matchedChrome || + parseInt(matchedChrome[1], 10) >= 43); + }; + + txn.onabort = function (e) { + // If the transaction aborts now its due to not being able to + // write to the database, likely due to the disk being full + e.preventDefault(); + e.stopPropagation(); + resolve(false); + }; + }).catch(function () { + return false; // error, so assume unsupported }); } - // IndexedDB requires a versioned database structure, so we use the - // version here to manage migrations. - var ADAPTER_VERSION = 5; + function countDocs(txn, cb) { + var index = txn.objectStore(DOC_STORE).index('deletedOrLocal'); + index.count(IDBKeyRange.only('0')).onsuccess = function (e) { + cb(e.target.result); + }; + } - // The object stores created for each database - // DOC_STORE stores the document meta data, its revision history and state - // Keyed by document id - var DOC_STORE = 'document-store'; - // BY_SEQ_STORE stores a particular version of a document, keyed by its - // sequence id - var BY_SEQ_STORE = 'by-sequence'; - // Where we store attachments - var ATTACH_STORE = 'attach-store'; - // Where we store many-to-many relations - // between attachment digests and seqs - var ATTACH_AND_SEQ_STORE = 'attach-seq-store'; + // This task queue ensures that IDB open calls are done in their own tick + // and sequentially - i.e. we wait for the async IDB open to *fully* complete + // before calling the next one. This works around IE/Edge race conditions in IDB. - // Where we store database-wide meta data in a single record - // keyed by id: META_STORE - var META_STORE = 'meta-store'; - // Where we store local documents - var LOCAL_STORE = 'local-store'; - // Where we detect blob support - var DETECT_BLOB_SUPPORT_STORE = 'detect-blob-support'; + var running = false; + var queue = []; - function safeJsonParse(str) { - // This try/catch guards against stack overflow errors. - // JSON.parse() is faster than vuvuzela.parse() but vuvuzela - // cannot overflow. + function tryCode(fun, err, res, PouchDB) { try { - return JSON.parse(str); - } catch (e) { - /* istanbul ignore next */ - return vuvuzela.parse(str); + fun(err, res); + } catch (err) { + // Shouldn't happen, but in some odd cases + // IndexedDB implementations might throw a sync + // error, in which case this will at least log it. + PouchDB.emit('error', err); } } - function safeJsonStringify(json) { - try { - return JSON.stringify(json); - } catch (e) { - /* istanbul ignore next */ - return vuvuzela.stringify(json); + function applyNext() { + if (running || !queue.length) { + return; } + running = true; + queue.shift()(); } - function idbError(callback) { - return function (evt) { - var message = 'unknown_error'; - if (evt.target && evt.target.error) { - message = evt.target.error.name || evt.target.error.message; - } - callback(createError(IDB_ERROR, message, evt.type)); - }; + function enqueueTask(action, callback, PouchDB) { + queue.push(function runAction() { + action(function runCallback(err, res) { + tryCode(callback, err, res, PouchDB); + running = false; + nextTick(function runNext() { + applyNext(PouchDB); + }); + }); + }); + applyNext(); } - // 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. - function encodeMetadata(metadata, winningRev, deleted) { - return { - data: safeJsonStringify(metadata), - winningRev: winningRev, - deletedOrLocal: deleted ? '1' : '0', - seq: metadata.seq, // highest seq for this doc - id: metadata.id - }; - } + function changes(opts, api, dbName, idb) { + opts = clone(opts); - function decodeMetadata(storedObject) { - if (!storedObject) { - return null; + if (opts.continuous) { + var id = dbName + ':' + uuid(); + changesHandler$$1.addListener(dbName, id, api, opts); + changesHandler$$1.notify(dbName); + return { + cancel: function () { + changesHandler$$1.removeListener(dbName, id); + } + }; } - var metadata = safeJsonParse(storedObject.data); - metadata.winningRev = storedObject.winningRev; - metadata.deleted = 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. - function decodeDoc(doc) { - if (!doc) { - return doc; - } - var idx = doc._doc_id_rev.lastIndexOf(':'); - 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; - } + var docIds = opts.doc_ids && new ExportedSet(opts.doc_ids); - // Read a blob from the database, encoding as necessary - // and translating from base64 if the IDB doesn't support - // native Blobs - function readBlobData(body, type, asBlob, callback) { - if (asBlob) { - if (!body) { - callback(createBlob([''], {type: type})); - } else if (typeof body !== 'string') { // we have blob support - callback(body); - } else { // no blob support - callback(b64ToBluffer(body, type)); - } - } else { // as base64 string - if (!body) { - callback(''); - } else if (typeof body !== 'string') { // we have blob support - readAsBinaryString(body, function (binary) { - callback(thisBtoa(binary)); - }); - } else { // no blob support - callback(body); - } - } - } + opts.since = opts.since || 0; + var lastSeq = opts.since; - function fetchAttachmentsIfNecessary(doc, opts, txn, cb) { - var attachments = Object.keys(doc._attachments || {}); - if (!attachments.length) { - return cb && cb(); + var limit = 'limit' in opts ? opts.limit : -1; + if (limit === 0) { + limit = 1; // per CouchDB _changes spec } - var numDone = 0; - - function checkDone() { - if (++numDone === attachments.length && cb) { - cb(); - } + var returnDocs; + if ('return_docs' in opts) { + returnDocs = opts.return_docs; + } else if ('returnDocs' in opts) { + // TODO: Remove 'returnDocs' in favor of 'return_docs' in a future release + returnDocs = opts.returnDocs; + } else { + returnDocs = true; } - function fetchAttachment(doc, att) { - var attObj = doc._attachments[att]; - var digest = attObj.digest; - var req = txn.objectStore(ATTACH_STORE).get(digest); - req.onsuccess = function (e) { - attObj.body = e.target.result.body; - checkDone(); - }; - } + var results = []; + var numResults = 0; + var filter = filterChange(opts); + var docIdsToMetadata = new ExportedMap(); - attachments.forEach(function (att) { - if (opts.attachments && opts.include_docs) { - fetchAttachment(doc, att); - } else { - doc._attachments[att].stub = true; - checkDone(); + var txn; + var bySeqStore; + var docStore; + var docIdRevIndex; + + function onBatch(batchKeys, batchValues, cursor) { + if (!cursor || !batchKeys.length) { // done + return; } - }); - } - // 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 - function postProcessAttachments(results, asBlob) { - return PouchPromise.all(results.map(function (row) { - if (row.doc && row.doc._attachments) { - var attNames = Object.keys(row.doc._attachments); - return PouchPromise.all(attNames.map(function (att) { - var attObj = row.doc._attachments[att]; - if (!('body' in attObj)) { // already processed - return; + var winningDocs = new Array(batchKeys.length); + var metadatas = new Array(batchKeys.length); + + function processMetadataAndWinningDoc(metadata, winningDoc) { + var change = opts.processChange(winningDoc, metadata, opts); + lastSeq = change.seq = metadata.seq; + + var filtered = filter(change); + if (typeof filtered === 'object') { // anything but true/false indicates error + return opts.complete(filtered); + } + + if (filtered) { + numResults++; + if (returnDocs) { + results.push(change); } - var body = attObj.body; - var type = attObj.content_type; - return new PouchPromise(function (resolve) { - readBlobData(body, type, asBlob, function (data) { - row.doc._attachments[att] = $inject_Object_assign( - pick(attObj, ['digest', 'content_type']), - {data: data} - ); - resolve(); + // process the attachment immediately + // for the benefit of live listeners + if (opts.attachments && opts.include_docs) { + fetchAttachmentsIfNecessary(winningDoc, opts, txn, function () { + postProcessAttachments([change], opts.binary).then(function () { + opts.onChange(change); + }); }); - }); - })); + } else { + opts.onChange(change); + } + } } - })); - } - - function compactRevs(revs, docId, txn) { - var possiblyOrphanedDigests = []; - var seqStore = txn.objectStore(BY_SEQ_STORE); - var attStore = txn.objectStore(ATTACH_STORE); - var attAndSeqStore = txn.objectStore(ATTACH_AND_SEQ_STORE); - var count = revs.length; + function onBatchDone() { + for (var i = 0, len = winningDocs.length; i < len; i++) { + if (numResults === limit) { + break; + } + var winningDoc = winningDocs[i]; + if (!winningDoc) { + continue; + } + var metadata = metadatas[i]; + processMetadataAndWinningDoc(metadata, winningDoc); + } - function checkDone() { - count--; - if (!count) { // done processing all revs - deleteOrphanedAttachments(); + if (numResults !== limit) { + cursor.continue(); + } } - } - 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); + // Fetch all metadatas/winningdocs from this batch in parallel, then process + // them all only once all data has been collected. This is done in parallel + // because it's faster than doing it one-at-a-time. + var numDone = 0; + batchValues.forEach(function (value, i) { + var doc = decodeDoc(value); + var seq = batchKeys[i]; + fetchWinningDocAndMetadata(doc, seq, function (metadata, winningDoc) { + metadatas[i] = metadata; + winningDocs[i] = winningDoc; + if (++numDone === batchKeys.length) { + onBatchDone(); } - }; + }); }); } - revs.forEach(function (rev$$1) { - var index = seqStore.index('_doc_id_rev'); - var key = docId + "::" + rev$$1; - 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)); + function onGetMetadata(doc, seq, metadata, cb) { + if (metadata.seq !== seq) { + // some other seq is later + return cb(); + } - 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(); - } - }; - }; - }); - } + if (metadata.winningRev === doc._rev) { + // this is the winning doc + return cb(metadata, doc); + } - function openTransactionSafely(idb, stores, mode) { - try { - return { - txn: idb.transaction(stores, mode) - }; - } catch (err) { - return { - error: err + // fetch winning doc in separate request + var docIdRev = doc._id + '::' + metadata.winningRev; + var req = docIdRevIndex.get(docIdRev); + req.onsuccess = function (e) { + cb(metadata, decodeDoc(e.target.result)); }; } - } - - var changesHandler$$1 = new Changes(); - - function idbBulkDocs(dbOpts, req, opts, api, idb, callback) { - var docInfos = req.docs; - var txn; - var docStore; - var bySeqStore; - var attachStore; - var attachAndSeqStore; - var metaStore; - var docInfoError; - var metaDoc; - - for (var i = 0, len = docInfos.length; i < len; i++) { - var doc = docInfos[i]; - if (doc._id && isLocalId(doc._id)) { - continue; - } - doc = docInfos[i] = parseDoc(doc, opts.new_edits); - if (doc.error && !docInfoError) { - docInfoError = doc; - } - } - - if (docInfoError) { - return callback(docInfoError); - } - - var allDocsProcessed = false; - var docCountDelta = 0; - var results = new Array(docInfos.length); - var fetchedDocs = new ExportedMap(); - var preconditionErrored = false; - var blobType = api._meta.blobSupport ? 'blob' : 'base64'; - preprocessAttachments(docInfos, blobType, function (err) { - if (err) { - return callback(err); + function fetchWinningDocAndMetadata(doc, seq, cb) { + if (docIds && !docIds.has(doc._id)) { + return cb(); } - startTransaction(); - }); - - function startTransaction() { - var stores = [ - DOC_STORE, BY_SEQ_STORE, - ATTACH_STORE, - LOCAL_STORE, ATTACH_AND_SEQ_STORE, - META_STORE - ]; - var txnResult = openTransactionSafely(idb, stores, 'readwrite'); - if (txnResult.error) { - return callback(txnResult.error); + var metadata = docIdsToMetadata.get(doc._id); + if (metadata) { // cached + return onGetMetadata(doc, seq, metadata, cb); } - txn = txnResult.txn; - txn.onabort = 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); - metaStore = txn.objectStore(META_STORE); - - metaStore.get(META_STORE).onsuccess = function (e) { - metaDoc = e.target.result; - updateDocCountIfReady(); + // metadata not cached, have to go fetch it + docStore.get(doc._id).onsuccess = function (e) { + metadata = decodeMetadata(e.target.result); + docIdsToMetadata.set(doc._id, metadata); + onGetMetadata(doc, seq, metadata, cb); }; + } - verifyAttachments(function (err) { - if (err) { - preconditionErrored = true; - return callback(err); - } - fetchExistingDocs(); + function finish() { + opts.complete(null, { + results: results, + last_seq: lastSeq }); } - function onAllDocsProcessed() { - allDocsProcessed = true; - updateDocCountIfReady(); + function onTxnComplete() { + if (!opts.continuous && opts.attachments) { + // cannot guarantee that postProcessing was already done, + // so do it again + postProcessAttachments(results).then(finish); + } else { + finish(); + } } - function idbProcessDocs() { - processDocs(dbOpts.revs_limit, docInfos, api, fetchedDocs, - txn, results, writeDoc, opts, onAllDocsProcessed); + var objectStores = [DOC_STORE, BY_SEQ_STORE]; + if (opts.attachments) { + objectStores.push(ATTACH_STORE); } - - function updateDocCountIfReady() { - if (!metaDoc || !allDocsProcessed) { - return; - } - // caching the docCount saves a lot of time in allDocs() and - // info(), which is why we go to all the trouble of doing this - metaDoc.docCount += docCountDelta; - metaStore.put(metaDoc); + var txnResult = openTransactionSafely(idb, objectStores, 'readonly'); + if (txnResult.error) { + return opts.complete(txnResult.error); } + txn = txnResult.txn; + txn.onabort = idbError(opts.complete); + txn.oncomplete = onTxnComplete; - function fetchExistingDocs() { + bySeqStore = txn.objectStore(BY_SEQ_STORE); + docStore = txn.objectStore(DOC_STORE); + docIdRevIndex = bySeqStore.index('_doc_id_rev'); - if (!docInfos.length) { - return; - } + var keyRange = (opts.since && !opts.descending) ? + IDBKeyRange.lowerBound(opts.since, true) : null; - var numFetched = 0; + runBatchedCursor(bySeqStore, keyRange, opts.descending, limit, onBatch); + } - function checkDone() { - if (++numFetched === docInfos.length) { - idbProcessDocs(); - } - } + var cachedDBs = new ExportedMap(); + var blobSupportPromise; + var openReqList = new ExportedMap(); - function readMetadata(event) { - var metadata = decodeMetadata(event.target.result); + function IdbPouch(opts, callback) { + var api = this; - if (metadata) { - fetchedDocs.set(metadata.id, metadata); - } - checkDone(); - } + enqueueTask(function (thisCallback) { + init(api, opts, thisCallback); + }, callback, api.constructor); + } - for (var i = 0, len = docInfos.length; i < len; i++) { - var docInfo = docInfos[i]; - if (docInfo._id && isLocalId(docInfo._id)) { - checkDone(); // skip local docs - continue; - } - var req = docStore.get(docInfo.metadata.id); - req.onsuccess = readMetadata; - } - } + function init(api, opts, callback) { - function complete() { - if (preconditionErrored) { - return; - } + var dbName = opts.name; - changesHandler$$1.notify(api._meta.name); - callback(null, results); + var idb = null; + api._meta = null; + + // 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}); } - function verifyAttachment(digest, callback) { + // 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}); - var req = attachStore.get(digest); - req.onsuccess = function (e) { - if (!e.target.result) { - var err = createError(MISSING_STUB, - 'unknown stub attachment with digest ' + - digest); - err.status = 412; - callback(err); + docStore.openCursor().onsuccess = function (event) { + var cursor = event.target.result; + if (cursor) { + var metadata = cursor.value; + var deleted = isDeleted(metadata); + metadata.deletedOrLocal = deleted ? "1" : "0"; + docStore.put(metadata); + cursor.continue(); } else { callback(); } }; } - function verifyAttachments(finish) { + // 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 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; + 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 = isLocalId(docId); + var rev = 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(); } - checkDone(); - }); - }); + } else if (cb) { + cb(); + } + }; } - function writeDoc(docInfo, winningRev$$1, winningRevIsDeleted, newRevIsDeleted, - isUpdate, delta, resultsIdx, callback) { - - docInfo.metadata.winningRev = winningRev$$1; - docInfo.metadata.deleted = winningRevIsDeleted; - - var doc = docInfo.data; - doc._id = docInfo.metadata.id; - doc._rev = docInfo.metadata.rev; - - if (newRevIsDeleted) { - doc._deleted = true; - } + // 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}); + } - var hasAttachments = doc._attachments && - Object.keys(doc._attachments).length; - if (hasAttachments) { - return writeAttachments(docInfo, winningRev$$1, winningRevIsDeleted, - isUpdate, resultsIdx, callback); - } + // 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); - docCountDelta += delta; - updateDocCountIfReady(); + // 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 + } - finishDoc(docInfo, winningRev$$1, winningRevIsDeleted, - isUpdate, resultsIdx, callback); + 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(); + }; + }; } - function finishDoc(docInfo, winningRev$$1, winningRevIsDeleted, - isUpdate, resultsIdx, callback) { + // 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) { - var doc = docInfo.data; - var metadata = docInfo.metadata; + function decodeMetadataCompat(storedObject) { + if (!storedObject.data) { + // old format, when we didn't store it stringified + storedObject.deleted = storedObject.deletedOrLocal === '1'; + return storedObject; + } + return decodeMetadata(storedObject); + } - doc._doc_id_rev = metadata.id + '::' + metadata.rev; - delete doc._id; - delete doc._rev; + // 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); - function afterPutDoc(e) { - var revsToDelete = docInfo.stemmedRevs || []; + metadata.winningRev = metadata.winningRev || + winningRev(metadata); - if (isUpdate && api.auto_compaction) { - revsToDelete = revsToDelete.concat(compactTree(docInfo.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)); - if (revsToDelete && revsToDelete.length) { - compactRevs(revsToDelete, docInfo.metadata.id, txn); + 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(); + }; } - metadata.seq = e.target.result; - // Current _rev is calculated from _rev_tree on read - // delete metadata.rev; - var metadataToStore = encodeMetadata(metadata, winningRev$$1, - winningRevIsDeleted); - var metaDataReq = docStore.put(metadataToStore); - metaDataReq.onsuccess = afterPutMetadata; - } + function onGetMetadataSeq() { + var metadataToStore = encodeMetadata(metadata, + metadata.winningRev, metadata.deleted); - 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; - }; - } + var req = docStore.put(metadataToStore); + req.onsuccess = function () { + cursor.continue(); + }; + } - function afterPutMetadata() { - results[resultsIdx] = { - ok: true, - id: metadata.id, - rev: metadata.rev - }; - fetchedDocs.set(docInfo.metadata.id, docInfo.metadata); - insertAttachmentMappings(docInfo, metadata.seq, callback); - } + if (metadata.seq) { + return onGetMetadataSeq(); + } - var putReq = bySeqStore.put(doc); + fetchMetadataSeq(); + }; - putReq.onsuccess = afterPutDoc; - putReq.onerror = afterPutDocError; } - function writeAttachments(docInfo, winningRev$$1, winningRevIsDeleted, - isUpdate, resultsIdx, callback) { - + api.type = function () { + return 'idb'; + }; - var doc = docInfo.data; + api._id = toPromise(function (callback) { + callback(null, api._meta.instanceId); + }); - var numDone = 0; - var attachments = Object.keys(doc._attachments); + api._bulkDocs = function idb_bulkDocs(req, reqOpts, callback) { + idbBulkDocs(opts, req, reqOpts, api, idb, callback); + }; - function collectResults() { - if (numDone === attachments.length) { - finishDoc(docInfo, winningRev$$1, winningRevIsDeleted, - isUpdate, resultsIdx, 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.ctx; + if (!txn) { + var txnResult = openTransactionSafely(idb, + [DOC_STORE, BY_SEQ_STORE, ATTACH_STORE], 'readonly'); + if (txnResult.error) { + return callback(txnResult.error); } + txn = txnResult.txn; } - function attachmentSaved() { - numDone++; - collectResults(); + function finish() { + callback(err, {doc: doc, metadata: metadata, ctx: txn}); } - attachments.forEach(function (key) { - var att = docInfo.data._attachments[key]; - if (!att.stub) { - var data = att.data; - delete att.data; - att.revpos = parseInt(winningRev$$1, 10); - var digest = att.digest; - saveAttachment(digest, data, attachmentSaved); - } else { - numDone++; - collectResults(); + 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 = createError(MISSING_DOC, 'missing'); + return finish(); } - }); - } - // map seqs to attachment digests, which - // we will need later during compaction - function insertAttachmentMappings(docInfo, seq, callback) { + var rev; + if(!opts.rev) { + rev = metadata.winningRev; + var deleted = isDeleted(metadata); + if (deleted) { + err = createError(MISSING_DOC, "deleted"); + return finish(); + } + } else { + rev = opts.latest ? latest(opts.rev, metadata) : opts.rev; + } - var attsAdded = 0; - var attsToAdd = Object.keys(docInfo.data._attachments || {}); + var objectStore = txn.objectStore(BY_SEQ_STORE); + var key = metadata.id + '::' + rev; - if (!attsToAdd.length) { - return callback(); - } + objectStore.index('_doc_id_rev').get(key).onsuccess = function (e) { + doc = e.target.result; + if (doc) { + doc = decodeDoc(doc); + } + if (!doc) { + err = createError(MISSING_DOC, 'missing'); + return finish(); + } + finish(); + }; + }; + }; - function checkDone() { - if (++attsAdded === attsToAdd.length) { - callback(); + api._getAttachment = function (docId, attachId, attachment, opts, callback) { + var txn; + 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; - function add(att) { - var digest = docInfo.data._attachments[att].digest; - var req = attachAndSeqStore.put({ - seq: seq, - digestSeq: digest + '::' + seq + txn.objectStore(ATTACH_STORE).get(digest).onsuccess = function (e) { + var body = e.target.result.body; + readBlobData(body, type, opts.binary, function (blobData) { + callback(null, blobData); }); + }; + }; - 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 + api._info = function idb_info(callback) { + var updateSeq; + var docCount; + + var txnResult = openTransactionSafely(idb, [META_STORE, BY_SEQ_STORE], 'readonly'); + if (txnResult.error) { + return callback(txnResult.error); } - } + var txn = txnResult.txn; + txn.objectStore(META_STORE).get(META_STORE).onsuccess = function (e) { + docCount = e.target.result.docCount; + }; + txn.objectStore(BY_SEQ_STORE).openCursor(null, 'prev').onsuccess = function (e) { + var cursor = e.target.result; + updateSeq = cursor ? cursor.key : 0; + }; - function saveAttachment(digest, data, callback) { + txn.oncomplete = function () { + callback(null, { + doc_count: docCount, + update_seq: updateSeq, + // for debugging + idb_attachment_format: (api._meta.blobSupport ? 'binary' : 'base64') + }); + }; + }; + api._allDocs = function idb_allDocs(opts, callback) { + idbAllDocs(opts, idb, callback); + }; - var getKeyReq = attachStore.count(digest); - getKeyReq.onsuccess = function (e) { - var count = e.target.result; - if (count) { - return callback(); // already exists + api._changes = function idbChanges(opts) { + changes(opts, api, dbName, idb); + }; + + api._close = function (callback) { + // https://developer.mozilla.org/en-US/docs/IndexedDB/IDBDatabase#close + // "Returns immediately and closes the connection in a separate thread..." + idb.close(); + cachedDBs.delete(dbName); + 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(createError(MISSING_DOC)); + } else { + callback(null, doc.rev_tree); } - var newAtt = { - digest: digest, - body: data - }; - var putReq = attachStore.put(newAtt); - putReq.onsuccess = callback; }; - } - } + }; - // Abstraction over IDBCursor and getAll()/getAllKeys() that allows us to batch our operations - // while falling back to a normal IDBCursor operation on browsers that don't support getAll() or - // getAllKeys(). This allows for a much faster implementation than just straight-up cursors, because - // we're not processing each document one-at-a-time. - function runBatchedCursor(objectStore, keyRange, descending, batchSize, onBatch) { + // 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; - // Bail out of getAll()/getAllKeys() in the following cases: - // 1) either method is unsupported - we need both - // 2) batchSize is 1 (might as well use IDBCursor), or batchSize is -1 (i.e. batchSize unlimited, - // not really clear the user wants a batched approach where the entire DB is read into memory, - // perhaps they are filtering on a per-doc basis) - // 3) descending – no real way to do this via getAll()/getAllKeys() + var docStore = txn.objectStore(DOC_STORE); - var useGetAll = typeof objectStore.getAll === 'function' && - typeof objectStore.getAllKeys === 'function' && - batchSize > 1 && !descending; + docStore.get(docId).onsuccess = function (event) { + var metadata = decodeMetadata(event.target.result); + 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$$1 = metadata.winningRev; + var deleted = metadata.deleted; + txn.objectStore(DOC_STORE).put( + encodeMetadata(metadata, winningRev$$1, deleted)); + }; + txn.onabort = idbError(callback); + txn.oncomplete = function () { + callback(); + }; + }; - var keysBatch; - var valuesBatch; - var pseudoCursor; - function onGetAll(e) { - valuesBatch = e.target.result; - if (keysBatch) { - onBatch(keysBatch, valuesBatch, pseudoCursor); + 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); - function onGetAllKeys(e) { - keysBatch = e.target.result; - if (valuesBatch) { - onBatch(keysBatch, valuesBatch, pseudoCursor); - } - } + req.onerror = idbError(callback); + req.onsuccess = function (e) { + var doc = e.target.result; + if (!doc) { + callback(createError(MISSING_DOC)); + } else { + delete doc['_doc_id_rev']; // for backwards compat + callback(null, doc); + } + }; + }; - function continuePseudoCursor() { - if (!keysBatch.length) { // no more results - return onBatch(); + api._putLocal = function (doc, opts, callback) { + if (typeof opts === 'function') { + callback = opts; + opts = {}; } - // fetch next batch, exclusive start - var lastKey = keysBatch[keysBatch.length - 1]; - var newKeyRange; - if (keyRange && keyRange.upper) { - try { - newKeyRange = IDBKeyRange.bound(lastKey, keyRange.upper, - true, keyRange.upperOpen); - } catch (e) { - if (e.name === "DataError" && e.code === 0) { - return onBatch(); // we're done, startkey and endkey are equal - } - } + delete doc._revisions; // ignore this, trust the rev + var oldRev = doc._rev; + var id = doc._id; + if (!oldRev) { + doc._rev = '0-1'; } else { - newKeyRange = IDBKeyRange.lowerBound(lastKey, true); + doc._rev = '0-' + (parseInt(oldRev.split('-')[1], 10) + 1); } - keyRange = newKeyRange; - keysBatch = null; - valuesBatch = null; - objectStore.getAll(keyRange, batchSize).onsuccess = onGetAll; - objectStore.getAllKeys(keyRange, batchSize).onsuccess = onGetAllKeys; - } - function onCursor(e) { - var cursor = e.target.result; - if (!cursor) { // done - return onBatch(); + 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); + } + }; } - // regular IDBCursor acts like a batch where batch size is always 1 - onBatch([cursor.key], [cursor.value], cursor); - } - - if (useGetAll) { - pseudoCursor = {"continue": continuePseudoCursor}; - objectStore.getAll(keyRange, batchSize).onsuccess = onGetAll; - objectStore.getAllKeys(keyRange, batchSize).onsuccess = onGetAllKeys; - } else if (descending) { - objectStore.openCursor(keyRange, 'prev').onsuccess = onCursor; - } else { - objectStore.openCursor(keyRange).onsuccess = onCursor; - } - } - // simple shim for objectStore.getAll(), falling back to IDBCursor - function getAll(objectStore, keyRange, onSuccess) { - if (typeof objectStore.getAll === 'function') { - // use native getAll - objectStore.getAll(keyRange).onsuccess = onSuccess; - return; - } - // fall back to cursors - var values = []; - - function onCursor(e) { - var cursor = e.target.result; - if (cursor) { - values.push(cursor.value); - cursor.continue(); - } else { - onSuccess({ - target: { - result: values + 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(createError(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(createError(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); + } + }; } - } - - objectStore.openCursor(keyRange).onsuccess = onCursor; - } + }; - function allDocsKeys(keys, docStore, onBatch) { - // It's not guaranted to be returned in right order - var valuesBatch = []; - var count = 0; - keys.forEach(function (key, index) { - docStore.get(key).onsuccess = function (event) { - if (event.target.result) { - valuesBatch[index] = event.target.result; - } else { - valuesBatch[index] = {key: key, error: 'not_found'}; - } - count++; - if (count === keys.length) { - onBatch(keys, valuesBatch, {}); + api._removeLocal = function (doc, opts, callback) { + if (typeof opts === 'function') { + callback = opts; + opts = {}; + } + var tx = opts.ctx; + if (!tx) { + var txnResult = openTransactionSafely(idb, [LOCAL_STORE], 'readwrite'); + if (txnResult.error) { + return callback(txnResult.error); } - }; - }); - } + tx = txnResult.txn; + tx.oncomplete = function () { + if (ret) { + callback(null, ret); + } + }; + } + var ret; + var id = doc._id; + var oStore = tx.objectStore(LOCAL_STORE); + var req = oStore.get(id); - function createKeyRange(start, end, inclusiveEnd, key, descending) { - try { - if (start && end) { - if (descending) { - return IDBKeyRange.bound(end, start, !inclusiveEnd, false); - } else { - return IDBKeyRange.bound(start, end, false, !inclusiveEnd); - } - } else if (start) { - if (descending) { - return IDBKeyRange.upperBound(start); - } else { - return IDBKeyRange.lowerBound(start); - } - } else if (end) { - if (descending) { - return IDBKeyRange.lowerBound(end, !inclusiveEnd); + req.onerror = idbError(callback); + req.onsuccess = function (e) { + var oldDoc = e.target.result; + if (!oldDoc || oldDoc._rev !== doc._rev) { + callback(createError(MISSING_DOC)); } else { - return IDBKeyRange.upperBound(end, !inclusiveEnd); + oStore.delete(id); + ret = {ok: true, id: id, rev: '0-0'}; + if (opts.ctx) { // return immediately + callback(null, ret); + } } - } else if (key) { - return IDBKeyRange.only(key); - } - } catch (e) { - return {error: e}; - } - return null; - } + }; + }; - function idbAllDocs(opts, idb, 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 keys = 'keys' in opts ? opts.keys : false; - var skip = opts.skip || 0; - var limit = typeof opts.limit === 'number' ? opts.limit : -1; - var inclusiveEnd = opts.inclusive_end !== false; + api._destroy = function (opts, callback) { + changesHandler$$1.removeAllListeners(dbName); - var keyRange; - var keyRangeError; - if (!keys) { - keyRange = createKeyRange(start, end, inclusiveEnd, key, opts.descending); - keyRangeError = keyRange && keyRange.error; - if (keyRangeError && - !(keyRangeError.name === "DataError" && keyRangeError.code === 0)) { - // DataError with error code 0 indicates start is less than end, so - // can just do an empty query. Else need to throw - return callback(createError(IDB_ERROR, - keyRangeError.name, keyRangeError.message)); + //Close open request for "dbName" database to fix ie delay. + var openReq = openReqList.get(dbName); + if (openReq && openReq.result) { + openReq.result.close(); + cachedDBs.delete(dbName); } - } - - var stores = [DOC_STORE, BY_SEQ_STORE, META_STORE]; + var req = indexedDB.deleteDatabase(dbName); - if (opts.attachments) { - stores.push(ATTACH_STORE); - } - var txnResult = openTransactionSafely(idb, stores, 'readonly'); - if (txnResult.error) { - return callback(txnResult.error); - } - var txn = txnResult.txn; - txn.oncomplete = onTxnComplete; - txn.onabort = idbError(callback); - var docStore = txn.objectStore(DOC_STORE); - var seqStore = txn.objectStore(BY_SEQ_STORE); - var metaStore = txn.objectStore(META_STORE); - var docIdRevIndex = seqStore.index('_doc_id_rev'); - var results = []; - var docCount; - var updateSeq; + req.onsuccess = function () { + //Remove open request from the list. + openReqList.delete(dbName); + if (hasLocalStorage() && (dbName in localStorage)) { + delete localStorage[dbName]; + } + callback(null, { 'ok': true }); + }; - metaStore.get(META_STORE).onsuccess = function (e) { - docCount = e.target.result.docCount; + req.onerror = idbError(callback); }; - /* istanbul ignore if */ - if (opts.update_seq) { - getMaxUpdateSeq(seqStore, function (e) { - if (e.target.result && e.target.result.length > 0) { - updateSeq = e.target.result[0]; - } + var cached = cachedDBs.get(dbName); + + if (cached) { + idb = cached.idb; + api._meta = cached.global; + return nextTick(function () { + callback(null, api); }); } - function getMaxUpdateSeq(objectStore, onSuccess) { - function onCursor(e) { - var cursor = e.target.result; - var maxKey = undefined; - if (cursor && cursor.key) { - maxKey = cursor.key; - } - return onSuccess({ - target: { - result: [maxKey] - } - }); - } - objectStore.openCursor(null, 'prev').onsuccess = onCursor; + var req; + if (opts.storage) { + req = tryStorageOption(dbName, opts.storage); + } else { + req = indexedDB.open(dbName, ADAPTER_VERSION); } - // if the user specifies include_docs=true, then we don't - // want to block the main cursor while we're fetching the doc - function fetchDocAsynchronously(metadata, row, winningRev$$1) { - var key = metadata.id + "::" + winningRev$$1; - docIdRevIndex.get(key).onsuccess = function onGetDoc(e) { - row.doc = decodeDoc(e.target.result); - if (opts.conflicts) { - var conflicts = collectConflicts(metadata); - if (conflicts.length) { - row.doc._conflicts = conflicts; - } - } - fetchAttachmentsIfNecessary(row.doc, opts, txn); - }; - } + openReqList.set(dbName, req); - function allDocsInner(winningRev$$1, metadata) { - var row = { - id: metadata.id, - key: metadata.id, - value: { - rev: winningRev$$1 - } - }; - var deleted = metadata.deleted; - if (deleted) { - if (keys) { - results.push(row); - // deleted docs are okay with "keys" requests - row.value.deleted = true; - row.doc = null; - } - } else if (skip-- <= 0) { - results.push(row); - if (opts.include_docs) { - fetchDocAsynchronously(metadata, row, winningRev$$1); - } + req.onupgradeneeded = function (e) { + var db = e.target.result; + if (e.oldVersion < 1) { + return createSchema(db); // new db, initial schema } - } + // do migrations - function processBatch(batchValues) { - for (var i = 0, len = batchValues.length; i < len; i++) { - if (results.length === limit) { - break; - } - var batchValue = batchValues[i]; - if (batchValue.error && keys) { - // key was not found with "keys" requests - results.push(batchValue); - continue; - } - var metadata = decodeMetadata(batchValue); - var winningRev$$1 = metadata.winningRev; - allDocsInner(winningRev$$1, metadata); - } - } + var txn = e.currentTarget.transaction; + // these migrations have to be done in this function, before + // control is returned to the event loop, because IndexedDB - function onBatch(batchKeys, batchValues, cursor) { - if (!cursor) { - return; + if (e.oldVersion < 3) { + createLocalStoreSchema(db); // v2 -> v3 } - processBatch(batchValues); - if (results.length < limit) { - cursor.continue(); + if (e.oldVersion < 4) { + addAttachAndSeqStore(db); // v3 -> v4 } - } - function onGetAll(e) { - var values = e.target.result; - if (opts.descending) { - values = values.reverse(); - } - processBatch(values); - } + var migrations = [ + addDeletedOrLocalIndex, // v1 -> v2 + migrateLocalStore, // v2 -> v3 + migrateAttsAndSeqs, // v3 -> v4 + migrateMetadata // v4 -> v5 + ]; - function onResultsReady() { - var returnVal = { - total_rows: docCount, - offset: opts.skip, - rows: results - }; - - /* istanbul ignore if */ - if (opts.update_seq && updateSeq !== undefined) { - returnVal.update_seq = updateSeq; - } - callback(null, returnVal); - } + var i = e.oldVersion; - function onTxnComplete() { - if (opts.attachments) { - postProcessAttachments(results, opts.binary).then(onResultsReady); - } else { - onResultsReady(); + function next() { + var migration = migrations[i - 1]; + i++; + if (migration) { + migration(txn, next); + } } - } - // don't bother doing any requests if start > end or limit === 0 - if (keyRangeError || limit === 0) { - return; - } - if (keys) { - return allDocsKeys(opts.keys, docStore, onBatch); - } - if (limit === -1) { // just fetch everything - return getAll(docStore, keyRange, onGetAll); - } - // else do a cursor - // choose a batch size based on the skip, since we'll need to skip that many - runBatchedCursor(docStore, keyRange, opts.descending, limit + skip, onBatch); - } + next(); + }; - // - // Blobs are not supported in all versions of IndexedDB, notably - // Chrome <37 and Android <5. In those versions, storing a blob will throw. - // - // Various other blob bugs exist in Chrome v37-42 (inclusive). - // Detecting them is expensive and confusing to users, and Chrome 37-42 - // is at very low usage worldwide, so we do a hacky userAgent check instead. - // - // content-type bug: https://code.google.com/p/chromium/issues/detail?id=408120 - // 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) { - return new PouchPromise(function (resolve) { - var blob$$1 = createBlob(['']); - var req = txn.objectStore(DETECT_BLOB_SUPPORT_STORE).put(blob$$1, 'key'); + req.onsuccess = function (e) { - req.onsuccess = function () { - var matchedChrome = navigator.userAgent.match(/Chrome\/(\d+)/); - var matchedEdge = navigator.userAgent.match(/Edge\//); - // MS Edge pretends to be Chrome 42: - // https://msdn.microsoft.com/en-us/library/hh869301%28v=vs.85%29.aspx - resolve(matchedEdge || !matchedChrome || - parseInt(matchedChrome[1], 10) >= 43); - }; + idb = e.target.result; - txn.onabort = function (e) { - // If the transaction aborts now its due to not being able to - // write to the database, likely due to the disk being full - e.preventDefault(); - e.stopPropagation(); - resolve(false); + idb.onversionchange = function () { + idb.close(); + cachedDBs.delete(dbName); }; - }).catch(function () { - return false; // error, so assume unsupported - }); - } - function countDocs(txn, cb) { - var index = txn.objectStore(DOC_STORE).index('deletedOrLocal'); - index.count(IDBKeyRange.only('0')).onsuccess = function (e) { - cb(e.target.result); - }; - } + idb.onabort = function (e) { + guardedConsole('error', 'Database has a global failure', e.target.error); + idb.close(); + cachedDBs.delete(dbName); + }; - // This task queue ensures that IDB open calls are done in their own tick - // and sequentially - i.e. we wait for the async IDB open to *fully* complete - // before calling the next one. This works around IE/Edge race conditions in IDB. + // Do a few setup operations (in parallel as much as possible): + // 1. Fetch meta doc + // 2. Check blob support + // 3. Calculate docCount + // 4. Generate an instanceId if necessary + // 5. Store docCount and instanceId on meta doc - var running = false; - var queue = []; + var txn = idb.transaction([ + META_STORE, + DETECT_BLOB_SUPPORT_STORE, + DOC_STORE + ], 'readwrite'); - function tryCode(fun, err, res, PouchDB) { - try { - fun(err, res); - } catch (err) { - // Shouldn't happen, but in some odd cases - // IndexedDB implementations might throw a sync - // error, in which case this will at least log it. - PouchDB.emit('error', err); - } - } + var storedMetaDoc = false; + var metaDoc; + var docCount; + var blobSupport; + var instanceId; - function applyNext() { - if (running || !queue.length) { - return; - } - running = true; - queue.shift()(); - } + function completeSetup() { + if (typeof blobSupport === 'undefined' || !storedMetaDoc) { + return; + } + api._meta = { + name: dbName, + instanceId: instanceId, + blobSupport: blobSupport + }; - function enqueueTask(action, callback, PouchDB) { - queue.push(function runAction() { - action(function runCallback(err, res) { - tryCode(callback, err, res, PouchDB); - running = false; - nextTick(function runNext() { - applyNext(PouchDB); + cachedDBs.set(dbName, { + idb: idb, + global: api._meta }); - }); - }); - applyNext(); - } - - function changes(opts, api, dbName, idb) { - opts = clone(opts); + callback(null, api); + } - if (opts.continuous) { - var id = dbName + ':' + uuid(); - changesHandler$$1.addListener(dbName, id, api, opts); - changesHandler$$1.notify(dbName); - return { - cancel: function () { - changesHandler$$1.removeListener(dbName, id); + function storeMetaDocIfReady() { + if (typeof docCount === 'undefined' || typeof metaDoc === 'undefined') { + return; + } + var instanceKey = dbName + '_id'; + if (instanceKey in metaDoc) { + instanceId = metaDoc[instanceKey]; + } else { + metaDoc[instanceKey] = instanceId = uuid(); } + metaDoc.docCount = docCount; + txn.objectStore(META_STORE).put(metaDoc); + } + + // + // fetch or generate the instanceId + // + txn.objectStore(META_STORE).get(META_STORE).onsuccess = function (e) { + metaDoc = e.target.result || { id: META_STORE }; + storeMetaDocIfReady(); }; - } - var docIds = opts.doc_ids && new ExportedSet(opts.doc_ids); + // + // countDocs + // + countDocs(txn, function (count) { + docCount = count; + storeMetaDocIfReady(); + }); - opts.since = opts.since || 0; - var lastSeq = opts.since; + // + // check blob support + // + if (!blobSupportPromise) { + // make sure blob support is only checked once + blobSupportPromise = checkBlobSupport(txn); + } - var limit = 'limit' in opts ? opts.limit : -1; - if (limit === 0) { - limit = 1; // per CouchDB _changes spec - } - var returnDocs; - if ('return_docs' in opts) { - returnDocs = opts.return_docs; - } else if ('returnDocs' in opts) { - // TODO: Remove 'returnDocs' in favor of 'return_docs' in a future release - returnDocs = opts.returnDocs; - } else { - returnDocs = true; - } - - var results = []; - var numResults = 0; - var filter = filterChange(opts); - var docIdsToMetadata = new ExportedMap(); - - var txn; - var bySeqStore; - var docStore; - var docIdRevIndex; - - function onBatch(batchKeys, batchValues, cursor) { - if (!cursor || !batchKeys.length) { // done - return; - } - - var winningDocs = new Array(batchKeys.length); - var metadatas = new Array(batchKeys.length); - - function processMetadataAndWinningDoc(metadata, winningDoc) { - var change = opts.processChange(winningDoc, metadata, opts); - lastSeq = change.seq = metadata.seq; + blobSupportPromise.then(function (val) { + blobSupport = val; + completeSetup(); + }); - var filtered = filter(change); - if (typeof filtered === 'object') { // anything but true/false indicates error - return opts.complete(filtered); - } + // only when the metadata put transaction has completed, + // consider the setup done + txn.oncomplete = function () { + storedMetaDoc = true; + completeSetup(); + }; + }; - if (filtered) { - 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], opts.binary).then(function () { - opts.onChange(change); - }); - }); - } else { - opts.onChange(change); - } - } - } + req.onerror = function () { + var msg = 'Failed to open indexedDB, are you in private browsing mode?'; + guardedConsole('error', msg); + callback(createError(IDB_ERROR, msg)); + }; + } - function onBatchDone() { - for (var i = 0, len = winningDocs.length; i < len; i++) { - if (numResults === limit) { - break; - } - var winningDoc = winningDocs[i]; - if (!winningDoc) { - continue; - } - var metadata = metadatas[i]; - processMetadataAndWinningDoc(metadata, winningDoc); - } + 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) && + !/BlackBerry/.test(navigator.platform); - if (numResults !== limit) { - cursor.continue(); - } - } + // 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'; + }; - // Fetch all metadatas/winningdocs from this batch in parallel, then process - // them all only once all data has been collected. This is done in parallel - // because it's faster than doing it one-at-a-time. - var numDone = 0; - batchValues.forEach(function (value, i) { - var doc = decodeDoc(value); - var seq = batchKeys[i]; - fetchWinningDocAndMetadata(doc, seq, function (metadata, winningDoc) { - metadatas[i] = metadata; - winningDocs[i] = winningDoc; - if (++numDone === batchKeys.length) { - onBatchDone(); - } - }); + function tryStorageOption(dbName, storage) { + try { // option only available in Firefox 26+ + return indexedDB.open(dbName, { + version: ADAPTER_VERSION, + storage: storage }); + } catch(err) { + return indexedDB.open(dbName, ADAPTER_VERSION); } + } - function onGetMetadata(doc, seq, metadata, cb) { - if (metadata.seq !== seq) { - // some other seq is later - return cb(); - } + var IDBPouch = function (PouchDB) { + PouchDB.adapter('idb', IdbPouch, true); + }; - if (metadata.winningRev === doc._rev) { - // this is the winning doc - return cb(metadata, doc); - } + // + // 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 + // - // fetch winning doc in separate request - var docIdRev = doc._id + '::' + metadata.winningRev; - var req = docIdRevIndex.get(docIdRev); - req.onsuccess = function (e) { - cb(metadata, decodeDoc(e.target.result)); - }; - } + function decodeUtf8(str) { + return decodeURIComponent(escape(str)); + } - function fetchWinningDocAndMetadata(doc, seq, cb) { - if (docIds && !docIds.has(doc._id)) { - return cb(); - } + 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); + } - var metadata = docIdsToMetadata.get(doc._id); - if (metadata) { // cached - return onGetMetadata(doc, seq, metadata, cb); - } - // metadata not cached, have to go fetch it - docStore.get(doc._id).onsuccess = function (e) { - metadata = decodeMetadata(e.target.result); - docIdsToMetadata.set(doc._id, metadata); - onGetMetadata(doc, seq, metadata, cb); - }; - } - function finish() { - opts.complete(null, { - results: results, - last_seq: lastSeq - }); + // 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; + } - function onTxnComplete() { - if (!opts.continuous && opts.attachments) { - // cannot guarantee that postProcessing was already done, - // so do it again - postProcessAttachments(results).then(finish); - } else { - finish(); - } + // 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; + } - 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); + function parseHexString(str, encoding) { + if (encoding === 'UTF-8') { + return decodeUtf8(parseHexUtf8(str, 0, str.length)); + } else { + return parseHexUtf16(str, 0, str.length); } - txn = txnResult.txn; - txn.onabort = idbError(opts.complete); - txn.oncomplete = onTxnComplete; - - bySeqStore = txn.objectStore(BY_SEQ_STORE); - docStore = txn.objectStore(DOC_STORE); - docIdRevIndex = bySeqStore.index('_doc_id_rev'); - - var keyRange = (opts.since && !opts.descending) ? - IDBKeyRange.lowerBound(opts.since, true) : null; - - runBatchedCursor(bySeqStore, keyRange, opts.descending, limit, onBatch); } - var cachedDBs = new ExportedMap(); - var blobSupportPromise; - var openReqList = new ExportedMap(); - - function IdbPouch(opts, callback) { - var api = this; - - enqueueTask(function (thisCallback) { - init(api, opts, thisCallback); - }, callback, api.constructor); + function quote(str) { + return "'" + str + "'"; } - function init(api, opts, callback) { - - var dbName = opts.name; - - var idb = null; - api._meta = null; + var ADAPTER_VERSION$1 = 7; // used to manage migrations - // 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); + // The object stores created for each database + // DOC_STORE stores the document meta data, its revision history and state + var DOC_STORE$1 = quote('document-store'); + // BY_SEQ_STORE stores a particular version of a document, keyed by its + // sequence id + var BY_SEQ_STORE$1 = quote('by-sequence'); + // Where we store attachments + var ATTACH_STORE$1 = quote('attach-store'); + var LOCAL_STORE$1 = quote('local-store'); + var META_STORE$1 = quote('metadata-store'); + // where we store many-to-many relations between attachment + // digests and seqs + var ATTACH_AND_SEQ_STORE$1 = quote('attach-seq-store'); - // added in v2 - docStore.createIndex('deletedOrLocal', 'deletedOrLocal', {unique : false}); + // 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'); + } - // added in v3 - db.createObjectStore(LOCAL_STORE, {keyPath: '_id'}); + function unescapeBlob(str) { + return str + .replace(/\u0001\u0001/g, '\u0000') + .replace(/\u0001\u0002/g, '\u0001') + .replace(/\u0002\u0002/g, '\u0002'); + } - // added in v4 - var attAndSeqStore = db.createObjectStore(ATTACH_AND_SEQ_STORE, - {autoIncrement: true}); - attAndSeqStore.createIndex('seq', 'seq'); - attAndSeqStore.createIndex('digestSeq', 'digestSeq', {unique: true}); - } + 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); + } - // 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}); + function unstringifyDoc(doc, id, rev) { + doc = JSON.parse(doc); + doc._id = id; + doc._rev = rev; + return doc; + } - docStore.openCursor().onsuccess = function (event) { - var cursor = event.target.result; - if (cursor) { - var metadata = cursor.value; - var deleted = isDeleted(metadata); - metadata.deletedOrLocal = deleted ? "1" : "0"; - docStore.put(metadata); - cursor.continue(); - } else { - callback(); - } - }; + // question mark groups IN queries, e.g. 3 -> '(?,?,?)' + function qMarks(num) { + var s = '('; + while (num--) { + s += '?'; + if (num) { + s += ','; + } } + return s + ')'; + } - // migration to version 3 (part 1) - function createLocalStoreSchema(db) { - db.createObjectStore(LOCAL_STORE, {keyPath: '_id'}) - .createIndex('_doc_id_rev', '_doc_id_rev', {unique: true}); - } + 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) : ''); + } - // 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); + function compactRevs$1(revs, docId, tx) { - 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 = isLocalId(docId); - var rev$$1 = winningRev(metadata); - if (local) { - var docIdRev = docId + "::" + rev$$1; - // 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(); - } - }; + if (!revs.length) { + return; } - // 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}); + var numDone = 0; + var seqs = []; + + function checkDone() { + if (++numDone === revs.length) { // done + deleteOrphans(); + } } - // 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); + function deleteOrphans() { + // find orphaned attachment digests - // 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 + if (!seqs.length) { + return; + } + + var sql = 'SELECT DISTINCT digest AS digest FROM ' + + ATTACH_AND_SEQ_STORE$1 + ' 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; } - 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 + var sql = 'DELETE FROM ' + ATTACH_AND_SEQ_STORE$1 + + ' WHERE seq IN (' + + seqs.map(function () { return '?'; }).join(',') + + ')'; + tx.executeSql(sql, seqs, function (tx) { + + var sql = 'SELECT digest FROM ' + ATTACH_AND_SEQ_STORE$1 + + ' WHERE digest IN (' + + digestsToCheck.map(function () { return '?'; }).join(',') + + ')'; + tx.executeSql(sql, digestsToCheck, function (tx, res) { + var nonOrphanedDigests = new ExportedSet(); + 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$1 + ' WHERE digest=?', + [digest]); + tx.executeSql( + 'DELETE FROM ' + ATTACH_STORE$1 + ' WHERE digest=?', [digest]); }); - } - 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) { + // update by-seq and attach stores in parallel + revs.forEach(function (rev) { + var sql = 'SELECT seq FROM ' + BY_SEQ_STORE$1 + + ' WHERE doc_id=? AND rev=?'; - function decodeMetadataCompat(storedObject) { - if (!storedObject.data) { - // old format, when we didn't store it stringified - storedObject.deleted = storedObject.deletedOrLocal === '1'; - return storedObject; + tx.executeSql(sql, [docId, rev], function (tx, res) { + if (!res.rows.length) { // already deleted + return checkDone(); } - return decodeMetadata(storedObject); - } + var seq = res.rows.item(0).seq; + seqs.push(seq); - // 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 || - 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(); - }; - } + tx.executeSql( + 'DELETE FROM ' + BY_SEQ_STORE$1 + ' WHERE seq=?', [seq], checkDone); + }); + }); + } - function onGetMetadataSeq() { - var metadataToStore = encodeMetadata(metadata, - metadata.winningRev, metadata.deleted); + function websqlError(callback) { + return function (event) { + guardedConsole('error', 'WebSQL threw an error', 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(createError(WSQ_ERROR, errorReason, errorName)); + }; + } - var req = docStore.put(metadataToStore); - req.onsuccess = function () { - cursor.continue(); - }; - } + 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 = typeof navigator !== 'undefined' && + /Android/.test(navigator.userAgent); + return isAndroid ? 5000000 : 1; // in PhantomJS, if you use 0 it will crash + } - if (metadata.seq) { - return onGetMetadataSeq(); - } + function websqlBulkDocs(dbOpts, req, opts, api, db, websqlChanges, callback) { + var newEdits = opts.new_edits; + var userDocs = req.docs; - fetchMetadataSeq(); - }; + // Parse the docs, give them a sequence number for the result + var docInfos = userDocs.map(function (doc) { + if (doc._id && isLocalId(doc._id)) { + return doc; + } + var newDoc = parseDoc(doc, newEdits); + return newDoc; + }); + var docInfoErrors = docInfos.filter(function (docInfo) { + return docInfo.error; + }); + if (docInfoErrors.length) { + return callback(docInfoErrors[0]); } - api._remote = false; - api.type = function () { - return 'idb'; - }; + var tx; + var results = new Array(docInfos.length); + var fetchedDocs = new ExportedMap(); - api._id = toPromise(function (callback) { - callback(null, api._meta.instanceId); - }); + var preconditionErrored; + function complete() { + if (preconditionErrored) { + return callback(preconditionErrored); + } + websqlChanges.notify(api._name); + callback(null, results); + } - api._bulkDocs = function idb_bulkDocs(req, reqOpts, callback) { - idbBulkDocs(opts, req, reqOpts, api, idb, callback); - }; + function verifyAttachment(digest, callback) { + var sql = 'SELECT count(*) as cnt FROM ' + ATTACH_STORE$1 + + ' WHERE digest=?'; + tx.executeSql(sql, [digest], function (tx, result) { + if (result.rows.item(0).cnt === 0) { + var err = createError(MISSING_STUB, + 'unknown stub attachment with digest ' + + digest); + callback(err); + } else { + 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.ctx; - if (!txn) { - var txnResult = openTransactionSafely(idb, - [DOC_STORE, BY_SEQ_STORE, ATTACH_STORE], 'readonly'); - if (txnResult.error) { - return callback(txnResult.error); + 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); + } + }); } - txn = txnResult.txn; + }); + if (!digests.length) { + return finish(); } + var numDone = 0; + var err; - function finish() { - callback(err, {doc: doc, metadata: metadata, ctx: txn}); + function checkDone() { + if (++numDone === digests.length) { + finish(err); + } } + digests.forEach(function (digest) { + verifyAttachment(digest, function (attErr) { + if (attErr && !err) { + err = attErr; + } + checkDone(); + }); + }); + } - 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 = createError(MISSING_DOC, 'missing'); - return finish(); - } + function writeDoc(docInfo, winningRev$$1, winningRevIsDeleted, newRevIsDeleted, + isUpdate, delta, resultsIdx, callback) { - var rev$$1; - if (!opts.rev) { - rev$$1 = metadata.winningRev; - var deleted = isDeleted(metadata); - if (deleted) { - err = createError(MISSING_DOC, "deleted"); - return finish(); - } - } else { - rev$$1 = opts.latest ? latest(opts.rev, metadata) : opts.rev; - } + function finish() { + var data = docInfo.data; + var deletedInt = newRevIsDeleted ? 1 : 0; - var objectStore = txn.objectStore(BY_SEQ_STORE); - var key = metadata.id + '::' + rev$$1; + var id = data._id; + var rev = data._rev; + var json = stringifyDoc(data); + var sql = 'INSERT INTO ' + BY_SEQ_STORE$1 + + ' (doc_id, rev, json, deleted) VALUES (?, ?, ?, ?);'; + var sqlArgs = [id, rev, json, deletedInt]; - objectStore.index('_doc_id_rev').get(key).onsuccess = function (e) { - doc = e.target.result; - if (doc) { - doc = decodeDoc(doc); + // 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(); } - if (!doc) { - err = createError(MISSING_DOC, 'missing'); - return finish(); + 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$1 + + ' (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 } - finish(); - }; - }; - }; - - api._getAttachment = function (docId, attachId, attachment, opts, callback) { - var txn; - 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.binary, function (blobData) { - callback(null, blobData); - }); - }; - }; - - api._info = function idb_info(callback) { - var updateSeq; - var docCount; + 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$1, 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$1 + + ' 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 + }); + } - var txnResult = openTransactionSafely(idb, [META_STORE, BY_SEQ_STORE], 'readonly'); - if (txnResult.error) { - return callback(txnResult.error); + function collectResults(attachmentErr) { + if (!err) { + if (attachmentErr) { + err = attachmentErr; + callback(err); + } else if (recv === attachments.length) { + finish(); + } + } } - var txn = txnResult.txn; - txn.objectStore(META_STORE).get(META_STORE).onsuccess = function (e) { - docCount = e.target.result.docCount; - }; - txn.objectStore(BY_SEQ_STORE).openCursor(null, 'prev').onsuccess = function (e) { - var cursor = e.target.result; - updateSeq = cursor ? cursor.key : 0; - }; - txn.oncomplete = function () { - callback(null, { - doc_count: docCount, - update_seq: updateSeq, - // for debugging - idb_attachment_format: (api._meta.blobSupport ? 'binary' : 'base64') - }); - }; - }; + var err = null; + var recv = 0; - api._allDocs = function idb_allDocs(opts, callback) { - idbAllDocs(opts, idb, callback); - }; + docInfo.data._id = docInfo.metadata.id; + docInfo.data._rev = docInfo.metadata.rev; + var attachments = Object.keys(docInfo.data._attachments || {}); - api._changes = function idbChanges(opts) { - return changes(opts, api, dbName, idb); - }; - api._close = function (callback) { - // https://developer.mozilla.org/en-US/docs/IndexedDB/IDBDatabase#close - // "Returns immediately and closes the connection in a separate thread..." - idb.close(); - cachedDBs.delete(dbName); - callback(); - }; + if (newRevIsDeleted) { + docInfo.data._deleted = true; + } - api._getRevisionTree = function (docId, callback) { - var txnResult = openTransactionSafely(idb, [DOC_STORE], 'readonly'); - if (txnResult.error) { - return callback(txnResult.error); + function attachmentSaved(err) { + recv++; + collectResults(err); } - 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(createError(MISSING_DOC)); + + attachments.forEach(function (key) { + var att = docInfo.data._attachments[key]; + if (!att.stub) { + var data = att.data; + delete att.data; + att.revpos = parseInt(winningRev$$1, 10); + var digest = att.digest; + saveAttachment(digest, data, attachmentSaved); } else { - callback(null, doc.rev_tree); + recv++; + collectResults(); } - }; - }; + }); - // 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); + if (!attachments.length) { + finish(); } - var txn = txnResult.txn; - var docStore = txn.objectStore(DOC_STORE); + function dataWritten(tx, seq) { + var id = docInfo.metadata.id; - docStore.get(docId).onsuccess = function (event) { - var metadata = decodeMetadata(event.target.result); - traverseRevTree(metadata.rev_tree, function (isLeaf, pos, - revHash, ctx, opts) { - var rev$$1 = pos + '-' + revHash; - if (revs.indexOf(rev$$1) !== -1) { - opts.status = 'missing'; - } - }); - compactRevs(revs, docId, txn); - var winningRev$$1 = metadata.winningRev; - var deleted = metadata.deleted; - txn.objectStore(DOC_STORE).put( - encodeMetadata(metadata, winningRev$$1, deleted)); - }; - txn.onabort = idbError(callback); - txn.oncomplete = function () { - callback(); - }; - }; + var revsToCompact = docInfo.stemmedRevs || []; + if (isUpdate && api.auto_compaction) { + revsToCompact = compactTree(docInfo.metadata).concat(revsToCompact); + } + if (revsToCompact.length) { + compactRevs$1(revsToCompact, id, tx); + } + docInfo.metadata.seq = seq; + var rev = docInfo.metadata.rev; + delete docInfo.metadata.rev; - api._getLocal = function (id, callback) { - var txnResult = openTransactionSafely(idb, [LOCAL_STORE], 'readonly'); - if (txnResult.error) { - return callback(txnResult.error); + var sql = isUpdate ? + 'UPDATE ' + DOC_STORE$1 + + ' SET json=?, max_seq=?, winningseq=' + + '(SELECT seq FROM ' + BY_SEQ_STORE$1 + + ' WHERE doc_id=' + DOC_STORE$1 + '.id AND rev=?) WHERE id=?' + : 'INSERT INTO ' + DOC_STORE$1 + + ' (id, winningseq, max_seq, json) VALUES (?,?,?,?);'; + var metadataStr = safeJsonStringify(docInfo.metadata); + var params = isUpdate ? + [metadataStr, seq, winningRev$$1, id] : + [id, seq, seq, metadataStr]; + tx.executeSql(sql, params, function () { + results[resultsIdx] = { + ok: true, + id: docInfo.metadata.id, + rev: rev + }; + fetchedDocs.set(id, docInfo.metadata); + callback(); + }); } - 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(createError(MISSING_DOC)); - } else { - delete doc['_doc_id_rev']; // for backwards compat - callback(null, doc); - } - }; - }; + function websqlProcessDocs() { + processDocs(dbOpts.revs_limit, docInfos, api, fetchedDocs, tx, + results, writeDoc, opts); + } - 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); + function fetchExistingDocs(callback) { + if (!docInfos.length) { + return callback(); } - var tx = opts.ctx; - var ret; - if (!tx) { - var txnResult = openTransactionSafely(idb, [LOCAL_STORE], 'readwrite'); - if (txnResult.error) { - return callback(txnResult.error); + var numFetched = 0; + + function checkDone() { + if (++numFetched === docInfos.length) { + callback(); } - 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(createError(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); - } - }; + docInfos.forEach(function (docInfo) { + if (docInfo._id && isLocalId(docInfo._id)) { + return checkDone(); // skip local docs + } + var id = docInfo.metadata.id; + tx.executeSql('SELECT json FROM ' + DOC_STORE$1 + + ' WHERE id = ?', [id], function (tx, result) { + if (result.rows.length) { + var metadata = safeJsonParse(result.rows.item(0).json); + fetchedDocs.set(id, metadata); } - }; - } else { // new doc - req = oStore.add(doc); - req.onerror = function (e) { - // constraint error, already exists - callback(createError(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); - } - }; - } - }; + checkDone(); + }); + }); + } - api._removeLocal = function (doc, opts, callback) { - if (typeof opts === 'function') { - callback = opts; - opts = {}; - } - var tx = opts.ctx; - if (!tx) { - var txnResult = openTransactionSafely(idb, [LOCAL_STORE], 'readwrite'); - if (txnResult.error) { - return callback(txnResult.error); + function saveAttachment(digest, data, callback) { + var sql = 'SELECT digest FROM ' + ATTACH_STORE$1 + ' WHERE digest=?'; + tx.executeSql(sql, [digest], function (tx, result) { + if (result.rows.length) { // attachment already exists + return callback(); } - tx = txnResult.txn; - tx.oncomplete = function () { - if (ret) { - callback(null, ret); - } - }; - } - var ret; - var id = doc._id; - var oStore = tx.objectStore(LOCAL_STORE); - var req = oStore.get(id); + // 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$1 + + ' (digest, body, escaped) VALUES (?,?,1)'; + tx.executeSql(sql, [digest, escapeBlob(data)], function () { + callback(); + }, function () { + // ignore constaint errors, means it already exists + callback(); + return false; // ack we handled the error + }); + }); + } - req.onerror = idbError(callback); - req.onsuccess = function (e) { - var oldDoc = e.target.result; - if (!oldDoc || oldDoc._rev !== doc._rev) { - callback(createError(MISSING_DOC)); - } else { - oStore.delete(id); - ret = {ok: true, id: id, rev: '0-0'}; - if (opts.ctx) { // return immediately - callback(null, ret); + 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(websqlProcessDocs); } - } - }; - }; + }); + }, websqlError(callback), complete); + }); + } - api._destroy = function (opts, callback) { - changesHandler$$1.removeAllListeners(dbName); + var cachedDatabases = new ExportedMap(); - //Close open request for "dbName" database to fix ie delay. - var openReq = openReqList.get(dbName); - if (openReq && openReq.result) { - openReq.result.close(); - cachedDBs.delete(dbName); - } - var req = indexedDB.deleteDatabase(dbName); + // openDatabase passed in through opts (e.g. for node-websql) + function openDatabaseWithOpts(opts) { + return opts.websql(opts.name, opts.version, opts.description, opts.size); + } - req.onsuccess = function () { - //Remove open request from the list. - openReqList.delete(dbName); - if (hasLocalStorage() && (dbName in localStorage)) { - delete localStorage[dbName]; - } - callback(null, { 'ok': true }); + function openDBSafely(opts) { + try { + return { + db: openDatabaseWithOpts(opts) }; + } catch (err) { + return { + error: err + }; + } + } - req.onerror = idbError(callback); - }; + function openDB$1(opts) { + var cachedResult = cachedDatabases.get(opts.name); + if (!cachedResult) { + cachedResult = openDBSafely(opts); + cachedDatabases.set(opts.name, cachedResult); + } + return cachedResult; + } - var cached = cachedDBs.get(dbName); + var websqlChanges = new Changes(); - if (cached) { - idb = cached.idb; - api._meta = cached.global; - return nextTick(function () { - callback(null, api); - }); + function fetchAttachmentsIfNecessary$1(doc, opts, api, txn, cb) { + var attachments = Object.keys(doc._attachments || {}); + if (!attachments.length) { + return cb && cb(); } + var numDone = 0; - var req; - if (opts.storage) { - req = tryStorageOption(dbName, opts.storage); - } else { - req = indexedDB.open(dbName, ADAPTER_VERSION); + function checkDone() { + if (++numDone === attachments.length && cb) { + cb(); + } } - openReqList.set(dbName, req); + function fetchAttachment(doc, att) { + var attObj = doc._attachments[att]; + var attOpts = {binary: opts.binary, ctx: txn}; + api._getAttachment(doc._id, att, attObj, attOpts, function (_, data) { + doc._attachments[att] = assign$1( + pick(attObj, ['digest', 'content_type']), + { data: data } + ); + checkDone(); + }); + } - req.onupgradeneeded = function (e) { - var db = e.target.result; - if (e.oldVersion < 1) { - return createSchema(db); // new db, initial schema + attachments.forEach(function (att) { + if (opts.attachments && opts.include_docs) { + fetchAttachment(doc, att); + } else { + doc._attachments[att].stub = true; + checkDone(); } - // 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 POUCH_VERSION = 1; - var migrations = [ - addDeletedOrLocalIndex, // v1 -> v2 - migrateLocalStore, // v2 -> v3 - migrateAttsAndSeqs, // v3 -> v4 - migrateMetadata // v4 -> v5 - ]; + // 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$1 + ' (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$1 + ' (doc_id, rev)'; + var DOC_STORE_WINNINGSEQ_INDEX_SQL = + 'CREATE INDEX IF NOT EXISTS \'doc-winningseq-idx\' ON ' + + DOC_STORE$1 + ' (winningseq)'; + var ATTACH_AND_SEQ_STORE_SEQ_INDEX_SQL = + 'CREATE INDEX IF NOT EXISTS \'attach-seq-seq-idx\' ON ' + + ATTACH_AND_SEQ_STORE$1 + ' (seq)'; + var ATTACH_AND_SEQ_STORE_ATTACH_INDEX_SQL = + 'CREATE UNIQUE INDEX IF NOT EXISTS \'attach-seq-digest-idx\' ON ' + + ATTACH_AND_SEQ_STORE$1 + ' (digest, seq)'; - var i = e.oldVersion; + var DOC_STORE_AND_BY_SEQ_JOINER = BY_SEQ_STORE$1 + + '.seq = ' + DOC_STORE$1 + '.winningseq'; - function next() { - var migration = migrations[i - 1]; - i++; - if (migration) { - migration(txn, next); - } - } + var SELECT_DOCS = BY_SEQ_STORE$1 + '.seq AS seq, ' + + BY_SEQ_STORE$1 + '.deleted AS deleted, ' + + BY_SEQ_STORE$1 + '.json AS data, ' + + BY_SEQ_STORE$1 + '.rev AS rev, ' + + DOC_STORE$1 + '.json AS metadata'; - next(); - }; + function WebSqlPouch$1(opts, callback) { + var api = this; + var instanceId = null; + var size = getSize(opts); + var idRequests = []; + var encoding; - req.onsuccess = function (e) { + api._name = opts.name; - idb = e.target.result; + // extend the options here, because sqlite plugin has a ton of options + // and they are constantly changing, so it's more prudent to allow anything + var websqlOpts = assign$1({}, opts, { + version: POUCH_VERSION, + description: opts.name, + size: size + }); + var openDBResult = openDB$1(websqlOpts); + if (openDBResult.error) { + return websqlError(callback)(openDBResult.error); + } + var db = openDBResult.db; + if (typeof db.readTransaction !== 'function') { + // doesn't exist in sqlite plugin + db.readTransaction = db.transaction; + } - idb.onversionchange = function () { - idb.close(); - cachedDBs.delete(dbName); - }; + function dbCreated() { + // note the db name in case the browser upgrades to idb + if (hasLocalStorage()) { + window.localStorage['_pouch__websqldb_' + api._name] = true; + } + callback(null, api); + } - idb.onabort = function (e) { - guardedConsole('error', 'Database has a global failure', e.target.error); - idb.close(); - cachedDBs.delete(dbName); - }; + // 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); - // Do a few setup operations (in parallel as much as possible): - // 1. Fetch meta doc - // 2. Check blob support - // 3. Calculate docCount - // 4. Generate an instanceId if necessary - // 5. Store docCount and instanceId on meta doc + tx.executeSql('ALTER TABLE ' + BY_SEQ_STORE$1 + + ' ADD COLUMN deleted TINYINT(1) DEFAULT 0', [], function () { + tx.executeSql(BY_SEQ_STORE_DELETED_INDEX_SQL); + tx.executeSql('ALTER TABLE ' + DOC_STORE$1 + + ' ADD COLUMN local TINYINT(1) DEFAULT 0', [], function () { + tx.executeSql('CREATE INDEX IF NOT EXISTS \'doc-store-local-idx\' ON ' + + DOC_STORE$1 + ' (local, id)'); - var txn = idb.transaction([ - META_STORE, - DETECT_BLOB_SUPPORT_STORE, - DOC_STORE - ], 'readwrite'); + var sql = 'SELECT ' + DOC_STORE$1 + '.winningseq AS seq, ' + DOC_STORE$1 + + '.json AS metadata FROM ' + BY_SEQ_STORE$1 + ' JOIN ' + DOC_STORE$1 + + ' ON ' + BY_SEQ_STORE$1 + '.seq = ' + DOC_STORE$1 + '.winningseq'; - var storedMetaDoc = false; - var metaDoc; - var docCount; - var blobSupport; - var instanceId; + tx.executeSql(sql, [], function (tx, result) { - function completeSetup() { - if (typeof blobSupport === 'undefined' || !storedMetaDoc) { - return; - } - api._meta = { - name: dbName, - instanceId: instanceId, - blobSupport: blobSupport - }; + var deleted = []; + var local = []; - cachedDBs.set(dbName, { - idb: idb, - global: api._meta + 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 (isDeleted(metadata)) { + deleted.push(seq); + } + if (isLocalId(metadata.id)) { + local.push(metadata.id); + } + } + tx.executeSql('UPDATE ' + DOC_STORE$1 + 'SET local = 1 WHERE id IN ' + + qMarks(local.length), local, function () { + tx.executeSql('UPDATE ' + BY_SEQ_STORE$1 + + ' SET deleted = 1 WHERE seq IN ' + + qMarks(deleted.length), deleted, callback); + }); + }); }); - callback(null, api); - } - - function storeMetaDocIfReady() { - if (typeof docCount === 'undefined' || typeof metaDoc === 'undefined') { - return; - } - var instanceKey = dbName + '_id'; - if (instanceKey in metaDoc) { - instanceId = metaDoc[instanceKey]; - } else { - metaDoc[instanceKey] = instanceId = uuid(); - } - metaDoc.docCount = docCount; - txn.objectStore(META_STORE).put(metaDoc); - } - - // - // fetch or generate the instanceId - // - txn.objectStore(META_STORE).get(META_STORE).onsuccess = function (e) { - metaDoc = e.target.result || { id: META_STORE }; - storeMetaDocIfReady(); - }; - - // - // countDocs - // - countDocs(txn, function (count) { - docCount = count; - storeMetaDocIfReady(); }); + } - // - // check blob support - // - if (!blobSupportPromise) { - // make sure blob support is only checked once - blobSupportPromise = checkBlobSupport(txn); - } - - blobSupportPromise.then(function (val) { - blobSupport = val; - completeSetup(); + // in this migration, we make all the local docs unversioned + function runMigration3(tx, callback) { + var local = 'CREATE TABLE IF NOT EXISTS ' + LOCAL_STORE$1 + + ' (id UNIQUE, rev, json)'; + tx.executeSql(local, [], function () { + var sql = 'SELECT ' + DOC_STORE$1 + '.id AS id, ' + + BY_SEQ_STORE$1 + '.json AS data ' + + 'FROM ' + BY_SEQ_STORE$1 + ' JOIN ' + + DOC_STORE$1 + ' ON ' + BY_SEQ_STORE$1 + '.seq = ' + + DOC_STORE$1 + '.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$1 + + ' (id, rev, json) VALUES (?,?,?)', + [row.id, rev, row.data], function (tx) { + tx.executeSql('DELETE FROM ' + DOC_STORE$1 + ' WHERE id=?', + [row.id], function (tx) { + tx.executeSql('DELETE FROM ' + BY_SEQ_STORE$1 + ' WHERE seq=?', + [row.seq], function () { + doNext(); + }); + }); + }); + } + doNext(); + }); }); + } - // only when the metadata put transaction has completed, - // consider the setup done - txn.oncomplete = function () { - storedMetaDoc = true; - completeSetup(); - }; - txn.onabort = idbError(callback); - }; - - req.onerror = function () { - var msg = 'Failed to open indexedDB, are you in private browsing mode?'; - guardedConsole('error', msg); - callback(createError(IDB_ERROR, msg)); - }; - } - - 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) && - !/BlackBerry/.test(navigator.platform); - - // Safari <10.1 does not meet our requirements for IDB support (#5572) - // since Safari 10.1 shipped with fetch, we can use that to detect it - var hasFetch = typeof fetch === 'function' && - fetch.toString().indexOf('[native code') !== -1; + // in this migration, we remove doc_id_rev and just use rev + function runMigration4(tx, callback) { - // On Firefox SecurityError is thrown while referencing indexedDB if cookies - // are not allowed. `typeof indexedDB` also triggers the error. - try { - // some outdated implementations of IDB that appear on Samsung - // and HTC Android devices <4.4 are missing IDBKeyRange - return (!isSafari || hasFetch) && typeof indexedDB !== 'undefined' && - typeof IDBKeyRange !== 'undefined'; - } catch (e) { - return false; - } - }; + 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$1 + + ' SET doc_id=?, rev=? WHERE doc_id_rev=?'; + tx.executeSql(sql, [doc_id, rev, doc_id_rev], function () { + doNext(); + }); + } + doNext(); + } - function tryStorageOption(dbName, storage) { - try { // option only available in Firefox 26+ - return indexedDB.open(dbName, { - version: ADAPTER_VERSION, - storage: storage + var sql = 'ALTER TABLE ' + BY_SEQ_STORE$1 + ' ADD COLUMN doc_id'; + tx.executeSql(sql, [], function (tx) { + var sql = 'ALTER TABLE ' + BY_SEQ_STORE$1 + ' 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$1; + 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); + }); + }); + }); }); - } catch (err) { - return indexedDB.open(dbName, ADAPTER_VERSION); } - } - - function IDBPouch (PouchDB) { - PouchDB.adapter('idb', IdbPouch, true); - } - - // - // 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(escape(str)); - } + // in this migration, we add the attach_and_seq table + // for issue #2818 + function runMigration5(tx, callback) { - 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); - } + 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$1; + tx.executeSql(sql, [], function (tx, res) { + var count = res.rows.item(0).cnt; + if (!count) { + return callback(tx); + } - - // 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); - } - } - - function quote(str) { - return "'" + str + "'"; - } - - var ADAPTER_VERSION$1 = 7; // used to manage migrations - - // The object stores created for each database - // DOC_STORE stores the document meta data, its revision history and state - var DOC_STORE$1 = quote('document-store'); - // BY_SEQ_STORE stores a particular version of a document, keyed by its - // sequence id - var BY_SEQ_STORE$1 = quote('by-sequence'); - // Where we store attachments - var ATTACH_STORE$1 = quote('attach-store'); - var LOCAL_STORE$1 = quote('local-store'); - var META_STORE$1 = quote('metadata-store'); - // where we store many-to-many relations between attachment - // digests and seqs - var ATTACH_AND_SEQ_STORE$1 = quote('attach-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$$1) { - doc = JSON.parse(doc); - doc._id = id; - doc._rev = rev$$1; - 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$1(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$1 + ' 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$1 + - ' WHERE seq IN (' + - seqs.map(function () { return '?'; }).join(',') + - ')'; - tx.executeSql(sql, seqs, function (tx) { - - var sql = 'SELECT digest FROM ' + ATTACH_AND_SEQ_STORE$1 + - ' WHERE digest IN (' + - digestsToCheck.map(function () { return '?'; }).join(',') + - ')'; - tx.executeSql(sql, digestsToCheck, function (tx, res) { - var nonOrphanedDigests = new ExportedSet(); - 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$1 + ' WHERE digest=?', - [digest]); - tx.executeSql( - 'DELETE FROM ' + ATTACH_STORE$1 + ' WHERE digest=?', [digest]); - }); - }); - }); - }); - } - - // update by-seq and attach stores in parallel - revs.forEach(function (rev$$1) { - var sql = 'SELECT seq FROM ' + BY_SEQ_STORE$1 + - ' WHERE doc_id=? AND rev=?'; - - tx.executeSql(sql, [docId, rev$$1], 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$1 + ' WHERE seq=?', [seq], checkDone); - }); - }); - } - - function websqlError(callback) { - return function (event) { - guardedConsole('error', 'WebSQL threw an error', 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(createError(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 = typeof navigator !== 'undefined' && - /Android/.test(navigator.userAgent); - return isAndroid ? 5000000 : 1; // in PhantomJS, if you use 0 it will crash - } - - function websqlBulkDocs(dbOpts, req, opts, api, db, websqlChanges, 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 && isLocalId(doc._id)) { - return doc; - } - var newDoc = 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 ExportedMap(); - - var preconditionErrored; - function complete() { - if (preconditionErrored) { - return callback(preconditionErrored); - } - websqlChanges.notify(api._name); - callback(null, results); - } - - function verifyAttachment(digest, callback) { - var sql = 'SELECT count(*) as cnt FROM ' + ATTACH_STORE$1 + - ' WHERE digest=?'; - tx.executeSql(sql, [digest], function (tx, result) { - if (result.rows.item(0).cnt === 0) { - var err = createError(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$$1, winningRevIsDeleted, newRevIsDeleted, - isUpdate, delta, resultsIdx, callback) { - - function finish() { - var data = docInfo.data; - var deletedInt = newRevIsDeleted ? 1 : 0; - - var id = data._id; - var rev = data._rev; - var json = stringifyDoc(data); - var sql = 'INSERT INTO ' + BY_SEQ_STORE$1 + - ' (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$1 + - ' (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$1, 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$1 + - ' 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 (newRevIsDeleted) { - 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; - att.revpos = parseInt(winningRev$$1, 10); - var digest = att.digest; - saveAttachment(digest, data, attachmentSaved); - } else { - recv++; - collectResults(); - } - }); - - if (!attachments.length) { - finish(); - } - - function dataWritten(tx, seq) { - var id = docInfo.metadata.id; - - var revsToCompact = docInfo.stemmedRevs || []; - if (isUpdate && api.auto_compaction) { - revsToCompact = compactTree(docInfo.metadata).concat(revsToCompact); - } - if (revsToCompact.length) { - compactRevs$1(revsToCompact, id, tx); - } - - docInfo.metadata.seq = seq; - var rev = docInfo.metadata.rev; - delete docInfo.metadata.rev; - - var sql = isUpdate ? - 'UPDATE ' + DOC_STORE$1 + - ' SET json=?, max_seq=?, winningseq=' + - '(SELECT seq FROM ' + BY_SEQ_STORE$1 + - ' WHERE doc_id=' + DOC_STORE$1 + '.id AND rev=?) WHERE id=?' - : 'INSERT INTO ' + DOC_STORE$1 + - ' (id, winningseq, max_seq, json) VALUES (?,?,?,?);'; - var metadataStr = safeJsonStringify(docInfo.metadata); - var params = isUpdate ? - [metadataStr, seq, winningRev$$1, id] : - [id, seq, seq, metadataStr]; - tx.executeSql(sql, params, function () { - results[resultsIdx] = { - ok: true, - id: docInfo.metadata.id, - rev: rev - }; - fetchedDocs.set(id, docInfo.metadata); - callback(); - }); - } - } - - function websqlProcessDocs() { - processDocs(dbOpts.revs_limit, 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 && isLocalId(docInfo._id)) { - return checkDone(); // skip local docs - } - var id = docInfo.metadata.id; - tx.executeSql('SELECT json FROM ' + DOC_STORE$1 + - ' WHERE id = ?', [id], function (tx, result) { - if (result.rows.length) { - var metadata = safeJsonParse(result.rows.item(0).json); - fetchedDocs.set(id, metadata); - } - checkDone(); - }); - }); - } - - function saveAttachment(digest, data, callback) { - var sql = 'SELECT digest FROM ' + ATTACH_STORE$1 + ' 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$1 + - ' (digest, body, escaped) VALUES (?,?,1)'; - tx.executeSql(sql, [digest, escapeBlob(data)], function () { - callback(); - }, function () { - // ignore constaint errors, means it already exists - callback(); - return false; // ack we handled the error - }); - }); - } - - 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(websqlProcessDocs); - } - }); - }, websqlError(callback), complete); - }); - } - - var cachedDatabases = new ExportedMap(); - - // openDatabase passed in through opts (e.g. for node-websql) - function openDatabaseWithOpts(opts) { - return opts.websql(opts.name, opts.version, opts.description, opts.size); - } - - function openDBSafely(opts) { - try { - return { - db: openDatabaseWithOpts(opts) - }; - } catch (err) { - return { - error: err - }; - } - } - - function openDB(opts) { - var cachedResult = cachedDatabases.get(opts.name); - if (!cachedResult) { - cachedResult = openDBSafely(opts); - cachedDatabases.set(opts.name, cachedResult); - } - return cachedResult; - } - - var websqlChanges = new Changes(); - - function fetchAttachmentsIfNecessary$1(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 = {binary: opts.binary, ctx: txn}; - api._getAttachment(doc._id, att, attObj, attOpts, function (_, data) { - doc._attachments[att] = $inject_Object_assign( - pick(attObj, ['digest', 'content_type']), - { data: data } - ); - checkDone(); - }); - } - - attachments.forEach(function (att) { - if (opts.attachments && opts.include_docs) { - fetchAttachment(doc, att); - } else { - doc._attachments[att].stub = true; - checkDone(); - } - }); - } - - 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$1 + ' (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$1 + ' (doc_id, rev)'; - var DOC_STORE_WINNINGSEQ_INDEX_SQL = - 'CREATE INDEX IF NOT EXISTS \'doc-winningseq-idx\' ON ' + - DOC_STORE$1 + ' (winningseq)'; - var ATTACH_AND_SEQ_STORE_SEQ_INDEX_SQL = - 'CREATE INDEX IF NOT EXISTS \'attach-seq-seq-idx\' ON ' + - ATTACH_AND_SEQ_STORE$1 + ' (seq)'; - var ATTACH_AND_SEQ_STORE_ATTACH_INDEX_SQL = - 'CREATE UNIQUE INDEX IF NOT EXISTS \'attach-seq-digest-idx\' ON ' + - ATTACH_AND_SEQ_STORE$1 + ' (digest, seq)'; - - var DOC_STORE_AND_BY_SEQ_JOINER = BY_SEQ_STORE$1 + - '.seq = ' + DOC_STORE$1 + '.winningseq'; - - var SELECT_DOCS = BY_SEQ_STORE$1 + '.seq AS seq, ' + - BY_SEQ_STORE$1 + '.deleted AS deleted, ' + - BY_SEQ_STORE$1 + '.json AS data, ' + - BY_SEQ_STORE$1 + '.rev AS rev, ' + - DOC_STORE$1 + '.json AS metadata'; - - function WebSqlPouch(opts, callback) { - var api = this; - var instanceId = null; - var size = getSize(opts); - var idRequests = []; - var encoding; - - api._name = opts.name; - - // extend the options here, because sqlite plugin has a ton of options - // and they are constantly changing, so it's more prudent to allow anything - var websqlOpts = $inject_Object_assign({}, opts, { - version: POUCH_VERSION, - description: opts.name, - size: size - }); - var openDBResult = openDB(websqlOpts); - if (openDBResult.error) { - return websqlError(callback)(openDBResult.error); - } - var db = openDBResult.db; - 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 (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$1 + - ' ADD COLUMN deleted TINYINT(1) DEFAULT 0', [], function () { - tx.executeSql(BY_SEQ_STORE_DELETED_INDEX_SQL); - tx.executeSql('ALTER TABLE ' + DOC_STORE$1 + - ' ADD COLUMN local TINYINT(1) DEFAULT 0', [], function () { - tx.executeSql('CREATE INDEX IF NOT EXISTS \'doc-store-local-idx\' ON ' + - DOC_STORE$1 + ' (local, id)'); - - var sql = 'SELECT ' + DOC_STORE$1 + '.winningseq AS seq, ' + DOC_STORE$1 + - '.json AS metadata FROM ' + BY_SEQ_STORE$1 + ' JOIN ' + DOC_STORE$1 + - ' ON ' + BY_SEQ_STORE$1 + '.seq = ' + DOC_STORE$1 + '.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 (isDeleted(metadata)) { - deleted.push(seq); - } - if (isLocalId(metadata.id)) { - local.push(metadata.id); - } - } - tx.executeSql('UPDATE ' + DOC_STORE$1 + 'SET local = 1 WHERE id IN ' + - qMarks(local.length), local, function () { - tx.executeSql('UPDATE ' + BY_SEQ_STORE$1 + - ' 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$1 + - ' (id UNIQUE, rev, json)'; - tx.executeSql(local, [], function () { - var sql = 'SELECT ' + DOC_STORE$1 + '.id AS id, ' + - BY_SEQ_STORE$1 + '.json AS data ' + - 'FROM ' + BY_SEQ_STORE$1 + ' JOIN ' + - DOC_STORE$1 + ' ON ' + BY_SEQ_STORE$1 + '.seq = ' + - DOC_STORE$1 + '.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$$1 = JSON.parse(row.data)._rev; - tx.executeSql('INSERT INTO ' + LOCAL_STORE$1 + - ' (id, rev, json) VALUES (?,?,?)', - [row.id, rev$$1, row.data], function (tx) { - tx.executeSql('DELETE FROM ' + DOC_STORE$1 + ' WHERE id=?', - [row.id], function (tx) { - tx.executeSql('DELETE FROM ' + BY_SEQ_STORE$1 + ' 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$$1 = doc_id_rev.substring(idx + 2); - var sql = 'UPDATE ' + BY_SEQ_STORE$1 + - ' SET doc_id=?, rev=? WHERE doc_id_rev=?'; - tx.executeSql(sql, [doc_id, rev$$1, doc_id_rev], function () { - doNext(); - }); - } - doNext(); - } - - var sql = 'ALTER TABLE ' + BY_SEQ_STORE$1 + ' ADD COLUMN doc_id'; - tx.executeSql(sql, [], function (tx) { - var sql = 'ALTER TABLE ' + BY_SEQ_STORE$1 + ' 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$1; - 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$1; - 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$1 + '.id AS id', - [DOC_STORE$1, BY_SEQ_STORE$1], - DOC_STORE_AND_BY_SEQ_JOINER, - null, - DOC_STORE$1 + '.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$1 + - ' (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$1 + ' (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$1 + - ' 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$1 + - ' ADD COLUMN max_seq INTEGER'; - tx.executeSql(sql, [], function (tx) { - var sql = 'UPDATE ' + DOC_STORE$1 + ' SET max_seq=(SELECT MAX(seq) FROM ' + - BY_SEQ_STORE$1 + ' 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$1 + ' (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$1 + - ' (dbid, db_version INTEGER)'; - var attach = 'CREATE TABLE IF NOT EXISTS ' + ATTACH_STORE$1 + - ' (digest UNIQUE, escaped TINYINT(1), body BLOB)'; - var attachAndRev = 'CREATE TABLE IF NOT EXISTS ' + - ATTACH_AND_SEQ_STORE$1 + ' (digest, seq INTEGER)'; - // TODO: migrate winningseq to INTEGER - var doc = 'CREATE TABLE IF NOT EXISTS ' + DOC_STORE$1 + - ' (id unique, json, winningseq, max_seq INTEGER UNIQUE)'; - var seq = 'CREATE TABLE IF NOT EXISTS ' + BY_SEQ_STORE$1 + - ' (seq INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, ' + - 'json, deleted TINYINT(1), doc_id, rev)'; - var local = 'CREATE TABLE IF NOT EXISTS ' + LOCAL_STORE$1 + - ' (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$1 + - ' (db_version, dbid) VALUES (?,?)'; - instanceId = uuid(); - var initSeqArgs = [ADAPTER_VERSION$1, instanceId]; - tx.executeSql(initSeq, initSeqArgs, function () { - onGetInstanceId(); - }); - }); - }); - }); - } else { // version > 0 - - var setupDone = function () { - var migrated = dbVersion < ADAPTER_VERSION$1; - if (migrated) { - // update the db version within this transaction - tx.executeSql('UPDATE ' + META_STORE$1 + ' SET db_version = ' + - ADAPTER_VERSION$1); - } - // notify db.id() callers - var sql = 'SELECT dbid FROM ' + META_STORE$1; - 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); - }); - }, websqlError(callback), dbCreated); - } - - function fetchVersion(tx) { - var sql = 'SELECT sql FROM sqlite_master WHERE tbl_name = ' + META_STORE$1; - 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$1 + - ' 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$1, - [], function (tx, result) { - var dbVersion = result.rows.item(0).db_version; - onGetVersion(tx, dbVersion); - }); - } - }); - } - - setup(); - - function getMaxSeq(tx, callback) { - var sql = 'SELECT MAX(seq) AS seq FROM ' + BY_SEQ_STORE$1; - tx.executeSql(sql, [], function (tx, res) { - var updateSeq = res.rows.item(0).seq || 0; - callback(updateSeq); - }); - } - - function countDocs(tx, callback) { - // count the total rows - var sql = select( - 'COUNT(' + DOC_STORE$1 + '.id) AS \'num\'', - [DOC_STORE$1, BY_SEQ_STORE$1], - DOC_STORE_AND_BY_SEQ_JOINER, - BY_SEQ_STORE$1 + '.deleted=0'); - - tx.executeSql(sql, [], function (tx, result) { - callback(result.rows.item(0).num); - }); - } - - api._remote = false; - api.type = function () { - return 'websql'; - }; - - api._id = toPromise(function (callback) { - callback(null, instanceId); - }); - - api._info = function (callback) { - var seq; - var docCount; - db.readTransaction(function (tx) { - getMaxSeq(tx, function (theSeq) { - seq = theSeq; - }); - countDocs(tx, function (theDocCount) { - docCount = theDocCount; - }); - }, websqlError(callback), function () { - callback(null, { - doc_count: docCount, - update_seq: seq, - websql_encoding: encoding - }); - }); - }; - - api._bulkDocs = function (req, reqOpts, callback) { - websqlBulkDocs(opts, req, reqOpts, api, db, websqlChanges, callback); - }; - - function latest$$1(tx, id, rev$$1, callback, finish) { - var sql = select( - SELECT_DOCS, - [DOC_STORE$1, BY_SEQ_STORE$1], - DOC_STORE_AND_BY_SEQ_JOINER, - DOC_STORE$1 + '.id=?'); - var sqlArgs = [id]; - - tx.executeSql(sql, sqlArgs, function (a, results) { - if (!results.rows.length) { - var err = createError(MISSING_DOC, 'missing'); - return finish(err); - } - var item = results.rows.item(0); - var metadata = safeJsonParse(item.metadata); - callback(latest(rev$$1, metadata)); - }); - } - - api._get = function (id, opts, callback) { - var doc; - var metadata; - var tx = opts.ctx; - if (!tx) { - return db.readTransaction(function (txn) { - api._get(id, $inject_Object_assign({ctx: txn}, opts), callback); - }); - } - - function finish(err) { - callback(err, {doc: doc, metadata: metadata, ctx: tx}); - } - - var sql; - var sqlArgs; - - if (!opts.rev) { - sql = select( - SELECT_DOCS, - [DOC_STORE$1, BY_SEQ_STORE$1], - DOC_STORE_AND_BY_SEQ_JOINER, - DOC_STORE$1 + '.id=?'); - sqlArgs = [id]; - } else if (opts.latest) { - latest$$1(tx, id, opts.rev, function (latestRev) { - opts.latest = false; - opts.rev = latestRev; - api._get(id, opts, callback); - }, finish); - return; - } else { - sql = select( - SELECT_DOCS, - [DOC_STORE$1, BY_SEQ_STORE$1], - DOC_STORE$1 + '.id=' + BY_SEQ_STORE$1 + '.doc_id', - [BY_SEQ_STORE$1 + '.doc_id=?', BY_SEQ_STORE$1 + '.rev=?']); - sqlArgs = [id, opts.rev]; - } - - tx.executeSql(sql, sqlArgs, function (a, results) { - if (!results.rows.length) { - var missingErr = createError(MISSING_DOC, 'missing'); - return finish(missingErr); - } - var item = results.rows.item(0); - metadata = safeJsonParse(item.metadata); - if (item.deleted && !opts.rev) { - var deletedErr = createError(MISSING_DOC, 'deleted'); - return finish(deletedErr); - } - doc = unstringifyDoc(item.data, metadata.id, item.rev); - finish(); - }); - }; - - api._allDocs = function (opts, callback) { - var results = []; - var totalRows; - var updateSeq; - - 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 keys = 'keys' in opts ? opts.keys : 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 (keys) { - var destinctKeys = []; - var bindingStr = ""; - keys.forEach(function (key) { - if (destinctKeys.indexOf(key) === -1) { - destinctKeys.push(key); - bindingStr += '?,'; - } - }); - bindingStr = bindingStr.substring(0, bindingStr.length - 1); // keys is never empty - criteria.push(DOC_STORE$1 + '.id IN (' + bindingStr + ')'); - sqlArgs = sqlArgs.concat(destinctKeys); - } else if (key !== false) { - criteria.push(DOC_STORE$1 + '.id = ?'); - sqlArgs.push(key); - } else if (start !== false || end !== false) { - if (start !== false) { - criteria.push(DOC_STORE$1 + '.id ' + (descending ? '<=' : '>=') + ' ?'); - sqlArgs.push(start); - } - if (end !== false) { - var comparator = descending ? '>' : '<'; - if (inclusiveEnd) { - comparator += '='; - } - criteria.push(DOC_STORE$1 + '.id ' + comparator + ' ?'); - sqlArgs.push(end); - } - if (key !== false) { - criteria.push(DOC_STORE$1 + '.id = ?'); - sqlArgs.push(key); - } - } - - if (!keys) { - // report deleted if keys are specified - criteria.push(BY_SEQ_STORE$1 + '.deleted = 0'); - } - - db.readTransaction(function (tx) { - // count the docs in parallel to other operations - countDocs(tx, function (docCount) { - totalRows = docCount; - }); - - /* istanbul ignore if */ - if (opts.update_seq) { - // get max sequence in parallel to other operations - getMaxSeq(tx, function (theSeq) { - updateSeq = theSeq; - }); - } - - if (limit === 0) { - return; - } - - // do a single query to fetch the documents - var sql = select( - SELECT_DOCS, - [DOC_STORE$1, BY_SEQ_STORE$1], - DOC_STORE_AND_BY_SEQ_JOINER, - criteria, - DOC_STORE$1 + '.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 = safeJsonParse(item.metadata); - var id = metadata.id; - var data = unstringifyDoc(item.data, id, item.rev); - var winningRev$$1 = data._rev; - var doc = { - id: id, - key: id, - value: {rev: winningRev$$1} - }; - if (opts.include_docs) { - doc.doc = data; - doc.doc._rev = winningRev$$1; - if (opts.conflicts) { - var conflicts = collectConflicts(metadata); - if (conflicts.length) { - doc.doc._conflicts = conflicts; - } - } - fetchAttachmentsIfNecessary$1(doc.doc, opts, api, tx); - } - if (item.deleted) { - if (keys) { - doc.value.deleted = true; - doc.doc = null; - } else { - // propably should not happen - continue; - } - } - if (!keys) { - results.push(doc); - } else { - var index = keys.indexOf(id, index); - do { - results[index] = doc; - index = keys.indexOf(id, index + 1); - } while (index > -1 && index < keys.length); - } - } - if (keys) { - keys.forEach(function (key, index) { - if (!results[index]) { - results[index] = {key: key, error: 'not_found'}; - } - }); - } - }); - }, websqlError(callback), function () { - var returnVal = { - total_rows: totalRows, - offset: opts.skip, - rows: results - }; - - /* istanbul ignore if */ - if (opts.update_seq) { - returnVal.update_seq = updateSeq; - } - callback(null, returnVal); - }); - }; - - api._changes = function (opts) { - opts = clone(opts); - - if (opts.continuous) { - var id = api._name + ':' + uuid(); - websqlChanges.addListener(api._name, id, api, opts); - websqlChanges.notify(api._name); - return { - cancel: function () { - websqlChanges.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 ('return_docs' in opts) { - returnDocs = opts.return_docs; - } else if ('returnDocs' in opts) { - // TODO: Remove 'returnDocs' in favor of 'return_docs' in a future release - returnDocs = opts.returnDocs; - } else { - returnDocs = true; - } - var results = []; - var numResults = 0; - - function fetchChanges() { - - var selectStmt = - DOC_STORE$1 + '.json AS metadata, ' + - DOC_STORE$1 + '.max_seq AS maxSeq, ' + - BY_SEQ_STORE$1 + '.json AS winningDoc, ' + - BY_SEQ_STORE$1 + '.rev AS winningRev '; - - var from = DOC_STORE$1 + ' JOIN ' + BY_SEQ_STORE$1; - - var joiner = DOC_STORE$1 + '.id=' + BY_SEQ_STORE$1 + '.doc_id' + - ' AND ' + DOC_STORE$1 + '.winningseq=' + BY_SEQ_STORE$1 + '.seq'; - - var criteria = ['maxSeq > ?']; - var sqlArgs = [opts.since]; - - if (opts.doc_ids) { - criteria.push(DOC_STORE$1 + '.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 = 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 = 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; - - var filtered = filter(change); - if (typeof filtered === 'object') { - return opts.complete(filtered); + var offset = 0; + var pageSize = 10; + function nextPage() { + var sql = select( + SELECT_DOCS + ', ' + DOC_STORE$1 + '.id AS id', + [DOC_STORE$1, BY_SEQ_STORE$1], + DOC_STORE_AND_BY_SEQ_JOINER, + null, + DOC_STORE$1 + '.id ' + ); + sql += ' LIMIT ' + pageSize + ' OFFSET ' + offset; + offset += pageSize; + tx.executeSql(sql, [], function (tx, res) { + if (!res.rows.length) { + return callback(tx); } - - if (filtered) { - numResults++; - if (returnDocs) { - results.push(change); + 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); } - // process the attachment immediately - // for the benefit of live listeners - if (opts.attachments && opts.include_docs) { - fetchAttachmentsIfNecessary$1(doc, opts, api, tx, - reportChange(change)); - } else { - reportChange(change)(); + } + 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); } } - if (numResults === limit) { - break; + 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(); } - } - }); - }, websqlError(opts.complete), function () { - if (!opts.continuous) { - opts.complete(null, { - results: results, - last_seq: lastSeq + var numDone = 0; + digestSeqPairs.forEach(function (pair) { + var sql = 'INSERT INTO ' + ATTACH_AND_SEQ_STORE$1 + + ' (digest, seq) VALUES (?,?)'; + tx.executeSql(sql, pair, function () { + if (++numDone === digestSeqPairs.length) { + nextPage(); + } + }); + }); }); } - }); - } - - fetchChanges(); - }; - - api._close = function (callback) { - //WebSQL databases do not need to be closed - callback(); - }; - - api._getAttachment = function (docId, attachId, 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$1 + ' 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 ? unescapeBlob(item.body) : - parseHexString(item.body, encoding); - if (opts.binary) { - res = binStringToBluffer(data, type); - } else { - res = thisBtoa(data); - } - callback(null, res); - }); - }; - - api._getRevisionTree = function (docId, callback) { - db.readTransaction(function (tx) { - var sql = 'SELECT json AS metadata FROM ' + DOC_STORE$1 + ' WHERE id = ?'; - tx.executeSql(sql, [docId], function (tx, result) { - if (!result.rows.length) { - callback(createError(MISSING_DOC)); - } else { - var data = safeJsonParse(result.rows.item(0).metadata); - callback(null, data.rev_tree); - } - }); - }); - }; - - api._doCompaction = function (docId, revs, callback) { - if (!revs.length) { - return callback(); + nextPage(); + }); } - db.transaction(function (tx) { - // update doc store - var sql = 'SELECT json AS metadata FROM ' + DOC_STORE$1 + ' WHERE id = ?'; - tx.executeSql(sql, [docId], function (tx, result) { - var metadata = safeJsonParse(result.rows.item(0).metadata); - traverseRevTree(metadata.rev_tree, function (isLeaf, pos, - revHash, ctx, opts) { - var rev$$1 = pos + '-' + revHash; - if (revs.indexOf(rev$$1) !== -1) { - opts.status = 'missing'; - } + var attachAndRev = 'CREATE TABLE IF NOT EXISTS ' + + ATTACH_AND_SEQ_STORE$1 + ' (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); }); - - var sql = 'UPDATE ' + DOC_STORE$1 + ' SET json = ? WHERE id = ?'; - tx.executeSql(sql, [safeJsonStringify(metadata), docId]); - }); - - compactRevs$1(revs, docId, tx); - }, websqlError(callback), function () { - callback(); }); - }; + } - api._getLocal = function (id, callback) { - db.readTransaction(function (tx) { - var sql = 'SELECT json, rev FROM ' + LOCAL_STORE$1 + ' 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(createError(MISSING_DOC)); - } + // 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$1 + + ' 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$1 + + ' ADD COLUMN max_seq INTEGER'; + tx.executeSql(sql, [], function (tx) { + var sql = 'UPDATE ' + DOC_STORE$1 + ' SET max_seq=(SELECT MAX(seq) FROM ' + + BY_SEQ_STORE$1 + ' 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$1 + ' (max_seq)'; + tx.executeSql(sql, [], callback); }); }); - }; - - 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$1 + ' SET rev=?, json=? ' + - 'WHERE id=? AND rev=?'; - values = [newRev, json, id, oldRev]; - } else { - sql = 'INSERT INTO ' + LOCAL_STORE$1 + ' (id, rev, json) VALUES (?,?,?)'; - values = [id, newRev, json]; + 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(); } - 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(createError(REV_CONFLICT)); - } - }, function () { - callback(createError(REV_CONFLICT)); - return false; // ack that we handled the error - }); - } + ); + } - if (opts.ctx) { - putLocal(opts.ctx); - } else { - db.transaction(putLocal, websqlError(callback), function () { - if (ret) { - callback(null, ret); - } - }); + function onGetInstanceId() { + while (idRequests.length > 0) { + var idCallback = idRequests.pop(); + idCallback(null, instanceId); } - }; + } - api._removeLocal = function (doc, opts, callback) { - if (typeof opts === 'function') { - callback = opts; - opts = {}; - } - var ret; + function onGetVersion(tx, dbVersion) { + if (dbVersion === 0) { + // initial schema - function removeLocal(tx) { - var sql = 'DELETE FROM ' + LOCAL_STORE$1 + ' WHERE id=? AND rev=?'; - var params = [doc._id, doc._rev]; - tx.executeSql(sql, params, function (tx, res) { - if (!res.rowsAffected) { - return callback(createError(MISSING_DOC)); - } - ret = {ok: true, id: doc._id, rev: '0-0'}; - if (opts.ctx) { // return immediately - callback(null, ret); - } - }); - } + var meta = 'CREATE TABLE IF NOT EXISTS ' + META_STORE$1 + + ' (dbid, db_version INTEGER)'; + var attach = 'CREATE TABLE IF NOT EXISTS ' + ATTACH_STORE$1 + + ' (digest UNIQUE, escaped TINYINT(1), body BLOB)'; + var attachAndRev = 'CREATE TABLE IF NOT EXISTS ' + + ATTACH_AND_SEQ_STORE$1 + ' (digest, seq INTEGER)'; + // TODO: migrate winningseq to INTEGER + var doc = 'CREATE TABLE IF NOT EXISTS ' + DOC_STORE$1 + + ' (id unique, json, winningseq, max_seq INTEGER UNIQUE)'; + var seq = 'CREATE TABLE IF NOT EXISTS ' + BY_SEQ_STORE$1 + + ' (seq INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, ' + + 'json, deleted TINYINT(1), doc_id, rev)'; + var local = 'CREATE TABLE IF NOT EXISTS ' + LOCAL_STORE$1 + + ' (id UNIQUE, rev, json)'; - if (opts.ctx) { - removeLocal(opts.ctx); - } else { - db.transaction(removeLocal, websqlError(callback), function () { - if (ret) { - callback(null, ret); - } + // 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); }); - } - }; - - api._destroy = function (opts, callback) { - websqlChanges.removeAllListeners(api._name); - db.transaction(function (tx) { - var stores = [DOC_STORE$1, BY_SEQ_STORE$1, ATTACH_STORE$1, META_STORE$1, - LOCAL_STORE$1, ATTACH_AND_SEQ_STORE$1]; - stores.forEach(function (store) { - tx.executeSql('DROP TABLE IF EXISTS ' + store, []); + 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$1 + + ' (db_version, dbid) VALUES (?,?)'; + instanceId = uuid(); + var initSeqArgs = [ADAPTER_VERSION$1, instanceId]; + tx.executeSql(initSeq, initSeqArgs, function () { + onGetInstanceId(); + }); + }); + }); }); - }, websqlError(callback), function () { - if (hasLocalStorage()) { - delete window.localStorage['_pouch__websqldb_' + api._name]; - delete window.localStorage[api._name]; - } - callback(null, {'ok': true}); - }); - }; - } + } else { // version > 0 - function canOpenTestDB() { - try { - openDatabase('_pouch_validate_websql', 1, '', 1); - return true; - } catch (err) { - return false; - } - } + var setupDone = function () { + var migrated = dbVersion < ADAPTER_VERSION$1; + if (migrated) { + // update the db version within this transaction + tx.executeSql('UPDATE ' + META_STORE$1 + ' SET db_version = ' + + ADAPTER_VERSION$1); + } + // notify db.id() callers + var sql = 'SELECT dbid FROM ' + META_STORE$1; + tx.executeSql(sql, [], function (tx, result) { + instanceId = result.rows.item(0).dbid; + onGetInstanceId(); + }); + }; - // WKWebView had a bug where WebSQL would throw a DOM Exception 18 - // (see https://bugs.webkit.org/show_bug.cgi?id=137760 and - // https://github.com/pouchdb/pouchdb/issues/5079) - // This has been fixed in latest WebKit, so we try to detect it here. - function isValidWebSQL() { - // WKWebView UA: - // Mozilla/5.0 (iPhone; CPU iPhone OS 9_2 like Mac OS X) - // AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13C75 - // Chrome for iOS UA: - // Mozilla/5.0 (iPhone; U; CPU iPhone OS 5_1_1 like Mac OS X; en) - // AppleWebKit/534.46.0 (KHTML, like Gecko) CriOS/19.0.1084.60 - // Mobile/9B206 Safari/7534.48.3 - // Firefox for iOS UA: - // Mozilla/5.0 (iPhone; CPU iPhone OS 8_3 like Mac OS X) AppleWebKit/600.1.4 - // (KHTML, like Gecko) FxiOS/1.0 Mobile/12F69 Safari/600.1.4 + // would love to use promises here, but then websql + // ends the transaction early + var tasks = [ + runMigration2, + runMigration3, + runMigration4, + runMigration5, + runMigration6, + runMigration7, + setupDone + ]; - // indexedDB is null on some UIWebViews and undefined in others - // see: https://bugs.webkit.org/show_bug.cgi?id=137034 - if (typeof indexedDB === 'undefined' || indexedDB === null || - !/iP(hone|od|ad)/.test(navigator.userAgent)) { - // definitely not WKWebView, avoid creating an unnecessary database - return true; - } - // Cache the result in LocalStorage. Reason we do this is because if we - // call openDatabase() too many times, Safari craps out in SauceLabs and - // starts throwing DOM Exception 14s. - var hasLS = hasLocalStorage(); - // Include user agent in the hash, so that if Safari is upgraded, we don't - // continually think it's broken. - var localStorageKey = '_pouch__websqldb_valid_' + navigator.userAgent; - if (hasLS && localStorage[localStorageKey]) { - return localStorage[localStorageKey] === '1'; + // run each migration sequentially + var i = dbVersion; + var nextMigration = function (tx) { + tasks[i - 1](tx, nextMigration); + i++; + }; + nextMigration(tx); + } } - var openedTestDB = canOpenTestDB(); - if (hasLS) { - localStorage[localStorageKey] = openedTestDB ? '1' : '0'; + + function setup() { + db.transaction(function (tx) { + // first check the encoding + checkEncoding(tx, function () { + // then get the version + fetchVersion(tx); + }); + }, websqlError(callback), dbCreated); } - return openedTestDB; - } - function valid() { - if (typeof openDatabase !== 'function') { - return false; + function fetchVersion(tx) { + var sql = 'SELECT sql FROM sqlite_master WHERE tbl_name = ' + META_STORE$1; + 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$1 + + ' 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$1, + [], function (tx, result) { + var dbVersion = result.rows.item(0).db_version; + onGetVersion(tx, dbVersion); + }); + } + }); } - return isValidWebSQL(); - } - function openDB$2(name, version, description, size) { - // Traditional WebSQL API - return openDatabase(name, version, description, size); - } + setup(); - function WebSQLPouch(opts, callback) { - var msg = 'WebSQL is deprecated and will be removed in future releases of PouchDB. ' + - 'Please migrate to IndexedDB: https://pouchdb.com/2018/01/23/pouchdb-6.4.2.html'; - guardedConsole('warn', msg); - var _opts = $inject_Object_assign({ - websql: openDB$2 - }, opts); + function getMaxSeq(tx, callback) { + var sql = 'SELECT MAX(seq) AS seq FROM ' + BY_SEQ_STORE$1; + tx.executeSql(sql, [], function (tx, res) { + var updateSeq = res.rows.item(0).seq || 0; + callback(updateSeq); + }); + } - WebSqlPouch.call(this, _opts, callback); - } + function countDocs(tx, callback) { + // count the total rows + var sql = select( + 'COUNT(' + DOC_STORE$1 + '.id) AS \'num\'', + [DOC_STORE$1, BY_SEQ_STORE$1], + DOC_STORE_AND_BY_SEQ_JOINER, + BY_SEQ_STORE$1 + '.deleted=0'); - WebSQLPouch.valid = valid; + tx.executeSql(sql, [], function (tx, result) { + callback(result.rows.item(0).num); + }); + } - WebSQLPouch.use_prefix = true; + api.type = function () { + return 'websql'; + }; - function WebSqlPouch$1 (PouchDB) { - PouchDB.adapter('websql', WebSQLPouch, true); - } + api._id = toPromise(function (callback) { + callback(null, instanceId); + }); - /* global fetch */ - /* global Headers */ - function wrappedFetch() { - var wrappedPromise = {}; + api._info = function (callback) { + var seq; + var docCount; + db.readTransaction(function (tx) { + getMaxSeq(tx, function (theSeq) { + seq = theSeq; + }); + countDocs(tx, function (theDocCount) { + docCount = theDocCount; + }); + }, websqlError(callback), function () { + callback(null, { + doc_count: docCount, + update_seq: seq, + websql_encoding: encoding + }); + }); + }; - var promise = new PouchPromise(function (resolve, reject) { - wrappedPromise.resolve = resolve; - wrappedPromise.reject = reject; - }); + api._bulkDocs = function (req, reqOpts, callback) { + websqlBulkDocs(opts, req, reqOpts, api, db, websqlChanges, callback); + }; - var args = new Array(arguments.length); + function latest$$1(tx, id, rev, callback, finish) { + var sql = select( + SELECT_DOCS, + [DOC_STORE$1, BY_SEQ_STORE$1], + DOC_STORE_AND_BY_SEQ_JOINER, + DOC_STORE$1 + '.id=?'); + var sqlArgs = [id]; - for (var i = 0; i < args.length; i++) { - args[i] = arguments[i]; + tx.executeSql(sql, sqlArgs, function (a, results) { + if (!results.rows.length) { + var err = createError(MISSING_DOC, 'missing'); + return finish(err); + } + var item = results.rows.item(0); + var metadata = safeJsonParse(item.metadata); + callback(latest(rev, metadata)); + }); } - wrappedPromise.promise = promise; - - PouchPromise.resolve().then(function () { - return fetch.apply(null, args); - }).then(function (response) { - wrappedPromise.resolve(response); - }).catch(function (error) { - wrappedPromise.reject(error); - }); + api._get = function (id, opts, callback) { + var doc; + var metadata; + var tx = opts.ctx; + if (!tx) { + return db.readTransaction(function (txn) { + api._get(id, assign$1({ctx: txn}, opts), callback); + }); + } - return wrappedPromise; - } + function finish(err) { + callback(err, {doc: doc, metadata: metadata, ctx: tx}); + } - function fetchRequest(options, callback) { - var wrappedPromise, timer, response; + var sql; + var sqlArgs; - var headers = new Headers(); + if(!opts.rev) { + sql = select( + SELECT_DOCS, + [DOC_STORE$1, BY_SEQ_STORE$1], + DOC_STORE_AND_BY_SEQ_JOINER, + DOC_STORE$1 + '.id=?'); + sqlArgs = [id]; + } else if (opts.latest) { + latest$$1(tx, id, opts.rev, function (latestRev) { + opts.latest = false; + opts.rev = latestRev; + api._get(id, opts, callback); + }, finish); + return; + } else { + sql = select( + SELECT_DOCS, + [DOC_STORE$1, BY_SEQ_STORE$1], + DOC_STORE$1 + '.id=' + BY_SEQ_STORE$1 + '.doc_id', + [BY_SEQ_STORE$1 + '.doc_id=?', BY_SEQ_STORE$1 + '.rev=?']); + sqlArgs = [id, opts.rev]; + } - var fetchOptions = { - method: options.method, - credentials: 'include', - headers: headers + tx.executeSql(sql, sqlArgs, function (a, results) { + if (!results.rows.length) { + var missingErr = createError(MISSING_DOC, 'missing'); + return finish(missingErr); + } + var item = results.rows.item(0); + metadata = safeJsonParse(item.metadata); + if (item.deleted && !opts.rev) { + var deletedErr = createError(MISSING_DOC, 'deleted'); + return finish(deletedErr); + } + doc = unstringifyDoc(item.data, metadata.id, item.rev); + finish(); + }); }; - if (options.json) { - headers.set('Accept', 'application/json'); - headers.set('Content-Type', options.headers['Content-Type'] || - 'application/json'); - } + api._allDocs = function (opts, callback) { + var results = []; + var totalRows; - if (options.body && - options.processData && - typeof options.body !== 'string') { - fetchOptions.body = JSON.stringify(options.body); - } else if ('body' in options) { - fetchOptions.body = options.body; - } else { - fetchOptions.body = null; - } + 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$1 + '.id = ?'); + sqlArgs.push(key); + } else if (start !== false || end !== false) { + if (start !== false) { + criteria.push(DOC_STORE$1 + '.id ' + (descending ? '<=' : '>=') + ' ?'); + sqlArgs.push(start); + } + if (end !== false) { + var comparator = descending ? '>' : '<'; + if (inclusiveEnd) { + comparator += '='; + } + criteria.push(DOC_STORE$1 + '.id ' + comparator + ' ?'); + sqlArgs.push(end); + } + if (key !== false) { + criteria.push(DOC_STORE$1 + '.id = ?'); + sqlArgs.push(key); + } + } - Object.keys(options.headers).forEach(function (key) { - if (options.headers.hasOwnProperty(key)) { - headers.set(key, options.headers[key]); + if (opts.deleted !== 'ok') { + // report deleted if keys are specified + criteria.push(BY_SEQ_STORE$1 + '.deleted = 0'); } - }); - wrappedPromise = wrappedFetch(options.url, fetchOptions); + db.readTransaction(function (tx) { + // count the docs in parallel to other operations + countDocs(tx, function (docCount) { + totalRows = docCount; + }); - if (options.timeout > 0) { - timer = setTimeout(function () { - wrappedPromise.reject(new Error('Load timeout for resource: ' + - options.url)); - }, options.timeout); - } + if (limit === 0) { + return; + } - wrappedPromise.promise.then(function (fetchResponse) { - response = { - statusCode: fetchResponse.status - }; + // do a single query to fetch the documents + var sql = select( + SELECT_DOCS, + [DOC_STORE$1, BY_SEQ_STORE$1], + DOC_STORE_AND_BY_SEQ_JOINER, + criteria, + DOC_STORE$1 + '.id ' + (descending ? 'DESC' : 'ASC') + ); + sql += ' LIMIT ' + limit + ' OFFSET ' + offset; - if (options.timeout > 0) { - clearTimeout(timer); + 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 = safeJsonParse(item.metadata); + var id = metadata.id; + var data = unstringifyDoc(item.data, id, item.rev); + var winningRev$$1 = data._rev; + var doc = { + id: id, + key: id, + value: {rev: winningRev$$1} + }; + if (opts.include_docs) { + doc.doc = data; + doc.doc._rev = winningRev$$1; + if (opts.conflicts) { + var conflicts = collectConflicts(metadata); + if (conflicts.length) { + doc.doc._conflicts = conflicts; + } + } + fetchAttachmentsIfNecessary$1(doc.doc, opts, api, tx); + } + if (item.deleted) { + if (opts.deleted === 'ok') { + doc.value.deleted = true; + doc.doc = null; + } else { + continue; + } + } + results.push(doc); + } + }); + }, websqlError(callback), function () { + callback(null, { + total_rows: totalRows, + offset: opts.skip, + rows: results + }); + }); + }; + + api._changes = function (opts) { + opts = clone(opts); + + if (opts.continuous) { + var id = api._name + ':' + uuid(); + websqlChanges.addListener(api._name, id, api, opts); + websqlChanges.notify(api._name); + return { + cancel: function () { + websqlChanges.removeListener(api._name, id); + } + }; } - if (response.statusCode >= 200 && response.statusCode < 300) { - return options.binary ? fetchResponse.blob() : fetchResponse.text(); + 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 } - return fetchResponse.json(); - }).then(function (result) { - if (response.statusCode >= 200 && response.statusCode < 300) { - callback(null, response, result); + var returnDocs; + if ('return_docs' in opts) { + returnDocs = opts.return_docs; + } else if ('returnDocs' in opts) { + // TODO: Remove 'returnDocs' in favor of 'return_docs' in a future release + returnDocs = opts.returnDocs; } else { - result.status = response.statusCode; - callback(result); - } - }).catch(function (error) { - if (!error) { - // this happens when the listener is canceled - error = new Error('canceled'); + returnDocs = true; } - callback(error); - }); - - return {abort: wrappedPromise.reject}; - } + var results = []; + var numResults = 0; - function xhRequest(options, callback) { + function fetchChanges() { - var xhr, timer; - var timedout = false; + var selectStmt = + DOC_STORE$1 + '.json AS metadata, ' + + DOC_STORE$1 + '.max_seq AS maxSeq, ' + + BY_SEQ_STORE$1 + '.json AS winningDoc, ' + + BY_SEQ_STORE$1 + '.rev AS winningRev '; - var abortReq = function () { - xhr.abort(); - cleanUp(); - }; + var from = DOC_STORE$1 + ' JOIN ' + BY_SEQ_STORE$1; - var timeoutReq = function () { - timedout = true; - xhr.abort(); - cleanUp(); - }; + var joiner = DOC_STORE$1 + '.id=' + BY_SEQ_STORE$1 + '.doc_id' + + ' AND ' + DOC_STORE$1 + '.winningseq=' + BY_SEQ_STORE$1 + '.seq'; - var ret = {abort: abortReq}; + var criteria = ['maxSeq > ?']; + var sqlArgs = [opts.since]; - var cleanUp = function () { - clearTimeout(timer); - ret.abort = function () {}; - if (xhr) { - xhr.onprogress = undefined; - if (xhr.upload) { - xhr.upload.onprogress = undefined; + if (opts.doc_ids) { + criteria.push(DOC_STORE$1 + '.id IN ' + qMarks(opts.doc_ids.length)); + sqlArgs = sqlArgs.concat(opts.doc_ids); } - xhr.onreadystatechange = undefined; - xhr = undefined; - } - }; - - if (options.xhr) { - xhr = new options.xhr(); - } else { - xhr = new XMLHttpRequest(); - } - try { - xhr.open(options.method, options.url); - } catch (exception) { - return callback(new Error(exception.name || 'Url is invalid')); - } + var orderBy = 'maxSeq ' + (descending ? 'DESC' : 'ASC'); - xhr.withCredentials = ('withCredentials' in options) ? - options.withCredentials : true; + var sql = select(selectStmt, from, joiner, criteria, orderBy); - if (options.method === 'GET') { - delete options.headers['Content-Type']; - } else 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); - } - } + var filter = filterChange(opts); + if (!opts.view && !opts.filter) { + // we can just limit in the query + sql += ' LIMIT ' + limit; + } - if (options.binary) { - xhr.responseType = 'arraybuffer'; - } + 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 = safeJsonParse(item.metadata); + lastSeq = item.maxSeq; - if (!('body' in options)) { - options.body = null; - } + var doc = unstringifyDoc(item.winningDoc, metadata.id, + item.winningRev); + var change = opts.processChange(doc, metadata, opts); + change.seq = item.maxSeq; - for (var key in options.headers) { - if (options.headers.hasOwnProperty(key)) { - xhr.setRequestHeader(key, options.headers[key]); - } - } + var filtered = filter(change); + if (typeof filtered === 'object') { + return opts.complete(filtered); + } - if (options.timeout > 0) { - timer = setTimeout(timeoutReq, options.timeout); - xhr.onprogress = function () { - clearTimeout(timer); - if (xhr.readyState !== 4) { - timer = setTimeout(timeoutReq, options.timeout); - } - }; - if (typeof xhr.upload !== 'undefined') { // does not exist in ie9 - xhr.upload.onprogress = xhr.onprogress; + if (filtered) { + numResults++; + if (returnDocs) { + results.push(change); + } + // process the attachment immediately + // for the benefit of live listeners + if (opts.attachments && opts.include_docs) { + fetchAttachmentsIfNecessary$1(doc, opts, api, tx, + reportChange(change)); + } else { + reportChange(change)(); + } + } + if (numResults === limit) { + break; + } + } + }); + }, websqlError(opts.complete), function () { + if (!opts.continuous) { + opts.complete(null, { + results: results, + last_seq: lastSeq + }); + } + }); } - } - xhr.onreadystatechange = function () { - if (xhr.readyState !== 4) { - return; - } + fetchChanges(); + }; - var response = { - statusCode: xhr.status - }; + api._close = function (callback) { + //WebSQL databases do not need to be closed + callback(); + }; - if (xhr.status >= 200 && xhr.status < 300) { - var data; - if (options.binary) { - data = createBlob([xhr.response || ''], { - type: xhr.getResponseHeader('Content-Type') - }); + api._getAttachment = function (docId, attachId, 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$1 + ' 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 ? unescapeBlob(item.body) : + parseHexString(item.body, encoding); + if (opts.binary) { + res = binStringToBluffer(data, type); } else { - data = xhr.responseText; - } - callback(null, response, data); - } else { - var err = {}; - if (timedout) { - err = new Error('ETIMEDOUT'); - err.code = 'ETIMEDOUT'; - } else if (typeof xhr.response === 'string' && xhr.response !== '') { - try { - err = JSON.parse(xhr.response); - } catch (e) {} + res = thisBtoa(data); } - - err.status = xhr.status; - - callback(err); - } - cleanUp(); + callback(null, res); + }); }; - if (options.body && (options.body instanceof Blob)) { - readAsArrayBuffer(options.body, function (arrayBuffer) { - xhr.send(arrayBuffer); + api._getRevisionTree = function (docId, callback) { + db.readTransaction(function (tx) { + var sql = 'SELECT json AS metadata FROM ' + DOC_STORE$1 + ' WHERE id = ?'; + tx.executeSql(sql, [docId], function (tx, result) { + if (!result.rows.length) { + callback(createError(MISSING_DOC)); + } else { + var data = safeJsonParse(result.rows.item(0).metadata); + callback(null, data.rev_tree); + } + }); }); - } else { - xhr.send(options.body); - } - - return ret; - } - - function testXhr() { - try { - new XMLHttpRequest(); - return true; - } catch (err) { - return false; - } - } - - var hasXhr = testXhr(); - - function ajax(options, callback) { - if (!false && (hasXhr || options.xhr)) { - return xhRequest(options, callback); - } else { - return fetchRequest(options, callback); - } - } + }; - // the blob already has a type; do nothing + api._doCompaction = function (docId, revs, callback) { + if (!revs.length) { + return callback(); + } + db.transaction(function (tx) { - function defaultBody() { - return ''; - } + // update doc store + var sql = 'SELECT json AS metadata FROM ' + DOC_STORE$1 + ' WHERE id = ?'; + tx.executeSql(sql, [docId], function (tx, result) { + var metadata = safeJsonParse(result.rows.item(0).metadata); + traverseRevTree(metadata.rev_tree, function (isLeaf, pos, + revHash, ctx, opts) { + var rev = pos + '-' + revHash; + if (revs.indexOf(rev) !== -1) { + opts.status = 'missing'; + } + }); - function ajaxCore(options, callback) { + var sql = 'UPDATE ' + DOC_STORE$1 + ' SET json = ? WHERE id = ?'; + tx.executeSql(sql, [safeJsonStringify(metadata), docId]); + }); - options = clone(options); + compactRevs$1(revs, docId, tx); + }, websqlError(callback), function () { + callback(); + }); + }; - var defaultOptions = { - method : "GET", - headers: {}, - json: true, - processData: true, - timeout: 10000, - cache: false + api._getLocal = function (id, callback) { + db.readTransaction(function (tx) { + var sql = 'SELECT json, rev FROM ' + LOCAL_STORE$1 + ' 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(createError(MISSING_DOC)); + } + }); + }); }; - options = $inject_Object_assign(defaultOptions, options); + 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); - function onSuccess(obj, resp, cb) { - if (!options.binary && options.json && typeof obj === 'string') { - /* istanbul ignore next */ - try { - obj = JSON.parse(obj); - } catch (e) { - // Probably a malformed JSON from server - return cb(e); + var ret; + function putLocal(tx) { + var sql; + var values; + if (oldRev) { + sql = 'UPDATE ' + LOCAL_STORE$1 + ' SET rev=?, json=? ' + + 'WHERE id=? AND rev=?'; + values = [newRev, json, id, oldRev]; + } else { + sql = 'INSERT INTO ' + LOCAL_STORE$1 + ' (id, rev, json) VALUES (?,?,?)'; + values = [id, newRev, json]; } - } - if (Array.isArray(obj)) { - obj = obj.map(function (v) { - if (v.error || v.missing) { - return generateErrorFromResponse(v); + 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 { - return v; + callback(createError(REV_CONFLICT)); } + }, function () { + callback(createError(REV_CONFLICT)); + return false; // ack that we handled the error }); } - if (options.binary) { - - } - cb(null, obj, resp); - } - if (options.json) { - if (!options.binary) { - options.headers.Accept = 'application/json'; + if (opts.ctx) { + putLocal(opts.ctx); + } else { + db.transaction(putLocal, websqlError(callback), function () { + if (ret) { + callback(null, ret); + } + }); } - options.headers['Content-Type'] = options.headers['Content-Type'] || - 'application/json'; - } - - if (options.binary) { - options.encoding = null; - options.json = false; - } - - if (!options.processData) { - options.json = false; - } - - return ajax(options, function (err, response, body) { + }; - if (err) { - return callback(generateErrorFromResponse(err)); + api._removeLocal = function (doc, opts, callback) { + if (typeof opts === 'function') { + callback = opts; + opts = {}; } + var ret; - var error; - var content_type = response.headers && response.headers['content-type']; - var data = body || defaultBody(); - - // CouchDB doesn't always return the right content-type for JSON data, so - // we check for ^{ and }$ (ignoring leading/trailing whitespace) - if (!options.binary && (options.json || !options.processData) && - typeof data !== 'object' && - (/json/.test(content_type) || - (/^[\s]*\{/.test(data) && /\}[\s]*$/.test(data)))) { - try { - data = JSON.parse(data.toString()); - } catch (e) {} + function removeLocal(tx) { + var sql = 'DELETE FROM ' + LOCAL_STORE$1 + ' WHERE id=? AND rev=?'; + var params = [doc._id, doc._rev]; + tx.executeSql(sql, params, function (tx, res) { + if (!res.rowsAffected) { + return callback(createError(MISSING_DOC)); + } + ret = {ok: true, id: doc._id, rev: '0-0'}; + if (opts.ctx) { // return immediately + callback(null, ret); + } + }); } - if (response.statusCode >= 200 && response.statusCode < 300) { - onSuccess(data, response, callback); + if (opts.ctx) { + removeLocal(opts.ctx); } else { - error = generateErrorFromResponse(data); - error.status = response.statusCode; - callback(error); + db.transaction(removeLocal, websqlError(callback), function () { + if (ret) { + callback(null, ret); + } + }); } - }); - } - - function ajax$1(opts, callback) { - - // cache-buster, specifically designed to work around IE's aggressive caching - // see http://www.dashbay.com/2011/05/internet-explorer-caches-ajax/ - // Also Safari caches POSTs, so we need to cache-bust those too. - var ua = (navigator && navigator.userAgent) ? - navigator.userAgent.toLowerCase() : ''; - - var isSafari = ua.indexOf('safari') !== -1 && ua.indexOf('chrome') === -1; - var isIE = ua.indexOf('msie') !== -1; - var isTrident = ua.indexOf('trident') !== -1; - var isEdge = ua.indexOf('edge') !== -1; + }; - // it appears the new version of safari also caches GETs, - // see https://github.com/pouchdb/pouchdb/issues/5010 - var shouldCacheBust = (isSafari || - ((isIE || isTrident || isEdge) && opts.method === 'GET')); + api._destroy = function (opts, callback) { + websqlChanges.removeAllListeners(api._name); + db.transaction(function (tx) { + var stores = [DOC_STORE$1, BY_SEQ_STORE$1, ATTACH_STORE$1, META_STORE$1, + LOCAL_STORE$1, ATTACH_AND_SEQ_STORE$1]; + stores.forEach(function (store) { + tx.executeSql('DROP TABLE IF EXISTS ' + store, []); + }); + }, websqlError(callback), function () { + if (hasLocalStorage()) { + delete window.localStorage['_pouch__websqldb_' + api._name]; + delete window.localStorage[api._name]; + } + callback(null, {'ok': true}); + }); + }; + } - var cache = 'cache' in opts ? opts.cache : true; + function canOpenTestDB() { + try { + openDatabase('_pouch_validate_websql', 1, '', 1); + return true; + } catch (err) { + return false; + } + } - var isBlobUrl = /^blob:/.test(opts.url); // don't append nonces for blob URLs + // WKWebView had a bug where WebSQL would throw a DOM Exception 18 + // (see https://bugs.webkit.org/show_bug.cgi?id=137760 and + // https://github.com/pouchdb/pouchdb/issues/5079) + // This has been fixed in latest WebKit, so we try to detect it here. + function isValidWebSQL() { + // WKWebView UA: + // Mozilla/5.0 (iPhone; CPU iPhone OS 9_2 like Mac OS X) + // AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13C75 + // Chrome for iOS UA: + // Mozilla/5.0 (iPhone; U; CPU iPhone OS 5_1_1 like Mac OS X; en) + // AppleWebKit/534.46.0 (KHTML, like Gecko) CriOS/19.0.1084.60 + // Mobile/9B206 Safari/7534.48.3 + // Firefox for iOS UA: + // Mozilla/5.0 (iPhone; CPU iPhone OS 8_3 like Mac OS X) AppleWebKit/600.1.4 + // (KHTML, like Gecko) FxiOS/1.0 Mobile/12F69 Safari/600.1.4 - if (!isBlobUrl && (shouldCacheBust || !cache)) { - var hasArgs = opts.url.indexOf('?') !== -1; - opts.url += (hasArgs ? '&' : '?') + '_nonce=' + Date.now(); + // indexedDB is null on some UIWebViews and undefined in others + // see: https://bugs.webkit.org/show_bug.cgi?id=137034 + if (typeof indexedDB === 'undefined' || indexedDB === null || + !/iP(hone|od|ad)/.test(navigator.userAgent)) { + // definitely not WKWebView, avoid creating an unnecessary database + return true; } - - return ajaxCore(opts, callback); + // Cache the result in LocalStorage. Reason we do this is because if we + // call openDatabase() too many times, Safari craps out in SauceLabs and + // starts throwing DOM Exception 14s. + var hasLS = hasLocalStorage(); + // Include user agent in the hash, so that if Safari is upgraded, we don't + // continually think it's broken. + var localStorageKey = '_pouch__websqldb_valid_' + navigator.userAgent; + if (hasLS && localStorage[localStorageKey]) { + return localStorage[localStorageKey] === '1'; + } + var openedTestDB = canOpenTestDB(); + if (hasLS) { + localStorage[localStorageKey] = openedTestDB ? '1' : '0'; + } + return openedTestDB; } - // dead simple promise pool, inspired by https://github.com/timdp/es6-promise-pool - // but much smaller in code size. limits the number of concurrent promises that are executed + function valid() { + if (typeof openDatabase !== 'function') { + return false; + } + return isValidWebSQL(); + } + function openDB(name, version, description, size) { + // Traditional WebSQL API + return openDatabase(name, version, description, size); + } - function pool(promiseFactories, limit) { - return new PouchPromise(function (resolve, reject) { - var running = 0; - var current = 0; - var done = 0; - var len = promiseFactories.length; - var err; + function WebSQLPouch(opts, callback) { + var _opts = assign$1({ + websql: openDB + }, opts); - function runNext() { - running++; - promiseFactories[current++]().then(onSuccess, onError); - } + WebSqlPouch$1.call(this, _opts, callback); + } - function doNext() { - if (++done === len) { - /* istanbul ignore if */ - if (err) { - reject(err); - } else { - resolve(); - } - } else { - runNextBatch(); - } - } + WebSQLPouch.valid = valid; - function onSuccess() { - running--; - doNext(); - } + WebSQLPouch.use_prefix = true; - /* istanbul ignore next */ - function onError(thisErr) { - running--; - err = err || thisErr; - doNext(); - } + var WebSqlPouch = function (PouchDB) { + PouchDB.adapter('websql', WebSQLPouch, true); + }; - function runNextBatch() { - while (running < limit && current < len) { - runNext(); - } - } + /* global fetch */ + /* global Headers */ + function wrappedFetch() { + var wrappedPromise = {}; - runNextBatch(); + var promise = new PouchPromise$1(function (resolve, reject) { + wrappedPromise.resolve = resolve; + wrappedPromise.reject = reject; }); - } - - var CHANGES_BATCH_SIZE = 25; - var MAX_SIMULTANEOUS_REVS = 50; - var CHANGES_TIMEOUT_BUFFER = 5000; - var DEFAULT_HEARTBEAT = 10000; - var supportsBulkGetMap = {}; + var args = new Array(arguments.length); - function readAttachmentsAsBlobOrBuffer(row) { - var doc = row.doc || row.ok; - var atts = doc._attachments; - if (!atts) { - return; + for (var i = 0; i < args.length; i++) { + args[i] = arguments[i]; } - Object.keys(atts).forEach(function (filename) { - var att = atts[filename]; - att.data = b64ToBluffer(att.data, att.content_type); - }); - } - function encodeDocId(id) { - if (/^_design/.test(id)) { - return '_design/' + encodeURIComponent(id.slice(8)); - } - if (/^_local/.test(id)) { - return '_local/' + encodeURIComponent(id.slice(7)); - } - return encodeURIComponent(id); - } + wrappedPromise.promise = promise; - function preprocessAttachments$2(doc) { - if (!doc._attachments || !Object.keys(doc._attachments)) { - return PouchPromise.resolve(); - } + PouchPromise$1.resolve().then(function () { + return fetch.apply(null, args); + }).then(function (response) { + wrappedPromise.resolve(response); + }).catch(function (error) { + wrappedPromise.reject(error); + }); - return PouchPromise.all(Object.keys(doc._attachments).map(function (key) { - var attachment = doc._attachments[key]; - if (attachment.data && typeof attachment.data !== 'string') { - return new PouchPromise(function (resolve) { - blobToBase64(attachment.data, resolve); - }).then(function (b64) { - attachment.data = b64; - }); - } - })); + return wrappedPromise; } - function hasUrlPrefix(opts) { - if (!opts.prefix) { - return false; - } + function fetchRequest(options, callback) { + var wrappedPromise, timer, response; - var protocol = parseUri(opts.prefix).protocol; + var headers = new Headers(); - return protocol === 'http' || protocol === 'https'; - } + var fetchOptions = { + method: options.method, + credentials: 'include', + headers: headers + }; - // 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 (options.json) { + headers.set('Accept', 'application/json'); + headers.set('Content-Type', options.headers['Content-Type'] || + 'application/json'); + } - // encode db name if opts.prefix is a url (#5574) - if (hasUrlPrefix(opts)) { - var dbName = opts.name.substr(opts.prefix.length); - name = opts.prefix + encodeURIComponent(dbName); + if (options.body && + options.processData && + typeof options.body !== 'string') { + fetchOptions.body = JSON.stringify(options.body); + } else if ('body' in options) { + fetchOptions.body = options.body; + } else { + fetchOptions.body = null; } - // Prase the URI into all its little bits - var uri = parseUri(name); + Object.keys(options.headers).forEach(function (key) { + if (options.headers.hasOwnProperty(key)) { + headers.set(key, options.headers[key]); + } + }); + + wrappedPromise = wrappedFetch(options.url, fetchOptions); - // Store the user and password as a separate auth object - if (uri.user || uri.password) { - uri.auth = {username: uri.user, password: uri.password}; + if (options.timeout > 0) { + timer = setTimeout(function () { + wrappedPromise.reject(new Error('Load timeout for resource: ' + + options.url)); + }, options.timeout); } - // 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('/'); + wrappedPromise.promise.then(function (fetchResponse) { + response = { + statusCode: fetchResponse.status + }; - // Store the first part as the database name and remove it from the parts - // array - uri.db = parts.pop(); - // Prevent double encoding of URI component - if (uri.db.indexOf('%') === -1) { - uri.db = encodeURIComponent(uri.db); - } + if (options.timeout > 0) { + clearTimeout(timer); + } - // Restore the path by joining all the remaining parts (all the parts - // except for the database name) with '/'s - uri.path = parts.join('/'); + if (response.statusCode >= 200 && response.statusCode < 300) { + return options.binary ? fetchResponse.blob() : fetchResponse.text(); + } - return uri; - } + return fetchResponse.json(); + }).then(function (result) { + if (response.statusCode >= 200 && response.statusCode < 300) { + callback(null, response, result); + } else { + result.status = response.statusCode; + callback(result); + } + }).catch(function (error) { + if (!error) { + // this happens when the listener is canceled + error = new Error('canceled'); + } + callback(error); + }); - // Generate a URL with the host data given by opts and the given path - function genDBUrl(opts, path) { - return genUrl(opts, opts.db + '/' + path); + return {abort: wrappedPromise.reject}; } - // Generate a URL with the host data given by opts and the given path - function genUrl(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 - var pathDel = !opts.path ? '' : '/'; + function xhRequest(options, callback) { - // 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.port) : '') + - '/' + opts.path + pathDel + path; - } + var xhr, timer; + var timedout = false; - function paramsToStr(params) { - return '?' + Object.keys(params).map(function (k) { - return k + '=' + encodeURIComponent(params[k]); - }).join('&'); - } + var abortReq = function () { + xhr.abort(); + cleanUp(); + }; - // Implements the PouchDB API for dealing with CouchDB instances over HTTP - function HttpPouch(opts, callback) { + var timeoutReq = function () { + timedout = true; + xhr.abort(); + cleanUp(); + }; - // The functions that will be publicly available for HttpPouch - var api = this; + var ret = {abort: abortReq}; - var host = getHost(opts.name, opts); - var dbUrl = genDBUrl(host, ''); + var cleanUp = function () { + clearTimeout(timer); + ret.abort = function () {}; + if (xhr) { + xhr.onprogress = undefined; + if (xhr.upload) { + xhr.upload.onprogress = undefined; + } + xhr.onreadystatechange = undefined; + xhr = undefined; + } + }; - opts = clone(opts); - var ajaxOpts = opts.ajax || {}; + if (options.xhr) { + xhr = new options.xhr(); + } else { + xhr = new XMLHttpRequest(); + } - if (opts.auth || host.auth) { - var nAuth = opts.auth || host.auth; - var str = nAuth.username + ':' + nAuth.password; - var token = thisBtoa(unescape(encodeURIComponent(str))); - ajaxOpts.headers = ajaxOpts.headers || {}; - ajaxOpts.headers.Authorization = 'Basic ' + token; + try { + xhr.open(options.method, options.url); + } catch (exception) { + return callback(new Error(exception.name || 'Url is invalid')); } - // Not strictly necessary, but we do this because numerous tests - // rely on swapping ajax in and out. - api._ajax = ajax$1; - - function ajax(userOpts, options, callback) { - var reqAjax = (userOpts || {}).ajax || {}; - var reqOpts = $inject_Object_assign(clone(ajaxOpts), reqAjax, options); - var defaultHeaders = clone(ajaxOpts.headers || {}); - reqOpts.headers = $inject_Object_assign(defaultHeaders, reqAjax.headers, - options.headers || {}); - /* istanbul ignore if */ - if (api.constructor.listeners('debug').length) { - api.constructor.emit('debug', ['http', reqOpts.method, reqOpts.url]); + xhr.withCredentials = ('withCredentials' in options) ? + options.withCredentials : true; + + if (options.method === 'GET') { + delete options.headers['Content-Type']; + } else 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); } - return api._ajax(reqOpts, callback); } - function ajaxPromise(userOpts, opts) { - return new PouchPromise(function (resolve, reject) { - ajax(userOpts, opts, function (err, res) { - /* istanbul ignore if */ - if (err) { - return reject(err); - } - resolve(res); - }); - }); + if (options.binary) { + xhr.responseType = 'arraybuffer'; } - function adapterFun$$1(name, fun) { - return adapterFun(name, getArguments(function (args) { - setup().then(function () { - return fun.apply(this, args); - }).catch(function (e) { - var callback = args.pop(); - callback(e); - }); - })); + if (!('body' in options)) { + options.body = null; } - var setupPromise; + for (var key in options.headers) { + if (options.headers.hasOwnProperty(key)) { + xhr.setRequestHeader(key, options.headers[key]); + } + } - function setup() { - // TODO: Remove `skipSetup` in favor of `skip_setup` in a future release - if (opts.skipSetup || opts.skip_setup) { - return PouchPromise.resolve(); + if (options.timeout > 0) { + timer = setTimeout(timeoutReq, options.timeout); + xhr.onprogress = function () { + clearTimeout(timer); + if(xhr.readyState !== 4) { + timer = setTimeout(timeoutReq, options.timeout); + } + }; + if (typeof xhr.upload !== 'undefined') { // does not exist in ie9 + xhr.upload.onprogress = xhr.onprogress; } + } - // If there is a setup in process or previous successful setup - // done then we will use that - // If previous setups have been rejected we will try again - if (setupPromise) { - return setupPromise; + xhr.onreadystatechange = function () { + if (xhr.readyState !== 4) { + return; } - var checkExists = {method: 'GET', url: dbUrl}; - setupPromise = ajaxPromise({}, checkExists).catch(function (err) { - if (err && err.status && err.status === 404) { - // Doesnt exist, create it - explainError(404, 'PouchDB is just detecting if the remote exists.'); - return ajaxPromise({}, {method: 'PUT', url: dbUrl}); + var response = { + statusCode: xhr.status + }; + + if (xhr.status >= 200 && xhr.status < 300) { + var data; + if (options.binary) { + data = createBlob([xhr.response || ''], { + type: xhr.getResponseHeader('Content-Type') + }); } else { - return PouchPromise.reject(err); + data = xhr.responseText; } - }).catch(function (err) { - // If we try to create a database that already exists, skipped in - // istanbul since its catching a race condition. - /* istanbul ignore if */ - if (err && err.status && err.status === 412) { - return true; + callback(null, response, data); + } else { + var err = {}; + if (timedout) { + err = new Error('ETIMEDOUT'); + err.code = 'ETIMEDOUT'; + } else if (typeof xhr.response === 'string') { + try { + err = JSON.parse(xhr.response); + } catch(e) {} } - return PouchPromise.reject(err); - }); + err.status = xhr.status; + callback(err); + } + cleanUp(); + }; - setupPromise.catch(function () { - setupPromise = null; + if (options.body && (options.body instanceof Blob)) { + readAsArrayBuffer(options.body, function (arrayBuffer) { + xhr.send(arrayBuffer); }); - - return setupPromise; + } else { + xhr.send(options.body); } - nextTick(function () { - callback(null, api); - }); + return ret; + } - api._remote = true; - /* istanbul ignore next */ - api.type = function () { - return 'http'; - }; + function testXhr() { + try { + new XMLHttpRequest(); + return true; + } catch (err) { + return false; + } + } - api.id = adapterFun$$1('id', function (callback) { - ajax({}, {method: 'GET', url: genUrl(host, '')}, function (err, result) { - var uuid$$1 = (result && result.uuid) ? - (result.uuid + host.db) : genDBUrl(host, ''); - callback(null, uuid$$1); - }); - }); + var hasXhr = testXhr(); + + function ajax$1(options, callback) { + if (!false && (hasXhr || options.xhr)) { + return xhRequest(options, callback); + } else { + return fetchRequest(options, callback); + } + } - api.request = adapterFun$$1('request', function (options, callback) { - options.url = genDBUrl(host, options.url); - ajax({}, options, callback); - }); + // the blob already has a type; do nothing + var res$2 = function () {}; - // Sends a POST request to the host calling the couchdb _compact function - // version: The version of CouchDB it is running - api.compact = adapterFun$$1('compact', function (opts, callback) { - if (typeof opts === 'function') { - callback = opts; - opts = {}; - } - opts = clone(opts); - ajax(opts, { - url: genDBUrl(host, '_compact'), - method: 'POST' - }, function () { - function ping() { - api.info(function (err, res) { - // CouchDB may send a "compact_running:true" if it's - // already compacting. PouchDB Server doesn't. - /* istanbul ignore else */ - if (res && !res.compact_running) { - callback(null, {ok: true}); - } else { - setTimeout(ping, opts.interval || 200); - } - }); - } - // Ping the http if it's finished compaction - ping(); - }); - }); + function defaultBody() { + return ''; + } - api.bulkGet = adapterFun('bulkGet', function (opts, callback) { - var self = this; + function ajaxCore$1(options, callback) { - function doBulkGet(cb) { - var params = {}; - if (opts.revs) { - params.revs = true; - } - if (opts.attachments) { - /* istanbul ignore next */ - params.attachments = true; - } - if (opts.latest) { - params.latest = true; - } - ajax(opts, { - url: genDBUrl(host, '_bulk_get' + paramsToStr(params)), - method: 'POST', - body: { docs: opts.docs} - }, function (err, result) { - if (!err && opts.attachments && opts.binary) { - result.results.forEach(function (res) { - res.docs.forEach(readAttachmentsAsBlobOrBuffer); - }); - } - cb(err, result); - }); - } + options = clone(options); - /* istanbul ignore next */ - function doBulkGetShim() { - // avoid "url too long error" by splitting up into multiple requests - var batchSize = MAX_SIMULTANEOUS_REVS; - var numBatches = Math.ceil(opts.docs.length / batchSize); - var numDone = 0; - var results = new Array(numBatches); + var defaultOptions = { + method : "GET", + headers: {}, + json: true, + processData: true, + timeout: 10000, + cache: false + }; - function onResult(batchNum) { - return function (err, res) { - // err is impossible because shim returns a list of errs in that case - results[batchNum] = res.results; - if (++numDone === numBatches) { - callback(null, {results: flatten(results)}); - } - }; - } + options = assign$1(defaultOptions, options); - for (var i = 0; i < numBatches; i++) { - var subOpts = pick(opts, ['revs', 'attachments', 'binary', 'latest']); - subOpts.ajax = ajaxOpts; - subOpts.docs = opts.docs.slice(i * batchSize, - Math.min(opts.docs.length, (i + 1) * batchSize)); - bulkGet(self, subOpts, onResult(i)); + function onSuccess(obj, resp, cb) { + if (!options.binary && options.json && typeof obj === 'string') { + /* istanbul ignore next */ + try { + obj = JSON.parse(obj); + } catch (e) { + // Probably a malformed JSON from server + return cb(e); } } - - // mark the whole database as either supporting or not supporting _bulk_get - var dbUrl = genUrl(host, ''); - var supportsBulkGet = supportsBulkGetMap[dbUrl]; - - /* istanbul ignore next */ - if (typeof supportsBulkGet !== 'boolean') { - // check if this database supports _bulk_get - doBulkGet(function (err, res) { - if (err) { - supportsBulkGetMap[dbUrl] = false; - explainError( - err.status, - 'PouchDB is just detecting if the remote ' + - 'supports the _bulk_get API.' - ); - doBulkGetShim(); + if (Array.isArray(obj)) { + obj = obj.map(function (v) { + if (v.error || v.missing) { + return generateErrorFromResponse(v); } else { - supportsBulkGetMap[dbUrl] = true; - callback(null, res); + return v; } }); - } else if (supportsBulkGet) { - doBulkGet(callback); - } else { - doBulkGetShim(); } - }); - - // 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) { - setup().then(function () { - ajax({}, { - method: 'GET', - url: genDBUrl(host, '') - }, function (err, res) { - /* istanbul ignore next */ - if (err) { - return callback(err); - } - res.host = genDBUrl(host, ''); - callback(null, res); - }); - }).catch(callback); - }; + if (options.binary) { + res$2(obj, resp); + } + cb(null, obj, resp); + } - // 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 = adapterFun$$1('get', function (id, opts, callback) { - // If no options were given, set the callback to the second parameter - if (typeof opts === 'function') { - callback = opts; - opts = {}; + if (options.json) { + if (!options.binary) { + options.headers.Accept = 'application/json'; } - opts = clone(opts); + options.headers['Content-Type'] = options.headers['Content-Type'] || + 'application/json'; + } - // List of parameters to add to the GET request - var params = {}; + if (options.binary) { + options.encoding = null; + options.json = false; + } - if (opts.revs) { - params.revs = true; - } + if (!options.processData) { + options.json = false; + } - if (opts.revs_info) { - params.revs_info = true; - } + return ajax$1(options, function (err, response, body) { - if (opts.latest) { - params.latest = true; + if (err) { + return callback(generateErrorFromResponse(err)); } - if (opts.open_revs) { - if (opts.open_revs !== "all") { - opts.open_revs = JSON.stringify(opts.open_revs); - } - params.open_revs = opts.open_revs; - } + var error; + var content_type = response.headers && response.headers['content-type']; + var data = body || defaultBody(); - if (opts.rev) { - params.rev = opts.rev; + // CouchDB doesn't always return the right content-type for JSON data, so + // we check for ^{ and }$ (ignoring leading/trailing whitespace) + if (!options.binary && (options.json || !options.processData) && + typeof data !== 'object' && + (/json/.test(content_type) || + (/^[\s]*\{/.test(data) && /\}[\s]*$/.test(data)))) { + try { + data = JSON.parse(data.toString()); + } catch (e) {} } - if (opts.conflicts) { - params.conflicts = opts.conflicts; + if (response.statusCode >= 200 && response.statusCode < 300) { + onSuccess(data, response, callback); + } else { + error = generateErrorFromResponse(data); + error.status = response.statusCode; + callback(error); } + }); + } - /* istanbul ignore if */ - if (opts.update_seq) { - params.update_seq = opts.update_seq; - } + function ajax(opts, callback) { - id = encodeDocId(id); + // cache-buster, specifically designed to work around IE's aggressive caching + // see http://www.dashbay.com/2011/05/internet-explorer-caches-ajax/ + // Also Safari caches POSTs, so we need to cache-bust those too. + var ua = (navigator && navigator.userAgent) ? + navigator.userAgent.toLowerCase() : ''; - // Set the options for the ajax call - var options = { - method: 'GET', - url: genDBUrl(host, id + paramsToStr(params)) - }; + var isSafari = ua.indexOf('safari') !== -1 && ua.indexOf('chrome') === -1; + var isIE = ua.indexOf('msie') !== -1; + var isEdge = ua.indexOf('edge') !== -1; - function fetchAttachments(doc) { - var atts = doc._attachments; - var filenames = atts && Object.keys(atts); - if (!atts || !filenames.length) { - return; - } - // we fetch these manually in separate XHRs, because - // Sync Gateway would normally send it back as multipart/mixed, - // which we cannot parse. Also, this is more efficient than - // receiving attachments as base64-encoded strings. - function fetch(filename) { - var att = atts[filename]; - var path = encodeDocId(doc._id) + '/' + encodeAttachmentId(filename) + - '?rev=' + doc._rev; - return ajaxPromise(opts, { - method: 'GET', - url: genDBUrl(host, path), - binary: true - }).then(function (blob) { - if (opts.binary) { - return blob; - } - return new PouchPromise(function (resolve) { - blobToBase64(blob, resolve); - }); - }).then(function (data) { - delete att.stub; - delete att.length; - att.data = data; - }); - } + // it appears the new version of safari also caches GETs, + // see https://github.com/pouchdb/pouchdb/issues/5010 + var shouldCacheBust = (isSafari || + ((isIE || isEdge) && opts.method === 'GET')); + + var cache = 'cache' in opts ? opts.cache : true; - var promiseFactories = filenames.map(function (filename) { - return function () { - return fetch(filename); - }; - }); + var isBlobUrl = /^blob:/.test(opts.url); // don't append nonces for blob URLs - // This limits the number of parallel xhr requests to 5 any time - // to avoid issues with maximum browser request limits - return pool(promiseFactories, 5); - } + if (!isBlobUrl && (shouldCacheBust || !cache)) { + var hasArgs = opts.url.indexOf('?') !== -1; + opts.url += (hasArgs ? '&' : '?') + '_nonce=' + Date.now(); + } - function fetchAllAttachments(docOrDocs) { - if (Array.isArray(docOrDocs)) { - return PouchPromise.all(docOrDocs.map(function (doc) { - if (doc.ok) { - return fetchAttachments(doc.ok); - } - })); - } - return fetchAttachments(docOrDocs); + return ajaxCore$1(opts, callback); + } + + // dead simple promise pool, inspired by https://github.com/timdp/es6-promise-pool + // but much smaller in code size. limits the number of concurrent promises that are executed + + function pool(promiseFactories, limit) { + return new PouchPromise$1(function (resolve, reject) { + var running = 0; + var current = 0; + var done = 0; + var len = promiseFactories.length; + var err; + + function runNext() { + running++; + promiseFactories[current++]().then(onSuccess, onError); } - ajaxPromise(opts, options).then(function (res) { - return PouchPromise.resolve().then(function () { - if (opts.attachments) { - return fetchAllAttachments(res); + function doNext() { + if (++done === len) { + /* istanbul ignore if */ + if (err) { + reject(err); + } else { + resolve(); } - }).then(function () { - callback(null, res); - }); - }).catch(function (e) { - e.docId = id; - callback(e); - }); - }); - - // Delete the document given by doc from the database given by host. - api.remove = adapterFun$$1('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; + runNextBatch(); } } - var rev$$1 = (doc._rev || opts.rev); - - // Delete the document - ajax(opts, { - method: 'DELETE', - url: genDBUrl(host, encodeDocId(doc._id)) + '?rev=' + rev$$1 - }, callback); - }); + function onSuccess() { + running--; + doNext(); + } - function encodeAttachmentId(attachmentId) { - return attachmentId.split("/").map(encodeURIComponent).join("/"); - } + /* istanbul ignore next */ + function onError(thisErr) { + running--; + err = err || thisErr; + doNext(); + } - // Get the attachment - api.getAttachment = - adapterFun$$1('getAttachment', function (docId, attachmentId, opts, - callback) { - if (typeof opts === 'function') { - callback = opts; - opts = {}; + function runNextBatch() { + while (running < limit && current < len) { + runNext(); + } } - var params = opts.rev ? ('?rev=' + opts.rev) : ''; - var url = genDBUrl(host, encodeDocId(docId)) + '/' + - encodeAttachmentId(attachmentId) + params; - ajax(opts, { - method: 'GET', - url: url, - binary: true - }, callback); + + runNextBatch(); }); + } - // Remove the attachment given by the id and rev - api.removeAttachment = - adapterFun$$1('removeAttachment', function (docId, attachmentId, rev$$1, - callback) { + var CHANGES_BATCH_SIZE = 25; + var MAX_SIMULTANEOUS_REVS = 50; - var url = genDBUrl(host, encodeDocId(docId) + '/' + - encodeAttachmentId(attachmentId)) + '?rev=' + rev$$1; + var supportsBulkGetMap = {}; - ajax({}, { - method: 'DELETE', - url: url - }, callback); + var log$1 = debug('pouchdb:http'); + + function readAttachmentsAsBlobOrBuffer(row) { + var atts = row.doc && row.doc._attachments; + if (!atts) { + return; + } + Object.keys(atts).forEach(function (filename) { + var att = atts[filename]; + att.data = b64ToBluffer(att.data, att.content_type); }); + } - // 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 = - adapterFun$$1('putAttachment', function (docId, attachmentId, rev$$1, blob, - type, callback) { - if (typeof type === 'function') { - callback = type; - type = blob; - blob = rev$$1; - rev$$1 = null; - } - var id = encodeDocId(docId) + '/' + encodeAttachmentId(attachmentId); - var url = genDBUrl(host, id); - if (rev$$1) { - url += '?rev=' + rev$$1; - } + function encodeDocId(id) { + if (/^_design/.test(id)) { + return '_design/' + encodeURIComponent(id.slice(8)); + } + if (/^_local/.test(id)) { + return '_local/' + encodeURIComponent(id.slice(7)); + } + return encodeURIComponent(id); + } - if (typeof blob === 'string') { - // input is assumed to be a base64 string - var binary; - try { - binary = thisAtob(blob); - } catch (err) { - return callback(createError(BAD_ARG, - 'Attachment is not a valid base64 string')); - } - blob = binary ? binStringToBluffer(binary, type) : ''; + function preprocessAttachments$2(doc) { + if (!doc._attachments || !Object.keys(doc._attachments)) { + return PouchPromise$1.resolve(); + } + + return PouchPromise$1.all(Object.keys(doc._attachments).map(function (key) { + var attachment = doc._attachments[key]; + if (attachment.data && typeof attachment.data !== 'string') { + return new PouchPromise$1(function (resolve) { + blobToBase64(attachment.data, resolve); + }).then(function (b64) { + attachment.data = b64; + }); } + })); + } - var opts = { - headers: {'Content-Type': type}, - method: 'PUT', - url: url, - processData: false, - body: blob, - timeout: ajaxOpts.timeout || 60000 - }; - // Add the attachment - ajax({}, opts, callback); - }); + function hasUrlPrefix(opts) { + if (!opts.prefix) { + return false; + } - // Update/create multiple documents given by req in the database - // given by host. - api._bulkDocs = function (req, opts, callback) { - // 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. - req.new_edits = opts.new_edits; + var protocol = parseUri(opts.prefix).protocol; - setup().then(function () { - return PouchPromise.all(req.docs.map(preprocessAttachments$2)); - }).then(function () { - // Update/create the documents - ajax(opts, { - method: 'POST', - url: genDBUrl(host, '_bulk_docs'), - timeout: opts.timeout, - 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); - }; + return protocol === 'http' || protocol === 'https'; + } + // Get all the information you possibly can about the URI given by name and + // return it as a suitable object. + function getHost(name, opts) { - // Update/create document - api._put = function (doc, opts, callback) { - setup().then(function () { - return preprocessAttachments$2(doc); - }).then(function () { - // Update/create the document - ajax(opts, { - method: 'PUT', - url: genDBUrl(host, encodeDocId(doc._id)), - body: doc - }, function (err, result) { - if (err) { - err.docId = doc && doc._id; - return callback(err); - } - callback(null, result); - }); - }).catch(callback); - }; + // encode db name if opts.prefix is a url (#5574) + if (hasUrlPrefix(opts)) { + var dbName = opts.name.substr(opts.prefix.length); + name = opts.prefix + encodeURIComponent(dbName); + } + + // Prase the URI into all its little bits + var uri = parseUri(name); + + // 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(); + // Prevent double encoding of URI component + if (uri.db.indexOf('%') === -1) { + uri.db = encodeURIComponent(uri.db); + } + // Restore the path by joining all the remaining parts (all the parts + // except for the database name) with '/'s + uri.path = parts.join('/'); - // Get a listing of the documents in the database given - // by host and ordered by increasing id. - api.allDocs = adapterFun$$1('allDocs', function (opts, callback) { - if (typeof opts === 'function') { - callback = opts; - opts = {}; - } - opts = clone(opts); + return uri; + } - // List of parameters to add to the GET request - var params = {}; - var body; - var method = 'GET'; + // Generate a URL with the host data given by opts and the given path + function genDBUrl(opts, path) { + return genUrl(opts, opts.db + '/' + path); + } - if (opts.conflicts) { - params.conflicts = true; - } + // Generate a URL with the host data given by opts and the given path + function genUrl(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 + var pathDel = !opts.path ? '' : '/'; - /* istanbul ignore if */ - if (opts.update_seq) { - params.update_seq = true; - } + // 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.port) : '') + + '/' + opts.path + pathDel + path; + } - if (opts.descending) { - params.descending = true; - } + function paramsToStr(params) { + return '?' + Object.keys(params).map(function (k) { + return k + '=' + encodeURIComponent(params[k]); + }).join('&'); + } - if (opts.include_docs) { - params.include_docs = true; - } + // Implements the PouchDB API for dealing with CouchDB instances over HTTP + function HttpPouch(opts, callback) { - // added in CouchDB 1.6.0 - if (opts.attachments) { - params.attachments = true; - } + // The functions that will be publicly available for HttpPouch + var api = this; - if (opts.key) { - params.key = JSON.stringify(opts.key); - } + var host = getHost(opts.name, opts); + var dbUrl = genDBUrl(host, ''); - if (opts.start_key) { - opts.startkey = opts.start_key; - } + opts = clone(opts); + var ajaxOpts = opts.ajax || {}; - if (opts.startkey) { - params.startkey = JSON.stringify(opts.startkey); - } + if (opts.auth || host.auth) { + var nAuth = opts.auth || host.auth; + var str = nAuth.username + ':' + nAuth.password; + var token = thisBtoa(unescape(encodeURIComponent(str))); + ajaxOpts.headers = ajaxOpts.headers || {}; + ajaxOpts.headers.Authorization = 'Basic ' + token; + } - if (opts.end_key) { - opts.endkey = opts.end_key; - } + // Not strictly necessary, but we do this because numerous tests + // rely on swapping ajax in and out. + api._ajax = ajax; - if (opts.endkey) { - params.endkey = JSON.stringify(opts.endkey); - } + function ajax$$1(userOpts, options, callback) { + var reqAjax = userOpts.ajax || {}; + var reqOpts = assign$1(clone(ajaxOpts), reqAjax, options); + log$1(reqOpts.method + ' ' + reqOpts.url); + return api._ajax(reqOpts, callback); + } - if (typeof opts.inclusive_end !== 'undefined') { - params.inclusive_end = !!opts.inclusive_end; - } + function ajaxPromise(userOpts, opts) { + return new PouchPromise$1(function (resolve, reject) { + ajax$$1(userOpts, opts, function (err, res) { + /* istanbul ignore if */ + if (err) { + return reject(err); + } + resolve(res); + }); + }); + } - if (typeof opts.limit !== 'undefined') { - params.limit = opts.limit; - } + function adapterFun$$1(name, fun) { + return adapterFun(name, getArguments(function (args) { + setup().then(function () { + return fun.apply(this, args); + }).catch(function (e) { + var callback = args.pop(); + callback(e); + }); + })); + } - if (typeof opts.skip !== 'undefined') { - params.skip = opts.skip; - } + var setupPromise; - var paramStr = paramsToStr(params); + function setup() { + // TODO: Remove `skipSetup` in favor of `skip_setup` in a future release + if (opts.skipSetup || opts.skip_setup) { + return PouchPromise$1.resolve(); + } - if (typeof opts.keys !== 'undefined') { - method = 'POST'; - body = {keys: opts.keys}; + // If there is a setup in process or previous successful setup + // done then we will use that + // If previous setups have been rejected we will try again + if (setupPromise) { + return setupPromise; } - // Get the document listing - ajaxPromise(opts, { - method: method, - url: genDBUrl(host, '_all_docs' + paramStr), - body: body - }).then(function (res) { - if (opts.include_docs && opts.attachments && opts.binary) { - res.rows.forEach(readAttachmentsAsBlobOrBuffer); + var checkExists = {method: 'GET', url: dbUrl}; + setupPromise = ajaxPromise({}, checkExists).catch(function (err) { + if (err && err.status && err.status === 404) { + // Doesnt exist, create it + explainError(404, 'PouchDB is just detecting if the remote exists.'); + return ajaxPromise({}, {method: 'PUT', url: dbUrl}); + } else { + return PouchPromise$1.reject(err); } - callback(null, res); - }).catch(callback); - }); + }).catch(function (err) { + // If we try to create a database that already exists, skipped in + // istanbul since its catching a race condition. + /* istanbul ignore if */ + if (err && err.status && err.status === 412) { + return true; + } + return PouchPromise$1.reject(err); + }); - // 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) { + setupPromise.catch(function () { + setupPromise = null; + }); - // 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; + return setupPromise; + } - opts = clone(opts); + nextTick(function () { + callback(null, api); + }); - if (opts.continuous && !('heartbeat' in opts)) { - opts.heartbeat = DEFAULT_HEARTBEAT; - } + api.type = function () { + return 'http'; + }; - var requestTimeout = ('timeout' in opts) ? opts.timeout : - ('timeout' in ajaxOpts) ? ajaxOpts.timeout : - 30 * 1000; + api.id = adapterFun$$1('id', function (callback) { + ajax$$1({}, {method: 'GET', url: genUrl(host, '')}, function (err, result) { + var uuid$$1 = (result && result.uuid) ? + (result.uuid + host.db) : genDBUrl(host, ''); + callback(null, uuid$$1); + }); + }); - // ensure CHANGES_TIMEOUT_BUFFER applies - if ('timeout' in opts && opts.timeout && - (requestTimeout - opts.timeout) < CHANGES_TIMEOUT_BUFFER) { - requestTimeout = opts.timeout + CHANGES_TIMEOUT_BUFFER; - } + api.request = adapterFun$$1('request', function (options, callback) { + options.url = genDBUrl(host, options.url); + ajax$$1({}, options, callback); + }); - if ('heartbeat' in opts && opts.heartbeat && - (requestTimeout - opts.heartbeat) < CHANGES_TIMEOUT_BUFFER) { - requestTimeout = opts.heartbeat + CHANGES_TIMEOUT_BUFFER; + // Sends a POST request to the host calling the couchdb _compact function + // version: The version of CouchDB it is running + api.compact = adapterFun$$1('compact', function (opts, callback) { + if (typeof opts === 'function') { + callback = opts; + opts = {}; } + opts = clone(opts); + ajax$$1(opts, { + url: genDBUrl(host, '_compact'), + method: 'POST' + }, function () { + function ping() { + api.info(function (err, res) { + if (res && !res.compact_running) { + callback(null, {ok: true}); + } else { + setTimeout(ping, opts.interval || 200); + } + }); + } + // Ping the http if it's finished compaction + ping(); + }); + }); - var params = {}; - if ('timeout' in opts && opts.timeout) { - params.timeout = opts.timeout; - } + api.bulkGet = adapterFun('bulkGet', function (opts, callback) { + var self = this; - var limit = (typeof opts.limit !== 'undefined') ? opts.limit : false; - var returnDocs; - if ('return_docs' in opts) { - returnDocs = opts.return_docs; - } else if ('returnDocs' in opts) { - // TODO: Remove 'returnDocs' in favor of 'return_docs' in a future release - returnDocs = opts.returnDocs; - } else { - returnDocs = true; + function doBulkGet(cb) { + var params = {}; + if (opts.revs) { + params.revs = true; + } + if (opts.attachments) { + /* istanbul ignore next */ + params.attachments = true; + } + if (opts.latest) { + params.latest = true; + } + ajax$$1(opts, { + url: genDBUrl(host, '_bulk_get' + paramsToStr(params)), + method: 'POST', + body: { docs: opts.docs} + }, cb); } - // - var leftToFetch = limit; - if (opts.style) { - params.style = opts.style; - } + function doBulkGetShim() { + // avoid "url too long error" by splitting up into multiple requests + var batchSize = MAX_SIMULTANEOUS_REVS; + var numBatches = Math.ceil(opts.docs.length / batchSize); + var numDone = 0; + var results = new Array(numBatches); + + function onResult(batchNum) { + return function (err, res) { + // err is impossible because shim returns a list of errs in that case + results[batchNum] = res.results; + if (++numDone === numBatches) { + callback(null, {results: flatten(results)}); + } + }; + } - if (opts.include_docs || opts.filter && typeof opts.filter === 'function') { - params.include_docs = true; + for (var i = 0; i < numBatches; i++) { + var subOpts = pick(opts, ['revs', 'attachments', 'latest']); + subOpts.ajax = ajaxOpts; + subOpts.docs = opts.docs.slice(i * batchSize, + Math.min(opts.docs.length, (i + 1) * batchSize)); + bulkGet(self, subOpts, onResult(i)); + } } - if (opts.attachments) { - params.attachments = true; - } + // mark the whole database as either supporting or not supporting _bulk_get + var dbUrl = genUrl(host, ''); + var supportsBulkGet = supportsBulkGetMap[dbUrl]; - if (opts.continuous) { - params.feed = 'longpoll'; + if (typeof supportsBulkGet !== 'boolean') { + // check if this database supports _bulk_get + doBulkGet(function (err, res) { + /* istanbul ignore else */ + if (err) { + supportsBulkGetMap[dbUrl] = false; + explainError( + err.status, + 'PouchDB is just detecting if the remote ' + + 'supports the _bulk_get API.' + ); + doBulkGetShim(); + } else { + supportsBulkGetMap[dbUrl] = true; + callback(null, res); + } + }); + } else if (supportsBulkGet) { + /* istanbul ignore next */ + doBulkGet(callback); + } else { + doBulkGetShim(); } + }); - if (opts.seq_interval) { - params.seq_interval = opts.seq_interval; - } + // 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) { + setup().then(function () { + ajax$$1({}, { + method: 'GET', + url: genDBUrl(host, '') + }, function (err, res) { + /* istanbul ignore next */ + if (err) { + return callback(err); + } + res.host = genDBUrl(host, ''); + callback(null, res); + }); + }).catch(callback); + }; - if (opts.conflicts) { - params.conflicts = true; + // 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 = adapterFun$$1('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 = clone(opts); - if (opts.descending) { - params.descending = true; - } - - /* istanbul ignore if */ - if (opts.update_seq) { - params.update_seq = true; - } + // List of parameters to add to the GET request + var params = {}; - if ('heartbeat' in opts) { - // If the heartbeat value is false, it disables the default heartbeat - if (opts.heartbeat) { - params.heartbeat = opts.heartbeat; - } + if (opts.revs) { + params.revs = true; } - if (opts.filter && typeof opts.filter === 'string') { - params.filter = opts.filter; + if (opts.revs_info) { + params.revs_info = true; } - if (opts.view && typeof opts.view === 'string') { - params.filter = '_view'; - params.view = opts.view; + if (opts.latest) { + params.latest = true; } - // 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) { - /* istanbul ignore else */ - if (opts.query_params.hasOwnProperty(param_name)) { - params[param_name] = opts.query_params[param_name]; - } + if (opts.open_revs) { + if (opts.open_revs !== "all") { + opts.open_revs = JSON.stringify(opts.open_revs); } + params.open_revs = opts.open_revs; } - 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'; - method = 'POST'; - body = {doc_ids: opts.doc_ids }; + if (opts.rev) { + params.rev = opts.rev; } - /* istanbul ignore next */ - else if (opts.selector) { - // set this automagically for the user, similar to above - params.filter = '_selector'; - method = 'POST'; - body = {selector: opts.selector }; + + if (opts.conflicts) { + params.conflicts = opts.conflicts; } - var xhr; - var lastFetchedSeq; + id = encodeDocId(id); - // Get all the changes starting wtih the one immediately after the - // sequence number given by since. - var fetch = function (since, callback) { - if (opts.aborted) { + // Set the options for the ajax call + var options = { + method: 'GET', + url: genDBUrl(host, id + paramsToStr(params)) + }; + + function fetchAttachments(doc) { + var atts = doc._attachments; + var filenames = atts && Object.keys(atts); + if (!atts || !filenames.length) { return; } - params.since = since; - // "since" can be any kind of json object in Coudant/CouchDB 2.x - /* istanbul ignore next */ - if (typeof params.since === "object") { - params.since = JSON.stringify(params.since); + // we fetch these manually in separate XHRs, because + // Sync Gateway would normally send it back as multipart/mixed, + // which we cannot parse. Also, this is more efficient than + // receiving attachments as base64-encoded strings. + function fetch(filename) { + var att = atts[filename]; + var path = encodeDocId(doc._id) + '/' + encodeAttachmentId(filename) + + '?rev=' + doc._rev; + return ajaxPromise(opts, { + method: 'GET', + url: genDBUrl(host, path), + binary: true + }).then(function (blob$$1) { + if (opts.binary) { + return blob$$1; + } + return new PouchPromise$1(function (resolve) { + blobToBase64(blob$$1, resolve); + }); + }).then(function (data) { + delete att.stub; + delete att.length; + att.data = data; + }); } - if (opts.descending) { - if (limit) { - params.limit = leftToFetch; - } - } else { - params.limit = (!limit || leftToFetch > batchSize) ? - batchSize : leftToFetch; - } + var promiseFactories = filenames.map(function (filename) { + return function () { + return fetch(filename); + }; + }); - // Set the options for the ajax call - var xhrOpts = { - method: method, - url: genDBUrl(host, '_changes' + paramsToStr(params)), - timeout: requestTimeout, - body: body - }; - lastFetchedSeq = since; + // This limits the number of parallel xhr requests to 5 any time + // to avoid issues with maximum browser request limits + return pool(promiseFactories, 5); + } - /* istanbul ignore if */ - if (opts.aborted) { - return; + function fetchAllAttachments(docOrDocs) { + if (Array.isArray(docOrDocs)) { + return PouchPromise$1.all(docOrDocs.map(function (doc) { + if (doc.ok) { + return fetchAttachments(doc.ok); + } + })); } + return fetchAttachments(docOrDocs); + } - // Get the changes - setup().then(function () { - xhr = ajax(opts, xhrOpts, callback); - }).catch(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 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; - var pending = null; - var lastSeq = null; - // Attach 'pending' property if server supports it (CouchDB 2.0+) - /* istanbul ignore if */ - if (typeof res.pending === 'number') { - pending = res.pending; - } - if (typeof results.last_seq === 'string' || typeof results.last_seq === 'number') { - lastSeq = results.last_seq; + ajaxPromise(opts, options).then(function (res) { + return PouchPromise$1.resolve().then(function () { + if (opts.attachments) { + return fetchAllAttachments(res); } - // For each change - var req = {}; - req.query = opts.query_params; - res.results = res.results.filter(function (c) { - leftToFetch--; - var ret = filterChange(opts)(c); - if (ret) { - if (opts.include_docs && opts.attachments && opts.binary) { - readAttachmentsAsBlobOrBuffer(c); - } - if (returnDocs) { - results.results.push(c); - } - opts.onChange(c, pending, lastSeq); - } - return ret; - }); - } else if (err) { - // In case of an error, stop listening for changes and call - // opts.complete - opts.aborted = true; - opts.complete(err); - return; - } + }).then(function () { + callback(null, res); + }); + }).catch(callback); + }); - // 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; + // Delete the document given by doc from the database given by host. + api.remove = adapterFun$$1('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 = {}; } - - var finished = (limit && leftToFetch <= 0) || - (res && raw_results_length < batchSize) || - (opts.descending); - - if ((opts.continuous && !(limit && leftToFetch <= 0)) || !finished) { - // Queue a call to fetch again with the newest sequence number - nextTick(function () { fetch(lastFetchedSeq, fetched); }); + } else { + // doc, opts, callback style + doc = docOrId; + if (typeof optsOrRev === 'function') { + callback = optsOrRev; + opts = {}; } else { - // We're done, call the callback - opts.complete(null, results); + callback = opts; + opts = optsOrRev; } - }; + } - fetch(opts.since || 0, fetched); + var rev = (doc._rev || opts.rev); - // Return a method to cancel this method from processing any more - return { - cancel: function () { - opts.aborted = true; - if (xhr) { - xhr.abort(); - } - } - }; - }; + // Delete the document + ajax$$1(opts, { + method: 'DELETE', + url: genDBUrl(host, encodeDocId(doc._id)) + '?rev=' + rev + }, callback); + }); - // 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 = adapterFun$$1('revsDiff', function (req, opts, callback) { - // If no options were given, set the callback to be the second parameter + function encodeAttachmentId(attachmentId) { + return attachmentId.split("/").map(encodeURIComponent).join("/"); + } + + // Get the attachment + api.getAttachment = + adapterFun$$1('getAttachment', function (docId, attachmentId, opts, + callback) { if (typeof opts === 'function') { callback = opts; opts = {}; } - - // Get the missing document/revision IDs - ajax(opts, { - method: 'POST', - url: genDBUrl(host, '_revs_diff'), - body: req + var params = opts.rev ? ('?rev=' + opts.rev) : ''; + var url = genDBUrl(host, encodeDocId(docId)) + '/' + + encodeAttachmentId(attachmentId) + params; + ajax$$1(opts, { + method: 'GET', + url: url, + binary: true }, callback); }); - api._close = function (callback) { - callback(); - }; - - api._destroy = function (options, callback) { - ajax(options, { - url: genDBUrl(host, ''), - method: 'DELETE' - }, function (err, resp) { - if (err && err.status && err.status !== 404) { - return callback(err); - } - callback(null, resp); - }); - }; - } + // Remove the attachment given by the id and rev + api.removeAttachment = + adapterFun$$1('removeAttachment', function (docId, attachmentId, rev, + callback) { - // HttpPouch is a valid adapter. - HttpPouch.valid = function () { - return true; - }; + var url = genDBUrl(host, encodeDocId(docId) + '/' + + encodeAttachmentId(attachmentId)) + '?rev=' + rev; - function HttpPouch$1 (PouchDB) { - PouchDB.adapter('http', HttpPouch, false); - PouchDB.adapter('https', HttpPouch, false); - } + ajax$$1({}, { + method: 'DELETE', + url: url + }, callback); + }); - function QueryParseError(message) { - this.status = 400; - this.name = 'query_parse_error'; - this.message = message; - this.error = true; - try { - Error.captureStackTrace(this, QueryParseError); - } catch (e) {} - } + // 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 = + adapterFun$$1('putAttachment', function (docId, attachmentId, rev, blob$$1, + type, callback) { + if (typeof type === 'function') { + callback = type; + type = blob$$1; + blob$$1 = rev; + rev = null; + } + var id = encodeDocId(docId) + '/' + encodeAttachmentId(attachmentId); + var url = genDBUrl(host, id); + if (rev) { + url += '?rev=' + rev; + } - inherits(QueryParseError, Error); + if (typeof blob$$1 === 'string') { + // input is assumed to be a base64 string + var binary; + try { + binary = thisAtob(blob$$1); + } catch (err) { + return callback(createError(BAD_ARG, + 'Attachment is not a valid base64 string')); + } + blob$$1 = binary ? binStringToBluffer(binary, type) : ''; + } - function NotFoundError(message) { - this.status = 404; - this.name = 'not_found'; - this.message = message; - this.error = true; - try { - Error.captureStackTrace(this, NotFoundError); - } catch (e) {} - } + var opts = { + headers: {'Content-Type': type}, + method: 'PUT', + url: url, + processData: false, + body: blob$$1, + timeout: ajaxOpts.timeout || 60000 + }; + // Add the attachment + ajax$$1({}, opts, callback); + }); - inherits(NotFoundError, Error); + // Update/create multiple documents given by req in the database + // given by host. + api._bulkDocs = function (req, opts, callback) { + // 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. + req.new_edits = opts.new_edits; - function BuiltInError(message) { - this.status = 500; - this.name = 'invalid_value'; - this.message = message; - this.error = true; - try { - Error.captureStackTrace(this, BuiltInError); - } catch (e) {} - } + setup().then(function () { + return PouchPromise$1.all(req.docs.map(preprocessAttachments$2)); + }).then(function () { + // Update/create the documents + ajax$$1(opts, { + method: 'POST', + url: genDBUrl(host, '_bulk_docs'), + timeout: opts.timeout, + 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); + }; - inherits(BuiltInError, Error); - function promisedCallback(promise, callback) { - if (callback) { - promise.then(function (res) { - nextTick(function () { - callback(null, res); - }); - }, function (reason) { - nextTick(function () { - callback(reason); + // Update/create document + api._put = function (doc, opts, callback) { + setup().then(function () { + return preprocessAttachments$2(doc); + }).then(function () { + // Update/create the document + ajax$$1(opts, { + method: 'PUT', + url: genDBUrl(host, encodeDocId(doc._id)), + body: doc + }, function (err, result) { + if (err) { + return callback(err); + } + callback(null, result); }); - }); - } - return promise; - } - - function callbackify(fun) { - return getArguments(function (args) { - var cb = args.pop(); - var promise = fun.apply(this, args); - if (typeof cb === 'function') { - promisedCallback(promise, cb); - } - return promise; - }); - } + }).catch(callback); + }; - // Promise finally util similar to Q.finally - function fin(promise, finalPromiseFactory) { - return promise.then(function (res) { - return finalPromiseFactory().then(function () { - return res; - }); - }, function (reason) { - return finalPromiseFactory().then(function () { - throw reason; - }); - }); - } - function sequentialize(queue, promiseFactory) { - return function () { - var args = arguments; - var that = this; - return queue.add(function () { - return promiseFactory.apply(that, args); - }); - }; - } + // Get a listing of the documents in the database given + // by host and ordered by increasing id. + api.allDocs = adapterFun$$1('allDocs', function (opts, callback) { + if (typeof opts === 'function') { + callback = opts; + opts = {}; + } + opts = clone(opts); - // uniq an array of strings, order not guaranteed - // similar to underscore/lodash _.uniq - function uniq(arr) { - var theSet = new ExportedSet(arr); - var result = new Array(theSet.size); - var index = -1; - theSet.forEach(function (value) { - result[++index] = value; - }); - return result; - } + // List of parameters to add to the GET request + var params = {}; + var body; + var method = 'GET'; - function mapToKeysArray(map) { - var result = new Array(map.size); - var index = -1; - map.forEach(function (value, key) { - result[++index] = key; - }); - return result; - } + if (opts.conflicts) { + params.conflicts = true; + } - function createBuiltInError(name) { - var message = 'builtin ' + name + - ' function requires map values to be numbers' + - ' or number arrays'; - return new BuiltInError(message); - } + if (opts.descending) { + params.descending = true; + } - function sum(values) { - var result = 0; - for (var i = 0, len = values.length; i < len; i++) { - var num = values[i]; - if (typeof num !== 'number') { - if (Array.isArray(num)) { - // lists of numbers are also allowed, sum them separately - result = typeof result === 'number' ? [result] : result; - for (var j = 0, jLen = num.length; j < jLen; j++) { - var jNum = num[j]; - if (typeof jNum !== 'number') { - throw createBuiltInError('_sum'); - } else if (typeof result[j] === 'undefined') { - result.push(jNum); - } else { - result[j] += jNum; - } - } - } else { // not array/number - throw createBuiltInError('_sum'); - } - } else if (typeof result === 'number') { - result += num; - } else { // add number to array - result[0] += num; + if (opts.include_docs) { + params.include_docs = true; } - } - return result; - } - var log = guardedConsole.bind(null, 'log'); - var isArray = Array.isArray; - var toJSON = JSON.parse; + // added in CouchDB 1.6.0 + if (opts.attachments) { + params.attachments = true; + } - function evalFunctionWithEval(func, emit) { - return scopeEval( - "return (" + func.replace(/;\s*$/, "") + ");", - { - emit: emit, - sum: sum, - log: log, - isArray: isArray, - toJSON: toJSON + if (opts.key) { + params.key = JSON.stringify(opts.key); } - ); - } - /* - * Simple task queue to sequentialize actions. Assumes - * callbacks will eventually fire (once). - */ + if (opts.start_key) { + opts.startkey = opts.start_key; + } + if (opts.startkey) { + params.startkey = JSON.stringify(opts.startkey); + } - function TaskQueue$2() { - this.promise = new PouchPromise(function (fulfill) {fulfill(); }); - } - TaskQueue$2.prototype.add = function (promiseFactory) { - this.promise = this.promise.catch(function () { - // just recover - }).then(function () { - return promiseFactory(); - }); - return this.promise; - }; - TaskQueue$2.prototype.finish = function () { - return this.promise; - }; + if (opts.end_key) { + opts.endkey = opts.end_key; + } - function stringify(input) { - if (!input) { - return 'undefined'; // backwards compat for empty reduce - } - // for backwards compat with mapreduce, functions/strings are stringified - // as-is. everything else is JSON-stringified. - switch (typeof input) { - case 'function': - // e.g. a mapreduce map - return input.toString(); - case 'string': - // e.g. a mapreduce built-in _reduce function - return input.toString(); - default: - // e.g. a JSON object in the case of mango queries - return JSON.stringify(input); - } - } + if (opts.endkey) { + params.endkey = JSON.stringify(opts.endkey); + } - /* create a string signature for a view so we can cache it and uniq it */ - function createViewSignature(mapFun, reduceFun) { - // the "undefined" part is for backwards compatibility - return stringify(mapFun) + stringify(reduceFun) + 'undefined'; - } + if (typeof opts.inclusive_end !== 'undefined') { + params.inclusive_end = !!opts.inclusive_end; + } - function createView(sourceDB, viewName, mapFun, reduceFun, temporary, localDocName) { - var viewSignature = createViewSignature(mapFun, reduceFun); + if (typeof opts.limit !== 'undefined') { + params.limit = opts.limit; + } - var cachedViews; - if (!temporary) { - // cache this to ensure we don't try to update the same view twice - cachedViews = sourceDB._cachedViews = sourceDB._cachedViews || {}; - if (cachedViews[viewSignature]) { - return cachedViews[viewSignature]; + if (typeof opts.skip !== 'undefined') { + params.skip = opts.skip; } - } - var promiseForView = sourceDB.info().then(function (info) { + var paramStr = paramsToStr(params); - var depDbName = info.db_name + '-mrview-' + - (temporary ? 'temp' : stringMd5(viewSignature)); + if (typeof opts.keys !== 'undefined') { + method = 'POST'; + body = {keys: opts.keys}; + } - // save the view name in the source db so it can be cleaned up if necessary - // (e.g. when the _design doc is deleted, remove all associated view data) - function diffFunction(doc) { - doc.views = doc.views || {}; - var fullViewName = viewName; - if (fullViewName.indexOf('/') === -1) { - fullViewName = viewName + '/' + viewName; - } - var depDbs = doc.views[fullViewName] = doc.views[fullViewName] || {}; - /* istanbul ignore if */ - if (depDbs[depDbName]) { - return; // no update necessary + // Get the document listing + ajaxPromise(opts, { + method: method, + url: genDBUrl(host, '_all_docs' + paramStr), + body: body + }).then(function (res) { + if (opts.include_docs && opts.attachments && opts.binary) { + res.rows.forEach(readAttachmentsAsBlobOrBuffer); } - depDbs[depDbName] = true; - return doc; - } - return upsert(sourceDB, '_local/' + localDocName, diffFunction).then(function () { - return sourceDB.registerDependentDatabase(depDbName).then(function (res) { - var db = res.db; - db.auto_compaction = true; - var view = { - name: depDbName, - db: db, - sourceDB: sourceDB, - adapter: sourceDB.adapter, - mapFun: mapFun, - reduceFun: reduceFun - }; - return view.db.get('_local/lastSeq').catch(function (err) { - /* istanbul ignore if */ - if (err.status !== 404) { - throw err; - } - }).then(function (lastSeqDoc) { - view.seq = lastSeqDoc ? lastSeqDoc.seq : 0; - if (cachedViews) { - view.db.once('destroyed', function () { - delete cachedViews[viewSignature]; - }); - } - return view; - }); - }); - }); + callback(null, res); + }).catch(callback); }); - if (cachedViews) { - cachedViews[viewSignature] = promiseForView; - } - return promiseForView; - } - - var persistentQueues = {}; - var tempViewQueue = new TaskQueue$2(); - var CHANGES_BATCH_SIZE$1 = 50; + // 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) { - function parseViewName(name) { - // can be either 'ddocname/viewname' or just 'viewname' - // (where the ddoc name is the same) - return name.indexOf('/') === -1 ? [name, name] : name.split('/'); - } + // 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; - function isGenOne(changes) { - // only return true if the current change is 1- - // and there are no other leafs - return changes.length === 1 && /^1-/.test(changes[0].rev); - } + opts = clone(opts); + opts.timeout = ('timeout' in opts) ? opts.timeout : + ('timeout' in ajaxOpts) ? ajaxOpts.timeout : + 30 * 1000; - function emitError(db, e) { - try { - db.emit('error', e); - } catch (err) { - guardedConsole('error', - 'The user\'s map/reduce function threw an uncaught error.\n' + - 'You can debug this error by doing:\n' + - 'myDatabase.on(\'error\', function (err) { debugger; });\n' + - 'Please double-check your map/reduce function.'); - guardedConsole('error', e); - } - } + // We give a 5 second buffer for CouchDB changes to respond with + // an ok timeout (if a timeout it set) + var params = opts.timeout ? {timeout: opts.timeout - (5 * 1000)} : {}; + var limit = (typeof opts.limit !== 'undefined') ? opts.limit : false; + var returnDocs; + if ('return_docs' in opts) { + returnDocs = opts.return_docs; + } else if ('returnDocs' in opts) { + // TODO: Remove 'returnDocs' in favor of 'return_docs' in a future release + returnDocs = opts.returnDocs; + } else { + returnDocs = true; + } + // + var leftToFetch = limit; - /** - * Returns an "abstract" mapreduce object of the form: - * - * { - * query: queryFun, - * viewCleanup: viewCleanupFun - * } - * - * Arguments are: - * - * localDoc: string - * This is for the local doc that gets saved in order to track the - * "dependent" DBs and clean them up for viewCleanup. It should be - * unique, so that indexer plugins don't collide with each other. - * mapper: function (mapFunDef, emit) - * Returns a map function based on the mapFunDef, which in the case of - * normal map/reduce is just the de-stringified function, but may be - * something else, such as an object in the case of pouchdb-find. - * reducer: function (reduceFunDef) - * Ditto, but for reducing. Modules don't have to support reducing - * (e.g. pouchdb-find). - * ddocValidator: function (ddoc, viewName) - * Throws an error if the ddoc or viewName is not valid. - * This could be a way to communicate to the user that the configuration for the - * indexer is invalid. - */ - function createAbstractMapReduce(localDocName, mapper, reducer, ddocValidator) { + if (opts.style) { + params.style = opts.style; + } - function tryMap(db, fun, doc) { - // emit an event if there was an error thrown by a map function. - // putting try/catches in a single function also avoids deoptimizations. - try { - fun(doc); - } catch (e) { - emitError(db, e); + if (opts.include_docs || opts.filter && typeof opts.filter === 'function') { + params.include_docs = true; } - } - function tryReduce(db, fun, keys, values, rereduce) { - // same as above, but returning the result or an error. there are two separate - // functions to avoid extra memory allocations since the tryCode() case is used - // for custom map functions (common) vs this function, which is only used for - // custom reduce functions (rare) - try { - return {output : fun(keys, values, rereduce)}; - } catch (e) { - emitError(db, e); - return {error: e}; + if (opts.attachments) { + params.attachments = true; } - } - function sortByKeyThenValue(x, y) { - var keyCompare = collate(x.key, y.key); - return keyCompare !== 0 ? keyCompare : collate(x.value, y.value); - } + if (opts.continuous) { + params.feed = 'longpoll'; + } - function sliceResults(results, limit, skip) { - skip = skip || 0; - if (typeof limit === 'number') { - return results.slice(skip, limit + skip); - } else if (skip > 0) { - return results.slice(skip); + if (opts.conflicts) { + params.conflicts = true; } - return results; - } - function rowToDocId(row) { - var val = row.value; - // Users can explicitly specify a joined doc _id, or it - // defaults to the doc _id that emitted the key/value. - var docId = (val && typeof val === 'object' && val._id) || row.id; - return docId; - } + if (opts.descending) { + params.descending = true; + } - function readAttachmentsAsBlobOrBuffer(res) { - res.rows.forEach(function (row) { - var atts = row.doc && row.doc._attachments; - if (!atts) { - return; + if ('heartbeat' in opts) { + // If the heartbeat value is false, it disables the default heartbeat + if (opts.heartbeat) { + params.heartbeat = opts.heartbeat; } - Object.keys(atts).forEach(function (filename) { - var att = atts[filename]; - atts[filename].data = b64ToBluffer(att.data, att.content_type); - }); - }); - } + } else if (opts.continuous) { + // Default heartbeat to 10 seconds + params.heartbeat = 10000; + } - function postprocessAttachments(opts) { - return function (res) { - if (opts.include_docs && opts.attachments && opts.binary) { - readAttachmentsAsBlobOrBuffer(res); - } - return res; - }; - } + if (opts.filter && typeof opts.filter === 'string') { + params.filter = opts.filter; + } - function addHttpParam(paramName, opts, params, asJson) { - // add an http param from opts to params, optionally json-encoded - var val = opts[paramName]; - if (typeof val !== 'undefined') { - if (asJson) { - val = encodeURIComponent(JSON.stringify(val)); - } - params.push(paramName + '=' + val); + if (opts.view && typeof opts.view === 'string') { + params.filter = '_view'; + params.view = opts.view; } - } - function coerceInteger(integerCandidate) { - if (typeof integerCandidate !== 'undefined') { - var asNumber = Number(integerCandidate); - // prevents e.g. '1foo' or '1.1' being coerced to 1 - if (!isNaN(asNumber) && asNumber === parseInt(integerCandidate, 10)) { - return asNumber; - } else { - return integerCandidate; + // 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) { + /* istanbul ignore else */ + if (opts.query_params.hasOwnProperty(param_name)) { + params[param_name] = opts.query_params[param_name]; + } } } - } - function coerceOptions(opts) { - opts.group_level = coerceInteger(opts.group_level); - opts.limit = coerceInteger(opts.limit); - opts.skip = coerceInteger(opts.skip); - return opts; - } + var method = 'GET'; + var body; - function checkPositiveInteger(number) { - if (number) { - if (typeof number !== 'number') { - return new QueryParseError('Invalid value for integer: "' + - number + '"'); - } - if (number < 0) { - return new QueryParseError('Invalid value for positive integer: ' + - '"' + number + '"'); - } + 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'; + method = 'POST'; + body = {doc_ids: opts.doc_ids }; } - } - function checkQueryParseError(options, fun) { - var startkeyName = options.descending ? 'endkey' : 'startkey'; - var endkeyName = options.descending ? 'startkey' : 'endkey'; + var xhr; + var lastFetchedSeq; - if (typeof options[startkeyName] !== 'undefined' && - typeof options[endkeyName] !== 'undefined' && - collate(options[startkeyName], options[endkeyName]) > 0) { - throw new QueryParseError('No rows can match your key range, ' + - 'reverse your start_key and end_key or set {descending : true}'); - } else if (fun.reduce && options.reduce !== false) { - if (options.include_docs) { - throw new QueryParseError('{include_docs:true} is invalid for reduce'); - } else if (options.keys && options.keys.length > 1 && - !options.group && !options.group_level) { - throw new QueryParseError('Multi-key fetches for reduce views must use ' + - '{group: true}'); + // 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; } - } - ['group_level', 'limit', 'skip'].forEach(function (optionName) { - var error = checkPositiveInteger(options[optionName]); - if (error) { - throw error; + params.since = since; + // "since" can be any kind of json object in Coudant/CouchDB 2.x + /* istanbul ignore next */ + if (typeof params.since === "object") { + params.since = JSON.stringify(params.since); } - }); - } - - function httpQuery(db, fun, opts) { - // List of parameters to add to the PUT request - var params = []; - var body; - var method = 'GET'; - // If opts.reduce exists and is defined, then add it to the list - // of parameters. - // If reduce=false then the results are that of only the map function - // not the final result of map and reduce. - addHttpParam('reduce', opts, params); - addHttpParam('include_docs', opts, params); - addHttpParam('attachments', opts, params); - addHttpParam('limit', opts, params); - addHttpParam('descending', opts, params); - addHttpParam('group', opts, params); - addHttpParam('group_level', opts, params); - addHttpParam('skip', opts, params); - addHttpParam('stale', opts, params); - addHttpParam('conflicts', opts, params); - addHttpParam('startkey', opts, params, true); - addHttpParam('start_key', opts, params, true); - addHttpParam('endkey', opts, params, true); - addHttpParam('end_key', opts, params, true); - addHttpParam('inclusive_end', opts, params); - addHttpParam('key', opts, params, true); - addHttpParam('update_seq', opts, params); - - // Format the list of parameters into a valid URI query string - params = params.join('&'); - params = params === '' ? '' : '?' + params; - - // If keys are supplied, issue a POST to circumvent GET query string limits - // see http://wiki.apache.org/couchdb/HTTP_view_API#Querying_Options - if (typeof opts.keys !== 'undefined') { - var MAX_URL_LENGTH = 2000; - // according to http://stackoverflow.com/a/417184/680742, - // the de facto URL length limit is 2000 characters - - var keysAsString = - 'keys=' + encodeURIComponent(JSON.stringify(opts.keys)); - if (keysAsString.length + params.length + 1 <= MAX_URL_LENGTH) { - // If the keys are short enough, do a GET. we do this to work around - // Safari not understanding 304s on POSTs (see pouchdb/pouchdb#1239) - params += (params[0] === '?' ? '&' : '?') + keysAsString; - } else { - method = 'POST'; - if (typeof fun === 'string') { - body = {keys: opts.keys}; - } else { // fun is {map : mapfun}, so append to this - fun.keys = opts.keys; + if (opts.descending) { + if (limit) { + params.limit = leftToFetch; } + } else { + params.limit = (!limit || leftToFetch > batchSize) ? + batchSize : leftToFetch; } - } - // We are referencing a query defined in the design doc - if (typeof fun === 'string') { - var parts = parseViewName(fun); - return db.request({ + // Set the options for the ajax call + var xhrOpts = { method: method, - url: '_design/' + parts[0] + '/_view/' + parts[1] + params, + url: genDBUrl(host, '_changes' + paramsToStr(params)), + timeout: opts.timeout, body: body - }).then( - /* istanbul ignore next */ - function (result) { - // fail the entire request if the result contains an error - result.rows.forEach(function (row) { - if (row.value && row.value.error && row.value.error === "builtin_reduce_error") { - throw new Error(row.reason); - } - }); - - return result; - }) - .then(postprocessAttachments(opts)); - } + }; + lastFetchedSeq = since; - // We are using a temporary view, terrible for performance, good for testing - body = body || {}; - Object.keys(fun).forEach(function (key) { - if (Array.isArray(fun[key])) { - body[key] = fun[key]; - } else { - body[key] = fun[key].toString(); + /* istanbul ignore if */ + if (opts.aborted) { + return; } - }); - return db.request({ - method: 'POST', - url: '_temp_view' + params, - body: body - }).then(postprocessAttachments(opts)); - } - - // custom adapters can define their own api._query - // and override the default behavior - /* istanbul ignore next */ - function customQuery(db, fun, opts) { - return new PouchPromise(function (resolve, reject) { - db._query(fun, opts, function (err, res) { - if (err) { - return reject(err); - } - resolve(res); - }); - }); - } - - // custom adapters can define their own api._viewCleanup - // and override the default behavior - /* istanbul ignore next */ - function customViewCleanup(db) { - return new PouchPromise(function (resolve, reject) { - db._viewCleanup(function (err, res) { - if (err) { - return reject(err); - } - resolve(res); - }); - }); - } - function defaultsTo(value) { - return function (reason) { - /* istanbul ignore else */ - if (reason.status === 404) { - return value; - } else { - throw reason; - } + // Get the changes + setup().then(function () { + xhr = ajax$$1(opts, xhrOpts, callback); + }).catch(callback); }; - } - // returns a promise for a list of docs to update, based on the input docId. - // the order doesn't matter, because post-3.2.0, bulkDocs - // is an atomic operation in all three adapters. - function getDocsToPersist(docId, view, docIdsToChangesAndEmits) { - var metaDocId = '_local/doc_' + docId; - var defaultMetaDoc = {_id: metaDocId, keys: []}; - var docData = docIdsToChangesAndEmits.get(docId); - var indexableKeysToKeyValues = docData[0]; - var changes = docData[1]; + // 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 results = {results: []}; - function getMetaDoc() { - if (isGenOne(changes)) { - // generation 1, so we can safely assume initial state - // for performance reasons (avoids unnecessary GETs) - return PouchPromise.resolve(defaultMetaDoc); + 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 = filterChange(opts)(c); + if (ret) { + if (opts.include_docs && opts.attachments && opts.binary) { + readAttachmentsAsBlobOrBuffer(c); + } + if (returnDocs) { + results.results.push(c); + } + opts.onChange(c); + } + return ret; + }); + } else if (err) { + // In case of an error, stop listening for changes and call + // opts.complete + opts.aborted = true; + opts.complete(err); + return; } - return view.db.get(metaDocId).catch(defaultsTo(defaultMetaDoc)); - } - function getKeyValueDocs(metaDoc) { - if (!metaDoc.keys.length) { - // no keys, no need for a lookup - return PouchPromise.resolve({rows: []}); + // 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; } - return view.db.allDocs({ - keys: metaDoc.keys, - include_docs: true - }); - } - function processKeyValueDocs(metaDoc, kvDocsRes) { - var kvDocs = []; - var oldKeys = new ExportedSet(); + var finished = (limit && leftToFetch <= 0) || + (res && raw_results_length < batchSize) || + (opts.descending); - for (var i = 0, len = kvDocsRes.rows.length; i < len; i++) { - var row = kvDocsRes.rows[i]; - var doc = row.doc; - if (!doc) { // deleted - continue; - } - kvDocs.push(doc); - oldKeys.add(doc._id); - doc._deleted = !indexableKeysToKeyValues.has(doc._id); - if (!doc._deleted) { - var keyValue = indexableKeysToKeyValues.get(doc._id); - if ('value' in keyValue) { - doc.value = keyValue.value; - } - } + if ((opts.continuous && !(limit && leftToFetch <= 0)) || !finished) { + // Queue a call to fetch again with the newest sequence number + nextTick(function () { fetch(lastFetchedSeq, fetched); }); + } else { + // We're done, call the callback + opts.complete(null, results); } - var newKeys = mapToKeysArray(indexableKeysToKeyValues); - newKeys.forEach(function (key) { - if (!oldKeys.has(key)) { - // new doc - var kvDoc = { - _id: key - }; - var keyValue = indexableKeysToKeyValues.get(key); - if ('value' in keyValue) { - kvDoc.value = keyValue.value; - } - kvDocs.push(kvDoc); + }; + + 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(); } - }); - metaDoc.keys = uniq(newKeys.concat(metaDoc.keys)); - kvDocs.push(metaDoc); + } + }; + }; - return kvDocs; + // 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 = adapterFun$$1('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 = {}; } - return getMetaDoc().then(function (metaDoc) { - return getKeyValueDocs(metaDoc).then(function (kvDocsRes) { - return processKeyValueDocs(metaDoc, kvDocsRes); - }); + // Get the missing document/revision IDs + ajax$$1(opts, { + method: 'POST', + url: genDBUrl(host, '_revs_diff'), + body: req + }, callback); + }); + + api._close = function (callback) { + callback(); + }; + + api._destroy = function (options, callback) { + ajax$$1(options, { + url: genDBUrl(host, ''), + method: 'DELETE' + }, function (err, resp) { + if (err && err.status && err.status !== 404) { + return callback(err); + } + callback(null, resp); }); - } + }; + } - // updates all emitted key/value docs and metaDocs in the mrview database - // for the given batch of documents from the source database - function saveKeyValues(view, docIdsToChangesAndEmits, seq) { - var seqDocId = '_local/lastSeq'; - return view.db.get(seqDocId) - .catch(defaultsTo({_id: seqDocId, seq: 0})) - .then(function (lastSeqDoc) { - var docIds = mapToKeysArray(docIdsToChangesAndEmits); - return PouchPromise.all(docIds.map(function (docId) { - return getDocsToPersist(docId, view, docIdsToChangesAndEmits); - })).then(function (listOfDocsToPersist) { - var docsToPersist = flatten(listOfDocsToPersist); - lastSeqDoc.seq = seq; - docsToPersist.push(lastSeqDoc); - // write all docs in a single operation, update the seq once - return view.db.bulkDocs({docs : docsToPersist}); - }); - }); - } + // HttpPouch is a valid adapter. + HttpPouch.valid = function () { + return true; + }; - function getQueue(view) { - var viewName = typeof view === 'string' ? view : view.name; - var queue = persistentQueues[viewName]; - if (!queue) { - queue = persistentQueues[viewName] = new TaskQueue$2(); - } - return queue; - } + var HttpPouch$1 = function (PouchDB) { + PouchDB.adapter('http', HttpPouch, false); + PouchDB.adapter('https', HttpPouch, false); + }; - function updateView(view) { - return sequentialize(getQueue(view), function () { - return updateViewInQueue(view); - })(); + function pad(str, padWith, upToLength) { + var padding = ''; + var targetLength = upToLength - str.length; + /* istanbul ignore next */ + while (padding.length < targetLength) { + padding += padWith; } + return padding; + } - function updateViewInQueue(view) { - // bind the emit function once - var mapResults; - var doc; - - function emit(key, value) { - var output = {id: doc._id, key: normalizeKey(key)}; - // Don't explicitly store the value unless it's defined and non-null. - // This saves on storage space, because often people don't use it. - if (typeof value !== 'undefined' && value !== null) { - output.value = normalizeKey(value); - } - mapResults.push(output); - } + function padLeft(str, padWith, upToLength) { + var padding = pad(str, padWith, upToLength); + return padding + str; + } - var mapFun = mapper(view.mapFun, emit); + var MIN_MAGNITUDE = -324; // verified by -Number.MIN_VALUE + var MAGNITUDE_DIGITS = 3; // ditto + var SEP = ''; // set to '_' for easier debugging - var currentSeq = view.seq || 0; + function collate(a, b) { - function processChange(docIdsToChangesAndEmits, seq) { - return function () { - return saveKeyValues(view, docIdsToChangesAndEmits, seq); - }; - } + if (a === b) { + return 0; + } - var queue = new TaskQueue$2(); + a = normalizeKey(a); + b = normalizeKey(b); - function processNextBatch() { - return view.sourceDB.changes({ - conflicts: true, - include_docs: true, - style: 'all_docs', - since: currentSeq, - limit: CHANGES_BATCH_SIZE$1 - }).then(processBatch); - } + var ai = collationIndex(a); + var bi = collationIndex(b); + if ((ai - bi) !== 0) { + return ai - bi; + } + switch (typeof a) { + case 'number': + return a - b; + case 'boolean': + return a < b ? -1 : 1; + case 'string': + return stringCollate(a, b); + } + return Array.isArray(a) ? arrayCollate(a, b) : objectCollate(a, b); + } - function processBatch(response) { - var results = response.results; - if (!results.length) { - return; - } - var docIdsToChangesAndEmits = createDocIdsToChangesAndEmits(results); - queue.add(processChange(docIdsToChangesAndEmits, currentSeq)); - if (results.length < CHANGES_BATCH_SIZE$1) { - return; + // couch considers null/NaN/Infinity/-Infinity === undefined, + // for the purposes of mapreduce indexes. also, dates get stringified. + function normalizeKey(key) { + switch (typeof key) { + case 'undefined': + return null; + case 'number': + if (key === Infinity || key === -Infinity || isNaN(key)) { + return null; } - return processNextBatch(); - } - - function createDocIdsToChangesAndEmits(results) { - var docIdsToChangesAndEmits = new ExportedMap(); - for (var i = 0, len = results.length; i < len; i++) { - var change = results[i]; - if (change.doc._id[0] !== '_') { - mapResults = []; - doc = change.doc; - - if (!doc._deleted) { - tryMap(view.sourceDB, mapFun, doc); + return key; + case 'object': + var origKey = key; + if (Array.isArray(key)) { + var len = key.length; + key = new Array(len); + for (var i = 0; i < len; i++) { + key[i] = normalizeKey(origKey[i]); + } + /* istanbul ignore next */ + } else if (key instanceof Date) { + return key.toJSON(); + } else if (key !== null) { // generic object + key = {}; + for (var k in origKey) { + if (origKey.hasOwnProperty(k)) { + var val = origKey[k]; + if (typeof val !== 'undefined') { + key[k] = normalizeKey(val); + } } - mapResults.sort(sortByKeyThenValue); - - var indexableKeysToKeyValues = createIndexableKeysToKeyValues(mapResults); - docIdsToChangesAndEmits.set(change.doc._id, [ - indexableKeysToKeyValues, - change.changes - ]); } - currentSeq = change.seq; } - return docIdsToChangesAndEmits; - } + } + return key; + } - function createIndexableKeysToKeyValues(mapResults) { - var indexableKeysToKeyValues = new ExportedMap(); - var lastKey; - for (var i = 0, len = mapResults.length; i < len; i++) { - var emittedKeyValue = mapResults[i]; - var complexKey = [emittedKeyValue.key, emittedKeyValue.id]; - if (i > 0 && collate(emittedKeyValue.key, lastKey) === 0) { - complexKey.push(i); // dup key+id, so make it unique + function indexify(key) { + if (key !== null) { + switch (typeof key) { + case 'boolean': + return key ? 1 : 0; + case 'number': + return numToIndexableString(key); + case 'string': + // We've to be sure that key does not contain \u0000 + // Do order-preserving replacements: + // 0 -> 1, 1 + // 1 -> 1, 2 + // 2 -> 2, 2 + return key + .replace(/\u0002/g, '\u0002\u0002') + .replace(/\u0001/g, '\u0001\u0002') + .replace(/\u0000/g, '\u0001\u0001'); + case 'object': + var isArray = Array.isArray(key); + var arr = isArray ? key : Object.keys(key); + var i = -1; + var len = arr.length; + var result = ''; + if (isArray) { + while (++i < len) { + result += toIndexableString(arr[i]); + } + } else { + while (++i < len) { + var objKey = arr[i]; + result += toIndexableString(objKey) + + toIndexableString(key[objKey]); + } } - indexableKeysToKeyValues.set(toIndexableString(complexKey), emittedKeyValue); - lastKey = emittedKeyValue.key; - } - return indexableKeysToKeyValues; + return result; } - - return processNextBatch().then(function () { - return queue.finish(); - }).then(function () { - view.seq = currentSeq; - }); } + return ''; + } - function reduceView(view, results, options) { - if (options.group_level === 0) { - delete options.group_level; - } - - var shouldGroup = options.group || options.group_level; - - var reduceFun = reducer(view.reduceFun); - - var groups = []; - var lvl = isNaN(options.group_level) ? Number.POSITIVE_INFINITY : - options.group_level; - results.forEach(function (e) { - var last = groups[groups.length - 1]; - var groupKey = shouldGroup ? e.key : null; - - // only set group_level for array keys - if (shouldGroup && Array.isArray(groupKey)) { - groupKey = groupKey.slice(0, lvl); - } + // convert the given key to a string that would be appropriate + // for lexical sorting, e.g. within a database, where the + // sorting is the same given by the collate() function. + function toIndexableString(key) { + var zero = '\u0000'; + key = normalizeKey(key); + return collationIndex(key) + SEP + indexify(key) + zero; + } - if (last && collate(last.groupKey, groupKey) === 0) { - last.keys.push([e.key, e.id]); - last.values.push(e.value); - return; - } - groups.push({ - keys: [[e.key, e.id]], - values: [e.value], - groupKey: groupKey - }); - }); - results = []; - for (var i = 0, len = groups.length; i < len; i++) { - var e = groups[i]; - var reduceTry = tryReduce(view.sourceDB, reduceFun, e.keys, e.values, false); - if (reduceTry.error && reduceTry.error instanceof BuiltInError) { - // CouchDB returns an error if a built-in errors out - throw reduceTry.error; + function parseNumber(str, i) { + var originalIdx = i; + var num; + var zero = str[i] === '1'; + if (zero) { + num = 0; + i++; + } else { + var neg = str[i] === '0'; + i++; + var numAsString = ''; + var magAsString = str.substring(i, i + MAGNITUDE_DIGITS); + var magnitude = parseInt(magAsString, 10) + MIN_MAGNITUDE; + /* istanbul ignore next */ + if (neg) { + magnitude = -magnitude; + } + i += MAGNITUDE_DIGITS; + while (true) { + var ch = str[i]; + if (ch === '\u0000') { + break; + } else { + numAsString += ch; } - results.push({ - // CouchDB just sets the value to null if a non-built-in errors out - value: reduceTry.error ? null : reduceTry.output, - key: e.groupKey - }); + i++; + } + numAsString = numAsString.split('.'); + if (numAsString.length === 1) { + num = parseInt(numAsString, 10); + } else { + /* istanbul ignore next */ + num = parseFloat(numAsString[0] + '.' + numAsString[1]); + } + /* istanbul ignore next */ + if (neg) { + num = num - 10; + } + /* istanbul ignore next */ + if (magnitude !== 0) { + // parseFloat is more reliable than pow due to rounding errors + // e.g. Number.MAX_VALUE would return Infinity if we did + // num * Math.pow(10, magnitude); + num = parseFloat(num + 'e' + magnitude); } - // no total_rows/offset when reducing - return {rows: sliceResults(results, options.limit, options.skip)}; } + return {num: num, length : i - originalIdx}; + } - function queryView(view, opts) { - return sequentialize(getQueue(view), function () { - return queryViewInQueue(view, opts); - })(); - } + // move up the stack while parsing + // this function moved outside of parseIndexableString for performance + function pop(stack, metaStack) { + var obj = stack.pop(); - function queryViewInQueue(view, opts) { - var totalRows; - var shouldReduce = view.reduceFun && opts.reduce !== false; - var skip = opts.skip || 0; - if (typeof opts.keys !== 'undefined' && !opts.keys.length) { - // equivalent query - opts.limit = 0; - delete opts.keys; + if (metaStack.length) { + var lastMetaElement = metaStack[metaStack.length - 1]; + if (obj === lastMetaElement.element) { + // popping a meta-element, e.g. an object whose value is another object + metaStack.pop(); + lastMetaElement = metaStack[metaStack.length - 1]; } - - function fetchFromView(viewOpts) { - viewOpts.include_docs = true; - return view.db.allDocs(viewOpts).then(function (res) { - totalRows = res.total_rows; - return res.rows.map(function (result) { - - // implicit migration - in older versions of PouchDB, - // we explicitly stored the doc as {id: ..., key: ..., value: ...} - // this is tested in a migration test - /* istanbul ignore next */ - if ('value' in result.doc && typeof result.doc.value === 'object' && - result.doc.value !== null) { - var keys = Object.keys(result.doc.value).sort(); - // this detection method is not perfect, but it's unlikely the user - // emitted a value which was an object with these 3 exact keys - var expectedKeys = ['id', 'key', 'value']; - if (!(keys < expectedKeys || keys > expectedKeys)) { - return result.doc.value; - } - } - - var parsedKeyAndDocId = parseIndexableString(result.doc._id); - return { - key: parsedKeyAndDocId[0], - id: parsedKeyAndDocId[1], - value: ('value' in result.doc ? result.doc.value : null) - }; - }); - }); + var element = lastMetaElement.element; + var lastElementIndex = lastMetaElement.index; + if (Array.isArray(element)) { + element.push(obj); + } else if (lastElementIndex === stack.length - 2) { // obj with key+value + var key = stack.pop(); + element[key] = obj; + } else { + stack.push(obj); // obj with key only } + } + } - function onMapResultsReady(rows) { - var finalResults; - if (shouldReduce) { - finalResults = reduceView(view, rows, opts); - } else { - finalResults = { - total_rows: totalRows, - offset: skip, - rows: rows - }; - } - /* istanbul ignore if */ - if (opts.update_seq) { - finalResults.update_seq = view.seq; - } - if (opts.include_docs) { - var docIds = uniq(rows.map(rowToDocId)); + function parseIndexableString(str) { + var stack = []; + var metaStack = []; // stack for arrays and objects + var i = 0; - return view.sourceDB.allDocs({ - keys: docIds, - include_docs: true, - conflicts: opts.conflicts, - attachments: opts.attachments, - binary: opts.binary - }).then(function (allDocsRes) { - var docIdsToDocs = new ExportedMap(); - allDocsRes.rows.forEach(function (row) { - docIdsToDocs.set(row.id, row.doc); - }); - rows.forEach(function (row) { - var docId = rowToDocId(row); - var doc = docIdsToDocs.get(docId); - if (doc) { - row.doc = doc; - } - }); - return finalResults; - }); + /*eslint no-constant-condition: ["error", { "checkLoops": false }]*/ + while (true) { + var collationIndex = str[i++]; + if (collationIndex === '\u0000') { + if (stack.length === 1) { + return stack.pop(); } else { - return finalResults; + pop(stack, metaStack); + continue; } } - - if (typeof opts.keys !== 'undefined') { - var keys = opts.keys; - var fetchPromises = keys.map(function (key) { - var viewOpts = { - startkey : toIndexableString([key]), - endkey : toIndexableString([key, {}]) - }; - /* istanbul ignore if */ - if (opts.update_seq) { - viewOpts.update_seq = true; - } - return fetchFromView(viewOpts); - }); - return PouchPromise.all(fetchPromises).then(flatten).then(onMapResultsReady); - } else { // normal query, no 'keys' - var viewOpts = { - descending : opts.descending - }; - /* istanbul ignore if */ - if (opts.update_seq) { - viewOpts.update_seq = true; - } - var startkey; - var endkey; - if ('start_key' in opts) { - startkey = opts.start_key; - } - if ('startkey' in opts) { - startkey = opts.startkey; - } - if ('end_key' in opts) { - endkey = opts.end_key; - } - if ('endkey' in opts) { - endkey = opts.endkey; - } - if (typeof startkey !== 'undefined') { - viewOpts.startkey = opts.descending ? - toIndexableString([startkey, {}]) : - toIndexableString([startkey]); - } - if (typeof endkey !== 'undefined') { - var inclusiveEnd = opts.inclusive_end !== false; - if (opts.descending) { - inclusiveEnd = !inclusiveEnd; + switch (collationIndex) { + case '1': + stack.push(null); + break; + case '2': + stack.push(str[i] === '1'); + i++; + break; + case '3': + var parsedNum = parseNumber(str, i); + stack.push(parsedNum.num); + i += parsedNum.length; + break; + case '4': + var parsedStr = ''; + /*eslint no-constant-condition: ["error", { "checkLoops": false }]*/ + while (true) { + var ch = str[i]; + if (ch === '\u0000') { + break; + } + parsedStr += ch; + i++; } + // perform the reverse of the order-preserving replacement + // algorithm (see above) + parsedStr = parsedStr.replace(/\u0001\u0001/g, '\u0000') + .replace(/\u0001\u0002/g, '\u0001') + .replace(/\u0002\u0002/g, '\u0002'); + stack.push(parsedStr); + break; + case '5': + var arrayElement = { element: [], index: stack.length }; + stack.push(arrayElement.element); + metaStack.push(arrayElement); + break; + case '6': + var objElement = { element: {}, index: stack.length }; + stack.push(objElement.element); + metaStack.push(objElement); + break; + /* istanbul ignore next */ + default: + throw new Error( + 'bad collationIndex or unexpectedly reached end of input: ' + + collationIndex); + } + } + } - viewOpts.endkey = toIndexableString( - inclusiveEnd ? [endkey, {}] : [endkey]); - } - if (typeof opts.key !== 'undefined') { - var keyStart = toIndexableString([opts.key]); - var keyEnd = toIndexableString([opts.key, {}]); - if (viewOpts.descending) { - viewOpts.endkey = keyStart; - viewOpts.startkey = keyEnd; - } else { - viewOpts.startkey = keyStart; - viewOpts.endkey = keyEnd; - } - } - if (!shouldReduce) { - if (typeof opts.limit === 'number') { - viewOpts.limit = opts.limit; - } - viewOpts.skip = skip; - } - return fetchFromView(viewOpts).then(onMapResultsReady); + function arrayCollate(a, b) { + var len = Math.min(a.length, b.length); + for (var i = 0; i < len; i++) { + var sort = collate(a[i], b[i]); + if (sort !== 0) { + return sort; + } + } + return (a.length === b.length) ? 0 : + (a.length > b.length) ? 1 : -1; + } + function stringCollate(a, b) { + // See: https://github.com/daleharvey/pouchdb/issues/40 + // This is incompatible with the CouchDB implementation, but its the + // best we can do for now + return (a === b) ? 0 : ((a > b) ? 1 : -1); + } + function objectCollate(a, b) { + var ak = Object.keys(a), bk = Object.keys(b); + var len = Math.min(ak.length, bk.length); + for (var i = 0; i < len; i++) { + // First sort the keys + var sort = collate(ak[i], bk[i]); + if (sort !== 0) { + return sort; + } + // if the keys are equal sort the values + sort = collate(a[ak[i]], b[bk[i]]); + if (sort !== 0) { + return sort; } - } - function httpViewCleanup(db) { - return db.request({ - method: 'POST', - url: '_view_cleanup' - }); } + return (ak.length === bk.length) ? 0 : + (ak.length > bk.length) ? 1 : -1; + } + // The collation is defined by erlangs ordered terms + // the atoms null, true, false come first, then numbers, strings, + // arrays, then objects + // null/undefined/NaN/Infinity/-Infinity are all considered null + function collationIndex(x) { + var id = ['boolean', 'number', 'string', 'object']; + var idx = id.indexOf(typeof x); + //false if -1 otherwise true, but fast!!!!1 + if (~idx) { + if (x === null) { + return 1; + } + if (Array.isArray(x)) { + return 5; + } + return idx < 3 ? (idx + 2) : (idx + 3); + } + /* istanbul ignore next */ + if (Array.isArray(x)) { + return 5; + } + } - function localViewCleanup(db) { - return db.get('_local/' + localDocName).then(function (metaDoc) { - var docsToViews = new ExportedMap(); - Object.keys(metaDoc.views).forEach(function (fullViewName) { - var parts = parseViewName(fullViewName); - var designDocName = '_design/' + parts[0]; - var viewName = parts[1]; - var views = docsToViews.get(designDocName); - if (!views) { - views = new ExportedSet(); - docsToViews.set(designDocName, views); - } - views.add(viewName); - }); - var opts = { - keys : mapToKeysArray(docsToViews), - include_docs : true - }; - return db.allDocs(opts).then(function (res) { - var viewsToStatus = {}; - res.rows.forEach(function (row) { - var ddocName = row.key.substring(8); // cuts off '_design/' - docsToViews.get(row.key).forEach(function (viewName) { - var fullViewName = ddocName + '/' + viewName; - /* istanbul ignore if */ - if (!metaDoc.views[fullViewName]) { - // new format, without slashes, to support PouchDB 2.2.0 - // migration test in pouchdb's browser.migration.js verifies this - fullViewName = viewName; - } - var viewDBNames = Object.keys(metaDoc.views[fullViewName]); - // design doc deleted, or view function nonexistent - var statusIsGood = row.doc && row.doc.views && - row.doc.views[viewName]; - viewDBNames.forEach(function (viewDBName) { - viewsToStatus[viewDBName] = - viewsToStatus[viewDBName] || statusIsGood; - }); - }); - }); - var dbsToDelete = Object.keys(viewsToStatus).filter( - function (viewDBName) { return !viewsToStatus[viewDBName]; }); - var destroyPromises = dbsToDelete.map(function (viewDBName) { - return sequentialize(getQueue(viewDBName), function () { - return new db.constructor(viewDBName, db.__opts).destroy(); - })(); - }); - return PouchPromise.all(destroyPromises).then(function () { - return {ok: true}; - }); - }); - }, defaultsTo({ok: true})); + // conversion: + // x yyy zz...zz + // x = 0 for negative, 1 for 0, 2 for positive + // y = exponent (for negative numbers negated) moved so that it's >= 0 + // z = mantisse + function numToIndexableString(num) { + + if (num === 0) { + return '1'; } - function queryPromised(db, fun, opts) { - /* istanbul ignore next */ - if (typeof db._query === 'function') { - return customQuery(db, fun, opts); - } - if (isRemote(db)) { - return httpQuery(db, fun, opts); - } - - if (typeof fun !== 'string') { - // temp_view - checkQueryParseError(opts, fun); + // convert number to exponential format for easier and + // more succinct string sorting + var expFormat = num.toExponential().split(/e\+?/); + var magnitude = parseInt(expFormat[1], 10); - tempViewQueue.add(function () { - var createViewPromise = createView( - /* sourceDB */ db, - /* viewName */ 'temp_view/temp_view', - /* mapFun */ fun.map, - /* reduceFun */ fun.reduce, - /* temporary */ true, - /* localDocName */ localDocName); - return createViewPromise.then(function (view) { - return fin(updateView(view).then(function () { - return queryView(view, opts); - }), function () { - return view.db.destroy(); - }); - }); - }); - return tempViewQueue.finish(); - } else { - // persistent view - var fullViewName = fun; - var parts = parseViewName(fullViewName); - var designDocName = parts[0]; - var viewName = parts[1]; - return db.get('_design/' + designDocName).then(function (doc) { - var fun = doc.views && doc.views[viewName]; + var neg = num < 0; - if (!fun) { - // basic validator; it's assumed that every subclass would want this - throw new NotFoundError('ddoc ' + doc._id + ' has no view named ' + - viewName); - } + var result = neg ? '0' : '2'; - ddocValidator(doc, viewName); - checkQueryParseError(opts, fun); + // first sort by magnitude + // it's easier if all magnitudes are positive + var magForComparison = ((neg ? -magnitude : magnitude) - MIN_MAGNITUDE); + var magString = padLeft((magForComparison).toString(), '0', MAGNITUDE_DIGITS); - var createViewPromise = createView( - /* sourceDB */ db, - /* viewName */ fullViewName, - /* mapFun */ fun.map, - /* reduceFun */ fun.reduce, - /* temporary */ false, - /* localDocName */ localDocName); - return createViewPromise.then(function (view) { - if (opts.stale === 'ok' || opts.stale === 'update_after') { - if (opts.stale === 'update_after') { - nextTick(function () { - updateView(view); - }); - } - return queryView(view, opts); - } else { // stale not ok - return updateView(view).then(function () { - return queryView(view, opts); - }); - } - }); - }); - } + result += SEP + magString; + + // then sort by the factor + var factor = Math.abs(parseFloat(expFormat[0])); // [1..10) + /* istanbul ignore next */ + if (neg) { // for negative reverse ordering + factor = 10 - factor; } - function abstractQuery(fun, opts, callback) { - var db = this; - if (typeof opts === 'function') { - callback = opts; - opts = {}; - } - opts = opts ? coerceOptions(opts) : {}; + var factorStr = factor.toFixed(20); - if (typeof fun === 'function') { - fun = {map : fun}; - } + // strip zeros from the end + factorStr = factorStr.replace(/\.?0+$/, ''); - var promise = PouchPromise.resolve().then(function () { - return queryPromised(db, fun, opts); - }); - promisedCallback(promise, callback); - return promise; - } + result += SEP + factorStr; - var abstractViewCleanup = callbackify(function () { - var db = this; - /* istanbul ignore next */ - if (typeof db._viewCleanup === 'function') { - return customViewCleanup(db); - } - if (isRemote(db)) { - return httpViewCleanup(db); - } - return localViewCleanup(db); - }); + return result; + } - return { - query: abstractQuery, - viewCleanup: abstractViewCleanup - }; + /* + * Simple task queue to sequentialize actions. Assumes + * callbacks will eventually fire (once). + */ + + function TaskQueue$2() { + this.promise = new PouchPromise$1(function (fulfill) {fulfill(); }); } + TaskQueue$2.prototype.add = function (promiseFactory) { + this.promise = this.promise.catch(function () { + // just recover + }).then(function () { + return promiseFactory(); + }); + return this.promise; + }; + TaskQueue$2.prototype.finish = function () { + return this.promise; + }; - var builtInReduce = { - _sum: function (keys, values) { - return sum(values); - }, + function createView(opts) { + var sourceDB = opts.db; + var viewName = opts.viewName; + var mapFun = opts.map; + var reduceFun = opts.reduce; + var temporary = opts.temporary; - _count: function (keys, values) { - return values.length; - }, + // the "undefined" part is for backwards compatibility + var viewSignature = mapFun.toString() + (reduceFun && reduceFun.toString()) + + 'undefined'; - _stats: function (keys, values) { - // no need to implement rereduce=true, because Pouch - // will never call it - function sumsqr(values) { - var _sumsqr = 0; - for (var i = 0, len = values.length; i < len; i++) { - var num = values[i]; - _sumsqr += (num * num); - } - return _sumsqr; + var cachedViews; + if (!temporary) { + // cache this to ensure we don't try to update the same view twice + cachedViews = sourceDB._cachedViews = sourceDB._cachedViews || {}; + if (cachedViews[viewSignature]) { + return cachedViews[viewSignature]; } - return { - sum : sum(values), - min : Math.min.apply(null, values), - max : Math.max.apply(null, values), - count : values.length, - sumsqr : sumsqr(values) - }; } - }; - function getBuiltIn(reduceFunString) { - if (/^_sum/.test(reduceFunString)) { - return builtInReduce._sum; - } else if (/^_count/.test(reduceFunString)) { - return builtInReduce._count; - } else if (/^_stats/.test(reduceFunString)) { - return builtInReduce._stats; - } else if (/^_/.test(reduceFunString)) { - throw new Error(reduceFunString + ' is not a supported reduce function.'); - } - } + var promiseForView = sourceDB.info().then(function (info) { - function mapper(mapFun, emit) { - // for temp_views one can use emit(doc, emit), see #38 - if (typeof mapFun === "function" && mapFun.length === 2) { - var origMap = mapFun; - return function (doc) { - return origMap(doc, emit); - }; - } else { - return evalFunctionWithEval(mapFun.toString(), emit); - } - } + var depDbName = info.db_name + '-mrview-' + + (temporary ? 'temp' : stringMd5(viewSignature)); + + // save the view name in the source db so it can be cleaned up if necessary + // (e.g. when the _design doc is deleted, remove all associated view data) + function diffFunction(doc) { + doc.views = doc.views || {}; + var fullViewName = viewName; + if (fullViewName.indexOf('/') === -1) { + fullViewName = viewName + '/' + viewName; + } + var depDbs = doc.views[fullViewName] = doc.views[fullViewName] || {}; + /* istanbul ignore if */ + if (depDbs[depDbName]) { + return; // no update necessary + } + depDbs[depDbName] = true; + return doc; + } + return upsert(sourceDB, '_local/mrviews', diffFunction).then(function () { + return sourceDB.registerDependentDatabase(depDbName).then(function (res) { + var db = res.db; + db.auto_compaction = true; + var view = { + name: depDbName, + db: db, + sourceDB: sourceDB, + adapter: sourceDB.adapter, + mapFun: mapFun, + reduceFun: reduceFun + }; + return view.db.get('_local/lastSeq').catch(function (err) { + /* istanbul ignore if */ + if (err.status !== 404) { + throw err; + } + }).then(function (lastSeqDoc) { + view.seq = lastSeqDoc ? lastSeqDoc.seq : 0; + if (cachedViews) { + view.db.once('destroyed', function () { + delete cachedViews[viewSignature]; + }); + } + return view; + }); + }); + }); + }); - function reducer(reduceFun) { - var reduceFunString = reduceFun.toString(); - var builtIn = getBuiltIn(reduceFunString); - if (builtIn) { - return builtIn; - } else { - return evalFunctionWithEval(reduceFunString); + if (cachedViews) { + cachedViews[viewSignature] = promiseForView; } + return promiseForView; } - function ddocValidator(ddoc, viewName) { - var fun = ddoc.views && ddoc.views[viewName]; - if (typeof fun.map !== 'string') { - throw new NotFoundError('ddoc ' + ddoc._id + ' has no string view named ' + - viewName + ', instead found object of type: ' + typeof fun.map); - } + function QueryParseError(message) { + this.status = 400; + this.name = 'query_parse_error'; + this.message = message; + this.error = true; + try { + Error.captureStackTrace(this, QueryParseError); + } catch (e) {} } - var localDocName = 'mrviews'; - var abstract = createAbstractMapReduce(localDocName, mapper, reducer, ddocValidator); + inherits(QueryParseError, Error); - function query(fun, opts, callback) { - return abstract.query.call(this, fun, opts, callback); + function NotFoundError(message) { + this.status = 404; + this.name = 'not_found'; + this.message = message; + this.error = true; + try { + Error.captureStackTrace(this, NotFoundError); + } catch (e) {} } - function viewCleanup(callback) { - return abstract.viewCleanup.call(this, callback); + inherits(NotFoundError, Error); + + function BuiltInError(message) { + this.status = 500; + this.name = 'invalid_value'; + this.message = message; + this.error = true; + try { + Error.captureStackTrace(this, BuiltInError); + } catch (e) {} } - var mapreduce = { - query: query, - viewCleanup: viewCleanup - }; + inherits(BuiltInError, Error); - function isGenOne$1(rev$$1) { - return /^1-/.test(rev$$1); + function createBuiltInError(name) { + var message = 'builtin ' + name + + ' function requires map values to be numbers' + + ' or number arrays'; + return new BuiltInError(message); } - function fileHasChanged(localDoc, remoteDoc, filename) { - return !localDoc._attachments || - !localDoc._attachments[filename] || - localDoc._attachments[filename].digest !== remoteDoc._attachments[filename].digest; + function sum(values) { + var result = 0; + for (var i = 0, len = values.length; i < len; i++) { + var num = values[i]; + if (typeof num !== 'number') { + if (Array.isArray(num)) { + // lists of numbers are also allowed, sum them separately + result = typeof result === 'number' ? [result] : result; + for (var j = 0, jLen = num.length; j < jLen; j++) { + var jNum = num[j]; + if (typeof jNum !== 'number') { + throw createBuiltInError('_sum'); + } else if (typeof result[j] === 'undefined') { + result.push(jNum); + } else { + result[j] += jNum; + } + } + } else { // not array/number + throw createBuiltInError('_sum'); + } + } else if (typeof result === 'number') { + result += num; + } else { // add number to array + result[0] += num; + } + } + return result; } - function getDocAttachments(db, doc) { - var filenames = Object.keys(doc._attachments); - return PouchPromise.all(filenames.map(function (filename) { - return db.getAttachment(doc._id, filename, {rev: doc._rev}); - })); - } + var log$2 = guardedConsole.bind(null, 'log'); + var isArray = Array.isArray; + var toJSON = JSON.parse; - function getDocAttachmentsFromTargetOrSource(target, src, doc) { - var doCheckForLocalAttachments = isRemote(src) && !isRemote(target); - var filenames = Object.keys(doc._attachments); + function evalFunctionWithEval(func, emit) { + return scopedEval( + "return (" + func.replace(/;\s*$/, "") + ");", + { + emit: emit, + sum: sum, + log: log$2, + isArray: isArray, + toJSON: toJSON + } + ); + } - if (!doCheckForLocalAttachments) { - return getDocAttachments(src, doc); + function promisedCallback(promise, callback) { + if (callback) { + promise.then(function (res) { + nextTick(function () { + callback(null, res); + }); + }, function (reason) { + nextTick(function () { + callback(reason); + }); + }); } + return promise; + } - return target.get(doc._id).then(function (localDoc) { - return PouchPromise.all(filenames.map(function (filename) { - if (fileHasChanged(localDoc, doc, filename)) { - return src.getAttachment(doc._id, filename); - } - - return target.getAttachment(localDoc._id, filename); - })); - }).catch(function (error) { - /* istanbul ignore if */ - if (error.status !== 404) { - throw error; + function callbackify(fun) { + return getArguments(function (args) { + var cb = args.pop(); + var promise = fun.apply(this, args); + if (typeof cb === 'function') { + promisedCallback(promise, cb); } - - return getDocAttachments(src, doc); + return promise; }); } - function createBulkGetOpts(diffs) { - var requests = []; - Object.keys(diffs).forEach(function (id) { - var missingRevs = diffs[id].missing; - missingRevs.forEach(function (missingRev) { - requests.push({ - id: id, - rev: missingRev - }); + // Promise finally util similar to Q.finally + function fin(promise, finalPromiseFactory) { + return promise.then(function (res) { + return finalPromiseFactory().then(function () { + return res; + }); + }, function (reason) { + return finalPromiseFactory().then(function () { + throw reason; }); }); + } - return { - docs: requests, - revs: true, - latest: true + function sequentialize(queue, promiseFactory) { + return function () { + var args = arguments; + var that = this; + return queue.add(function () { + return promiseFactory.apply(that, args); + }); }; } - // - // Fetch all the documents from the src as described in the "diffs", - // which is a mapping of docs IDs to revisions. If the state ever - // changes to "cancelled", then the returned promise will be rejected. - // Else it will be resolved with a list of fetched documents. - // - function getDocs(src, target, diffs, state) { - diffs = clone(diffs); // we do not need to modify this - - var resultDocs = [], - ok = true; - - function getAllDocs() { - - var bulkGetOpts = createBulkGetOpts(diffs); - - if (!bulkGetOpts.docs.length) { // optimization: skip empty requests - return; - } - - return src.bulkGet(bulkGetOpts).then(function (bulkGetResponse) { - /* istanbul ignore if */ - if (state.cancelled) { - throw new Error('cancelled'); - } - return PouchPromise.all(bulkGetResponse.results.map(function (bulkGetInfo) { - return PouchPromise.all(bulkGetInfo.docs.map(function (doc) { - var remoteDoc = doc.ok; - - if (doc.error) { - // when AUTO_COMPACTION is set, docs can be returned which look - // like this: {"missing":"1-7c3ac256b693c462af8442f992b83696"} - ok = false; - } - - if (!remoteDoc || !remoteDoc._attachments) { - return remoteDoc; - } - - return getDocAttachmentsFromTargetOrSource(target, src, remoteDoc) - .then(function (attachments) { - var filenames = Object.keys(remoteDoc._attachments); - attachments - .forEach(function (attachment, i) { - var att = remoteDoc._attachments[filenames[i]]; - delete att.stub; - delete att.length; - att.data = attachment; - }); - - return remoteDoc; - }); - })); - })) - - .then(function (results) { - resultDocs = resultDocs.concat(flatten(results).filter(Boolean)); - }); - }); - } + // uniq an array of strings, order not guaranteed + // similar to underscore/lodash _.uniq + function uniq(arr) { + var theSet = new ExportedSet(arr); + var result = new Array(theSet.size); + var index = -1; + theSet.forEach(function (value) { + result[++index] = value; + }); + return result; + } - function hasAttachments(doc) { - return doc._attachments && Object.keys(doc._attachments).length > 0; - } + function mapToKeysArray(map) { + var result = new Array(map.size); + var index = -1; + map.forEach(function (value, key) { + result[++index] = key; + }); + return result; + } - function hasConflicts(doc) { - return doc._conflicts && doc._conflicts.length > 0; - } + var persistentQueues = {}; + var tempViewQueue = new TaskQueue$2(); + var CHANGES_BATCH_SIZE$1 = 50; - function fetchRevisionOneDocs(ids) { - // Optimization: fetch gen-1 docs and attachments in - // a single request using _all_docs - return src.allDocs({ - keys: ids, - include_docs: true, - conflicts: true - }).then(function (res) { - if (state.cancelled) { - throw new Error('cancelled'); - } - res.rows.forEach(function (row) { - if (row.deleted || !row.doc || !isGenOne$1(row.value.rev) || - hasAttachments(row.doc) || hasConflicts(row.doc)) { - // if any of these conditions apply, we need to fetch using get() - return; - } + function parseViewName(name) { + // can be either 'ddocname/viewname' or just 'viewname' + // (where the ddoc name is the same) + return name.indexOf('/') === -1 ? [name, name] : name.split('/'); + } - // strip _conflicts array to appease CSG (#5793) - /* istanbul ignore if */ - if (row.doc._conflicts) { - delete row.doc._conflicts; - } + function isGenOne(changes) { + // only return true if the current change is 1- + // and there are no other leafs + return changes.length === 1 && /^1-/.test(changes[0].rev); + } - // the doc we got back from allDocs() is sufficient - resultDocs.push(row.doc); - delete diffs[row.id]; - }); - }); + function emitError(db, e) { + try { + db.emit('error', e); + } catch (err) { + guardedConsole('error', + 'The user\'s map/reduce function threw an uncaught error.\n' + + 'You can debug this error by doing:\n' + + 'myDatabase.on(\'error\', function (err) { debugger; });\n' + + 'Please double-check your map/reduce function.'); + guardedConsole('error', e); } - - 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(diffs).filter(function (id) { - var missing = diffs[id].missing; - return missing.length === 1 && isGenOne$1(missing[0]); - }); - if (ids.length > 0) { - return fetchRevisionOneDocs(ids); - } + } + function tryMap(db, fun, doc) { + // emit an event if there was an error thrown by a map function. + // putting try/catches in a single function also avoids deoptimizations. + try { + fun(doc); + } catch (e) { + emitError(db, e); } + } - function returnResult() { - return { ok:ok, docs:resultDocs }; + function tryReduce(db, fun, keys, values, rereduce) { + // same as above, but returning the result or an error. there are two separate + // functions to avoid extra memory allocations since the tryCode() case is used + // for custom map functions (common) vs this function, which is only used for + // custom reduce functions (rare) + try { + return {output : fun(keys, values, rereduce)}; + } catch (e) { + emitError(db, e); + return {error: e}; } + } - return PouchPromise.resolve() - .then(getRevisionOneDocs) - .then(getAllDocs) - .then(returnResult); + function sortByKeyThenValue(x, y) { + var keyCompare = collate(x.key, y.key); + return keyCompare !== 0 ? keyCompare : collate(x.value, y.value); } - var CHECKPOINT_VERSION = 1; - var REPLICATOR = "pouchdb"; - // This is an arbitrary number to limit the - // amount of replication history we save in the checkpoint. - // If we save too much, the checkpoing docs will become very big, - // if we save fewer, we'll run a greater risk of having to - // read all the changes from 0 when checkpoint PUTs fail - // CouchDB 2.0 has a more involved history pruning, - // but let's go for the simple version for now. - var CHECKPOINT_HISTORY_SIZE = 5; - var LOWEST_SEQ = 0; + function sliceResults(results, limit, skip) { + skip = skip || 0; + if (typeof limit === 'number') { + return results.slice(skip, limit + skip); + } else if (skip > 0) { + return results.slice(skip); + } + return results; + } - function updateCheckpoint(db, id, checkpoint, session, returnValue) { - return db.get(id).catch(function (err) { - if (err.status === 404) { - if (db.adapter === 'http' || db.adapter === 'https') { - explainError( - 404, 'PouchDB is just checking if a remote checkpoint exists.' - ); - } - return { - session_id: session, - _id: id, - history: [], - replicator: REPLICATOR, - version: CHECKPOINT_VERSION - }; - } - throw err; - }).then(function (doc) { - if (returnValue.cancelled) { - return; - } + function rowToDocId(row) { + var val = row.value; + // Users can explicitly specify a joined doc _id, or it + // defaults to the doc _id that emitted the key/value. + var docId = (val && typeof val === 'object' && val._id) || row.id; + return docId; + } - // if the checkpoint has not changed, do not update - if (doc.last_seq === checkpoint) { + function readAttachmentsAsBlobOrBuffer$1(res) { + res.rows.forEach(function (row) { + var atts = row.doc && row.doc._attachments; + if (!atts) { return; } - - // Filter out current entry for this replication - doc.history = (doc.history || []).filter(function (item) { - return item.session_id !== session; - }); - - // Add the latest checkpoint to history - doc.history.unshift({ - last_seq: checkpoint, - session_id: session - }); - - // Just take the last pieces in history, to - // avoid really big checkpoint docs. - // see comment on history size above - doc.history = doc.history.slice(0, CHECKPOINT_HISTORY_SIZE); - - doc.version = CHECKPOINT_VERSION; - doc.replicator = REPLICATOR; - - doc.session_id = session; - doc.last_seq = checkpoint; - - return db.put(doc).catch(function (err) { - if (err.status === 409) { - // retry; someone is trying to write a checkpoint simultaneously - return updateCheckpoint(db, id, checkpoint, session, returnValue); - } - throw err; + Object.keys(atts).forEach(function (filename) { + var att = atts[filename]; + atts[filename].data = b64ToBluffer(att.data, att.content_type); }); }); } - function Checkpointer(src, target, id, returnValue, opts) { - this.src = src; - this.target = target; - this.id = id; - this.returnValue = returnValue; - this.opts = opts || {}; + function postprocessAttachments(opts) { + return function (res) { + if (opts.include_docs && opts.attachments && opts.binary) { + readAttachmentsAsBlobOrBuffer$1(res); + } + return res; + }; } - Checkpointer.prototype.writeCheckpoint = function (checkpoint, session) { - var self = this; - return this.updateTarget(checkpoint, session).then(function () { - return self.updateSource(checkpoint, session); - }); - }; + var builtInReduce = { + _sum: function (keys, values) { + return sum(values); + }, - Checkpointer.prototype.updateTarget = function (checkpoint, session) { - if (this.opts.writeTargetCheckpoint) { - return updateCheckpoint(this.target, this.id, checkpoint, - session, this.returnValue); - } else { - return PouchPromise.resolve(true); + _count: function (keys, values) { + return values.length; + }, + + _stats: function (keys, values) { + // no need to implement rereduce=true, because Pouch + // will never call it + function sumsqr(values) { + var _sumsqr = 0; + for (var i = 0, len = values.length; i < len; i++) { + var num = values[i]; + _sumsqr += (num * num); + } + return _sumsqr; + } + return { + sum : sum(values), + min : Math.min.apply(null, values), + max : Math.max.apply(null, values), + count : values.length, + sumsqr : sumsqr(values) + }; } }; - Checkpointer.prototype.updateSource = function (checkpoint, session) { - if (this.opts.writeSourceCheckpoint) { - var self = this; - return updateCheckpoint(this.src, this.id, checkpoint, - session, this.returnValue) - .catch(function (err) { - if (isForbiddenError(err)) { - self.opts.writeSourceCheckpoint = false; - return true; - } - throw err; - }); - } else { - return PouchPromise.resolve(true); + function addHttpParam(paramName, opts, params, asJson) { + // add an http param from opts to params, optionally json-encoded + var val = opts[paramName]; + if (typeof val !== 'undefined') { + if (asJson) { + val = encodeURIComponent(JSON.stringify(val)); + } + params.push(paramName + '=' + val); } - }; + } - var comparisons = { - "undefined": function (targetDoc, sourceDoc) { - // This is the previous comparison function - if (collate(targetDoc.last_seq, sourceDoc.last_seq) === 0) { - return sourceDoc.last_seq; + function coerceInteger(integerCandidate) { + if (typeof integerCandidate !== 'undefined') { + var asNumber = Number(integerCandidate); + // prevents e.g. '1foo' or '1.1' being coerced to 1 + if (!isNaN(asNumber) && asNumber === parseInt(integerCandidate, 10)) { + return asNumber; + } else { + return integerCandidate; } - /* istanbul ignore next */ - return 0; - }, - "1": function (targetDoc, sourceDoc) { - // This is the comparison function ported from CouchDB - return compareReplicationLogs(sourceDoc, targetDoc).last_seq; } - }; + } - Checkpointer.prototype.getCheckpoint = function () { - var self = this; + function coerceOptions(opts) { + opts.group_level = coerceInteger(opts.group_level); + opts.limit = coerceInteger(opts.limit); + opts.skip = coerceInteger(opts.skip); + return opts; + } - if (self.opts && self.opts.writeSourceCheckpoint && !self.opts.writeTargetCheckpoint) { - return self.src.get(self.id).then(function (sourceDoc) { - return sourceDoc.last_seq || LOWEST_SEQ; - }).catch(function (err) { - /* istanbul ignore if */ - if (err.status !== 404) { - throw err; - } - return LOWEST_SEQ; - }); + function checkPositiveInteger(number) { + if (number) { + if (typeof number !== 'number') { + return new QueryParseError('Invalid value for integer: "' + + number + '"'); + } + if (number < 0) { + return new QueryParseError('Invalid value for positive integer: ' + + '"' + number + '"'); + } } + } - return self.target.get(self.id).then(function (targetDoc) { - if (self.opts && self.opts.writeTargetCheckpoint && !self.opts.writeSourceCheckpoint) { - return targetDoc.last_seq || LOWEST_SEQ; + function checkQueryParseError(options, fun) { + var startkeyName = options.descending ? 'endkey' : 'startkey'; + var endkeyName = options.descending ? 'startkey' : 'endkey'; + + if (typeof options[startkeyName] !== 'undefined' && + typeof options[endkeyName] !== 'undefined' && + collate(options[startkeyName], options[endkeyName]) > 0) { + throw new QueryParseError('No rows can match your key range, ' + + 'reverse your start_key and end_key or set {descending : true}'); + } else if (fun.reduce && options.reduce !== false) { + if (options.include_docs) { + throw new QueryParseError('{include_docs:true} is invalid for reduce'); + } else if (options.keys && options.keys.length > 1 && + !options.group && !options.group_level) { + throw new QueryParseError('Multi-key fetches for reduce views must use ' + + '{group: true}'); + } + } + ['group_level', 'limit', 'skip'].forEach(function (optionName) { + var error = checkPositiveInteger(options[optionName]); + if (error) { + throw error; } + }); + } - return self.src.get(self.id).then(function (sourceDoc) { - // Since we can't migrate an old version doc to a new one - // (no session id), we just go with the lowest seq in this case - /* istanbul ignore if */ - if (targetDoc.version !== sourceDoc.version) { - return LOWEST_SEQ; + function httpQuery(db, fun, opts) { + // List of parameters to add to the PUT request + var params = []; + var body; + var method = 'GET'; + + // If opts.reduce exists and is defined, then add it to the list + // of parameters. + // If reduce=false then the results are that of only the map function + // not the final result of map and reduce. + addHttpParam('reduce', opts, params); + addHttpParam('include_docs', opts, params); + addHttpParam('attachments', opts, params); + addHttpParam('limit', opts, params); + addHttpParam('descending', opts, params); + addHttpParam('group', opts, params); + addHttpParam('group_level', opts, params); + addHttpParam('skip', opts, params); + addHttpParam('stale', opts, params); + addHttpParam('conflicts', opts, params); + addHttpParam('startkey', opts, params, true); + addHttpParam('start_key', opts, params, true); + addHttpParam('endkey', opts, params, true); + addHttpParam('end_key', opts, params, true); + addHttpParam('inclusive_end', opts, params); + addHttpParam('key', opts, params, true); + + // Format the list of parameters into a valid URI query string + params = params.join('&'); + params = params === '' ? '' : '?' + params; + + // If keys are supplied, issue a POST to circumvent GET query string limits + // see http://wiki.apache.org/couchdb/HTTP_view_API#Querying_Options + if (typeof opts.keys !== 'undefined') { + var MAX_URL_LENGTH = 2000; + // according to http://stackoverflow.com/a/417184/680742, + // the de facto URL length limit is 2000 characters + + var keysAsString = + 'keys=' + encodeURIComponent(JSON.stringify(opts.keys)); + if (keysAsString.length + params.length + 1 <= MAX_URL_LENGTH) { + // If the keys are short enough, do a GET. we do this to work around + // Safari not understanding 304s on POSTs (see pouchdb/pouchdb#1239) + params += (params[0] === '?' ? '&' : '?') + keysAsString; + } else { + method = 'POST'; + if (typeof fun === 'string') { + body = {keys: opts.keys}; + } else { // fun is {map : mapfun}, so append to this + fun.keys = opts.keys; } + } + } - var version; - if (targetDoc.version) { - version = targetDoc.version.toString(); - } else { - version = "undefined"; - } + // We are referencing a query defined in the design doc + if (typeof fun === 'string') { + var parts = parseViewName(fun); + return db.request({ + method: method, + url: '_design/' + parts[0] + '/_view/' + parts[1] + params, + body: body + }).then(postprocessAttachments(opts)); + } - if (version in comparisons) { - return comparisons[version](targetDoc, sourceDoc); - } - /* istanbul ignore next */ - return LOWEST_SEQ; - }, function (err) { - if (err.status === 404 && targetDoc.last_seq) { - return self.src.put({ - _id: self.id, - last_seq: LOWEST_SEQ - }).then(function () { - return LOWEST_SEQ; - }, function (err) { - if (isForbiddenError(err)) { - self.opts.writeSourceCheckpoint = false; - return targetDoc.last_seq; - } - /* istanbul ignore next */ - return LOWEST_SEQ; - }); + // We are using a temporary view, terrible for performance, good for testing + body = body || {}; + Object.keys(fun).forEach(function (key) { + if (Array.isArray(fun[key])) { + body[key] = fun[key]; + } else { + body[key] = fun[key].toString(); + } + }); + return db.request({ + method: 'POST', + url: '_temp_view' + params, + body: body + }).then(postprocessAttachments(opts)); + } + + // custom adapters can define their own api._query + // and override the default behavior + /* istanbul ignore next */ + function customQuery(db, fun, opts) { + return new PouchPromise$1(function (resolve, reject) { + db._query(fun, opts, function (err, res) { + if (err) { + return reject(err); } - throw err; + resolve(res); }); - }).catch(function (err) { - if (err.status !== 404) { - throw err; - } - return LOWEST_SEQ; }); - }; - // This checkpoint comparison is ported from CouchDBs source - // they come from here: - // https://github.com/apache/couchdb-couch-replicator/blob/master/src/couch_replicator.erl#L863-L906 + } - function compareReplicationLogs(srcDoc, tgtDoc) { - if (srcDoc.session_id === tgtDoc.session_id) { - return { - last_seq: srcDoc.last_seq, - history: srcDoc.history - }; - } + // custom adapters can define their own api._viewCleanup + // and override the default behavior + /* istanbul ignore next */ + function customViewCleanup(db) { + return new PouchPromise$1(function (resolve, reject) { + db._viewCleanup(function (err, res) { + if (err) { + return reject(err); + } + resolve(res); + }); + }); + } - return compareReplicationHistory(srcDoc.history, tgtDoc.history); + function defaultsTo(value) { + return function (reason) { + /* istanbul ignore else */ + if (reason.status === 404) { + return value; + } else { + throw reason; + } + }; } - function compareReplicationHistory(sourceHistory, targetHistory) { - // the erlang loop via function arguments is not so easy to repeat in JS - // therefore, doing this as recursion - var S = sourceHistory[0]; - var sourceRest = sourceHistory.slice(1); - var T = targetHistory[0]; - var targetRest = targetHistory.slice(1); + // returns a promise for a list of docs to update, based on the input docId. + // the order doesn't matter, because post-3.2.0, bulkDocs + // is an atomic operation in all three adapters. + function getDocsToPersist(docId, view, docIdsToChangesAndEmits) { + var metaDocId = '_local/doc_' + docId; + var defaultMetaDoc = {_id: metaDocId, keys: []}; + var docData = docIdsToChangesAndEmits.get(docId); + var indexableKeysToKeyValues = docData[0]; + var changes = docData[1]; - if (!S || targetHistory.length === 0) { - return { - last_seq: LOWEST_SEQ, - history: [] - }; + function getMetaDoc() { + if (isGenOne(changes)) { + // generation 1, so we can safely assume initial state + // for performance reasons (avoids unnecessary GETs) + return PouchPromise$1.resolve(defaultMetaDoc); + } + return view.db.get(metaDocId).catch(defaultsTo(defaultMetaDoc)); } - var sourceId = S.session_id; - /* istanbul ignore if */ - if (hasSessionId(sourceId, targetHistory)) { - return { - last_seq: S.last_seq, - history: sourceHistory - }; + function getKeyValueDocs(metaDoc) { + if (!metaDoc.keys.length) { + // no keys, no need for a lookup + return PouchPromise$1.resolve({rows: []}); + } + return view.db.allDocs({ + keys: metaDoc.keys, + include_docs: true + }); } - var targetId = T.session_id; - if (hasSessionId(targetId, sourceRest)) { - return { - last_seq: T.last_seq, - history: targetRest - }; + function processKeyValueDocs(metaDoc, kvDocsRes) { + var kvDocs = []; + var oldKeys = new ExportedSet(); + + for (var i = 0, len = kvDocsRes.rows.length; i < len; i++) { + var row = kvDocsRes.rows[i]; + var doc = row.doc; + if (!doc) { // deleted + continue; + } + kvDocs.push(doc); + oldKeys.add(doc._id); + doc._deleted = !indexableKeysToKeyValues.has(doc._id); + if (!doc._deleted) { + var keyValue = indexableKeysToKeyValues.get(doc._id); + if ('value' in keyValue) { + doc.value = keyValue.value; + } + } + } + var newKeys = mapToKeysArray(indexableKeysToKeyValues); + newKeys.forEach(function (key) { + if (!oldKeys.has(key)) { + // new doc + var kvDoc = { + _id: key + }; + var keyValue = indexableKeysToKeyValues.get(key); + if ('value' in keyValue) { + kvDoc.value = keyValue.value; + } + kvDocs.push(kvDoc); + } + }); + metaDoc.keys = uniq(newKeys.concat(metaDoc.keys)); + kvDocs.push(metaDoc); + + return kvDocs; } - return compareReplicationHistory(sourceRest, targetRest); + return getMetaDoc().then(function (metaDoc) { + return getKeyValueDocs(metaDoc).then(function (kvDocsRes) { + return processKeyValueDocs(metaDoc, kvDocsRes); + }); + }); } - function hasSessionId(sessionId, history) { - var props = history[0]; - var rest = history.slice(1); - - if (!sessionId || history.length === 0) { - return false; - } + // updates all emitted key/value docs and metaDocs in the mrview database + // for the given batch of documents from the source database + function saveKeyValues(view, docIdsToChangesAndEmits, seq) { + var seqDocId = '_local/lastSeq'; + return view.db.get(seqDocId) + .catch(defaultsTo({_id: seqDocId, seq: 0})) + .then(function (lastSeqDoc) { + var docIds = mapToKeysArray(docIdsToChangesAndEmits); + return PouchPromise$1.all(docIds.map(function (docId) { + return getDocsToPersist(docId, view, docIdsToChangesAndEmits); + })).then(function (listOfDocsToPersist) { + var docsToPersist = flatten(listOfDocsToPersist); + lastSeqDoc.seq = seq; + docsToPersist.push(lastSeqDoc); + // write all docs in a single operation, update the seq once + return view.db.bulkDocs({docs : docsToPersist}); + }); + }); + } - if (sessionId === props.session_id) { - return true; + function getQueue(view) { + var viewName = typeof view === 'string' ? view : view.name; + var queue = persistentQueues[viewName]; + if (!queue) { + queue = persistentQueues[viewName] = new TaskQueue$2(); } - - return hasSessionId(sessionId, rest); + return queue; } - function isForbiddenError(err) { - return typeof err.status === 'number' && Math.floor(err.status / 100) === 4; + function updateView(view) { + return sequentialize(getQueue(view), function () { + return updateViewInQueue(view); + })(); } - var STARTING_BACK_OFF = 0; + function updateViewInQueue(view) { + // bind the emit function once + var mapResults; + var doc; - function backOff(opts, returnValue, error, callback) { - if (opts.retry === false) { - returnValue.emit('error', error); - returnValue.removeAllListeners(); - return; - } - if (typeof opts.back_off_function !== 'function') { - opts.back_off_function = defaultBackOff; + function emit(key, value) { + var output = {id: doc._id, key: normalizeKey(key)}; + // Don't explicitly store the value unless it's defined and non-null. + // This saves on storage space, because often people don't use it. + if (typeof value !== 'undefined' && value !== null) { + output.value = normalizeKey(value); + } + mapResults.push(output); } - returnValue.emit('requestError', error); - if (returnValue.state === 'active' || returnValue.state === 'pending') { - returnValue.emit('paused', error); - returnValue.state = 'stopped'; - var backOffSet = function backoffTimeSet() { - opts.current_back_off = STARTING_BACK_OFF; - }; - var removeBackOffSetter = function removeBackOffTimeSet() { - returnValue.removeListener('active', backOffSet); + + var mapFun; + // for temp_views one can use emit(doc, emit), see #38 + if (typeof view.mapFun === "function" && view.mapFun.length === 2) { + var origMap = view.mapFun; + mapFun = function (doc) { + return origMap(doc, emit); }; - returnValue.once('paused', removeBackOffSetter); - returnValue.once('active', backOffSet); + } else { + mapFun = evalFunctionWithEval(view.mapFun.toString(), emit); } - opts.current_back_off = opts.current_back_off || STARTING_BACK_OFF; - opts.current_back_off = opts.back_off_function(opts.current_back_off); - setTimeout(callback, opts.current_back_off); - } + var currentSeq = view.seq || 0; - function sortObjectPropertiesByKey(queryParams) { - return Object.keys(queryParams).sort(collate).reduce(function (result, key) { - result[key] = queryParams[key]; - return result; - }, {}); - } + function processChange(docIdsToChangesAndEmits, seq) { + return function () { + return saveKeyValues(view, docIdsToChangesAndEmits, seq); + }; + } - // Generate a unique id particular to this replication. - // Not guaranteed to align perfectly with CouchDB's rep ids. - function generateReplicationId(src, target, opts) { - var docIds = opts.doc_ids ? opts.doc_ids.sort(collate) : ''; - var filterFun = opts.filter ? opts.filter.toString() : ''; - var queryParams = ''; - var filterViewName = ''; - var selector = ''; + var queue = new TaskQueue$2(); - // possibility for checkpoints to be lost here as behaviour of - // JSON.stringify is not stable (see #6226) - /* istanbul ignore if */ - if (opts.selector) { - selector = JSON.stringify(opts.selector); + function processNextBatch() { + return view.sourceDB.changes({ + conflicts: true, + include_docs: true, + style: 'all_docs', + since: currentSeq, + limit: CHANGES_BATCH_SIZE$1 + }).then(processBatch); } - if (opts.filter && opts.query_params) { - queryParams = JSON.stringify(sortObjectPropertiesByKey(opts.query_params)); + function processBatch(response) { + var results = response.results; + if (!results.length) { + return; + } + var docIdsToChangesAndEmits = createDocIdsToChangesAndEmits(results); + queue.add(processChange(docIdsToChangesAndEmits, currentSeq)); + if (results.length < CHANGES_BATCH_SIZE$1) { + return; + } + return processNextBatch(); } - if (opts.filter && opts.filter === '_view') { - filterViewName = opts.view.toString(); + function createDocIdsToChangesAndEmits(results) { + var docIdsToChangesAndEmits = new ExportedMap(); + for (var i = 0, len = results.length; i < len; i++) { + var change = results[i]; + if (change.doc._id[0] !== '_') { + mapResults = []; + doc = change.doc; + + if (!doc._deleted) { + tryMap(view.sourceDB, mapFun, doc); + } + mapResults.sort(sortByKeyThenValue); + + var indexableKeysToKeyValues = createIndexableKeysToKeyValues(mapResults); + docIdsToChangesAndEmits.set(change.doc._id, [ + indexableKeysToKeyValues, + change.changes + ]); + } + currentSeq = change.seq; + } + return docIdsToChangesAndEmits; } - return PouchPromise.all([src.id(), target.id()]).then(function (res) { - var queryData = res[0] + res[1] + filterFun + filterViewName + - queryParams + docIds + selector; - return new PouchPromise(function (resolve) { - binaryMd5(queryData, resolve); - }); - }).then(function (md5sum) { - // can't use straight-up md5 alphabet, because - // the char '/' is interpreted as being for attachments, - // and + is also not url-safe - md5sum = md5sum.replace(/\//g, '.').replace(/\+/g, '_'); - return '_local/' + md5sum; + function createIndexableKeysToKeyValues(mapResults) { + var indexableKeysToKeyValues = new ExportedMap(); + var lastKey; + for (var i = 0, len = mapResults.length; i < len; i++) { + var emittedKeyValue = mapResults[i]; + var complexKey = [emittedKeyValue.key, emittedKeyValue.id]; + if (i > 0 && collate(emittedKeyValue.key, lastKey) === 0) { + complexKey.push(i); // dup key+id, so make it unique + } + indexableKeysToKeyValues.set(toIndexableString(complexKey), emittedKeyValue); + lastKey = emittedKeyValue.key; + } + return indexableKeysToKeyValues; + } + + return processNextBatch().then(function () { + return queue.finish(); + }).then(function () { + view.seq = currentSeq; }); } - function replicate(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 selector = opts.selector; - var repId; - var checkpointer; - var changedDocs = []; - // Like couchdb, every replication gets a unique session id - var session = uuid(); - var seq_interval = opts.seq_interval; + function reduceView(view, results, options) { + if (options.group_level === 0) { + delete options.group_level; + } - result = result || { - ok: true, - start_time: new Date(), - docs_read: 0, - docs_written: 0, - doc_write_failures: 0, - errors: [] - }; + var shouldGroup = options.group || options.group_level; - var changesOpts = {}; - returnValue.ready(src, target); + var reduceFun; + if (builtInReduce[view.reduceFun]) { + reduceFun = builtInReduce[view.reduceFun]; + } else { + reduceFun = evalFunctionWithEval(view.reduceFun.toString()); + } - function initCheckpointer() { - if (checkpointer) { - return PouchPromise.resolve(); - } - return generateReplicationId(src, target, opts).then(function (res) { - repId = res; + var groups = []; + var lvl = isNaN(options.group_level) ? Number.POSITIVE_INFINITY : + options.group_level; + results.forEach(function (e) { + var last = groups[groups.length - 1]; + var groupKey = shouldGroup ? e.key : null; - var checkpointOpts = {}; - if (opts.checkpoint === false) { - checkpointOpts = { writeSourceCheckpoint: false, writeTargetCheckpoint: false }; - } else if (opts.checkpoint === 'source') { - checkpointOpts = { writeSourceCheckpoint: true, writeTargetCheckpoint: false }; - } else if (opts.checkpoint === 'target') { - checkpointOpts = { writeSourceCheckpoint: false, writeTargetCheckpoint: true }; - } else { - checkpointOpts = { writeSourceCheckpoint: true, writeTargetCheckpoint: true }; - } + // only set group_level for array keys + if (shouldGroup && Array.isArray(groupKey)) { + groupKey = groupKey.slice(0, lvl); + } - checkpointer = new Checkpointer(src, target, repId, returnValue, checkpointOpts); + if (last && collate(last.groupKey, groupKey) === 0) { + last.keys.push([e.key, e.id]); + last.values.push(e.value); + return; + } + groups.push({ + keys: [[e.key, e.id]], + values: [e.value], + groupKey: groupKey + }); + }); + results = []; + for (var i = 0, len = groups.length; i < len; i++) { + var e = groups[i]; + var reduceTry = tryReduce(view.sourceDB, reduceFun, e.keys, e.values, false); + if (reduceTry.error && reduceTry.error instanceof BuiltInError) { + // CouchDB returns an error if a built-in errors out + throw reduceTry.error; + } + results.push({ + // CouchDB just sets the value to null if a non-built-in errors out + value: reduceTry.error ? null : reduceTry.output, + key: e.groupKey }); } + // no total_rows/offset when reducing + return {rows: sliceResults(results, options.limit, options.skip)}; + } - function writeDocs() { - changedDocs = []; + function queryView(view, opts) { + return sequentialize(getQueue(view), function () { + return queryViewInQueue(view, opts); + })(); + } - if (currentBatch.docs.length === 0) { - return; - } - var docs = currentBatch.docs; - var bulkOpts = {timeout: opts.timeout}; - return target.bulkDocs({docs: docs, new_edits: false}, bulkOpts).then(function (res) { - /* istanbul ignore if */ - if (returnValue.cancelled) { - completeReplication(); - throw new Error('cancelled'); - } + function queryViewInQueue(view, opts) { + var totalRows; + var shouldReduce = view.reduceFun && opts.reduce !== false; + var skip = opts.skip || 0; + if (typeof opts.keys !== 'undefined' && !opts.keys.length) { + // equivalent query + opts.limit = 0; + delete opts.keys; + } - // `res` doesn't include full documents (which live in `docs`), so we create a map of - // (id -> error), and check for errors while iterating over `docs` - var errorsById = Object.create(null); - res.forEach(function (res) { - if (res.error) { - errorsById[res.id] = res; + function fetchFromView(viewOpts) { + viewOpts.include_docs = true; + return view.db.allDocs(viewOpts).then(function (res) { + totalRows = res.total_rows; + return res.rows.map(function (result) { + + // implicit migration - in older versions of PouchDB, + // we explicitly stored the doc as {id: ..., key: ..., value: ...} + // this is tested in a migration test + /* istanbul ignore next */ + if ('value' in result.doc && typeof result.doc.value === 'object' && + result.doc.value !== null) { + var keys = Object.keys(result.doc.value).sort(); + // this detection method is not perfect, but it's unlikely the user + // emitted a value which was an object with these 3 exact keys + var expectedKeys = ['id', 'key', 'value']; + if (!(keys < expectedKeys || keys > expectedKeys)) { + return result.doc.value; + } } - }); - var errorsNo = Object.keys(errorsById).length; - result.doc_write_failures += errorsNo; - result.docs_written += docs.length - errorsNo; + var parsedKeyAndDocId = parseIndexableString(result.doc._id); + return { + key: parsedKeyAndDocId[0], + id: parsedKeyAndDocId[1], + value: ('value' in result.doc ? result.doc.value : null) + }; + }); + }); + } - docs.forEach(function (doc) { - var error = errorsById[doc._id]; - if (error) { - result.errors.push(error); - // Normalize error name. i.e. 'Unauthorized' -> 'unauthorized' (eg Sync Gateway) - var errorName = (error.name || '').toLowerCase(); - if (errorName === 'unauthorized' || errorName === 'forbidden') { - returnValue.emit('denied', clone(error)); - } else { - throw error; + function onMapResultsReady(rows) { + var finalResults; + if (shouldReduce) { + finalResults = reduceView(view, rows, opts); + } else { + finalResults = { + total_rows: totalRows, + offset: skip, + rows: rows + }; + } + if (opts.include_docs) { + var docIds = uniq(rows.map(rowToDocId)); + + return view.sourceDB.allDocs({ + keys: docIds, + include_docs: true, + conflicts: opts.conflicts, + attachments: opts.attachments, + binary: opts.binary + }).then(function (allDocsRes) { + var docIdsToDocs = new ExportedMap(); + allDocsRes.rows.forEach(function (row) { + docIdsToDocs.set(row.id, row.doc); + }); + rows.forEach(function (row) { + var docId = rowToDocId(row); + var doc = docIdsToDocs.get(docId); + if (doc) { + row.doc = doc; } - } else { - changedDocs.push(doc); - } + }); + return finalResults; }); + } else { + return finalResults; + } + } - }, function (err) { - result.doc_write_failures += docs.length; - throw err; + if (typeof opts.keys !== 'undefined') { + var keys = opts.keys; + var fetchPromises = keys.map(function (key) { + var viewOpts = { + startkey : toIndexableString([key]), + endkey : toIndexableString([key, {}]) + }; + return fetchFromView(viewOpts); }); - } + return PouchPromise$1.all(fetchPromises).then(flatten).then(onMapResultsReady); + } else { // normal query, no 'keys' + var viewOpts = { + descending : opts.descending + }; + if (opts.start_key) { + opts.startkey = opts.start_key; + } + if (opts.end_key) { + opts.endkey = opts.end_key; + } + if (typeof opts.startkey !== 'undefined') { + viewOpts.startkey = opts.descending ? + toIndexableString([opts.startkey, {}]) : + toIndexableString([opts.startkey]); + } + if (typeof opts.endkey !== 'undefined') { + var inclusiveEnd = opts.inclusive_end !== false; + if (opts.descending) { + inclusiveEnd = !inclusiveEnd; + } - function finishBatch() { - if (currentBatch.error) { - throw new Error('There was a problem getting docs.'); + viewOpts.endkey = toIndexableString( + inclusiveEnd ? [opts.endkey, {}] : [opts.endkey]); } - result.last_seq = last_seq = currentBatch.seq; - var outResult = clone(result); - if (changedDocs.length) { - outResult.docs = changedDocs; - // Attach 'pending' property if server supports it (CouchDB 2.0+) - /* istanbul ignore if */ - if (typeof currentBatch.pending === 'number') { - outResult.pending = currentBatch.pending; - delete currentBatch.pending; + if (typeof opts.key !== 'undefined') { + var keyStart = toIndexableString([opts.key]); + var keyEnd = toIndexableString([opts.key, {}]); + if (viewOpts.descending) { + viewOpts.endkey = keyStart; + viewOpts.startkey = keyEnd; + } else { + viewOpts.startkey = keyStart; + viewOpts.endkey = keyEnd; } - returnValue.emit('change', outResult); } - writingCheckpoint = true; - return checkpointer.writeCheckpoint(currentBatch.seq, - session).then(function () { - writingCheckpoint = false; - /* istanbul ignore if */ - if (returnValue.cancelled) { - completeReplication(); - throw new Error('cancelled'); + if (!shouldReduce) { + if (typeof opts.limit === 'number') { + viewOpts.limit = opts.limit; } - currentBatch = undefined; - getChanges(); - }).catch(function (err) { - onCheckpointError(err); - throw err; - }); + viewOpts.skip = skip; + } + return fetchFromView(viewOpts).then(onMapResultsReady); } + } - function getDiffs() { - var diff = {}; - currentBatch.changes.forEach(function (change) { - // Couchbase Sync Gateway emits these, but we can ignore them - /* istanbul ignore if */ - if (change.id === "_user/") { - return; + function httpViewCleanup(db) { + return db.request({ + method: 'POST', + url: '_view_cleanup' + }); + } + + function localViewCleanup(db) { + return db.get('_local/mrviews').then(function (metaDoc) { + var docsToViews = new ExportedMap(); + Object.keys(metaDoc.views).forEach(function (fullViewName) { + var parts = parseViewName(fullViewName); + var designDocName = '_design/' + parts[0]; + var viewName = parts[1]; + var views = docsToViews.get(designDocName); + if (!views) { + views = new ExportedSet(); + docsToViews.set(designDocName, views); } - diff[change.id] = change.changes.map(function (x) { - return x.rev; - }); + views.add(viewName); }); - return target.revsDiff(diff).then(function (diffs) { - /* istanbul ignore if */ - if (returnValue.cancelled) { - completeReplication(); - throw new Error('cancelled'); - } - // currentBatch.diffs elements are deleted as the documents are written - currentBatch.diffs = diffs; + var opts = { + keys : mapToKeysArray(docsToViews), + include_docs : true + }; + return db.allDocs(opts).then(function (res) { + var viewsToStatus = {}; + res.rows.forEach(function (row) { + var ddocName = row.key.substring(8); // cuts off '_design/' + docsToViews.get(row.key).forEach(function (viewName) { + var fullViewName = ddocName + '/' + viewName; + /* istanbul ignore if */ + if (!metaDoc.views[fullViewName]) { + // new format, without slashes, to support PouchDB 2.2.0 + // migration test in pouchdb's browser.migration.js verifies this + fullViewName = viewName; + } + var viewDBNames = Object.keys(metaDoc.views[fullViewName]); + // design doc deleted, or view function nonexistent + var statusIsGood = row.doc && row.doc.views && + row.doc.views[viewName]; + viewDBNames.forEach(function (viewDBName) { + viewsToStatus[viewDBName] = + viewsToStatus[viewDBName] || statusIsGood; + }); + }); + }); + var dbsToDelete = Object.keys(viewsToStatus).filter( + function (viewDBName) { return !viewsToStatus[viewDBName]; }); + var destroyPromises = dbsToDelete.map(function (viewDBName) { + return sequentialize(getQueue(viewDBName), function () { + return new db.constructor(viewDBName, db.__opts).destroy(); + })(); + }); + return PouchPromise$1.all(destroyPromises).then(function () { + return {ok: true}; + }); }); + }, defaultsTo({ok: true})); + } + + var viewCleanup = callbackify(function () { + var db = this; + if (db.type() === 'http') { + return httpViewCleanup(db); + } + /* istanbul ignore next */ + if (typeof db._viewCleanup === 'function') { + return customViewCleanup(db); } + return localViewCleanup(db); + }); - function getBatchDocs() { - return getDocs(src, target, currentBatch.diffs, returnValue).then(function (got) { - currentBatch.error = !got.ok; - got.docs.forEach(function (doc) { - delete currentBatch.diffs[doc._id]; - result.docs_read++; - currentBatch.docs.push(doc); - }); - }); + function queryPromised(db, fun, opts) { + if (db.type() === 'http') { + return httpQuery(db, fun, opts); } - function startNextBatch() { - if (returnValue.cancelled || currentBatch) { - return; - } - if (batches.length === 0) { - processPendingBatch(true); - return; - } - currentBatch = batches.shift(); - getDiffs() - .then(getBatchDocs) - .then(writeDocs) - .then(finishBatch) - .then(startNextBatch) - .catch(function (err) { - abortReplication('batch processing terminated with error', err); - }); + /* istanbul ignore next */ + if (typeof db._query === 'function') { + return customQuery(db, fun, opts); } + if (typeof fun !== 'string') { + // temp_view + checkQueryParseError(opts, fun); - function processPendingBatch(immediate) { - if (pendingBatch.changes.length === 0) { - if (batches.length === 0 && !currentBatch) { - if ((continuous && changesOpts.live) || changesCompleted) { - returnValue.state = 'pending'; - returnValue.emit('paused'); - } - if (changesCompleted) { - completeReplication(); + var createViewOpts = { + db : db, + viewName : 'temp_view/temp_view', + map : fun.map, + reduce : fun.reduce, + temporary : true + }; + tempViewQueue.add(function () { + return createView(createViewOpts).then(function (view) { + function cleanup() { + return view.db.destroy(); } + return fin(updateView(view).then(function () { + return queryView(view, opts); + }), cleanup); + }); + }); + return tempViewQueue.finish(); + } else { + // persistent view + var fullViewName = fun; + var parts = parseViewName(fullViewName); + var designDocName = parts[0]; + var viewName = parts[1]; + return db.get('_design/' + designDocName).then(function (doc) { + var fun = doc.views && doc.views[viewName]; + + if (!fun || typeof fun.map !== 'string') { + throw new NotFoundError('ddoc ' + designDocName + + ' has no view named ' + viewName); } - return; - } - if ( - immediate || - changesCompleted || - pendingBatch.changes.length >= batch_size - ) { - batches.push(pendingBatch); - pendingBatch = { - seq: 0, - changes: [], - docs: [] + checkQueryParseError(opts, fun); + + var createViewOpts = { + db : db, + viewName : fullViewName, + map : fun.map, + reduce : fun.reduce }; - if (returnValue.state === 'pending' || returnValue.state === 'stopped') { - returnValue.state = 'active'; - returnValue.emit('active'); - } - startNextBatch(); - } + return createView(createViewOpts).then(function (view) { + if (opts.stale === 'ok' || opts.stale === 'update_after') { + if (opts.stale === 'update_after') { + nextTick(function () { + updateView(view); + }); + } + return queryView(view, opts); + } else { // stale not ok + return updateView(view).then(function () { + return queryView(view, opts); + }); + } + }); + }); } + } + var query = function (fun, opts, callback) { + if (typeof opts === 'function') { + callback = opts; + opts = {}; + } + opts = opts ? coerceOptions(opts) : {}; - function abortReplication(reason, err) { - if (replicationCompleted) { - return; - } - if (!err.message) { - err.message = reason; - } - result.ok = false; - result.status = 'aborting'; - batches = []; - pendingBatch = { - seq: 0, - changes: [], - docs: [] - }; - completeReplication(err); + if (typeof fun === 'function') { + fun = {map : fun}; + } + + var db = this; + var promise = PouchPromise$1.resolve().then(function () { + return queryPromised(db, fun, opts); + }); + promisedCallback(promise, callback); + return promise; + }; + + + var mapreduce = { + query: query, + viewCleanup: viewCleanup + }; + + function isGenOne$1(rev) { + return /^1-/.test(rev); + } + + function fileHasChanged(localDoc, remoteDoc, filename) { + return !localDoc._attachments || + !localDoc._attachments[filename] || + localDoc._attachments[filename].digest !== remoteDoc._attachments[filename].digest; + } + + function getDocAttachments(db, doc) { + var filenames = Object.keys(doc._attachments); + return PouchPromise$1.all(filenames.map(function (filename) { + return db.getAttachment(doc._id, filename, {rev: doc._rev}); + })); + } + + function getDocAttachmentsFromTargetOrSource(target, src, doc) { + var doCheckForLocalAttachments = src.type() === 'http' && target.type() !== 'http'; + var filenames = Object.keys(doc._attachments); + + if (!doCheckForLocalAttachments) { + return getDocAttachments(src, doc); } + return target.get(doc._id).then(function (localDoc) { + return PouchPromise$1.all(filenames.map(function (filename) { + if (fileHasChanged(localDoc, doc, filename)) { + return src.getAttachment(doc._id, filename); + } + + return target.getAttachment(localDoc._id, filename); + })); + }).catch(function (error) { + /* istanbul ignore if */ + if (error.status !== 404) { + throw error; + } + + return getDocAttachments(src, doc); + }); + } + + function createBulkGetOpts(diffs) { + var requests = []; + Object.keys(diffs).forEach(function (id) { + var missingRevs = diffs[id].missing; + missingRevs.forEach(function (missingRev) { + requests.push({ + id: id, + rev: missingRev + }); + }); + }); + + return { + docs: requests, + revs: true, + latest: true + }; + } + + // + // Fetch all the documents from the src as described in the "diffs", + // which is a mapping of docs IDs to revisions. If the state ever + // changes to "cancelled", then the returned promise will be rejected. + // Else it will be resolved with a list of fetched documents. + // + function getDocs(src, target, diffs, state) { + diffs = clone(diffs); // we do not need to modify this + + var resultDocs = [], + ok = true; - function completeReplication(fatalError) { - if (replicationCompleted) { + function getAllDocs() { + + var bulkGetOpts = createBulkGetOpts(diffs); + + if (!bulkGetOpts.docs.length) { // optimization: skip empty requests return; } - /* istanbul ignore if */ - if (returnValue.cancelled) { - result.status = 'cancelled'; - if (writingCheckpoint) { - return; + + return src.bulkGet(bulkGetOpts).then(function (bulkGetResponse) { + /* istanbul ignore if */ + if (state.cancelled) { + throw new Error('cancelled'); } - } - result.status = result.status || 'complete'; - result.end_time = new Date(); - result.last_seq = last_seq; - replicationCompleted = true; + return PouchPromise$1.all(bulkGetResponse.results.map(function (bulkGetInfo) { + return PouchPromise$1.all(bulkGetInfo.docs.map(function (doc) { + var remoteDoc = doc.ok; - if (fatalError) { - // need to extend the error because Firefox considers ".result" read-only - fatalError = createError(fatalError); - fatalError.result = result; + if (doc.error) { + // when AUTO_COMPACTION is set, docs can be returned which look + // like this: {"missing":"1-7c3ac256b693c462af8442f992b83696"} + ok = false; + } - // Normalize error name. i.e. 'Unauthorized' -> 'unauthorized' (eg Sync Gateway) - var errorName = (fatalError.name || '').toLowerCase(); - if (errorName === 'unauthorized' || errorName === 'forbidden') { - returnValue.emit('error', fatalError); - returnValue.removeAllListeners(); - } else { - backOff(opts, returnValue, fatalError, function () { - replicate(src, target, opts, returnValue); - }); - } - } else { - returnValue.emit('complete', result); - returnValue.removeAllListeners(); - } - } + if (!remoteDoc || !remoteDoc._attachments) { + return remoteDoc; + } + return getDocAttachmentsFromTargetOrSource(target, src, remoteDoc).then(function (attachments) { + var filenames = Object.keys(remoteDoc._attachments); + attachments.forEach(function (attachment, i) { + var att = remoteDoc._attachments[filenames[i]]; + delete att.stub; + delete att.length; + att.data = attachment; + }); - function onChange(change, pending, lastSeq) { - /* istanbul ignore if */ - if (returnValue.cancelled) { - return completeReplication(); - } - // Attach 'pending' property if server supports it (CouchDB 2.0+) - /* istanbul ignore if */ - if (typeof pending === 'number') { - pendingBatch.pending = pending; - } + return remoteDoc; + }); + })); + })) - var filter = filterChange(opts)(change); - if (!filter) { - return; - } - pendingBatch.seq = change.seq || lastSeq; - pendingBatch.changes.push(change); - processPendingBatch(batches.length === 0 && changesOpts.live); + .then(function (results) { + resultDocs = resultDocs.concat(flatten(results).filter(Boolean)); + }); + }); } + function hasAttachments(doc) { + return doc._attachments && Object.keys(doc._attachments).length > 0; + } - function onChangesComplete(changes) { - changesPending = false; - /* istanbul ignore if */ - if (returnValue.cancelled) { - return completeReplication(); - } + function hasConflicts(doc) { + return doc._conflicts && doc._conflicts.length > 0; + } - // if no results were returned then we're done, - // else fetch more - if (changes.results.length > 0) { - changesOpts.since = changes.last_seq; - getChanges(); - processPendingBatch(true); - } else { + function fetchRevisionOneDocs(ids) { + // Optimization: fetch gen-1 docs and attachments in + // a single request using _all_docs + return src.allDocs({ + keys: ids, + include_docs: true, + conflicts: true + }).then(function (res) { + if (state.cancelled) { + throw new Error('cancelled'); + } + res.rows.forEach(function (row) { + if (row.deleted || !row.doc || !isGenOne$1(row.value.rev) || + hasAttachments(row.doc) || hasConflicts(row.doc)) { + // if any of these conditions apply, we need to fetch using get() + return; + } - var complete = function () { - if (continuous) { - changesOpts.live = true; - getChanges(); - } else { - changesCompleted = true; + // strip _conflicts array to appease CSG (#5793) + /* istanbul ignore if */ + if (row.doc._conflicts) { + delete row.doc._conflicts; } - processPendingBatch(true); - }; - // update the checkpoint so we start from the right seq next time - if (!currentBatch && changes.results.length === 0) { - writingCheckpoint = true; - checkpointer.writeCheckpoint(changes.last_seq, - session).then(function () { - writingCheckpoint = false; - result.last_seq = last_seq = changes.last_seq; - complete(); - }) - .catch(onCheckpointError); - } else { - complete(); - } - } + // the doc we got back from allDocs() is sufficient + resultDocs.push(row.doc); + delete diffs[row.id]; + }); + }); } - - function onChangesError(err) { - changesPending = false; - /* istanbul ignore if */ - if (returnValue.cancelled) { - return completeReplication(); + 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(diffs).filter(function (id) { + var missing = diffs[id].missing; + return missing.length === 1 && isGenOne$1(missing[0]); + }); + if (ids.length > 0) { + return fetchRevisionOneDocs(ids); } - 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); - } - - if (returnValue._changes) { // remove old changes() and listeners - returnValue.removeListener('cancel', returnValue._abortChanges); - returnValue._changes.cancel(); - } - returnValue.once('cancel', abortChanges); - - var changes = src.changes(changesOpts) - .on('change', onChange); - changes.then(removeListener, removeListener); - changes.then(onChangesComplete) - .catch(onChangesError); - - if (opts.retry) { - // save for later so we can cancel if necessary - returnValue._changes = changes; - returnValue._abortChanges = abortChanges; - } + function returnResult() { + return { ok:ok, docs:resultDocs }; } + return PouchPromise$1.resolve() + .then(getRevisionOneDocs) + .then(getAllDocs) + .then(returnResult); + } - function startChanges() { - initCheckpointer().then(function () { - /* istanbul ignore if */ - if (returnValue.cancelled) { - completeReplication(); - return; + var CHECKPOINT_VERSION = 1; + var REPLICATOR = "pouchdb"; + // This is an arbitrary number to limit the + // amount of replication history we save in the checkpoint. + // If we save too much, the checkpoing docs will become very big, + // if we save fewer, we'll run a greater risk of having to + // read all the changes from 0 when checkpoint PUTs fail + // CouchDB 2.0 has a more involved history pruning, + // but let's go for the simple version for now. + var CHECKPOINT_HISTORY_SIZE = 5; + var LOWEST_SEQ = 0; + + function updateCheckpoint(db, id, checkpoint, session, returnValue) { + return db.get(id).catch(function (err) { + if (err.status === 404) { + if (db.type() === 'http') { + explainError( + 404, 'PouchDB is just checking if a remote checkpoint exists.' + ); } - return 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, - selector: selector, - return_docs: true // required so we know when we're done - }; - if (seq_interval !== false) { - changesOpts.seq_interval = seq_interval || batch_size; - } - if (opts.filter) { - if (typeof opts.filter !== 'string') { - // required for the client-side filter in onChange - changesOpts.include_docs = true; - } else { // ddoc filter - changesOpts.filter = opts.filter; - } - } - if ('heartbeat' in opts) { - changesOpts.heartbeat = opts.heartbeat; - } - if ('timeout' in opts) { - changesOpts.timeout = opts.timeout; - } - if (opts.query_params) { - changesOpts.query_params = opts.query_params; - } - if (opts.view) { - changesOpts.view = opts.view; - } - getChanges(); - }); - }).catch(function (err) { - abortReplication('getCheckpoint rejected with ', err); + return { + session_id: session, + _id: id, + history: [], + replicator: REPLICATOR, + version: CHECKPOINT_VERSION + }; + } + throw err; + }).then(function (doc) { + if (returnValue.cancelled) { + return; + } + + // if the checkpoint has not changed, do not update + if (doc.last_seq === checkpoint) { + return; + } + + // Filter out current entry for this replication + doc.history = (doc.history || []).filter(function (item) { + return item.session_id !== session; }); - } - /* istanbul ignore next */ - function onCheckpointError(err) { - writingCheckpoint = false; - abortReplication('writeCheckpoint completed with error', err); - } + // Add the latest checkpoint to history + doc.history.unshift({ + last_seq: checkpoint, + session_id: session + }); - /* istanbul ignore if */ - if (returnValue.cancelled) { // cancelled immediately - completeReplication(); - return; - } + // Just take the last pieces in history, to + // avoid really big checkpoint docs. + // see comment on history size above + doc.history = doc.history.slice(0, CHECKPOINT_HISTORY_SIZE); - if (!returnValue._addedListeners) { - returnValue.once('cancel', completeReplication); + doc.version = CHECKPOINT_VERSION; + doc.replicator = REPLICATOR; - if (typeof opts.complete === 'function') { - returnValue.once('error', opts.complete); - returnValue.once('complete', function (result) { - opts.complete(null, result); - }); - } - returnValue._addedListeners = true; - } + doc.session_id = session; + doc.last_seq = checkpoint; - if (typeof opts.since === 'undefined') { - startChanges(); - } else { - initCheckpointer().then(function () { - writingCheckpoint = true; - return checkpointer.writeCheckpoint(opts.since, session); - }).then(function () { - writingCheckpoint = false; - /* istanbul ignore if */ - if (returnValue.cancelled) { - completeReplication(); - return; + return db.put(doc).catch(function (err) { + if (err.status === 409) { + // retry; someone is trying to write a checkpoint simultaneously + return updateCheckpoint(db, id, checkpoint, session, returnValue); } - last_seq = opts.since; - startChanges(); - }).catch(onCheckpointError); - } + throw err; + }); + }); } - // We create a basic promise so the caller can cancel the replication possibly - // before we have actually started listening to changes etc - inherits(Replication, events.EventEmitter); - function Replication() { - events.EventEmitter.call(this); - this.cancelled = false; - this.state = 'pending'; + function Checkpointer(src, target, id, returnValue) { + this.src = src; + this.target = target; + this.id = id; + this.returnValue = returnValue; + } + + Checkpointer.prototype.writeCheckpoint = function (checkpoint, session) { var self = this; - var promise = new PouchPromise(function (fulfill, reject) { - self.once('complete', fulfill); - self.once('error', reject); + return this.updateTarget(checkpoint, session).then(function () { + return self.updateSource(checkpoint, session); }); - 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'); + Checkpointer.prototype.updateTarget = function (checkpoint, session) { + return updateCheckpoint(this.target, this.id, checkpoint, + session, this.returnValue); }; - Replication.prototype.ready = function (src, target) { + Checkpointer.prototype.updateSource = function (checkpoint, session) { var self = this; - if (self._readyCalled) { - return; - } - self._readyCalled = true; + if (this.readOnlySource) { + return PouchPromise$1.resolve(true); + } + return updateCheckpoint(this.src, this.id, checkpoint, + session, this.returnValue) + .catch(function (err) { + if (isForbiddenError(err)) { + self.readOnlySource = true; + return true; + } + throw err; + }); + }; - function onDestroy() { - self.cancel(); - } - src.once('destroyed', onDestroy); - target.once('destroyed', onDestroy); - function cleanup() { - src.removeListener('destroyed', onDestroy); - target.removeListener('destroyed', onDestroy); + var comparisons = { + "undefined": function (targetDoc, sourceDoc) { + // This is the previous comparison function + if (collate(targetDoc.last_seq, sourceDoc.last_seq) === 0) { + return sourceDoc.last_seq; + } + /* istanbul ignore next */ + return 0; + }, + "1": function (targetDoc, sourceDoc) { + // This is the comparison function ported from CouchDB + return compareReplicationLogs(sourceDoc, targetDoc).last_seq; } - self.once('complete', cleanup); }; - function toPouch(db, opts) { - var PouchConstructor = opts.PouchConstructor; - if (typeof db === 'string') { - return new PouchConstructor(db, opts); - } else { - return db; - } - } + Checkpointer.prototype.getCheckpoint = function () { + var self = this; + return self.target.get(self.id).then(function (targetDoc) { + if (self.readOnlySource) { + return PouchPromise$1.resolve(targetDoc.last_seq); + } - function replicateWrapper(src, target, opts, callback) { + return self.src.get(self.id).then(function (sourceDoc) { + // Since we can't migrate an old version doc to a new one + // (no session id), we just go with the lowest seq in this case + /* istanbul ignore if */ + if (targetDoc.version !== sourceDoc.version) { + return LOWEST_SEQ; + } - if (typeof opts === 'function') { - callback = opts; - opts = {}; - } - if (typeof opts === 'undefined') { - opts = {}; - } + var version; + if (targetDoc.version) { + version = targetDoc.version.toString(); + } else { + version = "undefined"; + } - if (opts.doc_ids && !Array.isArray(opts.doc_ids)) { - throw createError(BAD_REQUEST, - "`doc_ids` filter parameter is not a list."); + if (version in comparisons) { + return comparisons[version](targetDoc, sourceDoc); + } + /* istanbul ignore next */ + return LOWEST_SEQ; + }, function (err) { + if (err.status === 404 && targetDoc.last_seq) { + return self.src.put({ + _id: self.id, + last_seq: LOWEST_SEQ + }).then(function () { + return LOWEST_SEQ; + }, function (err) { + if (isForbiddenError(err)) { + self.readOnlySource = true; + return targetDoc.last_seq; + } + /* istanbul ignore next */ + return LOWEST_SEQ; + }); + } + throw err; + }); + }).catch(function (err) { + if (err.status !== 404) { + throw err; + } + return LOWEST_SEQ; + }); + }; + // This checkpoint comparison is ported from CouchDBs source + // they come from here: + // https://github.com/apache/couchdb-couch-replicator/blob/master/src/couch_replicator.erl#L863-L906 + + function compareReplicationLogs(srcDoc, tgtDoc) { + if (srcDoc.session_id === tgtDoc.session_id) { + return { + last_seq: srcDoc.last_seq, + history: srcDoc.history + }; } - opts.complete = callback; - opts = clone(opts); - opts.continuous = opts.continuous || opts.live; - opts.retry = ('retry' in opts) ? opts.retry : false; - /*jshint validthis:true */ - opts.PouchConstructor = opts.PouchConstructor || this; - var replicateRet = new Replication(opts); - var srcPouch = toPouch(src, opts); - var targetPouch = toPouch(target, opts); - replicate(srcPouch, targetPouch, opts, replicateRet); - return replicateRet; + return compareReplicationHistory(srcDoc.history, tgtDoc.history); } - inherits(Sync, events.EventEmitter); - function sync$1(src, target, opts, callback) { - if (typeof opts === 'function') { - callback = opts; - opts = {}; + function compareReplicationHistory(sourceHistory, targetHistory) { + // the erlang loop via function arguments is not so easy to repeat in JS + // therefore, doing this as recursion + var S = sourceHistory[0]; + var sourceRest = sourceHistory.slice(1); + var T = targetHistory[0]; + var targetRest = targetHistory.slice(1); + + if (!S || targetHistory.length === 0) { + return { + last_seq: LOWEST_SEQ, + history: [] + }; + } + + var sourceId = S.session_id; + /* istanbul ignore if */ + if (hasSessionId(sourceId, targetHistory)) { + return { + last_seq: S.last_seq, + history: sourceHistory + }; } - if (typeof opts === 'undefined') { - opts = {}; + + var targetId = T.session_id; + if (hasSessionId(targetId, sourceRest)) { + return { + last_seq: T.last_seq, + history: targetRest + }; } - opts = clone(opts); - /*jshint validthis:true */ - opts.PouchConstructor = opts.PouchConstructor || this; - src = toPouch(src, opts); - target = toPouch(target, opts); - return new Sync(src, target, opts, callback); + + return compareReplicationHistory(sourceRest, targetRest); } - function Sync(src, target, opts, callback) { - var self = this; - this.canceled = false; + function hasSessionId(sessionId, history) { + var props = history[0]; + var rest = history.slice(1); - var optsPush = opts.push ? $inject_Object_assign({}, opts, opts.push) : opts; - var optsPull = opts.pull ? $inject_Object_assign({}, opts, opts.pull) : opts; + if (!sessionId || history.length === 0) { + return false; + } - this.push = replicateWrapper(src, target, optsPush); - this.pull = replicateWrapper(target, src, optsPull); + if (sessionId === props.session_id) { + return true; + } - this.pushPaused = true; - this.pullPaused = true; + return hasSessionId(sessionId, rest); + } - 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 - }); - } - function pushPaused() { - self.pushPaused = true; - /* istanbul ignore if */ - if (self.pullPaused) { - self.emit('paused'); - } - } - function pullPaused() { - self.pullPaused = true; - /* istanbul ignore if */ - if (self.pushPaused) { - self.emit('paused'); - } + function isForbiddenError(err) { + return typeof err.status === 'number' && Math.floor(err.status / 100) === 4; + } + + var STARTING_BACK_OFF = 0; + + function backOff(opts, returnValue, error, callback) { + if (opts.retry === false) { + returnValue.emit('error', error); + returnValue.removeAllListeners(); + return; } - function pushActive() { - self.pushPaused = false; - /* istanbul ignore if */ - if (self.pullPaused) { - self.emit('active', { - direction: 'push' - }); - } + if (typeof opts.back_off_function !== 'function') { + opts.back_off_function = defaultBackOff; } - function pullActive() { - self.pullPaused = false; - /* istanbul ignore if */ - if (self.pushPaused) { - self.emit('active', { - direction: 'pull' - }); - } + returnValue.emit('requestError', error); + if (returnValue.state === 'active' || returnValue.state === 'pending') { + returnValue.emit('paused', error); + returnValue.state = 'stopped'; + var backOffSet = function backoffTimeSet() { + opts.current_back_off = STARTING_BACK_OFF; + }; + var removeBackOffSetter = function removeBackOffTimeSet() { + returnValue.removeListener('active', backOffSet); + }; + returnValue.once('paused', removeBackOffSetter); + returnValue.once('active', backOffSet); } - var removed = {}; + opts.current_back_off = opts.current_back_off || STARTING_BACK_OFF; + opts.current_back_off = opts.back_off_function(opts.current_back_off); + setTimeout(callback, opts.current_back_off); + } - function removeAll(type) { // type is 'push' or 'pull' - return function (event, func) { - var isChange = event === 'change' && - (func === pullChange || func === pushChange); - var isDenied = event === 'denied' && - (func === pullDenied || func === pushDenied); - var isPaused = event === 'paused' && - (func === pullPaused || func === pushPaused); - var isActive = event === 'active' && - (func === pullActive || func === pushActive); + function sortObjectPropertiesByKey(queryParams) { + return Object.keys(queryParams).sort(collate).reduce(function (result, key) { + result[key] = queryParams[key]; + return result; + }, {}); + } - if (isChange || isDenied || isPaused || isActive) { - 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); - } - } - }; - } + // Generate a unique id particular to this replication. + // Not guaranteed to align perfectly with CouchDB's rep ids. + function generateReplicationId(src, target, opts) { + var docIds = opts.doc_ids ? opts.doc_ids.sort(collate) : ''; + var filterFun = opts.filter ? opts.filter.toString() : ''; + var queryParams = ''; + var filterViewName = ''; - if (opts.live) { - this.push.on('complete', self.pull.cancel.bind(self.pull)); - this.pull.on('complete', self.push.cancel.bind(self.push)); + if (opts.filter && opts.query_params) { + queryParams = JSON.stringify(sortObjectPropertiesByKey(opts.query_params)); } - function addOneListener(ee, event, listener) { - if (ee.listeners(event).indexOf(listener) == -1) { - ee.on(event, listener); - } + if (opts.filter && opts.filter === '_view') { + filterViewName = opts.view.toString(); } - this.on('newListener', function (event) { - if (event === 'change') { - addOneListener(self.pull, 'change', pullChange); - addOneListener(self.push, 'change', pushChange); - } else if (event === 'denied') { - addOneListener(self.pull, 'denied', pullDenied); - addOneListener(self.push, 'denied', pushDenied); - } else if (event === 'active') { - addOneListener(self.pull, 'active', pullActive); - addOneListener(self.push, 'active', pushActive); - } else if (event === 'paused') { - addOneListener(self.pull, 'paused', pullPaused); - addOneListener(self.push, 'paused', pushPaused); - } - }); - - this.on('removeListener', function (event) { - if (event === 'change') { - self.pull.removeListener('change', pullChange); - self.push.removeListener('change', pushChange); - } else if (event === 'denied') { - self.pull.removeListener('denied', pullDenied); - self.push.removeListener('denied', pushDenied); - } else if (event === 'active') { - self.pull.removeListener('active', pullActive); - self.push.removeListener('active', pushActive); - } else if (event === 'paused') { - self.pull.removeListener('paused', pullPaused); - self.push.removeListener('paused', pushPaused); - } + return PouchPromise$1.all([src.id(), target.id()]).then(function (res) { + var queryData = res[0] + res[1] + filterFun + filterViewName + + queryParams + docIds; + return new PouchPromise$1(function (resolve) { + binaryMd5(queryData, resolve); + }); + }).then(function (md5sum) { + // can't use straight-up md5 alphabet, because + // the char '/' is interpreted as being for attachments, + // and + is also not url-safe + md5sum = md5sum.replace(/\//g, '.').replace(/\+/g, '_'); + return '_local/' + md5sum; }); + } - this.pull.on('removeListener', removeAll('pull')); - this.push.on('removeListener', removeAll('push')); - - var promise = PouchPromise.all([ - this.push, - this.pull - ]).then(function (resp) { - var out = { - push: resp[0], - pull: resp[1] - }; - self.emit('complete', out); - if (callback) { - callback(null, out); - } - self.removeAllListeners(); - return out; - }, function (err) { - self.cancel(); - if (callback) { - // if there's a callback, then the callback can receive - // the error event - callback(err); - } else { - // if there's no callback, then we're safe to emit an error - // event, which would otherwise throw an unhandled error - // due to 'error' being a special event in EventEmitters - self.emit('error', err); - } - self.removeAllListeners(); - if (callback) { - // no sense throwing if we're already emitting an 'error' event - throw err; - } - }); + function replicate(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 repId; + var checkpointer; + var changedDocs = []; + // Like couchdb, every replication gets a unique session id + var session = uuid(); - this.then = function (success, err) { - return promise.then(success, err); + result = result || { + ok: true, + start_time: new Date(), + docs_read: 0, + docs_written: 0, + doc_write_failures: 0, + errors: [] }; - this.catch = function (err) { - return promise.catch(err); - }; - } + var changesOpts = {}; + returnValue.ready(src, target); - Sync.prototype.cancel = function () { - if (!this.canceled) { - this.canceled = true; - this.push.cancel(); - this.pull.cancel(); + function initCheckpointer() { + if (checkpointer) { + return PouchPromise$1.resolve(); + } + return generateReplicationId(src, target, opts).then(function (res) { + repId = res; + checkpointer = new Checkpointer(src, target, repId, returnValue); + }); } - }; - function replication(PouchDB) { - PouchDB.replicate = replicateWrapper; - PouchDB.sync = sync$1; + function writeDocs() { + changedDocs = []; - Object.defineProperty(PouchDB.prototype, 'replicate', { - get: function () { - var self = this; - if (typeof this.replicateMethods === 'undefined') { - this.replicateMethods = { - from: function (other, opts, callback) { - return self.constructor.replicate(other, self, opts, callback); - }, - to: function (other, opts, callback) { - return self.constructor.replicate(self, other, opts, callback); - } - }; - } - return this.replicateMethods; + if (currentBatch.docs.length === 0) { + return; } - }); + var docs = currentBatch.docs; + var bulkOpts = {timeout: opts.timeout}; + return target.bulkDocs({docs: docs, new_edits: false}, bulkOpts).then(function (res) { + /* istanbul ignore if */ + if (returnValue.cancelled) { + completeReplication(); + throw new Error('cancelled'); + } - PouchDB.prototype.sync = function (dbName, opts, callback) { - return this.constructor.sync(this, dbName, opts, callback); - }; - } + // `res` doesn't include full documents (which live in `docs`), so we create a map of + // (id -> error), and check for errors while iterating over `docs` + var errorsById = Object.create(null); + res.forEach(function (res) { + if (res.error) { + errorsById[res.id] = res; + } + }); - PouchDB.plugin(IDBPouch) - .plugin(WebSqlPouch$1) - .plugin(HttpPouch$1) - .plugin(mapreduce) - .plugin(replication); + var errorsNo = Object.keys(errorsById).length; + result.doc_write_failures += errorsNo; + result.docs_written += docs.length - errorsNo; - // Pull from src because pouchdb-node/pouchdb-browser themselves - // are aggressively optimized and jsnext:main would normally give us this - // aggressive bundle. + docs.forEach(function (doc) { + var error = errorsById[doc._id]; + if (error) { + result.errors.push(error); + if (error.name === 'unauthorized' || error.name === 'forbidden') { + returnValue.emit('denied', clone(error)); + } else { + throw error; + } + } else { + changedDocs.push(doc); + } + }); - module.exports = PouchDB; + }, function (err) { + result.doc_write_failures += docs.length; + throw err; + }); + } - /* WEBPACK VAR INJECTION */}.call(exports, (function() { return this; }()))) - -/***/ }, -/* 61 */ -/***/ function(module, exports, __webpack_require__) { - - 'use strict'; - var immediate = __webpack_require__(62); + function finishBatch() { + if (currentBatch.error) { + throw new Error('There was a problem getting docs.'); + } + result.last_seq = last_seq = currentBatch.seq; + var outResult = clone(result); + if (changedDocs.length) { + outResult.docs = changedDocs; + returnValue.emit('change', outResult); + } + writingCheckpoint = true; + return checkpointer.writeCheckpoint(currentBatch.seq, + session).then(function () { + writingCheckpoint = false; + /* istanbul ignore if */ + if (returnValue.cancelled) { + completeReplication(); + throw new Error('cancelled'); + } + currentBatch = undefined; + getChanges(); + }).catch(function (err) { + onCheckpointError(err); + throw err; + }); + } - /* istanbul ignore next */ - function INTERNAL() {} + function getDiffs() { + var diff = {}; + currentBatch.changes.forEach(function (change) { + // Couchbase Sync Gateway emits these, but we can ignore them + /* istanbul ignore if */ + if (change.id === "_user/") { + return; + } + diff[change.id] = change.changes.map(function (x) { + return x.rev; + }); + }); + return target.revsDiff(diff).then(function (diffs) { + /* istanbul ignore if */ + if (returnValue.cancelled) { + completeReplication(); + throw new Error('cancelled'); + } + // currentBatch.diffs elements are deleted as the documents are written + currentBatch.diffs = diffs; + }); + } - var handlers = {}; + function getBatchDocs() { + return getDocs(src, target, currentBatch.diffs, returnValue).then(function (got) { + currentBatch.error = !got.ok; + got.docs.forEach(function (doc) { + delete currentBatch.diffs[doc._id]; + result.docs_read++; + currentBatch.docs.push(doc); + }); + }); + } - var REJECTED = ['REJECTED']; - var FULFILLED = ['FULFILLED']; - var PENDING = ['PENDING']; + function startNextBatch() { + if (returnValue.cancelled || currentBatch) { + return; + } + if (batches.length === 0) { + processPendingBatch(true); + return; + } + currentBatch = batches.shift(); + getDiffs() + .then(getBatchDocs) + .then(writeDocs) + .then(finishBatch) + .then(startNextBatch) + .catch(function (err) { + abortReplication('batch processing terminated with error', err); + }); + } - module.exports = Promise; - function Promise(resolver) { - if (typeof resolver !== 'function') { - throw new TypeError('resolver must be a function'); - } - this.state = PENDING; - this.queue = []; - this.outcome = void 0; - if (resolver !== INTERNAL) { - safelyResolveThenable(this, resolver); + function processPendingBatch(immediate) { + if (pendingBatch.changes.length === 0) { + if (batches.length === 0 && !currentBatch) { + if ((continuous && changesOpts.live) || changesCompleted) { + returnValue.state = 'pending'; + returnValue.emit('paused'); + } + if (changesCompleted) { + completeReplication(); + } + } + return; + } + if ( + immediate || + changesCompleted || + pendingBatch.changes.length >= batch_size + ) { + batches.push(pendingBatch); + pendingBatch = { + seq: 0, + changes: [], + docs: [] + }; + if (returnValue.state === 'pending' || returnValue.state === 'stopped') { + returnValue.state = 'active'; + returnValue.emit('active'); + } + startNextBatch(); + } } - } - Promise.prototype["catch"] = function (onRejected) { - return this.then(null, onRejected); - }; - Promise.prototype.then = function (onFulfilled, onRejected) { - if (typeof onFulfilled !== 'function' && this.state === FULFILLED || - typeof onRejected !== 'function' && this.state === REJECTED) { - return this; - } - var promise = new this.constructor(INTERNAL); - if (this.state !== PENDING) { - var resolver = this.state === FULFILLED ? onFulfilled : onRejected; - unwrap(promise, resolver, this.outcome); - } else { - this.queue.push(new QueueItem(promise, onFulfilled, onRejected)); + + function abortReplication(reason, err) { + if (replicationCompleted) { + return; + } + if (!err.message) { + err.message = reason; + } + result.ok = false; + result.status = 'aborting'; + batches = []; + pendingBatch = { + seq: 0, + changes: [], + docs: [] + }; + completeReplication(err); } - return promise; - }; - 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); - }; - function unwrap(promise, func, value) { - immediate(function () { - var returnValue; - try { - returnValue = func(value); - } catch (e) { - return handlers.reject(promise, e); + function completeReplication(fatalError) { + if (replicationCompleted) { + return; } - if (returnValue === promise) { - handlers.reject(promise, new TypeError('Cannot resolve promise with itself')); - } else { - handlers.resolve(promise, returnValue); + /* istanbul ignore if */ + if (returnValue.cancelled) { + result.status = 'cancelled'; + if (writingCheckpoint) { + return; + } } - }); - } + result.status = result.status || 'complete'; + result.end_time = new Date(); + result.last_seq = last_seq; + replicationCompleted = true; - handlers.resolve = function (self, value) { - var result = tryCatch(getThen, value); - if (result.status === 'error') { - return handlers.reject(self, result.value); - } - var thenable = result.value; + if (fatalError) { + fatalError.result = result; - if (thenable) { - safelyResolveThenable(self, thenable); - } else { - self.state = FULFILLED; - self.outcome = value; - var i = -1; - var len = self.queue.length; - while (++i < len) { - self.queue[i].callFulfilled(value); + if (fatalError.name === 'unauthorized' || fatalError.name === 'forbidden') { + returnValue.emit('error', fatalError); + returnValue.removeAllListeners(); + } else { + backOff(opts, returnValue, fatalError, function () { + replicate(src, target, opts, returnValue); + }); + } + } else { + returnValue.emit('complete', result); + returnValue.removeAllListeners(); } } - return self; - }; - handlers.reject = function (self, error) { - self.state = 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 obj === 'function') && typeof then === 'function') { - return function appyThen() { - then.apply(obj, arguments); - }; - } - } - function safelyResolveThenable(self, thenable) { - // Either fulfill, reject or reject with error - var called = false; - function onError(value) { - if (called) { - return; + function onChange(change) { + /* istanbul ignore if */ + if (returnValue.cancelled) { + return completeReplication(); } - called = true; - handlers.reject(self, value); - } - - function onSuccess(value) { - if (called) { + var filter = filterChange(opts)(change); + if (!filter) { return; } - called = true; - handlers.resolve(self, value); + pendingBatch.seq = change.seq; + pendingBatch.changes.push(change); + processPendingBatch(batches.length === 0 && changesOpts.live); } - function tryToUnwrap() { - thenable(onSuccess, onError); - } - var result = tryCatch(tryToUnwrap); - if (result.status === 'error') { - onError(result.value); - } - } + function onChangesComplete(changes) { + changesPending = false; + /* istanbul ignore if */ + if (returnValue.cancelled) { + return completeReplication(); + } - function tryCatch(func, value) { - var out = {}; - try { - out.value = func(value); - out.status = 'success'; - } catch (e) { - out.status = 'error'; - out.value = e; - } - return out; - } + // if no results were returned then we're done, + // else fetch more + if (changes.results.length > 0) { + changesOpts.since = changes.last_seq; + getChanges(); + processPendingBatch(true); + } else { - Promise.resolve = resolve; - function resolve(value) { - if (value instanceof this) { - return value; + var complete = function () { + if (continuous) { + changesOpts.live = true; + getChanges(); + } else { + changesCompleted = true; + } + processPendingBatch(true); + }; + + // update the checkpoint so we start from the right seq next time + if (!currentBatch && changes.results.length === 0) { + writingCheckpoint = true; + checkpointer.writeCheckpoint(changes.last_seq, + session).then(function () { + writingCheckpoint = false; + result.last_seq = last_seq = changes.last_seq; + complete(); + }) + .catch(onCheckpointError); + } else { + complete(); + } + } } - return handlers.resolve(new this(INTERNAL), value); - } - Promise.reject = reject; - function reject(reason) { - var promise = new this(INTERNAL); - return handlers.reject(promise, reason); - } - Promise.all = all; - function all(iterable) { - var self = this; - if (Object.prototype.toString.call(iterable) !== '[object Array]') { - return this.reject(new TypeError('must be an array')); + function onChangesError(err) { + changesPending = false; + /* istanbul ignore if */ + if (returnValue.cancelled) { + return completeReplication(); + } + abortReplication('changes rejected', err); } - var len = iterable.length; - var called = false; - if (!len) { - return this.resolve([]); - } - var values = new Array(len); - var resolved = 0; - var i = -1; - var promise = new this(INTERNAL); + function getChanges() { + if (!( + !changesPending && + !changesCompleted && + batches.length < batches_limit + )) { + return; + } + changesPending = true; + function abortChanges() { + changes.cancel(); + } + function removeListener() { + returnValue.removeListener('cancel', abortChanges); + } - while (++i < len) { - allResolver(iterable[i], i); - } - return promise; - function allResolver(value, i) { - self.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); - } + if (returnValue._changes) { // remove old changes() and listeners + returnValue.removeListener('cancel', returnValue._abortChanges); + returnValue._changes.cancel(); } - } - } + returnValue.once('cancel', abortChanges); - Promise.race = race; - function race(iterable) { - var self = this; - if (Object.prototype.toString.call(iterable) !== '[object Array]') { - return this.reject(new TypeError('must be an array')); - } + var changes = src.changes(changesOpts) + .on('change', onChange); + changes.then(removeListener, removeListener); + changes.then(onChangesComplete) + .catch(onChangesError); - var len = iterable.length; - var called = false; - if (!len) { - return this.resolve([]); + if (opts.retry) { + // save for later so we can cancel if necessary + returnValue._changes = changes; + returnValue._abortChanges = abortChanges; + } } - var i = -1; - var promise = new this(INTERNAL); - while (++i < len) { - resolver(iterable[i]); - } - return promise; - function resolver(value) { - self.resolve(value).then(function (response) { - if (!called) { - called = true; - handlers.resolve(promise, response); - } - }, function (error) { - if (!called) { - called = true; - handlers.reject(promise, error); + function startChanges() { + initCheckpointer().then(function () { + /* istanbul ignore if */ + if (returnValue.cancelled) { + completeReplication(); + return; } + return 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, + return_docs: true // required so we know when we're done + }; + if (opts.filter) { + if (typeof opts.filter !== 'string') { + // required for the client-side filter in onChange + changesOpts.include_docs = true; + } else { // ddoc filter + changesOpts.filter = opts.filter; + } + } + if ('heartbeat' in opts) { + changesOpts.heartbeat = opts.heartbeat; + } + if ('timeout' in opts) { + changesOpts.timeout = opts.timeout; + } + if (opts.query_params) { + changesOpts.query_params = opts.query_params; + } + if (opts.view) { + changesOpts.view = opts.view; + } + getChanges(); + }); + }).catch(function (err) { + abortReplication('getCheckpoint rejected with ', err); }); } - } - - -/***/ }, -/* 62 */ -/***/ function(module, exports) { - - /* WEBPACK VAR INJECTION */(function(global) {'use strict'; - var Mutation = global.MutationObserver || global.WebKitMutationObserver; - var scheduleDrain; + /* istanbul ignore next */ + function onCheckpointError(err) { + writingCheckpoint = false; + abortReplication('writeCheckpoint completed with error', err); + } - { - if (Mutation) { - var called = 0; - var observer = new Mutation(nextTick); - var element = global.document.createTextNode(''); - observer.observe(element, { - characterData: true - }); - scheduleDrain = function () { - element.data = (called = ++called % 2); - }; - } else if (!global.setImmediate && typeof global.MessageChannel !== 'undefined') { - var channel = new global.MessageChannel(); - channel.port1.onmessage = nextTick; - scheduleDrain = function () { - channel.port2.postMessage(0); - }; - } else if ('document' in global && 'onreadystatechange' in global.document.createElement('script')) { - scheduleDrain = function () { + /* istanbul ignore if */ + if (returnValue.cancelled) { // cancelled immediately + completeReplication(); + return; + } - // Create a