pouchdb-ajax
Advanced tools
Comparing version 6.0.2 to 6.0.3
@@ -5,8 +5,53 @@ 'use strict'; | ||
var pouchdbBinaryUtils = require('pouchdb-binary-utils'); | ||
var Promise = _interopDefault(require('pouchdb-promise')); | ||
var lie = _interopDefault(require('lie')); | ||
var jsExtend = require('js-extend'); | ||
var pouchdbErrors = require('pouchdb-errors'); | ||
var pouchdbUtils = require('pouchdb-utils'); | ||
var inherits = _interopDefault(require('inherits')); | ||
var getArguments = _interopDefault(require('argsarray')); | ||
var debug = _interopDefault(require('debug')); | ||
var events = require('events'); | ||
// 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 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); | ||
} | ||
} | ||
// 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 reader = new FileReader(); | ||
reader.onloadend = function (e) { | ||
var result = e.target.result || new ArrayBuffer(0); | ||
callback(result); | ||
}; | ||
reader.readAsArrayBuffer(blob); | ||
} | ||
/* istanbul ignore next */ | ||
var PouchPromise = typeof Promise === 'function' ? Promise : lie; | ||
/* global fetch */ | ||
@@ -17,3 +62,3 @@ /* global Headers */ | ||
var promise = new Promise(function (resolve, reject) { | ||
var promise = new PouchPromise(function (resolve, reject) { | ||
wrappedPromise.resolve = resolve; | ||
@@ -31,3 +76,3 @@ wrappedPromise.reject = reject; | ||
Promise.resolve().then(function () { | ||
PouchPromise.resolve().then(function () { | ||
return fetch.apply(null, args); | ||
@@ -61,3 +106,3 @@ }).then(function (response) { | ||
if (options.body && (options.body instanceof Blob)) { | ||
pouchdbBinaryUtils.readAsArrayBuffer(options.body, function (arrayBuffer) { | ||
readAsArrayBuffer(options.body, function (arrayBuffer) { | ||
fetchOptions.body = arrayBuffer; | ||
@@ -215,3 +260,3 @@ }); | ||
if (options.binary) { | ||
data = pouchdbBinaryUtils.blob([xhr.response || ''], { | ||
data = createBlob([xhr.response || ''], { | ||
type: xhr.getResponseHeader('Content-Type') | ||
@@ -240,3 +285,3 @@ }); | ||
if (options.body && (options.body instanceof Blob)) { | ||
pouchdbBinaryUtils.readAsArrayBuffer(options.body, function (arrayBuffer) { | ||
readAsArrayBuffer(options.body, function (arrayBuffer) { | ||
xhr.send(arrayBuffer); | ||
@@ -270,4 +315,468 @@ }); | ||
inherits(PouchError, Error); | ||
function PouchError(opts) { | ||
Error.call(this, opts.reason); | ||
this.status = opts.status; | ||
this.name = opts.error; | ||
this.message = opts.reason; | ||
this.error = true; | ||
} | ||
PouchError.prototype.toString = function () { | ||
return JSON.stringify({ | ||
status: this.status, | ||
name: this.name, | ||
message: this.message, | ||
reason: this.reason | ||
}); | ||
}; | ||
var UNAUTHORIZED = new PouchError({ | ||
status: 401, | ||
error: 'unauthorized', | ||
reason: "Name or password is incorrect." | ||
}); | ||
var MISSING_BULK_DOCS = new PouchError({ | ||
status: 400, | ||
error: 'bad_request', | ||
reason: "Missing JSON list of 'docs'" | ||
}); | ||
var MISSING_DOC = new PouchError({ | ||
status: 404, | ||
error: 'not_found', | ||
reason: 'missing' | ||
}); | ||
var REV_CONFLICT = new PouchError({ | ||
status: 409, | ||
error: 'conflict', | ||
reason: 'Document update conflict' | ||
}); | ||
var INVALID_ID = new PouchError({ | ||
status: 400, | ||
error: 'bad_request', | ||
reason: '_id field must contain a string' | ||
}); | ||
var MISSING_ID = new PouchError({ | ||
status: 412, | ||
error: 'missing_id', | ||
reason: '_id is required for puts' | ||
}); | ||
var RESERVED_ID = new PouchError({ | ||
status: 400, | ||
error: 'bad_request', | ||
reason: 'Only reserved document ids may start with underscore.' | ||
}); | ||
var NOT_OPEN = new PouchError({ | ||
status: 412, | ||
error: 'precondition_failed', | ||
reason: 'Database not open' | ||
}); | ||
var UNKNOWN_ERROR = new PouchError({ | ||
status: 500, | ||
error: 'unknown_error', | ||
reason: 'Database encountered an unknown error' | ||
}); | ||
var BAD_ARG = new PouchError({ | ||
status: 500, | ||
error: 'badarg', | ||
reason: 'Some query argument is invalid' | ||
}); | ||
var INVALID_REQUEST = new PouchError({ | ||
status: 400, | ||
error: 'invalid_request', | ||
reason: 'Request was invalid' | ||
}); | ||
var QUERY_PARSE_ERROR = new PouchError({ | ||
status: 400, | ||
error: 'query_parse_error', | ||
reason: 'Some query parameter is invalid' | ||
}); | ||
var DOC_VALIDATION = new PouchError({ | ||
status: 500, | ||
error: 'doc_validation', | ||
reason: 'Bad special document member' | ||
}); | ||
var BAD_REQUEST = new PouchError({ | ||
status: 400, | ||
error: 'bad_request', | ||
reason: 'Something wrong with the request' | ||
}); | ||
var NOT_AN_OBJECT = new PouchError({ | ||
status: 400, | ||
error: 'bad_request', | ||
reason: 'Document must be a JSON object' | ||
}); | ||
var DB_MISSING = new PouchError({ | ||
status: 404, | ||
error: 'not_found', | ||
reason: 'Database not found' | ||
}); | ||
var IDB_ERROR = new PouchError({ | ||
status: 500, | ||
error: 'indexed_db_went_bad', | ||
reason: 'unknown' | ||
}); | ||
var WSQ_ERROR = new PouchError({ | ||
status: 500, | ||
error: 'web_sql_went_bad', | ||
reason: 'unknown' | ||
}); | ||
var LDB_ERROR = new PouchError({ | ||
status: 500, | ||
error: 'levelDB_went_went_bad', | ||
reason: 'unknown' | ||
}); | ||
var FORBIDDEN = new PouchError({ | ||
status: 403, | ||
error: 'forbidden', | ||
reason: 'Forbidden by design doc validate_doc_update function' | ||
}); | ||
var INVALID_REV = new PouchError({ | ||
status: 400, | ||
error: 'bad_request', | ||
reason: 'Invalid rev format' | ||
}); | ||
var FILE_EXISTS = new PouchError({ | ||
status: 412, | ||
error: 'file_exists', | ||
reason: 'The database could not be created, the file already exists.' | ||
}); | ||
var MISSING_STUB = new PouchError({ | ||
status: 412, | ||
error: 'missing_stub' | ||
}); | ||
var INVALID_URL = new PouchError({ | ||
status: 413, | ||
error: 'invalid_url', | ||
reason: 'Provided URL is invalid' | ||
}); | ||
function generateErrorFromResponse(err) { | ||
if (typeof err !== 'object') { | ||
var data = err; | ||
err = UNKNOWN_ERROR; | ||
err.data = data; | ||
} | ||
if ('error' in err && err.error === 'conflict') { | ||
err.name = 'conflict'; | ||
err.status = 409; | ||
} | ||
if (!('name' in err)) { | ||
err.name = err.error || 'unknown'; | ||
} | ||
if (!('status' in err)) { | ||
err.status = 500; | ||
} | ||
if (!('message' in err)) { | ||
err.message = err.message || err.reason; | ||
} | ||
return err; | ||
} | ||
function isBinaryObject(object) { | ||
return (typeof ArrayBuffer !== 'undefined' && object instanceof ArrayBuffer) || | ||
(typeof Blob !== 'undefined' && object instanceof Blob); | ||
} | ||
function cloneArrayBuffer(buff) { | ||
if (typeof buff.slice === 'function') { | ||
return buff.slice(0); | ||
} | ||
// IE10-11 slice() polyfill | ||
var target = new ArrayBuffer(buff.byteLength); | ||
var targetArray = new Uint8Array(target); | ||
var sourceArray = new Uint8Array(buff); | ||
targetArray.set(sourceArray); | ||
return target; | ||
} | ||
function cloneBinaryObject(object) { | ||
if (object instanceof ArrayBuffer) { | ||
return cloneArrayBuffer(object); | ||
} | ||
var size = object.size; | ||
var type = object.type; | ||
// Blob | ||
if (typeof object.slice === 'function') { | ||
return object.slice(0, size, type); | ||
} | ||
// PhantomJS slice() replacement | ||
return object.webkitSlice(0, size, type); | ||
} | ||
// most of this is borrowed from lodash.isPlainObject: | ||
// https://github.com/fis-components/lodash.isplainobject/ | ||
// blob/29c358140a74f252aeb08c9eb28bef86f2217d4a/index.js | ||
var funcToString = Function.prototype.toString; | ||
var objectCtorString = funcToString.call(Object); | ||
function isPlainObject(value) { | ||
var proto = Object.getPrototypeOf(value); | ||
/* istanbul ignore if */ | ||
if (proto === null) { // not sure when this happens, but I guess it can | ||
return true; | ||
} | ||
var Ctor = proto.constructor; | ||
return (typeof Ctor == 'function' && | ||
Ctor instanceof Ctor && funcToString.call(Ctor) == objectCtorString); | ||
} | ||
function clone(object) { | ||
var newObject; | ||
var i; | ||
var len; | ||
if (!object || typeof object !== 'object') { | ||
return object; | ||
} | ||
if (Array.isArray(object)) { | ||
newObject = []; | ||
for (i = 0, len = object.length; i < len; i++) { | ||
newObject[i] = clone(object[i]); | ||
} | ||
return newObject; | ||
} | ||
// special case: to avoid inconsistencies between IndexedDB | ||
// and other backends, we automatically stringify Dates | ||
if (object instanceof Date) { | ||
return object.toISOString(); | ||
} | ||
if (isBinaryObject(object)) { | ||
return cloneBinaryObject(object); | ||
} | ||
if (!isPlainObject(object)) { | ||
return object; // don't clone objects like Workers | ||
} | ||
newObject = {}; | ||
for (i in object) { | ||
/* istanbul ignore else */ | ||
if (Object.prototype.hasOwnProperty.call(object, i)) { | ||
var value = clone(object[i]); | ||
if (typeof value !== 'undefined') { | ||
newObject[i] = value; | ||
} | ||
} | ||
} | ||
return newObject; | ||
} | ||
var log = debug('pouchdb:api'); | ||
// 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 isChromeApp() { | ||
return (typeof chrome !== "undefined" && | ||
typeof chrome.storage !== "undefined" && | ||
typeof chrome.storage.local !== "undefined"); | ||
} | ||
var hasLocal; | ||
if (isChromeApp()) { | ||
hasLocal = false; | ||
} else { | ||
try { | ||
localStorage.setItem('_pouch_check_localstorage', 1); | ||
hasLocal = !!localStorage.getItem('_pouch_check_localstorage'); | ||
} catch (e) { | ||
hasLocal = false; | ||
} | ||
} | ||
function hasLocalStorage() { | ||
return hasLocal; | ||
} | ||
inherits(Changes, events.EventEmitter); | ||
/* istanbul ignore next */ | ||
function attachBrowserEvents(self) { | ||
if (isChromeApp()) { | ||
chrome.storage.onChanged.addListener(function (e) { | ||
// make sure it's event addressed to us | ||
if (e.db_name != null) { | ||
//object only has oldValue, newValue members | ||
self.emit(e.dbName.newValue); | ||
} | ||
}); | ||
} else if (hasLocalStorage()) { | ||
if (typeof addEventListener !== 'undefined') { | ||
addEventListener("storage", function (e) { | ||
self.emit(e.key); | ||
}); | ||
} else { // old IE | ||
window.attachEvent("storage", function (e) { | ||
self.emit(e.key); | ||
}); | ||
} | ||
} | ||
} | ||
function Changes() { | ||
events.EventEmitter.call(this); | ||
this._listeners = {}; | ||
attachBrowserEvents(this); | ||
} | ||
Changes.prototype.addListener = function (dbName, id, db, opts) { | ||
/* istanbul ignore if */ | ||
if (this._listeners[id]) { | ||
return; | ||
} | ||
var self = this; | ||
var inprogress = false; | ||
function eventFunction() { | ||
/* istanbul ignore if */ | ||
if (!self._listeners[id]) { | ||
return; | ||
} | ||
if (inprogress) { | ||
inprogress = 'waiting'; | ||
return; | ||
} | ||
inprogress = true; | ||
var changesOpts = pick(opts, [ | ||
'style', 'include_docs', 'attachments', 'conflicts', 'filter', | ||
'doc_ids', 'view', 'since', 'query_params', 'binary' | ||
]); | ||
/* istanbul ignore next */ | ||
function onError() { | ||
inprogress = false; | ||
} | ||
db.changes(changesOpts).on('change', function (c) { | ||
if (c.seq > opts.since && !opts.cancelled) { | ||
opts.since = c.seq; | ||
opts.onChange(c); | ||
} | ||
}).on('complete', function () { | ||
if (inprogress === 'waiting') { | ||
setTimeout(function (){ | ||
eventFunction(); | ||
},0); | ||
} | ||
inprogress = false; | ||
}).on('error', onError); | ||
} | ||
this._listeners[id] = eventFunction; | ||
this.on(dbName, eventFunction); | ||
}; | ||
Changes.prototype.removeListener = function (dbName, id) { | ||
/* istanbul ignore if */ | ||
if (!(id in this._listeners)) { | ||
return; | ||
} | ||
events.EventEmitter.prototype.removeListener.call(this, dbName, | ||
this._listeners[id]); | ||
delete this._listeners[id]; | ||
}; | ||
/* istanbul ignore next */ | ||
Changes.prototype.notifyLocalWindows = function (dbName) { | ||
//do a useless change on a storage thing | ||
//in order to get other windows's listeners to activate | ||
if (isChromeApp()) { | ||
chrome.storage.local.set({dbName: dbName}); | ||
} else if (hasLocalStorage()) { | ||
localStorage[dbName] = (localStorage[dbName] === "a") ? "b" : "a"; | ||
} | ||
}; | ||
Changes.prototype.notify = function (dbName) { | ||
this.emit(dbName); | ||
this.notifyLocalWindows(dbName); | ||
}; | ||
// 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(''); | ||
// the blob already has a type; do nothing | ||
var res = function () {}; | ||
var res$2 = function () {}; | ||
@@ -280,3 +789,3 @@ function defaultBody() { | ||
options = pouchdbUtils.clone(options); | ||
options = clone(options); | ||
@@ -307,3 +816,3 @@ var defaultOptions = { | ||
if (v.error || v.missing) { | ||
return pouchdbErrors.generateErrorFromResponse(v); | ||
return generateErrorFromResponse(v); | ||
} else { | ||
@@ -315,3 +824,3 @@ return v; | ||
if (options.binary) { | ||
res(obj, resp); | ||
res$2(obj, resp); | ||
} | ||
@@ -341,3 +850,3 @@ cb(null, obj, resp); | ||
if (err) { | ||
return callback(pouchdbErrors.generateErrorFromResponse(err)); | ||
return callback(generateErrorFromResponse(err)); | ||
} | ||
@@ -363,3 +872,3 @@ | ||
} else { | ||
error = pouchdbErrors.generateErrorFromResponse(data); | ||
error = generateErrorFromResponse(data); | ||
error.status = response.statusCode; | ||
@@ -366,0 +875,0 @@ callback(error); |
447
lib/index.js
'use strict'; | ||
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
var jsExtend = require('js-extend'); | ||
var pouchdbErrors = require('pouchdb-errors'); | ||
var pouchdbUtils = require('pouchdb-utils'); | ||
var inherits = _interopDefault(require('inherits')); | ||
var lie = _interopDefault(require('lie')); | ||
var getArguments = _interopDefault(require('argsarray')); | ||
var debug = _interopDefault(require('debug')); | ||
var events = require('events'); | ||
@@ -11,2 +16,432 @@ // May seem redundant, but this is to allow switching with | ||
inherits(PouchError, Error); | ||
function PouchError(opts) { | ||
Error.call(this, opts.reason); | ||
this.status = opts.status; | ||
this.name = opts.error; | ||
this.message = opts.reason; | ||
this.error = true; | ||
} | ||
PouchError.prototype.toString = function () { | ||
return JSON.stringify({ | ||
status: this.status, | ||
name: this.name, | ||
message: this.message, | ||
reason: this.reason | ||
}); | ||
}; | ||
var UNAUTHORIZED = new PouchError({ | ||
status: 401, | ||
error: 'unauthorized', | ||
reason: "Name or password is incorrect." | ||
}); | ||
var MISSING_BULK_DOCS = new PouchError({ | ||
status: 400, | ||
error: 'bad_request', | ||
reason: "Missing JSON list of 'docs'" | ||
}); | ||
var MISSING_DOC = new PouchError({ | ||
status: 404, | ||
error: 'not_found', | ||
reason: 'missing' | ||
}); | ||
var REV_CONFLICT = new PouchError({ | ||
status: 409, | ||
error: 'conflict', | ||
reason: 'Document update conflict' | ||
}); | ||
var INVALID_ID = new PouchError({ | ||
status: 400, | ||
error: 'bad_request', | ||
reason: '_id field must contain a string' | ||
}); | ||
var MISSING_ID = new PouchError({ | ||
status: 412, | ||
error: 'missing_id', | ||
reason: '_id is required for puts' | ||
}); | ||
var RESERVED_ID = new PouchError({ | ||
status: 400, | ||
error: 'bad_request', | ||
reason: 'Only reserved document ids may start with underscore.' | ||
}); | ||
var NOT_OPEN = new PouchError({ | ||
status: 412, | ||
error: 'precondition_failed', | ||
reason: 'Database not open' | ||
}); | ||
var UNKNOWN_ERROR = new PouchError({ | ||
status: 500, | ||
error: 'unknown_error', | ||
reason: 'Database encountered an unknown error' | ||
}); | ||
var BAD_ARG = new PouchError({ | ||
status: 500, | ||
error: 'badarg', | ||
reason: 'Some query argument is invalid' | ||
}); | ||
var INVALID_REQUEST = new PouchError({ | ||
status: 400, | ||
error: 'invalid_request', | ||
reason: 'Request was invalid' | ||
}); | ||
var QUERY_PARSE_ERROR = new PouchError({ | ||
status: 400, | ||
error: 'query_parse_error', | ||
reason: 'Some query parameter is invalid' | ||
}); | ||
var DOC_VALIDATION = new PouchError({ | ||
status: 500, | ||
error: 'doc_validation', | ||
reason: 'Bad special document member' | ||
}); | ||
var BAD_REQUEST = new PouchError({ | ||
status: 400, | ||
error: 'bad_request', | ||
reason: 'Something wrong with the request' | ||
}); | ||
var NOT_AN_OBJECT = new PouchError({ | ||
status: 400, | ||
error: 'bad_request', | ||
reason: 'Document must be a JSON object' | ||
}); | ||
var DB_MISSING = new PouchError({ | ||
status: 404, | ||
error: 'not_found', | ||
reason: 'Database not found' | ||
}); | ||
var IDB_ERROR = new PouchError({ | ||
status: 500, | ||
error: 'indexed_db_went_bad', | ||
reason: 'unknown' | ||
}); | ||
var WSQ_ERROR = new PouchError({ | ||
status: 500, | ||
error: 'web_sql_went_bad', | ||
reason: 'unknown' | ||
}); | ||
var LDB_ERROR = new PouchError({ | ||
status: 500, | ||
error: 'levelDB_went_went_bad', | ||
reason: 'unknown' | ||
}); | ||
var FORBIDDEN = new PouchError({ | ||
status: 403, | ||
error: 'forbidden', | ||
reason: 'Forbidden by design doc validate_doc_update function' | ||
}); | ||
var INVALID_REV = new PouchError({ | ||
status: 400, | ||
error: 'bad_request', | ||
reason: 'Invalid rev format' | ||
}); | ||
var FILE_EXISTS = new PouchError({ | ||
status: 412, | ||
error: 'file_exists', | ||
reason: 'The database could not be created, the file already exists.' | ||
}); | ||
var MISSING_STUB = new PouchError({ | ||
status: 412, | ||
error: 'missing_stub' | ||
}); | ||
var INVALID_URL = new PouchError({ | ||
status: 413, | ||
error: 'invalid_url', | ||
reason: 'Provided URL is invalid' | ||
}); | ||
function generateErrorFromResponse(err) { | ||
if (typeof err !== 'object') { | ||
var data = err; | ||
err = UNKNOWN_ERROR; | ||
err.data = data; | ||
} | ||
if ('error' in err && err.error === 'conflict') { | ||
err.name = 'conflict'; | ||
err.status = 409; | ||
} | ||
if (!('name' in err)) { | ||
err.name = err.error || 'unknown'; | ||
} | ||
if (!('status' in err)) { | ||
err.status = 500; | ||
} | ||
if (!('message' in err)) { | ||
err.message = err.message || err.reason; | ||
} | ||
return err; | ||
} | ||
function isBinaryObject(object) { | ||
return object instanceof Buffer; | ||
} | ||
function cloneBinaryObject(object) { | ||
var copy = new Buffer(object.length); | ||
object.copy(copy); | ||
return copy; | ||
} | ||
// most of this is borrowed from lodash.isPlainObject: | ||
// https://github.com/fis-components/lodash.isplainobject/ | ||
// blob/29c358140a74f252aeb08c9eb28bef86f2217d4a/index.js | ||
var funcToString = Function.prototype.toString; | ||
var objectCtorString = funcToString.call(Object); | ||
function isPlainObject(value) { | ||
var proto = Object.getPrototypeOf(value); | ||
/* istanbul ignore if */ | ||
if (proto === null) { // not sure when this happens, but I guess it can | ||
return true; | ||
} | ||
var Ctor = proto.constructor; | ||
return (typeof Ctor == 'function' && | ||
Ctor instanceof Ctor && funcToString.call(Ctor) == objectCtorString); | ||
} | ||
function clone(object) { | ||
var newObject; | ||
var i; | ||
var len; | ||
if (!object || typeof object !== 'object') { | ||
return object; | ||
} | ||
if (Array.isArray(object)) { | ||
newObject = []; | ||
for (i = 0, len = object.length; i < len; i++) { | ||
newObject[i] = clone(object[i]); | ||
} | ||
return newObject; | ||
} | ||
// special case: to avoid inconsistencies between IndexedDB | ||
// and other backends, we automatically stringify Dates | ||
if (object instanceof Date) { | ||
return object.toISOString(); | ||
} | ||
if (isBinaryObject(object)) { | ||
return cloneBinaryObject(object); | ||
} | ||
if (!isPlainObject(object)) { | ||
return object; // don't clone objects like Workers | ||
} | ||
newObject = {}; | ||
for (i in object) { | ||
/* istanbul ignore else */ | ||
if (Object.prototype.hasOwnProperty.call(object, i)) { | ||
var value = clone(object[i]); | ||
if (typeof value !== 'undefined') { | ||
newObject[i] = value; | ||
} | ||
} | ||
} | ||
return newObject; | ||
} | ||
var log = debug('pouchdb:api'); | ||
// 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; | ||
} | ||
// in Node of course this is false | ||
function isChromeApp() { | ||
return false; | ||
} | ||
// in Node of course this is false | ||
function hasLocalStorage() { | ||
return false; | ||
} | ||
inherits(Changes, events.EventEmitter); | ||
/* istanbul ignore next */ | ||
function attachBrowserEvents(self) { | ||
if (isChromeApp()) { | ||
chrome.storage.onChanged.addListener(function (e) { | ||
// make sure it's event addressed to us | ||
if (e.db_name != null) { | ||
//object only has oldValue, newValue members | ||
self.emit(e.dbName.newValue); | ||
} | ||
}); | ||
} else if (hasLocalStorage()) { | ||
if (typeof addEventListener !== 'undefined') { | ||
addEventListener("storage", function (e) { | ||
self.emit(e.key); | ||
}); | ||
} else { // old IE | ||
window.attachEvent("storage", function (e) { | ||
self.emit(e.key); | ||
}); | ||
} | ||
} | ||
} | ||
function Changes() { | ||
events.EventEmitter.call(this); | ||
this._listeners = {}; | ||
attachBrowserEvents(this); | ||
} | ||
Changes.prototype.addListener = function (dbName, id, db, opts) { | ||
/* istanbul ignore if */ | ||
if (this._listeners[id]) { | ||
return; | ||
} | ||
var self = this; | ||
var inprogress = false; | ||
function eventFunction() { | ||
/* istanbul ignore if */ | ||
if (!self._listeners[id]) { | ||
return; | ||
} | ||
if (inprogress) { | ||
inprogress = 'waiting'; | ||
return; | ||
} | ||
inprogress = true; | ||
var changesOpts = pick(opts, [ | ||
'style', 'include_docs', 'attachments', 'conflicts', 'filter', | ||
'doc_ids', 'view', 'since', 'query_params', 'binary' | ||
]); | ||
/* istanbul ignore next */ | ||
function onError() { | ||
inprogress = false; | ||
} | ||
db.changes(changesOpts).on('change', function (c) { | ||
if (c.seq > opts.since && !opts.cancelled) { | ||
opts.since = c.seq; | ||
opts.onChange(c); | ||
} | ||
}).on('complete', function () { | ||
if (inprogress === 'waiting') { | ||
setTimeout(function (){ | ||
eventFunction(); | ||
},0); | ||
} | ||
inprogress = false; | ||
}).on('error', onError); | ||
} | ||
this._listeners[id] = eventFunction; | ||
this.on(dbName, eventFunction); | ||
}; | ||
Changes.prototype.removeListener = function (dbName, id) { | ||
/* istanbul ignore if */ | ||
if (!(id in this._listeners)) { | ||
return; | ||
} | ||
events.EventEmitter.prototype.removeListener.call(this, dbName, | ||
this._listeners[id]); | ||
delete this._listeners[id]; | ||
}; | ||
/* istanbul ignore next */ | ||
Changes.prototype.notifyLocalWindows = function (dbName) { | ||
//do a useless change on a storage thing | ||
//in order to get other windows's listeners to activate | ||
if (isChromeApp()) { | ||
chrome.storage.local.set({dbName: dbName}); | ||
} else if (hasLocalStorage()) { | ||
localStorage[dbName] = (localStorage[dbName] === "a") ? "b" : "a"; | ||
} | ||
}; | ||
Changes.prototype.notify = function (dbName) { | ||
this.emit(dbName); | ||
this.notifyLocalWindows(dbName); | ||
}; | ||
// 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(''); | ||
// non-standard, but we do this to mimic blobs in the browser | ||
@@ -23,3 +458,3 @@ function applyTypeToBuffer(buffer, resp) { | ||
options = pouchdbUtils.clone(options); | ||
options = clone(options); | ||
@@ -50,3 +485,3 @@ var defaultOptions = { | ||
if (v.error || v.missing) { | ||
return pouchdbErrors.generateErrorFromResponse(v); | ||
return generateErrorFromResponse(v); | ||
} else { | ||
@@ -83,3 +518,3 @@ return v; | ||
if (err) { | ||
return callback(pouchdbErrors.generateErrorFromResponse(err)); | ||
return callback(generateErrorFromResponse(err)); | ||
} | ||
@@ -105,3 +540,3 @@ | ||
} else { | ||
error = pouchdbErrors.generateErrorFromResponse(data); | ||
error = generateErrorFromResponse(data); | ||
error.status = response.statusCode; | ||
@@ -108,0 +543,0 @@ callback(error); |
{ | ||
"name": "pouchdb-ajax", | ||
"version": "6.0.2", | ||
"version": "6.0.3", | ||
"description": "PouchDB's ajax() method.", | ||
@@ -27,8 +27,8 @@ "main": "./lib/index.js", | ||
"js-extend": "1.0.1", | ||
"pouchdb-binary-utils": "6.0.2", | ||
"pouchdb-errors": "6.0.2", | ||
"pouchdb-promise": "6.0.2", | ||
"pouchdb-utils": "6.0.2", | ||
"pouchdb-binary-utils": "6.0.3", | ||
"pouchdb-errors": "6.0.3", | ||
"pouchdb-promise": "6.0.3", | ||
"pouchdb-utils": "6.0.3", | ||
"request": "2.74.0" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
58629
1580
+ Addedpouchdb-binary-utils@6.0.3(transitive)
+ Addedpouchdb-collections@6.0.3(transitive)
+ Addedpouchdb-errors@6.0.3(transitive)
+ Addedpouchdb-md5@6.0.3(transitive)
+ Addedpouchdb-promise@6.0.3(transitive)
+ Addedpouchdb-utils@6.0.3(transitive)
- Removedpouchdb-binary-utils@6.0.2(transitive)
- Removedpouchdb-collections@6.0.2(transitive)
- Removedpouchdb-errors@6.0.2(transitive)
- Removedpouchdb-md5@6.0.2(transitive)
- Removedpouchdb-promise@6.0.2(transitive)
- Removedpouchdb-utils@6.0.2(transitive)
Updatedpouchdb-binary-utils@6.0.3
Updatedpouchdb-errors@6.0.3
Updatedpouchdb-promise@6.0.3
Updatedpouchdb-utils@6.0.3