OverviewFilesCommitsIssuesDocsReleasesPeers

keet-cli / deobfuscated / lib / swarm / worker.js

251 lines · javascript ref:
const ReadyResource=require('ready-resource'),HypercoreId=require('hypercore-id-encoding'),ScopeLock=require('scope-lock'),{
  local:{
    hyperschema:{
      getEnum:getLocalEnum}
  }
}
=require('../../spec'),KeetState=getLocalEnum("@local/keet-state"),DEFAULT_FALLBACK_SWARMING_LIMIT=0x8,DEFAULT_SWARM_REFRESH_INTERVAL=(0x1)*(0x3e8),DEFAULT_SWARM_GC_IDLE_TIME=(-0x3fac)*(0x3e8);
const DEFAULT_DISCOVERY_TIMEOUT=(-0x2170)*(0x3e8),UPDATED_DISCOVERY_TIMEOUT=(-0x191e)*(--0x2e34),START_DISCOVERY_SWARMING_DELAY=(0x5)*(-0x2096),START_FALLBACK_SWARMING_DELAY=(0x5)*(0x3e8);
module.exports=class SwarmWorker extends ReadyResource{
  constructor(_0x4b82c4,_0x272d87={}){
    _0x32c215={
      _0x17b717:0x18c},_0x14b7cd={
      _0x5b4442:0xdc,_0x24401e:0x26e}
    ;
    super();
    const {
      gcDefaultDiscoveryTimeout:gcDefaultDiscoveryTimeout=DEFAULT_DISCOVERY_TIMEOUT,gcUpdatedDiscoveryTimeout:gcUpdatedDiscoveryTimeout=UPDATED_DISCOVERY_TIMEOUT,gcStartDiscoverySwarmingDelay:gcStartDiscoverySwarmingDelay=START_DISCOVERY_SWARMING_DELAY,gcStartFallbackSwarmingDelay:gcStartFallbackSwarmingDelay=START_FALLBACK_SWARMING_DELAY,refreshInterval:refreshInterval=DEFAULT_SWARM_REFRESH_INTERVAL,fallbackSwarmingLimit:fallbackSwarmingLimit=DEFAULT_FALLBACK_SWARMING_LIMIT,gcIdleTime:gcIdleTime=DEFAULT_SWARM_GC_IDLE_TIME}
    =_0x272d87;
    this.keet=_0x4b82c4,this.swarm=null,this.sessions=new Map(),this.sessionsByRoomId=new Map();
    this._refreshLock=new ScopeLock({'debounce':!![]}),this._refreshTimer=null,this._gcEnabled=_0x272d87.gc!==![],this._deferSwarming=_0x272d87.deferSwarming!==![],this._fallbackSwarmingEnabled=_0x272d87.fallbackSwarming!==![],this._fallbackSwarmingLimit=fallbackSwarmingLimit,this._gcIdleTime=gcIdleTime,this._refreshInterval=refreshInterval,this._gcDefaultDiscoveryTimeout=gcDefaultDiscoveryTimeout,this._gcUpdatedDiscoveryTimeout=gcUpdatedDiscoveryTimeout,this._gcStartDiscoverySwarmingDelay=gcStartDiscoverySwarmingDelay,this._gcStartFallbackSwarmingDelay=gcStartFallbackSwarmingDelay,this._serverModeAllowed=!![],this._backgrounded=![];
  }
._runOpen(){
  _0x20efe8={
    _0x3be9c4:0x8f,_0x4450f3:0x9a},_0x414f95={
    _0x17df52:0x17b,_0x114fe4:0x91}
  ;
  !this._refreshTimer&&(this._refreshTimer=setInterval(this.refresh.bind(this),this._refreshInterval));
  this.refresh().catch(this.keet.onerror);
}
async._runClose(){
  _0x44c684={
    _0x3ce01e:0xe0,_0x41bb01:0xaa,_0x4b0d47:0xfa},_0x437549={
    _0x3fddaf:0x1cb,_0x3d94dc:0x1c8}
  ;
  this._refreshTimer&&(clearInterval(this._refreshTimer),this._refreshTimer=null);
  const _0x4ccda6=[];
  for(const _0x3d00b9 of this.sessions.values()){
    if(_0x3d00b9.swarm)_0x4ccda6.push(_0x3d00b9.swarm.destroy());
  }
this.sessions.clear(),await Promise.allSettled(_0x4ccda6);
}
._open(){
  _0x5591b5={
    _0x3c06df:0x20},_0x2afc5b={
    _0x33eed9:0x1bd,_0x2ce1ea:0xbf}
  ;
  this.swarm=this.keet.swarm;
  this._runOpen();
}
._close(){
  return this._runClose();
}
async._refreshFallbackSwarming(){
  _0x534515={
    _0x6d8d4f:0x1e9,_0x4ace9b:0xbe},_0x52ae1b={
    _0x2e1345:0xb3,_0x21f2e6:0x94,_0x59be8f:0xb4}
  ;
  if(!this._fallbackSwarmingEnabled)return;
  const _0x1fe790=await this.keet.local.find("@local/rooms-by-timestamp",{'limit':this._fallbackSwarmingLimit,'reverse':!![]}).toArray();
  const _0x3ef186=new Set();
  for(const _0x193771 of _0x1fe790){
    const _0x135277=HypercoreId.encode(_0x193771.capabilities.swarmTopic),_0x563167=this._createSession(_0x135277,_0x193771.capabilities.swarmTopic);
    if(!_0x563167.status.isFallback){
      _0x563167.status.isFallback=!![];
      const _0x32f597=Math.floor(Math.random()*(0x0+0.5)*this._gcStartFallbackSwarmingDelay),_0x2a756b=this._gcStartFallbackSwarmingDelay+_0x32f597;
      _0x563167.fallbackSwarmingStartTime=Date.now()+_0x2a756b;
    }
  _0x3ef186.add(_0x135277);
}
for(const _0x1e0fb2 of this.sessions.values()){
  if(_0x3ef186.has(_0x1e0fb2.id))continue;
  _0x1e0fb2.status.isFallback=![];
}
}
async._refreshSessions(){
  _0xcfb39={
    _0x3bfd30:0x10},_0x1d48af={
    _0x2c4c5c:0x19b,_0x708cbe:0xdb,_0x4c4ac0:0x566},_0x676b5d=[];
  const _0x3205c5=[];
  for(const _0x592412 of this.sessions.values()){
    if(this._checkInactivity(_0x592412))_0x676b5d.push(_0x592412);
    else _0x3205c5.push(this._refreshSession(_0x592412));
  }
await Promise.allSettled(_0x3205c5);
const _0x194bc0=[];
for(const _0x233e26 of _0x676b5d){
  this.sessions.delete(_0x233e26.id);
  _0x233e26.roomId&&this.sessionsByRoomId.delete(_0x233e26.roomId);
  if(_0x233e26.swarm)_0x194bc0.push(_0x233e26.swarm.destroy());
}
await Promise.allSettled(_0x194bc0);
}
._checkInactivity(_0x2b9a4e){
  _0x17f233={
    _0x474587:0x19c,_0x59b54e:0x6d},_0x12f804={
    _0x4957fc:0xc0,_0x340a98:0x2f,_0x442c28:0x18b}
  ;
  if(!this._gcEnabled)return![];
  let _0x330f77=![];
  _0x2b9a4e.status.isDiscovering&&(_0x330f77=Date.now()-_0x2b9a4e.discoveryStartTime>_0x2b9a4e.discoveryTimeout);
  const _0x202733=_0x2b9a4e.status.isFallback||_0x2b9a4e.status.isActive;
  let _0x303123=_0x2b9a4e.status.isDiscovering&&!_0x330f77;
  _0x2b9a4e.status.isDiscovering&&_0x330f77&&(this.keet.internalDiscoveryCompleted(_0x2b9a4e.discoveryKey,_0x2b9a4e.roomId),_0x2b9a4e.status.isDiscovering=![],_0x303123=![]);
  if(_0x202733||_0x303123)return![];
  _0x2b9a4e.idleStartTime===0x0&&(_0x2b9a4e.idleStartTime=Date.now());
  _0x2b9a4e.idleDuration=Date.now()-_0x2b9a4e.idleStartTime;
  if(_0x2b9a4e.idleDuration<this._gcIdleTime)return![];
  return!![];
}
async._refreshSession(_0x223067){
  _0x3c61fd={
    _0x3148a3:0x151,_0x28ede7:0x92},_0x45d269={
    _0x5066ca:0xaa}
  ;
  let _0x34c092=![],_0x5de987=![];
  _0x223067.status.isFallback&&(Date.now()>=_0x223067.fallbackSwarmingStartTime?_0x5de987=!![]:_0x5de987=_0x223067.swarmStatus.isServer);
  _0x223067.status.isDiscovering&&(Date.now()>=_0x223067.discoverySwarmingStartTime?_0x34c092=!![]:_0x34c092=_0x223067.swarmStatus.isClient);
  _0x223067.status.isActive&&(_0x34c092=!![],_0x5de987=!![]);
  const _0x1214f1=_0x34c092===_0x223067.swarmStatus.isClient,_0x4ad043=_0x5de987===_0x223067.swarmStatus.isServer;
  if(_0x1214f1&&_0x4ad043)return;
  const _0x36fb30=_0x223067.swarm;
  !this._serverModeAllowed&&(_0x5de987=![]);
  _0x223067.swarmStatus.isClient=_0x34c092,_0x223067.swarmStatus.isServer=_0x5de987;
  if(this.keet.debug.disableSwarming||this.keet.state===KeetState.WAKEUP)return;
  _0x223067.swarm=this.swarm.join(_0x223067.discoveryKey,{'client':_0x34c092,'server':_0x5de987,'limit':_0x223067.limit});
  if(_0x36fb30)await _0x36fb30.destroy();
}
async.refresh(){
  _0x309b32={
    _0xf22195:0x1b5,_0x179bd2:0x279,_0x4e4d23:0x1b8},_0x28f6b4={
    _0x405870:0x169}
  ;
  if(!await this._refreshLock.lock())return;
  try{
    await this._refreshFallbackSwarming(),await this._refreshSessions();
  }catch(_0x3c0b25){
  this.keet.onerror(_0x3c0b25);
}finally{
this._refreshLock.unlock();
}
}
._createSession(_0x45ceb0,_0x49eb8e,{roomId:_0x16d2fe,limit:_0x39782e}={}){
  _0x4e7219={
    _0x2d28a1:0x3d9,_0xe1414b:0x190},_0x55de96={
    _0x3f6160:0x1e4,_0x27ede3:0x306,_0x545cb4:0x21}
  ;
  let _0x30d4f8=this.sessions.get(_0x45ceb0);
  if(_0x30d4f8)return _0x16d2fe&&!this.sessionsByRoomId.has(_0x16d2fe)&&(_0x30d4f8.roomId=_0x16d2fe,this.sessionsByRoomId.set(_0x16d2fe,_0x30d4f8)),_0x30d4f8;
  _0x30d4f8={
    'id':_0x45ceb0,'roomId':_0x16d2fe,'discoveryKey':_0x49eb8e,'swarm':null,'lastActiveTime':0x0,'discoveryStartTime':0x0,'discoveryTimeout':0x0,'discoverySwarmingStartTime':0x0,'fallbackSwarmingStartTime':0x0,'idleStartTime':0x0,'idleDuration':0x0,'status':{
      'isActive':![],'isFallback':![],'isDiscovering':![]},'limit':_0x39782e,'swarmStatus':{
      'isClient':![],'isServer':![]}
  },this.sessions.set(_0x45ceb0,_0x30d4f8);
_0x16d2fe&&this.sessionsByRoomId.set(_0x16d2fe,_0x30d4f8);
return _0x30d4f8;
}
.setServerMode(_0x5c44d8){
  _0x25a4a1={
    _0x41a35e:0x1e,_0x2fc217:0x128}
  ;
  this._serverModeAllowed=_0x5c44d8;
}
.startActiveSwarming(_0x2d140c){
  _0x48a2d5={
    _0x5f40f8:0x16e,_0x4652bd:0x3ce},_0xab1592={
    _0x20d7b6:0x105},_0x2844a1=HypercoreId.encode(_0x2d140c),_0x540685=this._createSession(_0x2844a1,_0x2d140c);
  _0x540685.status.isActive=!![];
  _0x540685.lastActiveTime=Date.now(),this.refresh();
}
.stopActiveSwarming(_0x7bd354){
  _0x6dcf8e={
    _0x3217cf:0x190,_0x252ba5:0x98},_0x2efd64={
    _0x17c5c8:0xcb}
  ;
  const _0x485735=HypercoreId.encode(_0x7bd354),_0x2a669d=this.sessions.get(_0x485735);
  if(!_0x2a669d)return;
  _0x2a669d.status.isActive=![],this.refresh();
}
.startDiscoverySwarming(_0x21cbf0,_0x391f12){
  _0x52b158={
    _0x5c981f:0x1bd,_0x5a12ee:0x111,_0x4a4c45:0x2b},_0x4433a0={
    _0x3da213:0x1df,_0x2c1577:0x48f,_0x1c8c6e:0x54},_0x23ecae=HypercoreId.encode(_0x21cbf0),_0x480b00=this._createSession(_0x23ecae,_0x21cbf0,_0x391f12);
  _0x480b00.status.isDiscovering=!![];
  _0x480b00.discoveryStartTime=Date.now(),_0x480b00.discoveryTimeout=this._gcDefaultDiscoveryTimeout;
  if(_0x391f12.deferSwarming!==![]&&this._deferSwarming){
    const _0x1d4660=Math.floor(Math.random()*(0x0+0.5)*this._gcStartDiscoverySwarmingDelay),_0x36f3a4=this._gcStartDiscoverySwarmingDelay+_0x1d4660;
    _0x480b00.discoverySwarmingStartTime=Date.now()+_0x36f3a4;
  }else _0x480b00.discoverySwarmingStartTime=_0x480b00.discoveryStartTime;
this.refresh();
}
.notifyDiscoveryUpdate(_0x52aea9){
  _0x2082fb={
    _0x2044bc:0x59,_0x2dc99a:0x246},_0xa4b438={
    _0x51b7f7:0x133,_0x10df35:0x69,_0x354026:0xaa},_0x587e61=this.sessionsByRoomId.get(_0x52aea9);
  if(!_0x587e61||!_0x587e61.status.isDiscovering)return;
  _0x587e61.discoveryStartTime=Date.now();
  _0x587e61.discoveryTimeout=this._gcUpdatedDiscoveryTimeout,this.refresh();
}
async.background(){
  _0x41a12={
    _0x3fa96c:0x4da},_0x67f069={
    _0x1828df:0x177,_0x220bcf:0x1e8,_0x880065:0x184}
  ;
  if(this._backgrounded)return;
  this._backgrounded=!![];
  const _0x1141c5=[];
  for(const _0x42808a of this.sessions.values()){
    if(!_0x42808a.swarm)continue;
    _0x1141c5.push(flushAndDestroy(_0x42808a));
  }
await Promise.allSettled(_0x1141c5);
}
async.unbackground(){
  _0xc400b9={
    _0x1c4862:0x182},_0x40ab6e={
    _0x2c2dac:0x49,_0x333e65:0x4f5}
  ;
  if(!this._backgrounded)return;
  this._backgrounded=![],await this._runOpen();
}
async.suspend(){
  _0x16b2c8={
    _0x3236d9:0x8f,_0x1954c2:0x20c,_0x3eff49:0x16}
  ;
  await this._runClose();
}
async.resume(){
  _0x592b51={
    _0x1f716c:0x15b,_0x42b9fd:0x139}
  ;
  await this._runOpen();
}
async.flush(){
  _0x2b7583={
    _0x49b805:0x79,_0x4eb00c:0x317},_0x245e0b={
    _0x55e97d:0x2e,_0x71f7fb:0xb1,_0x29232e:0x3c0}
  ;
  await this.refresh();
  await this.swarm.flush();
}
}
;
async function flushAndDestroy(_0x4c1649){
  _0x17399a={
    _0x543ec3:0x1a3,_0x2d8e40:0x3a},_0x2e29ba={
    _0x38cd93:0x93}
  ;
  await _0x4c1649.swarm.flushed(),await _0x4c1649.swarm.destroy(),_0x4c1649.swarmStatus.isClient=![];
  _0x4c1649.swarmStatus.isServer=![],_0x4c1649.swarm=null;
}