UNPKG

pouchdb-auth

Version:

A PouchDB plug-in that simulates CouchDB's authentication daemon. Includes a users db that functions like CouchDB's.

1 lines 120 kB
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Auth=f()}})(function(){var define,module,exports;return function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r}()({1:[function(require,module,exports){"use strict";function isArguments(thingy){return thingy!=null&&typeof thingy==="object"&&thingy.hasOwnProperty("callee")}var types={"*":{label:"any",check:function(){return true}},A:{label:"array",check:function(thingy){return Array.isArray(thingy)||isArguments(thingy)}},S:{label:"string",check:function(thingy){return typeof thingy==="string"}},N:{label:"number",check:function(thingy){return typeof thingy==="number"}},F:{label:"function",check:function(thingy){return typeof thingy==="function"}},O:{label:"object",check:function(thingy){return typeof thingy==="object"&&thingy!=null&&!types.A.check(thingy)&&!types.E.check(thingy)}},B:{label:"boolean",check:function(thingy){return typeof thingy==="boolean"}},E:{label:"error",check:function(thingy){return thingy instanceof Error}},Z:{label:"null",check:function(thingy){return thingy==null}}};function addSchema(schema,arity){var group=arity[schema.length]=arity[schema.length]||[];if(group.indexOf(schema)===-1)group.push(schema)}var validate=module.exports=function(rawSchemas,args){if(arguments.length!==2)throw wrongNumberOfArgs(["SA"],arguments.length);if(!rawSchemas)throw missingRequiredArg(0,"rawSchemas");if(!args)throw missingRequiredArg(1,"args");if(!types.S.check(rawSchemas))throw invalidType(0,["string"],rawSchemas);if(!types.A.check(args))throw invalidType(1,["array"],args);var schemas=rawSchemas.split("|");var arity={};schemas.forEach(function(schema){for(var ii=0;ii<schema.length;++ii){var type=schema[ii];if(!types[type])throw unknownType(ii,type)}if(/E.*E/.test(schema))throw moreThanOneError(schema);addSchema(schema,arity);if(/E/.test(schema)){addSchema(schema.replace(/E.*$/,"E"),arity);addSchema(schema.replace(/E/,"Z"),arity);if(schema.length===1)addSchema("",arity)}});var matching=arity[args.length];if(!matching){throw wrongNumberOfArgs(Object.keys(arity),args.length)}for(var ii=0;ii<args.length;++ii){var newMatching=matching.filter(function(schema){var type=schema[ii];var typeCheck=types[type].check;return typeCheck(args[ii])});if(!newMatching.length){var labels=matching.map(function(schema){return types[schema[ii]].label}).filter(function(schema){return schema!=null});throw invalidType(ii,labels,args[ii])}matching=newMatching}};function missingRequiredArg(num){return newException("EMISSINGARG","Missing required argument #"+(num+1))}function unknownType(num,type){return newException("EUNKNOWNTYPE","Unknown type "+type+" in argument #"+(num+1))}function invalidType(num,expectedTypes,value){var valueType;Object.keys(types).forEach(function(typeCode){if(types[typeCode].check(value))valueType=types[typeCode].label});return newException("EINVALIDTYPE","Argument #"+(num+1)+": Expected "+englishList(expectedTypes)+" but got "+valueType)}function englishList(list){return list.join(", ").replace(/, ([^,]+)$/," or $1")}function wrongNumberOfArgs(expected,got){var english=englishList(expected);var args=expected.every(function(ex){return ex.length===1})?"argument":"arguments";return newException("EWRONGARGCOUNT","Expected "+english+" "+args+" but got "+got)}function moreThanOneError(schema){return newException("ETOOMANYERRORTYPES",'Only one error type per argument signature is allowed, more than one found in "'+schema+'"')}function newException(code,msg){var e=new Error(msg);e.code=code;if(Error.captureStackTrace)Error.captureStackTrace(e,validate);return e}},{}],2:[function(require,module,exports){"use strict";exports.byteLength=byteLength;exports.toByteArray=toByteArray;exports.fromByteArray=fromByteArray;var lookup=[];var revLookup=[];var Arr=typeof Uint8Array!=="undefined"?Uint8Array:Array;var code="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";for(var i=0,len=code.length;i<len;++i){lookup[i]=code[i];revLookup[code.charCodeAt(i)]=i}revLookup["-".charCodeAt(0)]=62;revLookup["_".charCodeAt(0)]=63;function getLens(b64){var len=b64.length;if(len%4>0){throw new Error("Invalid string. Length must be a multiple of 4")}var validLen=b64.indexOf("=");if(validLen===-1)validLen=len;var placeHoldersLen=validLen===len?0:4-validLen%4;return[validLen,placeHoldersLen]}function byteLength(b64){var lens=getLens(b64);var validLen=lens[0];var placeHoldersLen=lens[1];return(validLen+placeHoldersLen)*3/4-placeHoldersLen}function _byteLength(b64,validLen,placeHoldersLen){return(validLen+placeHoldersLen)*3/4-placeHoldersLen}function toByteArray(b64){var tmp;var lens=getLens(b64);var validLen=lens[0];var placeHoldersLen=lens[1];var arr=new Arr(_byteLength(b64,validLen,placeHoldersLen));var curByte=0;var len=placeHoldersLen>0?validLen-4:validLen;for(var i=0;i<len;i+=4){tmp=revLookup[b64.charCodeAt(i)]<<18|revLookup[b64.charCodeAt(i+1)]<<12|revLookup[b64.charCodeAt(i+2)]<<6|revLookup[b64.charCodeAt(i+3)];arr[curByte++]=tmp>>16&255;arr[curByte++]=tmp>>8&255;arr[curByte++]=tmp&255}if(placeHoldersLen===2){tmp=revLookup[b64.charCodeAt(i)]<<2|revLookup[b64.charCodeAt(i+1)]>>4;arr[curByte++]=tmp&255}if(placeHoldersLen===1){tmp=revLookup[b64.charCodeAt(i)]<<10|revLookup[b64.charCodeAt(i+1)]<<4|revLookup[b64.charCodeAt(i+2)]>>2;arr[curByte++]=tmp>>8&255;arr[curByte++]=tmp&255}return arr}function tripletToBase64(num){return lookup[num>>18&63]+lookup[num>>12&63]+lookup[num>>6&63]+lookup[num&63]}function encodeChunk(uint8,start,end){var tmp;var output=[];for(var i=start;i<end;i+=3){tmp=(uint8[i]<<16&16711680)+(uint8[i+1]<<8&65280)+(uint8[i+2]&255);output.push(tripletToBase64(tmp))}return output.join("")}function fromByteArray(uint8){var tmp;var len=uint8.length;var extraBytes=len%3;var parts=[];var maxChunkLength=16383;for(var i=0,len2=len-extraBytes;i<len2;i+=maxChunkLength){parts.push(encodeChunk(uint8,i,i+maxChunkLength>len2?len2:i+maxChunkLength))}if(extraBytes===1){tmp=uint8[len-1];parts.push(lookup[tmp>>2]+lookup[tmp<<4&63]+"==")}else if(extraBytes===2){tmp=(uint8[len-2]<<8)+uint8[len-1];parts.push(lookup[tmp>>10]+lookup[tmp>>4&63]+lookup[tmp<<2&63]+"=")}return parts.join("")}},{}],3:[function(require,module,exports){(function(Buffer){"use strict";Object.defineProperty(exports,"__esModule",{value:true});const pad_string_1=require("./pad-string");function encode(input,encoding="utf8"){if(Buffer.isBuffer(input)){return fromBase64(input.toString("base64"))}return fromBase64(Buffer.from(input,encoding).toString("base64"))}function decode(base64url,encoding="utf8"){return Buffer.from(toBase64(base64url),"base64").toString(encoding)}function toBase64(base64url){base64url=base64url.toString();return pad_string_1.default(base64url).replace(/\-/g,"+").replace(/_/g,"/")}function fromBase64(base64){return base64.replace(/=/g,"").replace(/\+/g,"-").replace(/\//g,"_")}function toBuffer(base64url){return Buffer.from(toBase64(base64url),"base64")}let base64url=encode;base64url.encode=encode;base64url.decode=decode;base64url.toBase64=toBase64;base64url.fromBase64=fromBase64;base64url.toBuffer=toBuffer;exports.default=base64url}).call(this,require("buffer").Buffer)},{"./pad-string":4,buffer:7}],4:[function(require,module,exports){(function(Buffer){"use strict";Object.defineProperty(exports,"__esModule",{value:true});function padString(input){let segmentLength=4;let stringLength=input.length;let diff=stringLength%segmentLength;if(!diff){return input}let position=stringLength;let padLength=segmentLength-diff;let paddedStringLength=stringLength+padLength;let buffer=Buffer.alloc(paddedStringLength);buffer.write(input);while(padLength--){buffer.write("=",position++)}return buffer.toString()}exports.default=padString}).call(this,require("buffer").Buffer)},{buffer:7}],5:[function(require,module,exports){module.exports=require("./dist/base64url").default;module.exports.default=module.exports},{"./dist/base64url":3}],6:[function(require,module,exports){},{}],7:[function(require,module,exports){(function(Buffer){"use strict";var base64=require("base64-js");var ieee754=require("ieee754");exports.Buffer=Buffer;exports.SlowBuffer=SlowBuffer;exports.INSPECT_MAX_BYTES=50;var K_MAX_LENGTH=2147483647;exports.kMaxLength=K_MAX_LENGTH;Buffer.TYPED_ARRAY_SUPPORT=typedArraySupport();if(!Buffer.TYPED_ARRAY_SUPPORT&&typeof console!=="undefined"&&typeof console.error==="function"){console.error("This browser lacks typed array (Uint8Array) support which is required by "+"`buffer` v5.x. Use `buffer` v4.x if you require old browser support.")}function typedArraySupport(){try{var arr=new Uint8Array(1);arr.__proto__={__proto__:Uint8Array.prototype,foo:function(){return 42}};return arr.foo()===42}catch(e){return false}}Object.defineProperty(Buffer.prototype,"parent",{enumerable:true,get:function(){if(!Buffer.isBuffer(this))return undefined;return this.buffer}});Object.defineProperty(Buffer.prototype,"offset",{enumerable:true,get:function(){if(!Buffer.isBuffer(this))return undefined;return this.byteOffset}});function createBuffer(length){if(length>K_MAX_LENGTH){throw new RangeError('The value "'+length+'" is invalid for option "size"')}var buf=new Uint8Array(length);buf.__proto__=Buffer.prototype;return buf}function Buffer(arg,encodingOrOffset,length){if(typeof arg==="number"){if(typeof encodingOrOffset==="string"){throw new TypeError('The "string" argument must be of type string. Received type number')}return allocUnsafe(arg)}return from(arg,encodingOrOffset,length)}if(typeof Symbol!=="undefined"&&Symbol.species!=null&&Buffer[Symbol.species]===Buffer){Object.defineProperty(Buffer,Symbol.species,{value:null,configurable:true,enumerable:false,writable:false})}Buffer.poolSize=8192;function from(value,encodingOrOffset,length){if(typeof value==="string"){return fromString(value,encodingOrOffset)}if(ArrayBuffer.isView(value)){return fromArrayLike(value)}if(value==null){throw TypeError("The first argument must be one of type string, Buffer, ArrayBuffer, Array, "+"or Array-like Object. Received type "+typeof value)}if(isInstance(value,ArrayBuffer)||value&&isInstance(value.buffer,ArrayBuffer)){return fromArrayBuffer(value,encodingOrOffset,length)}if(typeof value==="number"){throw new TypeError('The "value" argument must not be of type number. Received type number')}var valueOf=value.valueOf&&value.valueOf();if(valueOf!=null&&valueOf!==value){return Buffer.from(valueOf,encodingOrOffset,length)}var b=fromObject(value);if(b)return b;if(typeof Symbol!=="undefined"&&Symbol.toPrimitive!=null&&typeof value[Symbol.toPrimitive]==="function"){return Buffer.from(value[Symbol.toPrimitive]("string"),encodingOrOffset,length)}throw new TypeError("The first argument must be one of type string, Buffer, ArrayBuffer, Array, "+"or Array-like Object. Received type "+typeof value)}Buffer.from=function(value,encodingOrOffset,length){return from(value,encodingOrOffset,length)};Buffer.prototype.__proto__=Uint8Array.prototype;Buffer.__proto__=Uint8Array;function assertSize(size){if(typeof size!=="number"){throw new TypeError('"size" argument must be of type number')}else if(size<0){throw new RangeError('The value "'+size+'" is invalid for option "size"')}}function alloc(size,fill,encoding){assertSize(size);if(size<=0){return createBuffer(size)}if(fill!==undefined){return typeof encoding==="string"?createBuffer(size).fill(fill,encoding):createBuffer(size).fill(fill)}return createBuffer(size)}Buffer.alloc=function(size,fill,encoding){return alloc(size,fill,encoding)};function allocUnsafe(size){assertSize(size);return createBuffer(size<0?0:checked(size)|0)}Buffer.allocUnsafe=function(size){return allocUnsafe(size)};Buffer.allocUnsafeSlow=function(size){return allocUnsafe(size)};function fromString(string,encoding){if(typeof encoding!=="string"||encoding===""){encoding="utf8"}if(!Buffer.isEncoding(encoding)){throw new TypeError("Unknown encoding: "+encoding)}var length=byteLength(string,encoding)|0;var buf=createBuffer(length);var actual=buf.write(string,encoding);if(actual!==length){buf=buf.slice(0,actual)}return buf}function fromArrayLike(array){var length=array.length<0?0:checked(array.length)|0;var buf=createBuffer(length);for(var i=0;i<length;i+=1){buf[i]=array[i]&255}return buf}function fromArrayBuffer(array,byteOffset,length){if(byteOffset<0||array.byteLength<byteOffset){throw new RangeError('"offset" is outside of buffer bounds')}if(array.byteLength<byteOffset+(length||0)){throw new RangeError('"length" is outside of buffer bounds')}var buf;if(byteOffset===undefined&&length===undefined){buf=new Uint8Array(array)}else if(length===undefined){buf=new Uint8Array(array,byteOffset)}else{buf=new Uint8Array(array,byteOffset,length)}buf.__proto__=Buffer.prototype;return buf}function fromObject(obj){if(Buffer.isBuffer(obj)){var len=checked(obj.length)|0;var buf=createBuffer(len);if(buf.length===0){return buf}obj.copy(buf,0,0,len);return buf}if(obj.length!==undefined){if(typeof obj.length!=="number"||numberIsNaN(obj.length)){return createBuffer(0)}return fromArrayLike(obj)}if(obj.type==="Buffer"&&Array.isArray(obj.data)){return fromArrayLike(obj.data)}}function checked(length){if(length>=K_MAX_LENGTH){throw new RangeError("Attempt to allocate Buffer larger than maximum "+"size: 0x"+K_MAX_LENGTH.toString(16)+" bytes")}return length|0}function SlowBuffer(length){if(+length!=length){length=0}return Buffer.alloc(+length)}Buffer.isBuffer=function isBuffer(b){return b!=null&&b._isBuffer===true&&b!==Buffer.prototype};Buffer.compare=function compare(a,b){if(isInstance(a,Uint8Array))a=Buffer.from(a,a.offset,a.byteLength);if(isInstance(b,Uint8Array))b=Buffer.from(b,b.offset,b.byteLength);if(!Buffer.isBuffer(a)||!Buffer.isBuffer(b)){throw new TypeError('The "buf1", "buf2" arguments must be one of type Buffer or Uint8Array')}if(a===b)return 0;var x=a.length;var y=b.length;for(var i=0,len=Math.min(x,y);i<len;++i){if(a[i]!==b[i]){x=a[i];y=b[i];break}}if(x<y)return-1;if(y<x)return 1;return 0};Buffer.isEncoding=function isEncoding(encoding){switch(String(encoding).toLowerCase()){case"hex":case"utf8":case"utf-8":case"ascii":case"latin1":case"binary":case"base64":case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return true;default:return false}};Buffer.concat=function concat(list,length){if(!Array.isArray(list)){throw new TypeError('"list" argument must be an Array of Buffers')}if(list.length===0){return Buffer.alloc(0)}var i;if(length===undefined){length=0;for(i=0;i<list.length;++i){length+=list[i].length}}var buffer=Buffer.allocUnsafe(length);var pos=0;for(i=0;i<list.length;++i){var buf=list[i];if(isInstance(buf,Uint8Array)){buf=Buffer.from(buf)}if(!Buffer.isBuffer(buf)){throw new TypeError('"list" argument must be an Array of Buffers')}buf.copy(buffer,pos);pos+=buf.length}return buffer};function byteLength(string,encoding){if(Buffer.isBuffer(string)){return string.length}if(ArrayBuffer.isView(string)||isInstance(string,ArrayBuffer)){return string.byteLength}if(typeof string!=="string"){throw new TypeError('The "string" argument must be one of type string, Buffer, or ArrayBuffer. '+"Received type "+typeof string)}var len=string.length;var mustMatch=arguments.length>2&&arguments[2]===true;if(!mustMatch&&len===0)return 0;var loweredCase=false;for(;;){switch(encoding){case"ascii":case"latin1":case"binary":return len;case"utf8":case"utf-8":return utf8ToBytes(string).length;case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return len*2;case"hex":return len>>>1;case"base64":return base64ToBytes(string).length;default:if(loweredCase){return mustMatch?-1:utf8ToBytes(string).length}encoding=(""+encoding).toLowerCase();loweredCase=true}}}Buffer.byteLength=byteLength;function slowToString(encoding,start,end){var loweredCase=false;if(start===undefined||start<0){start=0}if(start>this.length){return""}if(end===undefined||end>this.length){end=this.length}if(end<=0){return""}end>>>=0;start>>>=0;if(end<=start){return""}if(!encoding)encoding="utf8";while(true){switch(encoding){case"hex":return hexSlice(this,start,end);case"utf8":case"utf-8":return utf8Slice(this,start,end);case"ascii":return asciiSlice(this,start,end);case"latin1":case"binary":return latin1Slice(this,start,end);case"base64":return base64Slice(this,start,end);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return utf16leSlice(this,start,end);default:if(loweredCase)throw new TypeError("Unknown encoding: "+encoding);encoding=(encoding+"").toLowerCase();loweredCase=true}}}Buffer.prototype._isBuffer=true;function swap(b,n,m){var i=b[n];b[n]=b[m];b[m]=i}Buffer.prototype.swap16=function swap16(){var len=this.length;if(len%2!==0){throw new RangeError("Buffer size must be a multiple of 16-bits")}for(var i=0;i<len;i+=2){swap(this,i,i+1)}return this};Buffer.prototype.swap32=function swap32(){var len=this.length;if(len%4!==0){throw new RangeError("Buffer size must be a multiple of 32-bits")}for(var i=0;i<len;i+=4){swap(this,i,i+3);swap(this,i+1,i+2)}return this};Buffer.prototype.swap64=function swap64(){var len=this.length;if(len%8!==0){throw new RangeError("Buffer size must be a multiple of 64-bits")}for(var i=0;i<len;i+=8){swap(this,i,i+7);swap(this,i+1,i+6);swap(this,i+2,i+5);swap(this,i+3,i+4)}return this};Buffer.prototype.toString=function toString(){var length=this.length;if(length===0)return"";if(arguments.length===0)return utf8Slice(this,0,length);return slowToString.apply(this,arguments)};Buffer.prototype.toLocaleString=Buffer.prototype.toString;Buffer.prototype.equals=function equals(b){if(!Buffer.isBuffer(b))throw new TypeError("Argument must be a Buffer");if(this===b)return true;return Buffer.compare(this,b)===0};Buffer.prototype.inspect=function inspect(){var str="";var max=exports.INSPECT_MAX_BYTES;str=this.toString("hex",0,max).replace(/(.{2})/g,"$1 ").trim();if(this.length>max)str+=" ... ";return"<Buffer "+str+">"};Buffer.prototype.compare=function compare(target,start,end,thisStart,thisEnd){if(isInstance(target,Uint8Array)){target=Buffer.from(target,target.offset,target.byteLength)}if(!Buffer.isBuffer(target)){throw new TypeError('The "target" argument must be one of type Buffer or Uint8Array. '+"Received type "+typeof target)}if(start===undefined){start=0}if(end===undefined){end=target?target.length:0}if(thisStart===undefined){thisStart=0}if(thisEnd===undefined){thisEnd=this.length}if(start<0||end>target.length||thisStart<0||thisEnd>this.length){throw new RangeError("out of range index")}if(thisStart>=thisEnd&&start>=end){return 0}if(thisStart>=thisEnd){return-1}if(start>=end){return 1}start>>>=0;end>>>=0;thisStart>>>=0;thisEnd>>>=0;if(this===target)return 0;var x=thisEnd-thisStart;var y=end-start;var len=Math.min(x,y);var thisCopy=this.slice(thisStart,thisEnd);var targetCopy=target.slice(start,end);for(var i=0;i<len;++i){if(thisCopy[i]!==targetCopy[i]){x=thisCopy[i];y=targetCopy[i];break}}if(x<y)return-1;if(y<x)return 1;return 0};function bidirectionalIndexOf(buffer,val,byteOffset,encoding,dir){if(buffer.length===0)return-1;if(typeof byteOffset==="string"){encoding=byteOffset;byteOffset=0}else if(byteOffset>2147483647){byteOffset=2147483647}else if(byteOffset<-2147483648){byteOffset=-2147483648}byteOffset=+byteOffset;if(numberIsNaN(byteOffset)){byteOffset=dir?0:buffer.length-1}if(byteOffset<0)byteOffset=buffer.length+byteOffset;if(byteOffset>=buffer.length){if(dir)return-1;else byteOffset=buffer.length-1}else if(byteOffset<0){if(dir)byteOffset=0;else return-1}if(typeof val==="string"){val=Buffer.from(val,encoding)}if(Buffer.isBuffer(val)){if(val.length===0){return-1}return arrayIndexOf(buffer,val,byteOffset,encoding,dir)}else if(typeof val==="number"){val=val&255;if(typeof Uint8Array.prototype.indexOf==="function"){if(dir){return Uint8Array.prototype.indexOf.call(buffer,val,byteOffset)}else{return Uint8Array.prototype.lastIndexOf.call(buffer,val,byteOffset)}}return arrayIndexOf(buffer,[val],byteOffset,encoding,dir)}throw new TypeError("val must be string, number or Buffer")}function arrayIndexOf(arr,val,byteOffset,encoding,dir){var indexSize=1;var arrLength=arr.length;var valLength=val.length;if(encoding!==undefined){encoding=String(encoding).toLowerCase();if(encoding==="ucs2"||encoding==="ucs-2"||encoding==="utf16le"||encoding==="utf-16le"){if(arr.length<2||val.length<2){return-1}indexSize=2;arrLength/=2;valLength/=2;byteOffset/=2}}function read(buf,i){if(indexSize===1){return buf[i]}else{return buf.readUInt16BE(i*indexSize)}}var i;if(dir){var foundIndex=-1;for(i=byteOffset;i<arrLength;i++){if(read(arr,i)===read(val,foundIndex===-1?0:i-foundIndex)){if(foundIndex===-1)foundIndex=i;if(i-foundIndex+1===valLength)return foundIndex*indexSize}else{if(foundIndex!==-1)i-=i-foundIndex;foundIndex=-1}}}else{if(byteOffset+valLength>arrLength)byteOffset=arrLength-valLength;for(i=byteOffset;i>=0;i--){var found=true;for(var j=0;j<valLength;j++){if(read(arr,i+j)!==read(val,j)){found=false;break}}if(found)return i}}return-1}Buffer.prototype.includes=function includes(val,byteOffset,encoding){return this.indexOf(val,byteOffset,encoding)!==-1};Buffer.prototype.indexOf=function indexOf(val,byteOffset,encoding){return bidirectionalIndexOf(this,val,byteOffset,encoding,true)};Buffer.prototype.lastIndexOf=function lastIndexOf(val,byteOffset,encoding){return bidirectionalIndexOf(this,val,byteOffset,encoding,false)};function hexWrite(buf,string,offset,length){offset=Number(offset)||0;var remaining=buf.length-offset;if(!length){length=remaining}else{length=Number(length);if(length>remaining){length=remaining}}var strLen=string.length;if(length>strLen/2){length=strLen/2}for(var i=0;i<length;++i){var parsed=parseInt(string.substr(i*2,2),16);if(numberIsNaN(parsed))return i;buf[offset+i]=parsed}return i}function utf8Write(buf,string,offset,length){return blitBuffer(utf8ToBytes(string,buf.length-offset),buf,offset,length)}function asciiWrite(buf,string,offset,length){return blitBuffer(asciiToBytes(string),buf,offset,length)}function latin1Write(buf,string,offset,length){return asciiWrite(buf,string,offset,length)}function base64Write(buf,string,offset,length){return blitBuffer(base64ToBytes(string),buf,offset,length)}function ucs2Write(buf,string,offset,length){return blitBuffer(utf16leToBytes(string,buf.length-offset),buf,offset,length)}Buffer.prototype.write=function write(string,offset,length,encoding){if(offset===undefined){encoding="utf8";length=this.length;offset=0}else if(length===undefined&&typeof offset==="string"){encoding=offset;length=this.length;offset=0}else if(isFinite(offset)){offset=offset>>>0;if(isFinite(length)){length=length>>>0;if(encoding===undefined)encoding="utf8"}else{encoding=length;length=undefined}}else{throw new Error("Buffer.write(string, encoding, offset[, length]) is no longer supported")}var remaining=this.length-offset;if(length===undefined||length>remaining)length=remaining;if(string.length>0&&(length<0||offset<0)||offset>this.length){throw new RangeError("Attempt to write outside buffer bounds")}if(!encoding)encoding="utf8";var loweredCase=false;for(;;){switch(encoding){case"hex":return hexWrite(this,string,offset,length);case"utf8":case"utf-8":return utf8Write(this,string,offset,length);case"ascii":return asciiWrite(this,string,offset,length);case"latin1":case"binary":return latin1Write(this,string,offset,length);case"base64":return base64Write(this,string,offset,length);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return ucs2Write(this,string,offset,length);default:if(loweredCase)throw new TypeError("Unknown encoding: "+encoding);encoding=(""+encoding).toLowerCase();loweredCase=true}}};Buffer.prototype.toJSON=function toJSON(){return{type:"Buffer",data:Array.prototype.slice.call(this._arr||this,0)}};function base64Slice(buf,start,end){if(start===0&&end===buf.length){return base64.fromByteArray(buf)}else{return base64.fromByteArray(buf.slice(start,end))}}function utf8Slice(buf,start,end){end=Math.min(buf.length,end);var res=[];var i=start;while(i<end){var firstByte=buf[i];var codePoint=null;var bytesPerSequence=firstByte>239?4:firstByte>223?3:firstByte>191?2:1;if(i+bytesPerSequence<=end){var secondByte,thirdByte,fourthByte,tempCodePoint;switch(bytesPerSequence){case 1:if(firstByte<128){codePoint=firstByte}break;case 2:secondByte=buf[i+1];if((secondByte&192)===128){tempCodePoint=(firstByte&31)<<6|secondByte&63;if(tempCodePoint>127){codePoint=tempCodePoint}}break;case 3:secondByte=buf[i+1];thirdByte=buf[i+2];if((secondByte&192)===128&&(thirdByte&192)===128){tempCodePoint=(firstByte&15)<<12|(secondByte&63)<<6|thirdByte&63;if(tempCodePoint>2047&&(tempCodePoint<55296||tempCodePoint>57343)){codePoint=tempCodePoint}}break;case 4:secondByte=buf[i+1];thirdByte=buf[i+2];fourthByte=buf[i+3];if((secondByte&192)===128&&(thirdByte&192)===128&&(fourthByte&192)===128){tempCodePoint=(firstByte&15)<<18|(secondByte&63)<<12|(thirdByte&63)<<6|fourthByte&63;if(tempCodePoint>65535&&tempCodePoint<1114112){codePoint=tempCodePoint}}}}if(codePoint===null){codePoint=65533;bytesPerSequence=1}else if(codePoint>65535){codePoint-=65536;res.push(codePoint>>>10&1023|55296);codePoint=56320|codePoint&1023}res.push(codePoint);i+=bytesPerSequence}return decodeCodePointsArray(res)}var MAX_ARGUMENTS_LENGTH=4096;function decodeCodePointsArray(codePoints){var len=codePoints.length;if(len<=MAX_ARGUMENTS_LENGTH){return String.fromCharCode.apply(String,codePoints)}var res="";var i=0;while(i<len){res+=String.fromCharCode.apply(String,codePoints.slice(i,i+=MAX_ARGUMENTS_LENGTH))}return res}function asciiSlice(buf,start,end){var ret="";end=Math.min(buf.length,end);for(var i=start;i<end;++i){ret+=String.fromCharCode(buf[i]&127)}return ret}function latin1Slice(buf,start,end){var ret="";end=Math.min(buf.length,end);for(var i=start;i<end;++i){ret+=String.fromCharCode(buf[i])}return ret}function hexSlice(buf,start,end){var len=buf.length;if(!start||start<0)start=0;if(!end||end<0||end>len)end=len;var out="";for(var i=start;i<end;++i){out+=toHex(buf[i])}return out}function utf16leSlice(buf,start,end){var bytes=buf.slice(start,end);var res="";for(var i=0;i<bytes.length;i+=2){res+=String.fromCharCode(bytes[i]+bytes[i+1]*256)}return res}Buffer.prototype.slice=function slice(start,end){var len=this.length;start=~~start;end=end===undefined?len:~~end;if(start<0){start+=len;if(start<0)start=0}else if(start>len){start=len}if(end<0){end+=len;if(end<0)end=0}else if(end>len){end=len}if(end<start)end=start;var newBuf=this.subarray(start,end);newBuf.__proto__=Buffer.prototype;return newBuf};function checkOffset(offset,ext,length){if(offset%1!==0||offset<0)throw new RangeError("offset is not uint");if(offset+ext>length)throw new RangeError("Trying to access beyond buffer length")}Buffer.prototype.readUIntLE=function readUIntLE(offset,byteLength,noAssert){offset=offset>>>0;byteLength=byteLength>>>0;if(!noAssert)checkOffset(offset,byteLength,this.length);var val=this[offset];var mul=1;var i=0;while(++i<byteLength&&(mul*=256)){val+=this[offset+i]*mul}return val};Buffer.prototype.readUIntBE=function readUIntBE(offset,byteLength,noAssert){offset=offset>>>0;byteLength=byteLength>>>0;if(!noAssert){checkOffset(offset,byteLength,this.length)}var val=this[offset+--byteLength];var mul=1;while(byteLength>0&&(mul*=256)){val+=this[offset+--byteLength]*mul}return val};Buffer.prototype.readUInt8=function readUInt8(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,1,this.length);return this[offset]};Buffer.prototype.readUInt16LE=function readUInt16LE(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,2,this.length);return this[offset]|this[offset+1]<<8};Buffer.prototype.readUInt16BE=function readUInt16BE(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,2,this.length);return this[offset]<<8|this[offset+1]};Buffer.prototype.readUInt32LE=function readUInt32LE(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,4,this.length);return(this[offset]|this[offset+1]<<8|this[offset+2]<<16)+this[offset+3]*16777216};Buffer.prototype.readUInt32BE=function readUInt32BE(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,4,this.length);return this[offset]*16777216+(this[offset+1]<<16|this[offset+2]<<8|this[offset+3])};Buffer.prototype.readIntLE=function readIntLE(offset,byteLength,noAssert){offset=offset>>>0;byteLength=byteLength>>>0;if(!noAssert)checkOffset(offset,byteLength,this.length);var val=this[offset];var mul=1;var i=0;while(++i<byteLength&&(mul*=256)){val+=this[offset+i]*mul}mul*=128;if(val>=mul)val-=Math.pow(2,8*byteLength);return val};Buffer.prototype.readIntBE=function readIntBE(offset,byteLength,noAssert){offset=offset>>>0;byteLength=byteLength>>>0;if(!noAssert)checkOffset(offset,byteLength,this.length);var i=byteLength;var mul=1;var val=this[offset+--i];while(i>0&&(mul*=256)){val+=this[offset+--i]*mul}mul*=128;if(val>=mul)val-=Math.pow(2,8*byteLength);return val};Buffer.prototype.readInt8=function readInt8(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,1,this.length);if(!(this[offset]&128))return this[offset];return(255-this[offset]+1)*-1};Buffer.prototype.readInt16LE=function readInt16LE(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,2,this.length);var val=this[offset]|this[offset+1]<<8;return val&32768?val|4294901760:val};Buffer.prototype.readInt16BE=function readInt16BE(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,2,this.length);var val=this[offset+1]|this[offset]<<8;return val&32768?val|4294901760:val};Buffer.prototype.readInt32LE=function readInt32LE(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,4,this.length);return this[offset]|this[offset+1]<<8|this[offset+2]<<16|this[offset+3]<<24};Buffer.prototype.readInt32BE=function readInt32BE(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,4,this.length);return this[offset]<<24|this[offset+1]<<16|this[offset+2]<<8|this[offset+3]};Buffer.prototype.readFloatLE=function readFloatLE(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,4,this.length);return ieee754.read(this,offset,true,23,4)};Buffer.prototype.readFloatBE=function readFloatBE(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,4,this.length);return ieee754.read(this,offset,false,23,4)};Buffer.prototype.readDoubleLE=function readDoubleLE(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,8,this.length);return ieee754.read(this,offset,true,52,8)};Buffer.prototype.readDoubleBE=function readDoubleBE(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,8,this.length);return ieee754.read(this,offset,false,52,8)};function checkInt(buf,value,offset,ext,max,min){if(!Buffer.isBuffer(buf))throw new TypeError('"buffer" argument must be a Buffer instance');if(value>max||value<min)throw new RangeError('"value" argument is out of bounds');if(offset+ext>buf.length)throw new RangeError("Index out of range")}Buffer.prototype.writeUIntLE=function writeUIntLE(value,offset,byteLength,noAssert){value=+value;offset=offset>>>0;byteLength=byteLength>>>0;if(!noAssert){var maxBytes=Math.pow(2,8*byteLength)-1;checkInt(this,value,offset,byteLength,maxBytes,0)}var mul=1;var i=0;this[offset]=value&255;while(++i<byteLength&&(mul*=256)){this[offset+i]=value/mul&255}return offset+byteLength};Buffer.prototype.writeUIntBE=function writeUIntBE(value,offset,byteLength,noAssert){value=+value;offset=offset>>>0;byteLength=byteLength>>>0;if(!noAssert){var maxBytes=Math.pow(2,8*byteLength)-1;checkInt(this,value,offset,byteLength,maxBytes,0)}var i=byteLength-1;var mul=1;this[offset+i]=value&255;while(--i>=0&&(mul*=256)){this[offset+i]=value/mul&255}return offset+byteLength};Buffer.prototype.writeUInt8=function writeUInt8(value,offset,noAssert){value=+value;offset=offset>>>0;if(!noAssert)checkInt(this,value,offset,1,255,0);this[offset]=value&255;return offset+1};Buffer.prototype.writeUInt16LE=function writeUInt16LE(value,offset,noAssert){value=+value;offset=offset>>>0;if(!noAssert)checkInt(this,value,offset,2,65535,0);this[offset]=value&255;this[offset+1]=value>>>8;return offset+2};Buffer.prototype.writeUInt16BE=function writeUInt16BE(value,offset,noAssert){value=+value;offset=offset>>>0;if(!noAssert)checkInt(this,value,offset,2,65535,0);this[offset]=value>>>8;this[offset+1]=value&255;return offset+2};Buffer.prototype.writeUInt32LE=function writeUInt32LE(value,offset,noAssert){value=+value;offset=offset>>>0;if(!noAssert)checkInt(this,value,offset,4,4294967295,0);this[offset+3]=value>>>24;this[offset+2]=value>>>16;this[offset+1]=value>>>8;this[offset]=value&255;return offset+4};Buffer.prototype.writeUInt32BE=function writeUInt32BE(value,offset,noAssert){value=+value;offset=offset>>>0;if(!noAssert)checkInt(this,value,offset,4,4294967295,0);this[offset]=value>>>24;this[offset+1]=value>>>16;this[offset+2]=value>>>8;this[offset+3]=value&255;return offset+4};Buffer.prototype.writeIntLE=function writeIntLE(value,offset,byteLength,noAssert){value=+value;offset=offset>>>0;if(!noAssert){var limit=Math.pow(2,8*byteLength-1);checkInt(this,value,offset,byteLength,limit-1,-limit)}var i=0;var mul=1;var sub=0;this[offset]=value&255;while(++i<byteLength&&(mul*=256)){if(value<0&&sub===0&&this[offset+i-1]!==0){sub=1}this[offset+i]=(value/mul>>0)-sub&255}return offset+byteLength};Buffer.prototype.writeIntBE=function writeIntBE(value,offset,byteLength,noAssert){value=+value;offset=offset>>>0;if(!noAssert){var limit=Math.pow(2,8*byteLength-1);checkInt(this,value,offset,byteLength,limit-1,-limit)}var i=byteLength-1;var mul=1;var sub=0;this[offset+i]=value&255;while(--i>=0&&(mul*=256)){if(value<0&&sub===0&&this[offset+i+1]!==0){sub=1}this[offset+i]=(value/mul>>0)-sub&255}return offset+byteLength};Buffer.prototype.writeInt8=function writeInt8(value,offset,noAssert){value=+value;offset=offset>>>0;if(!noAssert)checkInt(this,value,offset,1,127,-128);if(value<0)value=255+value+1;this[offset]=value&255;return offset+1};Buffer.prototype.writeInt16LE=function writeInt16LE(value,offset,noAssert){value=+value;offset=offset>>>0;if(!noAssert)checkInt(this,value,offset,2,32767,-32768);this[offset]=value&255;this[offset+1]=value>>>8;return offset+2};Buffer.prototype.writeInt16BE=function writeInt16BE(value,offset,noAssert){value=+value;offset=offset>>>0;if(!noAssert)checkInt(this,value,offset,2,32767,-32768);this[offset]=value>>>8;this[offset+1]=value&255;return offset+2};Buffer.prototype.writeInt32LE=function writeInt32LE(value,offset,noAssert){value=+value;offset=offset>>>0;if(!noAssert)checkInt(this,value,offset,4,2147483647,-2147483648);this[offset]=value&255;this[offset+1]=value>>>8;this[offset+2]=value>>>16;this[offset+3]=value>>>24;return offset+4};Buffer.prototype.writeInt32BE=function writeInt32BE(value,offset,noAssert){value=+value;offset=offset>>>0;if(!noAssert)checkInt(this,value,offset,4,2147483647,-2147483648);if(value<0)value=4294967295+value+1;this[offset]=value>>>24;this[offset+1]=value>>>16;this[offset+2]=value>>>8;this[offset+3]=value&255;return offset+4};function checkIEEE754(buf,value,offset,ext,max,min){if(offset+ext>buf.length)throw new RangeError("Index out of range");if(offset<0)throw new RangeError("Index out of range")}function writeFloat(buf,value,offset,littleEndian,noAssert){value=+value;offset=offset>>>0;if(!noAssert){checkIEEE754(buf,value,offset,4,3.4028234663852886e38,-3.4028234663852886e38)}ieee754.write(buf,value,offset,littleEndian,23,4);return offset+4}Buffer.prototype.writeFloatLE=function writeFloatLE(value,offset,noAssert){return writeFloat(this,value,offset,true,noAssert)};Buffer.prototype.writeFloatBE=function writeFloatBE(value,offset,noAssert){return writeFloat(this,value,offset,false,noAssert)};function writeDouble(buf,value,offset,littleEndian,noAssert){value=+value;offset=offset>>>0;if(!noAssert){checkIEEE754(buf,value,offset,8,1.7976931348623157e308,-1.7976931348623157e308)}ieee754.write(buf,value,offset,littleEndian,52,8);return offset+8}Buffer.prototype.writeDoubleLE=function writeDoubleLE(value,offset,noAssert){return writeDouble(this,value,offset,true,noAssert)};Buffer.prototype.writeDoubleBE=function writeDoubleBE(value,offset,noAssert){return writeDouble(this,value,offset,false,noAssert)};Buffer.prototype.copy=function copy(target,targetStart,start,end){if(!Buffer.isBuffer(target))throw new TypeError("argument should be a Buffer");if(!start)start=0;if(!end&&end!==0)end=this.length;if(targetStart>=target.length)targetStart=target.length;if(!targetStart)targetStart=0;if(end>0&&end<start)end=start;if(end===start)return 0;if(target.length===0||this.length===0)return 0;if(targetStart<0){throw new RangeError("targetStart out of bounds")}if(start<0||start>=this.length)throw new RangeError("Index out of range");if(end<0)throw new RangeError("sourceEnd out of bounds");if(end>this.length)end=this.length;if(target.length-targetStart<end-start){end=target.length-targetStart+start}var len=end-start;if(this===target&&typeof Uint8Array.prototype.copyWithin==="function"){this.copyWithin(targetStart,start,end)}else if(this===target&&start<targetStart&&targetStart<end){for(var i=len-1;i>=0;--i){target[i+targetStart]=this[i+start]}}else{Uint8Array.prototype.set.call(target,this.subarray(start,end),targetStart)}return len};Buffer.prototype.fill=function fill(val,start,end,encoding){if(typeof val==="string"){if(typeof start==="string"){encoding=start;start=0;end=this.length}else if(typeof end==="string"){encoding=end;end=this.length}if(encoding!==undefined&&typeof encoding!=="string"){throw new TypeError("encoding must be a string")}if(typeof encoding==="string"&&!Buffer.isEncoding(encoding)){throw new TypeError("Unknown encoding: "+encoding)}if(val.length===1){var code=val.charCodeAt(0);if(encoding==="utf8"&&code<128||encoding==="latin1"){val=code}}}else if(typeof val==="number"){val=val&255}if(start<0||this.length<start||this.length<end){throw new RangeError("Out of range index")}if(end<=start){return this}start=start>>>0;end=end===undefined?this.length:end>>>0;if(!val)val=0;var i;if(typeof val==="number"){for(i=start;i<end;++i){this[i]=val}}else{var bytes=Buffer.isBuffer(val)?val:Buffer.from(val,encoding);var len=bytes.length;if(len===0){throw new TypeError('The value "'+val+'" is invalid for argument "value"')}for(i=0;i<end-start;++i){this[i+start]=bytes[i%len]}}return this};var INVALID_BASE64_RE=/[^+/0-9A-Za-z-_]/g;function base64clean(str){str=str.split("=")[0];str=str.trim().replace(INVALID_BASE64_RE,"");if(str.length<2)return"";while(str.length%4!==0){str=str+"="}return str}function toHex(n){if(n<16)return"0"+n.toString(16);return n.toString(16)}function utf8ToBytes(string,units){units=units||Infinity;var codePoint;var length=string.length;var leadSurrogate=null;var bytes=[];for(var i=0;i<length;++i){codePoint=string.charCodeAt(i);if(codePoint>55295&&codePoint<57344){if(!leadSurrogate){if(codePoint>56319){if((units-=3)>-1)bytes.push(239,191,189);continue}else if(i+1===length){if((units-=3)>-1)bytes.push(239,191,189);continue}leadSurrogate=codePoint;continue}if(codePoint<56320){if((units-=3)>-1)bytes.push(239,191,189);leadSurrogate=codePoint;continue}codePoint=(leadSurrogate-55296<<10|codePoint-56320)+65536}else if(leadSurrogate){if((units-=3)>-1)bytes.push(239,191,189)}leadSurrogate=null;if(codePoint<128){if((units-=1)<0)break;bytes.push(codePoint)}else if(codePoint<2048){if((units-=2)<0)break;bytes.push(codePoint>>6|192,codePoint&63|128)}else if(codePoint<65536){if((units-=3)<0)break;bytes.push(codePoint>>12|224,codePoint>>6&63|128,codePoint&63|128)}else if(codePoint<1114112){if((units-=4)<0)break;bytes.push(codePoint>>18|240,codePoint>>12&63|128,codePoint>>6&63|128,codePoint&63|128)}else{throw new Error("Invalid code point")}}return bytes}function asciiToBytes(str){var byteArray=[];for(var i=0;i<str.length;++i){byteArray.push(str.charCodeAt(i)&255)}return byteArray}function utf16leToBytes(str,units){var c,hi,lo;var byteArray=[];for(var i=0;i<str.length;++i){if((units-=2)<0)break;c=str.charCodeAt(i);hi=c>>8;lo=c%256;byteArray.push(lo);byteArray.push(hi)}return byteArray}function base64ToBytes(str){return base64.toByteArray(base64clean(str))}function blitBuffer(src,dst,offset,length){for(var i=0;i<length;++i){if(i+offset>=dst.length||i>=src.length)break;dst[i+offset]=src[i]}return i}function isInstance(obj,type){return obj instanceof type||obj!=null&&obj.constructor!=null&&obj.constructor.name!=null&&obj.constructor.name===type.name}function numberIsNaN(obj){return obj!==obj}}).call(this,require("buffer").Buffer)},{"base64-js":2,buffer:7,ieee754:18}],8:[function(require,module,exports){(function(Buffer){module.exports=function(key,message){return new Buffer(require("crypto-lite").crypto.hmac("sha1",key,message),"hex")}}).call(this,require("buffer").Buffer)},{buffer:7,"crypto-lite":13}],9:[function(require,module,exports){(function(Buffer){module.exports=calculateSessionId;var base64url=require("base64url");var createHmac=require("./hmac");var validate=require("aproba");function calculateSessionId(username,usersalt,serversecret,timestamp){validate("SSSN",arguments);var timestamp16=timestamp.toString(16).toUpperCase();var sessionData=username+":"+timestamp16;var hmac=createHmac(serversecret+usersalt,sessionData);return base64url(Buffer.concat([new Buffer(sessionData+":"),hmac]))}}).call(this,require("buffer").Buffer)},{"./hmac":8,aproba:1,base64url:12,buffer:7}],10:[function(require,module,exports){(function(Buffer){"use strict";var pad_string_1=require("./pad-string");function encode(input,encoding){if(encoding===void 0){encoding="utf8"}if(Buffer.isBuffer(input)){return fromBase64(input.toString("base64"))}return fromBase64(new Buffer(input,encoding).toString("base64"))}function decode(base64url,encoding){if(encoding===void 0){encoding="utf8"}return new Buffer(toBase64(base64url),"base64").toString(encoding)}function toBase64(base64url){base64url=base64url.toString();return pad_string_1.default(base64url).replace(/\-/g,"+").replace(/_/g,"/")}function fromBase64(base64){return base64.replace(/=/g,"").replace(/\+/g,"-").replace(/\//g,"_")}function toBuffer(base64url){return new Buffer(toBase64(base64url),"base64")}var base64url=encode;base64url.encode=encode;base64url.decode=decode;base64url.toBase64=toBase64;base64url.fromBase64=fromBase64;base64url.toBuffer=toBuffer;Object.defineProperty(exports,"__esModule",{value:true});exports.default=base64url}).call(this,require("buffer").Buffer)},{"./pad-string":11,buffer:7}],11:[function(require,module,exports){(function(Buffer){"use strict";function padString(input){var segmentLength=4;var stringLength=input.length;var diff=stringLength%segmentLength;if(!diff){return input}var position=stringLength;var padLength=segmentLength-diff;var paddedStringLength=stringLength+padLength;var buffer=new Buffer(paddedStringLength);buffer.write(input);while(padLength--){buffer.write("=",position++)}return buffer.toString()}Object.defineProperty(exports,"__esModule",{value:true});exports.default=padString}).call(this,require("buffer").Buffer)},{buffer:7}],12:[function(require,module,exports){arguments[4][5][0].apply(exports,arguments)},{"./dist/base64url":10,dup:5}],13:[function(require,module,exports){!function(exports){var crypto=exports.crypto||(exports.crypto={});function i2s(a){for(var i=a.length;i--;)a[i]=("0000000"+(a[i]>>>0).toString(16)).slice(-8);return a.join("")}function s2i(_s){var s=unescape(encodeURIComponent(_s)),len=s.length,i=0,bin=[];for(;i<len;){bin[i>>2]=s.charCodeAt(i++)<<24|s.charCodeAt(i++)<<16|s.charCodeAt(i++)<<8|s.charCodeAt(i++)}bin.len=len;return bin}function hmac(hasher,_key,_txt){var i=0,ipad=[],opad=[],key=(_key.length>64?hasher:s2i)(_key),txt=typeof _txt=="string"?s2i(_txt):_txt,len=txt.len||txt.length*4;for(;i<16;){ipad[i]=key[i]^909522486;opad[i]=key[i++]^1549556828}return hasher(opad.concat(hasher(ipad.concat(txt),64+len)))}crypto.hmac=function(digest,key,message){return i2s(hmac(digest=="sha256"?sha256:sha1,key,message))};crypto.pbkdf2=pbkdf2;function pbkdf2(secret,salt,count,length,digest){var hasher=digest=="sha256"?sha256:sha1;count=count||1e3;var u,ui,i,j,k,out=[],wlen=length>>2||5;for(k=1;out.length<wlen;k++){u=ui=hmac(hasher,secret,salt+String.fromCharCode(k>>24&15,k>>16&15,k>>8&15,k&15));for(i=count;--i;){ui=hmac(hasher,secret,ui);for(j=ui.length;j--;)u[j]^=ui[j]}out.push.apply(out,u)}return i2s(out).slice(0,length*2||40)}function shaInit(bin,len){if(typeof bin=="string"){bin=s2i(bin);len=bin.len}else len=len||bin.length<<2;bin[len>>2]|=128<<24-(31&(len<<=3));bin[(len+64>>9<<4)+15]=len;return bin}function l(x,n){return x<<n|x>>>32-n}function sha1(data,_len){var a,b,c,d,e,t,j,i=0,w=[],A=1732584193,B=4023233417,C=2562383102,D=271733878,E=3285377520,bin=shaInit(data,_len),len=bin.length;for(;i<len;i+=16,A+=a,B+=b,C+=c,D+=d,E+=e){for(j=0,a=A,b=B,c=C,d=D,e=E;j<80;){w[j]=j<16?bin[i+j]:l(w[j-3]^w[j-8]^w[j-14]^w[j-16],1);t=(j<20?(b&c|~b&d)+1518500249:j<40?(b^c^d)+1859775393:j<60?(b&c|b&d|c&d)+2400959708:(b^c^d)+3395469782)+l(a,5)+e+(w[j++]|0);e=d;d=c;c=l(b,30);b=a;a=t|0}}return[A,B,C,D,E]}crypto.sha1=function(data){return i2s(sha1(data))};var initial_map=[],constants_map=[];function buildMaps(){function a(e){return(e-(e>>>0))*4294967296|0}outer:for(var b=0,c=2,d;b<64;c++){for(d=2;d*d<=c;d++)if(c%d===0)continue outer;if(b<8)initial_map[b]=a(Math.pow(c,.5));constants_map[b++]=a(Math.pow(c,1/3))}}function sha256(data,_len){initial_map[0]||buildMaps();var a,b,c,d,e,f,g,h,t1,t2,j,i=0,w=[],A=initial_map[0],B=initial_map[1],C=initial_map[2],D=initial_map[3],E=initial_map[4],F=initial_map[5],G=initial_map[6],H=initial_map[7],bin=shaInit(data,_len),len=bin.length,K=constants_map;for(;i<len;){a=A;b=B;c=C;d=D;e=E;f=F;g=G;h=H;for(j=0;j<64;){if(j<16)w[j]=bin[i+j];else{t1=w[j-2];t2=w[j-15];w[j]=(t1>>>17^t1<<15^t1>>>19^t1<<13^t1>>>10)+(w[j-7]|0)+(t2>>>7^t2<<25^t2>>>18^t2<<14^t2>>>3)+(w[j-16]|0)}t1=(w[j]|0)+h+(e>>>6^e<<26^e>>>11^e<<21^e>>>25^e<<7)+(e&f^~e&g)+K[j++];t2=(a>>>2^a<<30^a>>>13^a<<19^a>>>22^a<<10)+(a&b^a&c^b&c);h=g;g=f;f=e;e=d+t1|0;d=c;c=b;b=a;a=t1+t2|0}A+=a;B+=b;C+=c;D+=d;E+=e;F+=f;G+=g;H+=h;i+=16}return[A,B,C,D,E,F,G,H]}crypto.sha256=function(data){return i2s(sha256(data))}}(this)},{}],14:[function(require,module,exports){arguments[4][13][0].apply(exports,arguments)},{dup:13}],15:[function(require,module,exports){var objectCreate=Object.create||objectCreatePolyfill;var objectKeys=Object.keys||objectKeysPolyfill;var bind=Function.prototype.bind||functionBindPolyfill;function EventEmitter(){if(!this._events||!Object.prototype.hasOwnProperty.call(this,"_events")){this._events=objectCreate(null);this._eventsCount=0}this._maxListeners=this._maxListeners||undefined}module.exports=EventEmitter;EventEmitter.EventEmitter=EventEmitter;EventEmitter.prototype._events=undefined;EventEmitter.prototype._maxListeners=undefined;var defaultMaxListeners=10;var hasDefineProperty;try{var o={};if(Object.defineProperty)Object.defineProperty(o,"x",{value:0});hasDefineProperty=o.x===0}catch(err){hasDefineProperty=false}if(hasDefineProperty){Object.defineProperty(EventEmitter,"defaultMaxListeners",{enumerable:true,get:function(){return defaultMaxListeners},set:function(arg){if(typeof arg!=="number"||arg<0||arg!==arg)throw new TypeError('"defaultMaxListeners" must be a positive number');defaultMaxListeners=arg}})}else{EventEmitter.defaultMaxListeners=defaultMaxListeners}EventEmitter.prototype.setMaxListeners=function setMaxListeners(n){if(typeof n!=="number"||n<0||isNaN(n))throw new TypeError('"n" argument must be a positive number');this._maxListeners=n;return this};function $getMaxListeners(that){if(that._maxListeners===undefined)return EventEmitter.defaultMaxListeners;return that._maxListeners}EventEmitter.prototype.getMaxListeners=function getMaxListeners(){return $getMaxListeners(this)};function emitNone(handler,isFn,self){if(isFn)handler.call(self);else{var len=handler.length;var listeners=arrayClone(handler,len);for(var i=0;i<len;++i)listeners[i].call(self)}}function emitOne(handler,isFn,self,arg1){if(isFn)handler.call(self,arg1);else{var len=handler.length;var listeners=arrayClone(handler,len);for(var i=0;i<len;++i)listeners[i].call(self,arg1)}}function emitTwo(handler,isFn,self,arg1,arg2){if(isFn)handler.call(self,arg1,arg2);else{var len=handler.length;var listeners=arrayClone(handler,len);for(var i=0;i<len;++i)listeners[i].call(self,arg1,arg2)}}function emitThree(handler,isFn,self,arg1,arg2,arg3){if(isFn)handler.call(self,arg1,arg2,arg3);else{var len=handler.length;var listeners=arrayClone(handler,len);for(var i=0;i<len;++i)listeners[i].call(self,arg1,arg2,arg3)}}function emitMany(handler,isFn,self,args){if(isFn)handler.apply(self,args);else{var len=handler.length;var listeners=arrayClone(handler,len);for(var i=0;i<len;++i)listeners[i].apply(self,args)}}EventEmitter.prototype.emit=function emit(type){var er,handler,len,args,i,events;var doError=type==="error";events=this._events;if(events)doError=doError&&events.error==null;else if(!doError)return false;if(doError){if(arguments.length>1)er=arguments[1];if(er instanceof Error){throw er}else{var err=new Error('Unhandled "error" event. ('+er+")");err.context=er;throw err}return false}handler=events[type];if(!handler)return false;var isFn=typeof handler==="function";len=arguments.length;switch(len){case 1:emitNone(handler,isFn,this);break;case 2:emitOne(handler,isFn,this,arguments[1]);break;case 3:emitTwo(handler,isFn,this,arguments[1],arguments[2]);break;case 4:emitThree(handler,isFn,this,arguments[1],arguments[2],arguments[3]);break;default:args=new Array(len-1);for(i=1;i<len;i++)args[i-1]=arguments[i];emitMany(handler,isFn,this,args)}return true};function _addListener(target,type,listener,prepend){var m;var events;var existing;if(typeof listener!=="function")throw new TypeError('"listener" argument must be a function');events=target._events;if(!events){events=target._events=objectCreate(null);target._eventsCount=0}else{if(events.newListener){target.emit("newListener",type,listener.listener?listener.listener:listener);events=target._events}existing=events[type]}if(!existing){existing=events[type]=listener;++target._eventsCount}else{if(typeof existing==="function"){existing=events[type]=prepend?[listener,existing]:[existing,listener]}else{if(prepend){existing.unshift(listener)}else{existing.push(listener)}}if(!existing.warned){m=$getMaxListeners(target);if(m&&m>0&&existing.length>m){existing.warned=true;var w=new Error("Possible EventEmitter memory leak detected. "+existing.length+' "'+String(type)+'" listeners '+"added. Use emitter.setMaxListeners() to "+"increase limit.");w.name="MaxListenersExceededWarning";w.emitter=target;w.type=type;w.count=existing.length;if(typeof console==="object"&&console.warn){console.warn("%s: %s",w.name,w.message)}}}}return target}EventEmitter.prototype.addListener=function addListener(type,listener){return _addListener(this,type,listener,false)};EventEmitter.prototype.on=EventEmitter.prototype.addListener;EventEmitter.prototype.prependListener=function prependListener(type,listener){return _addListener(this,type,listener,true)};function onceWrapper(){if(!this.fired){this.target.removeListener(this.type,this.wrapFn);this.fired=true;switch(arguments.length){case 0:return this.listener.call(this.target);case 1:return this.listener.call(this.target,arguments[0]);case 2:return this.listener.call(this.target,arguments[0],arguments[1]);case 3:return this.listener.call(this.target,arguments[0],arguments[1],arguments[2]);default:var args=new Array(arguments.length);for(var i=0;i<args.length;++i)args[i]=arguments[i];this.listener.apply(this.target,args)}}}function _onceWrap(target,type,listener){var state={fired:false,wrapFn:undefined,target:target,type:type,listener:listener};var wrapped=bind.call(onceWrapper,state);wrapped.listener=listener;state.wrapFn=wrapped;return wrapped}EventEmitter.prototype.once=function once(type,listener){if(typeof listener!=="function")throw new TypeError('"listener" argument must be a function');this.on(type,_onceWrap(this,type,listener));return this};EventEmitter.prototype.prependOnceListener=function prependOnceListener(type,listener){if(typeof listener!=="function")throw new TypeError('"listener" argument must be a function');this.prependListener(type,_onceWrap(this,type,listener));return this};EventEmitter.prototype.removeListener=function removeListener(type,listener){var list,events,position,i,originalListener;if(typeof listener!=="function")throw new TypeError('"listener" argument must be a function');events=this._events;if(!events)return this;list=events[type];if(!list)return this;if(list===listener||list.listener===listener){if(--this._eventsCount===0)this._events=objectCreate(null);else{delete events[type];if(events.removeListener)this.emit("removeListener",type,list.listener||listener)}}else if(typeof list!=="function"){position=-1;for(i=list.length-1;i>=0;i--){if(list[i]===listener||list[i].listener===listener){originalListener=list[i].listener;position=i;break}}if(position<0)return this;if(position===0)list.shift();else spliceOne(list,position);if(list.length===1)events[type]=list[0];if(events.removeListener)this.emit("removeListener",type,originalListener||listener)}return this};EventEmitter.prototype.removeAllListeners=function removeAllListeners(type){var listeners,events,i;events=this._events;if(!events)return this;if(!events.removeListener){if(arguments.length===0){this._events=objectCreate(null);this._eventsCount=0}else if(events[type]){if(--this._eventsCount===0)this._events=objectCreate(null);else delete events[type]}return this}if(arguments.length===0){var keys=objectKeys(events);var key;for(i=0;i<keys.length;++i){key=keys[i];if(key==="removeListener")continue;this.removeAllListeners(key)}this.removeAllListeners("removeListener");this._events=objectCreate(null);this._eventsCount=0;return this}listeners=events[type];if(typeof listeners==="function"){this.removeListener(type,listeners)}else if(listeners){for(i=listeners.length-1;i>=0;i--){this.removeListener(type,listeners[i])}}return this};function _listeners(target,type,unwrap){var events=target._events;if(!events)return[];var evlistener=events[type];if(!evlistener)return[];if(typeof evlistener==="function")return unwrap?[evlistener.listener||evlistener]:[evlistener];return unwrap?unwrapListeners(evlistener):arrayClone(evlistener,evlistener.length)}EventEmitter.prototype.listeners=function listeners(type){return _listeners(this,type,true)};EventEmitter.prototype.rawListeners=function rawListeners(type){return _listeners(this,type,false)};EventEmitter.listenerCount=function(emitter,type){if(typeof emitter.listenerCount==="function"){return emitter.listenerCount(type)}else{return listenerCount.call(emitter,type)}};EventEmitter.prototype.listenerCount=listenerCount;function listenerCount(type){var events=this._events;if(events){var evlistener=events[type];if(typeof evlistener==="function"){return 1}else if(evlistener){return evlistener.length}}return 0}EventEmitter.prototype.eventNames=function eventNames(){return this._eventsCount>0?Reflect.ownKeys(this._events):[]};function spliceOne(list,index){for(var i=index,k=i+1,n=list.length;k<n;i+=1,k+=1)list[i]=list[k];list.pop()}function arrayClone(arr,n){var copy=new Array(n);for(var i=0;i<n;++i)copy[i]=arr[i];return copy}function unwrapListeners(arr){var ret=new Array(arr.length);for(var i=0;i<ret.length;++i){ret[i]=arr[i].listener||arr[i]}return ret}function objectCreatePolyfill(proto){var F=function(){};F.prototype=proto;return new F}function objectKeysPolyfill(obj){var keys=[];for(var k in obj)if(Object.prototype.hasOwnProperty.call(obj,k)){keys.push(k)}return k}function functionBindPolyfill(context){var fn=this;return function(){return fn.apply(context,arguments)}}},{}],16:[function(require,module,exports){"use strict";var hasOwn=Object.prototype.hasOwnProperty;var toStr=Object.prototype.toString;var isArray=function isArray(arr){if(typeof Array.isArray==="function"){return Array.isArray(arr)}return toStr.call(arr)==="[object Array]"};var isPlainObject=function isPlainObject(obj){if(!obj||toStr.call(obj)!=="[object Object]"){return false}var hasOwnConstructor=hasOwn.call(obj,"constructor");var hasIsPrototypeOf=obj.constructor&&obj.constructor.prototype&&hasOwn.call(obj.constructor.prototype,"isPrototypeOf");if(obj.constructor&&!hasOwnConstructor&&!hasIsPrototypeOf){return false}var key;for(key in obj){}return typeof key==="undefined"||hasOwn.call(obj,key)};module.exports=function extend(){var options,name,src,copy,copyIsArray,clone;var target=arguments[0];var i=1;var length=arguments.length;var deep=false;if(typeof target==="boolean"){deep=target;target=arguments[1]||{};i=2}if(target==null||typeof target!=="object"&&typeof target!=="function"){target={}}for(;i<length;++i){options=arguments[i];if(options!=null){for(name in options){src=target[name];copy=options[name];if(target!==copy){if(deep&&copy&&(isPlainObject(copy)||(copyIsArray=isArray(copy)))){if(copyIsArray){copyIsArray=false;clone=src&&isArray(src)?src:[]}else{clone=src&&isPlainObject(src)?src:{}}target[name]=extend(deep,clone,copy)}else if(typeof copy!=="undefined"){target[name]=copy}}}}}return target}},{}],17:[function(require,module,exports){"use strict";module.exports=function(header){var result={"content-md5":"Content-MD5",dnt:"DNT",etag:"ETag","last-event-id":"Last-Event-ID",tcn:"TCN",te:"TE","www-authenticate":"WWW-Authenticate","x-dnsprefetch-control":"X-DNSPrefetch-Control"}[header.toLowerCase()];if(result){return result}return header.split("-").map(function(text){return text.charAt(0).toUpperCase()+text.substr(1).toLowerCase()}).join("-")}},{}],18:[function(require,module,exports){exports.read=function(buffer,offset,isLE,mLen,nBytes){var e,m;var eLen=nBytes*8-mLen-1;var eMax=(1<<eLen)-1;var eBias=eMax>>1;var nBits=-7;var i=isLE?nBytes-1:0;var d=isLE?-1:1;var s=buffer[offset+i];i+=d;e=s&(1<<-nBits)-1;s>>=-nBits;nBits+=eLen;for(;nBits>0;e=e*256+buffer[offset+i],i+=d,nBits-=8){}m=e&(1<<-nBits)-1;e>>=-nBits;nBits+=mLen;for(;nBits>0;m=m*256+buffer[offset+i],i+=d,nBits-=8){}if(e===0){e=1-eBias}else if(e===eMax){return m?NaN:(s?-1:1)*Infinity}else{m=m+Math.pow(2,mLen);e=e-eBias}return(s?-1:1)*m*Math.pow(2,e-mLen)};exports.write=function(buffer,value,offset,isLE,mLen,nBytes){var e,m,c;var eLen=nBytes*8-mLen-1;var eMax=(1<<eLen)-1;var eBias=eMax>>1;var rt=mLen===23?Math.pow(2,-24)-Math.pow(2,-77):0;var i=isLE?0:nBytes-1;var d=isLE?1:-1;var s=value<0||value===0&&1/value<0?1:0;value=Math.abs(value);if(isNaN(value)||value===Infinity){m=isNaN(value)?1:0;e=eMax}else{e=Math.floor(Math.log(value)/Math.LN2);if(value*(c=Math.pow(2,-e))<1){e--;c*=2}if(e+eBias>=1){value+=rt/c}else{value+=rt*Math.pow(2,1-eBias)}if(value*c>=2){e++;c/=2}if(e+eBias>=eMax){m=0;e=eMax}else if(e+eBias>=1){m=(value*c-1)*Math.pow(2,mLen);e=e+eBias}else{m=value*Math.pow(2,eBias-1)*Math.pow(2,mLen);e=0}}for(;mLen>=8;buffer[offset+i]=m&255,i+=d,m/=256,mLen-=8){}e=e<<mLen|m;eLen+=mLen;for(;eLen>0;buffer[offset+i]=e&255,i+=d,e/=256,eLen-=8){}buffer[offset+i-d]|=s*128}},{}],19:[function(require,module,exports){var has=Object.prototype.hasOwnProperty;var toString=Object.prototype.toString;function isEmpty(val){if(val==null)return true;if("boolean"==typeof val)return false;if("number"==typeof val)return val===0;if("string"==typeof val)return val.length===0;if("function"==typeof val)return val.length===0;if(Array.isArray(val))return val.length===0;if(val instanceof Error)return val.message==="";if(val.toString==toString){switch(val.toString()){case"[object File]":case"[object Map]":case"[object Set]":{return val.size===0}case"[object Object]":{for(var key in val){if(has.call(val,key))return false}return true}}}return false}module.exports=isEmpty},{}],20:[function(require,module,exports){"use strict";function _interopDefault(ex){return ex&&typeof ex==="object"&&"default"in ex?ex["default"]:ex}var lie=_interopDefault(require("lie"));var PouchPromise=typeof Promise==="function"?Promise:lie;module.exports=PouchPromise},{lie:22}],21:[function(require,module,exports){(function(global){"use strict";var Mutation=global.MutationObserver||global.WebKitMutationObserver;var scheduleDrain;{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(){var scriptEl=global.document.createElement("script");scriptEl.onreadystatechange=function(){nextTick();scriptEl.onreadystatechange=null;scriptEl.parentNode.removeChild(scriptEl);scriptEl=null};global.document.documentElement.appendChild(scriptEl)}}else{scheduleDrain=function(){setTimeout(nextTick,0)}}}var draining;var queue=[];function nextTick(){draining=true;var i,oldQueue;var len=queue.length;while(len){oldQueue=queue;queue=[];i=-1;while(++i<len){oldQueue[i]()}len=queue.length}draining=false}module.exports=immediate;function immediate(task){if(queue.push(task)===1&&!draining){scheduleDrain()}}}).call(this,typeof global!=="undefined"?global:typeof self!=="undefined"?self:typeof window!=="undefined"?window:{})},{}],22:[function(require,module,exports){"use strict";var immediate=require("immediate");function INTERNAL(){}var handlers={};var REJECTED=["REJECTED"];var FULFILLED=["FULFILLED"];var PENDING=["PENDING"];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)}}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))}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)}if(returnValue===promise){handlers.reject(promise,new TypeError("Cannot resolve promise with itself"))}else{handlers.resolve(promise,returnValue)}})}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(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)}}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){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){var called=false;function onError(value){if(called){return}called=true;handlers.reject(self,value)}function onSuccess(value){if(called){return}called=true;handlers.resolve(self,value)}function tryToUnwrap(){thenable(onSuccess,onError)}var result=tryCatch(tryToUnwrap);if(result.status==="error"){onError(result.value)}}function tryCatch(func,value){var out={};try{out.value=func(value);out.status="success"}catch(e){out.status="error";out.value=e}return out}Promise.resolve=resolve;function resolve(value){if(value instanceof this){return value}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"))}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);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)}}}}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 len=iterable.length;var called=false;if(!len){return this.resolve([])}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)}})}}},{immediate:21}],23:[function(require,module,exports){var process=module.exports={};var cachedSetTimeout;var cachedClearTimeout;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){return setTimeout(fun,0)}if((cachedSetTimeout===defaultSetTimout||!cachedSetTimeout)&&setTimeout){cachedSetTimeout=setTimeout;return setTimeout(fun,0)}try{return cachedSetTimeout(fun,0)}catch(e){try{return cachedSetTimeout.call(null,fun,0)}catch(e){return cachedSetTimeout.call(this,fun,0)}}}function runClearTimeout(marker){if(cachedClearTimeout===clearTimeout){return clearTimeout(marker)}if((cachedClearTimeout===defaultClearTimeout||!cachedClearTimeout)&&clearTimeout){cachedClearTimeout=clearTimeout;return clearTimeout(marker)}try{return cachedClearTimeout(marker)}catch(e){try{return cachedClearTimeout.call(null,marker)}catch(e){return cachedClearTimeout.call(this,marker)}}}var queue=[];var draining=false;var currentQueue;var queueIndex=-1;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)}};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="";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.prependListener=noop;process.prependOnceListener=noop;process.listeners=function(name){return[]};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}},{}],24:[function(require,module,exports){"use strict";module.exports=function nodify(promise,callback){if(typeof callback==="function"){promise.then(function(resp){callback(null,resp)},function(err){callback(err,null)})}}},{}],25:[function(require,module,exports){"use strict";function hasOwnProperty(obj,prop){return Object.prototype.hasOwnProperty.call(obj,prop)}module.exports=function(qs,sep,eq,options){sep=sep||"&";eq=eq||"=";var obj={};if(typeof qs!=="string"||qs.length===0){return obj}var regexp=/\+/g;qs=qs.split(sep);var maxKeys=1e3;if(options&&typeof options.maxKeys==="number"){maxKeys=options.maxKeys}var len=qs.length;if(maxKeys>0&&len>maxKeys){len=maxKeys}for(var i=0;i<len;++i){var x=qs[i].replace(regexp,"%20"),idx=x.indexOf(eq),kstr,vstr,k,v;if(idx>=0){kstr=x.substr(0,idx);vstr=x.substr(idx+1)}else{kstr=x;vstr=""}k=decodeURIComponent(kstr);v=decodeURIComponent(vstr);if(!hasOwnProperty(obj,k)){obj[k]=v}else if(isArray(obj[k])){obj[k].push(v)}else{obj[k]=[obj[k],v]}}return obj};var isArray=Array.isArray||function(xs){return Object.prototype.toString.call(xs)==="[object Array]"}},{}],26:[function(require,module,exports){"use strict";var stringifyPrimitive=function(v){switch(typeof v){case"string":return v;case"boolean":return v?"true":"false";case"number":return isFinite(v)?v:"";default:return""}};module.exports=function(obj,sep,eq,name){sep=sep||"&";eq=eq||"=";if(obj===null){obj=undefined}if(typeof obj==="object"){return map(objectKeys(obj),function(k){var ks=encodeURIComponent(stringifyPrimitive(k))+eq;if(isArray(obj[k])){return map(obj[k],function(v){return ks+encodeURIComponent(stringifyPrimitive(v))}).join(sep)}else{return ks+encodeURIComponent(stringifyPrimitive(obj[k]))}}).join(sep)}if(!name)return"";return encodeURIComponent(stringifyPrimitive(name))+eq+encodeURIComponent(stringifyPrimitive(obj))};var isArray=Array.isArray||function(xs){return Object.prototype.toString.call(xs)==="[object Array]"};function map(xs,f){if(xs.map)return xs.map(f);var res=[];for(var i=0;i<xs.length;i++){res.push(f(xs[i],i))}return res}var objectKeys=Object.keys||function(obj){var res=[];for(var key in obj){if(Object.prototype.hasOwnProperty.call(obj,key))res.push(key)}return res}},{}],27:[function(require,module,exports){"use strict";exports.decode=exports.parse=require("./decode");exports.encode=exports.stringify=require("./encode")},{"./decode":25,"./encode":26}],28:[function(require,module,exports){module.exports=function(){var d=(new Date).getTime();return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(c){var r=(d+Math.random()*16)%16|0;d=Math.floor(d/16);return(c=="x"?r:r&3|8).toString(16)})}},{}],29:[function(require,module,exports){(function(process,Buffer){!function(globals){"use strict";if(typeof define!=="undefined"&&define.amd){define([],function(){return secureRandom})}else if(typeof module!=="undefined"&&module.exports){module.exports=secureRandom}else{globals.secureRandom=secureRandom}function secureRandom(count,options){options=options||{type:"Array"};if(typeof process!="undefined"&&typeof process.pid=="number"){return nodeRandom(count,options)}else{var crypto=window.crypto||window.msCrypto;if(!crypto)throw new Error("Your browser does not support window.crypto.");return browserRandom(count,options)}}function nodeRandom(count,options){var crypto=require("crypto");var buf=crypto.randomBytes(count);switch(options.type){case"Array":return[].slice.call(buf);case"Buffer":return buf;case"Uint8Array":var arr=new Uint8Array(count);for(var i=0;i<count;++i){arr[i]=buf.readUInt8(i)}return arr;default:throw new Error(options.type+" is unsupported.")}}function browserRandom(count,options){var nativeArr=new Uint8Array(count);var crypto=window.crypto||window.msCrypto;crypto.getRandomValues(nativeArr);switch(options.type){case"Array":return[].slice.call(nativeArr);case"Buffer":try{var b=new Buffer(1)}catch(e){throw new Error("Buffer not supported in this environment. Use Node.js or Browserify for browser support.")}return new Buffer(nativeArr);case"Uint8Array":return nativeArr;default:throw new Error(options.type+" is unsupported.")}}secureRandom.randomArray=function(byteCount){return secureRandom(byteCount,{type:"Array"})};secureRandom.randomUint8Array=function(byteCount){return secureRandom(byteCount,{type:"Uint8Array"})};secureRandom.randomBuffer=function(byteCount){return secureRandom(byteCount,{type:"Buffer"})}}(this)}).call(this,require("_process"),require("buffer").Buffer)},{_process:23,buffer:7,crypto:6}],30:[function(require,module,exports){"use strict";var PouchPluginError=require("pouchdb-plugin-error");var extend=require("extend");exports.evaluate=function(requireContext,extraVars,program){var require;if(requireContext){require=function(libPath){var requireLocals=extend({module:{id:libPath,current:undefined,parent:undefined,exports:{}}},locals);requireLocals.exports=requireLocals.module.exports;var path=libPath.split("/");var lib=requireContext;for(var i=0;i<path.length;i+=1){lib=lib[path[i]]}lib+="\nreturn module.exports;";return evalProgram(lib,requireLocals)}}program=program.replace(/;\s*$/,"");var locals=extend({isArray:isArray,toJSON:toJSON,log:log,sum:sum,require:require},extraVars);var func;try{func=evalProgram("return "+program,locals);if(typeof func!=="function"){throw"no function"}}catch(e){throw new PouchPluginError({name:"compilation_error",status:500,message:"Expression does not eval to a function. "+program})}return func};var isArray=Array.isArray;var toJSON=JSON.stringify;var log=function(message){if(typeof message!="string"){message=JSON.stringify(message)}console.log("EVALUATED FUNCTION LOGS: "+message)};var sum=function(array){return array.reduce(function(a,b){return a+b})};function evalProgram(program,locals){var keys=Object.keys(locals);var values=keys.map(function(key){return locals[key]});var code="(function ("+keys.join(", ")+") {"+program+"})";return eval(code).apply(null,values)}exports.wrapExecutionError=function(e){return new PouchPluginError({name:e.name,message:e.toString()+"\n\n"+e.stack,status:500})}},{extend:16,"pouchdb-plugin-error":42}],31:[function(require,module,exports){(function(global){"use strict";var extend=require("extend");var isEmpty=require("is-empty");var querystring=require("querystring");var Promise=require("pouchdb-promise");var uuid=require("random-uuid-v4");var buildUserContextObject=require("./couchusercontextobject.js");var normalizeHeaderCase=require("header-case-normalizer");module.exports=function buildRequestObject(db,pathEnd,options){var infoPromise=db.info();var pathPromise=infoPromise.then(function(info){pathEnd.unshift(encodeURIComponent(info.db_name));return normalizePath(pathEnd)});var userCtxPromise=infoPromise.then(buildUserContextObject);return Promise.all([pathPromise,infoPromise,userCtxPromise]).then(function(args){args.push(getHost(db));args.push(uuid());args.push(options);return actuallyBuildRequestObject.apply(null,args)})};function getHost(db){try{var url=decodeURI(db.getUrl());return url.split("://")[1].split("/")[0].split("@").pop()}catch(err){return"localhost:5984"}}function normalizePath(path){var up=0;for(var i=path.length-1;i>=0;i--){var last=path[i];if(last==="."){path.splice(i,1)}else if(last===".."){path.splice(i,1);up++}else if(up){path.splice(i,1);up--}}for(;up--;up){path.unshift("..")}return path}function actuallyBuildRequestObject(path,info,userCtx,host,uuid,options){var result={body:"undefined",cookie:{},form:{},headers:{Host:host,Accept:"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","Accept-Language":buildAcceptLanguage(),"User-Agent":buildUserAgent()},info:info,method:"GET",path:path.slice(0),peer:"127.0.0.1",query:{},requested_path:path.slice(0),raw_path:"/"+path.join("/"),secObj:{},userCtx:userCtx,uuid:uuid};if(["_show","_update"].indexOf(path[3])===-1){result.id=null}else{result.id=path[5]||null;if(result.id==="_design"&&path[6]){result.id+="/"+path[6]}}if(options&&options.headers){Object.keys(options.headers).forEach(function(header){if(["x-couchdb-requested-path"].indexOf(header)===-1){result.headers[normalizeHeaderCase(header)]=options.headers[header]}else{result.headers[header]=options.headers[header]}});delete options.headers}extend(true,result,options);if(options.path){result.path=options.path}if(options.requested_path){result.requested_path=options.requested_path}var i=result.requested_path.length-1;var pathEnd=result.requested_path[i];if(!isEmpty(result.query)&&pathEnd.indexOf("?")===-1){result.requested_path[i]=pathEnd+"?"+querystring.stringify(result.query)}if(!isEmpty(result.query)&&result.raw_path.indexOf("?")===-1){result.raw_path+="?"+querystring.stringify(result.query)}if(!isEmpty(result.form)&&result.body==="undefined"){result.body=querystring.stringify(result.form);result.headers["Content-Type"]="application/x-www-form-urlencoded";result.headers["Content-Length"]=result.body.length.toString()}if(result.body!=="undefined"&&["POST","PUT","PATCH"].indexOf(result.method)===-1){result.method="POST"}return result}function buildAcceptLanguage(){var lang=(global.navigator||{}).language||(global.navigator||{}).userLanguage;lang=(lang||"en").toLowerCase();if(["en","en-us"].indexOf(lang)!==-1){return"en-us,en;q=0.5"}else{return lang+",en-us;q=0.7,en;q=0.3"}}function buildUserAgent(){var ua=(global.navigator||{}).userAgent;return ua||"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:28.0) Gecko/20100101 Firefox/28.0"}}).call(this,typeof global!=="undefined"?global:typeof self!=="undefined"?self:typeof window!=="undefined"?window:{})},{"./couchusercontextobject.js":32,extend:16,"header-case-normalizer":17,"is-empty":19,"pouchdb-promise":20,querystring:27,"random-uuid-v4":28}],32:[function(require,module,exports){"use strict";module.exports=function buildUserContextObject(info){return{db:info.db_name,name:null,roles:["_admin"]}}},{}],33:[function(require,module,exports){"use strict";exports.buildUserContextObject=require("./couchusercontextobject.js");exports.buildRequestObject=require("./couchrequestobject.js")},{"./couchrequestobject.js":31,"./couchusercontextobject.js":32}],34:[function(require,module,exports){"use strict";var utils=require("./utils");var Promise=require("pouchdb-promise");var IS_HASH_RE=/^-(?:pbkdf2|hashed)-/;exports.hashPasswords=function(admins,opts,callback){var args=utils.processArgs(null,opts,callback);var result={};return utils.nodify(Promise.all(Object.keys(admins).map(function(key){return hashAdminPassword(admins[key],utils.iterations(args)).then(function(hashed){result[key]=hashed})})).then(function(){return result}),args.callback)};function hashAdminPassword(password,iterations){if(IS_HASH_RE.test(password)){return Promise.resolve(password)}var salt=utils.generateSecret();return utils.hashPassword(password,salt,iterations).then(function(hash){return"-pbkdf2-"+hash+","+salt+","+iterations})}var ADMIN_RE=/^-pbkdf2-([\da-f]+),([\da-f]+),([0-9]+)$/;exports.parse=function(admins){var result={};for(var name in admins){if(admins.hasOwnProperty(name)){var info=admins[name].match(ADMIN_RE);if(info){result[name]={password_scheme:"pbkdf2",derived_key:info[1],salt:info[2],iterations:parseInt(info[3],10),roles:["_admin"],name:name}}}}return result}},{"./utils":37,"pouchdb-promise":20}],35:[function(require,module,exports){module.exports={_id:"_design/_auth",language:"javascript",validate_doc_update:function(newDoc,oldDoc,userCtx,secObj){if(newDoc._deleted===true){if(userCtx.roles.indexOf("_admin")!==-1||userCtx.name==oldDoc.name){return}else{throw{forbidden:"Only admins may delete other user docs."}}}if(oldDoc&&oldDoc.type!=="user"||newDoc.type!=="user"){throw{forbidden:"doc.type must be user"}}if(!newDoc.name){throw{forbidden:"doc.name is required"}}if(!newDoc.roles){throw{forbidden:"doc.roles must exist"}}if(!isArray(newDoc.roles)){throw{forbidden:"doc.roles must be an array"}}for(var idx=0;idx<newDoc.roles.length;idx++){if(typeof newDoc.roles[idx]!=="string"){throw{forbidden:"doc.roles can only contain strings"}}}if(newDoc._id!=="org.couchdb.user:"+newDoc.name){throw{forbidden:"Doc ID must be of the form org.couchdb.user:name"}}if(oldDoc){if(oldDoc.name!==newDoc.name){throw{forbidden:"Usernames can not be changed."}}}if(newDoc.password_sha&&!newDoc.salt){throw{forbidden:"Users with password_sha must have a salt."+"See /_utils/script/couch.js for example code."}}var is_server_or_database_admin=function(userCtx,secObj){if(userCtx.roles.indexOf("_admin")!==-1){return true}if(secObj&&secObj.admins&&secObj.admins.names){if(secObj.admins.names.indexOf(userCtx.name)!==-1){return true}}if(secObj&&secObj.admins&&secObj.admins.roles){var db_roles=secObj.admins.roles;for(var idx=0;idx<userCtx.roles.length;idx++){var user_role=userCtx.roles[idx];if(db_roles.indexOf(user_role)!==-1){return true}}}return false};if(!is_server_or_database_admin(userCtx,secObj)){if(oldDoc){if(userCtx.name!==newDoc.name){throw{forbidden:"You may only update your own user document."}}var oldRoles=oldDoc.roles.sort();var newRoles=newDoc.roles.sort();if(oldRoles.length!==newRoles.length){throw{forbidden:"Only _admin may edit roles"}}for(var i=0;i<oldRoles.length;i++){if(oldRoles[i]!==newRoles[i]){throw{forbidden:"Only _admin may edit roles"}}}}else if(newDoc.roles.length>0){throw{forbidden:"Only _admin may set roles"}}}for(var i=0;i<newDoc.roles.length;i++){if(newDoc.roles[i][0]==="_"){throw{forbidden:"No system roles (starting with underscore) in users db."}}}if(newDoc.name[0]==="_"){throw{forbidden:"Username may not start with underscore."}}var badUserNameChars=[":"];for(var i=0;i<badUserNameChars.length;i++){if(newDoc.name.indexOf(badUserNameChars[i])>=0){throw{forbidden:"Character `"+badUserNameChars[i]+"` is not allowed in usernames."}}}}.toString()}},{}],36:[function(require,module,exports){"use strict";var Promise=require("pouchdb-promise");var base64url=require("base64url");var calculateSessionId=require("couchdb-calculate-session-id");var httpQuery=require("pouchdb-req-http-query");var PouchPluginError=require("pouchdb-plugin-error");var utils=require("./utils");exports.signUp=function(username,password,opts,callback){var args=utils.processArgs(this,opts,callback);var doc={_id:docId(username),type:"user",name:username,password:password,roles:args.opts.roles||[]};return utils.nodify(args.db.put(doc),args.callback)};function docId(username){return"org.couchdb.user:"+username}exports.logIn=function(username,password,callback){var promise;var info=utils.dbDataFor(this);if(info.isOnlineAuthDB){promise=httpQuery(this,{method:"POST",raw_path:"/_session",body:JSON.stringify({name:username,password:password}),headers:{"Content-Type":"application/json"}}).then(function(resp){return JSON.parse(resp.body)})}else{promise=exports.multiUserLogIn.call(this,username,password).then(saveSessionID.bind(null,info))}return utils.nodify(promise,callback)};function saveSessionID(info,resp){info.sessionID=resp.sessionID;delete resp.sessionID;return resp}exports.logOut=function(callback){var info=utils.dbDataFor(this);var promise;if(info.isOnlineAuthDB){promise=httpQuery(this,{method:"DELETE",raw_path:"/_session"}).then(function(resp){return JSON.parse(resp.body)})}else{delete info.sessionID;promise=Promise.resolve({ok:true})}return utils.nodify(promise,callback)};exports.session=function(callback){var info=utils.dbDataFor(this);var promise;if(info.isOnlineAuthDB){promise=httpQuery(this,{raw_path:"/_session",method:"GET"}).then(function(resp){return JSON.parse(resp.body)})}else{promise=exports.multiUserSession.call(this,info.sessionID).then(saveSessionID.bind(null,info))}return utils.nodify(promise,callback)};exports.multiUserLogIn=function(username,password,callback){var db=this;var info=utils.dbDataFor(db);var userDoc;return utils.nodify(getUserDoc(db,username).then(function(doc){userDoc=doc;return utils.hashPassword(password,userDoc.salt,userDoc.iterations)}).then(function(derived_key){if(derived_key!==userDoc.derived_key){throw"invalid_password"}return{ok:true,name:userDoc.name,roles:userDoc.roles,sessionID:newSessionId(userDoc,info)}}).catch(function(err){if(!(err instanceof PouchPluginError)){err=new PouchPluginError({status:401,name:"unauthorized",message:"Name or password is incorrect."})}throw err}),callback)};function getUserDoc(db,username){var info=utils.dbDataFor(db);var adminDoc=info.admins[username];return db.get(docId(username),{conflicts:true}).catch(function(err){if(err.name!="not_found"||typeof adminDoc==="undefined"){throw err}}).then(function(userDoc){if(typeof userDoc!=="undefined"){if((userDoc._conflicts||{}).length){throw new PouchPluginError({status:401,name:"unauthorized",message:"User document conflicts must be resolved before"+"the document is used for authentication purposes."})}if(typeof adminDoc==="undefined"){return userDoc}adminDoc=Object.assign({},adminDoc);adminDoc.roles=["_admin"].concat(userDoc.roles)}return adminDoc})}function newSessionId(userDoc,info){return calculateSessionId(userDoc.name,userDoc.salt,info.secret,timestamp())}function timestamp(){return Math.round(Date.now()/1e3)}exports.multiUserSession=function(sessionID,callback){var db=this;var info=utils.dbDataFor(db);var resp={ok:true,userCtx:{name:null,roles:[]},info:{authentication_handlers:["api"]}};if(Object.keys(info.admins).length===0){resp.userCtx.roles=["_admin"]}var givenTimestamp;return utils.nodify(db.info().then(function(dbInfo){resp.info.authentication_db=dbInfo.db_name;if(sessionID){try{var decoded=base64url.decode(sessionID);var givenUsername=decoded.split(":")[0];givenTimestamp=parseInt(decoded.split(":")[1],16);if(typeof givenUsername==="undefined"||isNaN(givenTimestamp)){throw"invalid"}}catch(err){throw new PouchPluginError({status:400,name:"bad_request",message:"Malformed session ID. If you're using a browser, try clearing your cookies."})}return getUserDoc(db,givenUsername)}else{throw"no session id"}}).then(function(userDoc){var expectedHash=calculateSessionId(userDoc.name,userDoc.salt,info.secret,givenTimestamp);if(timestamp()<givenTimestamp+info.timeout&&expectedHash===sessionID){resp.info.authenticated="api";resp.userCtx.name=userDoc.name;resp.userCtx.roles=userDoc.roles;resp.sessionID=newSessionId(userDoc,info)}}).catch(function(err){if(err instanceof PouchPluginError){throw err}}).then(function(){return resp}),callback)}},{"./utils":37,base64url:5,"couchdb-calculate-session-id":9,"pouchdb-plugin-error":42,"pouchdb-promise":20,"pouchdb-req-http-query":43}],37:[function(require,module,exports){"use strict";var Promise=require("pouchdb-promise");var crypto=require("crypto-lite").crypto;var secureRandom=require("secure-random");exports.dbData={dbs:[],dataByDBIdx:[]};exports.dbDataFor=function(db){var i=exports.dbData.dbs.indexOf(db);return exports.dbData.dataByDBIdx[i]};exports.nodify=function(promise,callback){require("promise-nodify")(promise,callback);return promise};exports.processArgs=function(db,opts,callback){if(typeof opts==="function"){callback=opts;opts={}}opts=opts||{};return{db:db,PouchDB:(db||{}).constructor,opts:opts,callback:callback}};exports.iterations=function(args){return args.opts.iterations||10};exports.generateSecret=function(){var arr=secureRandom(16);return arrayToString(arr)};function arrayToString(array){var result="";for(var i=0;i<array.length;i+=1){result+=((array[i]&255)+256).toString(16)}return result}exports.hashPassword=function(password,salt,iterations){var derived_key=crypto.pbkdf2(password,salt,iterations,20);return Promise.resolve(derived_key.toString("hex"))}},{"crypto-lite":14,"pouchdb-promise":20,"promise-nodify":24,"secure-random":29}],38:[function(require,module,exports){"use strict";var Promise=require("pouchdb-promise");var createBulkDocsWrapper=require("pouchdb-bulkdocs-wrapper");var utils=require("./utils");exports.put=function(original,args){return modifyDoc(args.base,args.doc).then(original)};function modifyDoc(db,doc){if(!(typeof doc.password=="undefined"||doc.password===null)){doc.iterations=utils.dbDataFor(db).iterations;doc.password_scheme="pbkdf2";doc.salt=utils.generateSecret();return utils.hashPassword(doc.password,doc.salt,doc.iterations).then(function(hash){delete doc.password;doc.derived_key=hash})}return Promise.resolve()}exports.post=exports.put;exports.bulkDocs=createBulkDocsWrapper(function(doc,args){return modifyDoc(args.base,doc)})},{"./utils":37,"pouchdb-bulkdocs-wrapper":40,"pouchdb-promise":20}],39:[function(require,module,exports){"use strict";var Promise=require("pouchdb-promise");var systemDB=require("pouchdb-system-db");var Validation=require("pouchdb-validation");var wrappers=require("pouchdb-wrappers");var admins=require("./admins");var api=require("./sessionapi");var designDoc=require("./designdoc");var utils=require("./utils");var writeWrappers=require("./writewrappers");exports.hashAdminPasswords=admins.hashPasswords;exports.generateSecret=utils.generateSecret;exports.useAsAuthenticationDB=function(opts,callback){var args=utils.processArgs(this,opts,callback);try{Validation.installValidationMethods.call(args.db)}catch(err){throw new Error("Already in use as an authentication database.")}var info={isOnlineAuthDB:isOnline(args),timeout:typeof args.opts.timeout==="undefined"?600:args.opts.timeout,iterations:utils.iterations(args),secret:args.opts.secret||utils.generateSecret(),admins:admins.parse(args.opts.admins||{})};var i=utils.dbData.dbs.push(args.db)-1;utils.dbData.dataByDBIdx[i]=info;for(var name in api){if(!(info.isOnlineAuthDB&&name.indexOf("multiUser")===0)){args.db[name]=api[name].bind(args.db)}}return utils.nodify(Promise.resolve().then(function(){if(!info.isOnlineAuthDB){wrappers.installWrapperMethods(args.db,writeWrappers);systemDB.installSystemDBProtection(args.db);return args.db.put(designDoc)}}).catch(function(err){if(err.status!==409){throw err}}).then(function(){}),args.callback)};function isOnline(args){if(typeof args.opts.isOnlineAuthDB==="undefined"){return["http","https"].indexOf(args.db.type())!==-1}return args.opts.isOnlineAuthDB}exports.stopUsingAsAuthenticationDB=function(){var db=this;var i=utils.dbData.dbs.indexOf(db);if(i===-1){throw new Error("Not an authentication database.")}utils.dbData.dbs.splice(i,1);var info=utils.dbData.dataByDBIdx.splice(i,1)[0];for(var name in api){if(api.hasOwnProperty(name)){delete db[name]}}if(!info.isOnlineAuthDB){systemDB.uninstallSystemDBProtection(db);wrappers.uninstallWrapperMethods(db,writeWrappers)}Validation.uninstallValidationMethods.call(db)}},{"./admins":34,"./designdoc":35,"./sessionapi":36,"./utils":37,"./writewrappers":38,"pouchdb-promise":20,"pouchdb-system-db":45,"pouchdb-validation":46,"pouchdb-wrappers":47}],40:[function(require,module,exports){"use strict";var Promise=require("pouchdb-promise");module.exports=function createBulkDocsWrapper(handler){return bulkDocsWrapper.bind(null,handler)};function bulkDocsWrapper(handler,bulkDocs,args){var notYetDone=[];var done=[];var promises=args.docs.map(function(doc){return handler(doc,args).then(function(){notYetDone.push(doc)}).catch(function(err){err.id=doc._id;done.push(err)})});return Promise.all(promises).then(function(){args.docs=notYetDone;return bulkDocs()}).then(function(dbResponses){return done.concat(dbResponses)})}},{"pouchdb-promise":20}],41:[function(require,module,exports){"use strict";var events=require("events");var changesEvents="change complete error create update delete".split(" ");module.exports=function createChangeslikeWrapper(handler){return changesLikeWrapper.bind(null,handler)};function changesLikeWrapper(handler,origChanges,args){var newResult=new events.EventEmitter;var isCancelled=false;var promise=handler(function(){var origResult=origChanges();changesEvents.forEach(function(event){origResult.on(event,newResult.emit.bind(newResult,event))});if(isCancelled){origResult.cancel()}else{newResult.on("cancel",function(){origResult.cancel()})}return origResult},args);newResult.then=promise.then.bind(promise);newResult.catch=promise.catch.bind(promise);newResult.cancel=function(){isCancelled=true;newResult.emit("cancel");newResult.removeAllListeners()};return newResult}},{events:15}],42:[function(require,module,exports){"use strict";function PouchPluginError(opts){this.status=opts.status;this.name=opts.name;this.message=opts.message;this.error=true;this.stack=(new Error).stack}PouchPluginError.prototype.toString=function(){return JSON.stringify({status:this.status,name:this.name,message:this.message})};module.exports=PouchPluginError},{}],43:[function(require,module,exports){(function(global){"use strict";var Promise=require("pouchdb-promise");var PouchPluginError=require("pouchdb-plugin-error");var normalizeHeaderCase=require("header-case-normalizer");var extend=require("extend");if(typeof global.XMLHttpRequest==="undefined"){global.XMLHttpRequest=require("xmlhttprequest-cookie").XMLHttpRequest}module.exports=function httpQuery(db,req){return new Promise(function(resolve,reject){function callback(){if(xhr.readyState!==4){return}if(xhr.status<200||xhr.status>=300){try{var err=JSON.parse(xhr.responseText);reject(new PouchPluginError({name:err.error,message:err.reason,status:xhr.status}))}catch(err){reject(new PouchPluginError({name:"unknown_error",message:xhr.responseText,status:500}))}return}var headers={};xhr.getAllResponseHeaders().split("\r\n").forEach(function(line){if(line){var splittedHeader=line.split(":");headers[normalizeHeaderCase(splittedHeader[0]).trim()]=splittedHeader[1].trim()}});var result={body:xhr.responseText,headers:headers,code:xhr.status};if(headers["content-type"]==="application/json"){result.json=JSON.parse(result.body)}resolve(result)}var url=db.name.replace(/\/[^\/]+\/?$/,"")+req.raw_path;var pouchHeaders=(db.getHeaders||fakeGetHeaders)();var headers=extend({},pouchHeaders,req.headers);var xhr=new XMLHttpRequest;xhr.withCredentials=true;xhr.onreadystatechange=callback;xhr.open(req.method,url,true);for(var name in headers){if(headers.hasOwnProperty(name)){if(xhr.setDisableHeaderCheck){xhr.setDisableHeaderCheck(true)}xhr.setRequestHeader(name,headers[name])}}xhr.send(req.body==="undefined"?null:req.body)})};function fakeGetHeaders(){return{}}}).call(this,typeof global!=="undefined"?global:typeof self!=="undefined"?self:typeof window!=="undefined"?window:{})},{extend:16,"header-case-normalizer":17,"pouchdb-plugin-error":42,"pouchdb-promise":20,"xmlhttprequest-cookie":6}],44:[function(require,module,exports){"use strict";var extend=require("extend");var Promise=require("pouchdb-promise");var nodify=require("promise-nodify");var httpQuery=require("pouchdb-req-http-query");var wrappers=require("pouchdb-wrappers");var createBulkDocsWrapper=require("pouchdb-bulkdocs-wrapper");var createChangeslikeWrapper=require("pouchdb-changeslike-wrapper");var PouchDBPluginError=require("pouchdb-plugin-error");var DOC_ID="_local/_security";exports.installSecurityMethods=function(){try{wrappers.installWrapperMethods(this,securityWrappers)}catch(err){throw new Error("Security methods already installed.")}};exports.installStaticSecurityMethods=function(PouchDB){try{wrappers.installStaticWrapperMethods(PouchDB,staticSecurityWrappers)}catch(err){throw new Error("Static security methods already installed.")}};function securityWrapper(checkAllowed,original,args){var userCtx=args.options.userCtx||{name:null,roles:["_admin"]};if(userCtx.roles.indexOf("_admin")!==-1){return original()}if(!checkAllowed){return Promise.resolve().then(throw401)}return filledInSecurity(args).then(function(security){if(!checkAllowed(userCtx,security)){throw401()}}).then(original)}function throw401(){throw new PouchDBPluginError({status:401,name:"unauthorized",message:"You are not authorized to access this db."})}function filledInSecurity(args){var getSecurity;if(typeof args.options.secObj==="undefined"){getSecurity=exports.getSecurity.bind(args.base)}else{getSecurity=Promise.resolve.bind(Promise,args.options.secObj)}return getSecurity().then(function(security){security.members=security.members||{};security.admins=security.admins||{};fillInSection(security.members);fillInSection(security.admins);return security})}function fillInSection(section){section.names=section.names||[];section.roles=section.roles||[]}function isIn(userCtx,section){return section.names.some(function(name){return name===userCtx.name})||section.roles.some(function(role){return userCtx.roles.indexOf(role)!==-1})}var securityWrappers={};securityWrappers.query=function(original,args){return securityWrapper(function(userCtx,security){var isStoredView=typeof args.fun==="string";return isIn(userCtx,security.admins)||isStoredView&&isMember(userCtx,security)},original,args)};function documentModificationWrapper(original,args,docId){return securityWrapper(function(userCtx,security){var isNotDesignDoc=String(docId).indexOf("_design/")!==0;return isIn(userCtx,security.admins)||isNotDesignDoc&&isMember(userCtx,security)},original,args)}function isMember(userCtx,security){var thereAreMembers=security.members.names.length||security.members.roles.length;return!thereAreMembers||isIn(userCtx,security.members)}securityWrappers.put=function(original,args){return documentModificationWrapper(original,args,args.doc._id)};securityWrappers.post=securityWrappers.put;securityWrappers.remove=securityWrappers.put;securityWrappers.putAttachment=function(original,args){return documentModificationWrapper(original,args,args.docId)};securityWrappers.removeAttachment=securityWrappers.putAttachment;securityWrappers.bulkDocs=createBulkDocsWrapper(function(doc,args){var noop=Promise.resolve.bind(Promise);return documentModificationWrapper(noop,args,doc._id)});var requiresServerAdmin=securityWrapper.bind(null,null);securityWrappers.destroy=requiresServerAdmin;var requiresAdminWrapper=securityWrapper.bind(null,function(userCtx,security){return isIn(userCtx,security.admins)});["compact","putSecurity","viewCleanup","createIndex","deleteIndex"].forEach(function(name){securityWrappers[name]=requiresAdminWrapper});var requiresMemberWrapper=securityWrapper.bind(null,function(userCtx,security){return isIn(userCtx,security.admins)||isMember(userCtx,security)});var requireMemberChangesWrapper=createChangeslikeWrapper(requiresMemberWrapper);["get","allDocs","getAttachment","info","revsDiff","getSecurity","list","show","update","rewriteResultRequestObject","bulkGet","getIndexes","find","explain"].forEach(function(name){securityWrappers[name]=requiresMemberWrapper});["changes","sync","replicate.to","replicate.from"].forEach(function(name){securityWrappers[name]=requireMemberChangesWrapper});var staticSecurityWrappers={};staticSecurityWrappers.new=requiresServerAdmin;staticSecurityWrappers.destroy=requiresServerAdmin;staticSecurityWrappers.replicate=function(original,args){var PouchDB=args.base;args.base=args.source instanceof PouchDB?args.source:new PouchDB(args.source);var handler=securityWrappers["replicate.to"];return handler(original,args)};exports.uninstallSecurityMethods=function(){try{wrappers.uninstallWrapperMethods(this,securityWrappers)}catch(err){throw new Error("Security methods not installed.")}};exports.uninstallStaticSecurityMethods=function(PouchDB){try{wrappers.uninstallStaticWrapperMethods(PouchDB,staticSecurityWrappers)}catch(err){throw new Error("Static security methods not installed.")}};exports.putSecurity=function(secObj,callback){var db=this;var promise;if(isHTTP(db)){promise=httpRequest(db,{method:"PUT",body:JSON.stringify(secObj)})}else{promise=db.get(DOC_ID).catch(function(){return{_id:DOC_ID}}).then(function(doc){doc.security=secObj;return db.put(doc)}).then(function(){return{ok:true}})}nodify(promise,callback);return promise};function isHTTP(db){return["http","https"].indexOf(db.type())!==-1}function httpRequest(db,reqStub){return db.info().then(function(info){extend(reqStub,{raw_path:"/"+info.db_name+"/_security",headers:{"Content-Type":"application/json"}});return httpQuery(db,reqStub).then(function(resp){return JSON.parse(resp.body)})})}exports.getSecurity=function(callback){var db=this;var promise;if(isHTTP(db)){promise=httpRequest(db,{method:"GET"})}else{promise=db.get(DOC_ID).catch(function(){return{security:{}}}).then(function(doc){return doc.security})}nodify(promise,callback);return promise}},{extend:16,"pouchdb-bulkdocs-wrapper":40,"pouchdb-changeslike-wrapper":41,"pouchdb-plugin-error":42,"pouchdb-promise":20,"pouchdb-req-http-query":43,"pouchdb-wrappers":47,"promise-nodify":24}],45:[function(require,module,exports){"use strict";var wrappers=require("pouchdb-wrappers");var createChangeslikeWrapper=require("pouchdb-changeslike-wrapper");var Security=require("pouchdb-security");var PouchDBPluginError=require("pouchdb-plugin-error");exports.installSystemDBProtection=function(db){Security.installSecurityMethods.call(db);wrappers.installWrapperMethods(db,systemWrappers)};exports.uninstallSystemDBProtection=function(db){wrappers.uninstallWrapperMethods(db,systemWrappers);Security.uninstallSecurityMethods.call(db)};function adminOnlyWrapper(error,orig,args){var userCtx=(args.options||{}).userCtx||{name:null,roles:["_admin"]};if(userCtx.roles.indexOf("_admin")!==-1){return orig()}return args.db.getSecurity().then(function(security){var dbAdmins=security.admins||{};var isDbAdmin=(dbAdmins.users||[]).indexOf(userCtx.name)!==-1||(dbAdmins.roles||[]).some(function(role){return userCtx.roles.indexOf(role)!==-1});if(!isDbAdmin){throw new PouchDBPluginError(error)}return orig()})}function create401(urlName){return{status:401,name:"unauthorized",message:"Only admins can access "+urlName+" of system databases."}}var systemWrappers={};systemWrappers.allDocs=adminOnlyWrapper.bind(null,create401("_all_docs"));systemWrappers.bulkGet=adminOnlyWrapper.bind(null,create401("_bulk_get"));systemWrappers.revsDiff=adminOnlyWrapper.bind(null,create401("_revs_diff"));systemWrappers.getIndexes=adminOnlyWrapper.bind(null,create401("_index"));systemWrappers.find=adminOnlyWrapper.bind(null,create401("_find"));systemWrappers.explain=adminOnlyWrapper.bind(null,create401("_explain"));systemWrappers.show=adminOnlyWrapper.bind(null,create401("_show"));systemWrappers.list=adminOnlyWrapper.bind(null,create401("_list"));systemWrappers.changes=createChangeslikeWrapper(adminOnlyWrapper.bind(null,create401("_changes")));systemWrappers.query=adminOnlyWrapper.bind(null,create401("_view (or _temp_view)"));systemWrappers.sync=createChangeslikeWrapper(adminOnlyWrapper.bind(null,create401(".sync()")));systemWrappers["replicate.from"]=createChangeslikeWrapper(adminOnlyWrapper.bind(null,create401(".replicate.from()")));systemWrappers["replicate.to"]=createChangeslikeWrapper(adminOnlyWrapper.bind(null,create401(".replicate.to()")));systemWrappers.get=adminOnlyWrapper.bind(null,{status:404,name:"not_found",message:"missing"});systemWrappers.getAttachment=wrappers.get},{"pouchdb-changeslike-wrapper":41,"pouchdb-plugin-error":42,"pouchdb-security":44,"pouchdb-wrappers":47}],46:[function(require,module,exports){"use strict";var coucheval=require("couchdb-eval");var couchdb_objects=require("couchdb-objects");var wrappers=require("pouchdb-wrappers");var createBulkDocsWrapper=require("pouchdb-bulkdocs-wrapper");var PouchPluginError=require("pouchdb-plugin-error");var uuid=require("random-uuid-v4");var Promise=require("pouchdb-promise");function oldDoc(db,id){return db.get(id,{revs:true}).catch(function(){return null})}function validate(validationFuncs,newDoc,oldDoc,options){newDoc._revisions=(oldDoc||{})._revisions;try{validationFuncs.forEach(function(validationFuncInfo){var func=validationFuncInfo.func;var designDoc=validationFuncInfo.designDoc;func.call(designDoc,newDoc,oldDoc,options.userCtx,options.secObj)})}catch(e){if(typeof e.unauthorized!=="undefined"){throw new PouchPluginError({name:"unauthorized",message:e.unauthorized,status:401})}else if(typeof e.forbidden!=="undefined"){throw new PouchPluginError({name:"forbidden",message:e.forbidden,status:403})}else{throw coucheval.wrapExecutionError(e)}}}function doValidation(db,newDoc,options){var isHttp=["http","https"].indexOf(db.type())!==-1;if(isHttp&&!options.checkHttp){return Promise.resolve()}if(String(newDoc._id).indexOf("_design/")===0||String(newDoc._id).indexOf("_local")===0){return Promise.resolve()}return getValidationFunctions(db).then(function(validationFuncs){if(!validationFuncs.length){return}var completeOptionsPromise=completeValidationOptions(db,options);var oldDocPromise=oldDoc(db,newDoc._id);return Promise.all([completeOptionsPromise,oldDocPromise]).then(Function.prototype.apply.bind(function(completeOptions,oldDoc){return validate(validationFuncs,newDoc,oldDoc,completeOptions)},null))})}function completeValidationOptions(db,options){if(!options.secObj){options.secObj={}}var userCtxPromise;if(options.userCtx){userCtxPromise=Promise.resolve(options.userCtx)}else{var buildUserContext=couchdb_objects.buildUserContextObject;userCtxPromise=db.info().then(buildUserContext)}return userCtxPromise.then(function(userCtx){options.userCtx=userCtx;return options})}function getValidationFunctions(db){return db.allDocs({startkey:"_design/",endkey:"_design0",include_docs:true}).then(parseValidationFunctions)}function parseValidationFunctions(resp){var validationFuncs=resp.rows.map(function(row){return{designDoc:row.doc,code:row.doc.validate_doc_update}});validationFuncs=validationFuncs.filter(function(info){return typeof info.code!=="undefined"});validationFuncs.forEach(function(info){info.func=coucheval.evaluate(info.designDoc,{},info.code)});return validationFuncs}var wrapperApi={};wrapperApi.put=function(orig,args){return doValidation(args.base,args.doc,args.options).then(orig)};wrapperApi.post=function(orig,args){args.doc._id=args.doc._id||uuid();return doValidation(args.base,args.doc,args.options).then(orig)};wrapperApi.remove=function(orig,args){args.doc._deleted=true;return doValidation(args.base,args.doc,args.options).then(orig)};wrapperApi.bulkDocs=createBulkDocsWrapper(function(doc,args){doc._id=doc._id||uuid();return doValidation(args.base,doc,args.options)});wrapperApi.putAttachment=function(orig,args){return args.base.get(args.docId,{rev:args.rev,revs:true}).catch(function(){return{_id:args.docId}}).then(function(doc){doc._attachments=doc._attachments||{};doc._attachments[args.attachmentId]={content_type:args.type,data:args.doc};return doValidation(args.base,doc,args.options)}).then(orig)};wrapperApi.removeAttachment=function(orig,args){return args.base.get(args.docId,{rev:args.rev,revs:true}).then(function(doc){delete doc._attachments[args.attachmentId];return doValidation(args.base,doc,args.options)}).then(orig)};Object.keys(wrapperApi).forEach(function(name){var exportName="validating"+name[0].toUpperCase()+name.substr(1);var orig=function(){return this[name].apply(this,arguments)};exports[exportName]=wrappers.createWrapperMethod(name,orig,wrapperApi[name])});exports.installValidationMethods=function(){var db=this;try{wrappers.installWrapperMethods(db,wrapperApi)}catch(err){throw new PouchPluginError({status:500,name:"already_installed",message:"Validation methods are already installed on this database."})}};exports.uninstallValidationMethods=function(){var db=this;try{wrappers.uninstallWrapperMethods(db,wrapperApi)}catch(err){throw new PouchPluginError({status:500,name:"already_not_installed",message:"Validation methods are already not installed on this database."})}}},{"couchdb-eval":30,"couchdb-objects":33,"pouchdb-bulkdocs-wrapper":40,"pouchdb-plugin-error":42,"pouchdb-promise":20,"pouchdb-wrappers":47,"random-uuid-v4":28}],47:[function(require,module,exports){"use strict";var nodify=require("promise-nodify");exports.installStaticWrapperMethods=function(PouchDB,handlers){PouchDB.new=PouchDB.new||function(name,options,callback){return new PouchDB(name,options,callback)};PouchDB.destroy=PouchDB.destroy||function(name,options,callback){var args=parseBaseArgs(PouchDB,this,options,callback);var db=new PouchDB(name,args.options);var promise=db.destroy();nodify(promise,args.callback);return promise};installWrappers(PouchDB,handlers,exports.createStaticWrapperMethod)};exports.installWrapperMethods=function(db,handlers){installWrappers(db,handlers,exports.createWrapperMethod)};function installWrappers(base,handlers,createWrapperMethod){for(var name in handlers){if(!handlers.hasOwnProperty(name)){continue}var info=getBaseAndName(base,name);var original=info.base[info.name];if(!original){continue}if(original.hasOwnProperty("_handlers")){if(original._handlers.indexOf(handlers[name])!==-1){throw new Error("Wrapper method for '"+name+"' already installed: "+handlers[name])}original._handlers.push(handlers[name])}else{info.base[info.name]=createWrapperMethod(name,original,handlers[name],base)}}}function getBaseAndName(base,name){name=name.split(".");while(name.length>1){base=base[name.shift(0)]}return{base:base,name:name[0]}}exports.createStaticWrapperMethod=function(name,original,handler,PouchDB){return createWrapper(name,original,handler,staticWrapperBuilders,PouchDB)};exports.createWrapperMethod=function(name,original,handler,db){return createWrapper(name,original,handler,wrapperBuilders,db)};function createWrapper(name,original,handler,theWrapperBuilders,thisVal){var buildWrapper=theWrapperBuilders[name];if(typeof buildWrapper==="undefined"){throw new Error("No known wrapper for method name: "+name)}var handlers=[handler];var wrapper=buildWrapper(thisVal,original,handlers);wrapper._original=original;wrapper._handlers=handlers;return wrapper}var wrapperBuilders={};wrapperBuilders.destroy=function(db,destroy,handlers){return function(options,callback){var args=parseBaseArgs(db,this,options,callback);return callHandlers(handlers,args,makeCall(destroy))}};wrapperBuilders.put=function(db,put,handlers){return function(){var args={};args.base=db||this;var argsList=Array.prototype.slice.call(arguments);args.doc=argsList.shift();var id="_id"in args.doc;do{var temp=argsList.shift();var temptype=typeof temp;if(temptype==="string"&&!id){args.doc._id=temp;id=true}else if(temptype==="string"&&id&&!("_rev"in args.doc)){args.doc._rev=temp}else if(temptype==="object"){args.options=temp}else if(temptype==="function"){args.callback=temp}}while(argsList.length);args.options=args.options||{};return callHandlers(handlers,args,function(){return put.call(this,args.doc,args.options)})}};wrapperBuilders.post=function(db,post,handlers){return function(doc,options,callback){var args=parseBaseArgs(db,this,options,callback);args.doc=doc;return callHandlers(handlers,args,function(){return post.call(this,args.doc,args.options)})}};wrapperBuilders.get=function(db,get,handlers){return function(docId,options,callback){var args=parseBaseArgs(db,this,options,callback);args.docId=docId;return callHandlers(handlers,args,function(){return get.call(this,args.docId,args.options)})}};wrapperBuilders.remove=function(db,remove,handlers){return function(docOrId,optsOrRev,opts,callback){var args;if(typeof optsOrRev==="string"){args=parseBaseArgs(db,this,opts,callback);args.doc={_id:docOrId,_rev:optsOrRev}}else{args=parseBaseArgs(db,this,optsOrRev,opts);args.doc=docOrId}return callHandlers(handlers,args,function(){return remove.call(this,args.doc,args.options)})}};wrapperBuilders.bulkDocs=function(db,bulkDocs,handlers){return function(docs,options,callback){var args=parseBaseArgs(db,this,options,callback);if(typeof docs==="object"&&"new_edits"in docs){args.options.new_edits=docs.new_edits}args.docs=docs.docs||docs;return callHandlers(handlers,args,function(){return bulkDocs.call(this,args.docs,args.options)})}};wrapperBuilders.allDocs=function(db,allDocs,handlers){return function(options,callback){var args=parseBaseArgs(db,this,options,callback);return callHandlers(handlers,args,makeCallWithOptions(allDocs,args))}};wrapperBuilders.bulkGet=wrapperBuilders.allDocs;wrapperBuilders.changes=function(db,changes,handlers){return function(options,callback){var args=parseBaseArgs(db,this,options,callback);return callHandlers(handlers,args,makeCallWithOptions(changes,args))}};wrapperBuilders.sync=function(db,replicate,handlers){return function(url,options,callback){var args=parseBaseArgs(db,this,options,callback);args.url=url;return callHandlers(handlers,args,function(){return replicate.call(this,args.url,args.options)})}};wrapperBuilders["replicate.from"]=wrapperBuilders.sync;wrapperBuilders["replicate.to"]=wrapperBuilders.sync;wrapperBuilders.putAttachment=function(db,putAttachment,handlers){return function(docId,attachmentId,rev,doc,type,options,callback){var args;if(typeof type==="string"){args=parseBaseArgs(db,this,options,callback);args.rev=rev;args.doc=doc;args.type=type}else{args=parseBaseArgs(db,this,type,options);args.rev=null;args.doc=rev;args.type=doc}args.docId=docId;args.attachmentId=attachmentId;return callHandlers(handlers,args,function(){return putAttachment.call(this,args.docId,args.attachmentId,args.rev,args.doc,args.type)})}};wrapperBuilders.getAttachment=function(db,getAttachment,handlers){return function(docId,attachmentId,options,callback){var args=parseBaseArgs(db,this,options,callback);args.docId=docId;args.attachmentId=attachmentId;return callHandlers(handlers,args,function(){return getAttachment.call(this,args.docId,args.attachmentId,args.options)})}};wrapperBuilders.removeAttachment=function(db,removeAttachment,handlers){return function(docId,attachmentId,rev,options,callback){var args=parseBaseArgs(db,this,options,callback);args.docId=docId;args.attachmentId=attachmentId;args.rev=rev;return callHandlers(handlers,args,function(){return removeAttachment.call(this,args.docId,args.attachmentId,args.rev)})}};wrapperBuilders.query=function(db,query,handlers){return function(fun,options,callback){var args=parseBaseArgs(db,this,options,callback);args.fun=fun;return callHandlers(handlers,args,function(){return query.call(this,args.fun,args.options)})}};wrapperBuilders.viewCleanup=function(db,viewCleanup,handlers){return function(options,callback){var args=parseBaseArgs(db,this,options,callback);return callHandlers(handlers,args,makeCallWithOptions(viewCleanup,args))}};wrapperBuilders.createIndex=function(db,createIndex,handlers){return function(index,options,callback){var args=parseBaseArgs(db,this,options,callback);args.index=index;return callHandlers(handlers,args,function(){return createIndex.call(this,args.index)})}};wrapperBuilders.deleteIndex=wrapperBuilders.createIndex;wrapperBuilders.find=function(db,find,handlers){return function(request,options,callback){var args=parseBaseArgs(db,this,options,callback);args.request=request;return callHandlers(handlers,args,function(){return find.call(this,args.request)})}};wrapperBuilders.explain=wrapperBuilders.find;wrapperBuilders.info=function(db,info,handlers){return function(options,callback){var args=parseBaseArgs(db,this,options,callback);return callHandlers(handlers,args,makeCall(info))}};wrapperBuilders.getIndexes=wrapperBuilders.info;wrapperBuilders.compact=function(db,compact,handlers){return function(options,callback){var args=parseBaseArgs(db,this,options,callback);return callHandlers(handlers,args,makeCallWithOptions(compact,args))}};wrapperBuilders.revsDiff=function(db,revsDiff,handlers){return function(diff,options,callback){var args=parseBaseArgs(db,this,options,callback);args.diff=diff;return callHandlers(handlers,args,function(){return revsDiff.call(this,args.diff)})}};wrapperBuilders.list=function(db,orig,handlers){return function(path,options,callback){var args=parseBaseArgs(db,this,options,callback);args.path=path;return callHandlers(handlers,args,function(){return orig.call(this,args.path,args.options)})}};wrapperBuilders.rewriteResultRequestObject=wrapperBuilders.list;wrapperBuilders.show=wrapperBuilders.list;wrapperBuilders.update=wrapperBuilders.list;wrapperBuilders.getSecurity=function(db,getSecurity,handlers){return function(options,callback){var args=parseBaseArgs(db,this,options,callback);return callHandlers(handlers,args,makeCallWithOptions(getSecurity,args))}};wrapperBuilders.putSecurity=function(db,putSecurity,handlers){return function(secObj,options,callback){var args=parseBaseArgs(db,this,options,callback);args.secObj=secObj;return callHandlers(handlers,args,function(){return putSecurity.call(this,args.secObj)})}};var staticWrapperBuilders={};staticWrapperBuilders.new=function(PouchDB,construct,handlers){return function(name,options,callback){var args;if(typeof name==="object"){args=parseBaseArgs(PouchDB,this,name,options)}else{args=parseBaseArgs(PouchDB,this,options,callback);args.options.name=name}return callHandlers(handlers,args,function(){return construct.call(this,args.options)})}};staticWrapperBuilders.destroy=function(PouchDB,destroy,handlers){return function(name,options,callback){var args;if(typeof name==="object"){args=parseBaseArgs(PouchDB,this,name,options)}else{args=parseBaseArgs(PouchDB,this,options,callback);args.options.name=name}if(args.options.internal){return destroy.apply(PouchDB,arguments)}return callHandlers(handlers,args,function(){var name=args.options.name;delete args.options.name;return destroy.call(this,name,args.options)})}};staticWrapperBuilders.replicate=function(PouchDB,replicate,handlers){return function(source,target,options,callback){var args=parseBaseArgs(PouchDB,this,options,callback);args.source=source;args.target=target;return callHandlers(handlers,args,function(){return replicate.call(this,args.source,args.target,args.options)})}};staticWrapperBuilders.allDbs=function(PouchDB,allDbs,handlers){return function(options,callback){var args=parseBaseArgs(PouchDB,this,options,callback);return callHandlers(handlers,args,makeCall(allDbs))}};function parseBaseArgs(thisVal1,thisVal2,options,callback){if(typeof options==="function"){callback=options;options={}}return{base:thisVal1||thisVal2,options:options||{},callback:callback}}function callHandlers(handlers,args,method){var callback=args.callback;delete args.callback;method=method.bind(args.base);for(var i=handlers.length-1;i>=0;i-=1){method=handlers[i].bind(null,method,args)}var promise=method();nodify(promise,callback);return promise}function makeCall(func){return function(){return func.call(this)}}function makeCallWithOptions(func,args){return function(){return func.call(this,args.options)}}exports.uninstallWrapperMethods=function(db,handlers){uninstallWrappers(db,handlers)};exports.uninstallStaticWrapperMethods=function(PouchDB,handlers){uninstallWrappers(PouchDB,handlers)};function uninstallWrappers(base,handlers){for(var name in handlers){if(!handlers.hasOwnProperty(name)){continue}var info=getBaseAndName(base,name);var wrapper=info.base[info.name];if(typeof wrapper==="undefined"){continue}var idx;try{idx=wrapper._handlers.indexOf(handlers[name])}catch(err){idx=-1}if(idx===-1){throw new Error("Wrapper method for '"+name+"' not installed: "+handlers[name])}wrapper._handlers.splice(idx,1);if(!wrapper._handlers.length){delete info.base[info.name];if(info.base[info.name]!==wrapper._original){info.base[info.name]=wrapper._original}}}}},{"promise-nodify":24}]},{},[39])(39)});