@wuisdk/plugin-wss 0.1.20 → 0.1.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +2 -2
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- import{a as e,c as t,i as n,n as r,o as i,r as a,s as o,t as s}from"./types-CzCNJdMK.js";import"@wuisdk/plugin-auth";var c=`@wuisdk/plugin-wss`,l=`0.1.20`,u=class{options;name=`wui-wss-plugin`;service;constructor(e){this.options=e}install(e){this.service=new d(e,this.options),e.registerPlugin(`wss`,this.service)}init(){this.service.init()}destroy(){this.service.destroy()}},d=class{ctx;options;abortController=new AbortController;worker=null;_wsuuid=null;subscribers=/* @__PURE__ */ new Map;constructor(e,t){this.ctx=e,this.options=t}init(){if(!window.Worker){console.warn(`[WssService] Web Workers not supported.`);return}if(!this.options.worker){console.warn(`[WssService] Missing Worker configuration.`);return}let{config:e}=this.ctx,{signal:n}=this.abortController;this.worker=new this.options.worker,this.worker.addEventListener(`message`,e=>{let{type:n,topic:r,data:i,senderId:a}=e.data;switch(n){case t.Ack:i&&this.handleMsgAck(i);break;case t.Error:i&&this.handleMsgError(i);break;case t.Auth:i&&this.handleMsgAuth(i);break;case t.Command:i&&this.handleMsgCommand(i);break;case t.Notify:i&&this.handleMsgNotify(i);break;case t.Response:i&&console.log(`[WSS-RSP]
2
- `,i);break;case t.Event:if(r&&i){let e=this.subscribers.get(r);e&&e.forEach(e=>e(i,a))}}},{signal:n});let r={type:t.Connect,data:{url:this.options.endpoint??`wss://wkrh.info/wss`,app:e.appName,uid:e.appCode,ver:e.version,rel:e.release}};this.worker.postMessage(r)}destroy(){this.abortController.abort(),this.worker?.terminate(),this.worker=null,this.subscribers.clear()}get wsuuid(){return this._wsuuid??``}post(e){this.worker?this.worker.postMessage(e):console.warn(`[WssService] Worker not initialized`)}direct(e,t,n){this.post({type:e,targetId:t,data:n})}publish(e,n){this.post({type:t.Publish,topic:e,data:n})}subscribe(e,n,r){this.subscribers.has(e)||(this.subscribers.set(e,/* @__PURE__ */ new Set),this._wsuuid&&this.post({type:t.Subscribe,topic:e}));let i=this.subscribers.get(e),a=n;i.add(a);let o=!1,s=()=>{if(o)return;o=!0,r&&r.removeEventListener(`abort`,s);let n=this.subscribers.get(e);n&&(n.delete(a),n.size===0&&(this.subscribers.delete(e),this._wsuuid&&this.post({type:t.Unsubscribe,topic:e})))};return r&&(r.aborted?s():r.addEventListener(`abort`,s,{once:!0})),s}handleMsgAck(e){switch(e.status){case n.CONNECTED:this.wsConnected(e.message);break;case n.DISCONNECTED:this.wsDisconnected();break;default:console.log(`[WSS-ACK]`,`${e.status} : ${e.message}`)}}handleMsgError(e){console.log(`[WSS-ERR]`,`${e.status} : ${e.message}`)}handleMsgAuth(t){switch(t.action){case e.expired:this.tokenExpired();break;default:console.log(`[WSS-AUTH]`,t.action)}}handleMsgCommand(e){switch(e.action){case i.CONRST:sessionStorage.clear(),window.location.reload();break;case i.LOGOUT:this.ctx.plugins.auth&&this.ctx.plugins.auth.logout();break;default:console.log(`[WSS-CMD]`,`${e.action} : ${e.payload}`)}}handleMsgNotify(e){let t=e.payload,n=t.title??`Hub Notify`,r=t.icon??`https://cdn.jsdelivr.net/gh/wkrdev/assets/img/icon/icon-06.png`,i=t.body??``;Notification.requestPermission().then(e=>{e===`granted`&&new Notification(n,{body:i,icon:r})}).catch(e=>console.error(e))}wsConnected(e){this._wsuuid=e,this.ctx.events.emit(`wss:connected`),this.tokenAction(`verify`);for(let e of this.subscribers.keys())this.post({type:t.Subscribe,topic:e})}wsDisconnected(){this._wsuuid=null,this.ctx.events.emit(`wss:disconnected`)}tokenAction(e){let{storage:n}=this.ctx.plugins.auth,r=n.get(`ACCESS_TOKEN`);r&&this.post({type:t.Auth,data:{action:e,token:r}})}async tokenExpired(){if(this.ctx.plugins.auth)try{await this.ctx.plugins.auth.refreshToken(),this.tokenAction(`refresh`)}catch(e){console.warn(`[WssService] Refresh token failed.`,e)}}};const f=()=>({version:l,name:c.split(`/`)[1]});export{s as AUTH_ERROR_CODES,r as AUTH_SUCCESS_CODES,e as AuthAction,a as CONN_STATUS_CODES,i as DefaultCmd,o as DirectMsgType,n as StatusCode,t as WSMsgType,u as WuiWssPlugin,f as info};
1
+ import{a as e,c as t,i as n,n as r,o as i,r as a,s as o,t as s}from"./types-CzCNJdMK.js";import"@wuisdk/plugin-auth";var c=`@wuisdk/plugin-wss`,l=`0.1.21`,u=class{options;name=`wui-wss-plugin`;service;constructor(e){this.options=e}install(e){this.service=new d(e,this.options),e.registerPlugin(`wss`,this.service)}init(){this.service.init()}destroy(){this.service.destroy()}},d=class{ctx;options;abortController=new AbortController;worker=null;_wsuuid=null;subscribers=/* @__PURE__ */ new Map;constructor(e,t){this.ctx=e,this.options=t}init(){if(!window.Worker){console.warn(`[WssService] Web Workers not supported.`);return}if(!this.options.worker){console.warn(`[WssService] Missing Worker configuration.`);return}let{config:e}=this.ctx,{signal:n}=this.abortController;this.worker=new this.options.worker,this.worker.addEventListener(`message`,e=>{let{type:n,topic:r,data:i,senderId:a}=e.data;switch(n){case t.Ack:i&&this.handleMsgAck(i);break;case t.Error:i&&this.handleMsgError(i);break;case t.Auth:i&&this.handleMsgAuth(i);break;case t.Command:i&&this.handleMsgCommand(i);break;case t.Notify:i&&this.handleMsgNotify(i);break;case t.Response:i&&console.log(`[WSS-RSP]
2
+ `,i);break;case t.Event:if(r&&i){let e=this.subscribers.get(r);e&&e.forEach(e=>e(i,a))}}},{signal:n});let r={type:t.Connect,data:{url:this.options.endpoint??`wss://wkrh.info/wss`,app:e.appName,uid:e.appCode,ver:e.version,rel:e.release}};this.worker.postMessage(r)}destroy(){this.abortController.abort(),this.worker?.terminate(),this.worker=null,this.subscribers.clear()}get wsuuid(){return this._wsuuid??``}post(e){this.worker?this.worker.postMessage(e):console.warn(`[WssService] Worker not initialized`)}direct(e,t,n){this.post({type:e,targetId:t,data:n})}publish(e,n){this.post({type:t.Publish,topic:e,data:n})}subscribe(e,n,r){this.subscribers.has(e)||(this.subscribers.set(e,/* @__PURE__ */ new Set),this._wsuuid&&this.post({type:t.Subscribe,topic:e}));let i=this.subscribers.get(e),a=n;i.add(a);let o=!1,s=()=>{if(o)return;o=!0,r&&r.removeEventListener(`abort`,s);let n=this.subscribers.get(e);n&&(n.delete(a),n.size===0&&(this.subscribers.delete(e),this._wsuuid&&this.post({type:t.Unsubscribe,topic:e})))};return r&&(r.aborted?s():r.addEventListener(`abort`,s,{once:!0})),s}handleMsgAck(e){switch(e.status){case n.CONNECTED:this.wsConnected(e.message);break;case n.DISCONNECTED:this.wsDisconnected();break;default:console.log(`[WSS-ACK]`,`${e.status} : ${e.message}`)}}handleMsgError(e){console.log(`[WSS-ERR]`,`${e.status} : ${e.message}`)}handleMsgAuth(t){switch(t.action){case e.expired:this.tokenExpired();break;default:console.log(`[WSS-AUTH]`,t.action)}}handleMsgCommand(e){switch(e.action){case i.CONRST:sessionStorage.clear(),window.location.reload();break;case i.LOGOUT:this.ctx.plugins.auth&&this.ctx.plugins.auth.logout();break;default:console.log(`[WSS-CMD]`,`${e.action} : ${e.payload}`)}}handleMsgNotify(e){let t=e.payload,n=t.title??`Hub Notify`,r=t.icon??`https://cdn.jsdelivr.net/gh/wkrdev/assets/img/icon/icon-06.png`,i=t.body??``;Notification.requestPermission().then(e=>{e===`granted`&&new Notification(n,{body:i,icon:r})}).catch(e=>console.error(e))}wsConnected(e){this._wsuuid=e,this.ctx.events.emit(`wss:connected`),this.tokenAction(`verify`);for(let e of this.subscribers.keys())this.post({type:t.Subscribe,topic:e})}wsDisconnected(){this._wsuuid=null,this.ctx.events.emit(`wss:disconnected`)}tokenAction(e){if(!this.ctx.plugins.auth)return;let{storage:n}=this.ctx.plugins.auth,r=n.get(`ACCESS_TOKEN`);r&&this.post({type:t.Auth,data:{action:e,token:r}})}async tokenExpired(){if(this.ctx.plugins.auth)try{await this.ctx.plugins.auth.refreshToken(),this.tokenAction(`refresh`)}catch(e){console.warn(`[WssService] Refresh token failed.`,e)}}};const f=()=>({version:l,name:c.split(`/`)[1]});export{s as AUTH_ERROR_CODES,r as AUTH_SUCCESS_CODES,e as AuthAction,a as CONN_STATUS_CODES,i as DefaultCmd,o as DirectMsgType,n as StatusCode,t as WSMsgType,u as WuiWssPlugin,f as info};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wuisdk/plugin-wss",
3
- "version": "0.1.20",
3
+ "version": "0.1.21",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {