@wuisdk/plugin-wss 0.1.11 → 0.1.12
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 +71 -39
- package/dist/index.js +1 -1
- package/dist/ws.types-9tIaKFzx.js +1 -0
- package/dist/wss.worker.js +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,72 @@
|
|
|
1
1
|
import "@wuisdk/plugin-auth";
|
|
2
2
|
import { WuiContext, WuiPlugin } from "@wuisdk/core";
|
|
3
|
+
//#region src/ws.types.d.ts
|
|
4
|
+
interface WSParams {
|
|
5
|
+
url: string;
|
|
6
|
+
app: string;
|
|
7
|
+
uid: string;
|
|
8
|
+
ver: string;
|
|
9
|
+
rel: string;
|
|
10
|
+
}
|
|
11
|
+
export declare const WSMsgType: {
|
|
12
|
+
readonly Subscribe: "SUBSCRIBE";
|
|
13
|
+
readonly Unsubscribe: "UNSUBSCRIBE";
|
|
14
|
+
readonly Publish: "PUBLISH";
|
|
15
|
+
readonly Direct: "DIRECT";
|
|
16
|
+
readonly Event: "EVENT";
|
|
17
|
+
readonly Ack: "ACK";
|
|
18
|
+
readonly Error: "ERROR";
|
|
19
|
+
readonly Ping: "PING";
|
|
20
|
+
readonly Pong: "PONG";
|
|
21
|
+
readonly Connect: "CONNECT";
|
|
22
|
+
readonly Disconnect: "DISCONNECT";
|
|
23
|
+
readonly Verify: "VERIFY";
|
|
24
|
+
readonly Expired: "EXPIRED";
|
|
25
|
+
};
|
|
26
|
+
export type WSMsgType = (typeof WSMsgType)[keyof typeof WSMsgType];
|
|
27
|
+
type WSTopic = 'server.time' | 'server.logs' | 'server.status' | 'client.online' | 'client.status';
|
|
28
|
+
interface WSMeta {
|
|
29
|
+
timestamp?: number;
|
|
30
|
+
requestId?: string;
|
|
31
|
+
publisherId?: string;
|
|
32
|
+
}
|
|
33
|
+
interface WSTarget {
|
|
34
|
+
type?: string;
|
|
35
|
+
id: string;
|
|
36
|
+
}
|
|
37
|
+
interface WSMessage<T = unknown> {
|
|
38
|
+
type: WSMsgType;
|
|
39
|
+
topic?: string;
|
|
40
|
+
target?: WSTarget;
|
|
41
|
+
data?: T;
|
|
42
|
+
meta?: WSMeta;
|
|
43
|
+
}
|
|
44
|
+
interface AckPayload {
|
|
45
|
+
status: string;
|
|
46
|
+
wsuuid?: string;
|
|
47
|
+
}
|
|
48
|
+
interface ErrorPayload {
|
|
49
|
+
code: string;
|
|
50
|
+
message: string;
|
|
51
|
+
}
|
|
52
|
+
interface TimePayload {
|
|
53
|
+
time: string;
|
|
54
|
+
unix: number;
|
|
55
|
+
}
|
|
56
|
+
interface LogsPayload {
|
|
57
|
+
code: string;
|
|
58
|
+
message: string;
|
|
59
|
+
}
|
|
60
|
+
interface DirectPayload<T = unknown> {
|
|
61
|
+
action: string;
|
|
62
|
+
payload?: T;
|
|
63
|
+
}
|
|
64
|
+
interface NotifyPayload {
|
|
65
|
+
title?: string;
|
|
66
|
+
icon?: string;
|
|
67
|
+
body: string;
|
|
68
|
+
}
|
|
69
|
+
//#endregion
|
|
3
70
|
//#region src/types.d.ts
|
|
4
71
|
declare module '@wuisdk/core' {
|
|
5
72
|
interface WuiPluginMap {
|
|
@@ -18,46 +85,11 @@ interface WssPluginOptions {
|
|
|
18
85
|
}
|
|
19
86
|
interface IWssService {
|
|
20
87
|
readonly wsuuid: string;
|
|
21
|
-
post(packet:
|
|
22
|
-
direct(
|
|
23
|
-
publish(topic: WSTopic,
|
|
88
|
+
post(packet: WSMessage): void;
|
|
89
|
+
direct(targetId: string, data: DirectPayload): void;
|
|
90
|
+
publish(topic: WSTopic, data: unknown): void;
|
|
24
91
|
subscribe<T>(topic: WSTopic, callback: (data: T) => void, signal?: AbortSignal): () => void;
|
|
25
92
|
}
|
|
26
|
-
interface WSParams {
|
|
27
|
-
url: string;
|
|
28
|
-
app: string;
|
|
29
|
-
uid: string;
|
|
30
|
-
ver: string;
|
|
31
|
-
rel: string;
|
|
32
|
-
}
|
|
33
|
-
type WSTopic = 'SRV_TIME' | 'SRV_LOGS' | 'SRV_STATUS' | 'CLT_ONLINE' | 'CLT_STATUS';
|
|
34
|
-
interface WSPacket<T = unknown> {
|
|
35
|
-
head: string;
|
|
36
|
-
data?: T;
|
|
37
|
-
}
|
|
38
|
-
interface WSPubSub<T = unknown> {
|
|
39
|
-
topic?: string;
|
|
40
|
-
sender?: string;
|
|
41
|
-
target?: string;
|
|
42
|
-
payload?: T;
|
|
43
|
-
}
|
|
44
|
-
interface WSTimeData {
|
|
45
|
-
time: string;
|
|
46
|
-
unix: number;
|
|
47
|
-
}
|
|
48
|
-
interface WSLogsData {
|
|
49
|
-
time: string;
|
|
50
|
-
msg: string;
|
|
51
|
-
}
|
|
52
|
-
interface WSCmdPayload {
|
|
53
|
-
sender: string;
|
|
54
|
-
command: string;
|
|
55
|
-
}
|
|
56
|
-
interface WSNotifyData {
|
|
57
|
-
title?: string;
|
|
58
|
-
icon?: string;
|
|
59
|
-
body: string;
|
|
60
|
-
}
|
|
61
93
|
//#endregion
|
|
62
94
|
//#region src/service.d.ts
|
|
63
95
|
export declare class WuiWssPlugin implements WuiPlugin {
|
|
@@ -76,4 +108,4 @@ export declare const info: () => {
|
|
|
76
108
|
name: string;
|
|
77
109
|
};
|
|
78
110
|
//#endregion
|
|
79
|
-
export type { IWssService,
|
|
111
|
+
export type { AckPayload, DirectPayload, ErrorPayload, IWssService, LogsPayload, NotifyPayload, TimePayload, WSMessage, WSMeta, WSParams, WSTarget, WSTopic, WssPluginOptions };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import"@wuisdk/plugin-auth";var
|
|
1
|
+
import{t as e}from"./ws.types-9tIaKFzx.js";import"@wuisdk/plugin-auth";var t=`@wuisdk/plugin-wss`,n=`0.1.12`,r=class{options;name=`wui-wss-plugin`;service;constructor(e){this.options=e}install(e){this.service=new i(e,this.options),e.registerPlugin(`wss`,this.service)}init(){this.service.init()}destroy(){this.service.destroy()}},i=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:t}=this.ctx,{signal:n}=this.abortController;this.worker=new this.options.worker,this.worker.addEventListener(`message`,t=>{let{type:n,topic:r,data:i}=t.data;switch(n){case e.Ack:i&&this.handleAckMessage(i);break;case e.Error:i&&this.handleErrorMessage(i);break;case e.Direct:i&&this.handleDirect(i);break;case e.Disconnect:this.handleDisconnect();break;case e.Expired:this.handleExpired()}if(n===e.Event&&r&&i){let e=this.subscribers.get(r);e&&e.forEach(e=>e(i))}},{signal:n});let r={type:e.Connect,data:{url:this.options.endpoint??`wss://wkrh.info/wss`,app:t.appName,uid:t.appCode,ver:t.version,rel:t.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(t,n){this.post({type:e.Direct,target:{id:t},data:n})}publish(t,n){this.post({type:e.Publish,topic:t,data:n})}subscribe(t,n,r){this.subscribers.has(t)||(this.subscribers.set(t,/* @__PURE__ */ new Set),this._wsuuid&&this.post({type:e.Subscribe,topic:t}));let i=this.subscribers.get(t),a=n;i.add(a);let o=!1,s=()=>{if(o)return;o=!0,r&&r.removeEventListener(`abort`,s);let n=this.subscribers.get(t);n&&(n.delete(a),n.size===0&&(this.subscribers.delete(t),this._wsuuid&&this.post({type:e.Unsubscribe,topic:t})))};return r&&(r.aborted?s():r.addEventListener(`abort`,s,{once:!0})),s}handleAckMessage(e){e.status===`connected`&&e.wsuuid&&this.handleConnected(e.wsuuid),e.status===`verified`&&console.log(`[WSS]`,`🐸Token verified successfully.`)}handleErrorMessage(e){console.log(`[WSS]`,`💥${e.code}: ${e.message}`)}handleConnected(t){this._wsuuid=t,console.log(`wsuuid:`,this._wsuuid),this.ctx.events.emit(`wss:connected`),this.verifyToken();for(let t of this.subscribers.keys())this.post({type:e.Subscribe,topic:t})}handleDisconnect(){this._wsuuid=null,this.ctx.events.emit(`wss:disconnected`)}verifyToken(){let{storage:t}=this.ctx.config,n=localStorage.getItem(t.AUTHKEY);n&&this.post({type:e.Verify,data:{token:n}})}async handleExpired(){if(this.ctx.plugins.auth)try{await this.ctx.plugins.auth.refreshToken(),this.verifyToken()}catch(e){console.warn(`[WssService] Refresh token failed.`,e)}}handleDirect(e){switch(e.action){case`CONRST`:sessionStorage.clear(),window.location.reload();break;case`LOGOUT`:localStorage.clear(),sessionStorage.clear(),window.location.reload();break;case`NOTIFY`:this.handleNotify(e.payload)}}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))}};const a=()=>({version:n,name:t.split(`/`)[1]});export{e as WSMsgType,r as WuiWssPlugin,a as info};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const e={Subscribe:`SUBSCRIBE`,Unsubscribe:`UNSUBSCRIBE`,Publish:`PUBLISH`,Direct:`DIRECT`,Event:`EVENT`,Ack:`ACK`,Error:`ERROR`,Ping:`PING`,Pong:`PONG`,Connect:`CONNECT`,Disconnect:`DISCONNECT`,Verify:`VERIFY`,Expired:`EXPIRED`};export{e as t};
|
package/dist/wss.worker.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
let
|
|
1
|
+
import{t as e}from"./ws.types-9tIaKFzx.js";let t=null,n,r;self.onmessage=n=>{let{type:r,data:a}=n.data;r===e.Connect?i(a):t?.readyState===WebSocket.OPEN&&t.send(JSON.stringify(n.data))};const i=c=>{let{url:l,app:u,uid:d,ver:f,rel:p}=c;if(!l||!u||!d||!f||!p){console.error(`[HubWorker] Invalid parameters`);return}let m=`${l}?app=${u}&uid=${d}&ver=${f}&rel=${p}`;t&&(t.onopen=null,t.onclose=null,t.onerror=null,t.onmessage=null,t.close()),t=new WebSocket(m),t.onopen=()=>{a({type:e.Connect})},t.onclose=()=>{a({type:e.Disconnect}),clearInterval(n),clearTimeout(r),r=self.setTimeout(()=>i(c),s())},t.onerror=e=>{console.error(`[HubWorker] WebSocket Error`,e)},t.onmessage=t=>{if(typeof t.data==`string`)try{let n=JSON.parse(t.data),r=n.data;n.type===e.Ack&&r.status===`connected`&&o(),a(n)}catch(e){console.error(`[HubWorker] Parse Error:`,e)}}},a=e=>self.postMessage(e),o=()=>{clearInterval(n),n=setInterval(()=>{if(t&&t.readyState===WebSocket.OPEN){let n={type:e.Ping};t.send(JSON.stringify(n))}},5e4)},s=()=>Math.floor(Math.random()*20001)+1e4;
|