@wuisdk/plugin-wss 0.1.13 → 0.1.14
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 +76 -44
- package/dist/index.js +1 -1
- package/dist/ws.types-DuEMwpzC.js +1 -0
- package/dist/wss.worker.js +1 -1
- package/package.json +1 -1
- package/dist/ws.types-9tIaKFzx.js +0 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,67 +1,99 @@
|
|
|
1
1
|
import "@wuisdk/plugin-auth";
|
|
2
2
|
import { WuiContext, WuiPlugin } from "@wuisdk/core";
|
|
3
|
+
//#region src/status.code.d.ts
|
|
4
|
+
export declare const CONN_STATUS_CODES: {
|
|
5
|
+
readonly CONNECTED: "CONNECTED";
|
|
6
|
+
readonly DISCONNECTED: "DISCONNECTED";
|
|
7
|
+
};
|
|
8
|
+
export declare const AUTH_ERROR_CODES: {
|
|
9
|
+
readonly AUTH_REQUIRED: "AUTH_REQUIRED";
|
|
10
|
+
readonly AUTH_TOKEN_MISSING: "AUTH_TOKEN_MISSING";
|
|
11
|
+
readonly AUTH_TOKEN_EXPIRED: "AUTH_TOKEN_EXPIRED";
|
|
12
|
+
readonly AUTH_TOKEN_INVALID: "AUTH_TOKEN_INVALID";
|
|
13
|
+
readonly AUTH_TOKEN_REVOKED: "AUTH_TOKEN_REVOKED";
|
|
14
|
+
};
|
|
15
|
+
export declare const AUTH_SUCCESS_CODES: {
|
|
16
|
+
readonly AUTH_LOGIN_SUCCESS: "AUTH_LOGIN_SUCCESS";
|
|
17
|
+
readonly AUTH_LOGOUT_SUCCESS: "AUTH_LOGOUT_SUCCESS";
|
|
18
|
+
readonly AUTH_TOKEN_VERIFIED: "AUTH_TOKEN_VERIFIED";
|
|
19
|
+
readonly AUTH_TOKEN_REFRESHED: "AUTH_TOKEN_REFRESHED";
|
|
20
|
+
};
|
|
21
|
+
export declare const StatusCode: {
|
|
22
|
+
readonly AUTH_LOGIN_SUCCESS: "AUTH_LOGIN_SUCCESS";
|
|
23
|
+
readonly AUTH_LOGOUT_SUCCESS: "AUTH_LOGOUT_SUCCESS";
|
|
24
|
+
readonly AUTH_TOKEN_VERIFIED: "AUTH_TOKEN_VERIFIED";
|
|
25
|
+
readonly AUTH_TOKEN_REFRESHED: "AUTH_TOKEN_REFRESHED";
|
|
26
|
+
readonly AUTH_REQUIRED: "AUTH_REQUIRED";
|
|
27
|
+
readonly AUTH_TOKEN_MISSING: "AUTH_TOKEN_MISSING";
|
|
28
|
+
readonly AUTH_TOKEN_EXPIRED: "AUTH_TOKEN_EXPIRED";
|
|
29
|
+
readonly AUTH_TOKEN_INVALID: "AUTH_TOKEN_INVALID";
|
|
30
|
+
readonly AUTH_TOKEN_REVOKED: "AUTH_TOKEN_REVOKED";
|
|
31
|
+
readonly CONNECTED: "CONNECTED";
|
|
32
|
+
readonly DISCONNECTED: "DISCONNECTED";
|
|
33
|
+
};
|
|
34
|
+
export type StatusCode = (typeof StatusCode)[keyof typeof StatusCode];
|
|
35
|
+
export type ConnStatusCode = (typeof CONN_STATUS_CODES)[keyof typeof CONN_STATUS_CODES];
|
|
36
|
+
export type AuthErrorCode = (typeof AUTH_ERROR_CODES)[keyof typeof AUTH_ERROR_CODES];
|
|
37
|
+
export type AuthSuccessCode = (typeof AUTH_SUCCESS_CODES)[keyof typeof AUTH_SUCCESS_CODES];
|
|
38
|
+
//#endregion
|
|
3
39
|
//#region src/ws.types.d.ts
|
|
4
|
-
interface WSParams {
|
|
40
|
+
export interface WSParams {
|
|
5
41
|
url: string;
|
|
6
42
|
app: string;
|
|
7
43
|
uid: string;
|
|
8
44
|
ver: string;
|
|
9
45
|
rel: string;
|
|
10
46
|
}
|
|
47
|
+
export declare const DirectMsgType: {
|
|
48
|
+
readonly Command: "command";
|
|
49
|
+
readonly Response: "response";
|
|
50
|
+
readonly Notify: "notify";
|
|
51
|
+
};
|
|
11
52
|
export declare const WSMsgType: {
|
|
12
|
-
readonly
|
|
13
|
-
readonly
|
|
14
|
-
readonly
|
|
15
|
-
readonly
|
|
16
|
-
readonly
|
|
17
|
-
readonly
|
|
18
|
-
readonly
|
|
19
|
-
readonly
|
|
20
|
-
readonly
|
|
21
|
-
readonly
|
|
22
|
-
readonly
|
|
23
|
-
readonly
|
|
24
|
-
readonly
|
|
53
|
+
readonly Command: "command";
|
|
54
|
+
readonly Response: "response";
|
|
55
|
+
readonly Notify: "notify";
|
|
56
|
+
readonly Subscribe: "subscribe";
|
|
57
|
+
readonly Unsubscribe: "unsubscribe";
|
|
58
|
+
readonly Publish: "publish";
|
|
59
|
+
readonly Event: "event";
|
|
60
|
+
readonly Ack: "ack";
|
|
61
|
+
readonly Error: "error";
|
|
62
|
+
readonly Auth: "auth";
|
|
63
|
+
readonly Ping: "ping";
|
|
64
|
+
readonly Pong: "pong";
|
|
65
|
+
readonly Connect: "connect";
|
|
25
66
|
};
|
|
26
67
|
export type WSMsgType = (typeof WSMsgType)[keyof typeof WSMsgType];
|
|
27
|
-
type
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
requestId?: string;
|
|
31
|
-
publisherId?: string;
|
|
32
|
-
}
|
|
33
|
-
interface WSTarget {
|
|
34
|
-
type?: string;
|
|
35
|
-
id: string;
|
|
36
|
-
}
|
|
37
|
-
interface WSMessage<T = unknown> {
|
|
68
|
+
export type DirectMsgType = (typeof DirectMsgType)[keyof typeof DirectMsgType];
|
|
69
|
+
export type WSTopic = 'server.time' | 'server.logs' | 'server.status' | 'client.online' | 'client.status';
|
|
70
|
+
export interface WSMessage<T = unknown> {
|
|
38
71
|
type: WSMsgType;
|
|
39
72
|
topic?: string;
|
|
40
|
-
|
|
73
|
+
timestamp?: number;
|
|
41
74
|
data?: T;
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
interface AckPayload {
|
|
45
|
-
status: string;
|
|
46
|
-
wsuuid?: string;
|
|
75
|
+
targetId?: string;
|
|
76
|
+
senderId?: string;
|
|
47
77
|
}
|
|
48
|
-
interface
|
|
49
|
-
|
|
78
|
+
export interface StatusPayload {
|
|
79
|
+
status: StatusCode;
|
|
50
80
|
message: string;
|
|
51
81
|
}
|
|
52
|
-
|
|
82
|
+
export type AuthAction = 'verify' | 'refresh' | 'expired';
|
|
83
|
+
export interface AuthPayload {
|
|
84
|
+
action: AuthAction;
|
|
85
|
+
token?: string;
|
|
86
|
+
}
|
|
87
|
+
export interface TimePayload {
|
|
53
88
|
time: string;
|
|
54
89
|
unix: number;
|
|
55
90
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
-
interface DirectPayload<T = unknown> {
|
|
61
|
-
action: string;
|
|
91
|
+
export type CmdAction = 'CONRST' | 'LOGOUT';
|
|
92
|
+
export interface DirectPayload<T = unknown> {
|
|
93
|
+
action?: CmdAction;
|
|
62
94
|
payload?: T;
|
|
63
95
|
}
|
|
64
|
-
interface NotifyPayload {
|
|
96
|
+
export interface NotifyPayload {
|
|
65
97
|
title?: string;
|
|
66
98
|
icon?: string;
|
|
67
99
|
body: string;
|
|
@@ -86,9 +118,9 @@ interface WssPluginOptions {
|
|
|
86
118
|
interface IWssService {
|
|
87
119
|
readonly wsuuid: string;
|
|
88
120
|
post(packet: WSMessage): void;
|
|
89
|
-
direct(targetId: string, data
|
|
121
|
+
direct(type: DirectMsgType, targetId: string, data?: DirectPayload): void;
|
|
90
122
|
publish(topic: WSTopic, data: unknown): void;
|
|
91
|
-
subscribe<T>(topic: WSTopic, callback: (
|
|
123
|
+
subscribe<T>(topic: WSTopic, callback: (data: T) => void, signal?: AbortSignal): () => void;
|
|
92
124
|
}
|
|
93
125
|
//#endregion
|
|
94
126
|
//#region src/service.d.ts
|
|
@@ -108,4 +140,4 @@ export declare const info: () => {
|
|
|
108
140
|
name: string;
|
|
109
141
|
};
|
|
110
142
|
//#endregion
|
|
111
|
-
export type {
|
|
143
|
+
export type { IWssService, WssPluginOptions };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{t as
|
|
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.14`,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`,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,console.log(`[WSS-UUID]`,this._wsuuid),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`:localStorage.clear(),sessionStorage.clear(),window.location.reload();break;default:console.log(`[WSS-CMD]`,e.action)}}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};
|
|
@@ -0,0 +1 @@
|
|
|
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};export{e as a,n as i,a as n,r as o,t as r,i as t};
|
package/dist/wss.worker.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{n as e,o as t}from"./ws.types-DuEMwpzC.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}}),clearInterval(r),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=()=>{clearInterval(r),r=setInterval(()=>{if(n&&n.readyState===WebSocket.OPEN){let t={type:e.Ping,timestamp:(/* @__PURE__ */ new Date()).getTime()};n.send(JSON.stringify(t))}},5e4)},c=()=>Math.floor(Math.random()*20001)+1e4;
|
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
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};
|