sonic-ws 1.3.0 → 1.3.2
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/README.md +50 -44
- package/dist/index.d.ts +4 -0
- package/dist/version.d.ts +5 -1
- package/dist/version.js +4 -23
- package/dist/ws/Connection.d.ts +52 -2
- package/dist/ws/Connection.js +4 -48
- package/dist/ws/PacketProcessor.d.ts +11 -3
- package/dist/ws/PacketProcessor.js +4 -17
- package/dist/ws/client/core/ClientCore.d.ts +17 -35
- package/dist/ws/client/core/ClientCore.js +4 -328
- package/dist/ws/client/node/ClientNode.d.ts +6 -2
- package/dist/ws/client/node/ClientNode.js +4 -34
- package/dist/ws/debug/DebugServer.d.ts +11 -0
- package/dist/ws/debug/DebugServer.js +6 -0
- package/dist/ws/packets/PacketProcessors.d.ts +6 -2
- package/dist/ws/packets/PacketProcessors.js +4 -314
- package/dist/ws/packets/PacketType.d.ts +4 -0
- package/dist/ws/packets/PacketType.js +4 -59
- package/dist/ws/packets/Packets.d.ts +12 -9
- package/dist/ws/packets/Packets.js +4 -313
- package/dist/ws/server/SonicWSConnection.d.ts +8 -35
- package/dist/ws/server/SonicWSConnection.js +4 -421
- package/dist/ws/server/SonicWSServer.d.ts +11 -10
- package/dist/ws/server/SonicWSServer.js +3 -906
- package/dist/ws/util/BufferUtil.d.ts +7 -2
- package/dist/ws/util/BufferUtil.js +4 -43
- package/dist/ws/util/StringUtil.d.ts +7 -4
- package/dist/ws/util/StringUtil.js +4 -64
- package/dist/ws/util/enums/EnumHandler.d.ts +4 -0
- package/dist/ws/util/enums/EnumHandler.js +4 -58
- package/dist/ws/util/enums/EnumType.d.ts +4 -12
- package/dist/ws/util/enums/EnumType.js +4 -69
- package/dist/ws/util/packets/BatchHelper.d.ts +5 -15
- package/dist/ws/util/packets/BatchHelper.js +4 -77
- package/dist/ws/util/packets/CompressionUtil.d.ts +17 -26
- package/dist/ws/util/packets/CompressionUtil.js +4 -533
- package/dist/ws/util/packets/HashUtil.d.ts +4 -0
- package/dist/ws/util/packets/HashUtil.js +4 -120
- package/dist/ws/util/packets/JSONUtil.d.ts +6 -0
- package/dist/ws/util/packets/JSONUtil.js +6 -0
- package/dist/ws/util/packets/PacketHolder.d.ts +3 -62
- package/dist/ws/util/packets/PacketHolder.js +4 -123
- package/dist/ws/util/packets/PacketUtils.d.ts +4 -17
- package/dist/ws/util/packets/PacketUtils.js +4 -272
- package/dist/ws/util/packets/RateHandler.d.ts +5 -14
- package/dist/ws/util/packets/RateHandler.js +4 -63
- package/package.json +4 -2
- package/dist/ws/util/ArrayUtil.d.ts +0 -1
- package/dist/ws/util/ArrayUtil.js +0 -24
package/README.md
CHANGED
|
@@ -1,6 +1,19 @@
|
|
|
1
1
|
# sonic-ws
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
### WebSockets that handle correctness, security, and performance for you
|
|
4
|
+
|
|
5
|
+
- No race conditions
|
|
6
|
+
- No invalid packets
|
|
7
|
+
- No accidental DoS
|
|
8
|
+
- No cleanup bugs
|
|
9
|
+
- No boilerplate
|
|
10
|
+
- Many built-in security features
|
|
11
|
+
- Lower bandwidth cost
|
|
12
|
+
- Works much better on mobile
|
|
13
|
+
- More readable than every other socket library
|
|
14
|
+
|
|
15
|
+
<details>
|
|
16
|
+
<summary>DETAILED INFO</summary>
|
|
4
17
|
|
|
5
18
|
SonicWS is an ultra-lightweight, high-performance WebSocket library focused on maximum bandwidth efficiency, speed, and security.
|
|
6
19
|
|
|
@@ -44,83 +57,76 @@ Developer Experience:
|
|
|
44
57
|
- Almost every case has a pre-made wire optimization and boilerplate removal.
|
|
45
58
|
|
|
46
59
|
Whether you're making a real-time game, a dashboard, a distributed system, or anything else, SonicWS gets you safe, structured packets; fast.
|
|
60
|
+
</details>
|
|
47
61
|
|
|
48
62
|
## SAMPLES
|
|
49
63
|
|
|
50
|
-
###
|
|
51
|
-
Node (Client & Server):
|
|
64
|
+
### Node (Client & Server)
|
|
52
65
|
```js
|
|
53
|
-
import {
|
|
66
|
+
import { SonicWS, SonicWSServer, CreatePacket, PacketType } from "sonic-ws";
|
|
54
67
|
```
|
|
68
|
+
|
|
55
69
|
Browser (Client):
|
|
56
70
|
```html
|
|
57
|
-
<script src="https://cdn.jsdelivr.net/gh/liwybloc/sonic-ws/release/SonicWS_bundle
|
|
71
|
+
<script src="https://cdn.jsdelivr.net/gh/liwybloc/sonic-ws/release/SonicWS_bundle"></script>
|
|
58
72
|
```
|
|
59
73
|
*This will always give the latest release build. I will add branches for each release if this project actually goes anywhere.
|
|
60
74
|
|
|
61
|
-
### Server
|
|
75
|
+
### Simple Example: Clicker Server
|
|
76
|
+
|
|
77
|
+
#### Server
|
|
62
78
|
```js
|
|
63
79
|
const wss = new SonicWSServer({
|
|
64
80
|
clientPackets: [
|
|
65
|
-
CreatePacket({tag: "
|
|
81
|
+
CreatePacket({ tag: "click", type: PacketType.NONE }),
|
|
82
|
+
CreatePacket({ tag: "token", type: PacketType.STRINGS }),
|
|
66
83
|
],
|
|
67
84
|
serverPackets: [
|
|
68
|
-
CreatePacket({tag: "
|
|
69
|
-
|
|
85
|
+
CreatePacket({ tag: "pointsInfo", type: PacketType.UVARINT }),
|
|
86
|
+
CreatePacket({ tag: "notification", type: PacketType.STRINGS }),
|
|
70
87
|
],
|
|
71
|
-
websocketOptions: { port: 1234 }
|
|
88
|
+
websocketOptions: { port: 1234 },
|
|
72
89
|
});
|
|
73
90
|
|
|
74
|
-
wss.
|
|
91
|
+
wss.requireHandshake("token");
|
|
75
92
|
|
|
76
|
-
|
|
93
|
+
wss.on_connect(ws => {
|
|
94
|
+
console.log("Client connected:", ws.id);
|
|
77
95
|
|
|
78
|
-
|
|
79
|
-
console.log("Ponged!", num);
|
|
80
|
-
socket.send("data", [Math.floor(Math.random() * 26), Math.floor(Math.random() * 256)], ["hello", "from", "server"]);
|
|
81
|
-
});
|
|
96
|
+
let clicks = 0;
|
|
82
97
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
98
|
+
ws.on("token", token => {
|
|
99
|
+
if(!isValidToken(token)) // No listeners will ever trigger after this, unlike base websocket
|
|
100
|
+
return socket.close();
|
|
101
|
+
})
|
|
86
102
|
|
|
87
|
-
|
|
103
|
+
// auto validation, no boilerplate. always passed the token check
|
|
104
|
+
ws.on("click", () => {
|
|
105
|
+
ws.send("pointsInfo", ++clicks);
|
|
106
|
+
});
|
|
88
107
|
|
|
89
|
-
|
|
90
|
-
console.log("Server ready!");
|
|
108
|
+
ws.setInterval(() => ws.send("notification", "Keep going!"), 5000); // auto cleanup on close
|
|
91
109
|
});
|
|
110
|
+
|
|
111
|
+
wss.on_ready(() => console.log("Server ready!"));
|
|
92
112
|
```
|
|
93
113
|
|
|
94
|
-
|
|
114
|
+
#### Client
|
|
95
115
|
```js
|
|
96
116
|
const ws = new SonicWS("ws://localhost:1234");
|
|
97
117
|
|
|
98
|
-
ws.on_ready(() =>
|
|
99
|
-
console.log("Connected to server");
|
|
100
|
-
});
|
|
118
|
+
ws.on_ready(() => console.log("Connected to server"));
|
|
101
119
|
|
|
102
|
-
ws.on("
|
|
103
|
-
console.log("Pinged!", num);
|
|
104
|
-
ws.send("pong", Date.now());
|
|
105
|
-
})
|
|
106
|
-
ws.on("data", (i, s) => {
|
|
107
|
-
console.log("data: ", i);
|
|
108
|
-
console.log("message: " + s.join(" "));
|
|
109
|
-
});
|
|
120
|
+
ws.on("pointsInfo", clicks => console.log("Total Clicks: ", clicks));
|
|
110
121
|
|
|
111
|
-
ws.
|
|
112
|
-
|
|
122
|
+
ws.on("notification", msg => console.log("Notification:", msg));
|
|
123
|
+
|
|
124
|
+
button.addEventListener("click", () => {
|
|
125
|
+
ws.send("click");
|
|
113
126
|
});
|
|
127
|
+
|
|
114
128
|
```
|
|
115
129
|
|
|
116
130
|
## KNOWN ISSUES
|
|
117
131
|
|
|
118
|
-
Some weird error messages when invalid inputs are in like CreatePacket() and stuff
|
|
119
|
-
|
|
120
132
|
## PLANNED FEATURES
|
|
121
|
-
|
|
122
|
-
Better error handling
|
|
123
|
-
|
|
124
|
-
Some middleware support
|
|
125
|
-
|
|
126
|
-
Debug menus for client/server
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 Lily (liwybloc)
|
|
3
|
+
* Licensed under the Apache License, Version 2.0.
|
|
4
|
+
*/
|
|
1
5
|
export { SonicWS } from './ws/client/node/ClientNode';
|
|
2
6
|
export { SonicWSConnection } from './ws/server/SonicWSConnection';
|
|
3
7
|
export { SonicWSServer } from './ws/server/SonicWSServer';
|
package/dist/version.d.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 Lily (liwybloc)
|
|
3
|
+
* Licensed under the Apache License, Version 2.0.
|
|
4
|
+
*/
|
|
1
5
|
/** Current protocol version */
|
|
2
|
-
export declare const VERSION =
|
|
6
|
+
export declare const VERSION = 20;
|
|
3
7
|
/** Server data suffix */
|
|
4
8
|
export declare const SERVER_SUFFIX = "SWS";
|
|
5
9
|
/** Server data suffix in array */
|
package/dist/version.js
CHANGED
|
@@ -1,25 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
/*
|
|
1
|
+
/**
|
|
3
2
|
* Copyright 2026 Lily (liwybloc)
|
|
4
|
-
*
|
|
5
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
-
* you may not use this file except in compliance with the License.
|
|
7
|
-
* You may obtain a copy of the License at
|
|
8
|
-
*
|
|
9
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
-
*
|
|
11
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
-
* See the License for the specific language governing permissions and
|
|
15
|
-
* limitations under the License.
|
|
3
|
+
* Licensed under the Apache License, Version 2.0.
|
|
16
4
|
*/
|
|
17
|
-
|
|
18
|
-
exports.SERVER_SUFFIX_NUMS
|
|
19
|
-
const StringUtil_1 = require("./ws/util/StringUtil");
|
|
20
|
-
/** Current protocol version */
|
|
21
|
-
exports.VERSION = 19;
|
|
22
|
-
/** Server data suffix */
|
|
23
|
-
exports.SERVER_SUFFIX = "SWS";
|
|
24
|
-
/** Server data suffix in array */
|
|
25
|
-
exports.SERVER_SUFFIX_NUMS = (0, StringUtil_1.processCharCodes)(exports.SERVER_SUFFIX);
|
|
5
|
+
|
|
6
|
+
Object.defineProperty(exports,"__esModule",{value:!0}),exports.SERVER_SUFFIX_NUMS=exports.SERVER_SUFFIX=exports.VERSION=void 0;const e=require("./ws/util/StringUtil");exports.VERSION=20,exports.SERVER_SUFFIX="SWS",exports.SERVER_SUFFIX_NUMS=(0,e.processCharCodes)(exports.SERVER_SUFFIX);
|
package/dist/ws/Connection.d.ts
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 Lily (liwybloc)
|
|
3
|
+
* Licensed under the Apache License, Version 2.0.
|
|
4
|
+
*/
|
|
5
|
+
import { ConnectionMiddleware, IMiddlewareHolder, MiddlewareHolder } from "./PacketProcessor";
|
|
6
|
+
import { BatchHelper } from "./util/packets/BatchHelper";
|
|
2
7
|
/**
|
|
3
8
|
* Holds shared connection values. Lets helper functions work on client and server.
|
|
4
9
|
*/
|
|
5
|
-
export interface
|
|
10
|
+
export interface IConnection<T> extends IMiddlewareHolder<ConnectionMiddleware> {
|
|
6
11
|
/**
|
|
7
12
|
* List of timers.
|
|
8
13
|
* For internal use only.
|
|
@@ -39,10 +44,55 @@ export interface Connection extends MiddlewareHolder<ConnectionMiddleware> {
|
|
|
39
44
|
* Sends raw uint8array data through the connection
|
|
40
45
|
*/
|
|
41
46
|
raw_send(data: Uint8Array): void;
|
|
47
|
+
/**
|
|
48
|
+
* Listens for all messages rawly
|
|
49
|
+
* @param listener Callback for when data is received
|
|
50
|
+
*/
|
|
51
|
+
raw_onmessage(listener: (data: T) => void): void;
|
|
42
52
|
/**
|
|
43
53
|
* Closes the connection
|
|
44
54
|
*/
|
|
45
55
|
close(code?: number, reason?: string): void;
|
|
56
|
+
/**
|
|
57
|
+
* Checks if the connection is closed
|
|
58
|
+
* @returns If it's closed or not
|
|
59
|
+
*/
|
|
60
|
+
isClosed(): boolean;
|
|
61
|
+
/**
|
|
62
|
+
* Sets the name of this connection for the debug menu; good for setting e.g. usernames on games
|
|
63
|
+
*/
|
|
64
|
+
setName(name: string): Promise<void>;
|
|
65
|
+
/**
|
|
66
|
+
* @returns Name of the socket, defaults to Socket [ID] or LocalSocket unless set with setName()
|
|
67
|
+
*/
|
|
68
|
+
getName(): string;
|
|
69
|
+
}
|
|
70
|
+
export declare abstract class Connection<T extends {
|
|
71
|
+
readyState: number;
|
|
72
|
+
send: (u: Uint8Array) => void;
|
|
73
|
+
close: (c: number, d: string | undefined) => void;
|
|
74
|
+
}, K> extends MiddlewareHolder<ConnectionMiddleware> implements IConnection<K> {
|
|
75
|
+
protected listeners: Record<string, Array<(...data: any[]) => void>>;
|
|
76
|
+
private name;
|
|
77
|
+
protected closed: boolean;
|
|
78
|
+
socket: T;
|
|
79
|
+
_timers: Record<number, [number, (closed: boolean) => void, boolean]>;
|
|
80
|
+
protected batcher: BatchHelper;
|
|
81
|
+
_on: Function;
|
|
82
|
+
_off: Function;
|
|
83
|
+
/** The index of the connection; unique for all connected, not unique after disconnection. */
|
|
84
|
+
id: number;
|
|
85
|
+
constructor(socket: T, id: number, name: string, addListener: Function, removeListener: Function);
|
|
86
|
+
setTimeout(call: () => void, time: number, callOnClose?: boolean): number;
|
|
87
|
+
setInterval(call: () => void, time: number, callOnClose?: boolean): number;
|
|
88
|
+
clearTimeout(id: number): void;
|
|
89
|
+
clearInterval(id: number): void;
|
|
90
|
+
raw_send(data: Uint8Array): void;
|
|
91
|
+
raw_onmessage(listener: (data: K) => void): void;
|
|
92
|
+
close(code?: number, reason?: string | Buffer): void;
|
|
93
|
+
isClosed(): boolean;
|
|
94
|
+
setName(name: string): Promise<void>;
|
|
95
|
+
getName(): string;
|
|
46
96
|
}
|
|
47
97
|
export declare enum CloseCodes {
|
|
48
98
|
RATELIMIT = 4000,
|
package/dist/ws/Connection.js
CHANGED
|
@@ -1,50 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
/*
|
|
1
|
+
/**
|
|
3
2
|
* Copyright 2026 Lily (liwybloc)
|
|
4
|
-
*
|
|
5
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
-
* you may not use this file except in compliance with the License.
|
|
7
|
-
* You may obtain a copy of the License at
|
|
8
|
-
*
|
|
9
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
-
*
|
|
11
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
-
* See the License for the specific language governing permissions and
|
|
15
|
-
* limitations under the License.
|
|
3
|
+
* Licensed under the Apache License, Version 2.0.
|
|
16
4
|
*/
|
|
17
|
-
|
|
18
|
-
exports.CloseCodes =
|
|
19
|
-
exports.getClosureCause = getClosureCause;
|
|
20
|
-
var CloseCodes;
|
|
21
|
-
(function (CloseCodes) {
|
|
22
|
-
CloseCodes[CloseCodes["RATELIMIT"] = 4000] = "RATELIMIT";
|
|
23
|
-
CloseCodes[CloseCodes["SMALL"] = 4001] = "SMALL";
|
|
24
|
-
CloseCodes[CloseCodes["INVALID_KEY"] = 4002] = "INVALID_KEY";
|
|
25
|
-
CloseCodes[CloseCodes["INVALID_PACKET"] = 4003] = "INVALID_PACKET";
|
|
26
|
-
CloseCodes[CloseCodes["INVALID_DATA"] = 4004] = "INVALID_DATA";
|
|
27
|
-
CloseCodes[CloseCodes["REPEATED_HANDSHAKE"] = 4005] = "REPEATED_HANDSHAKE";
|
|
28
|
-
CloseCodes[CloseCodes["DISABLED_PACKET"] = 4006] = "DISABLED_PACKET";
|
|
29
|
-
CloseCodes[CloseCodes["MIDDLEWARE"] = 4007] = "MIDDLEWARE";
|
|
30
|
-
CloseCodes[CloseCodes["MANUAL_SHUTDOWN"] = 4008] = "MANUAL_SHUTDOWN";
|
|
31
|
-
})(CloseCodes || (exports.CloseCodes = CloseCodes = {}));
|
|
32
|
-
function getClosureCause(id) {
|
|
33
|
-
if (id >= 4000) {
|
|
34
|
-
return CloseCodes[id] ?? 'UNKNOWN';
|
|
35
|
-
}
|
|
36
|
-
switch (id) {
|
|
37
|
-
case 1000:
|
|
38
|
-
return 'NORMAL_CLOSURE';
|
|
39
|
-
case 1001:
|
|
40
|
-
return 'GOING_AWAY';
|
|
41
|
-
case 1002:
|
|
42
|
-
return 'PROTOCOL_ERROR';
|
|
43
|
-
case 1003:
|
|
44
|
-
return 'UNSUPPORTED_DATA';
|
|
45
|
-
case 1006:
|
|
46
|
-
return 'ABNORMAL_CLOSURE';
|
|
47
|
-
default:
|
|
48
|
-
return 'UNKNOWN';
|
|
49
|
-
}
|
|
50
|
-
}
|
|
5
|
+
|
|
6
|
+
Object.defineProperty(exports,"__esModule",{value:!0}),exports.CloseCodes=exports.Connection=void 0,exports.getClosureCause=function(e){if(e>=4e3)return r[e]??"UNKNOWN";switch(e){case 1e3:return"NORMAL_CLOSURE";case 1001:return"GOING_AWAY";case 1002:return"PROTOCOL_ERROR";case 1003:return"UNSUPPORTED_DATA";case 1006:return"ABNORMAL_CLOSURE";default:return"UNKNOWN"}};const e=require("./PacketProcessor"),t=require("./util/packets/BatchHelper");class s extends e.MiddlewareHolder{listeners;name;closed=!1;socket;_timers={};batcher;_on;_off;id;constructor(e,s,r,o,i){super(),this.id=s,this.listeners={},this.name=r,this.socket=e,this.batcher=new t.BatchHelper,this._on=o,this._off=i,this._on("close",()=>{this.callMiddleware("onStatusChange",WebSocket.CLOSED),this.closed=!0;for(const[e,t,s]of Object.values(this._timers))this.clearTimeout(e),s&&t(!0)}),this._on("open",()=>this.callMiddleware("onStatusChange",WebSocket.OPEN))}setTimeout(e,t,s=!1){const r=setTimeout(()=>{e(),this.clearTimeout(r)},t);return this._timers[r]=[r,e,s],r}setInterval(e,t,s=!1){const r=setInterval(e,t);return this._timers[r]=[r,e,s],r}clearTimeout(e){clearTimeout(e),delete this._timers[e]}clearInterval(e){this.clearTimeout(e)}raw_send(e){this.socket.send(e)}raw_onmessage(e){this._on("message",e)}close(e=1e3,t){this.closed=!0,this.socket.close(e,t?.toString())}isClosed(){return this.closed||this.socket.readyState==WebSocket.CLOSED}async setName(e){await this.callMiddleware("onNameChange",e)||(this.name=e)}getName(){return this.name}}var r;exports.Connection=s,function(e){e[e.RATELIMIT=4e3]="RATELIMIT",e[e.SMALL=4001]="SMALL",e[e.INVALID_KEY=4002]="INVALID_KEY",e[e.INVALID_PACKET=4003]="INVALID_PACKET",e[e.INVALID_DATA=4004]="INVALID_DATA",e[e.REPEATED_HANDSHAKE=4005]="REPEATED_HANDSHAKE",e[e.DISABLED_PACKET=4006]="DISABLED_PACKET",e[e.MIDDLEWARE=4007]="MIDDLEWARE",e[e.MANUAL_SHUTDOWN=4008]="MANUAL_SHUTDOWN"}(r||(exports.CloseCodes=r={}));
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 Lily (liwybloc)
|
|
3
|
+
* Licensed under the Apache License, Version 2.0.
|
|
4
|
+
*/
|
|
2
5
|
import { SonicWSConnection } from "./server/SonicWSConnection";
|
|
3
6
|
/**
|
|
4
7
|
* A basic middleware interface; extended by other middleware types
|
|
@@ -8,12 +11,12 @@ export interface BasicMiddleware {
|
|
|
8
11
|
* Called when the middleware is initialized
|
|
9
12
|
* @param conn The connection instance
|
|
10
13
|
*/
|
|
11
|
-
init?(conn:
|
|
14
|
+
init?(conn: IMiddlewareHolder<BasicMiddleware>): void;
|
|
12
15
|
}
|
|
13
16
|
export type FuncKeys<T> = {
|
|
14
17
|
[K in keyof T]: NonNullable<T[K]> extends (...args: any[]) => any ? K : never;
|
|
15
18
|
}[keyof T];
|
|
16
|
-
export interface
|
|
19
|
+
export interface IMiddlewareHolder<T extends BasicMiddleware> {
|
|
17
20
|
/**
|
|
18
21
|
* Adds middleware which can interact with packets and other events
|
|
19
22
|
*/
|
|
@@ -25,6 +28,11 @@ export interface MiddlewareHolder<T extends BasicMiddleware> {
|
|
|
25
28
|
*/
|
|
26
29
|
callMiddleware<K extends FuncKeys<T> & keyof T>(method: K, ...values: Parameters<NonNullable<Extract<T[K], (...args: any[]) => any>>>): Promise<boolean>;
|
|
27
30
|
}
|
|
31
|
+
export declare class MiddlewareHolder<T extends BasicMiddleware> implements IMiddlewareHolder<T> {
|
|
32
|
+
private middlewares;
|
|
33
|
+
addMiddleware(middleware: T): void;
|
|
34
|
+
callMiddleware<K extends FuncKeys<T> & keyof T>(method: K, ...values: Parameters<NonNullable<Extract<T[K], (...args: any[]) => any>>>): Promise<boolean>;
|
|
35
|
+
}
|
|
28
36
|
/**
|
|
29
37
|
* A connection middleware interface, used in SonicWSConnection and ClientCore
|
|
30
38
|
*/
|
|
@@ -1,19 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
/*
|
|
1
|
+
/**
|
|
3
2
|
* Copyright 2026 Lily (liwybloc)
|
|
4
|
-
*
|
|
5
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
-
* you may not use this file except in compliance with the License.
|
|
7
|
-
* You may obtain a copy of the License at
|
|
8
|
-
*
|
|
9
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
-
*
|
|
11
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
-
* See the License for the specific language governing permissions and
|
|
15
|
-
* limitations under the License.
|
|
3
|
+
* Licensed under the Apache License, Version 2.0.
|
|
16
4
|
*/
|
|
17
|
-
|
|
18
|
-
;
|
|
19
|
-
;
|
|
5
|
+
|
|
6
|
+
Object.defineProperty(exports,"__esModule",{value:!0}),exports.MiddlewareHolder=void 0;exports.MiddlewareHolder=class{middlewares=[];addMiddleware(e){this.middlewares.push(e);const r=e;try{"function"==typeof r.init&&r.init(this)}catch(e){console.warn("Middleware init threw an error:",e)}}async callMiddleware(e,...r){let t=!1;for(const d of this.middlewares){const i=d[e];if(i)try{await i(...r)&&(t=!0)}catch(r){console.warn(`Middleware ${String(e)} threw an error:`,r)}}return t}};
|
|
@@ -1,52 +1,40 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
};
|
|
14
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 Lily (liwybloc)
|
|
3
|
+
* Licensed under the Apache License, Version 2.0.
|
|
4
|
+
*/
|
|
5
|
+
import { PacketHolder } from "../../util/packets/PacketHolder";
|
|
6
|
+
import { Connection } from "../../Connection";
|
|
7
|
+
import { AsyncPQ, ClientPQ, PacketQueue } from "../../PacketProcessor";
|
|
8
|
+
export declare abstract class SonicWSCore<T extends {
|
|
9
|
+
readyState: number;
|
|
10
|
+
send: (u: Uint8Array<ArrayBufferLike>) => void;
|
|
11
|
+
close: (c: number, d: string | undefined) => void;
|
|
12
|
+
}, K> extends Connection<T, K> {
|
|
15
13
|
protected preListen: {
|
|
16
14
|
[key: string]: Array<(data: any[]) => void>;
|
|
17
15
|
} | null;
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
clientPackets: PacketHolder;
|
|
17
|
+
serverPackets: PacketHolder;
|
|
20
18
|
private pastKeys;
|
|
21
19
|
private readyListeners;
|
|
22
|
-
private batcher;
|
|
23
20
|
private bufferHandler;
|
|
24
|
-
id: number;
|
|
25
21
|
_timers: Record<number, [number, (closed: boolean) => void, boolean]>;
|
|
26
22
|
private asyncData;
|
|
27
23
|
private asyncMap;
|
|
28
|
-
constructor(ws:
|
|
24
|
+
constructor(ws: T, bufferHandler: (val: K) => Promise<Uint8Array>, on: Function, off: Function);
|
|
29
25
|
private reading;
|
|
30
26
|
private readQueue;
|
|
31
27
|
private serverKeyHandler;
|
|
32
28
|
private invalidPacket;
|
|
33
29
|
private listenLock;
|
|
34
30
|
private packetQueue;
|
|
35
|
-
listenPacket(data: string | [any[], boolean],
|
|
31
|
+
listenPacket(data: string | [any[], boolean], tag: string, packetQueue: PacketQueue<ClientPQ>, isAsync: boolean, asyncData: AsyncPQ<ClientPQ>): Promise<void>;
|
|
36
32
|
private isAsync;
|
|
37
33
|
private enqueuePacket;
|
|
38
34
|
private triggerNextPacket;
|
|
39
|
-
private middlewares;
|
|
40
|
-
addMiddleware(middleware: ConnectionMiddleware): void;
|
|
41
|
-
callMiddleware<K extends FuncKeys<ConnectionMiddleware> & keyof ConnectionMiddleware>(method: K, ...values: Parameters<NonNullable<Extract<ConnectionMiddleware[K], (...args: any[]) => any>>>): Promise<boolean>;
|
|
42
35
|
private dataHandler;
|
|
43
36
|
private messageHandler;
|
|
44
|
-
protected listen(
|
|
45
|
-
/**
|
|
46
|
-
* Listens for all messages rawly
|
|
47
|
-
* @param listener Callback for when data is received
|
|
48
|
-
*/
|
|
49
|
-
raw_onmessage(listener: (data: Uint8Array) => void): void;
|
|
37
|
+
protected listen(tag: string, listener: (data: any[]) => void): void;
|
|
50
38
|
/**
|
|
51
39
|
* Listens for all sent messages rawly
|
|
52
40
|
* @param listener Callback for when data is received
|
|
@@ -75,10 +63,4 @@ export declare abstract class SonicWSCore implements Connection {
|
|
|
75
63
|
* @param listener The callback with the values
|
|
76
64
|
*/
|
|
77
65
|
on(tag: string, listener: (value: any[]) => void): void;
|
|
78
|
-
raw_send(data: Uint8Array): void;
|
|
79
|
-
setTimeout(call: () => void, time: number, callOnClose?: boolean): number;
|
|
80
|
-
setInterval(call: () => void, time: number, callOnClose?: boolean): number;
|
|
81
|
-
clearTimeout(id: number): void;
|
|
82
|
-
clearInterval(id: number): void;
|
|
83
|
-
close(code?: number, reason?: string): void;
|
|
84
66
|
}
|