Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

broadcast-channel

Package Overview
Dependencies
Maintainers
1
Versions
98
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

broadcast-channel - npm Package Compare versions

Comparing version 3.4.1 to 3.5.0

.github/README.md

5

CHANGELOG.md

@@ -5,2 +5,7 @@ # CHANGELOG

## 3.5.0 (11 March 2021)
Features:
- Added `LeaderElector.onduplicate`
## 3.4.0 (24 January 2021)

@@ -7,0 +12,0 @@

5

dist/es/index.es5.js

@@ -9,3 +9,3 @@ /**

*/
import { BroadcastChannel, createLeaderElection, clearNodeFolder, enforceOptions } from './index.js';
import { BroadcastChannel, createLeaderElection, clearNodeFolder, enforceOptions, beLeader } from './index.js';
module.exports = {

@@ -15,3 +15,4 @@ BroadcastChannel: BroadcastChannel,

clearNodeFolder: clearNodeFolder,
enforceOptions: enforceOptions
enforceOptions: enforceOptions,
beLeader: beLeader
};

2

dist/es/index.js
export { BroadcastChannel, clearNodeFolder, enforceOptions } from './broadcast-channel';
export { createLeaderElection } from './leader-election';
export { createLeaderElection, beLeader } from './leader-election';

@@ -19,2 +19,7 @@ import { sleep, randomToken } from './util.js';

this._invs = []; // _intervals
this._dpL = function () {}; // onduplicate listener
this._dpLC = false; // true when onduplicate called
};

@@ -71,3 +76,3 @@

}).then(function () {
return _beLeader(_this);
return beLeader(_this);
}) // no one disagreed -> this one is now leader

@@ -101,2 +106,7 @@ .then(function () {

},
set onduplicate(fn) {
this._dpL = fn;
},
die: function die() {

@@ -129,4 +139,7 @@ var _this2 = this;

var finish = function finish() {
if (resolved) return;
function finish() {
if (resolved) {
return;
}
resolved = true;

@@ -138,7 +151,9 @@ clearInterval(interval);

res(true);
}; // try once now
} // try once now
leaderElector.applyOnce().then(function () {
if (leaderElector.isLeader) finish();
if (leaderElector.isLeader) {
finish();
}
}); // try on fallbackInterval

@@ -148,3 +163,5 @@

leaderElector.applyOnce().then(function () {
if (leaderElector.isLeader) finish();
if (leaderElector.isLeader) {
finish();
}
});

@@ -183,3 +200,3 @@ }, leaderElector._options.fallbackInterval);

function _beLeader(leaderElector) {
export function beLeader(leaderElector) {
leaderElector.isLeader = true;

@@ -196,2 +213,20 @@ var unloadFn = unload.add(function () {

}
if (msg.context === 'leader' && msg.action === 'tell' && !leaderElector._dpLC) {
/**
* another instance is also leader!
* This can happen on rare events
* like when the CPU is at 100% for long time
* or the tabs are open very long and the browser throttles them.
* @link https://github.com/pubkey/broadcast-channel/issues/414
* @link https://github.com/pubkey/broadcast-channel/issues/385
*/
leaderElector._dpLC = true;
leaderElector._dpL(); // message the lib user so the app can handle the problem
_sendMessage(leaderElector, 'tell'); // ensure other leader also knows the problem
}
};

@@ -198,0 +233,0 @@

@@ -277,3 +277,4 @@ (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){

clearNodeFolder: _index.clearNodeFolder,
enforceOptions: _index.enforceOptions
enforceOptions: _index.enforceOptions,
beLeader: _index.beLeader
};

@@ -310,2 +311,8 @@ },{"./index.js":4}],4:[function(require,module,exports){

});
Object.defineProperty(exports, "beLeader", {
enumerable: true,
get: function get() {
return _leaderElection.beLeader;
}
});

@@ -323,2 +330,3 @@ var _broadcastChannel = require("./broadcast-channel");

});
exports.beLeader = beLeader;
exports.createLeaderElection = createLeaderElection;

@@ -345,2 +353,7 @@

this._invs = []; // _intervals
this._dpL = function () {}; // onduplicate listener
this._dpLC = false; // true when onduplicate called
};

@@ -397,3 +410,3 @@

}).then(function () {
return _beLeader(_this);
return beLeader(_this);
}) // no one disagreed -> this one is now leader

@@ -427,2 +440,7 @@ .then(function () {

},
set onduplicate(fn) {
this._dpL = fn;
},
die: function die() {

@@ -455,4 +473,7 @@ var _this2 = this;

var finish = function finish() {
if (resolved) return;
function finish() {
if (resolved) {
return;
}
resolved = true;

@@ -464,7 +485,9 @@ clearInterval(interval);

res(true);
}; // try once now
} // try once now
leaderElector.applyOnce().then(function () {
if (leaderElector.isLeader) finish();
if (leaderElector.isLeader) {
finish();
}
}); // try on fallbackInterval

@@ -474,3 +497,5 @@

leaderElector.applyOnce().then(function () {
if (leaderElector.isLeader) finish();
if (leaderElector.isLeader) {
finish();
}
});

@@ -509,3 +534,3 @@ }, leaderElector._options.fallbackInterval);

function _beLeader(leaderElector) {
function beLeader(leaderElector) {
leaderElector.isLeader = true;

@@ -523,2 +548,20 @@

}
if (msg.context === 'leader' && msg.action === 'tell' && !leaderElector._dpLC) {
/**
* another instance is also leader!
* This can happen on rare events
* like when the CPU is at 100% for long time
* or the tabs are open very long and the browser throttles them.
* @link https://github.com/pubkey/broadcast-channel/issues/414
* @link https://github.com/pubkey/broadcast-channel/issues/385
*/
leaderElector._dpLC = true;
leaderElector._dpL(); // message the lib user so the app can handle the problem
_sendMessage(leaderElector, 'tell'); // ensure other leader also knows the problem
}
};

@@ -525,0 +568,0 @@

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

!function r(o,i,s){function a(t,e){if(!i[t]){if(!o[t]){var n="function"==typeof require&&require;if(!e&&n)return n(t,!0);if(u)return u(t,!0);throw(n=new Error("Cannot find module '"+t+"'")).code="MODULE_NOT_FOUND",n}n=i[t]={exports:{}},o[t][0].call(n.exports,function(e){return a(o[t][1][e]||e)},n,n.exports,r,o,i,s)}return i[t].exports}for(var u="function"==typeof require&&require,e=0;e<s.length;e++)a(s[e]);return a}({1:[function(e,t,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0}),n.clearNodeFolder=function(e){e=(0,s.fillOptionsWithDefaults)(e);e=(0,i.chooseMethod)(e);return"node"===e.type?e.clearNodeFolder().then(function(){return!0}):Promise.resolve(!1)},n.enforceOptions=function(e){r=e},n.BroadcastChannel=void 0;var r,o=e("./util.js"),i=e("./method-chooser.js"),s=e("./options.js"),e=function(e,t){var n;this.name=e,r&&(t=r),this.options=(0,s.fillOptionsWithDefaults)(t),this.method=(0,i.chooseMethod)(this.options),this._iL=!1,this._onML=null,this._addEL={message:[],internal:[]},this._befC=[],this._prepP=null,t=(n=this).method.create(n.name,n.options),(0,o.isPromise)(t)?(n._prepP=t).then(function(e){n._state=e}):n._state=t};function a(e,t,n){var r={time:e.method.microSeconds(),type:t,data:n};return(e._prepP||Promise.resolve()).then(function(){return e.method.postMessage(e._state,r)})}function u(e){return 0<e._addEL.message.length||0<e._addEL.internal.length}function c(e,t,n){var r,o,i;e._addEL[t].push(n),!(r=e)._iL&&u(r)&&(o=function(t){r._addEL[t.type].forEach(function(e){t.time>=e.time&&e.fn(t.data)})},i=r.method.microSeconds(),r._prepP?r._prepP.then(function(){r._iL=!0,r.method.onMessage(r._state,o,i)}):(r._iL=!0,r.method.onMessage(r._state,o,i)))}function l(e,t,n){e._addEL[t]=e._addEL[t].filter(function(e){return e!==n}),(t=e)._iL&&!u(t)&&(t._iL=!1,e=t.method.microSeconds(),t.method.onMessage(t._state,null,e))}(n.BroadcastChannel=e)._pubkey=!0,e.prototype={postMessage:function(e){if(this.closed)throw new Error("BroadcastChannel.postMessage(): Cannot post message after channel has closed");return a(this,"message",e)},postInternal:function(e){return a(this,"internal",e)},set onmessage(e){var t={time:this.method.microSeconds(),fn:e};l(this,"message",this._onML),e&&"function"==typeof e?(this._onML=t,c(this,"message",t)):this._onML=null},addEventListener:function(e,t){var n=this.method.microSeconds();c(this,e,{time:n,fn:t})},removeEventListener:function(e,t){var n=this._addEL[e].find(function(e){return e.fn===t});l(this,e,n)},close:function(){var e=this;if(!this.closed){this.closed=!0;var t=this._prepP||Promise.resolve();return this._onML=null,this._addEL.message=[],t.then(function(){return Promise.all(e._befC.map(function(e){return e()}))}).then(function(){return e.method.close(e._state)})}},get type(){return this.method.type}}},{"./method-chooser.js":6,"./options.js":12,"./util.js":13}],2:[function(e,t,n){"use strict";var r=e("./index.es5.js"),e=r.BroadcastChannel,r=r.createLeaderElection;window.BroadcastChannel2=e,window.createLeaderElection=r},{"./index.es5.js":3}],3:[function(e,t,n){"use strict";e=e("./index.js");t.exports={BroadcastChannel:e.BroadcastChannel,createLeaderElection:e.createLeaderElection,clearNodeFolder:e.clearNodeFolder,enforceOptions:e.enforceOptions}},{"./index.js":4}],4:[function(e,t,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0}),Object.defineProperty(n,"BroadcastChannel",{enumerable:!0,get:function(){return r.BroadcastChannel}}),Object.defineProperty(n,"clearNodeFolder",{enumerable:!0,get:function(){return r.clearNodeFolder}}),Object.defineProperty(n,"enforceOptions",{enumerable:!0,get:function(){return r.enforceOptions}}),Object.defineProperty(n,"createLeaderElection",{enumerable:!0,get:function(){return o.createLeaderElection}});var r=e("./broadcast-channel"),o=e("./leader-election")},{"./broadcast-channel":1,"./leader-election":5}],5:[function(e,t,n){"use strict";var r=e("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(n,"__esModule",{value:!0}),n.createLeaderElection=function(e,t){if(e._leaderElector)throw new Error("BroadcastChannel already has a leader-elector");t=function(e,t){e=e||{};(e=JSON.parse(JSON.stringify(e))).fallbackInterval||(e.fallbackInterval=3e3);e.responseTime||(e.responseTime=t.method.averageResponseTime(t.options));return e}(t,e);var n=new o(e,t);return e._befC.push(function(){return n.die()}),e._leaderElector=n};var i=e("./util.js"),s=r(e("unload")),o=function(e,t){this._channel=e,this._options=t,this.isLeader=!1,this.isDead=!1,this.token=(0,i.randomToken)(),this._isApl=!1,this._reApply=!1,this._unl=[],this._lstns=[],this._invs=[]};function a(e,t){t={context:"leader",action:t,token:e.token};return e._channel.postInternal(t)}o.prototype={applyOnce:function(){var t=this;if(this.isLeader)return Promise.resolve(!1);if(this.isDead)return Promise.resolve(!1);if(this._isApl)return this._reApply=!0,Promise.resolve(!1);function n(e){"leader"===e.context&&e.token!=t.token&&(o.push(e),"apply"===e.action&&e.token>t.token&&(r=!0),"tell"===e.action&&(r=!0))}var r=!(this._isApl=!0),o=[];return this._channel.addEventListener("internal",n),a(this,"apply").then(function(){return(0,i.sleep)(t._options.responseTime)}).then(function(){return r?Promise.reject(new Error):a(t,"apply")}).then(function(){return(0,i.sleep)(t._options.responseTime)}).then(function(){return r?Promise.reject(new Error):a(t)}).then(function(){return function(t){t.isLeader=!0;var e=s.default.add(function(){return t.die()});t._unl.push(e);e=function(e){"leader"===e.context&&"apply"===e.action&&a(t,"tell")};return t._channel.addEventListener("internal",e),t._lstns.push(e),a(t,"tell")}(t)}).then(function(){return!0}).catch(function(){return!1}).then(function(e){return t._channel.removeEventListener("internal",n),t._isApl=!1,!e&&t._reApply?(t._reApply=!1,t.applyOnce()):e})},awaitLeadership:function(){var i;return this._aLP||(this._aLP=(i=this).isLeader?Promise.resolve():new Promise(function(e){function t(){n||(n=!0,clearInterval(r),i._channel.removeEventListener("internal",o),e(!0))}var n=!1;i.applyOnce().then(function(){i.isLeader&&t()});var r=setInterval(function(){i.applyOnce().then(function(){i.isLeader&&t()})},i._options.fallbackInterval);i._invs.push(r);var o=function(e){"leader"===e.context&&"death"===e.action&&i.applyOnce().then(function(){i.isLeader&&t()})};i._channel.addEventListener("internal",o),i._lstns.push(o)})),this._aLP},die:function(){var t=this;if(!this.isDead)return this.isDead=!0,this._lstns.forEach(function(e){return t._channel.removeEventListener("internal",e)}),this._invs.forEach(function(e){return clearInterval(e)}),this._unl.forEach(function(e){e.remove()}),a(this,"death")}}},{"./util.js":13,"@babel/runtime/helpers/interopRequireDefault":14,unload:19}],6:[function(e,t,n){"use strict";var r=e("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(n,"__esModule",{value:!0}),n.chooseMethod=function(t){var e=[].concat(t.methods,u).filter(Boolean);if(t.type){if("simulate"===t.type)return s.default;var n=e.find(function(e){return e.type===t.type});if(n)return n;throw new Error("method-type "+t.type+" not found")}t.webWorkerSupport||a.isNode||(e=e.filter(function(e){return"idb"!==e.type}));e=e.find(function(e){return e.canBeUsed()});{if(e)return e;throw new Error("No useable methode found:"+JSON.stringify(u.map(function(e){return e.type})))}};var o=r(e("./methods/native.js")),i=r(e("./methods/indexed-db.js")),n=r(e("./methods/localstorage.js")),s=r(e("./methods/simulate.js")),a=e("./util"),u=[o.default,i.default,n.default];!a.isNode||"function"==typeof(e=e("../../src/methods/node.js")).canBeUsed&&u.push(e)},{"./methods/indexed-db.js":7,"./methods/localstorage.js":8,"./methods/native.js":9,"./methods/simulate.js":10,"./util":13,"@babel/runtime/helpers/interopRequireDefault":14}],7:[function(e,t,n){"use strict";var r=e("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(n,"__esModule",{value:!0}),n.getIdb=c,n.createDatabase=l,n.writeMessage=d,n.getAllMessages=function(e){var n=e.transaction(u).objectStore(u),r=[];return new Promise(function(t){n.openCursor().onsuccess=function(e){e=e.target.result;e?(r.push(e.value),e.continue()):t(r)}})},n.getMessagesHigherThan=f,n.removeMessageById=h,n.getOldMessages=p,n.cleanOldMessages=m,n.create=v,n.close=g,n.postMessage=w,n.onMessage=_,n.canBeUsed=y,n.averageResponseTime=k,n.default=n.type=n.microSeconds=void 0;var o=e("../util.js"),i=r(e("../oblivious-set")),s=e("../options"),e=o.microSeconds;n.microSeconds=e;var a="pubkey.broadcast-channel-0-",u="messages";function c(){if("undefined"!=typeof indexedDB)return indexedDB;if("undefined"!=typeof window){if(void 0!==window.mozIndexedDB)return window.mozIndexedDB;if(void 0!==window.webkitIndexedDB)return window.webkitIndexedDB;if(void 0!==window.msIndexedDB)return window.msIndexedDB}return!1}function l(e){var t=c(),e=a+e,n=t.open(e,1);return n.onupgradeneeded=function(e){e.target.result.createObjectStore(u,{keyPath:"id",autoIncrement:!0})},new Promise(function(e,t){n.onerror=function(e){return t(e)},n.onsuccess=function(){e(n.result)}})}function d(e,t,n){var r={uuid:t,time:(new Date).getTime(),data:n},o=e.transaction([u],"readwrite");return new Promise(function(e,t){o.oncomplete=function(){return e()},o.onerror=function(e){return t(e)},o.objectStore(u).add(r)})}function f(e,n){var r=e.transaction(u).objectStore(u),o=[];return new Promise(function(t){(function(){try{var e=IDBKeyRange.bound(n+1,1/0);return r.openCursor(e)}catch(e){return r.openCursor()}})().onsuccess=function(e){e=e.target.result;e?e.value.id<n+1?e.continue(n+1):(o.push(e.value),e.continue()):t(o)}})}function h(e,t){var n=e.transaction([u],"readwrite").objectStore(u).delete(t);return new Promise(function(e){n.onsuccess=function(){return e()}})}function p(e,t){var r=(new Date).getTime()-t,o=e.transaction(u).objectStore(u),i=[];return new Promise(function(n){o.openCursor().onsuccess=function(e){var t,e=e.target.result;e&&(t=e.value).time<r?(i.push(t),e.continue()):n(i)}})}function m(t,e){return p(t,e).then(function(e){return Promise.all(e.map(function(e){return h(t,e.id)}))})}function v(n,r){return r=(0,s.fillOptionsWithDefaults)(r),l(n).then(function(e){var t={closed:!1,lastCursorId:0,channelName:n,options:r,uuid:(0,o.randomToken)(),eMIs:new i.default(2*r.idb.ttl),writeBlockPromise:Promise.resolve(),messagesCallback:null,readQueuePromises:[],db:e};return e.onclose=function(){t.closed=!0,r.idb.onclose&&r.idb.onclose()},function e(t){if(t.closed)return;b(t).then(function(){return(0,o.sleep)(t.options.idb.fallbackInterval)}).then(function(){return e(t)})}(t),t})}function b(n){return!n.closed&&n.messagesCallback?f(n.db,n.lastCursorId).then(function(e){return e.filter(function(e){return!!e}).map(function(e){return e.id>n.lastCursorId&&(n.lastCursorId=e.id),e}).filter(function(e){return t=n,(e=e).uuid!==t.uuid&&(!t.eMIs.has(e.id)&&!(e.data.time<t.messagesCallbackTime));var t}).sort(function(e,t){return e.time-t.time}).forEach(function(e){n.messagesCallback&&(n.eMIs.add(e.id),n.messagesCallback(e.data))}),Promise.resolve()}):Promise.resolve()}function g(e){e.closed=!0,e.db.close()}function w(e,t){return e.writeBlockPromise=e.writeBlockPromise.then(function(){return d(e.db,e.uuid,t)}).then(function(){0===(0,o.randomInt)(0,10)&&m(e.db,e.options.idb.ttl)}),e.writeBlockPromise}function _(e,t,n){e.messagesCallbackTime=n,e.messagesCallback=t,b(e)}function y(){return!o.isNode&&!!c()}function k(e){return 2*e.idb.fallbackInterval}e={create:v,close:g,onMessage:_,postMessage:w,canBeUsed:y,type:n.type="idb",averageResponseTime:k,microSeconds:e};n.default=e},{"../oblivious-set":11,"../options":12,"../util.js":13,"@babel/runtime/helpers/interopRequireDefault":14}],8:[function(e,t,n){"use strict";var r=e("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(n,"__esModule",{value:!0}),n.getLocalStorage=u,n.storageKey=c,n.postMessage=l,n.addStorageEventListener=d,n.removeStorageEventListener=f,n.create=h,n.close=p,n.onMessage=m,n.canBeUsed=v,n.averageResponseTime=b,n.default=n.type=n.microSeconds=void 0;var i=r(e("../oblivious-set")),s=e("../options"),a=e("../util"),r=a.microSeconds;n.microSeconds=r;var o="pubkey.broadcastChannel-",e="localstorage";function u(){var e;if("undefined"==typeof window)return null;try{e=window.localStorage,e=window["ie8-eventlistener/storage"]||window.localStorage}catch(e){}return e}function c(e){return o+e}function l(o,i){return new Promise(function(r){(0,a.sleep)().then(function(){var e=c(o.channelName),t={token:(0,a.randomToken)(),time:(new Date).getTime(),data:i,uuid:o.uuid},n=JSON.stringify(t);u().setItem(e,n);t=document.createEvent("Event");t.initEvent("storage",!0,!0),t.key=e,t.newValue=n,window.dispatchEvent(t),r()})})}function d(e,t){var n=o+e,e=function(e){e.key===n&&t(JSON.parse(e.newValue))};return window.addEventListener("storage",e),e}function f(e){window.removeEventListener("storage",e)}function h(e,t){if(t=(0,s.fillOptionsWithDefaults)(t),!v())throw new Error("BroadcastChannel: localstorage cannot be used");var n=(0,a.randomToken)(),r=new i.default(t.localstorage.removeTimeout),o={channelName:e,uuid:n,eMIs:r};return o.listener=d(e,function(e){o.messagesCallback&&e.uuid!==n&&e.token&&!r.has(e.token)&&(e.data.time&&e.data.time<o.messagesCallbackTime||(r.add(e.token),o.messagesCallback(e.data)))}),o}function p(e){f(e.listener)}function m(e,t,n){e.messagesCallbackTime=n,e.messagesCallback=t}function v(){if(a.isNode)return!1;var e=u();if(!e)return!1;try{var t="__broadcastchannel_check";e.setItem(t,"works"),e.removeItem(t)}catch(e){return!1}return!0}function b(){var e=navigator.userAgent.toLowerCase();return e.includes("safari")&&!e.includes("chrome")?240:120}r={create:h,close:p,onMessage:m,postMessage:l,canBeUsed:v,type:n.type=e,averageResponseTime:b,microSeconds:r};n.default=r},{"../oblivious-set":11,"../options":12,"../util":13,"@babel/runtime/helpers/interopRequireDefault":14}],9:[function(e,t,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0}),n.create=o,n.close=i,n.postMessage=s,n.onMessage=a,n.canBeUsed=u,n.averageResponseTime=c,n.default=n.type=n.microSeconds=void 0;var r=e("../util"),e=r.microSeconds;n.microSeconds=e;function o(e){var t={messagesCallback:null,bc:new BroadcastChannel(e),subFns:[]};return t.bc.onmessage=function(e){t.messagesCallback&&t.messagesCallback(e.data)},t}function i(e){e.bc.close(),e.subFns=[]}function s(e,t){e.bc.postMessage(t,!1)}function a(e,t){e.messagesCallback=t}function u(){if(r.isNode&&"undefined"==typeof window)return!1;if("function"!=typeof BroadcastChannel)return!1;if(BroadcastChannel._pubkey)throw new Error("BroadcastChannel: Do not overwrite window.BroadcastChannel with this module, this is not a polyfill");return!0}function c(){return 150}e={create:o,close:i,onMessage:a,postMessage:s,canBeUsed:u,type:n.type="native",averageResponseTime:c,microSeconds:e};n.default=e},{"../util":13}],10:[function(e,t,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0}),n.create=i,n.close=s,n.postMessage=a,n.onMessage=u,n.canBeUsed=c,n.averageResponseTime=l,n.default=n.type=n.microSeconds=void 0;var r=e("../util").microSeconds;n.microSeconds=r;e="simulate";n.type=e;var o=new Set;function i(e){e={name:e,messagesCallback:null};return o.add(e),e}function s(e){o.delete(e)}function a(t,n){return new Promise(function(e){return setTimeout(function(){Array.from(o).filter(function(e){return e.name===t.name}).filter(function(e){return e!==t}).filter(function(e){return!!e.messagesCallback}).forEach(function(e){return e.messagesCallback(n)}),e()},5)})}function u(e,t){e.messagesCallback=t}function c(){return!0}function l(){return 5}r={create:i,close:s,onMessage:u,postMessage:a,canBeUsed:c,type:e,averageResponseTime:l,microSeconds:r};n.default=r},{"../util":13}],11:[function(e,t,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0}),n.default=void 0;function s(){return(new Date).getTime()}function r(r){var o=new Set,i=new Map;this.has=o.has.bind(o),this.add=function(e){i.set(e,s()),o.add(e),function(){for(var e=s()-r,t=o[Symbol.iterator]();;){var n=t.next().value;if(!n)return;if(!(i.get(n)<e))return;i.delete(n),o.delete(n)}}()},this.clear=function(){o.clear(),i.clear()}}n.default=r},{}],12:[function(e,t,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0}),n.fillOptionsWithDefaults=function(){var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},t=JSON.parse(JSON.stringify(e));void 0===t.webWorkerSupport&&(t.webWorkerSupport=!0);t.idb||(t.idb={});t.idb.ttl||(t.idb.ttl=45e3);t.idb.fallbackInterval||(t.idb.fallbackInterval=150);e.idb&&"function"==typeof e.idb.onclose&&(t.idb.onclose=e.idb.onclose);t.localstorage||(t.localstorage={});t.localstorage.removeTimeout||(t.localstorage.removeTimeout=6e4);e.methods&&(t.methods=e.methods);t.node||(t.node={});t.node.ttl||(t.node.ttl=12e4);void 0===t.node.useFastPath&&(t.node.useFastPath=!0);return t}},{}],13:[function(e,t,o){(function(r){(function(){"use strict";Object.defineProperty(o,"__esModule",{value:!0}),o.isPromise=function(e){return!(!e||"function"!=typeof e.then)},o.sleep=function(t){t=t||0;return new Promise(function(e){return setTimeout(e,t)})},o.randomInt=function(e,t){return Math.floor(Math.random()*(t-e+1)+e)},o.randomToken=function(){return Math.random().toString(36).substring(2)},o.microSeconds=function(){var e=(new Date).getTime();return e===t?1e3*e+ ++n:(n=0,1e3*(t=e))},o.isNode=void 0;var t=0,n=0;var e="[object process]"===Object.prototype.toString.call(void 0!==r?r:0);o.isNode=e}).call(this)}).call(this,e("_process"))},{_process:17}],14:[function(e,t,n){t.exports=function(e){return e&&e.__esModule?e:{default:e}}},{}],15:[function(e,t,n){},{}],16:[function(e,t,n){t.exports=!1},{}],17:[function(e,t,n){var r,o,t=t.exports={};function i(){throw new Error("setTimeout has not been defined")}function s(){throw new Error("clearTimeout has not been defined")}function a(t){if(r===setTimeout)return setTimeout(t,0);if((r===i||!r)&&setTimeout)return r=setTimeout,setTimeout(t,0);try{return r(t,0)}catch(e){try{return r.call(null,t,0)}catch(e){return r.call(this,t,0)}}}!function(){try{r="function"==typeof setTimeout?setTimeout:i}catch(e){r=i}try{o="function"==typeof clearTimeout?clearTimeout:s}catch(e){o=s}}();var u,c=[],l=!1,d=-1;function f(){l&&u&&(l=!1,u.length?c=u.concat(c):d=-1,c.length&&h())}function h(){if(!l){var e=a(f);l=!0;for(var t=c.length;t;){for(u=c,c=[];++d<t;)u&&u[d].run();d=-1,t=c.length}u=null,l=!1,function(t){if(o===clearTimeout)return clearTimeout(t);if((o===s||!o)&&clearTimeout)return o=clearTimeout,clearTimeout(t);try{o(t)}catch(e){try{return o.call(null,t)}catch(e){return o.call(this,t)}}}(e)}}function p(e,t){this.fun=e,this.array=t}function m(){}t.nextTick=function(e){var t=new Array(arguments.length-1);if(1<arguments.length)for(var n=1;n<arguments.length;n++)t[n-1]=arguments[n];c.push(new p(e,t)),1!==c.length||l||a(h)},p.prototype.run=function(){this.fun.apply(null,this.array)},t.title="browser",t.browser=!0,t.env={},t.argv=[],t.version="",t.versions={},t.on=m,t.addListener=m,t.once=m,t.off=m,t.removeListener=m,t.removeAllListeners=m,t.emit=m,t.prependListener=m,t.prependOnceListener=m,t.listeners=function(e){return[]},t.binding=function(e){throw new Error("process.binding is not supported")},t.cwd=function(){return"/"},t.chdir=function(e){throw new Error("process.chdir is not supported")},t.umask=function(){return 0}},{}],18:[function(e,t,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0}),n.default=void 0;var r={add:function(e){"function"==typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope||"function"==typeof window.addEventListener&&(window.addEventListener("beforeunload",function(){e()},!0),window.addEventListener("unload",function(){e()},!0))}};n.default=r},{}],19:[function(e,t,n){"use strict";var r=e("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(n,"__esModule",{value:!0}),n.add=c,n.runAll=l,n.removeAll=d,n.getSize=f,n.default=void 0;var o=r(e("detect-node")),i=r(e("./browser.js")),e=r(e("./node.js")),s=(o.default?e:i).default,a=new Set,u=!1;function c(e){if(u||(u=!0,s.add(l)),"function"!=typeof e)throw new Error("Listener is no function");return a.add(e),{remove:function(){return a.delete(e)},run:function(){return a.delete(e),e()}}}function l(){var t=[];return a.forEach(function(e){t.push(e()),a.delete(e)}),Promise.all(t)}function d(){a.clear()}function f(){return a.size}i={add:c,runAll:l,removeAll:d,getSize:f};n.default=i},{"./browser.js":18,"./node.js":15,"@babel/runtime/helpers/interopRequireDefault":14,"detect-node":16}]},{},[2]);
!function r(o,i,s){function a(t,e){if(!i[t]){if(!o[t]){var n="function"==typeof require&&require;if(!e&&n)return n(t,!0);if(u)return u(t,!0);throw(n=new Error("Cannot find module '"+t+"'")).code="MODULE_NOT_FOUND",n}n=i[t]={exports:{}},o[t][0].call(n.exports,function(e){return a(o[t][1][e]||e)},n,n.exports,r,o,i,s)}return i[t].exports}for(var u="function"==typeof require&&require,e=0;e<s.length;e++)a(s[e]);return a}({1:[function(e,t,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0}),n.clearNodeFolder=function(e){e=(0,s.fillOptionsWithDefaults)(e);e=(0,i.chooseMethod)(e);return"node"===e.type?e.clearNodeFolder().then(function(){return!0}):Promise.resolve(!1)},n.enforceOptions=function(e){r=e},n.BroadcastChannel=void 0;var r,o=e("./util.js"),i=e("./method-chooser.js"),s=e("./options.js"),e=function(e,t){var n;this.name=e,r&&(t=r),this.options=(0,s.fillOptionsWithDefaults)(t),this.method=(0,i.chooseMethod)(this.options),this._iL=!1,this._onML=null,this._addEL={message:[],internal:[]},this._befC=[],this._prepP=null,t=(n=this).method.create(n.name,n.options),(0,o.isPromise)(t)?(n._prepP=t).then(function(e){n._state=e}):n._state=t};function a(e,t,n){var r={time:e.method.microSeconds(),type:t,data:n};return(e._prepP||Promise.resolve()).then(function(){return e.method.postMessage(e._state,r)})}function u(e){return 0<e._addEL.message.length||0<e._addEL.internal.length}function c(e,t,n){var r,o,i;e._addEL[t].push(n),!(r=e)._iL&&u(r)&&(o=function(t){r._addEL[t.type].forEach(function(e){t.time>=e.time&&e.fn(t.data)})},i=r.method.microSeconds(),r._prepP?r._prepP.then(function(){r._iL=!0,r.method.onMessage(r._state,o,i)}):(r._iL=!0,r.method.onMessage(r._state,o,i)))}function l(e,t,n){e._addEL[t]=e._addEL[t].filter(function(e){return e!==n}),(t=e)._iL&&!u(t)&&(t._iL=!1,e=t.method.microSeconds(),t.method.onMessage(t._state,null,e))}(n.BroadcastChannel=e)._pubkey=!0,e.prototype={postMessage:function(e){if(this.closed)throw new Error("BroadcastChannel.postMessage(): Cannot post message after channel has closed");return a(this,"message",e)},postInternal:function(e){return a(this,"internal",e)},set onmessage(e){var t={time:this.method.microSeconds(),fn:e};l(this,"message",this._onML),e&&"function"==typeof e?(this._onML=t,c(this,"message",t)):this._onML=null},addEventListener:function(e,t){var n=this.method.microSeconds();c(this,e,{time:n,fn:t})},removeEventListener:function(e,t){var n=this._addEL[e].find(function(e){return e.fn===t});l(this,e,n)},close:function(){var e=this;if(!this.closed){this.closed=!0;var t=this._prepP||Promise.resolve();return this._onML=null,this._addEL.message=[],t.then(function(){return Promise.all(e._befC.map(function(e){return e()}))}).then(function(){return e.method.close(e._state)})}},get type(){return this.method.type}}},{"./method-chooser.js":6,"./options.js":12,"./util.js":13}],2:[function(e,t,n){"use strict";var r=e("./index.es5.js"),e=r.BroadcastChannel,r=r.createLeaderElection;window.BroadcastChannel2=e,window.createLeaderElection=r},{"./index.es5.js":3}],3:[function(e,t,n){"use strict";e=e("./index.js");t.exports={BroadcastChannel:e.BroadcastChannel,createLeaderElection:e.createLeaderElection,clearNodeFolder:e.clearNodeFolder,enforceOptions:e.enforceOptions,beLeader:e.beLeader}},{"./index.js":4}],4:[function(e,t,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0}),Object.defineProperty(n,"BroadcastChannel",{enumerable:!0,get:function(){return r.BroadcastChannel}}),Object.defineProperty(n,"clearNodeFolder",{enumerable:!0,get:function(){return r.clearNodeFolder}}),Object.defineProperty(n,"enforceOptions",{enumerable:!0,get:function(){return r.enforceOptions}}),Object.defineProperty(n,"createLeaderElection",{enumerable:!0,get:function(){return o.createLeaderElection}}),Object.defineProperty(n,"beLeader",{enumerable:!0,get:function(){return o.beLeader}});var r=e("./broadcast-channel"),o=e("./leader-election")},{"./broadcast-channel":1,"./leader-election":5}],5:[function(e,t,n){"use strict";var r=e("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(n,"__esModule",{value:!0}),n.beLeader=u,n.createLeaderElection=function(e,t){if(e._leaderElector)throw new Error("BroadcastChannel already has a leader-elector");t=function(e,t){e=e||{};(e=JSON.parse(JSON.stringify(e))).fallbackInterval||(e.fallbackInterval=3e3);e.responseTime||(e.responseTime=t.method.averageResponseTime(t.options));return e}(t,e);var n=new s(e,t);return e._befC.push(function(){return n.die()}),e._leaderElector=n};var i=e("./util.js"),o=r(e("unload")),s=function(e,t){this._channel=e,this._options=t,this.isLeader=!1,this.isDead=!1,this.token=(0,i.randomToken)(),this._isApl=!1,this._reApply=!1,this._unl=[],this._lstns=[],this._invs=[],this._dpL=function(){},this._dpLC=!1};function a(e,t){t={context:"leader",action:t,token:e.token};return e._channel.postInternal(t)}function u(t){t.isLeader=!0;var e=o.default.add(function(){return t.die()});t._unl.push(e);e=function(e){"leader"===e.context&&"apply"===e.action&&a(t,"tell"),"leader"!==e.context||"tell"!==e.action||t._dpLC||(t._dpLC=!0,t._dpL(),a(t,"tell"))};return t._channel.addEventListener("internal",e),t._lstns.push(e),a(t,"tell")}s.prototype={applyOnce:function(){var t=this;if(this.isLeader)return Promise.resolve(!1);if(this.isDead)return Promise.resolve(!1);if(this._isApl)return this._reApply=!0,Promise.resolve(!1);function n(e){"leader"===e.context&&e.token!=t.token&&(o.push(e),"apply"===e.action&&e.token>t.token&&(r=!0),"tell"===e.action&&(r=!0))}var r=!(this._isApl=!0),o=[];return this._channel.addEventListener("internal",n),a(this,"apply").then(function(){return(0,i.sleep)(t._options.responseTime)}).then(function(){return r?Promise.reject(new Error):a(t,"apply")}).then(function(){return(0,i.sleep)(t._options.responseTime)}).then(function(){return r?Promise.reject(new Error):a(t)}).then(function(){return u(t)}).then(function(){return!0}).catch(function(){return!1}).then(function(e){return t._channel.removeEventListener("internal",n),t._isApl=!1,!e&&t._reApply?(t._reApply=!1,t.applyOnce()):e})},awaitLeadership:function(){var i;return this._aLP||(this._aLP=(i=this).isLeader?Promise.resolve():new Promise(function(e){var t=!1;function n(){t||(t=!0,clearInterval(r),i._channel.removeEventListener("internal",o),e(!0))}i.applyOnce().then(function(){i.isLeader&&n()});var r=setInterval(function(){i.applyOnce().then(function(){i.isLeader&&n()})},i._options.fallbackInterval);i._invs.push(r);var o=function(e){"leader"===e.context&&"death"===e.action&&i.applyOnce().then(function(){i.isLeader&&n()})};i._channel.addEventListener("internal",o),i._lstns.push(o)})),this._aLP},set onduplicate(e){this._dpL=e},die:function(){var t=this;if(!this.isDead)return this.isDead=!0,this._lstns.forEach(function(e){return t._channel.removeEventListener("internal",e)}),this._invs.forEach(function(e){return clearInterval(e)}),this._unl.forEach(function(e){e.remove()}),a(this,"death")}}},{"./util.js":13,"@babel/runtime/helpers/interopRequireDefault":14,unload:19}],6:[function(e,t,n){"use strict";var r=e("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(n,"__esModule",{value:!0}),n.chooseMethod=function(t){var e=[].concat(t.methods,u).filter(Boolean);if(t.type){if("simulate"===t.type)return s.default;var n=e.find(function(e){return e.type===t.type});if(n)return n;throw new Error("method-type "+t.type+" not found")}t.webWorkerSupport||a.isNode||(e=e.filter(function(e){return"idb"!==e.type}));e=e.find(function(e){return e.canBeUsed()});{if(e)return e;throw new Error("No useable methode found:"+JSON.stringify(u.map(function(e){return e.type})))}};var o=r(e("./methods/native.js")),i=r(e("./methods/indexed-db.js")),n=r(e("./methods/localstorage.js")),s=r(e("./methods/simulate.js")),a=e("./util"),u=[o.default,i.default,n.default];!a.isNode||"function"==typeof(e=e("../../src/methods/node.js")).canBeUsed&&u.push(e)},{"./methods/indexed-db.js":7,"./methods/localstorage.js":8,"./methods/native.js":9,"./methods/simulate.js":10,"./util":13,"@babel/runtime/helpers/interopRequireDefault":14}],7:[function(e,t,n){"use strict";var r=e("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(n,"__esModule",{value:!0}),n.getIdb=c,n.createDatabase=l,n.writeMessage=d,n.getAllMessages=function(e){var n=e.transaction(u).objectStore(u),r=[];return new Promise(function(t){n.openCursor().onsuccess=function(e){e=e.target.result;e?(r.push(e.value),e.continue()):t(r)}})},n.getMessagesHigherThan=f,n.removeMessageById=h,n.getOldMessages=p,n.cleanOldMessages=m,n.create=v,n.close=g,n.postMessage=w,n.onMessage=_,n.canBeUsed=y,n.averageResponseTime=L,n.default=n.type=n.microSeconds=void 0;var o=e("../util.js"),i=r(e("../oblivious-set")),s=e("../options"),e=o.microSeconds;n.microSeconds=e;var a="pubkey.broadcast-channel-0-",u="messages";function c(){if("undefined"!=typeof indexedDB)return indexedDB;if("undefined"!=typeof window){if(void 0!==window.mozIndexedDB)return window.mozIndexedDB;if(void 0!==window.webkitIndexedDB)return window.webkitIndexedDB;if(void 0!==window.msIndexedDB)return window.msIndexedDB}return!1}function l(e){var t=c(),e=a+e,n=t.open(e,1);return n.onupgradeneeded=function(e){e.target.result.createObjectStore(u,{keyPath:"id",autoIncrement:!0})},new Promise(function(e,t){n.onerror=function(e){return t(e)},n.onsuccess=function(){e(n.result)}})}function d(e,t,n){var r={uuid:t,time:(new Date).getTime(),data:n},o=e.transaction([u],"readwrite");return new Promise(function(e,t){o.oncomplete=function(){return e()},o.onerror=function(e){return t(e)},o.objectStore(u).add(r)})}function f(e,n){var r=e.transaction(u).objectStore(u),o=[];return new Promise(function(t){(function(){try{var e=IDBKeyRange.bound(n+1,1/0);return r.openCursor(e)}catch(e){return r.openCursor()}})().onsuccess=function(e){e=e.target.result;e?e.value.id<n+1?e.continue(n+1):(o.push(e.value),e.continue()):t(o)}})}function h(e,t){var n=e.transaction([u],"readwrite").objectStore(u).delete(t);return new Promise(function(e){n.onsuccess=function(){return e()}})}function p(e,t){var r=(new Date).getTime()-t,o=e.transaction(u).objectStore(u),i=[];return new Promise(function(n){o.openCursor().onsuccess=function(e){var t,e=e.target.result;e&&(t=e.value).time<r?(i.push(t),e.continue()):n(i)}})}function m(t,e){return p(t,e).then(function(e){return Promise.all(e.map(function(e){return h(t,e.id)}))})}function v(n,r){return r=(0,s.fillOptionsWithDefaults)(r),l(n).then(function(e){var t={closed:!1,lastCursorId:0,channelName:n,options:r,uuid:(0,o.randomToken)(),eMIs:new i.default(2*r.idb.ttl),writeBlockPromise:Promise.resolve(),messagesCallback:null,readQueuePromises:[],db:e};return e.onclose=function(){t.closed=!0,r.idb.onclose&&r.idb.onclose()},function e(t){if(t.closed)return;b(t).then(function(){return(0,o.sleep)(t.options.idb.fallbackInterval)}).then(function(){return e(t)})}(t),t})}function b(n){return!n.closed&&n.messagesCallback?f(n.db,n.lastCursorId).then(function(e){return e.filter(function(e){return!!e}).map(function(e){return e.id>n.lastCursorId&&(n.lastCursorId=e.id),e}).filter(function(e){return t=n,(e=e).uuid!==t.uuid&&(!t.eMIs.has(e.id)&&!(e.data.time<t.messagesCallbackTime));var t}).sort(function(e,t){return e.time-t.time}).forEach(function(e){n.messagesCallback&&(n.eMIs.add(e.id),n.messagesCallback(e.data))}),Promise.resolve()}):Promise.resolve()}function g(e){e.closed=!0,e.db.close()}function w(e,t){return e.writeBlockPromise=e.writeBlockPromise.then(function(){return d(e.db,e.uuid,t)}).then(function(){0===(0,o.randomInt)(0,10)&&m(e.db,e.options.idb.ttl)}),e.writeBlockPromise}function _(e,t,n){e.messagesCallbackTime=n,e.messagesCallback=t,b(e)}function y(){return!o.isNode&&!!c()}function L(e){return 2*e.idb.fallbackInterval}e={create:v,close:g,onMessage:_,postMessage:w,canBeUsed:y,type:n.type="idb",averageResponseTime:L,microSeconds:e};n.default=e},{"../oblivious-set":11,"../options":12,"../util.js":13,"@babel/runtime/helpers/interopRequireDefault":14}],8:[function(e,t,n){"use strict";var r=e("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(n,"__esModule",{value:!0}),n.getLocalStorage=u,n.storageKey=c,n.postMessage=l,n.addStorageEventListener=d,n.removeStorageEventListener=f,n.create=h,n.close=p,n.onMessage=m,n.canBeUsed=v,n.averageResponseTime=b,n.default=n.type=n.microSeconds=void 0;var i=r(e("../oblivious-set")),s=e("../options"),a=e("../util"),r=a.microSeconds;n.microSeconds=r;var o="pubkey.broadcastChannel-",e="localstorage";function u(){var e;if("undefined"==typeof window)return null;try{e=window.localStorage,e=window["ie8-eventlistener/storage"]||window.localStorage}catch(e){}return e}function c(e){return o+e}function l(o,i){return new Promise(function(r){(0,a.sleep)().then(function(){var e=c(o.channelName),t={token:(0,a.randomToken)(),time:(new Date).getTime(),data:i,uuid:o.uuid},n=JSON.stringify(t);u().setItem(e,n);t=document.createEvent("Event");t.initEvent("storage",!0,!0),t.key=e,t.newValue=n,window.dispatchEvent(t),r()})})}function d(e,t){var n=o+e,e=function(e){e.key===n&&t(JSON.parse(e.newValue))};return window.addEventListener("storage",e),e}function f(e){window.removeEventListener("storage",e)}function h(e,t){if(t=(0,s.fillOptionsWithDefaults)(t),!v())throw new Error("BroadcastChannel: localstorage cannot be used");var n=(0,a.randomToken)(),r=new i.default(t.localstorage.removeTimeout),o={channelName:e,uuid:n,eMIs:r};return o.listener=d(e,function(e){o.messagesCallback&&e.uuid!==n&&e.token&&!r.has(e.token)&&(e.data.time&&e.data.time<o.messagesCallbackTime||(r.add(e.token),o.messagesCallback(e.data)))}),o}function p(e){f(e.listener)}function m(e,t,n){e.messagesCallbackTime=n,e.messagesCallback=t}function v(){if(a.isNode)return!1;var e=u();if(!e)return!1;try{var t="__broadcastchannel_check";e.setItem(t,"works"),e.removeItem(t)}catch(e){return!1}return!0}function b(){var e=navigator.userAgent.toLowerCase();return e.includes("safari")&&!e.includes("chrome")?240:120}r={create:h,close:p,onMessage:m,postMessage:l,canBeUsed:v,type:n.type=e,averageResponseTime:b,microSeconds:r};n.default=r},{"../oblivious-set":11,"../options":12,"../util":13,"@babel/runtime/helpers/interopRequireDefault":14}],9:[function(e,t,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0}),n.create=o,n.close=i,n.postMessage=s,n.onMessage=a,n.canBeUsed=u,n.averageResponseTime=c,n.default=n.type=n.microSeconds=void 0;var r=e("../util"),e=r.microSeconds;n.microSeconds=e;function o(e){var t={messagesCallback:null,bc:new BroadcastChannel(e),subFns:[]};return t.bc.onmessage=function(e){t.messagesCallback&&t.messagesCallback(e.data)},t}function i(e){e.bc.close(),e.subFns=[]}function s(e,t){e.bc.postMessage(t,!1)}function a(e,t){e.messagesCallback=t}function u(){if(r.isNode&&"undefined"==typeof window)return!1;if("function"!=typeof BroadcastChannel)return!1;if(BroadcastChannel._pubkey)throw new Error("BroadcastChannel: Do not overwrite window.BroadcastChannel with this module, this is not a polyfill");return!0}function c(){return 150}e={create:o,close:i,onMessage:a,postMessage:s,canBeUsed:u,type:n.type="native",averageResponseTime:c,microSeconds:e};n.default=e},{"../util":13}],10:[function(e,t,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0}),n.create=i,n.close=s,n.postMessage=a,n.onMessage=u,n.canBeUsed=c,n.averageResponseTime=l,n.default=n.type=n.microSeconds=void 0;var r=e("../util").microSeconds;n.microSeconds=r;e="simulate";n.type=e;var o=new Set;function i(e){e={name:e,messagesCallback:null};return o.add(e),e}function s(e){o.delete(e)}function a(t,n){return new Promise(function(e){return setTimeout(function(){Array.from(o).filter(function(e){return e.name===t.name}).filter(function(e){return e!==t}).filter(function(e){return!!e.messagesCallback}).forEach(function(e){return e.messagesCallback(n)}),e()},5)})}function u(e,t){e.messagesCallback=t}function c(){return!0}function l(){return 5}r={create:i,close:s,onMessage:u,postMessage:a,canBeUsed:c,type:e,averageResponseTime:l,microSeconds:r};n.default=r},{"../util":13}],11:[function(e,t,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0}),n.default=void 0;function s(){return(new Date).getTime()}function r(r){var o=new Set,i=new Map;this.has=o.has.bind(o),this.add=function(e){i.set(e,s()),o.add(e),function(){for(var e=s()-r,t=o[Symbol.iterator]();;){var n=t.next().value;if(!n)return;if(!(i.get(n)<e))return;i.delete(n),o.delete(n)}}()},this.clear=function(){o.clear(),i.clear()}}n.default=r},{}],12:[function(e,t,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0}),n.fillOptionsWithDefaults=function(){var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},t=JSON.parse(JSON.stringify(e));void 0===t.webWorkerSupport&&(t.webWorkerSupport=!0);t.idb||(t.idb={});t.idb.ttl||(t.idb.ttl=45e3);t.idb.fallbackInterval||(t.idb.fallbackInterval=150);e.idb&&"function"==typeof e.idb.onclose&&(t.idb.onclose=e.idb.onclose);t.localstorage||(t.localstorage={});t.localstorage.removeTimeout||(t.localstorage.removeTimeout=6e4);e.methods&&(t.methods=e.methods);t.node||(t.node={});t.node.ttl||(t.node.ttl=12e4);void 0===t.node.useFastPath&&(t.node.useFastPath=!0);return t}},{}],13:[function(e,t,o){(function(r){(function(){"use strict";Object.defineProperty(o,"__esModule",{value:!0}),o.isPromise=function(e){return!(!e||"function"!=typeof e.then)},o.sleep=function(t){t=t||0;return new Promise(function(e){return setTimeout(e,t)})},o.randomInt=function(e,t){return Math.floor(Math.random()*(t-e+1)+e)},o.randomToken=function(){return Math.random().toString(36).substring(2)},o.microSeconds=function(){var e=(new Date).getTime();return e===t?1e3*e+ ++n:(n=0,1e3*(t=e))},o.isNode=void 0;var t=0,n=0;var e="[object process]"===Object.prototype.toString.call(void 0!==r?r:0);o.isNode=e}).call(this)}).call(this,e("_process"))},{_process:17}],14:[function(e,t,n){t.exports=function(e){return e&&e.__esModule?e:{default:e}}},{}],15:[function(e,t,n){},{}],16:[function(e,t,n){t.exports=!1},{}],17:[function(e,t,n){var r,o,t=t.exports={};function i(){throw new Error("setTimeout has not been defined")}function s(){throw new Error("clearTimeout has not been defined")}function a(t){if(r===setTimeout)return setTimeout(t,0);if((r===i||!r)&&setTimeout)return r=setTimeout,setTimeout(t,0);try{return r(t,0)}catch(e){try{return r.call(null,t,0)}catch(e){return r.call(this,t,0)}}}!function(){try{r="function"==typeof setTimeout?setTimeout:i}catch(e){r=i}try{o="function"==typeof clearTimeout?clearTimeout:s}catch(e){o=s}}();var u,c=[],l=!1,d=-1;function f(){l&&u&&(l=!1,u.length?c=u.concat(c):d=-1,c.length&&h())}function h(){if(!l){var e=a(f);l=!0;for(var t=c.length;t;){for(u=c,c=[];++d<t;)u&&u[d].run();d=-1,t=c.length}u=null,l=!1,function(t){if(o===clearTimeout)return clearTimeout(t);if((o===s||!o)&&clearTimeout)return o=clearTimeout,clearTimeout(t);try{o(t)}catch(e){try{return o.call(null,t)}catch(e){return o.call(this,t)}}}(e)}}function p(e,t){this.fun=e,this.array=t}function m(){}t.nextTick=function(e){var t=new Array(arguments.length-1);if(1<arguments.length)for(var n=1;n<arguments.length;n++)t[n-1]=arguments[n];c.push(new p(e,t)),1!==c.length||l||a(h)},p.prototype.run=function(){this.fun.apply(null,this.array)},t.title="browser",t.browser=!0,t.env={},t.argv=[],t.version="",t.versions={},t.on=m,t.addListener=m,t.once=m,t.off=m,t.removeListener=m,t.removeAllListeners=m,t.emit=m,t.prependListener=m,t.prependOnceListener=m,t.listeners=function(e){return[]},t.binding=function(e){throw new Error("process.binding is not supported")},t.cwd=function(){return"/"},t.chdir=function(e){throw new Error("process.chdir is not supported")},t.umask=function(){return 0}},{}],18:[function(e,t,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0}),n.default=void 0;var r={add:function(e){"function"==typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope||"function"==typeof window.addEventListener&&(window.addEventListener("beforeunload",function(){e()},!0),window.addEventListener("unload",function(){e()},!0))}};n.default=r},{}],19:[function(e,t,n){"use strict";var r=e("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(n,"__esModule",{value:!0}),n.add=c,n.runAll=l,n.removeAll=d,n.getSize=f,n.default=void 0;var o=r(e("detect-node")),i=r(e("./browser.js")),e=r(e("./node.js")),s=(o.default?e:i).default,a=new Set,u=!1;function c(e){if(u||(u=!0,s.add(l)),"function"!=typeof e)throw new Error("Listener is no function");return a.add(e),{remove:function(){return a.delete(e)},run:function(){return a.delete(e),e()}}}function l(){var t=[];return a.forEach(function(e){t.push(e()),a.delete(e)}),Promise.all(t)}function d(){a.clear()}function f(){return a.size}i={add:c,runAll:l,removeAll:d,getSize:f};n.default=i},{"./browser.js":18,"./node.js":15,"@babel/runtime/helpers/interopRequireDefault":14,"detect-node":16}]},{},[2]);

@@ -17,3 +17,4 @@ "use strict";

clearNodeFolder: _index.clearNodeFolder,
enforceOptions: _index.enforceOptions
enforceOptions: _index.enforceOptions,
beLeader: _index.beLeader
};

@@ -30,2 +30,8 @@ "use strict";

});
Object.defineProperty(exports, "beLeader", {
enumerable: true,
get: function get() {
return _leaderElection.beLeader;
}
});

@@ -32,0 +38,0 @@ var _broadcastChannel = require("./broadcast-channel");

@@ -8,2 +8,3 @@ "use strict";

});
exports.beLeader = beLeader;
exports.createLeaderElection = createLeaderElection;

@@ -30,2 +31,7 @@

this._invs = []; // _intervals
this._dpL = function () {}; // onduplicate listener
this._dpLC = false; // true when onduplicate called
};

@@ -82,3 +88,3 @@

}).then(function () {
return _beLeader(_this);
return beLeader(_this);
}) // no one disagreed -> this one is now leader

@@ -112,2 +118,7 @@ .then(function () {

},
set onduplicate(fn) {
this._dpL = fn;
},
die: function die() {

@@ -140,4 +151,7 @@ var _this2 = this;

var finish = function finish() {
if (resolved) return;
function finish() {
if (resolved) {
return;
}
resolved = true;

@@ -149,7 +163,9 @@ clearInterval(interval);

res(true);
}; // try once now
} // try once now
leaderElector.applyOnce().then(function () {
if (leaderElector.isLeader) finish();
if (leaderElector.isLeader) {
finish();
}
}); // try on fallbackInterval

@@ -159,3 +175,5 @@

leaderElector.applyOnce().then(function () {
if (leaderElector.isLeader) finish();
if (leaderElector.isLeader) {
finish();
}
});

@@ -194,3 +212,3 @@ }, leaderElector._options.fallbackInterval);

function _beLeader(leaderElector) {
function beLeader(leaderElector) {
leaderElector.isLeader = true;

@@ -208,2 +226,20 @@

}
if (msg.context === 'leader' && msg.action === 'tell' && !leaderElector._dpLC) {
/**
* another instance is also leader!
* This can happen on rare events
* like when the CPU is at 100% for long time
* or the tabs are open very long and the browser throttles them.
* @link https://github.com/pubkey/broadcast-channel/issues/414
* @link https://github.com/pubkey/broadcast-channel/issues/385
*/
leaderElector._dpLC = true;
leaderElector._dpL(); // message the lib user so the app can handle the problem
_sendMessage(leaderElector, 'tell'); // ensure other leader also knows the problem
}
};

@@ -210,0 +246,0 @@

{
"name": "broadcast-channel",
"version": "3.4.1",
"version": "3.5.0",
"description": "A BroadcastChannel that works in New Browsers, Old Browsers, WebWorkers and NodeJs",

@@ -76,12 +76,12 @@ "homepage": "https://github.com/pubkey/broadcast-channel#readme",

"devDependencies": {
"@babel/cli": "7.12.10",
"@babel/core": "7.12.10",
"@babel/cli": "7.13.10",
"@babel/core": "7.13.10",
"@babel/plugin-check-constants": "7.0.0-beta.38",
"@babel/plugin-proposal-object-rest-spread": "7.12.1",
"@babel/plugin-transform-member-expression-literals": "7.12.1",
"@babel/plugin-transform-property-literals": "7.12.1",
"@babel/plugin-transform-runtime": "7.12.10",
"@babel/plugin-proposal-object-rest-spread": "7.13.8",
"@babel/plugin-transform-member-expression-literals": "7.12.13",
"@babel/plugin-transform-property-literals": "7.12.13",
"@babel/plugin-transform-runtime": "7.13.10",
"@babel/polyfill": "7.12.1",
"@babel/preset-env": "7.12.11",
"@babel/types": "7.12.12",
"@babel/preset-env": "7.13.10",
"@babel/types": "7.13.0",
"@types/core-js": "2.5.4",

@@ -93,11 +93,11 @@ "assert": "2.0.0",

"clone": "2.1.2",
"concurrently": "5.3.0",
"concurrently": "6.0.0",
"convert-hrtime": "3.0.0",
"copyfiles": "2.4.1",
"cross-env": "7.0.3",
"eslint": "7.18.0",
"eslint": "7.21.0",
"gzip-size-cli": "4.0.0",
"http-server": "0.12.3",
"jest": "26.6.3",
"karma": "6.0.1",
"karma": "6.1.2",
"karma-babel-preprocessor": "8.0.1",

@@ -114,16 +114,16 @@ "karma-browserify": "8.0.0",

"karma-safari-launcher": "1.0.0",
"mocha": "8.2.1",
"mocha": "8.3.1",
"pre-commit": "1.2.2",
"random-int": "2.0.1",
"random-token": "0.0.8",
"rollup": "2.38.0",
"rollup": "2.41.0",
"rollup-plugin-node-resolve": "5.2.0",
"rollup-plugin-uglify": "6.0.4",
"testcafe": "1.10.1",
"testcafe-hammerhead": "19.1.1",
"testcafe": "1.12.0",
"testcafe-hammerhead": "19.4.2",
"ts-node": "9.1.1",
"typescript": "4.1.3",
"watchify": "3.11.1",
"webpack": "5.17.0",
"webpack-cli": "4.4.0"
"typescript": "4.2.3",
"watchify": "4.0.0",
"webpack": "5.24.4",
"webpack-cli": "4.5.0"
},

@@ -130,0 +130,0 @@ "browser": {

@@ -14,3 +14,4 @@ /**

clearNodeFolder,
enforceOptions
enforceOptions,
beLeader
} from './index.js';

@@ -22,3 +23,4 @@

clearNodeFolder,
enforceOptions
};
enforceOptions,
beLeader
};

@@ -7,3 +7,4 @@ export {

export {
createLeaderElection
} from './leader-election';
createLeaderElection,
beLeader
} from './leader-election';

@@ -23,2 +23,4 @@ import {

this._invs = []; // _intervals
this._dpL = () => { }; // onduplicate listener
this._dpLC = false; // true when onduplicate called
};

@@ -74,3 +76,3 @@

})
.then(() => _beLeader(this)) // no one disagreed -> this one is now leader
.then(() => beLeader(this)) // no one disagreed -> this one is now leader
.then(() => true)

@@ -99,2 +101,6 @@ .catch(() => false) // apply not successfull

set onduplicate(fn) {
this._dpL = fn;
},
die() {

@@ -119,4 +125,6 @@ if (this.isDead) return;

const finish = () => {
if (resolved) return;
function finish() {
if (resolved) {
return;
}
resolved = true;

@@ -126,7 +134,9 @@ clearInterval(interval);

res(true);
};
}
// try once now
leaderElector.applyOnce().then(() => {
if (leaderElector.isLeader) finish();
if (leaderElector.isLeader) {
finish();
}
});

@@ -137,3 +147,5 @@

leaderElector.applyOnce().then(() => {
if (leaderElector.isLeader) finish();
if (leaderElector.isLeader) {
finish();
}
});

@@ -168,3 +180,3 @@ }, leaderElector._options.fallbackInterval);

function _beLeader(leaderElector) {
export function beLeader(leaderElector) {
leaderElector.isLeader = true;

@@ -178,2 +190,16 @@ const unloadFn = unload.add(() => leaderElector.die());

}
if (msg.context === 'leader' && msg.action === 'tell' && !leaderElector._dpLC) {
/**
* another instance is also leader!
* This can happen on rare events
* like when the CPU is at 100% for long time
* or the tabs are open very long and the browser throttles them.
* @link https://github.com/pubkey/broadcast-channel/issues/414
* @link https://github.com/pubkey/broadcast-channel/issues/385
*/
leaderElector._dpLC = true;
leaderElector._dpL(); // message the lib user so the app can handle the problem
_sendMessage(leaderElector, 'tell'); // ensure other leader also knows the problem
}
};

@@ -212,2 +238,2 @@ leaderElector._channel.addEventListener('internal', isLeaderListener);

return elector;
}
}

@@ -37,3 +37,3 @@ declare type MethodType = 'node' | 'idb' | 'native' | 'localstorage' | 'simulate';

declare type EventType = 'message' | 'internal';
declare type EventContext = 'message' | 'internal' | 'leader';

@@ -59,4 +59,4 @@ declare type OnMessageHandler<T> = ((this: BroadcastChannel, ev: T) => any) | null;

// not defined in the offical standard
addEventListener(type: EventType, handler: OnMessageHandler<T>): void;
removeEventListener(type: EventType, handler: OnMessageHandler<T>): void;
addEventListener(type: EventContext, handler: OnMessageHandler<T>): void;
removeEventListener(type: EventContext, handler: OnMessageHandler<T>): void;

@@ -63,0 +63,0 @@ }

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

import { BroadcastChannel } from './broadcast-channel';
import {
BroadcastChannel,
EventContext,
OnMessageHandler
} from './broadcast-channel';

@@ -16,3 +20,2 @@ export type LeaderElectionOptions = {

export declare class LeaderElector {

@@ -34,3 +37,7 @@

/**
* Add an event handler that is run
* when it is detected that there are duplicate leaders
*/
onduplicate: OnMessageHandler<T>;
}

@@ -40,2 +47,2 @@

export const createLeaderElection: CreateFunction;
export const createLeaderElection: CreateFunction;

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