@wuisdk/plugin-wss 0.1.21 → 0.1.23

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
@@ -60,6 +60,7 @@ export declare const WSMsgType: {
60
60
  readonly Ping: "ping";
61
61
  readonly Pong: "pong";
62
62
  readonly Connect: "connect";
63
+ readonly Disconnect: "disconnect";
63
64
  };
64
65
  export type WSMsgType = (typeof WSMsgType)[keyof typeof WSMsgType];
65
66
  export type DirectMsgType = (typeof DirectMsgType)[keyof typeof DirectMsgType];
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.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};
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-B98wYG0f.js";import"@wuisdk/plugin-auth";var c=`@wuisdk/plugin-wss`,l=`0.1.23`,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{signal:e}=this.abortController;if(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:e}),this.ctx.plugins.auth){let t=this.ctx.stores.auth;t.getState().isAuth&&this.connect(),t.subscribe(e=>e.isAuth,(e,t)=>{e&&!t?this.connect():!e&&t&&this.disconnect()},{signal:e}),this.ctx.events.on(`auth:refresh`,()=>this.tokenAction(`refresh`),e)}else this.connect()}destroy(){this.abortController.abort(),this.worker?.terminate(),this.worker=null,this.subscribers.clear()}connect(){let{config:e}=this.ctx,n={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.post(n)}disconnect(){let e={type:t.Disconnect};this.post(e)}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 n=this.ctx.plugins.auth.accessToken;n&&this.post({type:t.Auth,data:{action:e,token:n}})}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};
@@ -0,0 +1 @@
1
+ const e={Command:`command`,Response:`response`,Notify:`notify`},t={Subscribe:`subscribe`,Unsubscribe:`unsubscribe`,Publish:`publish`,Event:`event`,Ack:`ack`,Error:`error`,Auth:`auth`,Ping:`ping`,Pong:`pong`,Connect:`connect`,Disconnect:`disconnect`,...e},n={verify:`verify`,refresh:`refresh`,expired:`expired`},r={CONRST:`CONRST`,LOGOUT:`LOGOUT`,CMDRUN:`CMDRUN`,GETLOG:`GETLOG`},i={CONNECTED:`CONNECTED`,DISCONNECTED:`DISCONNECTED`},a={AUTH_REQUIRED:`AUTH_REQUIRED`,AUTH_TOKEN_MISSING:`AUTH_TOKEN_MISSING`,AUTH_TOKEN_EXPIRED:`AUTH_TOKEN_EXPIRED`,AUTH_TOKEN_INVALID:`AUTH_TOKEN_INVALID`,AUTH_TOKEN_REVOKED:`AUTH_TOKEN_REVOKED`},o={AUTH_LOGIN_SUCCESS:`AUTH_LOGIN_SUCCESS`,AUTH_LOGOUT_SUCCESS:`AUTH_LOGOUT_SUCCESS`,AUTH_TOKEN_VERIFIED:`AUTH_TOKEN_VERIFIED`,AUTH_TOKEN_REFRESHED:`AUTH_TOKEN_REFRESHED`},s={...i,...a,...o};export{n as a,t as c,s as i,o as n,r as o,i as r,e as s,a as t};
@@ -1 +1 @@
1
- import{c as e,i as t}from"./types-CzCNJdMK.js";let n=null,r,i;self.onmessage=t=>{let{type:r,data:i}=t.data;r===e.Connect?a(i):n?.readyState===WebSocket.OPEN&&n.send(JSON.stringify(t.data))};const a=l=>{let{url:u,app:d,uid:f,ver:p,rel:m}=l;if(!u||!d||!f||!p||!m){console.error(`[HubWorker] Invalid parameters`);return}let h=`${u}?app=${d}&uid=${f}&ver=${p}&rel=${m}`;n&&(n.onopen=null,n.onclose=null,n.onerror=null,n.onmessage=null,n.close()),n=new WebSocket(h),n.onopen=()=>{},n.onclose=()=>{o({type:e.Ack,data:{status:t.DISCONNECTED}}),self.clearInterval(r),self.clearTimeout(i),i=self.setTimeout(()=>a(l),c())},n.onerror=e=>{console.error(`[HubWorker] WebSocket Error`,e)},n.onmessage=n=>{if(typeof n.data==`string`)try{let r=JSON.parse(n.data),i=r.data;r.type===e.Ack&&i.status===t.CONNECTED&&s(),o(r)}catch(e){console.error(`[HubWorker] Parse Error:`,e)}}},o=e=>self.postMessage(e),s=()=>{self.clearInterval(r),r=self.setInterval(()=>{if(n&&n.readyState===WebSocket.OPEN){let t={type:e.Ping};n.send(JSON.stringify(t))}},5e4)},c=()=>Math.floor(Math.random()*20001)+1e4;
1
+ import{c as e,i as t}from"./types-B98wYG0f.js";let n=null,r,i,a=!1;self.onmessage=t=>{let{type:r,data:i}=t.data;r===e.Connect?o(i):r===e.Disconnect?s():n?.readyState===WebSocket.OPEN&&n.send(JSON.stringify(t.data))};const o=s=>{let{url:f,app:p,uid:m,ver:h,rel:g}=s;if(!f||!p||!m||!h||!g){console.error(`[HubWorker] Invalid parameters`);return}a=!1,c();let _=`${f}?app=${p}&uid=${m}&ver=${h}&rel=${g}`;n=new WebSocket(_),n.onopen=()=>{},n.onclose=()=>{self.clearInterval(r),self.clearTimeout(i),l({type:e.Ack,data:{status:t.DISCONNECTED}}),a||(i=self.setTimeout(()=>o(s),d()))},n.onerror=e=>{console.error(`[HubWorker] WebSocket Error`,e)},n.onmessage=n=>{if(typeof n.data==`string`)try{let r=JSON.parse(n.data),i=r.data;r.type===e.Ack&&i.status===t.CONNECTED&&u(),l(r)}catch(e){console.error(`[HubWorker] Parse Error:`,e)}}},s=()=>{a=!0;let r=n!==null;c(),r&&l({type:e.Ack,data:{status:t.DISCONNECTED}})},c=()=>{self.clearInterval(r),self.clearTimeout(i),r=void 0,i=void 0,n&&=(n.onopen=null,n.onclose=null,n.onerror=null,n.onmessage=null,(n.readyState===WebSocket.OPEN||n.readyState===WebSocket.CONNECTING)&&n.close(),null)},l=e=>self.postMessage(e),u=()=>{self.clearInterval(r),r=self.setInterval(()=>{if(n&&n.readyState===WebSocket.OPEN){let t={type:e.Ping};n.send(JSON.stringify(t))}},5e4)},d=()=>Math.floor(Math.random()*20001)+1e4;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wuisdk/plugin-wss",
3
- "version": "0.1.21",
3
+ "version": "0.1.23",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -1 +0,0 @@
1
- const e={Command:`command`,Response:`response`,Notify:`notify`},t={Subscribe:`subscribe`,Unsubscribe:`unsubscribe`,Publish:`publish`,Event:`event`,Ack:`ack`,Error:`error`,Auth:`auth`,Ping:`ping`,Pong:`pong`,Connect:`connect`,...e},n={verify:`verify`,refresh:`refresh`,expired:`expired`},r={CONRST:`CONRST`,LOGOUT:`LOGOUT`,CMDRUN:`CMDRUN`,GETLOG:`GETLOG`},i={CONNECTED:`CONNECTED`,DISCONNECTED:`DISCONNECTED`},a={AUTH_REQUIRED:`AUTH_REQUIRED`,AUTH_TOKEN_MISSING:`AUTH_TOKEN_MISSING`,AUTH_TOKEN_EXPIRED:`AUTH_TOKEN_EXPIRED`,AUTH_TOKEN_INVALID:`AUTH_TOKEN_INVALID`,AUTH_TOKEN_REVOKED:`AUTH_TOKEN_REVOKED`},o={AUTH_LOGIN_SUCCESS:`AUTH_LOGIN_SUCCESS`,AUTH_LOGOUT_SUCCESS:`AUTH_LOGOUT_SUCCESS`,AUTH_TOKEN_VERIFIED:`AUTH_TOKEN_VERIFIED`,AUTH_TOKEN_REFRESHED:`AUTH_TOKEN_REFRESHED`},s={...i,...a,...o};export{n as a,t as c,s as i,o as n,r as o,i as r,e as s,a as t};