@wuisdk/plugin-wss 0.1.19 → 0.1.20
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 +0 -10
- package/dist/index.js +2 -2
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import "@wuisdk/plugin-auth";
|
|
2
2
|
import { WuiContext, WuiPlugin } from "@wuisdk/core";
|
|
3
|
-
//#region src/types/status.d.ts
|
|
4
3
|
export declare const CONN_STATUS_CODES: {
|
|
5
4
|
readonly CONNECTED: "CONNECTED";
|
|
6
5
|
readonly DISCONNECTED: "DISCONNECTED";
|
|
@@ -35,8 +34,6 @@ export type StatusCode = (typeof StatusCode)[keyof typeof StatusCode];
|
|
|
35
34
|
export type ConnStatusCode = (typeof CONN_STATUS_CODES)[keyof typeof CONN_STATUS_CODES];
|
|
36
35
|
export type AuthErrorCode = (typeof AUTH_ERROR_CODES)[keyof typeof AUTH_ERROR_CODES];
|
|
37
36
|
export type AuthSuccessCode = (typeof AUTH_SUCCESS_CODES)[keyof typeof AUTH_SUCCESS_CODES];
|
|
38
|
-
//#endregion
|
|
39
|
-
//#region src/types/socket.d.ts
|
|
40
37
|
export interface WSParams {
|
|
41
38
|
url: string;
|
|
42
39
|
app: string;
|
|
@@ -111,8 +108,6 @@ export interface NotifyPayload {
|
|
|
111
108
|
icon?: string;
|
|
112
109
|
body: string;
|
|
113
110
|
}
|
|
114
|
-
//#endregion
|
|
115
|
-
//#region src/types/types.d.ts
|
|
116
111
|
declare module '@wuisdk/core' {
|
|
117
112
|
interface WuiPluginMap {
|
|
118
113
|
readonly wss: IWssService;
|
|
@@ -135,8 +130,6 @@ interface IWssService {
|
|
|
135
130
|
publish(topic: WSTopic, data: unknown): void;
|
|
136
131
|
subscribe<T>(topic: WSTopic, callback: (data: T, senderId?: string) => void, signal?: AbortSignal): () => void;
|
|
137
132
|
}
|
|
138
|
-
//#endregion
|
|
139
|
-
//#region src/service.d.ts
|
|
140
133
|
export declare class WuiWssPlugin implements WuiPlugin {
|
|
141
134
|
private options;
|
|
142
135
|
readonly name = "wui-wss-plugin";
|
|
@@ -146,11 +139,8 @@ export declare class WuiWssPlugin implements WuiPlugin {
|
|
|
146
139
|
init(): void;
|
|
147
140
|
destroy(): void;
|
|
148
141
|
}
|
|
149
|
-
//#endregion
|
|
150
|
-
//#region src/index.d.ts
|
|
151
142
|
export declare const info: () => {
|
|
152
143
|
version: string;
|
|
153
144
|
name: string;
|
|
154
145
|
};
|
|
155
|
-
//#endregion
|
|
156
146
|
export type { IWssService, WssPluginOptions };
|
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.
|
|
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.
|
|
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};
|