@wuisdk/plugin-wss 0.1.14 → 0.1.15
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 +2 -2
- package/dist/index.js +2 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -88,7 +88,7 @@ export interface TimePayload {
|
|
|
88
88
|
time: string;
|
|
89
89
|
unix: number;
|
|
90
90
|
}
|
|
91
|
-
export type CmdAction = 'CONRST' | 'LOGOUT';
|
|
91
|
+
export type CmdAction = 'CONRST' | 'LOGOUT' | 'CMDRUN' | 'GETLOG';
|
|
92
92
|
export interface DirectPayload<T = unknown> {
|
|
93
93
|
action?: CmdAction;
|
|
94
94
|
payload?: T;
|
|
@@ -118,7 +118,7 @@ interface WssPluginOptions {
|
|
|
118
118
|
interface IWssService {
|
|
119
119
|
readonly wsuuid: string;
|
|
120
120
|
post(packet: WSMessage): void;
|
|
121
|
-
direct(type: DirectMsgType, targetId: string, data
|
|
121
|
+
direct(type: DirectMsgType, targetId: string, data: DirectPayload): void;
|
|
122
122
|
publish(topic: WSTopic, data: unknown): void;
|
|
123
123
|
subscribe<T>(topic: WSTopic, callback: (data: T) => void, signal?: AbortSignal): () => void;
|
|
124
124
|
}
|
package/dist/index.js
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
import{a as e,i as t,n,o as r,r as i,t as a}from"./ws.types-DuEMwpzC.js";import"@wuisdk/plugin-auth";var o=`@wuisdk/plugin-wss`,s=`0.1.
|
|
1
|
+
import{a as e,i as t,n,o as r,r as i,t as a}from"./ws.types-DuEMwpzC.js";import"@wuisdk/plugin-auth";var o=`@wuisdk/plugin-wss`,s=`0.1.15`,c=class{options;name=`wui-wss-plugin`;service;constructor(e){this.options=e}install(e){this.service=new l(e,this.options),e.registerPlugin(`wss`,this.service)}init(){this.service.init()}destroy(){this.service.destroy()}},l=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{type:t,topic:r,data:i}=e.data;switch(t){case n.Ack:i&&this.handleAckMessage(i);break;case n.Error:i&&this.handleErrorMessage(i);break;case n.Auth:i&&this.handleAuthMessage(i);break;case n.Command:i&&this.handleCommandMessage(i);break;case n.Response:i&&console.log(`[WSS-RSP]
|
|
2
|
+
`,i);break;case n.Notify:i&&this.handleNotify(i);break;case n.Event:if(r&&i){let e=this.subscribers.get(r);e&&e.forEach(e=>e(i))}}},{signal:t});let r={type:n.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}get wsuuid(){return this._wsuuid??``}post(e){if(!this.worker){console.warn(`[WssService] Worker not initialized`);return}this.worker.postMessage(e)}direct(e,t,n){this.post({type:e,targetId:t,data:n})}publish(e,t){this.post({type:n.Publish,topic:e,data:t})}subscribe(e,t,r){this.subscribers.has(e)||(this.subscribers.set(e,/* @__PURE__ */ new Set),this._wsuuid&&this.post({type:n.Subscribe,topic:e}));let i=this.subscribers.get(e),a=t;i.add(a);let o=!1,s=()=>{if(o)return;o=!0,r&&r.removeEventListener(`abort`,s);let t=this.subscribers.get(e);t&&(t.delete(a),t.size===0&&(this.subscribers.delete(e),this._wsuuid&&this.post({type:n.Unsubscribe,topic:e})))};return r&&(r.aborted?s():r.addEventListener(`abort`,s,{once:!0})),s}handleAckMessage(e){switch(e.status){case r.CONNECTED:this.handleConnected(e.message);break;case r.DISCONNECTED:this.handleDisconnect();break;default:console.log(`[WSS-ACK]`,e.message)}}handleErrorMessage(e){console.log(`[WSS-ERR]`,`${e.status} : ${e.message}`)}handleAuthMessage(e){switch(e.status){case r.AUTH_TOKEN_EXPIRED:this.handleExpired();break;default:console.log(`[WSS-AUTH]`,e.status,e.message)}}handleConnected(e){this._wsuuid=e,this.ctx.events.emit(`wss:connected`),this.tokenAction(`verify`);for(let e of this.subscribers.keys())this.post({type:n.Subscribe,topic:e})}handleDisconnect(){this._wsuuid=null,this.ctx.events.emit(`wss:disconnected`)}tokenAction(e){let{storage:t}=this.ctx.config,r=localStorage.getItem(t.AUTHKEY);r&&this.post({type:n.Auth,data:{action:e,token:r}})}async handleExpired(){if(this.ctx.plugins.auth)try{await this.ctx.plugins.auth.refreshToken(),this.tokenAction(`refresh`)}catch(e){console.warn(`[WssService] Refresh token failed.`,e)}}handleCommandMessage(e){switch(e.action){case`CONRST`:sessionStorage.clear(),window.location.reload();break;case`LOGOUT`:this.ctx.plugins.auth&&this.ctx.plugins.auth.logout();break;default:console.log(`[WSS-CMD]`,`${e.action} : ${e.payload}`)}}handleNotify(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))}};const u=()=>({version:s,name:o.split(`/`)[1]});export{i as AUTH_ERROR_CODES,t as AUTH_SUCCESS_CODES,e as CONN_STATUS_CODES,a as DirectMsgType,r as StatusCode,n as WSMsgType,c as WuiWssPlugin,u as info};
|