Socket
Socket
Sign inDemoInstall

@roomservice/browser

Package Overview
Dependencies
Maintainers
1
Versions
126
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@roomservice/browser - npm Package Compare versions

Comparing version 1.0.5-0 to 2.0.0-0

115

dist/browser.cjs.development.js

@@ -1057,80 +1057,61 @@ 'use strict';

_proto.list = function list(name) {
try {
var _this6 = this;
if (this.listClients[name]) {
return this.listClients[name];
} // create a list if it doesn't exist
if (_this6.listClients[name]) {
return Promise.resolve(_this6.listClients[name]);
} // create a list if it doesn't exist
if (!this.checkpoint.lists[name]) {
this.ws.send('doc:cmd', {
args: ['lcreate', this.docID, name],
room: this.roomID
}); // Assume success
if (!_this6.checkpoint.lists[name]) {
_this6.ws.send('doc:cmd', {
args: ['lcreate', _this6.docID, name],
room: _this6.roomID
}); // Assume success
this.checkpoint.lists[name] = {
afters: [],
ids: [],
values: []
};
}
_this6.checkpoint.lists[name] = {
afters: [],
ids: [],
values: []
};
}
var l = new ListClient(_this6.checkpoint, _this6.roomID, _this6.docID, name, _this6.ws, _this6.actor);
_this6.listClients[name] = l;
return Promise.resolve(l);
} catch (e) {
return Promise.reject(e);
}
var l = new ListClient(this.checkpoint, this.roomID, this.docID, name, this.ws, this.actor);
this.listClients[name] = l;
return l;
};
_proto.map = function map(name) {
try {
var _this8 = this;
if (this.mapClients[name]) {
return this.mapClients[name];
} // Create this map if it doesn't exist
if (_this8.mapClients[name]) {
return Promise.resolve(_this8.mapClients[name]);
} // Create this map if it doesn't exist
if (!this.checkpoint.maps[name]) {
this.ws.send('doc:cmd', {
args: ['mcreate', this.docID, name],
room: this.roomID
});
}
if (!_this8.checkpoint.maps[name]) {
_this8.ws.send('doc:cmd', {
args: ['mcreate', _this8.docID, name],
room: _this8.roomID
});
}
var m = new MapClient(_this8.checkpoint.maps[name] || {}, _this8.roomID, _this8.docID, name, _this8.ws);
_this8.mapClients[name] = m;
return Promise.resolve(m);
} catch (e) {
return Promise.reject(e);
}
var m = new MapClient(this.checkpoint.maps[name] || {}, this.roomID, this.docID, name, this.ws);
this.mapClients[name] = m;
return m;
};
_proto.presence = function presence() {
try {
var _this10 = this;
if (this.presenceClient) {
return this.presenceClient;
}
if (_this10.presenceClient) {
return Promise.resolve(_this10.presenceClient);
}
var p = new PresenceClient(this.roomID, this.ws, this.actor, this.token);
var p = new PresenceClient(_this10.roomID, _this10.ws, _this10.actor, _this10.token);
try {
this.presenceClient = p;
} catch (err) {
throw new Error("Don't Freeze State. See more: https://err.sh/getroomservice/browser/dont-freeze");
}
try {
_this10.presenceClient = p;
} catch (err) {
throw new Error("Don't Freeze State. See more: https://err.sh/getroomservice/browser/dont-freeze");
}
return Promise.resolve(_this10.presenceClient);
} catch (e) {
return Promise.reject(e);
}
return this.presenceClient;
};
_proto.subscribe = function subscribe(obj, onChangeFnOrString, onChangeFn) {
var _this11 = this;
var _this5 = this;

@@ -1141,5 +1122,5 @@ // Presence handler

var fwdListener = this.ws.bind('presence:fwd', function (body) {
if (body.room !== _this11.roomID) return;
if (body.room !== _this5.roomID) return;
if (body.key !== onChangeFnOrString) return;
if (body.from === _this11.actor) return;
if (body.from === _this5.actor) return;
var newObj = obj.dangerouslyUpdateClientDirectly('presence:fwd', body);

@@ -1151,3 +1132,3 @@ if (!newObj) return;

var leaveListener = this.ws.bind('room:rm_guest', function (body) {
if (body.room !== _this11.roomID) return;
if (body.room !== _this5.roomID) return;
var newObj = obj.dangerouslyUpdateClientDirectly('room:rm_guest', body);

@@ -1169,3 +1150,3 @@ if (!newObj) return;

var bound = this.ws.bind('doc:fwd', function (body) {
if (body.room !== _this11.roomID) return;
if (body.room !== _this5.roomID) return;

@@ -1180,11 +1161,11 @@ if (!body.args || body.args.length < 3) {

if (isOlderVS(body.vs, _this11.vs)) return; // Ignore validated commands
if (isOlderVS(body.vs, _this5.vs)) return; // Ignore validated commands
if (body.from === _this11.actor) return;
if (body.from === _this5.actor) return;
var _ref = [body.args[1], body.args[2]],
docID = _ref[0],
objID = _ref[1];
if (docID !== _this11.docID) return;
if (docID !== _this5.docID) return;
if (objID !== obj.id) return;
_this11.vs = body.vs;
_this5.vs = body.vs;
var newObj = obj.dangerouslyUpdateClientDirectly(body.args);

@@ -1191,0 +1172,0 @@ onChangeFnOrString(newObj, body.from);

@@ -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;function r(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 n(e,t,n){return t&&r(e.prototype,t),n&&r(e,n),e}function i(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 o(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 i(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)?i(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)}var s=function(){function e(e){var t=this;this.callbacks={},this.lastTime=0,this.msgsThisMilisecond=0,this.conn=e,this.conn.onmessage=function(e){var r=JSON.parse(e.data);t.dispatch(r.type,r.body)}}var t=e.prototype;return t.timestamp=function(){var e=Date.now();return e===this.lastTime?this.msgsThisMilisecond++:(this.lastTime=e,this.msgsThisMilisecond=0),e+":"+this.msgsThisMilisecond},t.send=function(e,t){var r=this,n=this.timestamp();this.conn.readyState!==this.conn.CONNECTING?this.conn.send(JSON.stringify({type:e,ts:n,ver:0,body:t})):setTimeout((function(){r.send(e,t)}),100+100*Math.random())},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){var r=this.callbacks[e];if(r)for(var n=0;n<r.length;n++)r[n](t)},e}();function c(e,t){if("root"===t)return"root";var r=t.split(":");return r[0]+":"+e.actors[parseInt(r[1])]}var h=function(){function e(e){this.count=0,this.actor=e,this.nodes={},this.log=[]}var r=e.prototype;return r.import=function(e,r){e||t(!1);for(var n=e.lists[r],i=n.afters||[],o=n.ids||[],s=n.values||[],h=0;h<i.length;h++){var u={after:c(e,i[h]),id:c(e,o[h]),value:s[h]};this.nodes[u.id]=u,this.log.push(u)}this.count=this.log.length},r.get=function(e){if(this.nodes[e])return this.nodes[e].value},r.insert=function(e,r,n){this.log||t(!1);var i=n;i||(i=this.count+":"+this.actor),this.count++;var o={after:e,value:r,id:i};return this.nodes[i]=o,this.log.push(o),i},r.put=function(e,t){this.nodes[e]&&(this.nodes[e].value=t)},r.has=function(e){return!!this.nodes[e]},r.delete=function(e){this.nodes[e]&&(this.nodes[e].value={t:""})},r.toTree=function(){for(var e,t={children:[],id:"root",value:""},r={root:t},n=o(this.log);!(e=n()).done;){var i=e.value,s={children:[],id:i.id,value:i.value};if(r[i.id]=s,"root"===i.after)t.children.push(s);else{if(!r[i.after])throw new Error("Unexpectedly missing node "+i.after);r[i.after].children.push(s)}}return t},r.sortLog=function(){this.log.sort((function(e,t){var r=e.id.split(":"),n=r[0],i=r[1],o=t.id.split(":"),s=o[0];return n===s?i.localeCompare(o[1]):parseInt(n)-parseInt(s)}))},r.lastID=function(){return 0===this.log.length?"root":(this.sortLog(),function e(t){return t.children&&0!==t.children.length?e(t.children[t.children.length-1]):t}(this.toTree()).id)},r.toArray=function(){return this.sortLog(),function e(t){if(!t.children||0===t.children.length)return[];for(var r,n=[],i=o(t.children);!(r=i()).done;){var s=r.value,c=s.value;if("string"!=typeof s.value){if(""===s.value.t){n=n.concat([].concat(e(s)));continue}throw new Error("Unimplemented")}n=n.concat([c].concat(e(s)))}return n}(this.toTree())},n(e,[{key:"length",get:function(){return Object.keys(this.nodes).length}}]),e}();function u(e){return JSON.stringify(e)}function a(e){try{return JSON.parse(e)}catch(t){return e}}var d=function(){function e(e,r,n,i,o,s){this.itemIDs=[],this.roomID=r,this.docID=n,this.id=i,this.ws=o,this.rt=new h(s),e.lists[i]||t(!1),this.rt.import(e,i);for(var u=e.lists[i].ids||[],a=0;a<u.length;a++){var d=e.lists[i].values[a];"object"==typeof d&&""===d.t||this.itemIDs.push(c(e,u[a]))}}var r=e.prototype;return r.sendCmd=function(e){this.ws.send("doc:cmd",{room:this.roomID,args:e})},r.clone=function(){return Object.assign(Object.create(Object.getPrototypeOf(this)),this)},r.dangerouslyUpdateClientDirectly=function(e){if(e.length<3)throw new Error("Unexpected command: "+e);var t=e[0];if(e[1]!==this.docID||e[2]!==this.id)throw new Error("Command unexpectedly routed to the wrong client");switch(t){case"lins":var r=e[3],n=e[4],i=e[5];this.itemIDs.splice(this.itemIDs.findIndex((function(e){return e===r}))+1,0,n),this.rt.insert(r,i,n);break;case"lput":this.rt.put(e[3],e[4]);break;case"ldel":var o=e[3];this.rt.delete(o),this.itemIDs.splice(this.itemIDs.findIndex((function(e){return e===o})),1);break;default:throw new Error("Unexpected command keyword: "+t)}return this.clone()},r.get=function(e){var t=this.itemIDs[e];if(t){var r=this.rt.get(t);if(r){if("object"==typeof r){if(""===r.t)return;throw new Error("Unimplemented references")}return a(r)}}},r.set=function(e,t){var r=this.itemIDs[e];if(!r)throw new Error("Index '"+e+"' doesn't already exist. Try .push() or .insertAfter() instead.");var n=u(t);return this.rt.put(r,n),this.sendCmd(["lput",this.docID,this.id,r,n]),this.clone()},r.delete=function(e){if(0===this.itemIDs.length)return this.clone();var t=this.itemIDs[e];return t?(this.rt.delete(t),this.itemIDs.splice(e,1),this.sendCmd(["ldel",this.docID,this.id,t]),this.clone()):(console.warn("Unknown index: ",e,this.itemIDs),this.clone())},r.insertAfter=function(e,t){var r=this.itemIDs[e];if(!r)throw new RangeError("List '"+this.id+"' has no index: '"+e+"'");var n=u(t),i=this.rt.insert(r,n);return this.itemIDs.splice(e,0,i),this.sendCmd(["lins",this.docID,this.id,r,i,n]),this.clone()},r.push=function(e){var t=this.rt.lastID(),r=u(e),n=this.rt.insert(t,r);return this.itemIDs.push(n),this.sendCmd(["lins",this.docID,this.id,t,n,r]),this.clone()},r.toArray=function(){return this.rt.toArray().map((function(e){return a(e)}))},e}(),l=function(){function e(e,t,r,n,i){for(var o in this.roomID=t,this.docID=r,this.id=n,this.ws=i,this.store={},e){var s=e[o];"string"==typeof s&&(this.store[o]=a(s))}}var t=e.prototype;return t.sendCmd=function(e){this.ws.send("doc:cmd",{room:this.roomID,args:e})},t.clone=function(){return Object.assign(Object.create(Object.getPrototypeOf(this)),this)},t.dangerouslyUpdateClientDirectly=function(e){if(e.length<3)throw new Error("Unexpected command: "+e);var t=e[0];if(e[1]!==this.docID||e[2]!==this.id)throw new Error("Command unexpectedly routed to the wrong client");switch(t){case"mput":if(5!==e.length){console.error("Malformed command ",e);break}this.store[e[3]]=a(e[4]);break;case"mdel":if(4!==e.length){console.error("Malformed command ",e);break}delete this.store[e[3]];break;default:throw new Error("Unexpected command keyword: "+t)}return this.clone()},t.get=function(e){return this.store[e]},t.set=function(e,t){var r=u(t);return this.store[e]=t,this.sendCmd(["mput",this.docID,this.id,e,r]),this.clone()},t.delete=function(e){return delete this.store[e],this.sendCmd(["mdel",this.docID,this.id,e]),this.clone()},e}(),f=function(){function e(e,t,r,n){this.roomID=e,this.ws=t,this.actor=r,this.token=n,this.cache={},this.send=function(e,t,r){void 0===r&&(r=!1);var n=null,i=!0;return function(){var t=arguments,o=this,s=r&&i,c=function(){e.apply(o,t),n=null};s&&(i=!1,c()),n||(n=setTimeout(c,40))}}(this.ws.send.bind(this.ws))}var t=e.prototype;return t.getAll=function(e){try{var t=this;return Promise.resolve(function(e,t,r,n){try{return Promise.resolve(fetch("https://super.roomservice.dev/presence/"+r+"/"+encodeURIComponent(n),{headers:{Authorization:"Bearer: "+t}})).then((function(e){return Promise.resolve(e.json()).then((function(e){for(var t in e)if("string"==typeof e[t].value){var r=void 0;try{r=JSON.parse(e[t].value)}catch(e){}r&&(e[t].value=r)}return e}))}))}catch(e){return Promise.reject(e)}}(0,t.token,t.roomID,e)).then((function(r){return t.cache[e]=r,t.withoutExpiredAndSelf(e)}))}catch(e){return Promise.reject(e)}},t.withoutExpiredAndSelf=function(e){var t={};for(var r in this.cache[e]){var n=this.cache[e][r];new Date>n.expAt?delete this.cache[e][r]:t[r]=n.value}return t},t.withoutActorOrExpired=function(e){var t={};for(var r in this.cache)for(var n in this.cache[r]){var i=this.cache[r][n];i&&(n===e&&this.cache[r][n]||new Date>i.expAt?delete this.cache[r][n]:t[n]=i.value)}return t},t.set=function(e,t,r){var n=r||60,i=Math.round((new Date).getTime()/1e3)+n;return this.send("presence:cmd",{room:this.roomID,key:e,value:JSON.stringify(t),expAt:i}),this.cache[e]||(this.cache[e]={}),this.cache[e][this.actor]={value:t,expAt:new Date(1e3*i)},this.withoutExpiredAndSelf(e)},t.dangerouslyUpdateClientDirectly=function(e,t){if("room:rm_guest"===e)return this.withoutActorOrExpired(t.guest);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.key]||(this.cache[t.key]={}),this.cache[t.key][t.from]=r,this.withoutExpiredAndSelf(t.key)},n(e,[{key:"me",get:function(){return console.warn("presence.me() is deprecated and will be removed in a future version!"),this.actor}}]),e}();function m(e){for(var t=window.atob(e),r=t.length,n=new Uint8Array(r),i=0;i<r;i++)n[i]=t.charCodeAt(i);return n.buffer}var v=function(){function e(e){this.listClients={},this.mapClients={},this.ws=new s(e.conn),this.token=e.token,this.roomID=e.roomID,this.docID=e.checkpoint.id,this.actor=e.actor,this.checkpoint=e.checkpoint,this.vs=this.checkpoint.vs,this.presenceClient=void 0}var r=e.prototype;return r.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.ws.bind(e,(function(e){n(e)}))}))]).then((function(){t&&r.ws.unbind(e,t)}))}catch(e){return Promise.reject(e)}},r.reconnect=function(){try{var e=this;e.errorListener||(e.errorListener=e.ws.bind("error",(function(e){console.error("Room Service encountered a server-side error. If you see this, please let us know; this could be a bug.",e)})));var t=e.once("guest:authenticated");return e.ws.send("guest:authenticate",e.token),Promise.resolve(t).then((function(){var t=e.once("room:joined");return e.ws.send("room:join",e.roomID),Promise.resolve(t).then((function(){}))}))}catch(e){return Promise.reject(e)}},r.list=function(e){try{if(this.listClients[e])return Promise.resolve(this.listClients[e]);this.checkpoint.lists[e]||(this.ws.send("doc:cmd",{args:["lcreate",this.docID,e],room:this.roomID}),this.checkpoint.lists[e]={afters:[],ids:[],values:[]});var t=new d(this.checkpoint,this.roomID,this.docID,e,this.ws,this.actor);return this.listClients[e]=t,Promise.resolve(t)}catch(e){return Promise.reject(e)}},r.map=function(e){try{if(this.mapClients[e])return Promise.resolve(this.mapClients[e]);this.checkpoint.maps[e]||this.ws.send("doc:cmd",{args:["mcreate",this.docID,e],room:this.roomID});var t=new l(this.checkpoint.maps[e]||{},this.roomID,this.docID,e,this.ws);return this.mapClients[e]=t,Promise.resolve(t)}catch(e){return Promise.reject(e)}},r.presence=function(){try{if(this.presenceClient)return Promise.resolve(this.presenceClient);var e=new f(this.roomID,this.ws,this.actor,this.token);try{this.presenceClient=e}catch(e){throw new Error("Don't Freeze State. See more: https://err.sh/getroomservice/browser/dont-freeze")}return Promise.resolve(this.presenceClient)}catch(e){return Promise.reject(e)}},r.subscribe=function(e,r,n){var i=this;return"string"==typeof r?(e||t(!1),[{event:"presence:fwd",fn:this.ws.bind("presence:fwd",(function(o){if(o.room===i.roomID&&o.key===r&&o.from!==i.actor){var s=e.dangerouslyUpdateClientDirectly("presence:fwd",o);s&&(n||t(!1),n(s,o.from))}}))},{event:"room:rm_guest",fn:this.ws.bind("room:rm_guest",(function(r){if(r.room===i.roomID){var o=e.dangerouslyUpdateClientDirectly("room:rm_guest",r);o&&(n||t(!1),n(o,r.guest))}}))}]):[{event:"doc:fwd",fn:this.ws.bind("doc:fwd",(function(t){if(t.room===i.roomID)if(!t.args||t.args.length<3)console.error("Unexpected command: ",t.args);else if(!function(e,t){if(!e)return!0;if(!t)return!1;for(var r=new Uint8Array(m(e).slice(0,9)),n=new Uint8Array(m(t).slice(0,9)),i=0;i<r.byteLength;i++){if(n[i]>r[i])return!0;if(n[i]<r[i])return!1}return!1}(t.vs,i.vs)&&t.from!==i.actor){var n=[t.args[1],t.args[2]];if(n[0]===i.docID&&n[1]===e.id){i.vs=t.vs;var o=e.dangerouslyUpdateClientDirectly(t.args);r(o,t.from)}}}))}]},r.unsubscribe=function(e){for(var t,r=o(e);!(t=r()).done;){var n=t.value;this.ws.unbind(n.event,n.fn)}},n(e,[{key:"me",get:function(){return this.actor}}]),e}(),p=function(){function e(e){this.roomClients={},this.auth=e.auth}return e.prototype.room=function(e){try{var t=this;if(t.roomClients[e])return Promise.resolve(t.roomClients[e]);var r=new WebSocket("wss://super.roomservice.dev/ws");return Promise.resolve(function(e,t,r,n,i){try{return Promise.resolve(function(e,t,r){try{var n=function(r){return i?r:Promise.resolve(fetch(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({resources:[{object:"document",reference:"default",permission:"read_write",room:t},{object:"room",reference:t,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}}))}))},i=!1,o=function(){if("function"==typeof e)return Promise.resolve(e(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 i=!0,{token:e.token,guestReference:e.user,docID:t,roomID:r}}))}();return Promise.resolve(o&&o.then?o.then(n):n(o))}catch(e){return Promise.reject(e)}}(r,n)).then((function(t){return Promise.resolve(function(e,t,r){try{return Promise.resolve(fetch("https://super.roomservice.dev/docs/"+r,{headers:{Authorization:"Bearer: "+t}})).then((function(e){return Promise.resolve(e.json())}))}catch(e){return Promise.reject(e)}}(0,t.token,t.docID)).then((function(r){var n=new v({conn:e,actor:t.guestReference,checkpoint:r.body,token:t.token,roomID:t.roomID});return Promise.resolve(n.reconnect()).then((function(){return n}))}))}))}catch(e){return Promise.reject(e)}}(r,0,t.auth,e)).then((function(r){return t.roomClients[e]=r,r}))}catch(e){return Promise.reject(e)}},e}();exports.ListClient=d,exports.MapClient=l,exports.PresenceClient=f,exports.RoomClient=v,exports.RoomService=p,exports.default=p;
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t,e=(t=require("tiny-invariant"))&&"object"==typeof t&&"default"in t?t.default:t;function r(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function n(t,e,n){return e&&r(t.prototype,e),n&&r(t,n),t}function i(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=new Array(e);r<e;r++)n[r]=t[r];return n}function o(t,e){var r;if("undefined"==typeof Symbol||null==t[Symbol.iterator]){if(Array.isArray(t)||(r=function(t,e){if(t){if("string"==typeof t)return i(t,void 0);var r=Object.prototype.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?i(t,void 0):void 0}}(t))||e&&t&&"number"==typeof t.length){r&&(t=r);var n=0;return function(){return n>=t.length?{done:!0}:{done:!1,value:t[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=t[Symbol.iterator]()).next.bind(r)}var s=function(){function t(t){var e=this;this.callbacks={},this.lastTime=0,this.msgsThisMilisecond=0,this.conn=t,this.conn.onmessage=function(t){var r=JSON.parse(t.data);e.dispatch(r.type,r.body)}}var e=t.prototype;return e.timestamp=function(){var t=Date.now();return t===this.lastTime?this.msgsThisMilisecond++:(this.lastTime=t,this.msgsThisMilisecond=0),t+":"+this.msgsThisMilisecond},e.send=function(t,e){var r=this,n=this.timestamp();this.conn.readyState!==this.conn.CONNECTING?this.conn.send(JSON.stringify({type:t,ts:n,ver:0,body:e})):setTimeout((function(){r.send(t,e)}),100+100*Math.random())},e.bind=function(t,e){return this.callbacks[t]=this.callbacks[t]||[],this.callbacks[t].push(e),e},e.unbind=function(t,e){this.callbacks[t]=this.callbacks[t].filter((function(t){return t!==e}))},e.dispatch=function(t,e){var r=this.callbacks[t];if(r)for(var n=0;n<r.length;n++)r[n](e)},t}();function c(t,e){if("root"===e)return"root";var r=e.split(":");return r[0]+":"+t.actors[parseInt(r[1])]}var h=function(){function t(t){this.count=0,this.actor=t,this.nodes={},this.log=[]}var r=t.prototype;return r.import=function(t,r){t||e(!1);for(var n=t.lists[r],i=n.afters||[],o=n.ids||[],s=n.values||[],h=0;h<i.length;h++){var u={after:c(t,i[h]),id:c(t,o[h]),value:s[h]};this.nodes[u.id]=u,this.log.push(u)}this.count=this.log.length},r.get=function(t){if(this.nodes[t])return this.nodes[t].value},r.insert=function(t,r,n){this.log||e(!1);var i=n;i||(i=this.count+":"+this.actor),this.count++;var o={after:t,value:r,id:i};return this.nodes[i]=o,this.log.push(o),i},r.put=function(t,e){this.nodes[t]&&(this.nodes[t].value=e)},r.has=function(t){return!!this.nodes[t]},r.delete=function(t){this.nodes[t]&&(this.nodes[t].value={t:""})},r.toTree=function(){for(var t,e={children:[],id:"root",value:""},r={root:e},n=o(this.log);!(t=n()).done;){var i=t.value,s={children:[],id:i.id,value:i.value};if(r[i.id]=s,"root"===i.after)e.children.push(s);else{if(!r[i.after])throw new Error("Unexpectedly missing node "+i.after);r[i.after].children.push(s)}}return e},r.sortLog=function(){this.log.sort((function(t,e){var r=t.id.split(":"),n=r[0],i=r[1],o=e.id.split(":"),s=o[0];return n===s?i.localeCompare(o[1]):parseInt(n)-parseInt(s)}))},r.lastID=function(){return 0===this.log.length?"root":(this.sortLog(),function t(e){return e.children&&0!==e.children.length?t(e.children[e.children.length-1]):e}(this.toTree()).id)},r.toArray=function(){return this.sortLog(),function t(e){if(!e.children||0===e.children.length)return[];for(var r,n=[],i=o(e.children);!(r=i()).done;){var s=r.value,c=s.value;if("string"!=typeof s.value){if(""===s.value.t){n=n.concat([].concat(t(s)));continue}throw new Error("Unimplemented")}n=n.concat([c].concat(t(s)))}return n}(this.toTree())},n(t,[{key:"length",get:function(){return Object.keys(this.nodes).length}}]),t}();function u(t){return JSON.stringify(t)}function a(t){try{return JSON.parse(t)}catch(e){return t}}var d=function(){function t(t,r,n,i,o,s){this.itemIDs=[],this.roomID=r,this.docID=n,this.id=i,this.ws=o,this.rt=new h(s),t.lists[i]||e(!1),this.rt.import(t,i);for(var u=t.lists[i].ids||[],a=0;a<u.length;a++){var d=t.lists[i].values[a];"object"==typeof d&&""===d.t||this.itemIDs.push(c(t,u[a]))}}var r=t.prototype;return r.sendCmd=function(t){this.ws.send("doc:cmd",{room:this.roomID,args:t})},r.clone=function(){return Object.assign(Object.create(Object.getPrototypeOf(this)),this)},r.dangerouslyUpdateClientDirectly=function(t){if(t.length<3)throw new Error("Unexpected command: "+t);var e=t[0];if(t[1]!==this.docID||t[2]!==this.id)throw new Error("Command unexpectedly routed to the wrong client");switch(e){case"lins":var r=t[3],n=t[4],i=t[5];this.itemIDs.splice(this.itemIDs.findIndex((function(t){return t===r}))+1,0,n),this.rt.insert(r,i,n);break;case"lput":this.rt.put(t[3],t[4]);break;case"ldel":var o=t[3];this.rt.delete(o),this.itemIDs.splice(this.itemIDs.findIndex((function(t){return t===o})),1);break;default:throw new Error("Unexpected command keyword: "+e)}return this.clone()},r.get=function(t){var e=this.itemIDs[t];if(e){var r=this.rt.get(e);if(r){if("object"==typeof r){if(""===r.t)return;throw new Error("Unimplemented references")}return a(r)}}},r.set=function(t,e){var r=this.itemIDs[t];if(!r)throw new Error("Index '"+t+"' doesn't already exist. Try .push() or .insertAfter() instead.");var n=u(e);return this.rt.put(r,n),this.sendCmd(["lput",this.docID,this.id,r,n]),this.clone()},r.delete=function(t){if(0===this.itemIDs.length)return this.clone();var e=this.itemIDs[t];return e?(this.rt.delete(e),this.itemIDs.splice(t,1),this.sendCmd(["ldel",this.docID,this.id,e]),this.clone()):(console.warn("Unknown index: ",t,this.itemIDs),this.clone())},r.insertAfter=function(t,e){var r=this.itemIDs[t];if(!r)throw new RangeError("List '"+this.id+"' has no index: '"+t+"'");var n=u(e),i=this.rt.insert(r,n);return this.itemIDs.splice(t,0,i),this.sendCmd(["lins",this.docID,this.id,r,i,n]),this.clone()},r.push=function(t){var e=this.rt.lastID(),r=u(t),n=this.rt.insert(e,r);return this.itemIDs.push(n),this.sendCmd(["lins",this.docID,this.id,e,n,r]),this.clone()},r.toArray=function(){return this.rt.toArray().map((function(t){return a(t)}))},t}(),l=function(){function t(t,e,r,n,i){for(var o in this.roomID=e,this.docID=r,this.id=n,this.ws=i,this.store={},t){var s=t[o];"string"==typeof s&&(this.store[o]=a(s))}}var e=t.prototype;return e.sendCmd=function(t){this.ws.send("doc:cmd",{room:this.roomID,args:t})},e.clone=function(){return Object.assign(Object.create(Object.getPrototypeOf(this)),this)},e.dangerouslyUpdateClientDirectly=function(t){if(t.length<3)throw new Error("Unexpected command: "+t);var e=t[0];if(t[1]!==this.docID||t[2]!==this.id)throw new Error("Command unexpectedly routed to the wrong client");switch(e){case"mput":if(5!==t.length){console.error("Malformed command ",t);break}this.store[t[3]]=a(t[4]);break;case"mdel":if(4!==t.length){console.error("Malformed command ",t);break}delete this.store[t[3]];break;default:throw new Error("Unexpected command keyword: "+e)}return this.clone()},e.get=function(t){return this.store[t]},e.set=function(t,e){var r=u(e);return this.store[t]=e,this.sendCmd(["mput",this.docID,this.id,t,r]),this.clone()},e.delete=function(t){return delete this.store[t],this.sendCmd(["mdel",this.docID,this.id,t]),this.clone()},t}(),f=function(){function t(t,e,r,n){this.roomID=t,this.ws=e,this.actor=r,this.token=n,this.cache={},this.send=function(t,e,r){void 0===r&&(r=!1);var n=null,i=!0;return function(){var e=arguments,o=this,s=r&&i,c=function(){t.apply(o,e),n=null};s&&(i=!1,c()),n||(n=setTimeout(c,40))}}(this.ws.send.bind(this.ws))}var e=t.prototype;return e.getAll=function(t){try{var e=this;return Promise.resolve(function(t,e,r,n){try{return Promise.resolve(fetch("https://super.roomservice.dev/presence/"+r+"/"+encodeURIComponent(n),{headers:{Authorization:"Bearer: "+e}})).then((function(t){return Promise.resolve(t.json()).then((function(t){for(var e in t)if("string"==typeof t[e].value){var r=void 0;try{r=JSON.parse(t[e].value)}catch(t){}r&&(t[e].value=r)}return t}))}))}catch(t){return Promise.reject(t)}}(0,e.token,e.roomID,t)).then((function(r){return e.cache[t]=r,e.withoutExpiredAndSelf(t)}))}catch(t){return Promise.reject(t)}},e.withoutExpiredAndSelf=function(t){var e={};for(var r in this.cache[t]){var n=this.cache[t][r];new Date>n.expAt?delete this.cache[t][r]:e[r]=n.value}return e},e.withoutActorOrExpired=function(t){var e={};for(var r in this.cache)for(var n in this.cache[r]){var i=this.cache[r][n];i&&(n===t&&this.cache[r][n]||new Date>i.expAt?delete this.cache[r][n]:e[n]=i.value)}return e},e.set=function(t,e,r){var n=r||60,i=Math.round((new Date).getTime()/1e3)+n;return this.send("presence:cmd",{room:this.roomID,key:t,value:JSON.stringify(e),expAt:i}),this.cache[t]||(this.cache[t]={}),this.cache[t][this.actor]={value:e,expAt:new Date(1e3*i)},this.withoutExpiredAndSelf(t)},e.dangerouslyUpdateClientDirectly=function(t,e){if("room:rm_guest"===t)return this.withoutActorOrExpired(e.guest);if(e.room!==this.roomID)return!1;if(e.from===this.actor)return!1;var r={expAt:new Date(1e3*e.expAt),value:JSON.parse(e.value)};return this.cache[e.key]||(this.cache[e.key]={}),this.cache[e.key][e.from]=r,this.withoutExpiredAndSelf(e.key)},n(t,[{key:"me",get:function(){return console.warn("presence.me() is deprecated and will be removed in a future version!"),this.actor}}]),t}();function m(t){for(var e=window.atob(t),r=e.length,n=new Uint8Array(r),i=0;i<r;i++)n[i]=e.charCodeAt(i);return n.buffer}var v=function(){function t(t){this.listClients={},this.mapClients={},this.ws=new s(t.conn),this.token=t.token,this.roomID=t.roomID,this.docID=t.checkpoint.id,this.actor=t.actor,this.checkpoint=t.checkpoint,this.vs=this.checkpoint.vs,this.presenceClient=void 0}var r=t.prototype;return r.once=function(t){try{var e,r=this;return Promise.race([new Promise((function(t,e){return setTimeout((function(){return e("timeout")}),2e3)})),new Promise((function(n){e=r.ws.bind(t,(function(t){n(t)}))}))]).then((function(){e&&r.ws.unbind(t,e)}))}catch(t){return Promise.reject(t)}},r.reconnect=function(){try{var t=this;t.errorListener||(t.errorListener=t.ws.bind("error",(function(t){console.error("Room Service encountered a server-side error. If you see this, please let us know; this could be a bug.",t)})));var e=t.once("guest:authenticated");return t.ws.send("guest:authenticate",t.token),Promise.resolve(e).then((function(){var e=t.once("room:joined");return t.ws.send("room:join",t.roomID),Promise.resolve(e).then((function(){}))}))}catch(t){return Promise.reject(t)}},r.list=function(t){if(this.listClients[t])return this.listClients[t];this.checkpoint.lists[t]||(this.ws.send("doc:cmd",{args:["lcreate",this.docID,t],room:this.roomID}),this.checkpoint.lists[t]={afters:[],ids:[],values:[]});var e=new d(this.checkpoint,this.roomID,this.docID,t,this.ws,this.actor);return this.listClients[t]=e,e},r.map=function(t){if(this.mapClients[t])return this.mapClients[t];this.checkpoint.maps[t]||this.ws.send("doc:cmd",{args:["mcreate",this.docID,t],room:this.roomID});var e=new l(this.checkpoint.maps[t]||{},this.roomID,this.docID,t,this.ws);return this.mapClients[t]=e,e},r.presence=function(){if(this.presenceClient)return this.presenceClient;var t=new f(this.roomID,this.ws,this.actor,this.token);try{this.presenceClient=t}catch(t){throw new Error("Don't Freeze State. See more: https://err.sh/getroomservice/browser/dont-freeze")}return this.presenceClient},r.subscribe=function(t,r,n){var i=this;return"string"==typeof r?(t||e(!1),[{event:"presence:fwd",fn:this.ws.bind("presence:fwd",(function(o){if(o.room===i.roomID&&o.key===r&&o.from!==i.actor){var s=t.dangerouslyUpdateClientDirectly("presence:fwd",o);s&&(n||e(!1),n(s,o.from))}}))},{event:"room:rm_guest",fn:this.ws.bind("room:rm_guest",(function(r){if(r.room===i.roomID){var o=t.dangerouslyUpdateClientDirectly("room:rm_guest",r);o&&(n||e(!1),n(o,r.guest))}}))}]):[{event:"doc:fwd",fn:this.ws.bind("doc:fwd",(function(e){if(e.room===i.roomID)if(!e.args||e.args.length<3)console.error("Unexpected command: ",e.args);else if(!function(t,e){if(!t)return!0;if(!e)return!1;for(var r=new Uint8Array(m(t).slice(0,9)),n=new Uint8Array(m(e).slice(0,9)),i=0;i<r.byteLength;i++){if(n[i]>r[i])return!0;if(n[i]<r[i])return!1}return!1}(e.vs,i.vs)&&e.from!==i.actor){var n=[e.args[1],e.args[2]];if(n[0]===i.docID&&n[1]===t.id){i.vs=e.vs;var o=t.dangerouslyUpdateClientDirectly(e.args);r(o,e.from)}}}))}]},r.unsubscribe=function(t){for(var e,r=o(t);!(e=r()).done;){var n=e.value;this.ws.unbind(n.event,n.fn)}},n(t,[{key:"me",get:function(){return this.actor}}]),t}(),p=function(){function t(t){this.roomClients={},this.auth=t.auth}return t.prototype.room=function(t){try{var e=this;if(e.roomClients[t])return Promise.resolve(e.roomClients[t]);var r=new WebSocket("wss://super.roomservice.dev/ws");return Promise.resolve(function(t,e,r,n,i){try{return Promise.resolve(function(t,e,r){try{var n=function(r){return i?r:Promise.resolve(fetch(t,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({resources:[{object:"document",reference:"default",permission:"read_write",room:e},{object:"room",reference:e,permission:"join"}]})})).then((function(t){if(401===t.status)throw new Error("The Auth Webhook returned unauthorized.");if(200!==t.status)throw new Error("The Auth Webhook returned a status code other than 200.");return Promise.resolve(t.json()).then((function(t){var e=t.resources,r=t.token,n=t.user;if(!e||!r||!n){if("Unauthorized"===t.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:e.find((function(t){return"document"===t.object})).id,roomID:e.find((function(t){return"room"===t.object})).id}}))}))},i=!1,o=function(){if("function"==typeof t)return Promise.resolve(t(e)).then((function(t){if(!t.user)throw new Error("The auth function must return a 'user' key.");var e=t.resources.find((function(t){return"document"===t.object})).id,r=t.resources.find((function(t){return"room"===t.object})).id;return i=!0,{token:t.token,guestReference:t.user,docID:e,roomID:r}}))}();return Promise.resolve(o&&o.then?o.then(n):n(o))}catch(t){return Promise.reject(t)}}(r,n)).then((function(e){return Promise.resolve(function(t,e,r){try{return Promise.resolve(fetch("https://super.roomservice.dev/docs/"+r,{headers:{Authorization:"Bearer: "+e}})).then((function(t){return Promise.resolve(t.json())}))}catch(t){return Promise.reject(t)}}(0,e.token,e.docID)).then((function(r){var n=new v({conn:t,actor:e.guestReference,checkpoint:r.body,token:e.token,roomID:e.roomID});return Promise.resolve(n.reconnect()).then((function(){return n}))}))}))}catch(t){return Promise.reject(t)}}(r,0,e.auth,t)).then((function(r){return e.roomClients[t]=r,r}))}catch(t){return Promise.reject(t)}},t}();exports.ListClient=d,exports.MapClient=l,exports.PresenceClient=f,exports.RoomClient=v,exports.RoomService=p,exports.default=p;
//# sourceMappingURL=browser.cjs.production.min.js.map

@@ -1051,80 +1051,61 @@ import invariant from 'tiny-invariant';

_proto.list = function list(name) {
try {
var _this6 = this;
if (this.listClients[name]) {
return this.listClients[name];
} // create a list if it doesn't exist
if (_this6.listClients[name]) {
return Promise.resolve(_this6.listClients[name]);
} // create a list if it doesn't exist
if (!this.checkpoint.lists[name]) {
this.ws.send('doc:cmd', {
args: ['lcreate', this.docID, name],
room: this.roomID
}); // Assume success
if (!_this6.checkpoint.lists[name]) {
_this6.ws.send('doc:cmd', {
args: ['lcreate', _this6.docID, name],
room: _this6.roomID
}); // Assume success
this.checkpoint.lists[name] = {
afters: [],
ids: [],
values: []
};
}
_this6.checkpoint.lists[name] = {
afters: [],
ids: [],
values: []
};
}
var l = new ListClient(_this6.checkpoint, _this6.roomID, _this6.docID, name, _this6.ws, _this6.actor);
_this6.listClients[name] = l;
return Promise.resolve(l);
} catch (e) {
return Promise.reject(e);
}
var l = new ListClient(this.checkpoint, this.roomID, this.docID, name, this.ws, this.actor);
this.listClients[name] = l;
return l;
};
_proto.map = function map(name) {
try {
var _this8 = this;
if (this.mapClients[name]) {
return this.mapClients[name];
} // Create this map if it doesn't exist
if (_this8.mapClients[name]) {
return Promise.resolve(_this8.mapClients[name]);
} // Create this map if it doesn't exist
if (!this.checkpoint.maps[name]) {
this.ws.send('doc:cmd', {
args: ['mcreate', this.docID, name],
room: this.roomID
});
}
if (!_this8.checkpoint.maps[name]) {
_this8.ws.send('doc:cmd', {
args: ['mcreate', _this8.docID, name],
room: _this8.roomID
});
}
var m = new MapClient(_this8.checkpoint.maps[name] || {}, _this8.roomID, _this8.docID, name, _this8.ws);
_this8.mapClients[name] = m;
return Promise.resolve(m);
} catch (e) {
return Promise.reject(e);
}
var m = new MapClient(this.checkpoint.maps[name] || {}, this.roomID, this.docID, name, this.ws);
this.mapClients[name] = m;
return m;
};
_proto.presence = function presence() {
try {
var _this10 = this;
if (this.presenceClient) {
return this.presenceClient;
}
if (_this10.presenceClient) {
return Promise.resolve(_this10.presenceClient);
}
var p = new PresenceClient(this.roomID, this.ws, this.actor, this.token);
var p = new PresenceClient(_this10.roomID, _this10.ws, _this10.actor, _this10.token);
try {
this.presenceClient = p;
} catch (err) {
throw new Error("Don't Freeze State. See more: https://err.sh/getroomservice/browser/dont-freeze");
}
try {
_this10.presenceClient = p;
} catch (err) {
throw new Error("Don't Freeze State. See more: https://err.sh/getroomservice/browser/dont-freeze");
}
return Promise.resolve(_this10.presenceClient);
} catch (e) {
return Promise.reject(e);
}
return this.presenceClient;
};
_proto.subscribe = function subscribe(obj, onChangeFnOrString, onChangeFn) {
var _this11 = this;
var _this5 = this;

@@ -1135,5 +1116,5 @@ // Presence handler

var fwdListener = this.ws.bind('presence:fwd', function (body) {
if (body.room !== _this11.roomID) return;
if (body.room !== _this5.roomID) return;
if (body.key !== onChangeFnOrString) return;
if (body.from === _this11.actor) return;
if (body.from === _this5.actor) return;
var newObj = obj.dangerouslyUpdateClientDirectly('presence:fwd', body);

@@ -1145,3 +1126,3 @@ if (!newObj) return;

var leaveListener = this.ws.bind('room:rm_guest', function (body) {
if (body.room !== _this11.roomID) return;
if (body.room !== _this5.roomID) return;
var newObj = obj.dangerouslyUpdateClientDirectly('room:rm_guest', body);

@@ -1163,3 +1144,3 @@ if (!newObj) return;

var bound = this.ws.bind('doc:fwd', function (body) {
if (body.room !== _this11.roomID) return;
if (body.room !== _this5.roomID) return;

@@ -1174,11 +1155,11 @@ if (!body.args || body.args.length < 3) {

if (isOlderVS(body.vs, _this11.vs)) return; // Ignore validated commands
if (isOlderVS(body.vs, _this5.vs)) return; // Ignore validated commands
if (body.from === _this11.actor) return;
if (body.from === _this5.actor) return;
var _ref = [body.args[1], body.args[2]],
docID = _ref[0],
objID = _ref[1];
if (docID !== _this11.docID) return;
if (docID !== _this5.docID) return;
if (objID !== obj.id) return;
_this11.vs = body.vs;
_this5.vs = body.vs;
var newObj = obj.dangerouslyUpdateClientDirectly(body.args);

@@ -1185,0 +1166,0 @@ onChangeFnOrString(newObj, body.from);

@@ -36,5 +36,5 @@ import { WebSocketLikeConnection, DocumentCheckpoint, AuthStrategy, Prop } from './types';

get me(): string;
list(name: string): Promise<ListClient>;
map(name: string): Promise<MapClient>;
presence(): Promise<PresenceClient>;
list(name: string): ListClient;
map(name: string): MapClient;
presence(): PresenceClient;
subscribe(list: ListClient, onChangeFn: (list: ListClient) => any): ListenerBundle;

@@ -41,0 +41,0 @@ subscribe(list: ListClient, onChangeFn: (list: ListClient, from: string) => any): ListenerBundle;

{
"version": "1.0.5-0",
"version": "2.0.0-0",
"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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc