@wuisdk/plugin-wss 0.1.16 → 0.1.17
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 +13 -8
- package/dist/index.js +2 -2
- package/dist/types-CzCNJdMK.js +1 -0
- package/dist/wss.worker.js +1 -1
- package/package.json +1 -1
- package/dist/ws.types-ILUng1Fh.js +0 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import "@wuisdk/plugin-auth";
|
|
2
2
|
import { WuiContext, WuiPlugin } from "@wuisdk/core";
|
|
3
|
-
//#region src/status.
|
|
3
|
+
//#region src/types/status.d.ts
|
|
4
4
|
export declare const CONN_STATUS_CODES: {
|
|
5
5
|
readonly CONNECTED: "CONNECTED";
|
|
6
6
|
readonly DISCONNECTED: "DISCONNECTED";
|
|
@@ -36,7 +36,7 @@ export type ConnStatusCode = (typeof CONN_STATUS_CODES)[keyof typeof CONN_STATUS
|
|
|
36
36
|
export type AuthErrorCode = (typeof AUTH_ERROR_CODES)[keyof typeof AUTH_ERROR_CODES];
|
|
37
37
|
export type AuthSuccessCode = (typeof AUTH_SUCCESS_CODES)[keyof typeof AUTH_SUCCESS_CODES];
|
|
38
38
|
//#endregion
|
|
39
|
-
//#region src/
|
|
39
|
+
//#region src/types/socket.d.ts
|
|
40
40
|
export interface WSParams {
|
|
41
41
|
url: string;
|
|
42
42
|
app: string;
|
|
@@ -80,15 +80,20 @@ export interface StatusPayload {
|
|
|
80
80
|
status: StatusCode;
|
|
81
81
|
message: string;
|
|
82
82
|
}
|
|
83
|
-
export type AuthAction = 'verify' | 'refresh' | 'expired';
|
|
84
|
-
export interface AuthPayload {
|
|
85
|
-
action: AuthAction;
|
|
86
|
-
token?: string;
|
|
87
|
-
}
|
|
88
83
|
export interface TimePayload {
|
|
89
84
|
time: string;
|
|
90
85
|
unix: number;
|
|
91
86
|
}
|
|
87
|
+
export declare const AuthAction: {
|
|
88
|
+
readonly verify: "verify";
|
|
89
|
+
readonly refresh: "refresh";
|
|
90
|
+
readonly expired: "expired";
|
|
91
|
+
};
|
|
92
|
+
export type AuthAction = (typeof AuthAction)[keyof typeof AuthAction];
|
|
93
|
+
export interface AuthPayload {
|
|
94
|
+
action: AuthAction;
|
|
95
|
+
token?: string;
|
|
96
|
+
}
|
|
92
97
|
export declare const DefaultCmd: {
|
|
93
98
|
readonly CONRST: "CONRST";
|
|
94
99
|
readonly LOGOUT: "LOGOUT";
|
|
@@ -107,7 +112,7 @@ export interface NotifyPayload {
|
|
|
107
112
|
body: string;
|
|
108
113
|
}
|
|
109
114
|
//#endregion
|
|
110
|
-
//#region src/types.d.ts
|
|
115
|
+
//#region src/types/types.d.ts
|
|
111
116
|
declare module '@wuisdk/core' {
|
|
112
117
|
interface WuiPluginMap {
|
|
113
118
|
readonly wss: IWssService;
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{a as e,
|
|
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.17`,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}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,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.config,r=localStorage.getItem(n.AUTHKEY);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};
|
|
@@ -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`,...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};
|
package/dist/wss.worker.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
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;
|
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
const e={CONNECTED:`CONNECTED`,DISCONNECTED:`DISCONNECTED`},t={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`},n={AUTH_LOGIN_SUCCESS:`AUTH_LOGIN_SUCCESS`,AUTH_LOGOUT_SUCCESS:`AUTH_LOGOUT_SUCCESS`,AUTH_TOKEN_VERIFIED:`AUTH_TOKEN_VERIFIED`,AUTH_TOKEN_REFRESHED:`AUTH_TOKEN_REFRESHED`},r={...e,...t,...n},i={Command:`command`,Response:`response`,Notify:`notify`},a={Subscribe:`subscribe`,Unsubscribe:`unsubscribe`,Publish:`publish`,Event:`event`,Ack:`ack`,Error:`error`,Auth:`auth`,Ping:`ping`,Pong:`pong`,Connect:`connect`,...i},o={CONRST:`CONRST`,LOGOUT:`LOGOUT`,CMDRUN:`CMDRUN`,GETLOG:`GETLOG`};export{n as a,t as i,i as n,e as o,a as r,r as s,o as t};
|