sonic-ws 1.3.0 → 1.3.1
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/index.js +4 -14
- package/dist/version.d.ts +5 -1
- package/dist/version.js +8 -18
- package/dist/ws/Connection.d.ts +28 -41
- package/dist/ws/Connection.js +81 -15
- package/dist/ws/PacketProcessor.d.ts +11 -3
- package/dist/ws/PacketProcessor.js +37 -14
- package/dist/ws/client/core/ClientCore.d.ts +17 -35
- package/dist/ws/client/core/ClientCore.js +36 -142
- package/dist/ws/client/node/ClientNode.d.ts +6 -2
- package/dist/ws/client/node/ClientNode.js +7 -21
- package/dist/ws/debug/DebugClient.d.ts +11 -0
- package/dist/ws/debug/DebugClient.js +448 -0
- package/dist/ws/debug/DebugServer.d.ts +11 -0
- package/dist/ws/debug/DebugServer.js +202 -0
- package/dist/ws/packets/PacketProcessors.d.ts +5 -1
- package/dist/ws/packets/PacketProcessors.js +28 -37
- package/dist/ws/packets/PacketType.d.ts +4 -0
- package/dist/ws/packets/PacketType.js +25 -35
- package/dist/ws/packets/Packets.d.ts +4 -0
- package/dist/ws/packets/Packets.js +56 -71
- package/dist/ws/server/SonicWSConnection.d.ts +8 -35
- package/dist/ws/server/SonicWSConnection.js +30 -198
- package/dist/ws/server/SonicWSServer.d.ts +11 -10
- package/dist/ws/server/SonicWSServer.js +26 -699
- package/dist/ws/util/BufferUtil.d.ts +5 -2
- package/dist/ws/util/BufferUtil.js +12 -27
- package/dist/ws/util/StringUtil.d.ts +7 -3
- package/dist/ws/util/StringUtil.js +26 -49
- package/dist/ws/util/enums/EnumHandler.d.ts +4 -0
- package/dist/ws/util/enums/EnumHandler.js +6 -26
- package/dist/ws/util/enums/EnumType.d.ts +4 -12
- package/dist/ws/util/enums/EnumType.js +13 -26
- package/dist/ws/util/packets/BatchHelper.d.ts +5 -15
- package/dist/ws/util/packets/BatchHelper.js +25 -29
- package/dist/ws/util/packets/CompressionUtil.d.ts +11 -13
- package/dist/ws/util/packets/CompressionUtil.js +69 -274
- package/dist/ws/util/packets/HashUtil.d.ts +4 -0
- package/dist/ws/util/packets/HashUtil.js +4 -14
- package/dist/ws/util/packets/JSONUtil.d.ts +6 -0
- package/dist/ws/util/packets/JSONUtil.js +169 -0
- package/dist/ws/util/packets/PacketHolder.d.ts +3 -62
- package/dist/ws/util/packets/PacketHolder.js +25 -58
- package/dist/ws/util/packets/PacketUtils.d.ts +4 -17
- package/dist/ws/util/packets/PacketUtils.js +17 -62
- package/dist/ws/util/packets/RateHandler.d.ts +5 -14
- package/dist/ws/util/packets/RateHandler.js +8 -17
- package/package.json +3 -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/index.js
CHANGED
|
@@ -1,19 +1,9 @@
|
|
|
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
|
*/
|
|
5
|
+
"use strict";
|
|
6
|
+
|
|
17
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
8
|
exports.WrapEnum = exports.DefineEnum = exports.UnFlattenData = exports.FlattenData = exports.CreateEnumPacket = exports.CreateObjPacket = exports.CreatePacket = exports.PacketType = exports.SonicWSServer = exports.SonicWSConnection = exports.SonicWS = void 0;
|
|
19
9
|
var ClientNode_1 = require("./ws/client/node/ClientNode");
|
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,15 @@
|
|
|
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
|
*/
|
|
5
|
+
"use strict";
|
|
6
|
+
|
|
17
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
8
|
exports.SERVER_SUFFIX_NUMS = exports.SERVER_SUFFIX = exports.VERSION = void 0;
|
|
19
9
|
const StringUtil_1 = require("./ws/util/StringUtil");
|
|
20
|
-
|
|
21
|
-
exports.VERSION =
|
|
22
|
-
|
|
10
|
+
|
|
11
|
+
exports.VERSION = 20;
|
|
12
|
+
|
|
23
13
|
exports.SERVER_SUFFIX = "SWS";
|
|
24
|
-
|
|
14
|
+
|
|
25
15
|
exports.SERVER_SUFFIX_NUMS = (0, StringUtil_1.processCharCodes)(exports.SERVER_SUFFIX);
|
package/dist/ws/Connection.d.ts
CHANGED
|
@@ -1,48 +1,35 @@
|
|
|
1
|
-
import { ConnectionMiddleware, MiddlewareHolder } from "./PacketProcessor";
|
|
2
1
|
/**
|
|
3
|
-
*
|
|
2
|
+
* Copyright 2026 Lily (liwybloc)
|
|
3
|
+
* Licensed under the Apache License, Version 2.0.
|
|
4
4
|
*/
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
import { ConnectionMiddleware, MiddlewareHolder } from "./PacketProcessor";
|
|
6
|
+
import { BatchHelper } from "./util/packets/BatchHelper";
|
|
7
|
+
export declare abstract class Connection<T extends {
|
|
8
|
+
readyState: number;
|
|
9
|
+
send: (u: Uint8Array) => void;
|
|
10
|
+
close: (c: number, d: string | undefined) => void;
|
|
11
|
+
}, K> extends MiddlewareHolder<ConnectionMiddleware> implements IConnection<K> {
|
|
12
|
+
protected listeners: Record<string, Array<(...data: any[]) => void>>;
|
|
13
|
+
private name;
|
|
14
|
+
protected closed: boolean;
|
|
15
|
+
socket: T;
|
|
10
16
|
_timers: Record<number, [number, (closed: boolean) => void, boolean]>;
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
* @param call The function to call
|
|
22
|
-
* @param time The time between calls (ms)
|
|
23
|
-
* @returns The interval id to be used with socket.clearInterval(id)
|
|
24
|
-
*/
|
|
25
|
-
setInterval(call: () => void, time: number): number;
|
|
26
|
-
/**
|
|
27
|
-
* Clears a timeout/interval
|
|
28
|
-
* @param id The timeout id
|
|
29
|
-
*/
|
|
30
|
-
clearTimeout(index: number): void;
|
|
31
|
-
/**
|
|
32
|
-
* Clears an interval
|
|
33
|
-
*
|
|
34
|
-
* Delegates to `clearTimeout`
|
|
35
|
-
* @param id The interval id
|
|
36
|
-
*/
|
|
37
|
-
clearInterval(index: number): void;
|
|
38
|
-
/**
|
|
39
|
-
* Sends raw uint8array data through the connection
|
|
40
|
-
*/
|
|
17
|
+
protected batcher: BatchHelper;
|
|
18
|
+
_on: Function;
|
|
19
|
+
_off: Function;
|
|
20
|
+
/** The index of the connection; unique for all connected, not unique after disconnection. */
|
|
21
|
+
id: number;
|
|
22
|
+
constructor(socket: T, id: number, name: string, addListener: Function, removeListener: Function);
|
|
23
|
+
setTimeout(call: () => void, time: number, callOnClose?: boolean): number;
|
|
24
|
+
setInterval(call: () => void, time: number, callOnClose?: boolean): number;
|
|
25
|
+
clearTimeout(id: number): void;
|
|
26
|
+
clearInterval(id: number): void;
|
|
41
27
|
raw_send(data: Uint8Array): void;
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
28
|
+
raw_onmessage(listener: (data: K) => void): void;
|
|
29
|
+
close(code?: number, reason?: string | Buffer): void;
|
|
30
|
+
isClosed(): boolean;
|
|
31
|
+
setName(name: string): Promise<void>;
|
|
32
|
+
getName(): string;
|
|
46
33
|
}
|
|
47
34
|
export declare enum CloseCodes {
|
|
48
35
|
RATELIMIT = 4000,
|
package/dist/ws/Connection.js
CHANGED
|
@@ -1,22 +1,88 @@
|
|
|
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
|
*/
|
|
5
|
+
"use strict";
|
|
6
|
+
|
|
17
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
-
exports.CloseCodes = void 0;
|
|
8
|
+
exports.CloseCodes = exports.Connection = void 0;
|
|
19
9
|
exports.getClosureCause = getClosureCause;
|
|
10
|
+
const PacketProcessor_1 = require("./PacketProcessor");
|
|
11
|
+
const BatchHelper_1 = require("./util/packets/BatchHelper");
|
|
12
|
+
class Connection extends PacketProcessor_1.MiddlewareHolder {
|
|
13
|
+
listeners;
|
|
14
|
+
name;
|
|
15
|
+
closed = false;
|
|
16
|
+
socket;
|
|
17
|
+
_timers = {};
|
|
18
|
+
batcher;
|
|
19
|
+
_on;
|
|
20
|
+
_off;
|
|
21
|
+
|
|
22
|
+
id;
|
|
23
|
+
constructor(socket, id, name, addListener, removeListener) {
|
|
24
|
+
super();
|
|
25
|
+
this.id = id;
|
|
26
|
+
this.listeners = {};
|
|
27
|
+
this.name = name;
|
|
28
|
+
this.socket = socket;
|
|
29
|
+
this.batcher = new BatchHelper_1.BatchHelper();
|
|
30
|
+
this._on = addListener;
|
|
31
|
+
this._off = removeListener;
|
|
32
|
+
this._on("close", () => {
|
|
33
|
+
this.callMiddleware('onStatusChange', WebSocket.CLOSED);
|
|
34
|
+
this.closed = true;
|
|
35
|
+
for (const [id, callback, shouldCall] of Object.values(this._timers)) {
|
|
36
|
+
this.clearTimeout(id);
|
|
37
|
+
if (shouldCall)
|
|
38
|
+
callback(true);
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
this._on('open', () => this.callMiddleware('onStatusChange', WebSocket.OPEN));
|
|
42
|
+
}
|
|
43
|
+
setTimeout(call, time, callOnClose = false) {
|
|
44
|
+
const timeout = setTimeout(() => {
|
|
45
|
+
call();
|
|
46
|
+
this.clearTimeout(timeout);
|
|
47
|
+
}, time);
|
|
48
|
+
this._timers[timeout] = [timeout, call, callOnClose];
|
|
49
|
+
return timeout;
|
|
50
|
+
}
|
|
51
|
+
setInterval(call, time, callOnClose = false) {
|
|
52
|
+
const interval = setInterval(call, time);
|
|
53
|
+
this._timers[interval] = [interval, call, callOnClose];
|
|
54
|
+
return interval;
|
|
55
|
+
}
|
|
56
|
+
clearTimeout(id) {
|
|
57
|
+
clearTimeout(id);
|
|
58
|
+
delete this._timers[id];
|
|
59
|
+
}
|
|
60
|
+
clearInterval(id) {
|
|
61
|
+
this.clearTimeout(id);
|
|
62
|
+
}
|
|
63
|
+
raw_send(data) {
|
|
64
|
+
this.socket.send(data);
|
|
65
|
+
}
|
|
66
|
+
raw_onmessage(listener) {
|
|
67
|
+
this._on("message", listener);
|
|
68
|
+
}
|
|
69
|
+
close(code = 1000, reason) {
|
|
70
|
+
this.closed = true;
|
|
71
|
+
this.socket.close(code, reason?.toString());
|
|
72
|
+
}
|
|
73
|
+
isClosed() {
|
|
74
|
+
return this.closed || this.socket.readyState == WebSocket.CLOSED;
|
|
75
|
+
}
|
|
76
|
+
async setName(name) {
|
|
77
|
+
if (await this.callMiddleware("onNameChange", name))
|
|
78
|
+
return;
|
|
79
|
+
this.name = name;
|
|
80
|
+
}
|
|
81
|
+
getName() {
|
|
82
|
+
return this.name;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
exports.Connection = Connection;
|
|
20
86
|
var CloseCodes;
|
|
21
87
|
(function (CloseCodes) {
|
|
22
88
|
CloseCodes[CloseCodes["RATELIMIT"] = 4000] = "RATELIMIT";
|
|
@@ -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,42 @@
|
|
|
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
|
*/
|
|
5
|
+
"use strict";
|
|
6
|
+
|
|
17
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.MiddlewareHolder = void 0;
|
|
9
|
+
class MiddlewareHolder {
|
|
10
|
+
middlewares = [];
|
|
11
|
+
addMiddleware(middleware) {
|
|
12
|
+
this.middlewares.push(middleware);
|
|
13
|
+
const m = middleware;
|
|
14
|
+
try {
|
|
15
|
+
if (typeof m.init === 'function')
|
|
16
|
+
m.init(this);
|
|
17
|
+
}
|
|
18
|
+
catch (e) {
|
|
19
|
+
console.warn('Middleware init threw an error:', e);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
async callMiddleware(method, ...values) {
|
|
23
|
+
let cancelled = false;
|
|
24
|
+
for (const middleware of this.middlewares) {
|
|
25
|
+
const fn = middleware[method];
|
|
26
|
+
if (!fn)
|
|
27
|
+
continue;
|
|
28
|
+
try {
|
|
29
|
+
if (await fn(...values)) {
|
|
30
|
+
cancelled = true;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
catch (e) {
|
|
34
|
+
console.warn(`Middleware ${String(method)} threw an error:`, e);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return cancelled;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.MiddlewareHolder = MiddlewareHolder;
|
|
18
41
|
;
|
|
19
42
|
;
|
|
@@ -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
|
}
|