Socket
Socket
Sign inDemoInstall

@roomservice/browser

Package Overview
Dependencies
2
Maintainers
2
Versions
126
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 3.0.0-8 to 3.0.0-9

1

dist/browser.cjs.development.js

@@ -1609,2 +1609,3 @@ 'use strict';

docsURL: DOCS_URL,
presenceURL: PRESENCE_URL,
authStrategy: _this2.auth,

@@ -1611,0 +1612,0 @@ authCtx: _this2.ctx,

2

dist/browser.cjs.production.min.js

@@ -1,2 +0,2 @@

"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t=(e=require("tiny-invariant"))&&"object"==typeof e&&"default"in e?e.default:e,r=require("@roomservice/core"),n="https://super.roomservice.dev/docs";function s(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function o(e,t,r){return t&&s(e.prototype,t),r&&s(e,r),e}function i(){return(i=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}function c(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function a(e,t){var r;if("undefined"==typeof Symbol||null==e[Symbol.iterator]){if(Array.isArray(e)||(r=function(e,t){if(e){if("string"==typeof e)return c(e,void 0);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?c(e,void 0):void 0}}(e))||t&&e&&"number"==typeof e.length){r&&(e=r);var n=0;return function(){return n>=e.length?{done:!0}:{done:!1,value:e[n++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}return(r=e[Symbol.iterator]()).next.bind(r)}const u=function(){function e(){}return e.prototype.then=function(t,r){const n=new e,s=this.s;if(s){const e=1&s?t:r;if(e){try{h(n,1,e(this.v))}catch(e){h(n,2,e)}return n}return this}return this.o=function(e){try{const s=e.v;1&e.s?h(n,1,t?t(s):s):r?h(n,1,r(s)):h(n,2,s)}catch(e){h(n,2,e)}},n},e}();function h(e,t,r){if(!e.s){if(r instanceof u){if(!r.s)return void(r.o=h.bind(null,e,t));1&t&&(t=r.s),r=r.v}if(r&&r.then)return void r.then(h.bind(null,e,t),h.bind(null,e,2));e.s=t,e.v=r;const n=e.o;n&&n(e)}}function l(e){return e instanceof u&&1&e.s}function d(e,t,r){for(var n;;){var s=e();if(l(s)&&(s=s.v),!s)return o;if(s.then){n=0;break}var o=r();if(o&&o.then){if(!l(o)){n=1;break}o=o.s}if(t){var i=t();if(i&&i.then&&!l(i)){n=2;break}}}var c=new u,a=h.bind(null,c,2);return(0===n?s.then(m):1===n?o.then(d):i.then(f)).then(void 0,a),c;function d(n){o=n;do{if(t&&(i=t())&&i.then&&!l(i))return void i.then(f).then(void 0,a);if(!(s=e())||l(s)&&!s.v)return void h(c,1,o);if(s.then)return void s.then(m).then(void 0,a);l(o=r())&&(o=o.v)}while(!o||!o.then);o.then(d).then(void 0,a)}function m(e){e?(o=r())&&o.then?o.then(d).then(void 0,a):d(o):h(c,1,o)}function f(){(s=e())?s.then?s.then(m).then(void 0,a):m(s):h(c,1,o)}}"undefined"!=typeof Symbol&&(Symbol.iterator||(Symbol.iterator=Symbol("Symbol.iterator"))),"undefined"!=typeof Symbol&&(Symbol.asyncIterator||(Symbol.asyncIterator=Symbol("Symbol.asyncIterator")));var m=function(e,t,r){try{return Promise.resolve(fetch(e+"/"+r,{headers:{Authorization:"Bearer: "+t}})).then((function(e){return Promise.resolve(e.json()).then((function(e){return e.body}))}))}catch(e){return Promise.reject(e)}},f=function(e,t,r){try{return Promise.resolve(fetch(e+"/"+r,{headers:{Authorization:"Bearer: "+t}})).then((function(e){return Promise.resolve(e.json()).then((function(e){for(var t in Object.keys(e))for(var r in Object.keys(e[t]))if("string"==typeof e[t][r].value){var n=void 0;try{n=JSON.parse(e[t][r].value)}catch(e){}n&&(e[t][r].value=n)}return e}))}))}catch(e){return Promise.reject(e)}},p=function(e){try{return Promise.resolve(Promise.all([f(e.presenceURL,e.token,e.roomID),m(e.docsURL,e.token,e.docID)])).then((function(e){return{presence:e[0],document:e[1]}}))}catch(e){return Promise.reject(e)}};function v(e){return new Promise((function(t){return setTimeout(t,e)}))}var b=function(e){try{return Promise.resolve(new Promise((function(t,r){var n=new WebSocket(e);n.onopen=function(){t(n)},n.onerror=function(e){r(e)}})))}catch(e){return Promise.reject(e)}},y=["doc:fwd","presence:fwd","room:rm_guest"],C=function(){function e(e){this.callbacks={},this.lastTime=0,this.msgsThisMilisecond=0,this.docCmdSendQueue=[],this.presenceCmdSendQueue=new Map,this.dispatcher=e.dispatcher,this.wsURL=e.wsURL,this.docsURL=e.docsURL,this.presenceURL=e.presenceURL,this.room=e.room,this.session=e.session,this.wsFactory=e.wsFactory||b,this.bootstrapFetch=e.bootstrapFetch||p,this.wsLoop()}var t=e.prototype;return t.close=function(){this.currentConn&&(this.currentConn.onmessage=null,this.currentConn.onclose=null,this.currentConn.close(),this.currentConn=void 0),this.pendingConn&&(this.pendingConn=void 0),this.dispatcher.startQueueingCmds()},t.connectAndAuth=function(){try{var e=this;return Promise.resolve(e.wsFactory(e.wsURL)).then((function(t){return t.onmessage=function(t){var r=JSON.parse(t.data);e.dispatch(r.type,r.body)},t.onclose=function(){return e.close()},Promise.resolve(t).then((function(t){try{return t.send(e.serializeMsg("guest:authenticate",e.session.token)),Promise.resolve(e.once("guest:authenticated")).then((function(){return t.send(e.serializeMsg("room:join",e.room)),Promise.resolve(e.once("room:joined")).then((function(){return Promise.resolve(e.bootstrapFetch({docID:e.session.docID,roomID:e.session.roomID,docsURL:e.docsURL,presenceURL:e.presenceURL,token:e.session.token})).then((function(r){return e.dispatcher.bootstrap(r),t}))}))}))}catch(e){return Promise.reject(e)}}))}))}catch(e){return Promise.reject(e)}},t.conn=function(){try{var e=this;return e.currentConn?Promise.resolve(e.currentConn):(e.pendingConn||(e.close(),e.pendingConn=function(){try{var t=!1,r=0;return Promise.resolve(d((function(){return!t}),void 0,(function(){var n=r*(Math.random()+1)/2;return Promise.resolve(v(n)).then((function(){return r=Math.min(2*r+100,6e4),function(r,n){try{var s=Promise.resolve(e.connectAndAuth()).then((function(r){return e.currentConn=r,e.pendingConn=void 0,t=!0,r}))}catch(e){return n(e)}return s&&s.then?s.then(void 0,n):s}(0,(function(e){console.error("Connection to RoomService failed with",e,"\nRetrying...")}))}))})))}catch(e){return Promise.reject(e)}}()),Promise.resolve(e.pendingConn))}catch(e){return Promise.reject(e)}},t.wsLoop=function(){try{var e=this,t=d((function(){return!0}),void 0,(function(){return Promise.resolve(e.conn()).then((function(){return e.processSendQueue(),Promise.resolve(v(1e3)).then((function(){}))}))}));return Promise.resolve(t&&t.then?t.then((function(){})):void 0)}catch(e){return Promise.reject(e)}},t.timestamp=function(){var e=Date.now();return e===this.lastTime?this.msgsThisMilisecond++:(this.lastTime=e,this.msgsThisMilisecond=0),e+":"+this.msgsThisMilisecond},t.serializeMsg=function(e,t){var r=this.timestamp();return JSON.stringify({type:e,ts:r,ver:0,body:t})},t.send=function(e,t){if("doc:cmd"==e){if(this.docCmdSendQueue.length>=1e4)throw"RoomService send queue full";var r=this.serializeMsg(e,t);this.docCmdSendQueue.push(r)}if("presence:cmd"==e){var n=this.serializeMsg(e,t);this.presenceCmdSendQueue.set(t.key,n)}this.processSendQueue()},t.processSendQueue=function(){if(this.currentConn)try{for(;this.presenceCmdSendQueue.size>0;){var e=this.presenceCmdSendQueue.entries().next();if(e){var t=e.value,r=t[0];this.currentConn.send(t[1]),this.presenceCmdSendQueue.delete(r)}}for(;this.docCmdSendQueue.length>0;)this.currentConn.send(this.docCmdSendQueue[0]),this.docCmdSendQueue.splice(0,1)}catch(e){console.error(e)}},t.bind=function(e,t){return this.callbacks[e]=this.callbacks[e]||[],this.callbacks[e].push(t),t},t.unbind=function(e,t){this.callbacks[e]=this.callbacks[e].filter((function(e){return e!==t}))},t.dispatch=function(e,t){"error"==e&&console.error(t);var r=this.callbacks[e];if(r)for(var n=0;n<r.length;n++)r[n](t);y.includes(e)&&this.dispatcher.forwardCmd(e,t)},t.once=function(e){try{var t,r=this;return Promise.race([new Promise((function(e,t){return setTimeout((function(){return t("timeout")}),2e3)})),new Promise((function(n){t=r.bind(e,(function(e){n(e)}))}))]).then((function(){t&&r.unbind(e,t)}))}catch(e){return Promise.reject(e)}},e}(),I=function(){function e(e){this.roomID=e.roomID,this.ws=e.ws,this.bus=e.bus,this.actor=e.actor,this.id=e.listID;var n=r.ListInterpreter.newList(e.docID,e.listID,e.actor),s=n.store;this.meta=n.meta,this.store=s,e.checkpoint.lists[e.listID]||t(!1),r.ListInterpreter.importFromRawCheckpoint(this.store,e.checkpoint,this.meta.listID)}var n=e.prototype;return n.bootstrap=function(e){r.ListInterpreter.importFromRawCheckpoint(this.store,e.document,this.meta.listID)},n.sendCmd=function(e){this.ws.send("doc:cmd",{room:this.roomID,args:e}),this.bus.publish({args:e,from:this.actor})},n.clone=function(){return Object.assign(Object.create(Object.getPrototypeOf(this)),this)},n.dangerouslyUpdateClientDirectly=function(e){return r.ListInterpreter.validateCommand(this.meta,e),r.ListInterpreter.applyCommand(this.store,e),this.clone()},n.get=function(e){return r.ListInterpreter.get(this.store,e)},n.set=function(e,t){var n=r.ListInterpreter.runSet(this.store,this.meta,e,t);return this.sendCmd(n),this.clone()},n.delete=function(e){var t=r.ListInterpreter.runDelete(this.store,this.meta,e);return t?(this.sendCmd(t),this.clone()):this.clone()},n.insertAfter=function(e,t){return this.insertAt(e+1,t)},n.insertAt=function(e,t){var n=r.ListInterpreter.runInsertAt(this.store,this.meta,e,t);return this.sendCmd(n),this.clone()},n.push=function(){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];for(var s,o=r.ListInterpreter.runPush.apply(r.ListInterpreter,[this.store,this.meta].concat(t)),i=a(o);!(s=i()).done;){var c=s.value;this.sendCmd(c)}return this},n.map=function(e){return r.ListInterpreter.map(this.store,e)},n.toArray=function(){return r.ListInterpreter.toArray(this.store)},e}(),g=function(){function e(e){this.roomID=e.roomID,this.ws=e.ws,this.bus=e.bus,this.actor=e.actor;var t=r.MapInterpreter.newMap(e.docID,e.mapID),n=t.meta;this.store=t.store,this.meta=n,r.MapInterpreter.importFromRawCheckpoint(this.store,e.checkpoint,this.meta.mapID)}var t=e.prototype;return t.bootstrap=function(e){r.MapInterpreter.importFromRawCheckpoint(this.store,e.document,this.meta.mapID)},t.sendCmd=function(e){this.ws.send("doc:cmd",{room:this.roomID,args:e}),this.bus.publish({from:this.actor,args:e})},t.clone=function(){return Object.assign(Object.create(Object.getPrototypeOf(this)),this)},t.dangerouslyUpdateClientDirectly=function(e){return r.MapInterpreter.validateCommand(this.meta,e),r.MapInterpreter.applyCommand(this.store,e),this.clone()},t.get=function(e){return this.store[e]},t.set=function(e,t){var n=r.MapInterpreter.runSet(this.store,this.meta,e,t);return this.sendCmd(n),this.clone()},t.toObject=function(){for(var e,t={},r=a(this.keys);!(e=r()).done;){var n=e.value;t[n]=this.get(n)}return t},t.delete=function(e){var t=r.MapInterpreter.runDelete(this.store,this.meta,e);return this.sendCmd(t),this.clone()},o(e,[{key:"id",get:function(){return this.meta.mapID}},{key:"keys",get:function(){return Object.keys(this.store)}}]),e}(),D=function(){function e(e){var t=this;this.roomID=e.roomID,this.ws=e.ws,this.actor=e.actor,this.key=e.key,this.cache={},this.bus=e.bus,this.sendPres=function(e,t,r){void 0===r&&(r=!1);var n={},s=!0;return function(){var t=arguments,o=this,i=r&&s,c=function(){e.apply(o,t),n[t[0]]=null};i&&(s=!1,c()),n[arguments[0]]||(n[arguments[0]]=setTimeout(c,40))}}((function(e,r){t.ws.send("presence:cmd",r)})),this.bootstrap(e.checkpoint)}var t=e.prototype;return t.bootstrap=function(e){this.cache=i({},this.cache,e.presence[this.key]||{})},t.getAll=function(){return this.withoutExpired()},t.getMine=function(){var e;return null===(e=(this.cache||{})[this.actor])||void 0===e?void 0:e.value},t.withoutExpired=function(){var e={};for(var t in this.cache){var r=this.cache[t];new Date>r.expAt?delete this.cache[t]:e[t]=r.value}return e},t.withoutActorOrExpired=function(e){var t={};for(var r in this.cache)for(var n in this.cache[r]){var s=this.cache[n];s&&(n===e&&this.cache[n]||new Date>s.expAt?delete this.cache[n]:t[n]=s.value)}return t},t.set=function(e,t){var r=t||60,n=Math.round((new Date).getTime()/1e3)+r;this.sendPres(this.key,{room:this.roomID,key:this.key,value:JSON.stringify(e),expAt:n}),this.cache[this.actor]={value:e,expAt:new Date(1e3*n)};var s=this.withoutExpired();return this.bus.publish({key:this.key,valuesByUser:s}),s},t.dangerouslyUpdateClientDirectly=function(e,t){if("room:rm_guest"===e)return this.withoutActorOrExpired(t.guest);if("presence:expire"===e)return this.withoutExpired();if(t.room!==this.roomID)return!1;if(t.from===this.actor)return!1;var r={expAt:new Date(1e3*t.expAt),value:JSON.parse(t.value)};return this.cache[t.from]=r,this.withoutExpired()},e}(),w=function(){function e(){this.isPublishing=!1,this.subs=new Set}var t=e.prototype;return t.unsubscribe=function(e){this.subs.delete(e)},t.subscribe=function(e){return this.subs.add(e),e},t.publish=function(e){if(this.isPublishing)throw new Error("Infinite loop detected, see more: See more: https://err.sh/getroomservice/browser/infinite-loop");this.isPublishing=!0,this.subs.forEach((function(t){t(e)})),this.isPublishing=!1},e}(),k=["mcreate","mput","mputref","mdel"],j=["lcreate","lins","linsref","lput","lputref","ldel"],S=function(){function e(e){this.presenceClients={},this.listClients={},this.mapClients={},this.expires={},this.queueIncomingCmds=!0,this.cmdQueue=[],this.mapCallbacksByObjID={},this.listCallbacksByObjID={},this.presenceCallbacksByKey={},this.ws=new C({dispatcher:this,wsURL:e.wsURL,docsURL:e.docsURL,presenceURL:e.presenceURL,room:e.roomID,session:e.session}),this.roomID=e.roomID,this.docID=e.bootstrapState.document.id,this.actor=e.actor,this.bootstrapState=e.bootstrapState}var n=e.prototype;return n.forwardCmd=function(e,t){this.queueIncomingCmds?this.cmdQueue.push([e,t]):this.processCmd(e,t)},n.processCmd=function(e,t){"doc:fwd"==e&&"args"in t&&this.dispatchDocCmd(t),"presence:fwd"==e&&"expAt"in t&&this.dispatchPresenceCmd(t),"room:rm_guest"==e&&"guest"in t&&this.dispatchRmGuest(t)},n.bootstrap=function(e){this.bootstrapState=e;for(var t=0,r=Object.entries(this.listClients);t<r.length;t++)r[t][1].bootstrap(e);for(var n=0,s=Object.entries(this.mapClients);n<s.length;n++)s[n][1].bootstrap(e);for(var o=0,i=Object.entries(this.presenceClients);o<i.length;o++)i[o][1].bootstrap(e);this.queueIncomingCmds=!1;for(var c,u=a(this.cmdQueue);!(c=u()).done;){var h=c.value;this.processCmd(h[0],h[1])}this.cmdQueue.length=0},n.startQueueingCmds=function(){this.queueIncomingCmds=!0},n.dispatchDocCmd=function(e){if(e.room===this.roomID)if(!e.args||e.args.length<3)console.error("Unexpected command: ",e.args);else if(!r.vsReader(atob).isOlderVS(e.vs,this.bootstrapState.document.vs)&&e.from!==this.actor){var t=[e.args[0],e.args[1],e.args[2]],n=t[0],s=t[2];t[1]===this.docID&&(k.includes(n)?this.dispatchMapCmd(s,e):j.includes(n)?this.dispatchListCmd(s,e):console.warn("Unhandled Room Service doc:fwd command: "+n+". Consider updating the Room Service client."))}},n.dispatchRmGuest=function(e){if(e.room===this.roomID)for(var t=0,r=Object.entries(this.presenceClients);t<r.length;t++)for(var n,s=r[t],o=s[0],i=s[1].dangerouslyUpdateClientDirectly("room:rm_guest",e),c=a(this.presenceCallbacksByKey[o]||[]);!(n=c()).done;)(0,n.value)(i,e.guest)},n.dispatchMapCmd=function(e,t){this.mapClients[e]||this.createMapLocally(e);for(var r,n=this.mapClients[e].dangerouslyUpdateClientDirectly(t.args),s=a(this.mapCallbacksByObjID[e]||[]);!(r=s()).done;)(0,r.value)(n.toObject(),t.from)},n.dispatchListCmd=function(e,t){this.listClients[e]||this.createListLocally(e);for(var r,n=this.listClients[e].dangerouslyUpdateClientDirectly(t.args),s=a(this.listCallbacksByObjID[e]||[]);!(r=s()).done;)(0,r.value)(n.toArray(),t.from)},n.dispatchPresenceCmd=function(e){var t=this;if(e.room===this.roomID&&e.from!==this.actor){var r=e.key,n=this.presence(r),s=(new Date).getTime()/1e3,o=e.expAt-s;if(!(o<0)){if(o<43200){this.expires[r]&&clearTimeout(this.expires[r]);var i=setTimeout((function(){var s=n.dangerouslyUpdateClientDirectly("presence:expire",{key:e.key});if(s)for(var o,i=a(null!==(c=t.presenceCallbacksByKey[r])&&void 0!==c?c:[]);!(o=i()).done;){var c;(0,o.value)(s,e.from)}}),1e3*o);this.expires[r]=i}var c=n.dangerouslyUpdateClientDirectly("presence:fwd",e);if(c)for(var u,h=a(null!==(l=this.presenceCallbacksByKey[r])&&void 0!==l?l:[]);!(u=h()).done;){var l;(0,u.value)(c,e.from)}}}},n.createListLocally=function(e){var t=this,r=new w;r.subscribe((function(r){for(var n,s=t.listClients[e],o=a(t.listCallbacksByObjID[e]||[]);!(n=o()).done;)(0,n.value)(s.toArray(),r.from)}));var n=new I({checkpoint:this.bootstrapState.document,roomID:this.roomID,docID:this.docID,listID:e,ws:this.ws,actor:this.actor,bus:r});return this.listClients[e]=n,n},n.list=function(e){return this.listClients[e]?this.listClients[e]:(this.bootstrapState.document.lists[e]||(this.ws.send("doc:cmd",{args:["lcreate",this.docID,e],room:this.roomID}),this.bootstrapState.document.lists[e]={afters:[],ids:[],values:[]}),this.createListLocally(e))},n.createMapLocally=function(e){var t=this,r=new w;r.subscribe((function(r){for(var n,s=t.mapClients[e],o=a(t.mapCallbacksByObjID[e]||[]);!(n=o()).done;)(0,n.value)(s.toObject(),r.from)}));var n=new g({checkpoint:this.bootstrapState.document,roomID:this.roomID,docID:this.docID,mapID:e,ws:this.ws,bus:r,actor:this.actor});return this.mapClients[e]=n,n},n.map=function(e){return this.mapClients[e]?this.mapClients[e]:(this.bootstrapState.document.maps[e]||this.ws.send("doc:cmd",{args:["mcreate",this.docID,e],room:this.roomID}),this.createMapLocally(e))},n.presence=function(e){var t=this;if(this.presenceClients[e])return this.presenceClients[e];var r=new w;r.subscribe((function(e){for(var r,n=a(t.presenceCallbacksByKey[e.key]||[]);!(r=n()).done;)(0,r.value)(e.valuesByUser,t.actor)}));var n=new D({checkpoint:this.bootstrapState,roomID:this.roomID,ws:this.ws,actor:this.actor,key:e,bus:r});try{this.presenceClients[e]=n}catch(e){throw new Error("Don't Freeze State. See more: https://err.sh/getroomservice/browser/dont-freeze")}return this.presenceClients[e]},n.subscribe=function(e,t){if(e instanceof D)return this.subscribePresence(e,t);var r,n=function(e,r){t(e,r)};return e instanceof g&&(this.mapCallbacksByObjID[r=e.id]=this.mapCallbacksByObjID[r]||[],this.mapCallbacksByObjID[r].push(n)),e instanceof I&&(this.listCallbacksByObjID[r=e.id]=this.listCallbacksByObjID[r]||[],this.listCallbacksByObjID[r].push(n)),[{objID:r,fn:n}]},n.subscribePresence=function(e,r){e||t(!1);var n=function(e,t){r&&r(e,t)},s=e.key;return this.presenceCallbacksByKey[s]=this.presenceCallbacksByKey[s]||[],this.presenceCallbacksByKey[s].push(n),[{objID:s,fn:n}]},n.unsubscribe=function(e){for(var t,r=a(e);!(t=r()).done;){var n=t.value;n.objID&&(this.mapCallbacksByObjID[n.objID]=P(this.mapCallbacksByObjID[n.objID],n.fn),this.listCallbacksByObjID[n.objID]=P(this.listCallbacksByObjID[n.objID],n.fn),this.presenceCallbacksByKey[n.objID]=P(this.presenceCallbacksByKey[n.objID],n.fn))}},o(e,[{key:"me",get:function(){return this.actor}}]),e}();function P(e,t){return e?e.filter((function(e){return e!==t})):[]}var L=function(){function e(e){this.roomClients={},this.auth=e.auth,this.ctx=e.ctx||{}}return e.prototype.room=function(e){try{var t=this;return t.roomClients[e]?Promise.resolve(t.roomClients[e]):Promise.resolve(function(e){try{return Promise.resolve(function(e,t,r,n){try{var s=function(t){return o?t:Promise.resolve(fetch(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({resources:[{object:"document",reference:n,permission:"read_write",room:r},{object:"room",reference:r,permission:"join"}]})})).then((function(e){if(401===e.status)throw new Error("The Auth Webhook returned unauthorized.");if(200!==e.status)throw new Error("The Auth Webhook returned a status code other than 200.");return Promise.resolve(e.json()).then((function(e){var t=e.resources,r=e.token,n=e.user;if(!t||!r||!n){if("Unauthorized"===e.body)throw new Error("The Auth Webhook unexpectedly return unauthorized. You may be using an invalid API key.");throw new Error("The Auth Webhook has an incorrectly formatted JSON response.")}return{token:r,guestReference:n,docID:t.find((function(e){return"document"===e.object})).id,roomID:t.find((function(e){return"room"===e.object})).id}}))}))},o=!1,i=function(){if("function"==typeof e)return Promise.resolve(e({room:r,ctx:t})).then((function(e){if(!e.user)throw new Error("The auth function must return a 'user' key.");var t=e.resources.find((function(e){return"document"===e.object})).id,r=e.resources.find((function(e){return"room"===e.object})).id;return o=!0,{token:e.token,guestReference:e.user,docID:t,roomID:r}}))}();return Promise.resolve(i&&i.then?i.then(s):s(i))}catch(e){return Promise.reject(e)}}(e.authStrategy,e.authCtx,e.room,e.document)).then((function(t){return Promise.resolve(p({docsURL:e.docsURL,presenceURL:e.presenceURL,token:t.token,docID:t.docID,roomID:t.roomID})).then((function(r){return new S({actor:t.guestReference,bootstrapState:r,token:t.token,roomID:t.roomID,docID:e.document,auth:e.authStrategy,authCtx:e.authCtx,wsURL:"wss://super.roomservice.dev/ws",docsURL:n,presenceURL:"https://super.roomservice.dev/presence",session:t})}))}))}catch(e){return Promise.reject(e)}}({docsURL:n,authStrategy:t.auth,authCtx:t.ctx,room:e,document:"default"})).then((function(r){return t.roomClients[e]=r,r}))}catch(e){return Promise.reject(e)}},e}();exports.RoomClient=S,exports.RoomService=L,exports.default=L;
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t=(e=require("tiny-invariant"))&&"object"==typeof e&&"default"in e?e.default:e,r=require("@roomservice/core"),n="https://super.roomservice.dev/docs",s="https://super.roomservice.dev/presence";function o(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function i(e,t,r){return t&&o(e.prototype,t),r&&o(e,r),e}function c(){return(c=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}function a(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function u(e,t){var r;if("undefined"==typeof Symbol||null==e[Symbol.iterator]){if(Array.isArray(e)||(r=function(e,t){if(e){if("string"==typeof e)return a(e,void 0);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?a(e,void 0):void 0}}(e))||t&&e&&"number"==typeof e.length){r&&(e=r);var n=0;return function(){return n>=e.length?{done:!0}:{done:!1,value:e[n++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}return(r=e[Symbol.iterator]()).next.bind(r)}const h=function(){function e(){}return e.prototype.then=function(t,r){const n=new e,s=this.s;if(s){const e=1&s?t:r;if(e){try{l(n,1,e(this.v))}catch(e){l(n,2,e)}return n}return this}return this.o=function(e){try{const s=e.v;1&e.s?l(n,1,t?t(s):s):r?l(n,1,r(s)):l(n,2,s)}catch(e){l(n,2,e)}},n},e}();function l(e,t,r){if(!e.s){if(r instanceof h){if(!r.s)return void(r.o=l.bind(null,e,t));1&t&&(t=r.s),r=r.v}if(r&&r.then)return void r.then(l.bind(null,e,t),l.bind(null,e,2));e.s=t,e.v=r;const n=e.o;n&&n(e)}}function d(e){return e instanceof h&&1&e.s}function m(e,t,r){for(var n;;){var s=e();if(d(s)&&(s=s.v),!s)return o;if(s.then){n=0;break}var o=r();if(o&&o.then){if(!d(o)){n=1;break}o=o.s}if(t){var i=t();if(i&&i.then&&!d(i)){n=2;break}}}var c=new h,a=l.bind(null,c,2);return(0===n?s.then(m):1===n?o.then(u):i.then(f)).then(void 0,a),c;function u(n){o=n;do{if(t&&(i=t())&&i.then&&!d(i))return void i.then(f).then(void 0,a);if(!(s=e())||d(s)&&!s.v)return void l(c,1,o);if(s.then)return void s.then(m).then(void 0,a);d(o=r())&&(o=o.v)}while(!o||!o.then);o.then(u).then(void 0,a)}function m(e){e?(o=r())&&o.then?o.then(u).then(void 0,a):u(o):l(c,1,o)}function f(){(s=e())?s.then?s.then(m).then(void 0,a):m(s):l(c,1,o)}}"undefined"!=typeof Symbol&&(Symbol.iterator||(Symbol.iterator=Symbol("Symbol.iterator"))),"undefined"!=typeof Symbol&&(Symbol.asyncIterator||(Symbol.asyncIterator=Symbol("Symbol.asyncIterator")));var f=function(e,t,r){try{return Promise.resolve(fetch(e+"/"+r,{headers:{Authorization:"Bearer: "+t}})).then((function(e){return Promise.resolve(e.json()).then((function(e){return e.body}))}))}catch(e){return Promise.reject(e)}},p=function(e,t,r){try{return Promise.resolve(fetch(e+"/"+r,{headers:{Authorization:"Bearer: "+t}})).then((function(e){return Promise.resolve(e.json()).then((function(e){for(var t in Object.keys(e))for(var r in Object.keys(e[t]))if("string"==typeof e[t][r].value){var n=void 0;try{n=JSON.parse(e[t][r].value)}catch(e){}n&&(e[t][r].value=n)}return e}))}))}catch(e){return Promise.reject(e)}},v=function(e){try{return Promise.resolve(Promise.all([p(e.presenceURL,e.token,e.roomID),f(e.docsURL,e.token,e.docID)])).then((function(e){return{presence:e[0],document:e[1]}}))}catch(e){return Promise.reject(e)}};function b(e){return new Promise((function(t){return setTimeout(t,e)}))}var y=function(e){try{return Promise.resolve(new Promise((function(t,r){var n=new WebSocket(e);n.onopen=function(){t(n)},n.onerror=function(e){r(e)}})))}catch(e){return Promise.reject(e)}},C=["doc:fwd","presence:fwd","room:rm_guest"],I=function(){function e(e){this.callbacks={},this.lastTime=0,this.msgsThisMilisecond=0,this.docCmdSendQueue=[],this.presenceCmdSendQueue=new Map,this.dispatcher=e.dispatcher,this.wsURL=e.wsURL,this.docsURL=e.docsURL,this.presenceURL=e.presenceURL,this.room=e.room,this.session=e.session,this.wsFactory=e.wsFactory||y,this.bootstrapFetch=e.bootstrapFetch||v,this.wsLoop()}var t=e.prototype;return t.close=function(){this.currentConn&&(this.currentConn.onmessage=null,this.currentConn.onclose=null,this.currentConn.close(),this.currentConn=void 0),this.pendingConn&&(this.pendingConn=void 0),this.dispatcher.startQueueingCmds()},t.connectAndAuth=function(){try{var e=this;return Promise.resolve(e.wsFactory(e.wsURL)).then((function(t){return t.onmessage=function(t){var r=JSON.parse(t.data);e.dispatch(r.type,r.body)},t.onclose=function(){return e.close()},Promise.resolve(t).then((function(t){try{return t.send(e.serializeMsg("guest:authenticate",e.session.token)),Promise.resolve(e.once("guest:authenticated")).then((function(){return t.send(e.serializeMsg("room:join",e.room)),Promise.resolve(e.once("room:joined")).then((function(){return Promise.resolve(e.bootstrapFetch({docID:e.session.docID,roomID:e.session.roomID,docsURL:e.docsURL,presenceURL:e.presenceURL,token:e.session.token})).then((function(r){return e.dispatcher.bootstrap(r),t}))}))}))}catch(e){return Promise.reject(e)}}))}))}catch(e){return Promise.reject(e)}},t.conn=function(){try{var e=this;return e.currentConn?Promise.resolve(e.currentConn):(e.pendingConn||(e.close(),e.pendingConn=function(){try{var t=!1,r=0;return Promise.resolve(m((function(){return!t}),void 0,(function(){var n=r*(Math.random()+1)/2;return Promise.resolve(b(n)).then((function(){return r=Math.min(2*r+100,6e4),function(r,n){try{var s=Promise.resolve(e.connectAndAuth()).then((function(r){return e.currentConn=r,e.pendingConn=void 0,t=!0,r}))}catch(e){return n(e)}return s&&s.then?s.then(void 0,n):s}(0,(function(e){console.error("Connection to RoomService failed with",e,"\nRetrying...")}))}))})))}catch(e){return Promise.reject(e)}}()),Promise.resolve(e.pendingConn))}catch(e){return Promise.reject(e)}},t.wsLoop=function(){try{var e=this,t=m((function(){return!0}),void 0,(function(){return Promise.resolve(e.conn()).then((function(){return e.processSendQueue(),Promise.resolve(b(1e3)).then((function(){}))}))}));return Promise.resolve(t&&t.then?t.then((function(){})):void 0)}catch(e){return Promise.reject(e)}},t.timestamp=function(){var e=Date.now();return e===this.lastTime?this.msgsThisMilisecond++:(this.lastTime=e,this.msgsThisMilisecond=0),e+":"+this.msgsThisMilisecond},t.serializeMsg=function(e,t){var r=this.timestamp();return JSON.stringify({type:e,ts:r,ver:0,body:t})},t.send=function(e,t){if("doc:cmd"==e){if(this.docCmdSendQueue.length>=1e4)throw"RoomService send queue full";var r=this.serializeMsg(e,t);this.docCmdSendQueue.push(r)}if("presence:cmd"==e){var n=this.serializeMsg(e,t);this.presenceCmdSendQueue.set(t.key,n)}this.processSendQueue()},t.processSendQueue=function(){if(this.currentConn)try{for(;this.presenceCmdSendQueue.size>0;){var e=this.presenceCmdSendQueue.entries().next();if(e){var t=e.value,r=t[0];this.currentConn.send(t[1]),this.presenceCmdSendQueue.delete(r)}}for(;this.docCmdSendQueue.length>0;)this.currentConn.send(this.docCmdSendQueue[0]),this.docCmdSendQueue.splice(0,1)}catch(e){console.error(e)}},t.bind=function(e,t){return this.callbacks[e]=this.callbacks[e]||[],this.callbacks[e].push(t),t},t.unbind=function(e,t){this.callbacks[e]=this.callbacks[e].filter((function(e){return e!==t}))},t.dispatch=function(e,t){"error"==e&&console.error(t);var r=this.callbacks[e];if(r)for(var n=0;n<r.length;n++)r[n](t);C.includes(e)&&this.dispatcher.forwardCmd(e,t)},t.once=function(e){try{var t,r=this;return Promise.race([new Promise((function(e,t){return setTimeout((function(){return t("timeout")}),2e3)})),new Promise((function(n){t=r.bind(e,(function(e){n(e)}))}))]).then((function(){t&&r.unbind(e,t)}))}catch(e){return Promise.reject(e)}},e}(),g=function(){function e(e){this.roomID=e.roomID,this.ws=e.ws,this.bus=e.bus,this.actor=e.actor,this.id=e.listID;var n=r.ListInterpreter.newList(e.docID,e.listID,e.actor),s=n.store;this.meta=n.meta,this.store=s,e.checkpoint.lists[e.listID]||t(!1),r.ListInterpreter.importFromRawCheckpoint(this.store,e.checkpoint,this.meta.listID)}var n=e.prototype;return n.bootstrap=function(e){r.ListInterpreter.importFromRawCheckpoint(this.store,e.document,this.meta.listID)},n.sendCmd=function(e){this.ws.send("doc:cmd",{room:this.roomID,args:e}),this.bus.publish({args:e,from:this.actor})},n.clone=function(){return Object.assign(Object.create(Object.getPrototypeOf(this)),this)},n.dangerouslyUpdateClientDirectly=function(e){return r.ListInterpreter.validateCommand(this.meta,e),r.ListInterpreter.applyCommand(this.store,e),this.clone()},n.get=function(e){return r.ListInterpreter.get(this.store,e)},n.set=function(e,t){var n=r.ListInterpreter.runSet(this.store,this.meta,e,t);return this.sendCmd(n),this.clone()},n.delete=function(e){var t=r.ListInterpreter.runDelete(this.store,this.meta,e);return t?(this.sendCmd(t),this.clone()):this.clone()},n.insertAfter=function(e,t){return this.insertAt(e+1,t)},n.insertAt=function(e,t){var n=r.ListInterpreter.runInsertAt(this.store,this.meta,e,t);return this.sendCmd(n),this.clone()},n.push=function(){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];for(var s,o=r.ListInterpreter.runPush.apply(r.ListInterpreter,[this.store,this.meta].concat(t)),i=u(o);!(s=i()).done;){var c=s.value;this.sendCmd(c)}return this},n.map=function(e){return r.ListInterpreter.map(this.store,e)},n.toArray=function(){return r.ListInterpreter.toArray(this.store)},e}(),D=function(){function e(e){this.roomID=e.roomID,this.ws=e.ws,this.bus=e.bus,this.actor=e.actor;var t=r.MapInterpreter.newMap(e.docID,e.mapID),n=t.meta;this.store=t.store,this.meta=n,r.MapInterpreter.importFromRawCheckpoint(this.store,e.checkpoint,this.meta.mapID)}var t=e.prototype;return t.bootstrap=function(e){r.MapInterpreter.importFromRawCheckpoint(this.store,e.document,this.meta.mapID)},t.sendCmd=function(e){this.ws.send("doc:cmd",{room:this.roomID,args:e}),this.bus.publish({from:this.actor,args:e})},t.clone=function(){return Object.assign(Object.create(Object.getPrototypeOf(this)),this)},t.dangerouslyUpdateClientDirectly=function(e){return r.MapInterpreter.validateCommand(this.meta,e),r.MapInterpreter.applyCommand(this.store,e),this.clone()},t.get=function(e){return this.store[e]},t.set=function(e,t){var n=r.MapInterpreter.runSet(this.store,this.meta,e,t);return this.sendCmd(n),this.clone()},t.toObject=function(){for(var e,t={},r=u(this.keys);!(e=r()).done;){var n=e.value;t[n]=this.get(n)}return t},t.delete=function(e){var t=r.MapInterpreter.runDelete(this.store,this.meta,e);return this.sendCmd(t),this.clone()},i(e,[{key:"id",get:function(){return this.meta.mapID}},{key:"keys",get:function(){return Object.keys(this.store)}}]),e}(),w=function(){function e(e){var t=this;this.roomID=e.roomID,this.ws=e.ws,this.actor=e.actor,this.key=e.key,this.cache={},this.bus=e.bus,this.sendPres=function(e,t,r){void 0===r&&(r=!1);var n={},s=!0;return function(){var t=arguments,o=this,i=r&&s,c=function(){e.apply(o,t),n[t[0]]=null};i&&(s=!1,c()),n[arguments[0]]||(n[arguments[0]]=setTimeout(c,40))}}((function(e,r){t.ws.send("presence:cmd",r)})),this.bootstrap(e.checkpoint)}var t=e.prototype;return t.bootstrap=function(e){this.cache=c({},this.cache,e.presence[this.key]||{})},t.getAll=function(){return this.withoutExpired()},t.getMine=function(){var e;return null===(e=(this.cache||{})[this.actor])||void 0===e?void 0:e.value},t.withoutExpired=function(){var e={};for(var t in this.cache){var r=this.cache[t];new Date>r.expAt?delete this.cache[t]:e[t]=r.value}return e},t.withoutActorOrExpired=function(e){var t={};for(var r in this.cache)for(var n in this.cache[r]){var s=this.cache[n];s&&(n===e&&this.cache[n]||new Date>s.expAt?delete this.cache[n]:t[n]=s.value)}return t},t.set=function(e,t){var r=t||60,n=Math.round((new Date).getTime()/1e3)+r;this.sendPres(this.key,{room:this.roomID,key:this.key,value:JSON.stringify(e),expAt:n}),this.cache[this.actor]={value:e,expAt:new Date(1e3*n)};var s=this.withoutExpired();return this.bus.publish({key:this.key,valuesByUser:s}),s},t.dangerouslyUpdateClientDirectly=function(e,t){if("room:rm_guest"===e)return this.withoutActorOrExpired(t.guest);if("presence:expire"===e)return this.withoutExpired();if(t.room!==this.roomID)return!1;if(t.from===this.actor)return!1;var r={expAt:new Date(1e3*t.expAt),value:JSON.parse(t.value)};return this.cache[t.from]=r,this.withoutExpired()},e}(),k=function(){function e(){this.isPublishing=!1,this.subs=new Set}var t=e.prototype;return t.unsubscribe=function(e){this.subs.delete(e)},t.subscribe=function(e){return this.subs.add(e),e},t.publish=function(e){if(this.isPublishing)throw new Error("Infinite loop detected, see more: See more: https://err.sh/getroomservice/browser/infinite-loop");this.isPublishing=!0,this.subs.forEach((function(t){t(e)})),this.isPublishing=!1},e}(),j=["mcreate","mput","mputref","mdel"],S=["lcreate","lins","linsref","lput","lputref","ldel"],P=function(){function e(e){this.presenceClients={},this.listClients={},this.mapClients={},this.expires={},this.queueIncomingCmds=!0,this.cmdQueue=[],this.mapCallbacksByObjID={},this.listCallbacksByObjID={},this.presenceCallbacksByKey={},this.ws=new I({dispatcher:this,wsURL:e.wsURL,docsURL:e.docsURL,presenceURL:e.presenceURL,room:e.roomID,session:e.session}),this.roomID=e.roomID,this.docID=e.bootstrapState.document.id,this.actor=e.actor,this.bootstrapState=e.bootstrapState}var n=e.prototype;return n.forwardCmd=function(e,t){this.queueIncomingCmds?this.cmdQueue.push([e,t]):this.processCmd(e,t)},n.processCmd=function(e,t){"doc:fwd"==e&&"args"in t&&this.dispatchDocCmd(t),"presence:fwd"==e&&"expAt"in t&&this.dispatchPresenceCmd(t),"room:rm_guest"==e&&"guest"in t&&this.dispatchRmGuest(t)},n.bootstrap=function(e){this.bootstrapState=e;for(var t=0,r=Object.entries(this.listClients);t<r.length;t++)r[t][1].bootstrap(e);for(var n=0,s=Object.entries(this.mapClients);n<s.length;n++)s[n][1].bootstrap(e);for(var o=0,i=Object.entries(this.presenceClients);o<i.length;o++)i[o][1].bootstrap(e);this.queueIncomingCmds=!1;for(var c,a=u(this.cmdQueue);!(c=a()).done;){var h=c.value;this.processCmd(h[0],h[1])}this.cmdQueue.length=0},n.startQueueingCmds=function(){this.queueIncomingCmds=!0},n.dispatchDocCmd=function(e){if(e.room===this.roomID)if(!e.args||e.args.length<3)console.error("Unexpected command: ",e.args);else if(!r.vsReader(atob).isOlderVS(e.vs,this.bootstrapState.document.vs)&&e.from!==this.actor){var t=[e.args[0],e.args[1],e.args[2]],n=t[0],s=t[2];t[1]===this.docID&&(j.includes(n)?this.dispatchMapCmd(s,e):S.includes(n)?this.dispatchListCmd(s,e):console.warn("Unhandled Room Service doc:fwd command: "+n+". Consider updating the Room Service client."))}},n.dispatchRmGuest=function(e){if(e.room===this.roomID)for(var t=0,r=Object.entries(this.presenceClients);t<r.length;t++)for(var n,s=r[t],o=s[0],i=s[1].dangerouslyUpdateClientDirectly("room:rm_guest",e),c=u(this.presenceCallbacksByKey[o]||[]);!(n=c()).done;)(0,n.value)(i,e.guest)},n.dispatchMapCmd=function(e,t){this.mapClients[e]||this.createMapLocally(e);for(var r,n=this.mapClients[e].dangerouslyUpdateClientDirectly(t.args),s=u(this.mapCallbacksByObjID[e]||[]);!(r=s()).done;)(0,r.value)(n.toObject(),t.from)},n.dispatchListCmd=function(e,t){this.listClients[e]||this.createListLocally(e);for(var r,n=this.listClients[e].dangerouslyUpdateClientDirectly(t.args),s=u(this.listCallbacksByObjID[e]||[]);!(r=s()).done;)(0,r.value)(n.toArray(),t.from)},n.dispatchPresenceCmd=function(e){var t=this;if(e.room===this.roomID&&e.from!==this.actor){var r=e.key,n=this.presence(r),s=(new Date).getTime()/1e3,o=e.expAt-s;if(!(o<0)){if(o<43200){this.expires[r]&&clearTimeout(this.expires[r]);var i=setTimeout((function(){var s=n.dangerouslyUpdateClientDirectly("presence:expire",{key:e.key});if(s)for(var o,i=u(null!==(c=t.presenceCallbacksByKey[r])&&void 0!==c?c:[]);!(o=i()).done;){var c;(0,o.value)(s,e.from)}}),1e3*o);this.expires[r]=i}var c=n.dangerouslyUpdateClientDirectly("presence:fwd",e);if(c)for(var a,h=u(null!==(l=this.presenceCallbacksByKey[r])&&void 0!==l?l:[]);!(a=h()).done;){var l;(0,a.value)(c,e.from)}}}},n.createListLocally=function(e){var t=this,r=new k;r.subscribe((function(r){for(var n,s=t.listClients[e],o=u(t.listCallbacksByObjID[e]||[]);!(n=o()).done;)(0,n.value)(s.toArray(),r.from)}));var n=new g({checkpoint:this.bootstrapState.document,roomID:this.roomID,docID:this.docID,listID:e,ws:this.ws,actor:this.actor,bus:r});return this.listClients[e]=n,n},n.list=function(e){return this.listClients[e]?this.listClients[e]:(this.bootstrapState.document.lists[e]||(this.ws.send("doc:cmd",{args:["lcreate",this.docID,e],room:this.roomID}),this.bootstrapState.document.lists[e]={afters:[],ids:[],values:[]}),this.createListLocally(e))},n.createMapLocally=function(e){var t=this,r=new k;r.subscribe((function(r){for(var n,s=t.mapClients[e],o=u(t.mapCallbacksByObjID[e]||[]);!(n=o()).done;)(0,n.value)(s.toObject(),r.from)}));var n=new D({checkpoint:this.bootstrapState.document,roomID:this.roomID,docID:this.docID,mapID:e,ws:this.ws,bus:r,actor:this.actor});return this.mapClients[e]=n,n},n.map=function(e){return this.mapClients[e]?this.mapClients[e]:(this.bootstrapState.document.maps[e]||this.ws.send("doc:cmd",{args:["mcreate",this.docID,e],room:this.roomID}),this.createMapLocally(e))},n.presence=function(e){var t=this;if(this.presenceClients[e])return this.presenceClients[e];var r=new k;r.subscribe((function(e){for(var r,n=u(t.presenceCallbacksByKey[e.key]||[]);!(r=n()).done;)(0,r.value)(e.valuesByUser,t.actor)}));var n=new w({checkpoint:this.bootstrapState,roomID:this.roomID,ws:this.ws,actor:this.actor,key:e,bus:r});try{this.presenceClients[e]=n}catch(e){throw new Error("Don't Freeze State. See more: https://err.sh/getroomservice/browser/dont-freeze")}return this.presenceClients[e]},n.subscribe=function(e,t){if(e instanceof w)return this.subscribePresence(e,t);var r,n=function(e,r){t(e,r)};return e instanceof D&&(this.mapCallbacksByObjID[r=e.id]=this.mapCallbacksByObjID[r]||[],this.mapCallbacksByObjID[r].push(n)),e instanceof g&&(this.listCallbacksByObjID[r=e.id]=this.listCallbacksByObjID[r]||[],this.listCallbacksByObjID[r].push(n)),[{objID:r,fn:n}]},n.subscribePresence=function(e,r){e||t(!1);var n=function(e,t){r&&r(e,t)},s=e.key;return this.presenceCallbacksByKey[s]=this.presenceCallbacksByKey[s]||[],this.presenceCallbacksByKey[s].push(n),[{objID:s,fn:n}]},n.unsubscribe=function(e){for(var t,r=u(e);!(t=r()).done;){var n=t.value;n.objID&&(this.mapCallbacksByObjID[n.objID]=L(this.mapCallbacksByObjID[n.objID],n.fn),this.listCallbacksByObjID[n.objID]=L(this.listCallbacksByObjID[n.objID],n.fn),this.presenceCallbacksByKey[n.objID]=L(this.presenceCallbacksByKey[n.objID],n.fn))}},i(e,[{key:"me",get:function(){return this.actor}}]),e}();function L(e,t){return e?e.filter((function(e){return e!==t})):[]}var O=function(){function e(e){this.roomClients={},this.auth=e.auth,this.ctx=e.ctx||{}}return e.prototype.room=function(e){try{var t=this;return t.roomClients[e]?Promise.resolve(t.roomClients[e]):Promise.resolve(function(e){try{return Promise.resolve(function(e,t,r,n){try{var s=function(t){return o?t:Promise.resolve(fetch(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({resources:[{object:"document",reference:n,permission:"read_write",room:r},{object:"room",reference:r,permission:"join"}]})})).then((function(e){if(401===e.status)throw new Error("The Auth Webhook returned unauthorized.");if(200!==e.status)throw new Error("The Auth Webhook returned a status code other than 200.");return Promise.resolve(e.json()).then((function(e){var t=e.resources,r=e.token,n=e.user;if(!t||!r||!n){if("Unauthorized"===e.body)throw new Error("The Auth Webhook unexpectedly return unauthorized. You may be using an invalid API key.");throw new Error("The Auth Webhook has an incorrectly formatted JSON response.")}return{token:r,guestReference:n,docID:t.find((function(e){return"document"===e.object})).id,roomID:t.find((function(e){return"room"===e.object})).id}}))}))},o=!1,i=function(){if("function"==typeof e)return Promise.resolve(e({room:r,ctx:t})).then((function(e){if(!e.user)throw new Error("The auth function must return a 'user' key.");var t=e.resources.find((function(e){return"document"===e.object})).id,r=e.resources.find((function(e){return"room"===e.object})).id;return o=!0,{token:e.token,guestReference:e.user,docID:t,roomID:r}}))}();return Promise.resolve(i&&i.then?i.then(s):s(i))}catch(e){return Promise.reject(e)}}(e.authStrategy,e.authCtx,e.room,e.document)).then((function(t){return Promise.resolve(v({docsURL:e.docsURL,presenceURL:e.presenceURL,token:t.token,docID:t.docID,roomID:t.roomID})).then((function(r){return new P({actor:t.guestReference,bootstrapState:r,token:t.token,roomID:t.roomID,docID:e.document,auth:e.authStrategy,authCtx:e.authCtx,wsURL:"wss://super.roomservice.dev/ws",docsURL:n,presenceURL:s,session:t})}))}))}catch(e){return Promise.reject(e)}}({docsURL:n,presenceURL:s,authStrategy:t.auth,authCtx:t.ctx,room:e,document:"default"})).then((function(r){return t.roomClients[e]=r,r}))}catch(e){return Promise.reject(e)}},e}();exports.RoomClient=P,exports.RoomService=O,exports.default=O;
//# sourceMappingURL=browser.cjs.production.min.js.map

@@ -1603,2 +1603,3 @@ import invariant from 'tiny-invariant';

docsURL: DOCS_URL,
presenceURL: PRESENCE_URL,
authStrategy: _this2.auth,

@@ -1605,0 +1606,0 @@ authCtx: _this2.ctx,

{
"version": "3.0.0-8",
"version": "3.0.0-9",
"license": "MIT",

@@ -4,0 +4,0 @@ "main": "dist/index.js",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc