@wuisdk/plugin-wss 0.1.7 → 0.1.9

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.
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import "@wuisdk/plugin-auth";
1
2
  import { WuiContext, WuiPlugin } from "@wuisdk/core";
2
3
  //#region src/types.d.ts
3
4
  declare module '@wuisdk/core' {
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- var e=class{options;name=`wui-wss-plugin`;service;constructor(e){this.options=e}install(e){this.service=new t(e,this.options),e.registerPlugin(`wss`,this.service)}init(){this.service.init()}destroy(){this.service.destroy()}},t=class{ctx;options;abortController=new AbortController;worker=null;_wsuuid=null;subscribers=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:t}=this.abortController;this.worker=new this.options.worker,this.worker.addEventListener(`message`,e=>{let{head:t,data:n}=e.data;switch(t){case`ACCEPT`:this.handleAccept(n);break;case`DISCON`:this.handleDisconnect();break;case`BRDMSG`:this.handleBroadcast(n);break;case`VERIFY`:console.log(`🐸`,t,n)}let r=this.subscribers.get(t);r&&r.forEach(e=>e(n))},{signal:t});let n={head:`INIT`,data:{url:this.options.endpoint??`wss://wkrh.info/ws`,app:e.appName,uid:e.appCode,ver:e.version,rel:e.release}};this.worker.postMessage(n)}destroy(){this.abortController.abort(),this.worker?.terminate(),this.worker=null}get wsuuid(){return this._wsuuid??``}post(e){if(!this.worker){console.warn(`[WssService] Worker not initialized`);return}this.worker.postMessage(e)}subscribe(e,t,n){this.subscribers.has(e)||(this.subscribers.set(e,new Set),this.post({head:`SUBSCRIBE`,data:{topic:e}}));let r=this.subscribers.get(e),i=t;r.add(i);let a=!1,o=()=>{if(a)return;a=!0,n&&n.removeEventListener(`abort`,o);let t=this.subscribers.get(e);t&&(t.delete(i),t.size===0&&(this.subscribers.delete(e),this.post({head:`UNSUBSCRIBE`,data:{topic:e}})))};return n&&(n.aborted?o():n.addEventListener(`abort`,o,{once:!0})),o}handleAccept(e){this._wsuuid=e.uuid??``,console.log(`wsuuid:`,this._wsuuid),this.ctx.events.emit(`wss:connected`),this.verifyToken()}handleDisconnect(){this.ctx.events.emit(`wss:disconnected`)}handleBroadcast(e){let t=e.to===this._wsuuid||e.to==`@all`;e.cmd===`LOGS`&&console.log(e.msg),e.cmd===`CONRST`&&t&&(sessionStorage.clear(),window.location.reload()),e.cmd===`LOGOUT`&&t&&(localStorage.clear(),sessionStorage.clear(),window.location.reload()),e.cmd===`RESULT`&&t&&(console.log(e.cmd,`From:`,e.from),console.log(e.msg)),e.cmd===`NOTIFY`&&t&&(this.handleNotify(e.msg),console.log(e.cmd,e.msg))}handleNotify(e){let t=e.title??`Hub Notify`,n=e.icon??`https://cdn.jsdelivr.net/gh/wkrdev/assets/img/icon/icon-06.png`,r=e.body??``;Notification.requestPermission().then(e=>{e==`granted`&&new Notification(t,{body:r,icon:n})}).catch(e=>console.error(e))}verifyToken(){let{storage:e}=this.ctx.config,t=localStorage.getItem(e.AUTH_TOKEN);t&&this.post({head:`VERIFY`,data:t})}};const n=()=>({version:`0.1.7`,name:`@wuisdk/plugin-wss`.split(`/`)[1]});export{e as WuiWssPlugin,n as info};
1
+ import"@wuisdk/plugin-auth";var e=`@wuisdk/plugin-wss`,t=`0.1.9`,n=class{options;name=`wui-wss-plugin`;service;constructor(e){this.options=e}install(e){this.service=new r(e,this.options),e.registerPlugin(`wss`,this.service)}init(){this.service.init()}destroy(){this.service.destroy()}},r=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:t}=this.abortController;this.worker=new this.options.worker,this.worker.addEventListener(`message`,e=>{let{head:t,data:n}=e.data;switch(t){case`ACCEPT`:this.handleAccept(n);break;case`DISCON`:this.handleDisconnect();break;case`BRDMSG`:this.handleBroadcast(n);break;case`VERIFY`:console.log(`🐸`,t,n);break;case`KEYEXP`:this.handleKeyExp()}let r=this.subscribers.get(t);r&&r.forEach(e=>e(n))},{signal:t});let n={head:`INIT`,data:{url:this.options.endpoint??`wss://wkrh.info/wss`,app:e.appName,uid:e.appCode,ver:e.version,rel:e.release}};this.worker.postMessage(n)}destroy(){this.abortController.abort(),this.worker?.terminate(),this.worker=null}get wsuuid(){return this._wsuuid??``}post(e){if(!this.worker){console.warn(`[WssService] Worker not initialized`);return}this.worker.postMessage(e)}subscribe(e,t,n){this.subscribers.has(e)||(this.subscribers.set(e,/* @__PURE__ */ new Set),this.post({head:`SUBSCRIBE`,data:{topic:e}}));let r=this.subscribers.get(e),i=t;r.add(i);let a=!1,o=()=>{if(a)return;a=!0,n&&n.removeEventListener(`abort`,o);let t=this.subscribers.get(e);t&&(t.delete(i),t.size===0&&(this.subscribers.delete(e),this.post({head:`UNSUBSCRIBE`,data:{topic:e}})))};return n&&(n.aborted?o():n.addEventListener(`abort`,o,{once:!0})),o}handleAccept(e){this._wsuuid=e.uuid??``,console.log(`wsuuid:`,this._wsuuid),this.ctx.events.emit(`wss:connected`),this.verifyToken()}handleDisconnect(){this.ctx.events.emit(`wss:disconnected`)}handleBroadcast(e){let t=e.to===this._wsuuid||e.to==`@all`;e.cmd===`LOGS`&&console.log(e.msg),e.cmd===`CONRST`&&t&&(sessionStorage.clear(),window.location.reload()),e.cmd===`LOGOUT`&&t&&(localStorage.clear(),sessionStorage.clear(),window.location.reload()),e.cmd===`RESULT`&&t&&(console.log(e.cmd,`From:`,e.from),console.log(e.msg)),e.cmd===`NOTIFY`&&t&&(this.handleNotify(e.msg),console.log(e.cmd,e.msg))}handleNotify(e){let t=e.title??`Hub Notify`,n=e.icon??`https://cdn.jsdelivr.net/gh/wkrdev/assets/img/icon/icon-06.png`,r=e.body??``;Notification.requestPermission().then(e=>{e==`granted`&&new Notification(t,{body:r,icon:n})}).catch(e=>console.error(e))}async handleKeyExp(){try{await this.ctx.plugins.auth.refreshToken(),this.verifyToken()}catch(e){console.warn(`[WssService] Refresh token failed.`,e)}}verifyToken(){let{storage:e}=this.ctx.config,t=localStorage.getItem(e.AUTHKEY);t&&this.post({head:`VERIFY`,data:t})}};const i=()=>({version:t,name:e.split(`/`)[1]});export{n as WuiWssPlugin,i as info};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wuisdk/plugin-wss",
3
- "version": "0.1.7",
3
+ "version": "0.1.9",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -23,7 +23,8 @@
23
23
  "dev": "tsdown --no-clean --watch"
24
24
  },
25
25
  "dependencies": {
26
- "@wuisdk/core": "*"
26
+ "@wuisdk/core": "*",
27
+ "@wuisdk/plugin-auth": "*"
27
28
  },
28
29
  "author": "hs3lcs@gmail.com",
29
30
  "license": "MIT"