diff --git a/dist/web/pubnub.js b/dist/web/pubnub.js index a58892aef..c34e700a2 100644 --- a/dist/web/pubnub.js +++ b/dist/web/pubnub.js @@ -11210,7 +11210,7 @@ } get path() { const { keySet: { subscribeKey }, uuid, channels, } = this.parameters; - return `/v2/presence/sub-key/${subscribeKey}/channel/${encodeNames(channels !== null && channels !== void 0 ? channels : [], ',')}/uuid/${uuid}`; + return `/v2/presence/sub-key/${subscribeKey}/channel/${encodeNames(channels !== null && channels !== void 0 ? channels : [], ',')}/uuid/${encodeString(uuid !== null && uuid !== void 0 ? uuid : '')}`; } get queryParameters() { const { channelGroups } = this.parameters; @@ -11243,7 +11243,7 @@ const { keySet: { subscribeKey }, state, channels = [], channelGroups = [], } = this.parameters; if (!subscribeKey) return 'Missing Subscribe Key'; - if (!state) + if (state === undefined) return 'Missing State'; if ((channels === null || channels === void 0 ? void 0 : channels.length) === 0 && (channelGroups === null || channelGroups === void 0 ? void 0 : channelGroups.length) === 0) return 'Please provide a list of channels and/or channel-groups'; @@ -11309,7 +11309,7 @@ const query = { heartbeat: `${heartbeat}` }; if (channelGroups && channelGroups.length !== 0) query['channel-group'] = channelGroups.join(','); - if (state) + if (state !== undefined) query.state = JSON.stringify(state); return query; } diff --git a/dist/web/pubnub.min.js b/dist/web/pubnub.min.js index 4d881cb9b..acd035b03 100644 --- a/dist/web/pubnub.min.js +++ b/dist/web/pubnub.min.js @@ -1,2 +1,2 @@ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).PubNub=t()}(this,(function(){"use strict";var e="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};function t(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var s={exports:{}};!function(t){!function(e,s){var n=Math.pow(2,-24),r=Math.pow(2,32),i=Math.pow(2,53);var a={encode:function(e){var t,n=new ArrayBuffer(256),a=new DataView(n),o=0;function c(e){for(var s=n.byteLength,r=o+e;s>2,u=0;u>6),r.push(128|63&a)):a<55296?(r.push(224|a>>12),r.push(128|a>>6&63),r.push(128|63&a)):(a=(1023&a)<<10,a|=1023&t.charCodeAt(++n),a+=65536,r.push(240|a>>18),r.push(128|a>>12&63),r.push(128|a>>6&63),r.push(128|63&a))}return d(3,r.length),h(r);default:var p;if(Array.isArray(t))for(d(4,p=t.length),n=0;n>5!==e)throw"Invalid indefinite length element";return s}function y(e,t){for(var s=0;s>10),e.push(56320|1023&n))}}"function"!=typeof t&&(t=function(e){return e}),"function"!=typeof i&&(i=function(){return s});var m=function e(){var r,d,m=l(),f=m>>5,v=31&m;if(7===f)switch(v){case 25:return function(){var e=new ArrayBuffer(4),t=new DataView(e),s=h(),r=32768&s,i=31744&s,a=1023&s;if(31744===i)i=261120;else if(0!==i)i+=114688;else if(0!==a)return a*n;return t.setUint32(0,r<<16|i<<13|a<<13),t.getFloat32(0)}();case 26:return c(a.getFloat32(o),4);case 27:return c(a.getFloat64(o),8)}if((d=g(v))<0&&(f<2||6=0;)w+=d,S.push(u(d));var O=new Uint8Array(w),k=0;for(r=0;r=0;)y(C,d);else y(C,d);return String.fromCharCode.apply(null,C);case 4:var P;if(d<0)for(P=[];!p();)P.push(e());else for(P=new Array(d),r=0;re.toString())).join(", ")}]}`}}a.encoder=new TextEncoder,a.decoder=new TextDecoder;class o{static create(e){return new o(e)}constructor(e){let t,s,n,r;if(e instanceof File)r=e,n=e.name,s=e.type,t=e.size;else if("data"in e){const i=e.data;s=e.mimeType,n=e.name,r=new File([i],n,{type:s}),t=r.size}if(void 0===r)throw new Error("Couldn't construct a file out of supplied options.");if(void 0===n)throw new Error("Couldn't guess filename out of the options. Please provide one.");t&&(this.contentLength=t),this.mimeType=s,this.data=r,this.name=n}toBuffer(){return i(this,void 0,void 0,(function*(){throw new Error("This feature is only supported in Node.js environments.")}))}toArrayBuffer(){return i(this,void 0,void 0,(function*(){return new Promise(((e,t)=>{const s=new FileReader;s.addEventListener("load",(()=>{if(s.result instanceof ArrayBuffer)return e(s.result)})),s.addEventListener("error",(()=>t(s.error))),s.readAsArrayBuffer(this.data)}))}))}toString(){return i(this,void 0,void 0,(function*(){return new Promise(((e,t)=>{const s=new FileReader;s.addEventListener("load",(()=>{if("string"==typeof s.result)return e(s.result)})),s.addEventListener("error",(()=>{t(s.error)})),s.readAsBinaryString(this.data)}))}))}toStream(){return i(this,void 0,void 0,(function*(){throw new Error("This feature is only supported in Node.js environments.")}))}toFile(){return i(this,void 0,void 0,(function*(){return this.data}))}toFileUri(){return i(this,void 0,void 0,(function*(){throw new Error("This feature is only supported in React Native environments.")}))}toBlob(){return i(this,void 0,void 0,(function*(){return this.data}))}}o.supportsBlob="undefined"!=typeof Blob,o.supportsFile="undefined"!=typeof File,o.supportsBuffer=!1,o.supportsStream=!1,o.supportsString=!0,o.supportsArrayBuffer=!0,o.supportsEncryptFile=!0,o.supportsFileUri=!1;function c(e){const t=e.replace(/==?$/,""),s=Math.floor(t.length/4*3),n=new ArrayBuffer(s),r=new Uint8Array(n);let i=0;function a(){const e=t.charAt(i++),s="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".indexOf(e);if(-1===s)throw new Error(`Illegal character at ${i}: ${t.charAt(i-1)}`);return s}for(let e=0;e>4,c=(15&s)<<4|n>>2,u=(3&n)<<6|i;r[e]=o,64!=n&&(r[e+1]=c),64!=i&&(r[e+2]=u)}return n}function u(e){let t="";const s="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",n=new Uint8Array(e),r=n.byteLength,i=r%3,a=r-i;let o,c,u,l,h;for(let e=0;e>18,c=(258048&h)>>12,u=(4032&h)>>6,l=63&h,t+=s[o]+s[c]+s[u]+s[l];return 1==i?(h=n[a],o=(252&h)>>2,c=(3&h)<<4,t+=s[o]+s[c]+"=="):2==i&&(h=n[a]<<8|n[a+1],o=(64512&h)>>10,c=(1008&h)>>4,u=(15&h)<<2,t+=s[o]+s[c]+s[u]+"="),t}var l;!function(e){e.PNNetworkIssuesCategory="PNNetworkIssuesCategory",e.PNTimeoutCategory="PNTimeoutCategory",e.PNCancelledCategory="PNCancelledCategory",e.PNBadRequestCategory="PNBadRequestCategory",e.PNAccessDeniedCategory="PNAccessDeniedCategory",e.PNValidationErrorCategory="PNValidationErrorCategory",e.PNAcknowledgmentCategory="PNAcknowledgmentCategory",e.PNMalformedResponseCategory="PNMalformedResponseCategory",e.PNServerErrorCategory="PNServerErrorCategory",e.PNUnknownCategory="PNUnknownCategory",e.PNNetworkUpCategory="PNNetworkUpCategory",e.PNNetworkDownCategory="PNNetworkDownCategory",e.PNReconnectedCategory="PNReconnectedCategory",e.PNConnectedCategory="PNConnectedCategory",e.PNSubscriptionChangedCategory="PNSubscriptionChangedCategory",e.PNRequestMessageCountExceededCategory="PNRequestMessageCountExceededCategory",e.PNDisconnectedCategory="PNDisconnectedCategory",e.PNConnectionErrorCategory="PNConnectionErrorCategory",e.PNDisconnectedUnexpectedlyCategory="PNDisconnectedUnexpectedlyCategory",e.PNSharedWorkerUpdatedCategory="PNSharedWorkerUpdatedCategory"}(l||(l={}));var h=l;class d extends Error{constructor(e,t){super(e),this.status=t,this.name="PubNubError",this.message=e,Object.setPrototypeOf(this,new.target.prototype)}}function p(e,t){var s;return null!==(s=e.statusCode)&&void 0!==s||(e.statusCode=0),Object.assign(Object.assign({},e),{statusCode:e.statusCode,category:t,error:!0})}function g(e,t){return p(Object.assign(Object.assign({message:"Unable to deserialize service response"},void 0!==e?{responseText:e}:{}),void 0!==t?{statusCode:t}:{}),h.PNMalformedResponseCategory)}var b,y,m,f,v,S=S||function(e){var t={},s=t.lib={},n=function(){},r=s.Base={extend:function(e){n.prototype=this;var t=new n;return e&&t.mixIn(e),t.hasOwnProperty("init")||(t.init=function(){t.$super.init.apply(this,arguments)}),t.init.prototype=t,t.$super=this,t},create:function(){var e=this.extend();return e.init.apply(e,arguments),e},init:function(){},mixIn:function(e){for(var t in e)e.hasOwnProperty(t)&&(this[t]=e[t]);e.hasOwnProperty("toString")&&(this.toString=e.toString)},clone:function(){return this.init.prototype.extend(this)}},i=s.WordArray=r.extend({init:function(e,t){e=this.words=e||[],this.sigBytes=null!=t?t:4*e.length},toString:function(e){return(e||o).stringify(this)},concat:function(e){var t=this.words,s=e.words,n=this.sigBytes;if(e=e.sigBytes,this.clamp(),n%4)for(var r=0;r>>2]|=(s[r>>>2]>>>24-r%4*8&255)<<24-(n+r)%4*8;else if(65535>>2]=s[r>>>2];else t.push.apply(t,s);return this.sigBytes+=e,this},clamp:function(){var t=this.words,s=this.sigBytes;t[s>>>2]&=4294967295<<32-s%4*8,t.length=e.ceil(s/4)},clone:function(){var e=r.clone.call(this);return e.words=this.words.slice(0),e},random:function(t){for(var s=[],n=0;n>>2]>>>24-n%4*8&255;s.push((r>>>4).toString(16)),s.push((15&r).toString(16))}return s.join("")},parse:function(e){for(var t=e.length,s=[],n=0;n>>3]|=parseInt(e.substr(n,2),16)<<24-n%8*4;return new i.init(s,t/2)}},c=a.Latin1={stringify:function(e){var t=e.words;e=e.sigBytes;for(var s=[],n=0;n>>2]>>>24-n%4*8&255));return s.join("")},parse:function(e){for(var t=e.length,s=[],n=0;n>>2]|=(255&e.charCodeAt(n))<<24-n%4*8;return new i.init(s,t)}},u=a.Utf8={stringify:function(e){try{return decodeURIComponent(escape(c.stringify(e)))}catch(e){throw Error("Malformed UTF-8 data")}},parse:function(e){return c.parse(unescape(encodeURIComponent(e)))}},l=s.BufferedBlockAlgorithm=r.extend({reset:function(){this._data=new i.init,this._nDataBytes=0},_append:function(e){"string"==typeof e&&(e=u.parse(e)),this._data.concat(e),this._nDataBytes+=e.sigBytes},_process:function(t){var s=this._data,n=s.words,r=s.sigBytes,a=this.blockSize,o=r/(4*a);if(t=(o=t?e.ceil(o):e.max((0|o)-this._minBufferSize,0))*a,r=e.min(4*t,r),t){for(var c=0;cu;){var l;e:{l=c;for(var h=e.sqrt(l),d=2;d<=h;d++)if(!(l%d)){l=!1;break e}l=!0}l&&(8>u&&(i[u]=o(e.pow(c,.5))),a[u]=o(e.pow(c,1/3)),u++),c++}var p=[];r=r.SHA256=n.extend({_doReset:function(){this._hash=new s.init(i.slice(0))},_doProcessBlock:function(e,t){for(var s=this._hash.words,n=s[0],r=s[1],i=s[2],o=s[3],c=s[4],u=s[5],l=s[6],h=s[7],d=0;64>d;d++){if(16>d)p[d]=0|e[t+d];else{var g=p[d-15],b=p[d-2];p[d]=((g<<25|g>>>7)^(g<<14|g>>>18)^g>>>3)+p[d-7]+((b<<15|b>>>17)^(b<<13|b>>>19)^b>>>10)+p[d-16]}g=h+((c<<26|c>>>6)^(c<<21|c>>>11)^(c<<7|c>>>25))+(c&u^~c&l)+a[d]+p[d],b=((n<<30|n>>>2)^(n<<19|n>>>13)^(n<<10|n>>>22))+(n&r^n&i^r&i),h=l,l=u,u=c,c=o+g|0,o=i,i=r,r=n,n=g+b|0}s[0]=s[0]+n|0,s[1]=s[1]+r|0,s[2]=s[2]+i|0,s[3]=s[3]+o|0,s[4]=s[4]+c|0,s[5]=s[5]+u|0,s[6]=s[6]+l|0,s[7]=s[7]+h|0},_doFinalize:function(){var t=this._data,s=t.words,n=8*this._nDataBytes,r=8*t.sigBytes;return s[r>>>5]|=128<<24-r%32,s[14+(r+64>>>9<<4)]=e.floor(n/4294967296),s[15+(r+64>>>9<<4)]=n,t.sigBytes=4*s.length,this._process(),this._hash},clone:function(){var e=n.clone.call(this);return e._hash=this._hash.clone(),e}});t.SHA256=n._createHelper(r),t.HmacSHA256=n._createHmacHelper(r)}(Math),y=(b=S).enc.Utf8,b.algo.HMAC=b.lib.Base.extend({init:function(e,t){e=this._hasher=new e.init,"string"==typeof t&&(t=y.parse(t));var s=e.blockSize,n=4*s;t.sigBytes>n&&(t=e.finalize(t)),t.clamp();for(var r=this._oKey=t.clone(),i=this._iKey=t.clone(),a=r.words,o=i.words,c=0;c>>2]>>>24-r%4*8&255)<<16|(t[r+1>>>2]>>>24-(r+1)%4*8&255)<<8|t[r+2>>>2]>>>24-(r+2)%4*8&255,a=0;4>a&&r+.75*a>>6*(3-a)&63));if(t=n.charAt(64))for(;e.length%4;)e.push(t);return e.join("")},parse:function(e){var t=e.length,s=this._map;(n=s.charAt(64))&&-1!=(n=e.indexOf(n))&&(t=n);for(var n=[],r=0,i=0;i>>6-i%4*2;n[r>>>2]|=(a|o)<<24-r%4*8,r++}return f.create(n,r)},_map:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="},function(e){function t(e,t,s,n,r,i,a){return((e=e+(t&s|~t&n)+r+a)<>>32-i)+t}function s(e,t,s,n,r,i,a){return((e=e+(t&n|s&~n)+r+a)<>>32-i)+t}function n(e,t,s,n,r,i,a){return((e=e+(t^s^n)+r+a)<>>32-i)+t}function r(e,t,s,n,r,i,a){return((e=e+(s^(t|~n))+r+a)<>>32-i)+t}for(var i=S,a=(c=i.lib).WordArray,o=c.Hasher,c=i.algo,u=[],l=0;64>l;l++)u[l]=4294967296*e.abs(e.sin(l+1))|0;c=c.MD5=o.extend({_doReset:function(){this._hash=new a.init([1732584193,4023233417,2562383102,271733878])},_doProcessBlock:function(e,i){for(var a=0;16>a;a++){var o=e[c=i+a];e[c]=16711935&(o<<8|o>>>24)|4278255360&(o<<24|o>>>8)}a=this._hash.words;var c=e[i+0],l=(o=e[i+1],e[i+2]),h=e[i+3],d=e[i+4],p=e[i+5],g=e[i+6],b=e[i+7],y=e[i+8],m=e[i+9],f=e[i+10],v=e[i+11],S=e[i+12],w=e[i+13],O=e[i+14],k=e[i+15],C=t(C=a[0],E=a[1],j=a[2],P=a[3],c,7,u[0]),P=t(P,C,E,j,o,12,u[1]),j=t(j,P,C,E,l,17,u[2]),E=t(E,j,P,C,h,22,u[3]);C=t(C,E,j,P,d,7,u[4]),P=t(P,C,E,j,p,12,u[5]),j=t(j,P,C,E,g,17,u[6]),E=t(E,j,P,C,b,22,u[7]),C=t(C,E,j,P,y,7,u[8]),P=t(P,C,E,j,m,12,u[9]),j=t(j,P,C,E,f,17,u[10]),E=t(E,j,P,C,v,22,u[11]),C=t(C,E,j,P,S,7,u[12]),P=t(P,C,E,j,w,12,u[13]),j=t(j,P,C,E,O,17,u[14]),C=s(C,E=t(E,j,P,C,k,22,u[15]),j,P,o,5,u[16]),P=s(P,C,E,j,g,9,u[17]),j=s(j,P,C,E,v,14,u[18]),E=s(E,j,P,C,c,20,u[19]),C=s(C,E,j,P,p,5,u[20]),P=s(P,C,E,j,f,9,u[21]),j=s(j,P,C,E,k,14,u[22]),E=s(E,j,P,C,d,20,u[23]),C=s(C,E,j,P,m,5,u[24]),P=s(P,C,E,j,O,9,u[25]),j=s(j,P,C,E,h,14,u[26]),E=s(E,j,P,C,y,20,u[27]),C=s(C,E,j,P,w,5,u[28]),P=s(P,C,E,j,l,9,u[29]),j=s(j,P,C,E,b,14,u[30]),C=n(C,E=s(E,j,P,C,S,20,u[31]),j,P,p,4,u[32]),P=n(P,C,E,j,y,11,u[33]),j=n(j,P,C,E,v,16,u[34]),E=n(E,j,P,C,O,23,u[35]),C=n(C,E,j,P,o,4,u[36]),P=n(P,C,E,j,d,11,u[37]),j=n(j,P,C,E,b,16,u[38]),E=n(E,j,P,C,f,23,u[39]),C=n(C,E,j,P,w,4,u[40]),P=n(P,C,E,j,c,11,u[41]),j=n(j,P,C,E,h,16,u[42]),E=n(E,j,P,C,g,23,u[43]),C=n(C,E,j,P,m,4,u[44]),P=n(P,C,E,j,S,11,u[45]),j=n(j,P,C,E,k,16,u[46]),C=r(C,E=n(E,j,P,C,l,23,u[47]),j,P,c,6,u[48]),P=r(P,C,E,j,b,10,u[49]),j=r(j,P,C,E,O,15,u[50]),E=r(E,j,P,C,p,21,u[51]),C=r(C,E,j,P,S,6,u[52]),P=r(P,C,E,j,h,10,u[53]),j=r(j,P,C,E,f,15,u[54]),E=r(E,j,P,C,o,21,u[55]),C=r(C,E,j,P,y,6,u[56]),P=r(P,C,E,j,k,10,u[57]),j=r(j,P,C,E,g,15,u[58]),E=r(E,j,P,C,w,21,u[59]),C=r(C,E,j,P,d,6,u[60]),P=r(P,C,E,j,v,10,u[61]),j=r(j,P,C,E,l,15,u[62]),E=r(E,j,P,C,m,21,u[63]);a[0]=a[0]+C|0,a[1]=a[1]+E|0,a[2]=a[2]+j|0,a[3]=a[3]+P|0},_doFinalize:function(){var t=this._data,s=t.words,n=8*this._nDataBytes,r=8*t.sigBytes;s[r>>>5]|=128<<24-r%32;var i=e.floor(n/4294967296);for(s[15+(r+64>>>9<<4)]=16711935&(i<<8|i>>>24)|4278255360&(i<<24|i>>>8),s[14+(r+64>>>9<<4)]=16711935&(n<<8|n>>>24)|4278255360&(n<<24|n>>>8),t.sigBytes=4*(s.length+1),this._process(),s=(t=this._hash).words,n=0;4>n;n++)r=s[n],s[n]=16711935&(r<<8|r>>>24)|4278255360&(r<<24|r>>>8);return t},clone:function(){var e=o.clone.call(this);return e._hash=this._hash.clone(),e}}),i.MD5=o._createHelper(c),i.HmacMD5=o._createHmacHelper(c)}(Math),function(){var e,t=S,s=(e=t.lib).Base,n=e.WordArray,r=(e=t.algo).EvpKDF=s.extend({cfg:s.extend({keySize:4,hasher:e.MD5,iterations:1}),init:function(e){this.cfg=this.cfg.extend(e)},compute:function(e,t){for(var s=(o=this.cfg).hasher.create(),r=n.create(),i=r.words,a=o.keySize,o=o.iterations;i.length>>2]}},e.BlockCipher=a.extend({cfg:a.cfg.extend({mode:o,padding:u}),reset:function(){a.reset.call(this);var e=(t=this.cfg).iv,t=t.mode;if(this._xformMode==this._ENC_XFORM_MODE)var s=t.createEncryptor;else s=t.createDecryptor,this._minBufferSize=1;this._mode=s.call(t,this,e&&e.words)},_doProcessBlock:function(e,t){this._mode.processBlock(e,t)},_doFinalize:function(){var e=this.cfg.padding;if(this._xformMode==this._ENC_XFORM_MODE){e.pad(this._data,this.blockSize);var t=this._process(!0)}else t=this._process(!0),e.unpad(t);return t},blockSize:4});var l=e.CipherParams=t.extend({init:function(e){this.mixIn(e)},toString:function(e){return(e||this.formatter).stringify(this)}}),h=(o=(d.format={}).OpenSSL={stringify:function(e){var t=e.ciphertext;return((e=e.salt)?s.create([1398893684,1701076831]).concat(e).concat(t):t).toString(r)},parse:function(e){var t=(e=r.parse(e)).words;if(1398893684==t[0]&&1701076831==t[1]){var n=s.create(t.slice(2,4));t.splice(0,4),e.sigBytes-=16}return l.create({ciphertext:e,salt:n})}},e.SerializableCipher=t.extend({cfg:t.extend({format:o}),encrypt:function(e,t,s,n){n=this.cfg.extend(n);var r=e.createEncryptor(s,n);return t=r.finalize(t),r=r.cfg,l.create({ciphertext:t,key:s,iv:r.iv,algorithm:e,mode:r.mode,padding:r.padding,blockSize:e.blockSize,formatter:n.format})},decrypt:function(e,t,s,n){return n=this.cfg.extend(n),t=this._parse(t,n.format),e.createDecryptor(s,n).finalize(t.ciphertext)},_parse:function(e,t){return"string"==typeof e?t.parse(e,this):e}})),d=(d.kdf={}).OpenSSL={execute:function(e,t,n,r){return r||(r=s.random(8)),e=i.create({keySize:t+n}).compute(e,r),n=s.create(e.words.slice(t),4*n),e.sigBytes=4*t,l.create({key:e,iv:n,salt:r})}},p=e.PasswordBasedCipher=h.extend({cfg:h.cfg.extend({kdf:d}),encrypt:function(e,t,s,n){return s=(n=this.cfg.extend(n)).kdf.execute(s,e.keySize,e.ivSize),n.iv=s.iv,(e=h.encrypt.call(this,e,t,s.key,n)).mixIn(s),e},decrypt:function(e,t,s,n){return n=this.cfg.extend(n),t=this._parse(t,n.format),s=n.kdf.execute(s,e.keySize,e.ivSize,t.salt),n.iv=s.iv,h.decrypt.call(this,e,t,s.key,n)}})}(),function(){for(var e=S,t=e.lib.BlockCipher,s=e.algo,n=[],r=[],i=[],a=[],o=[],c=[],u=[],l=[],h=[],d=[],p=[],g=0;256>g;g++)p[g]=128>g?g<<1:g<<1^283;var b=0,y=0;for(g=0;256>g;g++){var m=(m=y^y<<1^y<<2^y<<3^y<<4)>>>8^255&m^99;n[b]=m,r[m]=b;var f=p[b],v=p[f],w=p[v],O=257*p[m]^16843008*m;i[b]=O<<24|O>>>8,a[b]=O<<16|O>>>16,o[b]=O<<8|O>>>24,c[b]=O,O=16843009*w^65537*v^257*f^16843008*b,u[m]=O<<24|O>>>8,l[m]=O<<16|O>>>16,h[m]=O<<8|O>>>24,d[m]=O,b?(b=f^p[p[p[w^f]]],y^=p[p[y]]):b=y=1}var k=[0,1,2,4,8,16,32,64,128,27,54];s=s.AES=t.extend({_doReset:function(){for(var e=(s=this._key).words,t=s.sigBytes/4,s=4*((this._nRounds=t+6)+1),r=this._keySchedule=[],i=0;i>>24]<<24|n[a>>>16&255]<<16|n[a>>>8&255]<<8|n[255&a]):(a=n[(a=a<<8|a>>>24)>>>24]<<24|n[a>>>16&255]<<16|n[a>>>8&255]<<8|n[255&a],a^=k[i/t|0]<<24),r[i]=r[i-t]^a}for(e=this._invKeySchedule=[],t=0;tt||4>=i?a:u[n[a>>>24]]^l[n[a>>>16&255]]^h[n[a>>>8&255]]^d[n[255&a]]},encryptBlock:function(e,t){this._doCryptBlock(e,t,this._keySchedule,i,a,o,c,n)},decryptBlock:function(e,t){var s=e[t+1];e[t+1]=e[t+3],e[t+3]=s,this._doCryptBlock(e,t,this._invKeySchedule,u,l,h,d,r),s=e[t+1],e[t+1]=e[t+3],e[t+3]=s},_doCryptBlock:function(e,t,s,n,r,i,a,o){for(var c=this._nRounds,u=e[t]^s[0],l=e[t+1]^s[1],h=e[t+2]^s[2],d=e[t+3]^s[3],p=4,g=1;g>>24]^r[l>>>16&255]^i[h>>>8&255]^a[255&d]^s[p++],y=n[l>>>24]^r[h>>>16&255]^i[d>>>8&255]^a[255&u]^s[p++],m=n[h>>>24]^r[d>>>16&255]^i[u>>>8&255]^a[255&l]^s[p++];d=n[d>>>24]^r[u>>>16&255]^i[l>>>8&255]^a[255&h]^s[p++],u=b,l=y,h=m}b=(o[u>>>24]<<24|o[l>>>16&255]<<16|o[h>>>8&255]<<8|o[255&d])^s[p++],y=(o[l>>>24]<<24|o[h>>>16&255]<<16|o[d>>>8&255]<<8|o[255&u])^s[p++],m=(o[h>>>24]<<24|o[d>>>16&255]<<16|o[u>>>8&255]<<8|o[255&l])^s[p++],d=(o[d>>>24]<<24|o[u>>>16&255]<<16|o[l>>>8&255]<<8|o[255&h])^s[p++],e[t]=b,e[t+1]=y,e[t+2]=m,e[t+3]=d},keySize:8});e.AES=t._createHelper(s)}(),S.mode.ECB=((v=S.lib.BlockCipherMode.extend()).Encryptor=v.extend({processBlock:function(e,t){this._cipher.encryptBlock(e,t)}}),v.Decryptor=v.extend({processBlock:function(e,t){this._cipher.decryptBlock(e,t)}}),v);var w,O=t(S);class k{constructor({cipherKey:e}){this.cipherKey=e,this.CryptoJS=O,this.encryptedKey=this.CryptoJS.SHA256(e)}encrypt(e){if(0===("string"==typeof e?e:k.decoder.decode(e)).length)throw new Error("encryption error. empty content");const t=this.getIv();return{metadata:t,data:c(this.CryptoJS.AES.encrypt(e,this.encryptedKey,{iv:this.bufferToWordArray(t),mode:this.CryptoJS.mode.CBC}).ciphertext.toString(this.CryptoJS.enc.Base64))}}encryptFileData(e){return i(this,void 0,void 0,(function*(){const t=yield this.getKey(),s=this.getIv();return{data:yield crypto.subtle.encrypt({name:this.algo,iv:s},t,e),metadata:s}}))}decrypt(e){if("string"==typeof e.data)throw new Error("Decryption error: data for decryption should be ArrayBuffed.");const t=this.bufferToWordArray(new Uint8ClampedArray(e.metadata)),s=this.bufferToWordArray(new Uint8ClampedArray(e.data));return k.encoder.encode(this.CryptoJS.AES.decrypt({ciphertext:s},this.encryptedKey,{iv:t,mode:this.CryptoJS.mode.CBC}).toString(this.CryptoJS.enc.Utf8)).buffer}decryptFileData(e){return i(this,void 0,void 0,(function*(){if("string"==typeof e.data)throw new Error("Decryption error: data for decryption should be ArrayBuffed.");const t=yield this.getKey();return crypto.subtle.decrypt({name:this.algo,iv:e.metadata},t,e.data)}))}get identifier(){return"ACRH"}get algo(){return"AES-CBC"}getIv(){return crypto.getRandomValues(new Uint8Array(k.BLOCK_SIZE))}getKey(){return i(this,void 0,void 0,(function*(){const e=k.encoder.encode(this.cipherKey),t=yield crypto.subtle.digest("SHA-256",e.buffer);return crypto.subtle.importKey("raw",t,this.algo,!0,["encrypt","decrypt"])}))}bufferToWordArray(e){const t=[];let s;for(s=0;s({messageType:"object",message:this.configuration,details:"Create with configuration:",ignoredKeys:(e,t)=>"function"==typeof t[e]||"logger"===e})))}get logger(){return this._logger}HMACSHA256(e){return O.HmacSHA256(e,this.configuration.secretKey).toString(O.enc.Base64)}SHA256(e){return O.SHA256(e).toString(O.enc.Hex)}encrypt(e,t,s){return this.configuration.customEncrypt?(this.logger&&this.logger.warn("Crypto","'customEncrypt' is deprecated. Consult docs for better alternative."),this.configuration.customEncrypt(e)):this.pnEncrypt(e,t,s)}decrypt(e,t,s){return this.configuration.customDecrypt?(this.logger&&this.logger.warn("Crypto","'customDecrypt' is deprecated. Consult docs for better alternative."),this.configuration.customDecrypt(e)):this.pnDecrypt(e,t,s)}pnEncrypt(e,t,s){const n=null!=t?t:this.configuration.cipherKey;if(!n)return e;this.logger&&this.logger.debug("Crypto",(()=>({messageType:"object",message:Object.assign({data:e,cipherKey:n},null!=s?s:{}),details:"Encrypt with parameters:"}))),s=this.parseOptions(s);const r=this.getMode(s),i=this.getPaddedKey(n,s);if(this.configuration.useRandomIVs){const t=this.getRandomIV(),s=O.AES.encrypt(e,i,{iv:t,mode:r}).ciphertext;return t.clone().concat(s.clone()).toString(O.enc.Base64)}const a=this.getIV(s);return O.AES.encrypt(e,i,{iv:a,mode:r}).ciphertext.toString(O.enc.Base64)||e}pnDecrypt(e,t,s){const n=null!=t?t:this.configuration.cipherKey;if(!n)return e;this.logger&&this.logger.debug("Crypto",(()=>({messageType:"object",message:Object.assign({data:e,cipherKey:n},null!=s?s:{}),details:"Decrypt with parameters:"}))),s=this.parseOptions(s);const r=this.getMode(s),i=this.getPaddedKey(n,s);if(this.configuration.useRandomIVs){const t=new Uint8ClampedArray(c(e)),s=C(t.slice(0,16)),n=C(t.slice(16));try{const e=O.AES.decrypt({ciphertext:n},i,{iv:s,mode:r}).toString(O.enc.Utf8);return JSON.parse(e)}catch(e){return this.logger&&this.logger.error("Crypto",(()=>({messageType:"error",message:e}))),null}}else{const t=this.getIV(s);try{const s=O.enc.Base64.parse(e),n=O.AES.decrypt({ciphertext:s},i,{iv:t,mode:r}).toString(O.enc.Utf8);return JSON.parse(n)}catch(e){return this.logger&&this.logger.error("Crypto",(()=>({messageType:"error",message:e}))),null}}}parseOptions(e){var t,s,n,r;if(!e)return this.defaultOptions;const i={encryptKey:null!==(t=e.encryptKey)&&void 0!==t?t:this.defaultOptions.encryptKey,keyEncoding:null!==(s=e.keyEncoding)&&void 0!==s?s:this.defaultOptions.keyEncoding,keyLength:null!==(n=e.keyLength)&&void 0!==n?n:this.defaultOptions.keyLength,mode:null!==(r=e.mode)&&void 0!==r?r:this.defaultOptions.mode};return-1===this.allowedKeyEncodings.indexOf(i.keyEncoding.toLowerCase())&&(i.keyEncoding=this.defaultOptions.keyEncoding),-1===this.allowedKeyLengths.indexOf(i.keyLength)&&(i.keyLength=this.defaultOptions.keyLength),-1===this.allowedModes.indexOf(i.mode.toLowerCase())&&(i.mode=this.defaultOptions.mode),i}decodeKey(e,t){return"base64"===t.keyEncoding?O.enc.Base64.parse(e):"hex"===t.keyEncoding?O.enc.Hex.parse(e):e}getPaddedKey(e,t){return e=this.decodeKey(e,t),t.encryptKey?O.enc.Utf8.parse(this.SHA256(e).slice(0,32)):e}getMode(e){return"ecb"===e.mode?O.mode.ECB:O.mode.CBC}getIV(e){return"cbc"===e.mode?O.enc.Utf8.parse(this.iv):null}getRandomIV(){return O.lib.WordArray.random(16)}}class j{encrypt(e,t){return i(this,void 0,void 0,(function*(){if(!(t instanceof ArrayBuffer)&&"string"!=typeof t)throw new Error("Cannot encrypt this file. In browsers file encryption supports only string or ArrayBuffer");const s=yield this.getKey(e);return t instanceof ArrayBuffer?this.encryptArrayBuffer(s,t):this.encryptString(s,t)}))}encryptArrayBuffer(e,t){return i(this,void 0,void 0,(function*(){const s=crypto.getRandomValues(new Uint8Array(16));return this.concatArrayBuffer(s.buffer,yield crypto.subtle.encrypt({name:"AES-CBC",iv:s},e,t))}))}encryptString(e,t){return i(this,void 0,void 0,(function*(){const s=crypto.getRandomValues(new Uint8Array(16)),n=j.encoder.encode(t).buffer,r=yield crypto.subtle.encrypt({name:"AES-CBC",iv:s},e,n),i=this.concatArrayBuffer(s.buffer,r);return j.decoder.decode(i)}))}encryptFile(e,t,s){return i(this,void 0,void 0,(function*(){var n,r;if((null!==(n=t.contentLength)&&void 0!==n?n:0)<=0)throw new Error("encryption error. empty content");const i=yield this.getKey(e),a=yield t.toArrayBuffer(),o=yield this.encryptArrayBuffer(i,a);return s.create({name:t.name,mimeType:null!==(r=t.mimeType)&&void 0!==r?r:"application/octet-stream",data:o})}))}decrypt(e,t){return i(this,void 0,void 0,(function*(){if(!(t instanceof ArrayBuffer)&&"string"!=typeof t)throw new Error("Cannot decrypt this file. In browsers file decryption supports only string or ArrayBuffer");const s=yield this.getKey(e);return t instanceof ArrayBuffer?this.decryptArrayBuffer(s,t):this.decryptString(s,t)}))}decryptArrayBuffer(e,t){return i(this,void 0,void 0,(function*(){const s=t.slice(0,16);if(t.slice(j.IV_LENGTH).byteLength<=0)throw new Error("decryption error: empty content");return yield crypto.subtle.decrypt({name:"AES-CBC",iv:s},e,t.slice(j.IV_LENGTH))}))}decryptString(e,t){return i(this,void 0,void 0,(function*(){const s=j.encoder.encode(t).buffer,n=s.slice(0,16),r=s.slice(16),i=yield crypto.subtle.decrypt({name:"AES-CBC",iv:n},e,r);return j.decoder.decode(i)}))}decryptFile(e,t,s){return i(this,void 0,void 0,(function*(){const n=yield this.getKey(e),r=yield t.toArrayBuffer(),i=yield this.decryptArrayBuffer(n,r);return s.create({name:t.name,mimeType:t.mimeType,data:i})}))}getKey(e){return i(this,void 0,void 0,(function*(){const t=yield crypto.subtle.digest("SHA-256",j.encoder.encode(e)),s=Array.from(new Uint8Array(t)).map((e=>e.toString(16).padStart(2,"0"))).join(""),n=j.encoder.encode(s.slice(0,32)).buffer;return crypto.subtle.importKey("raw",n,"AES-CBC",!0,["encrypt","decrypt"])}))}concatArrayBuffer(e,t){const s=new Uint8Array(e.byteLength+t.byteLength);return s.set(new Uint8Array(e),0),s.set(new Uint8Array(t),e.byteLength),s.buffer}}j.IV_LENGTH=16,j.encoder=new TextEncoder,j.decoder=new TextDecoder;class E{constructor(e){this.config=e,this.cryptor=new P(Object.assign({},e)),this.fileCryptor=new j}set logger(e){this.cryptor.logger=e}encrypt(e){const t="string"==typeof e?e:E.decoder.decode(e);return{data:this.cryptor.encrypt(t),metadata:null}}encryptFile(e,t){return i(this,void 0,void 0,(function*(){var s;if(!this.config.cipherKey)throw new d("File encryption error: cipher key not set.");return this.fileCryptor.encryptFile(null===(s=this.config)||void 0===s?void 0:s.cipherKey,e,t)}))}decrypt(e){const t="string"==typeof e.data?e.data:u(e.data);return this.cryptor.decrypt(t)}decryptFile(e,t){return i(this,void 0,void 0,(function*(){if(!this.config.cipherKey)throw new d("File encryption error: cipher key not set.");return this.fileCryptor.decryptFile(this.config.cipherKey,e,t)}))}get identifier(){return""}toString(){return`AesCbcCryptor { ${Object.entries(this.config).reduce(((e,[t,s])=>("logger"===t||e.push(`${t}: ${"function"==typeof s?"":s}`),e)),[]).join(", ")} }`}}E.encoder=new TextEncoder,E.decoder=new TextDecoder;class N extends a{set logger(e){if(this.defaultCryptor.identifier===N.LEGACY_IDENTIFIER)this.defaultCryptor.logger=e;else{const t=this.cryptors.find((e=>e.identifier===N.LEGACY_IDENTIFIER));t&&(t.logger=e)}}static legacyCryptoModule(e){var t;if(!e.cipherKey)throw new d("Crypto module error: cipher key not set.");return new N({default:new E(Object.assign(Object.assign({},e),{useRandomIVs:null===(t=e.useRandomIVs)||void 0===t||t})),cryptors:[new k({cipherKey:e.cipherKey})]})}static aesCbcCryptoModule(e){var t;if(!e.cipherKey)throw new d("Crypto module error: cipher key not set.");return new N({default:new k({cipherKey:e.cipherKey}),cryptors:[new E(Object.assign(Object.assign({},e),{useRandomIVs:null===(t=e.useRandomIVs)||void 0===t||t}))]})}static withDefaultCryptor(e){return new this({default:e})}encrypt(e){const t=e instanceof ArrayBuffer&&this.defaultCryptor.identifier===N.LEGACY_IDENTIFIER?this.defaultCryptor.encrypt(N.decoder.decode(e)):this.defaultCryptor.encrypt(e);if(!t.metadata)return t.data;if("string"==typeof t.data)throw new Error("Encryption error: encrypted data should be ArrayBuffed.");const s=this.getHeaderData(t);return this.concatArrayBuffer(s,t.data)}encryptFile(e,t){return i(this,void 0,void 0,(function*(){if(this.defaultCryptor.identifier===T.LEGACY_IDENTIFIER)return this.defaultCryptor.encryptFile(e,t);const s=yield this.getFileData(e),n=yield this.defaultCryptor.encryptFileData(s);if("string"==typeof n.data)throw new Error("Encryption error: encrypted data should be ArrayBuffed.");return t.create({name:e.name,mimeType:"application/octet-stream",data:this.concatArrayBuffer(this.getHeaderData(n),n.data)})}))}decrypt(e){const t="string"==typeof e?c(e):e,s=T.tryParse(t),n=this.getCryptor(s),r=s.length>0?t.slice(s.length-s.metadataLength,s.length):null;if(t.slice(s.length).byteLength<=0)throw new Error("Decryption error: empty content");return n.decrypt({data:t.slice(s.length),metadata:r})}decryptFile(e,t){return i(this,void 0,void 0,(function*(){const s=yield e.data.arrayBuffer(),n=T.tryParse(s),r=this.getCryptor(n);if((null==r?void 0:r.identifier)===T.LEGACY_IDENTIFIER)return r.decryptFile(e,t);const i=(yield this.getFileData(s)).slice(n.length-n.metadataLength,n.length);return t.create({name:e.name,data:yield this.defaultCryptor.decryptFileData({data:s.slice(n.length),metadata:i})})}))}getCryptorFromId(e){const t=this.getAllCryptors().find((t=>e===t.identifier));if(t)return t;throw Error("Unknown cryptor error")}getCryptor(e){if("string"==typeof e){const t=this.getAllCryptors().find((t=>t.identifier===e));if(t)return t;throw new Error("Unknown cryptor error")}if(e instanceof _)return this.getCryptorFromId(e.identifier)}getHeaderData(e){if(!e.metadata)return;const t=T.from(this.defaultCryptor.identifier,e.metadata),s=new Uint8Array(t.length);let n=0;return s.set(t.data,n),n+=t.length-e.metadata.byteLength,s.set(new Uint8Array(e.metadata),n),s.buffer}concatArrayBuffer(e,t){const s=new Uint8Array(e.byteLength+t.byteLength);return s.set(new Uint8Array(e),0),s.set(new Uint8Array(t),e.byteLength),s.buffer}getFileData(e){return i(this,void 0,void 0,(function*(){if(e instanceof ArrayBuffer)return e;if(e instanceof o)return e.toArrayBuffer();throw new Error("Cannot decrypt/encrypt file. In browsers file encrypt/decrypt supported for string, ArrayBuffer or Blob")}))}}N.LEGACY_IDENTIFIER="";class T{static from(e,t){if(e!==T.LEGACY_IDENTIFIER)return new _(e,t.byteLength)}static tryParse(e){const t=new Uint8Array(e);let s,n,r=null;if(t.byteLength>=4&&(s=t.slice(0,4),this.decoder.decode(s)!==T.SENTINEL))return N.LEGACY_IDENTIFIER;if(!(t.byteLength>=5))throw new Error("Decryption error: invalid header version");if(r=t[4],r>T.MAX_VERSION)throw new Error("Decryption error: Unknown cryptor error");let i=5+T.IDENTIFIER_LENGTH;if(!(t.byteLength>=i))throw new Error("Decryption error: invalid crypto identifier");n=t.slice(5,i);let a=null;if(!(t.byteLength>=i+1))throw new Error("Decryption error: invalid metadata length");return a=t[i],i+=1,255===a&&t.byteLength>=i+2&&(a=new Uint16Array(t.slice(i,i+2)).reduce(((e,t)=>(e<<8)+t),0)),new _(this.decoder.decode(n),a)}}T.SENTINEL="PNED",T.LEGACY_IDENTIFIER="",T.IDENTIFIER_LENGTH=4,T.VERSION=1,T.MAX_VERSION=1,T.decoder=new TextDecoder;class _{constructor(e,t){this._identifier=e,this._metadataLength=t}get identifier(){return this._identifier}set identifier(e){this._identifier=e}get metadataLength(){return this._metadataLength}set metadataLength(e){this._metadataLength=e}get version(){return T.VERSION}get length(){return T.SENTINEL.length+1+T.IDENTIFIER_LENGTH+(this.metadataLength<255?1:3)+this.metadataLength}get data(){let e=0;const t=new Uint8Array(this.length),s=new TextEncoder;t.set(s.encode(T.SENTINEL)),e+=T.SENTINEL.length,t[e]=this.version,e++,this.identifier&&t.set(s.encode(this.identifier),e);const n=this.metadataLength;return e+=T.IDENTIFIER_LENGTH,n<255?t[e]=n:t.set([255,n>>8,255&n],e),t}}_.IDENTIFIER_LENGTH=4,_.SENTINEL="PNED";class I extends Error{static create(e,t){return I.isErrorObject(e)?I.createFromError(e):I.createFromServiceResponse(e,t)}static createFromError(e){let t=h.PNUnknownCategory,s="Unknown error",n="Error";if(!e)return new I(s,t,0);if(e instanceof I)return e;if(I.isErrorObject(e)&&(s=e.message,n=e.name),"AbortError"===n||-1!==s.indexOf("Aborted"))t=h.PNCancelledCategory,s="Request cancelled";else if(-1!==s.toLowerCase().indexOf("timeout"))t=h.PNTimeoutCategory,s="Request timeout";else if(-1!==s.toLowerCase().indexOf("network"))t=h.PNNetworkIssuesCategory,s="Network issues";else if("TypeError"===n)t=-1!==s.indexOf("Load failed")||-1!=s.indexOf("Failed to fetch")?h.PNNetworkIssuesCategory:h.PNBadRequestCategory;else if("FetchError"===n){const n=e.code;["ECONNREFUSED","ENETUNREACH","ENOTFOUND","ECONNRESET","EAI_AGAIN"].includes(n)&&(t=h.PNNetworkIssuesCategory),"ECONNREFUSED"===n?s="Connection refused":"ENETUNREACH"===n?s="Network not reachable":"ENOTFOUND"===n?s="Server not found":"ECONNRESET"===n?s="Connection reset by peer":"EAI_AGAIN"===n?s="Name resolution error":"ETIMEDOUT"===n?(t=h.PNTimeoutCategory,s="Request timeout"):s=`Unknown system error: ${e}`}else"Request timeout"===s&&(t=h.PNTimeoutCategory);return new I(s,t,0,e)}static createFromServiceResponse(e,t){let s,n=h.PNUnknownCategory,r="Unknown error",{status:i}=e;if(null!=t||(t=e.body),402===i?r="Not available for used key set. Contact support@pubnub.com":400===i?(n=h.PNBadRequestCategory,r="Bad request"):403===i?(n=h.PNAccessDeniedCategory,r="Access denied"):i>=500&&(n=h.PNServerErrorCategory,r="Internal server error"),"object"==typeof e&&0===Object.keys(e).length&&(n=h.PNMalformedResponseCategory,r="Malformed response (network issues)",i=400),t&&t.byteLength>0){const n=(new TextDecoder).decode(t);if(-1!==e.headers["content-type"].indexOf("text/javascript")||-1!==e.headers["content-type"].indexOf("application/json"))try{const e=JSON.parse(n);"object"==typeof e&&(Array.isArray(e)?"number"==typeof e[0]&&0===e[0]&&e.length>1&&"string"==typeof e[1]&&(s=e[1]):("error"in e&&(1===e.error||!0===e.error)&&"status"in e&&"number"==typeof e.status&&"message"in e&&"service"in e?(s=e,i=e.status):s=e,"error"in e&&e.error instanceof Error&&(s=e.error)))}catch(e){s=n}else if(-1!==e.headers["content-type"].indexOf("xml")){const e=/(.*)<\/Message>/gi.exec(n);r=e?`Upload to bucket failed: ${e[1]}`:"Upload to bucket failed."}else s=n}return new I(r,n,i,s)}constructor(e,t,s,n){super(e),this.category=t,this.statusCode=s,this.errorData=n,this.name="PubNubAPIError"}toStatus(e){return{error:!0,category:this.category,operation:e,statusCode:this.statusCode,errorData:this.errorData,toJSON:function(){let e;const t=this.errorData;if(t)try{if("object"==typeof t){const s=Object.assign(Object.assign(Object.assign(Object.assign({},"name"in t?{name:t.name}:{}),"message"in t?{message:t.message}:{}),"stack"in t?{stack:t.stack}:{}),t);e=JSON.parse(JSON.stringify(s,I.circularReplacer()))}else e=t}catch(t){e={error:"Could not serialize the error object"}}const s=r(this,["toJSON"]);return JSON.stringify(Object.assign(Object.assign({},s),{errorData:e}))}}}toPubNubError(e,t){return new d(null!=t?t:this.message,this.toStatus(e))}static circularReplacer(){const e=new WeakSet;return function(t,s){if("object"==typeof s&&null!==s){if(e.has(s))return"[Circular]";e.add(s)}return s}}static isErrorObject(e){return!(!e||"object"!=typeof e)&&(e instanceof Error||("name"in e&&"message"in e&&"string"==typeof e.name&&"string"==typeof e.message||"[object Error]"===Object.prototype.toString.call(e)))}}!function(e){e.PNPublishOperation="PNPublishOperation",e.PNSignalOperation="PNSignalOperation",e.PNSubscribeOperation="PNSubscribeOperation",e.PNUnsubscribeOperation="PNUnsubscribeOperation",e.PNWhereNowOperation="PNWhereNowOperation",e.PNHereNowOperation="PNHereNowOperation",e.PNGlobalHereNowOperation="PNGlobalHereNowOperation",e.PNSetStateOperation="PNSetStateOperation",e.PNGetStateOperation="PNGetStateOperation",e.PNHeartbeatOperation="PNHeartbeatOperation",e.PNAddMessageActionOperation="PNAddActionOperation",e.PNRemoveMessageActionOperation="PNRemoveMessageActionOperation",e.PNGetMessageActionsOperation="PNGetMessageActionsOperation",e.PNTimeOperation="PNTimeOperation",e.PNHistoryOperation="PNHistoryOperation",e.PNDeleteMessagesOperation="PNDeleteMessagesOperation",e.PNFetchMessagesOperation="PNFetchMessagesOperation",e.PNMessageCounts="PNMessageCountsOperation",e.PNGetAllUUIDMetadataOperation="PNGetAllUUIDMetadataOperation",e.PNGetUUIDMetadataOperation="PNGetUUIDMetadataOperation",e.PNSetUUIDMetadataOperation="PNSetUUIDMetadataOperation",e.PNRemoveUUIDMetadataOperation="PNRemoveUUIDMetadataOperation",e.PNGetAllChannelMetadataOperation="PNGetAllChannelMetadataOperation",e.PNGetChannelMetadataOperation="PNGetChannelMetadataOperation",e.PNSetChannelMetadataOperation="PNSetChannelMetadataOperation",e.PNRemoveChannelMetadataOperation="PNRemoveChannelMetadataOperation",e.PNGetMembersOperation="PNGetMembersOperation",e.PNSetMembersOperation="PNSetMembersOperation",e.PNGetMembershipsOperation="PNGetMembershipsOperation",e.PNSetMembershipsOperation="PNSetMembershipsOperation",e.PNListFilesOperation="PNListFilesOperation",e.PNGenerateUploadUrlOperation="PNGenerateUploadUrlOperation",e.PNPublishFileOperation="PNPublishFileOperation",e.PNPublishFileMessageOperation="PNPublishFileMessageOperation",e.PNGetFileUrlOperation="PNGetFileUrlOperation",e.PNDownloadFileOperation="PNDownloadFileOperation",e.PNDeleteFileOperation="PNDeleteFileOperation",e.PNAddPushNotificationEnabledChannelsOperation="PNAddPushNotificationEnabledChannelsOperation",e.PNRemovePushNotificationEnabledChannelsOperation="PNRemovePushNotificationEnabledChannelsOperation",e.PNPushNotificationEnabledChannelsOperation="PNPushNotificationEnabledChannelsOperation",e.PNRemoveAllPushNotificationsOperation="PNRemoveAllPushNotificationsOperation",e.PNChannelGroupsOperation="PNChannelGroupsOperation",e.PNRemoveGroupOperation="PNRemoveGroupOperation",e.PNChannelsForGroupOperation="PNChannelsForGroupOperation",e.PNAddChannelsToGroupOperation="PNAddChannelsToGroupOperation",e.PNRemoveChannelsFromGroupOperation="PNRemoveChannelsFromGroupOperation",e.PNAccessManagerGrant="PNAccessManagerGrant",e.PNAccessManagerGrantToken="PNAccessManagerGrantToken",e.PNAccessManagerAudit="PNAccessManagerAudit",e.PNAccessManagerRevokeToken="PNAccessManagerRevokeToken",e.PNHandshakeOperation="PNHandshakeOperation",e.PNReceiveMessagesOperation="PNReceiveMessagesOperation"}(w||(w={}));var M=w;class A{constructor(e){this.configuration=e,this.subscriptionWorkerReady=!1,this.accessTokensMap={},this.workerEventsQueue=[],this.callbacks=new Map,this.setupSubscriptionWorker()}set emitStatus(e){this._emitStatus=e}onUserIdChange(e){this.configuration.userId=e,this.scheduleEventPost({type:"client-update",heartbeatInterval:this.configuration.heartbeatInterval,clientIdentifier:this.configuration.clientIdentifier,subscriptionKey:this.configuration.subscriptionKey,userId:this.configuration.userId,workerLogLevel:this.configuration.workerLogLevel})}onPresenceStateChange(e){this.scheduleEventPost({type:"client-presence-state-update",clientIdentifier:this.configuration.clientIdentifier,subscriptionKey:this.configuration.subscriptionKey,workerLogLevel:this.configuration.workerLogLevel,state:e})}onHeartbeatIntervalChange(e){this.configuration.heartbeatInterval=e,this.scheduleEventPost({type:"client-update",heartbeatInterval:this.configuration.heartbeatInterval,clientIdentifier:this.configuration.clientIdentifier,subscriptionKey:this.configuration.subscriptionKey,userId:this.configuration.userId,workerLogLevel:this.configuration.workerLogLevel})}onTokenChange(e){const t={type:"client-update",heartbeatInterval:this.configuration.heartbeatInterval,clientIdentifier:this.configuration.clientIdentifier,subscriptionKey:this.configuration.subscriptionKey,userId:this.configuration.userId,workerLogLevel:this.configuration.workerLogLevel};this.parsedAccessToken(e).then((s=>{t.preProcessedToken=s,t.accessToken=e})).then((()=>this.scheduleEventPost(t)))}disconnect(){this.scheduleEventPost({type:"client-disconnect",clientIdentifier:this.configuration.clientIdentifier,subscriptionKey:this.configuration.subscriptionKey,workerLogLevel:this.configuration.workerLogLevel})}terminate(){this.scheduleEventPost({type:"client-unregister",clientIdentifier:this.configuration.clientIdentifier,subscriptionKey:this.configuration.subscriptionKey,workerLogLevel:this.configuration.workerLogLevel})}makeSendable(e){if(!e.path.startsWith("/v2/subscribe")&&!e.path.endsWith("/heartbeat")&&!e.path.endsWith("/leave"))return this.configuration.transport.makeSendable(e);let t;this.configuration.logger.debug("SubscriptionWorkerMiddleware","Process request with SharedWorker transport.");const s={type:"send-request",clientIdentifier:this.configuration.clientIdentifier,subscriptionKey:this.configuration.subscriptionKey,request:e,workerLogLevel:this.configuration.workerLogLevel};return e.cancellable&&(t={abort:()=>{const t={type:"cancel-request",clientIdentifier:this.configuration.clientIdentifier,subscriptionKey:this.configuration.subscriptionKey,identifier:e.identifier,workerLogLevel:this.configuration.workerLogLevel};this.scheduleEventPost(t)}}),[new Promise(((t,n)=>{this.callbacks.set(e.identifier,{resolve:t,reject:n}),this.parsedAccessTokenForRequest(e).then((e=>s.preProcessedToken=e)).then((()=>this.scheduleEventPost(s)))})),t]}request(e){return e}scheduleEventPost(e,t=!1){const s=this.sharedSubscriptionWorker;s?s.port.postMessage(e):t?this.workerEventsQueue.splice(0,0,e):this.workerEventsQueue.push(e)}flushScheduledEvents(){const e=this.sharedSubscriptionWorker;if(!e||0===this.workerEventsQueue.length)return;const t=[];for(let e=0;e!t.includes(e))),this.workerEventsQueue.forEach((t=>e.port.postMessage(t))),this.workerEventsQueue=[]}get sharedSubscriptionWorker(){return this.subscriptionWorkerReady?this.subscriptionWorker:null}setupSubscriptionWorker(){if("undefined"!=typeof SharedWorker){try{this.subscriptionWorker=new SharedWorker(this.configuration.workerUrl,`/pubnub-${this.configuration.sdkVersion}`)}catch(e){throw this.configuration.logger.error("SubscriptionWorkerMiddleware",(()=>({messageType:"error",message:e}))),e}this.subscriptionWorker.port.start(),this.scheduleEventPost({type:"client-register",clientIdentifier:this.configuration.clientIdentifier,subscriptionKey:this.configuration.subscriptionKey,userId:this.configuration.userId,heartbeatInterval:this.configuration.heartbeatInterval,workerOfflineClientsCheckInterval:this.configuration.workerOfflineClientsCheckInterval,workerUnsubscribeOfflineClients:this.configuration.workerUnsubscribeOfflineClients,workerLogLevel:this.configuration.workerLogLevel},!0),this.subscriptionWorker.port.onmessage=e=>this.handleWorkerEvent(e),this.shouldAnnounceNewerSharedWorkerVersionAvailability()&&localStorage.setItem("PNSubscriptionSharedWorkerVersion",this.configuration.sdkVersion),window.addEventListener("storage",(e=>{"PNSubscriptionSharedWorkerVersion"===e.key&&e.newValue&&this._emitStatus&&this.isNewerSharedWorkerVersion(e.newValue)&&this._emitStatus({error:!1,category:h.PNSharedWorkerUpdatedCategory})}))}}handleWorkerEvent(e){const{data:t}=e;if("shared-worker-ping"===t.type||"shared-worker-connected"===t.type||"shared-worker-console-log"===t.type||"shared-worker-console-dir"===t.type||t.clientIdentifier===this.configuration.clientIdentifier)if("shared-worker-connected"===t.type)this.configuration.logger.trace("SharedWorker","Ready for events processing."),this.subscriptionWorkerReady=!0,this.flushScheduledEvents();else if("shared-worker-console-log"===t.type)this.configuration.logger.debug("SharedWorker",(()=>"string"==typeof t.message||"number"==typeof t.message||"boolean"==typeof t.message?{messageType:"text",message:t.message}:t.message));else if("shared-worker-console-dir"===t.type)this.configuration.logger.debug("SharedWorker",(()=>({messageType:"object",message:t.data,details:t.message?t.message:void 0})));else if("shared-worker-ping"===t.type){const{subscriptionKey:e,clientIdentifier:t}=this.configuration;this.scheduleEventPost({type:"client-pong",subscriptionKey:e,clientIdentifier:t,workerLogLevel:this.configuration.workerLogLevel})}else if("request-process-success"===t.type||"request-process-error"===t.type)if(this.callbacks.has(t.identifier)){const{resolve:e,reject:s}=this.callbacks.get(t.identifier);this.callbacks.delete(t.identifier),"request-process-success"===t.type?e({status:t.response.status,url:t.url,headers:t.response.headers,body:t.response.body}):s(this.errorFromRequestSendingError(t))}else this._emitStatus&&t.url.indexOf("/v2/presence")>=0&&t.url.indexOf("/heartbeat")>=0&&("request-process-success"===t.type&&this.configuration.announceSuccessfulHeartbeats?this._emitStatus({statusCode:t.response.status,error:!1,operation:M.PNHeartbeatOperation,category:h.PNAcknowledgmentCategory}):"request-process-error"===t.type&&this.configuration.announceFailedHeartbeats&&this._emitStatus(this.errorFromRequestSendingError(t).toStatus(M.PNHeartbeatOperation)))}parsedAccessTokenForRequest(e){return i(this,void 0,void 0,(function*(){var t;return this.parsedAccessToken(e.queryParameters?null!==(t=e.queryParameters.auth)&&void 0!==t?t:"":void 0)}))}parsedAccessToken(e){return i(this,void 0,void 0,(function*(){if(e)return this.accessTokensMap[e]?this.accessTokensMap[e]:this.stringifyAccessToken(e).then((([t,s])=>{if(t&&s)return(this.accessTokensMap={[e]:{token:s,expiration:t.timestamp+60*t.ttl}})[e]}))}))}stringifyAccessToken(e){return i(this,void 0,void 0,(function*(){if(!this.configuration.tokenManager)return[void 0,void 0];const t=this.configuration.tokenManager.parseToken(e);if(!t)return[void 0,void 0];const s=e=>e?Object.entries(e).sort((([e],[t])=>e.localeCompare(t))).map((([e,t])=>Object.entries(t||{}).sort((([e],[t])=>e.localeCompare(t))).map((([t,s])=>{return`${e}:${t}=${s?(n=s,Object.entries(n).filter((([e,t])=>t)).map((([e])=>e[0])).sort().join("")):""}`;var n})).join(","))).join(";"):"";let n=[s(t.resources),s(t.patterns),t.authorized_uuid].filter(Boolean).join("|");if("undefined"!=typeof crypto&&crypto.subtle){const e=yield crypto.subtle.digest("SHA-256",(new TextEncoder).encode(n));n=String.fromCharCode(...Array.from(new Uint8Array(e)))}return[t,"undefined"!=typeof btoa?btoa(n):n]}))}errorFromRequestSendingError(e){let t=h.PNUnknownCategory,s="Unknown error";if(e.error)"NETWORK_ISSUE"===e.error.type?t=h.PNNetworkIssuesCategory:"TIMEOUT"===e.error.type?t=h.PNTimeoutCategory:"ABORTED"===e.error.type&&(t=h.PNCancelledCategory),s=`${e.error.message} (${e.identifier})`;else if(e.response){const{url:t,response:s}=e;return I.create({url:t,headers:s.headers,body:s.body,status:s.status},s.body)}return new I(s,t,0,new Error(s))}shouldAnnounceNewerSharedWorkerVersionAvailability(){const e=localStorage.getItem("PNSubscriptionSharedWorkerVersion");return!e||!this.isNewerSharedWorkerVersion(e)}isNewerSharedWorkerVersion(e){const[t,s,n]=this.configuration.sdkVersion.split(".").map(Number),[r,i,a]=e.split(".").map(Number);return r>t||i>s||a>n}}function U(e,t=0){const s=e=>"object"==typeof e&&null!==e&&e.constructor===Object,n=e=>"number"==typeof e&&isFinite(e);if(!s(e))return e;const r={};return Object.keys(e).forEach((i=>{const a=(e=>"string"==typeof e||e instanceof String)(i);let o=i;const c=e[i];if(t<2)if(a&&i.indexOf(",")>=0){o=i.split(",").map(Number).reduce(((e,t)=>e+String.fromCharCode(t)),"")}else(n(i)||a&&!isNaN(Number(i)))&&(o=String.fromCharCode(n(i)?i:parseInt(i,10)));r[o]=s(c)?U(c,t+1):c})),r}const D=e=>{var t,s,n,r,i,a;return e.subscriptionWorkerUrl&&"undefined"==typeof SharedWorker&&(e.subscriptionWorkerUrl=null),Object.assign(Object.assign({},(e=>{var t,s,n,r,i,a,o,c,u,l,h,p,g,b,y;const m=Object.assign({},e);if(null!==(t=m.ssl)&&void 0!==t||(m.ssl=!0),null!==(s=m.transactionalRequestTimeout)&&void 0!==s||(m.transactionalRequestTimeout=15),null!==(n=m.subscribeRequestTimeout)&&void 0!==n||(m.subscribeRequestTimeout=310),null!==(r=m.fileRequestTimeout)&&void 0!==r||(m.fileRequestTimeout=300),null!==(i=m.restore)&&void 0!==i||(m.restore=!1),null!==(a=m.useInstanceId)&&void 0!==a||(m.useInstanceId=!1),null!==(o=m.suppressLeaveEvents)&&void 0!==o||(m.suppressLeaveEvents=!1),null!==(c=m.requestMessageCountThreshold)&&void 0!==c||(m.requestMessageCountThreshold=100),null!==(u=m.autoNetworkDetection)&&void 0!==u||(m.autoNetworkDetection=!1),null!==(l=m.enableEventEngine)&&void 0!==l||(m.enableEventEngine=!1),null!==(h=m.maintainPresenceState)&&void 0!==h||(m.maintainPresenceState=!0),null!==(p=m.useSmartHeartbeat)&&void 0!==p||(m.useSmartHeartbeat=!1),null!==(g=m.keepAlive)&&void 0!==g||(m.keepAlive=!1),m.userId&&m.uuid)throw new d("PubNub client configuration error: use only 'userId'");if(null!==(b=m.userId)&&void 0!==b||(m.userId=m.uuid),!m.userId)throw new d("PubNub client configuration error: 'userId' not set");if(0===(null===(y=m.userId)||void 0===y?void 0:y.trim().length))throw new d("PubNub client configuration error: 'userId' is empty");m.origin||(m.origin=Array.from({length:20},((e,t)=>`ps${t+1}.pndsn.com`)));const f={subscribeKey:m.subscribeKey,publishKey:m.publishKey,secretKey:m.secretKey};void 0!==m.presenceTimeout&&(m.presenceTimeout>320?(m.presenceTimeout=320,console.warn("WARNING: Presence timeout is larger than the maximum. Using maximum value: ",320)):m.presenceTimeout<=0&&(console.warn("WARNING: Presence timeout should be larger than zero."),delete m.presenceTimeout)),void 0!==m.presenceTimeout?m.heartbeatInterval=m.presenceTimeout/2-1:m.presenceTimeout=300;let v=!1,S=!0,w=5,O=!1,k=100,C=!0;return void 0!==m.dedupeOnSubscribe&&"boolean"==typeof m.dedupeOnSubscribe&&(O=m.dedupeOnSubscribe),void 0!==m.maximumCacheSize&&"number"==typeof m.maximumCacheSize&&(k=m.maximumCacheSize),void 0!==m.useRequestId&&"boolean"==typeof m.useRequestId&&(C=m.useRequestId),void 0!==m.announceSuccessfulHeartbeats&&"boolean"==typeof m.announceSuccessfulHeartbeats&&(v=m.announceSuccessfulHeartbeats),void 0!==m.announceFailedHeartbeats&&"boolean"==typeof m.announceFailedHeartbeats&&(S=m.announceFailedHeartbeats),void 0!==m.fileUploadPublishRetryLimit&&"number"==typeof m.fileUploadPublishRetryLimit&&(w=m.fileUploadPublishRetryLimit),Object.assign(Object.assign({},m),{keySet:f,dedupeOnSubscribe:O,maximumCacheSize:k,useRequestId:C,announceSuccessfulHeartbeats:v,announceFailedHeartbeats:S,fileUploadPublishRetryLimit:w})})(e)),{listenToBrowserNetworkEvents:null===(t=e.listenToBrowserNetworkEvents)||void 0===t||t,subscriptionWorkerUrl:e.subscriptionWorkerUrl,subscriptionWorkerOfflineClientsCheckInterval:null!==(s=e.subscriptionWorkerOfflineClientsCheckInterval)&&void 0!==s?s:10,subscriptionWorkerUnsubscribeOfflineClients:null!==(n=e.subscriptionWorkerUnsubscribeOfflineClients)&&void 0!==n&&n,subscriptionWorkerLogVerbosity:null!==(r=e.subscriptionWorkerLogVerbosity)&&void 0!==r&&r,transport:null!==(i=e.transport)&&void 0!==i?i:"fetch",keepAlive:null===(a=e.keepAlive)||void 0===a||a})};var F;!function(e){e[e.Trace=0]="Trace",e[e.Debug=1]="Debug",e[e.Info=2]="Info",e[e.Warn=3]="Warn",e[e.Error=4]="Error",e[e.None=5]="None"}(F||(F={}));const R=e=>encodeURIComponent(e).replace(/[!~*'()]/g,(e=>`%${e.charCodeAt(0).toString(16).toUpperCase()}`)),$=(e,t)=>{const s=e.map((e=>R(e)));return s.length?s.join(","):null!=t?t:""},x=(e,t)=>{const s=Object.fromEntries(t.map((e=>[e,!1])));return e.filter((e=>!(t.includes(e)&&!s[e])||(s[e]=!0,!1)))},L=(e,t)=>[...e].filter((s=>t.includes(s)&&e.indexOf(s)===e.lastIndexOf(s)&&t.indexOf(s)===t.lastIndexOf(s))),q=e=>Object.keys(e).map((t=>{const s=e[t];return Array.isArray(s)?s.map((e=>`${t}=${R(e)}`)).join("&"):`${t}=${R(s)}`})).join("&"),G=(e,t)=>{if("0"===t||"0"===e)return;const s=H(`${Date.now()}0000`,t,!1);return H(e,s,!0)},K=(e,t,s)=>{if(e&&0!==e.length){if(t&&t.length>0&&"0"!==t){const n=H(e,t,!1);return H(null!=s?s:`${Date.now()}0000`,n.replace("-",""),Number(n)<0)}return s&&s.length>0&&"0"!==s?s:`${Date.now()}0000`}},H=(e,t,s)=>{t.startsWith("-")&&(t=t.replace("-",""),s=!1),t=t.padStart(17,"0");const n=e.slice(0,10),r=e.slice(10,17),i=t.slice(0,10),a=t.slice(10,17);let o=Number(n),c=Number(r);return o+=Number(i)*(s?1:-1),c+=Number(a)*(s?1:-1),c>=1e7?(o+=Math.floor(c/1e7),c%=1e7):c<0?o>0?(o-=1,c+=1e7):o<0&&(c*=-1):o<0&&c>0&&(o+=1,c=1e7-c),0!==o?`${o}${`${c}`.padStart(7,"0")}`:`${c}`},B=e=>{const t="string"!=typeof e?JSON.stringify(e):e,s=new Uint32Array(1);let n=0,r=t.length;for(;r-- >0;)s[0]=(s[0]<<5)-s[0]+t.charCodeAt(n++);return s[0].toString(16).padStart(8,"0")};class W{debug(e){this.log(e)}error(e){this.log(e)}info(e){this.log(e)}trace(e){this.log(e)}warn(e){this.log(e)}toString(){return"ConsoleLogger {}"}log(e){const t=F[e.level],s=t.toLowerCase();console["trace"===s?"debug":s](`${e.timestamp.toISOString()} PubNub-${e.pubNubId} ${t.padEnd(5," ")}${e.location?` ${e.location}`:""} ${this.logMessage(e)}`)}logMessage(e){if("text"===e.messageType)return e.message;if("object"===e.messageType)return`${e.details?`${e.details}\n`:""}${this.formattedObject(e)}`;if("network-request"===e.messageType){const t=!!e.canceled||!!e.failed,s=e.minimumLevel!==F.Trace||t?void 0:this.formattedHeaders(e),n=e.message,r=n.queryParameters&&Object.keys(n.queryParameters).length>0?q(n.queryParameters):void 0,i=`${n.origin}${n.path}${r?`?${r}`:""}`,a=t?void 0:this.formattedBody(e);let o="Sending";t&&(o=`${e.canceled?"Canceled":"Failed"}${e.details?` (${e.details})`:""}`);const c=((null==a?void 0:a.formData)?"FormData":"Method").length;return`${o} HTTP request:\n ${this.paddedString("Method",c)}: ${n.method}\n ${this.paddedString("URL",c)}: ${i}${s?`\n ${this.paddedString("Headers",c)}:\n${s}`:""}${(null==a?void 0:a.formData)?`\n ${this.paddedString("FormData",c)}:\n${a.formData}`:""}${(null==a?void 0:a.body)?`\n ${this.paddedString("Body",c)}:\n${a.body}`:""}`}if("network-response"===e.messageType){const t=e.minimumLevel===F.Trace?this.formattedHeaders(e):void 0,s=this.formattedBody(e),n=((null==s?void 0:s.formData)?"Headers":"Status").length,r=e.message;return`Received HTTP response:\n ${this.paddedString("URL",n)}: ${r.url}\n ${this.paddedString("Status",n)}: ${r.status}${t?`\n ${this.paddedString("Headers",n)}:\n${t}`:""}${(null==s?void 0:s.body)?`\n ${this.paddedString("Body",n)}:\n${s.body}`:""}`}if("error"===e.messageType){const t=this.formattedErrorStatus(e),s=e.message;return`${s.name}: ${s.message}${t?`\n${t}`:""}`}return""}formattedObject(e){const t=(s,n=1,r=!1)=>{const i=10===n,a=" ".repeat(2*n),o=[],c=(t,s)=>!!e.ignoredKeys&&("function"==typeof e.ignoredKeys?e.ignoredKeys(t,s):e.ignoredKeys.includes(t));if("string"==typeof s)o.push(`${a}- ${s}`);else if("number"==typeof s)o.push(`${a}- ${s}`);else if("boolean"==typeof s)o.push(`${a}- ${s}`);else if(null===s)o.push(`${a}- null`);else if(void 0===s)o.push(`${a}- undefined`);else if("function"==typeof s)o.push(`${a}- `);else if("object"==typeof s)if(Array.isArray(s)||"function"!=typeof s.toString||0===s.toString().indexOf("[object"))if(Array.isArray(s))for(const e of s){const s=r?"":a;if(null===e)o.push(`${s}- null`);else if(void 0===e)o.push(`${s}- undefined`);else if("function"==typeof e)o.push(`${s}- `);else if("object"==typeof e){const r=Array.isArray(e),a=i?"...":t(e,n+1,!r);o.push(`${s}-${r&&!i?"\n":" "}${a}`)}else o.push(`${s}- ${e}`);r=!1}else{const e=s,u=Object.keys(e),l=u.reduce(((t,s)=>Math.max(t,c(s,e)?t:s.length)),0);for(const s of u){if(c(s,e))continue;const u=r?"":a,h=e[s],d=s.padEnd(l," ");if(null===h)o.push(`${u}${d}: null`);else if(void 0===h)o.push(`${u}${d}: undefined`);else if("function"==typeof h)o.push(`${u}${d}: `);else if("object"==typeof h){const e=Array.isArray(h),s=e&&0===h.length,r=!(e||h instanceof String||0!==Object.keys(h).length),a=!e&&"function"==typeof h.toString&&0!==h.toString().indexOf("[object"),c=i?"...":s?"[]":r?"{}":t(h,n+1,a);o.push(`${u}${d}:${i||a||s||r?" ":"\n"}${c}`)}else o.push(`${u}${d}: ${h}`);r=!1}}else o.push(`${r?"":a}${s.toString()}`),r=!1;return o.join("\n")};return t(e.message)}formattedHeaders(e){if(!e.message.headers)return;const t=e.message.headers,s=Object.keys(t).reduce(((e,t)=>Math.max(e,t.length)),0);return Object.keys(t).map((e=>` - ${e.toLowerCase().padEnd(s," ")}: ${t[e]}`)).join("\n")}formattedBody(e){var t;if(!e.message.headers)return;let s,n;const r=e.message.headers,i=null!==(t=r["content-type"])&&void 0!==t?t:r["Content-Type"],a="formData"in e.message?e.message.formData:void 0,o=e.message.body;if(a){const e=a.reduce(((e,{key:t})=>Math.max(e,t.length)),0);s=a.map((({key:t,value:s})=>` - ${t.padEnd(e," ")}: ${s}`)).join("\n")}return o?(n="string"==typeof o?` ${o}`:o instanceof ArrayBuffer||"[object ArrayBuffer]"===Object.prototype.toString.call(o)?!i||-1===i.indexOf("javascript")&&-1===i.indexOf("json")?` ArrayBuffer { byteLength: ${o.byteLength} }`:` ${W.decoder.decode(o)}`:` File { name: ${o.name}${o.contentLength?`, contentLength: ${o.contentLength}`:""}${o.mimeType?`, mimeType: ${o.mimeType}`:""} }`,{body:n,formData:s}):{formData:s}}formattedErrorStatus(e){if(!e.message.status)return;const t=e.message.status,s=t.errorData;let n;if(W.isError(s))n=` ${s.name}: ${s.message}`,s.stack&&(n+=`\n${s.stack.split("\n").map((e=>` ${e}`)).join("\n")}`);else if(s)try{n=` ${JSON.stringify(s)}`}catch(e){n=` ${s}`}return` Category : ${t.category}\n Operation : ${t.operation}\n Status : ${t.statusCode}${n?`\n Error data:\n${n}`:""}`}paddedString(e,t){return e.padEnd(t-e.length," ")}static isError(e){return!!e&&(e instanceof Error||"[object Error]"===Object.prototype.toString.call(e))}}var z;W.decoder=new TextDecoder,function(e){e.Unknown="UnknownEndpoint",e.MessageSend="MessageSendEndpoint",e.Subscribe="SubscribeEndpoint",e.Presence="PresenceEndpoint",e.Files="FilesEndpoint",e.MessageStorage="MessageStorageEndpoint",e.ChannelGroups="ChannelGroupsEndpoint",e.DevicePushNotifications="DevicePushNotificationsEndpoint",e.AppContext="AppContextEndpoint",e.MessageReactions="MessageReactionsEndpoint"}(z||(z={}));class V{static None(){return{shouldRetry:(e,t,s,n)=>!1,getDelay:(e,t)=>-1,validate:()=>!0}}static LinearRetryPolicy(e){var t;return{delay:e.delay,maximumRetry:e.maximumRetry,excluded:null!==(t=e.excluded)&&void 0!==t?t:[],shouldRetry(e,t,s,n){return J(e,t,s,null!=n?n:0,this.maximumRetry,this.excluded)},getDelay(e,t){let s=-1;return t&&void 0!==t.headers["retry-after"]&&(s=parseInt(t.headers["retry-after"],10)),-1===s&&(s=this.delay),1e3*(s+Math.random())},validate(){if(this.delay<2)throw new Error("Delay can not be set less than 2 seconds for retry");if(this.maximumRetry>10)throw new Error("Maximum retry for linear retry policy can not be more than 10")}}}static ExponentialRetryPolicy(e){var t;return{minimumDelay:e.minimumDelay,maximumDelay:e.maximumDelay,maximumRetry:e.maximumRetry,excluded:null!==(t=e.excluded)&&void 0!==t?t:[],shouldRetry(e,t,s,n){return J(e,t,s,null!=n?n:0,this.maximumRetry,this.excluded)},getDelay(e,t){let s=-1;return t&&void 0!==t.headers["retry-after"]&&(s=parseInt(t.headers["retry-after"],10)),-1===s&&(s=Math.min(Math.pow(2,e),this.maximumDelay)),1e3*(s+Math.random())},validate(){if(this.minimumDelay<2)throw new Error("Minimum delay can not be set less than 2 seconds for retry");if(this.maximumDelay>150)throw new Error("Maximum delay can not be set more than 150 seconds for retry");if(this.maximumRetry>6)throw new Error("Maximum retry for exponential retry policy can not be more than 6")}}}}const J=(e,t,s,n,r,i)=>(!s||s!==h.PNCancelledCategory&&s!==h.PNBadRequestCategory&&s!==h.PNAccessDeniedCategory)&&(!X(e,i)&&(!(n>r)&&(!t||(429===t.status||t.status>=500)))),X=(e,t)=>!!(t&&t.length>0)&&t.includes(Q(e)),Q=e=>{let t=z.Unknown;return e.path.startsWith("/v2/subscribe")?t=z.Subscribe:e.path.startsWith("/publish/")||e.path.startsWith("/signal/")?t=z.MessageSend:e.path.startsWith("/v2/presence")?t=z.Presence:e.path.startsWith("/v2/history")||e.path.startsWith("/v3/history")?t=z.MessageStorage:e.path.startsWith("/v1/message-actions/")?t=z.MessageReactions:e.path.startsWith("/v1/channel-registration/")||e.path.startsWith("/v2/objects/")?t=z.ChannelGroups:e.path.startsWith("/v1/push/")||e.path.startsWith("/v2/push/")?t=z.DevicePushNotifications:e.path.startsWith("/v1/files/")&&(t=z.Files),t};class Y{constructor(e,t,s){this.previousEntryTimestamp=0,this.pubNubId=e,this.minLogLevel=t,this.loggers=s}get logLevel(){return this.minLogLevel}trace(e,t){this.log(F.Trace,e,t)}debug(e,t){this.log(F.Debug,e,t)}info(e,t){this.log(F.Info,e,t)}warn(e,t){this.log(F.Warn,e,t)}error(e,t){this.log(F.Error,e,t)}log(e,t,s){if(ee[r](i)))}}var Z={exports:{}}; -/*! lil-uuid - v0.1 - MIT License - https://github.com/lil-js/uuid */!function(e,t){!function(e){var t="0.1.0",s={3:/^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i,4:/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,5:/^[0-9A-F]{8}-[0-9A-F]{4}-5[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,all:/^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i};function n(){var e,t,s="";for(e=0;e<32;e++)t=16*Math.random()|0,8!==e&&12!==e&&16!==e&&20!==e||(s+="-"),s+=(12===e?4:16===e?3&t|8:t).toString(16);return s}function r(e,t){var n=s[t||"all"];return n&&n.test(e)||!1}n.isUUID=r,n.VERSION=t,e.uuid=n,e.isUUID=r}(t),null!==e&&(e.exports=t.uuid)}(Z,Z.exports);var ee=t(Z.exports),te={createUUID:()=>ee.uuid?ee.uuid():ee()};const se=(e,t)=>{var s,n,r,i;!e.retryConfiguration&&e.enableEventEngine&&(e.retryConfiguration=V.ExponentialRetryPolicy({minimumDelay:2,maximumDelay:150,maximumRetry:6,excluded:[z.MessageSend,z.Presence,z.Files,z.MessageStorage,z.ChannelGroups,z.DevicePushNotifications,z.AppContext,z.MessageReactions]}));const a=`pn-${te.createUUID()}`;e.logVerbosity?e.logLevel=F.Debug:void 0===e.logLevel&&(e.logLevel=F.None);const o=new Y(re(a),e.logLevel,[...null!==(s=e.loggers)&&void 0!==s?s:[],new W]);void 0!==e.logVerbosity&&o.warn("Configuration","'logVerbosity' is deprecated. Use 'logLevel' instead."),null===(n=e.retryConfiguration)||void 0===n||n.validate(),null!==(r=e.useRandomIVs)&&void 0!==r||(e.useRandomIVs=true),e.useRandomIVs&&o.warn("Configuration","'useRandomIVs' is deprecated. Use 'cryptoModule' instead."),e.origin=ne(null!==(i=e.ssl)&&void 0!==i&&i,e.origin);const c=e.cryptoModule;c&&delete e.cryptoModule;const u=Object.assign(Object.assign({},e),{_pnsdkSuffix:{},_loggerManager:o,_instanceId:a,_cryptoModule:void 0,_cipherKey:void 0,_setupCryptoModule:t,get instanceId(){if(e.useInstanceId)return this._instanceId},getInstanceId(){if(e.useInstanceId)return this._instanceId},getUserId(){return this.userId},setUserId(e){if(!e||"string"!=typeof e||0===e.trim().length)throw new Error("Missing or invalid userId parameter. Provide a valid string userId");this.userId=e},logger(){return this._loggerManager},getAuthKey(){return this.authKey},setAuthKey(e){this.authKey=e},getFilterExpression(){return this.filterExpression},setFilterExpression(e){this.filterExpression=e},getCipherKey(){return this._cipherKey},setCipherKey(t){this._cipherKey=t,t||!this._cryptoModule?t&&this._setupCryptoModule&&(this._cryptoModule=this._setupCryptoModule({cipherKey:t,useRandomIVs:e.useRandomIVs,customEncrypt:this.getCustomEncrypt(),customDecrypt:this.getCustomDecrypt(),logger:this.logger()})):this._cryptoModule=void 0},getCryptoModule(){return this._cryptoModule},getUseRandomIVs:()=>e.useRandomIVs,isSharedWorkerEnabled:()=>"Web"===e.sdkFamily&&e.subscriptionWorkerUrl,getKeepPresenceChannelsInPresenceRequests:()=>"Web"===e.sdkFamily&&e.subscriptionWorkerUrl,setPresenceTimeout(e){this.heartbeatInterval=e/2-1,this.presenceTimeout=e},getPresenceTimeout(){return this.presenceTimeout},getHeartbeatInterval(){return this.heartbeatInterval},setHeartbeatInterval(e){this.heartbeatInterval=e},getTransactionTimeout(){return this.transactionalRequestTimeout},getSubscribeTimeout(){return this.subscribeRequestTimeout},getFileTimeout(){return this.fileRequestTimeout},get PubNubFile(){return e.PubNubFile},get version(){return"9.10.0"},getVersion(){return this.version},_addPnsdkSuffix(e,t){this._pnsdkSuffix[e]=`${t}`},_getPnsdkSuffix(e){const t=Object.values(this._pnsdkSuffix).join(e);return t.length>0?e+t:""},getUUID(){return this.getUserId()},setUUID(e){this.setUserId(e)},getCustomEncrypt:()=>e.customEncrypt,getCustomDecrypt:()=>e.customDecrypt});return e.cipherKey?(o.warn("Configuration","'cipherKey' is deprecated. Use 'cryptoModule' instead."),u.setCipherKey(e.cipherKey)):c&&(u._cryptoModule=c),u},ne=(e,t)=>{const s=e?"https://":"http://";return"string"==typeof t?`${s}${t}`:`${s}${t[Math.floor(Math.random()*t.length)]}`},re=e=>{let t=2166136261;for(let s=0;s>>0;return t.toString(16).padStart(8,"0")};class ie{constructor(e){this.cbor=e}setToken(e){e&&e.length>0?this.token=e:this.token=void 0}getToken(){return this.token}parseToken(e){const t=this.cbor.decodeToken(e);if(void 0!==t){const e=t.res.uuid?Object.keys(t.res.uuid):[],s=Object.keys(t.res.chan),n=Object.keys(t.res.grp),r=t.pat.uuid?Object.keys(t.pat.uuid):[],i=Object.keys(t.pat.chan),a=Object.keys(t.pat.grp),o={version:t.v,timestamp:t.t,ttl:t.ttl,authorized_uuid:t.uuid,signature:t.sig},c=e.length>0,u=s.length>0,l=n.length>0;if(c||u||l){if(o.resources={},c){const s=o.resources.uuids={};e.forEach((e=>s[e]=this.extractPermissions(t.res.uuid[e])))}if(u){const e=o.resources.channels={};s.forEach((s=>e[s]=this.extractPermissions(t.res.chan[s])))}if(l){const e=o.resources.groups={};n.forEach((s=>e[s]=this.extractPermissions(t.res.grp[s])))}}const h=r.length>0,d=i.length>0,p=a.length>0;if(h||d||p){if(o.patterns={},h){const e=o.patterns.uuids={};r.forEach((s=>e[s]=this.extractPermissions(t.pat.uuid[s])))}if(d){const e=o.patterns.channels={};i.forEach((s=>e[s]=this.extractPermissions(t.pat.chan[s])))}if(p){const e=o.patterns.groups={};a.forEach((s=>e[s]=this.extractPermissions(t.pat.grp[s])))}}return t.meta&&Object.keys(t.meta).length>0&&(o.meta=t.meta),o}}extractPermissions(e){const t={read:!1,write:!1,manage:!1,delete:!1,get:!1,update:!1,join:!1};return 128&~e||(t.join=!0),64&~e||(t.update=!0),32&~e||(t.get=!0),8&~e||(t.delete=!0),4&~e||(t.manage=!0),2&~e||(t.write=!0),1&~e||(t.read=!0),t}}var ae;!function(e){e.GET="GET",e.POST="POST",e.PATCH="PATCH",e.DELETE="DELETE",e.LOCAL="LOCAL"}(ae||(ae={}));class oe{constructor(e,t,s,n){this.publishKey=e,this.secretKey=t,this.hasher=s,this.logger=n}signature(e){const t=e.path.startsWith("/publish")?ae.GET:e.method;let s=`${t}\n${this.publishKey}\n${e.path}\n${this.queryParameters(e.queryParameters)}\n`;if(t===ae.POST||t===ae.PATCH){const t=e.body;let n;t&&t instanceof ArrayBuffer?n=oe.textDecoder.decode(t):t&&"object"!=typeof t&&(n=t),n&&(s+=n)}return this.logger.trace("RequestSignature",(()=>({messageType:"text",message:`Request signature input:\n${s}`}))),`v2.${this.hasher(s,this.secretKey)}`.replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")}queryParameters(e){return Object.keys(e).sort().map((t=>{const s=e[t];return Array.isArray(s)?s.sort().map((e=>`${t}=${R(e)}`)).join("&"):`${t}=${R(s)}`})).join("&")}}oe.textDecoder=new TextDecoder("utf-8");class ce{constructor(e){this.configuration=e;const{clientConfiguration:{keySet:t},shaHMAC:s}=e;t.secretKey&&s&&(this.signatureGenerator=new oe(t.publishKey,t.secretKey,s,this.logger))}get logger(){return this.configuration.clientConfiguration.logger()}makeSendable(e){const t=this.configuration.clientConfiguration.retryConfiguration,s=this.configuration.transport;if(void 0!==t){let n,r,i=!1,a=0;const o={abort:e=>{i=!0,n&&clearTimeout(n),r&&r.abort(e)}};return[new Promise(((o,c)=>{const u=()=>{if(i)return;const[l,d]=s.makeSendable(this.request(e));r=d;const p=(s,r)=>{const i=!r||r.category!==h.PNCancelledCategory,l=!s||s.status>=400;let d=-1;i&&l&&t.shouldRetry(e,s,null==r?void 0:r.category,a+1)&&(d=t.getDelay(a,s)),d>0?(a++,this.logger.warn("PubNubMiddleware",`HTTP request retry #${a} in ${d}ms.`),n=setTimeout((()=>u()),d)):s?o(s):r&&c(r)};l.then((e=>p(e))).catch((e=>p(void 0,e)))};u()})),r?o:void 0]}return s.makeSendable(this.request(e))}request(e){var t;const{clientConfiguration:s}=this.configuration;return(e=this.configuration.transport.request(e)).queryParameters||(e.queryParameters={}),s.useInstanceId&&(e.queryParameters.instanceid=s.getInstanceId()),e.queryParameters.uuid||(e.queryParameters.uuid=s.userId),s.useRequestId&&(e.queryParameters.requestid=e.identifier),e.queryParameters.pnsdk=this.generatePNSDK(),null!==(t=e.origin)&&void 0!==t||(e.origin=s.origin),this.authenticateRequest(e),this.signRequest(e),e}authenticateRequest(e){var t;if(e.path.startsWith("/v2/auth/")||e.path.startsWith("/v3/pam/")||e.path.startsWith("/time"))return;const{clientConfiguration:s,tokenManager:n}=this.configuration,r=null!==(t=n&&n.getToken())&&void 0!==t?t:s.authKey;r&&(e.queryParameters.auth=r)}signRequest(e){this.signatureGenerator&&!e.path.startsWith("/time")&&(e.queryParameters.timestamp=String(Math.floor((new Date).getTime()/1e3)),e.queryParameters.signature=this.signatureGenerator.signature(e))}generatePNSDK(){const{clientConfiguration:e}=this.configuration;if(e.sdkName)return e.sdkName;let t=`PubNub-JS-${e.sdkFamily}`;e.partnerId&&(t+=`-${e.partnerId}`),t+=`/${e.getVersion()}`;const s=e._getPnsdkSuffix(" ");return s.length>0&&(t+=s),t}}class ue{constructor(e,t="fetch"){this.logger=e,this.transport=t,e.debug("WebTransport",`Create with configuration:\n - transport: ${t}`),"fetch"!==t||window&&window.fetch||(e.warn("WebTransport",`'${t}' not supported in this browser. Fallback to the 'xhr' transport.`),this.transport="xhr"),"fetch"===this.transport&&(ue.originalFetch=fetch.bind(window),this.isFetchMonkeyPatched()&&(ue.originalFetch=ue.getOriginalFetch(),e.warn("WebTransport","Native Web Fetch API 'fetch' function monkey patched."),this.isFetchMonkeyPatched(ue.originalFetch)?e.warn("WebTransport","Unable receive native Web Fetch API. There can be issues with subscribe long-poll cancellation"):e.info("WebTransport","Use native Web Fetch API 'fetch' implementation from iframe as APM workaround.")))}makeSendable(e){const t=new AbortController,s={abortController:t,abort:e=>{t.signal.aborted||(this.logger.trace("WebTransport",`On-demand request aborting: ${e}`),t.abort(e))}};return[this.webTransportRequestFromTransportRequest(e).then((t=>(this.logger.debug("WebTransport",(()=>({messageType:"network-request",message:e}))),this.sendRequest(t,s).then((e=>e.arrayBuffer().then((t=>[e,t])))).then((e=>{const s=e[1].byteLength>0?e[1]:void 0,{status:n,headers:r}=e[0],i={};r.forEach(((e,t)=>i[t]=e.toLowerCase()));const a={status:n,url:t.url,headers:i,body:s};if(this.logger.debug("WebTransport",(()=>({messageType:"network-response",message:a}))),n>=400)throw I.create(a);return a})).catch((t=>{const s=("string"==typeof t?t:t.message).toLowerCase();let n="string"==typeof t?new Error(t):t;throw s.includes("timeout")?this.logger.warn("WebTransport",(()=>({messageType:"network-request",message:e,details:"Timeout",canceled:!0}))):s.includes("cancel")||s.includes("abort")?(this.logger.debug("WebTransport",(()=>({messageType:"network-request",message:e,details:"Aborted",canceled:!0}))),n=new Error("Aborted"),n.name="AbortError"):s.includes("network")?this.logger.warn("WebTransport",(()=>({messageType:"network-request",message:e,details:"Network error",failed:!0}))):this.logger.warn("WebTransport",(()=>({messageType:"network-request",message:e,details:I.create(n).message,failed:!0}))),I.create(n)}))))),s]}request(e){return e}sendRequest(e,t){return i(this,void 0,void 0,(function*(){return"fetch"===this.transport?this.sendFetchRequest(e,t):this.sendXHRRequest(e,t)}))}sendFetchRequest(e,t){return i(this,void 0,void 0,(function*(){let s;const n=new Promise(((n,r)=>{s=setTimeout((()=>{clearTimeout(s),r(new Error("Request timeout")),t.abort("Cancel because of timeout")}),1e3*e.timeout)})),r=new Request(e.url,{method:e.method,headers:e.headers,redirect:"follow",body:e.body});return Promise.race([ue.originalFetch(r,{signal:t.abortController.signal,credentials:"omit",cache:"no-cache"}).then((e=>(s&&clearTimeout(s),e))),n])}))}sendXHRRequest(e,t){return i(this,void 0,void 0,(function*(){return new Promise(((s,n)=>{var r;const i=new XMLHttpRequest;i.open(e.method,e.url,!0);let a=!1;i.responseType="arraybuffer",i.timeout=1e3*e.timeout,t.abortController.signal.onabort=()=>{i.readyState!=XMLHttpRequest.DONE&&i.readyState!=XMLHttpRequest.UNSENT&&(a=!0,i.abort())},Object.entries(null!==(r=e.headers)&&void 0!==r?r:{}).forEach((([e,t])=>i.setRequestHeader(e,t))),i.onabort=()=>{n(new Error("Aborted"))},i.ontimeout=()=>{n(new Error("Request timeout"))},i.onerror=()=>{if(!a){const t=this.transportResponseFromXHR(e.url,i);n(new Error(I.create(t).message))}},i.onload=()=>{const e=new Headers;i.getAllResponseHeaders().split("\r\n").forEach((t=>{const[s,n]=t.split(": ");s.length>1&&n.length>1&&e.append(s,n)})),s(new Response(i.response,{status:i.status,headers:e,statusText:i.statusText}))},i.send(e.body)}))}))}webTransportRequestFromTransportRequest(e){return i(this,void 0,void 0,(function*(){let t,s=e.path;if(e.formData&&e.formData.length>0){e.queryParameters={};const s=e.body,n=new FormData;for(const{key:t,value:s}of e.formData)n.append(t,s);try{const e=yield s.toArrayBuffer();n.append("file",new Blob([e],{type:"application/octet-stream"}),s.name)}catch(e){this.logger.warn("WebTransport",(()=>({messageType:"error",message:e})));try{const e=yield s.toFileUri();n.append("file",e,s.name)}catch(e){this.logger.error("WebTransport",(()=>({messageType:"error",message:e})))}}t=n}else if(e.body&&("string"==typeof e.body||e.body instanceof ArrayBuffer))if(e.compressible&&"undefined"!=typeof CompressionStream){const s="string"==typeof e.body?ue.encoder.encode(e.body):e.body,n=s.byteLength,r=new ReadableStream({start(e){e.enqueue(s),e.close()}});t=yield new Response(r.pipeThrough(new CompressionStream("deflate"))).arrayBuffer(),this.logger.trace("WebTransport",(()=>{const e=t.byteLength,s=(e/n).toFixed(2);return{messageType:"text",message:`Body of ${n} bytes, compressed by ${s}x to ${e} bytes.`}}))}else t=e.body;return e.queryParameters&&0!==Object.keys(e.queryParameters).length&&(s=`${s}?${q(e.queryParameters)}`),{url:`${e.origin}${s}`,method:e.method,headers:e.headers,timeout:e.timeout,body:t}}))}isFetchMonkeyPatched(e){return!(null!=e?e:fetch).toString().includes("[native code]")&&"fetch"!==fetch.name}transportResponseFromXHR(e,t){const s=t.getAllResponseHeaders().split("\n"),n={};for(const e of s){const[t,s]=e.trim().split(":");t&&s&&(n[t.toLowerCase()]=s.trim())}return{status:t.status,url:e,headers:n,body:t.response}}static getOriginalFetch(){let e=document.querySelector('iframe[name="pubnub-context-unpatched-fetch"]');return e||(e=document.createElement("iframe"),e.style.display="none",e.name="pubnub-context-unpatched-fetch",e.src="about:blank",document.body.appendChild(e)),e.contentWindow?e.contentWindow.fetch.bind(e.contentWindow):fetch}}ue.encoder=new TextEncoder,ue.decoder=new TextDecoder;class le{constructor(e){this.params=e,this.requestIdentifier=te.createUUID(),this._cancellationController=null}get cancellationController(){return this._cancellationController}set cancellationController(e){this._cancellationController=e}abort(e){this&&this.cancellationController&&this.cancellationController.abort(e)}operation(){throw Error("Should be implemented by subclass.")}validate(){}parse(e){return i(this,void 0,void 0,(function*(){return this.deserializeResponse(e)}))}request(){var e,t,s,n,r,i;const a={method:null!==(t=null===(e=this.params)||void 0===e?void 0:e.method)&&void 0!==t?t:ae.GET,path:this.path,queryParameters:this.queryParameters,cancellable:null!==(n=null===(s=this.params)||void 0===s?void 0:s.cancellable)&&void 0!==n&&n,compressible:null!==(i=null===(r=this.params)||void 0===r?void 0:r.compressible)&&void 0!==i&&i,timeout:10,identifier:this.requestIdentifier},o=this.headers;if(o&&(a.headers=o),a.method===ae.POST||a.method===ae.PATCH){const[e,t]=[this.body,this.formData];t&&(a.formData=t),e&&(a.body=e)}return a}get headers(){var e,t;return Object.assign({"Accept-Encoding":"gzip, deflate"},null!==(t=null===(e=this.params)||void 0===e?void 0:e.compressible)&&void 0!==t&&t?{"Content-Encoding":"deflate"}:{})}get path(){throw Error("`path` getter should be implemented by subclass.")}get queryParameters(){return{}}get formData(){}get body(){}deserializeResponse(e){const t=le.decoder.decode(e.body),s=e.headers["content-type"];let n;if(!s||-1===s.indexOf("javascript")&&-1===s.indexOf("json"))throw new d("Service response error, check status for details",g(t,e.status));try{n=JSON.parse(t)}catch(s){throw console.error("Error parsing JSON response:",s),new d("Service response error, check status for details",g(t,e.status))}if("status"in n&&"number"==typeof n.status&&n.status>=400)throw I.create(e);return n}}le.decoder=new TextDecoder;var he;!function(e){e[e.Presence=-2]="Presence",e[e.Message=-1]="Message",e[e.Signal=1]="Signal",e[e.AppContext=2]="AppContext",e[e.MessageAction=3]="MessageAction",e[e.Files=4]="Files"}(he||(he={}));class de extends le{constructor(e){var t,s,n,r,i,a;super({cancellable:!0}),this.parameters=e,null!==(t=(r=this.parameters).withPresence)&&void 0!==t||(r.withPresence=false),null!==(s=(i=this.parameters).channelGroups)&&void 0!==s||(i.channelGroups=[]),null!==(n=(a=this.parameters).channels)&&void 0!==n||(a.channels=[])}operation(){return M.PNSubscribeOperation}validate(){const{keySet:{subscribeKey:e},channels:t,channelGroups:s}=this.parameters;return e?t||s?void 0:"`channels` and `channelGroups` both should not be empty":"Missing Subscribe Key"}parse(e){return i(this,void 0,void 0,(function*(){let t,s;try{s=le.decoder.decode(e.body);t=JSON.parse(s)}catch(e){console.error("Error parsing JSON response:",e)}if(!t)throw new d("Service response error, check status for details",g(s,e.status));const n=t.m.filter((e=>{const t=void 0===e.b?e.c:e.b;return this.parameters.channels&&this.parameters.channels.includes(t)||this.parameters.channelGroups&&this.parameters.channelGroups.includes(t)})).map((e=>{let{e:t}=e;null!=t||(t=e.c.endsWith("-pnpres")?he.Presence:he.Message);const s=B(e.d);return t!=he.Signal&&"string"==typeof e.d?t==he.Message?{type:he.Message,data:this.messageFromEnvelope(e),pn_mfp:s}:{type:he.Files,data:this.fileFromEnvelope(e),pn_mfp:s}:t==he.Message?{type:he.Message,data:this.messageFromEnvelope(e),pn_mfp:s}:t===he.Presence?{type:he.Presence,data:this.presenceEventFromEnvelope(e),pn_mfp:s}:t==he.Signal?{type:he.Signal,data:this.signalFromEnvelope(e),pn_mfp:s}:t===he.AppContext?{type:he.AppContext,data:this.appContextFromEnvelope(e),pn_mfp:s}:t===he.MessageAction?{type:he.MessageAction,data:this.messageActionFromEnvelope(e),pn_mfp:s}:{type:he.Files,data:this.fileFromEnvelope(e),pn_mfp:s}}));return{cursor:{timetoken:t.t.t,region:t.t.r},messages:n}}))}get headers(){var e;return Object.assign(Object.assign({},null!==(e=super.headers)&&void 0!==e?e:{}),{accept:"text/javascript"})}presenceEventFromEnvelope(e){var t;const{d:s}=e,[n,r]=this.subscriptionChannelFromEnvelope(e),i=n.replace("-pnpres",""),a=null!==r?i:null,o=null!==r?r:i;return"string"!=typeof s&&("data"in s?(s.state=s.data,delete s.data):"action"in s&&"interval"===s.action&&(s.hereNowRefresh=null!==(t=s.here_now_refresh)&&void 0!==t&&t,delete s.here_now_refresh)),Object.assign({channel:i,subscription:r,actualChannel:a,subscribedChannel:o,timetoken:e.p.t},s)}messageFromEnvelope(e){const[t,s]=this.subscriptionChannelFromEnvelope(e),[n,r]=this.decryptedData(e.d),i={channel:t,subscription:s,actualChannel:null!==s?t:null,subscribedChannel:null!==s?s:t,timetoken:e.p.t,publisher:e.i,message:n};return e.u&&(i.userMetadata=e.u),e.cmt&&(i.customMessageType=e.cmt),r&&(i.error=r),i}signalFromEnvelope(e){const[t,s]=this.subscriptionChannelFromEnvelope(e),n={channel:t,subscription:s,timetoken:e.p.t,publisher:e.i,message:e.d};return e.u&&(n.userMetadata=e.u),e.cmt&&(n.customMessageType=e.cmt),n}messageActionFromEnvelope(e){const[t,s]=this.subscriptionChannelFromEnvelope(e),n=e.d;return{channel:t,subscription:s,timetoken:e.p.t,publisher:e.i,event:n.event,data:Object.assign(Object.assign({},n.data),{uuid:e.i})}}appContextFromEnvelope(e){const[t,s]=this.subscriptionChannelFromEnvelope(e),n=e.d;return{channel:t,subscription:s,timetoken:e.p.t,message:n}}fileFromEnvelope(e){const[t,s]=this.subscriptionChannelFromEnvelope(e),[n,r]=this.decryptedData(e.d);let i=r;const a={channel:t,subscription:s,timetoken:e.p.t,publisher:e.i};return e.u&&(a.userMetadata=e.u),n?"string"==typeof n?null!=i||(i="Unexpected file information payload data type."):(a.message=n.message,n.file&&(a.file={id:n.file.id,name:n.file.name,url:this.parameters.getFileUrl({id:n.file.id,name:n.file.name,channel:t})})):null!=i||(i="File information payload is missing."),e.cmt&&(a.customMessageType=e.cmt),i&&(a.error=i),a}subscriptionChannelFromEnvelope(e){return[e.c,void 0===e.b?e.c:e.b]}decryptedData(e){if(!this.parameters.crypto||"string"!=typeof e)return[e,void 0];let t,s;try{const s=this.parameters.crypto.decrypt(e);t=s instanceof ArrayBuffer?JSON.parse(pe.decoder.decode(s)):s}catch(e){t=null,s=`Error while decrypting message content: ${e.message}`}return[null!=t?t:e,s]}}class pe extends de{get path(){var e;const{keySet:{subscribeKey:t},channels:s}=this.parameters;return`/v2/subscribe/${t}/${$(null!==(e=null==s?void 0:s.sort())&&void 0!==e?e:[],",")}/0`}get queryParameters(){const{channelGroups:e,filterExpression:t,heartbeat:s,state:n,timetoken:r,region:i,onDemand:a}=this.parameters,o={};return a&&(o["on-demand"]=1),e&&e.length>0&&(o["channel-group"]=e.sort().join(",")),t&&t.length>0&&(o["filter-expr"]=t),s&&(o.heartbeat=s),n&&Object.keys(n).length>0&&(o.state=JSON.stringify(n)),void 0!==r&&"string"==typeof r?r.length>0&&"0"!==r&&(o.tt=r):void 0!==r&&r>0&&(o.tt=r),i&&(o.tr=i),o}}class ge{constructor(){this.hasListeners=!1,this.listeners=[{count:-1,listener:{}}]}set onStatus(e){this.updateTypeOrObjectListener({add:!!e,listener:e,type:"status"})}set onMessage(e){this.updateTypeOrObjectListener({add:!!e,listener:e,type:"message"})}set onPresence(e){this.updateTypeOrObjectListener({add:!!e,listener:e,type:"presence"})}set onSignal(e){this.updateTypeOrObjectListener({add:!!e,listener:e,type:"signal"})}set onObjects(e){this.updateTypeOrObjectListener({add:!!e,listener:e,type:"objects"})}set onMessageAction(e){this.updateTypeOrObjectListener({add:!!e,listener:e,type:"messageAction"})}set onFile(e){this.updateTypeOrObjectListener({add:!!e,listener:e,type:"file"})}handleEvent(e){if(this.hasListeners)if(e.type===he.Message)this.announce("message",e.data);else if(e.type===he.Signal)this.announce("signal",e.data);else if(e.type===he.Presence)this.announce("presence",e.data);else if(e.type===he.AppContext){const{data:t}=e,{message:s}=t;if(this.announce("objects",t),"uuid"===s.type){const{message:e,channel:n}=t,i=r(t,["message","channel"]),{event:a,type:o}=s,c=r(s,["event","type"]),u=Object.assign(Object.assign({},i),{spaceId:n,message:Object.assign(Object.assign({},c),{event:"set"===a?"updated":"removed",type:"user"})});this.announce("user",u)}else if("channel"===s.type){const{message:e,channel:n}=t,i=r(t,["message","channel"]),{event:a,type:o}=s,c=r(s,["event","type"]),u=Object.assign(Object.assign({},i),{spaceId:n,message:Object.assign(Object.assign({},c),{event:"set"===a?"updated":"removed",type:"space"})});this.announce("space",u)}else if("membership"===s.type){const{message:e,channel:n}=t,i=r(t,["message","channel"]),{event:a,data:o}=s,c=r(s,["event","data"]),{uuid:u,channel:l}=o,h=r(o,["uuid","channel"]),d=Object.assign(Object.assign({},i),{spaceId:n,message:Object.assign(Object.assign({},c),{event:"set"===a?"updated":"removed",data:Object.assign(Object.assign({},h),{user:u,space:l})})});this.announce("membership",d)}}else e.type===he.MessageAction?this.announce("messageAction",e.data):e.type===he.Files&&this.announce("file",e.data)}handleStatus(e){this.hasListeners&&this.announce("status",e)}addListener(e){this.updateTypeOrObjectListener({add:!0,listener:e})}removeListener(e){this.updateTypeOrObjectListener({add:!1,listener:e})}removeAllListeners(){this.listeners=[{count:-1,listener:{}}],this.hasListeners=!1}updateTypeOrObjectListener(e){if(e.type)"function"==typeof e.listener?this.listeners[0].listener[e.type]=e.listener:delete this.listeners[0].listener[e.type];else if(e.listener&&"function"!=typeof e.listener){let t,s=!1;for(t of this.listeners)if(t.listener===e.listener){e.add?(t.count++,s=!0):(t.count--,0===t.count&&this.listeners.splice(this.listeners.indexOf(t),1));break}e.add&&!s&&this.listeners.push({count:1,listener:e.listener})}this.hasListeners=this.listeners.length>1||Object.keys(this.listeners[0]).length>0}announce(e,t){this.listeners.forEach((({listener:s})=>{const n=s[e];n&&n(t)}))}}class be{constructor(e){this.time=e}onReconnect(e){this.callback=e}startPolling(){this.timeTimer=setInterval((()=>this.callTime()),3e3)}stopPolling(){this.timeTimer&&clearInterval(this.timeTimer),this.timeTimer=null}callTime(){this.time((e=>{e.error||(this.stopPolling(),this.callback&&this.callback())}))}}class ye{constructor(e){this.config=e,e.logger().debug("DedupingManager",(()=>({messageType:"object",message:{maximumCacheSize:e.maximumCacheSize},details:"Create with configuration:"}))),this.maximumCacheSize=e.maximumCacheSize,this.hashHistory=[]}getKey(e){var t;return`${e.timetoken}-${this.hashCode(JSON.stringify(null!==(t=e.message)&&void 0!==t?t:"")).toString()}`}isDuplicate(e){return this.hashHistory.includes(this.getKey(e))}addEntry(e){this.hashHistory.length>=this.maximumCacheSize&&this.hashHistory.shift(),this.hashHistory.push(this.getKey(e))}clearHistory(){this.hashHistory=[]}hashCode(e){let t=0;if(0===e.length)return t;for(let s=0;s{this.pendingChannelSubscriptions.add(e),this.channels[e]={},r&&(this.presenceChannels[e]={}),(i||this.configuration.getHeartbeatInterval())&&(this.heartbeatChannels[e]={})})),null==s||s.forEach((e=>{this.pendingChannelGroupSubscriptions.add(e),this.channelGroups[e]={},r&&(this.presenceChannelGroups[e]={}),(i||this.configuration.getHeartbeatInterval())&&(this.heartbeatChannelGroups[e]={})})),this.subscriptionStatusAnnounced=!1,this.reconnect()}unsubscribe(e,t=!1){let{channels:s,channelGroups:n}=e;const i=new Set,a=new Set;if(null==s||s.forEach((e=>{e in this.channels&&(delete this.channels[e],a.add(e),e in this.heartbeatChannels&&delete this.heartbeatChannels[e]),e in this.presenceState&&delete this.presenceState[e],e in this.presenceChannels&&(delete this.presenceChannels[e],a.add(e))})),null==n||n.forEach((e=>{e in this.channelGroups&&(delete this.channelGroups[e],i.add(e),e in this.heartbeatChannelGroups&&delete this.heartbeatChannelGroups[e]),e in this.presenceState&&delete this.presenceState[e],e in this.presenceChannelGroups&&(delete this.presenceChannelGroups[e],i.add(e))})),0===a.size&&0===i.size)return;const o=this.lastTimetoken,c=this.currentTimetoken;0===Object.keys(this.channels).length&&0===Object.keys(this.presenceChannels).length&&0===Object.keys(this.channelGroups).length&&0===Object.keys(this.presenceChannelGroups).length&&(this.lastTimetoken="0",this.currentTimetoken="0",this.referenceTimetoken=null,this.storedTimetoken=null,this.region=null,this.reconnectionManager.stopPolling()),this.reconnect(!0),!1!==this.configuration.suppressLeaveEvents||t||(n=Array.from(i),s=Array.from(a),this.leaveCall({channels:s,channelGroups:n},(e=>{const{error:t}=e,i=r(e,["error"]);let a;t&&(e.errorData&&"object"==typeof e.errorData&&"message"in e.errorData&&"string"==typeof e.errorData.message?a=e.errorData.message:"message"in e&&"string"==typeof e.message&&(a=e.message)),this.emitStatus(Object.assign(Object.assign({},i),{error:null!=a&&a,affectedChannels:s,affectedChannelGroups:n,currentTimetoken:c,lastTimetoken:o}))})))}unsubscribeAll(e=!1){this.disconnectedWhileHandledEvent=!0,this.unsubscribe({channels:this.subscribedChannels,channelGroups:this.subscribedChannelGroups},e)}startSubscribeLoop(e=!1){this.disconnectedWhileHandledEvent=!1,this.stopSubscribeLoop();const t=[...Object.keys(this.channelGroups)],s=[...Object.keys(this.channels)];Object.keys(this.presenceChannelGroups).forEach((e=>t.push(`${e}-pnpres`))),Object.keys(this.presenceChannels).forEach((e=>s.push(`${e}-pnpres`))),0===s.length&&0===t.length||(this.subscribeCall(Object.assign(Object.assign(Object.assign({channels:s,channelGroups:t,state:this.presenceState,heartbeat:this.configuration.getPresenceTimeout(),timetoken:this.currentTimetoken},null!==this.region?{region:this.region}:{}),this.configuration.filterExpression?{filterExpression:this.configuration.filterExpression}:{}),{onDemand:!this.subscriptionStatusAnnounced||e}),((e,t)=>{this.processSubscribeResponse(e,t)})),!e&&this.configuration.useSmartHeartbeat&&this.startHeartbeatTimer())}stopSubscribeLoop(){this._subscribeAbort&&(this._subscribeAbort(),this._subscribeAbort=null)}processSubscribeResponse(e,t){if(e.error){if("object"==typeof e.errorData&&"name"in e.errorData&&"AbortError"===e.errorData.name||e.category===h.PNCancelledCategory)return;return void(e.category===h.PNTimeoutCategory?this.startSubscribeLoop():e.category===h.PNNetworkIssuesCategory||e.category===h.PNMalformedResponseCategory?(this.disconnect(),e.error&&this.configuration.autoNetworkDetection&&this.isOnline&&(this.isOnline=!1,this.emitStatus({category:h.PNNetworkDownCategory})),this.reconnectionManager.onReconnect((()=>{this.configuration.autoNetworkDetection&&!this.isOnline&&(this.isOnline=!0,this.emitStatus({category:h.PNNetworkUpCategory})),this.reconnect(),this.subscriptionStatusAnnounced=!0;const t={category:h.PNReconnectedCategory,operation:e.operation,lastTimetoken:this.lastTimetoken,currentTimetoken:this.currentTimetoken};this.emitStatus(t)})),this.reconnectionManager.startPolling(),this.emitStatus(Object.assign(Object.assign({},e),{category:h.PNNetworkIssuesCategory}))):e.category===h.PNBadRequestCategory?(this.stopHeartbeatTimer(),this.emitStatus(e)):this.emitStatus(e))}if(this.referenceTimetoken=K(t.cursor.timetoken,this.storedTimetoken),this.storedTimetoken?(this.currentTimetoken=this.storedTimetoken,this.storedTimetoken=null):(this.lastTimetoken=this.currentTimetoken,this.currentTimetoken=t.cursor.timetoken),!this.subscriptionStatusAnnounced){const t={category:h.PNConnectedCategory,operation:e.operation,affectedChannels:Array.from(this.pendingChannelSubscriptions),subscribedChannels:this.subscribedChannels,affectedChannelGroups:Array.from(this.pendingChannelGroupSubscriptions),lastTimetoken:this.lastTimetoken,currentTimetoken:this.currentTimetoken};this.subscriptionStatusAnnounced=!0,this.emitStatus(t),this.pendingChannelGroupSubscriptions.clear(),this.pendingChannelSubscriptions.clear()}const{messages:s}=t,{requestMessageCountThreshold:n,dedupeOnSubscribe:r}=this.configuration;n&&s.length>=n&&this.emitStatus({category:h.PNRequestMessageCountExceededCategory,operation:e.operation});try{const e={timetoken:this.currentTimetoken,region:this.region?this.region:void 0};this.configuration.logger().debug("SubscriptionManager",(()=>({messageType:"object",message:s.map((e=>({type:e.type,data:Object.assign(Object.assign({},e.data),{pn_mfp:e.pn_mfp})}))),details:"Received events:"}))),s.forEach((t=>{if(r&&"message"in t.data&&"timetoken"in t.data){if(this.dedupingManager.isDuplicate(t.data))return void this.configuration.logger().warn("SubscriptionManager",(()=>({messageType:"object",message:t.data,details:"Duplicate message detected (skipped):"})));this.dedupingManager.addEntry(t.data)}this.emitEvent(e,t)}))}catch(e){const t={error:!0,category:h.PNUnknownCategory,errorData:e,statusCode:0};this.emitStatus(t)}this.region=t.cursor.region,this.disconnectedWhileHandledEvent?this.disconnectedWhileHandledEvent=!1:this.startSubscribeLoop()}setState(e){const{state:t,channels:s,channelGroups:n}=e;null==s||s.forEach((e=>e in this.channels&&(this.presenceState[e]=t))),null==n||n.forEach((e=>e in this.channelGroups&&(this.presenceState[e]=t)))}changePresence(e){const{connected:t,channels:s,channelGroups:n}=e;t?(null==s||s.forEach((e=>this.heartbeatChannels[e]={})),null==n||n.forEach((e=>this.heartbeatChannelGroups[e]={}))):(null==s||s.forEach((e=>{e in this.heartbeatChannels&&delete this.heartbeatChannels[e]})),null==n||n.forEach((e=>{e in this.heartbeatChannelGroups&&delete this.heartbeatChannelGroups[e]})),!1===this.configuration.suppressLeaveEvents&&this.leaveCall({channels:s,channelGroups:n},(e=>this.emitStatus(e)))),this.reconnect()}startHeartbeatTimer(){this.stopHeartbeatTimer();const e=this.configuration.getHeartbeatInterval();e&&0!==e&&(this.configuration.useSmartHeartbeat||this.sendHeartbeat(),this.heartbeatTimer=setInterval((()=>this.sendHeartbeat()),1e3*e))}stopHeartbeatTimer(){this.heartbeatTimer&&(clearInterval(this.heartbeatTimer),this.heartbeatTimer=null)}sendHeartbeat(){const e=Object.keys(this.heartbeatChannelGroups),t=Object.keys(this.heartbeatChannels);0===t.length&&0===e.length||this.heartbeatCall({channels:t,channelGroups:e,heartbeat:this.configuration.getPresenceTimeout(),state:this.presenceState},(e=>{e.error&&this.configuration.announceFailedHeartbeats&&this.emitStatus(e),e.error&&this.configuration.autoNetworkDetection&&this.isOnline&&(this.isOnline=!1,this.disconnect(),this.emitStatus({category:h.PNNetworkDownCategory}),this.reconnect()),!e.error&&this.configuration.announceSuccessfulHeartbeats&&this.emitStatus(e)}))}}class fe{constructor(e,t,s){this._payload=e,this.setDefaultPayloadStructure(),this.title=t,this.body=s}get payload(){return this._payload}set title(e){this._title=e}set subtitle(e){this._subtitle=e}set body(e){this._body=e}set badge(e){this._badge=e}set sound(e){this._sound=e}setDefaultPayloadStructure(){}toObject(){return{}}}class ve extends fe{constructor(){super(...arguments),this._apnsPushType="apns",this._isSilent=!1}get payload(){return this._payload}set configurations(e){e&&e.length&&(this._configurations=e)}get notification(){return this.payload.aps}get title(){return this._title}set title(e){e&&e.length&&(this.payload.aps.alert.title=e,this._title=e)}get subtitle(){return this._subtitle}set subtitle(e){e&&e.length&&(this.payload.aps.alert.subtitle=e,this._subtitle=e)}get body(){return this._body}set body(e){e&&e.length&&(this.payload.aps.alert.body=e,this._body=e)}get badge(){return this._badge}set badge(e){null!=e&&(this.payload.aps.badge=e,this._badge=e)}get sound(){return this._sound}set sound(e){e&&e.length&&(this.payload.aps.sound=e,this._sound=e)}set silent(e){this._isSilent=e}setDefaultPayloadStructure(){this.payload.aps={alert:{}}}toObject(){const e=Object.assign({},this.payload),{aps:t}=e;let{alert:s}=t;if(this._isSilent&&(t["content-available"]=1),"apns2"===this._apnsPushType){if(!this._configurations||!this._configurations.length)throw new ReferenceError("APNS2 configuration is missing");const t=[];this._configurations.forEach((e=>{t.push(this.objectFromAPNS2Configuration(e))})),t.length&&(e.pn_push=t)}return s&&Object.keys(s).length||delete t.alert,this._isSilent&&(delete t.alert,delete t.badge,delete t.sound,s={}),this._isSilent||s&&Object.keys(s).length?e:null}objectFromAPNS2Configuration(e){if(!e.targets||!e.targets.length)throw new ReferenceError("At least one APNS2 target should be provided");const{collapseId:t,expirationDate:s}=e,n={auth_method:"token",targets:e.targets.map((e=>this.objectFromAPNSTarget(e))),version:"v2"};return t&&t.length&&(n.collapse_id=t),s&&(n.expiration=s.toISOString()),n}objectFromAPNSTarget(e){if(!e.topic||!e.topic.length)throw new TypeError("Target 'topic' undefined.");const{topic:t,environment:s="development",excludedDevices:n=[]}=e,r={topic:t,environment:s};return n.length&&(r.excluded_devices=n),r}}class Se extends fe{get payload(){return this._payload}get notification(){return this.payload.notification}get data(){return this.payload.data}get title(){return this._title}set title(e){e&&e.length&&(this.payload.notification.title=e,this._title=e)}get body(){return this._body}set body(e){e&&e.length&&(this.payload.notification.body=e,this._body=e)}get sound(){return this._sound}set sound(e){e&&e.length&&(this.payload.notification.sound=e,this._sound=e)}get icon(){return this._icon}set icon(e){e&&e.length&&(this.payload.notification.icon=e,this._icon=e)}get tag(){return this._tag}set tag(e){e&&e.length&&(this.payload.notification.tag=e,this._tag=e)}set silent(e){this._isSilent=e}setDefaultPayloadStructure(){this.payload.notification={},this.payload.data={}}toObject(){let e=Object.assign({},this.payload.data),t=null;const s={};if(Object.keys(this.payload).length>2){const t=r(this.payload,["notification","data"]);e=Object.assign(Object.assign({},e),t)}return this._isSilent?e.notification=this.payload.notification:t=this.payload.notification,Object.keys(e).length&&(s.data=e),t&&Object.keys(t).length&&(s.notification=t),Object.keys(s).length?s:null}}class we{constructor(e,t){this._payload={apns:{},fcm:{}},this._title=e,this._body=t,this.apns=new ve(this._payload.apns,e,t),this.fcm=new Se(this._payload.fcm,e,t)}set debugging(e){this._debugging=e}get title(){return this._title}get subtitle(){return this._subtitle}set subtitle(e){this._subtitle=e,this.apns.subtitle=e,this.fcm.subtitle=e}get body(){return this._body}get badge(){return this._badge}set badge(e){this._badge=e,this.apns.badge=e,this.fcm.badge=e}get sound(){return this._sound}set sound(e){this._sound=e,this.apns.sound=e,this.fcm.sound=e}buildPayload(e){const t={};if(e.includes("apns")||e.includes("apns2")){this.apns._apnsPushType=e.includes("apns")?"apns":"apns2";const s=this.apns.toObject();s&&Object.keys(s).length&&(t.pn_apns=s)}if(e.includes("fcm")){const e=this.fcm.toObject();e&&Object.keys(e).length&&(t.pn_gcm=e)}return Object.keys(t).length&&this._debugging&&(t.pn_debug=!0),t}}class Oe{constructor(e=!1){this.sync=e,this.listeners=new Set}subscribe(e){return this.listeners.add(e),()=>{this.listeners.delete(e)}}notify(e){const t=()=>{this.listeners.forEach((t=>{t(e)}))};this.sync?t():setTimeout(t,0)}}class ke{transition(e,t){var s;if(this.transitionMap.has(t.type))return null===(s=this.transitionMap.get(t.type))||void 0===s?void 0:s(e,t)}constructor(e){this.label=e,this.transitionMap=new Map,this.enterEffects=[],this.exitEffects=[]}on(e,t){return this.transitionMap.set(e,t),this}with(e,t){return[this,e,null!=t?t:[]]}onEnter(e){return this.enterEffects.push(e),this}onExit(e){return this.exitEffects.push(e),this}}class Ce extends Oe{constructor(e){super(!0),this.logger=e,this._pendingEvents=[],this._inTransition=!1}get currentState(){return this._currentState}get currentContext(){return this._currentContext}describe(e){return new ke(e)}start(e,t){this._currentState=e,this._currentContext=t,this.notify({type:"engineStarted",state:e,context:t})}transition(e){if(!this._currentState)throw this.logger.error("Engine","Finite state machine is not started"),new Error("Start the engine first");if(this._inTransition)return this.logger.trace("Engine",(()=>({messageType:"object",message:e,details:"Event engine in transition. Enqueue received event:"}))),void this._pendingEvents.push(e);this._inTransition=!0,this.logger.trace("Engine",(()=>({messageType:"object",message:e,details:"Event engine received event:"}))),this.notify({type:"eventReceived",event:e});const t=this._currentState.transition(this._currentContext,e);if(t){const[s,n,r]=t;this.logger.trace("Engine",`Exiting state: ${this._currentState.label}`);for(const e of this._currentState.exitEffects)this.notify({type:"invocationDispatched",invocation:e(this._currentContext)});this.logger.trace("Engine",(()=>({messageType:"object",details:`Entering '${s.label}' state with context:`,message:n})));const i=this._currentState;this._currentState=s;const a=this._currentContext;this._currentContext=n,this.notify({type:"transitionDone",fromState:i,fromContext:a,toState:s,toContext:n,event:e});for(const e of r)this.notify({type:"invocationDispatched",invocation:e});for(const e of this._currentState.enterEffects)this.notify({type:"invocationDispatched",invocation:e(this._currentContext)})}else this.logger.warn("Engine",`No transition from '${this._currentState.label}' found for event: ${e.type}`);if(this._inTransition=!1,this._pendingEvents.length>0){const e=this._pendingEvents.shift();e&&(this.logger.trace("Engine",(()=>({messageType:"object",message:e,details:"De-queueing pending event:"}))),this.transition(e))}}}class Pe{constructor(e,t){this.dependencies=e,this.logger=t,this.instances=new Map,this.handlers=new Map}on(e,t){this.handlers.set(e,t)}dispatch(e){if(this.logger.trace("Dispatcher",`Process invocation: ${e.type}`),"CANCEL"===e.type){if(this.instances.has(e.payload)){const t=this.instances.get(e.payload);null==t||t.cancel(),this.instances.delete(e.payload)}return}const t=this.handlers.get(e.type);if(!t)throw this.logger.error("Dispatcher",`Unhandled invocation '${e.type}'`),new Error(`Unhandled invocation '${e.type}'`);const s=t(e.payload,this.dependencies);this.logger.trace("Dispatcher",(()=>({messageType:"object",details:"Call invocation handler with parameters:",message:e.payload,ignoredKeys:["abortSignal"]}))),e.managed&&this.instances.set(e.type,s),s.start()}dispose(){for(const[e,t]of this.instances.entries())t.cancel(),this.instances.delete(e)}}function je(e,t){const s=function(...s){return{type:e,payload:null==t?void 0:t(...s)}};return s.type=e,s}function Ee(e,t){const s=(...s)=>({type:e,payload:t(...s),managed:!1});return s.type=e,s}function Ne(e,t){const s=(...s)=>({type:e,payload:t(...s),managed:!0});return s.type=e,s.cancel={type:"CANCEL",payload:e,managed:!1},s}class Te extends Error{constructor(){super("The operation was aborted."),this.name="AbortError",Object.setPrototypeOf(this,new.target.prototype)}}class _e extends Oe{constructor(){super(...arguments),this._aborted=!1}get aborted(){return this._aborted}throwIfAborted(){if(this._aborted)throw new Te}abort(){this._aborted=!0,this.notify(new Te)}}class Ie{constructor(e,t){this.payload=e,this.dependencies=t}}class Me extends Ie{constructor(e,t,s){super(e,t),this.asyncFunction=s,this.abortSignal=new _e}start(){this.asyncFunction(this.payload,this.abortSignal,this.dependencies).catch((e=>{}))}cancel(){this.abortSignal.abort()}}const Ae=e=>(t,s)=>new Me(t,s,e),Ue=Ne("HEARTBEAT",((e,t)=>({channels:e,groups:t}))),De=Ee("LEAVE",((e,t)=>({channels:e,groups:t}))),Fe=Ee("EMIT_STATUS",(e=>e)),Re=Ne("WAIT",(()=>({}))),$e=je("RECONNECT",(()=>({}))),xe=je("DISCONNECT",((e=!1)=>({isOffline:e}))),Le=je("JOINED",((e,t)=>({channels:e,groups:t}))),qe=je("LEFT",((e,t)=>({channels:e,groups:t}))),Ge=je("LEFT_ALL",((e=!1)=>({isOffline:e}))),Ke=je("HEARTBEAT_SUCCESS",(e=>({statusCode:e}))),He=je("HEARTBEAT_FAILURE",(e=>e)),Be=je("TIMES_UP",(()=>({})));class We extends Pe{constructor(e,t){super(t,t.config.logger()),this.on(Ue.type,Ae(((t,s,n)=>i(this,[t,s,n],void 0,(function*(t,s,{heartbeat:n,presenceState:r,config:i}){s.throwIfAborted();try{yield n(Object.assign(Object.assign({abortSignal:s,channels:t.channels,channelGroups:t.groups},i.maintainPresenceState&&{state:r}),{heartbeat:i.presenceTimeout}));e.transition(Ke(200))}catch(t){if(t instanceof d){if(t.status&&t.status.category==h.PNCancelledCategory)return;e.transition(He(t))}}}))))),this.on(De.type,Ae(((e,t,s)=>i(this,[e,t,s],void 0,(function*(e,t,{leave:s,config:n}){if(!n.suppressLeaveEvents)try{s({channels:e.channels,channelGroups:e.groups})}catch(e){}}))))),this.on(Re.type,Ae(((t,s,n)=>i(this,[t,s,n],void 0,(function*(t,s,{heartbeatDelay:n}){return s.throwIfAborted(),yield n(),s.throwIfAborted(),e.transition(Be())}))))),this.on(Fe.type,Ae(((e,t,s)=>i(this,[e,t,s],void 0,(function*(e,t,{emitStatus:s,config:n}){n.announceFailedHeartbeats&&!0===(null==e?void 0:e.error)?s(Object.assign(Object.assign({},e),{operation:M.PNHeartbeatOperation})):n.announceSuccessfulHeartbeats&&200===e.statusCode&&s(Object.assign(Object.assign({},e),{error:!1,operation:M.PNHeartbeatOperation,category:h.PNAcknowledgmentCategory}))})))))}}const ze=new ke("HEARTBEAT_STOPPED");ze.on(Le.type,((e,t)=>ze.with({channels:[...e.channels,...t.payload.channels.filter((t=>!e.channels.includes(t)))],groups:[...e.groups,...t.payload.groups.filter((t=>!e.groups.includes(t)))]}))),ze.on(qe.type,((e,t)=>ze.with({channels:e.channels.filter((e=>!t.payload.channels.includes(e))),groups:e.groups.filter((e=>!t.payload.groups.includes(e)))}))),ze.on($e.type,((e,t)=>Xe.with({channels:e.channels,groups:e.groups}))),ze.on(Ge.type,((e,t)=>Qe.with(void 0)));const Ve=new ke("HEARTBEAT_COOLDOWN");Ve.onEnter((()=>Re())),Ve.onExit((()=>Re.cancel)),Ve.on(Be.type,((e,t)=>Xe.with({channels:e.channels,groups:e.groups}))),Ve.on(Le.type,((e,t)=>Xe.with({channels:[...e.channels,...t.payload.channels.filter((t=>!e.channels.includes(t)))],groups:[...e.groups,...t.payload.groups.filter((t=>!e.groups.includes(t)))]}))),Ve.on(qe.type,((e,t)=>Xe.with({channels:e.channels.filter((e=>!t.payload.channels.includes(e))),groups:e.groups.filter((e=>!t.payload.groups.includes(e)))},[De(t.payload.channels,t.payload.groups)]))),Ve.on(xe.type,((e,t)=>ze.with({channels:e.channels,groups:e.groups},[...t.payload.isOffline?[]:[De(e.channels,e.groups)]]))),Ve.on(Ge.type,((e,t)=>Qe.with(void 0,[...t.payload.isOffline?[]:[De(e.channels,e.groups)]])));const Je=new ke("HEARTBEAT_FAILED");Je.on(Le.type,((e,t)=>Xe.with({channels:[...e.channels,...t.payload.channels.filter((t=>!e.channels.includes(t)))],groups:[...e.groups,...t.payload.groups.filter((t=>!e.groups.includes(t)))]}))),Je.on(qe.type,((e,t)=>Xe.with({channels:e.channels.filter((e=>!t.payload.channels.includes(e))),groups:e.groups.filter((e=>!t.payload.groups.includes(e)))},[De(t.payload.channels,t.payload.groups)]))),Je.on($e.type,((e,t)=>Xe.with({channels:e.channels,groups:e.groups}))),Je.on(xe.type,((e,t)=>ze.with({channels:e.channels,groups:e.groups},[...t.payload.isOffline?[]:[De(e.channels,e.groups)]]))),Je.on(Ge.type,((e,t)=>Qe.with(void 0,[...t.payload.isOffline?[]:[De(e.channels,e.groups)]])));const Xe=new ke("HEARTBEATING");Xe.onEnter((e=>Ue(e.channels,e.groups))),Xe.onExit((()=>Ue.cancel)),Xe.on(Ke.type,((e,t)=>Ve.with({channels:e.channels,groups:e.groups},[Fe(Object.assign({},t.payload))]))),Xe.on(Le.type,((e,t)=>Xe.with({channels:[...e.channels,...t.payload.channels.filter((t=>!e.channels.includes(t)))],groups:[...e.groups,...t.payload.groups.filter((t=>!e.groups.includes(t)))]}))),Xe.on(qe.type,((e,t)=>Xe.with({channels:e.channels.filter((e=>!t.payload.channels.includes(e))),groups:e.groups.filter((e=>!t.payload.groups.includes(e)))},[De(t.payload.channels,t.payload.groups)]))),Xe.on(He.type,((e,t)=>Je.with(Object.assign({},e),[...t.payload.status?[Fe(Object.assign({},t.payload.status))]:[]]))),Xe.on(xe.type,((e,t)=>ze.with({channels:e.channels,groups:e.groups},[...t.payload.isOffline?[]:[De(e.channels,e.groups)]]))),Xe.on(Ge.type,((e,t)=>Qe.with(void 0,[...t.payload.isOffline?[]:[De(e.channels,e.groups)]])));const Qe=new ke("HEARTBEAT_INACTIVE");Qe.on(Le.type,((e,t)=>Xe.with({channels:t.payload.channels,groups:t.payload.groups})));class Ye{get _engine(){return this.engine}constructor(e){this.dependencies=e,this.channels=[],this.groups=[],this.engine=new Ce(e.config.logger()),this.dispatcher=new We(this.engine,e),e.config.logger().debug("PresenceEventEngine","Create presence event engine."),this._unsubscribeEngine=this.engine.subscribe((e=>{"invocationDispatched"===e.type&&this.dispatcher.dispatch(e.invocation)})),this.engine.start(Qe,void 0)}join({channels:e,groups:t}){this.channels=[...this.channels,...(null!=e?e:[]).filter((e=>!this.channels.includes(e)))],this.groups=[...this.groups,...(null!=t?t:[]).filter((e=>!this.groups.includes(e)))],0===this.channels.length&&0===this.groups.length||this.engine.transition(Le(this.channels.slice(0),this.groups.slice(0)))}leave({channels:e,groups:t}){this.dependencies.presenceState&&(null==e||e.forEach((e=>delete this.dependencies.presenceState[e])),null==t||t.forEach((e=>delete this.dependencies.presenceState[e]))),this.engine.transition(qe(null!=e?e:[],null!=t?t:[]))}leaveAll(e=!1){this.engine.transition(Ge(e))}reconnect(){this.engine.transition($e())}disconnect(e=!1){this.engine.transition(xe(e))}dispose(){this.disconnect(!0),this._unsubscribeEngine(),this.dispatcher.dispose()}}const Ze=Ne("HANDSHAKE",((e,t,s)=>({channels:e,groups:t,onDemand:s}))),et=Ne("RECEIVE_MESSAGES",((e,t,s,n)=>({channels:e,groups:t,cursor:s,onDemand:n}))),tt=Ee("EMIT_MESSAGES",((e,t)=>({cursor:e,events:t}))),st=Ee("EMIT_STATUS",(e=>e)),nt=je("SUBSCRIPTION_CHANGED",((e,t,s=!1)=>({channels:e,groups:t,isOffline:s}))),rt=je("SUBSCRIPTION_RESTORED",((e,t,s,n)=>({channels:e,groups:t,cursor:{timetoken:s,region:null!=n?n:0}}))),it=je("HANDSHAKE_SUCCESS",(e=>e)),at=je("HANDSHAKE_FAILURE",(e=>e)),ot=je("RECEIVE_SUCCESS",((e,t)=>({cursor:e,events:t}))),ct=je("RECEIVE_FAILURE",(e=>e)),ut=je("DISCONNECT",((e=!1)=>({isOffline:e}))),lt=je("RECONNECT",((e,t)=>({cursor:{timetoken:null!=e?e:"",region:null!=t?t:0}}))),ht=je("UNSUBSCRIBE_ALL",(()=>({}))),dt=new ke("UNSUBSCRIBED");dt.on(nt.type,((e,{payload:t})=>0===t.channels.length&&0===t.groups.length?dt.with(void 0):bt.with({channels:t.channels,groups:t.groups,onDemand:!0}))),dt.on(rt.type,((e,{payload:t})=>0===t.channels.length&&0===t.groups.length?dt.with(void 0):bt.with({channels:t.channels,groups:t.groups,cursor:{timetoken:`${t.cursor.timetoken}`,region:t.cursor.region},onDemand:!0})));const pt=new ke("HANDSHAKE_STOPPED");pt.on(nt.type,((e,{payload:t})=>0===t.channels.length&&0===t.groups.length?dt.with(void 0):pt.with({channels:t.channels,groups:t.groups,cursor:e.cursor}))),pt.on(lt.type,((e,{payload:t})=>bt.with(Object.assign(Object.assign({},e),{cursor:t.cursor||e.cursor,onDemand:!0})))),pt.on(rt.type,((e,{payload:t})=>{var s;return 0===t.channels.length&&0===t.groups.length?dt.with(void 0):pt.with({channels:t.channels,groups:t.groups,cursor:{timetoken:`${t.cursor.timetoken}`,region:t.cursor.region||(null===(s=e.cursor)||void 0===s?void 0:s.region)||0}})})),pt.on(ht.type,(e=>dt.with()));const gt=new ke("HANDSHAKE_FAILED");gt.on(nt.type,((e,{payload:t})=>0===t.channels.length&&0===t.groups.length?dt.with(void 0):bt.with({channels:t.channels,groups:t.groups,cursor:e.cursor,onDemand:!0}))),gt.on(lt.type,((e,{payload:t})=>bt.with(Object.assign(Object.assign({},e),{cursor:t.cursor||e.cursor,onDemand:!0})))),gt.on(rt.type,((e,{payload:t})=>{var s,n;return 0===t.channels.length&&0===t.groups.length?dt.with(void 0):bt.with({channels:t.channels,groups:t.groups,cursor:{timetoken:`${t.cursor.timetoken}`,region:t.cursor.region?t.cursor.region:null!==(n=null===(s=null==e?void 0:e.cursor)||void 0===s?void 0:s.region)&&void 0!==n?n:0},onDemand:!0})})),gt.on(ht.type,(e=>dt.with()));const bt=new ke("HANDSHAKING");bt.onEnter((e=>{var t;return Ze(e.channels,e.groups,null!==(t=e.onDemand)&&void 0!==t&&t)})),bt.onExit((()=>Ze.cancel)),bt.on(nt.type,((e,{payload:t})=>0===t.channels.length&&0===t.groups.length?dt.with(void 0):bt.with({channels:t.channels,groups:t.groups,cursor:e.cursor,onDemand:!0}))),bt.on(it.type,((e,{payload:t})=>{var s,n,r,i,a;return ft.with({channels:e.channels,groups:e.groups,cursor:{timetoken:(null===(s=e.cursor)||void 0===s?void 0:s.timetoken)?null===(n=e.cursor)||void 0===n?void 0:n.timetoken:t.timetoken,region:t.region},referenceTimetoken:K(t.timetoken,null===(r=e.cursor)||void 0===r?void 0:r.timetoken)},[st({category:h.PNConnectedCategory,affectedChannels:e.channels.slice(0),affectedChannelGroups:e.groups.slice(0),currentTimetoken:(null===(i=e.cursor)||void 0===i?void 0:i.timetoken)?null===(a=e.cursor)||void 0===a?void 0:a.timetoken:t.timetoken})])})),bt.on(at.type,((e,t)=>{var s;return gt.with(Object.assign(Object.assign({},e),{reason:t.payload}),[st({category:h.PNConnectionErrorCategory,error:null===(s=t.payload.status)||void 0===s?void 0:s.category})])})),bt.on(ut.type,((e,t)=>{var s;if(t.payload.isOffline){const t=I.create(new Error("Network connection error")).toPubNubError(M.PNSubscribeOperation);return gt.with(Object.assign(Object.assign({},e),{reason:t}),[st({category:h.PNConnectionErrorCategory,error:null===(s=t.status)||void 0===s?void 0:s.category})])}return pt.with(Object.assign({},e))})),bt.on(rt.type,((e,{payload:t})=>{var s;return 0===t.channels.length&&0===t.groups.length?dt.with(void 0):bt.with({channels:t.channels,groups:t.groups,cursor:{timetoken:`${t.cursor.timetoken}`,region:t.cursor.region||(null===(s=null==e?void 0:e.cursor)||void 0===s?void 0:s.region)||0},onDemand:!0})})),bt.on(ht.type,(e=>dt.with()));const yt=new ke("RECEIVE_STOPPED");yt.on(nt.type,((e,{payload:t})=>0===t.channels.length&&0===t.groups.length?dt.with(void 0):yt.with({channels:t.channels,groups:t.groups,cursor:e.cursor}))),yt.on(rt.type,((e,{payload:t})=>0===t.channels.length&&0===t.groups.length?dt.with(void 0):yt.with({channels:t.channels,groups:t.groups,cursor:{timetoken:`${t.cursor.timetoken}`,region:t.cursor.region||e.cursor.region}}))),yt.on(lt.type,((e,{payload:t})=>{var s;return bt.with({channels:e.channels,groups:e.groups,cursor:{timetoken:t.cursor.timetoken?null===(s=t.cursor)||void 0===s?void 0:s.timetoken:e.cursor.timetoken,region:t.cursor.region||e.cursor.region},onDemand:!0})})),yt.on(ht.type,(()=>dt.with(void 0)));const mt=new ke("RECEIVE_FAILED");mt.on(lt.type,((e,{payload:t})=>{var s;return bt.with({channels:e.channels,groups:e.groups,cursor:{timetoken:t.cursor.timetoken?null===(s=t.cursor)||void 0===s?void 0:s.timetoken:e.cursor.timetoken,region:t.cursor.region||e.cursor.region},onDemand:!0})})),mt.on(nt.type,((e,{payload:t})=>0===t.channels.length&&0===t.groups.length?dt.with(void 0):bt.with({channels:t.channels,groups:t.groups,cursor:e.cursor,onDemand:!0}))),mt.on(rt.type,((e,{payload:t})=>0===t.channels.length&&0===t.groups.length?dt.with(void 0):bt.with({channels:t.channels,groups:t.groups,cursor:{timetoken:`${t.cursor.timetoken}`,region:t.cursor.region||e.cursor.region},onDemand:!0}))),mt.on(ht.type,(e=>dt.with(void 0)));const ft=new ke("RECEIVING");ft.onEnter((e=>{var t;return et(e.channels,e.groups,e.cursor,null!==(t=e.onDemand)&&void 0!==t&&t)})),ft.onExit((()=>et.cancel)),ft.on(ot.type,((e,{payload:t})=>ft.with({channels:e.channels,groups:e.groups,cursor:t.cursor,referenceTimetoken:K(t.cursor.timetoken)},[tt(e.cursor,t.events)]))),ft.on(nt.type,((e,{payload:t})=>{var s;if(0===t.channels.length&&0===t.groups.length){let e;return t.isOffline&&(e=null===(s=I.create(new Error("Network connection error")).toPubNubError(M.PNSubscribeOperation).status)||void 0===s?void 0:s.category),dt.with(void 0,[st(Object.assign({category:t.isOffline?h.PNDisconnectedUnexpectedlyCategory:h.PNDisconnectedCategory},e?{error:e}:{}))])}return ft.with({channels:t.channels,groups:t.groups,cursor:e.cursor,referenceTimetoken:e.referenceTimetoken,onDemand:!0},[st({category:h.PNSubscriptionChangedCategory,affectedChannels:t.channels.slice(0),affectedChannelGroups:t.groups.slice(0),currentTimetoken:e.cursor.timetoken})])})),ft.on(rt.type,((e,{payload:t})=>0===t.channels.length&&0===t.groups.length?dt.with(void 0,[st({category:h.PNDisconnectedCategory})]):ft.with({channels:t.channels,groups:t.groups,cursor:{timetoken:`${t.cursor.timetoken}`,region:t.cursor.region||e.cursor.region},referenceTimetoken:K(e.cursor.timetoken,`${t.cursor.timetoken}`,e.referenceTimetoken),onDemand:!0},[st({category:h.PNSubscriptionChangedCategory,affectedChannels:t.channels.slice(0),affectedChannelGroups:t.groups.slice(0),currentTimetoken:t.cursor.timetoken})]))),ft.on(ct.type,((e,{payload:t})=>{var s;return mt.with(Object.assign(Object.assign({},e),{reason:t}),[st({category:h.PNDisconnectedUnexpectedlyCategory,error:null===(s=t.status)||void 0===s?void 0:s.category})])})),ft.on(ut.type,((e,t)=>{var s;if(t.payload.isOffline){const t=I.create(new Error("Network connection error")).toPubNubError(M.PNSubscribeOperation);return mt.with(Object.assign(Object.assign({},e),{reason:t}),[st({category:h.PNDisconnectedUnexpectedlyCategory,error:null===(s=t.status)||void 0===s?void 0:s.category})])}return yt.with(Object.assign({},e),[st({category:h.PNDisconnectedCategory})])})),ft.on(ht.type,(e=>dt.with(void 0,[st({category:h.PNDisconnectedCategory})])));class vt extends Pe{constructor(e,t){super(t,t.config.logger()),this.on(Ze.type,Ae(((t,s,n)=>i(this,[t,s,n],void 0,(function*(t,s,{handshake:n,presenceState:r,config:i}){s.throwIfAborted();try{const a=yield n(Object.assign(Object.assign({abortSignal:s,channels:t.channels,channelGroups:t.groups,filterExpression:i.filterExpression},i.maintainPresenceState&&{state:r}),{onDemand:t.onDemand}));return e.transition(it(a))}catch(t){if(t instanceof d){if(t.status&&t.status.category==h.PNCancelledCategory)return;return e.transition(at(t))}}}))))),this.on(et.type,Ae(((t,s,n)=>i(this,[t,s,n],void 0,(function*(t,s,{receiveMessages:n,config:r}){s.throwIfAborted();try{const i=yield n({abortSignal:s,channels:t.channels,channelGroups:t.groups,timetoken:t.cursor.timetoken,region:t.cursor.region,filterExpression:r.filterExpression,onDemand:t.onDemand});e.transition(ot(i.cursor,i.messages))}catch(t){if(t instanceof d){if(t.status&&t.status.category==h.PNCancelledCategory)return;if(!s.aborted)return e.transition(ct(t))}}}))))),this.on(tt.type,Ae(((e,t,s)=>i(this,[e,t,s],void 0,(function*({cursor:e,events:t},s,{emitMessages:n}){t.length>0&&n(e,t)}))))),this.on(st.type,Ae(((e,t,s)=>i(this,[e,t,s],void 0,(function*(e,t,{emitStatus:s}){return s(e)})))))}}class St{get _engine(){return this.engine}constructor(e){this.channels=[],this.groups=[],this.dependencies=e,this.engine=new Ce(e.config.logger()),this.dispatcher=new vt(this.engine,e),e.config.logger().debug("EventEngine","Create subscribe event engine."),this._unsubscribeEngine=this.engine.subscribe((e=>{"invocationDispatched"===e.type&&this.dispatcher.dispatch(e.invocation)})),this.engine.start(dt,void 0)}get subscriptionTimetoken(){const e=this.engine.currentState;if(!e)return;let t,s="0";if(e.label===ft.label){const e=this.engine.currentContext;s=e.cursor.timetoken,t=e.referenceTimetoken}return G(s,null!=t?t:"0")}subscribe({channels:e,channelGroups:t,timetoken:s,withPresence:n}){this.channels=[...this.channels,...null!=e?e:[]],this.groups=[...this.groups,...null!=t?t:[]],n&&(this.channels.map((e=>this.channels.push(`${e}-pnpres`))),this.groups.map((e=>this.groups.push(`${e}-pnpres`)))),s?this.engine.transition(rt(Array.from(new Set([...this.channels,...null!=e?e:[]])),Array.from(new Set([...this.groups,...null!=t?t:[]])),s)):this.engine.transition(nt(Array.from(new Set([...this.channels,...null!=e?e:[]])),Array.from(new Set([...this.groups,...null!=t?t:[]])))),this.dependencies.join&&this.dependencies.join({channels:Array.from(new Set(this.channels.filter((e=>!e.endsWith("-pnpres"))))),groups:Array.from(new Set(this.groups.filter((e=>!e.endsWith("-pnpres")))))})}unsubscribe({channels:e=[],channelGroups:t=[]}){const s=x(this.channels,[...e,...e.map((e=>`${e}-pnpres`))]),n=x(this.groups,[...t,...t.map((e=>`${e}-pnpres`))]);if(new Set(this.channels).size!==new Set(s).size||new Set(this.groups).size!==new Set(n).size){const r=L(this.channels,e),i=L(this.groups,t);this.dependencies.presenceState&&(null==r||r.forEach((e=>delete this.dependencies.presenceState[e])),null==i||i.forEach((e=>delete this.dependencies.presenceState[e]))),this.channels=s,this.groups=n,this.engine.transition(nt(Array.from(new Set(this.channels.slice(0))),Array.from(new Set(this.groups.slice(0))))),this.dependencies.leave&&this.dependencies.leave({channels:r.slice(0),groups:i.slice(0)})}}unsubscribeAll(e=!1){const t=this.getSubscribedChannelGroups(),s=this.getSubscribedChannels();this.channels=[],this.groups=[],this.dependencies.presenceState&&Object.keys(this.dependencies.presenceState).forEach((e=>{delete this.dependencies.presenceState[e]})),this.engine.transition(nt(this.channels.slice(0),this.groups.slice(0),e)),this.dependencies.leaveAll&&this.dependencies.leaveAll({channels:s,groups:t,isOffline:e})}reconnect({timetoken:e,region:t}){const s=this.getSubscribedChannels(),n=this.getSubscribedChannels();this.engine.transition(lt(e,t)),this.dependencies.presenceReconnect&&this.dependencies.presenceReconnect({channels:n,groups:s})}disconnect(e=!1){const t=this.getSubscribedChannels(),s=this.getSubscribedChannels();this.engine.transition(ut(e)),this.dependencies.presenceDisconnect&&this.dependencies.presenceDisconnect({channels:s,groups:t,isOffline:e})}getSubscribedChannels(){return Array.from(new Set(this.channels.slice(0)))}getSubscribedChannelGroups(){return Array.from(new Set(this.groups.slice(0)))}dispose(){this.disconnect(!0),this._unsubscribeEngine(),this.dispatcher.dispose()}}class wt extends le{constructor(e){var t;const s=null!==(t=e.sendByPost)&&void 0!==t&&t;super({method:s?ae.POST:ae.GET,compressible:s}),this.parameters=e,this.parameters.sendByPost=s}operation(){return M.PNPublishOperation}validate(){const{message:e,channel:t,keySet:{publishKey:s}}=this.parameters;return t?e?s?void 0:"Missing 'publishKey'":"Missing 'message'":"Missing 'channel'"}parse(e){return i(this,void 0,void 0,(function*(){return{timetoken:this.deserializeResponse(e)[2]}}))}get path(){const{message:e,channel:t,keySet:s}=this.parameters,n=this.prepareMessagePayload(e);return`/publish/${s.publishKey}/${s.subscribeKey}/0/${R(t)}/0${this.parameters.sendByPost?"":`/${R(n)}`}`}get queryParameters(){const{customMessageType:e,meta:t,replicate:s,storeInHistory:n,ttl:r}=this.parameters,i={};return e&&(i.custom_message_type=e),void 0!==n&&(i.store=n?"1":"0"),void 0!==r&&(i.ttl=r),void 0===s||s||(i.norep="true"),t&&"object"==typeof t&&(i.meta=JSON.stringify(t)),i}get headers(){var e;return this.parameters.sendByPost?Object.assign(Object.assign({},null!==(e=super.headers)&&void 0!==e?e:{}),{"Content-Type":"application/json"}):super.headers}get body(){return this.prepareMessagePayload(this.parameters.message)}prepareMessagePayload(e){const{crypto:t}=this.parameters;if(!t)return JSON.stringify(e)||"";const s=t.encrypt(JSON.stringify(e));return JSON.stringify("string"==typeof s?s:u(s))}}class Ot extends le{constructor(e){super(),this.parameters=e}operation(){return M.PNSignalOperation}validate(){const{message:e,channel:t,keySet:{publishKey:s}}=this.parameters;return t?e?s?void 0:"Missing 'publishKey'":"Missing 'message'":"Missing 'channel'"}parse(e){return i(this,void 0,void 0,(function*(){return{timetoken:this.deserializeResponse(e)[2]}}))}get path(){const{keySet:{publishKey:e,subscribeKey:t},channel:s,message:n}=this.parameters,r=JSON.stringify(n);return`/signal/${e}/${t}/0/${R(s)}/0/${R(r)}`}get queryParameters(){const{customMessageType:e}=this.parameters,t={};return e&&(t.custom_message_type=e),t}}class kt extends de{operation(){return M.PNReceiveMessagesOperation}validate(){const e=super.validate();return e||(this.parameters.timetoken?this.parameters.region?void 0:"region can not be empty":"timetoken can not be empty")}get path(){const{keySet:{subscribeKey:e},channels:t=[]}=this.parameters;return`/v2/subscribe/${e}/${$(t.sort(),",")}/0`}get queryParameters(){const{channelGroups:e,filterExpression:t,timetoken:s,region:n,onDemand:r}=this.parameters,i={ee:""};return r&&(i["on-demand"]=1),e&&e.length>0&&(i["channel-group"]=e.sort().join(",")),t&&t.length>0&&(i["filter-expr"]=t),"string"==typeof s?s&&"0"!==s&&s.length>0&&(i.tt=s):s&&s>0&&(i.tt=s),n&&(i.tr=n),i}}class Ct extends de{operation(){return M.PNHandshakeOperation}get path(){const{keySet:{subscribeKey:e},channels:t=[]}=this.parameters;return`/v2/subscribe/${e}/${$(t.sort(),",")}/0`}get queryParameters(){const{channelGroups:e,filterExpression:t,state:s,onDemand:n}=this.parameters,r={ee:""};return n&&(r["on-demand"]=1),e&&e.length>0&&(r["channel-group"]=e.sort().join(",")),t&&t.length>0&&(r["filter-expr"]=t),s&&Object.keys(s).length>0&&(r.state=JSON.stringify(s)),r}}var Pt;!function(e){e[e.Channel=0]="Channel",e[e.ChannelGroup=1]="ChannelGroup"}(Pt||(Pt={}));class jt{constructor({channels:e,channelGroups:t}){this.isEmpty=!0,this._channelGroups=new Set((null!=t?t:[]).filter((e=>e.length>0))),this._channels=new Set((null!=e?e:[]).filter((e=>e.length>0))),this.isEmpty=0===this._channels.size&&0===this._channelGroups.size}get length(){return this.isEmpty?0:this._channels.size+this._channelGroups.size}get channels(){return this.isEmpty?[]:Array.from(this._channels)}get channelGroups(){return this.isEmpty?[]:Array.from(this._channelGroups)}contains(e){return!this.isEmpty&&(this._channels.has(e)||this._channelGroups.has(e))}with(e){return new jt({channels:[...this._channels,...e._channels],channelGroups:[...this._channelGroups,...e._channelGroups]})}without(e){return new jt({channels:[...this._channels].filter((t=>!e._channels.has(t))),channelGroups:[...this._channelGroups].filter((t=>!e._channelGroups.has(t)))})}add(e){return e._channelGroups.size>0&&(this._channelGroups=new Set([...this._channelGroups,...e._channelGroups])),e._channels.size>0&&(this._channels=new Set([...this._channels,...e._channels])),this.isEmpty=0===this._channels.size&&0===this._channelGroups.size,this}remove(e){return e._channelGroups.size>0&&(this._channelGroups=new Set([...this._channelGroups].filter((t=>!e._channelGroups.has(t))))),e._channels.size>0&&(this._channels=new Set([...this._channels].filter((t=>!e._channels.has(t))))),this}removeAll(){return this._channels.clear(),this._channelGroups.clear(),this.isEmpty=!0,this}toString(){return`SubscriptionInput { channels: [${this.channels.join(", ")}], channelGroups: [${this.channelGroups.join(", ")}], is empty: ${this.isEmpty?"true":"false"}} }`}}class Et{constructor(e,t,s,n){this._isSubscribed=!1,this.clones={},this.parents=[],this._id=te.createUUID(),this.referenceTimetoken=n,this.subscriptionInput=t,this.options=s,this.client=e}get id(){return this._id}get isLastClone(){return 1===Object.keys(this.clones).length}get isSubscribed(){return!!this._isSubscribed||this.parents.length>0&&this.parents.some((e=>e.isSubscribed))}set isSubscribed(e){this.isSubscribed!==e&&(this._isSubscribed=e)}addParentState(e){this.parents.includes(e)||this.parents.push(e)}removeParentState(e){const t=this.parents.indexOf(e);-1!==t&&this.parents.splice(t,1)}storeClone(e,t){this.clones[e]||(this.clones[e]=t)}}class Nt{constructor(e,t="Subscription"){this.subscriptionType=t,this.id=te.createUUID(),this.eventDispatcher=new ge,this._state=e}get state(){return this._state}get channels(){return this.state.subscriptionInput.channels.slice(0)}get channelGroups(){return this.state.subscriptionInput.channelGroups.slice(0)}set onMessage(e){this.eventDispatcher.onMessage=e}set onPresence(e){this.eventDispatcher.onPresence=e}set onSignal(e){this.eventDispatcher.onSignal=e}set onObjects(e){this.eventDispatcher.onObjects=e}set onMessageAction(e){this.eventDispatcher.onMessageAction=e}set onFile(e){this.eventDispatcher.onFile=e}addListener(e){this.eventDispatcher.addListener(e)}removeListener(e){this.eventDispatcher.removeListener(e)}removeAllListeners(){this.eventDispatcher.removeAllListeners()}handleEvent(e,t){var s;if((!this.state.cursor||e>this.state.cursor)&&(this.state.cursor=e),this.state.referenceTimetoken&&t.data.timetoken({messageType:"text",message:`Event timetoken (${t.data.timetoken}) is older than reference timetoken (${this.state.referenceTimetoken}) for ${this.id} subscription object. Ignoring event.`})));if((null===(s=this.state.options)||void 0===s?void 0:s.filter)&&!this.state.options.filter(t))return void this.state.client.logger.trace(this.subscriptionType,`Event filtered out by filter function for ${this.id} subscription object. Ignoring event.`);const n=Object.values(this.state.clones);n.length>0&&this.state.client.logger.trace(this.subscriptionType,`Notify ${this.id} subscription object clones (count: ${n.length}) about received event.`),n.forEach((e=>e.eventDispatcher.handleEvent(t)))}dispose(){const e=Object.keys(this.state.clones);e.length>1?(this.state.client.logger.debug(this.subscriptionType,`Remove subscription object clone on dispose: ${this.id}`),delete this.state.clones[this.id]):1===e.length&&this.state.clones[this.id]&&(this.state.client.logger.debug(this.subscriptionType,`Unsubscribe subscription object on dispose: ${this.id}`),this.unsubscribe())}invalidate(e=!1){this.state._isSubscribed=!1,e&&(delete this.state.clones[this.id],0===Object.keys(this.state.clones).length&&(this.state.client.logger.trace(this.subscriptionType,"Last clone removed. Reset shared subscription state."),this.state.subscriptionInput.removeAll(),this.state.parents=[]))}subscribe(e){this.state.isSubscribed?this.state.client.logger.trace(this.subscriptionType,"Already subscribed. Ignoring subscribe request."):(this.state.client.logger.debug(this.subscriptionType,(()=>e?{messageType:"object",message:e,details:"Subscribe with parameters:"}:{messageType:"text",message:"Subscribe"})),this.state.isSubscribed=!0,this.updateSubscription({subscribing:!0,timetoken:null==e?void 0:e.timetoken}))}unsubscribe(){if(!this.state._isSubscribed||this.state.isSubscribed){if(!this.state._isSubscribed&&this.state.parents.length>0&&this.state.isSubscribed)return void this.state.client.logger.warn(this.subscriptionType,(()=>({messageType:"object",details:"Subscription is subscribed as part of a subscription set. Remove from active sets to unsubscribe:",message:this.state.parents.filter((e=>e.isSubscribed))})));if(!this.state._isSubscribed)return void this.state.client.logger.trace(this.subscriptionType,"Not subscribed. Ignoring unsubscribe request.")}this.state.client.logger.debug(this.subscriptionType,"Unsubscribe"),this.state.isSubscribed=!1,delete this.state.cursor,this.updateSubscription({subscribing:!1})}updateSubscription(e){var t,s;(null==e?void 0:e.timetoken)&&((null===(t=this.state.cursor)||void 0===t?void 0:t.timetoken)&&"0"!==(null===(s=this.state.cursor)||void 0===s?void 0:s.timetoken)?"0"!==e.timetoken&&e.timetoken>this.state.cursor.timetoken&&(this.state.cursor.timetoken=e.timetoken):this.state.cursor={timetoken:e.timetoken});const n=e.subscriptions&&e.subscriptions.length>0?e.subscriptions:void 0;e.subscribing?this.register(Object.assign(Object.assign({},e.timetoken?{cursor:this.state.cursor}:{}),n?{subscriptions:n}:{})):this.unregister(n)}}class Tt extends Et{constructor(e){const t=new jt({});e.subscriptions.forEach((e=>t.add(e.state.subscriptionInput))),super(e.client,t,e.options,e.client.subscriptionTimetoken),this.subscriptions=e.subscriptions}addSubscription(e){this.subscriptions.includes(e)||(e.state.addParentState(this),this.subscriptions.push(e),this.subscriptionInput.add(e.state.subscriptionInput))}removeSubscription(e,t){const s=this.subscriptions.indexOf(e);-1!==s&&(this.subscriptions.splice(s,1),t||e.state.removeParentState(this),this.subscriptionInput.remove(e.state.subscriptionInput))}removeAllSubscriptions(){this.subscriptions.forEach((e=>e.state.removeParentState(this))),this.subscriptions.splice(0,this.subscriptions.length),this.subscriptionInput.removeAll()}}class _t extends Nt{constructor(e){let t;if("client"in e){let s=[];!e.subscriptions&&e.entities?e.entities.forEach((t=>s.push(t.subscription(e.options)))):e.subscriptions&&(s=e.subscriptions),t=new Tt({client:e.client,subscriptions:s,options:e.options}),s.forEach((e=>e.state.addParentState(t))),t.client.logger.debug("SubscriptionSet",(()=>({messageType:"object",details:"Create subscription set with parameters:",message:Object.assign({subscriptions:t.subscriptions},e.options?e.options:{})})))}else t=e.state,t.client.logger.debug("SubscriptionSet","Create subscription set clone");super(t,"SubscriptionSet"),this.state.storeClone(this.id,this),t.subscriptions.forEach((e=>e.addParentSet(this)))}get state(){return super.state}get subscriptions(){return this.state.subscriptions.slice(0)}handleEvent(e,t){var s;this.state.subscriptionInput.contains(null!==(s=t.data.subscription)&&void 0!==s?s:t.data.channel)&&(this.state._isSubscribed?(super.handleEvent(e,t),this.state.subscriptions.length>0&&this.state.client.logger.trace(this.subscriptionType,`Notify ${this.id} subscription set subscriptions (count: ${this.state.subscriptions.length}) about received event.`),this.state.subscriptions.forEach((s=>s.handleEvent(e,t)))):this.state.client.logger.trace(this.subscriptionType,`Subscription set ${this.id} is not subscribed. Ignoring event.`))}subscriptionInput(e=!1){let t=this.state.subscriptionInput;return this.state.subscriptions.forEach((s=>{e&&s.state.entity.subscriptionsCount>0&&(t=t.without(s.state.subscriptionInput))})),t}cloneEmpty(){return new _t({state:this.state})}dispose(){const e=this.state.isLastClone;this.state.subscriptions.forEach((t=>{t.removeParentSet(this),e&&t.state.removeParentState(this.state)})),super.dispose()}invalidate(e=!1){(e?this.state.subscriptions.slice(0):this.state.subscriptions).forEach((t=>{e&&(t.state.entity.decreaseSubscriptionCount(this.state.id),t.removeParentSet(this)),t.invalidate(e)})),e&&this.state.removeAllSubscriptions(),super.invalidate()}addSubscription(e){this.addSubscriptions([e])}addSubscriptions(e){const t=[],s=[];this.state.client.logger.debug(this.subscriptionType,(()=>{const t=[],s=[];return e.forEach((e=>{this.state.subscriptions.includes(e)?t.push(e):s.push(e)})),{messageType:"object",details:`Add subscriptions to ${this.id} (subscriptions count: ${this.state.subscriptions.length+s.length}):`,message:{addedSubscriptions:s,ignoredSubscriptions:t}}})),e.filter((e=>!this.state.subscriptions.includes(e))).forEach((e=>{e.state.isSubscribed?s.push(e):t.push(e),e.addParentSet(this),this.state.addSubscription(e)})),0===s.length&&0===t.length||!this.state.isSubscribed||(s.forEach((({state:e})=>e.entity.increaseSubscriptionCount(this.state.id))),t.length>0&&this.updateSubscription({subscribing:!0,subscriptions:t}))}removeSubscription(e){this.removeSubscriptions([e])}removeSubscriptions(e){const t=[];this.state.client.logger.debug(this.subscriptionType,(()=>{const t=[],s=[];return e.forEach((e=>{this.state.subscriptions.includes(e)?s.push(e):t.push(e)})),{messageType:"object",details:`Remove subscriptions from ${this.id} (subscriptions count: ${this.state.subscriptions.length}):`,message:{removedSubscriptions:s,ignoredSubscriptions:t}}})),e.filter((e=>this.state.subscriptions.includes(e))).forEach((e=>{e.state.isSubscribed&&t.push(e),e.removeParentSet(this),this.state.removeSubscription(e,e.parentSetsCount>1)})),0!==t.length&&this.state.isSubscribed&&this.updateSubscription({subscribing:!1,subscriptions:t})}addSubscriptionSet(e){this.addSubscriptions(e.subscriptions)}removeSubscriptionSet(e){this.removeSubscriptions(e.subscriptions)}register(e){var t;const s=null!==(t=e.subscriptions)&&void 0!==t?t:this.state.subscriptions;s.forEach((({state:e})=>e.entity.increaseSubscriptionCount(this.state.id))),this.state.client.logger.trace(this.subscriptionType,(()=>({messageType:"text",message:`Register subscription for real-time events: ${this}`}))),this.state.client.registerEventHandleCapable(this,e.cursor,s)}unregister(e){const t=null!=e?e:this.state.subscriptions;t.forEach((({state:e})=>e.entity.decreaseSubscriptionCount(this.state.id))),this.state.client.logger.trace(this.subscriptionType,(()=>e?{messageType:"object",message:{subscription:this,subscriptions:e},details:"Unregister subscriptions of subscription set from real-time events:"}:{messageType:"text",message:`Unregister subscription from real-time events: ${this}`})),this.state.client.unregisterEventHandleCapable(this,t)}toString(){const e=this.state;return`${this.subscriptionType} { id: ${this.id}, stateId: ${e.id}, clonesCount: ${Object.keys(this.state.clones).length}, isSubscribed: ${e.isSubscribed}, subscriptions: [${e.subscriptions.map((e=>e.toString())).join(", ")}] }`}}class It extends Et{constructor(e){var t,s;const n=e.entity.subscriptionNames(null!==(s=null===(t=e.options)||void 0===t?void 0:t.receivePresenceEvents)&&void 0!==s&&s),r=new jt({[e.entity.subscriptionType==Pt.Channel?"channels":"channelGroups"]:n});super(e.client,r,e.options,e.client.subscriptionTimetoken),this.entity=e.entity}}class Mt extends Nt{constructor(e){"client"in e?e.client.logger.debug("Subscription",(()=>({messageType:"object",details:"Create subscription with parameters:",message:Object.assign({entity:e.entity},e.options?e.options:{})}))):e.state.client.logger.debug("Subscription","Create subscription clone"),super("state"in e?e.state:new It(e)),this.parents=[],this.handledUpdates=[],this.state.storeClone(this.id,this)}get state(){return super.state}get parentSetsCount(){return this.parents.length}handleEvent(e,t){var s,n;if(this.state.isSubscribed&&this.state.subscriptionInput.contains(null!==(s=t.data.subscription)&&void 0!==s?s:t.data.channel)){if(this.parentSetsCount>0){const e=B(t.data);if(this.handledUpdates.includes(e))return void this.state.client.logger.trace(this.subscriptionType,`Event (${e}) already handled by ${this.id}. Ignoring.`);this.handledUpdates.push(e),this.handledUpdates.length>10&&this.handledUpdates.shift()}this.state.subscriptionInput.contains(null!==(n=t.data.subscription)&&void 0!==n?n:t.data.channel)&&super.handleEvent(e,t)}}subscriptionInput(e=!1){return e&&this.state.entity.subscriptionsCount>0?new jt({}):this.state.subscriptionInput}cloneEmpty(){return new Mt({state:this.state})}dispose(){this.parentSetsCount>0?this.state.client.logger.debug(this.subscriptionType,(()=>({messageType:"text",message:`'${this.state.entity.subscriptionNames()}' subscription still in use. Ignore dispose request.`}))):(this.handledUpdates.splice(0,this.handledUpdates.length),super.dispose())}invalidate(e=!1){e&&this.state.entity.decreaseSubscriptionCount(this.state.id),this.handledUpdates.splice(0,this.handledUpdates.length),super.invalidate(e)}addParentSet(e){this.parents.includes(e)||(this.parents.push(e),this.state.client.logger.trace(this.subscriptionType,`Add parent subscription set for ${this.id}: ${e.id}. Parent subscription set count: ${this.parentSetsCount}`))}removeParentSet(e){const t=this.parents.indexOf(e);-1!==t&&(this.parents.splice(t,1),this.state.client.logger.trace(this.subscriptionType,`Remove parent subscription set from ${this.id}: ${e.id}. Parent subscription set count: ${this.parentSetsCount}`)),0===this.parentSetsCount&&this.handledUpdates.splice(0,this.handledUpdates.length)}addSubscription(e){this.state.client.logger.debug(this.subscriptionType,(()=>({messageType:"text",message:`Create set with subscription: ${e}`})));const t=new _t({client:this.state.client,subscriptions:[this,e],options:this.state.options});return this.state.isSubscribed||e.state.isSubscribed?(this.state.client.logger.trace(this.subscriptionType,"Subscribe resulting set because the receiver is already subscribed."),t.subscribe(),t):t}register(e){this.state.entity.increaseSubscriptionCount(this.state.id),this.state.client.logger.trace(this.subscriptionType,(()=>({messageType:"text",message:`Register subscription for real-time events: ${this}`}))),this.state.client.registerEventHandleCapable(this,e.cursor)}unregister(e){this.state.entity.decreaseSubscriptionCount(this.state.id),this.state.client.logger.trace(this.subscriptionType,(()=>({messageType:"text",message:`Unregister subscription from real-time events: ${this}`}))),this.handledUpdates.splice(0,this.handledUpdates.length),this.state.client.unregisterEventHandleCapable(this)}toString(){const e=this.state;return`${this.subscriptionType} { id: ${this.id}, stateId: ${e.id}, entity: ${e.entity.subscriptionNames(!1).pop()}, clonesCount: ${Object.keys(e.clones).length}, isSubscribed: ${e.isSubscribed}, parentSetsCount: ${this.parentSetsCount}, cursor: ${e.cursor?e.cursor.timetoken:"not set"}, referenceTimetoken: ${e.referenceTimetoken?e.referenceTimetoken:"not set"} }`}}class At extends le{constructor(e){var t,s,n,r;super(),this.parameters=e,null!==(t=(n=this.parameters).channels)&&void 0!==t||(n.channels=[]),null!==(s=(r=this.parameters).channelGroups)&&void 0!==s||(r.channelGroups=[])}operation(){return M.PNGetStateOperation}validate(){const{keySet:{subscribeKey:e},channels:t,channelGroups:s}=this.parameters;if(!e)return"Missing Subscribe Key"}parse(e){return i(this,void 0,void 0,(function*(){const t=this.deserializeResponse(e),{channels:s=[],channelGroups:n=[]}=this.parameters,r={channels:{}};return 1===s.length&&0===n.length?r.channels[s[0]]=t.payload:r.channels=t.payload,r}))}get path(){const{keySet:{subscribeKey:e},uuid:t,channels:s}=this.parameters;return`/v2/presence/sub-key/${e}/channel/${$(null!=s?s:[],",")}/uuid/${t}`}get queryParameters(){const{channelGroups:e}=this.parameters;return e&&0!==e.length?{"channel-group":e.join(",")}:{}}}class Ut extends le{constructor(e){super(),this.parameters=e}operation(){return M.PNSetStateOperation}validate(){const{keySet:{subscribeKey:e},state:t,channels:s=[],channelGroups:n=[]}=this.parameters;return e?t?0===(null==s?void 0:s.length)&&0===(null==n?void 0:n.length)?"Please provide a list of channels and/or channel-groups":void 0:"Missing State":"Missing Subscribe Key"}parse(e){return i(this,void 0,void 0,(function*(){return{state:this.deserializeResponse(e).payload}}))}get path(){const{keySet:{subscribeKey:e},uuid:t,channels:s}=this.parameters;return`/v2/presence/sub-key/${e}/channel/${$(null!=s?s:[],",")}/uuid/${R(t)}/data`}get queryParameters(){const{channelGroups:e,state:t}=this.parameters,s={state:JSON.stringify(t)};return e&&0!==e.length&&(s["channel-group"]=e.join(",")),s}}class Dt extends le{constructor(e){super({cancellable:!0}),this.parameters=e}operation(){return M.PNHeartbeatOperation}validate(){const{keySet:{subscribeKey:e},channels:t=[],channelGroups:s=[]}=this.parameters;return e?0===t.length&&0===s.length?"Please provide a list of channels and/or channel-groups":void 0:"Missing Subscribe Key"}parse(e){const t=Object.create(null,{parse:{get:()=>super.parse}});return i(this,void 0,void 0,(function*(){return t.parse.call(this,e).then((e=>({})))}))}get path(){const{keySet:{subscribeKey:e},channels:t}=this.parameters;return`/v2/presence/sub-key/${e}/channel/${$(null!=t?t:[],",")}/heartbeat`}get queryParameters(){const{channelGroups:e,state:t,heartbeat:s}=this.parameters,n={heartbeat:`${s}`};return e&&0!==e.length&&(n["channel-group"]=e.join(",")),t&&(n.state=JSON.stringify(t)),n}}class Ft extends le{constructor(e){super(),this.parameters=e,this.parameters.channelGroups&&(this.parameters.channelGroups=Array.from(new Set(this.parameters.channelGroups))),this.parameters.channels&&(this.parameters.channels=Array.from(new Set(this.parameters.channels)))}operation(){return M.PNUnsubscribeOperation}validate(){const{keySet:{subscribeKey:e},channels:t=[],channelGroups:s=[]}=this.parameters;return e?0===t.length&&0===s.length?"At least one `channel` or `channel group` should be provided.":void 0:"Missing Subscribe Key"}parse(e){const t=Object.create(null,{parse:{get:()=>super.parse}});return i(this,void 0,void 0,(function*(){return t.parse.call(this,e).then((e=>({})))}))}get path(){var e;const{keySet:{subscribeKey:t},channels:s}=this.parameters;return`/v2/presence/sub-key/${t}/channel/${$(null!==(e=null==s?void 0:s.sort())&&void 0!==e?e:[],",")}/leave`}get queryParameters(){const{channelGroups:e}=this.parameters;return e&&0!==e.length?{"channel-group":e.sort().join(",")}:{}}}class Rt extends le{constructor(e){super(),this.parameters=e}operation(){return M.PNWhereNowOperation}validate(){if(!this.parameters.keySet.subscribeKey)return"Missing Subscribe Key"}parse(e){return i(this,void 0,void 0,(function*(){const t=this.deserializeResponse(e);return t.payload?{channels:t.payload.channels}:{channels:[]}}))}get path(){const{keySet:{subscribeKey:e},uuid:t}=this.parameters;return`/v2/presence/sub-key/${e}/uuid/${R(t)}`}}class $t extends le{constructor(e){var t,s,n,r,i,a;super(),this.parameters=e,null!==(t=(r=this.parameters).queryParameters)&&void 0!==t||(r.queryParameters={}),null!==(s=(i=this.parameters).includeUUIDs)&&void 0!==s||(i.includeUUIDs=true),null!==(n=(a=this.parameters).includeState)&&void 0!==n||(a.includeState=false)}operation(){const{channels:e=[],channelGroups:t=[]}=this.parameters;return 0===e.length&&0===t.length?M.PNGlobalHereNowOperation:M.PNHereNowOperation}validate(){if(!this.parameters.keySet.subscribeKey)return"Missing Subscribe Key"}parse(e){return i(this,void 0,void 0,(function*(){var t,s;const n=this.deserializeResponse(e),r="occupancy"in n?1:n.payload.total_channels,i="occupancy"in n?n.occupancy:n.payload.total_occupancy,a={};let o={};if("occupancy"in n){const e=this.parameters.channels[0];o[e]={uuids:null!==(t=n.uuids)&&void 0!==t?t:[],occupancy:i}}else o=null!==(s=n.payload.channels)&&void 0!==s?s:{};return Object.keys(o).forEach((e=>{const t=o[e];a[e]={occupants:this.parameters.includeUUIDs?t.uuids.map((e=>"string"==typeof e?{uuid:e,state:null}:e)):[],name:e,occupancy:t.occupancy}})),{totalChannels:r,totalOccupancy:i,channels:a}}))}get path(){const{keySet:{subscribeKey:e},channels:t,channelGroups:s}=this.parameters;let n=`/v2/presence/sub-key/${e}`;return(t&&t.length>0||s&&s.length>0)&&(n+=`/channel/${$(null!=t?t:[],",")}`),n}get queryParameters(){const{channelGroups:e,includeUUIDs:t,includeState:s,queryParameters:n}=this.parameters;return Object.assign(Object.assign(Object.assign(Object.assign({},t?{}:{disable_uuids:"1"}),null!=s&&s?{state:"1"}:{}),e&&e.length>0?{"channel-group":e.join(",")}:{}),n)}}class xt extends le{constructor(e){super({method:ae.DELETE}),this.parameters=e}operation(){return M.PNDeleteMessagesOperation}validate(){return this.parameters.keySet.subscribeKey?this.parameters.channel?void 0:"Missing channel":"Missing Subscribe Key"}parse(e){const t=Object.create(null,{parse:{get:()=>super.parse}});return i(this,void 0,void 0,(function*(){return t.parse.call(this,e).then((e=>({})))}))}get path(){const{keySet:{subscribeKey:e},channel:t}=this.parameters;return`/v3/history/sub-key/${e}/channel/${R(t)}`}get queryParameters(){const{start:e,end:t}=this.parameters;return Object.assign(Object.assign({},e?{start:e}:{}),t?{end:t}:{})}}class Lt extends le{constructor(e){super(),this.parameters=e}operation(){return M.PNMessageCounts}validate(){const{keySet:{subscribeKey:e},channels:t,timetoken:s,channelTimetokens:n}=this.parameters;return e?t?s&&n?"`timetoken` and `channelTimetokens` are incompatible together":s||n?n&&n.length>1&&n.length!==t.length?"Length of `channelTimetokens` and `channels` do not match":void 0:"`timetoken` or `channelTimetokens` need to be set":"Missing channels":"Missing Subscribe Key"}parse(e){return i(this,void 0,void 0,(function*(){return{channels:this.deserializeResponse(e).channels}}))}get path(){return`/v3/history/sub-key/${this.parameters.keySet.subscribeKey}/message-counts/${$(this.parameters.channels)}`}get queryParameters(){let{channelTimetokens:e}=this.parameters;return this.parameters.timetoken&&(e=[this.parameters.timetoken]),Object.assign(Object.assign({},1===e.length?{timetoken:e[0]}:{}),e.length>1?{channelsTimetoken:e.join(",")}:{})}}class qt extends le{constructor(e){var t,s,n;super(),this.parameters=e,e.count?e.count=Math.min(e.count,100):e.count=100,null!==(t=e.stringifiedTimeToken)&&void 0!==t||(e.stringifiedTimeToken=false),null!==(s=e.includeMeta)&&void 0!==s||(e.includeMeta=false),null!==(n=e.logVerbosity)&&void 0!==n||(e.logVerbosity=false)}operation(){return M.PNHistoryOperation}validate(){return this.parameters.keySet.subscribeKey?this.parameters.channel?void 0:"Missing channel":"Missing Subscribe Key"}parse(e){return i(this,void 0,void 0,(function*(){const t=this.deserializeResponse(e),s=t[0],n=t[1],r=t[2];return Array.isArray(s)?{messages:s.map((e=>{const t=this.processPayload(e.message),s={entry:t.payload,timetoken:e.timetoken};return t.error&&(s.error=t.error),e.meta&&(s.meta=e.meta),s})),startTimeToken:n,endTimeToken:r}:{messages:[],startTimeToken:n,endTimeToken:r}}))}get path(){const{keySet:{subscribeKey:e},channel:t}=this.parameters;return`/v2/history/sub-key/${e}/channel/${R(t)}`}get queryParameters(){const{start:e,end:t,reverse:s,count:n,stringifiedTimeToken:r,includeMeta:i}=this.parameters;return Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({count:n,include_token:"true"},e?{start:e}:{}),t?{end:t}:{}),r?{string_message_token:"true"}:{}),null!=s?{reverse:s.toString()}:{}),i?{include_meta:"true"}:{})}processPayload(e){const{crypto:t,logVerbosity:s}=this.parameters;if(!t||"string"!=typeof e)return{payload:e};let n,r;try{const s=t.decrypt(e);n=s instanceof ArrayBuffer?JSON.parse(qt.decoder.decode(s)):s}catch(t){s&&console.log("decryption error",t.message),n=e,r=`Error while decrypting message content: ${t.message}`}return{payload:n,error:r}}}var Gt;!function(e){e[e.Message=-1]="Message",e[e.Files=4]="Files"}(Gt||(Gt={}));class Kt extends le{constructor(e){var t,s,n,r,i;super(),this.parameters=e;const a=null!==(t=e.includeMessageActions)&&void 0!==t&&t,o=e.channels.length>1||a?25:100;e.count?e.count=Math.min(e.count,o):e.count=o,e.includeUuid?e.includeUUID=e.includeUuid:null!==(s=e.includeUUID)&&void 0!==s||(e.includeUUID=true),null!==(n=e.stringifiedTimeToken)&&void 0!==n||(e.stringifiedTimeToken=false),null!==(r=e.includeMessageType)&&void 0!==r||(e.includeMessageType=true),null!==(i=e.logVerbosity)&&void 0!==i||(e.logVerbosity=false)}operation(){return M.PNFetchMessagesOperation}validate(){const{keySet:{subscribeKey:e},channels:t,includeMessageActions:s}=this.parameters;return e?t?void 0!==s&&s&&t.length>1?"History can return actions data for a single channel only. Either pass a single channel or disable the includeMessageActions flag.":void 0:"Missing channels":"Missing Subscribe Key"}parse(e){return i(this,void 0,void 0,(function*(){var t;const s=this.deserializeResponse(e),n=null!==(t=s.channels)&&void 0!==t?t:{},r={};return Object.keys(n).forEach((e=>{r[e]=n[e].map((t=>{null===t.message_type&&(t.message_type=Gt.Message);const s=this.processPayload(e,t),n=Object.assign(Object.assign({channel:e,timetoken:t.timetoken,message:s.payload,messageType:t.message_type},t.custom_message_type?{customMessageType:t.custom_message_type}:{}),{uuid:t.uuid});if(t.actions){const e=n;e.actions=t.actions,e.data=t.actions}return t.meta&&(n.meta=t.meta),s.error&&(n.error=s.error),n}))})),s.more?{channels:r,more:s.more}:{channels:r}}))}get path(){const{keySet:{subscribeKey:e},channels:t,includeMessageActions:s}=this.parameters;return`/v3/${s?"history-with-actions":"history"}/sub-key/${e}/channel/${$(t)}`}get queryParameters(){const{start:e,end:t,count:s,includeCustomMessageType:n,includeMessageType:r,includeMeta:i,includeUUID:a,stringifiedTimeToken:o}=this.parameters;return Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({max:s},e?{start:e}:{}),t?{end:t}:{}),o?{string_message_token:"true"}:{}),void 0!==i&&i?{include_meta:"true"}:{}),a?{include_uuid:"true"}:{}),null!=n?{include_custom_message_type:n?"true":"false"}:{}),r?{include_message_type:"true"}:{})}processPayload(e,t){const{crypto:s,logVerbosity:n}=this.parameters;if(!s||"string"!=typeof t.message)return{payload:t.message};let r,i;try{const e=s.decrypt(t.message);r=e instanceof ArrayBuffer?JSON.parse(Kt.decoder.decode(e)):e}catch(e){n&&console.log("decryption error",e.message),r=t.message,i=`Error while decrypting message content: ${e.message}`}if(!i&&r&&t.message_type==Gt.Files&&"object"==typeof r&&this.isFileMessage(r)){const t=r;return{payload:{message:t.message,file:Object.assign(Object.assign({},t.file),{url:this.parameters.getFileUrl({channel:e,id:t.file.id,name:t.file.name})})},error:i}}return{payload:r,error:i}}isFileMessage(e){return void 0!==e.file}}class Ht extends le{constructor(e){super(),this.parameters=e}operation(){return M.PNGetMessageActionsOperation}validate(){return this.parameters.keySet.subscribeKey?this.parameters.channel?void 0:"Missing message channel":"Missing Subscribe Key"}parse(e){return i(this,void 0,void 0,(function*(){const t=this.deserializeResponse(e);let s=null,n=null;return t.data.length>0&&(s=t.data[0].actionTimetoken,n=t.data[t.data.length-1].actionTimetoken),{data:t.data,more:t.more,start:s,end:n}}))}get path(){const{keySet:{subscribeKey:e},channel:t}=this.parameters;return`/v1/message-actions/${e}/channel/${R(t)}`}get queryParameters(){const{limit:e,start:t,end:s}=this.parameters;return Object.assign(Object.assign(Object.assign({},t?{start:t}:{}),s?{end:s}:{}),e?{limit:e}:{})}}class Bt extends le{constructor(e){super({method:ae.POST}),this.parameters=e}operation(){return M.PNAddMessageActionOperation}validate(){const{keySet:{subscribeKey:e},action:t,channel:s,messageTimetoken:n}=this.parameters;return e?s?n?t?t.value?t.type?t.type.length>15?"Action.type value exceed maximum length of 15":void 0:"Missing Action.type":"Missing Action.value":"Missing Action":"Missing message timetoken":"Missing message channel":"Missing Subscribe Key"}parse(e){const t=Object.create(null,{parse:{get:()=>super.parse}});return i(this,void 0,void 0,(function*(){return t.parse.call(this,e).then((({data:e})=>({data:e})))}))}get path(){const{keySet:{subscribeKey:e},channel:t,messageTimetoken:s}=this.parameters;return`/v1/message-actions/${e}/channel/${R(t)}/message/${s}`}get headers(){var e;return Object.assign(Object.assign({},null!==(e=super.headers)&&void 0!==e?e:{}),{"Content-Type":"application/json"})}get body(){return JSON.stringify(this.parameters.action)}}class Wt extends le{constructor(e){super({method:ae.DELETE}),this.parameters=e}operation(){return M.PNRemoveMessageActionOperation}validate(){const{keySet:{subscribeKey:e},channel:t,messageTimetoken:s,actionTimetoken:n}=this.parameters;return e?t?s?n?void 0:"Missing action timetoken":"Missing message timetoken":"Missing message action channel":"Missing Subscribe Key"}parse(e){const t=Object.create(null,{parse:{get:()=>super.parse}});return i(this,void 0,void 0,(function*(){return t.parse.call(this,e).then((({data:e})=>({data:e})))}))}get path(){const{keySet:{subscribeKey:e},channel:t,actionTimetoken:s,messageTimetoken:n}=this.parameters;return`/v1/message-actions/${e}/channel/${R(t)}/message/${n}/action/${s}`}}class zt extends le{constructor(e){var t,s;super(),this.parameters=e,null!==(t=(s=this.parameters).storeInHistory)&&void 0!==t||(s.storeInHistory=true)}operation(){return M.PNPublishFileMessageOperation}validate(){const{channel:e,fileId:t,fileName:s}=this.parameters;return e?t?s?void 0:"file name can't be empty":"file id can't be empty":"channel can't be empty"}parse(e){return i(this,void 0,void 0,(function*(){return{timetoken:this.deserializeResponse(e)[2]}}))}get path(){const{message:e,channel:t,keySet:{publishKey:s,subscribeKey:n},fileId:r,fileName:i}=this.parameters,a=Object.assign({file:{name:i,id:r}},e?{message:e}:{});return`/v1/files/publish-file/${s}/${n}/0/${R(t)}/0/${R(this.prepareMessagePayload(a))}`}get queryParameters(){const{customMessageType:e,storeInHistory:t,ttl:s,meta:n}=this.parameters;return Object.assign(Object.assign(Object.assign({store:t?"1":"0"},e?{custom_message_type:e}:{}),s?{ttl:s}:{}),n&&"object"==typeof n?{meta:JSON.stringify(n)}:{})}prepareMessagePayload(e){const{crypto:t}=this.parameters;if(!t)return JSON.stringify(e)||"";const s=t.encrypt(JSON.stringify(e));return JSON.stringify("string"==typeof s?s:u(s))}}class Vt extends le{constructor(e){super({method:ae.LOCAL}),this.parameters=e}operation(){return M.PNGetFileUrlOperation}validate(){const{channel:e,id:t,name:s}=this.parameters;return e?t?s?void 0:"file name can't be empty":"file id can't be empty":"channel can't be empty"}parse(e){return i(this,void 0,void 0,(function*(){return e.url}))}get path(){const{channel:e,id:t,name:s,keySet:{subscribeKey:n}}=this.parameters;return`/v1/files/${n}/channels/${R(e)}/files/${t}/${s}`}}class Jt extends le{constructor(e){super({method:ae.DELETE}),this.parameters=e}operation(){return M.PNDeleteFileOperation}validate(){const{channel:e,id:t,name:s}=this.parameters;return e?t?s?void 0:"file name can't be empty":"file id can't be empty":"channel can't be empty"}get path(){const{keySet:{subscribeKey:e},id:t,channel:s,name:n}=this.parameters;return`/v1/files/${e}/channels/${R(s)}/files/${t}/${n}`}}class Xt extends le{constructor(e){var t,s;super(),this.parameters=e,null!==(t=(s=this.parameters).limit)&&void 0!==t||(s.limit=100)}operation(){return M.PNListFilesOperation}validate(){if(!this.parameters.channel)return"channel can't be empty"}get path(){const{keySet:{subscribeKey:e},channel:t}=this.parameters;return`/v1/files/${e}/channels/${R(t)}/files`}get queryParameters(){const{limit:e,next:t}=this.parameters;return Object.assign({limit:e},t?{next:t}:{})}}class Qt extends le{constructor(e){super({method:ae.POST}),this.parameters=e}operation(){return M.PNGenerateUploadUrlOperation}validate(){return this.parameters.channel?this.parameters.name?void 0:"'name' can't be empty":"channel can't be empty"}parse(e){return i(this,void 0,void 0,(function*(){const t=this.deserializeResponse(e);return{id:t.data.id,name:t.data.name,url:t.file_upload_request.url,formFields:t.file_upload_request.form_fields}}))}get path(){const{keySet:{subscribeKey:e},channel:t}=this.parameters;return`/v1/files/${e}/channels/${R(t)}/generate-upload-url`}get headers(){var e;return Object.assign(Object.assign({},null!==(e=super.headers)&&void 0!==e?e:{}),{"Content-Type":"application/json"})}get body(){return JSON.stringify({name:this.parameters.name})}}class Yt extends le{constructor(e){super({method:ae.POST}),this.parameters=e;const t=e.file.mimeType;t&&(e.formFields=e.formFields.map((e=>"Content-Type"===e.name?{name:e.name,value:t}:e)))}operation(){return M.PNPublishFileOperation}validate(){const{fileId:e,fileName:t,file:s,uploadUrl:n}=this.parameters;return e?t?s?n?void 0:"Validation failed: file upload 'url' can't be empty":"Validation failed: 'file' can't be empty":"Validation failed: file 'name' can't be empty":"Validation failed: file 'id' can't be empty"}parse(e){return i(this,void 0,void 0,(function*(){return{status:e.status,message:e.body?Yt.decoder.decode(e.body):"OK"}}))}request(){return Object.assign(Object.assign({},super.request()),{origin:new URL(this.parameters.uploadUrl).origin,timeout:300})}get path(){const{pathname:e,search:t}=new URL(this.parameters.uploadUrl);return`${e}${t}`}get body(){return this.parameters.file}get formData(){return this.parameters.formFields}}class Zt{constructor(e){var t;if(this.parameters=e,this.file=null===(t=this.parameters.PubNubFile)||void 0===t?void 0:t.create(e.file),!this.file)throw new Error("File upload error: unable to create File object.")}process(){return i(this,void 0,void 0,(function*(){let e,t;return this.generateFileUploadUrl().then((s=>(e=s.name,t=s.id,this.uploadFile(s)))).then((e=>{if(204!==e.status)throw new d("Upload to bucket was unsuccessful",{error:!0,statusCode:e.status,category:h.PNUnknownCategory,operation:M.PNPublishFileOperation,errorData:{message:e.message}})})).then((()=>this.publishFileMessage(t,e))).catch((e=>{if(e instanceof d)throw e;const t=e instanceof I?e:I.create(e);throw new d("File upload error.",t.toStatus(M.PNPublishFileOperation))}))}))}generateFileUploadUrl(){return i(this,void 0,void 0,(function*(){const e=new Qt(Object.assign(Object.assign({},this.parameters),{name:this.file.name,keySet:this.parameters.keySet}));return this.parameters.sendRequest(e)}))}uploadFile(e){return i(this,void 0,void 0,(function*(){const{cipherKey:t,PubNubFile:s,crypto:n,cryptography:r}=this.parameters,{id:i,name:a,url:o,formFields:c}=e;return this.parameters.PubNubFile.supportsEncryptFile&&(!t&&n?this.file=yield n.encryptFile(this.file,s):t&&r&&(this.file=yield r.encryptFile(t,this.file,s))),this.parameters.sendRequest(new Yt({fileId:i,fileName:a,file:this.file,uploadUrl:o,formFields:c}))}))}publishFileMessage(e,t){return i(this,void 0,void 0,(function*(){var s,n,r,i;let a,o={timetoken:"0"},c=this.parameters.fileUploadPublishRetryLimit,u=!1;do{try{o=yield this.parameters.publishFile(Object.assign(Object.assign({},this.parameters),{fileId:e,fileName:t})),u=!0}catch(e){e instanceof d&&(a=e),c-=1}}while(!u&&c>0);if(u)return{status:200,timetoken:o.timetoken,id:e,name:t};throw new d("Publish failed. You may want to execute that operation manually using pubnub.publishFile",{error:!0,category:null!==(n=null===(s=a.status)||void 0===s?void 0:s.category)&&void 0!==n?n:h.PNUnknownCategory,statusCode:null!==(i=null===(r=a.status)||void 0===r?void 0:r.statusCode)&&void 0!==i?i:0,channel:this.parameters.channel,id:e,name:t})}))}}class es{constructor(e,t){this.subscriptionStateIds=[],this.client=t,this._nameOrId=e}get entityType(){return"Channel"}get subscriptionType(){return Pt.Channel}subscriptionNames(e){return[this._nameOrId,...e&&!this._nameOrId.endsWith("-pnpres")?[`${this._nameOrId}-pnpres`]:[]]}subscription(e){return new Mt({client:this.client,entity:this,options:e})}get subscriptionsCount(){return this.subscriptionStateIds.length}increaseSubscriptionCount(e){this.subscriptionStateIds.includes(e)||this.subscriptionStateIds.push(e)}decreaseSubscriptionCount(e){{const t=this.subscriptionStateIds.indexOf(e);t>=0&&this.subscriptionStateIds.splice(t,1)}}toString(){return`${this.entityType} { nameOrId: ${this._nameOrId}, subscriptionsCount: ${this.subscriptionsCount} }`}}class ts extends es{get entityType(){return"ChannelMetadata"}get id(){return this._nameOrId}subscriptionNames(e){return[this.id]}}class ss extends es{get entityType(){return"ChannelGroups"}get name(){return this._nameOrId}get subscriptionType(){return Pt.ChannelGroup}}class ns extends es{get entityType(){return"UserMetadata"}get id(){return this._nameOrId}subscriptionNames(e){return[this.id]}}class rs extends es{get entityType(){return"Channel"}get name(){return this._nameOrId}}class is extends le{constructor(e){super(),this.parameters=e}operation(){return M.PNRemoveChannelsFromGroupOperation}validate(){const{keySet:{subscribeKey:e},channels:t,channelGroup:s}=this.parameters;return e?s?t?void 0:"Missing channels":"Missing Channel Group":"Missing Subscribe Key"}parse(e){const t=Object.create(null,{parse:{get:()=>super.parse}});return i(this,void 0,void 0,(function*(){return t.parse.call(this,e).then((e=>({})))}))}get path(){const{keySet:{subscribeKey:e},channelGroup:t}=this.parameters;return`/v1/channel-registration/sub-key/${e}/channel-group/${R(t)}`}get queryParameters(){return{remove:this.parameters.channels.join(",")}}}class as extends le{constructor(e){super(),this.parameters=e}operation(){return M.PNAddChannelsToGroupOperation}validate(){const{keySet:{subscribeKey:e},channels:t,channelGroup:s}=this.parameters;return e?s?t?void 0:"Missing channels":"Missing Channel Group":"Missing Subscribe Key"}parse(e){const t=Object.create(null,{parse:{get:()=>super.parse}});return i(this,void 0,void 0,(function*(){return t.parse.call(this,e).then((e=>({})))}))}get path(){const{keySet:{subscribeKey:e},channelGroup:t}=this.parameters;return`/v1/channel-registration/sub-key/${e}/channel-group/${R(t)}`}get queryParameters(){return{add:this.parameters.channels.join(",")}}}class os extends le{constructor(e){super(),this.parameters=e}operation(){return M.PNChannelsForGroupOperation}validate(){return this.parameters.keySet.subscribeKey?this.parameters.channelGroup?void 0:"Missing Channel Group":"Missing Subscribe Key"}parse(e){return i(this,void 0,void 0,(function*(){return{channels:this.deserializeResponse(e).payload.channels}}))}get path(){const{keySet:{subscribeKey:e},channelGroup:t}=this.parameters;return`/v1/channel-registration/sub-key/${e}/channel-group/${R(t)}`}}class cs extends le{constructor(e){super(),this.parameters=e}operation(){return M.PNRemoveGroupOperation}validate(){return this.parameters.keySet.subscribeKey?this.parameters.channelGroup?void 0:"Missing Channel Group":"Missing Subscribe Key"}parse(e){const t=Object.create(null,{parse:{get:()=>super.parse}});return i(this,void 0,void 0,(function*(){return t.parse.call(this,e).then((e=>({})))}))}get path(){const{keySet:{subscribeKey:e},channelGroup:t}=this.parameters;return`/v1/channel-registration/sub-key/${e}/channel-group/${R(t)}/remove`}}class us extends le{constructor(e){super(),this.parameters=e}operation(){return M.PNChannelGroupsOperation}validate(){if(!this.parameters.keySet.subscribeKey)return"Missing Subscribe Key"}parse(e){return i(this,void 0,void 0,(function*(){return{groups:this.deserializeResponse(e).payload.groups}}))}get path(){return`/v1/channel-registration/sub-key/${this.parameters.keySet.subscribeKey}/channel-group`}}class ls{constructor(e,t,s){this.sendRequest=s,this.logger=e,this.keySet=t}listChannels(e,t){return i(this,void 0,void 0,(function*(){this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"List channel group channels with parameters:"})));const s=new os(Object.assign(Object.assign({},e),{keySet:this.keySet})),n=e=>{e&&this.logger.debug("PubNub",`List channel group channels success. Received ${e.channels.length} channels.`)};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}))}listGroups(e){return i(this,void 0,void 0,(function*(){this.logger.debug("PubNub","List all channel groups.");const t=new us({keySet:this.keySet}),s=e=>{e&&this.logger.debug("PubNub",`List all channel groups success. Received ${e.groups.length} groups.`)};return e?this.sendRequest(t,((t,n)=>{s(n),e(t,n)})):this.sendRequest(t).then((e=>(s(e),e)))}))}addChannels(e,t){return i(this,void 0,void 0,(function*(){this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Add channels to the channel group with parameters:"})));const s=new as(Object.assign(Object.assign({},e),{keySet:this.keySet})),n=()=>{this.logger.debug("PubNub","Add channels to the channel group success.")};return t?this.sendRequest(s,(e=>{e.error||n(),t(e)})):this.sendRequest(s).then((e=>(n(),e)))}))}removeChannels(e,t){return i(this,void 0,void 0,(function*(){this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Remove channels from the channel group with parameters:"})));const s=new is(Object.assign(Object.assign({},e),{keySet:this.keySet})),n=()=>{this.logger.debug("PubNub","Remove channels from the channel group success.")};return t?this.sendRequest(s,(e=>{e.error||n(),t(e)})):this.sendRequest(s).then((e=>(n(),e)))}))}deleteGroup(e,t){return i(this,void 0,void 0,(function*(){this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Remove a channel group with parameters:"})));const s=new cs(Object.assign(Object.assign({},e),{keySet:this.keySet})),n=()=>{this.logger.debug("PubNub",`Remove a channel group success. Removed '${e.channelGroup}' channel group.'`)};return t?this.sendRequest(s,(e=>{e.error||n(),t(e)})):this.sendRequest(s).then((e=>(n(),e)))}))}}class hs extends le{constructor(e){var t,s;super(),this.parameters=e,"apns2"===this.parameters.pushGateway&&(null!==(t=(s=this.parameters).environment)&&void 0!==t||(s.environment="development")),this.parameters.count&&this.parameters.count>1e3&&(this.parameters.count=1e3)}operation(){throw Error("Should be implemented in subclass.")}validate(){const{keySet:{subscribeKey:e},action:t,device:s,pushGateway:n}=this.parameters;return e?s?"add"!==t&&"remove"!==t||"channels"in this.parameters&&0!==this.parameters.channels.length?n?"apns2"!==this.parameters.pushGateway||this.parameters.topic?void 0:"Missing APNS2 topic":"Missing GW Type (pushGateway: gcm or apns2)":"Missing Channels":"Missing Device ID (device)":"Missing Subscribe Key"}get path(){const{keySet:{subscribeKey:e},action:t,device:s,pushGateway:n}=this.parameters;let r="apns2"===n?`/v2/push/sub-key/${e}/devices-apns2/${s}`:`/v1/push/sub-key/${e}/devices/${s}`;return"remove-device"===t&&(r=`${r}/remove`),r}get queryParameters(){const{start:e,count:t}=this.parameters;let s=Object.assign(Object.assign({type:this.parameters.pushGateway},e?{start:e}:{}),t&&t>0?{count:t}:{});if("channels"in this.parameters&&(s[this.parameters.action]=this.parameters.channels.join(",")),"apns2"===this.parameters.pushGateway){const{environment:e,topic:t}=this.parameters;s=Object.assign(Object.assign({},s),{environment:e,topic:t})}return s}}class ds extends hs{constructor(e){super(Object.assign(Object.assign({},e),{action:"remove"}))}operation(){return M.PNRemovePushNotificationEnabledChannelsOperation}parse(e){const t=Object.create(null,{parse:{get:()=>super.parse}});return i(this,void 0,void 0,(function*(){return t.parse.call(this,e).then((e=>({})))}))}}class ps extends hs{constructor(e){super(Object.assign(Object.assign({},e),{action:"list"}))}operation(){return M.PNPushNotificationEnabledChannelsOperation}parse(e){return i(this,void 0,void 0,(function*(){return{channels:this.deserializeResponse(e)}}))}}class gs extends hs{constructor(e){super(Object.assign(Object.assign({},e),{action:"add"}))}operation(){return M.PNAddPushNotificationEnabledChannelsOperation}parse(e){const t=Object.create(null,{parse:{get:()=>super.parse}});return i(this,void 0,void 0,(function*(){return t.parse.call(this,e).then((e=>({})))}))}}class bs extends hs{constructor(e){super(Object.assign(Object.assign({},e),{action:"remove-device"}))}operation(){return M.PNRemoveAllPushNotificationsOperation}parse(e){const t=Object.create(null,{parse:{get:()=>super.parse}});return i(this,void 0,void 0,(function*(){return t.parse.call(this,e).then((e=>({})))}))}}class ys{constructor(e,t,s){this.sendRequest=s,this.logger=e,this.keySet=t}listChannels(e,t){return i(this,void 0,void 0,(function*(){this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"List push-enabled channels with parameters:"})));const s=new ps(Object.assign(Object.assign({},e),{keySet:this.keySet})),n=e=>{e&&this.logger.debug("PubNub",`List push-enabled channels success. Received ${e.channels.length} channels.`)};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}))}addChannels(e,t){return i(this,void 0,void 0,(function*(){this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Add push-enabled channels with parameters:"})));const s=new gs(Object.assign(Object.assign({},e),{keySet:this.keySet})),n=()=>{this.logger.debug("PubNub","Add push-enabled channels success.")};return t?this.sendRequest(s,(e=>{e.error||n(),t(e)})):this.sendRequest(s).then((e=>(n(),e)))}))}removeChannels(e,t){return i(this,void 0,void 0,(function*(){this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Remove push-enabled channels with parameters:"})));const s=new ds(Object.assign(Object.assign({},e),{keySet:this.keySet})),n=()=>{this.logger.debug("PubNub","Remove push-enabled channels success.")};return t?this.sendRequest(s,(e=>{e.error||n(),t(e)})):this.sendRequest(s).then((e=>(n(),e)))}))}deleteDevice(e,t){return i(this,void 0,void 0,(function*(){this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Remove push notifications for device with parameters:"})));const s=new bs(Object.assign(Object.assign({},e),{keySet:this.keySet})),n=()=>{this.logger.debug("PubNub","Remove push notifications for device success.")};return t?this.sendRequest(s,(e=>{e.error||n(),t(e)})):this.sendRequest(s).then((e=>(n(),e)))}))}}class ms extends le{constructor(e){var t,s,n,r,i,a;super(),this.parameters=e,null!==(t=e.include)&&void 0!==t||(e.include={}),null!==(s=(i=e.include).customFields)&&void 0!==s||(i.customFields=false),null!==(n=(a=e.include).totalCount)&&void 0!==n||(a.totalCount=false),null!==(r=e.limit)&&void 0!==r||(e.limit=100)}operation(){return M.PNGetAllChannelMetadataOperation}get path(){return`/v2/objects/${this.parameters.keySet.subscribeKey}/channels`}get queryParameters(){const{include:e,page:t,filter:s,sort:n,limit:r}=this.parameters;let i="";return i="string"==typeof n?n:Object.entries(null!=n?n:{}).map((([e,t])=>null!==t?`${e}:${t}`:e)),Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({include:["status","type",...e.customFields?["custom"]:[]].join(","),count:`${e.totalCount}`},s?{filter:s}:{}),(null==t?void 0:t.next)?{start:t.next}:{}),(null==t?void 0:t.prev)?{end:t.prev}:{}),r?{limit:r}:{}),i.length?{sort:i}:{})}}class fs extends le{constructor(e){super({method:ae.DELETE}),this.parameters=e}operation(){return M.PNRemoveChannelMetadataOperation}validate(){if(!this.parameters.channel)return"Channel cannot be empty"}get path(){const{keySet:{subscribeKey:e},channel:t}=this.parameters;return`/v2/objects/${e}/channels/${R(t)}`}}class vs extends le{constructor(e){var t,s,n,r,i,a,o,c,u,l,h,d,p,g,b,y,m,f;super(),this.parameters=e,null!==(t=e.include)&&void 0!==t||(e.include={}),null!==(s=(h=e.include).customFields)&&void 0!==s||(h.customFields=false),null!==(n=(d=e.include).totalCount)&&void 0!==n||(d.totalCount=false),null!==(r=(p=e.include).statusField)&&void 0!==r||(p.statusField=false),null!==(i=(g=e.include).typeField)&&void 0!==i||(g.typeField=false),null!==(a=(b=e.include).channelFields)&&void 0!==a||(b.channelFields=false),null!==(o=(y=e.include).customChannelFields)&&void 0!==o||(y.customChannelFields=false),null!==(c=(m=e.include).channelStatusField)&&void 0!==c||(m.channelStatusField=false),null!==(u=(f=e.include).channelTypeField)&&void 0!==u||(f.channelTypeField=false),null!==(l=e.limit)&&void 0!==l||(e.limit=100),this.parameters.userId&&(this.parameters.uuid=this.parameters.userId)}operation(){return M.PNGetMembershipsOperation}validate(){if(!this.parameters.uuid)return"'uuid' cannot be empty"}get path(){const{keySet:{subscribeKey:e},uuid:t}=this.parameters;return`/v2/objects/${e}/uuids/${R(t)}/channels`}get queryParameters(){const{include:e,page:t,filter:s,sort:n,limit:r}=this.parameters;let i="";i="string"==typeof n?n:Object.entries(null!=n?n:{}).map((([e,t])=>null!==t?`${e}:${t}`:e));const a=[];return e.statusField&&a.push("status"),e.typeField&&a.push("type"),e.customFields&&a.push("custom"),e.channelFields&&a.push("channel"),e.channelStatusField&&a.push("channel.status"),e.channelTypeField&&a.push("channel.type"),e.customChannelFields&&a.push("channel.custom"),Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({count:`${e.totalCount}`},a.length>0?{include:a.join(",")}:{}),s?{filter:s}:{}),(null==t?void 0:t.next)?{start:t.next}:{}),(null==t?void 0:t.prev)?{end:t.prev}:{}),r?{limit:r}:{}),i.length?{sort:i}:{})}}class Ss extends le{constructor(e){var t,s,n,r,i,a,o,c,u,l,h,d,p,g,b,y,m,f;super({method:ae.PATCH}),this.parameters=e,null!==(t=e.include)&&void 0!==t||(e.include={}),null!==(s=(h=e.include).customFields)&&void 0!==s||(h.customFields=false),null!==(n=(d=e.include).totalCount)&&void 0!==n||(d.totalCount=false),null!==(r=(p=e.include).statusField)&&void 0!==r||(p.statusField=false),null!==(i=(g=e.include).typeField)&&void 0!==i||(g.typeField=false),null!==(a=(b=e.include).channelFields)&&void 0!==a||(b.channelFields=false),null!==(o=(y=e.include).customChannelFields)&&void 0!==o||(y.customChannelFields=false),null!==(c=(m=e.include).channelStatusField)&&void 0!==c||(m.channelStatusField=false),null!==(u=(f=e.include).channelTypeField)&&void 0!==u||(f.channelTypeField=false),null!==(l=e.limit)&&void 0!==l||(e.limit=100),this.parameters.userId&&(this.parameters.uuid=this.parameters.userId)}operation(){return M.PNSetMembershipsOperation}validate(){const{uuid:e,channels:t}=this.parameters;return e?t&&0!==t.length?void 0:"Channels cannot be empty":"'uuid' cannot be empty"}get path(){const{keySet:{subscribeKey:e},uuid:t}=this.parameters;return`/v2/objects/${e}/uuids/${R(t)}/channels`}get queryParameters(){const{include:e,page:t,filter:s,sort:n,limit:r}=this.parameters;let i="";i="string"==typeof n?n:Object.entries(null!=n?n:{}).map((([e,t])=>null!==t?`${e}:${t}`:e));const a=["channel.status","channel.type","status"];return e.statusField&&a.push("status"),e.typeField&&a.push("type"),e.customFields&&a.push("custom"),e.channelFields&&a.push("channel"),e.channelStatusField&&a.push("channel.status"),e.channelTypeField&&a.push("channel.type"),e.customChannelFields&&a.push("channel.custom"),Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({count:`${e.totalCount}`},a.length>0?{include:a.join(",")}:{}),s?{filter:s}:{}),(null==t?void 0:t.next)?{start:t.next}:{}),(null==t?void 0:t.prev)?{end:t.prev}:{}),r?{limit:r}:{}),i.length?{sort:i}:{})}get headers(){var e;return Object.assign(Object.assign({},null!==(e=super.headers)&&void 0!==e?e:{}),{"Content-Type":"application/json"})}get body(){const{channels:e,type:t}=this.parameters;return JSON.stringify({[`${t}`]:e.map((e=>"string"==typeof e?{channel:{id:e}}:{channel:{id:e.id},status:e.status,type:e.type,custom:e.custom}))})}}class ws extends le{constructor(e){var t,s,n,r;super(),this.parameters=e,null!==(t=e.include)&&void 0!==t||(e.include={}),null!==(s=(r=e.include).customFields)&&void 0!==s||(r.customFields=false),null!==(n=e.limit)&&void 0!==n||(e.limit=100)}operation(){return M.PNGetAllUUIDMetadataOperation}get path(){return`/v2/objects/${this.parameters.keySet.subscribeKey}/uuids`}get queryParameters(){const{include:e,page:t,filter:s,sort:n,limit:r}=this.parameters;let i="";return i="string"==typeof n?n:Object.entries(null!=n?n:{}).map((([e,t])=>null!==t?`${e}:${t}`:e)),Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({include:["status","type",...e.customFields?["custom"]:[]].join(",")},void 0!==e.totalCount?{count:`${e.totalCount}`}:{}),s?{filter:s}:{}),(null==t?void 0:t.next)?{start:t.next}:{}),(null==t?void 0:t.prev)?{end:t.prev}:{}),r?{limit:r}:{}),i.length?{sort:i}:{})}}class Os extends le{constructor(e){var t,s,n;super(),this.parameters=e,null!==(t=e.include)&&void 0!==t||(e.include={}),null!==(s=(n=e.include).customFields)&&void 0!==s||(n.customFields=true)}operation(){return M.PNGetChannelMetadataOperation}validate(){if(!this.parameters.channel)return"Channel cannot be empty"}get path(){const{keySet:{subscribeKey:e},channel:t}=this.parameters;return`/v2/objects/${e}/channels/${R(t)}`}get queryParameters(){return{include:["status","type",...this.parameters.include.customFields?["custom"]:[]].join(",")}}}class ks extends le{constructor(e){var t,s,n;super({method:ae.PATCH}),this.parameters=e,null!==(t=e.include)&&void 0!==t||(e.include={}),null!==(s=(n=e.include).customFields)&&void 0!==s||(n.customFields=true)}operation(){return M.PNSetChannelMetadataOperation}validate(){return this.parameters.channel?this.parameters.data?void 0:"Data cannot be empty":"Channel cannot be empty"}get headers(){var e;let t=null!==(e=super.headers)&&void 0!==e?e:{};return this.parameters.ifMatchesEtag&&(t=Object.assign(Object.assign({},t),{"If-Match":this.parameters.ifMatchesEtag})),Object.assign(Object.assign({},t),{"Content-Type":"application/json"})}get path(){const{keySet:{subscribeKey:e},channel:t}=this.parameters;return`/v2/objects/${e}/channels/${R(t)}`}get queryParameters(){return{include:["status","type",...this.parameters.include.customFields?["custom"]:[]].join(",")}}get body(){return JSON.stringify(this.parameters.data)}}class Cs extends le{constructor(e){super({method:ae.DELETE}),this.parameters=e,this.parameters.userId&&(this.parameters.uuid=this.parameters.userId)}operation(){return M.PNRemoveUUIDMetadataOperation}validate(){if(!this.parameters.uuid)return"'uuid' cannot be empty"}get path(){const{keySet:{subscribeKey:e},uuid:t}=this.parameters;return`/v2/objects/${e}/uuids/${R(t)}`}}class Ps extends le{constructor(e){var t,s,n,r,i,a,o,c,u,l,h,d,p,g,b,y,m,f;super(),this.parameters=e,null!==(t=e.include)&&void 0!==t||(e.include={}),null!==(s=(h=e.include).customFields)&&void 0!==s||(h.customFields=false),null!==(n=(d=e.include).totalCount)&&void 0!==n||(d.totalCount=false),null!==(r=(p=e.include).statusField)&&void 0!==r||(p.statusField=false),null!==(i=(g=e.include).typeField)&&void 0!==i||(g.typeField=false),null!==(a=(b=e.include).UUIDFields)&&void 0!==a||(b.UUIDFields=false),null!==(o=(y=e.include).customUUIDFields)&&void 0!==o||(y.customUUIDFields=false),null!==(c=(m=e.include).UUIDStatusField)&&void 0!==c||(m.UUIDStatusField=false),null!==(u=(f=e.include).UUIDTypeField)&&void 0!==u||(f.UUIDTypeField=false),null!==(l=e.limit)&&void 0!==l||(e.limit=100)}operation(){return M.PNSetMembersOperation}validate(){if(!this.parameters.channel)return"Channel cannot be empty"}get path(){const{keySet:{subscribeKey:e},channel:t}=this.parameters;return`/v2/objects/${e}/channels/${R(t)}/uuids`}get queryParameters(){const{include:e,page:t,filter:s,sort:n,limit:r}=this.parameters;let i="";i="string"==typeof n?n:Object.entries(null!=n?n:{}).map((([e,t])=>null!==t?`${e}:${t}`:e));const a=[];return e.statusField&&a.push("status"),e.typeField&&a.push("type"),e.customFields&&a.push("custom"),e.UUIDFields&&a.push("uuid"),e.UUIDStatusField&&a.push("uuid.status"),e.UUIDTypeField&&a.push("uuid.type"),e.customUUIDFields&&a.push("uuid.custom"),Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({count:`${e.totalCount}`},a.length>0?{include:a.join(",")}:{}),s?{filter:s}:{}),(null==t?void 0:t.next)?{start:t.next}:{}),(null==t?void 0:t.prev)?{end:t.prev}:{}),r?{limit:r}:{}),i.length?{sort:i}:{})}}class js extends le{constructor(e){var t,s,n,r,i,a,o,c,u,l,h,d,p,g,b,y,m,f;super({method:ae.PATCH}),this.parameters=e,null!==(t=e.include)&&void 0!==t||(e.include={}),null!==(s=(h=e.include).customFields)&&void 0!==s||(h.customFields=false),null!==(n=(d=e.include).totalCount)&&void 0!==n||(d.totalCount=false),null!==(r=(p=e.include).statusField)&&void 0!==r||(p.statusField=false),null!==(i=(g=e.include).typeField)&&void 0!==i||(g.typeField=false),null!==(a=(b=e.include).UUIDFields)&&void 0!==a||(b.UUIDFields=false),null!==(o=(y=e.include).customUUIDFields)&&void 0!==o||(y.customUUIDFields=false),null!==(c=(m=e.include).UUIDStatusField)&&void 0!==c||(m.UUIDStatusField=false),null!==(u=(f=e.include).UUIDTypeField)&&void 0!==u||(f.UUIDTypeField=false),null!==(l=e.limit)&&void 0!==l||(e.limit=100)}operation(){return M.PNSetMembersOperation}validate(){const{channel:e,uuids:t}=this.parameters;return e?t&&0!==t.length?void 0:"UUIDs cannot be empty":"Channel cannot be empty"}get path(){const{keySet:{subscribeKey:e},channel:t}=this.parameters;return`/v2/objects/${e}/channels/${R(t)}/uuids`}get queryParameters(){const{include:e,page:t,filter:s,sort:n,limit:r}=this.parameters;let i="";i="string"==typeof n?n:Object.entries(null!=n?n:{}).map((([e,t])=>null!==t?`${e}:${t}`:e));const a=["uuid.status","uuid.type","type"];return e.statusField&&a.push("status"),e.typeField&&a.push("type"),e.customFields&&a.push("custom"),e.UUIDFields&&a.push("uuid"),e.UUIDStatusField&&a.push("uuid.status"),e.UUIDTypeField&&a.push("uuid.type"),e.customUUIDFields&&a.push("uuid.custom"),Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({count:`${e.totalCount}`},a.length>0?{include:a.join(",")}:{}),s?{filter:s}:{}),(null==t?void 0:t.next)?{start:t.next}:{}),(null==t?void 0:t.prev)?{end:t.prev}:{}),r?{limit:r}:{}),i.length?{sort:i}:{})}get headers(){var e;return Object.assign(Object.assign({},null!==(e=super.headers)&&void 0!==e?e:{}),{"Content-Type":"application/json"})}get body(){const{uuids:e,type:t}=this.parameters;return JSON.stringify({[`${t}`]:e.map((e=>"string"==typeof e?{uuid:{id:e}}:{uuid:{id:e.id},status:e.status,type:e.type,custom:e.custom}))})}}class Es extends le{constructor(e){var t,s,n;super(),this.parameters=e,null!==(t=e.include)&&void 0!==t||(e.include={}),null!==(s=(n=e.include).customFields)&&void 0!==s||(n.customFields=true),this.parameters.userId&&(this.parameters.uuid=this.parameters.userId)}operation(){return M.PNGetUUIDMetadataOperation}validate(){if(!this.parameters.uuid)return"'uuid' cannot be empty"}get path(){const{keySet:{subscribeKey:e},uuid:t}=this.parameters;return`/v2/objects/${e}/uuids/${R(t)}`}get queryParameters(){const{include:e}=this.parameters;return{include:["status","type",...e.customFields?["custom"]:[]].join(",")}}}class Ns extends le{constructor(e){var t,s,n;super({method:ae.PATCH}),this.parameters=e,null!==(t=e.include)&&void 0!==t||(e.include={}),null!==(s=(n=e.include).customFields)&&void 0!==s||(n.customFields=true),this.parameters.userId&&(this.parameters.uuid=this.parameters.userId)}operation(){return M.PNSetUUIDMetadataOperation}validate(){return this.parameters.uuid?this.parameters.data?void 0:"Data cannot be empty":"'uuid' cannot be empty"}get headers(){var e;let t=null!==(e=super.headers)&&void 0!==e?e:{};return this.parameters.ifMatchesEtag&&(t=Object.assign(Object.assign({},t),{"If-Match":this.parameters.ifMatchesEtag})),Object.assign(Object.assign({},t),{"Content-Type":"application/json"})}get path(){const{keySet:{subscribeKey:e},uuid:t}=this.parameters;return`/v2/objects/${e}/uuids/${R(t)}`}get queryParameters(){return{include:["status","type",...this.parameters.include.customFields?["custom"]:[]].join(",")}}get body(){return JSON.stringify(this.parameters.data)}}class Ts{constructor(e,t){this.keySet=e.keySet,this.configuration=e,this.sendRequest=t}get logger(){return this.configuration.logger()}getAllUUIDMetadata(e,t){return i(this,void 0,void 0,(function*(){return this.logger.debug("PubNub",(()=>({messageType:"object",message:e&&"function"!=typeof e?e:{},details:"Get all UUID metadata objects with parameters:"}))),this._getAllUUIDMetadata(e,t)}))}_getAllUUIDMetadata(e,t){return i(this,void 0,void 0,(function*(){const s=e&&"function"!=typeof e?e:{};null!=t||(t="function"==typeof e?e:void 0);const n=new ws(Object.assign(Object.assign({},s),{keySet:this.keySet})),r=e=>{e&&this.logger.debug("PubNub",`Get all UUID metadata success. Received ${e.totalCount} UUID metadata objects.`)};return t?this.sendRequest(n,((e,s)=>{r(s),t(e,s)})):this.sendRequest(n).then((e=>(r(e),e)))}))}getUUIDMetadata(e,t){return i(this,void 0,void 0,(function*(){return this.logger.debug("PubNub",(()=>({messageType:"object",message:e&&"function"!=typeof e?e:{uuid:this.configuration.userId},details:`Get ${e&&"function"!=typeof e?"":" current"} UUID metadata object with parameters:`}))),this._getUUIDMetadata(e,t)}))}_getUUIDMetadata(e,t){return i(this,void 0,void 0,(function*(){var s;const n=e&&"function"!=typeof e?e:{};null!=t||(t="function"==typeof e?e:void 0),n.userId&&(this.logger.warn("PubNub","'userId' parameter is deprecated. Use 'uuid' instead."),n.uuid=n.userId),null!==(s=n.uuid)&&void 0!==s||(n.uuid=this.configuration.userId);const r=new Es(Object.assign(Object.assign({},n),{keySet:this.keySet})),i=e=>{e&&this.logger.debug("PubNub",`Get UUID metadata object success. Received '${n.uuid}' UUID metadata object.`)};return t?this.sendRequest(r,((e,s)=>{i(s),t(e,s)})):this.sendRequest(r).then((e=>(i(e),e)))}))}setUUIDMetadata(e,t){return i(this,void 0,void 0,(function*(){return this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Set UUID metadata object with parameters:"}))),this._setUUIDMetadata(e,t)}))}_setUUIDMetadata(e,t){return i(this,void 0,void 0,(function*(){var s;e.userId&&(this.logger.warn("PubNub","'userId' parameter is deprecated. Use 'uuid' instead."),e.uuid=e.userId),null!==(s=e.uuid)&&void 0!==s||(e.uuid=this.configuration.userId);const n=new Ns(Object.assign(Object.assign({},e),{keySet:this.keySet})),r=t=>{t&&this.logger.debug("PubNub",`Set UUID metadata object success. Updated '${e.uuid}' UUID metadata object.`)};return t?this.sendRequest(n,((e,s)=>{r(s),t(e,s)})):this.sendRequest(n).then((e=>(r(e),e)))}))}removeUUIDMetadata(e,t){return i(this,void 0,void 0,(function*(){return this.logger.debug("PubNub",(()=>({messageType:"object",message:e&&"function"!=typeof e?e:{uuid:this.configuration.userId},details:`Remove${e&&"function"!=typeof e?"":" current"} UUID metadata object with parameters:`}))),this._removeUUIDMetadata(e,t)}))}_removeUUIDMetadata(e,t){return i(this,void 0,void 0,(function*(){var s;const n=e&&"function"!=typeof e?e:{};null!=t||(t="function"==typeof e?e:void 0),n.userId&&(this.logger.warn("PubNub","'userId' parameter is deprecated. Use 'uuid' instead."),n.uuid=n.userId),null!==(s=n.uuid)&&void 0!==s||(n.uuid=this.configuration.userId);const r=new Cs(Object.assign(Object.assign({},n),{keySet:this.keySet})),i=e=>{e&&this.logger.debug("PubNub",`Remove UUID metadata object success. Removed '${n.uuid}' UUID metadata object.`)};return t?this.sendRequest(r,((e,s)=>{i(s),t(e,s)})):this.sendRequest(r).then((e=>(i(e),e)))}))}getAllChannelMetadata(e,t){return i(this,void 0,void 0,(function*(){return this.logger.debug("PubNub",(()=>({messageType:"object",message:e&&"function"!=typeof e?e:{},details:"Get all Channel metadata objects with parameters:"}))),this._getAllChannelMetadata(e,t)}))}_getAllChannelMetadata(e,t){return i(this,void 0,void 0,(function*(){const s=e&&"function"!=typeof e?e:{};null!=t||(t="function"==typeof e?e:void 0);const n=new ms(Object.assign(Object.assign({},s),{keySet:this.keySet})),r=e=>{e&&this.logger.debug("PubNub",`Get all Channel metadata objects success. Received ${e.totalCount} Channel metadata objects.`)};return t?this.sendRequest(n,((e,s)=>{r(s),t(e,s)})):this.sendRequest(n).then((e=>(r(e),e)))}))}getChannelMetadata(e,t){return i(this,void 0,void 0,(function*(){return this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Get Channel metadata object with parameters:"}))),this._getChannelMetadata(e,t)}))}_getChannelMetadata(e,t){return i(this,void 0,void 0,(function*(){const s=new Os(Object.assign(Object.assign({},e),{keySet:this.keySet})),n=t=>{t&&this.logger.debug("PubNub",`Get Channel metadata object success. Received '${e.channel}' Channel metadata object.`)};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}))}setChannelMetadata(e,t){return i(this,void 0,void 0,(function*(){return this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Set Channel metadata object with parameters:"}))),this._setChannelMetadata(e,t)}))}_setChannelMetadata(e,t){return i(this,void 0,void 0,(function*(){const s=new ks(Object.assign(Object.assign({},e),{keySet:this.keySet})),n=t=>{t&&this.logger.debug("PubNub",`Set Channel metadata object success. Updated '${e.channel}' Channel metadata object.`)};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}))}removeChannelMetadata(e,t){return i(this,void 0,void 0,(function*(){return this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Remove Channel metadata object with parameters:"}))),this._removeChannelMetadata(e,t)}))}_removeChannelMetadata(e,t){return i(this,void 0,void 0,(function*(){const s=new fs(Object.assign(Object.assign({},e),{keySet:this.keySet})),n=t=>{t&&this.logger.debug("PubNub",`Remove Channel metadata object success. Removed '${e.channel}' Channel metadata object.`)};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}))}getChannelMembers(e,t){return i(this,void 0,void 0,(function*(){this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Get channel members with parameters:"})));const s=new Ps(Object.assign(Object.assign({},e),{keySet:this.keySet})),n=e=>{e&&this.logger.debug("PubNub",`Get channel members success. Received ${e.totalCount} channel members.`)};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}))}setChannelMembers(e,t){return i(this,void 0,void 0,(function*(){this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Set channel members with parameters:"})));const s=new js(Object.assign(Object.assign({},e),{type:"set",keySet:this.keySet})),n=e=>{e&&this.logger.debug("PubNub",`Set channel members success. There are ${e.totalCount} channel members now.`)};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}))}removeChannelMembers(e,t){return i(this,void 0,void 0,(function*(){this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Remove channel members with parameters:"})));const s=new js(Object.assign(Object.assign({},e),{type:"delete",keySet:this.keySet})),n=e=>{e&&this.logger.debug("PubNub",`Remove channel members success. There are ${e.totalCount} channel members now.`)};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}))}getMemberships(e,t){return i(this,void 0,void 0,(function*(){var s;const n=e&&"function"!=typeof e?e:{};null!=t||(t="function"==typeof e?e:void 0),n.userId&&(this.logger.warn("PubNub","'userId' parameter is deprecated. Use 'uuid' instead."),n.uuid=n.userId),null!==(s=n.uuid)&&void 0!==s||(n.uuid=this.configuration.userId),this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},n),details:"Get memberships with parameters:"})));const r=new vs(Object.assign(Object.assign({},n),{keySet:this.keySet})),i=e=>{e&&this.logger.debug("PubNub",`Get memberships success. Received ${e.totalCount} memberships.`)};return t?this.sendRequest(r,((e,s)=>{i(s),t(e,s)})):this.sendRequest(r).then((e=>(i(e),e)))}))}setMemberships(e,t){return i(this,void 0,void 0,(function*(){var s;e.userId&&(this.logger.warn("PubNub","'userId' parameter is deprecated. Use 'uuid' instead."),e.uuid=e.userId),null!==(s=e.uuid)&&void 0!==s||(e.uuid=this.configuration.userId),this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Set memberships with parameters:"})));const n=new Ss(Object.assign(Object.assign({},e),{type:"set",keySet:this.keySet})),r=e=>{e&&this.logger.debug("PubNub",`Set memberships success. There are ${e.totalCount} memberships now.`)};return t?this.sendRequest(n,((e,s)=>{r(s),t(e,s)})):this.sendRequest(n).then((e=>(r(e),e)))}))}removeMemberships(e,t){return i(this,void 0,void 0,(function*(){var s;e.userId&&(this.logger.warn("PubNub","'userId' parameter is deprecated. Use 'uuid' instead."),e.uuid=e.userId),null!==(s=e.uuid)&&void 0!==s||(e.uuid=this.configuration.userId),this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Remove memberships with parameters:"})));const n=new Ss(Object.assign(Object.assign({},e),{type:"delete",keySet:this.keySet})),r=e=>{e&&this.logger.debug("PubNub",`Remove memberships success. There are ${e.totalCount} memberships now.`)};return t?this.sendRequest(n,((e,s)=>{r(s),t(e,s)})):this.sendRequest(n).then((e=>(r(e),e)))}))}fetchMemberships(e,t){return i(this,void 0,void 0,(function*(){var s,n;if(this.logger.warn("PubNub","'fetchMemberships' is deprecated. Use 'pubnub.objects.getChannelMembers' or 'pubnub.objects.getMemberships' instead."),this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Fetch memberships with parameters:"}))),"spaceId"in e){const n=e,r={channel:null!==(s=n.spaceId)&&void 0!==s?s:n.channel,filter:n.filter,limit:n.limit,page:n.page,include:Object.assign({},n.include),sort:n.sort?Object.fromEntries(Object.entries(n.sort).map((([e,t])=>[e.replace("user","uuid"),t]))):void 0},i=e=>({status:e.status,data:e.data.map((e=>({user:e.uuid,custom:e.custom,updated:e.updated,eTag:e.eTag}))),totalCount:e.totalCount,next:e.next,prev:e.prev});return t?this.getChannelMembers(r,((e,s)=>{t(e,s?i(s):s)})):this.getChannelMembers(r).then(i)}const r=e,i={uuid:null!==(n=r.userId)&&void 0!==n?n:r.uuid,filter:r.filter,limit:r.limit,page:r.page,include:Object.assign({},r.include),sort:r.sort?Object.fromEntries(Object.entries(r.sort).map((([e,t])=>[e.replace("space","channel"),t]))):void 0},a=e=>({status:e.status,data:e.data.map((e=>({space:e.channel,custom:e.custom,updated:e.updated,eTag:e.eTag}))),totalCount:e.totalCount,next:e.next,prev:e.prev});return t?this.getMemberships(i,((e,s)=>{t(e,s?a(s):s)})):this.getMemberships(i).then(a)}))}addMemberships(e,t){return i(this,void 0,void 0,(function*(){var s,n,r,i,a,o;if(this.logger.warn("PubNub","'addMemberships' is deprecated. Use 'pubnub.objects.setChannelMembers' or 'pubnub.objects.setMemberships' instead."),this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Add memberships with parameters:"}))),"spaceId"in e){const i=e,a={channel:null!==(s=i.spaceId)&&void 0!==s?s:i.channel,uuids:null!==(r=null===(n=i.users)||void 0===n?void 0:n.map((e=>"string"==typeof e?e:{id:e.userId,custom:e.custom})))&&void 0!==r?r:i.uuids,limit:0};return t?this.setChannelMembers(a,t):this.setChannelMembers(a)}const c=e,u={uuid:null!==(i=c.userId)&&void 0!==i?i:c.uuid,channels:null!==(o=null===(a=c.spaces)||void 0===a?void 0:a.map((e=>"string"==typeof e?e:{id:e.spaceId,custom:e.custom})))&&void 0!==o?o:c.channels,limit:0};return t?this.setMemberships(u,t):this.setMemberships(u)}))}}class _s extends le{constructor(){super()}operation(){return M.PNTimeOperation}parse(e){return i(this,void 0,void 0,(function*(){return{timetoken:this.deserializeResponse(e)[0]}}))}get path(){return"/time/0"}}class Is extends le{constructor(e){super(),this.parameters=e}operation(){return M.PNDownloadFileOperation}validate(){const{channel:e,id:t,name:s}=this.parameters;return e?t?s?void 0:"file name can't be empty":"file id can't be empty":"channel can't be empty"}parse(e){return i(this,void 0,void 0,(function*(){const{cipherKey:t,crypto:s,cryptography:n,name:r,PubNubFile:i}=this.parameters,a=e.headers["content-type"];let o,c=e.body;return i.supportsEncryptFile&&(t||s)&&(t&&n?c=yield n.decrypt(t,c):!t&&s&&(o=yield s.decryptFile(i.create({data:c,name:r,mimeType:a}),i))),o||i.create({data:c,name:r,mimeType:a})}))}get path(){const{keySet:{subscribeKey:e},channel:t,id:s,name:n}=this.parameters;return`/v1/files/${e}/channels/${R(t)}/files/${s}/${n}`}}class Ms{static notificationPayload(e,t){return new we(e,t)}static generateUUID(){return te.createUUID()}constructor(e){if(this.eventHandleCapable={},this.entities={},this._configuration=e.configuration,this.cryptography=e.cryptography,this.tokenManager=e.tokenManager,this.transport=e.transport,this.crypto=e.crypto,this.logger.debug("PubNub",(()=>({messageType:"object",message:e.configuration,details:"Create with configuration:",ignoredKeys:(e,t)=>"function"==typeof t[e]||e.startsWith("_")}))),this._objects=new Ts(this._configuration,this.sendRequest.bind(this)),this._channelGroups=new ls(this._configuration.logger(),this._configuration.keySet,this.sendRequest.bind(this)),this._push=new ys(this._configuration.logger(),this._configuration.keySet,this.sendRequest.bind(this)),this.eventDispatcher=new ge,this._configuration.enableEventEngine){this.logger.debug("PubNub","Using new subscription loop management.");let e=this._configuration.getHeartbeatInterval();this.presenceState={},e&&(this.presenceEventEngine=new Ye({heartbeat:(e,t)=>(this.logger.trace("PresenceEventEngine",(()=>({messageType:"object",message:Object.assign({},e),details:"Heartbeat with parameters:"}))),this.heartbeat(e,t)),leave:e=>{this.logger.trace("PresenceEventEngine",(()=>({messageType:"object",message:Object.assign({},e),details:"Leave with parameters:"}))),this.makeUnsubscribe(e,(()=>{}))},heartbeatDelay:()=>new Promise(((t,s)=>{e=this._configuration.getHeartbeatInterval(),e?setTimeout(t,1e3*e):s(new d("Heartbeat interval has been reset."))})),emitStatus:e=>this.emitStatus(e),config:this._configuration,presenceState:this.presenceState})),this.eventEngine=new St({handshake:e=>(this.logger.trace("EventEngine",(()=>({messageType:"object",message:Object.assign({},e),details:"Handshake with parameters:",ignoredKeys:["abortSignal","crypto","timeout","keySet","getFileUrl"]}))),this.subscribeHandshake(e)),receiveMessages:e=>(this.logger.trace("EventEngine",(()=>({messageType:"object",message:Object.assign({},e),details:"Receive messages with parameters:",ignoredKeys:["abortSignal","crypto","timeout","keySet","getFileUrl"]}))),this.subscribeReceiveMessages(e)),delay:e=>new Promise((t=>setTimeout(t,e))),join:e=>{var t,s;this.logger.trace("EventEngine",(()=>({messageType:"object",message:Object.assign({},e),details:"Join with parameters:"}))),e&&0===(null!==(t=e.channels)&&void 0!==t?t:[]).length&&0===(null!==(s=e.groups)&&void 0!==s?s:[]).length?this.logger.trace("EventEngine","Ignoring 'join' announcement request."):this.join(e)},leave:e=>{var t,s;this.logger.trace("EventEngine",(()=>({messageType:"object",message:Object.assign({},e),details:"Leave with parameters:"}))),e&&0===(null!==(t=e.channels)&&void 0!==t?t:[]).length&&0===(null!==(s=e.groups)&&void 0!==s?s:[]).length?this.logger.trace("EventEngine","Ignoring 'leave' announcement request."):this.leave(e)},leaveAll:e=>{this.logger.trace("EventEngine",(()=>({messageType:"object",message:Object.assign({},e),details:"Leave all with parameters:"}))),this.leaveAll(e)},presenceReconnect:e=>{this.logger.trace("EventEngine",(()=>({messageType:"object",message:Object.assign({},e),details:"Reconnect with parameters:"}))),this.presenceReconnect(e)},presenceDisconnect:e=>{this.logger.trace("EventEngine",(()=>({messageType:"object",message:Object.assign({},e),details:"Disconnect with parameters:"}))),this.presenceDisconnect(e)},presenceState:this.presenceState,config:this._configuration,emitMessages:(e,t)=>{try{this.logger.debug("EventEngine",(()=>({messageType:"object",message:t.map((e=>{const t=e.type===he.Message||e.type===he.Signal?B(e.data.message):void 0;return t?{type:e.type,data:Object.assign(Object.assign({},e.data),{pn_mfp:t})}:e})),details:"Received events:"}))),t.forEach((t=>this.emitEvent(e,t)))}catch(e){const t={error:!0,category:h.PNUnknownCategory,errorData:e,statusCode:0};this.emitStatus(t)}},emitStatus:e=>this.emitStatus(e)})}else this.logger.debug("PubNub","Using legacy subscription loop management."),this.subscriptionManager=new me(this._configuration,((e,t)=>{try{this.emitEvent(e,t)}catch(e){const t={error:!0,category:h.PNUnknownCategory,errorData:e,statusCode:0};this.emitStatus(t)}}),this.emitStatus.bind(this),((e,t)=>{this.logger.trace("SubscriptionManager",(()=>({messageType:"object",message:Object.assign({},e),details:"Subscribe with parameters:",ignoredKeys:["crypto","timeout","keySet","getFileUrl"]}))),this.makeSubscribe(e,t)}),((e,t)=>(this.logger.trace("SubscriptionManager",(()=>({messageType:"object",message:Object.assign({},e),details:"Heartbeat with parameters:",ignoredKeys:["crypto","timeout","keySet","getFileUrl"]}))),this.heartbeat(e,t))),((e,t)=>{this.logger.trace("SubscriptionManager",(()=>({messageType:"object",message:Object.assign({},e),details:"Leave with parameters:"}))),this.makeUnsubscribe(e,t)}),this.time.bind(this))}get configuration(){return this._configuration}get _config(){return this.configuration}get authKey(){var e;return null!==(e=this._configuration.authKey)&&void 0!==e?e:void 0}getAuthKey(){return this.authKey}setAuthKey(e){this.logger.debug("PubNub",`Set auth key: ${e}`),this._configuration.setAuthKey(e),this.onAuthenticationChange&&this.onAuthenticationChange(e)}get userId(){return this._configuration.userId}set userId(e){if(!e||"string"!=typeof e||0===e.trim().length){const e=new Error("Missing or invalid userId parameter. Provide a valid string userId");throw this.logger.error("PubNub",(()=>({messageType:"error",message:e}))),e}this.logger.debug("PubNub",`Set user ID: ${e}`),this._configuration.userId=e,this.onUserIdChange&&this.onUserIdChange(this._configuration.userId)}getUserId(){return this._configuration.userId}setUserId(e){this.userId=e}get filterExpression(){var e;return null!==(e=this._configuration.getFilterExpression())&&void 0!==e?e:void 0}getFilterExpression(){return this.filterExpression}set filterExpression(e){this.logger.debug("PubNub",`Set filter expression: ${e}`),this._configuration.setFilterExpression(e)}setFilterExpression(e){this.logger.debug("PubNub",`Set filter expression: ${e}`),this.filterExpression=e}get cipherKey(){return this._configuration.getCipherKey()}set cipherKey(e){this._configuration.setCipherKey(e)}setCipherKey(e){this.logger.debug("PubNub",`Set cipher key: ${e}`),this.cipherKey=e}set heartbeatInterval(e){var t;this.logger.debug("PubNub",`Set heartbeat interval: ${e}`),this._configuration.setHeartbeatInterval(e),this.onHeartbeatIntervalChange&&this.onHeartbeatIntervalChange(null!==(t=this._configuration.getHeartbeatInterval())&&void 0!==t?t:0)}setHeartbeatInterval(e){this.heartbeatInterval=e}get logger(){return this._configuration.logger()}getVersion(){return this._configuration.getVersion()}_addPnsdkSuffix(e,t){this.logger.debug("PubNub",`Add '${e}' 'pnsdk' suffix: ${t}`),this._configuration._addPnsdkSuffix(e,t)}getUUID(){return this.userId}setUUID(e){this.logger.warn("PubNub","'setUserId` is deprecated, please use 'setUserId' or 'userId' setter instead."),this.logger.debug("PubNub",`Set UUID: ${e}`),this.userId=e}get customEncrypt(){return this._configuration.getCustomEncrypt()}get customDecrypt(){return this._configuration.getCustomDecrypt()}channel(e){let t=this.entities[`${e}_ch`];return t||(t=this.entities[`${e}_ch`]=new rs(e,this)),t}channelGroup(e){let t=this.entities[`${e}_chg`];return t||(t=this.entities[`${e}_chg`]=new ss(e,this)),t}channelMetadata(e){let t=this.entities[`${e}_chm`];return t||(t=this.entities[`${e}_chm`]=new ts(e,this)),t}userMetadata(e){let t=this.entities[`${e}_um`];return t||(t=this.entities[`${e}_um`]=new ns(e,this)),t}subscriptionSet(e){var t,s;{const n=[];return null===(t=e.channels)||void 0===t||t.forEach((e=>n.push(this.channel(e)))),null===(s=e.channelGroups)||void 0===s||s.forEach((e=>n.push(this.channelGroup(e)))),new _t({client:this,entities:n,options:e.subscriptionOptions})}}sendRequest(e,t){return i(this,void 0,void 0,(function*(){const s=e.validate();if(s){const e=(n=s,p(Object.assign({message:n},{}),h.PNValidationErrorCategory));if(this.logger.error("PubNub",(()=>({messageType:"error",message:e}))),t)return t(e,null);throw new d("Validation failed, check status for details",e)}var n;const r=e.request(),i=e.operation();r.formData&&r.formData.length>0||i===M.PNDownloadFileOperation?r.timeout=this._configuration.getFileTimeout():i===M.PNSubscribeOperation||i===M.PNReceiveMessagesOperation?r.timeout=this._configuration.getSubscribeTimeout():r.timeout=this._configuration.getTransactionTimeout();const a={error:!1,operation:i,category:h.PNAcknowledgmentCategory,statusCode:0},[o,c]=this.transport.makeSendable(r);return e.cancellationController=c||null,o.then((t=>{if(a.statusCode=t.status,200!==t.status&&204!==t.status){const e=Ms.decoder.decode(t.body),s=t.headers["content-type"];if(s||-1!==s.indexOf("javascript")||-1!==s.indexOf("json")){const t=JSON.parse(e);"object"==typeof t&&"error"in t&&t.error&&"object"==typeof t.error&&(a.errorData=t.error)}else a.responseText=e}return e.parse(t)})).then((e=>t?t(a,e):e)).catch((e=>{const s=e instanceof I?e:I.create(e);if(t)return s.category!==h.PNCancelledCategory&&this.logger.error("PubNub",(()=>({messageType:"error",message:s.toPubNubError(i,"REST API request processing error, check status for details")}))),t(s.toStatus(i),null);const n=s.toPubNubError(i,"REST API request processing error, check status for details");throw s.category!==h.PNCancelledCategory&&this.logger.error("PubNub",(()=>({messageType:"error",message:n}))),n}))}))}destroy(e=!1){this.logger.info("PubNub","Destroying PubNub client."),this._globalSubscriptionSet&&(this._globalSubscriptionSet.invalidate(!0),this._globalSubscriptionSet=void 0),Object.values(this.eventHandleCapable).forEach((e=>e.invalidate(!0))),this.eventHandleCapable={},this.subscriptionManager?(this.subscriptionManager.unsubscribeAll(e),this.subscriptionManager.disconnect()):this.eventEngine&&this.eventEngine.unsubscribeAll(e),this.presenceEventEngine&&this.presenceEventEngine.leaveAll(e)}stop(){this.logger.warn("PubNub","'stop' is deprecated, please use 'destroy' instead."),this.destroy()}publish(e,t){return i(this,void 0,void 0,(function*(){{this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Publish with parameters:"})));const s=!1===e.replicate&&!1===e.storeInHistory,n=new wt(Object.assign(Object.assign({},e),{keySet:this._configuration.keySet,crypto:this._configuration.getCryptoModule()})),r=e=>{e&&this.logger.debug("PubNub",`${s?"Fire":"Publish"} success with timetoken: ${e.timetoken}`)};return t?this.sendRequest(n,((e,s)=>{r(s),t(e,s)})):this.sendRequest(n).then((e=>(r(e),e)))}}))}signal(e,t){return i(this,void 0,void 0,(function*(){{this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Signal with parameters:"})));const s=new Ot(Object.assign(Object.assign({},e),{keySet:this._configuration.keySet})),n=e=>{e&&this.logger.debug("PubNub",`Publish success with timetoken: ${e.timetoken}`)};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}}))}fire(e,t){return i(this,void 0,void 0,(function*(){return this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Fire with parameters:"}))),null!=t||(t=()=>{}),this.publish(Object.assign(Object.assign({},e),{replicate:!1,storeInHistory:!1}),t)}))}get globalSubscriptionSet(){return this._globalSubscriptionSet||(this._globalSubscriptionSet=this.subscriptionSet({})),this._globalSubscriptionSet}get subscriptionTimetoken(){return this.subscriptionManager?this.subscriptionManager.subscriptionTimetoken:this.eventEngine?this.eventEngine.subscriptionTimetoken:void 0}getSubscribedChannels(){return this.subscriptionManager?this.subscriptionManager.subscribedChannels:this.eventEngine?this.eventEngine.getSubscribedChannels():[]}getSubscribedChannelGroups(){return this.subscriptionManager?this.subscriptionManager.subscribedChannelGroups:this.eventEngine?this.eventEngine.getSubscribedChannelGroups():[]}registerEventHandleCapable(e,t,s){{let n;this.logger.trace("PubNub",(()=>({messageType:"object",message:Object.assign(Object.assign({subscription:e},t?{cursor:t}:[]),s?{subscriptions:s}:{}),details:"Register event handle capable:"}))),this.eventHandleCapable[e.state.id]||(this.eventHandleCapable[e.state.id]=e),s&&0!==s.length?(n=new jt({}),s.forEach((e=>n.add(e.subscriptionInput(!1))))):n=e.subscriptionInput(!1);const r={};r.channels=n.channels,r.channelGroups=n.channelGroups,t&&(r.timetoken=t.timetoken),this.subscriptionManager?this.subscriptionManager.subscribe(r):this.eventEngine&&this.eventEngine.subscribe(r)}}unregisterEventHandleCapable(e,t){{if(!this.eventHandleCapable[e.state.id])return;const s=[];this.logger.trace("PubNub",(()=>({messageType:"object",message:{subscription:e,subscriptions:t},details:"Unregister event handle capable:"})));let n,r=!t||0===t.length;if(!r&&e instanceof _t&&e.subscriptions.length===(null==t?void 0:t.length)&&(r=e.subscriptions.every((e=>t.includes(e)))),r&&delete this.eventHandleCapable[e.state.id],t&&0!==t.length?(n=new jt({}),t.forEach((e=>{const t=e.subscriptionInput(!0);t.isEmpty?s.push(e):n.add(t)}))):(n=e.subscriptionInput(!0),n.isEmpty&&s.push(e)),s.length>0&&this.logger.trace("PubNub",(()=>{const e=[];return s[0]instanceof _t?s[0].subscriptions.forEach((t=>e.push(t.state.entity))):s.forEach((t=>e.push(t.state.entity))),{messageType:"object",message:{entities:e},details:"Can't unregister event handle capable because entities still in use:"}})),n.isEmpty)return;{const e=[],t=[];if(Object.values(this.eventHandleCapable).forEach((s=>{const r=s.subscriptionInput(!1),i=r.channelGroups,a=r.channels;e.push(...n.channelGroups.filter((e=>i.includes(e)))),t.push(...n.channels.filter((e=>a.includes(e))))})),(t.length>0||e.length>0)&&(this.logger.trace("PubNub",(()=>{const s=[],r=n=>{const r=n.subscriptionNames(!0),i=n.subscriptionType===Pt.Channel?t:e;r.some((e=>i.includes(e)))&&s.push(n)};Object.values(this.eventHandleCapable).forEach((e=>{e instanceof _t?e.subscriptions.forEach((e=>{r(e.state.entity)})):e instanceof Mt&&r(e.state.entity)}));let i="Some entities still in use:";return t.length+e.length===n.length&&(i="Can't unregister event handle capable because entities still in use:"),{messageType:"object",message:{entities:s},details:i}})),n.remove(new jt({channels:t,channelGroups:e})),n.isEmpty))return}const i={};i.channels=n.channels,i.channelGroups=n.channelGroups,this.subscriptionManager?this.subscriptionManager.unsubscribe(i):this.eventEngine&&this.eventEngine.unsubscribe(i)}}subscribe(e){{this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Subscribe with parameters:"})));const t=this.subscriptionSet(Object.assign(Object.assign({},e),{subscriptionOptions:{receivePresenceEvents:e.withPresence}}));this.globalSubscriptionSet.addSubscriptionSet(t),t.dispose();const s="number"==typeof e.timetoken?`${e.timetoken}`:e.timetoken;this.globalSubscriptionSet.subscribe({timetoken:s})}}makeSubscribe(e,t){{this._configuration.isSharedWorkerEnabled()||(e.onDemand=!1);const s=new pe(Object.assign(Object.assign({},e),{keySet:this._configuration.keySet,crypto:this._configuration.getCryptoModule(),getFileUrl:this.getFileUrl.bind(this)}));if(this.sendRequest(s,((e,n)=>{var r;this.subscriptionManager&&(null===(r=this.subscriptionManager.abort)||void 0===r?void 0:r.identifier)===s.requestIdentifier&&(this.subscriptionManager.abort=null),t(e,n)})),this.subscriptionManager){const e=()=>s.abort("Cancel long-poll subscribe request");e.identifier=s.requestIdentifier,this.subscriptionManager.abort=e}}}unsubscribe(e){{if(this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Unsubscribe with parameters:"}))),!this._globalSubscriptionSet)return void this.logger.debug("PubNub","There are no active subscriptions. Ignore.");const t=this.globalSubscriptionSet.subscriptions.filter((t=>{var s,n;const r=t.subscriptionInput(!1);if(r.isEmpty)return!1;for(const t of null!==(s=e.channels)&&void 0!==s?s:[])if(r.contains(t))return!0;for(const t of null!==(n=e.channelGroups)&&void 0!==n?n:[])if(r.contains(t))return!0}));t.length>0&&this.globalSubscriptionSet.removeSubscriptions(t)}}makeUnsubscribe(e,t){{let{channels:s,channelGroups:n}=e;if(this._configuration.getKeepPresenceChannelsInPresenceRequests()||(n&&(n=n.filter((e=>!e.endsWith("-pnpres")))),s&&(s=s.filter((e=>!e.endsWith("-pnpres"))))),0===(null!=n?n:[]).length&&0===(null!=s?s:[]).length)return t({error:!1,operation:M.PNUnsubscribeOperation,category:h.PNAcknowledgmentCategory,statusCode:200});this.sendRequest(new Ft({channels:s,channelGroups:n,keySet:this._configuration.keySet}),t)}}unsubscribeAll(){this.logger.debug("PubNub","Unsubscribe all channels and groups"),this._globalSubscriptionSet&&this._globalSubscriptionSet.invalidate(!1),Object.values(this.eventHandleCapable).forEach((e=>e.invalidate(!1))),this.eventHandleCapable={},this.subscriptionManager?this.subscriptionManager.unsubscribeAll():this.eventEngine&&this.eventEngine.unsubscribeAll()}disconnect(e=!1){this.logger.debug("PubNub",`Disconnect (while offline? ${e?"yes":"no"})`),this.subscriptionManager?this.subscriptionManager.disconnect():this.eventEngine&&this.eventEngine.disconnect(e)}reconnect(e){this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Reconnect with parameters:"}))),this.subscriptionManager?this.subscriptionManager.reconnect():this.eventEngine&&this.eventEngine.reconnect(null!=e?e:{})}subscribeHandshake(e){return i(this,void 0,void 0,(function*(){{this._configuration.isSharedWorkerEnabled()||(e.onDemand=!1);const t=new Ct(Object.assign(Object.assign({},e),{keySet:this._configuration.keySet,crypto:this._configuration.getCryptoModule(),getFileUrl:this.getFileUrl.bind(this)})),s=e.abortSignal.subscribe((e=>{t.abort("Cancel subscribe handshake request")}));return this.sendRequest(t).then((e=>(s(),e.cursor)))}}))}subscribeReceiveMessages(e){return i(this,void 0,void 0,(function*(){{this._configuration.isSharedWorkerEnabled()||(e.onDemand=!1);const t=new kt(Object.assign(Object.assign({},e),{keySet:this._configuration.keySet,crypto:this._configuration.getCryptoModule(),getFileUrl:this.getFileUrl.bind(this)})),s=e.abortSignal.subscribe((e=>{t.abort("Cancel long-poll subscribe request")}));return this.sendRequest(t).then((e=>(s(),e)))}}))}getMessageActions(e,t){return i(this,void 0,void 0,(function*(){{this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Get message actions with parameters:"})));const s=new Ht(Object.assign(Object.assign({},e),{keySet:this._configuration.keySet})),n=e=>{e&&this.logger.debug("PubNub",`Get message actions success. Received ${e.data.length} message actions.`)};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}}))}addMessageAction(e,t){return i(this,void 0,void 0,(function*(){{this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Add message action with parameters:"})));const s=new Bt(Object.assign(Object.assign({},e),{keySet:this._configuration.keySet})),n=e=>{e&&this.logger.debug("PubNub",`Message action add success. Message action added with timetoken: ${e.data.actionTimetoken}`)};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}}))}removeMessageAction(e,t){return i(this,void 0,void 0,(function*(){{this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Remove message action with parameters:"})));const s=new Wt(Object.assign(Object.assign({},e),{keySet:this._configuration.keySet})),n=t=>{t&&this.logger.debug("PubNub",`Message action remove success. Removed message action with ${e.actionTimetoken} timetoken.`)};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}}))}fetchMessages(e,t){return i(this,void 0,void 0,(function*(){{this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Fetch messages with parameters:"})));const s=new Kt(Object.assign(Object.assign({},e),{keySet:this._configuration.keySet,crypto:this._configuration.getCryptoModule(),getFileUrl:this.getFileUrl.bind(this)})),n=e=>{if(!e)return;const t=Object.values(e.channels).reduce(((e,t)=>e+t.length),0);this.logger.debug("PubNub",`Fetch messages success. Received ${t} messages.`)};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}}))}deleteMessages(e,t){return i(this,void 0,void 0,(function*(){{this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Delete messages with parameters:"})));const s=new xt(Object.assign(Object.assign({},e),{keySet:this._configuration.keySet})),n=e=>{e&&this.logger.debug("PubNub","Delete messages success.")};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}}))}messageCounts(e,t){return i(this,void 0,void 0,(function*(){{this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Get messages count with parameters:"})));const s=new Lt(Object.assign(Object.assign({},e),{keySet:this._configuration.keySet})),n=t=>{if(!t)return;const s=Object.values(t.channels).reduce(((e,t)=>e+t),0);this.logger.debug("PubNub",`Get messages count success. There are ${s} messages since provided reference timetoken${e.channelTimetokens?e.channelTimetokens.join(","):""}.`)};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}}))}history(e,t){return i(this,void 0,void 0,(function*(){{this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Fetch history with parameters:"})));const s=new qt(Object.assign(Object.assign({},e),{keySet:this._configuration.keySet,crypto:this._configuration.getCryptoModule()})),n=e=>{e&&this.logger.debug("PubNub",`Fetch history success. Received ${e.messages.length} messages.`)};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}}))}hereNow(e,t){return i(this,void 0,void 0,(function*(){{this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Here now with parameters:"})));const s=new $t(Object.assign(Object.assign({},e),{keySet:this._configuration.keySet})),n=e=>{e&&this.logger.debug("PubNub",`Here now success. There are ${e.totalOccupancy} participants in ${e.totalChannels} channels.`)};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}}))}whereNow(e,t){return i(this,void 0,void 0,(function*(){var s;{this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Where now with parameters:"})));const n=new Rt({uuid:null!==(s=e.uuid)&&void 0!==s?s:this._configuration.userId,keySet:this._configuration.keySet}),r=e=>{e&&this.logger.debug("PubNub",`Where now success. Currently present in ${e.channels.length} channels.`)};return t?this.sendRequest(n,((e,s)=>{r(s),t(e,s)})):this.sendRequest(n).then((e=>(r(e),e)))}}))}getState(e,t){return i(this,void 0,void 0,(function*(){var s;{this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Get presence state with parameters:"})));const n=new At(Object.assign(Object.assign({},e),{uuid:null!==(s=e.uuid)&&void 0!==s?s:this._configuration.userId,keySet:this._configuration.keySet})),r=e=>{e&&this.logger.debug("PubNub",`Get presence state success. Received presence state for ${Object.keys(e.channels).length} channels.`)};return t?this.sendRequest(n,((e,s)=>{r(s),t(e,s)})):this.sendRequest(n).then((e=>(r(e),e)))}}))}setState(e,t){return i(this,void 0,void 0,(function*(){var s,n;{this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Set presence state with parameters:"})));const{keySet:r,userId:i}=this._configuration,a=this._configuration.getPresenceTimeout();let o;if(this._configuration.enableEventEngine&&this.presenceState){const t=this.presenceState;null===(s=e.channels)||void 0===s||s.forEach((s=>t[s]=e.state)),"channelGroups"in e&&(null===(n=e.channelGroups)||void 0===n||n.forEach((s=>t[s]=e.state))),this.onPresenceStateChange&&this.onPresenceStateChange(this.presenceState)}o="withHeartbeat"in e&&e.withHeartbeat?new Dt(Object.assign(Object.assign({},e),{keySet:r,heartbeat:a})):new Ut(Object.assign(Object.assign({},e),{keySet:r,uuid:i}));const c=e=>{e&&this.logger.debug("PubNub","Set presence state success."+(o instanceof Dt?" Presence state has been set using heartbeat endpoint.":""))};return this.subscriptionManager&&(this.subscriptionManager.setState(e),this.onPresenceStateChange&&this.onPresenceStateChange(this.subscriptionManager.presenceState)),t?this.sendRequest(o,((e,s)=>{c(s),t(e,s)})):this.sendRequest(o).then((e=>(c(e),e)))}}))}presence(e){var t;this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Change presence with parameters:"}))),null===(t=this.subscriptionManager)||void 0===t||t.changePresence(e)}heartbeat(e,t){return i(this,void 0,void 0,(function*(){var s;{this.logger.trace("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Heartbeat with parameters:"})));let{channels:n,channelGroups:r}=e;if(r&&(r=r.filter((e=>!e.endsWith("-pnpres")))),n&&(n=n.filter((e=>!e.endsWith("-pnpres")))),0===(null!=r?r:[]).length&&0===(null!=n?n:[]).length){const e={error:!1,operation:M.PNHeartbeatOperation,category:h.PNAcknowledgmentCategory,statusCode:200};return this.logger.trace("PubNub","There are no active subscriptions. Ignore."),t?t(e,{}):Promise.resolve(e)}const i=new Dt(Object.assign(Object.assign({},e),{channels:[...new Set(n)],channelGroups:[...new Set(r)],keySet:this._configuration.keySet})),a=e=>{e&&this.logger.trace("PubNub","Heartbeat success.")},o=null===(s=e.abortSignal)||void 0===s?void 0:s.subscribe((e=>{i.abort("Cancel long-poll subscribe request")}));return t?this.sendRequest(i,((e,s)=>{a(s),o&&o(),t(e,s)})):this.sendRequest(i).then((e=>(a(e),o&&o(),e)))}}))}join(e){var t,s;this.logger.trace("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Join with parameters:"}))),e&&0===(null!==(t=e.channels)&&void 0!==t?t:[]).length&&0===(null!==(s=e.groups)&&void 0!==s?s:[]).length?this.logger.trace("PubNub","Ignoring 'join' announcement request."):this.presenceEventEngine?this.presenceEventEngine.join(e):this.heartbeat(Object.assign(Object.assign({channels:e.channels,channelGroups:e.groups},this._configuration.maintainPresenceState&&this.presenceState&&Object.keys(this.presenceState).length>0&&{state:this.presenceState}),{heartbeat:this._configuration.getPresenceTimeout()}),(()=>{}))}presenceReconnect(e){this.logger.trace("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Presence reconnect with parameters:"}))),this.presenceEventEngine?this.presenceEventEngine.reconnect():this.heartbeat(Object.assign(Object.assign({channels:e.channels,channelGroups:e.groups},this._configuration.maintainPresenceState&&{state:this.presenceState}),{heartbeat:this._configuration.getPresenceTimeout()}),(()=>{}))}leave(e){var t,s,n;this.logger.trace("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Leave with parameters:"}))),e&&0===(null!==(t=e.channels)&&void 0!==t?t:[]).length&&0===(null!==(s=e.groups)&&void 0!==s?s:[]).length?this.logger.trace("PubNub","Ignoring 'leave' announcement request."):this.presenceEventEngine?null===(n=this.presenceEventEngine)||void 0===n||n.leave(e):this.makeUnsubscribe({channels:e.channels,channelGroups:e.groups},(()=>{}))}leaveAll(e={}){this.logger.trace("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Leave all with parameters:"}))),this.presenceEventEngine?this.presenceEventEngine.leaveAll(!!e.isOffline):e.isOffline||this.makeUnsubscribe({channels:e.channels,channelGroups:e.groups},(()=>{}))}presenceDisconnect(e){this.logger.trace("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Presence disconnect parameters:"}))),this.presenceEventEngine?this.presenceEventEngine.disconnect(!!e.isOffline):e.isOffline||this.makeUnsubscribe({channels:e.channels,channelGroups:e.groups},(()=>{}))}grantToken(e,t){return i(this,void 0,void 0,(function*(){throw new Error("Grant Token error: PAM module disabled")}))}revokeToken(e,t){return i(this,void 0,void 0,(function*(){throw new Error("Revoke Token error: PAM module disabled")}))}get token(){return this.tokenManager&&this.tokenManager.getToken()}getToken(){return this.token}set token(e){this.tokenManager&&this.tokenManager.setToken(e),this.onAuthenticationChange&&this.onAuthenticationChange(e)}setToken(e){this.token=e}parseToken(e){return this.tokenManager&&this.tokenManager.parseToken(e)}grant(e,t){return i(this,void 0,void 0,(function*(){throw new Error("Grant error: PAM module disabled")}))}audit(e,t){return i(this,void 0,void 0,(function*(){throw new Error("Grant Permissions error: PAM module disabled")}))}get objects(){return this._objects}fetchUsers(e,t){return i(this,void 0,void 0,(function*(){return this.logger.warn("PubNub","'fetchUsers' is deprecated. Use 'pubnub.objects.getAllUUIDMetadata' instead."),this.logger.debug("PubNub",(()=>({messageType:"object",message:e&&"function"!=typeof e?e:{},details:"Fetch all User objects with parameters:"}))),this.objects._getAllUUIDMetadata(e,t)}))}fetchUser(e,t){return i(this,void 0,void 0,(function*(){return this.logger.warn("PubNub","'fetchUser' is deprecated. Use 'pubnub.objects.getUUIDMetadata' instead."),this.logger.debug("PubNub",(()=>({messageType:"object",message:e&&"function"!=typeof e?e:{uuid:this.userId},details:`Fetch${e&&"function"!=typeof e?"":" current"} User object with parameters:`}))),this.objects._getUUIDMetadata(e,t)}))}createUser(e,t){return i(this,void 0,void 0,(function*(){return this.logger.warn("PubNub","'createUser' is deprecated. Use 'pubnub.objects.setUUIDMetadata' instead."),this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Create User object with parameters:"}))),this.objects._setUUIDMetadata(e,t)}))}updateUser(e,t){return i(this,void 0,void 0,(function*(){return this.logger.warn("PubNub","'updateUser' is deprecated. Use 'pubnub.objects.setUUIDMetadata' instead."),this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Update User object with parameters:"}))),this.objects._setUUIDMetadata(e,t)}))}removeUser(e,t){return i(this,void 0,void 0,(function*(){return this.logger.warn("PubNub","'removeUser' is deprecated. Use 'pubnub.objects.removeUUIDMetadata' instead."),this.logger.debug("PubNub",(()=>({messageType:"object",message:e&&"function"!=typeof e?e:{uuid:this.userId},details:`Remove${e&&"function"!=typeof e?"":" current"} User object with parameters:`}))),this.objects._removeUUIDMetadata(e,t)}))}fetchSpaces(e,t){return i(this,void 0,void 0,(function*(){return this.logger.warn("PubNub","'fetchSpaces' is deprecated. Use 'pubnub.objects.getAllChannelMetadata' instead."),this.logger.debug("PubNub",(()=>({messageType:"object",message:e&&"function"!=typeof e?e:{},details:"Fetch all Space objects with parameters:"}))),this.objects._getAllChannelMetadata(e,t)}))}fetchSpace(e,t){return i(this,void 0,void 0,(function*(){return this.logger.warn("PubNub","'fetchSpace' is deprecated. Use 'pubnub.objects.getChannelMetadata' instead."),this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Fetch Space object with parameters:"}))),this.objects._getChannelMetadata(e,t)}))}createSpace(e,t){return i(this,void 0,void 0,(function*(){return this.logger.warn("PubNub","'createSpace' is deprecated. Use 'pubnub.objects.setChannelMetadata' instead."),this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Create Space object with parameters:"}))),this.objects._setChannelMetadata(e,t)}))}updateSpace(e,t){return i(this,void 0,void 0,(function*(){return this.logger.warn("PubNub","'updateSpace' is deprecated. Use 'pubnub.objects.setChannelMetadata' instead."),this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Update Space object with parameters:"}))),this.objects._setChannelMetadata(e,t)}))}removeSpace(e,t){return i(this,void 0,void 0,(function*(){return this.logger.warn("PubNub","'removeSpace' is deprecated. Use 'pubnub.objects.removeChannelMetadata' instead."),this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Remove Space object with parameters:"}))),this.objects._removeChannelMetadata(e,t)}))}fetchMemberships(e,t){return i(this,void 0,void 0,(function*(){return this.objects.fetchMemberships(e,t)}))}addMemberships(e,t){return i(this,void 0,void 0,(function*(){return this.objects.addMemberships(e,t)}))}updateMemberships(e,t){return i(this,void 0,void 0,(function*(){return this.logger.warn("PubNub","'addMemberships' is deprecated. Use 'pubnub.objects.setChannelMembers' or 'pubnub.objects.setMemberships' instead."),this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Update memberships with parameters:"}))),this.objects.addMemberships(e,t)}))}removeMemberships(e,t){return i(this,void 0,void 0,(function*(){var s,n,r;{if(this.logger.warn("PubNub","'removeMemberships' is deprecated. Use 'pubnub.objects.removeMemberships' or 'pubnub.objects.removeChannelMembers' instead."),this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Remove memberships with parameters:"}))),"spaceId"in e){const r=e,i={channel:null!==(s=r.spaceId)&&void 0!==s?s:r.channel,uuids:null!==(n=r.userIds)&&void 0!==n?n:r.uuids,limit:0};return t?this.objects.removeChannelMembers(i,t):this.objects.removeChannelMembers(i)}const i=e,a={uuid:i.userId,channels:null!==(r=i.spaceIds)&&void 0!==r?r:i.channels,limit:0};return t?this.objects.removeMemberships(a,t):this.objects.removeMemberships(a)}}))}get channelGroups(){return this._channelGroups}get push(){return this._push}sendFile(e,t){return i(this,void 0,void 0,(function*(){{if(!this._configuration.PubNubFile)throw new Error("Validation failed: 'PubNubFile' not configured or file upload not supported by the platform.");this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Send file with parameters:"})));const s=new Zt(Object.assign(Object.assign({},e),{keySet:this._configuration.keySet,PubNubFile:this._configuration.PubNubFile,fileUploadPublishRetryLimit:this._configuration.fileUploadPublishRetryLimit,file:e.file,sendRequest:this.sendRequest.bind(this),publishFile:this.publishFile.bind(this),crypto:this._configuration.getCryptoModule(),cryptography:this.cryptography?this.cryptography:void 0})),n={error:!1,operation:M.PNPublishFileOperation,category:h.PNAcknowledgmentCategory,statusCode:0},r=e=>{e&&this.logger.debug("PubNub",`Send file success. File shared with ${e.id} ID.`)};return s.process().then((e=>(n.statusCode=e.status,r(e),t?t(n,e):e))).catch((e=>{let s;throw e instanceof d?s=e.status:e instanceof I&&(s=e.toStatus(n.operation)),this.logger.error("PubNub",(()=>({messageType:"error",message:new d("File sending error. Check status for details",s)}))),t&&s&&t(s,null),new d("REST API request processing error. Check status for details",s)}))}}))}publishFile(e,t){return i(this,void 0,void 0,(function*(){{if(!this._configuration.PubNubFile)throw new Error("Validation failed: 'PubNubFile' not configured or file upload not supported by the platform.");this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Publish file message with parameters:"})));const s=new zt(Object.assign(Object.assign({},e),{keySet:this._configuration.keySet,crypto:this._configuration.getCryptoModule()})),n=e=>{e&&this.logger.debug("PubNub",`Publish file message success. File message published with timetoken: ${e.timetoken}`)};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}}))}listFiles(e,t){return i(this,void 0,void 0,(function*(){{this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"List files with parameters:"})));const s=new Xt(Object.assign(Object.assign({},e),{keySet:this._configuration.keySet})),n=e=>{e&&this.logger.debug("PubNub",`List files success. There are ${e.count} uploaded files.`)};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}}))}getFileUrl(e){var t;{const s=this.transport.request(new Vt(Object.assign(Object.assign({},e),{keySet:this._configuration.keySet})).request()),n=null!==(t=s.queryParameters)&&void 0!==t?t:{},r=Object.keys(n).map((e=>{const t=n[e];return Array.isArray(t)?t.map((t=>`${e}=${R(t)}`)).join("&"):`${e}=${R(t)}`})).join("&");return`${s.origin}${s.path}?${r}`}}downloadFile(e,t){return i(this,void 0,void 0,(function*(){{if(!this._configuration.PubNubFile)throw new Error("Validation failed: 'PubNubFile' not configured or file upload not supported by the platform.");this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Download file with parameters:"})));const s=new Is(Object.assign(Object.assign({},e),{keySet:this._configuration.keySet,PubNubFile:this._configuration.PubNubFile,cryptography:this.cryptography?this.cryptography:void 0,crypto:this._configuration.getCryptoModule()})),n=e=>{e&&this.logger.debug("PubNub","Download file success.")};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):yield this.sendRequest(s).then((e=>(n(e),e)))}}))}deleteFile(e,t){return i(this,void 0,void 0,(function*(){{this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Delete file with parameters:"})));const s=new Jt(Object.assign(Object.assign({},e),{keySet:this._configuration.keySet})),n=t=>{t&&this.logger.debug("PubNub",`Delete file success. Deleted file with ${e.id} ID.`)};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}}))}time(e){return i(this,void 0,void 0,(function*(){this.logger.debug("PubNub","Get service time.");const t=new _s,s=e=>{e&&this.logger.debug("PubNub",`Get service time success. Current timetoken: ${e.timetoken}`)};return e?this.sendRequest(t,((t,n)=>{s(n),e(t,n)})):this.sendRequest(t).then((e=>(s(e),e)))}))}emitStatus(e){var t;null===(t=this.eventDispatcher)||void 0===t||t.handleStatus(e)}emitEvent(e,t){var s;this._globalSubscriptionSet&&this._globalSubscriptionSet.handleEvent(e,t),null===(s=this.eventDispatcher)||void 0===s||s.handleEvent(t),Object.values(this.eventHandleCapable).forEach((s=>{s!==this._globalSubscriptionSet&&s.handleEvent(e,t)}))}set onStatus(e){this.eventDispatcher&&(this.eventDispatcher.onStatus=e)}set onMessage(e){this.eventDispatcher&&(this.eventDispatcher.onMessage=e)}set onPresence(e){this.eventDispatcher&&(this.eventDispatcher.onPresence=e)}set onSignal(e){this.eventDispatcher&&(this.eventDispatcher.onSignal=e)}set onObjects(e){this.eventDispatcher&&(this.eventDispatcher.onObjects=e)}set onMessageAction(e){this.eventDispatcher&&(this.eventDispatcher.onMessageAction=e)}set onFile(e){this.eventDispatcher&&(this.eventDispatcher.onFile=e)}addListener(e){this.eventDispatcher&&this.eventDispatcher.addListener(e)}removeListener(e){this.eventDispatcher&&this.eventDispatcher.removeListener(e)}removeAllListeners(){this.eventDispatcher&&this.eventDispatcher.removeAllListeners()}encrypt(e,t){this.logger.warn("PubNub","'encrypt' is deprecated. Use cryptoModule instead.");const s=this._configuration.getCryptoModule();if(!t&&s&&"string"==typeof e){const t=s.encrypt(e);return"string"==typeof t?t:u(t)}if(!this.crypto)throw new Error("Encryption error: cypher key not set");return this.crypto.encrypt(e,t)}decrypt(e,t){this.logger.warn("PubNub","'decrypt' is deprecated. Use cryptoModule instead.");const s=this._configuration.getCryptoModule();if(!t&&s){const t=s.decrypt(e);return t instanceof ArrayBuffer?JSON.parse((new TextDecoder).decode(t)):t}if(!this.crypto)throw new Error("Decryption error: cypher key not set");return this.crypto.decrypt(e,t)}encryptFile(e,t){return i(this,void 0,void 0,(function*(){var s;if("string"!=typeof e&&(t=e),!t)throw new Error("File encryption error. Source file is missing.");if(!this._configuration.PubNubFile)throw new Error("File encryption error. File constructor not configured.");if("string"!=typeof e&&!this._configuration.getCryptoModule())throw new Error("File encryption error. Crypto module not configured.");if("string"==typeof e){if(!this.cryptography)throw new Error("File encryption error. File encryption not available");return this.cryptography.encryptFile(e,t,this._configuration.PubNubFile)}return null===(s=this._configuration.getCryptoModule())||void 0===s?void 0:s.encryptFile(t,this._configuration.PubNubFile)}))}decryptFile(e,t){return i(this,void 0,void 0,(function*(){var s;if("string"!=typeof e&&(t=e),!t)throw new Error("File encryption error. Source file is missing.");if(!this._configuration.PubNubFile)throw new Error("File decryption error. File constructor not configured.");if("string"==typeof e&&!this._configuration.getCryptoModule())throw new Error("File decryption error. Crypto module not configured.");if("string"==typeof e){if(!this.cryptography)throw new Error("File decryption error. File decryption not available");return this.cryptography.decryptFile(e,t,this._configuration.PubNubFile)}return null===(s=this._configuration.getCryptoModule())||void 0===s?void 0:s.decryptFile(t,this._configuration.PubNubFile)}))}}Ms.decoder=new TextDecoder,Ms.OPERATIONS=M,Ms.CATEGORIES=h,Ms.Endpoint=z,Ms.ExponentialRetryPolicy=V.ExponentialRetryPolicy,Ms.LinearRetryPolicy=V.LinearRetryPolicy,Ms.NoneRetryPolicy=V.None,Ms.LogLevel=F;class As{constructor(e,t){this.decode=e,this.base64ToBinary=t}decodeToken(e){let t="";e.length%4==3?t="=":e.length%4==2&&(t="==");const s=e.replace(/-/gi,"+").replace(/_/gi,"/")+t,n=this.decode(this.base64ToBinary(s));return"object"==typeof n?n:void 0}}class Us extends Ms{constructor(e){var t;const s=void 0!==e.subscriptionWorkerUrl,r=D(e),i=Object.assign(Object.assign({},r),{sdkFamily:"Web"});i.PubNubFile=o;const a=se(i,(e=>{if(e.cipherKey){return new N({default:new E(Object.assign(Object.assign({},e),e.logger?{}:{logger:a.logger()})),cryptors:[new k({cipherKey:e.cipherKey})]})}}));let u,l;e.subscriptionWorkerLogVerbosity?e.subscriptionWorkerLogLevel=F.Debug:void 0===e.subscriptionWorkerLogLevel&&(e.subscriptionWorkerLogLevel=F.None),void 0!==e.subscriptionWorkerLogVerbosity&&a.logger().warn("Configuration","'subscriptionWorkerLogVerbosity' is deprecated. Use 'subscriptionWorkerLogLevel' instead."),a.getCryptoModule()&&(a.getCryptoModule().logger=a.logger()),u=new ie(new As((e=>U(n.decode(e))),c)),(a.getCipherKey()||a.secretKey)&&(l=new P({secretKey:a.secretKey,cipherKey:a.getCipherKey(),useRandomIVs:a.getUseRandomIVs(),customEncrypt:a.getCustomEncrypt(),customDecrypt:a.getCustomDecrypt(),logger:a.logger()}));let h,d=()=>{},p=()=>{},g=()=>{},b=()=>{};h=new j;let y=new ue(a.logger(),i.transport);if(r.subscriptionWorkerUrl)try{const e=new A({clientIdentifier:a._instanceId,subscriptionKey:a.subscribeKey,userId:a.getUserId(),workerUrl:r.subscriptionWorkerUrl,sdkVersion:a.getVersion(),heartbeatInterval:a.getHeartbeatInterval(),announceSuccessfulHeartbeats:a.announceSuccessfulHeartbeats,announceFailedHeartbeats:a.announceFailedHeartbeats,workerOfflineClientsCheckInterval:i.subscriptionWorkerOfflineClientsCheckInterval,workerUnsubscribeOfflineClients:i.subscriptionWorkerUnsubscribeOfflineClients,workerLogLevel:i.subscriptionWorkerLogLevel,tokenManager:u,transport:y,logger:a.logger()});p=t=>e.onPresenceStateChange(t),d=t=>e.onHeartbeatIntervalChange(t),g=t=>e.onTokenChange(t),b=t=>e.onUserIdChange(t),y=e,r.subscriptionWorkerUnsubscribeOfflineClients&&window.addEventListener("pagehide",(t=>{t.persisted||e.terminate()}),{once:!0})}catch(e){a.logger().error("PubNub",(()=>({messageType:"error",message:e})))}else s&&a.logger().warn("PubNub","SharedWorker not supported in this browser. Fallback to the original transport.");const m=new ce({clientConfiguration:a,tokenManager:u,transport:y});if(super({configuration:a,transport:m,cryptography:h,tokenManager:u,crypto:l}),this.onHeartbeatIntervalChange=d,this.onAuthenticationChange=g,this.onPresenceStateChange=p,this.onUserIdChange=b,y instanceof A){y.emitStatus=this.emitStatus.bind(this);const e=this.disconnect.bind(this);this.disconnect=t=>{y.disconnect(),e()}}(null===(t=e.listenToBrowserNetworkEvents)||void 0===t||t)&&(window.addEventListener("offline",(()=>{this.networkDownDetected()})),window.addEventListener("online",(()=>{this.networkUpDetected()})))}networkDownDetected(){this.logger.debug("PubNub","Network down detected"),this.emitStatus({category:Us.CATEGORIES.PNNetworkDownCategory}),this._configuration.restore?this.disconnect(!0):this.destroy(!0)}networkUpDetected(){this.logger.debug("PubNub","Network up detected"),this.emitStatus({category:Us.CATEGORIES.PNNetworkUpCategory}),this.reconnect()}}return Us.CryptoModule=N,Us})); +/*! lil-uuid - v0.1 - MIT License - https://github.com/lil-js/uuid */!function(e,t){!function(e){var t="0.1.0",s={3:/^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i,4:/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,5:/^[0-9A-F]{8}-[0-9A-F]{4}-5[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,all:/^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i};function n(){var e,t,s="";for(e=0;e<32;e++)t=16*Math.random()|0,8!==e&&12!==e&&16!==e&&20!==e||(s+="-"),s+=(12===e?4:16===e?3&t|8:t).toString(16);return s}function r(e,t){var n=s[t||"all"];return n&&n.test(e)||!1}n.isUUID=r,n.VERSION=t,e.uuid=n,e.isUUID=r}(t),null!==e&&(e.exports=t.uuid)}(Z,Z.exports);var ee=t(Z.exports),te={createUUID:()=>ee.uuid?ee.uuid():ee()};const se=(e,t)=>{var s,n,r,i;!e.retryConfiguration&&e.enableEventEngine&&(e.retryConfiguration=V.ExponentialRetryPolicy({minimumDelay:2,maximumDelay:150,maximumRetry:6,excluded:[z.MessageSend,z.Presence,z.Files,z.MessageStorage,z.ChannelGroups,z.DevicePushNotifications,z.AppContext,z.MessageReactions]}));const a=`pn-${te.createUUID()}`;e.logVerbosity?e.logLevel=F.Debug:void 0===e.logLevel&&(e.logLevel=F.None);const o=new Y(re(a),e.logLevel,[...null!==(s=e.loggers)&&void 0!==s?s:[],new W]);void 0!==e.logVerbosity&&o.warn("Configuration","'logVerbosity' is deprecated. Use 'logLevel' instead."),null===(n=e.retryConfiguration)||void 0===n||n.validate(),null!==(r=e.useRandomIVs)&&void 0!==r||(e.useRandomIVs=true),e.useRandomIVs&&o.warn("Configuration","'useRandomIVs' is deprecated. Use 'cryptoModule' instead."),e.origin=ne(null!==(i=e.ssl)&&void 0!==i&&i,e.origin);const c=e.cryptoModule;c&&delete e.cryptoModule;const u=Object.assign(Object.assign({},e),{_pnsdkSuffix:{},_loggerManager:o,_instanceId:a,_cryptoModule:void 0,_cipherKey:void 0,_setupCryptoModule:t,get instanceId(){if(e.useInstanceId)return this._instanceId},getInstanceId(){if(e.useInstanceId)return this._instanceId},getUserId(){return this.userId},setUserId(e){if(!e||"string"!=typeof e||0===e.trim().length)throw new Error("Missing or invalid userId parameter. Provide a valid string userId");this.userId=e},logger(){return this._loggerManager},getAuthKey(){return this.authKey},setAuthKey(e){this.authKey=e},getFilterExpression(){return this.filterExpression},setFilterExpression(e){this.filterExpression=e},getCipherKey(){return this._cipherKey},setCipherKey(t){this._cipherKey=t,t||!this._cryptoModule?t&&this._setupCryptoModule&&(this._cryptoModule=this._setupCryptoModule({cipherKey:t,useRandomIVs:e.useRandomIVs,customEncrypt:this.getCustomEncrypt(),customDecrypt:this.getCustomDecrypt(),logger:this.logger()})):this._cryptoModule=void 0},getCryptoModule(){return this._cryptoModule},getUseRandomIVs:()=>e.useRandomIVs,isSharedWorkerEnabled:()=>"Web"===e.sdkFamily&&e.subscriptionWorkerUrl,getKeepPresenceChannelsInPresenceRequests:()=>"Web"===e.sdkFamily&&e.subscriptionWorkerUrl,setPresenceTimeout(e){this.heartbeatInterval=e/2-1,this.presenceTimeout=e},getPresenceTimeout(){return this.presenceTimeout},getHeartbeatInterval(){return this.heartbeatInterval},setHeartbeatInterval(e){this.heartbeatInterval=e},getTransactionTimeout(){return this.transactionalRequestTimeout},getSubscribeTimeout(){return this.subscribeRequestTimeout},getFileTimeout(){return this.fileRequestTimeout},get PubNubFile(){return e.PubNubFile},get version(){return"9.10.0"},getVersion(){return this.version},_addPnsdkSuffix(e,t){this._pnsdkSuffix[e]=`${t}`},_getPnsdkSuffix(e){const t=Object.values(this._pnsdkSuffix).join(e);return t.length>0?e+t:""},getUUID(){return this.getUserId()},setUUID(e){this.setUserId(e)},getCustomEncrypt:()=>e.customEncrypt,getCustomDecrypt:()=>e.customDecrypt});return e.cipherKey?(o.warn("Configuration","'cipherKey' is deprecated. Use 'cryptoModule' instead."),u.setCipherKey(e.cipherKey)):c&&(u._cryptoModule=c),u},ne=(e,t)=>{const s=e?"https://":"http://";return"string"==typeof t?`${s}${t}`:`${s}${t[Math.floor(Math.random()*t.length)]}`},re=e=>{let t=2166136261;for(let s=0;s>>0;return t.toString(16).padStart(8,"0")};class ie{constructor(e){this.cbor=e}setToken(e){e&&e.length>0?this.token=e:this.token=void 0}getToken(){return this.token}parseToken(e){const t=this.cbor.decodeToken(e);if(void 0!==t){const e=t.res.uuid?Object.keys(t.res.uuid):[],s=Object.keys(t.res.chan),n=Object.keys(t.res.grp),r=t.pat.uuid?Object.keys(t.pat.uuid):[],i=Object.keys(t.pat.chan),a=Object.keys(t.pat.grp),o={version:t.v,timestamp:t.t,ttl:t.ttl,authorized_uuid:t.uuid,signature:t.sig},c=e.length>0,u=s.length>0,l=n.length>0;if(c||u||l){if(o.resources={},c){const s=o.resources.uuids={};e.forEach((e=>s[e]=this.extractPermissions(t.res.uuid[e])))}if(u){const e=o.resources.channels={};s.forEach((s=>e[s]=this.extractPermissions(t.res.chan[s])))}if(l){const e=o.resources.groups={};n.forEach((s=>e[s]=this.extractPermissions(t.res.grp[s])))}}const h=r.length>0,d=i.length>0,p=a.length>0;if(h||d||p){if(o.patterns={},h){const e=o.patterns.uuids={};r.forEach((s=>e[s]=this.extractPermissions(t.pat.uuid[s])))}if(d){const e=o.patterns.channels={};i.forEach((s=>e[s]=this.extractPermissions(t.pat.chan[s])))}if(p){const e=o.patterns.groups={};a.forEach((s=>e[s]=this.extractPermissions(t.pat.grp[s])))}}return t.meta&&Object.keys(t.meta).length>0&&(o.meta=t.meta),o}}extractPermissions(e){const t={read:!1,write:!1,manage:!1,delete:!1,get:!1,update:!1,join:!1};return 128&~e||(t.join=!0),64&~e||(t.update=!0),32&~e||(t.get=!0),8&~e||(t.delete=!0),4&~e||(t.manage=!0),2&~e||(t.write=!0),1&~e||(t.read=!0),t}}var ae;!function(e){e.GET="GET",e.POST="POST",e.PATCH="PATCH",e.DELETE="DELETE",e.LOCAL="LOCAL"}(ae||(ae={}));class oe{constructor(e,t,s,n){this.publishKey=e,this.secretKey=t,this.hasher=s,this.logger=n}signature(e){const t=e.path.startsWith("/publish")?ae.GET:e.method;let s=`${t}\n${this.publishKey}\n${e.path}\n${this.queryParameters(e.queryParameters)}\n`;if(t===ae.POST||t===ae.PATCH){const t=e.body;let n;t&&t instanceof ArrayBuffer?n=oe.textDecoder.decode(t):t&&"object"!=typeof t&&(n=t),n&&(s+=n)}return this.logger.trace("RequestSignature",(()=>({messageType:"text",message:`Request signature input:\n${s}`}))),`v2.${this.hasher(s,this.secretKey)}`.replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")}queryParameters(e){return Object.keys(e).sort().map((t=>{const s=e[t];return Array.isArray(s)?s.sort().map((e=>`${t}=${R(e)}`)).join("&"):`${t}=${R(s)}`})).join("&")}}oe.textDecoder=new TextDecoder("utf-8");class ce{constructor(e){this.configuration=e;const{clientConfiguration:{keySet:t},shaHMAC:s}=e;t.secretKey&&s&&(this.signatureGenerator=new oe(t.publishKey,t.secretKey,s,this.logger))}get logger(){return this.configuration.clientConfiguration.logger()}makeSendable(e){const t=this.configuration.clientConfiguration.retryConfiguration,s=this.configuration.transport;if(void 0!==t){let n,r,i=!1,a=0;const o={abort:e=>{i=!0,n&&clearTimeout(n),r&&r.abort(e)}};return[new Promise(((o,c)=>{const u=()=>{if(i)return;const[l,d]=s.makeSendable(this.request(e));r=d;const p=(s,r)=>{const i=!r||r.category!==h.PNCancelledCategory,l=!s||s.status>=400;let d=-1;i&&l&&t.shouldRetry(e,s,null==r?void 0:r.category,a+1)&&(d=t.getDelay(a,s)),d>0?(a++,this.logger.warn("PubNubMiddleware",`HTTP request retry #${a} in ${d}ms.`),n=setTimeout((()=>u()),d)):s?o(s):r&&c(r)};l.then((e=>p(e))).catch((e=>p(void 0,e)))};u()})),r?o:void 0]}return s.makeSendable(this.request(e))}request(e){var t;const{clientConfiguration:s}=this.configuration;return(e=this.configuration.transport.request(e)).queryParameters||(e.queryParameters={}),s.useInstanceId&&(e.queryParameters.instanceid=s.getInstanceId()),e.queryParameters.uuid||(e.queryParameters.uuid=s.userId),s.useRequestId&&(e.queryParameters.requestid=e.identifier),e.queryParameters.pnsdk=this.generatePNSDK(),null!==(t=e.origin)&&void 0!==t||(e.origin=s.origin),this.authenticateRequest(e),this.signRequest(e),e}authenticateRequest(e){var t;if(e.path.startsWith("/v2/auth/")||e.path.startsWith("/v3/pam/")||e.path.startsWith("/time"))return;const{clientConfiguration:s,tokenManager:n}=this.configuration,r=null!==(t=n&&n.getToken())&&void 0!==t?t:s.authKey;r&&(e.queryParameters.auth=r)}signRequest(e){this.signatureGenerator&&!e.path.startsWith("/time")&&(e.queryParameters.timestamp=String(Math.floor((new Date).getTime()/1e3)),e.queryParameters.signature=this.signatureGenerator.signature(e))}generatePNSDK(){const{clientConfiguration:e}=this.configuration;if(e.sdkName)return e.sdkName;let t=`PubNub-JS-${e.sdkFamily}`;e.partnerId&&(t+=`-${e.partnerId}`),t+=`/${e.getVersion()}`;const s=e._getPnsdkSuffix(" ");return s.length>0&&(t+=s),t}}class ue{constructor(e,t="fetch"){this.logger=e,this.transport=t,e.debug("WebTransport",`Create with configuration:\n - transport: ${t}`),"fetch"!==t||window&&window.fetch||(e.warn("WebTransport",`'${t}' not supported in this browser. Fallback to the 'xhr' transport.`),this.transport="xhr"),"fetch"===this.transport&&(ue.originalFetch=fetch.bind(window),this.isFetchMonkeyPatched()&&(ue.originalFetch=ue.getOriginalFetch(),e.warn("WebTransport","Native Web Fetch API 'fetch' function monkey patched."),this.isFetchMonkeyPatched(ue.originalFetch)?e.warn("WebTransport","Unable receive native Web Fetch API. There can be issues with subscribe long-poll cancellation"):e.info("WebTransport","Use native Web Fetch API 'fetch' implementation from iframe as APM workaround.")))}makeSendable(e){const t=new AbortController,s={abortController:t,abort:e=>{t.signal.aborted||(this.logger.trace("WebTransport",`On-demand request aborting: ${e}`),t.abort(e))}};return[this.webTransportRequestFromTransportRequest(e).then((t=>(this.logger.debug("WebTransport",(()=>({messageType:"network-request",message:e}))),this.sendRequest(t,s).then((e=>e.arrayBuffer().then((t=>[e,t])))).then((e=>{const s=e[1].byteLength>0?e[1]:void 0,{status:n,headers:r}=e[0],i={};r.forEach(((e,t)=>i[t]=e.toLowerCase()));const a={status:n,url:t.url,headers:i,body:s};if(this.logger.debug("WebTransport",(()=>({messageType:"network-response",message:a}))),n>=400)throw I.create(a);return a})).catch((t=>{const s=("string"==typeof t?t:t.message).toLowerCase();let n="string"==typeof t?new Error(t):t;throw s.includes("timeout")?this.logger.warn("WebTransport",(()=>({messageType:"network-request",message:e,details:"Timeout",canceled:!0}))):s.includes("cancel")||s.includes("abort")?(this.logger.debug("WebTransport",(()=>({messageType:"network-request",message:e,details:"Aborted",canceled:!0}))),n=new Error("Aborted"),n.name="AbortError"):s.includes("network")?this.logger.warn("WebTransport",(()=>({messageType:"network-request",message:e,details:"Network error",failed:!0}))):this.logger.warn("WebTransport",(()=>({messageType:"network-request",message:e,details:I.create(n).message,failed:!0}))),I.create(n)}))))),s]}request(e){return e}sendRequest(e,t){return i(this,void 0,void 0,(function*(){return"fetch"===this.transport?this.sendFetchRequest(e,t):this.sendXHRRequest(e,t)}))}sendFetchRequest(e,t){return i(this,void 0,void 0,(function*(){let s;const n=new Promise(((n,r)=>{s=setTimeout((()=>{clearTimeout(s),r(new Error("Request timeout")),t.abort("Cancel because of timeout")}),1e3*e.timeout)})),r=new Request(e.url,{method:e.method,headers:e.headers,redirect:"follow",body:e.body});return Promise.race([ue.originalFetch(r,{signal:t.abortController.signal,credentials:"omit",cache:"no-cache"}).then((e=>(s&&clearTimeout(s),e))),n])}))}sendXHRRequest(e,t){return i(this,void 0,void 0,(function*(){return new Promise(((s,n)=>{var r;const i=new XMLHttpRequest;i.open(e.method,e.url,!0);let a=!1;i.responseType="arraybuffer",i.timeout=1e3*e.timeout,t.abortController.signal.onabort=()=>{i.readyState!=XMLHttpRequest.DONE&&i.readyState!=XMLHttpRequest.UNSENT&&(a=!0,i.abort())},Object.entries(null!==(r=e.headers)&&void 0!==r?r:{}).forEach((([e,t])=>i.setRequestHeader(e,t))),i.onabort=()=>{n(new Error("Aborted"))},i.ontimeout=()=>{n(new Error("Request timeout"))},i.onerror=()=>{if(!a){const t=this.transportResponseFromXHR(e.url,i);n(new Error(I.create(t).message))}},i.onload=()=>{const e=new Headers;i.getAllResponseHeaders().split("\r\n").forEach((t=>{const[s,n]=t.split(": ");s.length>1&&n.length>1&&e.append(s,n)})),s(new Response(i.response,{status:i.status,headers:e,statusText:i.statusText}))},i.send(e.body)}))}))}webTransportRequestFromTransportRequest(e){return i(this,void 0,void 0,(function*(){let t,s=e.path;if(e.formData&&e.formData.length>0){e.queryParameters={};const s=e.body,n=new FormData;for(const{key:t,value:s}of e.formData)n.append(t,s);try{const e=yield s.toArrayBuffer();n.append("file",new Blob([e],{type:"application/octet-stream"}),s.name)}catch(e){this.logger.warn("WebTransport",(()=>({messageType:"error",message:e})));try{const e=yield s.toFileUri();n.append("file",e,s.name)}catch(e){this.logger.error("WebTransport",(()=>({messageType:"error",message:e})))}}t=n}else if(e.body&&("string"==typeof e.body||e.body instanceof ArrayBuffer))if(e.compressible&&"undefined"!=typeof CompressionStream){const s="string"==typeof e.body?ue.encoder.encode(e.body):e.body,n=s.byteLength,r=new ReadableStream({start(e){e.enqueue(s),e.close()}});t=yield new Response(r.pipeThrough(new CompressionStream("deflate"))).arrayBuffer(),this.logger.trace("WebTransport",(()=>{const e=t.byteLength,s=(e/n).toFixed(2);return{messageType:"text",message:`Body of ${n} bytes, compressed by ${s}x to ${e} bytes.`}}))}else t=e.body;return e.queryParameters&&0!==Object.keys(e.queryParameters).length&&(s=`${s}?${q(e.queryParameters)}`),{url:`${e.origin}${s}`,method:e.method,headers:e.headers,timeout:e.timeout,body:t}}))}isFetchMonkeyPatched(e){return!(null!=e?e:fetch).toString().includes("[native code]")&&"fetch"!==fetch.name}transportResponseFromXHR(e,t){const s=t.getAllResponseHeaders().split("\n"),n={};for(const e of s){const[t,s]=e.trim().split(":");t&&s&&(n[t.toLowerCase()]=s.trim())}return{status:t.status,url:e,headers:n,body:t.response}}static getOriginalFetch(){let e=document.querySelector('iframe[name="pubnub-context-unpatched-fetch"]');return e||(e=document.createElement("iframe"),e.style.display="none",e.name="pubnub-context-unpatched-fetch",e.src="about:blank",document.body.appendChild(e)),e.contentWindow?e.contentWindow.fetch.bind(e.contentWindow):fetch}}ue.encoder=new TextEncoder,ue.decoder=new TextDecoder;class le{constructor(e){this.params=e,this.requestIdentifier=te.createUUID(),this._cancellationController=null}get cancellationController(){return this._cancellationController}set cancellationController(e){this._cancellationController=e}abort(e){this&&this.cancellationController&&this.cancellationController.abort(e)}operation(){throw Error("Should be implemented by subclass.")}validate(){}parse(e){return i(this,void 0,void 0,(function*(){return this.deserializeResponse(e)}))}request(){var e,t,s,n,r,i;const a={method:null!==(t=null===(e=this.params)||void 0===e?void 0:e.method)&&void 0!==t?t:ae.GET,path:this.path,queryParameters:this.queryParameters,cancellable:null!==(n=null===(s=this.params)||void 0===s?void 0:s.cancellable)&&void 0!==n&&n,compressible:null!==(i=null===(r=this.params)||void 0===r?void 0:r.compressible)&&void 0!==i&&i,timeout:10,identifier:this.requestIdentifier},o=this.headers;if(o&&(a.headers=o),a.method===ae.POST||a.method===ae.PATCH){const[e,t]=[this.body,this.formData];t&&(a.formData=t),e&&(a.body=e)}return a}get headers(){var e,t;return Object.assign({"Accept-Encoding":"gzip, deflate"},null!==(t=null===(e=this.params)||void 0===e?void 0:e.compressible)&&void 0!==t&&t?{"Content-Encoding":"deflate"}:{})}get path(){throw Error("`path` getter should be implemented by subclass.")}get queryParameters(){return{}}get formData(){}get body(){}deserializeResponse(e){const t=le.decoder.decode(e.body),s=e.headers["content-type"];let n;if(!s||-1===s.indexOf("javascript")&&-1===s.indexOf("json"))throw new d("Service response error, check status for details",g(t,e.status));try{n=JSON.parse(t)}catch(s){throw console.error("Error parsing JSON response:",s),new d("Service response error, check status for details",g(t,e.status))}if("status"in n&&"number"==typeof n.status&&n.status>=400)throw I.create(e);return n}}le.decoder=new TextDecoder;var he;!function(e){e[e.Presence=-2]="Presence",e[e.Message=-1]="Message",e[e.Signal=1]="Signal",e[e.AppContext=2]="AppContext",e[e.MessageAction=3]="MessageAction",e[e.Files=4]="Files"}(he||(he={}));class de extends le{constructor(e){var t,s,n,r,i,a;super({cancellable:!0}),this.parameters=e,null!==(t=(r=this.parameters).withPresence)&&void 0!==t||(r.withPresence=false),null!==(s=(i=this.parameters).channelGroups)&&void 0!==s||(i.channelGroups=[]),null!==(n=(a=this.parameters).channels)&&void 0!==n||(a.channels=[])}operation(){return M.PNSubscribeOperation}validate(){const{keySet:{subscribeKey:e},channels:t,channelGroups:s}=this.parameters;return e?t||s?void 0:"`channels` and `channelGroups` both should not be empty":"Missing Subscribe Key"}parse(e){return i(this,void 0,void 0,(function*(){let t,s;try{s=le.decoder.decode(e.body);t=JSON.parse(s)}catch(e){console.error("Error parsing JSON response:",e)}if(!t)throw new d("Service response error, check status for details",g(s,e.status));const n=t.m.filter((e=>{const t=void 0===e.b?e.c:e.b;return this.parameters.channels&&this.parameters.channels.includes(t)||this.parameters.channelGroups&&this.parameters.channelGroups.includes(t)})).map((e=>{let{e:t}=e;null!=t||(t=e.c.endsWith("-pnpres")?he.Presence:he.Message);const s=B(e.d);return t!=he.Signal&&"string"==typeof e.d?t==he.Message?{type:he.Message,data:this.messageFromEnvelope(e),pn_mfp:s}:{type:he.Files,data:this.fileFromEnvelope(e),pn_mfp:s}:t==he.Message?{type:he.Message,data:this.messageFromEnvelope(e),pn_mfp:s}:t===he.Presence?{type:he.Presence,data:this.presenceEventFromEnvelope(e),pn_mfp:s}:t==he.Signal?{type:he.Signal,data:this.signalFromEnvelope(e),pn_mfp:s}:t===he.AppContext?{type:he.AppContext,data:this.appContextFromEnvelope(e),pn_mfp:s}:t===he.MessageAction?{type:he.MessageAction,data:this.messageActionFromEnvelope(e),pn_mfp:s}:{type:he.Files,data:this.fileFromEnvelope(e),pn_mfp:s}}));return{cursor:{timetoken:t.t.t,region:t.t.r},messages:n}}))}get headers(){var e;return Object.assign(Object.assign({},null!==(e=super.headers)&&void 0!==e?e:{}),{accept:"text/javascript"})}presenceEventFromEnvelope(e){var t;const{d:s}=e,[n,r]=this.subscriptionChannelFromEnvelope(e),i=n.replace("-pnpres",""),a=null!==r?i:null,o=null!==r?r:i;return"string"!=typeof s&&("data"in s?(s.state=s.data,delete s.data):"action"in s&&"interval"===s.action&&(s.hereNowRefresh=null!==(t=s.here_now_refresh)&&void 0!==t&&t,delete s.here_now_refresh)),Object.assign({channel:i,subscription:r,actualChannel:a,subscribedChannel:o,timetoken:e.p.t},s)}messageFromEnvelope(e){const[t,s]=this.subscriptionChannelFromEnvelope(e),[n,r]=this.decryptedData(e.d),i={channel:t,subscription:s,actualChannel:null!==s?t:null,subscribedChannel:null!==s?s:t,timetoken:e.p.t,publisher:e.i,message:n};return e.u&&(i.userMetadata=e.u),e.cmt&&(i.customMessageType=e.cmt),r&&(i.error=r),i}signalFromEnvelope(e){const[t,s]=this.subscriptionChannelFromEnvelope(e),n={channel:t,subscription:s,timetoken:e.p.t,publisher:e.i,message:e.d};return e.u&&(n.userMetadata=e.u),e.cmt&&(n.customMessageType=e.cmt),n}messageActionFromEnvelope(e){const[t,s]=this.subscriptionChannelFromEnvelope(e),n=e.d;return{channel:t,subscription:s,timetoken:e.p.t,publisher:e.i,event:n.event,data:Object.assign(Object.assign({},n.data),{uuid:e.i})}}appContextFromEnvelope(e){const[t,s]=this.subscriptionChannelFromEnvelope(e),n=e.d;return{channel:t,subscription:s,timetoken:e.p.t,message:n}}fileFromEnvelope(e){const[t,s]=this.subscriptionChannelFromEnvelope(e),[n,r]=this.decryptedData(e.d);let i=r;const a={channel:t,subscription:s,timetoken:e.p.t,publisher:e.i};return e.u&&(a.userMetadata=e.u),n?"string"==typeof n?null!=i||(i="Unexpected file information payload data type."):(a.message=n.message,n.file&&(a.file={id:n.file.id,name:n.file.name,url:this.parameters.getFileUrl({id:n.file.id,name:n.file.name,channel:t})})):null!=i||(i="File information payload is missing."),e.cmt&&(a.customMessageType=e.cmt),i&&(a.error=i),a}subscriptionChannelFromEnvelope(e){return[e.c,void 0===e.b?e.c:e.b]}decryptedData(e){if(!this.parameters.crypto||"string"!=typeof e)return[e,void 0];let t,s;try{const s=this.parameters.crypto.decrypt(e);t=s instanceof ArrayBuffer?JSON.parse(pe.decoder.decode(s)):s}catch(e){t=null,s=`Error while decrypting message content: ${e.message}`}return[null!=t?t:e,s]}}class pe extends de{get path(){var e;const{keySet:{subscribeKey:t},channels:s}=this.parameters;return`/v2/subscribe/${t}/${$(null!==(e=null==s?void 0:s.sort())&&void 0!==e?e:[],",")}/0`}get queryParameters(){const{channelGroups:e,filterExpression:t,heartbeat:s,state:n,timetoken:r,region:i,onDemand:a}=this.parameters,o={};return a&&(o["on-demand"]=1),e&&e.length>0&&(o["channel-group"]=e.sort().join(",")),t&&t.length>0&&(o["filter-expr"]=t),s&&(o.heartbeat=s),n&&Object.keys(n).length>0&&(o.state=JSON.stringify(n)),void 0!==r&&"string"==typeof r?r.length>0&&"0"!==r&&(o.tt=r):void 0!==r&&r>0&&(o.tt=r),i&&(o.tr=i),o}}class ge{constructor(){this.hasListeners=!1,this.listeners=[{count:-1,listener:{}}]}set onStatus(e){this.updateTypeOrObjectListener({add:!!e,listener:e,type:"status"})}set onMessage(e){this.updateTypeOrObjectListener({add:!!e,listener:e,type:"message"})}set onPresence(e){this.updateTypeOrObjectListener({add:!!e,listener:e,type:"presence"})}set onSignal(e){this.updateTypeOrObjectListener({add:!!e,listener:e,type:"signal"})}set onObjects(e){this.updateTypeOrObjectListener({add:!!e,listener:e,type:"objects"})}set onMessageAction(e){this.updateTypeOrObjectListener({add:!!e,listener:e,type:"messageAction"})}set onFile(e){this.updateTypeOrObjectListener({add:!!e,listener:e,type:"file"})}handleEvent(e){if(this.hasListeners)if(e.type===he.Message)this.announce("message",e.data);else if(e.type===he.Signal)this.announce("signal",e.data);else if(e.type===he.Presence)this.announce("presence",e.data);else if(e.type===he.AppContext){const{data:t}=e,{message:s}=t;if(this.announce("objects",t),"uuid"===s.type){const{message:e,channel:n}=t,i=r(t,["message","channel"]),{event:a,type:o}=s,c=r(s,["event","type"]),u=Object.assign(Object.assign({},i),{spaceId:n,message:Object.assign(Object.assign({},c),{event:"set"===a?"updated":"removed",type:"user"})});this.announce("user",u)}else if("channel"===s.type){const{message:e,channel:n}=t,i=r(t,["message","channel"]),{event:a,type:o}=s,c=r(s,["event","type"]),u=Object.assign(Object.assign({},i),{spaceId:n,message:Object.assign(Object.assign({},c),{event:"set"===a?"updated":"removed",type:"space"})});this.announce("space",u)}else if("membership"===s.type){const{message:e,channel:n}=t,i=r(t,["message","channel"]),{event:a,data:o}=s,c=r(s,["event","data"]),{uuid:u,channel:l}=o,h=r(o,["uuid","channel"]),d=Object.assign(Object.assign({},i),{spaceId:n,message:Object.assign(Object.assign({},c),{event:"set"===a?"updated":"removed",data:Object.assign(Object.assign({},h),{user:u,space:l})})});this.announce("membership",d)}}else e.type===he.MessageAction?this.announce("messageAction",e.data):e.type===he.Files&&this.announce("file",e.data)}handleStatus(e){this.hasListeners&&this.announce("status",e)}addListener(e){this.updateTypeOrObjectListener({add:!0,listener:e})}removeListener(e){this.updateTypeOrObjectListener({add:!1,listener:e})}removeAllListeners(){this.listeners=[{count:-1,listener:{}}],this.hasListeners=!1}updateTypeOrObjectListener(e){if(e.type)"function"==typeof e.listener?this.listeners[0].listener[e.type]=e.listener:delete this.listeners[0].listener[e.type];else if(e.listener&&"function"!=typeof e.listener){let t,s=!1;for(t of this.listeners)if(t.listener===e.listener){e.add?(t.count++,s=!0):(t.count--,0===t.count&&this.listeners.splice(this.listeners.indexOf(t),1));break}e.add&&!s&&this.listeners.push({count:1,listener:e.listener})}this.hasListeners=this.listeners.length>1||Object.keys(this.listeners[0]).length>0}announce(e,t){this.listeners.forEach((({listener:s})=>{const n=s[e];n&&n(t)}))}}class be{constructor(e){this.time=e}onReconnect(e){this.callback=e}startPolling(){this.timeTimer=setInterval((()=>this.callTime()),3e3)}stopPolling(){this.timeTimer&&clearInterval(this.timeTimer),this.timeTimer=null}callTime(){this.time((e=>{e.error||(this.stopPolling(),this.callback&&this.callback())}))}}class ye{constructor(e){this.config=e,e.logger().debug("DedupingManager",(()=>({messageType:"object",message:{maximumCacheSize:e.maximumCacheSize},details:"Create with configuration:"}))),this.maximumCacheSize=e.maximumCacheSize,this.hashHistory=[]}getKey(e){var t;return`${e.timetoken}-${this.hashCode(JSON.stringify(null!==(t=e.message)&&void 0!==t?t:"")).toString()}`}isDuplicate(e){return this.hashHistory.includes(this.getKey(e))}addEntry(e){this.hashHistory.length>=this.maximumCacheSize&&this.hashHistory.shift(),this.hashHistory.push(this.getKey(e))}clearHistory(){this.hashHistory=[]}hashCode(e){let t=0;if(0===e.length)return t;for(let s=0;s{this.pendingChannelSubscriptions.add(e),this.channels[e]={},r&&(this.presenceChannels[e]={}),(i||this.configuration.getHeartbeatInterval())&&(this.heartbeatChannels[e]={})})),null==s||s.forEach((e=>{this.pendingChannelGroupSubscriptions.add(e),this.channelGroups[e]={},r&&(this.presenceChannelGroups[e]={}),(i||this.configuration.getHeartbeatInterval())&&(this.heartbeatChannelGroups[e]={})})),this.subscriptionStatusAnnounced=!1,this.reconnect()}unsubscribe(e,t=!1){let{channels:s,channelGroups:n}=e;const i=new Set,a=new Set;if(null==s||s.forEach((e=>{e in this.channels&&(delete this.channels[e],a.add(e),e in this.heartbeatChannels&&delete this.heartbeatChannels[e]),e in this.presenceState&&delete this.presenceState[e],e in this.presenceChannels&&(delete this.presenceChannels[e],a.add(e))})),null==n||n.forEach((e=>{e in this.channelGroups&&(delete this.channelGroups[e],i.add(e),e in this.heartbeatChannelGroups&&delete this.heartbeatChannelGroups[e]),e in this.presenceState&&delete this.presenceState[e],e in this.presenceChannelGroups&&(delete this.presenceChannelGroups[e],i.add(e))})),0===a.size&&0===i.size)return;const o=this.lastTimetoken,c=this.currentTimetoken;0===Object.keys(this.channels).length&&0===Object.keys(this.presenceChannels).length&&0===Object.keys(this.channelGroups).length&&0===Object.keys(this.presenceChannelGroups).length&&(this.lastTimetoken="0",this.currentTimetoken="0",this.referenceTimetoken=null,this.storedTimetoken=null,this.region=null,this.reconnectionManager.stopPolling()),this.reconnect(!0),!1!==this.configuration.suppressLeaveEvents||t||(n=Array.from(i),s=Array.from(a),this.leaveCall({channels:s,channelGroups:n},(e=>{const{error:t}=e,i=r(e,["error"]);let a;t&&(e.errorData&&"object"==typeof e.errorData&&"message"in e.errorData&&"string"==typeof e.errorData.message?a=e.errorData.message:"message"in e&&"string"==typeof e.message&&(a=e.message)),this.emitStatus(Object.assign(Object.assign({},i),{error:null!=a&&a,affectedChannels:s,affectedChannelGroups:n,currentTimetoken:c,lastTimetoken:o}))})))}unsubscribeAll(e=!1){this.disconnectedWhileHandledEvent=!0,this.unsubscribe({channels:this.subscribedChannels,channelGroups:this.subscribedChannelGroups},e)}startSubscribeLoop(e=!1){this.disconnectedWhileHandledEvent=!1,this.stopSubscribeLoop();const t=[...Object.keys(this.channelGroups)],s=[...Object.keys(this.channels)];Object.keys(this.presenceChannelGroups).forEach((e=>t.push(`${e}-pnpres`))),Object.keys(this.presenceChannels).forEach((e=>s.push(`${e}-pnpres`))),0===s.length&&0===t.length||(this.subscribeCall(Object.assign(Object.assign(Object.assign({channels:s,channelGroups:t,state:this.presenceState,heartbeat:this.configuration.getPresenceTimeout(),timetoken:this.currentTimetoken},null!==this.region?{region:this.region}:{}),this.configuration.filterExpression?{filterExpression:this.configuration.filterExpression}:{}),{onDemand:!this.subscriptionStatusAnnounced||e}),((e,t)=>{this.processSubscribeResponse(e,t)})),!e&&this.configuration.useSmartHeartbeat&&this.startHeartbeatTimer())}stopSubscribeLoop(){this._subscribeAbort&&(this._subscribeAbort(),this._subscribeAbort=null)}processSubscribeResponse(e,t){if(e.error){if("object"==typeof e.errorData&&"name"in e.errorData&&"AbortError"===e.errorData.name||e.category===h.PNCancelledCategory)return;return void(e.category===h.PNTimeoutCategory?this.startSubscribeLoop():e.category===h.PNNetworkIssuesCategory||e.category===h.PNMalformedResponseCategory?(this.disconnect(),e.error&&this.configuration.autoNetworkDetection&&this.isOnline&&(this.isOnline=!1,this.emitStatus({category:h.PNNetworkDownCategory})),this.reconnectionManager.onReconnect((()=>{this.configuration.autoNetworkDetection&&!this.isOnline&&(this.isOnline=!0,this.emitStatus({category:h.PNNetworkUpCategory})),this.reconnect(),this.subscriptionStatusAnnounced=!0;const t={category:h.PNReconnectedCategory,operation:e.operation,lastTimetoken:this.lastTimetoken,currentTimetoken:this.currentTimetoken};this.emitStatus(t)})),this.reconnectionManager.startPolling(),this.emitStatus(Object.assign(Object.assign({},e),{category:h.PNNetworkIssuesCategory}))):e.category===h.PNBadRequestCategory?(this.stopHeartbeatTimer(),this.emitStatus(e)):this.emitStatus(e))}if(this.referenceTimetoken=K(t.cursor.timetoken,this.storedTimetoken),this.storedTimetoken?(this.currentTimetoken=this.storedTimetoken,this.storedTimetoken=null):(this.lastTimetoken=this.currentTimetoken,this.currentTimetoken=t.cursor.timetoken),!this.subscriptionStatusAnnounced){const t={category:h.PNConnectedCategory,operation:e.operation,affectedChannels:Array.from(this.pendingChannelSubscriptions),subscribedChannels:this.subscribedChannels,affectedChannelGroups:Array.from(this.pendingChannelGroupSubscriptions),lastTimetoken:this.lastTimetoken,currentTimetoken:this.currentTimetoken};this.subscriptionStatusAnnounced=!0,this.emitStatus(t),this.pendingChannelGroupSubscriptions.clear(),this.pendingChannelSubscriptions.clear()}const{messages:s}=t,{requestMessageCountThreshold:n,dedupeOnSubscribe:r}=this.configuration;n&&s.length>=n&&this.emitStatus({category:h.PNRequestMessageCountExceededCategory,operation:e.operation});try{const e={timetoken:this.currentTimetoken,region:this.region?this.region:void 0};this.configuration.logger().debug("SubscriptionManager",(()=>({messageType:"object",message:s.map((e=>({type:e.type,data:Object.assign(Object.assign({},e.data),{pn_mfp:e.pn_mfp})}))),details:"Received events:"}))),s.forEach((t=>{if(r&&"message"in t.data&&"timetoken"in t.data){if(this.dedupingManager.isDuplicate(t.data))return void this.configuration.logger().warn("SubscriptionManager",(()=>({messageType:"object",message:t.data,details:"Duplicate message detected (skipped):"})));this.dedupingManager.addEntry(t.data)}this.emitEvent(e,t)}))}catch(e){const t={error:!0,category:h.PNUnknownCategory,errorData:e,statusCode:0};this.emitStatus(t)}this.region=t.cursor.region,this.disconnectedWhileHandledEvent?this.disconnectedWhileHandledEvent=!1:this.startSubscribeLoop()}setState(e){const{state:t,channels:s,channelGroups:n}=e;null==s||s.forEach((e=>e in this.channels&&(this.presenceState[e]=t))),null==n||n.forEach((e=>e in this.channelGroups&&(this.presenceState[e]=t)))}changePresence(e){const{connected:t,channels:s,channelGroups:n}=e;t?(null==s||s.forEach((e=>this.heartbeatChannels[e]={})),null==n||n.forEach((e=>this.heartbeatChannelGroups[e]={}))):(null==s||s.forEach((e=>{e in this.heartbeatChannels&&delete this.heartbeatChannels[e]})),null==n||n.forEach((e=>{e in this.heartbeatChannelGroups&&delete this.heartbeatChannelGroups[e]})),!1===this.configuration.suppressLeaveEvents&&this.leaveCall({channels:s,channelGroups:n},(e=>this.emitStatus(e)))),this.reconnect()}startHeartbeatTimer(){this.stopHeartbeatTimer();const e=this.configuration.getHeartbeatInterval();e&&0!==e&&(this.configuration.useSmartHeartbeat||this.sendHeartbeat(),this.heartbeatTimer=setInterval((()=>this.sendHeartbeat()),1e3*e))}stopHeartbeatTimer(){this.heartbeatTimer&&(clearInterval(this.heartbeatTimer),this.heartbeatTimer=null)}sendHeartbeat(){const e=Object.keys(this.heartbeatChannelGroups),t=Object.keys(this.heartbeatChannels);0===t.length&&0===e.length||this.heartbeatCall({channels:t,channelGroups:e,heartbeat:this.configuration.getPresenceTimeout(),state:this.presenceState},(e=>{e.error&&this.configuration.announceFailedHeartbeats&&this.emitStatus(e),e.error&&this.configuration.autoNetworkDetection&&this.isOnline&&(this.isOnline=!1,this.disconnect(),this.emitStatus({category:h.PNNetworkDownCategory}),this.reconnect()),!e.error&&this.configuration.announceSuccessfulHeartbeats&&this.emitStatus(e)}))}}class fe{constructor(e,t,s){this._payload=e,this.setDefaultPayloadStructure(),this.title=t,this.body=s}get payload(){return this._payload}set title(e){this._title=e}set subtitle(e){this._subtitle=e}set body(e){this._body=e}set badge(e){this._badge=e}set sound(e){this._sound=e}setDefaultPayloadStructure(){}toObject(){return{}}}class ve extends fe{constructor(){super(...arguments),this._apnsPushType="apns",this._isSilent=!1}get payload(){return this._payload}set configurations(e){e&&e.length&&(this._configurations=e)}get notification(){return this.payload.aps}get title(){return this._title}set title(e){e&&e.length&&(this.payload.aps.alert.title=e,this._title=e)}get subtitle(){return this._subtitle}set subtitle(e){e&&e.length&&(this.payload.aps.alert.subtitle=e,this._subtitle=e)}get body(){return this._body}set body(e){e&&e.length&&(this.payload.aps.alert.body=e,this._body=e)}get badge(){return this._badge}set badge(e){null!=e&&(this.payload.aps.badge=e,this._badge=e)}get sound(){return this._sound}set sound(e){e&&e.length&&(this.payload.aps.sound=e,this._sound=e)}set silent(e){this._isSilent=e}setDefaultPayloadStructure(){this.payload.aps={alert:{}}}toObject(){const e=Object.assign({},this.payload),{aps:t}=e;let{alert:s}=t;if(this._isSilent&&(t["content-available"]=1),"apns2"===this._apnsPushType){if(!this._configurations||!this._configurations.length)throw new ReferenceError("APNS2 configuration is missing");const t=[];this._configurations.forEach((e=>{t.push(this.objectFromAPNS2Configuration(e))})),t.length&&(e.pn_push=t)}return s&&Object.keys(s).length||delete t.alert,this._isSilent&&(delete t.alert,delete t.badge,delete t.sound,s={}),this._isSilent||s&&Object.keys(s).length?e:null}objectFromAPNS2Configuration(e){if(!e.targets||!e.targets.length)throw new ReferenceError("At least one APNS2 target should be provided");const{collapseId:t,expirationDate:s}=e,n={auth_method:"token",targets:e.targets.map((e=>this.objectFromAPNSTarget(e))),version:"v2"};return t&&t.length&&(n.collapse_id=t),s&&(n.expiration=s.toISOString()),n}objectFromAPNSTarget(e){if(!e.topic||!e.topic.length)throw new TypeError("Target 'topic' undefined.");const{topic:t,environment:s="development",excludedDevices:n=[]}=e,r={topic:t,environment:s};return n.length&&(r.excluded_devices=n),r}}class Se extends fe{get payload(){return this._payload}get notification(){return this.payload.notification}get data(){return this.payload.data}get title(){return this._title}set title(e){e&&e.length&&(this.payload.notification.title=e,this._title=e)}get body(){return this._body}set body(e){e&&e.length&&(this.payload.notification.body=e,this._body=e)}get sound(){return this._sound}set sound(e){e&&e.length&&(this.payload.notification.sound=e,this._sound=e)}get icon(){return this._icon}set icon(e){e&&e.length&&(this.payload.notification.icon=e,this._icon=e)}get tag(){return this._tag}set tag(e){e&&e.length&&(this.payload.notification.tag=e,this._tag=e)}set silent(e){this._isSilent=e}setDefaultPayloadStructure(){this.payload.notification={},this.payload.data={}}toObject(){let e=Object.assign({},this.payload.data),t=null;const s={};if(Object.keys(this.payload).length>2){const t=r(this.payload,["notification","data"]);e=Object.assign(Object.assign({},e),t)}return this._isSilent?e.notification=this.payload.notification:t=this.payload.notification,Object.keys(e).length&&(s.data=e),t&&Object.keys(t).length&&(s.notification=t),Object.keys(s).length?s:null}}class we{constructor(e,t){this._payload={apns:{},fcm:{}},this._title=e,this._body=t,this.apns=new ve(this._payload.apns,e,t),this.fcm=new Se(this._payload.fcm,e,t)}set debugging(e){this._debugging=e}get title(){return this._title}get subtitle(){return this._subtitle}set subtitle(e){this._subtitle=e,this.apns.subtitle=e,this.fcm.subtitle=e}get body(){return this._body}get badge(){return this._badge}set badge(e){this._badge=e,this.apns.badge=e,this.fcm.badge=e}get sound(){return this._sound}set sound(e){this._sound=e,this.apns.sound=e,this.fcm.sound=e}buildPayload(e){const t={};if(e.includes("apns")||e.includes("apns2")){this.apns._apnsPushType=e.includes("apns")?"apns":"apns2";const s=this.apns.toObject();s&&Object.keys(s).length&&(t.pn_apns=s)}if(e.includes("fcm")){const e=this.fcm.toObject();e&&Object.keys(e).length&&(t.pn_gcm=e)}return Object.keys(t).length&&this._debugging&&(t.pn_debug=!0),t}}class Oe{constructor(e=!1){this.sync=e,this.listeners=new Set}subscribe(e){return this.listeners.add(e),()=>{this.listeners.delete(e)}}notify(e){const t=()=>{this.listeners.forEach((t=>{t(e)}))};this.sync?t():setTimeout(t,0)}}class ke{transition(e,t){var s;if(this.transitionMap.has(t.type))return null===(s=this.transitionMap.get(t.type))||void 0===s?void 0:s(e,t)}constructor(e){this.label=e,this.transitionMap=new Map,this.enterEffects=[],this.exitEffects=[]}on(e,t){return this.transitionMap.set(e,t),this}with(e,t){return[this,e,null!=t?t:[]]}onEnter(e){return this.enterEffects.push(e),this}onExit(e){return this.exitEffects.push(e),this}}class Ce extends Oe{constructor(e){super(!0),this.logger=e,this._pendingEvents=[],this._inTransition=!1}get currentState(){return this._currentState}get currentContext(){return this._currentContext}describe(e){return new ke(e)}start(e,t){this._currentState=e,this._currentContext=t,this.notify({type:"engineStarted",state:e,context:t})}transition(e){if(!this._currentState)throw this.logger.error("Engine","Finite state machine is not started"),new Error("Start the engine first");if(this._inTransition)return this.logger.trace("Engine",(()=>({messageType:"object",message:e,details:"Event engine in transition. Enqueue received event:"}))),void this._pendingEvents.push(e);this._inTransition=!0,this.logger.trace("Engine",(()=>({messageType:"object",message:e,details:"Event engine received event:"}))),this.notify({type:"eventReceived",event:e});const t=this._currentState.transition(this._currentContext,e);if(t){const[s,n,r]=t;this.logger.trace("Engine",`Exiting state: ${this._currentState.label}`);for(const e of this._currentState.exitEffects)this.notify({type:"invocationDispatched",invocation:e(this._currentContext)});this.logger.trace("Engine",(()=>({messageType:"object",details:`Entering '${s.label}' state with context:`,message:n})));const i=this._currentState;this._currentState=s;const a=this._currentContext;this._currentContext=n,this.notify({type:"transitionDone",fromState:i,fromContext:a,toState:s,toContext:n,event:e});for(const e of r)this.notify({type:"invocationDispatched",invocation:e});for(const e of this._currentState.enterEffects)this.notify({type:"invocationDispatched",invocation:e(this._currentContext)})}else this.logger.warn("Engine",`No transition from '${this._currentState.label}' found for event: ${e.type}`);if(this._inTransition=!1,this._pendingEvents.length>0){const e=this._pendingEvents.shift();e&&(this.logger.trace("Engine",(()=>({messageType:"object",message:e,details:"De-queueing pending event:"}))),this.transition(e))}}}class Pe{constructor(e,t){this.dependencies=e,this.logger=t,this.instances=new Map,this.handlers=new Map}on(e,t){this.handlers.set(e,t)}dispatch(e){if(this.logger.trace("Dispatcher",`Process invocation: ${e.type}`),"CANCEL"===e.type){if(this.instances.has(e.payload)){const t=this.instances.get(e.payload);null==t||t.cancel(),this.instances.delete(e.payload)}return}const t=this.handlers.get(e.type);if(!t)throw this.logger.error("Dispatcher",`Unhandled invocation '${e.type}'`),new Error(`Unhandled invocation '${e.type}'`);const s=t(e.payload,this.dependencies);this.logger.trace("Dispatcher",(()=>({messageType:"object",details:"Call invocation handler with parameters:",message:e.payload,ignoredKeys:["abortSignal"]}))),e.managed&&this.instances.set(e.type,s),s.start()}dispose(){for(const[e,t]of this.instances.entries())t.cancel(),this.instances.delete(e)}}function je(e,t){const s=function(...s){return{type:e,payload:null==t?void 0:t(...s)}};return s.type=e,s}function Ee(e,t){const s=(...s)=>({type:e,payload:t(...s),managed:!1});return s.type=e,s}function Ne(e,t){const s=(...s)=>({type:e,payload:t(...s),managed:!0});return s.type=e,s.cancel={type:"CANCEL",payload:e,managed:!1},s}class Te extends Error{constructor(){super("The operation was aborted."),this.name="AbortError",Object.setPrototypeOf(this,new.target.prototype)}}class _e extends Oe{constructor(){super(...arguments),this._aborted=!1}get aborted(){return this._aborted}throwIfAborted(){if(this._aborted)throw new Te}abort(){this._aborted=!0,this.notify(new Te)}}class Ie{constructor(e,t){this.payload=e,this.dependencies=t}}class Me extends Ie{constructor(e,t,s){super(e,t),this.asyncFunction=s,this.abortSignal=new _e}start(){this.asyncFunction(this.payload,this.abortSignal,this.dependencies).catch((e=>{}))}cancel(){this.abortSignal.abort()}}const Ae=e=>(t,s)=>new Me(t,s,e),Ue=Ne("HEARTBEAT",((e,t)=>({channels:e,groups:t}))),De=Ee("LEAVE",((e,t)=>({channels:e,groups:t}))),Fe=Ee("EMIT_STATUS",(e=>e)),Re=Ne("WAIT",(()=>({}))),$e=je("RECONNECT",(()=>({}))),xe=je("DISCONNECT",((e=!1)=>({isOffline:e}))),Le=je("JOINED",((e,t)=>({channels:e,groups:t}))),qe=je("LEFT",((e,t)=>({channels:e,groups:t}))),Ge=je("LEFT_ALL",((e=!1)=>({isOffline:e}))),Ke=je("HEARTBEAT_SUCCESS",(e=>({statusCode:e}))),He=je("HEARTBEAT_FAILURE",(e=>e)),Be=je("TIMES_UP",(()=>({})));class We extends Pe{constructor(e,t){super(t,t.config.logger()),this.on(Ue.type,Ae(((t,s,n)=>i(this,[t,s,n],void 0,(function*(t,s,{heartbeat:n,presenceState:r,config:i}){s.throwIfAborted();try{yield n(Object.assign(Object.assign({abortSignal:s,channels:t.channels,channelGroups:t.groups},i.maintainPresenceState&&{state:r}),{heartbeat:i.presenceTimeout}));e.transition(Ke(200))}catch(t){if(t instanceof d){if(t.status&&t.status.category==h.PNCancelledCategory)return;e.transition(He(t))}}}))))),this.on(De.type,Ae(((e,t,s)=>i(this,[e,t,s],void 0,(function*(e,t,{leave:s,config:n}){if(!n.suppressLeaveEvents)try{s({channels:e.channels,channelGroups:e.groups})}catch(e){}}))))),this.on(Re.type,Ae(((t,s,n)=>i(this,[t,s,n],void 0,(function*(t,s,{heartbeatDelay:n}){return s.throwIfAborted(),yield n(),s.throwIfAborted(),e.transition(Be())}))))),this.on(Fe.type,Ae(((e,t,s)=>i(this,[e,t,s],void 0,(function*(e,t,{emitStatus:s,config:n}){n.announceFailedHeartbeats&&!0===(null==e?void 0:e.error)?s(Object.assign(Object.assign({},e),{operation:M.PNHeartbeatOperation})):n.announceSuccessfulHeartbeats&&200===e.statusCode&&s(Object.assign(Object.assign({},e),{error:!1,operation:M.PNHeartbeatOperation,category:h.PNAcknowledgmentCategory}))})))))}}const ze=new ke("HEARTBEAT_STOPPED");ze.on(Le.type,((e,t)=>ze.with({channels:[...e.channels,...t.payload.channels.filter((t=>!e.channels.includes(t)))],groups:[...e.groups,...t.payload.groups.filter((t=>!e.groups.includes(t)))]}))),ze.on(qe.type,((e,t)=>ze.with({channels:e.channels.filter((e=>!t.payload.channels.includes(e))),groups:e.groups.filter((e=>!t.payload.groups.includes(e)))}))),ze.on($e.type,((e,t)=>Xe.with({channels:e.channels,groups:e.groups}))),ze.on(Ge.type,((e,t)=>Qe.with(void 0)));const Ve=new ke("HEARTBEAT_COOLDOWN");Ve.onEnter((()=>Re())),Ve.onExit((()=>Re.cancel)),Ve.on(Be.type,((e,t)=>Xe.with({channels:e.channels,groups:e.groups}))),Ve.on(Le.type,((e,t)=>Xe.with({channels:[...e.channels,...t.payload.channels.filter((t=>!e.channels.includes(t)))],groups:[...e.groups,...t.payload.groups.filter((t=>!e.groups.includes(t)))]}))),Ve.on(qe.type,((e,t)=>Xe.with({channels:e.channels.filter((e=>!t.payload.channels.includes(e))),groups:e.groups.filter((e=>!t.payload.groups.includes(e)))},[De(t.payload.channels,t.payload.groups)]))),Ve.on(xe.type,((e,t)=>ze.with({channels:e.channels,groups:e.groups},[...t.payload.isOffline?[]:[De(e.channels,e.groups)]]))),Ve.on(Ge.type,((e,t)=>Qe.with(void 0,[...t.payload.isOffline?[]:[De(e.channels,e.groups)]])));const Je=new ke("HEARTBEAT_FAILED");Je.on(Le.type,((e,t)=>Xe.with({channels:[...e.channels,...t.payload.channels.filter((t=>!e.channels.includes(t)))],groups:[...e.groups,...t.payload.groups.filter((t=>!e.groups.includes(t)))]}))),Je.on(qe.type,((e,t)=>Xe.with({channels:e.channels.filter((e=>!t.payload.channels.includes(e))),groups:e.groups.filter((e=>!t.payload.groups.includes(e)))},[De(t.payload.channels,t.payload.groups)]))),Je.on($e.type,((e,t)=>Xe.with({channels:e.channels,groups:e.groups}))),Je.on(xe.type,((e,t)=>ze.with({channels:e.channels,groups:e.groups},[...t.payload.isOffline?[]:[De(e.channels,e.groups)]]))),Je.on(Ge.type,((e,t)=>Qe.with(void 0,[...t.payload.isOffline?[]:[De(e.channels,e.groups)]])));const Xe=new ke("HEARTBEATING");Xe.onEnter((e=>Ue(e.channels,e.groups))),Xe.onExit((()=>Ue.cancel)),Xe.on(Ke.type,((e,t)=>Ve.with({channels:e.channels,groups:e.groups},[Fe(Object.assign({},t.payload))]))),Xe.on(Le.type,((e,t)=>Xe.with({channels:[...e.channels,...t.payload.channels.filter((t=>!e.channels.includes(t)))],groups:[...e.groups,...t.payload.groups.filter((t=>!e.groups.includes(t)))]}))),Xe.on(qe.type,((e,t)=>Xe.with({channels:e.channels.filter((e=>!t.payload.channels.includes(e))),groups:e.groups.filter((e=>!t.payload.groups.includes(e)))},[De(t.payload.channels,t.payload.groups)]))),Xe.on(He.type,((e,t)=>Je.with(Object.assign({},e),[...t.payload.status?[Fe(Object.assign({},t.payload.status))]:[]]))),Xe.on(xe.type,((e,t)=>ze.with({channels:e.channels,groups:e.groups},[...t.payload.isOffline?[]:[De(e.channels,e.groups)]]))),Xe.on(Ge.type,((e,t)=>Qe.with(void 0,[...t.payload.isOffline?[]:[De(e.channels,e.groups)]])));const Qe=new ke("HEARTBEAT_INACTIVE");Qe.on(Le.type,((e,t)=>Xe.with({channels:t.payload.channels,groups:t.payload.groups})));class Ye{get _engine(){return this.engine}constructor(e){this.dependencies=e,this.channels=[],this.groups=[],this.engine=new Ce(e.config.logger()),this.dispatcher=new We(this.engine,e),e.config.logger().debug("PresenceEventEngine","Create presence event engine."),this._unsubscribeEngine=this.engine.subscribe((e=>{"invocationDispatched"===e.type&&this.dispatcher.dispatch(e.invocation)})),this.engine.start(Qe,void 0)}join({channels:e,groups:t}){this.channels=[...this.channels,...(null!=e?e:[]).filter((e=>!this.channels.includes(e)))],this.groups=[...this.groups,...(null!=t?t:[]).filter((e=>!this.groups.includes(e)))],0===this.channels.length&&0===this.groups.length||this.engine.transition(Le(this.channels.slice(0),this.groups.slice(0)))}leave({channels:e,groups:t}){this.dependencies.presenceState&&(null==e||e.forEach((e=>delete this.dependencies.presenceState[e])),null==t||t.forEach((e=>delete this.dependencies.presenceState[e]))),this.engine.transition(qe(null!=e?e:[],null!=t?t:[]))}leaveAll(e=!1){this.engine.transition(Ge(e))}reconnect(){this.engine.transition($e())}disconnect(e=!1){this.engine.transition(xe(e))}dispose(){this.disconnect(!0),this._unsubscribeEngine(),this.dispatcher.dispose()}}const Ze=Ne("HANDSHAKE",((e,t,s)=>({channels:e,groups:t,onDemand:s}))),et=Ne("RECEIVE_MESSAGES",((e,t,s,n)=>({channels:e,groups:t,cursor:s,onDemand:n}))),tt=Ee("EMIT_MESSAGES",((e,t)=>({cursor:e,events:t}))),st=Ee("EMIT_STATUS",(e=>e)),nt=je("SUBSCRIPTION_CHANGED",((e,t,s=!1)=>({channels:e,groups:t,isOffline:s}))),rt=je("SUBSCRIPTION_RESTORED",((e,t,s,n)=>({channels:e,groups:t,cursor:{timetoken:s,region:null!=n?n:0}}))),it=je("HANDSHAKE_SUCCESS",(e=>e)),at=je("HANDSHAKE_FAILURE",(e=>e)),ot=je("RECEIVE_SUCCESS",((e,t)=>({cursor:e,events:t}))),ct=je("RECEIVE_FAILURE",(e=>e)),ut=je("DISCONNECT",((e=!1)=>({isOffline:e}))),lt=je("RECONNECT",((e,t)=>({cursor:{timetoken:null!=e?e:"",region:null!=t?t:0}}))),ht=je("UNSUBSCRIBE_ALL",(()=>({}))),dt=new ke("UNSUBSCRIBED");dt.on(nt.type,((e,{payload:t})=>0===t.channels.length&&0===t.groups.length?dt.with(void 0):bt.with({channels:t.channels,groups:t.groups,onDemand:!0}))),dt.on(rt.type,((e,{payload:t})=>0===t.channels.length&&0===t.groups.length?dt.with(void 0):bt.with({channels:t.channels,groups:t.groups,cursor:{timetoken:`${t.cursor.timetoken}`,region:t.cursor.region},onDemand:!0})));const pt=new ke("HANDSHAKE_STOPPED");pt.on(nt.type,((e,{payload:t})=>0===t.channels.length&&0===t.groups.length?dt.with(void 0):pt.with({channels:t.channels,groups:t.groups,cursor:e.cursor}))),pt.on(lt.type,((e,{payload:t})=>bt.with(Object.assign(Object.assign({},e),{cursor:t.cursor||e.cursor,onDemand:!0})))),pt.on(rt.type,((e,{payload:t})=>{var s;return 0===t.channels.length&&0===t.groups.length?dt.with(void 0):pt.with({channels:t.channels,groups:t.groups,cursor:{timetoken:`${t.cursor.timetoken}`,region:t.cursor.region||(null===(s=e.cursor)||void 0===s?void 0:s.region)||0}})})),pt.on(ht.type,(e=>dt.with()));const gt=new ke("HANDSHAKE_FAILED");gt.on(nt.type,((e,{payload:t})=>0===t.channels.length&&0===t.groups.length?dt.with(void 0):bt.with({channels:t.channels,groups:t.groups,cursor:e.cursor,onDemand:!0}))),gt.on(lt.type,((e,{payload:t})=>bt.with(Object.assign(Object.assign({},e),{cursor:t.cursor||e.cursor,onDemand:!0})))),gt.on(rt.type,((e,{payload:t})=>{var s,n;return 0===t.channels.length&&0===t.groups.length?dt.with(void 0):bt.with({channels:t.channels,groups:t.groups,cursor:{timetoken:`${t.cursor.timetoken}`,region:t.cursor.region?t.cursor.region:null!==(n=null===(s=null==e?void 0:e.cursor)||void 0===s?void 0:s.region)&&void 0!==n?n:0},onDemand:!0})})),gt.on(ht.type,(e=>dt.with()));const bt=new ke("HANDSHAKING");bt.onEnter((e=>{var t;return Ze(e.channels,e.groups,null!==(t=e.onDemand)&&void 0!==t&&t)})),bt.onExit((()=>Ze.cancel)),bt.on(nt.type,((e,{payload:t})=>0===t.channels.length&&0===t.groups.length?dt.with(void 0):bt.with({channels:t.channels,groups:t.groups,cursor:e.cursor,onDemand:!0}))),bt.on(it.type,((e,{payload:t})=>{var s,n,r,i,a;return ft.with({channels:e.channels,groups:e.groups,cursor:{timetoken:(null===(s=e.cursor)||void 0===s?void 0:s.timetoken)?null===(n=e.cursor)||void 0===n?void 0:n.timetoken:t.timetoken,region:t.region},referenceTimetoken:K(t.timetoken,null===(r=e.cursor)||void 0===r?void 0:r.timetoken)},[st({category:h.PNConnectedCategory,affectedChannels:e.channels.slice(0),affectedChannelGroups:e.groups.slice(0),currentTimetoken:(null===(i=e.cursor)||void 0===i?void 0:i.timetoken)?null===(a=e.cursor)||void 0===a?void 0:a.timetoken:t.timetoken})])})),bt.on(at.type,((e,t)=>{var s;return gt.with(Object.assign(Object.assign({},e),{reason:t.payload}),[st({category:h.PNConnectionErrorCategory,error:null===(s=t.payload.status)||void 0===s?void 0:s.category})])})),bt.on(ut.type,((e,t)=>{var s;if(t.payload.isOffline){const t=I.create(new Error("Network connection error")).toPubNubError(M.PNSubscribeOperation);return gt.with(Object.assign(Object.assign({},e),{reason:t}),[st({category:h.PNConnectionErrorCategory,error:null===(s=t.status)||void 0===s?void 0:s.category})])}return pt.with(Object.assign({},e))})),bt.on(rt.type,((e,{payload:t})=>{var s;return 0===t.channels.length&&0===t.groups.length?dt.with(void 0):bt.with({channels:t.channels,groups:t.groups,cursor:{timetoken:`${t.cursor.timetoken}`,region:t.cursor.region||(null===(s=null==e?void 0:e.cursor)||void 0===s?void 0:s.region)||0},onDemand:!0})})),bt.on(ht.type,(e=>dt.with()));const yt=new ke("RECEIVE_STOPPED");yt.on(nt.type,((e,{payload:t})=>0===t.channels.length&&0===t.groups.length?dt.with(void 0):yt.with({channels:t.channels,groups:t.groups,cursor:e.cursor}))),yt.on(rt.type,((e,{payload:t})=>0===t.channels.length&&0===t.groups.length?dt.with(void 0):yt.with({channels:t.channels,groups:t.groups,cursor:{timetoken:`${t.cursor.timetoken}`,region:t.cursor.region||e.cursor.region}}))),yt.on(lt.type,((e,{payload:t})=>{var s;return bt.with({channels:e.channels,groups:e.groups,cursor:{timetoken:t.cursor.timetoken?null===(s=t.cursor)||void 0===s?void 0:s.timetoken:e.cursor.timetoken,region:t.cursor.region||e.cursor.region},onDemand:!0})})),yt.on(ht.type,(()=>dt.with(void 0)));const mt=new ke("RECEIVE_FAILED");mt.on(lt.type,((e,{payload:t})=>{var s;return bt.with({channels:e.channels,groups:e.groups,cursor:{timetoken:t.cursor.timetoken?null===(s=t.cursor)||void 0===s?void 0:s.timetoken:e.cursor.timetoken,region:t.cursor.region||e.cursor.region},onDemand:!0})})),mt.on(nt.type,((e,{payload:t})=>0===t.channels.length&&0===t.groups.length?dt.with(void 0):bt.with({channels:t.channels,groups:t.groups,cursor:e.cursor,onDemand:!0}))),mt.on(rt.type,((e,{payload:t})=>0===t.channels.length&&0===t.groups.length?dt.with(void 0):bt.with({channels:t.channels,groups:t.groups,cursor:{timetoken:`${t.cursor.timetoken}`,region:t.cursor.region||e.cursor.region},onDemand:!0}))),mt.on(ht.type,(e=>dt.with(void 0)));const ft=new ke("RECEIVING");ft.onEnter((e=>{var t;return et(e.channels,e.groups,e.cursor,null!==(t=e.onDemand)&&void 0!==t&&t)})),ft.onExit((()=>et.cancel)),ft.on(ot.type,((e,{payload:t})=>ft.with({channels:e.channels,groups:e.groups,cursor:t.cursor,referenceTimetoken:K(t.cursor.timetoken)},[tt(e.cursor,t.events)]))),ft.on(nt.type,((e,{payload:t})=>{var s;if(0===t.channels.length&&0===t.groups.length){let e;return t.isOffline&&(e=null===(s=I.create(new Error("Network connection error")).toPubNubError(M.PNSubscribeOperation).status)||void 0===s?void 0:s.category),dt.with(void 0,[st(Object.assign({category:t.isOffline?h.PNDisconnectedUnexpectedlyCategory:h.PNDisconnectedCategory},e?{error:e}:{}))])}return ft.with({channels:t.channels,groups:t.groups,cursor:e.cursor,referenceTimetoken:e.referenceTimetoken,onDemand:!0},[st({category:h.PNSubscriptionChangedCategory,affectedChannels:t.channels.slice(0),affectedChannelGroups:t.groups.slice(0),currentTimetoken:e.cursor.timetoken})])})),ft.on(rt.type,((e,{payload:t})=>0===t.channels.length&&0===t.groups.length?dt.with(void 0,[st({category:h.PNDisconnectedCategory})]):ft.with({channels:t.channels,groups:t.groups,cursor:{timetoken:`${t.cursor.timetoken}`,region:t.cursor.region||e.cursor.region},referenceTimetoken:K(e.cursor.timetoken,`${t.cursor.timetoken}`,e.referenceTimetoken),onDemand:!0},[st({category:h.PNSubscriptionChangedCategory,affectedChannels:t.channels.slice(0),affectedChannelGroups:t.groups.slice(0),currentTimetoken:t.cursor.timetoken})]))),ft.on(ct.type,((e,{payload:t})=>{var s;return mt.with(Object.assign(Object.assign({},e),{reason:t}),[st({category:h.PNDisconnectedUnexpectedlyCategory,error:null===(s=t.status)||void 0===s?void 0:s.category})])})),ft.on(ut.type,((e,t)=>{var s;if(t.payload.isOffline){const t=I.create(new Error("Network connection error")).toPubNubError(M.PNSubscribeOperation);return mt.with(Object.assign(Object.assign({},e),{reason:t}),[st({category:h.PNDisconnectedUnexpectedlyCategory,error:null===(s=t.status)||void 0===s?void 0:s.category})])}return yt.with(Object.assign({},e),[st({category:h.PNDisconnectedCategory})])})),ft.on(ht.type,(e=>dt.with(void 0,[st({category:h.PNDisconnectedCategory})])));class vt extends Pe{constructor(e,t){super(t,t.config.logger()),this.on(Ze.type,Ae(((t,s,n)=>i(this,[t,s,n],void 0,(function*(t,s,{handshake:n,presenceState:r,config:i}){s.throwIfAborted();try{const a=yield n(Object.assign(Object.assign({abortSignal:s,channels:t.channels,channelGroups:t.groups,filterExpression:i.filterExpression},i.maintainPresenceState&&{state:r}),{onDemand:t.onDemand}));return e.transition(it(a))}catch(t){if(t instanceof d){if(t.status&&t.status.category==h.PNCancelledCategory)return;return e.transition(at(t))}}}))))),this.on(et.type,Ae(((t,s,n)=>i(this,[t,s,n],void 0,(function*(t,s,{receiveMessages:n,config:r}){s.throwIfAborted();try{const i=yield n({abortSignal:s,channels:t.channels,channelGroups:t.groups,timetoken:t.cursor.timetoken,region:t.cursor.region,filterExpression:r.filterExpression,onDemand:t.onDemand});e.transition(ot(i.cursor,i.messages))}catch(t){if(t instanceof d){if(t.status&&t.status.category==h.PNCancelledCategory)return;if(!s.aborted)return e.transition(ct(t))}}}))))),this.on(tt.type,Ae(((e,t,s)=>i(this,[e,t,s],void 0,(function*({cursor:e,events:t},s,{emitMessages:n}){t.length>0&&n(e,t)}))))),this.on(st.type,Ae(((e,t,s)=>i(this,[e,t,s],void 0,(function*(e,t,{emitStatus:s}){return s(e)})))))}}class St{get _engine(){return this.engine}constructor(e){this.channels=[],this.groups=[],this.dependencies=e,this.engine=new Ce(e.config.logger()),this.dispatcher=new vt(this.engine,e),e.config.logger().debug("EventEngine","Create subscribe event engine."),this._unsubscribeEngine=this.engine.subscribe((e=>{"invocationDispatched"===e.type&&this.dispatcher.dispatch(e.invocation)})),this.engine.start(dt,void 0)}get subscriptionTimetoken(){const e=this.engine.currentState;if(!e)return;let t,s="0";if(e.label===ft.label){const e=this.engine.currentContext;s=e.cursor.timetoken,t=e.referenceTimetoken}return G(s,null!=t?t:"0")}subscribe({channels:e,channelGroups:t,timetoken:s,withPresence:n}){this.channels=[...this.channels,...null!=e?e:[]],this.groups=[...this.groups,...null!=t?t:[]],n&&(this.channels.map((e=>this.channels.push(`${e}-pnpres`))),this.groups.map((e=>this.groups.push(`${e}-pnpres`)))),s?this.engine.transition(rt(Array.from(new Set([...this.channels,...null!=e?e:[]])),Array.from(new Set([...this.groups,...null!=t?t:[]])),s)):this.engine.transition(nt(Array.from(new Set([...this.channels,...null!=e?e:[]])),Array.from(new Set([...this.groups,...null!=t?t:[]])))),this.dependencies.join&&this.dependencies.join({channels:Array.from(new Set(this.channels.filter((e=>!e.endsWith("-pnpres"))))),groups:Array.from(new Set(this.groups.filter((e=>!e.endsWith("-pnpres")))))})}unsubscribe({channels:e=[],channelGroups:t=[]}){const s=x(this.channels,[...e,...e.map((e=>`${e}-pnpres`))]),n=x(this.groups,[...t,...t.map((e=>`${e}-pnpres`))]);if(new Set(this.channels).size!==new Set(s).size||new Set(this.groups).size!==new Set(n).size){const r=L(this.channels,e),i=L(this.groups,t);this.dependencies.presenceState&&(null==r||r.forEach((e=>delete this.dependencies.presenceState[e])),null==i||i.forEach((e=>delete this.dependencies.presenceState[e]))),this.channels=s,this.groups=n,this.engine.transition(nt(Array.from(new Set(this.channels.slice(0))),Array.from(new Set(this.groups.slice(0))))),this.dependencies.leave&&this.dependencies.leave({channels:r.slice(0),groups:i.slice(0)})}}unsubscribeAll(e=!1){const t=this.getSubscribedChannelGroups(),s=this.getSubscribedChannels();this.channels=[],this.groups=[],this.dependencies.presenceState&&Object.keys(this.dependencies.presenceState).forEach((e=>{delete this.dependencies.presenceState[e]})),this.engine.transition(nt(this.channels.slice(0),this.groups.slice(0),e)),this.dependencies.leaveAll&&this.dependencies.leaveAll({channels:s,groups:t,isOffline:e})}reconnect({timetoken:e,region:t}){const s=this.getSubscribedChannels(),n=this.getSubscribedChannels();this.engine.transition(lt(e,t)),this.dependencies.presenceReconnect&&this.dependencies.presenceReconnect({channels:n,groups:s})}disconnect(e=!1){const t=this.getSubscribedChannels(),s=this.getSubscribedChannels();this.engine.transition(ut(e)),this.dependencies.presenceDisconnect&&this.dependencies.presenceDisconnect({channels:s,groups:t,isOffline:e})}getSubscribedChannels(){return Array.from(new Set(this.channels.slice(0)))}getSubscribedChannelGroups(){return Array.from(new Set(this.groups.slice(0)))}dispose(){this.disconnect(!0),this._unsubscribeEngine(),this.dispatcher.dispose()}}class wt extends le{constructor(e){var t;const s=null!==(t=e.sendByPost)&&void 0!==t&&t;super({method:s?ae.POST:ae.GET,compressible:s}),this.parameters=e,this.parameters.sendByPost=s}operation(){return M.PNPublishOperation}validate(){const{message:e,channel:t,keySet:{publishKey:s}}=this.parameters;return t?e?s?void 0:"Missing 'publishKey'":"Missing 'message'":"Missing 'channel'"}parse(e){return i(this,void 0,void 0,(function*(){return{timetoken:this.deserializeResponse(e)[2]}}))}get path(){const{message:e,channel:t,keySet:s}=this.parameters,n=this.prepareMessagePayload(e);return`/publish/${s.publishKey}/${s.subscribeKey}/0/${R(t)}/0${this.parameters.sendByPost?"":`/${R(n)}`}`}get queryParameters(){const{customMessageType:e,meta:t,replicate:s,storeInHistory:n,ttl:r}=this.parameters,i={};return e&&(i.custom_message_type=e),void 0!==n&&(i.store=n?"1":"0"),void 0!==r&&(i.ttl=r),void 0===s||s||(i.norep="true"),t&&"object"==typeof t&&(i.meta=JSON.stringify(t)),i}get headers(){var e;return this.parameters.sendByPost?Object.assign(Object.assign({},null!==(e=super.headers)&&void 0!==e?e:{}),{"Content-Type":"application/json"}):super.headers}get body(){return this.prepareMessagePayload(this.parameters.message)}prepareMessagePayload(e){const{crypto:t}=this.parameters;if(!t)return JSON.stringify(e)||"";const s=t.encrypt(JSON.stringify(e));return JSON.stringify("string"==typeof s?s:u(s))}}class Ot extends le{constructor(e){super(),this.parameters=e}operation(){return M.PNSignalOperation}validate(){const{message:e,channel:t,keySet:{publishKey:s}}=this.parameters;return t?e?s?void 0:"Missing 'publishKey'":"Missing 'message'":"Missing 'channel'"}parse(e){return i(this,void 0,void 0,(function*(){return{timetoken:this.deserializeResponse(e)[2]}}))}get path(){const{keySet:{publishKey:e,subscribeKey:t},channel:s,message:n}=this.parameters,r=JSON.stringify(n);return`/signal/${e}/${t}/0/${R(s)}/0/${R(r)}`}get queryParameters(){const{customMessageType:e}=this.parameters,t={};return e&&(t.custom_message_type=e),t}}class kt extends de{operation(){return M.PNReceiveMessagesOperation}validate(){const e=super.validate();return e||(this.parameters.timetoken?this.parameters.region?void 0:"region can not be empty":"timetoken can not be empty")}get path(){const{keySet:{subscribeKey:e},channels:t=[]}=this.parameters;return`/v2/subscribe/${e}/${$(t.sort(),",")}/0`}get queryParameters(){const{channelGroups:e,filterExpression:t,timetoken:s,region:n,onDemand:r}=this.parameters,i={ee:""};return r&&(i["on-demand"]=1),e&&e.length>0&&(i["channel-group"]=e.sort().join(",")),t&&t.length>0&&(i["filter-expr"]=t),"string"==typeof s?s&&"0"!==s&&s.length>0&&(i.tt=s):s&&s>0&&(i.tt=s),n&&(i.tr=n),i}}class Ct extends de{operation(){return M.PNHandshakeOperation}get path(){const{keySet:{subscribeKey:e},channels:t=[]}=this.parameters;return`/v2/subscribe/${e}/${$(t.sort(),",")}/0`}get queryParameters(){const{channelGroups:e,filterExpression:t,state:s,onDemand:n}=this.parameters,r={ee:""};return n&&(r["on-demand"]=1),e&&e.length>0&&(r["channel-group"]=e.sort().join(",")),t&&t.length>0&&(r["filter-expr"]=t),s&&Object.keys(s).length>0&&(r.state=JSON.stringify(s)),r}}var Pt;!function(e){e[e.Channel=0]="Channel",e[e.ChannelGroup=1]="ChannelGroup"}(Pt||(Pt={}));class jt{constructor({channels:e,channelGroups:t}){this.isEmpty=!0,this._channelGroups=new Set((null!=t?t:[]).filter((e=>e.length>0))),this._channels=new Set((null!=e?e:[]).filter((e=>e.length>0))),this.isEmpty=0===this._channels.size&&0===this._channelGroups.size}get length(){return this.isEmpty?0:this._channels.size+this._channelGroups.size}get channels(){return this.isEmpty?[]:Array.from(this._channels)}get channelGroups(){return this.isEmpty?[]:Array.from(this._channelGroups)}contains(e){return!this.isEmpty&&(this._channels.has(e)||this._channelGroups.has(e))}with(e){return new jt({channels:[...this._channels,...e._channels],channelGroups:[...this._channelGroups,...e._channelGroups]})}without(e){return new jt({channels:[...this._channels].filter((t=>!e._channels.has(t))),channelGroups:[...this._channelGroups].filter((t=>!e._channelGroups.has(t)))})}add(e){return e._channelGroups.size>0&&(this._channelGroups=new Set([...this._channelGroups,...e._channelGroups])),e._channels.size>0&&(this._channels=new Set([...this._channels,...e._channels])),this.isEmpty=0===this._channels.size&&0===this._channelGroups.size,this}remove(e){return e._channelGroups.size>0&&(this._channelGroups=new Set([...this._channelGroups].filter((t=>!e._channelGroups.has(t))))),e._channels.size>0&&(this._channels=new Set([...this._channels].filter((t=>!e._channels.has(t))))),this}removeAll(){return this._channels.clear(),this._channelGroups.clear(),this.isEmpty=!0,this}toString(){return`SubscriptionInput { channels: [${this.channels.join(", ")}], channelGroups: [${this.channelGroups.join(", ")}], is empty: ${this.isEmpty?"true":"false"}} }`}}class Et{constructor(e,t,s,n){this._isSubscribed=!1,this.clones={},this.parents=[],this._id=te.createUUID(),this.referenceTimetoken=n,this.subscriptionInput=t,this.options=s,this.client=e}get id(){return this._id}get isLastClone(){return 1===Object.keys(this.clones).length}get isSubscribed(){return!!this._isSubscribed||this.parents.length>0&&this.parents.some((e=>e.isSubscribed))}set isSubscribed(e){this.isSubscribed!==e&&(this._isSubscribed=e)}addParentState(e){this.parents.includes(e)||this.parents.push(e)}removeParentState(e){const t=this.parents.indexOf(e);-1!==t&&this.parents.splice(t,1)}storeClone(e,t){this.clones[e]||(this.clones[e]=t)}}class Nt{constructor(e,t="Subscription"){this.subscriptionType=t,this.id=te.createUUID(),this.eventDispatcher=new ge,this._state=e}get state(){return this._state}get channels(){return this.state.subscriptionInput.channels.slice(0)}get channelGroups(){return this.state.subscriptionInput.channelGroups.slice(0)}set onMessage(e){this.eventDispatcher.onMessage=e}set onPresence(e){this.eventDispatcher.onPresence=e}set onSignal(e){this.eventDispatcher.onSignal=e}set onObjects(e){this.eventDispatcher.onObjects=e}set onMessageAction(e){this.eventDispatcher.onMessageAction=e}set onFile(e){this.eventDispatcher.onFile=e}addListener(e){this.eventDispatcher.addListener(e)}removeListener(e){this.eventDispatcher.removeListener(e)}removeAllListeners(){this.eventDispatcher.removeAllListeners()}handleEvent(e,t){var s;if((!this.state.cursor||e>this.state.cursor)&&(this.state.cursor=e),this.state.referenceTimetoken&&t.data.timetoken({messageType:"text",message:`Event timetoken (${t.data.timetoken}) is older than reference timetoken (${this.state.referenceTimetoken}) for ${this.id} subscription object. Ignoring event.`})));if((null===(s=this.state.options)||void 0===s?void 0:s.filter)&&!this.state.options.filter(t))return void this.state.client.logger.trace(this.subscriptionType,`Event filtered out by filter function for ${this.id} subscription object. Ignoring event.`);const n=Object.values(this.state.clones);n.length>0&&this.state.client.logger.trace(this.subscriptionType,`Notify ${this.id} subscription object clones (count: ${n.length}) about received event.`),n.forEach((e=>e.eventDispatcher.handleEvent(t)))}dispose(){const e=Object.keys(this.state.clones);e.length>1?(this.state.client.logger.debug(this.subscriptionType,`Remove subscription object clone on dispose: ${this.id}`),delete this.state.clones[this.id]):1===e.length&&this.state.clones[this.id]&&(this.state.client.logger.debug(this.subscriptionType,`Unsubscribe subscription object on dispose: ${this.id}`),this.unsubscribe())}invalidate(e=!1){this.state._isSubscribed=!1,e&&(delete this.state.clones[this.id],0===Object.keys(this.state.clones).length&&(this.state.client.logger.trace(this.subscriptionType,"Last clone removed. Reset shared subscription state."),this.state.subscriptionInput.removeAll(),this.state.parents=[]))}subscribe(e){this.state.isSubscribed?this.state.client.logger.trace(this.subscriptionType,"Already subscribed. Ignoring subscribe request."):(this.state.client.logger.debug(this.subscriptionType,(()=>e?{messageType:"object",message:e,details:"Subscribe with parameters:"}:{messageType:"text",message:"Subscribe"})),this.state.isSubscribed=!0,this.updateSubscription({subscribing:!0,timetoken:null==e?void 0:e.timetoken}))}unsubscribe(){if(!this.state._isSubscribed||this.state.isSubscribed){if(!this.state._isSubscribed&&this.state.parents.length>0&&this.state.isSubscribed)return void this.state.client.logger.warn(this.subscriptionType,(()=>({messageType:"object",details:"Subscription is subscribed as part of a subscription set. Remove from active sets to unsubscribe:",message:this.state.parents.filter((e=>e.isSubscribed))})));if(!this.state._isSubscribed)return void this.state.client.logger.trace(this.subscriptionType,"Not subscribed. Ignoring unsubscribe request.")}this.state.client.logger.debug(this.subscriptionType,"Unsubscribe"),this.state.isSubscribed=!1,delete this.state.cursor,this.updateSubscription({subscribing:!1})}updateSubscription(e){var t,s;(null==e?void 0:e.timetoken)&&((null===(t=this.state.cursor)||void 0===t?void 0:t.timetoken)&&"0"!==(null===(s=this.state.cursor)||void 0===s?void 0:s.timetoken)?"0"!==e.timetoken&&e.timetoken>this.state.cursor.timetoken&&(this.state.cursor.timetoken=e.timetoken):this.state.cursor={timetoken:e.timetoken});const n=e.subscriptions&&e.subscriptions.length>0?e.subscriptions:void 0;e.subscribing?this.register(Object.assign(Object.assign({},e.timetoken?{cursor:this.state.cursor}:{}),n?{subscriptions:n}:{})):this.unregister(n)}}class Tt extends Et{constructor(e){const t=new jt({});e.subscriptions.forEach((e=>t.add(e.state.subscriptionInput))),super(e.client,t,e.options,e.client.subscriptionTimetoken),this.subscriptions=e.subscriptions}addSubscription(e){this.subscriptions.includes(e)||(e.state.addParentState(this),this.subscriptions.push(e),this.subscriptionInput.add(e.state.subscriptionInput))}removeSubscription(e,t){const s=this.subscriptions.indexOf(e);-1!==s&&(this.subscriptions.splice(s,1),t||e.state.removeParentState(this),this.subscriptionInput.remove(e.state.subscriptionInput))}removeAllSubscriptions(){this.subscriptions.forEach((e=>e.state.removeParentState(this))),this.subscriptions.splice(0,this.subscriptions.length),this.subscriptionInput.removeAll()}}class _t extends Nt{constructor(e){let t;if("client"in e){let s=[];!e.subscriptions&&e.entities?e.entities.forEach((t=>s.push(t.subscription(e.options)))):e.subscriptions&&(s=e.subscriptions),t=new Tt({client:e.client,subscriptions:s,options:e.options}),s.forEach((e=>e.state.addParentState(t))),t.client.logger.debug("SubscriptionSet",(()=>({messageType:"object",details:"Create subscription set with parameters:",message:Object.assign({subscriptions:t.subscriptions},e.options?e.options:{})})))}else t=e.state,t.client.logger.debug("SubscriptionSet","Create subscription set clone");super(t,"SubscriptionSet"),this.state.storeClone(this.id,this),t.subscriptions.forEach((e=>e.addParentSet(this)))}get state(){return super.state}get subscriptions(){return this.state.subscriptions.slice(0)}handleEvent(e,t){var s;this.state.subscriptionInput.contains(null!==(s=t.data.subscription)&&void 0!==s?s:t.data.channel)&&(this.state._isSubscribed?(super.handleEvent(e,t),this.state.subscriptions.length>0&&this.state.client.logger.trace(this.subscriptionType,`Notify ${this.id} subscription set subscriptions (count: ${this.state.subscriptions.length}) about received event.`),this.state.subscriptions.forEach((s=>s.handleEvent(e,t)))):this.state.client.logger.trace(this.subscriptionType,`Subscription set ${this.id} is not subscribed. Ignoring event.`))}subscriptionInput(e=!1){let t=this.state.subscriptionInput;return this.state.subscriptions.forEach((s=>{e&&s.state.entity.subscriptionsCount>0&&(t=t.without(s.state.subscriptionInput))})),t}cloneEmpty(){return new _t({state:this.state})}dispose(){const e=this.state.isLastClone;this.state.subscriptions.forEach((t=>{t.removeParentSet(this),e&&t.state.removeParentState(this.state)})),super.dispose()}invalidate(e=!1){(e?this.state.subscriptions.slice(0):this.state.subscriptions).forEach((t=>{e&&(t.state.entity.decreaseSubscriptionCount(this.state.id),t.removeParentSet(this)),t.invalidate(e)})),e&&this.state.removeAllSubscriptions(),super.invalidate()}addSubscription(e){this.addSubscriptions([e])}addSubscriptions(e){const t=[],s=[];this.state.client.logger.debug(this.subscriptionType,(()=>{const t=[],s=[];return e.forEach((e=>{this.state.subscriptions.includes(e)?t.push(e):s.push(e)})),{messageType:"object",details:`Add subscriptions to ${this.id} (subscriptions count: ${this.state.subscriptions.length+s.length}):`,message:{addedSubscriptions:s,ignoredSubscriptions:t}}})),e.filter((e=>!this.state.subscriptions.includes(e))).forEach((e=>{e.state.isSubscribed?s.push(e):t.push(e),e.addParentSet(this),this.state.addSubscription(e)})),0===s.length&&0===t.length||!this.state.isSubscribed||(s.forEach((({state:e})=>e.entity.increaseSubscriptionCount(this.state.id))),t.length>0&&this.updateSubscription({subscribing:!0,subscriptions:t}))}removeSubscription(e){this.removeSubscriptions([e])}removeSubscriptions(e){const t=[];this.state.client.logger.debug(this.subscriptionType,(()=>{const t=[],s=[];return e.forEach((e=>{this.state.subscriptions.includes(e)?s.push(e):t.push(e)})),{messageType:"object",details:`Remove subscriptions from ${this.id} (subscriptions count: ${this.state.subscriptions.length}):`,message:{removedSubscriptions:s,ignoredSubscriptions:t}}})),e.filter((e=>this.state.subscriptions.includes(e))).forEach((e=>{e.state.isSubscribed&&t.push(e),e.removeParentSet(this),this.state.removeSubscription(e,e.parentSetsCount>1)})),0!==t.length&&this.state.isSubscribed&&this.updateSubscription({subscribing:!1,subscriptions:t})}addSubscriptionSet(e){this.addSubscriptions(e.subscriptions)}removeSubscriptionSet(e){this.removeSubscriptions(e.subscriptions)}register(e){var t;const s=null!==(t=e.subscriptions)&&void 0!==t?t:this.state.subscriptions;s.forEach((({state:e})=>e.entity.increaseSubscriptionCount(this.state.id))),this.state.client.logger.trace(this.subscriptionType,(()=>({messageType:"text",message:`Register subscription for real-time events: ${this}`}))),this.state.client.registerEventHandleCapable(this,e.cursor,s)}unregister(e){const t=null!=e?e:this.state.subscriptions;t.forEach((({state:e})=>e.entity.decreaseSubscriptionCount(this.state.id))),this.state.client.logger.trace(this.subscriptionType,(()=>e?{messageType:"object",message:{subscription:this,subscriptions:e},details:"Unregister subscriptions of subscription set from real-time events:"}:{messageType:"text",message:`Unregister subscription from real-time events: ${this}`})),this.state.client.unregisterEventHandleCapable(this,t)}toString(){const e=this.state;return`${this.subscriptionType} { id: ${this.id}, stateId: ${e.id}, clonesCount: ${Object.keys(this.state.clones).length}, isSubscribed: ${e.isSubscribed}, subscriptions: [${e.subscriptions.map((e=>e.toString())).join(", ")}] }`}}class It extends Et{constructor(e){var t,s;const n=e.entity.subscriptionNames(null!==(s=null===(t=e.options)||void 0===t?void 0:t.receivePresenceEvents)&&void 0!==s&&s),r=new jt({[e.entity.subscriptionType==Pt.Channel?"channels":"channelGroups"]:n});super(e.client,r,e.options,e.client.subscriptionTimetoken),this.entity=e.entity}}class Mt extends Nt{constructor(e){"client"in e?e.client.logger.debug("Subscription",(()=>({messageType:"object",details:"Create subscription with parameters:",message:Object.assign({entity:e.entity},e.options?e.options:{})}))):e.state.client.logger.debug("Subscription","Create subscription clone"),super("state"in e?e.state:new It(e)),this.parents=[],this.handledUpdates=[],this.state.storeClone(this.id,this)}get state(){return super.state}get parentSetsCount(){return this.parents.length}handleEvent(e,t){var s,n;if(this.state.isSubscribed&&this.state.subscriptionInput.contains(null!==(s=t.data.subscription)&&void 0!==s?s:t.data.channel)){if(this.parentSetsCount>0){const e=B(t.data);if(this.handledUpdates.includes(e))return void this.state.client.logger.trace(this.subscriptionType,`Event (${e}) already handled by ${this.id}. Ignoring.`);this.handledUpdates.push(e),this.handledUpdates.length>10&&this.handledUpdates.shift()}this.state.subscriptionInput.contains(null!==(n=t.data.subscription)&&void 0!==n?n:t.data.channel)&&super.handleEvent(e,t)}}subscriptionInput(e=!1){return e&&this.state.entity.subscriptionsCount>0?new jt({}):this.state.subscriptionInput}cloneEmpty(){return new Mt({state:this.state})}dispose(){this.parentSetsCount>0?this.state.client.logger.debug(this.subscriptionType,(()=>({messageType:"text",message:`'${this.state.entity.subscriptionNames()}' subscription still in use. Ignore dispose request.`}))):(this.handledUpdates.splice(0,this.handledUpdates.length),super.dispose())}invalidate(e=!1){e&&this.state.entity.decreaseSubscriptionCount(this.state.id),this.handledUpdates.splice(0,this.handledUpdates.length),super.invalidate(e)}addParentSet(e){this.parents.includes(e)||(this.parents.push(e),this.state.client.logger.trace(this.subscriptionType,`Add parent subscription set for ${this.id}: ${e.id}. Parent subscription set count: ${this.parentSetsCount}`))}removeParentSet(e){const t=this.parents.indexOf(e);-1!==t&&(this.parents.splice(t,1),this.state.client.logger.trace(this.subscriptionType,`Remove parent subscription set from ${this.id}: ${e.id}. Parent subscription set count: ${this.parentSetsCount}`)),0===this.parentSetsCount&&this.handledUpdates.splice(0,this.handledUpdates.length)}addSubscription(e){this.state.client.logger.debug(this.subscriptionType,(()=>({messageType:"text",message:`Create set with subscription: ${e}`})));const t=new _t({client:this.state.client,subscriptions:[this,e],options:this.state.options});return this.state.isSubscribed||e.state.isSubscribed?(this.state.client.logger.trace(this.subscriptionType,"Subscribe resulting set because the receiver is already subscribed."),t.subscribe(),t):t}register(e){this.state.entity.increaseSubscriptionCount(this.state.id),this.state.client.logger.trace(this.subscriptionType,(()=>({messageType:"text",message:`Register subscription for real-time events: ${this}`}))),this.state.client.registerEventHandleCapable(this,e.cursor)}unregister(e){this.state.entity.decreaseSubscriptionCount(this.state.id),this.state.client.logger.trace(this.subscriptionType,(()=>({messageType:"text",message:`Unregister subscription from real-time events: ${this}`}))),this.handledUpdates.splice(0,this.handledUpdates.length),this.state.client.unregisterEventHandleCapable(this)}toString(){const e=this.state;return`${this.subscriptionType} { id: ${this.id}, stateId: ${e.id}, entity: ${e.entity.subscriptionNames(!1).pop()}, clonesCount: ${Object.keys(e.clones).length}, isSubscribed: ${e.isSubscribed}, parentSetsCount: ${this.parentSetsCount}, cursor: ${e.cursor?e.cursor.timetoken:"not set"}, referenceTimetoken: ${e.referenceTimetoken?e.referenceTimetoken:"not set"} }`}}class At extends le{constructor(e){var t,s,n,r;super(),this.parameters=e,null!==(t=(n=this.parameters).channels)&&void 0!==t||(n.channels=[]),null!==(s=(r=this.parameters).channelGroups)&&void 0!==s||(r.channelGroups=[])}operation(){return M.PNGetStateOperation}validate(){const{keySet:{subscribeKey:e},channels:t,channelGroups:s}=this.parameters;if(!e)return"Missing Subscribe Key"}parse(e){return i(this,void 0,void 0,(function*(){const t=this.deserializeResponse(e),{channels:s=[],channelGroups:n=[]}=this.parameters,r={channels:{}};return 1===s.length&&0===n.length?r.channels[s[0]]=t.payload:r.channels=t.payload,r}))}get path(){const{keySet:{subscribeKey:e},uuid:t,channels:s}=this.parameters;return`/v2/presence/sub-key/${e}/channel/${$(null!=s?s:[],",")}/uuid/${R(null!=t?t:"")}`}get queryParameters(){const{channelGroups:e}=this.parameters;return e&&0!==e.length?{"channel-group":e.join(",")}:{}}}class Ut extends le{constructor(e){super(),this.parameters=e}operation(){return M.PNSetStateOperation}validate(){const{keySet:{subscribeKey:e},state:t,channels:s=[],channelGroups:n=[]}=this.parameters;return e?void 0===t?"Missing State":0===(null==s?void 0:s.length)&&0===(null==n?void 0:n.length)?"Please provide a list of channels and/or channel-groups":void 0:"Missing Subscribe Key"}parse(e){return i(this,void 0,void 0,(function*(){return{state:this.deserializeResponse(e).payload}}))}get path(){const{keySet:{subscribeKey:e},uuid:t,channels:s}=this.parameters;return`/v2/presence/sub-key/${e}/channel/${$(null!=s?s:[],",")}/uuid/${R(t)}/data`}get queryParameters(){const{channelGroups:e,state:t}=this.parameters,s={state:JSON.stringify(t)};return e&&0!==e.length&&(s["channel-group"]=e.join(",")),s}}class Dt extends le{constructor(e){super({cancellable:!0}),this.parameters=e}operation(){return M.PNHeartbeatOperation}validate(){const{keySet:{subscribeKey:e},channels:t=[],channelGroups:s=[]}=this.parameters;return e?0===t.length&&0===s.length?"Please provide a list of channels and/or channel-groups":void 0:"Missing Subscribe Key"}parse(e){const t=Object.create(null,{parse:{get:()=>super.parse}});return i(this,void 0,void 0,(function*(){return t.parse.call(this,e).then((e=>({})))}))}get path(){const{keySet:{subscribeKey:e},channels:t}=this.parameters;return`/v2/presence/sub-key/${e}/channel/${$(null!=t?t:[],",")}/heartbeat`}get queryParameters(){const{channelGroups:e,state:t,heartbeat:s}=this.parameters,n={heartbeat:`${s}`};return e&&0!==e.length&&(n["channel-group"]=e.join(",")),void 0!==t&&(n.state=JSON.stringify(t)),n}}class Ft extends le{constructor(e){super(),this.parameters=e,this.parameters.channelGroups&&(this.parameters.channelGroups=Array.from(new Set(this.parameters.channelGroups))),this.parameters.channels&&(this.parameters.channels=Array.from(new Set(this.parameters.channels)))}operation(){return M.PNUnsubscribeOperation}validate(){const{keySet:{subscribeKey:e},channels:t=[],channelGroups:s=[]}=this.parameters;return e?0===t.length&&0===s.length?"At least one `channel` or `channel group` should be provided.":void 0:"Missing Subscribe Key"}parse(e){const t=Object.create(null,{parse:{get:()=>super.parse}});return i(this,void 0,void 0,(function*(){return t.parse.call(this,e).then((e=>({})))}))}get path(){var e;const{keySet:{subscribeKey:t},channels:s}=this.parameters;return`/v2/presence/sub-key/${t}/channel/${$(null!==(e=null==s?void 0:s.sort())&&void 0!==e?e:[],",")}/leave`}get queryParameters(){const{channelGroups:e}=this.parameters;return e&&0!==e.length?{"channel-group":e.sort().join(",")}:{}}}class Rt extends le{constructor(e){super(),this.parameters=e}operation(){return M.PNWhereNowOperation}validate(){if(!this.parameters.keySet.subscribeKey)return"Missing Subscribe Key"}parse(e){return i(this,void 0,void 0,(function*(){const t=this.deserializeResponse(e);return t.payload?{channels:t.payload.channels}:{channels:[]}}))}get path(){const{keySet:{subscribeKey:e},uuid:t}=this.parameters;return`/v2/presence/sub-key/${e}/uuid/${R(t)}`}}class $t extends le{constructor(e){var t,s,n,r,i,a;super(),this.parameters=e,null!==(t=(r=this.parameters).queryParameters)&&void 0!==t||(r.queryParameters={}),null!==(s=(i=this.parameters).includeUUIDs)&&void 0!==s||(i.includeUUIDs=true),null!==(n=(a=this.parameters).includeState)&&void 0!==n||(a.includeState=false)}operation(){const{channels:e=[],channelGroups:t=[]}=this.parameters;return 0===e.length&&0===t.length?M.PNGlobalHereNowOperation:M.PNHereNowOperation}validate(){if(!this.parameters.keySet.subscribeKey)return"Missing Subscribe Key"}parse(e){return i(this,void 0,void 0,(function*(){var t,s;const n=this.deserializeResponse(e),r="occupancy"in n?1:n.payload.total_channels,i="occupancy"in n?n.occupancy:n.payload.total_occupancy,a={};let o={};if("occupancy"in n){const e=this.parameters.channels[0];o[e]={uuids:null!==(t=n.uuids)&&void 0!==t?t:[],occupancy:i}}else o=null!==(s=n.payload.channels)&&void 0!==s?s:{};return Object.keys(o).forEach((e=>{const t=o[e];a[e]={occupants:this.parameters.includeUUIDs?t.uuids.map((e=>"string"==typeof e?{uuid:e,state:null}:e)):[],name:e,occupancy:t.occupancy}})),{totalChannels:r,totalOccupancy:i,channels:a}}))}get path(){const{keySet:{subscribeKey:e},channels:t,channelGroups:s}=this.parameters;let n=`/v2/presence/sub-key/${e}`;return(t&&t.length>0||s&&s.length>0)&&(n+=`/channel/${$(null!=t?t:[],",")}`),n}get queryParameters(){const{channelGroups:e,includeUUIDs:t,includeState:s,queryParameters:n}=this.parameters;return Object.assign(Object.assign(Object.assign(Object.assign({},t?{}:{disable_uuids:"1"}),null!=s&&s?{state:"1"}:{}),e&&e.length>0?{"channel-group":e.join(",")}:{}),n)}}class xt extends le{constructor(e){super({method:ae.DELETE}),this.parameters=e}operation(){return M.PNDeleteMessagesOperation}validate(){return this.parameters.keySet.subscribeKey?this.parameters.channel?void 0:"Missing channel":"Missing Subscribe Key"}parse(e){const t=Object.create(null,{parse:{get:()=>super.parse}});return i(this,void 0,void 0,(function*(){return t.parse.call(this,e).then((e=>({})))}))}get path(){const{keySet:{subscribeKey:e},channel:t}=this.parameters;return`/v3/history/sub-key/${e}/channel/${R(t)}`}get queryParameters(){const{start:e,end:t}=this.parameters;return Object.assign(Object.assign({},e?{start:e}:{}),t?{end:t}:{})}}class Lt extends le{constructor(e){super(),this.parameters=e}operation(){return M.PNMessageCounts}validate(){const{keySet:{subscribeKey:e},channels:t,timetoken:s,channelTimetokens:n}=this.parameters;return e?t?s&&n?"`timetoken` and `channelTimetokens` are incompatible together":s||n?n&&n.length>1&&n.length!==t.length?"Length of `channelTimetokens` and `channels` do not match":void 0:"`timetoken` or `channelTimetokens` need to be set":"Missing channels":"Missing Subscribe Key"}parse(e){return i(this,void 0,void 0,(function*(){return{channels:this.deserializeResponse(e).channels}}))}get path(){return`/v3/history/sub-key/${this.parameters.keySet.subscribeKey}/message-counts/${$(this.parameters.channels)}`}get queryParameters(){let{channelTimetokens:e}=this.parameters;return this.parameters.timetoken&&(e=[this.parameters.timetoken]),Object.assign(Object.assign({},1===e.length?{timetoken:e[0]}:{}),e.length>1?{channelsTimetoken:e.join(",")}:{})}}class qt extends le{constructor(e){var t,s,n;super(),this.parameters=e,e.count?e.count=Math.min(e.count,100):e.count=100,null!==(t=e.stringifiedTimeToken)&&void 0!==t||(e.stringifiedTimeToken=false),null!==(s=e.includeMeta)&&void 0!==s||(e.includeMeta=false),null!==(n=e.logVerbosity)&&void 0!==n||(e.logVerbosity=false)}operation(){return M.PNHistoryOperation}validate(){return this.parameters.keySet.subscribeKey?this.parameters.channel?void 0:"Missing channel":"Missing Subscribe Key"}parse(e){return i(this,void 0,void 0,(function*(){const t=this.deserializeResponse(e),s=t[0],n=t[1],r=t[2];return Array.isArray(s)?{messages:s.map((e=>{const t=this.processPayload(e.message),s={entry:t.payload,timetoken:e.timetoken};return t.error&&(s.error=t.error),e.meta&&(s.meta=e.meta),s})),startTimeToken:n,endTimeToken:r}:{messages:[],startTimeToken:n,endTimeToken:r}}))}get path(){const{keySet:{subscribeKey:e},channel:t}=this.parameters;return`/v2/history/sub-key/${e}/channel/${R(t)}`}get queryParameters(){const{start:e,end:t,reverse:s,count:n,stringifiedTimeToken:r,includeMeta:i}=this.parameters;return Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({count:n,include_token:"true"},e?{start:e}:{}),t?{end:t}:{}),r?{string_message_token:"true"}:{}),null!=s?{reverse:s.toString()}:{}),i?{include_meta:"true"}:{})}processPayload(e){const{crypto:t,logVerbosity:s}=this.parameters;if(!t||"string"!=typeof e)return{payload:e};let n,r;try{const s=t.decrypt(e);n=s instanceof ArrayBuffer?JSON.parse(qt.decoder.decode(s)):s}catch(t){s&&console.log("decryption error",t.message),n=e,r=`Error while decrypting message content: ${t.message}`}return{payload:n,error:r}}}var Gt;!function(e){e[e.Message=-1]="Message",e[e.Files=4]="Files"}(Gt||(Gt={}));class Kt extends le{constructor(e){var t,s,n,r,i;super(),this.parameters=e;const a=null!==(t=e.includeMessageActions)&&void 0!==t&&t,o=e.channels.length>1||a?25:100;e.count?e.count=Math.min(e.count,o):e.count=o,e.includeUuid?e.includeUUID=e.includeUuid:null!==(s=e.includeUUID)&&void 0!==s||(e.includeUUID=true),null!==(n=e.stringifiedTimeToken)&&void 0!==n||(e.stringifiedTimeToken=false),null!==(r=e.includeMessageType)&&void 0!==r||(e.includeMessageType=true),null!==(i=e.logVerbosity)&&void 0!==i||(e.logVerbosity=false)}operation(){return M.PNFetchMessagesOperation}validate(){const{keySet:{subscribeKey:e},channels:t,includeMessageActions:s}=this.parameters;return e?t?void 0!==s&&s&&t.length>1?"History can return actions data for a single channel only. Either pass a single channel or disable the includeMessageActions flag.":void 0:"Missing channels":"Missing Subscribe Key"}parse(e){return i(this,void 0,void 0,(function*(){var t;const s=this.deserializeResponse(e),n=null!==(t=s.channels)&&void 0!==t?t:{},r={};return Object.keys(n).forEach((e=>{r[e]=n[e].map((t=>{null===t.message_type&&(t.message_type=Gt.Message);const s=this.processPayload(e,t),n=Object.assign(Object.assign({channel:e,timetoken:t.timetoken,message:s.payload,messageType:t.message_type},t.custom_message_type?{customMessageType:t.custom_message_type}:{}),{uuid:t.uuid});if(t.actions){const e=n;e.actions=t.actions,e.data=t.actions}return t.meta&&(n.meta=t.meta),s.error&&(n.error=s.error),n}))})),s.more?{channels:r,more:s.more}:{channels:r}}))}get path(){const{keySet:{subscribeKey:e},channels:t,includeMessageActions:s}=this.parameters;return`/v3/${s?"history-with-actions":"history"}/sub-key/${e}/channel/${$(t)}`}get queryParameters(){const{start:e,end:t,count:s,includeCustomMessageType:n,includeMessageType:r,includeMeta:i,includeUUID:a,stringifiedTimeToken:o}=this.parameters;return Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({max:s},e?{start:e}:{}),t?{end:t}:{}),o?{string_message_token:"true"}:{}),void 0!==i&&i?{include_meta:"true"}:{}),a?{include_uuid:"true"}:{}),null!=n?{include_custom_message_type:n?"true":"false"}:{}),r?{include_message_type:"true"}:{})}processPayload(e,t){const{crypto:s,logVerbosity:n}=this.parameters;if(!s||"string"!=typeof t.message)return{payload:t.message};let r,i;try{const e=s.decrypt(t.message);r=e instanceof ArrayBuffer?JSON.parse(Kt.decoder.decode(e)):e}catch(e){n&&console.log("decryption error",e.message),r=t.message,i=`Error while decrypting message content: ${e.message}`}if(!i&&r&&t.message_type==Gt.Files&&"object"==typeof r&&this.isFileMessage(r)){const t=r;return{payload:{message:t.message,file:Object.assign(Object.assign({},t.file),{url:this.parameters.getFileUrl({channel:e,id:t.file.id,name:t.file.name})})},error:i}}return{payload:r,error:i}}isFileMessage(e){return void 0!==e.file}}class Ht extends le{constructor(e){super(),this.parameters=e}operation(){return M.PNGetMessageActionsOperation}validate(){return this.parameters.keySet.subscribeKey?this.parameters.channel?void 0:"Missing message channel":"Missing Subscribe Key"}parse(e){return i(this,void 0,void 0,(function*(){const t=this.deserializeResponse(e);let s=null,n=null;return t.data.length>0&&(s=t.data[0].actionTimetoken,n=t.data[t.data.length-1].actionTimetoken),{data:t.data,more:t.more,start:s,end:n}}))}get path(){const{keySet:{subscribeKey:e},channel:t}=this.parameters;return`/v1/message-actions/${e}/channel/${R(t)}`}get queryParameters(){const{limit:e,start:t,end:s}=this.parameters;return Object.assign(Object.assign(Object.assign({},t?{start:t}:{}),s?{end:s}:{}),e?{limit:e}:{})}}class Bt extends le{constructor(e){super({method:ae.POST}),this.parameters=e}operation(){return M.PNAddMessageActionOperation}validate(){const{keySet:{subscribeKey:e},action:t,channel:s,messageTimetoken:n}=this.parameters;return e?s?n?t?t.value?t.type?t.type.length>15?"Action.type value exceed maximum length of 15":void 0:"Missing Action.type":"Missing Action.value":"Missing Action":"Missing message timetoken":"Missing message channel":"Missing Subscribe Key"}parse(e){const t=Object.create(null,{parse:{get:()=>super.parse}});return i(this,void 0,void 0,(function*(){return t.parse.call(this,e).then((({data:e})=>({data:e})))}))}get path(){const{keySet:{subscribeKey:e},channel:t,messageTimetoken:s}=this.parameters;return`/v1/message-actions/${e}/channel/${R(t)}/message/${s}`}get headers(){var e;return Object.assign(Object.assign({},null!==(e=super.headers)&&void 0!==e?e:{}),{"Content-Type":"application/json"})}get body(){return JSON.stringify(this.parameters.action)}}class Wt extends le{constructor(e){super({method:ae.DELETE}),this.parameters=e}operation(){return M.PNRemoveMessageActionOperation}validate(){const{keySet:{subscribeKey:e},channel:t,messageTimetoken:s,actionTimetoken:n}=this.parameters;return e?t?s?n?void 0:"Missing action timetoken":"Missing message timetoken":"Missing message action channel":"Missing Subscribe Key"}parse(e){const t=Object.create(null,{parse:{get:()=>super.parse}});return i(this,void 0,void 0,(function*(){return t.parse.call(this,e).then((({data:e})=>({data:e})))}))}get path(){const{keySet:{subscribeKey:e},channel:t,actionTimetoken:s,messageTimetoken:n}=this.parameters;return`/v1/message-actions/${e}/channel/${R(t)}/message/${n}/action/${s}`}}class zt extends le{constructor(e){var t,s;super(),this.parameters=e,null!==(t=(s=this.parameters).storeInHistory)&&void 0!==t||(s.storeInHistory=true)}operation(){return M.PNPublishFileMessageOperation}validate(){const{channel:e,fileId:t,fileName:s}=this.parameters;return e?t?s?void 0:"file name can't be empty":"file id can't be empty":"channel can't be empty"}parse(e){return i(this,void 0,void 0,(function*(){return{timetoken:this.deserializeResponse(e)[2]}}))}get path(){const{message:e,channel:t,keySet:{publishKey:s,subscribeKey:n},fileId:r,fileName:i}=this.parameters,a=Object.assign({file:{name:i,id:r}},e?{message:e}:{});return`/v1/files/publish-file/${s}/${n}/0/${R(t)}/0/${R(this.prepareMessagePayload(a))}`}get queryParameters(){const{customMessageType:e,storeInHistory:t,ttl:s,meta:n}=this.parameters;return Object.assign(Object.assign(Object.assign({store:t?"1":"0"},e?{custom_message_type:e}:{}),s?{ttl:s}:{}),n&&"object"==typeof n?{meta:JSON.stringify(n)}:{})}prepareMessagePayload(e){const{crypto:t}=this.parameters;if(!t)return JSON.stringify(e)||"";const s=t.encrypt(JSON.stringify(e));return JSON.stringify("string"==typeof s?s:u(s))}}class Vt extends le{constructor(e){super({method:ae.LOCAL}),this.parameters=e}operation(){return M.PNGetFileUrlOperation}validate(){const{channel:e,id:t,name:s}=this.parameters;return e?t?s?void 0:"file name can't be empty":"file id can't be empty":"channel can't be empty"}parse(e){return i(this,void 0,void 0,(function*(){return e.url}))}get path(){const{channel:e,id:t,name:s,keySet:{subscribeKey:n}}=this.parameters;return`/v1/files/${n}/channels/${R(e)}/files/${t}/${s}`}}class Jt extends le{constructor(e){super({method:ae.DELETE}),this.parameters=e}operation(){return M.PNDeleteFileOperation}validate(){const{channel:e,id:t,name:s}=this.parameters;return e?t?s?void 0:"file name can't be empty":"file id can't be empty":"channel can't be empty"}get path(){const{keySet:{subscribeKey:e},id:t,channel:s,name:n}=this.parameters;return`/v1/files/${e}/channels/${R(s)}/files/${t}/${n}`}}class Xt extends le{constructor(e){var t,s;super(),this.parameters=e,null!==(t=(s=this.parameters).limit)&&void 0!==t||(s.limit=100)}operation(){return M.PNListFilesOperation}validate(){if(!this.parameters.channel)return"channel can't be empty"}get path(){const{keySet:{subscribeKey:e},channel:t}=this.parameters;return`/v1/files/${e}/channels/${R(t)}/files`}get queryParameters(){const{limit:e,next:t}=this.parameters;return Object.assign({limit:e},t?{next:t}:{})}}class Qt extends le{constructor(e){super({method:ae.POST}),this.parameters=e}operation(){return M.PNGenerateUploadUrlOperation}validate(){return this.parameters.channel?this.parameters.name?void 0:"'name' can't be empty":"channel can't be empty"}parse(e){return i(this,void 0,void 0,(function*(){const t=this.deserializeResponse(e);return{id:t.data.id,name:t.data.name,url:t.file_upload_request.url,formFields:t.file_upload_request.form_fields}}))}get path(){const{keySet:{subscribeKey:e},channel:t}=this.parameters;return`/v1/files/${e}/channels/${R(t)}/generate-upload-url`}get headers(){var e;return Object.assign(Object.assign({},null!==(e=super.headers)&&void 0!==e?e:{}),{"Content-Type":"application/json"})}get body(){return JSON.stringify({name:this.parameters.name})}}class Yt extends le{constructor(e){super({method:ae.POST}),this.parameters=e;const t=e.file.mimeType;t&&(e.formFields=e.formFields.map((e=>"Content-Type"===e.name?{name:e.name,value:t}:e)))}operation(){return M.PNPublishFileOperation}validate(){const{fileId:e,fileName:t,file:s,uploadUrl:n}=this.parameters;return e?t?s?n?void 0:"Validation failed: file upload 'url' can't be empty":"Validation failed: 'file' can't be empty":"Validation failed: file 'name' can't be empty":"Validation failed: file 'id' can't be empty"}parse(e){return i(this,void 0,void 0,(function*(){return{status:e.status,message:e.body?Yt.decoder.decode(e.body):"OK"}}))}request(){return Object.assign(Object.assign({},super.request()),{origin:new URL(this.parameters.uploadUrl).origin,timeout:300})}get path(){const{pathname:e,search:t}=new URL(this.parameters.uploadUrl);return`${e}${t}`}get body(){return this.parameters.file}get formData(){return this.parameters.formFields}}class Zt{constructor(e){var t;if(this.parameters=e,this.file=null===(t=this.parameters.PubNubFile)||void 0===t?void 0:t.create(e.file),!this.file)throw new Error("File upload error: unable to create File object.")}process(){return i(this,void 0,void 0,(function*(){let e,t;return this.generateFileUploadUrl().then((s=>(e=s.name,t=s.id,this.uploadFile(s)))).then((e=>{if(204!==e.status)throw new d("Upload to bucket was unsuccessful",{error:!0,statusCode:e.status,category:h.PNUnknownCategory,operation:M.PNPublishFileOperation,errorData:{message:e.message}})})).then((()=>this.publishFileMessage(t,e))).catch((e=>{if(e instanceof d)throw e;const t=e instanceof I?e:I.create(e);throw new d("File upload error.",t.toStatus(M.PNPublishFileOperation))}))}))}generateFileUploadUrl(){return i(this,void 0,void 0,(function*(){const e=new Qt(Object.assign(Object.assign({},this.parameters),{name:this.file.name,keySet:this.parameters.keySet}));return this.parameters.sendRequest(e)}))}uploadFile(e){return i(this,void 0,void 0,(function*(){const{cipherKey:t,PubNubFile:s,crypto:n,cryptography:r}=this.parameters,{id:i,name:a,url:o,formFields:c}=e;return this.parameters.PubNubFile.supportsEncryptFile&&(!t&&n?this.file=yield n.encryptFile(this.file,s):t&&r&&(this.file=yield r.encryptFile(t,this.file,s))),this.parameters.sendRequest(new Yt({fileId:i,fileName:a,file:this.file,uploadUrl:o,formFields:c}))}))}publishFileMessage(e,t){return i(this,void 0,void 0,(function*(){var s,n,r,i;let a,o={timetoken:"0"},c=this.parameters.fileUploadPublishRetryLimit,u=!1;do{try{o=yield this.parameters.publishFile(Object.assign(Object.assign({},this.parameters),{fileId:e,fileName:t})),u=!0}catch(e){e instanceof d&&(a=e),c-=1}}while(!u&&c>0);if(u)return{status:200,timetoken:o.timetoken,id:e,name:t};throw new d("Publish failed. You may want to execute that operation manually using pubnub.publishFile",{error:!0,category:null!==(n=null===(s=a.status)||void 0===s?void 0:s.category)&&void 0!==n?n:h.PNUnknownCategory,statusCode:null!==(i=null===(r=a.status)||void 0===r?void 0:r.statusCode)&&void 0!==i?i:0,channel:this.parameters.channel,id:e,name:t})}))}}class es{constructor(e,t){this.subscriptionStateIds=[],this.client=t,this._nameOrId=e}get entityType(){return"Channel"}get subscriptionType(){return Pt.Channel}subscriptionNames(e){return[this._nameOrId,...e&&!this._nameOrId.endsWith("-pnpres")?[`${this._nameOrId}-pnpres`]:[]]}subscription(e){return new Mt({client:this.client,entity:this,options:e})}get subscriptionsCount(){return this.subscriptionStateIds.length}increaseSubscriptionCount(e){this.subscriptionStateIds.includes(e)||this.subscriptionStateIds.push(e)}decreaseSubscriptionCount(e){{const t=this.subscriptionStateIds.indexOf(e);t>=0&&this.subscriptionStateIds.splice(t,1)}}toString(){return`${this.entityType} { nameOrId: ${this._nameOrId}, subscriptionsCount: ${this.subscriptionsCount} }`}}class ts extends es{get entityType(){return"ChannelMetadata"}get id(){return this._nameOrId}subscriptionNames(e){return[this.id]}}class ss extends es{get entityType(){return"ChannelGroups"}get name(){return this._nameOrId}get subscriptionType(){return Pt.ChannelGroup}}class ns extends es{get entityType(){return"UserMetadata"}get id(){return this._nameOrId}subscriptionNames(e){return[this.id]}}class rs extends es{get entityType(){return"Channel"}get name(){return this._nameOrId}}class is extends le{constructor(e){super(),this.parameters=e}operation(){return M.PNRemoveChannelsFromGroupOperation}validate(){const{keySet:{subscribeKey:e},channels:t,channelGroup:s}=this.parameters;return e?s?t?void 0:"Missing channels":"Missing Channel Group":"Missing Subscribe Key"}parse(e){const t=Object.create(null,{parse:{get:()=>super.parse}});return i(this,void 0,void 0,(function*(){return t.parse.call(this,e).then((e=>({})))}))}get path(){const{keySet:{subscribeKey:e},channelGroup:t}=this.parameters;return`/v1/channel-registration/sub-key/${e}/channel-group/${R(t)}`}get queryParameters(){return{remove:this.parameters.channels.join(",")}}}class as extends le{constructor(e){super(),this.parameters=e}operation(){return M.PNAddChannelsToGroupOperation}validate(){const{keySet:{subscribeKey:e},channels:t,channelGroup:s}=this.parameters;return e?s?t?void 0:"Missing channels":"Missing Channel Group":"Missing Subscribe Key"}parse(e){const t=Object.create(null,{parse:{get:()=>super.parse}});return i(this,void 0,void 0,(function*(){return t.parse.call(this,e).then((e=>({})))}))}get path(){const{keySet:{subscribeKey:e},channelGroup:t}=this.parameters;return`/v1/channel-registration/sub-key/${e}/channel-group/${R(t)}`}get queryParameters(){return{add:this.parameters.channels.join(",")}}}class os extends le{constructor(e){super(),this.parameters=e}operation(){return M.PNChannelsForGroupOperation}validate(){return this.parameters.keySet.subscribeKey?this.parameters.channelGroup?void 0:"Missing Channel Group":"Missing Subscribe Key"}parse(e){return i(this,void 0,void 0,(function*(){return{channels:this.deserializeResponse(e).payload.channels}}))}get path(){const{keySet:{subscribeKey:e},channelGroup:t}=this.parameters;return`/v1/channel-registration/sub-key/${e}/channel-group/${R(t)}`}}class cs extends le{constructor(e){super(),this.parameters=e}operation(){return M.PNRemoveGroupOperation}validate(){return this.parameters.keySet.subscribeKey?this.parameters.channelGroup?void 0:"Missing Channel Group":"Missing Subscribe Key"}parse(e){const t=Object.create(null,{parse:{get:()=>super.parse}});return i(this,void 0,void 0,(function*(){return t.parse.call(this,e).then((e=>({})))}))}get path(){const{keySet:{subscribeKey:e},channelGroup:t}=this.parameters;return`/v1/channel-registration/sub-key/${e}/channel-group/${R(t)}/remove`}}class us extends le{constructor(e){super(),this.parameters=e}operation(){return M.PNChannelGroupsOperation}validate(){if(!this.parameters.keySet.subscribeKey)return"Missing Subscribe Key"}parse(e){return i(this,void 0,void 0,(function*(){return{groups:this.deserializeResponse(e).payload.groups}}))}get path(){return`/v1/channel-registration/sub-key/${this.parameters.keySet.subscribeKey}/channel-group`}}class ls{constructor(e,t,s){this.sendRequest=s,this.logger=e,this.keySet=t}listChannels(e,t){return i(this,void 0,void 0,(function*(){this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"List channel group channels with parameters:"})));const s=new os(Object.assign(Object.assign({},e),{keySet:this.keySet})),n=e=>{e&&this.logger.debug("PubNub",`List channel group channels success. Received ${e.channels.length} channels.`)};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}))}listGroups(e){return i(this,void 0,void 0,(function*(){this.logger.debug("PubNub","List all channel groups.");const t=new us({keySet:this.keySet}),s=e=>{e&&this.logger.debug("PubNub",`List all channel groups success. Received ${e.groups.length} groups.`)};return e?this.sendRequest(t,((t,n)=>{s(n),e(t,n)})):this.sendRequest(t).then((e=>(s(e),e)))}))}addChannels(e,t){return i(this,void 0,void 0,(function*(){this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Add channels to the channel group with parameters:"})));const s=new as(Object.assign(Object.assign({},e),{keySet:this.keySet})),n=()=>{this.logger.debug("PubNub","Add channels to the channel group success.")};return t?this.sendRequest(s,(e=>{e.error||n(),t(e)})):this.sendRequest(s).then((e=>(n(),e)))}))}removeChannels(e,t){return i(this,void 0,void 0,(function*(){this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Remove channels from the channel group with parameters:"})));const s=new is(Object.assign(Object.assign({},e),{keySet:this.keySet})),n=()=>{this.logger.debug("PubNub","Remove channels from the channel group success.")};return t?this.sendRequest(s,(e=>{e.error||n(),t(e)})):this.sendRequest(s).then((e=>(n(),e)))}))}deleteGroup(e,t){return i(this,void 0,void 0,(function*(){this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Remove a channel group with parameters:"})));const s=new cs(Object.assign(Object.assign({},e),{keySet:this.keySet})),n=()=>{this.logger.debug("PubNub",`Remove a channel group success. Removed '${e.channelGroup}' channel group.'`)};return t?this.sendRequest(s,(e=>{e.error||n(),t(e)})):this.sendRequest(s).then((e=>(n(),e)))}))}}class hs extends le{constructor(e){var t,s;super(),this.parameters=e,"apns2"===this.parameters.pushGateway&&(null!==(t=(s=this.parameters).environment)&&void 0!==t||(s.environment="development")),this.parameters.count&&this.parameters.count>1e3&&(this.parameters.count=1e3)}operation(){throw Error("Should be implemented in subclass.")}validate(){const{keySet:{subscribeKey:e},action:t,device:s,pushGateway:n}=this.parameters;return e?s?"add"!==t&&"remove"!==t||"channels"in this.parameters&&0!==this.parameters.channels.length?n?"apns2"!==this.parameters.pushGateway||this.parameters.topic?void 0:"Missing APNS2 topic":"Missing GW Type (pushGateway: gcm or apns2)":"Missing Channels":"Missing Device ID (device)":"Missing Subscribe Key"}get path(){const{keySet:{subscribeKey:e},action:t,device:s,pushGateway:n}=this.parameters;let r="apns2"===n?`/v2/push/sub-key/${e}/devices-apns2/${s}`:`/v1/push/sub-key/${e}/devices/${s}`;return"remove-device"===t&&(r=`${r}/remove`),r}get queryParameters(){const{start:e,count:t}=this.parameters;let s=Object.assign(Object.assign({type:this.parameters.pushGateway},e?{start:e}:{}),t&&t>0?{count:t}:{});if("channels"in this.parameters&&(s[this.parameters.action]=this.parameters.channels.join(",")),"apns2"===this.parameters.pushGateway){const{environment:e,topic:t}=this.parameters;s=Object.assign(Object.assign({},s),{environment:e,topic:t})}return s}}class ds extends hs{constructor(e){super(Object.assign(Object.assign({},e),{action:"remove"}))}operation(){return M.PNRemovePushNotificationEnabledChannelsOperation}parse(e){const t=Object.create(null,{parse:{get:()=>super.parse}});return i(this,void 0,void 0,(function*(){return t.parse.call(this,e).then((e=>({})))}))}}class ps extends hs{constructor(e){super(Object.assign(Object.assign({},e),{action:"list"}))}operation(){return M.PNPushNotificationEnabledChannelsOperation}parse(e){return i(this,void 0,void 0,(function*(){return{channels:this.deserializeResponse(e)}}))}}class gs extends hs{constructor(e){super(Object.assign(Object.assign({},e),{action:"add"}))}operation(){return M.PNAddPushNotificationEnabledChannelsOperation}parse(e){const t=Object.create(null,{parse:{get:()=>super.parse}});return i(this,void 0,void 0,(function*(){return t.parse.call(this,e).then((e=>({})))}))}}class bs extends hs{constructor(e){super(Object.assign(Object.assign({},e),{action:"remove-device"}))}operation(){return M.PNRemoveAllPushNotificationsOperation}parse(e){const t=Object.create(null,{parse:{get:()=>super.parse}});return i(this,void 0,void 0,(function*(){return t.parse.call(this,e).then((e=>({})))}))}}class ys{constructor(e,t,s){this.sendRequest=s,this.logger=e,this.keySet=t}listChannels(e,t){return i(this,void 0,void 0,(function*(){this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"List push-enabled channels with parameters:"})));const s=new ps(Object.assign(Object.assign({},e),{keySet:this.keySet})),n=e=>{e&&this.logger.debug("PubNub",`List push-enabled channels success. Received ${e.channels.length} channels.`)};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}))}addChannels(e,t){return i(this,void 0,void 0,(function*(){this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Add push-enabled channels with parameters:"})));const s=new gs(Object.assign(Object.assign({},e),{keySet:this.keySet})),n=()=>{this.logger.debug("PubNub","Add push-enabled channels success.")};return t?this.sendRequest(s,(e=>{e.error||n(),t(e)})):this.sendRequest(s).then((e=>(n(),e)))}))}removeChannels(e,t){return i(this,void 0,void 0,(function*(){this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Remove push-enabled channels with parameters:"})));const s=new ds(Object.assign(Object.assign({},e),{keySet:this.keySet})),n=()=>{this.logger.debug("PubNub","Remove push-enabled channels success.")};return t?this.sendRequest(s,(e=>{e.error||n(),t(e)})):this.sendRequest(s).then((e=>(n(),e)))}))}deleteDevice(e,t){return i(this,void 0,void 0,(function*(){this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Remove push notifications for device with parameters:"})));const s=new bs(Object.assign(Object.assign({},e),{keySet:this.keySet})),n=()=>{this.logger.debug("PubNub","Remove push notifications for device success.")};return t?this.sendRequest(s,(e=>{e.error||n(),t(e)})):this.sendRequest(s).then((e=>(n(),e)))}))}}class ms extends le{constructor(e){var t,s,n,r,i,a;super(),this.parameters=e,null!==(t=e.include)&&void 0!==t||(e.include={}),null!==(s=(i=e.include).customFields)&&void 0!==s||(i.customFields=false),null!==(n=(a=e.include).totalCount)&&void 0!==n||(a.totalCount=false),null!==(r=e.limit)&&void 0!==r||(e.limit=100)}operation(){return M.PNGetAllChannelMetadataOperation}get path(){return`/v2/objects/${this.parameters.keySet.subscribeKey}/channels`}get queryParameters(){const{include:e,page:t,filter:s,sort:n,limit:r}=this.parameters;let i="";return i="string"==typeof n?n:Object.entries(null!=n?n:{}).map((([e,t])=>null!==t?`${e}:${t}`:e)),Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({include:["status","type",...e.customFields?["custom"]:[]].join(","),count:`${e.totalCount}`},s?{filter:s}:{}),(null==t?void 0:t.next)?{start:t.next}:{}),(null==t?void 0:t.prev)?{end:t.prev}:{}),r?{limit:r}:{}),i.length?{sort:i}:{})}}class fs extends le{constructor(e){super({method:ae.DELETE}),this.parameters=e}operation(){return M.PNRemoveChannelMetadataOperation}validate(){if(!this.parameters.channel)return"Channel cannot be empty"}get path(){const{keySet:{subscribeKey:e},channel:t}=this.parameters;return`/v2/objects/${e}/channels/${R(t)}`}}class vs extends le{constructor(e){var t,s,n,r,i,a,o,c,u,l,h,d,p,g,b,y,m,f;super(),this.parameters=e,null!==(t=e.include)&&void 0!==t||(e.include={}),null!==(s=(h=e.include).customFields)&&void 0!==s||(h.customFields=false),null!==(n=(d=e.include).totalCount)&&void 0!==n||(d.totalCount=false),null!==(r=(p=e.include).statusField)&&void 0!==r||(p.statusField=false),null!==(i=(g=e.include).typeField)&&void 0!==i||(g.typeField=false),null!==(a=(b=e.include).channelFields)&&void 0!==a||(b.channelFields=false),null!==(o=(y=e.include).customChannelFields)&&void 0!==o||(y.customChannelFields=false),null!==(c=(m=e.include).channelStatusField)&&void 0!==c||(m.channelStatusField=false),null!==(u=(f=e.include).channelTypeField)&&void 0!==u||(f.channelTypeField=false),null!==(l=e.limit)&&void 0!==l||(e.limit=100),this.parameters.userId&&(this.parameters.uuid=this.parameters.userId)}operation(){return M.PNGetMembershipsOperation}validate(){if(!this.parameters.uuid)return"'uuid' cannot be empty"}get path(){const{keySet:{subscribeKey:e},uuid:t}=this.parameters;return`/v2/objects/${e}/uuids/${R(t)}/channels`}get queryParameters(){const{include:e,page:t,filter:s,sort:n,limit:r}=this.parameters;let i="";i="string"==typeof n?n:Object.entries(null!=n?n:{}).map((([e,t])=>null!==t?`${e}:${t}`:e));const a=[];return e.statusField&&a.push("status"),e.typeField&&a.push("type"),e.customFields&&a.push("custom"),e.channelFields&&a.push("channel"),e.channelStatusField&&a.push("channel.status"),e.channelTypeField&&a.push("channel.type"),e.customChannelFields&&a.push("channel.custom"),Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({count:`${e.totalCount}`},a.length>0?{include:a.join(",")}:{}),s?{filter:s}:{}),(null==t?void 0:t.next)?{start:t.next}:{}),(null==t?void 0:t.prev)?{end:t.prev}:{}),r?{limit:r}:{}),i.length?{sort:i}:{})}}class Ss extends le{constructor(e){var t,s,n,r,i,a,o,c,u,l,h,d,p,g,b,y,m,f;super({method:ae.PATCH}),this.parameters=e,null!==(t=e.include)&&void 0!==t||(e.include={}),null!==(s=(h=e.include).customFields)&&void 0!==s||(h.customFields=false),null!==(n=(d=e.include).totalCount)&&void 0!==n||(d.totalCount=false),null!==(r=(p=e.include).statusField)&&void 0!==r||(p.statusField=false),null!==(i=(g=e.include).typeField)&&void 0!==i||(g.typeField=false),null!==(a=(b=e.include).channelFields)&&void 0!==a||(b.channelFields=false),null!==(o=(y=e.include).customChannelFields)&&void 0!==o||(y.customChannelFields=false),null!==(c=(m=e.include).channelStatusField)&&void 0!==c||(m.channelStatusField=false),null!==(u=(f=e.include).channelTypeField)&&void 0!==u||(f.channelTypeField=false),null!==(l=e.limit)&&void 0!==l||(e.limit=100),this.parameters.userId&&(this.parameters.uuid=this.parameters.userId)}operation(){return M.PNSetMembershipsOperation}validate(){const{uuid:e,channels:t}=this.parameters;return e?t&&0!==t.length?void 0:"Channels cannot be empty":"'uuid' cannot be empty"}get path(){const{keySet:{subscribeKey:e},uuid:t}=this.parameters;return`/v2/objects/${e}/uuids/${R(t)}/channels`}get queryParameters(){const{include:e,page:t,filter:s,sort:n,limit:r}=this.parameters;let i="";i="string"==typeof n?n:Object.entries(null!=n?n:{}).map((([e,t])=>null!==t?`${e}:${t}`:e));const a=["channel.status","channel.type","status"];return e.statusField&&a.push("status"),e.typeField&&a.push("type"),e.customFields&&a.push("custom"),e.channelFields&&a.push("channel"),e.channelStatusField&&a.push("channel.status"),e.channelTypeField&&a.push("channel.type"),e.customChannelFields&&a.push("channel.custom"),Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({count:`${e.totalCount}`},a.length>0?{include:a.join(",")}:{}),s?{filter:s}:{}),(null==t?void 0:t.next)?{start:t.next}:{}),(null==t?void 0:t.prev)?{end:t.prev}:{}),r?{limit:r}:{}),i.length?{sort:i}:{})}get headers(){var e;return Object.assign(Object.assign({},null!==(e=super.headers)&&void 0!==e?e:{}),{"Content-Type":"application/json"})}get body(){const{channels:e,type:t}=this.parameters;return JSON.stringify({[`${t}`]:e.map((e=>"string"==typeof e?{channel:{id:e}}:{channel:{id:e.id},status:e.status,type:e.type,custom:e.custom}))})}}class ws extends le{constructor(e){var t,s,n,r;super(),this.parameters=e,null!==(t=e.include)&&void 0!==t||(e.include={}),null!==(s=(r=e.include).customFields)&&void 0!==s||(r.customFields=false),null!==(n=e.limit)&&void 0!==n||(e.limit=100)}operation(){return M.PNGetAllUUIDMetadataOperation}get path(){return`/v2/objects/${this.parameters.keySet.subscribeKey}/uuids`}get queryParameters(){const{include:e,page:t,filter:s,sort:n,limit:r}=this.parameters;let i="";return i="string"==typeof n?n:Object.entries(null!=n?n:{}).map((([e,t])=>null!==t?`${e}:${t}`:e)),Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({include:["status","type",...e.customFields?["custom"]:[]].join(",")},void 0!==e.totalCount?{count:`${e.totalCount}`}:{}),s?{filter:s}:{}),(null==t?void 0:t.next)?{start:t.next}:{}),(null==t?void 0:t.prev)?{end:t.prev}:{}),r?{limit:r}:{}),i.length?{sort:i}:{})}}class Os extends le{constructor(e){var t,s,n;super(),this.parameters=e,null!==(t=e.include)&&void 0!==t||(e.include={}),null!==(s=(n=e.include).customFields)&&void 0!==s||(n.customFields=true)}operation(){return M.PNGetChannelMetadataOperation}validate(){if(!this.parameters.channel)return"Channel cannot be empty"}get path(){const{keySet:{subscribeKey:e},channel:t}=this.parameters;return`/v2/objects/${e}/channels/${R(t)}`}get queryParameters(){return{include:["status","type",...this.parameters.include.customFields?["custom"]:[]].join(",")}}}class ks extends le{constructor(e){var t,s,n;super({method:ae.PATCH}),this.parameters=e,null!==(t=e.include)&&void 0!==t||(e.include={}),null!==(s=(n=e.include).customFields)&&void 0!==s||(n.customFields=true)}operation(){return M.PNSetChannelMetadataOperation}validate(){return this.parameters.channel?this.parameters.data?void 0:"Data cannot be empty":"Channel cannot be empty"}get headers(){var e;let t=null!==(e=super.headers)&&void 0!==e?e:{};return this.parameters.ifMatchesEtag&&(t=Object.assign(Object.assign({},t),{"If-Match":this.parameters.ifMatchesEtag})),Object.assign(Object.assign({},t),{"Content-Type":"application/json"})}get path(){const{keySet:{subscribeKey:e},channel:t}=this.parameters;return`/v2/objects/${e}/channels/${R(t)}`}get queryParameters(){return{include:["status","type",...this.parameters.include.customFields?["custom"]:[]].join(",")}}get body(){return JSON.stringify(this.parameters.data)}}class Cs extends le{constructor(e){super({method:ae.DELETE}),this.parameters=e,this.parameters.userId&&(this.parameters.uuid=this.parameters.userId)}operation(){return M.PNRemoveUUIDMetadataOperation}validate(){if(!this.parameters.uuid)return"'uuid' cannot be empty"}get path(){const{keySet:{subscribeKey:e},uuid:t}=this.parameters;return`/v2/objects/${e}/uuids/${R(t)}`}}class Ps extends le{constructor(e){var t,s,n,r,i,a,o,c,u,l,h,d,p,g,b,y,m,f;super(),this.parameters=e,null!==(t=e.include)&&void 0!==t||(e.include={}),null!==(s=(h=e.include).customFields)&&void 0!==s||(h.customFields=false),null!==(n=(d=e.include).totalCount)&&void 0!==n||(d.totalCount=false),null!==(r=(p=e.include).statusField)&&void 0!==r||(p.statusField=false),null!==(i=(g=e.include).typeField)&&void 0!==i||(g.typeField=false),null!==(a=(b=e.include).UUIDFields)&&void 0!==a||(b.UUIDFields=false),null!==(o=(y=e.include).customUUIDFields)&&void 0!==o||(y.customUUIDFields=false),null!==(c=(m=e.include).UUIDStatusField)&&void 0!==c||(m.UUIDStatusField=false),null!==(u=(f=e.include).UUIDTypeField)&&void 0!==u||(f.UUIDTypeField=false),null!==(l=e.limit)&&void 0!==l||(e.limit=100)}operation(){return M.PNSetMembersOperation}validate(){if(!this.parameters.channel)return"Channel cannot be empty"}get path(){const{keySet:{subscribeKey:e},channel:t}=this.parameters;return`/v2/objects/${e}/channels/${R(t)}/uuids`}get queryParameters(){const{include:e,page:t,filter:s,sort:n,limit:r}=this.parameters;let i="";i="string"==typeof n?n:Object.entries(null!=n?n:{}).map((([e,t])=>null!==t?`${e}:${t}`:e));const a=[];return e.statusField&&a.push("status"),e.typeField&&a.push("type"),e.customFields&&a.push("custom"),e.UUIDFields&&a.push("uuid"),e.UUIDStatusField&&a.push("uuid.status"),e.UUIDTypeField&&a.push("uuid.type"),e.customUUIDFields&&a.push("uuid.custom"),Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({count:`${e.totalCount}`},a.length>0?{include:a.join(",")}:{}),s?{filter:s}:{}),(null==t?void 0:t.next)?{start:t.next}:{}),(null==t?void 0:t.prev)?{end:t.prev}:{}),r?{limit:r}:{}),i.length?{sort:i}:{})}}class js extends le{constructor(e){var t,s,n,r,i,a,o,c,u,l,h,d,p,g,b,y,m,f;super({method:ae.PATCH}),this.parameters=e,null!==(t=e.include)&&void 0!==t||(e.include={}),null!==(s=(h=e.include).customFields)&&void 0!==s||(h.customFields=false),null!==(n=(d=e.include).totalCount)&&void 0!==n||(d.totalCount=false),null!==(r=(p=e.include).statusField)&&void 0!==r||(p.statusField=false),null!==(i=(g=e.include).typeField)&&void 0!==i||(g.typeField=false),null!==(a=(b=e.include).UUIDFields)&&void 0!==a||(b.UUIDFields=false),null!==(o=(y=e.include).customUUIDFields)&&void 0!==o||(y.customUUIDFields=false),null!==(c=(m=e.include).UUIDStatusField)&&void 0!==c||(m.UUIDStatusField=false),null!==(u=(f=e.include).UUIDTypeField)&&void 0!==u||(f.UUIDTypeField=false),null!==(l=e.limit)&&void 0!==l||(e.limit=100)}operation(){return M.PNSetMembersOperation}validate(){const{channel:e,uuids:t}=this.parameters;return e?t&&0!==t.length?void 0:"UUIDs cannot be empty":"Channel cannot be empty"}get path(){const{keySet:{subscribeKey:e},channel:t}=this.parameters;return`/v2/objects/${e}/channels/${R(t)}/uuids`}get queryParameters(){const{include:e,page:t,filter:s,sort:n,limit:r}=this.parameters;let i="";i="string"==typeof n?n:Object.entries(null!=n?n:{}).map((([e,t])=>null!==t?`${e}:${t}`:e));const a=["uuid.status","uuid.type","type"];return e.statusField&&a.push("status"),e.typeField&&a.push("type"),e.customFields&&a.push("custom"),e.UUIDFields&&a.push("uuid"),e.UUIDStatusField&&a.push("uuid.status"),e.UUIDTypeField&&a.push("uuid.type"),e.customUUIDFields&&a.push("uuid.custom"),Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({count:`${e.totalCount}`},a.length>0?{include:a.join(",")}:{}),s?{filter:s}:{}),(null==t?void 0:t.next)?{start:t.next}:{}),(null==t?void 0:t.prev)?{end:t.prev}:{}),r?{limit:r}:{}),i.length?{sort:i}:{})}get headers(){var e;return Object.assign(Object.assign({},null!==(e=super.headers)&&void 0!==e?e:{}),{"Content-Type":"application/json"})}get body(){const{uuids:e,type:t}=this.parameters;return JSON.stringify({[`${t}`]:e.map((e=>"string"==typeof e?{uuid:{id:e}}:{uuid:{id:e.id},status:e.status,type:e.type,custom:e.custom}))})}}class Es extends le{constructor(e){var t,s,n;super(),this.parameters=e,null!==(t=e.include)&&void 0!==t||(e.include={}),null!==(s=(n=e.include).customFields)&&void 0!==s||(n.customFields=true),this.parameters.userId&&(this.parameters.uuid=this.parameters.userId)}operation(){return M.PNGetUUIDMetadataOperation}validate(){if(!this.parameters.uuid)return"'uuid' cannot be empty"}get path(){const{keySet:{subscribeKey:e},uuid:t}=this.parameters;return`/v2/objects/${e}/uuids/${R(t)}`}get queryParameters(){const{include:e}=this.parameters;return{include:["status","type",...e.customFields?["custom"]:[]].join(",")}}}class Ns extends le{constructor(e){var t,s,n;super({method:ae.PATCH}),this.parameters=e,null!==(t=e.include)&&void 0!==t||(e.include={}),null!==(s=(n=e.include).customFields)&&void 0!==s||(n.customFields=true),this.parameters.userId&&(this.parameters.uuid=this.parameters.userId)}operation(){return M.PNSetUUIDMetadataOperation}validate(){return this.parameters.uuid?this.parameters.data?void 0:"Data cannot be empty":"'uuid' cannot be empty"}get headers(){var e;let t=null!==(e=super.headers)&&void 0!==e?e:{};return this.parameters.ifMatchesEtag&&(t=Object.assign(Object.assign({},t),{"If-Match":this.parameters.ifMatchesEtag})),Object.assign(Object.assign({},t),{"Content-Type":"application/json"})}get path(){const{keySet:{subscribeKey:e},uuid:t}=this.parameters;return`/v2/objects/${e}/uuids/${R(t)}`}get queryParameters(){return{include:["status","type",...this.parameters.include.customFields?["custom"]:[]].join(",")}}get body(){return JSON.stringify(this.parameters.data)}}class Ts{constructor(e,t){this.keySet=e.keySet,this.configuration=e,this.sendRequest=t}get logger(){return this.configuration.logger()}getAllUUIDMetadata(e,t){return i(this,void 0,void 0,(function*(){return this.logger.debug("PubNub",(()=>({messageType:"object",message:e&&"function"!=typeof e?e:{},details:"Get all UUID metadata objects with parameters:"}))),this._getAllUUIDMetadata(e,t)}))}_getAllUUIDMetadata(e,t){return i(this,void 0,void 0,(function*(){const s=e&&"function"!=typeof e?e:{};null!=t||(t="function"==typeof e?e:void 0);const n=new ws(Object.assign(Object.assign({},s),{keySet:this.keySet})),r=e=>{e&&this.logger.debug("PubNub",`Get all UUID metadata success. Received ${e.totalCount} UUID metadata objects.`)};return t?this.sendRequest(n,((e,s)=>{r(s),t(e,s)})):this.sendRequest(n).then((e=>(r(e),e)))}))}getUUIDMetadata(e,t){return i(this,void 0,void 0,(function*(){return this.logger.debug("PubNub",(()=>({messageType:"object",message:e&&"function"!=typeof e?e:{uuid:this.configuration.userId},details:`Get ${e&&"function"!=typeof e?"":" current"} UUID metadata object with parameters:`}))),this._getUUIDMetadata(e,t)}))}_getUUIDMetadata(e,t){return i(this,void 0,void 0,(function*(){var s;const n=e&&"function"!=typeof e?e:{};null!=t||(t="function"==typeof e?e:void 0),n.userId&&(this.logger.warn("PubNub","'userId' parameter is deprecated. Use 'uuid' instead."),n.uuid=n.userId),null!==(s=n.uuid)&&void 0!==s||(n.uuid=this.configuration.userId);const r=new Es(Object.assign(Object.assign({},n),{keySet:this.keySet})),i=e=>{e&&this.logger.debug("PubNub",`Get UUID metadata object success. Received '${n.uuid}' UUID metadata object.`)};return t?this.sendRequest(r,((e,s)=>{i(s),t(e,s)})):this.sendRequest(r).then((e=>(i(e),e)))}))}setUUIDMetadata(e,t){return i(this,void 0,void 0,(function*(){return this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Set UUID metadata object with parameters:"}))),this._setUUIDMetadata(e,t)}))}_setUUIDMetadata(e,t){return i(this,void 0,void 0,(function*(){var s;e.userId&&(this.logger.warn("PubNub","'userId' parameter is deprecated. Use 'uuid' instead."),e.uuid=e.userId),null!==(s=e.uuid)&&void 0!==s||(e.uuid=this.configuration.userId);const n=new Ns(Object.assign(Object.assign({},e),{keySet:this.keySet})),r=t=>{t&&this.logger.debug("PubNub",`Set UUID metadata object success. Updated '${e.uuid}' UUID metadata object.`)};return t?this.sendRequest(n,((e,s)=>{r(s),t(e,s)})):this.sendRequest(n).then((e=>(r(e),e)))}))}removeUUIDMetadata(e,t){return i(this,void 0,void 0,(function*(){return this.logger.debug("PubNub",(()=>({messageType:"object",message:e&&"function"!=typeof e?e:{uuid:this.configuration.userId},details:`Remove${e&&"function"!=typeof e?"":" current"} UUID metadata object with parameters:`}))),this._removeUUIDMetadata(e,t)}))}_removeUUIDMetadata(e,t){return i(this,void 0,void 0,(function*(){var s;const n=e&&"function"!=typeof e?e:{};null!=t||(t="function"==typeof e?e:void 0),n.userId&&(this.logger.warn("PubNub","'userId' parameter is deprecated. Use 'uuid' instead."),n.uuid=n.userId),null!==(s=n.uuid)&&void 0!==s||(n.uuid=this.configuration.userId);const r=new Cs(Object.assign(Object.assign({},n),{keySet:this.keySet})),i=e=>{e&&this.logger.debug("PubNub",`Remove UUID metadata object success. Removed '${n.uuid}' UUID metadata object.`)};return t?this.sendRequest(r,((e,s)=>{i(s),t(e,s)})):this.sendRequest(r).then((e=>(i(e),e)))}))}getAllChannelMetadata(e,t){return i(this,void 0,void 0,(function*(){return this.logger.debug("PubNub",(()=>({messageType:"object",message:e&&"function"!=typeof e?e:{},details:"Get all Channel metadata objects with parameters:"}))),this._getAllChannelMetadata(e,t)}))}_getAllChannelMetadata(e,t){return i(this,void 0,void 0,(function*(){const s=e&&"function"!=typeof e?e:{};null!=t||(t="function"==typeof e?e:void 0);const n=new ms(Object.assign(Object.assign({},s),{keySet:this.keySet})),r=e=>{e&&this.logger.debug("PubNub",`Get all Channel metadata objects success. Received ${e.totalCount} Channel metadata objects.`)};return t?this.sendRequest(n,((e,s)=>{r(s),t(e,s)})):this.sendRequest(n).then((e=>(r(e),e)))}))}getChannelMetadata(e,t){return i(this,void 0,void 0,(function*(){return this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Get Channel metadata object with parameters:"}))),this._getChannelMetadata(e,t)}))}_getChannelMetadata(e,t){return i(this,void 0,void 0,(function*(){const s=new Os(Object.assign(Object.assign({},e),{keySet:this.keySet})),n=t=>{t&&this.logger.debug("PubNub",`Get Channel metadata object success. Received '${e.channel}' Channel metadata object.`)};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}))}setChannelMetadata(e,t){return i(this,void 0,void 0,(function*(){return this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Set Channel metadata object with parameters:"}))),this._setChannelMetadata(e,t)}))}_setChannelMetadata(e,t){return i(this,void 0,void 0,(function*(){const s=new ks(Object.assign(Object.assign({},e),{keySet:this.keySet})),n=t=>{t&&this.logger.debug("PubNub",`Set Channel metadata object success. Updated '${e.channel}' Channel metadata object.`)};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}))}removeChannelMetadata(e,t){return i(this,void 0,void 0,(function*(){return this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Remove Channel metadata object with parameters:"}))),this._removeChannelMetadata(e,t)}))}_removeChannelMetadata(e,t){return i(this,void 0,void 0,(function*(){const s=new fs(Object.assign(Object.assign({},e),{keySet:this.keySet})),n=t=>{t&&this.logger.debug("PubNub",`Remove Channel metadata object success. Removed '${e.channel}' Channel metadata object.`)};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}))}getChannelMembers(e,t){return i(this,void 0,void 0,(function*(){this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Get channel members with parameters:"})));const s=new Ps(Object.assign(Object.assign({},e),{keySet:this.keySet})),n=e=>{e&&this.logger.debug("PubNub",`Get channel members success. Received ${e.totalCount} channel members.`)};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}))}setChannelMembers(e,t){return i(this,void 0,void 0,(function*(){this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Set channel members with parameters:"})));const s=new js(Object.assign(Object.assign({},e),{type:"set",keySet:this.keySet})),n=e=>{e&&this.logger.debug("PubNub",`Set channel members success. There are ${e.totalCount} channel members now.`)};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}))}removeChannelMembers(e,t){return i(this,void 0,void 0,(function*(){this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Remove channel members with parameters:"})));const s=new js(Object.assign(Object.assign({},e),{type:"delete",keySet:this.keySet})),n=e=>{e&&this.logger.debug("PubNub",`Remove channel members success. There are ${e.totalCount} channel members now.`)};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}))}getMemberships(e,t){return i(this,void 0,void 0,(function*(){var s;const n=e&&"function"!=typeof e?e:{};null!=t||(t="function"==typeof e?e:void 0),n.userId&&(this.logger.warn("PubNub","'userId' parameter is deprecated. Use 'uuid' instead."),n.uuid=n.userId),null!==(s=n.uuid)&&void 0!==s||(n.uuid=this.configuration.userId),this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},n),details:"Get memberships with parameters:"})));const r=new vs(Object.assign(Object.assign({},n),{keySet:this.keySet})),i=e=>{e&&this.logger.debug("PubNub",`Get memberships success. Received ${e.totalCount} memberships.`)};return t?this.sendRequest(r,((e,s)=>{i(s),t(e,s)})):this.sendRequest(r).then((e=>(i(e),e)))}))}setMemberships(e,t){return i(this,void 0,void 0,(function*(){var s;e.userId&&(this.logger.warn("PubNub","'userId' parameter is deprecated. Use 'uuid' instead."),e.uuid=e.userId),null!==(s=e.uuid)&&void 0!==s||(e.uuid=this.configuration.userId),this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Set memberships with parameters:"})));const n=new Ss(Object.assign(Object.assign({},e),{type:"set",keySet:this.keySet})),r=e=>{e&&this.logger.debug("PubNub",`Set memberships success. There are ${e.totalCount} memberships now.`)};return t?this.sendRequest(n,((e,s)=>{r(s),t(e,s)})):this.sendRequest(n).then((e=>(r(e),e)))}))}removeMemberships(e,t){return i(this,void 0,void 0,(function*(){var s;e.userId&&(this.logger.warn("PubNub","'userId' parameter is deprecated. Use 'uuid' instead."),e.uuid=e.userId),null!==(s=e.uuid)&&void 0!==s||(e.uuid=this.configuration.userId),this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Remove memberships with parameters:"})));const n=new Ss(Object.assign(Object.assign({},e),{type:"delete",keySet:this.keySet})),r=e=>{e&&this.logger.debug("PubNub",`Remove memberships success. There are ${e.totalCount} memberships now.`)};return t?this.sendRequest(n,((e,s)=>{r(s),t(e,s)})):this.sendRequest(n).then((e=>(r(e),e)))}))}fetchMemberships(e,t){return i(this,void 0,void 0,(function*(){var s,n;if(this.logger.warn("PubNub","'fetchMemberships' is deprecated. Use 'pubnub.objects.getChannelMembers' or 'pubnub.objects.getMemberships' instead."),this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Fetch memberships with parameters:"}))),"spaceId"in e){const n=e,r={channel:null!==(s=n.spaceId)&&void 0!==s?s:n.channel,filter:n.filter,limit:n.limit,page:n.page,include:Object.assign({},n.include),sort:n.sort?Object.fromEntries(Object.entries(n.sort).map((([e,t])=>[e.replace("user","uuid"),t]))):void 0},i=e=>({status:e.status,data:e.data.map((e=>({user:e.uuid,custom:e.custom,updated:e.updated,eTag:e.eTag}))),totalCount:e.totalCount,next:e.next,prev:e.prev});return t?this.getChannelMembers(r,((e,s)=>{t(e,s?i(s):s)})):this.getChannelMembers(r).then(i)}const r=e,i={uuid:null!==(n=r.userId)&&void 0!==n?n:r.uuid,filter:r.filter,limit:r.limit,page:r.page,include:Object.assign({},r.include),sort:r.sort?Object.fromEntries(Object.entries(r.sort).map((([e,t])=>[e.replace("space","channel"),t]))):void 0},a=e=>({status:e.status,data:e.data.map((e=>({space:e.channel,custom:e.custom,updated:e.updated,eTag:e.eTag}))),totalCount:e.totalCount,next:e.next,prev:e.prev});return t?this.getMemberships(i,((e,s)=>{t(e,s?a(s):s)})):this.getMemberships(i).then(a)}))}addMemberships(e,t){return i(this,void 0,void 0,(function*(){var s,n,r,i,a,o;if(this.logger.warn("PubNub","'addMemberships' is deprecated. Use 'pubnub.objects.setChannelMembers' or 'pubnub.objects.setMemberships' instead."),this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Add memberships with parameters:"}))),"spaceId"in e){const i=e,a={channel:null!==(s=i.spaceId)&&void 0!==s?s:i.channel,uuids:null!==(r=null===(n=i.users)||void 0===n?void 0:n.map((e=>"string"==typeof e?e:{id:e.userId,custom:e.custom})))&&void 0!==r?r:i.uuids,limit:0};return t?this.setChannelMembers(a,t):this.setChannelMembers(a)}const c=e,u={uuid:null!==(i=c.userId)&&void 0!==i?i:c.uuid,channels:null!==(o=null===(a=c.spaces)||void 0===a?void 0:a.map((e=>"string"==typeof e?e:{id:e.spaceId,custom:e.custom})))&&void 0!==o?o:c.channels,limit:0};return t?this.setMemberships(u,t):this.setMemberships(u)}))}}class _s extends le{constructor(){super()}operation(){return M.PNTimeOperation}parse(e){return i(this,void 0,void 0,(function*(){return{timetoken:this.deserializeResponse(e)[0]}}))}get path(){return"/time/0"}}class Is extends le{constructor(e){super(),this.parameters=e}operation(){return M.PNDownloadFileOperation}validate(){const{channel:e,id:t,name:s}=this.parameters;return e?t?s?void 0:"file name can't be empty":"file id can't be empty":"channel can't be empty"}parse(e){return i(this,void 0,void 0,(function*(){const{cipherKey:t,crypto:s,cryptography:n,name:r,PubNubFile:i}=this.parameters,a=e.headers["content-type"];let o,c=e.body;return i.supportsEncryptFile&&(t||s)&&(t&&n?c=yield n.decrypt(t,c):!t&&s&&(o=yield s.decryptFile(i.create({data:c,name:r,mimeType:a}),i))),o||i.create({data:c,name:r,mimeType:a})}))}get path(){const{keySet:{subscribeKey:e},channel:t,id:s,name:n}=this.parameters;return`/v1/files/${e}/channels/${R(t)}/files/${s}/${n}`}}class Ms{static notificationPayload(e,t){return new we(e,t)}static generateUUID(){return te.createUUID()}constructor(e){if(this.eventHandleCapable={},this.entities={},this._configuration=e.configuration,this.cryptography=e.cryptography,this.tokenManager=e.tokenManager,this.transport=e.transport,this.crypto=e.crypto,this.logger.debug("PubNub",(()=>({messageType:"object",message:e.configuration,details:"Create with configuration:",ignoredKeys:(e,t)=>"function"==typeof t[e]||e.startsWith("_")}))),this._objects=new Ts(this._configuration,this.sendRequest.bind(this)),this._channelGroups=new ls(this._configuration.logger(),this._configuration.keySet,this.sendRequest.bind(this)),this._push=new ys(this._configuration.logger(),this._configuration.keySet,this.sendRequest.bind(this)),this.eventDispatcher=new ge,this._configuration.enableEventEngine){this.logger.debug("PubNub","Using new subscription loop management.");let e=this._configuration.getHeartbeatInterval();this.presenceState={},e&&(this.presenceEventEngine=new Ye({heartbeat:(e,t)=>(this.logger.trace("PresenceEventEngine",(()=>({messageType:"object",message:Object.assign({},e),details:"Heartbeat with parameters:"}))),this.heartbeat(e,t)),leave:e=>{this.logger.trace("PresenceEventEngine",(()=>({messageType:"object",message:Object.assign({},e),details:"Leave with parameters:"}))),this.makeUnsubscribe(e,(()=>{}))},heartbeatDelay:()=>new Promise(((t,s)=>{e=this._configuration.getHeartbeatInterval(),e?setTimeout(t,1e3*e):s(new d("Heartbeat interval has been reset."))})),emitStatus:e=>this.emitStatus(e),config:this._configuration,presenceState:this.presenceState})),this.eventEngine=new St({handshake:e=>(this.logger.trace("EventEngine",(()=>({messageType:"object",message:Object.assign({},e),details:"Handshake with parameters:",ignoredKeys:["abortSignal","crypto","timeout","keySet","getFileUrl"]}))),this.subscribeHandshake(e)),receiveMessages:e=>(this.logger.trace("EventEngine",(()=>({messageType:"object",message:Object.assign({},e),details:"Receive messages with parameters:",ignoredKeys:["abortSignal","crypto","timeout","keySet","getFileUrl"]}))),this.subscribeReceiveMessages(e)),delay:e=>new Promise((t=>setTimeout(t,e))),join:e=>{var t,s;this.logger.trace("EventEngine",(()=>({messageType:"object",message:Object.assign({},e),details:"Join with parameters:"}))),e&&0===(null!==(t=e.channels)&&void 0!==t?t:[]).length&&0===(null!==(s=e.groups)&&void 0!==s?s:[]).length?this.logger.trace("EventEngine","Ignoring 'join' announcement request."):this.join(e)},leave:e=>{var t,s;this.logger.trace("EventEngine",(()=>({messageType:"object",message:Object.assign({},e),details:"Leave with parameters:"}))),e&&0===(null!==(t=e.channels)&&void 0!==t?t:[]).length&&0===(null!==(s=e.groups)&&void 0!==s?s:[]).length?this.logger.trace("EventEngine","Ignoring 'leave' announcement request."):this.leave(e)},leaveAll:e=>{this.logger.trace("EventEngine",(()=>({messageType:"object",message:Object.assign({},e),details:"Leave all with parameters:"}))),this.leaveAll(e)},presenceReconnect:e=>{this.logger.trace("EventEngine",(()=>({messageType:"object",message:Object.assign({},e),details:"Reconnect with parameters:"}))),this.presenceReconnect(e)},presenceDisconnect:e=>{this.logger.trace("EventEngine",(()=>({messageType:"object",message:Object.assign({},e),details:"Disconnect with parameters:"}))),this.presenceDisconnect(e)},presenceState:this.presenceState,config:this._configuration,emitMessages:(e,t)=>{try{this.logger.debug("EventEngine",(()=>({messageType:"object",message:t.map((e=>{const t=e.type===he.Message||e.type===he.Signal?B(e.data.message):void 0;return t?{type:e.type,data:Object.assign(Object.assign({},e.data),{pn_mfp:t})}:e})),details:"Received events:"}))),t.forEach((t=>this.emitEvent(e,t)))}catch(e){const t={error:!0,category:h.PNUnknownCategory,errorData:e,statusCode:0};this.emitStatus(t)}},emitStatus:e=>this.emitStatus(e)})}else this.logger.debug("PubNub","Using legacy subscription loop management."),this.subscriptionManager=new me(this._configuration,((e,t)=>{try{this.emitEvent(e,t)}catch(e){const t={error:!0,category:h.PNUnknownCategory,errorData:e,statusCode:0};this.emitStatus(t)}}),this.emitStatus.bind(this),((e,t)=>{this.logger.trace("SubscriptionManager",(()=>({messageType:"object",message:Object.assign({},e),details:"Subscribe with parameters:",ignoredKeys:["crypto","timeout","keySet","getFileUrl"]}))),this.makeSubscribe(e,t)}),((e,t)=>(this.logger.trace("SubscriptionManager",(()=>({messageType:"object",message:Object.assign({},e),details:"Heartbeat with parameters:",ignoredKeys:["crypto","timeout","keySet","getFileUrl"]}))),this.heartbeat(e,t))),((e,t)=>{this.logger.trace("SubscriptionManager",(()=>({messageType:"object",message:Object.assign({},e),details:"Leave with parameters:"}))),this.makeUnsubscribe(e,t)}),this.time.bind(this))}get configuration(){return this._configuration}get _config(){return this.configuration}get authKey(){var e;return null!==(e=this._configuration.authKey)&&void 0!==e?e:void 0}getAuthKey(){return this.authKey}setAuthKey(e){this.logger.debug("PubNub",`Set auth key: ${e}`),this._configuration.setAuthKey(e),this.onAuthenticationChange&&this.onAuthenticationChange(e)}get userId(){return this._configuration.userId}set userId(e){if(!e||"string"!=typeof e||0===e.trim().length){const e=new Error("Missing or invalid userId parameter. Provide a valid string userId");throw this.logger.error("PubNub",(()=>({messageType:"error",message:e}))),e}this.logger.debug("PubNub",`Set user ID: ${e}`),this._configuration.userId=e,this.onUserIdChange&&this.onUserIdChange(this._configuration.userId)}getUserId(){return this._configuration.userId}setUserId(e){this.userId=e}get filterExpression(){var e;return null!==(e=this._configuration.getFilterExpression())&&void 0!==e?e:void 0}getFilterExpression(){return this.filterExpression}set filterExpression(e){this.logger.debug("PubNub",`Set filter expression: ${e}`),this._configuration.setFilterExpression(e)}setFilterExpression(e){this.logger.debug("PubNub",`Set filter expression: ${e}`),this.filterExpression=e}get cipherKey(){return this._configuration.getCipherKey()}set cipherKey(e){this._configuration.setCipherKey(e)}setCipherKey(e){this.logger.debug("PubNub",`Set cipher key: ${e}`),this.cipherKey=e}set heartbeatInterval(e){var t;this.logger.debug("PubNub",`Set heartbeat interval: ${e}`),this._configuration.setHeartbeatInterval(e),this.onHeartbeatIntervalChange&&this.onHeartbeatIntervalChange(null!==(t=this._configuration.getHeartbeatInterval())&&void 0!==t?t:0)}setHeartbeatInterval(e){this.heartbeatInterval=e}get logger(){return this._configuration.logger()}getVersion(){return this._configuration.getVersion()}_addPnsdkSuffix(e,t){this.logger.debug("PubNub",`Add '${e}' 'pnsdk' suffix: ${t}`),this._configuration._addPnsdkSuffix(e,t)}getUUID(){return this.userId}setUUID(e){this.logger.warn("PubNub","'setUserId` is deprecated, please use 'setUserId' or 'userId' setter instead."),this.logger.debug("PubNub",`Set UUID: ${e}`),this.userId=e}get customEncrypt(){return this._configuration.getCustomEncrypt()}get customDecrypt(){return this._configuration.getCustomDecrypt()}channel(e){let t=this.entities[`${e}_ch`];return t||(t=this.entities[`${e}_ch`]=new rs(e,this)),t}channelGroup(e){let t=this.entities[`${e}_chg`];return t||(t=this.entities[`${e}_chg`]=new ss(e,this)),t}channelMetadata(e){let t=this.entities[`${e}_chm`];return t||(t=this.entities[`${e}_chm`]=new ts(e,this)),t}userMetadata(e){let t=this.entities[`${e}_um`];return t||(t=this.entities[`${e}_um`]=new ns(e,this)),t}subscriptionSet(e){var t,s;{const n=[];return null===(t=e.channels)||void 0===t||t.forEach((e=>n.push(this.channel(e)))),null===(s=e.channelGroups)||void 0===s||s.forEach((e=>n.push(this.channelGroup(e)))),new _t({client:this,entities:n,options:e.subscriptionOptions})}}sendRequest(e,t){return i(this,void 0,void 0,(function*(){const s=e.validate();if(s){const e=(n=s,p(Object.assign({message:n},{}),h.PNValidationErrorCategory));if(this.logger.error("PubNub",(()=>({messageType:"error",message:e}))),t)return t(e,null);throw new d("Validation failed, check status for details",e)}var n;const r=e.request(),i=e.operation();r.formData&&r.formData.length>0||i===M.PNDownloadFileOperation?r.timeout=this._configuration.getFileTimeout():i===M.PNSubscribeOperation||i===M.PNReceiveMessagesOperation?r.timeout=this._configuration.getSubscribeTimeout():r.timeout=this._configuration.getTransactionTimeout();const a={error:!1,operation:i,category:h.PNAcknowledgmentCategory,statusCode:0},[o,c]=this.transport.makeSendable(r);return e.cancellationController=c||null,o.then((t=>{if(a.statusCode=t.status,200!==t.status&&204!==t.status){const e=Ms.decoder.decode(t.body),s=t.headers["content-type"];if(s||-1!==s.indexOf("javascript")||-1!==s.indexOf("json")){const t=JSON.parse(e);"object"==typeof t&&"error"in t&&t.error&&"object"==typeof t.error&&(a.errorData=t.error)}else a.responseText=e}return e.parse(t)})).then((e=>t?t(a,e):e)).catch((e=>{const s=e instanceof I?e:I.create(e);if(t)return s.category!==h.PNCancelledCategory&&this.logger.error("PubNub",(()=>({messageType:"error",message:s.toPubNubError(i,"REST API request processing error, check status for details")}))),t(s.toStatus(i),null);const n=s.toPubNubError(i,"REST API request processing error, check status for details");throw s.category!==h.PNCancelledCategory&&this.logger.error("PubNub",(()=>({messageType:"error",message:n}))),n}))}))}destroy(e=!1){this.logger.info("PubNub","Destroying PubNub client."),this._globalSubscriptionSet&&(this._globalSubscriptionSet.invalidate(!0),this._globalSubscriptionSet=void 0),Object.values(this.eventHandleCapable).forEach((e=>e.invalidate(!0))),this.eventHandleCapable={},this.subscriptionManager?(this.subscriptionManager.unsubscribeAll(e),this.subscriptionManager.disconnect()):this.eventEngine&&this.eventEngine.unsubscribeAll(e),this.presenceEventEngine&&this.presenceEventEngine.leaveAll(e)}stop(){this.logger.warn("PubNub","'stop' is deprecated, please use 'destroy' instead."),this.destroy()}publish(e,t){return i(this,void 0,void 0,(function*(){{this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Publish with parameters:"})));const s=!1===e.replicate&&!1===e.storeInHistory,n=new wt(Object.assign(Object.assign({},e),{keySet:this._configuration.keySet,crypto:this._configuration.getCryptoModule()})),r=e=>{e&&this.logger.debug("PubNub",`${s?"Fire":"Publish"} success with timetoken: ${e.timetoken}`)};return t?this.sendRequest(n,((e,s)=>{r(s),t(e,s)})):this.sendRequest(n).then((e=>(r(e),e)))}}))}signal(e,t){return i(this,void 0,void 0,(function*(){{this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Signal with parameters:"})));const s=new Ot(Object.assign(Object.assign({},e),{keySet:this._configuration.keySet})),n=e=>{e&&this.logger.debug("PubNub",`Publish success with timetoken: ${e.timetoken}`)};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}}))}fire(e,t){return i(this,void 0,void 0,(function*(){return this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Fire with parameters:"}))),null!=t||(t=()=>{}),this.publish(Object.assign(Object.assign({},e),{replicate:!1,storeInHistory:!1}),t)}))}get globalSubscriptionSet(){return this._globalSubscriptionSet||(this._globalSubscriptionSet=this.subscriptionSet({})),this._globalSubscriptionSet}get subscriptionTimetoken(){return this.subscriptionManager?this.subscriptionManager.subscriptionTimetoken:this.eventEngine?this.eventEngine.subscriptionTimetoken:void 0}getSubscribedChannels(){return this.subscriptionManager?this.subscriptionManager.subscribedChannels:this.eventEngine?this.eventEngine.getSubscribedChannels():[]}getSubscribedChannelGroups(){return this.subscriptionManager?this.subscriptionManager.subscribedChannelGroups:this.eventEngine?this.eventEngine.getSubscribedChannelGroups():[]}registerEventHandleCapable(e,t,s){{let n;this.logger.trace("PubNub",(()=>({messageType:"object",message:Object.assign(Object.assign({subscription:e},t?{cursor:t}:[]),s?{subscriptions:s}:{}),details:"Register event handle capable:"}))),this.eventHandleCapable[e.state.id]||(this.eventHandleCapable[e.state.id]=e),s&&0!==s.length?(n=new jt({}),s.forEach((e=>n.add(e.subscriptionInput(!1))))):n=e.subscriptionInput(!1);const r={};r.channels=n.channels,r.channelGroups=n.channelGroups,t&&(r.timetoken=t.timetoken),this.subscriptionManager?this.subscriptionManager.subscribe(r):this.eventEngine&&this.eventEngine.subscribe(r)}}unregisterEventHandleCapable(e,t){{if(!this.eventHandleCapable[e.state.id])return;const s=[];this.logger.trace("PubNub",(()=>({messageType:"object",message:{subscription:e,subscriptions:t},details:"Unregister event handle capable:"})));let n,r=!t||0===t.length;if(!r&&e instanceof _t&&e.subscriptions.length===(null==t?void 0:t.length)&&(r=e.subscriptions.every((e=>t.includes(e)))),r&&delete this.eventHandleCapable[e.state.id],t&&0!==t.length?(n=new jt({}),t.forEach((e=>{const t=e.subscriptionInput(!0);t.isEmpty?s.push(e):n.add(t)}))):(n=e.subscriptionInput(!0),n.isEmpty&&s.push(e)),s.length>0&&this.logger.trace("PubNub",(()=>{const e=[];return s[0]instanceof _t?s[0].subscriptions.forEach((t=>e.push(t.state.entity))):s.forEach((t=>e.push(t.state.entity))),{messageType:"object",message:{entities:e},details:"Can't unregister event handle capable because entities still in use:"}})),n.isEmpty)return;{const e=[],t=[];if(Object.values(this.eventHandleCapable).forEach((s=>{const r=s.subscriptionInput(!1),i=r.channelGroups,a=r.channels;e.push(...n.channelGroups.filter((e=>i.includes(e)))),t.push(...n.channels.filter((e=>a.includes(e))))})),(t.length>0||e.length>0)&&(this.logger.trace("PubNub",(()=>{const s=[],r=n=>{const r=n.subscriptionNames(!0),i=n.subscriptionType===Pt.Channel?t:e;r.some((e=>i.includes(e)))&&s.push(n)};Object.values(this.eventHandleCapable).forEach((e=>{e instanceof _t?e.subscriptions.forEach((e=>{r(e.state.entity)})):e instanceof Mt&&r(e.state.entity)}));let i="Some entities still in use:";return t.length+e.length===n.length&&(i="Can't unregister event handle capable because entities still in use:"),{messageType:"object",message:{entities:s},details:i}})),n.remove(new jt({channels:t,channelGroups:e})),n.isEmpty))return}const i={};i.channels=n.channels,i.channelGroups=n.channelGroups,this.subscriptionManager?this.subscriptionManager.unsubscribe(i):this.eventEngine&&this.eventEngine.unsubscribe(i)}}subscribe(e){{this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Subscribe with parameters:"})));const t=this.subscriptionSet(Object.assign(Object.assign({},e),{subscriptionOptions:{receivePresenceEvents:e.withPresence}}));this.globalSubscriptionSet.addSubscriptionSet(t),t.dispose();const s="number"==typeof e.timetoken?`${e.timetoken}`:e.timetoken;this.globalSubscriptionSet.subscribe({timetoken:s})}}makeSubscribe(e,t){{this._configuration.isSharedWorkerEnabled()||(e.onDemand=!1);const s=new pe(Object.assign(Object.assign({},e),{keySet:this._configuration.keySet,crypto:this._configuration.getCryptoModule(),getFileUrl:this.getFileUrl.bind(this)}));if(this.sendRequest(s,((e,n)=>{var r;this.subscriptionManager&&(null===(r=this.subscriptionManager.abort)||void 0===r?void 0:r.identifier)===s.requestIdentifier&&(this.subscriptionManager.abort=null),t(e,n)})),this.subscriptionManager){const e=()=>s.abort("Cancel long-poll subscribe request");e.identifier=s.requestIdentifier,this.subscriptionManager.abort=e}}}unsubscribe(e){{if(this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Unsubscribe with parameters:"}))),!this._globalSubscriptionSet)return void this.logger.debug("PubNub","There are no active subscriptions. Ignore.");const t=this.globalSubscriptionSet.subscriptions.filter((t=>{var s,n;const r=t.subscriptionInput(!1);if(r.isEmpty)return!1;for(const t of null!==(s=e.channels)&&void 0!==s?s:[])if(r.contains(t))return!0;for(const t of null!==(n=e.channelGroups)&&void 0!==n?n:[])if(r.contains(t))return!0}));t.length>0&&this.globalSubscriptionSet.removeSubscriptions(t)}}makeUnsubscribe(e,t){{let{channels:s,channelGroups:n}=e;if(this._configuration.getKeepPresenceChannelsInPresenceRequests()||(n&&(n=n.filter((e=>!e.endsWith("-pnpres")))),s&&(s=s.filter((e=>!e.endsWith("-pnpres"))))),0===(null!=n?n:[]).length&&0===(null!=s?s:[]).length)return t({error:!1,operation:M.PNUnsubscribeOperation,category:h.PNAcknowledgmentCategory,statusCode:200});this.sendRequest(new Ft({channels:s,channelGroups:n,keySet:this._configuration.keySet}),t)}}unsubscribeAll(){this.logger.debug("PubNub","Unsubscribe all channels and groups"),this._globalSubscriptionSet&&this._globalSubscriptionSet.invalidate(!1),Object.values(this.eventHandleCapable).forEach((e=>e.invalidate(!1))),this.eventHandleCapable={},this.subscriptionManager?this.subscriptionManager.unsubscribeAll():this.eventEngine&&this.eventEngine.unsubscribeAll()}disconnect(e=!1){this.logger.debug("PubNub",`Disconnect (while offline? ${e?"yes":"no"})`),this.subscriptionManager?this.subscriptionManager.disconnect():this.eventEngine&&this.eventEngine.disconnect(e)}reconnect(e){this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Reconnect with parameters:"}))),this.subscriptionManager?this.subscriptionManager.reconnect():this.eventEngine&&this.eventEngine.reconnect(null!=e?e:{})}subscribeHandshake(e){return i(this,void 0,void 0,(function*(){{this._configuration.isSharedWorkerEnabled()||(e.onDemand=!1);const t=new Ct(Object.assign(Object.assign({},e),{keySet:this._configuration.keySet,crypto:this._configuration.getCryptoModule(),getFileUrl:this.getFileUrl.bind(this)})),s=e.abortSignal.subscribe((e=>{t.abort("Cancel subscribe handshake request")}));return this.sendRequest(t).then((e=>(s(),e.cursor)))}}))}subscribeReceiveMessages(e){return i(this,void 0,void 0,(function*(){{this._configuration.isSharedWorkerEnabled()||(e.onDemand=!1);const t=new kt(Object.assign(Object.assign({},e),{keySet:this._configuration.keySet,crypto:this._configuration.getCryptoModule(),getFileUrl:this.getFileUrl.bind(this)})),s=e.abortSignal.subscribe((e=>{t.abort("Cancel long-poll subscribe request")}));return this.sendRequest(t).then((e=>(s(),e)))}}))}getMessageActions(e,t){return i(this,void 0,void 0,(function*(){{this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Get message actions with parameters:"})));const s=new Ht(Object.assign(Object.assign({},e),{keySet:this._configuration.keySet})),n=e=>{e&&this.logger.debug("PubNub",`Get message actions success. Received ${e.data.length} message actions.`)};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}}))}addMessageAction(e,t){return i(this,void 0,void 0,(function*(){{this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Add message action with parameters:"})));const s=new Bt(Object.assign(Object.assign({},e),{keySet:this._configuration.keySet})),n=e=>{e&&this.logger.debug("PubNub",`Message action add success. Message action added with timetoken: ${e.data.actionTimetoken}`)};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}}))}removeMessageAction(e,t){return i(this,void 0,void 0,(function*(){{this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Remove message action with parameters:"})));const s=new Wt(Object.assign(Object.assign({},e),{keySet:this._configuration.keySet})),n=t=>{t&&this.logger.debug("PubNub",`Message action remove success. Removed message action with ${e.actionTimetoken} timetoken.`)};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}}))}fetchMessages(e,t){return i(this,void 0,void 0,(function*(){{this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Fetch messages with parameters:"})));const s=new Kt(Object.assign(Object.assign({},e),{keySet:this._configuration.keySet,crypto:this._configuration.getCryptoModule(),getFileUrl:this.getFileUrl.bind(this)})),n=e=>{if(!e)return;const t=Object.values(e.channels).reduce(((e,t)=>e+t.length),0);this.logger.debug("PubNub",`Fetch messages success. Received ${t} messages.`)};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}}))}deleteMessages(e,t){return i(this,void 0,void 0,(function*(){{this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Delete messages with parameters:"})));const s=new xt(Object.assign(Object.assign({},e),{keySet:this._configuration.keySet})),n=e=>{e&&this.logger.debug("PubNub","Delete messages success.")};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}}))}messageCounts(e,t){return i(this,void 0,void 0,(function*(){{this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Get messages count with parameters:"})));const s=new Lt(Object.assign(Object.assign({},e),{keySet:this._configuration.keySet})),n=t=>{if(!t)return;const s=Object.values(t.channels).reduce(((e,t)=>e+t),0);this.logger.debug("PubNub",`Get messages count success. There are ${s} messages since provided reference timetoken${e.channelTimetokens?e.channelTimetokens.join(","):""}.`)};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}}))}history(e,t){return i(this,void 0,void 0,(function*(){{this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Fetch history with parameters:"})));const s=new qt(Object.assign(Object.assign({},e),{keySet:this._configuration.keySet,crypto:this._configuration.getCryptoModule()})),n=e=>{e&&this.logger.debug("PubNub",`Fetch history success. Received ${e.messages.length} messages.`)};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}}))}hereNow(e,t){return i(this,void 0,void 0,(function*(){{this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Here now with parameters:"})));const s=new $t(Object.assign(Object.assign({},e),{keySet:this._configuration.keySet})),n=e=>{e&&this.logger.debug("PubNub",`Here now success. There are ${e.totalOccupancy} participants in ${e.totalChannels} channels.`)};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}}))}whereNow(e,t){return i(this,void 0,void 0,(function*(){var s;{this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Where now with parameters:"})));const n=new Rt({uuid:null!==(s=e.uuid)&&void 0!==s?s:this._configuration.userId,keySet:this._configuration.keySet}),r=e=>{e&&this.logger.debug("PubNub",`Where now success. Currently present in ${e.channels.length} channels.`)};return t?this.sendRequest(n,((e,s)=>{r(s),t(e,s)})):this.sendRequest(n).then((e=>(r(e),e)))}}))}getState(e,t){return i(this,void 0,void 0,(function*(){var s;{this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Get presence state with parameters:"})));const n=new At(Object.assign(Object.assign({},e),{uuid:null!==(s=e.uuid)&&void 0!==s?s:this._configuration.userId,keySet:this._configuration.keySet})),r=e=>{e&&this.logger.debug("PubNub",`Get presence state success. Received presence state for ${Object.keys(e.channels).length} channels.`)};return t?this.sendRequest(n,((e,s)=>{r(s),t(e,s)})):this.sendRequest(n).then((e=>(r(e),e)))}}))}setState(e,t){return i(this,void 0,void 0,(function*(){var s,n;{this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Set presence state with parameters:"})));const{keySet:r,userId:i}=this._configuration,a=this._configuration.getPresenceTimeout();let o;if(this._configuration.enableEventEngine&&this.presenceState){const t=this.presenceState;null===(s=e.channels)||void 0===s||s.forEach((s=>t[s]=e.state)),"channelGroups"in e&&(null===(n=e.channelGroups)||void 0===n||n.forEach((s=>t[s]=e.state))),this.onPresenceStateChange&&this.onPresenceStateChange(this.presenceState)}o="withHeartbeat"in e&&e.withHeartbeat?new Dt(Object.assign(Object.assign({},e),{keySet:r,heartbeat:a})):new Ut(Object.assign(Object.assign({},e),{keySet:r,uuid:i}));const c=e=>{e&&this.logger.debug("PubNub","Set presence state success."+(o instanceof Dt?" Presence state has been set using heartbeat endpoint.":""))};return this.subscriptionManager&&(this.subscriptionManager.setState(e),this.onPresenceStateChange&&this.onPresenceStateChange(this.subscriptionManager.presenceState)),t?this.sendRequest(o,((e,s)=>{c(s),t(e,s)})):this.sendRequest(o).then((e=>(c(e),e)))}}))}presence(e){var t;this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Change presence with parameters:"}))),null===(t=this.subscriptionManager)||void 0===t||t.changePresence(e)}heartbeat(e,t){return i(this,void 0,void 0,(function*(){var s;{this.logger.trace("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Heartbeat with parameters:"})));let{channels:n,channelGroups:r}=e;if(r&&(r=r.filter((e=>!e.endsWith("-pnpres")))),n&&(n=n.filter((e=>!e.endsWith("-pnpres")))),0===(null!=r?r:[]).length&&0===(null!=n?n:[]).length){const e={error:!1,operation:M.PNHeartbeatOperation,category:h.PNAcknowledgmentCategory,statusCode:200};return this.logger.trace("PubNub","There are no active subscriptions. Ignore."),t?t(e,{}):Promise.resolve(e)}const i=new Dt(Object.assign(Object.assign({},e),{channels:[...new Set(n)],channelGroups:[...new Set(r)],keySet:this._configuration.keySet})),a=e=>{e&&this.logger.trace("PubNub","Heartbeat success.")},o=null===(s=e.abortSignal)||void 0===s?void 0:s.subscribe((e=>{i.abort("Cancel long-poll subscribe request")}));return t?this.sendRequest(i,((e,s)=>{a(s),o&&o(),t(e,s)})):this.sendRequest(i).then((e=>(a(e),o&&o(),e)))}}))}join(e){var t,s;this.logger.trace("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Join with parameters:"}))),e&&0===(null!==(t=e.channels)&&void 0!==t?t:[]).length&&0===(null!==(s=e.groups)&&void 0!==s?s:[]).length?this.logger.trace("PubNub","Ignoring 'join' announcement request."):this.presenceEventEngine?this.presenceEventEngine.join(e):this.heartbeat(Object.assign(Object.assign({channels:e.channels,channelGroups:e.groups},this._configuration.maintainPresenceState&&this.presenceState&&Object.keys(this.presenceState).length>0&&{state:this.presenceState}),{heartbeat:this._configuration.getPresenceTimeout()}),(()=>{}))}presenceReconnect(e){this.logger.trace("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Presence reconnect with parameters:"}))),this.presenceEventEngine?this.presenceEventEngine.reconnect():this.heartbeat(Object.assign(Object.assign({channels:e.channels,channelGroups:e.groups},this._configuration.maintainPresenceState&&{state:this.presenceState}),{heartbeat:this._configuration.getPresenceTimeout()}),(()=>{}))}leave(e){var t,s,n;this.logger.trace("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Leave with parameters:"}))),e&&0===(null!==(t=e.channels)&&void 0!==t?t:[]).length&&0===(null!==(s=e.groups)&&void 0!==s?s:[]).length?this.logger.trace("PubNub","Ignoring 'leave' announcement request."):this.presenceEventEngine?null===(n=this.presenceEventEngine)||void 0===n||n.leave(e):this.makeUnsubscribe({channels:e.channels,channelGroups:e.groups},(()=>{}))}leaveAll(e={}){this.logger.trace("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Leave all with parameters:"}))),this.presenceEventEngine?this.presenceEventEngine.leaveAll(!!e.isOffline):e.isOffline||this.makeUnsubscribe({channels:e.channels,channelGroups:e.groups},(()=>{}))}presenceDisconnect(e){this.logger.trace("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Presence disconnect parameters:"}))),this.presenceEventEngine?this.presenceEventEngine.disconnect(!!e.isOffline):e.isOffline||this.makeUnsubscribe({channels:e.channels,channelGroups:e.groups},(()=>{}))}grantToken(e,t){return i(this,void 0,void 0,(function*(){throw new Error("Grant Token error: PAM module disabled")}))}revokeToken(e,t){return i(this,void 0,void 0,(function*(){throw new Error("Revoke Token error: PAM module disabled")}))}get token(){return this.tokenManager&&this.tokenManager.getToken()}getToken(){return this.token}set token(e){this.tokenManager&&this.tokenManager.setToken(e),this.onAuthenticationChange&&this.onAuthenticationChange(e)}setToken(e){this.token=e}parseToken(e){return this.tokenManager&&this.tokenManager.parseToken(e)}grant(e,t){return i(this,void 0,void 0,(function*(){throw new Error("Grant error: PAM module disabled")}))}audit(e,t){return i(this,void 0,void 0,(function*(){throw new Error("Grant Permissions error: PAM module disabled")}))}get objects(){return this._objects}fetchUsers(e,t){return i(this,void 0,void 0,(function*(){return this.logger.warn("PubNub","'fetchUsers' is deprecated. Use 'pubnub.objects.getAllUUIDMetadata' instead."),this.logger.debug("PubNub",(()=>({messageType:"object",message:e&&"function"!=typeof e?e:{},details:"Fetch all User objects with parameters:"}))),this.objects._getAllUUIDMetadata(e,t)}))}fetchUser(e,t){return i(this,void 0,void 0,(function*(){return this.logger.warn("PubNub","'fetchUser' is deprecated. Use 'pubnub.objects.getUUIDMetadata' instead."),this.logger.debug("PubNub",(()=>({messageType:"object",message:e&&"function"!=typeof e?e:{uuid:this.userId},details:`Fetch${e&&"function"!=typeof e?"":" current"} User object with parameters:`}))),this.objects._getUUIDMetadata(e,t)}))}createUser(e,t){return i(this,void 0,void 0,(function*(){return this.logger.warn("PubNub","'createUser' is deprecated. Use 'pubnub.objects.setUUIDMetadata' instead."),this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Create User object with parameters:"}))),this.objects._setUUIDMetadata(e,t)}))}updateUser(e,t){return i(this,void 0,void 0,(function*(){return this.logger.warn("PubNub","'updateUser' is deprecated. Use 'pubnub.objects.setUUIDMetadata' instead."),this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Update User object with parameters:"}))),this.objects._setUUIDMetadata(e,t)}))}removeUser(e,t){return i(this,void 0,void 0,(function*(){return this.logger.warn("PubNub","'removeUser' is deprecated. Use 'pubnub.objects.removeUUIDMetadata' instead."),this.logger.debug("PubNub",(()=>({messageType:"object",message:e&&"function"!=typeof e?e:{uuid:this.userId},details:`Remove${e&&"function"!=typeof e?"":" current"} User object with parameters:`}))),this.objects._removeUUIDMetadata(e,t)}))}fetchSpaces(e,t){return i(this,void 0,void 0,(function*(){return this.logger.warn("PubNub","'fetchSpaces' is deprecated. Use 'pubnub.objects.getAllChannelMetadata' instead."),this.logger.debug("PubNub",(()=>({messageType:"object",message:e&&"function"!=typeof e?e:{},details:"Fetch all Space objects with parameters:"}))),this.objects._getAllChannelMetadata(e,t)}))}fetchSpace(e,t){return i(this,void 0,void 0,(function*(){return this.logger.warn("PubNub","'fetchSpace' is deprecated. Use 'pubnub.objects.getChannelMetadata' instead."),this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Fetch Space object with parameters:"}))),this.objects._getChannelMetadata(e,t)}))}createSpace(e,t){return i(this,void 0,void 0,(function*(){return this.logger.warn("PubNub","'createSpace' is deprecated. Use 'pubnub.objects.setChannelMetadata' instead."),this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Create Space object with parameters:"}))),this.objects._setChannelMetadata(e,t)}))}updateSpace(e,t){return i(this,void 0,void 0,(function*(){return this.logger.warn("PubNub","'updateSpace' is deprecated. Use 'pubnub.objects.setChannelMetadata' instead."),this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Update Space object with parameters:"}))),this.objects._setChannelMetadata(e,t)}))}removeSpace(e,t){return i(this,void 0,void 0,(function*(){return this.logger.warn("PubNub","'removeSpace' is deprecated. Use 'pubnub.objects.removeChannelMetadata' instead."),this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Remove Space object with parameters:"}))),this.objects._removeChannelMetadata(e,t)}))}fetchMemberships(e,t){return i(this,void 0,void 0,(function*(){return this.objects.fetchMemberships(e,t)}))}addMemberships(e,t){return i(this,void 0,void 0,(function*(){return this.objects.addMemberships(e,t)}))}updateMemberships(e,t){return i(this,void 0,void 0,(function*(){return this.logger.warn("PubNub","'addMemberships' is deprecated. Use 'pubnub.objects.setChannelMembers' or 'pubnub.objects.setMemberships' instead."),this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Update memberships with parameters:"}))),this.objects.addMemberships(e,t)}))}removeMemberships(e,t){return i(this,void 0,void 0,(function*(){var s,n,r;{if(this.logger.warn("PubNub","'removeMemberships' is deprecated. Use 'pubnub.objects.removeMemberships' or 'pubnub.objects.removeChannelMembers' instead."),this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Remove memberships with parameters:"}))),"spaceId"in e){const r=e,i={channel:null!==(s=r.spaceId)&&void 0!==s?s:r.channel,uuids:null!==(n=r.userIds)&&void 0!==n?n:r.uuids,limit:0};return t?this.objects.removeChannelMembers(i,t):this.objects.removeChannelMembers(i)}const i=e,a={uuid:i.userId,channels:null!==(r=i.spaceIds)&&void 0!==r?r:i.channels,limit:0};return t?this.objects.removeMemberships(a,t):this.objects.removeMemberships(a)}}))}get channelGroups(){return this._channelGroups}get push(){return this._push}sendFile(e,t){return i(this,void 0,void 0,(function*(){{if(!this._configuration.PubNubFile)throw new Error("Validation failed: 'PubNubFile' not configured or file upload not supported by the platform.");this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Send file with parameters:"})));const s=new Zt(Object.assign(Object.assign({},e),{keySet:this._configuration.keySet,PubNubFile:this._configuration.PubNubFile,fileUploadPublishRetryLimit:this._configuration.fileUploadPublishRetryLimit,file:e.file,sendRequest:this.sendRequest.bind(this),publishFile:this.publishFile.bind(this),crypto:this._configuration.getCryptoModule(),cryptography:this.cryptography?this.cryptography:void 0})),n={error:!1,operation:M.PNPublishFileOperation,category:h.PNAcknowledgmentCategory,statusCode:0},r=e=>{e&&this.logger.debug("PubNub",`Send file success. File shared with ${e.id} ID.`)};return s.process().then((e=>(n.statusCode=e.status,r(e),t?t(n,e):e))).catch((e=>{let s;throw e instanceof d?s=e.status:e instanceof I&&(s=e.toStatus(n.operation)),this.logger.error("PubNub",(()=>({messageType:"error",message:new d("File sending error. Check status for details",s)}))),t&&s&&t(s,null),new d("REST API request processing error. Check status for details",s)}))}}))}publishFile(e,t){return i(this,void 0,void 0,(function*(){{if(!this._configuration.PubNubFile)throw new Error("Validation failed: 'PubNubFile' not configured or file upload not supported by the platform.");this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Publish file message with parameters:"})));const s=new zt(Object.assign(Object.assign({},e),{keySet:this._configuration.keySet,crypto:this._configuration.getCryptoModule()})),n=e=>{e&&this.logger.debug("PubNub",`Publish file message success. File message published with timetoken: ${e.timetoken}`)};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}}))}listFiles(e,t){return i(this,void 0,void 0,(function*(){{this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"List files with parameters:"})));const s=new Xt(Object.assign(Object.assign({},e),{keySet:this._configuration.keySet})),n=e=>{e&&this.logger.debug("PubNub",`List files success. There are ${e.count} uploaded files.`)};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}}))}getFileUrl(e){var t;{const s=this.transport.request(new Vt(Object.assign(Object.assign({},e),{keySet:this._configuration.keySet})).request()),n=null!==(t=s.queryParameters)&&void 0!==t?t:{},r=Object.keys(n).map((e=>{const t=n[e];return Array.isArray(t)?t.map((t=>`${e}=${R(t)}`)).join("&"):`${e}=${R(t)}`})).join("&");return`${s.origin}${s.path}?${r}`}}downloadFile(e,t){return i(this,void 0,void 0,(function*(){{if(!this._configuration.PubNubFile)throw new Error("Validation failed: 'PubNubFile' not configured or file upload not supported by the platform.");this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Download file with parameters:"})));const s=new Is(Object.assign(Object.assign({},e),{keySet:this._configuration.keySet,PubNubFile:this._configuration.PubNubFile,cryptography:this.cryptography?this.cryptography:void 0,crypto:this._configuration.getCryptoModule()})),n=e=>{e&&this.logger.debug("PubNub","Download file success.")};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):yield this.sendRequest(s).then((e=>(n(e),e)))}}))}deleteFile(e,t){return i(this,void 0,void 0,(function*(){{this.logger.debug("PubNub",(()=>({messageType:"object",message:Object.assign({},e),details:"Delete file with parameters:"})));const s=new Jt(Object.assign(Object.assign({},e),{keySet:this._configuration.keySet})),n=t=>{t&&this.logger.debug("PubNub",`Delete file success. Deleted file with ${e.id} ID.`)};return t?this.sendRequest(s,((e,s)=>{n(s),t(e,s)})):this.sendRequest(s).then((e=>(n(e),e)))}}))}time(e){return i(this,void 0,void 0,(function*(){this.logger.debug("PubNub","Get service time.");const t=new _s,s=e=>{e&&this.logger.debug("PubNub",`Get service time success. Current timetoken: ${e.timetoken}`)};return e?this.sendRequest(t,((t,n)=>{s(n),e(t,n)})):this.sendRequest(t).then((e=>(s(e),e)))}))}emitStatus(e){var t;null===(t=this.eventDispatcher)||void 0===t||t.handleStatus(e)}emitEvent(e,t){var s;this._globalSubscriptionSet&&this._globalSubscriptionSet.handleEvent(e,t),null===(s=this.eventDispatcher)||void 0===s||s.handleEvent(t),Object.values(this.eventHandleCapable).forEach((s=>{s!==this._globalSubscriptionSet&&s.handleEvent(e,t)}))}set onStatus(e){this.eventDispatcher&&(this.eventDispatcher.onStatus=e)}set onMessage(e){this.eventDispatcher&&(this.eventDispatcher.onMessage=e)}set onPresence(e){this.eventDispatcher&&(this.eventDispatcher.onPresence=e)}set onSignal(e){this.eventDispatcher&&(this.eventDispatcher.onSignal=e)}set onObjects(e){this.eventDispatcher&&(this.eventDispatcher.onObjects=e)}set onMessageAction(e){this.eventDispatcher&&(this.eventDispatcher.onMessageAction=e)}set onFile(e){this.eventDispatcher&&(this.eventDispatcher.onFile=e)}addListener(e){this.eventDispatcher&&this.eventDispatcher.addListener(e)}removeListener(e){this.eventDispatcher&&this.eventDispatcher.removeListener(e)}removeAllListeners(){this.eventDispatcher&&this.eventDispatcher.removeAllListeners()}encrypt(e,t){this.logger.warn("PubNub","'encrypt' is deprecated. Use cryptoModule instead.");const s=this._configuration.getCryptoModule();if(!t&&s&&"string"==typeof e){const t=s.encrypt(e);return"string"==typeof t?t:u(t)}if(!this.crypto)throw new Error("Encryption error: cypher key not set");return this.crypto.encrypt(e,t)}decrypt(e,t){this.logger.warn("PubNub","'decrypt' is deprecated. Use cryptoModule instead.");const s=this._configuration.getCryptoModule();if(!t&&s){const t=s.decrypt(e);return t instanceof ArrayBuffer?JSON.parse((new TextDecoder).decode(t)):t}if(!this.crypto)throw new Error("Decryption error: cypher key not set");return this.crypto.decrypt(e,t)}encryptFile(e,t){return i(this,void 0,void 0,(function*(){var s;if("string"!=typeof e&&(t=e),!t)throw new Error("File encryption error. Source file is missing.");if(!this._configuration.PubNubFile)throw new Error("File encryption error. File constructor not configured.");if("string"!=typeof e&&!this._configuration.getCryptoModule())throw new Error("File encryption error. Crypto module not configured.");if("string"==typeof e){if(!this.cryptography)throw new Error("File encryption error. File encryption not available");return this.cryptography.encryptFile(e,t,this._configuration.PubNubFile)}return null===(s=this._configuration.getCryptoModule())||void 0===s?void 0:s.encryptFile(t,this._configuration.PubNubFile)}))}decryptFile(e,t){return i(this,void 0,void 0,(function*(){var s;if("string"!=typeof e&&(t=e),!t)throw new Error("File encryption error. Source file is missing.");if(!this._configuration.PubNubFile)throw new Error("File decryption error. File constructor not configured.");if("string"==typeof e&&!this._configuration.getCryptoModule())throw new Error("File decryption error. Crypto module not configured.");if("string"==typeof e){if(!this.cryptography)throw new Error("File decryption error. File decryption not available");return this.cryptography.decryptFile(e,t,this._configuration.PubNubFile)}return null===(s=this._configuration.getCryptoModule())||void 0===s?void 0:s.decryptFile(t,this._configuration.PubNubFile)}))}}Ms.decoder=new TextDecoder,Ms.OPERATIONS=M,Ms.CATEGORIES=h,Ms.Endpoint=z,Ms.ExponentialRetryPolicy=V.ExponentialRetryPolicy,Ms.LinearRetryPolicy=V.LinearRetryPolicy,Ms.NoneRetryPolicy=V.None,Ms.LogLevel=F;class As{constructor(e,t){this.decode=e,this.base64ToBinary=t}decodeToken(e){let t="";e.length%4==3?t="=":e.length%4==2&&(t="==");const s=e.replace(/-/gi,"+").replace(/_/gi,"/")+t,n=this.decode(this.base64ToBinary(s));return"object"==typeof n?n:void 0}}class Us extends Ms{constructor(e){var t;const s=void 0!==e.subscriptionWorkerUrl,r=D(e),i=Object.assign(Object.assign({},r),{sdkFamily:"Web"});i.PubNubFile=o;const a=se(i,(e=>{if(e.cipherKey){return new N({default:new E(Object.assign(Object.assign({},e),e.logger?{}:{logger:a.logger()})),cryptors:[new k({cipherKey:e.cipherKey})]})}}));let u,l;e.subscriptionWorkerLogVerbosity?e.subscriptionWorkerLogLevel=F.Debug:void 0===e.subscriptionWorkerLogLevel&&(e.subscriptionWorkerLogLevel=F.None),void 0!==e.subscriptionWorkerLogVerbosity&&a.logger().warn("Configuration","'subscriptionWorkerLogVerbosity' is deprecated. Use 'subscriptionWorkerLogLevel' instead."),a.getCryptoModule()&&(a.getCryptoModule().logger=a.logger()),u=new ie(new As((e=>U(n.decode(e))),c)),(a.getCipherKey()||a.secretKey)&&(l=new P({secretKey:a.secretKey,cipherKey:a.getCipherKey(),useRandomIVs:a.getUseRandomIVs(),customEncrypt:a.getCustomEncrypt(),customDecrypt:a.getCustomDecrypt(),logger:a.logger()}));let h,d=()=>{},p=()=>{},g=()=>{},b=()=>{};h=new j;let y=new ue(a.logger(),i.transport);if(r.subscriptionWorkerUrl)try{const e=new A({clientIdentifier:a._instanceId,subscriptionKey:a.subscribeKey,userId:a.getUserId(),workerUrl:r.subscriptionWorkerUrl,sdkVersion:a.getVersion(),heartbeatInterval:a.getHeartbeatInterval(),announceSuccessfulHeartbeats:a.announceSuccessfulHeartbeats,announceFailedHeartbeats:a.announceFailedHeartbeats,workerOfflineClientsCheckInterval:i.subscriptionWorkerOfflineClientsCheckInterval,workerUnsubscribeOfflineClients:i.subscriptionWorkerUnsubscribeOfflineClients,workerLogLevel:i.subscriptionWorkerLogLevel,tokenManager:u,transport:y,logger:a.logger()});p=t=>e.onPresenceStateChange(t),d=t=>e.onHeartbeatIntervalChange(t),g=t=>e.onTokenChange(t),b=t=>e.onUserIdChange(t),y=e,r.subscriptionWorkerUnsubscribeOfflineClients&&window.addEventListener("pagehide",(t=>{t.persisted||e.terminate()}),{once:!0})}catch(e){a.logger().error("PubNub",(()=>({messageType:"error",message:e})))}else s&&a.logger().warn("PubNub","SharedWorker not supported in this browser. Fallback to the original transport.");const m=new ce({clientConfiguration:a,tokenManager:u,transport:y});if(super({configuration:a,transport:m,cryptography:h,tokenManager:u,crypto:l}),this.onHeartbeatIntervalChange=d,this.onAuthenticationChange=g,this.onPresenceStateChange=p,this.onUserIdChange=b,y instanceof A){y.emitStatus=this.emitStatus.bind(this);const e=this.disconnect.bind(this);this.disconnect=t=>{y.disconnect(),e()}}(null===(t=e.listenToBrowserNetworkEvents)||void 0===t||t)&&(window.addEventListener("offline",(()=>{this.networkDownDetected()})),window.addEventListener("online",(()=>{this.networkUpDetected()})))}networkDownDetected(){this.logger.debug("PubNub","Network down detected"),this.emitStatus({category:Us.CATEGORIES.PNNetworkDownCategory}),this._configuration.restore?this.disconnect(!0):this.destroy(!0)}networkUpDetected(){this.logger.debug("PubNub","Network up detected"),this.emitStatus({category:Us.CATEGORIES.PNNetworkUpCategory}),this.reconnect()}}return Us.CryptoModule=N,Us})); diff --git a/lib/core/endpoints/presence/get_state.js b/lib/core/endpoints/presence/get_state.js index a38d4a65a..1135f35f5 100644 --- a/lib/core/endpoints/presence/get_state.js +++ b/lib/core/endpoints/presence/get_state.js @@ -59,7 +59,7 @@ class GetPresenceStateRequest extends request_1.AbstractRequest { } get path() { const { keySet: { subscribeKey }, uuid, channels, } = this.parameters; - return `/v2/presence/sub-key/${subscribeKey}/channel/${(0, utils_1.encodeNames)(channels !== null && channels !== void 0 ? channels : [], ',')}/uuid/${uuid}`; + return `/v2/presence/sub-key/${subscribeKey}/channel/${(0, utils_1.encodeNames)(channels !== null && channels !== void 0 ? channels : [], ',')}/uuid/${(0, utils_1.encodeString)(uuid !== null && uuid !== void 0 ? uuid : '')}`; } get queryParameters() { const { channelGroups } = this.parameters; diff --git a/lib/core/endpoints/presence/heartbeat.js b/lib/core/endpoints/presence/heartbeat.js index 604acd76e..9d8669e3e 100644 --- a/lib/core/endpoints/presence/heartbeat.js +++ b/lib/core/endpoints/presence/heartbeat.js @@ -59,7 +59,7 @@ class HeartbeatRequest extends request_1.AbstractRequest { const query = { heartbeat: `${heartbeat}` }; if (channelGroups && channelGroups.length !== 0) query['channel-group'] = channelGroups.join(','); - if (state) + if (state !== undefined) query.state = JSON.stringify(state); return query; } diff --git a/lib/core/endpoints/presence/set_state.js b/lib/core/endpoints/presence/set_state.js index ade901f85..6f5b7c143 100644 --- a/lib/core/endpoints/presence/set_state.js +++ b/lib/core/endpoints/presence/set_state.js @@ -39,7 +39,7 @@ class SetPresenceStateRequest extends request_1.AbstractRequest { const { keySet: { subscribeKey }, state, channels = [], channelGroups = [], } = this.parameters; if (!subscribeKey) return 'Missing Subscribe Key'; - if (!state) + if (state === undefined) return 'Missing State'; if ((channels === null || channels === void 0 ? void 0 : channels.length) === 0 && (channelGroups === null || channelGroups === void 0 ? void 0 : channelGroups.length) === 0) return 'Please provide a list of channels and/or channel-groups'; diff --git a/package-lock.json b/package-lock.json index 00a8b4e2e..9bea6299c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "pubnub", - "version": "9.8.1", + "version": "9.10.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "pubnub", - "version": "9.8.1", + "version": "9.10.0", "license": "SEE LICENSE IN LICENSE", "dependencies": { "agentkeepalive": "^3.5.2", diff --git a/src/core/endpoints/presence/get_state.ts b/src/core/endpoints/presence/get_state.ts index ab7905ad3..b2a0bbb1d 100644 --- a/src/core/endpoints/presence/get_state.ts +++ b/src/core/endpoints/presence/get_state.ts @@ -9,7 +9,7 @@ import { AbstractRequest } from '../../components/request'; import RequestOperation from '../../constants/operations'; import { KeySet, Payload, Query } from '../../types/api'; import * as Presence from '../../types/api/presence'; -import { encodeNames } from '../../utils'; +import { encodeNames, encodeString } from '../../utils'; // -------------------------------------------------------- // ------------------------ Types ------------------------- @@ -103,7 +103,10 @@ export class GetPresenceStateRequest extends AbstractRequest = { heartbeat: `${heartbeat}` }; if (channelGroups && channelGroups.length !== 0) query['channel-group'] = channelGroups.join(','); - if (state) query.state = JSON.stringify(state); + if (state !== undefined) query.state = JSON.stringify(state); return query; } diff --git a/src/core/endpoints/presence/set_state.ts b/src/core/endpoints/presence/set_state.ts index 2bd2aec08..67ff4abb5 100644 --- a/src/core/endpoints/presence/set_state.ts +++ b/src/core/endpoints/presence/set_state.ts @@ -80,7 +80,7 @@ export class SetPresenceStateRequest extends AbstractRequest { diff --git a/test/integration/endpoints/channel_groups.test.ts b/test/integration/endpoints/channel_groups.test.ts index 04e3464ac..ce8a1265d 100644 --- a/test/integration/endpoints/channel_groups.test.ts +++ b/test/integration/endpoints/channel_groups.test.ts @@ -163,4 +163,440 @@ describe('channel group endpoints', () => { }); }); }); + + describe('edge cases and Promise-based execution', () => { + describe('addChannels - Promise-based API', () => { + it('should resolve with correct response', async () => { + const scope = utils + .createNock() + .get('/v1/channel-registration/sub-key/mySubKey/channel-group/test-group') + .query({ + add: 'ch1,ch2', + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + }) + .reply(200, '{"status": 200, "message": "OK", "payload": {}, "service": "ChannelGroups"}', { + 'content-type': 'text/javascript', + }); + + const result = await pubnub.channelGroups.addChannels({ + channelGroup: 'test-group', + channels: ['ch1', 'ch2'] + }); + + assert.deepEqual(result, {}); + assert.equal(scope.isDone(), true); + }); + + it('should reject on HTTP errors', async () => { + const scope = utils + .createNock() + .get('/v1/channel-registration/sub-key/mySubKey/channel-group/test-group') + .query({ + add: 'ch1,ch2', + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + }) + .reply(403, '{"status": 403, "message": "Forbidden", "error": true, "service": "ChannelGroups"}', { + 'content-type': 'text/javascript', + }); + + try { + await pubnub.channelGroups.addChannels({ + channelGroup: 'test-group', + channels: ['ch1', 'ch2'] + }); + assert.fail('Should have thrown error'); + } catch (error) { + assert(error); + assert.equal(scope.isDone(), true); + } + }); + + it('should handle large channel list', async () => { + const largeChannelList = Array.from({ length: 100 }, (_, i) => `channel${i + 1}`); + const scope = utils + .createNock() + .get('/v1/channel-registration/sub-key/mySubKey/channel-group/large-group') + .query({ + add: largeChannelList.join(','), + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + }) + .reply(200, '{"status": 200, "message": "OK", "payload": {}, "service": "ChannelGroups"}', { + 'content-type': 'text/javascript', + }); + + const result = await pubnub.channelGroups.addChannels({ + channelGroup: 'large-group', + channels: largeChannelList + }); + + assert.deepEqual(result, {}); + assert.equal(scope.isDone(), true); + }); + + it('should handle special characters in names', async () => { + const scope = utils + .createNock() + .get('/v1/channel-registration/sub-key/mySubKey/channel-group/group%20with%20spaces') + .query({ + add: 'channel with spaces,channel/with/slashes', + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + }) + .reply(200, '{"status": 200, "message": "OK", "payload": {}, "service": "ChannelGroups"}', { + 'content-type': 'text/javascript', + }); + + const result = await pubnub.channelGroups.addChannels({ + channelGroup: 'group with spaces', + channels: ['channel with spaces', 'channel/with/slashes'] + }); + + assert.deepEqual(result, {}); + assert.equal(scope.isDone(), true); + }); + + it('should handle validation errors', async () => { + try { + await pubnub.channelGroups.addChannels({ + channelGroup: '', + channels: ['ch1'] + }); + assert.fail('Should have thrown validation error'); + } catch (error) { + // Just verify that an error was thrown for invalid parameters + assert(error); + assert(typeof error.message === 'string' && error.message.length > 0); + } + }); + }); + + describe('removeChannels - Promise-based API', () => { + it('should resolve with correct response', async () => { + const scope = utils + .createNock() + .get('/v1/channel-registration/sub-key/mySubKey/channel-group/test-group') + .query({ + remove: 'ch1,ch2', + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + }) + .reply(200, '{"status": 200, "message": "OK", "payload": {}, "service": "ChannelGroups"}', { + 'content-type': 'text/javascript', + }); + + const result = await pubnub.channelGroups.removeChannels({ + channelGroup: 'test-group', + channels: ['ch1', 'ch2'] + }); + + assert.deepEqual(result, {}); + assert.equal(scope.isDone(), true); + }); + + it('should handle removing from empty group', async () => { + const scope = utils + .createNock() + .get('/v1/channel-registration/sub-key/mySubKey/channel-group/empty-group') + .query({ + remove: 'ch1,ch2', + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + }) + .reply(200, '{"status": 200, "message": "OK", "payload": {}, "service": "ChannelGroups"}', { + 'content-type': 'text/javascript', + }); + + const result = await pubnub.channelGroups.removeChannels({ + channelGroup: 'empty-group', + channels: ['ch1', 'ch2'] + }); + + assert.deepEqual(result, {}); + assert.equal(scope.isDone(), true); + }); + + it('should handle removing non-existent channels', async () => { + const scope = utils + .createNock() + .get('/v1/channel-registration/sub-key/mySubKey/channel-group/test-group') + .query({ + remove: 'non-existent1,non-existent2', + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + }) + .reply(200, '{"status": 200, "message": "OK", "payload": {}, "service": "ChannelGroups"}', { + 'content-type': 'text/javascript', + }); + + const result = await pubnub.channelGroups.removeChannels({ + channelGroup: 'test-group', + channels: ['non-existent1', 'non-existent2'] + }); + + assert.deepEqual(result, {}); + assert.equal(scope.isDone(), true); + }); + }); + + describe('listChannels - Promise-based API', () => { + it('should resolve with channels array', async () => { + const scope = utils + .createNock() + .get('/v1/channel-registration/sub-key/mySubKey/channel-group/test-group') + .query({ + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + }) + .reply( + 200, + '{"status": 200, "message": "OK", "payload": {"channels": ["channel1", "channel2"]}, "service": "ChannelGroups"}', + { + 'content-type': 'text/javascript', + } + ); + + const result = await pubnub.channelGroups.listChannels({ channelGroup: 'test-group' }); + assert.deepEqual(result.channels, ['channel1', 'channel2']); + assert.equal(scope.isDone(), true); + }); + + it('should handle empty channel group', async () => { + const scope = utils + .createNock() + .get('/v1/channel-registration/sub-key/mySubKey/channel-group/empty-group') + .query({ + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + }) + .reply( + 200, + '{"status": 200, "message": "OK", "payload": {"channels": []}, "service": "ChannelGroups"}', + { + 'content-type': 'text/javascript', + } + ); + + const result = await pubnub.channelGroups.listChannels({ channelGroup: 'empty-group' }); + assert.deepEqual(result.channels, []); + assert.equal(scope.isDone(), true); + }); + + it('should handle large channel list', async () => { + const largeChannelList = Array.from({ length: 100 }, (_, i) => `channel${i + 1}`); + const scope = utils + .createNock() + .get('/v1/channel-registration/sub-key/mySubKey/channel-group/large-group') + .query({ + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + }) + .reply( + 200, + `{"status": 200, "message": "OK", "payload": {"channels": ${JSON.stringify(largeChannelList)}}, "service": "ChannelGroups"}`, + { + 'content-type': 'text/javascript', + } + ); + + const result = await pubnub.channelGroups.listChannels({ channelGroup: 'large-group' }); + assert.deepEqual(result.channels, largeChannelList); + assert.equal(scope.isDone(), true); + }); + }); + + describe('listGroups - Promise-based API', () => { + it('should resolve with groups array', async () => { + const scope = utils + .createNock() + .get('/v1/channel-registration/sub-key/mySubKey/channel-group') + .query({ + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + }) + .reply( + 200, + '{"status": 200, "message": "OK", "payload": {"groups": ["group1", "group2"]}, "service": "ChannelGroups"}', + { + 'content-type': 'text/javascript', + } + ); + + const result = await pubnub.channelGroups.listGroups(); + assert.deepEqual(result.groups, ['group1', 'group2']); + assert.equal(scope.isDone(), true); + }); + + it('should handle empty account', async () => { + const scope = utils + .createNock() + .get('/v1/channel-registration/sub-key/mySubKey/channel-group') + .query({ + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + }) + .reply( + 200, + '{"status": 200, "message": "OK", "payload": {"groups": []}, "service": "ChannelGroups"}', + { + 'content-type': 'text/javascript', + } + ); + + const result = await pubnub.channelGroups.listGroups(); + assert.deepEqual(result.groups, []); + assert.equal(scope.isDone(), true); + }); + }); + + describe('deleteGroup - Promise-based API', () => { + it('should resolve with correct response', async () => { + const scope = utils + .createNock() + .get('/v1/channel-registration/sub-key/mySubKey/channel-group/test-group/remove') + .query({ + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + }) + .reply(200, '{"status": 200, "message": "OK", "payload": {}, "service": "ChannelGroups"}', { + 'content-type': 'text/javascript', + }); + + const result = await pubnub.channelGroups.deleteGroup({ channelGroup: 'test-group' }); + assert.deepEqual(result, {}); + assert.equal(scope.isDone(), true); + }); + + it('should handle deleting group with channels', async () => { + const scope = utils + .createNock() + .get('/v1/channel-registration/sub-key/mySubKey/channel-group/group-with-channels/remove') + .query({ + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + }) + .reply(200, '{"status": 200, "message": "OK", "payload": {}, "service": "ChannelGroups"}', { + 'content-type': 'text/javascript', + }); + + const result = await pubnub.channelGroups.deleteGroup({ channelGroup: 'group-with-channels' }); + assert.deepEqual(result, {}); + assert.equal(scope.isDone(), true); + }); + + it('should handle deleting non-existent group', async () => { + const scope = utils + .createNock() + .get('/v1/channel-registration/sub-key/mySubKey/channel-group/non-existent-group/remove') + .query({ + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + }) + .reply(404, '{"status": 404, "message": "Not Found", "error": true, "service": "ChannelGroups"}', { + 'content-type': 'text/javascript', + }); + + try { + await pubnub.channelGroups.deleteGroup({ channelGroup: 'non-existent-group' }); + assert.fail('Should have thrown error'); + } catch (error) { + assert(error); + assert.equal(scope.isDone(), true); + } + }); + }); + + describe('Concurrent operations', () => { + it('should handle multiple addChannels calls independently', async () => { + const scope1 = utils + .createNock() + .get('/v1/channel-registration/sub-key/mySubKey/channel-group/group1') + .query({ + add: 'ch1,ch2', + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + }) + .reply(200, '{"status": 200, "message": "OK", "payload": {}, "service": "ChannelGroups"}', { + 'content-type': 'text/javascript', + }); + + const scope2 = utils + .createNock() + .get('/v1/channel-registration/sub-key/mySubKey/channel-group/group2') + .query({ + add: 'ch3,ch4', + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + }) + .reply(200, '{"status": 200, "message": "OK", "payload": {}, "service": "ChannelGroups"}', { + 'content-type': 'text/javascript', + }); + + const [result1, result2] = await Promise.all([ + pubnub.channelGroups.addChannels({ channelGroup: 'group1', channels: ['ch1', 'ch2'] }), + pubnub.channelGroups.addChannels({ channelGroup: 'group2', channels: ['ch3', 'ch4'] }) + ]); + + assert.deepEqual(result1, {}); + assert.deepEqual(result2, {}); + assert.equal(scope1.isDone(), true); + assert.equal(scope2.isDone(), true); + }); + + it('should handle mixed operations concurrently', async () => { + const addScope = utils + .createNock() + .get('/v1/channel-registration/sub-key/mySubKey/channel-group/group1') + .query({ + add: 'ch1', + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + }) + .reply(200, '{"status": 200, "message": "OK", "payload": {}, "service": "ChannelGroups"}', { + 'content-type': 'text/javascript', + }); + + const listScope = utils + .createNock() + .get('/v1/channel-registration/sub-key/mySubKey/channel-group/group2') + .query({ + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + }) + .reply( + 200, + '{"status": 200, "message": "OK", "payload": {"channels": ["ch2", "ch3"]}, "service": "ChannelGroups"}', + { + 'content-type': 'text/javascript', + } + ); + + const deleteScope = utils + .createNock() + .get('/v1/channel-registration/sub-key/mySubKey/channel-group/group3/remove') + .query({ + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + }) + .reply(200, '{"status": 200, "message": "OK", "payload": {}, "service": "ChannelGroups"}', { + 'content-type': 'text/javascript', + }); + + const [addResult, listResult, deleteResult] = await Promise.all([ + pubnub.channelGroups.addChannels({ channelGroup: 'group1', channels: ['ch1'] }), + pubnub.channelGroups.listChannels({ channelGroup: 'group2' }), + pubnub.channelGroups.deleteGroup({ channelGroup: 'group3' }) + ]); + + assert.deepEqual(addResult, {}); + assert.deepEqual(listResult.channels, ['ch2', 'ch3']); + assert.deepEqual(deleteResult, {}); + assert.equal(addScope.isDone(), true); + assert.equal(listScope.isDone(), true); + assert.equal(deleteScope.isDone(), true); + }); + }); + }); }); diff --git a/test/integration/endpoints/fetch_messages.test.ts b/test/integration/endpoints/fetch_messages.test.ts index e3abb68db..42bc70f71 100644 --- a/test/integration/endpoints/fetch_messages.test.ts +++ b/test/integration/endpoints/fetch_messages.test.ts @@ -733,4 +733,1014 @@ describe('fetch messages endpoints', () => { } }); }); + + it('throws error when MESSAGE_PERSISTENCE_MODULE disabled', async () => { + const originalEnv = process.env.MESSAGE_PERSISTENCE_MODULE; + process.env.MESSAGE_PERSISTENCE_MODULE = 'disabled'; + + try { + let errorCaught = false; + try { + await pubnub.fetchMessages({ channels: ['ch1'] }); + } catch (error) { + assert(error instanceof Error); + assert(error.message.includes('message persistence module disabled')); + errorCaught = true; + } + assert(errorCaught, 'Expected error was not thrown'); + } finally { + if (originalEnv !== undefined) { + process.env.MESSAGE_PERSISTENCE_MODULE = originalEnv; + } else { + delete process.env.MESSAGE_PERSISTENCE_MODULE; + } + } + }); + + it('handles empty response gracefully', (done) => { + nock.disableNetConnect(); + const scope = utils + .createNock() + .get(`/v3/history/sub-key/${subscribeKey}/channel/ch1`) + .query({ + max: '100', + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + include_uuid: 'true', + include_message_type: 'true', + }) + .reply( + 200, + '{ "channels": {} }', + { 'content-type': 'text/javascript' }, + ); + + pubnub.fetchMessages({ channels: ['ch1'] }, (status, response) => { + try { + assert.equal(status.error, false); + assert.deepEqual(response, { channels: {} }); + assert.equal(scope.isDone(), true); + done(); + } catch (error) { + done(error); + } + }); + }); + + it('handles null message types correctly', (done) => { + nock.disableNetConnect(); + const scope = utils + .createNock() + .get(`/v3/history/sub-key/${subscribeKey}/channel/ch1`) + .query({ + max: '100', + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + include_uuid: 'true', + include_message_type: 'true', + }) + .reply( + 200, + '{ "channels": { "ch1": [{"message": {"text": "hello"}, "timetoken": "16048329933709932", "message_type": null, "uuid": "test-uuid"}] } }', + { 'content-type': 'text/javascript' }, + ); + + pubnub.fetchMessages({ channels: ['ch1'] }, (status, response) => { + try { + assert.equal(status.error, false); + assert(response !== null); + const message = response.channels.ch1[0]; + assert.equal(message.messageType, -1); // PubNubMessageType.Message + assert.equal(scope.isDone(), true); + done(); + } catch (error) { + done(error); + } + }); + }); + + it('processes file messages with URL generation', (done) => { + nock.disableNetConnect(); + const scope = utils + .createNock() + .get(`/v3/history/sub-key/${subscribeKey}/channel/ch1`) + .query({ + max: '100', + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + include_uuid: 'true', + include_message_type: 'true', + }) + .reply( + 200, + '{ "channels": { "ch1": [{"message": "{\\"message\\": \\"Hello\\", \\"file\\": {\\"id\\": \\"file-id\\", \\"name\\": \\"file-name\\", \\"mime-type\\": \\"image/png\\", \\"size\\": 1024}}", "timetoken": "16048329933709932", "message_type": 4, "uuid": "test-uuid"}] } }', + { 'content-type': 'text/javascript' }, + ); + + pubnub.setCipherKey('cipherKey'); + pubnub.fetchMessages({ channels: ['ch1'] }, (status, response) => { + try { + assert.equal(status.error, false); + assert(response !== null); + const message = response.channels.ch1[0]; + assert.equal(message.messageType, 4); // PubNubMessageType.Files + // Just check that the message is processed, URL generation is a complex feature that depends on PubNub configuration + assert.equal(scope.isDone(), true); + done(); + } catch (error) { + done(error); + } + }); + }); + + it('handles various encryption failure scenarios', (done) => { + nock.disableNetConnect(); + const scope = utils + .createNock() + .get(`/v3/history/sub-key/${subscribeKey}/channel/ch1`) + .query({ + max: '100', + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + include_uuid: 'true', + include_message_type: 'true', + }) + .reply( + 200, + '{ "channels": { "ch1": [{"message": "invalid-encrypted-data", "timetoken": "16048329933709932", "uuid": "test-uuid"}] } }', + { 'content-type': 'text/javascript' }, + ); + + pubnub.setCipherKey('wrongkey'); + pubnub.fetchMessages({ channels: ['ch1'] }, (status, response) => { + try { + assert.equal(status.error, false); + assert(response !== null); + const message = response.channels.ch1[0]; + assert.equal(message.message, 'invalid-encrypted-data'); // Should return original payload + assert(message.error); // Should have error field + assert(message.error.includes('Error while decrypting message content')); + assert.equal(scope.isDone(), true); + done(); + } catch (error) { + done(error); + } + }); + }); + + it('handles binary encryption results with ArrayBuffer', (done) => { + nock.disableNetConnect(); + + // Create a mock crypto module that returns ArrayBuffer + const mockCrypto = { + logger: { + log: () => {}, + debug: () => {}, + info: () => {}, + warn: () => {}, + error: () => {}, + trace: () => {} + } as any, + decrypt: () => new TextEncoder().encode('{"text": "hello"}').buffer, + encrypt: (data: string | ArrayBuffer) => data, + encryptFile: (data: ArrayBuffer) => data, + decryptFile: (data: ArrayBuffer) => data, + } as any; + + const pubnubWithMockCrypto = new PubNub({ + subscribeKey, + publishKey, + uuid: 'myUUID', + // @ts-expect-error Force override default value. + useRequestId: false, + useRandomIVs: false, + cryptoModule: mockCrypto, + }); + + const scope = utils + .createNock() + .get(`/v3/history/sub-key/${subscribeKey}/channel/ch1`) + .query({ + max: '100', + pnsdk: `PubNub-JS-Nodejs/${pubnubWithMockCrypto.getVersion()}`, + uuid: 'myUUID', + include_uuid: 'true', + include_message_type: 'true', + }) + .reply( + 200, + '{ "channels": { "ch1": [{"message": "encrypted-data", "timetoken": "16048329933709932", "uuid": "test-uuid"}] } }', + { 'content-type': 'text/javascript' }, + ); + + pubnubWithMockCrypto.fetchMessages({ channels: ['ch1'] }, (status, response) => { + try { + assert.equal(status.error, false); + assert(response !== null); + const message = response.channels.ch1[0]; + assert.deepEqual(message.message, { text: 'hello' }); + assert.equal(scope.isDone(), true); + pubnubWithMockCrypto.destroy(true); + done(); + } catch (error) { + pubnubWithMockCrypto.destroy(true); + done(error); + } + }); + }); + + it('supports both actions and data fields for backward compatibility', (done) => { + nock.disableNetConnect(); + const scope = utils + .createNock() + .get(`/v3/history-with-actions/sub-key/${subscribeKey}/channel/ch1`) + .query({ + max: '25', + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + include_uuid: 'true', + include_message_type: 'true', + }) + .reply( + 200, + '{ "channels": { "ch1": [{"message": {"text": "hello"}, "timetoken": "16048329933709932", "uuid": "test-uuid", "actions": {"reaction": {"like": [{"uuid": "user1", "actionTimetoken": "16048329933709933"}]}}}] } }', + { 'content-type': 'text/javascript' }, + ); + + pubnub.fetchMessages({ channels: ['ch1'], includeMessageActions: true }, (status, response) => { + try { + assert.equal(status.error, false); + assert(response !== null); + const message = response.channels.ch1[0]; + // TypeScript requires type assertion + assert('actions' in message); + assert('data' in message); + assert.deepEqual((message as any).actions, (message as any).data); + assert.equal(scope.isDone(), true); + done(); + } catch (error) { + done(error); + } + }); + }); + + it('validates includeMessageActions single channel constraint', async () => { + let errorCaught = false; + try { + await pubnub.fetchMessages({ channels: ['ch1', 'ch2'], includeMessageActions: true }); + } catch (error) { + assert(error instanceof PubNubError); + assert.equal( + error.status!.message, + 'History can return actions data for a single channel only. Either pass a single channel or disable the includeMessageActions flag.' + ); + errorCaught = true; + } + assert(errorCaught); + }); + + it('handles server error responses gracefully', (done) => { + nock.disableNetConnect(); + const scope = utils + .createNock() + .get(`/v3/history/sub-key/${subscribeKey}/channel/ch1`) + .query({ + max: '100', + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + include_uuid: 'true', + include_message_type: 'true', + }) + .reply( + 403, + '{"status": 403, "error": true, "error_message": "Forbidden"}', + { 'content-type': 'text/javascript' }, + ); + + pubnub.fetchMessages({ channels: ['ch1'] }, (status, response) => { + try { + assert.equal(status.error, true); + assert.equal(status.category, 'PNAccessDeniedCategory'); + assert.equal(scope.isDone(), true); + done(); + } catch (error) { + done(error); + } + }); + }); + + it('processes pagination more field correctly', (done) => { + nock.disableNetConnect(); + const scope = utils + .createNock() + .get(`/v3/history-with-actions/sub-key/${subscribeKey}/channel/ch1`) + .query({ + max: '25', + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + include_uuid: 'true', + include_message_type: 'true', + }) + .reply( + 200, + '{"channels": {"ch1": [{"message": {"text": "hello"}, "timetoken": "16048329933709932", "uuid": "test-uuid"}]}, "more": {"url": "/v3/history-with-actions/sub-key/sub-key/channel/ch1?start=16048329933709932&max=25", "start": "16048329933709932", "max": 25}}', + { 'content-type': 'text/javascript' }, + ); + + pubnub.fetchMessages({ channels: ['ch1'], includeMessageActions: true }, (status, response) => { + try { + assert.equal(status.error, false); + assert(response !== null); + assert('more' in response); + assert.equal(response.more.url, '/v3/history-with-actions/sub-key/sub-key/channel/ch1?start=16048329933709932&max=25'); + assert.equal(response.more.start, '16048329933709932'); + assert.equal(response.more.max, 25); + assert.equal(scope.isDone(), true); + done(); + } catch (error) { + done(error); + } + }); + }); + + it('handles stringified timetokens option', (done) => { + nock.disableNetConnect(); + const scope = utils + .createNock() + .get(`/v3/history/sub-key/${subscribeKey}/channel/ch1`) + .query({ + max: '100', + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + include_uuid: 'true', + include_message_type: 'true', + string_message_token: 'true', + }) + .reply( + 200, + '{ "channels": { "ch1": [{"message": {"text": "hello"}, "timetoken": "16048329933709932", "uuid": "test-uuid"}] } }', + { 'content-type': 'text/javascript' }, + ); + + pubnub.fetchMessages({ channels: ['ch1'], stringifiedTimeToken: true }, (status, response) => { + try { + assert.equal(status.error, false); + assert.equal(scope.isDone(), true); + done(); + } catch (error) { + done(error); + } + }); + }); + + it('includes meta data when requested', (done) => { + nock.disableNetConnect(); + const scope = utils + .createNock() + .get(`/v3/history/sub-key/${subscribeKey}/channel/ch1`) + .query({ + max: '100', + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + include_uuid: 'true', + include_message_type: 'true', + include_meta: 'true', + }) + .reply( + 200, + '{ "channels": { "ch1": [{"message": {"text": "hello"}, "timetoken": "16048329933709932", "uuid": "test-uuid", "meta": {"custom": "data"}}] } }', + { 'content-type': 'text/javascript' }, + ); + + pubnub.fetchMessages({ channels: ['ch1'], includeMeta: true }, (status, response) => { + try { + assert.equal(status.error, false); + assert(response !== null); + const message = response.channels.ch1[0]; + assert.deepEqual(message.meta, { custom: 'data' }); + assert.equal(scope.isDone(), true); + done(); + } catch (error) { + done(error); + } + }); + }); + + it('excludes meta data when not requested', (done) => { + nock.disableNetConnect(); + const scope = utils + .createNock() + .get(`/v3/history/sub-key/${subscribeKey}/channel/ch1`) + .query({ + max: '100', + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + include_uuid: 'true', + include_message_type: 'true', + }) + .reply( + 200, + '{ "channels": { "ch1": [{"message": {"text": "hello"}, "timetoken": "16048329933709932", "uuid": "test-uuid"}] } }', + { 'content-type': 'text/javascript' }, + ); + + pubnub.fetchMessages({ channels: ['ch1'], includeMeta: false }, (status, response) => { + try { + assert.equal(status.error, false); + assert(response !== null); + const message = response.channels.ch1[0]; + // When includeMeta is false, the query should not include include_meta parameter + // and the server response should not include meta field + assert.equal(message.meta, undefined); + assert.equal(scope.isDone(), true); + done(); + } catch (error) { + done(error); + } + }); + }); + + it('handles custom message types correctly', (done) => { + nock.disableNetConnect(); + const scope = utils + .createNock() + .get(`/v3/history/sub-key/${subscribeKey}/channel/ch1`) + .query({ + max: '100', + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + include_uuid: 'true', + include_message_type: 'true', + include_custom_message_type: 'true', + }) + .reply( + 200, + '{ "channels": { "ch1": [{"message": {"text": "hello"}, "timetoken": "16048329933709932", "uuid": "test-uuid", "custom_message_type": "my-custom-type"}] } }', + { 'content-type': 'text/javascript' }, + ); + + pubnub.fetchMessages({ channels: ['ch1'], includeCustomMessageType: true }, (status, response) => { + try { + assert.equal(status.error, false); + assert(response !== null); + const message = response.channels.ch1[0]; + assert.equal(message.customMessageType, 'my-custom-type'); + assert.equal(scope.isDone(), true); + done(); + } catch (error) { + done(error); + } + }); + }); + + it('processes UUID field when included', (done) => { + nock.disableNetConnect(); + const scope = utils + .createNock() + .get(`/v3/history/sub-key/${subscribeKey}/channel/ch1`) + .query({ + max: '100', + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + include_uuid: 'true', + include_message_type: 'true', + }) + .reply( + 200, + '{ "channels": { "ch1": [{"message": {"text": "hello"}, "timetoken": "16048329933709932", "uuid": "publisher-uuid"}] } }', + { 'content-type': 'text/javascript' }, + ); + + pubnub.fetchMessages({ channels: ['ch1'], includeUUID: true }, (status, response) => { + try { + assert.equal(status.error, false); + assert(response !== null); + const message = response.channels.ch1[0]; + assert.equal(message.uuid, 'publisher-uuid'); + assert.equal(scope.isDone(), true); + done(); + } catch (error) { + done(error); + } + }); + }); + + it('omits UUID field when not requested', (done) => { + nock.disableNetConnect(); + const scope = utils + .createNock() + .get(`/v3/history/sub-key/${subscribeKey}/channel/ch1`) + .query({ + max: '100', + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + include_message_type: 'true', + }) + .reply( + 200, + '{ "channels": { "ch1": [{"message": {"text": "hello"}, "timetoken": "16048329933709932", "uuid": "publisher-uuid"}] } }', + { 'content-type': 'text/javascript' }, + ); + + pubnub.fetchMessages({ channels: ['ch1'], includeUUID: false }, (status, response) => { + try { + assert.equal(status.error, false); + // The nock interceptor correctly matches the query without include_uuid parameter + // which verifies that includeUUID: false works as expected + assert.equal(scope.isDone(), true); + done(); + } catch (error) { + done(error); + } + }); + }); + + it('handles start and end timetoken parameters', (done) => { + nock.disableNetConnect(); + const scope = utils + .createNock() + .get(`/v3/history/sub-key/${subscribeKey}/channel/ch1`) + .query({ + max: '100', + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + include_uuid: 'true', + include_message_type: 'true', + start: '15610547826970000', + end: '15610547826970100', + }) + .reply( + 200, + '{ "channels": { "ch1": [{"message": {"text": "hello"}, "timetoken": "15610547826970050", "uuid": "test-uuid"}] } }', + { 'content-type': 'text/javascript' }, + ); + + pubnub.fetchMessages({ + channels: ['ch1'], + start: '15610547826970000', + end: '15610547826970100' + }, (status, response) => { + try { + assert.equal(status.error, false); + assert(response !== null); + const message = response.channels.ch1[0]; + assert.equal(message.timetoken, '15610547826970050'); + assert.equal(scope.isDone(), true); + done(); + } catch (error) { + done(error); + } + }); + }); + + it('supports both callback and promise patterns', async () => { + nock.disableNetConnect(); + + // Test Promise pattern + const promiseScope = utils + .createNock() + .get(`/v3/history/sub-key/${subscribeKey}/channel/ch1`) + .query({ + max: '100', + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + include_uuid: 'true', + include_message_type: 'true', + }) + .reply( + 200, + '{ "channels": { "ch1": [{"message": {"text": "hello"}, "timetoken": "16048329933709932", "uuid": "test-uuid"}] } }', + { 'content-type': 'text/javascript' }, + ); + + try { + const response = await pubnub.fetchMessages({ channels: ['ch1'] }); + assert(response !== null); + assert(response.channels.ch1); + assert.equal(promiseScope.isDone(), true); + } catch (error) { + throw error; + } + + // Test Callback pattern + const callbackScope = utils + .createNock() + .get(`/v3/history/sub-key/${subscribeKey}/channel/ch2`) + .query({ + max: '100', + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + include_uuid: 'true', + include_message_type: 'true', + }) + .reply( + 200, + '{ "channels": { "ch2": [{"message": {"text": "hello"}, "timetoken": "16048329933709932", "uuid": "test-uuid"}] } }', + { 'content-type': 'text/javascript' }, + ); + + return new Promise((resolve, reject) => { + pubnub.fetchMessages({ channels: ['ch2'] }, (status, response) => { + try { + assert.equal(status.error, false); + assert(response !== null); + assert(response.channels.ch2); + assert.equal(callbackScope.isDone(), true); + resolve(); + } catch (error) { + reject(error); + } + }); + }); + }); + + it('logs requests and responses when logVerbosity enabled', (done) => { + nock.disableNetConnect(); + + // Create PubNub instance with logVerbosity enabled + const pubnubWithLogging = new PubNub({ + subscribeKey, + publishKey, + uuid: 'myUUID', + // @ts-expect-error Force override default value. + useRequestId: false, + logVerbosity: true, + }); + + const scope = utils + .createNock() + .get(`/v3/history/sub-key/${subscribeKey}/channel/ch1`) + .query({ + max: '100', + pnsdk: `PubNub-JS-Nodejs/${pubnubWithLogging.getVersion()}`, + uuid: 'myUUID', + include_uuid: 'true', + include_message_type: 'true', + }) + .reply( + 200, + '{ "channels": { "ch1": [{"message": {"text": "hello"}, "timetoken": "16048329933709932", "uuid": "test-uuid"}] } }', + { 'content-type': 'text/javascript' }, + ); + + // Mock console.log to capture log calls + const originalLog = console.log; + let logCalled = false; + console.log = (...args) => { + if (args.some(arg => typeof arg === 'string' && arg.includes('decryption'))) { + logCalled = true; + } + originalLog.apply(console, args); + }; + + pubnubWithLogging.fetchMessages({ channels: ['ch1'] }, (status, response) => { + try { + assert.equal(status.error, false); + assert.equal(scope.isDone(), true); + console.log = originalLog; // Restore console.log + pubnubWithLogging.destroy(true); + done(); + } catch (error) { + console.log = originalLog; // Restore console.log + pubnubWithLogging.destroy(true); + done(error); + } + }); + }); + + it('handles concurrent fetchMessages calls safely', async () => { + nock.disableNetConnect(); + + const scopes = [1, 2, 3].map(i => + utils + .createNock() + .get(`/v3/history/sub-key/${subscribeKey}/channel/ch${i}`) + .query({ + max: '100', + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + include_uuid: 'true', + include_message_type: 'true', + }) + .reply( + 200, + `{ "channels": { "ch${i}": [{"message": {"text": "hello${i}"}, "timetoken": "1604832993370993${i}", "uuid": "test-uuid"}] } }`, + { 'content-type': 'text/javascript' }, + ) + ); + + const promises = [1, 2, 3].map(i => + pubnub.fetchMessages({ channels: [`ch${i}`] }) + ); + + const responses = await Promise.all(promises); + + responses.forEach((response, index) => { + assert(response !== null); + assert(response.channels[`ch${index + 1}`]); + assert.equal(scopes[index].isDone(), true); + }); + }); + + it('supports large channel lists within limits', (done) => { + nock.disableNetConnect(); + + const channels = Array.from({ length: 10 }, (_, i) => `channel${i}`); + const encodedChannels = channels.join(','); + + const scope = utils + .createNock() + .get(`/v3/history/sub-key/${subscribeKey}/channel/${encodedChannels}`) + .query({ + max: '25', // Should default to 25 for multiple channels + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + include_uuid: 'true', + include_message_type: 'true', + }) + .reply( + 200, + '{ "channels": {} }', + { 'content-type': 'text/javascript' }, + ); + + pubnub.fetchMessages({ channels }, (status, response) => { + try { + assert.equal(status.error, false); + assert.equal(scope.isDone(), true); + done(); + } catch (error) { + done(error); + } + }); + }); + + it('handles malformed service responses gracefully', (done) => { + nock.disableNetConnect(); + const scope = utils + .createNock() + .get(`/v3/history/sub-key/${subscribeKey}/channel/ch1`) + .query({ + max: '100', + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + include_uuid: 'true', + include_message_type: 'true', + }) + .reply( + 200, + 'invalid json response', + { 'content-type': 'text/javascript' }, + ); + + pubnub.fetchMessages({ channels: ['ch1'] }, (status, response) => { + try { + assert.equal(status.error, true); + assert.equal(scope.isDone(), true); + done(); + } catch (error) { + done(error); + } + }); + }); + + it('adds signature when secretKey configured', (done) => { + nock.disableNetConnect(); + + const pubnubWithSecret = new PubNub({ + subscribeKey, + publishKey, + secretKey: 'my-secret-key', + uuid: 'myUUID', + // @ts-expect-error Force override default value. + useRequestId: false, + }); + + const scope = utils + .createNock() + .get(`/v3/history/sub-key/${subscribeKey}/channel/ch1`) + .query((queryObject) => { + // Ensure the return value is always boolean to satisfy type requirements + return !!(queryObject.signature && queryObject.signature.toString().startsWith('v2.')); + }) + .reply( + 200, + '{ "channels": { "ch1": [{"message": {"text": "hello"}, "timetoken": "16048329933709932", "uuid": "test-uuid"}] } }', + { 'content-type': 'text/javascript' }, + ); + + pubnubWithSecret.fetchMessages({ channels: ['ch1'] }, (status, response) => { + try { + assert.equal(status.error, false); + assert.equal(scope.isDone(), true); + pubnubWithSecret.destroy(true); + done(); + } catch (error) { + pubnubWithSecret.destroy(true); + done(error); + } + }); + }); + + it('handles very large message payloads', (done) => { + nock.disableNetConnect(); + + const largeMessage = 'x'.repeat(10000); // Large message payload + const scope = utils + .createNock() + .get(`/v3/history/sub-key/${subscribeKey}/channel/ch1`) + .query({ + max: '100', + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + include_uuid: 'true', + include_message_type: 'true', + }) + .reply( + 200, + `{ "channels": { "ch1": [{"message": {"text": "${largeMessage}"}, "timetoken": "16048329933709932", "uuid": "test-uuid"}] } }`, + { 'content-type': 'text/javascript' }, + ); + + pubnub.fetchMessages({ channels: ['ch1'] }, (status, response) => { + try { + assert.equal(status.error, false); + assert(response !== null); + const message = response.channels.ch1[0]; + assert.equal((message.message as any).text, largeMessage); + assert.equal(scope.isDone(), true); + done(); + } catch (error) { + done(error); + } + }); + }); + + it('processes mixed file and regular messages', (done) => { + nock.disableNetConnect(); + const scope = utils + .createNock() + .get(`/v3/history/sub-key/${subscribeKey}/channel/ch1`) + .query({ + max: '100', + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + include_uuid: 'true', + include_message_type: 'true', + }) + .reply( + 200, + '{ "channels": { "ch1": [{"message": {"text": "regular message"}, "timetoken": "16048329933709932", "message_type": null, "uuid": "test-uuid"}, {"message": "{\\"message\\": \\"file message\\", \\"file\\": {\\"id\\": \\"file-id\\", \\"name\\": \\"file.txt\\", \\"mime-type\\": \\"text/plain\\", \\"size\\": 100}}", "timetoken": "16048329933709933", "message_type": 4, "uuid": "test-uuid"}] } }', + { 'content-type': 'text/javascript' }, + ); + + pubnub.setCipherKey('cipherKey'); + pubnub.fetchMessages({ channels: ['ch1'] }, (status, response) => { + try { + assert.equal(status.error, false); + assert(response !== null); + const messages = response.channels.ch1; + + // Regular message + assert.equal(messages[0].messageType, -1); + assert.deepEqual(messages[0].message, { text: 'regular message' }); + + // File message - just check that message type is correct + assert.equal(messages[1].messageType, 4); + + assert.equal(scope.isDone(), true); + done(); + } catch (error) { + done(error); + } + }); + }); + + it('handles includeCustomMessageType flag variations', (done) => { + nock.disableNetConnect(); + + // Test with includeCustomMessageType: true + const trueScope = utils + .createNock() + .get(`/v3/history/sub-key/${subscribeKey}/channel/ch1`) + .query({ + max: '100', + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + include_uuid: 'true', + include_message_type: 'true', + include_custom_message_type: 'true', + }) + .reply( + 200, + '{ "channels": { "ch1": [] } }', + { 'content-type': 'text/javascript' }, + ); + + pubnub.fetchMessages({ channels: ['ch1'], includeCustomMessageType: true }, (status, response) => { + try { + assert.equal(status.error, false); + assert.equal(trueScope.isDone(), true); + + // Test with includeCustomMessageType: false + const falseScope = utils + .createNock() + .get(`/v3/history/sub-key/${subscribeKey}/channel/ch2`) + .query({ + max: '100', + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + include_uuid: 'true', + include_message_type: 'true', + include_custom_message_type: 'false', + }) + .reply( + 200, + '{ "channels": { "ch2": [] } }', + { 'content-type': 'text/javascript' }, + ); + + pubnub.fetchMessages({ channels: ['ch2'], includeCustomMessageType: false }, (status2, response2) => { + try { + assert.equal(status2.error, false); + assert.equal(falseScope.isDone(), true); + done(); + } catch (error) { + done(error); + } + }); + } catch (error) { + done(error); + } + }); + }); + + it('validates operation type correctly', () => { + const request = new (require('../../../src/core/endpoints/fetch_messages').FetchMessagesRequest)({ + keySet: { subscribeKey: 'test-key', publishKey: 'pub-key' }, + channels: ['ch1'], + getFileUrl: () => 'https://example.com/file', + }); + + const operation = request.operation(); + const RequestOperation = require('../../../src/core/constants/operations').default; + assert.equal(operation, RequestOperation.PNFetchMessagesOperation); + }); + + it('handles edge case count values', (done) => { + nock.disableNetConnect(); + + // Test count=0 should use defaults + const scope1 = utils + .createNock() + .get(`/v3/history/sub-key/${subscribeKey}/channel/ch1`) + .query({ + max: '100', // Should default to 100 for single channel + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + include_uuid: 'true', + include_message_type: 'true', + }) + .reply( + 200, + '{ "channels": { "ch1": [] } }', + { 'content-type': 'text/javascript' }, + ); + + pubnub.fetchMessages({ channels: ['ch1'], count: 0 }, (status, response) => { + try { + assert.equal(status.error, false); + assert.equal(scope1.isDone(), true); + + // Test count=1 should work as specified + const scope2 = utils + .createNock() + .get(`/v3/history/sub-key/${subscribeKey}/channel/ch2`) + .query({ + max: '1', + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + include_uuid: 'true', + include_message_type: 'true', + }) + .reply( + 200, + '{ "channels": { "ch2": [] } }', + { 'content-type': 'text/javascript' }, + ); + + pubnub.fetchMessages({ channels: ['ch2'], count: 1 }, (status2, response2) => { + try { + assert.equal(status2.error, false); + assert.equal(scope2.isDone(), true); + done(); + } catch (error) { + done(error); + } + }); + } catch (error) { + done(error); + } + }); + }); }); diff --git a/test/integration/endpoints/message_actions.test.ts b/test/integration/endpoints/message_actions.test.ts index d93a749fd..af649a47c 100644 --- a/test/integration/endpoints/message_actions.test.ts +++ b/test/integration/endpoints/message_actions.test.ts @@ -751,4 +751,723 @@ describe('message actions endpoints', () => { }); }).timeout(60000); }); + + describe('edge cases and error handling', () => { + it('should handle network connection errors gracefully', (done) => { + nock.disableNetConnect(); + const scope = utils + .createNock() + .post(`/v1/message-actions/${subscribeKey}/channel/test-channel/message/1234567890`) + .replyWithError('Network connection failed'); + + pubnub.addMessageAction( + { channel: 'test-channel', messageTimetoken: '1234567890', action: { type: 'reaction', value: 'test' } }, + (status) => { + try { + assert.equal(status.error, true); + done(); + } catch (error) { + done(error); + } + }, + ); + }); + + it('should handle 403 forbidden error', (done) => { + nock.disableNetConnect(); + const scope = utils + .createNock() + .post(`/v1/message-actions/${subscribeKey}/channel/test-channel/message/1234567890`) + .query({ + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + auth: 'myAuthKey', + }) + .reply(403, { + error: { + message: 'Forbidden', + source: 'actions', + }, + }); + + pubnub.addMessageAction( + { channel: 'test-channel', messageTimetoken: '1234567890', action: { type: 'reaction', value: 'test' } }, + (status) => { + try { + assert.equal(status.error, true); + assert.equal(status.statusCode, 403); + done(); + } catch (error) { + done(error); + } + }, + ); + }); + + it('should handle 404 channel not found error', (done) => { + nock.disableNetConnect(); + const scope = utils + .createNock() + .get(`/v1/message-actions/${subscribeKey}/channel/nonexistent-channel`) + .query({ + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + auth: 'myAuthKey', + }) + .reply(404, { + error: { + message: 'Channel not found', + source: 'actions', + }, + }); + + pubnub.getMessageActions({ channel: 'nonexistent-channel' }, (status) => { + try { + assert.equal(status.error, true); + assert.equal(status.statusCode, 404); + done(); + } catch (error) { + done(error); + } + }); + }); + + it('should handle 500 internal server error', (done) => { + nock.disableNetConnect(); + const scope = utils + .createNock() + .delete(`/v1/message-actions/${subscribeKey}/channel/test-channel/message/1234567890/action/15610547826970050`) + .query({ + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + auth: 'myAuthKey', + }) + .reply(500, { + error: { + message: 'Internal Server Error', + source: 'actions', + }, + }); + + pubnub.removeMessageAction( + { channel: 'test-channel', messageTimetoken: '1234567890', actionTimetoken: '15610547826970050' }, + (status) => { + try { + assert.equal(status.error, true); + assert.equal(status.statusCode, 500); + done(); + } catch (error) { + done(error); + } + }, + ); + }); + + it('should handle malformed response', (done) => { + nock.disableNetConnect(); + const scope = utils + .createNock() + .get(`/v1/message-actions/${subscribeKey}/channel/test-channel`) + .reply(200, 'invalid json response'); + + pubnub.getMessageActions({ channel: 'test-channel' }, (status) => { + try { + assert.equal(status.error, true); + done(); + } catch (error) { + done(error); + } + }); + }); + }); + + describe('Unicode and special character handling', () => { + it('should handle Unicode characters in action type and value', (done) => { + nock.disableNetConnect(); + const scope = utils + .createNock() + .post(`/v1/message-actions/${subscribeKey}/channel/test-channel/message/1234567890`) + .query({ + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + auth: 'myAuthKey', + }) + .reply(200, { + status: 200, + data: { + type: 'emoji', + value: '😀🎉', + uuid: 'myUUID', + actionTimetoken: '15610547826970050', + messageTimetoken: '1234567890', + }, + }); + + pubnub.addMessageAction( + { channel: 'test-channel', messageTimetoken: '1234567890', action: { type: 'emoji', value: '😀🎉' } }, + (status, response) => { + try { + assert.equal(scope.isDone(), true); + assert.equal(status.error, false); + assert(response !== null); + assert.equal(response.data.type, 'emoji'); + assert.equal(response.data.value, '😀🎉'); + done(); + } catch (error) { + done(error); + } + }, + ); + }); + + it('should handle Unicode channel names', (done) => { + nock.disableNetConnect(); + const scope = utils + .createNock() + .get(`/v1/message-actions/${subscribeKey}/channel/caf%C3%A9`) + .query({ + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + auth: 'myAuthKey', + }) + .reply(200, { + status: 200, + data: [], + }); + + pubnub.getMessageActions({ channel: 'café' }, (status) => { + try { + assert.equal(scope.isDone(), true); + assert.equal(status.error, false); + done(); + } catch (error) { + done(error); + } + }); + }); + + it('should handle special characters in channel names', (done) => { + nock.disableNetConnect(); + const scope = utils + .createNock() + .post(`/v1/message-actions/${subscribeKey}/channel/test%20channel%2Bspecial%26chars/message/1234567890`) + .query({ + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + auth: 'myAuthKey', + }) + .reply(200, { + status: 200, + data: { + type: 'reaction', + value: 'test', + uuid: 'myUUID', + actionTimetoken: '15610547826970050', + messageTimetoken: '1234567890', + }, + }); + + pubnub.addMessageAction( + { + channel: 'test channel+special&chars', + messageTimetoken: '1234567890', + action: { type: 'reaction', value: 'test' } + }, + (status) => { + try { + assert.equal(scope.isDone(), true); + assert.equal(status.error, false); + done(); + } catch (error) { + done(error); + } + }, + ); + }); + }); + + describe('pagination and response limits', () => { + it('should handle empty message actions response', (done) => { + nock.disableNetConnect(); + const scope = utils + .createNock() + .get(`/v1/message-actions/${subscribeKey}/channel/empty-channel`) + .query({ + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + auth: 'myAuthKey', + }) + .reply(200, { + status: 200, + data: [], + }); + + pubnub.getMessageActions({ channel: 'empty-channel' }, (status, response) => { + try { + assert.equal(scope.isDone(), true); + assert.equal(status.error, false); + assert(response !== null); + assert.equal(response.data.length, 0); + assert.equal(response.start, null); + assert.equal(response.end, null); + done(); + } catch (error) { + done(error); + } + }); + }); + + it('should handle pagination with start parameter', (done) => { + nock.disableNetConnect(); + const scope = utils + .createNock() + .get(`/v1/message-actions/${subscribeKey}/channel/test-channel`) + .query({ + start: '15610547826970050', + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + auth: 'myAuthKey', + }) + .reply(200, { + status: 200, + data: [ + { + type: 'reaction', + value: 'test', + uuid: 'user1', + actionTimetoken: '15610547826970040', + messageTimetoken: '1234567890', + }, + ], + }); + + pubnub.getMessageActions({ channel: 'test-channel', start: '15610547826970050' }, (status, response) => { + try { + assert.equal(scope.isDone(), true); + assert.equal(status.error, false); + assert(response !== null); + assert.equal(response.data.length, 1); + assert.equal(response.start, '15610547826970040'); + assert.equal(response.end, '15610547826970040'); + done(); + } catch (error) { + done(error); + } + }); + }); + + it('should handle pagination with limit parameter', (done) => { + nock.disableNetConnect(); + const scope = utils + .createNock() + .get(`/v1/message-actions/${subscribeKey}/channel/test-channel`) + .query({ + limit: 5, + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + auth: 'myAuthKey', + }) + .reply(200, { + status: 200, + data: Array.from({ length: 5 }, (_, i) => ({ + type: 'reaction', + value: `value${i}`, + uuid: `user${i}`, + actionTimetoken: `1561054782697005${i}`, + messageTimetoken: '1234567890', + })), + }); + + pubnub.getMessageActions({ channel: 'test-channel', limit: 5 }, (status, response) => { + try { + assert.equal(scope.isDone(), true); + assert.equal(status.error, false); + assert(response !== null); + assert.equal(response.data.length, 5); + done(); + } catch (error) { + done(error); + } + }); + }); + + it('should handle response with more field for pagination', (done) => { + nock.disableNetConnect(); + const scope = utils + .createNock() + .get(`/v1/message-actions/${subscribeKey}/channel/test-channel`) + .query({ + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + auth: 'myAuthKey', + }) + .reply(200, { + status: 200, + data: [ + { + type: 'reaction', + value: 'test', + uuid: 'user1', + actionTimetoken: '15610547826970050', + messageTimetoken: '1234567890', + }, + ], + more: { + url: `/v1/message-actions/${subscribeKey}/channel/test-channel?start=15610547826970049`, + start: '15610547826970049', + end: '15610547826970000', + limit: 100, + }, + }); + + pubnub.getMessageActions({ channel: 'test-channel' }, (status, response) => { + try { + assert.equal(scope.isDone(), true); + assert.equal(status.error, false); + assert(response !== null); + assert.equal(response.data.length, 1); + assert(response.more); + assert.equal(response.more?.start, '15610547826970049'); + assert.equal(response.more?.limit, 100); + done(); + } catch (error) { + done(error); + } + }); + }); + }); + + describe('boundary conditions', () => { + it('should handle action type at maximum length (15 characters)', (done) => { + nock.disableNetConnect(); + const maxLengthType = '123456789012345'; // exactly 15 characters + const scope = utils + .createNock() + .post(`/v1/message-actions/${subscribeKey}/channel/test-channel/message/1234567890`) + .query({ + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + auth: 'myAuthKey', + }) + .reply(200, { + status: 200, + data: { + type: maxLengthType, + value: 'test', + uuid: 'myUUID', + actionTimetoken: '15610547826970050', + messageTimetoken: '1234567890', + }, + }); + + pubnub.addMessageAction( + { channel: 'test-channel', messageTimetoken: '1234567890', action: { type: maxLengthType, value: 'test' } }, + (status, response) => { + try { + assert.equal(scope.isDone(), true); + assert.equal(status.error, false); + assert(response !== null); + assert.equal(response.data.type, maxLengthType); + done(); + } catch (error) { + done(error); + } + }, + ); + }); + + it('should handle very long action values', (done) => { + nock.disableNetConnect(); + const longValue = 'a'.repeat(1000); + const scope = utils + .createNock() + .post(`/v1/message-actions/${subscribeKey}/channel/test-channel/message/1234567890`) + .query({ + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + auth: 'myAuthKey', + }) + .reply(200, { + status: 200, + data: { + type: 'reaction', + value: longValue, + uuid: 'myUUID', + actionTimetoken: '15610547826970050', + messageTimetoken: '1234567890', + }, + }); + + pubnub.addMessageAction( + { channel: 'test-channel', messageTimetoken: '1234567890', action: { type: 'reaction', value: longValue } }, + (status, response) => { + try { + assert.equal(scope.isDone(), true); + assert.equal(status.error, false); + assert(response !== null); + assert.equal(response.data.value, longValue); + done(); + } catch (error) { + done(error); + } + }, + ); + }); + + it('should handle very large timetoken values', (done) => { + nock.disableNetConnect(); + const largeTimetoken = '99999999999999999999'; + const scope = utils + .createNock() + .post(`/v1/message-actions/${subscribeKey}/channel/test-channel/message/${largeTimetoken}`) + .query({ + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + auth: 'myAuthKey', + }) + .reply(200, { + status: 200, + data: { + type: 'reaction', + value: 'test', + uuid: 'myUUID', + actionTimetoken: '15610547826970050', + messageTimetoken: largeTimetoken, + }, + }); + + pubnub.addMessageAction( + { channel: 'test-channel', messageTimetoken: largeTimetoken, action: { type: 'reaction', value: 'test' } }, + (status, response) => { + try { + assert.equal(scope.isDone(), true); + assert.equal(status.error, false); + assert(response !== null); + assert.equal(response.data.messageTimetoken, largeTimetoken); + done(); + } catch (error) { + done(error); + } + }, + ); + }); + }); + + describe('promise API support', () => { + it('should support promise-based addMessageAction', async () => { + nock.disableNetConnect(); + const scope = utils + .createNock() + .post(`/v1/message-actions/${subscribeKey}/channel/test-channel/message/1234567890`) + .query({ + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + auth: 'myAuthKey', + }) + .reply(200, { + status: 200, + data: { + type: 'reaction', + value: 'test', + uuid: 'myUUID', + actionTimetoken: '15610547826970050', + messageTimetoken: '1234567890', + }, + }); + + try { + const response = await pubnub.addMessageAction({ + channel: 'test-channel', + messageTimetoken: '1234567890', + action: { type: 'reaction', value: 'test' }, + }); + + assert.equal(scope.isDone(), true); + assert.equal(response.data.type, 'reaction'); + assert.equal(response.data.value, 'test'); + } catch (error) { + throw error; + } + }); + + it('should support promise-based getMessageActions', async () => { + nock.disableNetConnect(); + const scope = utils + .createNock() + .get(`/v1/message-actions/${subscribeKey}/channel/test-channel`) + .query({ + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + auth: 'myAuthKey', + }) + .reply(200, { + status: 200, + data: [ + { + type: 'reaction', + value: 'test', + uuid: 'user1', + actionTimetoken: '15610547826970050', + messageTimetoken: '1234567890', + }, + ], + }); + + try { + const response = await pubnub.getMessageActions({ channel: 'test-channel' }); + + assert.equal(scope.isDone(), true); + assert.equal(response.data.length, 1); + assert.equal(response.data[0].type, 'reaction'); + } catch (error) { + throw error; + } + }); + + it('should support promise-based removeMessageAction', async () => { + nock.disableNetConnect(); + const scope = utils + .createNock() + .delete(`/v1/message-actions/${subscribeKey}/channel/test-channel/message/1234567890/action/15610547826970050`) + .query({ + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + auth: 'myAuthKey', + }) + .reply(200, { + status: 200, + data: {}, + }); + + try { + const response = await pubnub.removeMessageAction({ + channel: 'test-channel', + messageTimetoken: '1234567890', + actionTimetoken: '15610547826970050', + }); + + assert.equal(scope.isDone(), true); + assert(response.data); + } catch (error) { + throw error; + } + }); + }); + + describe('HTTP compliance verification', () => { + it('should use correct HTTP method for add action', (done) => { + nock.disableNetConnect(); + const scope = utils + .createNock() + .post(`/v1/message-actions/${subscribeKey}/channel/test-channel/message/1234567890`) + .query({ + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + auth: 'myAuthKey', + }) + .reply(200, { + status: 200, + data: { + type: 'reaction', + value: 'test', + uuid: 'test', + actionTimetoken: '123', + messageTimetoken: '1234567890' + } + }); + + pubnub.addMessageAction( + { channel: 'test-channel', messageTimetoken: '1234567890', action: { type: 'reaction', value: 'test' } }, + (status) => { + try { + assert.equal(status.error, false); + done(); + } catch (error) { + done(error); + } + }, + ); + }); + + it('should use correct HTTP method for get actions', (done) => { + nock.disableNetConnect(); + const scope = utils + .createNock() + .get(`/v1/message-actions/${subscribeKey}/channel/test-channel`) + .query({ + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + auth: 'myAuthKey', + }) + .reply(200, { status: 200, data: [] }); + + pubnub.getMessageActions({ channel: 'test-channel' }, (status) => { + try { + assert.equal(status.error, false); + done(); + } catch (error) { + done(error); + } + }); + }); + + it('should use correct HTTP method for remove action', (done) => { + nock.disableNetConnect(); + const scope = utils + .createNock() + .delete(`/v1/message-actions/${subscribeKey}/channel/test-channel/message/1234567890/action/15610547826970050`) + .query({ + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + auth: 'myAuthKey', + }) + .reply(200, { status: 200, data: {} }); + + pubnub.removeMessageAction( + { channel: 'test-channel', messageTimetoken: '1234567890', actionTimetoken: '15610547826970050' }, + (status) => { + try { + assert.equal(status.error, false); + done(); + } catch (error) { + done(error); + } + }, + ); + }); + + it('should include correct Content-Type header for POST requests', (done) => { + nock.disableNetConnect(); + const scope = utils + .createNock() + .post(`/v1/message-actions/${subscribeKey}/channel/test-channel/message/1234567890`) + .query({ + pnsdk: `PubNub-JS-Nodejs/${pubnub.getVersion()}`, + uuid: 'myUUID', + auth: 'myAuthKey', + }) + .reply(200, { + status: 200, + data: { + type: 'reaction', + value: 'test', + uuid: 'test', + actionTimetoken: '123', + messageTimetoken: '1234567890' + } + }); + + pubnub.addMessageAction( + { channel: 'test-channel', messageTimetoken: '1234567890', action: { type: 'reaction', value: 'test' } }, + (status) => { + try { + assert.equal(status.error, false); + done(); + } catch (error) { + done(error); + } + }, + ); + }); + }); }); diff --git a/test/integration/endpoints/objects/channel.test.ts b/test/integration/endpoints/objects/channel.test.ts index 69c39491c..3c229339c 100644 --- a/test/integration/endpoints/objects/channel.test.ts +++ b/test/integration/endpoints/objects/channel.test.ts @@ -255,4 +255,476 @@ describe('objects channel', () => { await expect(resultP).to.be.rejected; }); }); + + describe('error handling', () => { + it('should handle 400 Bad Request errors', async () => { + const channelName = 'test-channel'; + const scope = utils + .createNock() + .get(`/v2/objects/${SUBSCRIBE_KEY}/channels/${channelName}`) + .query({ + auth: AUTH_KEY, + uuid: UUID, + pnsdk: PNSDK, + include: 'status,type,custom', + }) + .reply(400, { + status: 400, + error: { + message: 'Bad Request', + source: 'objects', + details: [ + { + message: 'Invalid channel name', + location: 'channel', + locationType: 'path', + } + ] + } + }); + + const resultP = pubnub.objects.getChannelMetadata({ channel: channelName }); + + await expect(scope).to.have.been.requested; + await expect(resultP).to.be.rejected; + }); + + it('should handle 403 Forbidden errors', async () => { + const channelName = 'test-channel'; + const scope = utils + .createNock() + .patch(`/v2/objects/${SUBSCRIBE_KEY}/channels/${channelName}`) + .query({ + auth: AUTH_KEY, + uuid: UUID, + pnsdk: PNSDK, + include: 'status,type,custom', + }) + .reply(403, { + status: 403, + error: { + message: 'Forbidden', + source: 'objects', + } + }); + + const resultP = pubnub.objects.setChannelMetadata({ + channel: channelName, + data: { name: 'Test Channel' } + }); + + await expect(scope).to.have.been.requested; + await expect(resultP).to.be.rejected; + }); + + it('should handle 404 Not Found errors', async () => { + const channelName = 'non-existent-channel'; + const scope = utils + .createNock() + .get(`/v2/objects/${SUBSCRIBE_KEY}/channels/${channelName}`) + .query({ + auth: AUTH_KEY, + uuid: UUID, + pnsdk: PNSDK, + include: 'status,type,custom', + }) + .reply(404, { + status: 404, + error: { + message: 'Not Found', + source: 'objects', + } + }); + + const resultP = pubnub.objects.getChannelMetadata({ channel: channelName }); + + await expect(scope).to.have.been.requested; + await expect(resultP).to.be.rejected; + }); + + it('should handle network timeout errors', async () => { + const channelName = 'test-channel-timeout'; + const scope = utils + .createNock() + .get(`/v2/objects/${SUBSCRIBE_KEY}/channels/${channelName}`) + .query({ + auth: AUTH_KEY, + uuid: UUID, + pnsdk: PNSDK, + include: 'status,type,custom', + }) + .reply(200, { + status: 200, + data: asResponse(channel1), + }); + + const resultP = pubnub.objects.getChannelMetadata({ channel: channelName }); + + await expect(scope).to.have.been.requested; + await expect(resultP).to.eventually.have.property('status', 200); + }); + }); + + describe('data validation', () => { + it('should handle large payload data', async () => { + const channelName = 'test-channel'; + const largeCustomData = { + description: 'A'.repeat(1000), // Large description + custom: Array.from({ length: 50 }, (_, i) => [ + [`property_${i}`, `value_${'x'.repeat(50)}_${i}`] + ]).reduce((acc, curr) => ({ ...acc, [curr[0][0]]: curr[0][1] }), {}), + }; + + const scope = utils + .createNock() + .patch(`/v2/objects/${SUBSCRIBE_KEY}/channels/${channelName}`) + .query({ + auth: AUTH_KEY, + uuid: UUID, + pnsdk: PNSDK, + include: 'status,type,custom', + }) + .reply(200, { + status: 200, + data: asResponse({ ...channel1, data: largeCustomData }), + }); + + const resultP = pubnub.objects.setChannelMetadata({ + channel: channelName, + data: largeCustomData + }); + + await expect(scope).to.have.been.requested; + await expect(resultP).to.eventually.have.property('status', 200); + }); + + it('should handle special characters in channel name', async () => { + const channelName = 'test-channel-with-special-chars'; + const encodedChannelName = 'test-channel-with-special-chars'; + + const scope = utils + .createNock() + .get(`/v2/objects/${SUBSCRIBE_KEY}/channels/${encodedChannelName}`) + .query({ + auth: AUTH_KEY, + uuid: UUID, + pnsdk: PNSDK, + include: 'status,type,custom', + }) + .reply(200, { + status: 200, + data: asResponse({ ...channel1, id: channelName }), + }); + + const resultP = pubnub.objects.getChannelMetadata({ channel: channelName }); + + await expect(scope).to.have.been.requested; + await expect(resultP).to.eventually.have.property('status', 200); + }); + + it('should handle unicode characters in metadata', async () => { + const channelName = 'test-channel'; + const unicodeData = { + name: 'Test Channel 测试频道', + description: 'A test channel with unicode: 🚀 💫 ⭐ 🌟', + custom: { + emoji: '😀😃😄😁😆😅😂🤣', + chinese: '你好世界', + japanese: 'こんにちは世界', + arabic: 'مرحبا بالعالم', + }, + }; + + const scope = utils + .createNock() + .patch(`/v2/objects/${SUBSCRIBE_KEY}/channels/${channelName}`) + .query({ + auth: AUTH_KEY, + uuid: UUID, + pnsdk: PNSDK, + include: 'status,type,custom', + }) + .reply(200, { + status: 200, + data: asResponse({ ...channel1, data: unicodeData }), + }); + + const resultP = pubnub.objects.setChannelMetadata({ + channel: channelName, + data: unicodeData + }); + + await expect(scope).to.have.been.requested; + await expect(resultP).to.eventually.have.property('status', 200); + }); + }); + + describe('advanced features', () => { + it('should support conditional updates with ETag', async () => { + const channelName = 'test-channel'; + const etag = 'AaG95A8Y1bq_8g'; + + const scope = utils + .createNock() + .patch(`/v2/objects/${SUBSCRIBE_KEY}/channels/${channelName}`) + .query({ + auth: AUTH_KEY, + uuid: UUID, + pnsdk: PNSDK, + include: 'status,type,custom', + }) + .matchHeader('If-Match', etag) + .reply(200, { + status: 200, + data: asResponse(channel1), + }); + + const resultP = pubnub.objects.setChannelMetadata({ + channel: channelName, + data: channel1.data, + ifMatchesEtag: etag, + }); + + await expect(scope).to.have.been.requested; + await expect(resultP).to.eventually.have.property('status', 200); + }); + + it('should reject updates with mismatched ETag', async () => { + const channelName = 'test-channel'; + const wrongEtag = 'WrongETag123'; + + const scope = utils + .createNock() + .patch(`/v2/objects/${SUBSCRIBE_KEY}/channels/${channelName}`) + .query({ + auth: AUTH_KEY, + uuid: UUID, + pnsdk: PNSDK, + include: 'status,type,custom', + }) + .matchHeader('If-Match', wrongEtag) + .reply(412, { + status: 412, + error: { + message: 'Precondition Failed', + source: 'objects', + details: [ + { + message: 'ETag mismatch', + location: 'If-Match', + locationType: 'header', + } + ] + } + }); + + const resultP = pubnub.objects.setChannelMetadata({ + channel: channelName, + data: channel1.data, + ifMatchesEtag: wrongEtag, + }); + + await expect(scope).to.have.been.requested; + await expect(resultP).to.be.rejected; + }); + + it('should handle complex sorting combinations', async () => { + const scope = utils + .createNock() + .get(`/v2/objects/${SUBSCRIBE_KEY}/channels`) + .query({ + auth: AUTH_KEY, + uuid: UUID, + pnsdk: PNSDK, + include: 'status,type,custom', + count: false, + sort: ['name:asc', 'updated:desc', 'status'], + limit: 100, + }) + .reply(200, { + status: 200, + data: allChannels.map(asResponse), + }); + + const resultP = pubnub.objects.getAllChannelMetadata({ + sort: { + name: 'asc', + updated: 'desc', + status: null, + }, + include: { + customFields: true, + totalCount: false, + }, + }); + + await expect(scope).to.have.been.requested; + await expect(resultP).to.eventually.have.property('status', 200); + }); + + it('should handle complex filter expressions', async () => { + const complexFilter = 'name LIKE "test*" AND (status = "active" OR priority > 5)'; + + const scope = utils + .createNock() + .get(`/v2/objects/${SUBSCRIBE_KEY}/channels`) + .query({ + auth: AUTH_KEY, + uuid: UUID, + pnsdk: PNSDK, + include: 'status,type,custom', + count: false, + filter: complexFilter, + limit: 100, + }) + .reply(200, { + status: 200, + data: allChannels.slice(0, 2).map(asResponse), // Return filtered subset + }); + + const resultP = pubnub.objects.getAllChannelMetadata({ + filter: complexFilter, + include: { + customFields: true, + totalCount: false, + }, + }); + + await expect(scope).to.have.been.requested; + await expect(resultP).to.eventually.have.property('status', 200); + }); + }); + + describe('boundary testing', () => { + it('should handle empty results for getAllChannelMetadata', async () => { + const scope = utils + .createNock() + .get(`/v2/objects/${SUBSCRIBE_KEY}/channels`) + .query({ + auth: AUTH_KEY, + uuid: UUID, + pnsdk: PNSDK, + include: 'status,type,custom', + count: false, + limit: 100, + }) + .reply(200, { + status: 200, + data: [], + totalCount: 0, + next: null, + prev: null, + }); + + const resultP = pubnub.objects.getAllChannelMetadata({ + include: { + customFields: true, + totalCount: false, + }, + }); + + await expect(scope).to.have.been.requested; + const result = await resultP; + expect(result.data).to.be.an('array').that.is.empty; + }); + + it('should handle maximum limit for getAllChannelMetadata', async () => { + const maxLimit = 100; + const scope = utils + .createNock() + .get(`/v2/objects/${SUBSCRIBE_KEY}/channels`) + .query({ + auth: AUTH_KEY, + uuid: UUID, + pnsdk: PNSDK, + include: 'status,type,custom', + count: false, + limit: maxLimit, + }) + .reply(200, { + status: 200, + data: Array.from({ length: maxLimit }, (_, i) => + asResponse({ ...channel1, id: `channel_${i}` }) + ), + }); + + const resultP = pubnub.objects.getAllChannelMetadata({ + limit: maxLimit, + include: { + customFields: true, + totalCount: false, + }, + }); + + await expect(scope).to.have.been.requested; + const result = await resultP; + expect(result.data).to.have.length(maxLimit); + }); + + it('should handle pagination edge cases', async () => { + // Test first page + const firstPageScope = utils + .createNock() + .get(`/v2/objects/${SUBSCRIBE_KEY}/channels`) + .query({ + auth: AUTH_KEY, + uuid: UUID, + pnsdk: PNSDK, + include: 'status,type,custom', + count: false, + limit: 10, + }) + .reply(200, { + status: 200, + data: allChannels.slice(0, 10).map(asResponse), + next: 'page2_cursor', + prev: null, + }); + + // Test last page + const lastPageScope = utils + .createNock() + .get(`/v2/objects/${SUBSCRIBE_KEY}/channels`) + .query({ + auth: AUTH_KEY, + uuid: UUID, + pnsdk: PNSDK, + include: 'status,type,custom', + count: false, + limit: 10, + start: 'last_page_cursor', + }) + .reply(200, { + status: 200, + data: allChannels.slice(-5).map(asResponse), // Less than full page + next: null, + prev: 'prev_page_cursor', + }); + + // First page request + const firstPageResult = pubnub.objects.getAllChannelMetadata({ + limit: 10, + include: { + customFields: true, + totalCount: false, + }, + }); + + await expect(firstPageScope).to.have.been.requested; + + // Last page request + const lastPageResult = pubnub.objects.getAllChannelMetadata({ + limit: 10, + page: { next: 'last_page_cursor' }, + include: { + customFields: true, + totalCount: false, + }, + }); + + await expect(lastPageScope).to.have.been.requested; + const lastResult = await lastPageResult; + expect(lastResult.data).to.have.length.lessThan(10); + }); + }); }); diff --git a/test/integration/endpoints/presence_errors.test.ts b/test/integration/endpoints/presence_errors.test.ts new file mode 100644 index 000000000..b0817b4b7 --- /dev/null +++ b/test/integration/endpoints/presence_errors.test.ts @@ -0,0 +1,507 @@ +/* global describe, beforeEach, it, before, afterEach */ + +import assert from 'assert'; +import nock from 'nock'; + +import PubNub from '../../../src/node/index'; +import utils from '../../utils'; + +describe('presence error handling', () => { + let pubnub: PubNub; + + before(() => { + nock.disableNetConnect(); + }); + + beforeEach(() => { + nock.cleanAll(); + pubnub = new PubNub({ + subscribeKey: 'mySubscribeKey', + publishKey: 'myPublishKey', + uuid: 'myUUID', + // @ts-expect-error Force override default value. + useRequestId: false, + }); + }); + + afterEach(() => { + pubnub.destroy(true); + }); + + describe('network connectivity errors', () => { + it('should handle network unreachable for all presence endpoints', (done) => { + // Test just one endpoint to verify network error handling + const scope = utils + .createNock() + .get('/v2/presence/sub-key/mySubscribeKey/uuid/myUUID') + .query(true) + .replyWithError('ENETUNREACH'); + + pubnub.whereNow({}, (status, response) => { + try { + assert.equal(status.error, true); + assert(status.errorData); + assert.equal(scope.isDone(), true); + done(); + } catch (error) { + done(error); + } + }); + }); + + it('should handle DNS resolution failure', (done) => { + const scope = utils + .createNock() + .get('/v2/presence/sub-key/mySubscribeKey/uuid/myUUID') + .query(true) + .replyWithError('ENOTFOUND'); + + pubnub.whereNow({}, (status, response) => { + try { + assert.equal(status.error, true); + assert(status.errorData); + assert.equal(scope.isDone(), true); + done(); + } catch (error) { + done(error); + } + }); + }); + + it('should handle connection refused', (done) => { + const scope = utils + .createNock() + .get('/v2/presence/sub-key/mySubscribeKey/uuid/myUUID') + .query(true) + .replyWithError('ECONNREFUSED'); + + pubnub.whereNow({}, (status, response) => { + try { + assert.equal(status.error, true); + assert(status.errorData); + assert.equal(scope.isDone(), true); + done(); + } catch (error) { + done(error); + } + }); + }); + + it('should handle connection reset', (done) => { + const scope = utils + .createNock() + .get('/v2/presence/sub-key/mySubscribeKey/channel/testChannel') + .query(true) + .replyWithError('ECONNRESET'); + + pubnub.hereNow({ channels: ['testChannel'] }, (status, response) => { + try { + assert.equal(status.error, true); + assert(status.errorData); + assert.equal(scope.isDone(), true); + done(); + } catch (error) { + done(error); + } + }); + }); + }); + + describe('HTTP status code errors', () => { + it('should handle 401 unauthorized for all endpoints', (done) => { + // Test one endpoint at a time to avoid race conditions + const scope = utils + .createNock() + .get('/v2/presence/sub-key/mySubscribeKey/uuid/myUUID') + .query(true) + .reply(401, { + status: 401, + error: true, + message: 'Unauthorized', + service: 'Presence' + }, { 'content-type': 'application/json' }); + + pubnub.whereNow({}, (status, response) => { + try { + assert.equal(status.error, true); + assert.equal(status.statusCode, 401); + assert.equal(scope.isDone(), true); + done(); + } catch (error) { + done(error); + } + }); + }); + + it('should handle 429 rate limit exceeded', (done) => { + const scope = utils + .createNock() + .get('/v2/presence/sub-key/mySubscribeKey/uuid/myUUID') + .query(true) + .reply(429, { + status: 429, + error: true, + message: 'Too Many Requests', + service: 'Presence' + }, { + 'Retry-After': '60', + 'content-type': 'application/json' + }); + + pubnub.whereNow({}, (status, response) => { + try { + assert.equal(status.error, true); + assert.equal(status.statusCode, 429); + assert(status.errorData); + assert.equal(scope.isDone(), true); + done(); + } catch (error) { + done(error); + } + }); + }); + + it('should handle 502 bad gateway', (done) => { + const scope = utils + .createNock() + .get('/v2/presence/sub-key/mySubscribeKey/channel/test') + .query(true) + .reply(502, { + status: 502, + error: true, + message: 'Bad Gateway', + service: 'Presence' + }, { 'content-type': 'application/json' }); + + pubnub.hereNow({ channels: ['test'] }, (status, response) => { + try { + assert.equal(status.error, true); + assert.equal(status.statusCode, 502); + assert.equal(scope.isDone(), true); + done(); + } catch (error) { + done(error); + } + }); + }); + + it('should handle 503 service unavailable with retry-after', (done) => { + const scope = utils + .createNock() + .get('/v2/presence/sub-key/mySubscribeKey/channel/test/uuid/myUUID') + .query(true) + .reply(503, { + status: 503, + error: true, + message: 'Service Unavailable', + service: 'Presence' + }, { + 'Retry-After': '30', + 'content-type': 'application/json' + }); + + pubnub.getState({ channels: ['test'] }, (status, response) => { + try { + assert.equal(status.error, true); + assert.equal(status.statusCode, 503); + assert(status.errorData); + assert.equal(scope.isDone(), true); + done(); + } catch (error) { + done(error); + } + }); + }); + }); + + describe('malformed response handling', () => { + it('should handle empty response body', (done) => { + const endpoints = [ + { method: 'whereNow', params: {}, path: '/v2/presence/sub-key/mySubscribeKey/uuid/myUUID' }, + { method: 'hereNow', params: { channels: ['test'] }, path: '/v2/presence/sub-key/mySubscribeKey/channel/test' }, + ]; + + let completedTests = 0; + const expectedTests = endpoints.length; + + endpoints.forEach((config) => { + const scope = utils + .createNock() + .get(config.path) + .query(true) + .reply(200, '', { 'content-type': 'application/json' }); + + (pubnub as any)[config.method](config.params, (status: any) => { + try { + assert.equal(status.error, true); + assert(status.errorData); + assert.equal(scope.isDone(), true); + + completedTests++; + if (completedTests === expectedTests) { + done(); + } + } catch (error) { + done(error); + } + }); + }); + }); + + it('should handle invalid JSON in response', (done) => { + const scope = utils + .createNock() + .get('/v2/presence/sub-key/mySubscribeKey/channel/test/uuid/myUUID/data') + .query(true) + .reply(200, '{"status": 200, "incomplete": json', { 'content-type': 'application/json' }); + + pubnub.setState({ channels: ['test'], state: { key: 'value' } }, (status, response) => { + try { + assert.equal(status.error, true); + assert(status.errorData); + assert.equal(scope.isDone(), true); + done(); + } catch (error) { + done(error); + } + }); + }); + + it('should handle HTML response instead of JSON', (done) => { + const scope = utils + .createNock() + .get('/v2/presence/sub-key/mySubscribeKey/channel/test/heartbeat') + .query(true) + .reply(200, 'Error Page', { 'content-type': 'text/html' }); + + (pubnub as any).heartbeat({ channels: ['test'], heartbeat: 300 }, (status: any, response: any) => { + try { + assert.equal(status.error, true); + assert(status.errorData); + assert.equal(scope.isDone(), true); + done(); + } catch (error) { + done(error); + } + }); + }); + + it('should handle missing required fields in JSON response', (done) => { + const scope = utils + .createNock() + .get('/v2/presence/sub-key/mySubscribeKey/uuid/myUUID') + .query(true) + .reply(200, '{"message": "OK", "service": "Presence"}', { 'content-type': 'application/json' }); // Missing status field + + pubnub.whereNow({}, (status, response) => { + try { + // The response should still be handled, but may have unexpected structure + assert.equal(scope.isDone(), true); + done(); + } catch (error) { + done(error); + } + }); + }); + }); + + describe('false success responses', () => { + it('should handle 200 OK with error status in body', (done) => { + const scope = utils + .createNock() + .get('/v2/presence/sub-key/mySubscribeKey/channel/test') + .query(true) + .reply(200, '{"status": 403, "error": 1, "message": "Access Denied", "service": "Presence"}', { + 'content-type': 'application/json', + }); + + pubnub.hereNow({ channels: ['test'] }, (status, response) => { + try { + assert.equal(status.error, true); + assert.equal(scope.isDone(), true); + done(); + } catch (error) { + done(error); + } + }); + }); + + it('should handle 200 OK with inconsistent data', (done) => { + const scope = utils + .createNock() + .get('/v2/presence/sub-key/mySubscribeKey/channel/test/uuid/myUUID') + .query(true) + .reply( + 200, + '{"status": 200, "message": "OK", "payload": "this should be an object", "service": "Presence"}', + { 'content-type': 'application/json' }, + ); + + pubnub.getState({ channels: ['test'] }, (status, response) => { + try { + // Should handle gracefully even with wrong payload type + assert.equal(status.error, false); + assert.equal(scope.isDone(), true); + done(); + } catch (error) { + done(error); + } + }); + }); + }); + + describe('resource limits and edge cases', () => { + it('should handle extremely large response payload', (done) => { + const largeChannels: Record = {}; + // Create a large response (but not too large to cause memory issues in tests) + for (let i = 0; i < 1000; i++) { + largeChannels[`channel-${i}`] = { + uuids: Array.from({ length: 10 }, (_, j) => `user-${i}-${j}`), + occupancy: 10, + }; + } + + const scope = utils + .createNock() + .get('/v2/presence/sub-key/mySubscribeKey') + .query(true) + .reply( + 200, + JSON.stringify({ + status: 200, + message: 'OK', + payload: { + channels: largeChannels, + total_channels: 1000, + total_occupancy: 10000, + }, + service: 'Presence', + }), + { 'content-type': 'application/json' }, + ); + + pubnub.hereNow({}, (status, response) => { + try { + assert.equal(status.error, false); + assert(response !== null); + assert.equal(response.totalChannels, 1000); + assert.equal(response.totalOccupancy, 10000); + assert.equal(Object.keys(response.channels).length, 1000); + assert.equal(scope.isDone(), true); + done(); + } catch (error) { + done(error); + } + }); + }); + + it('should handle response with null or undefined values', (done) => { + const scope = utils + .createNock() + .get('/v2/presence/sub-key/mySubscribeKey/channel/test/uuid/myUUID/data') + .query(true) + .reply( + 200, + '{"status": 200, "message": "OK", "payload": null, "service": "Presence"}', + { 'content-type': 'application/json' }, + ); + + pubnub.setState({ channels: ['test'], state: { key: 'value' } }, (status, response) => { + try { + assert.equal(status.error, false); + // The response should handle null payload gracefully + assert.equal(scope.isDone(), true); + done(); + } catch (error) { + done(error); + } + }); + }); + }); + + describe('concurrent error scenarios', () => { + it('should handle mixed success and error responses concurrently', (done) => { + // Simplified test with just two scenarios - one success and one error + const successScope = utils + .createNock() + .get('/v2/presence/sub-key/mySubscribeKey/uuid/myUUID') + .query(true) + .reply(200, { + status: 200, + message: 'OK', + payload: { channels: [] }, + service: 'Presence' + }, { 'content-type': 'application/json' }); + + const errorScope = utils + .createNock() + .get('/v2/presence/sub-key/mySubscribeKey/channel/test1') + .query(true) + .reply(403, { + status: 403, + error: true, + message: 'Forbidden', + service: 'Presence' + }, { 'content-type': 'application/json' }); + + let completedTests = 0; + const expectedTests = 2; + + // First call - should succeed + pubnub.whereNow({}, (status, response) => { + try { + assert.equal(status.error, false); + assert.equal(successScope.isDone(), true); + + completedTests++; + if (completedTests === expectedTests) { + done(); + } + } catch (error) { + done(error); + } + }); + + // Second call - should error + pubnub.hereNow({ channels: ['test1'] }, (status, response) => { + try { + assert.equal(status.error, true); + assert.equal(status.statusCode, 403); + assert.equal(errorScope.isDone(), true); + + completedTests++; + if (completedTests === expectedTests) { + done(); + } + } catch (error) { + done(error); + } + }); + }); + + it('should handle timeout with retry attempts', (done) => { + const scope = utils + .createNock() + .get('/v2/presence/sub-key/mySubscribeKey/uuid/myUUID') + .query(true) + .reply(408, { + status: 408, + error: true, + message: 'Request Timeout', + service: 'Presence' + }, { 'content-type': 'application/json' }); + + pubnub.whereNow({}, (status, response) => { + try { + assert.equal(status.error, true); + assert.equal(status.statusCode, 408); + assert(status.errorData); + assert.equal(scope.isDone(), true); + done(); + } catch (error) { + done(error); + } + }); + }); + }); +}); diff --git a/test/unit/access_manager/access_manager_grant_token.test.ts b/test/unit/access_manager/access_manager_grant_token.test.ts new file mode 100644 index 000000000..cd2976c97 --- /dev/null +++ b/test/unit/access_manager/access_manager_grant_token.test.ts @@ -0,0 +1,790 @@ +/* global describe, it, beforeEach */ + +import assert from 'assert'; +import { GrantTokenRequest } from '../../../src/core/endpoints/access_manager/grant_token'; +import { TransportResponse } from '../../../src/core/types/transport-response'; +import { TransportMethod } from '../../../src/core/types/transport-request'; +import RequestOperation from '../../../src/core/constants/operations'; +import { KeySet } from '../../../src/core/types/api'; +import * as PAM from '../../../src/core/types/api/access-manager'; + +// Helper function to parse body as string +function parseBodyAsString(body: string | ArrayBuffer | any): any { + if (typeof body === 'string') { + return JSON.parse(body); + } + throw new Error('Expected body to be string'); +} + +describe('GrantTokenRequest', () => { + let defaultKeySet: KeySet; + let defaultParameters: PAM.GrantTokenParameters & { keySet: KeySet }; + + beforeEach(() => { + defaultKeySet = { + publishKey: 'test_publish_key', + subscribeKey: 'test_subscribe_key', + secretKey: 'test_secret_key', + }; + + defaultParameters = { + keySet: defaultKeySet, + ttl: 60, + resources: { + channels: { + test_channel: { + read: true, + write: true, + }, + }, + }, + }; + }); + + describe('validation', () => { + it('should validate required subscribe key', () => { + const requestWithoutSubscribeKey = new GrantTokenRequest({ + ...defaultParameters, + keySet: { ...defaultKeySet, subscribeKey: '' }, + }); + assert.equal(requestWithoutSubscribeKey.validate(), "Missing Subscribe Key"); + }); + + it('should validate required publish key', () => { + const requestWithoutPublishKey = new GrantTokenRequest({ + ...defaultParameters, + keySet: { ...defaultKeySet, publishKey: '' }, + }); + assert.equal(requestWithoutPublishKey.validate(), "Missing Publish Key"); + }); + + it('should validate required secret key', () => { + const requestWithoutSecretKey = new GrantTokenRequest({ + ...defaultParameters, + keySet: { ...defaultKeySet, secretKey: '' }, + }); + assert.equal(requestWithoutSecretKey.validate(), "Missing Secret Key"); + }); + + it('should require either resources or patterns', () => { + const requestWithoutResourcesOrPatterns = new GrantTokenRequest({ + keySet: defaultKeySet, + ttl: 60, + // Both resources and patterns are undefined + } as any); + assert.equal(requestWithoutResourcesOrPatterns.validate(), "Missing values for either Resources or Patterns"); + }); + + it('should require non-empty resources or patterns', () => { + const requestWithEmptyResources = new GrantTokenRequest({ + ...defaultParameters, + resources: {}, + patterns: {}, + }); + assert.equal(requestWithEmptyResources.validate(), "Missing values for either Resources or Patterns"); + }); + + it('should require non-empty resource objects', () => { + const requestWithEmptyResourceObjects = new GrantTokenRequest({ + ...defaultParameters, + resources: { + channels: {}, + groups: {}, + uuids: {}, + }, + }); + assert.equal(requestWithEmptyResourceObjects.validate(), "Missing values for either Resources or Patterns"); + }); + + it('should pass validation with valid channel resources', () => { + const validRequest = new GrantTokenRequest(defaultParameters); + assert.equal(validRequest.validate(), undefined); + }); + + it('should pass validation with patterns', () => { + const validPatternRequest = new GrantTokenRequest({ + ...defaultParameters, + resources: undefined, + patterns: { + channels: { + '.*': { + read: true, + }, + }, + }, + }); + assert.equal(validPatternRequest.validate(), undefined); + }); + + it('should pass validation with both resources and patterns', () => { + const validBothRequest = new GrantTokenRequest({ + ...defaultParameters, + patterns: { + channels: { + 'pattern.*': { + read: true, + }, + }, + }, + }); + assert.equal(validBothRequest.validate(), undefined); + }); + }); + + describe('VSP legacy permissions validation', () => { + it('should validate VSP authorizedUserId without new permission fields', () => { + const vspRequest = new GrantTokenRequest({ + keySet: defaultKeySet, + ttl: 60, + authorizedUserId: 'user123', + resources: { + users: { + user1: { + get: true, // VSP legacy - using valid UuidTokenPermissions property + }, + }, + }, + } as any); // Type assertion for VSP legacy + assert.equal(vspRequest.validate(), undefined); + }); + + it('should reject mixing VSP and new permissions in resources', () => { + const mixedRequest = new GrantTokenRequest({ + keySet: defaultKeySet, + ttl: 60, + authorizedUserId: 'user123', + resources: { + users: { + user1: { + get: true, + }, + }, + channels: { + channel1: { + read: true, + }, + }, + }, + } as any); // Type assertion for VSP legacy + assert.equal( + mixedRequest.validate(), + "Cannot mix `users`, `spaces` and `authorizedUserId` with `uuids`, `channels`, `groups` and `authorized_uuid`" + ); + }); + + it('should reject mixing VSP and new permissions in patterns', () => { + const mixedRequest = new GrantTokenRequest({ + keySet: defaultKeySet, + ttl: 60, + authorizedUserId: 'user123', + resources: { + users: { + user1: { + get: true, + }, + }, + }, + patterns: { + channels: { + '.*': { + read: true, + }, + }, + }, + } as any); // Type assertion for VSP legacy + assert.equal( + mixedRequest.validate(), + "Cannot mix `users`, `spaces` and `authorizedUserId` with `uuids`, `channels`, `groups` and `authorized_uuid`" + ); + }); + }); + + describe('operation', () => { + it('should return PNAccessManagerGrantToken operation', () => { + const request = new GrantTokenRequest(defaultParameters); + assert.equal(request.operation(), RequestOperation.PNAccessManagerGrantToken); + }); + }); + + describe('request method', () => { + it('should use POST method', () => { + const request = new GrantTokenRequest(defaultParameters); + const transportRequest = request.request(); + assert.equal(transportRequest.method, TransportMethod.POST); + }); + }); + + describe('URL construction', () => { + it('should construct correct URL path', () => { + const request = new GrantTokenRequest(defaultParameters); + const transportRequest = request.request(); + + const expectedPath = `/v3/pam/${defaultKeySet.subscribeKey}/grant`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should construct URL with different subscribe key', () => { + const customKeySet = { ...defaultKeySet, subscribeKey: 'custom_sub_key' }; + const request = new GrantTokenRequest({ + ...defaultParameters, + keySet: customKeySet, + }); + + const transportRequest = request.request(); + assert.equal(transportRequest.path, `/v3/pam/custom_sub_key/grant`); + }); + }); + + describe('headers', () => { + it('should set Content-Type header to application/json', () => { + const request = new GrantTokenRequest(defaultParameters); + const transportRequest = request.request(); + + assert.equal(transportRequest.headers?.['Content-Type'], 'application/json'); + }); + }); + + describe('JSON body construction', () => { + it('should include TTL in body when specified', () => { + const request = new GrantTokenRequest({ + ...defaultParameters, + ttl: 1440, + }); + + const transportRequest = request.request(); + const body = parseBodyAsString(transportRequest.body!); + assert.equal(body.ttl, 1440); + }); + + it('should include TTL when TTL is 0', () => { + const request = new GrantTokenRequest({ + ...defaultParameters, + ttl: 0, + }); + + const transportRequest = request.request(); + const body = parseBodyAsString(transportRequest.body!); + assert.equal(body.ttl, 0); + }); + + it('should not include TTL when not specified', () => { + const params = { ...defaultParameters }; + delete (params as any).ttl; + const request = new GrantTokenRequest(params); + + const transportRequest = request.request(); + const body = parseBodyAsString(transportRequest.body!); + assert.equal(body.ttl, undefined); + }); + + it('should include authorized_uuid when specified', () => { + const request = new GrantTokenRequest({ + ...defaultParameters, + authorized_uuid: 'test_uuid', + }); + + const transportRequest = request.request(); + const body = parseBodyAsString(transportRequest.body!); + assert.equal(body.permissions.uuid, 'test_uuid'); + }); + + it('should include authorizedUserId for VSP permissions', () => { + const request = new GrantTokenRequest({ + keySet: defaultKeySet, + ttl: 60, + authorizedUserId: 'vsp_user', + resources: { + users: { + user1: { + get: true, + }, + }, + }, + } as any); // Type assertion for VSP legacy + + const transportRequest = request.request(); + const body = parseBodyAsString(transportRequest.body!); + assert.equal(body.permissions.uuid, 'vsp_user'); + }); + + it('should include meta when specified', () => { + const metaData = { key1: 'value1', key2: 'value2' }; + const request = new GrantTokenRequest({ + ...defaultParameters, + meta: metaData, + }); + + const transportRequest = request.request(); + const body = parseBodyAsString(transportRequest.body!); + assert.deepEqual(body.permissions.meta, metaData); + }); + + it('should include empty meta object when not specified', () => { + const request = new GrantTokenRequest(defaultParameters); + + const transportRequest = request.request(); + const body = parseBodyAsString(transportRequest.body!); + assert.deepEqual(body.permissions.meta, {}); + }); + }); + + describe('permission bit calculation', () => { + it('should calculate read permission bit (1)', () => { + const request = new GrantTokenRequest({ + ...defaultParameters, + resources: { + channels: { + test_channel: { + read: true, + }, + }, + }, + }); + + const transportRequest = request.request(); + const body = parseBodyAsString(transportRequest.body!); + assert.equal(body.permissions.resources.channels.test_channel, 1); + }); + + it('should calculate write permission bit (2)', () => { + const request = new GrantTokenRequest({ + ...defaultParameters, + resources: { + channels: { + test_channel: { + write: true, + }, + }, + }, + }); + + const transportRequest = request.request(); + const body = parseBodyAsString(transportRequest.body!); + assert.equal(body.permissions.resources.channels.test_channel, 2); + }); + + it('should calculate manage permission bit (4)', () => { + const request = new GrantTokenRequest({ + ...defaultParameters, + resources: { + channels: { + test_channel: { + manage: true, + }, + }, + }, + }); + + const transportRequest = request.request(); + const body = parseBodyAsString(transportRequest.body!); + assert.equal(body.permissions.resources.channels.test_channel, 4); + }); + + it('should calculate delete permission bit (8)', () => { + const request = new GrantTokenRequest({ + ...defaultParameters, + resources: { + channels: { + test_channel: { + delete: true, + }, + }, + }, + }); + + const transportRequest = request.request(); + const body = parseBodyAsString(transportRequest.body!); + assert.equal(body.permissions.resources.channels.test_channel, 8); + }); + + it('should calculate get permission bit (32)', () => { + const request = new GrantTokenRequest({ + ...defaultParameters, + resources: { + uuids: { + test_uuid: { + get: true, + }, + }, + }, + }); + + const transportRequest = request.request(); + const body = parseBodyAsString(transportRequest.body!); + assert.equal(body.permissions.resources.uuids.test_uuid, 32); + }); + + it('should calculate update permission bit (64)', () => { + const request = new GrantTokenRequest({ + ...defaultParameters, + resources: { + uuids: { + test_uuid: { + update: true, + }, + }, + }, + }); + + const transportRequest = request.request(); + const body = parseBodyAsString(transportRequest.body!); + assert.equal(body.permissions.resources.uuids.test_uuid, 64); + }); + + it('should calculate join permission bit (128)', () => { + const request = new GrantTokenRequest({ + ...defaultParameters, + resources: { + channels: { + test_channel: { + join: true, // join is valid for channels + }, + }, + }, + }); + + const transportRequest = request.request(); + const body = parseBodyAsString(transportRequest.body!); + assert.equal(body.permissions.resources.channels.test_channel, 128); + }); + + it('should combine multiple permission bits', () => { + const request = new GrantTokenRequest({ + ...defaultParameters, + resources: { + channels: { + test_channel: { + read: true, // 1 + write: true, // 2 + manage: true, // 4 + // Total: 1 + 2 + 4 = 7 + }, + }, + }, + }); + + const transportRequest = request.request(); + const body = parseBodyAsString(transportRequest.body!); + assert.equal(body.permissions.resources.channels.test_channel, 7); + }); + + it('should combine all permission bits', () => { + const request = new GrantTokenRequest({ + ...defaultParameters, + resources: { + channels: { + test_channel: { + read: true, // 1 + write: true, // 2 + manage: true, // 4 + delete: true, // 8 + get: true, // 32 + update: true, // 64 + join: true, // 128 + // Total: 1 + 2 + 4 + 8 + 32 + 64 + 128 = 239 + }, + }, + }, + }); + + const transportRequest = request.request(); + const body = parseBodyAsString(transportRequest.body!); + assert.equal(body.permissions.resources.channels.test_channel, 239); + }); + + it('should handle false permissions as 0', () => { + const request = new GrantTokenRequest({ + ...defaultParameters, + resources: { + channels: { + test_channel: { + read: false, + write: false, + manage: false, + delete: false, + }, + }, + }, + }); + + const transportRequest = request.request(); + const body = parseBodyAsString(transportRequest.body!); + assert.equal(body.permissions.resources.channels.test_channel, 0); + }); + }); + + describe('resources and patterns structure', () => { + it('should structure channel resources correctly', () => { + const request = new GrantTokenRequest({ + ...defaultParameters, + resources: { + channels: { + channel1: { read: true }, + channel2: { write: true }, + }, + }, + }); + + const transportRequest = request.request(); + const body = parseBodyAsString(transportRequest.body!); + + assert.equal(body.permissions.resources.channels.channel1, 1); + assert.equal(body.permissions.resources.channels.channel2, 2); + assert.deepEqual(body.permissions.resources.groups, {}); + assert.deepEqual(body.permissions.resources.uuids, {}); + }); + + it('should structure channel group resources correctly', () => { + const request = new GrantTokenRequest({ + ...defaultParameters, + resources: { + groups: { + group1: { read: true, manage: true }, + group2: { read: true }, + }, + }, + }); + + const transportRequest = request.request(); + const body = parseBodyAsString(transportRequest.body!); + + assert.equal(body.permissions.resources.groups.group1, 5); // 1 + 4 + assert.equal(body.permissions.resources.groups.group2, 1); // read only + assert.deepEqual(body.permissions.resources.channels, {}); + assert.deepEqual(body.permissions.resources.uuids, {}); + }); + + it('should structure uuid resources correctly', () => { + const request = new GrantTokenRequest({ + ...defaultParameters, + resources: { + uuids: { + uuid1: { get: true, update: true }, + uuid2: { delete: true }, + }, + }, + }); + + const transportRequest = request.request(); + const body = parseBodyAsString(transportRequest.body!); + + assert.equal(body.permissions.resources.uuids.uuid1, 96); // 32 + 64 + assert.equal(body.permissions.resources.uuids.uuid2, 8); // delete + assert.deepEqual(body.permissions.resources.channels, {}); + assert.deepEqual(body.permissions.resources.groups, {}); + }); + + it('should structure patterns correctly', () => { + const request = new GrantTokenRequest({ + ...defaultParameters, + resources: undefined, + patterns: { + channels: { + 'channel.*': { read: true }, + 'private.*': { read: true, write: true }, + }, + groups: { + 'group.*': { manage: true }, + }, + }, + }); + + const transportRequest = request.request(); + const body = parseBodyAsString(transportRequest.body!); + + assert.equal(body.permissions.patterns.channels['channel.*'], 1); + assert.equal(body.permissions.patterns.channels['private.*'], 3); // 1 + 2 + assert.equal(body.permissions.patterns.groups['group.*'], 4); + assert.deepEqual(body.permissions.patterns.uuids, {}); + }); + + it('should handle both resources and patterns', () => { + const request = new GrantTokenRequest({ + ...defaultParameters, + resources: { + channels: { + specific_channel: { read: true }, + }, + }, + patterns: { + channels: { + 'dynamic.*': { write: true }, + }, + }, + }); + + const transportRequest = request.request(); + const body = parseBodyAsString(transportRequest.body!); + + assert.equal(body.permissions.resources.channels.specific_channel, 1); + assert.equal(body.permissions.patterns.channels['dynamic.*'], 2); + }); + }); + + describe('VSP legacy permissions handling', () => { + it('should handle VSP users as uuids', () => { + const request = new GrantTokenRequest({ + keySet: defaultKeySet, + ttl: 60, + resources: { + users: { + user1: { get: true }, // VSP legacy - mapped to uuids + user2: { get: true, update: true }, + }, + }, + } as any); // Type assertion for VSP legacy + + const transportRequest = request.request(); + const body = parseBodyAsString(transportRequest.body!); + + assert.equal(body.permissions.resources.uuids.user1, 32); // get + assert.equal(body.permissions.resources.uuids.user2, 96); // 32 + 64 + }); + + it('should handle VSP spaces as channels', () => { + const request = new GrantTokenRequest({ + keySet: defaultKeySet, + ttl: 60, + resources: { + spaces: { + space1: { read: true, write: true }, + space2: { manage: true }, + }, + }, + } as any); // Type assertion for VSP legacy + + const transportRequest = request.request(); + const body = parseBodyAsString(transportRequest.body!); + + assert.equal(body.permissions.resources.channels.space1, 3); // 1 + 2 + assert.equal(body.permissions.resources.channels.space2, 4); + }); + + it('should handle VSP patterns correctly', () => { + const request = new GrantTokenRequest({ + keySet: defaultKeySet, + ttl: 60, + patterns: { + users: { + 'user.*': { get: true }, + }, + spaces: { + 'space.*': { read: true, write: true }, + }, + }, + } as any); // Type assertion for VSP legacy + + const transportRequest = request.request(); + const body = parseBodyAsString(transportRequest.body!); + + assert.equal(body.permissions.patterns.uuids['user.*'], 32); + assert.equal(body.permissions.patterns.channels['space.*'], 3); // 1 + 2 + }); + }); + + describe('response parsing', () => { + it('should parse successful grant token response', async () => { + const request = new GrantTokenRequest(defaultParameters); + const mockResponse: TransportResponse = { + status: 200, + url: 'https://test.pubnub.com', + headers: { 'content-type': 'application/json' }, + body: new TextEncoder().encode(JSON.stringify({ + status: 200, + data: { + message: 'Success', + token: 'p0AkFl043rhDdHRsple3KgQ3NwY6BDcENnctokenVzcqBDczaWdYIGOAeTyWGJI', + }, + service: 'Access Manager', + })), + }; + + const parsedResponse = await request.parse(mockResponse); + assert.equal(parsedResponse, 'p0AkFl043rhDdHRsple3KgQ3NwY6BDcENnctokenVzcqBDczaWdYIGOAeTyWGJI'); + }); + }); + + describe('edge cases', () => { + it('should handle empty resource objects with default structure', () => { + const request = new GrantTokenRequest({ + ...defaultParameters, + resources: { + channels: { + test: { read: true }, + }, + }, + }); + + const transportRequest = request.request(); + const body = parseBodyAsString(transportRequest.body!); + + // Should have all resource types even if not specified + assert.equal(typeof body.permissions.resources.channels, 'object'); + assert.equal(typeof body.permissions.resources.groups, 'object'); + assert.equal(typeof body.permissions.resources.uuids, 'object'); + + // Should have all pattern types even if not specified + assert.equal(typeof body.permissions.patterns.channels, 'object'); + assert.equal(typeof body.permissions.patterns.groups, 'object'); + assert.equal(typeof body.permissions.patterns.uuids, 'object'); + }); + + it('should handle special characters in resource names', () => { + const request = new GrantTokenRequest({ + ...defaultParameters, + resources: { + channels: { + 'channel-with-dashes': { read: true }, + 'channel_with_underscores': { write: true }, + 'channel.with.dots': { manage: true }, + }, + }, + }); + + const transportRequest = request.request(); + const body = parseBodyAsString(transportRequest.body!); + + assert.equal(body.permissions.resources.channels['channel-with-dashes'], 1); + assert.equal(body.permissions.resources.channels['channel_with_underscores'], 2); + assert.equal(body.permissions.resources.channels['channel.with.dots'], 4); + }); + + it('should handle regex patterns in pattern names', () => { + const request = new GrantTokenRequest({ + ...defaultParameters, + resources: undefined, + patterns: { + channels: { + '^channel-[A-Za-z0-9]+$': { read: true }, + '.*private.*': { write: true }, + }, + }, + }); + + const transportRequest = request.request(); + const body = parseBodyAsString(transportRequest.body!); + + assert.equal(body.permissions.patterns.channels['^channel-[A-Za-z0-9]+$'], 1); + assert.equal(body.permissions.patterns.channels['.*private.*'], 2); + }); + + it('should handle large numbers of resources', () => { + const channels: Record = {}; + for (let i = 0; i < 100; i++) { + channels[`channel_${i}`] = { read: true }; + } + + const request = new GrantTokenRequest({ + ...defaultParameters, + resources: { channels }, + }); + + const transportRequest = request.request(); + const body = parseBodyAsString(transportRequest.body!); + + assert.equal(Object.keys(body.permissions.resources.channels).length, 100); + assert.equal(body.permissions.resources.channels.channel_0, 1); + assert.equal(body.permissions.resources.channels.channel_99, 1); + }); + }); +}); diff --git a/test/unit/access_manager/access_manager_revoke_token.test.ts b/test/unit/access_manager/access_manager_revoke_token.test.ts new file mode 100644 index 000000000..f6bcbb10c --- /dev/null +++ b/test/unit/access_manager/access_manager_revoke_token.test.ts @@ -0,0 +1,406 @@ +/* global describe, it, beforeEach */ + +import assert from 'assert'; +import { RevokeTokenRequest } from '../../../src/core/endpoints/access_manager/revoke_token'; +import { TransportResponse } from '../../../src/core/types/transport-response'; +import { TransportMethod } from '../../../src/core/types/transport-request'; +import RequestOperation from '../../../src/core/constants/operations'; +import { KeySet } from '../../../src/core/types/api'; + +describe('RevokeTokenRequest', () => { + let defaultKeySet: KeySet; + let defaultParameters: { token: string; keySet: KeySet }; + + beforeEach(() => { + defaultKeySet = { + publishKey: 'test_publish_key', + subscribeKey: 'test_subscribe_key', + secretKey: 'test_secret_key', + }; + + defaultParameters = { + token: 'p0AkFl043rhDdHRsple3KgQ3NwY6BDcENnctokenVzcqBDczaWdYIGOAeTyWGJI', + keySet: defaultKeySet, + }; + }); + + describe('validation', () => { + it('should validate required secret key', () => { + const requestWithoutSecretKey = new RevokeTokenRequest({ + ...defaultParameters, + keySet: { ...defaultKeySet, secretKey: '' }, + }); + assert.equal(requestWithoutSecretKey.validate(), "Missing Secret Key"); + }); + + it('should validate required token', () => { + const requestWithoutToken = new RevokeTokenRequest({ + ...defaultParameters, + token: '', + }); + assert.equal(requestWithoutToken.validate(), "token can't be empty"); + }); + + it('should pass validation with valid parameters', () => { + const validRequest = new RevokeTokenRequest(defaultParameters); + assert.equal(validRequest.validate(), undefined); + }); + + it('should pass validation with only secret key and token', () => { + const minimalRequest = new RevokeTokenRequest({ + token: 'test_token', + keySet: { subscribeKey: 'test_sub_key', secretKey: 'test_secret_key' }, + }); + assert.equal(minimalRequest.validate(), undefined); + }); + }); + + describe('operation', () => { + it('should return PNAccessManagerRevokeToken operation', () => { + const request = new RevokeTokenRequest(defaultParameters); + assert.equal(request.operation(), RequestOperation.PNAccessManagerRevokeToken); + }); + }); + + describe('request method', () => { + it('should use DELETE method', () => { + const request = new RevokeTokenRequest(defaultParameters); + const transportRequest = request.request(); + assert.equal(transportRequest.method, TransportMethod.DELETE); + }); + }); + + describe('URL construction', () => { + it('should construct correct URL path with encoded token', () => { + const request = new RevokeTokenRequest(defaultParameters); + const transportRequest = request.request(); + + const expectedPath = `/v3/pam/${defaultKeySet.subscribeKey}/grant/${encodeURIComponent(defaultParameters.token)}`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should construct URL with different subscribe key', () => { + const customKeySet = { ...defaultKeySet, subscribeKey: 'custom_sub_key' }; + const request = new RevokeTokenRequest({ + ...defaultParameters, + keySet: customKeySet, + }); + + const transportRequest = request.request(); + const expectedPath = `/v3/pam/custom_sub_key/grant/${encodeURIComponent(defaultParameters.token)}`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should handle different token values', () => { + const customToken = 'qEF2AkF0GmEI03xDdHRsGDxDcmVzpURjaGFuoWljaGFubmVsLTEY70NncnChb2NoYW5uZWxfZ3JvdXAtMQVDdXNyoENzcGOgRHV1aWShZnV1aWQtMRhoQ3BhdKVEY2hhbqFtXmNoYW5uZWwtXFMqJBjvQ2dycKF0XjpjaGFubmVsX2dyb3VwLVxTKiQFQ3VzcqBDc3BjoER1dWlkoWpedXVpZC1cUyokGGhEbWV0YaBEdXVpZHR0ZXN0LWF1dGhvcml6ZWQtdXVpZENzaWdYIPpU-vCe9rkpYs87YUrFNWkyNq8CVvmKwEjVinnDrJJc'; + const request = new RevokeTokenRequest({ + ...defaultParameters, + token: customToken, + }); + + const transportRequest = request.request(); + const expectedPath = `/v3/pam/${defaultKeySet.subscribeKey}/grant/${encodeURIComponent(customToken)}`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should handle tokens with special characters that need encoding', () => { + const tokenWithSpecialChars = 'token+with/special=chars&more?data'; + const request = new RevokeTokenRequest({ + ...defaultParameters, + token: tokenWithSpecialChars, + }); + + const transportRequest = request.request(); + const expectedPath = `/v3/pam/${defaultKeySet.subscribeKey}/grant/${encodeURIComponent(tokenWithSpecialChars)}`; + assert.equal(transportRequest.path, expectedPath); + + // Verify that special characters are actually encoded using encodeString + // encodeString does additional encoding beyond encodeURIComponent + const encoded = transportRequest.path; + assert(encoded.includes(encodeURIComponent(tokenWithSpecialChars))); + }); + + it('should handle tokens with spaces', () => { + const tokenWithSpaces = 'token with spaces'; + const request = new RevokeTokenRequest({ + ...defaultParameters, + token: tokenWithSpaces, + }); + + const transportRequest = request.request(); + const expectedPath = `/v3/pam/${defaultKeySet.subscribeKey}/grant/${encodeURIComponent(tokenWithSpaces)}`; + assert.equal(transportRequest.path, expectedPath); + + // Verify that spaces are encoded as %20 + assert(transportRequest.path.includes('token%20with%20spaces')); + }); + + it('should handle empty subscribe key', () => { + const emptySubKeyRequest = new RevokeTokenRequest({ + ...defaultParameters, + keySet: { ...defaultKeySet, subscribeKey: '' }, + }); + + const transportRequest = emptySubKeyRequest.request(); + const expectedPath = `/v3/pam//grant/${encodeURIComponent(defaultParameters.token)}`; + assert.equal(transportRequest.path, expectedPath); + }); + }); + + describe('token encoding edge cases', () => { + it('should handle very long tokens', () => { + const longToken = 'a'.repeat(1000); + const request = new RevokeTokenRequest({ + ...defaultParameters, + token: longToken, + }); + + const transportRequest = request.request(); + assert(transportRequest.path.includes(encodeURIComponent(longToken))); + }); + + it('should handle tokens with Unicode characters', () => { + const unicodeToken = 'token_with_unicode_🚀_characters_ñ_ü'; + const request = new RevokeTokenRequest({ + ...defaultParameters, + token: unicodeToken, + }); + + const transportRequest = request.request(); + assert(transportRequest.path.includes(encodeURIComponent(unicodeToken))); + }); + + it('should handle tokens with Base64-like characters', () => { + const base64Token = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; + const request = new RevokeTokenRequest({ + ...defaultParameters, + token: base64Token, + }); + + const transportRequest = request.request(); + assert(transportRequest.path.includes(encodeURIComponent(base64Token))); + }); + + it('should handle tokens with URL-unsafe characters', () => { + const unsafeToken = 'token#with%unsafe&characters?and=more'; + const request = new RevokeTokenRequest({ + ...defaultParameters, + token: unsafeToken, + }); + + const transportRequest = request.request(); + assert(transportRequest.path.includes(encodeURIComponent(unsafeToken))); + }); + }); + + describe('response parsing', () => { + it('should parse successful revoke token response', async () => { + const request = new RevokeTokenRequest(defaultParameters); + const mockResponse: TransportResponse = { + status: 200, + url: 'https://test.pubnub.com', + headers: { 'content-type': 'application/json' }, + body: new TextEncoder().encode(JSON.stringify({ + status: 200, + data: {}, + service: 'Access Manager', + })), + }; + + const parsedResponse = await request.parse(mockResponse); + assert.deepEqual(parsedResponse, {}); + }); + + it('should parse successful revoke response with additional data', async () => { + const request = new RevokeTokenRequest(defaultParameters); + const mockResponse: TransportResponse = { + status: 200, + url: 'https://test.pubnub.com', + headers: { 'content-type': 'application/json' }, + body: new TextEncoder().encode(JSON.stringify({ + status: 200, + data: { + message: 'Token successfully revoked', + timestamp: 1234567890, + }, + service: 'Access Manager', + })), + }; + + const parsedResponse = await request.parse(mockResponse); + // Should always return empty object regardless of response content + assert.deepEqual(parsedResponse, {}); + }); + + it('should handle empty response body', async () => { + const request = new RevokeTokenRequest(defaultParameters); + const mockResponse: TransportResponse = { + status: 200, + url: 'https://test.pubnub.com', + headers: { 'content-type': 'application/json' }, + body: new TextEncoder().encode(''), + }; + + try { + await request.parse(mockResponse); + // Should not reach here if parsing fails + assert.fail('Expected parsing to fail with empty body'); + } catch (error) { + // Expected to throw due to invalid JSON + assert(error instanceof Error); + } + }); + + it('should handle non-JSON response', async () => { + const request = new RevokeTokenRequest(defaultParameters); + const mockResponse: TransportResponse = { + status: 200, + url: 'https://test.pubnub.com', + headers: { 'content-type': 'text/plain' }, + body: new TextEncoder().encode('Success'), + }; + + try { + await request.parse(mockResponse); + // Should not reach here if parsing fails + assert.fail('Expected parsing to fail with non-JSON body'); + } catch (error) { + // Expected to throw due to invalid JSON + assert(error instanceof Error); + } + }); + }); + + describe('minimal configurations', () => { + it('should work with only required parameters', () => { + const minimalRequest = new RevokeTokenRequest({ + token: 'test_token', + keySet: { subscribeKey: 'test_sub_key', secretKey: 'test_secret' }, + }); + + assert.equal(minimalRequest.validate(), undefined); + assert.equal(minimalRequest.operation(), RequestOperation.PNAccessManagerRevokeToken); + + const transportRequest = minimalRequest.request(); + assert.equal(transportRequest.method, TransportMethod.DELETE); + assert(transportRequest.path.includes('test_token')); + }); + + it('should not require publish key for validation', () => { + const requestWithoutPubKey = new RevokeTokenRequest({ + token: 'test_token', + keySet: { subscribeKey: 'test_sub_key', secretKey: 'test_secret' }, + }); + + assert.equal(requestWithoutPubKey.validate(), undefined); + }); + }); + + describe('edge cases', () => { + it('should handle undefined keySet properties gracefully', () => { + const request = new RevokeTokenRequest({ + token: 'test_token', + keySet: { + secretKey: 'test_secret', + subscribeKey: undefined as any, + publishKey: undefined as any, + }, + }); + + const transportRequest = request.request(); + assert.equal(transportRequest.path, `/v3/pam/undefined/grant/${encodeURIComponent('test_token')}`); + }); + + it('should handle very short tokens', () => { + const shortToken = 'a'; + const request = new RevokeTokenRequest({ + ...defaultParameters, + token: shortToken, + }); + + assert.equal(request.validate(), undefined); + const transportRequest = request.request(); + assert(transportRequest.path.includes(shortToken)); + }); + + it('should handle numeric-like tokens', () => { + const numericToken = '1234567890'; + const request = new RevokeTokenRequest({ + ...defaultParameters, + token: numericToken, + }); + + const transportRequest = request.request(); + assert(transportRequest.path.includes(numericToken)); + }); + + it('should handle tokens with only special characters', () => { + const specialCharsToken = '!@#$%^&*()_+-=[]{}|;:,.<>?'; + const request = new RevokeTokenRequest({ + ...defaultParameters, + token: specialCharsToken, + }); + + const transportRequest = request.request(); + // Verify the token is in the path (it will be encoded) + assert(transportRequest.path.includes('/grant/')); + }); + }); + + describe('request properties', () => { + it('should have empty query parameters object', () => { + const request = new RevokeTokenRequest(defaultParameters); + const transportRequest = request.request(); + + // DELETE requests have empty query parameters object (not undefined) + assert.deepEqual(transportRequest.queryParameters, {}); + }); + + it('should not have request body', () => { + const request = new RevokeTokenRequest(defaultParameters); + const transportRequest = request.request(); + + // DELETE requests typically don't have bodies + assert.equal(transportRequest.body, undefined); + }); + + it('should have default headers from AbstractRequest', () => { + const request = new RevokeTokenRequest(defaultParameters); + const transportRequest = request.request(); + + // Should have default headers from AbstractRequest + assert.deepEqual(transportRequest.headers, { + 'Accept-Encoding': 'gzip, deflate' + }); + }); + + it('should be configured as non-compressible', () => { + const request = new RevokeTokenRequest(defaultParameters); + const transportRequest = request.request(); + + // DELETE requests are typically not compressible + assert.equal(transportRequest.compressible, false); + }); + }); + + describe('consistency with other access manager endpoints', () => { + it('should follow same URL pattern as other v3 PAM endpoints', () => { + const request = new RevokeTokenRequest(defaultParameters); + const transportRequest = request.request(); + + // Should follow /v3/pam/{subscribeKey}/* pattern + assert(transportRequest.path.startsWith('/v3/pam/')); + assert(transportRequest.path.includes(defaultKeySet.subscribeKey!)); + }); + + it('should require secret key like other PAM operations', () => { + const requestWithoutSecret = new RevokeTokenRequest({ + ...defaultParameters, + keySet: { ...defaultKeySet, secretKey: '' }, + }); + + // All PAM operations should require secret key + assert.equal(requestWithoutSecret.validate(), "Missing Secret Key"); + }); + }); +}); diff --git a/test/unit/app_context/channel_objects_get.test.ts b/test/unit/app_context/channel_objects_get.test.ts new file mode 100644 index 000000000..dff7e2235 --- /dev/null +++ b/test/unit/app_context/channel_objects_get.test.ts @@ -0,0 +1,116 @@ +import assert from 'assert'; + +import { GetChannelMetadataRequest } from '../../../src/core/endpoints/objects/channel/get'; +import { TransportMethod } from '../../../src/core/types/transport-request'; +import RequestOperation from '../../../src/core/constants/operations'; +import { KeySet } from '../../../src/core/types/api'; +import * as AppContext from '../../../src/core/types/api/app-context'; + +describe('GetChannelMetadataRequest', () => { + let defaultKeySet: KeySet; + let defaultParameters: AppContext.GetChannelMetadataParameters & { keySet: KeySet }; + + beforeEach(() => { + defaultKeySet = { + publishKey: 'test_publish_key', + subscribeKey: 'test_subscribe_key', + }; + + defaultParameters = { + channel: 'test_channel', + keySet: defaultKeySet, + }; + }); + + describe('parameter validation', () => { + it('should return "Channel cannot be empty" when channel is empty string', () => { + const request = new GetChannelMetadataRequest({ + ...defaultParameters, + channel: '', + }); + assert.equal(request.validate(), 'Channel cannot be empty'); + }); + + it('should return "Channel cannot be empty" when channel is null', () => { + const request = new GetChannelMetadataRequest({ + ...defaultParameters, + channel: null as any, + }); + assert.equal(request.validate(), 'Channel cannot be empty'); + }); + + it('should return "Channel cannot be empty" when channel is undefined', () => { + const request = new GetChannelMetadataRequest({ + ...defaultParameters, + channel: undefined as any, + }); + assert.equal(request.validate(), 'Channel cannot be empty'); + }); + + it('should return undefined when channel is provided', () => { + const request = new GetChannelMetadataRequest(defaultParameters); + assert.equal(request.validate(), undefined); + }); + }); + + describe('operation type', () => { + it('should return correct operation type', () => { + const request = new GetChannelMetadataRequest(defaultParameters); + assert.equal(request.operation(), RequestOperation.PNGetChannelMetadataOperation); + }); + }); + + describe('URL construction', () => { + it('should construct correct path with subscribeKey and channel', () => { + const request = new GetChannelMetadataRequest(defaultParameters); + const transportRequest = request.request(); + const expectedPath = `/v2/objects/${defaultKeySet.subscribeKey}/channels/${defaultParameters.channel}`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should encode special characters in channel name', () => { + const request = new GetChannelMetadataRequest({ + ...defaultParameters, + channel: 'test-channel#1@2', + }); + const transportRequest = request.request(); + const expectedPath = `/v2/objects/${defaultKeySet.subscribeKey}/channels/test-channel%231%402`; + assert.equal(transportRequest.path, expectedPath); + }); + }); + + describe('query parameters', () => { + it('should include custom fields by default', () => { + const request = new GetChannelMetadataRequest(defaultParameters); + const transportRequest = request.request(); + const includeParam = transportRequest.queryParameters?.include as string; + assert(includeParam?.includes('custom')); + }); + + it('should exclude custom fields when include.customFields is false', () => { + const request = new GetChannelMetadataRequest({ + ...defaultParameters, + include: { customFields: false }, + }); + const transportRequest = request.request(); + const includeParam = transportRequest.queryParameters?.include as string; + assert(!includeParam?.includes('custom')); + }); + + it('should include status and type fields', () => { + const request = new GetChannelMetadataRequest(defaultParameters); + const transportRequest = request.request(); + const includeParam = transportRequest.queryParameters?.include as string; + assert(includeParam?.includes('status')); + assert(includeParam?.includes('type')); + }); + }); + + describe('HTTP method', () => { + it('should use GET method', () => { + const request = new GetChannelMetadataRequest(defaultParameters); + const transportRequest = request.request(); + assert.equal(transportRequest.method, TransportMethod.GET); + }); + }); +}); diff --git a/test/unit/app_context/channel_objects_get_all.test.ts b/test/unit/app_context/channel_objects_get_all.test.ts new file mode 100644 index 000000000..5a5fbe027 --- /dev/null +++ b/test/unit/app_context/channel_objects_get_all.test.ts @@ -0,0 +1,122 @@ +import assert from 'assert'; + +import { GetAllChannelsMetadataRequest } from '../../../src/core/endpoints/objects/channel/get_all'; +import { TransportMethod } from '../../../src/core/types/transport-request'; +import RequestOperation from '../../../src/core/constants/operations'; +import { KeySet } from '../../../src/core/types/api'; +import * as AppContext from '../../../src/core/types/api/app-context'; + +describe('GetAllChannelsMetadataRequest', () => { + let defaultKeySet: KeySet; + let defaultParameters: AppContext.GetAllMetadataParameters> & { keySet: KeySet }; + + beforeEach(() => { + defaultKeySet = { + publishKey: 'test_publish_key', + subscribeKey: 'test_subscribe_key', + }; + + defaultParameters = { + keySet: defaultKeySet, + }; + }); + + describe('operation type', () => { + it('should return correct operation type', () => { + const request = new GetAllChannelsMetadataRequest>(defaultParameters); + assert.equal(request.operation(), RequestOperation.PNGetAllChannelMetadataOperation); + }); + }); + + describe('default parameters', () => { + it('should set default limit to 100', () => { + const request = new GetAllChannelsMetadataRequest>(defaultParameters); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.limit, 100); + }); + + it('should set includeCustomFields to false by default', () => { + const request = new GetAllChannelsMetadataRequest>(defaultParameters); + const transportRequest = request.request(); + const includeParam = transportRequest.queryParameters?.include as string; + assert(!includeParam?.includes('custom')); + }); + + it('should set includeTotalCount to false by default', () => { + const request = new GetAllChannelsMetadataRequest>(defaultParameters); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.count, 'false'); + }); + }); + + describe('query parameters', () => { + it('should handle custom limit parameter', () => { + const request = new GetAllChannelsMetadataRequest>({ + ...defaultParameters, + limit: 50, + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.limit, 50); + }); + + it('should handle filter parameter', () => { + const request = new GetAllChannelsMetadataRequest>({ + ...defaultParameters, + filter: 'name LIKE "test*"', + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.filter, 'name LIKE "test*"'); + }); + + it('should handle pagination start cursor', () => { + const request = new GetAllChannelsMetadataRequest>({ + ...defaultParameters, + page: { next: 'test-next-cursor' }, + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.start, 'test-next-cursor'); + }); + + it('should handle pagination end cursor', () => { + const request = new GetAllChannelsMetadataRequest>({ + ...defaultParameters, + page: { prev: 'test-prev-cursor' }, + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.end, 'test-prev-cursor'); + }); + }); + + describe('sorting', () => { + it('should handle string sort parameter', () => { + const request = new GetAllChannelsMetadataRequest>({ + ...defaultParameters, + sort: 'name', + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.sort, 'name'); + }); + + it('should handle object sort with direction', () => { + const request = new GetAllChannelsMetadataRequest>({ + ...defaultParameters, + sort: { name: 'desc', updated: 'asc' }, + }); + const transportRequest = request.request(); + const sortParams = transportRequest.queryParameters?.sort as string[]; + assert(sortParams.includes('name:desc')); + assert(sortParams.includes('updated:asc')); + }); + + it('should handle object sort with null direction', () => { + const request = new GetAllChannelsMetadataRequest>({ + ...defaultParameters, + sort: { name: null, updated: 'asc' }, + }); + const transportRequest = request.request(); + const sortParams = transportRequest.queryParameters?.sort as string[]; + assert(sortParams.includes('name')); + assert(sortParams.includes('updated:asc')); + }); + }); +}); diff --git a/test/unit/app_context/channel_objects_remove.test.ts b/test/unit/app_context/channel_objects_remove.test.ts new file mode 100644 index 000000000..ad893b89f --- /dev/null +++ b/test/unit/app_context/channel_objects_remove.test.ts @@ -0,0 +1,65 @@ +import assert from 'assert'; + +import { RemoveChannelMetadataRequest } from '../../../src/core/endpoints/objects/channel/remove'; +import { TransportMethod } from '../../../src/core/types/transport-request'; +import { KeySet } from '../../../src/core/types/api'; +import * as AppContext from '../../../src/core/types/api/app-context'; + +describe('RemoveChannelMetadataRequest', () => { + let defaultKeySet: KeySet; + let defaultParameters: AppContext.RemoveChannelMetadataParameters & { keySet: KeySet }; + + beforeEach(() => { + defaultKeySet = { + publishKey: 'test_publish_key', + subscribeKey: 'test_subscribe_key', + }; + + defaultParameters = { + channel: 'test_channel', + keySet: defaultKeySet, + }; + }); + + describe('parameter validation', () => { + it('should return "Channel cannot be empty" when channel is empty', () => { + const request = new RemoveChannelMetadataRequest({ + ...defaultParameters, + channel: '', + }); + assert.equal(request.validate(), 'Channel cannot be empty'); + }); + + it('should return undefined when channel provided', () => { + const request = new RemoveChannelMetadataRequest(defaultParameters); + assert.equal(request.validate(), undefined); + }); + }); + + describe('HTTP method', () => { + it('should use DELETE method', () => { + const request = new RemoveChannelMetadataRequest(defaultParameters); + const transportRequest = request.request(); + assert.equal(transportRequest.method, TransportMethod.DELETE); + }); + }); + + describe('URL construction', () => { + it('should construct correct path', () => { + const request = new RemoveChannelMetadataRequest(defaultParameters); + const transportRequest = request.request(); + const expectedPath = `/v2/objects/${defaultKeySet.subscribeKey}/channels/${defaultParameters.channel}`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should encode channel name in path', () => { + const request = new RemoveChannelMetadataRequest({ + ...defaultParameters, + channel: 'test-channel#1@2', + }); + const transportRequest = request.request(); + const expectedPath = `/v2/objects/${defaultKeySet.subscribeKey}/channels/test-channel%231%402`; + assert.equal(transportRequest.path, expectedPath); + }); + }); +}); diff --git a/test/unit/app_context/channel_objects_set.test.ts b/test/unit/app_context/channel_objects_set.test.ts new file mode 100644 index 000000000..47b038221 --- /dev/null +++ b/test/unit/app_context/channel_objects_set.test.ts @@ -0,0 +1,129 @@ +import assert from 'assert'; + +import { SetChannelMetadataRequest } from '../../../src/core/endpoints/objects/channel/set'; +import { TransportMethod } from '../../../src/core/types/transport-request'; +import { KeySet } from '../../../src/core/types/api'; +import * as AppContext from '../../../src/core/types/api/app-context'; + +describe('SetChannelMetadataRequest', () => { + let defaultKeySet: KeySet; + let defaultParameters: AppContext.SetChannelMetadataParameters & { keySet: KeySet }; + + beforeEach(() => { + defaultKeySet = { + publishKey: 'test_publish_key', + subscribeKey: 'test_subscribe_key', + }; + + defaultParameters = { + channel: 'test_channel', + data: { + name: 'Test Channel', + description: 'A test channel', + custom: { + category: 'test', + priority: 1, + }, + }, + keySet: defaultKeySet, + }; + }); + + describe('parameter validation', () => { + it('should return "Channel cannot be empty" when channel is empty', () => { + const request = new SetChannelMetadataRequest({ + ...defaultParameters, + channel: '', + }); + assert.equal(request.validate(), 'Channel cannot be empty'); + }); + + it('should return "Data cannot be empty" when data is null', () => { + const request = new SetChannelMetadataRequest({ + ...defaultParameters, + data: null as any, + }); + assert.equal(request.validate(), 'Data cannot be empty'); + }); + + it('should return "Data cannot be empty" when data is undefined', () => { + const request = new SetChannelMetadataRequest({ + ...defaultParameters, + data: undefined as any, + }); + assert.equal(request.validate(), 'Data cannot be empty'); + }); + + it('should return undefined when all required parameters provided', () => { + const request = new SetChannelMetadataRequest(defaultParameters); + assert.equal(request.validate(), undefined); + }); + }); + + describe('HTTP method and headers', () => { + it('should use PATCH method', () => { + const request = new SetChannelMetadataRequest(defaultParameters); + const transportRequest = request.request(); + assert.equal(transportRequest.method, TransportMethod.PATCH); + }); + + it('should set Content-Type header', () => { + const request = new SetChannelMetadataRequest(defaultParameters); + const transportRequest = request.request(); + assert.equal(transportRequest.headers?.['Content-Type'], 'application/json'); + }); + + it('should set If-Match header when ifMatchesEtag provided', () => { + const request = new SetChannelMetadataRequest({ + ...defaultParameters, + ifMatchesEtag: 'test-etag-123', + }); + const transportRequest = request.request(); + assert.equal(transportRequest.headers?.['If-Match'], 'test-etag-123'); + }); + + it('should not set If-Match header when ifMatchesEtag not provided', () => { + const request = new SetChannelMetadataRequest(defaultParameters); + const transportRequest = request.request(); + assert.equal(transportRequest.headers?.['If-Match'], undefined); + }); + }); + + describe('body serialization', () => { + it('should serialize data as JSON string', () => { + const request = new SetChannelMetadataRequest(defaultParameters); + const transportRequest = request.request(); + assert.equal(transportRequest.body, JSON.stringify(defaultParameters.data)); + }); + + it('should handle complex nested data objects', () => { + const complexData = { + name: 'Complex Channel', + description: 'A complex test channel', + custom: { + metadata: { + tags: ['test', 'complex'], + settings: { + notifications: true, + theme: 'dark', + }, + }, + permissions: { + read: ['user1', 'user2'], + write: ['admin'], + }, + }, + }; + + const request = new SetChannelMetadataRequest({ + ...defaultParameters, + data: complexData as any, + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.body, JSON.stringify(complexData)); + const parsedBody = JSON.parse(transportRequest.body as string); + assert.deepEqual(parsedBody, complexData); + }); + }); +}); diff --git a/test/unit/app_context/membership_objects_get.test.ts b/test/unit/app_context/membership_objects_get.test.ts new file mode 100644 index 000000000..818a20822 --- /dev/null +++ b/test/unit/app_context/membership_objects_get.test.ts @@ -0,0 +1,309 @@ +/* global describe, it, beforeEach */ + +import assert from 'assert'; +import { GetUUIDMembershipsRequest } from '../../../src/core/endpoints/objects/membership/get'; +import { TransportMethod } from '../../../src/core/types/transport-request'; +import RequestOperation from '../../../src/core/constants/operations'; +import { KeySet } from '../../../src/core/types/api'; +import * as AppContext from '../../../src/core/types/api/app-context'; + +describe('GetUUIDMembershipsRequest', () => { + let defaultKeySet: KeySet; + let defaultParameters: AppContext.GetMembershipsParameters & { keySet: KeySet }; + + beforeEach(() => { + defaultKeySet = { + publishKey: 'test_publish_key', + subscribeKey: 'test_subscribe_key', + }; + + defaultParameters = { + uuid: 'test_user_uuid', + keySet: defaultKeySet, + }; + }); + + describe('parameter validation', () => { + it("should return \"'uuid' cannot be empty\" when uuid is empty string", () => { + const request = new GetUUIDMembershipsRequest({ + ...defaultParameters, + uuid: '', + }); + assert.equal(request.validate(), "'uuid' cannot be empty"); + }); + + it("should return \"'uuid' cannot be empty\" when uuid is null", () => { + const request = new GetUUIDMembershipsRequest({ + ...defaultParameters, + uuid: null as any, + }); + assert.equal(request.validate(), "'uuid' cannot be empty"); + }); + + it("should return \"'uuid' cannot be empty\" when uuid is undefined", () => { + const request = new GetUUIDMembershipsRequest({ + ...defaultParameters, + uuid: undefined, + }); + assert.equal(request.validate(), "'uuid' cannot be empty"); + }); + + it('should return undefined when uuid is provided', () => { + const request = new GetUUIDMembershipsRequest(defaultParameters); + assert.equal(request.validate(), undefined); + }); + }); + + describe('operation type', () => { + it('should return correct operation type', () => { + const request = new GetUUIDMembershipsRequest(defaultParameters); + assert.equal(request.operation(), RequestOperation.PNGetMembershipsOperation); + }); + }); + + describe('URL construction', () => { + it('should construct correct path with subscribeKey and uuid', () => { + const request = new GetUUIDMembershipsRequest(defaultParameters); + const transportRequest = request.request(); + const expectedPath = `/v2/objects/${defaultKeySet.subscribeKey}/uuids/${defaultParameters.uuid}/channels`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should encode special characters in uuid', () => { + const specialUuid = 'test-uuid#1@2'; + const request = new GetUUIDMembershipsRequest({ + ...defaultParameters, + uuid: specialUuid, + }); + const transportRequest = request.request(); + const expectedEncodedUuid = 'test-uuid%231%402'; + assert(transportRequest.path.includes(expectedEncodedUuid)); + }); + }); + + describe('HTTP method', () => { + it('should use GET method', () => { + const request = new GetUUIDMembershipsRequest(defaultParameters); + const transportRequest = request.request(); + assert.equal(transportRequest.method, TransportMethod.GET); + }); + }); + + describe('query parameters', () => { + it('should construct query parameters with all include options', () => { + const request = new GetUUIDMembershipsRequest({ + ...defaultParameters, + include: { + customFields: true, + totalCount: true, + statusField: true, + typeField: true, + channelFields: true, + customChannelFields: true, + channelStatusField: true, + channelTypeField: true, + }, + }); + const transportRequest = request.request(); + const queryParams = transportRequest.queryParameters; + assert.equal(queryParams?.include, 'status,type,custom,channel,channel.status,channel.type,channel.custom'); + }); + + it('should handle pagination parameters', () => { + const request = new GetUUIDMembershipsRequest({ + ...defaultParameters, + page: { + next: 'nextToken', + prev: 'prevToken', + }, + }); + const transportRequest = request.request(); + const queryParams = transportRequest.queryParameters; + // Both start and end can be present if both next and prev are provided + assert.equal(queryParams?.start, 'nextToken'); + assert.equal(queryParams?.end, 'prevToken'); + }); + + it('should handle filter parameter', () => { + const filterString = 'name == "test"'; + const request = new GetUUIDMembershipsRequest({ + ...defaultParameters, + filter: filterString, + }); + const transportRequest = request.request(); + const queryParams = transportRequest.queryParameters; + assert.equal(queryParams?.filter, filterString); + }); + + it('should handle sort parameter as string', () => { + const sortString = 'updated:desc'; + const request = new GetUUIDMembershipsRequest({ + ...defaultParameters, + sort: 'updated' as any, + }); + const transportRequest = request.request(); + const queryParams = transportRequest.queryParameters; + assert.equal(queryParams?.sort, 'updated'); + }); + + it('should handle sort parameter as object', () => { + const sortObject = { updated: 'desc', status: 'asc' }; + const request = new GetUUIDMembershipsRequest({ + ...defaultParameters, + sort: sortObject as any, + }); + const transportRequest = request.request(); + const queryParams = transportRequest.queryParameters; + // Should contain both sort fields as array + assert(Array.isArray(queryParams?.sort)); + const sortArray = queryParams?.sort as string[]; + assert(sortArray.includes('updated:desc')); + assert(sortArray.includes('status:asc')); + }); + + it('should handle limit parameter', () => { + const request = new GetUUIDMembershipsRequest({ + ...defaultParameters, + limit: 50, + }); + const transportRequest = request.request(); + const queryParams = transportRequest.queryParameters; + assert.equal(queryParams?.limit, 50); + }); + }); + + describe('default values', () => { + it('should apply default values for include options', () => { + const request = new GetUUIDMembershipsRequest({ + ...defaultParameters, + // No include specified + }); + const transportRequest = request.request(); + const queryParams = transportRequest.queryParameters; + // By default, all include flags should be false, so include should not be present + assert.equal(queryParams?.include, undefined); + }); + + it('should apply default limit', () => { + const request = new GetUUIDMembershipsRequest({ + ...defaultParameters, + // No limit specified + }); + const transportRequest = request.request(); + const queryParams = transportRequest.queryParameters; + assert.equal(queryParams?.limit, 100); // Default limit from the source code + }); + }); + + describe('backward compatibility', () => { + it('should map userId to uuid parameter', () => { + const request = new GetUUIDMembershipsRequest({ + keySet: defaultKeySet, + userId: 'test-user-id', // Using userId instead of uuid + // uuid is not provided + } as any); + + // The request should be valid as userId gets mapped to uuid internally + assert.equal(request.validate(), undefined); + + const transportRequest = request.request(); + const expectedPath = `/v2/objects/${defaultKeySet.subscribeKey}/uuids/test-user-id/channels`; + assert.equal(transportRequest.path, expectedPath); + }); + }); + + describe('query parameter combinations', () => { + it('should handle multiple query parameters together', () => { + const request = new GetUUIDMembershipsRequest({ + ...defaultParameters, + include: { + customFields: true, + statusField: true, + channelFields: true, + }, + limit: 25, + filter: 'channel.name like "*test*"', + sort: 'channel.name', + page: { + next: 'token123', + }, + }); + + const transportRequest = request.request(); + const queryParams = transportRequest.queryParameters; + + assert.equal(queryParams?.include, 'status,custom,channel'); + assert.equal(queryParams?.limit, 25); + assert.equal(queryParams?.filter, 'channel.name like "*test*"'); + assert.equal(queryParams?.sort, 'channel.name'); + assert.equal(queryParams?.start, 'token123'); + assert.equal(queryParams?.count, 'false'); // totalCount default + }); + + it('should handle page prev without next', () => { + const request = new GetUUIDMembershipsRequest({ + ...defaultParameters, + page: { + prev: 'prevToken', + // no next token + }, + }); + const transportRequest = request.request(); + const queryParams = transportRequest.queryParameters; + assert.equal(queryParams?.end, 'prevToken'); + assert.equal(queryParams?.start, undefined); + }); + + it('should handle totalCount flag', () => { + const requestWithCount = new GetUUIDMembershipsRequest({ + ...defaultParameters, + include: { + totalCount: true, + }, + }); + const transportRequestWithCount = requestWithCount.request(); + const queryParamsWithCount = transportRequestWithCount.queryParameters; + assert.equal(queryParamsWithCount?.count, 'true'); + + const requestWithoutCount = new GetUUIDMembershipsRequest(defaultParameters); + const transportRequestWithoutCount = requestWithoutCount.request(); + const queryParamsWithoutCount = transportRequestWithoutCount.queryParameters; + assert.equal(queryParamsWithoutCount?.count, 'false'); + }); + }); + + describe('edge cases', () => { + it('should handle empty sort object', () => { + const request = new GetUUIDMembershipsRequest({ + ...defaultParameters, + sort: {} as any, + }); + const transportRequest = request.request(); + const queryParams = transportRequest.queryParameters; + // Empty sort object results in no sort parameter in query (undefined) + assert.equal(queryParams?.sort, undefined); + }); + + it('should handle null sort values in object', () => { + const request = new GetUUIDMembershipsRequest({ + ...defaultParameters, + sort: { updated: null, created: 'asc' } as any, + }); + const transportRequest = request.request(); + const queryParams = transportRequest.queryParameters; + const sortArray = queryParams?.sort as string[]; + assert(sortArray.includes('updated')); // null becomes just the field name + assert(sortArray.includes('created:asc')); + }); + + it('should handle very long uuid', () => { + const longUuid = 'a'.repeat(100); + const request = new GetUUIDMembershipsRequest({ + ...defaultParameters, + uuid: longUuid, + }); + const transportRequest = request.request(); + assert(transportRequest.path.includes(longUuid)); + }); + }); +}); diff --git a/test/unit/app_context/membership_objects_remove.test.ts b/test/unit/app_context/membership_objects_remove.test.ts new file mode 100644 index 000000000..8db74892b --- /dev/null +++ b/test/unit/app_context/membership_objects_remove.test.ts @@ -0,0 +1,418 @@ +/* global describe, it, beforeEach */ + +import assert from 'assert'; +import { SetUUIDMembershipsRequest } from '../../../src/core/endpoints/objects/membership/set'; +import { TransportMethod } from '../../../src/core/types/transport-request'; +import RequestOperation from '../../../src/core/constants/operations'; +import { KeySet } from '../../../src/core/types/api'; +import * as AppContext from '../../../src/core/types/api/app-context'; + +describe('SetUUIDMembershipsRequest (Remove Operation)', () => { + let defaultKeySet: KeySet; + let defaultParameters: AppContext.SetMembershipsParameters & { keySet: KeySet; type: 'delete' }; + + beforeEach(() => { + defaultKeySet = { + publishKey: 'test_publish_key', + subscribeKey: 'test_subscribe_key', + }; + + defaultParameters = { + uuid: 'test_user_uuid', + channels: ['channel1'], + type: 'delete', + keySet: defaultKeySet, + }; + }); + + describe('parameter validation', () => { + it("should return \"'uuid' cannot be empty\" when uuid is empty", () => { + const request = new SetUUIDMembershipsRequest({ + ...defaultParameters, + uuid: '', + }); + assert.equal(request.validate(), "'uuid' cannot be empty"); + }); + + it('should return "Channels cannot be empty" when channels is empty array', () => { + const request = new SetUUIDMembershipsRequest({ + ...defaultParameters, + channels: [], + }); + assert.equal(request.validate(), 'Channels cannot be empty'); + }); + + it('should return "Channels cannot be empty" when channels is null', () => { + const request = new SetUUIDMembershipsRequest({ + ...defaultParameters, + channels: null as any, + }); + assert.equal(request.validate(), 'Channels cannot be empty'); + }); + + it('should return undefined when required parameters provided', () => { + const request = new SetUUIDMembershipsRequest({ + ...defaultParameters, + uuid: 'test', + channels: ['channel1'], + }); + assert.equal(request.validate(), undefined); + }); + }); + + describe('operation type', () => { + it('should return correct operation type', () => { + const request = new SetUUIDMembershipsRequest(defaultParameters); + assert.equal(request.operation(), RequestOperation.PNSetMembershipsOperation); + }); + }); + + describe('URL construction', () => { + it('should construct correct path for remove operation', () => { + const request = new SetUUIDMembershipsRequest(defaultParameters); + const transportRequest = request.request(); + const expectedPath = `/v2/objects/${defaultKeySet.subscribeKey}/uuids/${defaultParameters.uuid}/channels`; + assert.equal(transportRequest.path, expectedPath); + }); + }); + + describe('HTTP method', () => { + it('should use PATCH method', () => { + const request = new SetUUIDMembershipsRequest(defaultParameters); + const transportRequest = request.request(); + assert.equal(transportRequest.method, TransportMethod.PATCH); + }); + }); + + describe('headers', () => { + it('should set correct Content-Type header', () => { + const request = new SetUUIDMembershipsRequest(defaultParameters); + const transportRequest = request.request(); + assert.equal(transportRequest.headers?.['Content-Type'], 'application/json'); + }); + }); + + describe('request body', () => { + it('should create proper body for remove operation', () => { + const request = new SetUUIDMembershipsRequest({ + ...defaultParameters, + channels: ['channel1', 'channel2'], + type: 'delete', + }); + const transportRequest = request.request(); + const body = JSON.parse(transportRequest.body as string); + + const expectedBody = { + delete: [ + { channel: { id: 'channel1' } }, + { channel: { id: 'channel2' } } + ] + }; + + assert.deepEqual(body, expectedBody); + }); + + it('should create proper body for object channels in remove operation', () => { + const request = new SetUUIDMembershipsRequest({ + ...defaultParameters, + channels: [{ + id: 'channel1', + custom: { role: 'admin' }, + status: 'active', + type: 'member' + }], + type: 'delete', + }); + const transportRequest = request.request(); + const body = JSON.parse(transportRequest.body as string); + + const expectedBody = { + delete: [{ + channel: { id: 'channel1' }, + status: 'active', + type: 'member', + custom: { role: 'admin' } + }] + }; + + assert.deepEqual(body, expectedBody); + }); + + it('should handle mixed string and object channels for delete', () => { + const request = new SetUUIDMembershipsRequest({ + ...defaultParameters, + channels: [ + 'channel1', + { id: 'channel2', status: 'active' } + ], + type: 'delete', + }); + const transportRequest = request.request(); + const body = JSON.parse(transportRequest.body as string); + + const expectedBody = { + delete: [ + { channel: { id: 'channel1' } }, + { + channel: { id: 'channel2' }, + status: 'active' + // undefined values are omitted by JSON.stringify + } + ] + }; + + assert.deepEqual(body, expectedBody); + }); + }); + + describe('backward compatibility', () => { + it('should map userId to uuid parameter', () => { + const request = new SetUUIDMembershipsRequest({ + keySet: defaultKeySet, + userId: 'test-user-id', // Using userId instead of uuid + channels: ['channel1'], + type: 'delete', + // uuid is not provided + } as any); + + // The request should be valid as userId gets mapped to uuid internally + assert.equal(request.validate(), undefined); + + const transportRequest = request.request(); + const expectedPath = `/v2/objects/${defaultKeySet.subscribeKey}/uuids/test-user-id/channels`; + assert.equal(transportRequest.path, expectedPath); + }); + }); + + describe('remove operation specifics', () => { + it('should use delete key in request body', () => { + const request = new SetUUIDMembershipsRequest({ + ...defaultParameters, + channels: ['channel1', 'channel2', 'channel3'], + type: 'delete', + }); + + const transportRequest = request.request(); + const body = JSON.parse(transportRequest.body as string); + + assert(body.hasOwnProperty('delete')); + assert(!body.hasOwnProperty('set')); + assert.equal(body.delete.length, 3); + }); + + it('should handle single channel removal', () => { + const request = new SetUUIDMembershipsRequest({ + ...defaultParameters, + channels: ['single-channel'], + type: 'delete', + }); + + const transportRequest = request.request(); + const body = JSON.parse(transportRequest.body as string); + + assert.equal(body.delete.length, 1); + assert.equal(body.delete[0].channel.id, 'single-channel'); + }); + + it('should handle bulk channel removal', () => { + const channels = Array.from({ length: 50 }, (_, i) => `channel_${i}`); + const request = new SetUUIDMembershipsRequest({ + ...defaultParameters, + channels, + type: 'delete', + }); + + const transportRequest = request.request(); + const body = JSON.parse(transportRequest.body as string); + + assert.equal(body.delete.length, 50); + channels.forEach((channel, index) => { + assert.equal(body.delete[index].channel.id, channel); + }); + }); + }); + + describe('query parameters for delete operation', () => { + it('should include default include flags', () => { + const request = new SetUUIDMembershipsRequest(defaultParameters); + const transportRequest = request.request(); + const queryParams = transportRequest.queryParameters; + + // Default include flags as per source code + const includeParam = queryParams?.include as string; + assert(includeParam?.includes('channel.status')); + assert(includeParam?.includes('channel.type')); + assert(includeParam?.includes('status')); + }); + + it('should handle pagination parameters with delete', () => { + const request = new SetUUIDMembershipsRequest({ + ...defaultParameters, + page: { + next: 'nextToken', + }, + }); + const transportRequest = request.request(); + const queryParams = transportRequest.queryParameters; + assert.equal(queryParams?.start, 'nextToken'); + }); + + it('should handle include options with delete', () => { + const request = new SetUUIDMembershipsRequest({ + ...defaultParameters, + include: { + customFields: true, + channelFields: true, + totalCount: true, + }, + }); + const transportRequest = request.request(); + const queryParams = transportRequest.queryParameters; + + const includeString = queryParams?.include as string; + assert(includeString.includes('custom')); + assert(includeString.includes('channel')); + assert.equal(queryParams?.count, 'true'); + }); + }); + + describe('channel removal edge cases', () => { + it('should handle channels with special characters', () => { + const specialChannels = [ + 'channel#1@domain.com', + 'channel with spaces', + 'channel/with/slashes', + ]; + + const request = new SetUUIDMembershipsRequest({ + ...defaultParameters, + channels: specialChannels, + type: 'delete', + }); + + const transportRequest = request.request(); + const body = JSON.parse(transportRequest.body as string); + + specialChannels.forEach((channelId, index) => { + assert.equal(body.delete[index].channel.id, channelId); + }); + }); + + it('should handle empty custom objects in remove', () => { + const request = new SetUUIDMembershipsRequest({ + ...defaultParameters, + channels: [{ + id: 'channel1', + custom: {}, + status: 'inactive' + }], + type: 'delete', + }); + + const transportRequest = request.request(); + const body = JSON.parse(transportRequest.body as string); + + assert.deepEqual(body.delete[0].custom, {}); + assert.equal(body.delete[0].status, 'inactive'); + }); + + it('should handle null values in channel objects for delete', () => { + const request = new SetUUIDMembershipsRequest({ + ...defaultParameters, + channels: [{ + id: 'channel1', + custom: null as any, + status: null as any + }], + type: 'delete', + }); + + const transportRequest = request.request(); + const body = JSON.parse(transportRequest.body as string); + + assert.equal(body.delete[0].custom, null); + assert.equal(body.delete[0].status, null); + }); + }); + + describe('comprehensive validation tests', () => { + it('should handle uuid with special characters for delete', () => { + const specialUuid = 'user@domain.com#123'; + const request = new SetUUIDMembershipsRequest({ + ...defaultParameters, + uuid: specialUuid, + }); + + const transportRequest = request.request(); + assert(transportRequest.path.includes(encodeURIComponent(specialUuid))); + }); + + it('should maintain request isolation between set and delete', () => { + const setRequest = new SetUUIDMembershipsRequest({ + ...defaultParameters, + type: 'set', + }); + + const deleteRequest = new SetUUIDMembershipsRequest({ + ...defaultParameters, + type: 'delete', + }); + + const setBody = JSON.parse(setRequest.request().body as string); + const deleteBody = JSON.parse(deleteRequest.request().body as string); + + assert(setBody.hasOwnProperty('set')); + assert(!setBody.hasOwnProperty('delete')); + assert(deleteBody.hasOwnProperty('delete')); + assert(!deleteBody.hasOwnProperty('set')); + }); + }); + + describe('large dataset handling', () => { + it('should handle removal of many channels', () => { + const manyChannels = Array.from({ length: 100 }, (_, i) => ({ + id: `channel_${i}`, + status: i % 2 === 0 ? 'active' : 'inactive', + custom: { index: i } + })); + + const request = new SetUUIDMembershipsRequest({ + ...defaultParameters, + channels: manyChannels, + type: 'delete', + }); + + const transportRequest = request.request(); + const body = JSON.parse(transportRequest.body as string); + + assert.equal(body.delete.length, 100); + assert.equal(body.delete[0].channel.id, 'channel_0'); + assert.equal(body.delete[99].channel.id, 'channel_99'); + assert.deepEqual(body.delete[50].custom, { index: 50 }); + }); + }); + + describe('filter and sort parameters for delete', () => { + it('should handle filter parameter with delete operation', () => { + const request = new SetUUIDMembershipsRequest({ + ...defaultParameters, + filter: 'channel.status == "active"', + }); + const transportRequest = request.request(); + const queryParams = transportRequest.queryParameters; + assert.equal(queryParams?.filter, 'channel.status == "active"'); + }); + + it('should handle sort parameter with delete operation', () => { + const request = new SetUUIDMembershipsRequest({ + ...defaultParameters, + sort: { 'channel.updated': 'desc' }, + }); + const transportRequest = request.request(); + const queryParams = transportRequest.queryParameters; + const sortArray = queryParams?.sort as string[]; + assert(Array.isArray(sortArray)); + assert(sortArray.includes('channel.updated:desc')); + }); + }); +}); diff --git a/test/unit/app_context/membership_objects_set.test.ts b/test/unit/app_context/membership_objects_set.test.ts new file mode 100644 index 000000000..34bc5de2e --- /dev/null +++ b/test/unit/app_context/membership_objects_set.test.ts @@ -0,0 +1,401 @@ +/* global describe, it, beforeEach */ + +import assert from 'assert'; +import { SetUUIDMembershipsRequest } from '../../../src/core/endpoints/objects/membership/set'; +import { TransportMethod } from '../../../src/core/types/transport-request'; +import RequestOperation from '../../../src/core/constants/operations'; +import { KeySet } from '../../../src/core/types/api'; +import * as AppContext from '../../../src/core/types/api/app-context'; + +describe('SetUUIDMembershipsRequest', () => { + let defaultKeySet: KeySet; + let defaultParameters: AppContext.SetMembershipsParameters & { keySet: KeySet; type: 'set' }; + + beforeEach(() => { + defaultKeySet = { + publishKey: 'test_publish_key', + subscribeKey: 'test_subscribe_key', + }; + + defaultParameters = { + uuid: 'test_user_uuid', + channels: ['channel1'], + type: 'set', + keySet: defaultKeySet, + }; + }); + + describe('parameter validation', () => { + it("should return \"'uuid' cannot be empty\" when uuid is empty", () => { + const request = new SetUUIDMembershipsRequest({ + ...defaultParameters, + uuid: '', + }); + assert.equal(request.validate(), "'uuid' cannot be empty"); + }); + + it('should return "Channels cannot be empty" when channels is empty array', () => { + const request = new SetUUIDMembershipsRequest({ + ...defaultParameters, + channels: [], + }); + assert.equal(request.validate(), 'Channels cannot be empty'); + }); + + it('should return "Channels cannot be empty" when channels is null', () => { + const request = new SetUUIDMembershipsRequest({ + ...defaultParameters, + channels: null as any, + }); + assert.equal(request.validate(), 'Channels cannot be empty'); + }); + + it('should return undefined when required parameters provided', () => { + const request = new SetUUIDMembershipsRequest({ + ...defaultParameters, + uuid: 'test', + channels: ['channel1'], + }); + assert.equal(request.validate(), undefined); + }); + }); + + describe('operation type', () => { + it('should return correct operation type', () => { + const request = new SetUUIDMembershipsRequest(defaultParameters); + assert.equal(request.operation(), RequestOperation.PNSetMembershipsOperation); + }); + }); + + describe('URL construction', () => { + it('should construct correct path for set operation', () => { + const request = new SetUUIDMembershipsRequest(defaultParameters); + const transportRequest = request.request(); + const expectedPath = `/v2/objects/${defaultKeySet.subscribeKey}/uuids/${defaultParameters.uuid}/channels`; + assert.equal(transportRequest.path, expectedPath); + }); + }); + + describe('HTTP method', () => { + it('should use PATCH method', () => { + const request = new SetUUIDMembershipsRequest(defaultParameters); + const transportRequest = request.request(); + assert.equal(transportRequest.method, TransportMethod.PATCH); + }); + }); + + describe('headers', () => { + it('should set correct Content-Type header', () => { + const request = new SetUUIDMembershipsRequest(defaultParameters); + const transportRequest = request.request(); + assert.equal(transportRequest.headers?.['Content-Type'], 'application/json'); + }); + }); + + describe('request body', () => { + it('should create proper body for string channels', () => { + const request = new SetUUIDMembershipsRequest({ + ...defaultParameters, + channels: ['channel1', 'channel2'], + type: 'set', + }); + const transportRequest = request.request(); + const body = JSON.parse(transportRequest.body as string); + + const expectedBody = { + set: [ + { channel: { id: 'channel1' } }, + { channel: { id: 'channel2' } } + ] + }; + + assert.deepEqual(body, expectedBody); + }); + + it('should create proper body for object channels', () => { + const request = new SetUUIDMembershipsRequest({ + ...defaultParameters, + channels: [{ + id: 'channel1', + custom: { role: 'admin' }, + status: 'active', + type: 'member' + }], + type: 'set', + }); + const transportRequest = request.request(); + const body = JSON.parse(transportRequest.body as string); + + const expectedBody = { + set: [{ + channel: { id: 'channel1' }, + status: 'active', + type: 'member', + custom: { role: 'admin' } + }] + }; + + assert.deepEqual(body, expectedBody); + }); + + it('should handle mixed string and object channels', () => { + const request = new SetUUIDMembershipsRequest({ + ...defaultParameters, + channels: [ + 'channel1', + { id: 'channel2', status: 'active' } + ], + type: 'set', + }); + const transportRequest = request.request(); + const body = JSON.parse(transportRequest.body as string); + + const expectedBody = { + set: [ + { channel: { id: 'channel1' } }, + { + channel: { id: 'channel2' }, + status: 'active' + // undefined values are omitted by JSON.stringify + } + ] + }; + + assert.deepEqual(body, expectedBody); + }); + }); + + describe('query parameters', () => { + it('should include default include flags in query', () => { + const request = new SetUUIDMembershipsRequest(defaultParameters); + const transportRequest = request.request(); + const queryParams = transportRequest.queryParameters; + + // Default include flags as per source code + const includeParam = queryParams?.include as string; + assert(includeParam?.includes('channel.status')); + assert(includeParam?.includes('channel.type')); + assert(includeParam?.includes('status')); + }); + + it('should handle all include options', () => { + const request = new SetUUIDMembershipsRequest({ + ...defaultParameters, + include: { + customFields: true, + totalCount: true, + statusField: true, + typeField: true, + channelFields: true, + customChannelFields: true, + channelStatusField: true, + channelTypeField: true, + }, + }); + const transportRequest = request.request(); + const queryParams = transportRequest.queryParameters; + + // Should contain all include flags + const includeString = queryParams?.include as string; + assert(includeString.includes('status')); + assert(includeString.includes('type')); + assert(includeString.includes('custom')); + assert(includeString.includes('channel')); + assert(includeString.includes('channel.status')); + assert(includeString.includes('channel.type')); + assert(includeString.includes('channel.custom')); + }); + }); + + describe('backward compatibility', () => { + it('should map userId to uuid parameter', () => { + const request = new SetUUIDMembershipsRequest({ + keySet: defaultKeySet, + userId: 'test-user-id', // Using userId instead of uuid + channels: ['channel1'], + type: 'set', + // uuid is not provided + } as any); + + // The request should be valid as userId gets mapped to uuid internally + assert.equal(request.validate(), undefined); + + const transportRequest = request.request(); + const expectedPath = `/v2/objects/${defaultKeySet.subscribeKey}/uuids/test-user-id/channels`; + assert.equal(transportRequest.path, expectedPath); + }); + }); + + describe('channel object structure', () => { + it('should preserve custom data in channel objects', () => { + const customData = { + role: 'moderator', + level: 5, + isActive: true + }; + + const request = new SetUUIDMembershipsRequest({ + ...defaultParameters, + channels: [{ + id: 'channel1', + custom: customData, + status: 'active', + type: 'premium' + }], + }); + + const transportRequest = request.request(); + const body = JSON.parse(transportRequest.body as string); + + assert.deepEqual(body.set[0].custom, customData); + assert.equal(body.set[0].status, 'active'); + assert.equal(body.set[0].type, 'premium'); + }); + + it('should handle partial channel objects', () => { + const request = new SetUUIDMembershipsRequest({ + ...defaultParameters, + channels: [ + { id: 'channel1' }, // minimal object + { id: 'channel2', status: 'inactive' }, // with status only + { id: 'channel3', custom: { note: 'test' } } // with custom only + ], + }); + + const transportRequest = request.request(); + const body = JSON.parse(transportRequest.body as string); + + // First channel - minimal (undefined values omitted by JSON.stringify) + assert.deepEqual(body.set[0], { + channel: { id: 'channel1' } + }); + + // Second channel - with status + assert.deepEqual(body.set[1], { + channel: { id: 'channel2' }, + status: 'inactive' + }); + + // Third channel - with custom + assert.deepEqual(body.set[2], { + channel: { id: 'channel3' }, + custom: { note: 'test' } + }); + }); + }); + + describe('large data handling', () => { + it('should handle large channel lists', () => { + const channels = Array.from({ length: 100 }, (_, i) => `channel_${i}`); + const request = new SetUUIDMembershipsRequest({ + ...defaultParameters, + channels, + }); + + const transportRequest = request.request(); + const body = JSON.parse(transportRequest.body as string); + + assert.equal(body.set.length, 100); + assert.equal(body.set[0].channel.id, 'channel_0'); + assert.equal(body.set[99].channel.id, 'channel_99'); + }); + + it('should handle large custom data objects', () => { + const largeCustomData = { + description: 'x'.repeat(1000), + category: 'premium', + priority: 10, + isEnabled: true + }; + + const request = new SetUUIDMembershipsRequest({ + ...defaultParameters, + channels: [{ + id: 'channel1', + custom: largeCustomData + }], + }); + + const transportRequest = request.request(); + const body = JSON.parse(transportRequest.body as string); + + assert.deepEqual(body.set[0].custom, largeCustomData); + }); + }); + + describe('query parameter combinations', () => { + it('should handle pagination parameters', () => { + const request = new SetUUIDMembershipsRequest({ + ...defaultParameters, + page: { + next: 'nextToken', + prev: 'prevToken', + }, + }); + const transportRequest = request.request(); + const queryParams = transportRequest.queryParameters; + // Both start and end can be present if both next and prev are provided + assert.equal(queryParams?.start, 'nextToken'); + assert.equal(queryParams?.end, 'prevToken'); + }); + + it('should handle filter and sort parameters', () => { + const request = new SetUUIDMembershipsRequest({ + ...defaultParameters, + filter: 'channel.name like "*test*"', + sort: { 'channel.updated': 'desc' }, + }); + const transportRequest = request.request(); + const queryParams = transportRequest.queryParameters; + assert.equal(queryParams?.filter, 'channel.name like "*test*"'); + const sortArray = queryParams?.sort as string[]; + assert(Array.isArray(sortArray)); + assert(sortArray.includes('channel.updated:desc')); + }); + + it('should handle limit parameter', () => { + const request = new SetUUIDMembershipsRequest({ + ...defaultParameters, + limit: 25, + }); + const transportRequest = request.request(); + const queryParams = transportRequest.queryParameters; + assert.equal(queryParams?.limit, 25); + }); + }); + + describe('edge cases', () => { + it('should handle channels with special characters in id', () => { + const specialChannels = [ + 'channel#1@domain.com', + 'channel with spaces', + 'channel/with/slashes', + 'channel?with=query¶ms' + ]; + + const request = new SetUUIDMembershipsRequest({ + ...defaultParameters, + channels: specialChannels, + }); + + const transportRequest = request.request(); + const body = JSON.parse(transportRequest.body as string); + + specialChannels.forEach((channelId, index) => { + assert.equal(body.set[index].channel.id, channelId); + }); + }); + + it('should handle uuid with special characters', () => { + const specialUuid = 'user#1@domain.com'; + const request = new SetUUIDMembershipsRequest({ + ...defaultParameters, + uuid: specialUuid, + }); + + const transportRequest = request.request(); + // Should URL encode the uuid in path + assert(transportRequest.path.includes(encodeURIComponent(specialUuid))); + }); + }); +}); diff --git a/test/unit/chanel_groups/channel_groups_delete_group.test.ts b/test/unit/chanel_groups/channel_groups_delete_group.test.ts new file mode 100644 index 000000000..4874f812a --- /dev/null +++ b/test/unit/chanel_groups/channel_groups_delete_group.test.ts @@ -0,0 +1,270 @@ +/* global describe, beforeEach, it */ + +import assert from 'assert'; +import { DeleteChannelGroupRequest } from '../../../src/core/endpoints/channel_groups/delete_group'; +import RequestOperation from '../../../src/core/constants/operations'; + +describe('DeleteChannelGroupRequest', () => { + let request: DeleteChannelGroupRequest; + const keySet = { + subscribeKey: 'mySubKey', + publishKey: 'myPublishKey', + secretKey: 'mySecretKey' + }; + + describe('Parameter validation', () => { + it('should return "Missing Subscribe Key" when subscribeKey is missing', () => { + const requestWithoutSubKey = new DeleteChannelGroupRequest({ + keySet: { subscribeKey: '', publishKey: 'myPublishKey', secretKey: 'mySecretKey' }, + channelGroup: 'test-group' + }); + + const result = requestWithoutSubKey.validate(); + assert.strictEqual(result, 'Missing Subscribe Key'); + }); + + it('should return "Missing Channel Group" when channelGroup is missing', () => { + const requestWithoutChannelGroup = new DeleteChannelGroupRequest({ + keySet, + channelGroup: '' + }); + + const result = requestWithoutChannelGroup.validate(); + assert.strictEqual(result, 'Missing Channel Group'); + }); + + it('should return undefined when all required parameters are provided', () => { + request = new DeleteChannelGroupRequest({ + keySet, + channelGroup: 'test-group' + }); + + const result = request.validate(); + assert.strictEqual(result, undefined); + }); + }); + + describe('Operation type', () => { + beforeEach(() => { + request = new DeleteChannelGroupRequest({ + keySet, + channelGroup: 'test-group' + }); + }); + + it('should return correct operation type', () => { + const operation = request.operation(); + assert.strictEqual(operation, RequestOperation.PNRemoveGroupOperation); + }); + }); + + describe('URL path construction', () => { + beforeEach(() => { + request = new DeleteChannelGroupRequest({ + keySet, + channelGroup: 'test-group' + }); + }); + + it('should construct correct REST endpoint path with /remove suffix', () => { + const path = (request as any).path; + const expectedPathComponents = [ + '/v1/channel-registration', + 'sub-key', + keySet.subscribeKey, + 'channel-group', + 'test-group', + 'remove' + ]; + + // Split path and verify components + const pathComponents = path.split('/').filter((component: string) => component !== ''); + // Expected path: /v1/channel-registration/sub-key/mySubKey/channel-group/test-group/remove + // Components: ['v1', 'channel-registration', 'sub-key', 'mySubKey', 'channel-group', 'test-group', 'remove'] + assert.strictEqual(pathComponents.length, 7); + assert.strictEqual(pathComponents[0], 'v1'); + assert.strictEqual(pathComponents[1], 'channel-registration'); + assert.strictEqual(pathComponents[2], 'sub-key'); + assert.strictEqual(pathComponents[3], keySet.subscribeKey); + assert.strictEqual(pathComponents[4], 'channel-group'); + assert.strictEqual(pathComponents[5], 'test-group'); + assert.strictEqual(pathComponents[6], 'remove'); + + // Verify the exact path structure + assert.strictEqual(path, `/v1/channel-registration/sub-key/${keySet.subscribeKey}/channel-group/test-group/remove`); + }); + }); + + describe('Response parsing', () => { + beforeEach(() => { + request = new DeleteChannelGroupRequest({ + keySet, + channelGroup: 'test-group' + }); + }); + + it('should parse empty service response correctly', async () => { + const mockResponse = { + status: 200, + url: '', + headers: {}, + body: new ArrayBuffer(0) + }; + + // Mock the deserializeResponse method to return expected service response + (request as any).deserializeResponse = () => ({ + status: 200, + message: 'OK', + service: 'ChannelGroups', + error: false + }); + + const result = await request.parse(mockResponse); + assert.deepStrictEqual(result, {}); + }); + }); + + describe('Channel group encoding', () => { + it('should handle channel group names with special characters', () => { + const specialGroupRequest = new DeleteChannelGroupRequest({ + keySet, + channelGroup: 'test group with spaces' + }); + + const path = (specialGroupRequest as any).path; + + // Split path and verify encoded channel group name + const pathComponents = path.split('/').filter((component: string) => component !== ''); + assert.strictEqual(pathComponents[5], 'test%20group%20with%20spaces'); + assert.strictEqual(pathComponents[6], 'remove'); + }); + + it('should handle channel group names with unicode characters', () => { + const unicodeGroupRequest = new DeleteChannelGroupRequest({ + keySet, + channelGroup: 'test-group-éñ中文🚀' + }); + + const path = (unicodeGroupRequest as any).path; + + // Verify the unicode characters are properly encoded in the path + assert(path.includes('test-group-%C3%A9%C3%B1%E4%B8%AD%E6%96%87%F0%9F%9A%80')); + assert(path.endsWith('/remove')); + }); + + it('should handle channel group names with symbols', () => { + const symbolGroupRequest = new DeleteChannelGroupRequest({ + keySet, + channelGroup: 'test-group!@#$%^&*()' + }); + + const path = (symbolGroupRequest as any).path; + + // Verify the symbols are properly URL encoded in the path + assert(path.includes('test-group%21%40%23%24%25%5E%26%2A%28%29')); + assert(path.endsWith('/remove')); + }); + }); + + describe('Non-existent group', () => { + it('should handle deleting non-existent group gracefully', () => { + const nonExistentGroupRequest = new DeleteChannelGroupRequest({ + keySet, + channelGroup: 'non-existent-group' + }); + + // Validation should succeed even for non-existent groups + const result = nonExistentGroupRequest.validate(); + assert.strictEqual(result, undefined); + + // Path should be constructed correctly + const path = (nonExistentGroupRequest as any).path; + assert.strictEqual(path, `/v1/channel-registration/sub-key/${keySet.subscribeKey}/channel-group/non-existent-group/remove`); + }); + }); + + describe('Path components validation', () => { + beforeEach(() => { + request = new DeleteChannelGroupRequest({ + keySet, + channelGroup: 'test-group' + }); + }); + + it('should have exactly the required path components in correct order', () => { + const path = (request as any).path; + const pathSegments = path.split('/').filter((segment: string) => segment !== ''); + + assert.strictEqual(pathSegments.length, 7); + assert.strictEqual(pathSegments[0], 'v1'); + assert.strictEqual(pathSegments[1], 'channel-registration'); + assert.strictEqual(pathSegments[2], 'sub-key'); + assert.strictEqual(pathSegments[3], keySet.subscribeKey); + assert.strictEqual(pathSegments[4], 'channel-group'); + assert.strictEqual(pathSegments[5], 'test-group'); + assert.strictEqual(pathSegments[6], 'remove'); + }); + + it('should construct path correctly with different subscribeKey and channelGroup values', () => { + const differentKeySet = { + subscribeKey: 'different-sub-key-123', + publishKey: 'myPublishKey', + secretKey: 'mySecretKey' + }; + + const requestWithDifferentValues = new DeleteChannelGroupRequest({ + keySet: differentKeySet, + channelGroup: 'different-channel-group' + }); + + const path = (requestWithDifferentValues as any).path; + assert.strictEqual(path, `/v1/channel-registration/sub-key/${differentKeySet.subscribeKey}/channel-group/different-channel-group/remove`); + }); + }); + + describe('Query parameters', () => { + beforeEach(() => { + request = new DeleteChannelGroupRequest({ + keySet, + channelGroup: 'test-group' + }); + }); + + it('should not have any query parameters for delete group request', () => { + const queryParams = (request as any).queryParameters; + + // DeleteChannelGroupRequest doesn't override queryParameters, so it should be undefined or empty + assert(queryParams === undefined || Object.keys(queryParams).length === 0); + }); + }); + + describe('Edge cases', () => { + it('should handle very long channel group names', () => { + const longGroupName = 'a'.repeat(100); + const longGroupRequest = new DeleteChannelGroupRequest({ + keySet, + channelGroup: longGroupName + }); + + const result = longGroupRequest.validate(); + assert.strictEqual(result, undefined); + + const path = (longGroupRequest as any).path; + assert(path.includes(longGroupName)); + assert(path.endsWith('/remove')); + }); + + it('should handle channel group names with forward slashes', () => { + const slashGroupRequest = new DeleteChannelGroupRequest({ + keySet, + channelGroup: 'group/with/slashes' + }); + + const path = (slashGroupRequest as any).path; + + // Forward slashes should be encoded in URL + assert(path.includes('group%2Fwith%2Fslashes')); + assert(path.endsWith('/remove')); + }); + }); +}); diff --git a/test/unit/chanel_groups/channel_groups_list_channels.test.ts b/test/unit/chanel_groups/channel_groups_list_channels.test.ts new file mode 100644 index 000000000..320728246 --- /dev/null +++ b/test/unit/chanel_groups/channel_groups_list_channels.test.ts @@ -0,0 +1,284 @@ +/* global describe, beforeEach, it */ + +import assert from 'assert'; +import { ListChannelGroupChannels } from '../../../src/core/endpoints/channel_groups/list_channels'; +import RequestOperation from '../../../src/core/constants/operations'; + +describe('ListChannelGroupChannels', () => { + let request: ListChannelGroupChannels; + const keySet = { + subscribeKey: 'mySubKey', + publishKey: 'myPublishKey', + secretKey: 'mySecretKey' + }; + + describe('Parameter validation', () => { + it('should return "Missing Subscribe Key" when subscribeKey is missing', () => { + const requestWithoutSubKey = new ListChannelGroupChannels({ + keySet: { subscribeKey: '', publishKey: 'myPublishKey', secretKey: 'mySecretKey' }, + channelGroup: 'test-group' + }); + + const result = requestWithoutSubKey.validate(); + assert.strictEqual(result, 'Missing Subscribe Key'); + }); + + it('should return "Missing Channel Group" when channelGroup is missing', () => { + const requestWithoutChannelGroup = new ListChannelGroupChannels({ + keySet, + channelGroup: '' + }); + + const result = requestWithoutChannelGroup.validate(); + assert.strictEqual(result, 'Missing Channel Group'); + }); + + it('should return undefined when all required parameters are provided', () => { + request = new ListChannelGroupChannels({ + keySet, + channelGroup: 'test-group' + }); + + const result = request.validate(); + assert.strictEqual(result, undefined); + }); + }); + + describe('Operation type', () => { + beforeEach(() => { + request = new ListChannelGroupChannels({ + keySet, + channelGroup: 'test-group' + }); + }); + + it('should return correct operation type', () => { + const operation = request.operation(); + assert.strictEqual(operation, RequestOperation.PNChannelsForGroupOperation); + }); + }); + + describe('URL path construction', () => { + beforeEach(() => { + request = new ListChannelGroupChannels({ + keySet, + channelGroup: 'test-group' + }); + }); + + it('should construct correct REST endpoint path', () => { + const path = (request as any).path; + const expectedPathComponents = [ + '/v1/channel-registration', + 'sub-key', + keySet.subscribeKey, + 'channel-group', + 'test-group' + ]; + + // Split path and verify components + const pathComponents = path.split('/').filter((component: string) => component !== ''); + // Expected path: /v1/channel-registration/sub-key/mySubKey/channel-group/test-group + // Components: ['v1', 'channel-registration', 'sub-key', 'mySubKey', 'channel-group', 'test-group'] + assert.strictEqual(pathComponents.length, 6); + assert.strictEqual(pathComponents[0], 'v1'); + assert.strictEqual(pathComponents[1], 'channel-registration'); + assert.strictEqual(pathComponents[2], 'sub-key'); + assert.strictEqual(pathComponents[3], keySet.subscribeKey); + assert.strictEqual(pathComponents[4], 'channel-group'); + assert.strictEqual(pathComponents[5], 'test-group'); + }); + }); + + describe('Response parsing with channels', () => { + beforeEach(() => { + request = new ListChannelGroupChannels({ + keySet, + channelGroup: 'test-group' + }); + }); + + it('should parse service response with channels correctly', async () => { + const mockResponse = { + status: 200, + url: '', + headers: {}, + body: new ArrayBuffer(0) + }; + + // Mock the deserializeResponse method to return expected service response + (request as any).deserializeResponse = () => ({ + status: 200, + message: 'OK', + service: 'ChannelGroups', + error: false, + payload: { + channels: ['channel1', 'channel2'] + } + }); + + const result = await request.parse(mockResponse); + assert.deepStrictEqual(result, { channels: ['channel1', 'channel2'] }); + }); + }); + + describe('Response parsing empty channels', () => { + beforeEach(() => { + request = new ListChannelGroupChannels({ + keySet, + channelGroup: 'test-group' + }); + }); + + it('should parse service response with empty channels array correctly', async () => { + const mockResponse = { + status: 200, + url: '', + headers: {}, + body: new ArrayBuffer(0) + }; + + // Mock the deserializeResponse method to return expected service response + (request as any).deserializeResponse = () => ({ + status: 200, + message: 'OK', + service: 'ChannelGroups', + error: false, + payload: { + channels: [] + } + }); + + const result = await request.parse(mockResponse); + assert.deepStrictEqual(result, { channels: [] }); + }); + }); + + describe('Channel group encoding', () => { + it('should handle channel group names with special characters', () => { + const specialGroupRequest = new ListChannelGroupChannels({ + keySet, + channelGroup: 'test group with spaces' + }); + + const path = (specialGroupRequest as any).path; + + // Split path and verify encoded channel group name + const pathComponents = path.split('/').filter((component: string) => component !== ''); + assert.strictEqual(pathComponents[pathComponents.length - 1], 'test%20group%20with%20spaces'); + }); + + it('should handle channel group names with unicode characters', () => { + const unicodeGroupRequest = new ListChannelGroupChannels({ + keySet, + channelGroup: 'test-group-éñ中文🚀' + }); + + const path = (unicodeGroupRequest as any).path; + + // Verify the unicode characters are properly encoded in the path + assert(path.includes('test-group-éñ中文🚀') || path.includes('test-group-%C3%A9%C3%B1%E4%B8%AD%E6%96%87%F0%9F%9A%80')); + }); + + it('should handle channel group names with symbols', () => { + const symbolGroupRequest = new ListChannelGroupChannels({ + keySet, + channelGroup: 'test-group!@#$%^&*()' + }); + + const path = (symbolGroupRequest as any).path; + + // Verify the symbols are properly URL encoded in the path + assert(path.includes('test-group%21%40%23%24%25%5E%26%2A%28%29')); + }); + }); + + describe('Large channel list parsing', () => { + beforeEach(() => { + request = new ListChannelGroupChannels({ + keySet, + channelGroup: 'test-group' + }); + }); + + it('should parse response with many channels correctly', async () => { + const mockResponse = { + status: 200, + url: '', + headers: {}, + body: new ArrayBuffer(0) + }; + + // Create a large array of channels + const largeChannelList = Array.from({ length: 100 }, (_, i) => `channel${i + 1}`); + + // Mock the deserializeResponse method to return expected service response + (request as any).deserializeResponse = () => ({ + status: 200, + message: 'OK', + service: 'ChannelGroups', + error: false, + payload: { + channels: largeChannelList + } + }); + + const result = await request.parse(mockResponse); + + // Verify all channels in payload.channels array are returned + assert.deepStrictEqual(result, { channels: largeChannelList }); + assert.strictEqual(result.channels.length, 100); + assert.strictEqual(result.channels[0], 'channel1'); + assert.strictEqual(result.channels[99], 'channel100'); + }); + + it('should handle response with mixed channel name types', async () => { + const mockResponse = { + status: 200, + url: '', + headers: {}, + body: new ArrayBuffer(0) + }; + + const mixedChannels = [ + 'simple-channel', + 'channel with spaces', + 'channel/with/slashes', + 'channel-éñ', + 'channel-中文', + 'channel-🚀', + 'channel!@#$%^&*()' + ]; + + // Mock the deserializeResponse method to return expected service response + (request as any).deserializeResponse = () => ({ + status: 200, + message: 'OK', + service: 'ChannelGroups', + error: false, + payload: { + channels: mixedChannels + } + }); + + const result = await request.parse(mockResponse); + assert.deepStrictEqual(result, { channels: mixedChannels }); + }); + }); + + describe('Query parameters', () => { + beforeEach(() => { + request = new ListChannelGroupChannels({ + keySet, + channelGroup: 'test-group' + }); + }); + + it('should not have any query parameters for list channels request', () => { + const queryParams = (request as any).queryParameters; + + // ListChannelGroupChannels doesn't override queryParameters, so it should be undefined or empty + assert(queryParams === undefined || Object.keys(queryParams).length === 0); + }); + }); +}); diff --git a/test/unit/chanel_groups/channel_groups_list_groups.test.ts b/test/unit/chanel_groups/channel_groups_list_groups.test.ts new file mode 100644 index 000000000..10208ea29 --- /dev/null +++ b/test/unit/chanel_groups/channel_groups_list_groups.test.ts @@ -0,0 +1,292 @@ +/* global describe, beforeEach, it */ + +import assert from 'assert'; +import { ListChannelGroupsRequest } from '../../../src/core/endpoints/channel_groups/list_groups'; +import RequestOperation from '../../../src/core/constants/operations'; + +describe('ListChannelGroupsRequest', () => { + let request: ListChannelGroupsRequest; + const keySet = { + subscribeKey: 'mySubKey', + publishKey: 'myPublishKey', + secretKey: 'mySecretKey' + }; + + describe('Parameter validation', () => { + it('should return "Missing Subscribe Key" when subscribeKey is missing', () => { + const requestWithoutSubKey = new ListChannelGroupsRequest({ + keySet: { subscribeKey: '', publishKey: 'myPublishKey', secretKey: 'mySecretKey' } + }); + + const result = requestWithoutSubKey.validate(); + assert.strictEqual(result, 'Missing Subscribe Key'); + }); + + it('should return undefined when valid parameters are provided', () => { + request = new ListChannelGroupsRequest({ + keySet + }); + + const result = request.validate(); + assert.strictEqual(result, undefined); + }); + }); + + describe('Operation type', () => { + beforeEach(() => { + request = new ListChannelGroupsRequest({ + keySet + }); + }); + + it('should return correct operation type', () => { + const operation = request.operation(); + assert.strictEqual(operation, RequestOperation.PNChannelGroupsOperation); + }); + }); + + describe('URL path construction', () => { + beforeEach(() => { + request = new ListChannelGroupsRequest({ + keySet + }); + }); + + it('should construct correct REST endpoint path', () => { + const path = (request as any).path; + const expectedPathComponents = [ + '/v1/channel-registration', + 'sub-key', + keySet.subscribeKey, + 'channel-group' + ]; + + // Split path and verify components + const pathComponents = path.split('/').filter((component: string) => component !== ''); + // Expected path: /v1/channel-registration/sub-key/mySubKey/channel-group + // Components: ['v1', 'channel-registration', 'sub-key', 'mySubKey', 'channel-group'] + assert.strictEqual(pathComponents.length, 5); + assert.strictEqual(pathComponents[0], 'v1'); + assert.strictEqual(pathComponents[1], 'channel-registration'); + assert.strictEqual(pathComponents[2], 'sub-key'); + assert.strictEqual(pathComponents[3], keySet.subscribeKey); + assert.strictEqual(pathComponents[4], 'channel-group'); + + // Verify the exact path structure + assert.strictEqual(path, `/v1/channel-registration/sub-key/${keySet.subscribeKey}/channel-group`); + }); + }); + + describe('Response parsing with groups', () => { + beforeEach(() => { + request = new ListChannelGroupsRequest({ + keySet + }); + }); + + it('should parse service response with groups correctly', async () => { + const mockResponse = { + status: 200, + url: '', + headers: {}, + body: new ArrayBuffer(0) + }; + + // Mock the deserializeResponse method to return expected service response + (request as any).deserializeResponse = () => ({ + status: 200, + message: 'OK', + service: 'ChannelGroups', + error: false, + payload: { + groups: ['group1', 'group2'] + } + }); + + const result = await request.parse(mockResponse); + assert.deepStrictEqual(result, { groups: ['group1', 'group2'] }); + }); + }); + + describe('Response parsing empty groups', () => { + beforeEach(() => { + request = new ListChannelGroupsRequest({ + keySet + }); + }); + + it('should parse service response with empty groups array correctly', async () => { + const mockResponse = { + status: 200, + url: '', + headers: {}, + body: new ArrayBuffer(0) + }; + + // Mock the deserializeResponse method to return expected service response + (request as any).deserializeResponse = () => ({ + status: 200, + message: 'OK', + service: 'ChannelGroups', + error: false, + payload: { + groups: [] + } + }); + + const result = await request.parse(mockResponse); + assert.deepStrictEqual(result, { groups: [] }); + }); + }); + + describe('Large groups list parsing', () => { + beforeEach(() => { + request = new ListChannelGroupsRequest({ + keySet + }); + }); + + it('should parse response with many groups correctly', async () => { + const mockResponse = { + status: 200, + url: '', + headers: {}, + body: new ArrayBuffer(0) + }; + + // Create a large array of groups + const largeGroupList = Array.from({ length: 50 }, (_, i) => `group${i + 1}`); + + // Mock the deserializeResponse method to return expected service response + (request as any).deserializeResponse = () => ({ + status: 200, + message: 'OK', + service: 'ChannelGroups', + error: false, + payload: { + groups: largeGroupList + } + }); + + const result = await request.parse(mockResponse); + + // Verify all groups in payload.groups array are returned + assert.deepStrictEqual(result, { groups: largeGroupList }); + assert.strictEqual(result.groups.length, 50); + assert.strictEqual(result.groups[0], 'group1'); + assert.strictEqual(result.groups[49], 'group50'); + }); + + it('should handle response with mixed group name types', async () => { + const mockResponse = { + status: 200, + url: '', + headers: {}, + body: new ArrayBuffer(0) + }; + + const mixedGroups = [ + 'simple-group', + 'group with spaces', + 'group/with/slashes', + 'group-éñ', + 'group-中文', + 'group-🚀', + 'group!@#$%^&*()' + ]; + + // Mock the deserializeResponse method to return expected service response + (request as any).deserializeResponse = () => ({ + status: 200, + message: 'OK', + service: 'ChannelGroups', + error: false, + payload: { + groups: mixedGroups + } + }); + + const result = await request.parse(mockResponse); + assert.deepStrictEqual(result, { groups: mixedGroups }); + }); + }); + + describe('Query parameters', () => { + beforeEach(() => { + request = new ListChannelGroupsRequest({ + keySet + }); + }); + + it('should not have any query parameters for list groups request', () => { + const queryParams = (request as any).queryParameters; + + // ListChannelGroupsRequest doesn't override queryParameters, so it should be undefined or empty + assert(queryParams === undefined || Object.keys(queryParams).length === 0); + }); + }); + + describe('Path components validation', () => { + beforeEach(() => { + request = new ListChannelGroupsRequest({ + keySet + }); + }); + + it('should have exactly the required path components in correct order', () => { + const path = (request as any).path; + const pathSegments = path.split('/').filter((segment: string) => segment !== ''); + + assert.strictEqual(pathSegments.length, 5); + assert.strictEqual(pathSegments[0], 'v1'); + assert.strictEqual(pathSegments[1], 'channel-registration'); + assert.strictEqual(pathSegments[2], 'sub-key'); + assert.strictEqual(pathSegments[3], keySet.subscribeKey); + assert.strictEqual(pathSegments[4], 'channel-group'); + }); + + it('should construct path correctly with different subscribeKey values', () => { + const differentKeySet = { + subscribeKey: 'different-sub-key-123', + publishKey: 'myPublishKey', + secretKey: 'mySecretKey' + }; + + const requestWithDifferentKey = new ListChannelGroupsRequest({ + keySet: differentKeySet + }); + + const path = (requestWithDifferentKey as any).path; + assert.strictEqual(path, `/v1/channel-registration/sub-key/${differentKeySet.subscribeKey}/channel-group`); + }); + }); + + describe('Edge cases', () => { + it('should handle null or undefined in service response', async () => { + const request = new ListChannelGroupsRequest({ + keySet + }); + + const mockResponse = { + status: 200, + url: '', + headers: {}, + body: new ArrayBuffer(0) + }; + + // Mock the deserializeResponse method to return null groups + (request as any).deserializeResponse = () => ({ + status: 200, + message: 'OK', + service: 'ChannelGroups', + error: false, + payload: { + groups: null + } + }); + + const result = await request.parse(mockResponse); + assert.deepStrictEqual(result, { groups: null }); + }); + }); +}); diff --git a/test/unit/chanel_groups/channel_groups_remove_channels.test.ts b/test/unit/chanel_groups/channel_groups_remove_channels.test.ts new file mode 100644 index 000000000..ad49df5e9 --- /dev/null +++ b/test/unit/chanel_groups/channel_groups_remove_channels.test.ts @@ -0,0 +1,226 @@ +/* global describe, beforeEach, it */ + +import assert from 'assert'; +import { RemoveChannelGroupChannelsRequest } from '../../../src/core/endpoints/channel_groups/remove_channels'; +import RequestOperation from '../../../src/core/constants/operations'; + +describe('RemoveChannelGroupChannelsRequest', () => { + let request: RemoveChannelGroupChannelsRequest; + const keySet = { + subscribeKey: 'mySubKey', + publishKey: 'myPublishKey', + secretKey: 'mySecretKey' + }; + + describe('Parameter validation', () => { + it('should return "Missing Subscribe Key" when subscribeKey is missing', () => { + const requestWithoutSubKey = new RemoveChannelGroupChannelsRequest({ + keySet: { subscribeKey: '', publishKey: 'myPublishKey', secretKey: 'mySecretKey' }, + channelGroup: 'test-group', + channels: ['channel1', 'channel2'] + }); + + const result = requestWithoutSubKey.validate(); + assert.strictEqual(result, 'Missing Subscribe Key'); + }); + + it('should return "Missing Channel Group" when channelGroup is missing', () => { + const requestWithoutChannelGroup = new RemoveChannelGroupChannelsRequest({ + keySet, + channelGroup: '', + channels: ['channel1', 'channel2'] + }); + + const result = requestWithoutChannelGroup.validate(); + assert.strictEqual(result, 'Missing Channel Group'); + }); + + it('should return "Missing channels" when channels array is missing', () => { + const requestWithoutChannels = new RemoveChannelGroupChannelsRequest({ + keySet, + channelGroup: 'test-group', + // @ts-expect-error Testing missing channels + channels: undefined + }); + + const result = requestWithoutChannels.validate(); + assert.strictEqual(result, 'Missing channels'); + }); + + it('should return undefined when all required parameters are provided', () => { + request = new RemoveChannelGroupChannelsRequest({ + keySet, + channelGroup: 'test-group', + channels: ['channel1', 'channel2'] + }); + + const result = request.validate(); + assert.strictEqual(result, undefined); + }); + }); + + describe('Operation type', () => { + beforeEach(() => { + request = new RemoveChannelGroupChannelsRequest({ + keySet, + channelGroup: 'test-group', + channels: ['channel1', 'channel2'] + }); + }); + + it('should return correct operation type', () => { + const operation = request.operation(); + assert.strictEqual(operation, RequestOperation.PNRemoveChannelsFromGroupOperation); + }); + }); + + describe('URL path construction', () => { + beforeEach(() => { + request = new RemoveChannelGroupChannelsRequest({ + keySet, + channelGroup: 'test-group', + channels: ['channel1', 'channel2'] + }); + }); + + it('should construct correct REST endpoint path', () => { + const path = (request as any).path; + const expectedPathComponents = [ + '/v1/channel-registration', + 'sub-key', + keySet.subscribeKey, + 'channel-group', + 'test-group' + ]; + + // Split path and verify components + const pathComponents = path.split('/').filter((component: string) => component !== ''); + // Expected path: /v1/channel-registration/sub-key/mySubKey/channel-group/test-group + // Components: ['v1', 'channel-registration', 'sub-key', 'mySubKey', 'channel-group', 'test-group'] + assert.strictEqual(pathComponents.length, 6); + assert.strictEqual(pathComponents[0], 'v1'); + assert.strictEqual(pathComponents[1], 'channel-registration'); + assert.strictEqual(pathComponents[2], 'sub-key'); + assert.strictEqual(pathComponents[3], keySet.subscribeKey); + assert.strictEqual(pathComponents[4], 'channel-group'); + assert.strictEqual(pathComponents[5], 'test-group'); + }); + }); + + describe('Query parameters', () => { + beforeEach(() => { + request = new RemoveChannelGroupChannelsRequest({ + keySet, + channelGroup: 'test-group', + channels: ['channel1', 'channel2'] + }); + }); + + it('should format channels correctly in remove query string', () => { + const queryParams = (request as any).queryParameters; + assert.deepStrictEqual(queryParams, { remove: 'channel1,channel2' }); + }); + + it('should handle single channel correctly', () => { + const singleChannelRequest = new RemoveChannelGroupChannelsRequest({ + keySet, + channelGroup: 'test-group', + channels: ['single-channel'] + }); + + const queryParams = (singleChannelRequest as any).queryParameters; + assert.deepStrictEqual(queryParams, { remove: 'single-channel' }); + }); + }); + + describe('Channel encoding', () => { + it('should handle channels with special characters', () => { + const specialChannelsRequest = new RemoveChannelGroupChannelsRequest({ + keySet, + channelGroup: 'test group with spaces', + channels: ['channel with spaces', 'channel/with/slashes', 'channel?with=query¶ms'] + }); + + const path = (specialChannelsRequest as any).path; + const queryParams = (specialChannelsRequest as any).queryParameters; + + // Verify the channel group is URL encoded in path + assert(path.includes('test%20group%20with%20spaces')); + + // Verify channels are properly formatted in query parameters + assert.strictEqual(queryParams.remove, 'channel with spaces,channel/with/slashes,channel?with=query¶ms'); + }); + + it('should handle channels with unicode characters', () => { + const unicodeChannelsRequest = new RemoveChannelGroupChannelsRequest({ + keySet, + channelGroup: 'test-group', + channels: ['channel-éñ', 'channel-中文', 'channel-🚀'] + }); + + const queryParams = (unicodeChannelsRequest as any).queryParameters; + assert.strictEqual(queryParams.remove, 'channel-éñ,channel-中文,channel-🚀'); + }); + }); + + describe('Response parsing', () => { + beforeEach(() => { + request = new RemoveChannelGroupChannelsRequest({ + keySet, + channelGroup: 'test-group', + channels: ['channel1', 'channel2'] + }); + }); + + it('should parse empty service response correctly', async () => { + const mockResponse = { + status: 200, + url: '', + headers: {}, + body: new ArrayBuffer(0) + }; + + // Mock the deserializeResponse method to return expected service response + (request as any).deserializeResponse = () => ({ + status: 200, + message: 'OK', + service: 'ChannelGroups', + error: false + }); + + const result = await request.parse(mockResponse); + assert.deepStrictEqual(result, {}); + }); + }); + + describe('Multiple channels', () => { + it('should format array of multiple channels correctly', () => { + const multiChannelRequest = new RemoveChannelGroupChannelsRequest({ + keySet, + channelGroup: 'test-group', + channels: ['ch1', 'ch2', 'ch3', 'ch4', 'ch5'] + }); + + const queryParams = (multiChannelRequest as any).queryParameters; + assert.strictEqual(queryParams.remove, 'ch1,ch2,ch3,ch4,ch5'); + }); + }); + + describe('Non-existent channels', () => { + it('should handle removing non-existent channels gracefully', () => { + const nonExistentChannelsRequest = new RemoveChannelGroupChannelsRequest({ + keySet, + channelGroup: 'test-group', + channels: ['non-existent-channel1', 'non-existent-channel2'] + }); + + // Validation should succeed even for non-existent channels + const result = nonExistentChannelsRequest.validate(); + assert.strictEqual(result, undefined); + + // Query parameters should be formatted correctly + const queryParams = (nonExistentChannelsRequest as any).queryParameters; + assert.strictEqual(queryParams.remove, 'non-existent-channel1,non-existent-channel2'); + }); + }); +}); diff --git a/test/unit/chanel_groups/uuid_objects_get.test.ts b/test/unit/chanel_groups/uuid_objects_get.test.ts new file mode 100644 index 000000000..4d666f68a --- /dev/null +++ b/test/unit/chanel_groups/uuid_objects_get.test.ts @@ -0,0 +1,133 @@ +import assert from 'assert'; + +import { GetUUIDMetadataRequest } from '../../../src/core/endpoints/objects/uuid/get'; +import { TransportMethod } from '../../../src/core/types/transport-request'; +import RequestOperation from '../../../src/core/constants/operations'; +import { KeySet } from '../../../src/core/types/api'; +import * as AppContext from '../../../src/core/types/api/app-context'; + +describe('GetUUIDMetadataRequest', () => { + let defaultKeySet: KeySet; + let defaultParameters: AppContext.GetUUIDMetadataParameters & { keySet: KeySet }; + + beforeEach(() => { + defaultKeySet = { + publishKey: 'test_publish_key', + subscribeKey: 'test_subscribe_key', + }; + + defaultParameters = { + uuid: 'test_uuid', + keySet: defaultKeySet, + }; + }); + + describe('parameter validation', () => { + it('should return "\'uuid\' cannot be empty" when uuid is empty string', () => { + const request = new GetUUIDMetadataRequest({ + ...defaultParameters, + uuid: '', + }); + assert.equal(request.validate(), "'uuid' cannot be empty"); + }); + + it('should return "\'uuid\' cannot be empty" when uuid is null', () => { + const request = new GetUUIDMetadataRequest({ + ...defaultParameters, + uuid: null as any, + }); + assert.equal(request.validate(), "'uuid' cannot be empty"); + }); + + it('should return "\'uuid\' cannot be empty" when uuid is undefined', () => { + const request = new GetUUIDMetadataRequest({ + ...defaultParameters, + uuid: undefined as any, + }); + assert.equal(request.validate(), "'uuid' cannot be empty"); + }); + + it('should return undefined when uuid is provided', () => { + const request = new GetUUIDMetadataRequest(defaultParameters); + assert.equal(request.validate(), undefined); + }); + }); + + describe('operation type', () => { + it('should return correct operation type', () => { + const request = new GetUUIDMetadataRequest(defaultParameters); + assert.equal(request.operation(), RequestOperation.PNGetUUIDMetadataOperation); + }); + }); + + describe('URL construction', () => { + it('should construct correct path with subscribeKey and uuid', () => { + const request = new GetUUIDMetadataRequest(defaultParameters); + const transportRequest = request.request(); + const expectedPath = `/v2/objects/${defaultKeySet.subscribeKey}/uuids/${defaultParameters.uuid}`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should encode special characters in uuid', () => { + const request = new GetUUIDMetadataRequest({ + ...defaultParameters, + uuid: 'test-uuid#1@2', + }); + const transportRequest = request.request(); + const expectedPath = `/v2/objects/${defaultKeySet.subscribeKey}/uuids/test-uuid%231%402`; + assert.equal(transportRequest.path, expectedPath); + }); + }); + + describe('query parameters', () => { + it('should include custom fields by default', () => { + const request = new GetUUIDMetadataRequest(defaultParameters); + const transportRequest = request.request(); + const includeParam = transportRequest.queryParameters?.include as string; + assert(includeParam?.includes('custom')); + }); + + it('should exclude custom fields when include.customFields is false', () => { + const request = new GetUUIDMetadataRequest({ + ...defaultParameters, + include: { customFields: false }, + }); + const transportRequest = request.request(); + const includeParam = transportRequest.queryParameters?.include as string; + assert(!includeParam?.includes('custom')); + }); + + it('should include status and type fields', () => { + const request = new GetUUIDMetadataRequest(defaultParameters); + const transportRequest = request.request(); + const includeParam = transportRequest.queryParameters?.include as string; + assert(includeParam?.includes('status')); + assert(includeParam?.includes('type')); + }); + }); + + describe('HTTP method', () => { + it('should use GET method', () => { + const request = new GetUUIDMetadataRequest(defaultParameters); + const transportRequest = request.request(); + assert.equal(transportRequest.method, TransportMethod.GET); + }); + }); + + describe('backward compatibility', () => { + it('should map userId parameter to uuid', () => { + const request = new GetUUIDMetadataRequest({ + ...defaultParameters, + uuid: undefined as any, + userId: 'legacy_user_id', + }); + const transportRequest = request.request(); + const expectedPath = `/v2/objects/${defaultKeySet.subscribeKey}/uuids/legacy_user_id`; + assert.equal(transportRequest.path, expectedPath); + }); + }); +}); + + + + diff --git a/test/unit/chanel_groups/uuid_objects_get_all.test.ts b/test/unit/chanel_groups/uuid_objects_get_all.test.ts new file mode 100644 index 000000000..db93beab3 --- /dev/null +++ b/test/unit/chanel_groups/uuid_objects_get_all.test.ts @@ -0,0 +1,187 @@ +import assert from 'assert'; + +import { GetAllUUIDMetadataRequest } from '../../../src/core/endpoints/objects/uuid/get_all'; +import { TransportMethod } from '../../../src/core/types/transport-request'; +import RequestOperation from '../../../src/core/constants/operations'; +import { KeySet } from '../../../src/core/types/api'; +import * as AppContext from '../../../src/core/types/api/app-context'; + +describe('GetAllUUIDMetadataRequest', () => { + let defaultKeySet: KeySet; + let defaultParameters: AppContext.GetAllMetadataParameters> & { keySet: KeySet }; + + beforeEach(() => { + defaultKeySet = { + publishKey: 'test_publish_key', + subscribeKey: 'test_subscribe_key', + }; + + defaultParameters = { + keySet: defaultKeySet, + }; + }); + + describe('operation type', () => { + it('should return correct operation type', () => { + const request = new GetAllUUIDMetadataRequest>(defaultParameters); + assert.equal(request.operation(), RequestOperation.PNGetAllUUIDMetadataOperation); + }); + }); + + describe('URL construction', () => { + it('should construct correct path with subscribeKey', () => { + const request = new GetAllUUIDMetadataRequest>(defaultParameters); + const transportRequest = request.request(); + const expectedPath = `/v2/objects/${defaultKeySet.subscribeKey}/uuids`; + assert.equal(transportRequest.path, expectedPath); + }); + }); + + describe('default parameters', () => { + it('should set default limit to 100', () => { + const request = new GetAllUUIDMetadataRequest>(defaultParameters); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.limit, 100); + }); + + it('should set includeCustomFields to false by default', () => { + const request = new GetAllUUIDMetadataRequest>(defaultParameters); + const transportRequest = request.request(); + const includeParam = transportRequest.queryParameters?.include as string; + assert(!includeParam?.includes('custom')); + }); + + it('should include status and type fields by default', () => { + const request = new GetAllUUIDMetadataRequest>(defaultParameters); + const transportRequest = request.request(); + const includeParam = transportRequest.queryParameters?.include as string; + assert(includeParam?.includes('status')); + assert(includeParam?.includes('type')); + }); + }); + + describe('query parameters', () => { + it('should handle custom limit parameter', () => { + const request = new GetAllUUIDMetadataRequest>({ + ...defaultParameters, + limit: 50, + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.limit, 50); + }); + + it('should handle filter parameter', () => { + const request = new GetAllUUIDMetadataRequest>({ + ...defaultParameters, + filter: 'name == "test"', + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.filter, 'name == "test"'); + }); + + it('should handle pagination start cursor', () => { + const request = new GetAllUUIDMetadataRequest>({ + ...defaultParameters, + page: { next: 'next-cursor-token' }, + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.start, 'next-cursor-token'); + }); + + it('should handle pagination end cursor', () => { + const request = new GetAllUUIDMetadataRequest>({ + ...defaultParameters, + page: { prev: 'prev-cursor-token' }, + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.end, 'prev-cursor-token'); + }); + + it('should include custom fields when include.customFields is true', () => { + const request = new GetAllUUIDMetadataRequest>({ + ...defaultParameters, + include: { customFields: true }, + }); + const transportRequest = request.request(); + const includeParam = transportRequest.queryParameters?.include as string; + assert(includeParam?.includes('custom')); + }); + + it('should handle totalCount include option', () => { + const request = new GetAllUUIDMetadataRequest>({ + ...defaultParameters, + include: { totalCount: true }, + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.count, 'true'); + }); + + it('should not include count when totalCount is false', () => { + const request = new GetAllUUIDMetadataRequest>({ + ...defaultParameters, + include: { totalCount: false }, + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.count, 'false'); + }); + }); + + describe('sorting', () => { + it('should handle string sort parameter', () => { + const request = new GetAllUUIDMetadataRequest>({ + ...defaultParameters, + sort: 'name', + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.sort, 'name'); + }); + + it('should handle object sort with direction', () => { + const request = new GetAllUUIDMetadataRequest>({ + ...defaultParameters, + sort: { name: 'asc', updated: 'desc' }, + }); + const transportRequest = request.request(); + const sortArray = transportRequest.queryParameters?.sort as string[]; + assert(Array.isArray(sortArray)); + assert(sortArray.includes('name:asc')); + assert(sortArray.includes('updated:desc')); + }); + + it('should handle object sort with null direction', () => { + const request = new GetAllUUIDMetadataRequest>({ + ...defaultParameters, + sort: { name: null }, + }); + const transportRequest = request.request(); + const sortArray = transportRequest.queryParameters?.sort as string[]; + assert(Array.isArray(sortArray)); + assert(sortArray.includes('name')); + }); + + it('should handle mixed sort directions', () => { + const request = new GetAllUUIDMetadataRequest>({ + ...defaultParameters, + sort: { name: 'asc', updated: null, status: 'desc' }, + }); + const transportRequest = request.request(); + const sortArray = transportRequest.queryParameters?.sort as string[]; + assert(Array.isArray(sortArray)); + assert(sortArray.includes('name:asc')); + assert(sortArray.includes('updated')); + assert(sortArray.includes('status:desc')); + }); + }); + + describe('HTTP method', () => { + it('should use GET method', () => { + const request = new GetAllUUIDMetadataRequest>(defaultParameters); + const transportRequest = request.request(); + assert.equal(transportRequest.method, TransportMethod.GET); + }); + }); +}); + + + + diff --git a/test/unit/chanel_groups/uuid_objects_remove.test.ts b/test/unit/chanel_groups/uuid_objects_remove.test.ts new file mode 100644 index 000000000..09b789638 --- /dev/null +++ b/test/unit/chanel_groups/uuid_objects_remove.test.ts @@ -0,0 +1,146 @@ +import assert from 'assert'; + +import { RemoveUUIDMetadataRequest } from '../../../src/core/endpoints/objects/uuid/remove'; +import { TransportMethod } from '../../../src/core/types/transport-request'; +import RequestOperation from '../../../src/core/constants/operations'; +import { KeySet } from '../../../src/core/types/api'; +import * as AppContext from '../../../src/core/types/api/app-context'; + +describe('RemoveUUIDMetadataRequest', () => { + let defaultKeySet: KeySet; + let defaultParameters: AppContext.RemoveUUIDMetadataParameters & { keySet: KeySet }; + + beforeEach(() => { + defaultKeySet = { + publishKey: 'test_publish_key', + subscribeKey: 'test_subscribe_key', + }; + + defaultParameters = { + uuid: 'test_uuid', + keySet: defaultKeySet, + }; + }); + + describe('parameter validation', () => { + it('should return "\'uuid\' cannot be empty" when uuid is empty', () => { + const request = new RemoveUUIDMetadataRequest({ + ...defaultParameters, + uuid: '', + }); + assert.equal(request.validate(), "'uuid' cannot be empty"); + }); + + it('should return "\'uuid\' cannot be empty" when uuid is null', () => { + const request = new RemoveUUIDMetadataRequest({ + ...defaultParameters, + uuid: null as any, + }); + assert.equal(request.validate(), "'uuid' cannot be empty"); + }); + + it('should return "\'uuid\' cannot be empty" when uuid is undefined', () => { + const request = new RemoveUUIDMetadataRequest({ + ...defaultParameters, + uuid: undefined as any, + }); + assert.equal(request.validate(), "'uuid' cannot be empty"); + }); + + it('should return undefined when uuid provided', () => { + const request = new RemoveUUIDMetadataRequest(defaultParameters); + assert.equal(request.validate(), undefined); + }); + }); + + describe('operation type', () => { + it('should return correct operation type', () => { + const request = new RemoveUUIDMetadataRequest(defaultParameters); + assert.equal(request.operation(), RequestOperation.PNRemoveUUIDMetadataOperation); + }); + }); + + describe('URL construction', () => { + it('should construct correct path', () => { + const request = new RemoveUUIDMetadataRequest(defaultParameters); + const transportRequest = request.request(); + const expectedPath = `/v2/objects/${defaultKeySet.subscribeKey}/uuids/${defaultParameters.uuid}`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should encode uuid name in path', () => { + const request = new RemoveUUIDMetadataRequest({ + ...defaultParameters, + uuid: 'test-uuid#1@2', + }); + const transportRequest = request.request(); + const expectedPath = `/v2/objects/${defaultKeySet.subscribeKey}/uuids/test-uuid%231%402`; + assert.equal(transportRequest.path, expectedPath); + }); + }); + + describe('HTTP method', () => { + it('should use DELETE method', () => { + const request = new RemoveUUIDMetadataRequest(defaultParameters); + const transportRequest = request.request(); + assert.equal(transportRequest.method, TransportMethod.DELETE); + }); + }); + + describe('query parameters', () => { + it('should not include any query parameters', () => { + const request = new RemoveUUIDMetadataRequest(defaultParameters); + const transportRequest = request.request(); + + // RemoveUUIDMetadataRequest doesn't define any query parameters + // The request should either have no queryParameters or empty queryParameters + const queryParams = transportRequest.queryParameters; + if (queryParams) { + // If queryParameters exist, they should be empty or contain no meaningful parameters + assert.equal(Object.keys(queryParams).length, 0); + } + }); + }); + + describe('headers', () => { + it('should not set any custom headers', () => { + const request = new RemoveUUIDMetadataRequest(defaultParameters); + const transportRequest = request.request(); + + // RemoveUUIDMetadataRequest doesn't define custom headers + // Check that no application-specific headers are set + const headers = transportRequest.headers; + if (headers) { + assert.equal(headers['Content-Type'], undefined); + assert.equal(headers['If-Match'], undefined); + } + }); + }); + + describe('body', () => { + it('should not have a request body', () => { + const request = new RemoveUUIDMetadataRequest(defaultParameters); + const transportRequest = request.request(); + + // DELETE requests typically don't have a body + assert.equal(transportRequest.body, undefined); + }); + }); + + describe('backward compatibility', () => { + it('should map userId parameter to uuid', () => { + const request = new RemoveUUIDMetadataRequest({ + ...defaultParameters, + uuid: undefined as any, + userId: 'legacy_user_id', + }); + const transportRequest = request.request(); + const expectedPath = `/v2/objects/${defaultKeySet.subscribeKey}/uuids/legacy_user_id`; + assert.equal(transportRequest.path, expectedPath); + }); + }); +}); + + + + diff --git a/test/unit/chanel_groups/uuid_objects_set.test.ts b/test/unit/chanel_groups/uuid_objects_set.test.ts new file mode 100644 index 000000000..dce590244 --- /dev/null +++ b/test/unit/chanel_groups/uuid_objects_set.test.ts @@ -0,0 +1,232 @@ +import assert from 'assert'; + +import { SetUUIDMetadataRequest } from '../../../src/core/endpoints/objects/uuid/set'; +import { TransportMethod } from '../../../src/core/types/transport-request'; +import RequestOperation from '../../../src/core/constants/operations'; +import { KeySet } from '../../../src/core/types/api'; +import * as AppContext from '../../../src/core/types/api/app-context'; + +// Local type alias for UUIDMetadata since it's not exported +type UUIDMetadata = { + name?: string; + email?: string; + externalId?: string; + profileUrl?: string; + type?: string; + status?: string; + custom?: Custom; +}; + +describe('SetUUIDMetadataRequest', () => { + let defaultKeySet: KeySet; + let defaultParameters: AppContext.SetUUIDMetadataParameters & { keySet: KeySet }; + let testData: UUIDMetadata; + + beforeEach(() => { + defaultKeySet = { + publishKey: 'test_publish_key', + subscribeKey: 'test_subscribe_key', + }; + + testData = { + name: 'Test User', + email: 'test@example.com', + custom: { + age: 25, + location: 'San Francisco', + }, + }; + + defaultParameters = { + uuid: 'test_uuid', + data: testData, + keySet: defaultKeySet, + }; + }); + + describe('parameter validation', () => { + it('should return "\'uuid\' cannot be empty" when uuid is empty', () => { + const request = new SetUUIDMetadataRequest({ + ...defaultParameters, + uuid: '', + }); + assert.equal(request.validate(), "'uuid' cannot be empty"); + }); + + it('should return "\'uuid\' cannot be empty" when uuid is null', () => { + const request = new SetUUIDMetadataRequest({ + ...defaultParameters, + uuid: null as any, + }); + assert.equal(request.validate(), "'uuid' cannot be empty"); + }); + + it('should return "\'uuid\' cannot be empty" when uuid is undefined', () => { + const request = new SetUUIDMetadataRequest({ + ...defaultParameters, + uuid: undefined as any, + }); + assert.equal(request.validate(), "'uuid' cannot be empty"); + }); + + it('should return "Data cannot be empty" when data is null', () => { + const request = new SetUUIDMetadataRequest({ + ...defaultParameters, + data: null as any, + }); + assert.equal(request.validate(), 'Data cannot be empty'); + }); + + it('should return "Data cannot be empty" when data is undefined', () => { + const request = new SetUUIDMetadataRequest({ + ...defaultParameters, + data: undefined as any, + }); + assert.equal(request.validate(), 'Data cannot be empty'); + }); + + it('should return undefined when all required parameters provided', () => { + const request = new SetUUIDMetadataRequest(defaultParameters); + assert.equal(request.validate(), undefined); + }); + }); + + describe('operation type', () => { + it('should return correct operation type', () => { + const request = new SetUUIDMetadataRequest(defaultParameters); + assert.equal(request.operation(), RequestOperation.PNSetUUIDMetadataOperation); + }); + }); + + describe('URL construction', () => { + it('should construct correct path with subscribeKey and uuid', () => { + const request = new SetUUIDMetadataRequest(defaultParameters); + const transportRequest = request.request(); + const expectedPath = `/v2/objects/${defaultKeySet.subscribeKey}/uuids/${defaultParameters.uuid}`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should encode special characters in uuid', () => { + const request = new SetUUIDMetadataRequest({ + ...defaultParameters, + uuid: 'test-uuid#1@2', + }); + const transportRequest = request.request(); + const expectedPath = `/v2/objects/${defaultKeySet.subscribeKey}/uuids/test-uuid%231%402`; + assert.equal(transportRequest.path, expectedPath); + }); + }); + + describe('HTTP method and headers', () => { + it('should use PATCH method', () => { + const request = new SetUUIDMetadataRequest(defaultParameters); + const transportRequest = request.request(); + assert.equal(transportRequest.method, TransportMethod.PATCH); + }); + + it('should set Content-Type header', () => { + const request = new SetUUIDMetadataRequest(defaultParameters); + const transportRequest = request.request(); + assert.equal(transportRequest.headers?.['Content-Type'], 'application/json'); + }); + + it('should set If-Match header when ifMatchesEtag provided', () => { + const request = new SetUUIDMetadataRequest({ + ...defaultParameters, + ifMatchesEtag: 'test-etag-value', + }); + const transportRequest = request.request(); + assert.equal(transportRequest.headers?.['If-Match'], 'test-etag-value'); + }); + + it('should not set If-Match header when ifMatchesEtag not provided', () => { + const request = new SetUUIDMetadataRequest(defaultParameters); + const transportRequest = request.request(); + assert.equal(transportRequest.headers?.['If-Match'], undefined); + }); + }); + + describe('query parameters', () => { + it('should include custom fields by default', () => { + const request = new SetUUIDMetadataRequest(defaultParameters); + const transportRequest = request.request(); + const includeParam = transportRequest.queryParameters?.include as string; + assert(includeParam?.includes('custom')); + }); + + it('should exclude custom fields when include.customFields is false', () => { + const request = new SetUUIDMetadataRequest({ + ...defaultParameters, + include: { customFields: false }, + }); + const transportRequest = request.request(); + const includeParam = transportRequest.queryParameters?.include as string; + assert(!includeParam?.includes('custom')); + }); + + it('should include status and type fields', () => { + const request = new SetUUIDMetadataRequest(defaultParameters); + const transportRequest = request.request(); + const includeParam = transportRequest.queryParameters?.include as string; + assert(includeParam?.includes('status')); + assert(includeParam?.includes('type')); + }); + }); + + describe('body serialization', () => { + it('should serialize data as JSON string', () => { + const request = new SetUUIDMetadataRequest(defaultParameters); + const transportRequest = request.request(); + assert.equal(transportRequest.body, JSON.stringify(testData)); + }); + + it('should handle complex nested data objects', () => { + const complexData = { + name: 'Complex User', + email: 'complex@example.com', + custom: { + profile: { + settings: { + theme: 'dark', + notifications: true, + }, + preferences: ['option1', 'option2'], + }, + metadata: { + tags: ['tag1', 'tag2', 'tag3'], + scores: { math: 95, science: 87 }, + }, + }, + }; + + const request = new SetUUIDMetadataRequest({ + ...defaultParameters, + data: complexData as any, + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.body, JSON.stringify(complexData)); + + // Verify that the serialized data can be parsed back correctly + const parsedBody = JSON.parse(transportRequest.body as string); + assert.deepEqual(parsedBody, complexData); + }); + }); + + describe('backward compatibility', () => { + it('should map userId parameter to uuid', () => { + const request = new SetUUIDMetadataRequest({ + ...defaultParameters, + uuid: undefined as any, + userId: 'legacy_user_id', + }); + const transportRequest = request.request(); + const expectedPath = `/v2/objects/${defaultKeySet.subscribeKey}/uuids/legacy_user_id`; + assert.equal(transportRequest.path, expectedPath); + }); + }); +}); + + + + diff --git a/test/unit/fetch_messages/fetch_messages.test.ts b/test/unit/fetch_messages/fetch_messages.test.ts new file mode 100644 index 000000000..c72e72101 --- /dev/null +++ b/test/unit/fetch_messages/fetch_messages.test.ts @@ -0,0 +1,330 @@ +/* global describe, beforeEach, it */ + +import assert from 'assert'; + +import { FetchMessagesRequest } from '../../../src/core/endpoints/fetch_messages'; +import RequestOperation from '../../../src/core/constants/operations'; +import { PubNubMessageType } from '../../../src/core/types/api/history'; +import { KeySet } from '../../../src/core/types/api'; + +describe('FetchMessagesRequest', () => { + let keySet: KeySet; + let getFileUrl: (params: any) => string; + + beforeEach(() => { + keySet = { + subscribeKey: 'sub-key', + publishKey: 'pub-key', + }; + getFileUrl = (params: any) => `https://example.com/files/${params.id}/${params.name}`; + }); + + describe('validates required parameters', () => { + it('should return error for missing subscribeKey', () => { + const request = new FetchMessagesRequest({ + keySet: { subscribeKey: '', publishKey: 'pub-key' }, + channels: ['channel1'], + getFileUrl, + }); + + const error = request.validate(); + assert.equal(error, 'Missing Subscribe Key'); + }); + + it('should return error for missing channels', () => { + // We can't test undefined/null channels because constructor accesses .length + // But we can test the validate() method directly with a mock request + const mockRequest = { + parameters: { + keySet: { subscribeKey: 'test-key' }, + channels: null, + getFileUrl: () => '', + }, + validate: FetchMessagesRequest.prototype.validate, + }; + + const error = mockRequest.validate(); + assert.equal(error, 'Missing channels'); + }); + + it('should return error for includeMessageActions with multiple channels', () => { + const request = new FetchMessagesRequest({ + keySet, + channels: ['channel1', 'channel2'], + includeMessageActions: true, + getFileUrl, + }); + + const error = request.validate(); + assert.equal( + error, + 'History can return actions data for a single channel only. Either pass a single channel or disable the includeMessageActions flag.' + ); + }); + + it('should return undefined for valid parameters', () => { + const request = new FetchMessagesRequest({ + keySet, + channels: ['channel1'], + getFileUrl, + }); + + const error = request.validate(); + assert.equal(error, undefined); + }); + }); + + describe('applies correct default values', () => { + it('should default count to 100 for single channel', () => { + const request = new FetchMessagesRequest({ + keySet, + channels: ['channel1'], + getFileUrl, + }); + + // Access private parameters property via type assertion + const parameters = (request as any).parameters; + assert.equal(parameters.count, 100); + assert.equal(parameters.includeUUID, true); + assert.equal(parameters.includeMessageType, true); + assert.equal(parameters.stringifiedTimeToken, false); + }); + + it('should default count to 25 for multiple channels', () => { + const request = new FetchMessagesRequest({ + keySet, + channels: ['channel1', 'channel2'], + getFileUrl, + }); + + const parameters = (request as any).parameters; + assert.equal(parameters.count, 25); + }); + + it('should default count to 25 when includeMessageActions is true', () => { + const request = new FetchMessagesRequest({ + keySet, + channels: ['channel1'], + includeMessageActions: true, + getFileUrl, + }); + + const parameters = (request as any).parameters; + assert.equal(parameters.count, 25); + }); + }); + + describe('constructs correct path for regular history', () => { + it('should build path without includeMessageActions', () => { + const request = new FetchMessagesRequest({ + keySet, + channels: ['channel1'], + getFileUrl, + }); + + const path = (request as any).path; + assert.equal(path, '/v3/history/sub-key/sub-key/channel/channel1'); + }); + + it('should encode channel names with special characters', () => { + const request = new FetchMessagesRequest({ + keySet, + channels: ['channel#1', 'channel/2', 'channel 3'], + getFileUrl, + }); + + const path = (request as any).path; + assert.equal(path, '/v3/history/sub-key/sub-key/channel/channel%231,channel%2F2,channel%203'); + }); + }); + + describe('constructs correct path for history-with-actions', () => { + it('should build path with includeMessageActions=true', () => { + const request = new FetchMessagesRequest({ + keySet, + channels: ['channel1'], + includeMessageActions: true, + getFileUrl, + }); + + const path = (request as any).path; + assert.equal(path, '/v3/history-with-actions/sub-key/sub-key/channel/channel1'); + }); + }); + + describe('builds query parameters correctly', () => { + it('should include all optional parameters in query', () => { + const request = new FetchMessagesRequest({ + keySet, + channels: ['channel1'], + count: 50, + start: '12345', + end: '67890', + includeUUID: true, + includeMessageType: true, + includeMeta: true, + includeCustomMessageType: true, + stringifiedTimeToken: true, + getFileUrl, + }); + + const queryParams = (request as any).queryParameters; + assert.equal(queryParams.max, 50); + assert.equal(queryParams.start, '12345'); + assert.equal(queryParams.end, '67890'); + assert.equal(queryParams.include_uuid, 'true'); + assert.equal(queryParams.include_message_type, 'true'); + assert.equal(queryParams.include_meta, 'true'); + assert.equal(queryParams.include_custom_message_type, 'true'); + assert.equal(queryParams.string_message_token, 'true'); + }); + + it('should omit optional parameters when not provided', () => { + const request = new FetchMessagesRequest({ + keySet, + channels: ['channel1'], + getFileUrl, + }); + + const queryParams = (request as any).queryParameters; + assert.equal(queryParams.start, undefined); + assert.equal(queryParams.end, undefined); + assert.equal(queryParams.include_meta, undefined); + assert.equal(queryParams.string_message_token, undefined); + }); + + it('should handle includeCustomMessageType false value', () => { + const request = new FetchMessagesRequest({ + keySet, + channels: ['channel1'], + includeCustomMessageType: false, + getFileUrl, + }); + + const queryParams = (request as any).queryParameters; + assert.equal(queryParams.include_custom_message_type, 'false'); + }); + }); + + describe('handles channel name encoding', () => { + it('should properly encode special characters', () => { + const request = new FetchMessagesRequest({ + keySet, + channels: ['channel#test', 'channel/test', 'channel test', 'channel@test', 'channel+test'], + getFileUrl, + }); + + const path = (request as any).path; + assert(path.includes('channel%23test')); // # encoded + assert(path.includes('channel%2Ftest')); // / encoded + assert(path.includes('channel%20test')); // space encoded + assert(path.includes('channel%40test')); // @ encoded + assert(path.includes('channel%2Btest')); // + encoded + }); + + it('should handle unicode characters', () => { + const request = new FetchMessagesRequest({ + keySet, + channels: ['café', '测试'], + getFileUrl, + }); + + const path = (request as any).path; + // Unicode characters should be properly encoded + assert(path.includes('caf%C3%A9')); + assert(path.includes('%E6%B5%8B%E8%AF%95')); + }); + }); + + describe('enforces count limits correctly', () => { + it('should clamp count to 100 for single channel', () => { + const request = new FetchMessagesRequest({ + keySet, + channels: ['channel1'], + count: 150, + getFileUrl, + }); + + const parameters = (request as any).parameters; + assert.equal(parameters.count, 100); + }); + + it('should clamp count to 25 for multiple channels', () => { + const request = new FetchMessagesRequest({ + keySet, + channels: ['channel1', 'channel2'], + count: 50, + getFileUrl, + }); + + const parameters = (request as any).parameters; + assert.equal(parameters.count, 25); + }); + + it('should clamp count to 25 when includeMessageActions is true', () => { + const request = new FetchMessagesRequest({ + keySet, + channels: ['channel1'], + count: 50, + includeMessageActions: true, + getFileUrl, + }); + + const parameters = (request as any).parameters; + assert.equal(parameters.count, 25); + }); + }); + + describe('handles backward compatibility for includeUuid', () => { + it('should map includeUuid to includeUUID when truthy', () => { + const request = new FetchMessagesRequest({ + keySet, + channels: ['channel1'], + includeUuid: true, + getFileUrl, + }); + + const parameters = (request as any).parameters; + assert.equal(parameters.includeUUID, true); + assert.equal(parameters.includeUuid, true); + }); + + it('should use default includeUUID when includeUuid is falsy', () => { + const request = new FetchMessagesRequest({ + keySet, + channels: ['channel1'], + includeUuid: false, + getFileUrl, + }); + + const parameters = (request as any).parameters; + assert.equal(parameters.includeUUID, true); // Should default to true + assert.equal(parameters.includeUuid, false); // Original value preserved + }); + + it('should prefer includeUUID over includeUuid when both provided', () => { + const request = new FetchMessagesRequest({ + keySet, + channels: ['channel1'], + includeUuid: false, + includeUUID: true, + getFileUrl, + }); + + const parameters = (request as any).parameters; + assert.equal(parameters.includeUUID, true); + }); + }); + + describe('operation type', () => { + it('should return correct operation type', () => { + const request = new FetchMessagesRequest({ + keySet, + channels: ['channel1'], + getFileUrl, + }); + + assert.equal(request.operation(), RequestOperation.PNFetchMessagesOperation); + }); + }); +}); diff --git a/test/unit/message_actions/message_actions.test.ts b/test/unit/message_actions/message_actions.test.ts new file mode 100644 index 000000000..e0154533a --- /dev/null +++ b/test/unit/message_actions/message_actions.test.ts @@ -0,0 +1,664 @@ +/* global describe, beforeEach, it */ + +import assert from 'assert'; + +import { AddMessageActionRequest } from '../../../src/core/endpoints/actions/add_message_action'; +import { GetMessageActionsRequest } from '../../../src/core/endpoints/actions/get_message_actions'; +import { RemoveMessageAction } from '../../../src/core/endpoints/actions/remove_message_action'; +import { TransportMethod } from '../../../src/core/types/transport-request'; +import { TransportResponse } from '../../../src/core/types/transport-response'; +import RequestOperation from '../../../src/core/constants/operations'; +import { KeySet } from '../../../src/core/types/api'; +import * as MessageAction from '../../../src/core/types/api/message-action'; + +describe('Message Actions Request Classes', () => { + let defaultKeySet: KeySet; + + beforeEach(() => { + defaultKeySet = { + subscribeKey: 'test_subscribe_key', + publishKey: 'test_publish_key', + }; + }); + + describe('AddMessageActionRequest', () => { + let defaultParameters: MessageAction.AddMessageActionParameters & { keySet: KeySet }; + + beforeEach(() => { + defaultParameters = { + channel: 'test_channel', + messageTimetoken: '1234567890', + action: { + type: 'reaction', + value: 'smiley_face', + }, + keySet: defaultKeySet, + }; + }); + + describe('validation', () => { + it('should validate required subscribeKey', () => { + const request = new AddMessageActionRequest({ + ...defaultParameters, + keySet: { ...defaultKeySet, subscribeKey: '' }, + }); + assert.equal(request.validate(), 'Missing Subscribe Key'); + }); + + it('should validate required channel', () => { + const request = new AddMessageActionRequest({ + ...defaultParameters, + channel: '', + }); + assert.equal(request.validate(), 'Missing message channel'); + }); + + it('should validate required messageTimetoken', () => { + const request = new AddMessageActionRequest({ + ...defaultParameters, + messageTimetoken: '', + }); + assert.equal(request.validate(), 'Missing message timetoken'); + }); + + it('should validate required action', () => { + const request = new AddMessageActionRequest({ + ...defaultParameters, + action: undefined as any, + }); + assert.equal(request.validate(), 'Missing Action'); + }); + + it('should validate required action.type', () => { + const request = new AddMessageActionRequest({ + ...defaultParameters, + action: { value: 'test' } as any, + }); + assert.equal(request.validate(), 'Missing Action.type'); + }); + + it('should validate required action.value', () => { + const request = new AddMessageActionRequest({ + ...defaultParameters, + action: { type: 'reaction' } as any, + }); + assert.equal(request.validate(), 'Missing Action.value'); + }); + + it('should validate action.type length limit', () => { + const request = new AddMessageActionRequest({ + ...defaultParameters, + action: { + type: '1234567890123456', // 16 characters (over limit) + value: 'test', + }, + }); + assert.equal(request.validate(), 'Action.type value exceed maximum length of 15'); + }); + + it('should allow action.type at maximum length', () => { + const request = new AddMessageActionRequest({ + ...defaultParameters, + action: { + type: '123456789012345', // 15 characters (exactly at limit) + value: 'test', + }, + }); + assert.equal(request.validate(), undefined); + }); + + it('should pass validation with valid parameters', () => { + const request = new AddMessageActionRequest(defaultParameters); + assert.equal(request.validate(), undefined); + }); + }); + + describe('operation', () => { + it('should return PNAddMessageActionOperation', () => { + const request = new AddMessageActionRequest(defaultParameters); + assert.equal(request.operation(), RequestOperation.PNAddMessageActionOperation); + }); + }); + + describe('URL construction', () => { + it('should construct correct path', () => { + const request = new AddMessageActionRequest(defaultParameters); + const transportRequest = request.request(); + + const expectedPath = `/v1/message-actions/${defaultKeySet.subscribeKey}/channel/${defaultParameters.channel}/message/${defaultParameters.messageTimetoken}`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should encode special characters in channel name', () => { + const request = new AddMessageActionRequest({ + ...defaultParameters, + channel: 'test channel#1/2', + }); + const transportRequest = request.request(); + + assert(transportRequest.path.includes('test%20channel%231%2F2')); + }); + + it('should encode Unicode characters in channel name', () => { + const request = new AddMessageActionRequest({ + ...defaultParameters, + channel: 'café测试', + }); + const transportRequest = request.request(); + + assert(transportRequest.path.includes('caf%C3%A9%E6%B5%8B%E8%AF%95')); + }); + }); + + describe('HTTP method and headers', () => { + it('should use POST method', () => { + const request = new AddMessageActionRequest(defaultParameters); + const transportRequest = request.request(); + + assert.equal(transportRequest.method, TransportMethod.POST); + }); + + it('should set correct Content-Type header', () => { + const request = new AddMessageActionRequest(defaultParameters); + const transportRequest = request.request(); + + assert.equal(transportRequest.headers?.['Content-Type'], 'application/json'); + }); + }); + + describe('request body', () => { + it('should serialize action to JSON in body', () => { + const request = new AddMessageActionRequest(defaultParameters); + const transportRequest = request.request(); + + const expectedBody = JSON.stringify(defaultParameters.action); + assert.equal(transportRequest.body, expectedBody); + }); + + it('should handle action with Unicode characters', () => { + const request = new AddMessageActionRequest({ + ...defaultParameters, + action: { + type: 'emoji', + value: '😀🎉', + }, + }); + const transportRequest = request.request(); + + const expectedBody = JSON.stringify({ type: 'emoji', value: '😀🎉' }); + assert.equal(transportRequest.body, expectedBody); + }); + }); + + describe('response parsing', () => { + it('should parse successful response', async () => { + const request = new AddMessageActionRequest(defaultParameters); + const mockResponse: TransportResponse = { + status: 200, + url: 'https://test.pubnub.com', + headers: { 'content-type': 'application/json' }, + body: new TextEncoder().encode(JSON.stringify({ + status: 200, + data: { + type: 'reaction', + value: 'smiley_face', + uuid: 'test_user', + actionTimetoken: '15610547826970050', + messageTimetoken: '1234567890', + }, + })), + }; + + const parsedResponse = await request.parse(mockResponse); + assert.equal(parsedResponse.data.type, 'reaction'); + assert.equal(parsedResponse.data.value, 'smiley_face'); + assert.equal(parsedResponse.data.uuid, 'test_user'); + assert.equal(parsedResponse.data.actionTimetoken, '15610547826970050'); + assert.equal(parsedResponse.data.messageTimetoken, '1234567890'); + }); + }); + + describe('edge cases', () => { + it('should handle empty string action.type', () => { + const request = new AddMessageActionRequest({ + ...defaultParameters, + action: { type: '', value: 'test' }, + }); + assert.equal(request.validate(), 'Missing Action.type'); + }); + + it('should handle empty string action.value', () => { + const request = new AddMessageActionRequest({ + ...defaultParameters, + action: { type: 'reaction', value: '' }, + }); + assert.equal(request.validate(), 'Missing Action.value'); + }); + + it('should handle null action', () => { + const request = new AddMessageActionRequest({ + ...defaultParameters, + action: null as any, + }); + assert.equal(request.validate(), 'Missing Action'); + }); + }); + }); + + describe('GetMessageActionsRequest', () => { + let defaultParameters: MessageAction.GetMessageActionsParameters & { keySet: KeySet }; + + beforeEach(() => { + defaultParameters = { + channel: 'test_channel', + keySet: defaultKeySet, + }; + }); + + describe('validation', () => { + it('should validate required subscribeKey', () => { + const request = new GetMessageActionsRequest({ + ...defaultParameters, + keySet: { ...defaultKeySet, subscribeKey: '' }, + }); + assert.equal(request.validate(), 'Missing Subscribe Key'); + }); + + it('should validate required channel', () => { + const request = new GetMessageActionsRequest({ + ...defaultParameters, + channel: '', + }); + assert.equal(request.validate(), 'Missing message channel'); + }); + + it('should pass validation with valid parameters', () => { + const request = new GetMessageActionsRequest(defaultParameters); + assert.equal(request.validate(), undefined); + }); + }); + + describe('operation', () => { + it('should return PNGetMessageActionsOperation', () => { + const request = new GetMessageActionsRequest(defaultParameters); + assert.equal(request.operation(), RequestOperation.PNGetMessageActionsOperation); + }); + }); + + describe('URL construction', () => { + it('should construct correct path', () => { + const request = new GetMessageActionsRequest(defaultParameters); + const transportRequest = request.request(); + + const expectedPath = `/v1/message-actions/${defaultKeySet.subscribeKey}/channel/${defaultParameters.channel}`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should encode special characters in channel name', () => { + const request = new GetMessageActionsRequest({ + ...defaultParameters, + channel: 'test channel#1/2', + }); + const transportRequest = request.request(); + + assert(transportRequest.path.includes('test%20channel%231%2F2')); + }); + + it('should use GET method by default', () => { + const request = new GetMessageActionsRequest(defaultParameters); + const transportRequest = request.request(); + + assert.equal(transportRequest.method, TransportMethod.GET); + }); + }); + + describe('query parameters', () => { + it('should include start parameter when provided', () => { + const request = new GetMessageActionsRequest({ + ...defaultParameters, + start: '1234567890', + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.start, '1234567890'); + }); + + it('should include end parameter when provided', () => { + const request = new GetMessageActionsRequest({ + ...defaultParameters, + end: '9876543210', + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.end, '9876543210'); + }); + + it('should include limit parameter when provided', () => { + const request = new GetMessageActionsRequest({ + ...defaultParameters, + limit: 50, + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.limit, 50); + }); + + it('should include all parameters when provided', () => { + const request = new GetMessageActionsRequest({ + ...defaultParameters, + start: '1234567890', + end: '9876543210', + limit: 25, + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.start, '1234567890'); + assert.equal(transportRequest.queryParameters?.end, '9876543210'); + assert.equal(transportRequest.queryParameters?.limit, 25); + }); + + it('should not include optional parameters when not provided', () => { + const request = new GetMessageActionsRequest(defaultParameters); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.start, undefined); + assert.equal(transportRequest.queryParameters?.end, undefined); + assert.equal(transportRequest.queryParameters?.limit, undefined); + }); + }); + + describe('response parsing', () => { + it('should parse response with data', async () => { + const request = new GetMessageActionsRequest(defaultParameters); + const mockResponse: TransportResponse = { + status: 200, + url: 'https://test.pubnub.com', + headers: { 'content-type': 'application/json' }, + body: new TextEncoder().encode(JSON.stringify({ + status: 200, + data: [ + { + type: 'reaction', + value: 'smiley_face', + uuid: 'user1', + actionTimetoken: '15610547826970050', + messageTimetoken: '1234567890', + }, + { + type: 'reaction', + value: 'thumbs_up', + uuid: 'user2', + actionTimetoken: '15610547826970051', + messageTimetoken: '1234567890', + }, + ], + })), + }; + + const parsedResponse = await request.parse(mockResponse); + assert.equal(parsedResponse.data.length, 2); + assert.equal(parsedResponse.start, '15610547826970050'); + assert.equal(parsedResponse.end, '15610547826970051'); + assert.equal(parsedResponse.data[0].type, 'reaction'); + assert.equal(parsedResponse.data[1].value, 'thumbs_up'); + }); + + it('should handle empty response', async () => { + const request = new GetMessageActionsRequest(defaultParameters); + const mockResponse: TransportResponse = { + status: 200, + url: 'https://test.pubnub.com', + headers: { 'content-type': 'application/json' }, + body: new TextEncoder().encode(JSON.stringify({ + status: 200, + data: [], + })), + }; + + const parsedResponse = await request.parse(mockResponse); + assert.equal(parsedResponse.data.length, 0); + assert.equal(parsedResponse.start, null); + assert.equal(parsedResponse.end, null); + }); + + it('should handle response with more data', async () => { + const request = new GetMessageActionsRequest(defaultParameters); + const mockResponse: TransportResponse = { + status: 200, + url: 'https://test.pubnub.com', + headers: { 'content-type': 'application/json' }, + body: new TextEncoder().encode(JSON.stringify({ + status: 200, + data: [ + { + type: 'reaction', + value: 'smiley_face', + uuid: 'user1', + actionTimetoken: '15610547826970050', + messageTimetoken: '1234567890', + }, + ], + more: { + url: '/v1/message-actions/test_subscribe_key/channel/test_channel?start=15610547826970049', + start: '15610547826970049', + end: '15610547826970000', + limit: 100, + }, + })), + }; + + const parsedResponse = await request.parse(mockResponse); + assert.equal(parsedResponse.data.length, 1); + assert.equal(parsedResponse.start, '15610547826970050'); + assert.equal(parsedResponse.end, '15610547826970050'); + assert(parsedResponse.more); + assert.equal(parsedResponse.more?.start, '15610547826970049'); + assert.equal(parsedResponse.more?.limit, 100); + }); + }); + }); + + describe('RemoveMessageAction', () => { + let defaultParameters: MessageAction.RemoveMessageActionParameters & { keySet: KeySet }; + + beforeEach(() => { + defaultParameters = { + channel: 'test_channel', + messageTimetoken: '1234567890', + actionTimetoken: '15610547826970050', + keySet: defaultKeySet, + }; + }); + + describe('validation', () => { + it('should validate required subscribeKey', () => { + const request = new RemoveMessageAction({ + ...defaultParameters, + keySet: { ...defaultKeySet, subscribeKey: '' }, + }); + assert.equal(request.validate(), 'Missing Subscribe Key'); + }); + + it('should validate required channel', () => { + const request = new RemoveMessageAction({ + ...defaultParameters, + channel: '', + }); + assert.equal(request.validate(), 'Missing message action channel'); + }); + + it('should validate required messageTimetoken', () => { + const request = new RemoveMessageAction({ + ...defaultParameters, + messageTimetoken: '', + }); + assert.equal(request.validate(), 'Missing message timetoken'); + }); + + it('should validate required actionTimetoken', () => { + const request = new RemoveMessageAction({ + ...defaultParameters, + actionTimetoken: '', + }); + assert.equal(request.validate(), 'Missing action timetoken'); + }); + + it('should pass validation with valid parameters', () => { + const request = new RemoveMessageAction(defaultParameters); + assert.equal(request.validate(), undefined); + }); + }); + + describe('operation', () => { + it('should return PNRemoveMessageActionOperation', () => { + const request = new RemoveMessageAction(defaultParameters); + assert.equal(request.operation(), RequestOperation.PNRemoveMessageActionOperation); + }); + }); + + describe('URL construction', () => { + it('should construct correct path', () => { + const request = new RemoveMessageAction(defaultParameters); + const transportRequest = request.request(); + + const expectedPath = `/v1/message-actions/${defaultKeySet.subscribeKey}/channel/${defaultParameters.channel}/message/${defaultParameters.messageTimetoken}/action/${defaultParameters.actionTimetoken}`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should encode special characters in channel name', () => { + const request = new RemoveMessageAction({ + ...defaultParameters, + channel: 'test channel#1/2', + }); + const transportRequest = request.request(); + + assert(transportRequest.path.includes('test%20channel%231%2F2')); + }); + + it('should use DELETE method', () => { + const request = new RemoveMessageAction(defaultParameters); + const transportRequest = request.request(); + + assert.equal(transportRequest.method, TransportMethod.DELETE); + }); + }); + + describe('response parsing', () => { + it('should parse successful response', async () => { + const request = new RemoveMessageAction(defaultParameters); + const mockResponse: TransportResponse = { + status: 200, + url: 'https://test.pubnub.com', + headers: { 'content-type': 'application/json' }, + body: new TextEncoder().encode(JSON.stringify({ + status: 200, + data: {}, + })), + }; + + const parsedResponse = await request.parse(mockResponse); + assert(parsedResponse.data); + assert.equal(typeof parsedResponse.data, 'object'); + }); + }); + + describe('edge cases', () => { + it('should handle undefined channel', () => { + const request = new RemoveMessageAction({ + ...defaultParameters, + channel: undefined as any, + }); + assert.equal(request.validate(), 'Missing message action channel'); + }); + + it('should handle null messageTimetoken', () => { + const request = new RemoveMessageAction({ + ...defaultParameters, + messageTimetoken: null as any, + }); + assert.equal(request.validate(), 'Missing message timetoken'); + }); + + it('should handle null actionTimetoken', () => { + const request = new RemoveMessageAction({ + ...defaultParameters, + actionTimetoken: null as any, + }); + assert.equal(request.validate(), 'Missing action timetoken'); + }); + }); + }); + + describe('boundary value testing', () => { + it('should handle minimum valid action.type length', () => { + const request = new AddMessageActionRequest({ + channel: 'test', + messageTimetoken: '1234567890', + action: { type: 'a', value: 'test' }, + keySet: defaultKeySet, + }); + assert.equal(request.validate(), undefined); + }); + + it('should handle special characters in action values', () => { + const request = new AddMessageActionRequest({ + channel: 'test', + messageTimetoken: '1234567890', + action: { type: 'reaction', value: '!@#$%^&*()_+-={}[]|\\:";\'<>?,./~`' }, + keySet: defaultKeySet, + }); + assert.equal(request.validate(), undefined); + }); + + it('should handle very long action values', () => { + const longValue = 'a'.repeat(1000); + const request = new AddMessageActionRequest({ + channel: 'test', + messageTimetoken: '1234567890', + action: { type: 'reaction', value: longValue }, + keySet: defaultKeySet, + }); + assert.equal(request.validate(), undefined); + }); + + it('should handle numeric strings as timetoken', () => { + const request = new AddMessageActionRequest({ + channel: 'test', + messageTimetoken: '999999999999999999', + action: { type: 'reaction', value: 'test' }, + keySet: defaultKeySet, + }); + assert.equal(request.validate(), undefined); + }); + }); + + describe('concurrent request isolation', () => { + it('should maintain isolated configurations across multiple requests', () => { + const request1 = new AddMessageActionRequest({ + channel: 'channel1', + messageTimetoken: '1111111111', + action: { type: 'reaction', value: 'value1' }, + keySet: defaultKeySet, + }); + + const request2 = new AddMessageActionRequest({ + channel: 'channel2', + messageTimetoken: '2222222222', + action: { type: 'custom', value: 'value2' }, + keySet: { subscribeKey: 'different_key', publishKey: 'pub' }, + }); + + const transport1 = request1.request(); + const transport2 = request2.request(); + + assert(transport1.path.includes('channel1')); + assert(transport1.path.includes('1111111111')); + assert.equal(transport1.body, JSON.stringify({ type: 'reaction', value: 'value1' })); + + assert(transport2.path.includes('channel2')); + assert(transport2.path.includes('2222222222')); + assert(transport2.path.includes('different_key')); + assert.equal(transport2.body, JSON.stringify({ type: 'custom', value: 'value2' })); + }); + }); +}); diff --git a/test/unit/presence/presence_get_state.test.ts b/test/unit/presence/presence_get_state.test.ts new file mode 100644 index 000000000..90d64ba02 --- /dev/null +++ b/test/unit/presence/presence_get_state.test.ts @@ -0,0 +1,365 @@ +import assert from 'assert'; + +import { TransportResponse } from '../../../src/core/types/transport-response'; +import { GetPresenceStateRequest } from '../../../src/core/endpoints/presence/get_state'; +import { KeySet, Payload } from '../../../src/core/types/api'; +import * as Presence from '../../../src/core/types/api/presence'; +import RequestOperation from '../../../src/core/constants/operations'; +import { createMockResponse } from '../test-utils'; + +describe('GetPresenceStateRequest', () => { + let defaultKeySet: KeySet; + let defaultParameters: Presence.GetPresenceStateParameters & { keySet: KeySet; uuid: string }; + + beforeEach(() => { + defaultKeySet = { + publishKey: 'test_publish_key', + subscribeKey: 'test_subscribe_key', + }; + defaultParameters = { + uuid: 'test_uuid', + keySet: defaultKeySet, + }; + }); + + describe('validation', () => { + it('should validate required subscribeKey', () => { + const request = new GetPresenceStateRequest({ + ...defaultParameters, + keySet: { ...defaultKeySet, subscribeKey: '' }, + }); + assert.equal(request.validate(), 'Missing Subscribe Key'); + }); + + it('should pass validation with minimal parameters', () => { + const request = new GetPresenceStateRequest(defaultParameters); + assert.equal(request.validate(), undefined); + }); + + it('should pass validation with channels', () => { + const request = new GetPresenceStateRequest({ + ...defaultParameters, + channels: ['channel1'], + }); + assert.equal(request.validate(), undefined); + }); + + it('should pass validation with channel groups', () => { + const request = new GetPresenceStateRequest({ + ...defaultParameters, + channelGroups: ['group1'], + }); + assert.equal(request.validate(), undefined); + }); + + it('should pass validation with both channels and groups', () => { + const request = new GetPresenceStateRequest({ + ...defaultParameters, + channels: ['channel1'], + channelGroups: ['group1'], + }); + assert.equal(request.validate(), undefined); + }); + }); + + describe('operation', () => { + it('should return correct operation type', () => { + const request = new GetPresenceStateRequest(defaultParameters); + assert.equal(request.operation(), RequestOperation.PNGetStateOperation); + }); + }); + + describe('default parameter handling', () => { + it('should handle default empty arrays', () => { + const request = new GetPresenceStateRequest(defaultParameters); + // Access private field for testing + const params = (request as any).parameters; + assert.deepEqual(params.channels, []); + assert.deepEqual(params.channelGroups, []); + }); + + it('should preserve provided channels', () => { + const request = new GetPresenceStateRequest({ + ...defaultParameters, + channels: ['ch1', 'ch2'], + }); + const params = (request as any).parameters; + assert.deepEqual(params.channels, ['ch1', 'ch2']); + }); + + it('should preserve provided channel groups', () => { + const request = new GetPresenceStateRequest({ + ...defaultParameters, + channelGroups: ['cg1', 'cg2'], + }); + const params = (request as any).parameters; + assert.deepEqual(params.channelGroups, ['cg1', 'cg2']); + }); + }); + + describe('URL construction', () => { + it('should construct correct path with UUID and channels', () => { + const request = new GetPresenceStateRequest({ + ...defaultParameters, + channels: ['channel1'], + }); + const transportRequest = request.request(); + const expectedPath = `/v2/presence/sub-key/${defaultKeySet.subscribeKey}/channel/channel1/uuid/test_uuid`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should construct path for multiple channels', () => { + const request = new GetPresenceStateRequest({ + ...defaultParameters, + channels: ['channel1', 'channel2'], + }); + const transportRequest = request.request(); + const expectedPath = `/v2/presence/sub-key/${defaultKeySet.subscribeKey}/channel/channel1,channel2/uuid/test_uuid`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should encode special characters in channel names', () => { + const request = new GetPresenceStateRequest({ + ...defaultParameters, + channels: ['channel#1', 'channel@2'], + }); + const transportRequest = request.request(); + const expectedPath = `/v2/presence/sub-key/${defaultKeySet.subscribeKey}/channel/channel%231,channel%402/uuid/test_uuid`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should encode special characters in UUID', () => { + const request = new GetPresenceStateRequest({ + ...defaultParameters, + uuid: 'test#uuid@123', + channels: ['channel1'], + }); + const transportRequest = request.request(); + const expectedPath = `/v2/presence/sub-key/${defaultKeySet.subscribeKey}/channel/channel1/uuid/test%23uuid%40123`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should handle empty channels array', () => { + const request = new GetPresenceStateRequest({ + ...defaultParameters, + channels: [], + }); + const transportRequest = request.request(); + const expectedPath = `/v2/presence/sub-key/${defaultKeySet.subscribeKey}/channel/,/uuid/test_uuid`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should handle undefined UUID', () => { + const request = new GetPresenceStateRequest({ + ...defaultParameters, + uuid: undefined as any, // Explicit type assertion for test case + channels: ['channel1'], + }); + const transportRequest = request.request(); + const expectedPath = `/v2/presence/sub-key/${defaultKeySet.subscribeKey}/channel/channel1/uuid/`; + assert.equal(transportRequest.path, expectedPath); + }); + }); + + describe('query parameters', () => { + it('should not include channel-group when no channel groups provided', () => { + const request = new GetPresenceStateRequest({ + ...defaultParameters, + channels: ['channel1'], + }); + const transportRequest = request.request(); + assert.deepEqual(transportRequest.queryParameters, {}); + }); + + it('should include channel-group when provided', () => { + const request = new GetPresenceStateRequest({ + ...defaultParameters, + channelGroups: ['group1', 'group2'], + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.['channel-group'], 'group1,group2'); + }); + + it('should handle empty channel groups array', () => { + const request = new GetPresenceStateRequest({ + ...defaultParameters, + channelGroups: [], + }); + const transportRequest = request.request(); + assert.deepEqual(transportRequest.queryParameters, {}); + }); + + it('should handle single channel group', () => { + const request = new GetPresenceStateRequest({ + ...defaultParameters, + channelGroups: ['single-group'], + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.['channel-group'], 'single-group'); + }); + }); + + describe('response parsing', () => { + it('should parse single channel response', async () => { + const request = new GetPresenceStateRequest({ + ...defaultParameters, + channels: ['channel1'], + channelGroups: [], + }); + const mockState = { status: 'online', age: 25 }; + const mockResponse = createMockResponse({ + status: 200, + message: 'OK', + uuid: 'test_uuid', + payload: mockState, + service: 'Presence', + }); + const result = await request.parse(mockResponse); + + assert.deepEqual(result.channels, { + channel1: mockState, + }); + }); + + it('should parse multiple channels response', async () => { + const request = new GetPresenceStateRequest({ + ...defaultParameters, + channels: ['ch1', 'ch2'], + }); + const mockStates = { + ch1: { status: 'online' }, + ch2: { status: 'away', mood: 'happy' }, + }; + const mockResponse = createMockResponse({ + status: 200, + message: 'OK', + uuid: 'test_uuid', + payload: mockStates, + service: 'Presence', + }); + const result = await request.parse(mockResponse); + + assert.deepEqual(result.channels, mockStates); + }); + + it('should handle empty state response', async () => { + const request = new GetPresenceStateRequest({ + ...defaultParameters, + channels: ['channel1'], + channelGroups: [], + }); + const mockResponse = createMockResponse({ + status: 200, + message: 'OK', + uuid: 'test_uuid', + payload: {}, + service: 'Presence', + }); + const result = await request.parse(mockResponse); + + assert.deepEqual(result.channels, { + channel1: {}, + }); + }); + + it('should handle null state response', async () => { + const request = new GetPresenceStateRequest({ + ...defaultParameters, + channels: ['channel1'], + channelGroups: [], + }); + const mockResponse = createMockResponse({ + status: 200, + message: 'OK', + uuid: 'test_uuid', + payload: null, + service: 'Presence', + }); + const result = await request.parse(mockResponse); + + assert.deepEqual(result.channels, { + channel1: null, + }); + }); + + it('should handle complex state objects', async () => { + const request = new GetPresenceStateRequest({ + ...defaultParameters, + channels: ['channel1'], + channelGroups: [], + }); + const complexState = { + user: { + name: 'John', + preferences: { + theme: 'dark', + notifications: true, + }, + }, + location: { + country: 'US', + city: 'New York', + }, + activity: ['typing', 'online'], + metadata: null, + }; + const mockResponse = createMockResponse({ + status: 200, + message: 'OK', + uuid: 'test_uuid', + payload: complexState, + service: 'Presence', + }); + const result = await request.parse(mockResponse); + + assert.deepEqual(result.channels, { + channel1: complexState, + }); + }); + + it('should determine single vs multiple channels correctly', async () => { + // Test with exactly 1 channel and 0 channel groups + const singleChannelRequest = new GetPresenceStateRequest({ + ...defaultParameters, + channels: ['only-channel'], + channelGroups: [], + }); + + const mockState = { single: true }; + const mockResponse = createMockResponse({ + status: 200, + message: 'OK', + uuid: 'test_uuid', + payload: mockState, + service: 'Presence', + }); + const result = await singleChannelRequest.parse(mockResponse); + + assert.deepEqual(result.channels, { + 'only-channel': mockState, + }); + }); + + it('should handle multiple channels with groups', async () => { + const request = new GetPresenceStateRequest({ + ...defaultParameters, + channels: ['ch1'], + channelGroups: ['group1'], + }); + const mockStates = { + ch1: { status: 'online' }, + 'group1-ch1': { status: 'away' }, + }; + const mockResponse = createMockResponse({ + status: 200, + message: 'OK', + uuid: 'test_uuid', + payload: mockStates, + service: 'Presence', + }); + const result = await request.parse(mockResponse); + + assert.deepEqual(result.channels, mockStates); + }); + }); +}); diff --git a/test/unit/presence/presence_heartbeat.test.ts b/test/unit/presence/presence_heartbeat.test.ts new file mode 100644 index 000000000..7856c4926 --- /dev/null +++ b/test/unit/presence/presence_heartbeat.test.ts @@ -0,0 +1,384 @@ +import assert from 'assert'; + +import { TransportResponse } from '../../../src/core/types/transport-response'; +import { HeartbeatRequest } from '../../../src/core/endpoints/presence/heartbeat'; +import { KeySet } from '../../../src/core/types/api'; +import * as Presence from '../../../src/core/types/api/presence'; +import RequestOperation from '../../../src/core/constants/operations'; +import { createMockResponse } from '../test-utils'; + +describe('HeartbeatRequest', () => { + let defaultKeySet: KeySet; + let defaultParameters: Presence.PresenceHeartbeatParameters & { keySet: KeySet }; + + beforeEach(() => { + defaultKeySet = { + publishKey: 'test_publish_key', + subscribeKey: 'test_subscribe_key', + }; + defaultParameters = { + heartbeat: 300, + channels: ['channel1'], + keySet: defaultKeySet, + }; + }); + + describe('validation', () => { + it('should validate required subscribeKey', () => { + const request = new HeartbeatRequest({ + ...defaultParameters, + keySet: { ...defaultKeySet, subscribeKey: '' }, + }); + assert.equal(request.validate(), 'Missing Subscribe Key'); + }); + + it('should validate channels or channelGroups required', () => { + const request = new HeartbeatRequest({ + ...defaultParameters, + channels: [], + channelGroups: [], + }); + assert.equal(request.validate(), 'Please provide a list of channels and/or channel-groups'); + }); + + it('should validate channels or channelGroups required when undefined', () => { + const request = new HeartbeatRequest({ + ...defaultParameters, + channels: undefined, + channelGroups: undefined, + }); + assert.equal(request.validate(), 'Please provide a list of channels and/or channel-groups'); + }); + + it('should pass validation with channels only', () => { + const request = new HeartbeatRequest({ + ...defaultParameters, + channels: ['channel1'], + channelGroups: undefined, + }); + assert.equal(request.validate(), undefined); + }); + + it('should pass validation with channel groups only', () => { + const request = new HeartbeatRequest({ + ...defaultParameters, + channels: undefined, + channelGroups: ['group1'], + }); + assert.equal(request.validate(), undefined); + }); + + it('should pass validation with both channels and groups', () => { + const request = new HeartbeatRequest({ + ...defaultParameters, + channels: ['channel1'], + channelGroups: ['group1'], + }); + assert.equal(request.validate(), undefined); + }); + + it('should pass validation with empty channels but non-empty groups', () => { + const request = new HeartbeatRequest({ + ...defaultParameters, + channels: [], + channelGroups: ['group1'], + }); + assert.equal(request.validate(), undefined); + }); + + it('should pass validation with non-empty channels but empty groups', () => { + const request = new HeartbeatRequest({ + ...defaultParameters, + channels: ['channel1'], + channelGroups: [], + }); + assert.equal(request.validate(), undefined); + }); + }); + + describe('operation', () => { + it('should return correct operation type', () => { + const request = new HeartbeatRequest(defaultParameters); + assert.equal(request.operation(), RequestOperation.PNHeartbeatOperation); + }); + }); + + describe('constructor options', () => { + it('should set cancellable option', () => { + const request = new HeartbeatRequest(defaultParameters); + // Check that the request was created with cancellable: true + // This is validated by checking the generated transport request + const transportRequest = request.request(); + assert.equal(transportRequest.cancellable, true); + }); + }); + + describe('URL construction', () => { + it('should construct correct path with single channel', () => { + const request = new HeartbeatRequest({ + ...defaultParameters, + channels: ['channel1'], + }); + const transportRequest = request.request(); + const expectedPath = `/v2/presence/sub-key/${defaultKeySet.subscribeKey}/channel/channel1/heartbeat`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should construct path for multiple channels', () => { + const request = new HeartbeatRequest({ + ...defaultParameters, + channels: ['channel1', 'channel2'], + }); + const transportRequest = request.request(); + const expectedPath = `/v2/presence/sub-key/${defaultKeySet.subscribeKey}/channel/channel1,channel2/heartbeat`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should encode special characters in channel names', () => { + const request = new HeartbeatRequest({ + ...defaultParameters, + channels: ['channel#1', 'channel@2'], + }); + const transportRequest = request.request(); + const expectedPath = `/v2/presence/sub-key/${defaultKeySet.subscribeKey}/channel/channel%231,channel%402/heartbeat`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should handle empty channels array', () => { + const request = new HeartbeatRequest({ + ...defaultParameters, + channels: [], + channelGroups: ['group1'], + }); + const transportRequest = request.request(); + const expectedPath = `/v2/presence/sub-key/${defaultKeySet.subscribeKey}/channel/,/heartbeat`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should handle undefined channels', () => { + const request = new HeartbeatRequest({ + ...defaultParameters, + channels: undefined, + channelGroups: ['group1'], + }); + const transportRequest = request.request(); + const expectedPath = `/v2/presence/sub-key/${defaultKeySet.subscribeKey}/channel/,/heartbeat`; + assert.equal(transportRequest.path, expectedPath); + }); + }); + + describe('query parameters', () => { + it('should include heartbeat parameter', () => { + const request = new HeartbeatRequest({ + ...defaultParameters, + heartbeat: 300, + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.heartbeat, '300'); + }); + + it('should include heartbeat parameter with different values', () => { + const testValues = [60, 120, 300, 600, 1800]; + + testValues.forEach(heartbeatValue => { + const request = new HeartbeatRequest({ + ...defaultParameters, + heartbeat: heartbeatValue, + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.heartbeat, heartbeatValue.toString()); + }); + }); + + it('should include state when provided', () => { + const state = { status: 'online' }; + const request = new HeartbeatRequest({ + ...defaultParameters, + state, + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.state, JSON.stringify(state)); + }); + + it('should not include state when not provided', () => { + const request = new HeartbeatRequest(defaultParameters); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.state, undefined); + }); + + it('should include channel groups when provided', () => { + const request = new HeartbeatRequest({ + ...defaultParameters, + channelGroups: ['group1', 'group2'], + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.['channel-group'], 'group1,group2'); + }); + + it('should not include channel-group when empty', () => { + const request = new HeartbeatRequest({ + ...defaultParameters, + channelGroups: [], + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.['channel-group'], undefined); + }); + + it('should handle single channel group', () => { + const request = new HeartbeatRequest({ + ...defaultParameters, + channelGroups: ['single-group'], + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.['channel-group'], 'single-group'); + }); + + it('should serialize complex state objects', () => { + const complexState = { + user: { + name: 'Alice', + activity: 'typing', + }, + preferences: { + notifications: true, + }, + location: 'US', + timestamp: 1234567890, + }; + const request = new HeartbeatRequest({ + ...defaultParameters, + state: complexState, + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.state, JSON.stringify(complexState)); + }); + + it('should serialize null state', () => { + const request = new HeartbeatRequest({ + ...defaultParameters, + state: null as any, // Cast to bypass TypeScript restriction while testing runtime behavior + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.state, 'null'); + }); + + it('should serialize empty state object', () => { + const request = new HeartbeatRequest({ + ...defaultParameters, + state: {}, + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.state, '{}'); + }); + + it('should combine all query parameters', () => { + const state = { active: true }; + const request = new HeartbeatRequest({ + ...defaultParameters, + heartbeat: 450, + channelGroups: ['cg1', 'cg2'], + state, + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.heartbeat, '450'); + assert.equal(transportRequest.queryParameters?.['channel-group'], 'cg1,cg2'); + assert.equal(transportRequest.queryParameters?.state, JSON.stringify(state)); + }); + + it('should handle zero heartbeat value', () => { + const request = new HeartbeatRequest({ + ...defaultParameters, + heartbeat: 0, + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.heartbeat, '0'); + }); + + it('should handle large heartbeat value', () => { + const request = new HeartbeatRequest({ + ...defaultParameters, + heartbeat: 86400, // 24 hours in seconds + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.heartbeat, '86400'); + }); + }); + + describe('response parsing', () => { + it('should parse successful response to empty object', async () => { + const request = new HeartbeatRequest(defaultParameters); + const mockResponse = createMockResponse({ + status: 200, + message: 'OK', + service: 'Presence', + }); + const result = await request.parse(mockResponse); + + assert.deepEqual(result, {}); + }); + + it('should parse successful response with payload to empty object', async () => { + const request = new HeartbeatRequest(defaultParameters); + const mockResponse = createMockResponse({ + status: 200, + message: 'OK', + payload: { some: 'data' }, + service: 'Presence', + }); + const result = await request.parse(mockResponse); + + // Heartbeat response should always return empty object regardless of payload + assert.deepEqual(result, {}); + }); + + it('should handle empty response body', async () => { + const request = new HeartbeatRequest(defaultParameters); + const encoder = new TextEncoder(); + const mockResponse: TransportResponse = { + url: 'https://ps.pndsn.com/v2/presence/sub-key/test/channel/test/heartbeat', + status: 200, + headers: { 'content-type': 'text/javascript' }, + body: encoder.encode(''), + }; + + // Should throw error for empty body + await assert.rejects(async () => { + await request.parse(mockResponse); + }); + }); + }); + + describe('edge cases', () => { + it('should handle very long channel names', () => { + const longChannelName = 'a'.repeat(1000); + const request = new HeartbeatRequest({ + ...defaultParameters, + channels: [longChannelName], + }); + const transportRequest = request.request(); + assert(transportRequest.path.includes(encodeURIComponent(longChannelName))); + }); + + it('should handle many channels', () => { + const manyChannels = Array.from({ length: 100 }, (_, i) => `channel-${i}`); + const request = new HeartbeatRequest({ + ...defaultParameters, + channels: manyChannels, + }); + const transportRequest = request.request(); + const expectedChannelsPart = manyChannels.join(','); + assert(transportRequest.path.includes(expectedChannelsPart)); + }); + + it('should handle many channel groups', () => { + const manyGroups = Array.from({ length: 50 }, (_, i) => `group-${i}`); + const request = new HeartbeatRequest({ + ...defaultParameters, + channelGroups: manyGroups, + }); + const transportRequest = request.request(); + const expectedGroupsPart = manyGroups.join(','); + assert.equal(transportRequest.queryParameters?.['channel-group'], expectedGroupsPart); + }); + }); +}); diff --git a/test/unit/presence/presence_here_now.test.ts b/test/unit/presence/presence_here_now.test.ts new file mode 100644 index 000000000..1ffea149d --- /dev/null +++ b/test/unit/presence/presence_here_now.test.ts @@ -0,0 +1,401 @@ +import assert from 'assert'; + +import { TransportResponse } from '../../../src/core/types/transport-response'; +import { HereNowRequest } from '../../../src/core/endpoints/presence/here_now'; +import { KeySet, Payload } from '../../../src/core/types/api'; +import * as Presence from '../../../src/core/types/api/presence'; +import RequestOperation from '../../../src/core/constants/operations'; +import { createMockResponse } from '../test-utils'; + +describe('HereNowRequest', () => { + let defaultKeySet: KeySet; + let defaultParameters: Presence.HereNowParameters & { keySet: KeySet }; + + beforeEach(() => { + defaultKeySet = { + publishKey: 'test_publish_key', + subscribeKey: 'test_subscribe_key', + }; + defaultParameters = { + keySet: defaultKeySet, + }; + }); + + describe('validation', () => { + it('should validate required subscribeKey', () => { + const request = new HereNowRequest({ + ...defaultParameters, + keySet: { ...defaultKeySet, subscribeKey: '' }, + }); + assert.equal(request.validate(), 'Missing Subscribe Key'); + }); + + it('should pass validation with minimal parameters', () => { + const request = new HereNowRequest(defaultParameters); + assert.equal(request.validate(), undefined); + }); + + it('should pass validation with channels', () => { + const request = new HereNowRequest({ + ...defaultParameters, + channels: ['channel1', 'channel2'], + }); + assert.equal(request.validate(), undefined); + }); + + it('should pass validation with channel groups', () => { + const request = new HereNowRequest({ + ...defaultParameters, + channelGroups: ['group1', 'group2'], + }); + assert.equal(request.validate(), undefined); + }); + + it('should pass validation with both channels and groups', () => { + const request = new HereNowRequest({ + ...defaultParameters, + channels: ['channel1'], + channelGroups: ['group1'], + }); + assert.equal(request.validate(), undefined); + }); + }); + + describe('operation', () => { + it('should return PNGlobalHereNowOperation for empty channels/groups', () => { + const request = new HereNowRequest(defaultParameters); + assert.equal(request.operation(), RequestOperation.PNGlobalHereNowOperation); + }); + + it('should return PNGlobalHereNowOperation for empty arrays', () => { + const request = new HereNowRequest({ + ...defaultParameters, + channels: [], + channelGroups: [], + }); + assert.equal(request.operation(), RequestOperation.PNGlobalHereNowOperation); + }); + + it('should return PNHereNowOperation for specific channels', () => { + const request = new HereNowRequest({ + ...defaultParameters, + channels: ['channel1'], + }); + assert.equal(request.operation(), RequestOperation.PNHereNowOperation); + }); + + it('should return PNHereNowOperation for specific channel groups', () => { + const request = new HereNowRequest({ + ...defaultParameters, + channelGroups: ['group1'], + }); + assert.equal(request.operation(), RequestOperation.PNHereNowOperation); + }); + + it('should return PNHereNowOperation for both channels and groups', () => { + const request = new HereNowRequest({ + ...defaultParameters, + channels: ['channel1'], + channelGroups: ['group1'], + }); + assert.equal(request.operation(), RequestOperation.PNHereNowOperation); + }); + }); + + describe('URL construction', () => { + it('should construct global here now path', () => { + const request = new HereNowRequest(defaultParameters); + const transportRequest = request.request(); + const expectedPath = `/v2/presence/sub-key/${defaultKeySet.subscribeKey}`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should construct path for single channel', () => { + const request = new HereNowRequest({ + ...defaultParameters, + channels: ['channel1'], + }); + const transportRequest = request.request(); + const expectedPath = `/v2/presence/sub-key/${defaultKeySet.subscribeKey}/channel/channel1`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should construct path for multiple channels', () => { + const request = new HereNowRequest({ + ...defaultParameters, + channels: ['channel1', 'channel2'], + }); + const transportRequest = request.request(); + const expectedPath = `/v2/presence/sub-key/${defaultKeySet.subscribeKey}/channel/channel1,channel2`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should encode special characters in channel names', () => { + const request = new HereNowRequest({ + ...defaultParameters, + channels: ['channel#1', 'channel@2'], + }); + const transportRequest = request.request(); + const expectedPath = `/v2/presence/sub-key/${defaultKeySet.subscribeKey}/channel/channel%231,channel%402`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should construct path for channel groups only', () => { + const request = new HereNowRequest({ + ...defaultParameters, + channelGroups: ['group1'], + }); + const transportRequest = request.request(); + const expectedPath = `/v2/presence/sub-key/${defaultKeySet.subscribeKey}/channel/,`; + assert.equal(transportRequest.path, expectedPath); + }); + }); + + describe('query parameters', () => { + it('should include includeUUIDs=true by default', () => { + const request = new HereNowRequest(defaultParameters); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.disable_uuids, undefined); + }); + + it('should set disable_uuids when includeUUIDs is false', () => { + const request = new HereNowRequest({ + ...defaultParameters, + includeUUIDs: false, + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.disable_uuids, '1'); + }); + + it('should include state when includeState is true', () => { + const request = new HereNowRequest({ + ...defaultParameters, + includeState: true, + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.state, '1'); + }); + + it('should include channel groups in query', () => { + const request = new HereNowRequest({ + ...defaultParameters, + channelGroups: ['group1', 'group2'], + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.['channel-group'], 'group1,group2'); + }); + + it('should include custom query parameters', () => { + const request = new HereNowRequest({ + ...defaultParameters, + queryParameters: { custom: 'value', test: 'param' }, + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.custom, 'value'); + assert.equal(transportRequest.queryParameters?.test, 'param'); + }); + + it('should combine all query parameters', () => { + const request = new HereNowRequest({ + ...defaultParameters, + includeUUIDs: false, + includeState: true, + channelGroups: ['group1'], + queryParameters: { custom: 'value' }, + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.disable_uuids, '1'); + assert.equal(transportRequest.queryParameters?.state, '1'); + assert.equal(transportRequest.queryParameters?.['channel-group'], 'group1'); + assert.equal(transportRequest.queryParameters?.custom, 'value'); + }); + }); + + describe('response parsing', () => { + it('should parse single channel response', async () => { + const request = new HereNowRequest({ + ...defaultParameters, + channels: ['channel1'], + }); + const mockResponse = createMockResponse({ + status: 200, + message: 'OK', + uuids: ['uuid1', 'uuid2'], + occupancy: 2, + service: 'Presence', + }); + const result = await request.parse(mockResponse); + + assert.equal(result.totalChannels, 1); + assert.equal(result.totalOccupancy, 2); + assert.deepEqual(result.channels, { + channel1: { + name: 'channel1', + occupancy: 2, + occupants: [ + { uuid: 'uuid1', state: null }, + { uuid: 'uuid2', state: null }, + ], + }, + }); + }); + + it('should parse single channel response without uuids', async () => { + const request = new HereNowRequest({ + ...defaultParameters, + channels: ['channel1'], + includeUUIDs: false, + }); + const mockResponse = createMockResponse({ + status: 200, + message: 'OK', + occupancy: 2, + service: 'Presence', + }); + const result = await request.parse(mockResponse); + + assert.equal(result.totalChannels, 1); + assert.equal(result.totalOccupancy, 2); + assert.deepEqual(result.channels, { + channel1: { + name: 'channel1', + occupancy: 2, + occupants: [], + }, + }); + }); + + it('should parse multiple channels response', async () => { + const request = new HereNowRequest({ + ...defaultParameters, + channels: ['ch1', 'ch2'], + }); + const mockResponse = createMockResponse({ + status: 200, + message: 'OK', + payload: { + total_channels: 2, + total_occupancy: 3, + channels: { + ch1: { uuids: ['uuid1'], occupancy: 1 }, + ch2: { uuids: ['uuid2', 'uuid3'], occupancy: 2 }, + }, + }, + service: 'Presence', + }); + const result = await request.parse(mockResponse); + + assert.equal(result.totalChannels, 2); + assert.equal(result.totalOccupancy, 3); + assert.deepEqual(result.channels, { + ch1: { + name: 'ch1', + occupancy: 1, + occupants: [{ uuid: 'uuid1', state: null }], + }, + ch2: { + name: 'ch2', + occupancy: 2, + occupants: [ + { uuid: 'uuid2', state: null }, + { uuid: 'uuid3', state: null }, + ], + }, + }); + }); + + it('should parse response with state data', async () => { + const request = new HereNowRequest({ + ...defaultParameters, + channels: ['channel1'], + includeState: true, + }); + const mockResponse = createMockResponse({ + status: 200, + message: 'OK', + uuids: [ + 'uuid1', + { uuid: 'uuid2', state: { status: 'online' } }, + ], + occupancy: 2, + service: 'Presence', + }); + const result = await request.parse(mockResponse); + + assert.deepEqual(result.channels, { + channel1: { + name: 'channel1', + occupancy: 2, + occupants: [ + { uuid: 'uuid1', state: null }, + { uuid: 'uuid2', state: { status: 'online' } }, + ], + }, + }); + }); + + it('should handle empty channels response', async () => { + const request = new HereNowRequest(defaultParameters); + const mockResponse = createMockResponse({ + status: 200, + message: 'OK', + payload: { + total_channels: 0, + total_occupancy: 0, + channels: {}, + }, + service: 'Presence', + }); + const result = await request.parse(mockResponse); + + assert.equal(result.totalChannels, 0); + assert.equal(result.totalOccupancy, 0); + assert.deepEqual(result.channels, {}); + }); + + it('should handle response without payload channels', async () => { + const request = new HereNowRequest(defaultParameters); + const mockResponse = createMockResponse({ + status: 200, + message: 'OK', + payload: { + total_channels: 0, + total_occupancy: 0, + }, + service: 'Presence', + }); + const result = await request.parse(mockResponse); + + assert.equal(result.totalChannels, 0); + assert.equal(result.totalOccupancy, 0); + assert.deepEqual(result.channels, {}); + }); + }); + + describe('defaults handling', () => { + it('should apply default values for includeUUIDs and includeState', () => { + const request = new HereNowRequest(defaultParameters); + // Access private field for testing - this validates the defaults are applied + const params = (request as any).parameters; + assert.equal(params.includeUUIDs, true); + assert.equal(params.includeState, false); + }); + + it('should preserve custom values over defaults', () => { + const request = new HereNowRequest({ + ...defaultParameters, + includeUUIDs: false, + includeState: true, + }); + const params = (request as any).parameters; + assert.equal(params.includeUUIDs, false); + assert.equal(params.includeState, true); + }); + + it('should initialize empty queryParameters if not provided', () => { + const request = new HereNowRequest(defaultParameters); + const params = (request as any).parameters; + assert.deepEqual(params.queryParameters, {}); + }); + }); +}); diff --git a/test/unit/presence/presence_leave.test.ts b/test/unit/presence/presence_leave.test.ts new file mode 100644 index 000000000..423c22890 --- /dev/null +++ b/test/unit/presence/presence_leave.test.ts @@ -0,0 +1,420 @@ +import assert from 'assert'; + +import { TransportResponse } from '../../../src/core/types/transport-response'; +import { PresenceLeaveRequest } from '../../../src/core/endpoints/presence/leave'; +import { KeySet } from '../../../src/core/types/api'; +import * as Presence from '../../../src/core/types/api/presence'; +import RequestOperation from '../../../src/core/constants/operations'; +import { createMockResponse } from '../test-utils'; +import { encodeString } from '../../../src/core/utils'; + +describe('PresenceLeaveRequest', () => { + let defaultKeySet: KeySet; + let defaultParameters: Presence.PresenceLeaveParameters & { keySet: KeySet }; + + beforeEach(() => { + defaultKeySet = { + publishKey: 'test_publish_key', + subscribeKey: 'test_subscribe_key', + }; + defaultParameters = { + channels: ['channel1'], + keySet: defaultKeySet, + }; + }); + + describe('validation', () => { + it('should validate required subscribeKey', () => { + const request = new PresenceLeaveRequest({ + ...defaultParameters, + keySet: { ...defaultKeySet, subscribeKey: '' }, + }); + assert.equal(request.validate(), 'Missing Subscribe Key'); + }); + + it('should validate channels or channelGroups required', () => { + const request = new PresenceLeaveRequest({ + ...defaultParameters, + channels: [], + channelGroups: [], + }); + assert.equal(request.validate(), 'At least one `channel` or `channel group` should be provided.'); + }); + + it('should validate channels or channelGroups required when undefined', () => { + const request = new PresenceLeaveRequest({ + ...defaultParameters, + channels: undefined, + channelGroups: undefined, + }); + assert.equal(request.validate(), 'At least one `channel` or `channel group` should be provided.'); + }); + + it('should pass validation with channels only', () => { + const request = new PresenceLeaveRequest({ + ...defaultParameters, + channels: ['channel1'], + channelGroups: undefined, + }); + assert.equal(request.validate(), undefined); + }); + + it('should pass validation with channel groups only', () => { + const request = new PresenceLeaveRequest({ + ...defaultParameters, + channels: undefined, + channelGroups: ['group1'], + }); + assert.equal(request.validate(), undefined); + }); + + it('should pass validation with both channels and groups', () => { + const request = new PresenceLeaveRequest({ + ...defaultParameters, + channels: ['channel1'], + channelGroups: ['group1'], + }); + assert.equal(request.validate(), undefined); + }); + + it('should pass validation with empty channels but non-empty groups', () => { + const request = new PresenceLeaveRequest({ + ...defaultParameters, + channels: [], + channelGroups: ['group1'], + }); + assert.equal(request.validate(), undefined); + }); + + it('should pass validation with non-empty channels but empty groups', () => { + const request = new PresenceLeaveRequest({ + ...defaultParameters, + channels: ['channel1'], + channelGroups: [], + }); + assert.equal(request.validate(), undefined); + }); + }); + + describe('operation', () => { + it('should return correct operation type', () => { + const request = new PresenceLeaveRequest(defaultParameters); + assert.equal(request.operation(), RequestOperation.PNUnsubscribeOperation); + }); + }); + + describe('channel handling', () => { + it('should deduplicate channels', () => { + const request = new PresenceLeaveRequest({ + ...defaultParameters, + channels: ['channel1', 'channel1', 'channel2'], + channelGroups: undefined, + }); + const transportRequest = request.request(); + const expectedPath = `/v2/presence/sub-key/${defaultKeySet.subscribeKey}/channel/channel1,channel2/leave`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should deduplicate channel groups', () => { + const request = new PresenceLeaveRequest({ + ...defaultParameters, + channels: undefined, + channelGroups: ['group1', 'group1', 'group2'], + }); + // Access private field for testing + const params = (request as any).parameters; + assert.deepEqual(params.channelGroups, ['group1', 'group2']); + }); + + it('should sort channels in path', () => { + const request = new PresenceLeaveRequest({ + ...defaultParameters, + channels: ['channelZ', 'channelA', 'channelM'], + }); + const transportRequest = request.request(); + const expectedPath = `/v2/presence/sub-key/${defaultKeySet.subscribeKey}/channel/channelA,channelM,channelZ/leave`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should sort channel groups in query', () => { + const request = new PresenceLeaveRequest({ + ...defaultParameters, + channels: ['channel1'], + channelGroups: ['groupZ', 'groupA', 'groupM'], + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.['channel-group'], 'groupA,groupM,groupZ'); + }); + + it('should handle complex deduplication and sorting', () => { + const request = new PresenceLeaveRequest({ + ...defaultParameters, + channels: ['c', 'a', 'b', 'a', 'c'], + channelGroups: ['g3', 'g1', 'g2', 'g1'], + }); + const transportRequest = request.request(); + + // Channels should be deduplicated and sorted + const expectedPath = `/v2/presence/sub-key/${defaultKeySet.subscribeKey}/channel/a,b,c/leave`; + assert.equal(transportRequest.path, expectedPath); + + // Channel groups should be deduplicated and sorted + assert.equal(transportRequest.queryParameters?.['channel-group'], 'g1,g2,g3'); + }); + + it('should preserve original arrays without mutation', () => { + const originalChannels = ['channel1', 'channel1', 'channel2']; + const originalGroups = ['group1', 'group1', 'group2']; + + new PresenceLeaveRequest({ + ...defaultParameters, + channels: originalChannels, + channelGroups: originalGroups, + }); + + // Original arrays should not be modified + assert.deepEqual(originalChannels, ['channel1', 'channel1', 'channel2']); + assert.deepEqual(originalGroups, ['group1', 'group1', 'group2']); + }); + }); + + describe('URL construction', () => { + it('should construct correct path with single channel', () => { + const request = new PresenceLeaveRequest({ + ...defaultParameters, + channels: ['channel1'], + }); + const transportRequest = request.request(); + const expectedPath = `/v2/presence/sub-key/${defaultKeySet.subscribeKey}/channel/channel1/leave`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should construct path for multiple channels', () => { + const request = new PresenceLeaveRequest({ + ...defaultParameters, + channels: ['channel1', 'channel2'], + }); + const transportRequest = request.request(); + const expectedPath = `/v2/presence/sub-key/${defaultKeySet.subscribeKey}/channel/channel1,channel2/leave`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should encode special characters in channel names', () => { + const request = new PresenceLeaveRequest({ + ...defaultParameters, + channels: ['channel#1', 'channel@2'], + }); + const transportRequest = request.request(); + const expectedPath = `/v2/presence/sub-key/${defaultKeySet.subscribeKey}/channel/channel%231,channel%402/leave`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should handle empty channels array with groups', () => { + const request = new PresenceLeaveRequest({ + ...defaultParameters, + channels: [], + channelGroups: ['group1'], + }); + const transportRequest = request.request(); + const expectedPath = `/v2/presence/sub-key/${defaultKeySet.subscribeKey}/channel/,/leave`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should handle undefined channels with groups', () => { + const request = new PresenceLeaveRequest({ + ...defaultParameters, + channels: undefined, + channelGroups: ['group1'], + }); + const transportRequest = request.request(); + const expectedPath = `/v2/presence/sub-key/${defaultKeySet.subscribeKey}/channel/,/leave`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should handle null channels with groups', () => { + const request = new PresenceLeaveRequest({ + ...defaultParameters, + channels: null as any, + channelGroups: ['group1'], + }); + const transportRequest = request.request(); + const expectedPath = `/v2/presence/sub-key/${defaultKeySet.subscribeKey}/channel/,/leave`; + assert.equal(transportRequest.path, expectedPath); + }); + }); + + describe('query parameters', () => { + it('should not include channel-group when no channel groups provided', () => { + const request = new PresenceLeaveRequest({ + ...defaultParameters, + channels: ['channel1'], + }); + const transportRequest = request.request(); + assert.deepEqual(transportRequest.queryParameters, {}); + }); + + it('should include channel-group when provided', () => { + const request = new PresenceLeaveRequest({ + ...defaultParameters, + channelGroups: ['group1', 'group2'], + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.['channel-group'], 'group1,group2'); + }); + + it('should handle empty channel groups array', () => { + const request = new PresenceLeaveRequest({ + ...defaultParameters, + channelGroups: [], + }); + const transportRequest = request.request(); + assert.deepEqual(transportRequest.queryParameters, {}); + }); + + it('should handle single channel group', () => { + const request = new PresenceLeaveRequest({ + ...defaultParameters, + channelGroups: ['single-group'], + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.['channel-group'], 'single-group'); + }); + + it('should handle undefined channel groups', () => { + const request = new PresenceLeaveRequest({ + ...defaultParameters, + channelGroups: undefined, + }); + const transportRequest = request.request(); + assert.deepEqual(transportRequest.queryParameters, {}); + }); + }); + + describe('response parsing', () => { + it('should parse successful response to empty object', async () => { + const request = new PresenceLeaveRequest(defaultParameters); + const mockResponse = createMockResponse({ + status: 200, + message: 'OK', + action: 'leave', + service: 'Presence', + }); + const result = await request.parse(mockResponse); + + assert.deepEqual(result, {}); + }); + + it('should parse successful response with payload to empty object', async () => { + const request = new PresenceLeaveRequest(defaultParameters); + const mockResponse = createMockResponse({ + status: 200, + message: 'OK', + action: 'leave', + payload: { some: 'data' }, + service: 'Presence', + }); + const result = await request.parse(mockResponse); + + // Leave response should always return empty object regardless of payload + assert.deepEqual(result, {}); + }); + + it('should handle malformed response', async () => { + const request = new PresenceLeaveRequest(defaultParameters); + const mockResponse: TransportResponse = { + url: 'test-url', + status: 200, + headers: {}, + body: new TextEncoder().encode('invalid json').buffer, + }; + + // Should throw error for invalid JSON + await assert.rejects(async () => { + await request.parse(mockResponse); + }); + }); + + it('should handle empty response body', async () => { + const request = new PresenceLeaveRequest(defaultParameters); + const mockResponse: TransportResponse = { + url: 'test-url', + status: 200, + headers: {}, + body: new ArrayBuffer(0), + }; + + // Should throw error for empty body + await assert.rejects(async () => { + await request.parse(mockResponse); + }); + }); + }); + + describe('edge cases', () => { + it('should handle very long channel names', () => { + const longChannelName = 'a'.repeat(1000); + const request = new PresenceLeaveRequest({ + ...defaultParameters, + channels: [longChannelName], + }); + const transportRequest = request.request(); + assert(transportRequest.path.includes(encodeURIComponent(longChannelName))); + }); + + it('should handle many channels', () => { + const manyChannels = Array.from({ length: 100 }, (_, i) => `channel-${i}`); + const request = new PresenceLeaveRequest({ + ...defaultParameters, + channels: manyChannels, + }); + const transportRequest = request.request(); + + // Should be sorted + const sortedChannels = [...manyChannels].sort(); + const expectedChannelsPart = sortedChannels.join(','); + assert(transportRequest.path.includes(expectedChannelsPart)); + }); + + it('should handle many channel groups', () => { + const manyGroups = Array.from({ length: 50 }, (_, i) => `group-${i}`); + const request = new PresenceLeaveRequest({ + ...defaultParameters, + channelGroups: manyGroups, + }); + const transportRequest = request.request(); + + // Should be sorted + const sortedGroups = [...manyGroups].sort(); + const expectedGroupsPart = sortedGroups.join(','); + assert.equal(transportRequest.queryParameters?.['channel-group'], expectedGroupsPart); + }); + + it('should handle channels with special characters requiring sorting', () => { + const specialChannels = ['channel-!', 'channel-@', 'channel-#', 'channel-$']; + const request = new PresenceLeaveRequest({ + ...defaultParameters, + channels: specialChannels, + }); + const transportRequest = request.request(); + + // Should be sorted lexicographically + const sortedChannels = [...specialChannels].sort(); + const expectedPath = `/v2/presence/sub-key/${defaultKeySet.subscribeKey}/channel/${sortedChannels.map(c => encodeString(c)).join(',')}/leave`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should handle mixed case channel names', () => { + const mixedCaseChannels = ['Channel-A', 'channel-b', 'CHANNEL-C', 'channel-D']; + const request = new PresenceLeaveRequest({ + ...defaultParameters, + channels: mixedCaseChannels, + }); + const transportRequest = request.request(); + + // Should maintain case but sort properly + const sortedChannels = [...mixedCaseChannels].sort(); + const expectedChannelsPart = sortedChannels.join(','); + assert(transportRequest.path.includes(expectedChannelsPart)); + }); + }); +}); diff --git a/test/unit/presence/presence_set_state.test.ts b/test/unit/presence/presence_set_state.test.ts new file mode 100644 index 000000000..9d80e1175 --- /dev/null +++ b/test/unit/presence/presence_set_state.test.ts @@ -0,0 +1,417 @@ +import assert from 'assert'; + +import { TransportResponse } from '../../../src/core/types/transport-response'; +import { SetPresenceStateRequest } from '../../../src/core/endpoints/presence/set_state'; +import { KeySet, Payload } from '../../../src/core/types/api'; +import * as Presence from '../../../src/core/types/api/presence'; +import RequestOperation from '../../../src/core/constants/operations'; +import { createMockResponse } from '../test-utils'; + +describe('SetPresenceStateRequest', () => { + let defaultKeySet: KeySet; + let defaultParameters: Presence.SetPresenceStateParameters & { uuid: string; keySet: KeySet }; + + beforeEach(() => { + defaultKeySet = { + publishKey: 'test_publish_key', + subscribeKey: 'test_subscribe_key', + }; + defaultParameters = { + uuid: 'test_uuid', + state: { status: 'online' }, + channels: ['channel1'], + keySet: defaultKeySet, + }; + }); + + describe('validation', () => { + it('should validate required subscribeKey', () => { + const request = new SetPresenceStateRequest({ + ...defaultParameters, + keySet: { ...defaultKeySet, subscribeKey: '' }, + }); + assert.equal(request.validate(), 'Missing Subscribe Key'); + }); + + it('should validate required state', () => { + const request = new SetPresenceStateRequest({ + ...defaultParameters, + state: undefined as any, + }); + assert.equal(request.validate(), 'Missing State'); + }); + + it('should validate channels or channelGroups required', () => { + const request = new SetPresenceStateRequest({ + ...defaultParameters, + channels: [], + channelGroups: [], + }); + assert.equal(request.validate(), 'Please provide a list of channels and/or channel-groups'); + }); + + it('should validate channels or channelGroups required when undefined', () => { + const request = new SetPresenceStateRequest({ + ...defaultParameters, + channels: undefined, + channelGroups: undefined, + }); + assert.equal(request.validate(), 'Please provide a list of channels and/or channel-groups'); + }); + + it('should pass validation with channels only', () => { + const request = new SetPresenceStateRequest({ + ...defaultParameters, + channels: ['channel1'], + channelGroups: undefined, + }); + assert.equal(request.validate(), undefined); + }); + + it('should pass validation with channel groups only', () => { + const request = new SetPresenceStateRequest({ + ...defaultParameters, + channels: undefined, + channelGroups: ['group1'], + }); + assert.equal(request.validate(), undefined); + }); + + it('should pass validation with both channels and groups', () => { + const request = new SetPresenceStateRequest({ + ...defaultParameters, + channels: ['channel1'], + channelGroups: ['group1'], + }); + assert.equal(request.validate(), undefined); + }); + + it('should pass validation with empty state object', () => { + const request = new SetPresenceStateRequest({ + ...defaultParameters, + state: {}, + }); + assert.equal(request.validate(), undefined); + }); + + it('should pass validation with null state', () => { + const request = new SetPresenceStateRequest({ + ...defaultParameters, + state: null as any, + }); + assert.equal(request.validate(), undefined); + }); + + it('should pass validation with complex state', () => { + const request = new SetPresenceStateRequest({ + ...defaultParameters, + state: { + user: { name: 'John', age: 30 }, + preferences: { theme: 'dark' }, + activities: ['typing', 'online'], + }, + }); + assert.equal(request.validate(), undefined); + }); + }); + + describe('operation', () => { + it('should return correct operation type', () => { + const request = new SetPresenceStateRequest(defaultParameters); + assert.equal(request.operation(), RequestOperation.PNSetStateOperation); + }); + }); + + describe('URL construction', () => { + it('should construct correct path with single channel', () => { + const request = new SetPresenceStateRequest({ + ...defaultParameters, + channels: ['channel1'], + }); + const transportRequest = request.request(); + const expectedPath = `/v2/presence/sub-key/${defaultKeySet.subscribeKey}/channel/channel1/uuid/test_uuid/data`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should construct path for multiple channels', () => { + const request = new SetPresenceStateRequest({ + ...defaultParameters, + channels: ['channel1', 'channel2'], + }); + const transportRequest = request.request(); + const expectedPath = `/v2/presence/sub-key/${defaultKeySet.subscribeKey}/channel/channel1,channel2/uuid/test_uuid/data`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should encode special characters in channel names', () => { + const request = new SetPresenceStateRequest({ + ...defaultParameters, + channels: ['channel#1', 'channel@2'], + }); + const transportRequest = request.request(); + const expectedPath = `/v2/presence/sub-key/${defaultKeySet.subscribeKey}/channel/channel%231,channel%402/uuid/test_uuid/data`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should encode special characters in UUID', () => { + const request = new SetPresenceStateRequest({ + ...defaultParameters, + uuid: 'test#uuid@123', + channels: ['channel1'], + }); + const transportRequest = request.request(); + const expectedPath = `/v2/presence/sub-key/${defaultKeySet.subscribeKey}/channel/channel1/uuid/test%23uuid%40123/data`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should handle empty channels array', () => { + const request = new SetPresenceStateRequest({ + ...defaultParameters, + channels: [], + channelGroups: ['group1'], + }); + const transportRequest = request.request(); + const expectedPath = `/v2/presence/sub-key/${defaultKeySet.subscribeKey}/channel/,/uuid/test_uuid/data`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should handle undefined channels', () => { + const request = new SetPresenceStateRequest({ + ...defaultParameters, + channels: undefined, + channelGroups: ['group1'], + }); + const transportRequest = request.request(); + const expectedPath = `/v2/presence/sub-key/${defaultKeySet.subscribeKey}/channel/,/uuid/test_uuid/data`; + assert.equal(transportRequest.path, expectedPath); + }); + }); + + describe('query parameters', () => { + it('should include serialized state', () => { + const state = { status: 'online', mood: 'happy' }; + const request = new SetPresenceStateRequest({ + ...defaultParameters, + state, + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.state, JSON.stringify(state)); + }); + + it('should include channel groups when provided', () => { + const request = new SetPresenceStateRequest({ + ...defaultParameters, + channelGroups: ['group1', 'group2'], + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.['channel-group'], 'group1,group2'); + assert.equal(transportRequest.queryParameters?.state, JSON.stringify(defaultParameters.state)); + }); + + it('should not include channel-group when empty', () => { + const request = new SetPresenceStateRequest({ + ...defaultParameters, + channelGroups: [], + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.['channel-group'], undefined); + }); + + it('should handle single channel group', () => { + const request = new SetPresenceStateRequest({ + ...defaultParameters, + channelGroups: ['single-group'], + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.['channel-group'], 'single-group'); + }); + + it('should serialize complex state objects', () => { + const complexState = { + user: { + name: 'Alice', + details: { + age: 25, + location: 'NYC', + }, + }, + preferences: { + notifications: true, + theme: 'dark', + }, + activities: ['typing', 'online'], + metadata: null, + timestamp: 1234567890, + }; + const request = new SetPresenceStateRequest({ + ...defaultParameters, + state: complexState, + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.state, JSON.stringify(complexState)); + }); + + it('should serialize null state', () => { + const request = new SetPresenceStateRequest({ + ...defaultParameters, + state: null as any, + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.state, 'null'); + }); + + it('should serialize empty state object', () => { + const request = new SetPresenceStateRequest({ + ...defaultParameters, + state: {}, + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.state, '{}'); + }); + + it('should combine state and channel groups', () => { + const state = { active: true }; + const request = new SetPresenceStateRequest({ + ...defaultParameters, + state, + channelGroups: ['cg1', 'cg2'], + }); + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.state, JSON.stringify(state)); + assert.equal(transportRequest.queryParameters?.['channel-group'], 'cg1,cg2'); + }); + }); + + describe('response parsing', () => { + it('should parse successful response', async () => { + const request = new SetPresenceStateRequest(defaultParameters); + const returnedState = { status: 'online', updated: true }; + const mockResponse = createMockResponse({ + status: 200, + message: 'OK', + payload: returnedState, + service: 'Presence', + }); + const result = await request.parse(mockResponse); + + assert.deepEqual(result.state, returnedState); + }); + + it('should handle empty payload response', async () => { + const request = new SetPresenceStateRequest(defaultParameters); + const mockResponse = createMockResponse({ + status: 200, + message: 'OK', + payload: {}, + service: 'Presence', + }); + const result = await request.parse(mockResponse); + + assert.deepEqual(result.state, {}); + }); + + it('should handle null payload response', async () => { + const request = new SetPresenceStateRequest(defaultParameters); + const mockResponse = createMockResponse({ + status: 200, + message: 'OK', + payload: null, + service: 'Presence', + }); + const result = await request.parse(mockResponse); + + assert.deepEqual(result.state, null); + }); + + it('should handle complex payload response', async () => { + const request = new SetPresenceStateRequest(defaultParameters); + const complexState = { + user: { + name: 'Bob', + profile: { + avatar: 'url', + status: 'premium', + }, + }, + settings: { + privacy: 'public', + notifications: { + email: true, + push: false, + }, + }, + lastActivity: { + action: 'message_sent', + timestamp: 1234567890, + channel: 'general', + }, + metadata: ['tag1', 'tag2'], + }; + const mockResponse = createMockResponse({ + status: 200, + message: 'OK', + payload: complexState, + service: 'Presence', + }); + const result = await request.parse(mockResponse); + + assert.deepEqual(result.state, complexState); + }); + + it('should handle array payload response', async () => { + const request = new SetPresenceStateRequest(defaultParameters); + const arrayState = ['active', 'typing', 'away']; + const mockResponse = createMockResponse({ + status: 200, + message: 'OK', + payload: arrayState, + service: 'Presence', + }); + const result = await request.parse(mockResponse); + + assert.deepEqual(result.state, arrayState); + }); + + it('should handle string payload response', async () => { + const request = new SetPresenceStateRequest(defaultParameters); + const stringState = 'online'; + const mockResponse = createMockResponse({ + status: 200, + message: 'OK', + payload: stringState, + service: 'Presence', + }); + const result = await request.parse(mockResponse); + + assert.deepEqual(result.state, stringState); + }); + + it('should handle number payload response', async () => { + const request = new SetPresenceStateRequest(defaultParameters); + const numberState = 42; + const mockResponse = createMockResponse({ + status: 200, + message: 'OK', + payload: numberState, + service: 'Presence', + }); + const result = await request.parse(mockResponse); + + assert.deepEqual(result.state, numberState); + }); + + it('should handle boolean payload response', async () => { + const request = new SetPresenceStateRequest(defaultParameters); + const booleanState = true; + const mockResponse = createMockResponse({ + status: 200, + message: 'OK', + payload: booleanState, + service: 'Presence', + }); + const result = await request.parse(mockResponse); + + assert.deepEqual(result.state, booleanState); + }); + }); +}); diff --git a/test/unit/presence/presence_where_now.test.ts b/test/unit/presence/presence_where_now.test.ts new file mode 100644 index 000000000..fbf103049 --- /dev/null +++ b/test/unit/presence/presence_where_now.test.ts @@ -0,0 +1,146 @@ +import assert from 'assert'; + +import { TransportResponse } from '../../../src/core/types/transport-response'; +import { WhereNowRequest } from '../../../src/core/endpoints/presence/where_now'; +import { KeySet } from '../../../src/core/types/api'; +import * as Presence from '../../../src/core/types/api/presence'; +import RequestOperation from '../../../src/core/constants/operations'; + +import { createMockResponse } from '../test-utils'; + +describe('WhereNowRequest', () => { + let defaultKeySet: KeySet; + let defaultParameters: Required & { keySet: KeySet }; + + beforeEach(() => { + defaultKeySet = { + publishKey: 'test_publish_key', + subscribeKey: 'test_subscribe_key', + }; + defaultParameters = { + uuid: 'test_uuid', + keySet: defaultKeySet, + }; + }); + + describe('validation', () => { + it('should validate required subscribeKey', () => { + const request = new WhereNowRequest({ + ...defaultParameters, + keySet: { ...defaultKeySet, subscribeKey: '' }, + }); + assert.equal(request.validate(), 'Missing Subscribe Key'); + }); + + it('should pass validation with valid parameters', () => { + const request = new WhereNowRequest(defaultParameters); + assert.equal(request.validate(), undefined); + }); + }); + + describe('operation', () => { + it('should return correct operation type', () => { + const request = new WhereNowRequest(defaultParameters); + assert.equal(request.operation(), RequestOperation.PNWhereNowOperation); + }); + }); + + describe('URL construction', () => { + it('should construct correct path with UUID', () => { + const request = new WhereNowRequest(defaultParameters); + const transportRequest = request.request(); + const expectedPath = `/v2/presence/sub-key/${defaultKeySet.subscribeKey}/uuid/test_uuid`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should encode special characters in UUID', () => { + const request = new WhereNowRequest({ + ...defaultParameters, + uuid: 'test#uuid@123', + }); + const transportRequest = request.request(); + const expectedPath = `/v2/presence/sub-key/${defaultKeySet.subscribeKey}/uuid/test%23uuid%40123`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should handle empty UUID', () => { + const request = new WhereNowRequest({ + ...defaultParameters, + uuid: '', + }); + const transportRequest = request.request(); + const expectedPath = `/v2/presence/sub-key/${defaultKeySet.subscribeKey}/uuid/`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should handle UUID with spaces', () => { + const request = new WhereNowRequest({ + ...defaultParameters, + uuid: 'test uuid with spaces', + }); + const transportRequest = request.request(); + const expectedPath = `/v2/presence/sub-key/${defaultKeySet.subscribeKey}/uuid/test%20uuid%20with%20spaces`; + assert.equal(transportRequest.path, expectedPath); + }); + }); + + describe('response parsing', () => { + it('should parse response with channels', async () => { + const request = new WhereNowRequest(defaultParameters); + const mockResponse = createMockResponse({ + status: 200, + message: 'OK', + payload: { channels: ['channel1', 'channel2'] }, + service: 'Presence', + }); + const result = await request.parse(mockResponse); + assert.deepEqual(result.channels, ['channel1', 'channel2']); + }); + + it('should handle empty payload', async () => { + const request = new WhereNowRequest(defaultParameters); + const mockResponse = createMockResponse({ + status: 200, + message: 'OK', + service: 'Presence', + }); + const result = await request.parse(mockResponse); + assert.deepEqual(result.channels, []); + }); + + it('should handle single channel', async () => { + const request = new WhereNowRequest(defaultParameters); + const mockResponse = createMockResponse({ + status: 200, + message: 'OK', + payload: { channels: ['single-channel'] }, + service: 'Presence', + }); + const result = await request.parse(mockResponse); + assert.deepEqual(result.channels, ['single-channel']); + assert.equal(result.channels.length, 1); + }); + + it('should handle many channels', async () => { + const request = new WhereNowRequest(defaultParameters); + const manyChannels = Array.from({ length: 100 }, (_, i) => `channel-${i}`); + const mockResponse = createMockResponse({ + status: 200, + message: 'OK', + payload: { channels: manyChannels }, + service: 'Presence', + }); + const result = await request.parse(mockResponse); + assert.deepEqual(result.channels, manyChannels); + assert.equal(result.channels.length, 100); + }); + }); + + describe('query parameters', () => { + it('should not include any query parameters by default', () => { + const request = new WhereNowRequest(defaultParameters); + const transportRequest = request.request(); + assert.deepEqual(transportRequest.queryParameters, {}); + }); + }); +}); diff --git a/test/unit/publish/publish.test.ts b/test/unit/publish/publish.test.ts new file mode 100644 index 000000000..2655f3585 --- /dev/null +++ b/test/unit/publish/publish.test.ts @@ -0,0 +1,508 @@ +/* global describe, it, beforeEach */ + +import assert from 'assert'; +import { PublishRequest, PublishParameters, PublishResponse } from '../../../src/core/endpoints/publish'; +import { TransportMethod } from '../../../src/core/types/transport-request'; +import { TransportResponse } from '../../../src/core/types/transport-response'; +import RequestOperation from '../../../src/core/constants/operations'; +import { ICryptoModule } from '../../../src/core/interfaces/crypto-module'; +import { KeySet } from '../../../src/core/types/api'; +import { encode } from '../../../src/core/components/base64_codec'; + +describe('PublishRequest', () => { + let defaultKeySet: KeySet; + let defaultParameters: PublishParameters & { keySet: KeySet }; + + beforeEach(() => { + defaultKeySet = { + publishKey: 'test_publish_key', + subscribeKey: 'test_subscribe_key', + }; + + defaultParameters = { + channel: 'test_channel', + message: { test: 'message' }, + keySet: defaultKeySet, + }; + }); + + describe('validation', () => { + it('should validate required parameters', () => { + // Test missing channel + const requestWithoutChannel = new PublishRequest({ + ...defaultParameters, + channel: '', + }); + assert.equal(requestWithoutChannel.validate(), "Missing 'channel'"); + + // Test missing message + const requestWithoutMessage = new PublishRequest({ + ...defaultParameters, + message: undefined as any, + }); + assert.equal(requestWithoutMessage.validate(), "Missing 'message'"); + + // Test missing publishKey + const requestWithoutPublishKey = new PublishRequest({ + ...defaultParameters, + keySet: { ...defaultKeySet, publishKey: '' }, + }); + assert.equal(requestWithoutPublishKey.validate(), "Missing 'publishKey'"); + + // Test valid parameters + const validRequest = new PublishRequest(defaultParameters); + assert.equal(validRequest.validate(), undefined); + }); + }); + + describe('operation', () => { + it('should return PNPublishOperation', () => { + const request = new PublishRequest(defaultParameters); + assert.equal(request.operation(), RequestOperation.PNPublishOperation); + }); + }); + + describe('URL construction', () => { + it('should construct GET URL with encoded payload and channel', () => { + const request = new PublishRequest({ + ...defaultParameters, + channel: 'test channel', + message: { test: 'message' }, + sendByPost: false, + }); + + const transportRequest = request.request(); + assert.equal(transportRequest.method, TransportMethod.GET); + + const expectedPath = `/publish/${defaultKeySet.publishKey}/${defaultKeySet.subscribeKey}/0/test%20channel/0/${encodeURIComponent(JSON.stringify({ test: 'message' }))}`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should construct POST URL without payload in path', () => { + const request = new PublishRequest({ + ...defaultParameters, + sendByPost: true, + }); + + const transportRequest = request.request(); + assert.equal(transportRequest.method, TransportMethod.POST); + + const expectedPath = `/publish/${defaultKeySet.publishKey}/${defaultKeySet.subscribeKey}/0/${defaultParameters.channel}/0`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should encode special character channels', () => { + const specialChannel = 'a b/#'; + const request = new PublishRequest({ + ...defaultParameters, + channel: specialChannel, + sendByPost: false, + }); + + const transportRequest = request.request(); + const encodedChannel = encodeURIComponent(specialChannel).replace(/[!~*'()]/g, (x) => `%${x.charCodeAt(0).toString(16).toUpperCase()}`); + assert(transportRequest.path.includes(encodedChannel)); + }); + }); + + describe('POST method handling', () => { + it('should set correct headers for POST requests', () => { + const request = new PublishRequest({ + ...defaultParameters, + sendByPost: true, + }); + + const transportRequest = request.request(); + assert.equal(transportRequest.headers?.['Content-Type'], 'application/json'); + assert.equal(transportRequest.headers?.['Accept-Encoding'], 'gzip, deflate'); + }); + + it('should include message in body for POST requests', () => { + const testMessage = { test: 'data' }; + const request = new PublishRequest({ + ...defaultParameters, + message: testMessage, + sendByPost: true, + }); + + const transportRequest = request.request(); + assert.equal(transportRequest.body, JSON.stringify(testMessage)); + }); + + it('should not include headers for GET requests', () => { + const request = new PublishRequest({ + ...defaultParameters, + sendByPost: false, + }); + + const transportRequest = request.request(); + assert.notEqual(transportRequest.headers?.['Content-Type'], 'application/json'); + }); + }); + + describe('query parameters mapping', () => { + it('should map storeInHistory parameter', () => { + // Test storeInHistory: true + const requestStoreTrue = new PublishRequest({ + ...defaultParameters, + storeInHistory: true, + }); + const queryParams1 = requestStoreTrue.request().queryParameters; + assert.equal(queryParams1?.store, '1'); + + // Test storeInHistory: false + const requestStoreFalse = new PublishRequest({ + ...defaultParameters, + storeInHistory: false, + }); + const queryParams2 = requestStoreFalse.request().queryParameters; + assert.equal(queryParams2?.store, '0'); + + // Test storeInHistory: undefined (should not be present) + const requestStoreUndefined = new PublishRequest(defaultParameters); + const queryParams3 = requestStoreUndefined.request().queryParameters; + assert.equal(queryParams3?.store, undefined); + }); + + it('should map ttl parameter', () => { + const request = new PublishRequest({ + ...defaultParameters, + ttl: 24, + }); + const queryParams4 = request.request().queryParameters; + assert.equal(queryParams4?.ttl, 24); + }); + + it('should map replicate parameter when false', () => { + const request = new PublishRequest({ + ...defaultParameters, + replicate: false, + }); + const queryParams5 = request.request().queryParameters; + assert.equal(queryParams5?.norep, 'true'); + + // Should not be present when true + const requestReplicateTrue = new PublishRequest({ + ...defaultParameters, + replicate: true, + }); + const queryParams6 = requestReplicateTrue.request().queryParameters; + assert.equal(queryParams6?.norep, undefined); + }); + + it('should map meta parameter when object', () => { + const metaData = { userId: '123', type: 'chat' }; + const request = new PublishRequest({ + ...defaultParameters, + meta: metaData, + }); + const queryParams7 = request.request().queryParameters; + assert.equal(queryParams7?.meta, JSON.stringify(metaData)); + + // Should not be present when not an object + const requestNonObjectMeta = new PublishRequest({ + ...defaultParameters, + meta: 'string_meta' as any, + }); + const queryParams8 = requestNonObjectMeta.request().queryParameters; + assert.equal(queryParams8?.meta, undefined); + }); + + it('should map custom_message_type parameter', () => { + const customType = 'test-message-type'; + const request = new PublishRequest({ + ...defaultParameters, + customMessageType: customType, + }); + const queryParams9 = request.request().queryParameters; + assert.equal(queryParams9?.custom_message_type, customType); + }); + + it('should combine multiple query parameters', () => { + const request = new PublishRequest({ + ...defaultParameters, + storeInHistory: false, + ttl: 12, + replicate: false, + meta: { test: 'meta' }, + customMessageType: 'test-type', + }); + + const queryParams = request.request().queryParameters; + assert.equal(queryParams?.store, '0'); + assert.equal(queryParams?.ttl, 12); + assert.equal(queryParams?.norep, 'true'); + assert.equal(queryParams?.meta, JSON.stringify({ test: 'meta' })); + assert.equal(queryParams?.custom_message_type, 'test-type'); + }); + }); + + describe('encryption handling', () => { + const mockCryptoModule: ICryptoModule = { + set logger(_logger: any) {}, + encrypt: (data: string) => `encrypted_${data}`, + decrypt: (data: string | ArrayBuffer) => data.toString().replace('encrypted_', ''), + encryptFile: undefined as any, + decryptFile: undefined as any, + }; + + it('should encrypt message with cryptoModule for GET', () => { + const testMessage = { secret: 'data' }; + const request = new PublishRequest({ + ...defaultParameters, + message: testMessage, + crypto: mockCryptoModule, + sendByPost: false, + }); + + const transportRequest = request.request(); + const expectedEncryptedMessage = JSON.stringify(`encrypted_${JSON.stringify(testMessage)}`); + assert(transportRequest.path.includes(encodeURIComponent(expectedEncryptedMessage))); + }); + + it('should encrypt message with cryptoModule for POST', () => { + const testMessage = { secret: 'data' }; + const request = new PublishRequest({ + ...defaultParameters, + message: testMessage, + crypto: mockCryptoModule, + sendByPost: true, + }); + + const transportRequest = request.request(); + const expectedEncryptedMessage = JSON.stringify(`encrypted_${JSON.stringify(testMessage)}`); + assert.equal(transportRequest.body, expectedEncryptedMessage); + }); + + it('should handle ArrayBuffer encryption result', () => { + const arrayBufferCrypto: ICryptoModule = { + set logger(_logger: any) {}, + encrypt: (data: string) => { + const buffer = new ArrayBuffer(8); + const view = new Uint8Array(buffer); + view.set([1, 2, 3, 4, 5, 6, 7, 8]); + return buffer; + }, + decrypt: undefined as any, + encryptFile: undefined as any, + decryptFile: undefined as any, + }; + + const request = new PublishRequest({ + ...defaultParameters, + crypto: arrayBufferCrypto, + sendByPost: true, + }); + + const transportRequest = request.request(); + const expectedEncodedBuffer = JSON.stringify(encode(new ArrayBuffer(8))); + // We can't easily compare ArrayBuffer content, so just verify it's a string + assert.equal(typeof transportRequest.body, 'string'); + }); + + it('should handle encryption failure', () => { + const failingCrypto: ICryptoModule = { + set logger(_logger: any) {}, + encrypt: () => { + throw new Error('Encryption failed'); + }, + decrypt: undefined as any, + encryptFile: undefined as any, + decryptFile: undefined as any, + }; + + assert.throws(() => { + new PublishRequest({ + ...defaultParameters, + crypto: failingCrypto, + }).request(); + }, /Encryption failed/); + }); + }); + + describe('payload types support', () => { + it('should handle string payloads', () => { + const request = new PublishRequest({ + ...defaultParameters, + message: 'test string', + sendByPost: false, + }); + + const transportRequest = request.request(); + assert(transportRequest.path.includes(encodeURIComponent(JSON.stringify('test string')))); + }); + + it('should handle number payloads', () => { + const request = new PublishRequest({ + ...defaultParameters, + message: 42, + sendByPost: false, + }); + + const transportRequest = request.request(); + assert(transportRequest.path.includes(encodeURIComponent(JSON.stringify(42)))); + }); + + it('should handle boolean payloads', () => { + const request = new PublishRequest({ + ...defaultParameters, + message: true, + sendByPost: false, + }); + + const transportRequest = request.request(); + assert(transportRequest.path.includes(encodeURIComponent(JSON.stringify(true)))); + }); + + it('should handle object payloads', () => { + const objectMessage = { key: 'value', nested: { prop: 123 } }; + const request = new PublishRequest({ + ...defaultParameters, + message: objectMessage, + sendByPost: false, + }); + + const transportRequest = request.request(); + assert(transportRequest.path.includes(encodeURIComponent(JSON.stringify(objectMessage)))); + }); + + it('should handle array payloads', () => { + const arrayMessage = [1, 2, 'three', { four: 4 }]; + const request = new PublishRequest({ + ...defaultParameters, + message: arrayMessage, + sendByPost: false, + }); + + const transportRequest = request.request(); + assert(transportRequest.path.includes(encodeURIComponent(JSON.stringify(arrayMessage)))); + }); + }); + + describe('response parsing', () => { + it('should parse timetoken from service response tuple', async () => { + const request = new PublishRequest(defaultParameters); + const mockResponse: TransportResponse = { + status: 200, + url: 'https://test.pubnub.com', + headers: { 'content-type': 'application/json' }, + body: new TextEncoder().encode('[1, "Sent", "14647523059145592"]'), + }; + + const parsedResponse = await request.parse(mockResponse); + assert.equal(parsedResponse.timetoken, '14647523059145592'); + }); + + it('should handle different service response formats', async () => { + const request = new PublishRequest(defaultParameters); + const mockResponse: TransportResponse = { + status: 200, + url: 'https://test.pubnub.com', + headers: { 'content-type': 'application/json' }, + body: new TextEncoder().encode('[0, "Failed", "123456789"]'), + }; + + const parsedResponse = await request.parse(mockResponse); + assert.equal(parsedResponse.timetoken, '123456789'); + }); + }); + + describe('request configuration', () => { + it('should default to GET method when sendByPost is false', () => { + const request = new PublishRequest({ + ...defaultParameters, + sendByPost: false, + }); + + const transportRequest = request.request(); + assert.equal(transportRequest.method, TransportMethod.GET); + }); + + it('should use POST method when sendByPost is true', () => { + const request = new PublishRequest({ + ...defaultParameters, + sendByPost: true, + }); + + const transportRequest = request.request(); + assert.equal(transportRequest.method, TransportMethod.POST); + }); + + it('should default sendByPost to false', () => { + const request = new PublishRequest(defaultParameters); + const transportRequest = request.request(); + assert.equal(transportRequest.method, TransportMethod.GET); + }); + + it('should set compressible flag based on sendByPost', () => { + const getRequest = new PublishRequest({ + ...defaultParameters, + sendByPost: false, + }); + assert.equal(getRequest.request().compressible, false); + + const postRequest = new PublishRequest({ + ...defaultParameters, + sendByPost: true, + }); + assert.equal(postRequest.request().compressible, true); + }); + }); + + describe('concurrent operations simulation', () => { + it('should handle multiple publish requests with different methods', () => { + const getRequest = new PublishRequest({ + ...defaultParameters, + channel: 'channel1', + message: 'GET message', + sendByPost: false, + }); + + const postRequest = new PublishRequest({ + ...defaultParameters, + channel: 'channel2', + message: 'POST message', + sendByPost: true, + }); + + const getTransportRequest = getRequest.request(); + const postTransportRequest = postRequest.request(); + + // Verify they maintain their individual configurations + assert.equal(getTransportRequest.method, TransportMethod.GET); + assert.equal(postTransportRequest.method, TransportMethod.POST); + + assert(getTransportRequest.path.includes('channel1')); + assert(postTransportRequest.path.includes('channel2')); + + assert(getTransportRequest.path.includes(encodeURIComponent(JSON.stringify('GET message')))); + assert.equal(postTransportRequest.body, JSON.stringify('POST message')); + }); + + it('should maintain request isolation', () => { + const requests = Array.from({ length: 5 }, (_, i) => + new PublishRequest({ + ...defaultParameters, + channel: `channel_${i}`, + message: `message_${i}`, + sendByPost: i % 2 !== 0, // Alternate between GET and POST + }) + ); + + requests.forEach((request, index) => { + const transportRequest = request.request(); + assert(transportRequest.path.includes(`channel_${index}`)); + + if (index % 2 === 0) { + // GET request + assert.equal(transportRequest.method, TransportMethod.GET); + assert(transportRequest.path.includes(encodeURIComponent(JSON.stringify(`message_${index}`)))); + } else { + // POST request + assert.equal(transportRequest.method, TransportMethod.POST); + assert.equal(transportRequest.body, JSON.stringify(`message_${index}`)); + } + }); + }); + }); +}); diff --git a/test/unit/push_notification/push_add_channels.test.ts b/test/unit/push_notification/push_add_channels.test.ts new file mode 100644 index 000000000..1df6159fe --- /dev/null +++ b/test/unit/push_notification/push_add_channels.test.ts @@ -0,0 +1,272 @@ +/* global describe, it, beforeEach */ + +import assert from 'assert'; +import { AddDevicePushNotificationChannelsRequest } from '../../../src/core/endpoints/push/add_push_channels'; +import RequestOperation from '../../../src/core/constants/operations'; +import { KeySet } from '../../../src/core/types/api'; +import { TransportMethod } from '../../../src/core/types/transport-request'; +import { createMockResponse } from '../test-utils'; + +describe('AddDevicePushNotificationChannelsRequest', () => { + let defaultKeySet: KeySet; + let defaultParameters: any; + + beforeEach(() => { + defaultKeySet = { + subscribeKey: 'test_subscribe_key', + publishKey: 'test_publish_key', + }; + + defaultParameters = { + device: 'test_device_id', + pushGateway: 'gcm', + channels: ['channel1', 'channel2'], + keySet: defaultKeySet, + }; + }); + + describe('validation', () => { + it('should validate required subscribeKey', () => { + const request = new AddDevicePushNotificationChannelsRequest({ + ...defaultParameters, + keySet: { ...defaultKeySet, subscribeKey: '' }, + }); + assert.equal(request.validate(), 'Missing Subscribe Key'); + }); + + it('should validate required device', () => { + const request = new AddDevicePushNotificationChannelsRequest({ + ...defaultParameters, + device: '', + }); + assert.equal(request.validate(), 'Missing Device ID (device)'); + }); + + it('should validate required channels', () => { + const request = new AddDevicePushNotificationChannelsRequest({ + ...defaultParameters, + channels: [], + }); + assert.equal(request.validate(), 'Missing Channels'); + }); + + it('should validate required pushGateway', () => { + const request = new AddDevicePushNotificationChannelsRequest({ + ...defaultParameters, + pushGateway: undefined, + }); + assert.equal(request.validate(), 'Missing GW Type (pushGateway: gcm or apns2)'); + }); + + it('should validate APNS2 topic when using apns2', () => { + const request = new AddDevicePushNotificationChannelsRequest({ + ...defaultParameters, + pushGateway: 'apns2', + topic: undefined, + }); + assert.equal(request.validate(), 'Missing APNS2 topic'); + }); + + it('should pass validation with valid parameters', () => { + const request = new AddDevicePushNotificationChannelsRequest(defaultParameters); + assert.equal(request.validate(), undefined); + }); + + it('should pass validation for APNS2 with topic', () => { + const request = new AddDevicePushNotificationChannelsRequest({ + ...defaultParameters, + pushGateway: 'apns2', + topic: 'com.test.app', + }); + assert.equal(request.validate(), undefined); + }); + }); + + describe('operation', () => { + it('should return PNAddPushNotificationEnabledChannelsOperation', () => { + const request = new AddDevicePushNotificationChannelsRequest(defaultParameters); + assert.equal(request.operation(), RequestOperation.PNAddPushNotificationEnabledChannelsOperation); + }); + }); + + describe('path construction', () => { + it('should construct path for GCM', () => { + const request = new AddDevicePushNotificationChannelsRequest(defaultParameters); + const transportRequest = request.request(); + const expectedPath = `/v1/push/sub-key/${defaultKeySet.subscribeKey}/devices/${defaultParameters.device}`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should construct path for APNS2', () => { + const request = new AddDevicePushNotificationChannelsRequest({ + ...defaultParameters, + pushGateway: 'apns2', + topic: 'com.test.app', + }); + const transportRequest = request.request(); + const expectedPath = `/v2/push/sub-key/${defaultKeySet.subscribeKey}/devices-apns2/${defaultParameters.device}`; + assert.equal(transportRequest.path, expectedPath); + }); + }); + + describe('query parameters', () => { + it('should include required query parameters for GCM', () => { + const request = new AddDevicePushNotificationChannelsRequest(defaultParameters); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.type, 'gcm'); + assert.equal(transportRequest.queryParameters?.add, 'channel1,channel2'); + assert.equal(transportRequest.queryParameters?.environment, undefined); + assert.equal(transportRequest.queryParameters?.topic, undefined); + }); + + it('should include environment and topic for APNS2', () => { + const request = new AddDevicePushNotificationChannelsRequest({ + ...defaultParameters, + pushGateway: 'apns2', + topic: 'com.test.app', + environment: 'production', + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.type, 'apns2'); + assert.equal(transportRequest.queryParameters?.add, 'channel1,channel2'); + assert.equal(transportRequest.queryParameters?.environment, 'production'); + assert.equal(transportRequest.queryParameters?.topic, 'com.test.app'); + }); + + it('should default APNS2 environment to development', () => { + const request = new AddDevicePushNotificationChannelsRequest({ + ...defaultParameters, + pushGateway: 'apns2', + topic: 'com.test.app', + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.environment, 'development'); + }); + + it('should include start and count parameters when provided', () => { + const request = new AddDevicePushNotificationChannelsRequest({ + ...defaultParameters, + start: 'start_token', + count: 50, + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.start, 'start_token'); + assert.equal(transportRequest.queryParameters?.count, 50); + }); + + it('should limit count to maximum value', () => { + const request = new AddDevicePushNotificationChannelsRequest({ + ...defaultParameters, + count: 2000, // Exceeds MAX_COUNT of 1000 + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.count, 1000); + }); + }); + + describe('request method', () => { + it('should use GET method', () => { + const request = new AddDevicePushNotificationChannelsRequest(defaultParameters); + const transportRequest = request.request(); + assert.equal(transportRequest.method, TransportMethod.GET); + }); + }); + + describe('response parsing', () => { + it('should parse successful response', async () => { + const request = new AddDevicePushNotificationChannelsRequest(defaultParameters); + const mockResponse = createMockResponse([1, 'Modified Channels']); + + const result = await request.parse(mockResponse); + assert.deepEqual(result, {}); + }); + + it('should parse error response', async () => { + const request = new AddDevicePushNotificationChannelsRequest(defaultParameters); + const mockResponse = createMockResponse([0, 'Error Message']); + + const result = await request.parse(mockResponse); + assert.deepEqual(result, {}); + }); + }); + + describe('channel handling', () => { + it('should handle single channel', () => { + const request = new AddDevicePushNotificationChannelsRequest({ + ...defaultParameters, + channels: ['single_channel'], + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.add, 'single_channel'); + }); + + it('should handle multiple channels', () => { + const request = new AddDevicePushNotificationChannelsRequest({ + ...defaultParameters, + channels: ['ch1', 'ch2', 'ch3'], + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.add, 'ch1,ch2,ch3'); + }); + + it('should handle channels with special characters', () => { + const request = new AddDevicePushNotificationChannelsRequest({ + ...defaultParameters, + channels: ['channel-1', 'channel_2', 'channel.3'], + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.add, 'channel-1,channel_2,channel.3'); + }); + }); + + describe('device handling', () => { + it('should handle device ID with special characters', () => { + const deviceId = 'device-123_abc.def'; + const request = new AddDevicePushNotificationChannelsRequest({ + ...defaultParameters, + device: deviceId, + }); + const transportRequest = request.request(); + + assert(transportRequest.path.includes(deviceId)); + }); + }); + + describe('environment defaults', () => { + it('should not set environment for GCM', () => { + const request = new AddDevicePushNotificationChannelsRequest({ + ...defaultParameters, + pushGateway: 'gcm', + environment: 'production', // Should be ignored for GCM + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.environment, undefined); + }); + }); + + describe('error conditions', () => { + it('should handle missing channels array', () => { + const { channels, ...parametersWithoutChannels } = defaultParameters; + const request = new AddDevicePushNotificationChannelsRequest(parametersWithoutChannels); + assert.equal(request.validate(), 'Missing Channels'); + }); + + it('should handle invalid pushGateway', () => { + const request = new AddDevicePushNotificationChannelsRequest({ + ...defaultParameters, + pushGateway: 'invalid_gateway' as any, + }); + // Should still validate since base validation only checks for presence + assert.equal(request.validate(), undefined); + }); + }); +}); diff --git a/test/unit/push_notification/push_base.test.ts b/test/unit/push_notification/push_base.test.ts new file mode 100644 index 000000000..9c0e84fbf --- /dev/null +++ b/test/unit/push_notification/push_base.test.ts @@ -0,0 +1,522 @@ +/* global describe, it, beforeEach */ + +import assert from 'assert'; +import { BasePushNotificationChannelsRequest } from '../../../src/core/endpoints/push/push'; +import RequestOperation from '../../../src/core/constants/operations'; +import { KeySet } from '../../../src/core/types/api'; +import { TransportMethod } from '../../../src/core/types/transport-request'; +import { createMockResponse } from '../test-utils'; + +// Concrete implementation for testing the abstract base class +class TestPushNotificationRequest extends BasePushNotificationChannelsRequest { + constructor(parameters: any) { + super(parameters); + } + + operation(): RequestOperation { + return RequestOperation.PNAddPushNotificationEnabledChannelsOperation; + } + + async parse(response: any): Promise { + return this.deserializeResponse(response); + } +} + +describe('BasePushNotificationChannelsRequest', () => { + let defaultKeySet: KeySet; + let defaultParameters: any; + + beforeEach(() => { + defaultKeySet = { + subscribeKey: 'test_subscribe_key', + publishKey: 'test_publish_key', + }; + + defaultParameters = { + device: 'test_device_id', + pushGateway: 'gcm', + channels: ['channel1', 'channel2'], + keySet: defaultKeySet, + action: 'add', + }; + }); + + describe('validation', () => { + it('should validate required subscribeKey', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + keySet: { ...defaultKeySet, subscribeKey: '' }, + }); + assert.equal(request.validate(), 'Missing Subscribe Key'); + }); + + it('should validate missing subscribeKey', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + keySet: { ...defaultKeySet, subscribeKey: undefined }, + }); + assert.equal(request.validate(), 'Missing Subscribe Key'); + }); + + it('should validate required device', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + device: '', + }); + assert.equal(request.validate(), 'Missing Device ID (device)'); + }); + + it('should validate missing device', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + device: undefined, + }); + assert.equal(request.validate(), 'Missing Device ID (device)'); + }); + + it('should validate required channels for add action', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + action: 'add', + channels: [], + }); + assert.equal(request.validate(), 'Missing Channels'); + }); + + it('should validate required channels for remove action', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + action: 'remove', + channels: [], + }); + assert.equal(request.validate(), 'Missing Channels'); + }); + + it('should not validate channels for list action', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + action: 'list', + channels: undefined, + }); + assert.equal(request.validate(), undefined); + }); + + it('should not validate channels for remove-device action', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + action: 'remove-device', + channels: undefined, + }); + assert.equal(request.validate(), undefined); + }); + + it('should validate required pushGateway', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + pushGateway: undefined, + }); + assert.equal(request.validate(), 'Missing GW Type (pushGateway: gcm or apns2)'); + }); + + it('should validate missing pushGateway', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + pushGateway: '', + }); + assert.equal(request.validate(), 'Missing GW Type (pushGateway: gcm or apns2)'); + }); + + it('should validate APNS2 topic when using apns2', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + pushGateway: 'apns2', + topic: undefined, + }); + assert.equal(request.validate(), 'Missing APNS2 topic'); + }); + + it('should validate missing APNS2 topic', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + pushGateway: 'apns2', + topic: '', + }); + assert.equal(request.validate(), 'Missing APNS2 topic'); + }); + + it('should pass validation with valid GCM parameters', () => { + const request = new TestPushNotificationRequest(defaultParameters); + assert.equal(request.validate(), undefined); + }); + + it('should pass validation for APNS2 with topic', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + pushGateway: 'apns2', + topic: 'com.test.app', + }); + assert.equal(request.validate(), undefined); + }); + }); + + describe('path construction', () => { + it('should construct base path for GCM', () => { + const request = new TestPushNotificationRequest(defaultParameters); + const transportRequest = request.request(); + const expectedPath = `/v1/push/sub-key/${defaultKeySet.subscribeKey}/devices/${defaultParameters.device}`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should construct base path for APNS2', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + pushGateway: 'apns2', + topic: 'com.test.app', + }); + const transportRequest = request.request(); + const expectedPath = `/v2/push/sub-key/${defaultKeySet.subscribeKey}/devices-apns2/${defaultParameters.device}`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should append /remove for remove-device action', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + action: 'remove-device', + }); + const transportRequest = request.request(); + const expectedPath = `/v1/push/sub-key/${defaultKeySet.subscribeKey}/devices/${defaultParameters.device}/remove`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should append /remove for remove-device action with APNS2', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + pushGateway: 'apns2', + topic: 'com.test.app', + action: 'remove-device', + }); + const transportRequest = request.request(); + const expectedPath = `/v2/push/sub-key/${defaultKeySet.subscribeKey}/devices-apns2/${defaultParameters.device}/remove`; + assert.equal(transportRequest.path, expectedPath); + }); + }); + + describe('query parameters', () => { + it('should include type parameter for GCM', () => { + const request = new TestPushNotificationRequest(defaultParameters); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.type, 'gcm'); + }); + + it('should include type parameter for APNS2', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + pushGateway: 'apns2', + topic: 'com.test.app', + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.type, 'apns2'); + }); + + it('should include channels for add action', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + action: 'add', + channels: ['ch1', 'ch2'], + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.add, 'ch1,ch2'); + assert.equal(transportRequest.queryParameters?.remove, undefined); + }); + + it('should include channels for remove action', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + action: 'remove', + channels: ['ch1', 'ch2'], + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.remove, 'ch1,ch2'); + assert.equal(transportRequest.queryParameters?.add, undefined); + }); + + it('should not include channels for list action', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + action: 'list', + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.add, undefined); + assert.equal(transportRequest.queryParameters?.remove, undefined); + }); + + it('should not include channels for remove-device action', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + action: 'remove-device', + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.add, undefined); + assert.equal(transportRequest.queryParameters?.remove, undefined); + }); + + it('should include APNS2 environment and topic', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + pushGateway: 'apns2', + topic: 'com.test.app', + environment: 'production', + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.environment, 'production'); + assert.equal(transportRequest.queryParameters?.topic, 'com.test.app'); + }); + + it('should default APNS2 environment to development', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + pushGateway: 'apns2', + topic: 'com.test.app', + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.environment, 'development'); + }); + + it('should not include environment for GCM', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + pushGateway: 'gcm', + environment: 'production', + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.environment, undefined); + }); + + it('should include start parameter when provided', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + start: 'start_token', + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.start, 'start_token'); + }); + + it('should not include start parameter when empty', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + start: '', + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.start, undefined); + }); + + it('should include count parameter when provided and positive', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + count: 50, + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.count, 50); + }); + + it('should not include count parameter when zero', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + count: 0, + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.count, undefined); + }); + + it('should not include count parameter when negative', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + count: -5, + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.count, undefined); + }); + }); + + describe('count limits', () => { + it('should limit count to maximum value', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + count: 2000, // Exceeds MAX_COUNT of 1000 + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.count, 1000); + }); + + it('should preserve count when within limits', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + count: 500, + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.count, 500); + }); + + it('should set count to maximum when exactly at limit', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + count: 1000, + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.count, 1000); + }); + }); + + describe('environment handling', () => { + it('should apply development environment default for APNS2', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + pushGateway: 'apns2', + topic: 'com.test.app', + // No environment specified + }); + + // Check that environment is set in parameters after construction + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.environment, 'development'); + }); + + it('should preserve explicit environment for APNS2', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + pushGateway: 'apns2', + topic: 'com.test.app', + environment: 'production', + }); + + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.environment, 'production'); + }); + + it('should not apply environment default for GCM', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + pushGateway: 'gcm', + // No environment specified + }); + + const transportRequest = request.request(); + assert.equal(transportRequest.queryParameters?.environment, undefined); + }); + }); + + describe('request method', () => { + it('should use GET method', () => { + const request = new TestPushNotificationRequest(defaultParameters); + const transportRequest = request.request(); + assert.equal(transportRequest.method, TransportMethod.GET); + }); + }); + + describe('channel formatting', () => { + it('should format single channel', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + action: 'add', + channels: ['single_channel'], + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.add, 'single_channel'); + }); + + it('should format multiple channels with commas', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + action: 'add', + channels: ['ch1', 'ch2', 'ch3'], + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.add, 'ch1,ch2,ch3'); + }); + + it('should handle empty channel names in array', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + action: 'add', + channels: ['', 'valid_channel', ''], + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.add, ',valid_channel,'); + }); + }); + + describe('abstract method requirements', () => { + it('should throw error when operation() is not overridden in base class', () => { + class IncompleteRequest extends BasePushNotificationChannelsRequest { + constructor(parameters: any) { + super(parameters); + } + // Missing operation() implementation + } + + const request = new IncompleteRequest(defaultParameters); + assert.throws(() => request.operation(), /Should be implemented in subclass/); + }); + }); + + describe('response deserialization', () => { + it('should deserialize valid response', async () => { + const request = new TestPushNotificationRequest(defaultParameters); + const mockResponse = createMockResponse([1, 'Success', 'Extra']); + + const result = await request.parse(mockResponse); + assert.deepEqual(result, [1, 'Success', 'Extra']); + }); + + it('should handle error response', async () => { + const request = new TestPushNotificationRequest(defaultParameters); + const mockResponse = createMockResponse([0, 'Error Message']); + + const result = await request.parse(mockResponse); + assert.deepEqual(result, [0, 'Error Message']); + }); + }); + + describe('parameter combinations', () => { + it('should handle all parameters together', () => { + const request = new TestPushNotificationRequest({ + ...defaultParameters, + pushGateway: 'apns2', + topic: 'com.test.app', + environment: 'production', + action: 'add', + channels: ['ch1', 'ch2'], + start: 'start_token', + count: 100, + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.type, 'apns2'); + assert.equal(transportRequest.queryParameters?.topic, 'com.test.app'); + assert.equal(transportRequest.queryParameters?.environment, 'production'); + assert.equal(transportRequest.queryParameters?.add, 'ch1,ch2'); + assert.equal(transportRequest.queryParameters?.start, 'start_token'); + assert.equal(transportRequest.queryParameters?.count, 100); + }); + }); +}); diff --git a/test/unit/push_notification/push_list_channels.test.ts b/test/unit/push_notification/push_list_channels.test.ts new file mode 100644 index 000000000..8addce7e7 --- /dev/null +++ b/test/unit/push_notification/push_list_channels.test.ts @@ -0,0 +1,313 @@ +/* global describe, it, beforeEach */ + +import assert from 'assert'; +import { ListDevicePushNotificationChannelsRequest } from '../../../src/core/endpoints/push/list_push_channels'; +import RequestOperation from '../../../src/core/constants/operations'; +import { KeySet } from '../../../src/core/types/api'; +import { TransportMethod } from '../../../src/core/types/transport-request'; +import { createMockResponse } from '../test-utils'; + +describe('ListDevicePushNotificationChannelsRequest', () => { + let defaultKeySet: KeySet; + let defaultParameters: any; + + beforeEach(() => { + defaultKeySet = { + subscribeKey: 'test_subscribe_key', + publishKey: 'test_publish_key', + }; + + defaultParameters = { + device: 'test_device_id', + pushGateway: 'gcm', + keySet: defaultKeySet, + }; + }); + + describe('validation', () => { + it('should validate required subscribeKey', () => { + const request = new ListDevicePushNotificationChannelsRequest({ + ...defaultParameters, + keySet: { ...defaultKeySet, subscribeKey: '' }, + }); + assert.equal(request.validate(), 'Missing Subscribe Key'); + }); + + it('should validate required device', () => { + const request = new ListDevicePushNotificationChannelsRequest({ + ...defaultParameters, + device: '', + }); + assert.equal(request.validate(), 'Missing Device ID (device)'); + }); + + it('should validate required pushGateway', () => { + const request = new ListDevicePushNotificationChannelsRequest({ + ...defaultParameters, + pushGateway: undefined, + }); + assert.equal(request.validate(), 'Missing GW Type (pushGateway: gcm or apns2)'); + }); + + it('should validate APNS2 topic when using apns2', () => { + const request = new ListDevicePushNotificationChannelsRequest({ + ...defaultParameters, + pushGateway: 'apns2', + topic: undefined, + }); + assert.equal(request.validate(), 'Missing APNS2 topic'); + }); + + it('should pass validation with valid GCM parameters', () => { + const request = new ListDevicePushNotificationChannelsRequest(defaultParameters); + assert.equal(request.validate(), undefined); + }); + + it('should pass validation for APNS2 with topic', () => { + const request = new ListDevicePushNotificationChannelsRequest({ + ...defaultParameters, + pushGateway: 'apns2', + topic: 'com.test.app', + }); + assert.equal(request.validate(), undefined); + }); + + it('should not require channels for list operation', () => { + const request = new ListDevicePushNotificationChannelsRequest(defaultParameters); + // Channels are not required for list operation + assert.equal(request.validate(), undefined); + }); + }); + + describe('operation', () => { + it('should return PNPushNotificationEnabledChannelsOperation', () => { + const request = new ListDevicePushNotificationChannelsRequest(defaultParameters); + assert.equal(request.operation(), RequestOperation.PNPushNotificationEnabledChannelsOperation); + }); + }); + + describe('path construction', () => { + it('should construct path for GCM', () => { + const request = new ListDevicePushNotificationChannelsRequest(defaultParameters); + const transportRequest = request.request(); + const expectedPath = `/v1/push/sub-key/${defaultKeySet.subscribeKey}/devices/${defaultParameters.device}`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should construct path for APNS2', () => { + const request = new ListDevicePushNotificationChannelsRequest({ + ...defaultParameters, + pushGateway: 'apns2', + topic: 'com.test.app', + }); + const transportRequest = request.request(); + const expectedPath = `/v2/push/sub-key/${defaultKeySet.subscribeKey}/devices-apns2/${defaultParameters.device}`; + assert.equal(transportRequest.path, expectedPath); + }); + }); + + describe('query parameters', () => { + it('should include required query parameters for GCM', () => { + const request = new ListDevicePushNotificationChannelsRequest(defaultParameters); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.type, 'gcm'); + assert.equal(transportRequest.queryParameters?.environment, undefined); + assert.equal(transportRequest.queryParameters?.topic, undefined); + assert.equal(transportRequest.queryParameters?.add, undefined); + assert.equal(transportRequest.queryParameters?.remove, undefined); + }); + + it('should include environment and topic for APNS2', () => { + const request = new ListDevicePushNotificationChannelsRequest({ + ...defaultParameters, + pushGateway: 'apns2', + topic: 'com.test.app', + environment: 'production', + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.type, 'apns2'); + assert.equal(transportRequest.queryParameters?.environment, 'production'); + assert.equal(transportRequest.queryParameters?.topic, 'com.test.app'); + }); + + it('should default APNS2 environment to development', () => { + const request = new ListDevicePushNotificationChannelsRequest({ + ...defaultParameters, + pushGateway: 'apns2', + topic: 'com.test.app', + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.environment, 'development'); + }); + + it('should include start and count parameters when provided', () => { + const request = new ListDevicePushNotificationChannelsRequest({ + ...defaultParameters, + start: 'start_token', + count: 50, + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.start, 'start_token'); + assert.equal(transportRequest.queryParameters?.count, 50); + }); + + it('should limit count to maximum value', () => { + const request = new ListDevicePushNotificationChannelsRequest({ + ...defaultParameters, + count: 2000, // Exceeds MAX_COUNT of 1000 + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.count, 1000); + }); + + it('should not include count when zero', () => { + const request = new ListDevicePushNotificationChannelsRequest({ + ...defaultParameters, + count: 0, + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.count, undefined); + }); + + it('should not include start when empty', () => { + const request = new ListDevicePushNotificationChannelsRequest({ + ...defaultParameters, + start: '', + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.start, undefined); + }); + }); + + describe('request method', () => { + it('should use GET method', () => { + const request = new ListDevicePushNotificationChannelsRequest(defaultParameters); + const transportRequest = request.request(); + assert.equal(transportRequest.method, TransportMethod.GET); + }); + }); + + describe('response parsing', () => { + it('should parse successful response with channels', async () => { + const request = new ListDevicePushNotificationChannelsRequest(defaultParameters); + const mockChannels = ['channel1', 'channel2', 'channel3']; + const mockResponse = createMockResponse(mockChannels); + + const result = await request.parse(mockResponse); + assert.deepEqual(result, { channels: mockChannels }); + }); + + it('should parse successful response with empty channels', async () => { + const request = new ListDevicePushNotificationChannelsRequest(defaultParameters); + const mockChannels: string[] = []; + const mockResponse = createMockResponse(mockChannels); + + const result = await request.parse(mockResponse); + assert.deepEqual(result, { channels: mockChannels }); + }); + + it('should parse response with single channel', async () => { + const request = new ListDevicePushNotificationChannelsRequest(defaultParameters); + const mockChannels = ['single_channel']; + const mockResponse = createMockResponse(mockChannels); + + const result = await request.parse(mockResponse); + assert.deepEqual(result, { channels: mockChannels }); + }); + + it('should parse response with channels containing special characters', async () => { + const request = new ListDevicePushNotificationChannelsRequest(defaultParameters); + const mockChannels = ['channel-1', 'channel_2', 'channel.3']; + const mockResponse = createMockResponse(mockChannels); + + const result = await request.parse(mockResponse); + assert.deepEqual(result, { channels: mockChannels }); + }); + }); + + describe('device handling', () => { + it('should handle device ID with special characters', () => { + const deviceId = 'device-123_abc.def'; + const request = new ListDevicePushNotificationChannelsRequest({ + ...defaultParameters, + device: deviceId, + }); + const transportRequest = request.request(); + + assert(transportRequest.path.includes(deviceId)); + }); + + it('should handle long device IDs', () => { + const longDeviceId = 'a'.repeat(200); + const request = new ListDevicePushNotificationChannelsRequest({ + ...defaultParameters, + device: longDeviceId, + }); + const transportRequest = request.request(); + + assert(transportRequest.path.includes(longDeviceId)); + }); + }); + + describe('pagination', () => { + it('should handle pagination parameters', () => { + const request = new ListDevicePushNotificationChannelsRequest({ + ...defaultParameters, + start: 'pagination_start_token', + count: 100, + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.start, 'pagination_start_token'); + assert.equal(transportRequest.queryParameters?.count, 100); + }); + + it('should handle maximum count limit', () => { + const request = new ListDevicePushNotificationChannelsRequest({ + ...defaultParameters, + count: 1500, // Above 1000 limit + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.count, 1000); + }); + }); + + describe('environment defaults', () => { + it('should not set environment for GCM', () => { + const request = new ListDevicePushNotificationChannelsRequest({ + ...defaultParameters, + pushGateway: 'gcm', + environment: 'production', // Should be ignored for GCM + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.environment, undefined); + }); + }); + + describe('error conditions', () => { + it('should handle missing device gracefully', () => { + const request = new ListDevicePushNotificationChannelsRequest({ + ...defaultParameters, + device: undefined, + }); + assert.equal(request.validate(), 'Missing Device ID (device)'); + }); + + it('should handle null device gracefully', () => { + const request = new ListDevicePushNotificationChannelsRequest({ + ...defaultParameters, + device: null as any, + }); + assert.equal(request.validate(), 'Missing Device ID (device)'); + }); + }); +}); diff --git a/test/unit/push_notification/push_remove_channels.test.ts b/test/unit/push_notification/push_remove_channels.test.ts new file mode 100644 index 000000000..ad74508f2 --- /dev/null +++ b/test/unit/push_notification/push_remove_channels.test.ts @@ -0,0 +1,315 @@ +/* global describe, it, beforeEach */ + +import assert from 'assert'; +import { RemoveDevicePushNotificationChannelsRequest } from '../../../src/core/endpoints/push/remove_push_channels'; +import RequestOperation from '../../../src/core/constants/operations'; +import { KeySet } from '../../../src/core/types/api'; +import { TransportMethod } from '../../../src/core/types/transport-request'; +import { createMockResponse } from '../test-utils'; + +describe('RemoveDevicePushNotificationChannelsRequest', () => { + let defaultKeySet: KeySet; + let defaultParameters: any; + + beforeEach(() => { + defaultKeySet = { + subscribeKey: 'test_subscribe_key', + publishKey: 'test_publish_key', + }; + + defaultParameters = { + device: 'test_device_id', + pushGateway: 'gcm', + channels: ['channel1', 'channel2'], + keySet: defaultKeySet, + }; + }); + + describe('validation', () => { + it('should validate required subscribeKey', () => { + const request = new RemoveDevicePushNotificationChannelsRequest({ + ...defaultParameters, + keySet: { ...defaultKeySet, subscribeKey: '' }, + }); + assert.equal(request.validate(), 'Missing Subscribe Key'); + }); + + it('should validate required device', () => { + const request = new RemoveDevicePushNotificationChannelsRequest({ + ...defaultParameters, + device: '', + }); + assert.equal(request.validate(), 'Missing Device ID (device)'); + }); + + it('should validate required channels', () => { + const request = new RemoveDevicePushNotificationChannelsRequest({ + ...defaultParameters, + channels: [], + }); + assert.equal(request.validate(), 'Missing Channels'); + }); + + it('should validate required pushGateway', () => { + const request = new RemoveDevicePushNotificationChannelsRequest({ + ...defaultParameters, + pushGateway: undefined, + }); + assert.equal(request.validate(), 'Missing GW Type (pushGateway: gcm or apns2)'); + }); + + it('should validate APNS2 topic when using apns2', () => { + const request = new RemoveDevicePushNotificationChannelsRequest({ + ...defaultParameters, + pushGateway: 'apns2', + topic: undefined, + }); + assert.equal(request.validate(), 'Missing APNS2 topic'); + }); + + it('should pass validation with valid GCM parameters', () => { + const request = new RemoveDevicePushNotificationChannelsRequest(defaultParameters); + assert.equal(request.validate(), undefined); + }); + + it('should pass validation for APNS2 with topic', () => { + const request = new RemoveDevicePushNotificationChannelsRequest({ + ...defaultParameters, + pushGateway: 'apns2', + topic: 'com.test.app', + }); + assert.equal(request.validate(), undefined); + }); + }); + + describe('operation', () => { + it('should return PNRemovePushNotificationEnabledChannelsOperation', () => { + const request = new RemoveDevicePushNotificationChannelsRequest(defaultParameters); + assert.equal(request.operation(), RequestOperation.PNRemovePushNotificationEnabledChannelsOperation); + }); + }); + + describe('path construction', () => { + it('should construct path for GCM', () => { + const request = new RemoveDevicePushNotificationChannelsRequest(defaultParameters); + const transportRequest = request.request(); + const expectedPath = `/v1/push/sub-key/${defaultKeySet.subscribeKey}/devices/${defaultParameters.device}`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should construct path for APNS2', () => { + const request = new RemoveDevicePushNotificationChannelsRequest({ + ...defaultParameters, + pushGateway: 'apns2', + topic: 'com.test.app', + }); + const transportRequest = request.request(); + const expectedPath = `/v2/push/sub-key/${defaultKeySet.subscribeKey}/devices-apns2/${defaultParameters.device}`; + assert.equal(transportRequest.path, expectedPath); + }); + }); + + describe('query parameters', () => { + it('should include required query parameters for GCM', () => { + const request = new RemoveDevicePushNotificationChannelsRequest(defaultParameters); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.type, 'gcm'); + assert.equal(transportRequest.queryParameters?.remove, 'channel1,channel2'); + assert.equal(transportRequest.queryParameters?.environment, undefined); + assert.equal(transportRequest.queryParameters?.topic, undefined); + }); + + it('should include environment and topic for APNS2', () => { + const request = new RemoveDevicePushNotificationChannelsRequest({ + ...defaultParameters, + pushGateway: 'apns2', + topic: 'com.test.app', + environment: 'production', + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.type, 'apns2'); + assert.equal(transportRequest.queryParameters?.remove, 'channel1,channel2'); + assert.equal(transportRequest.queryParameters?.environment, 'production'); + assert.equal(transportRequest.queryParameters?.topic, 'com.test.app'); + }); + + it('should default APNS2 environment to development', () => { + const request = new RemoveDevicePushNotificationChannelsRequest({ + ...defaultParameters, + pushGateway: 'apns2', + topic: 'com.test.app', + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.environment, 'development'); + }); + + it('should include start and count parameters when provided', () => { + const request = new RemoveDevicePushNotificationChannelsRequest({ + ...defaultParameters, + start: 'start_token', + count: 50, + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.start, 'start_token'); + assert.equal(transportRequest.queryParameters?.count, 50); + }); + + it('should limit count to maximum value', () => { + const request = new RemoveDevicePushNotificationChannelsRequest({ + ...defaultParameters, + count: 2000, // Exceeds MAX_COUNT of 1000 + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.count, 1000); + }); + }); + + describe('request method', () => { + it('should use GET method', () => { + const request = new RemoveDevicePushNotificationChannelsRequest(defaultParameters); + const transportRequest = request.request(); + assert.equal(transportRequest.method, TransportMethod.GET); + }); + }); + + describe('response parsing', () => { + it('should parse successful response', async () => { + const request = new RemoveDevicePushNotificationChannelsRequest(defaultParameters); + const mockResponse = createMockResponse([1, 'Modified Channels']); + + const result = await request.parse(mockResponse); + assert.deepEqual(result, {}); + }); + + it('should parse error response', async () => { + const request = new RemoveDevicePushNotificationChannelsRequest(defaultParameters); + const mockResponse = createMockResponse([0, 'Error Message']); + + const result = await request.parse(mockResponse); + assert.deepEqual(result, {}); + }); + }); + + describe('channel handling', () => { + it('should handle single channel', () => { + const request = new RemoveDevicePushNotificationChannelsRequest({ + ...defaultParameters, + channels: ['single_channel'], + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.remove, 'single_channel'); + }); + + it('should handle multiple channels', () => { + const request = new RemoveDevicePushNotificationChannelsRequest({ + ...defaultParameters, + channels: ['ch1', 'ch2', 'ch3'], + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.remove, 'ch1,ch2,ch3'); + }); + + it('should handle channels with special characters', () => { + const request = new RemoveDevicePushNotificationChannelsRequest({ + ...defaultParameters, + channels: ['channel-1', 'channel_2', 'channel.3'], + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.remove, 'channel-1,channel_2,channel.3'); + }); + + it('should handle empty channel names', () => { + const request = new RemoveDevicePushNotificationChannelsRequest({ + ...defaultParameters, + channels: ['', 'valid_channel', ''], + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.remove, ',valid_channel,'); + }); + }); + + describe('device handling', () => { + it('should handle device ID with special characters', () => { + const deviceId = 'device-123_abc.def'; + const request = new RemoveDevicePushNotificationChannelsRequest({ + ...defaultParameters, + device: deviceId, + }); + const transportRequest = request.request(); + + assert(transportRequest.path.includes(deviceId)); + }); + }); + + describe('environment defaults', () => { + it('should not set environment for GCM', () => { + const request = new RemoveDevicePushNotificationChannelsRequest({ + ...defaultParameters, + pushGateway: 'gcm', + environment: 'production', // Should be ignored for GCM + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.environment, undefined); + }); + }); + + describe('error conditions', () => { + it('should handle missing channels array', () => { + const { channels, ...parametersWithoutChannels } = defaultParameters; + const request = new RemoveDevicePushNotificationChannelsRequest(parametersWithoutChannels); + assert.equal(request.validate(), 'Missing Channels'); + }); + + it('should handle null channels', () => { + const { channels, ...parametersWithoutChannels } = defaultParameters; + const request = new RemoveDevicePushNotificationChannelsRequest(parametersWithoutChannels); + assert.equal(request.validate(), 'Missing Channels'); + }); + + it('should handle empty channel array', () => { + const request = new RemoveDevicePushNotificationChannelsRequest({ + ...defaultParameters, + channels: [], + }); + assert.equal(request.validate(), 'Missing Channels'); + }); + }); + + describe('consistency with add channels operation', () => { + it('should use same path structure as add operation', () => { + const request = new RemoveDevicePushNotificationChannelsRequest(defaultParameters); + const transportRequest = request.request(); + + // Path should be identical to add operation + const expectedPath = `/v1/push/sub-key/${defaultKeySet.subscribeKey}/devices/${defaultParameters.device}`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should use same query parameter structure as add operation for APNS2', () => { + const request = new RemoveDevicePushNotificationChannelsRequest({ + ...defaultParameters, + pushGateway: 'apns2', + topic: 'com.test.app', + environment: 'production', + }); + const transportRequest = request.request(); + + // Same structure as add, but with 'remove' instead of 'add' + assert.equal(transportRequest.queryParameters?.type, 'apns2'); + assert.equal(transportRequest.queryParameters?.environment, 'production'); + assert.equal(transportRequest.queryParameters?.topic, 'com.test.app'); + assert.equal(transportRequest.queryParameters?.remove, 'channel1,channel2'); + assert.equal(transportRequest.queryParameters?.add, undefined); + }); + }); +}); diff --git a/test/unit/push_notification/push_remove_device.test.ts b/test/unit/push_notification/push_remove_device.test.ts new file mode 100644 index 000000000..f5970996c --- /dev/null +++ b/test/unit/push_notification/push_remove_device.test.ts @@ -0,0 +1,369 @@ +/* global describe, it, beforeEach */ + +import assert from 'assert'; +import { RemoveDevicePushNotificationRequest } from '../../../src/core/endpoints/push/remove_device'; +import RequestOperation from '../../../src/core/constants/operations'; +import { KeySet } from '../../../src/core/types/api'; +import { TransportMethod } from '../../../src/core/types/transport-request'; +import { createMockResponse } from '../test-utils'; + +describe('RemoveDevicePushNotificationRequest', () => { + let defaultKeySet: KeySet; + let defaultParameters: any; + + beforeEach(() => { + defaultKeySet = { + subscribeKey: 'test_subscribe_key', + publishKey: 'test_publish_key', + }; + + defaultParameters = { + device: 'test_device_id', + pushGateway: 'gcm', + keySet: defaultKeySet, + }; + }); + + describe('validation', () => { + it('should validate required subscribeKey', () => { + const request = new RemoveDevicePushNotificationRequest({ + ...defaultParameters, + keySet: { ...defaultKeySet, subscribeKey: '' }, + }); + assert.equal(request.validate(), 'Missing Subscribe Key'); + }); + + it('should validate required device', () => { + const request = new RemoveDevicePushNotificationRequest({ + ...defaultParameters, + device: '', + }); + assert.equal(request.validate(), 'Missing Device ID (device)'); + }); + + it('should validate required pushGateway', () => { + const request = new RemoveDevicePushNotificationRequest({ + ...defaultParameters, + pushGateway: undefined, + }); + assert.equal(request.validate(), 'Missing GW Type (pushGateway: gcm or apns2)'); + }); + + it('should validate APNS2 topic when using apns2', () => { + const request = new RemoveDevicePushNotificationRequest({ + ...defaultParameters, + pushGateway: 'apns2', + topic: undefined, + }); + assert.equal(request.validate(), 'Missing APNS2 topic'); + }); + + it('should pass validation with valid GCM parameters', () => { + const request = new RemoveDevicePushNotificationRequest(defaultParameters); + assert.equal(request.validate(), undefined); + }); + + it('should pass validation for APNS2 with topic', () => { + const request = new RemoveDevicePushNotificationRequest({ + ...defaultParameters, + pushGateway: 'apns2', + topic: 'com.test.app', + }); + assert.equal(request.validate(), undefined); + }); + + it('should not require channels for remove device operation', () => { + const request = new RemoveDevicePushNotificationRequest(defaultParameters); + // Channels are not required for remove device operation + assert.equal(request.validate(), undefined); + }); + }); + + describe('operation', () => { + it('should return PNRemoveAllPushNotificationsOperation', () => { + const request = new RemoveDevicePushNotificationRequest(defaultParameters); + assert.equal(request.operation(), RequestOperation.PNRemoveAllPushNotificationsOperation); + }); + }); + + describe('path construction', () => { + it('should construct path for GCM with /remove suffix', () => { + const request = new RemoveDevicePushNotificationRequest(defaultParameters); + const transportRequest = request.request(); + const expectedPath = `/v1/push/sub-key/${defaultKeySet.subscribeKey}/devices/${defaultParameters.device}/remove`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should construct path for APNS2 with /remove suffix', () => { + const request = new RemoveDevicePushNotificationRequest({ + ...defaultParameters, + pushGateway: 'apns2', + topic: 'com.test.app', + }); + const transportRequest = request.request(); + const expectedPath = `/v2/push/sub-key/${defaultKeySet.subscribeKey}/devices-apns2/${defaultParameters.device}/remove`; + assert.equal(transportRequest.path, expectedPath); + }); + + it('should always include /remove suffix regardless of gateway', () => { + const gcmRequest = new RemoveDevicePushNotificationRequest({ + ...defaultParameters, + pushGateway: 'gcm', + }); + const gcmTransportRequest = gcmRequest.request(); + assert(gcmTransportRequest.path.endsWith('/remove')); + + const apnsRequest = new RemoveDevicePushNotificationRequest({ + ...defaultParameters, + pushGateway: 'apns2', + topic: 'com.test.app', + }); + const apnsTransportRequest = apnsRequest.request(); + assert(apnsTransportRequest.path.endsWith('/remove')); + }); + }); + + describe('query parameters', () => { + it('should include required query parameters for GCM', () => { + const request = new RemoveDevicePushNotificationRequest(defaultParameters); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.type, 'gcm'); + assert.equal(transportRequest.queryParameters?.environment, undefined); + assert.equal(transportRequest.queryParameters?.topic, undefined); + assert.equal(transportRequest.queryParameters?.add, undefined); + assert.equal(transportRequest.queryParameters?.remove, undefined); + }); + + it('should include environment and topic for APNS2', () => { + const request = new RemoveDevicePushNotificationRequest({ + ...defaultParameters, + pushGateway: 'apns2', + topic: 'com.test.app', + environment: 'production', + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.type, 'apns2'); + assert.equal(transportRequest.queryParameters?.environment, 'production'); + assert.equal(transportRequest.queryParameters?.topic, 'com.test.app'); + }); + + it('should default APNS2 environment to development', () => { + const request = new RemoveDevicePushNotificationRequest({ + ...defaultParameters, + pushGateway: 'apns2', + topic: 'com.test.app', + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.environment, 'development'); + }); + + it('should include start and count parameters when provided', () => { + const request = new RemoveDevicePushNotificationRequest({ + ...defaultParameters, + start: 'start_token', + count: 50, + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.start, 'start_token'); + assert.equal(transportRequest.queryParameters?.count, 50); + }); + + it('should limit count to maximum value', () => { + const request = new RemoveDevicePushNotificationRequest({ + ...defaultParameters, + count: 2000, // Exceeds MAX_COUNT of 1000 + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.count, 1000); + }); + + it('should not include channel-related parameters', () => { + const request = new RemoveDevicePushNotificationRequest(defaultParameters); + const transportRequest = request.request(); + + // Remove device should not have add/remove parameters + assert.equal(transportRequest.queryParameters?.add, undefined); + assert.equal(transportRequest.queryParameters?.remove, undefined); + }); + }); + + describe('request method', () => { + it('should use GET method', () => { + const request = new RemoveDevicePushNotificationRequest(defaultParameters); + const transportRequest = request.request(); + assert.equal(transportRequest.method, TransportMethod.GET); + }); + }); + + describe('response parsing', () => { + it('should parse successful response', async () => { + const request = new RemoveDevicePushNotificationRequest(defaultParameters); + const mockResponse = createMockResponse([1, 'Modified Channels']); + + const result = await request.parse(mockResponse); + assert.deepEqual(result, {}); + }); + + it('should parse error response', async () => { + const request = new RemoveDevicePushNotificationRequest(defaultParameters); + const mockResponse = createMockResponse([0, 'Error Message']); + + const result = await request.parse(mockResponse); + assert.deepEqual(result, {}); + }); + + it('should parse response with different status codes', async () => { + const request = new RemoveDevicePushNotificationRequest(defaultParameters); + + // Success case + const successResponse = createMockResponse([1, 'Success']); + const successResult = await request.parse(successResponse); + assert.deepEqual(successResult, {}); + + // Failure case + const failureResponse = createMockResponse([0, 'Device not found']); + const failureResult = await request.parse(failureResponse); + assert.deepEqual(failureResult, {}); + }); + }); + + describe('device handling', () => { + it('should handle device ID with special characters', () => { + const deviceId = 'device-123_abc.def'; + const request = new RemoveDevicePushNotificationRequest({ + ...defaultParameters, + device: deviceId, + }); + const transportRequest = request.request(); + + assert(transportRequest.path.includes(deviceId)); + assert(transportRequest.path.endsWith('/remove')); + }); + + it('should handle long device IDs', () => { + const longDeviceId = 'a'.repeat(200); + const request = new RemoveDevicePushNotificationRequest({ + ...defaultParameters, + device: longDeviceId, + }); + const transportRequest = request.request(); + + assert(transportRequest.path.includes(longDeviceId)); + }); + + it('should handle device ID with URL-encoded characters', () => { + const deviceId = 'device%20with%20spaces'; + const request = new RemoveDevicePushNotificationRequest({ + ...defaultParameters, + device: deviceId, + }); + const transportRequest = request.request(); + + assert(transportRequest.path.includes(deviceId)); + }); + }); + + describe('environment defaults', () => { + it('should not set environment for GCM', () => { + const request = new RemoveDevicePushNotificationRequest({ + ...defaultParameters, + pushGateway: 'gcm', + environment: 'production', // Should be ignored for GCM + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.environment, undefined); + }); + + it('should set development as default for APNS2', () => { + const request = new RemoveDevicePushNotificationRequest({ + ...defaultParameters, + pushGateway: 'apns2', + topic: 'com.test.app', + // No environment specified + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.environment, 'development'); + }); + }); + + describe('error conditions', () => { + it('should handle missing device gracefully', () => { + const request = new RemoveDevicePushNotificationRequest({ + ...defaultParameters, + device: undefined, + }); + assert.equal(request.validate(), 'Missing Device ID (device)'); + }); + + it('should handle null device gracefully', () => { + const request = new RemoveDevicePushNotificationRequest({ + ...defaultParameters, + device: null as any, + }); + assert.equal(request.validate(), 'Missing Device ID (device)'); + }); + + it('should handle empty string device', () => { + const request = new RemoveDevicePushNotificationRequest({ + ...defaultParameters, + device: '', + }); + assert.equal(request.validate(), 'Missing Device ID (device)'); + }); + }); + + describe('difference from channel operations', () => { + it('should use different path suffix than channel operations', () => { + const request = new RemoveDevicePushNotificationRequest(defaultParameters); + const transportRequest = request.request(); + + // Should end with /remove, unlike channel operations + assert(transportRequest.path.endsWith('/remove')); + assert(!transportRequest.path.endsWith('/devices/test_device_id')); + }); + + it('should not include channel parameters', () => { + const request = new RemoveDevicePushNotificationRequest({ + ...defaultParameters, + // These should be ignored since this is device removal + channels: ['channel1', 'channel2'] as any, + }); + const transportRequest = request.request(); + + assert.equal(transportRequest.queryParameters?.add, undefined); + assert.equal(transportRequest.queryParameters?.remove, undefined); + }); + }); + + describe('consistency across gateways', () => { + it('should use consistent structure for both GCM and APNS2', () => { + const gcmRequest = new RemoveDevicePushNotificationRequest({ + ...defaultParameters, + pushGateway: 'gcm', + }); + const gcmTransport = gcmRequest.request(); + + const apnsRequest = new RemoveDevicePushNotificationRequest({ + ...defaultParameters, + pushGateway: 'apns2', + topic: 'com.test.app', + }); + const apnsTransport = apnsRequest.request(); + + // Both should end with /remove + assert(gcmTransport.path.endsWith('/remove')); + assert(apnsTransport.path.endsWith('/remove')); + + // Both should have type parameter + assert.equal(gcmTransport.queryParameters?.type, 'gcm'); + assert.equal(apnsTransport.queryParameters?.type, 'apns2'); + }); + }); +}); diff --git a/test/unit/test-utils.ts b/test/unit/test-utils.ts new file mode 100644 index 000000000..0442bd041 --- /dev/null +++ b/test/unit/test-utils.ts @@ -0,0 +1,17 @@ +import { TransportResponse } from '../../src/core/types/transport-response'; + +/** + * Helper function to create proper TransportResponse with encoded body + * for unit tests. This ensures the body is properly encoded as ArrayBuffer + * instead of a raw string, which is required by the TextDecoder. + */ +export function createMockResponse(data: any, status: number = 200): TransportResponse { + const jsonString = JSON.stringify(data); + const encoder = new TextEncoder(); + return { + url: 'test-url', + status, + body: encoder.encode(jsonString), + headers: { 'content-type': 'text/javascript' }, + }; +}