sonic-ws 1.0.0-beta.9 → 1.0.0-rc.0-patch
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 +4 -4
- package/dist/ws/client/core/ClientCore.d.ts +5 -1
- package/dist/ws/client/core/ClientCore.js +43 -10
- package/dist/ws/enums/EnumHandler.d.ts +5 -5
- package/dist/ws/enums/EnumHandler.js +1 -1
- package/dist/ws/enums/EnumType.d.ts +3 -8
- package/dist/ws/enums/EnumType.js +30 -11
- package/dist/ws/packets/PacketListener.d.ts +1 -1
- package/dist/ws/packets/PacketListener.js +2 -12
- package/dist/ws/packets/PacketProcessors.js +12 -9
- package/dist/ws/packets/PacketType.d.ts +2 -1
- package/dist/ws/packets/PacketType.js +3 -2
- package/dist/ws/packets/Packets.d.ts +16 -11
- package/dist/ws/packets/Packets.js +65 -32
- package/dist/ws/server/SonicWSConnection.d.ts +67 -6
- package/dist/ws/server/SonicWSConnection.js +204 -41
- package/dist/ws/server/SonicWSServer.d.ts +80 -1
- package/dist/ws/server/SonicWSServer.js +91 -6
- package/dist/ws/util/CodePointUtil.js +12 -11
- package/dist/ws/util/PacketHolder.d.ts +4 -1
- package/dist/ws/util/PacketHolder.js +11 -6
- package/dist/ws/util/PacketUtils.d.ts +59 -21
- package/dist/ws/util/PacketUtils.js +68 -41
- package/package.json +1 -1
|
@@ -35,8 +35,10 @@ function overflowPow(num) {
|
|
|
35
35
|
var _a;
|
|
36
36
|
return (_a = OVERFLOW_POWS[num]) !== null && _a !== void 0 ? _a : (OVERFLOW_POWS[num] = Math.pow(exports.OVERFLOW, num));
|
|
37
37
|
}
|
|
38
|
+
for (var i = 0; i < 3; i++)
|
|
39
|
+
overflowPow(i);
|
|
38
40
|
function processCharCodes(text) {
|
|
39
|
-
return
|
|
41
|
+
return Array.from(text, function (char) { return char.charCodeAt(0); });
|
|
40
42
|
}
|
|
41
43
|
// this converts an encoded code point back to a signed number
|
|
42
44
|
function fromSignedINT_C(point) {
|
|
@@ -61,12 +63,13 @@ function sectorSize(number) {
|
|
|
61
63
|
// 0 would make -Infinity;
|
|
62
64
|
if (number == 0)
|
|
63
65
|
return 1;
|
|
66
|
+
number = Math.abs(number);
|
|
64
67
|
// iterative system because it's faster than log
|
|
65
68
|
var count = 1;
|
|
66
|
-
var num =
|
|
69
|
+
var num = overflowPow(1);
|
|
67
70
|
while (number >= num) {
|
|
68
71
|
count++;
|
|
69
|
-
num
|
|
72
|
+
num = overflowPow(count);
|
|
70
73
|
}
|
|
71
74
|
return count;
|
|
72
75
|
}
|
|
@@ -78,24 +81,22 @@ function convertINT_D(number, chars) {
|
|
|
78
81
|
// store the sign and work with the absolute value
|
|
79
82
|
var negative = number < 0;
|
|
80
83
|
number = Math.abs(number);
|
|
81
|
-
var
|
|
84
|
+
var result = [];
|
|
82
85
|
// for each character except the last, extract the digit at that position
|
|
83
86
|
// this is similar to how base conversion works: divide by base^position
|
|
84
87
|
for (var i = 0; i < chars - 1; i++) {
|
|
85
|
-
var power =
|
|
88
|
+
var power = overflowPow(chars - i - 1);
|
|
86
89
|
var based = Math.floor(number / power);
|
|
87
|
-
|
|
90
|
+
result.push(based);
|
|
88
91
|
// remove it from the number so it doesnt effect future iterations
|
|
89
92
|
number -= based * power;
|
|
90
93
|
}
|
|
91
94
|
// the last digit is just the remainder
|
|
92
|
-
|
|
95
|
+
result.push(number % exports.OVERFLOW);
|
|
93
96
|
// if the number was negative, we offset each character to indicate the sign
|
|
94
97
|
// we only offset non-zero digits to avoid collisions with the null character
|
|
95
|
-
var stringified = negative ?
|
|
96
|
-
|
|
97
|
-
.join("")
|
|
98
|
-
: string;
|
|
98
|
+
var stringified = negative ? result.map(function (part) { return String.fromCharCode(part > 0 ? part + exports.NEGATIVE_C : part); }).join("")
|
|
99
|
+
: String.fromCharCode.apply(String, result);
|
|
99
100
|
return stringified;
|
|
100
101
|
}
|
|
101
102
|
// decodes a string created by convertINT_D back into the original signed integer
|
|
@@ -10,9 +10,12 @@ export declare class PacketHolder {
|
|
|
10
10
|
createPackets(packets: Packet[]): void;
|
|
11
11
|
get(tag: string): number;
|
|
12
12
|
getChar(tag: string): string;
|
|
13
|
+
getTag(key: string): string;
|
|
13
14
|
getPacket(tag: string): Packet;
|
|
14
|
-
has(
|
|
15
|
+
has(key: string): boolean;
|
|
16
|
+
hasTag(tag: string): boolean;
|
|
15
17
|
getKeys(): Record<string, number>;
|
|
18
|
+
getTags(): Record<number, string>;
|
|
16
19
|
serialize(): string;
|
|
17
20
|
static empty(): PacketHolder;
|
|
18
21
|
}
|
|
@@ -11,10 +11,6 @@ var PacketHolder = /** @class */ (function () {
|
|
|
11
11
|
this.createPackets(packets);
|
|
12
12
|
}
|
|
13
13
|
PacketHolder.prototype.createKey = function (tag) {
|
|
14
|
-
if (tag.includes(",")) {
|
|
15
|
-
console.log("Tag \"".concat(tag, "\" is invalid; keys cannot contain commas."));
|
|
16
|
-
return;
|
|
17
|
-
}
|
|
18
14
|
this.keys[tag] = this.key;
|
|
19
15
|
this.tags[this.key] = tag;
|
|
20
16
|
this.key++;
|
|
@@ -32,15 +28,24 @@ var PacketHolder = /** @class */ (function () {
|
|
|
32
28
|
PacketHolder.prototype.getChar = function (tag) {
|
|
33
29
|
return String.fromCharCode(this.get(tag));
|
|
34
30
|
};
|
|
31
|
+
PacketHolder.prototype.getTag = function (key) {
|
|
32
|
+
return this.tags[key.charCodeAt(0)];
|
|
33
|
+
};
|
|
35
34
|
PacketHolder.prototype.getPacket = function (tag) {
|
|
36
35
|
return this.packetMap[tag];
|
|
37
36
|
};
|
|
38
|
-
PacketHolder.prototype.has = function (
|
|
39
|
-
return
|
|
37
|
+
PacketHolder.prototype.has = function (key) {
|
|
38
|
+
return key.charCodeAt(0) in this.tags;
|
|
39
|
+
};
|
|
40
|
+
PacketHolder.prototype.hasTag = function (tag) {
|
|
41
|
+
return tag in this.keys;
|
|
40
42
|
};
|
|
41
43
|
PacketHolder.prototype.getKeys = function () {
|
|
42
44
|
return this.keys;
|
|
43
45
|
};
|
|
46
|
+
PacketHolder.prototype.getTags = function () {
|
|
47
|
+
return this.tags;
|
|
48
|
+
};
|
|
44
49
|
PacketHolder.prototype.serialize = function () {
|
|
45
50
|
return this.packets.map(function (p) { return p.serialize(); }).join("");
|
|
46
51
|
};
|
|
@@ -3,30 +3,68 @@ import { Packet } from "../packets/Packets";
|
|
|
3
3
|
import { PacketType } from "../packets/PacketType";
|
|
4
4
|
import { EnumPackage } from "../enums/EnumType";
|
|
5
5
|
export declare function emitPacket(packets: PacketHolder, send: (data: string) => void, tag: string, values: any[]): void;
|
|
6
|
+
export type ArguableType = PacketType | EnumPackage;
|
|
7
|
+
/** Settings for single-typed packets */
|
|
8
|
+
export type SinglePacketSettings = {
|
|
9
|
+
/** The tag of the packet; used for on(tag) and send(tag) */
|
|
10
|
+
tag: string;
|
|
11
|
+
/** The data type of the packet; defaults to PacketType.NONE */
|
|
12
|
+
type?: ArguableType;
|
|
13
|
+
/** The maximum amount of values that can be sent through this packet; defaults to 1 */
|
|
14
|
+
dataMax?: number;
|
|
15
|
+
/** The minimum amount of values that can be sent through this packet; defaults to the max */
|
|
16
|
+
dataMin?: number;
|
|
17
|
+
/** If the values should be kept in an array or spread along the listener; defaults to false */
|
|
18
|
+
dontSpread?: boolean;
|
|
19
|
+
};
|
|
20
|
+
/** Settings for multi-typed packets */
|
|
21
|
+
export type MultiPacketSettings = {
|
|
22
|
+
/** The tag of the packet; used for on(tag) and send(tag) */
|
|
23
|
+
tag: string;
|
|
24
|
+
/** The data types of the packet */
|
|
25
|
+
types: ArguableType[];
|
|
26
|
+
/** The maximum amount of values that can be sent through each type of packet; defaults to 1 for each */
|
|
27
|
+
dataMaxes?: number[];
|
|
28
|
+
/** The minimum amount of values that can be sent through each type of packet; defaults to the max for each */
|
|
29
|
+
dataMins?: number[];
|
|
30
|
+
/** If the values should be kept in an array or spread along the listener; defaults to false */
|
|
31
|
+
dontSpread?: boolean;
|
|
32
|
+
};
|
|
33
|
+
/** Settings for single-typed enum packets */
|
|
34
|
+
export type EnumPacketSettings = {
|
|
35
|
+
/** The tag of the packet; used for on(packetTag) and send(packetTag) */
|
|
36
|
+
packetTag: string;
|
|
37
|
+
/** The tag of the enum; used for WrapEnum(enumTag) */
|
|
38
|
+
enumTag: string;
|
|
39
|
+
/** The possible values of the enum */
|
|
40
|
+
values: any[];
|
|
41
|
+
/** The maximum amount of values that can be sent through this packet; defaults to 1 */
|
|
42
|
+
dataMax?: number;
|
|
43
|
+
/** The minimum amount of values that can be sent through this packet; defaults to the max */
|
|
44
|
+
dataMin?: number;
|
|
45
|
+
/** If the values should be kept in an array or spread along the listener; defaults to false */
|
|
46
|
+
dontSpread?: boolean;
|
|
47
|
+
};
|
|
6
48
|
/**
|
|
7
|
-
* Creates a structure for a simple single-typed packet
|
|
8
|
-
*
|
|
9
|
-
* @param
|
|
10
|
-
* @
|
|
11
|
-
* @
|
|
12
|
-
* @returns The packet structure data
|
|
49
|
+
* Creates a structure for a simple single-typed packet.
|
|
50
|
+
* This packet can be sent and received with the specified tag, type, and data cap.
|
|
51
|
+
* @param settings The settings object containing `tag`, `type`, `dataMax`, `dataMin`, and `dontSpread`.
|
|
52
|
+
* @returns The constructed packet structure data.
|
|
53
|
+
* @throws {Error} If the `type` is invalid.
|
|
13
54
|
*/
|
|
14
|
-
export declare function CreatePacket(
|
|
55
|
+
export declare function CreatePacket(settings: SinglePacketSettings): Packet;
|
|
15
56
|
/**
|
|
16
|
-
* Creates a structure for an object (multi-typed) packet
|
|
17
|
-
*
|
|
18
|
-
* @param
|
|
19
|
-
* @
|
|
20
|
-
* @
|
|
21
|
-
* @returns The packet structure data
|
|
57
|
+
* Creates a structure for an object (multi-typed) packet.
|
|
58
|
+
* This packet allows multiple types and their associated data caps.
|
|
59
|
+
* @param settings The settings object containing `tag`, `types`, `dataMaxes`, `dataMins`, and `dontSpread`.
|
|
60
|
+
* @returns The constructed packet structure data.
|
|
61
|
+
* @throws {Error} If any type in `types` is invalid.
|
|
22
62
|
*/
|
|
23
|
-
export declare function CreateObjPacket(
|
|
63
|
+
export declare function CreateObjPacket(settings: MultiPacketSettings): Packet;
|
|
24
64
|
/**
|
|
25
|
-
* Creates and defines an enum packet.
|
|
26
|
-
*
|
|
27
|
-
* @param
|
|
28
|
-
* @
|
|
29
|
-
* @param dataCap The data cap (amount of values that can be sent); defaults to 1
|
|
30
|
-
* @returns The packet structure data
|
|
65
|
+
* Creates and defines an enum packet. This can be used to create an enum-based packet
|
|
66
|
+
* with a specific tag and possible values.
|
|
67
|
+
* @param settings The settings object containing `packetTag`, `enumTag`, `strings` (enum values), `dataMax`, `dataMin`, and `dontSpread`.
|
|
68
|
+
* @returns The constructed packet structure data.
|
|
31
69
|
*/
|
|
32
|
-
export declare function CreateEnumPacket(
|
|
70
|
+
export declare function CreateEnumPacket(settings: EnumPacketSettings): Packet;
|
|
@@ -14,62 +14,89 @@ function emitPacket(packets, send, tag, values) {
|
|
|
14
14
|
if (code == CodePointUtil_1.NULL)
|
|
15
15
|
throw new Error("Tag \"".concat(tag, "\" has not been created!"));
|
|
16
16
|
var packet = packets.getPacket(tag);
|
|
17
|
-
if (values.length > packet.
|
|
18
|
-
throw new Error("Packet \"".concat(tag, "\" only allows ").concat(packet.
|
|
19
|
-
|
|
17
|
+
if (values.length > packet.maxSize)
|
|
18
|
+
throw new Error("Packet \"".concat(tag, "\" only allows ").concat(packet.maxSize, " values!"));
|
|
19
|
+
if (values.length < packet.minSize)
|
|
20
|
+
throw new Error("Packet \"".concat(tag, "\" requires at least ").concat(packet.minSize, " values!"));
|
|
21
|
+
if (!packet.object) {
|
|
22
|
+
var found = values.find(function (v) { return typeof v == 'object' && v != null; });
|
|
23
|
+
if (found)
|
|
24
|
+
console.warn("Passing an array will result in undefined behavior (".concat(JSON.stringify(found), "). Spread the array with ...arr"));
|
|
25
|
+
}
|
|
26
|
+
send(code + (values.length > 0 ? packet.processSend(values) : ""));
|
|
20
27
|
}
|
|
21
28
|
function isValidType(type) {
|
|
22
29
|
return (typeof type == 'number' && type in PacketType_1.PacketType) || type instanceof EnumType_1.EnumPackage;
|
|
23
30
|
}
|
|
24
|
-
function
|
|
25
|
-
if (
|
|
31
|
+
function clampDataMax(dataMax) {
|
|
32
|
+
if (dataMax > CodePointUtil_1.MAX_C) {
|
|
26
33
|
console.warn("Only ".concat(CodePointUtil_1.MAX_C, " values can be sent in a single type! Use CreateObjPacket() if you want to send more."));
|
|
27
34
|
return CodePointUtil_1.MAX_C;
|
|
28
35
|
}
|
|
29
|
-
return
|
|
36
|
+
return dataMax;
|
|
37
|
+
}
|
|
38
|
+
function clampDataMin(dataMin, dataMax) {
|
|
39
|
+
if (dataMin < 0) {
|
|
40
|
+
console.warn("Having a data minimum below 0 does not do anything!");
|
|
41
|
+
return 0;
|
|
42
|
+
}
|
|
43
|
+
// also catches >MAX_C
|
|
44
|
+
if (dataMin > dataMax) {
|
|
45
|
+
console.warn("Data minimum can not be higher than the data maximum!");
|
|
46
|
+
return dataMax;
|
|
47
|
+
}
|
|
48
|
+
return dataMin;
|
|
30
49
|
}
|
|
31
50
|
/**
|
|
32
|
-
* Creates a structure for a simple single-typed packet
|
|
33
|
-
*
|
|
34
|
-
* @param
|
|
35
|
-
* @
|
|
36
|
-
* @
|
|
37
|
-
* @returns The packet structure data
|
|
51
|
+
* Creates a structure for a simple single-typed packet.
|
|
52
|
+
* This packet can be sent and received with the specified tag, type, and data cap.
|
|
53
|
+
* @param settings The settings object containing `tag`, `type`, `dataMax`, `dataMin`, and `dontSpread`.
|
|
54
|
+
* @returns The constructed packet structure data.
|
|
55
|
+
* @throws {Error} If the `type` is invalid.
|
|
38
56
|
*/
|
|
39
|
-
function CreatePacket(
|
|
40
|
-
|
|
41
|
-
if (
|
|
42
|
-
|
|
43
|
-
if (!isValidType(type))
|
|
44
|
-
throw new Error("Invalid packet type: "
|
|
45
|
-
|
|
57
|
+
function CreatePacket(settings) {
|
|
58
|
+
var tag = settings.tag, _a = settings.type, type = _a === void 0 ? PacketType_1.PacketType.NONE : _a, _b = settings.dataMax, dataMax = _b === void 0 ? 1 : _b, dataMin = settings.dataMin, _c = settings.dontSpread, dontSpread = _c === void 0 ? false : _c;
|
|
59
|
+
if (dataMin == undefined)
|
|
60
|
+
dataMin = type == PacketType_1.PacketType.NONE ? 0 : dataMax;
|
|
61
|
+
if (!isValidType(type)) {
|
|
62
|
+
throw new Error("Invalid packet type: ".concat(type));
|
|
63
|
+
}
|
|
64
|
+
return new Packets_1.Packet(tag, Packets_1.PacketSchema.single(type, clampDataMax(dataMax), clampDataMin(dataMin, dataMax), dontSpread), false);
|
|
46
65
|
}
|
|
47
66
|
/**
|
|
48
|
-
* Creates a structure for an object (multi-typed) packet
|
|
49
|
-
*
|
|
50
|
-
* @param
|
|
51
|
-
* @
|
|
52
|
-
* @
|
|
53
|
-
* @returns The packet structure data
|
|
67
|
+
* Creates a structure for an object (multi-typed) packet.
|
|
68
|
+
* This packet allows multiple types and their associated data caps.
|
|
69
|
+
* @param settings The settings object containing `tag`, `types`, `dataMaxes`, `dataMins`, and `dontSpread`.
|
|
70
|
+
* @returns The constructed packet structure data.
|
|
71
|
+
* @throws {Error} If any type in `types` is invalid.
|
|
54
72
|
*/
|
|
55
|
-
function CreateObjPacket(
|
|
56
|
-
|
|
73
|
+
function CreateObjPacket(settings) {
|
|
74
|
+
var tag = settings.tag, types = settings.types, dataMaxes = settings.dataMaxes, dataMins = settings.dataMins, _a = settings.dontSpread, dontSpread = _a === void 0 ? false : _a;
|
|
57
75
|
var invalid = types.find(function (type) { return !isValidType(type); });
|
|
58
|
-
if (invalid)
|
|
59
|
-
throw new Error("Invalid packet type: "
|
|
60
|
-
|
|
61
|
-
|
|
76
|
+
if (invalid) {
|
|
77
|
+
throw new Error("Invalid packet type: ".concat(invalid));
|
|
78
|
+
}
|
|
79
|
+
if (dataMaxes == undefined)
|
|
80
|
+
dataMaxes = Array.from({ length: types.length }).map(function (_) { return 1; });
|
|
81
|
+
if (dataMins == undefined)
|
|
82
|
+
dataMins = Array.from({ length: types.length }).map(function (_, i) { return dataMaxes[i]; });
|
|
83
|
+
var clampedDataMaxes = dataMaxes.map(clampDataMax);
|
|
84
|
+
var clampedDataMins = dataMins.map(function (m, i) { return types[i] == PacketType_1.PacketType.NONE ? 0 : clampDataMin(m, clampedDataMaxes[i]); });
|
|
85
|
+
return new Packets_1.Packet(tag, Packets_1.PacketSchema.object(types, clampedDataMaxes, clampedDataMins, dontSpread), false);
|
|
62
86
|
}
|
|
63
87
|
/**
|
|
64
|
-
* Creates and defines an enum packet.
|
|
65
|
-
*
|
|
66
|
-
* @param
|
|
67
|
-
* @
|
|
68
|
-
* @param dataCap The data cap (amount of values that can be sent); defaults to 1
|
|
69
|
-
* @returns The packet structure data
|
|
88
|
+
* Creates and defines an enum packet. This can be used to create an enum-based packet
|
|
89
|
+
* with a specific tag and possible values.
|
|
90
|
+
* @param settings The settings object containing `packetTag`, `enumTag`, `strings` (enum values), `dataMax`, `dataMin`, and `dontSpread`.
|
|
91
|
+
* @returns The constructed packet structure data.
|
|
70
92
|
*/
|
|
71
|
-
function CreateEnumPacket(
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
93
|
+
function CreateEnumPacket(settings) {
|
|
94
|
+
var packetTag = settings.packetTag, enumTag = settings.enumTag, values = settings.values, _a = settings.dataMax, dataMax = _a === void 0 ? 1 : _a, _b = settings.dataMin, dataMin = _b === void 0 ? 0 : _b, _c = settings.dontSpread, dontSpread = _c === void 0 ? false : _c;
|
|
95
|
+
return CreatePacket({
|
|
96
|
+
tag: packetTag,
|
|
97
|
+
type: (0, EnumHandler_1.DefineEnum)(enumTag, values),
|
|
98
|
+
dataMax: dataMax,
|
|
99
|
+
dataMin: dataMin,
|
|
100
|
+
dontSpread: dontSpread
|
|
101
|
+
});
|
|
75
102
|
}
|