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.
@@ -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 text.split("").map(function (v) { return v.charCodeAt(0); });
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 = exports.OVERFLOW;
69
+ var num = overflowPow(1);
67
70
  while (number >= num) {
68
71
  count++;
69
- num *= exports.OVERFLOW;
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 string = "";
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 = Math.pow(exports.OVERFLOW, chars - i - 1);
88
+ var power = overflowPow(chars - i - 1);
86
89
  var based = Math.floor(number / power);
87
- string += String.fromCharCode(based);
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
- string += String.fromCharCode(number % exports.OVERFLOW);
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 ? processCharCodes(string)
96
- .map(function (part) { return String.fromCharCode(part > 0 ? part + exports.NEGATIVE_C : part); })
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(data: string): boolean;
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 (data) {
39
- return this.tags[data.charCodeAt(0)] != null;
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
- * @param tag The tag of the packet; for on(tag) and send(tag, ...)
9
- * @param type The packet type; defaults to none
10
- * @param dataCap The data cap (amount of values that can be sent); defaults to 1
11
- * @param dontSpread If true, the values will be kept in an array instead of spread
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(tag: string, type?: (PacketType | EnumPackage), dataCap?: number, dontSpread?: boolean): Packet;
55
+ export declare function CreatePacket(settings: SinglePacketSettings): Packet;
15
56
  /**
16
- * Creates a structure for an object (multi-typed) packet
17
- * @param tag The tag of the packet; for on(tag) and send(tag, ...)
18
- * @param types The types in the packet, in order
19
- * @param dataCaps The data cap (amount of values that can be sent) for each type, in order
20
- * @param dontSpread If true, the values will be kept in an array instead of spread
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(tag: string, types: (PacketType | EnumPackage)[], dataCaps: number[], dontSpread?: boolean): Packet;
63
+ export declare function CreateObjPacket(settings: MultiPacketSettings): Packet;
24
64
  /**
25
- * Creates and defines an enum packet. In an object, you can do DefineEnum() for the type
26
- * @param packetTag The tag of the packet; for on(tag) and send(tag, ...)
27
- * @param enumTag The tag of the enum; for send(tag, WrapEnum(enumTag, value))
28
- * @param strings The possible values of the enum
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(packetTag: string, enumTag: string, strings: string[], dataCap?: number, dontSpread?: boolean): Packet;
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.size)
18
- throw new Error("Packet \"".concat(tag, "\" only allows ").concat(packet.size, " values!"));
19
- send(code + packet.processSend(values));
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 clampDataCap(dataCap) {
25
- if (dataCap > CodePointUtil_1.MAX_C) {
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 dataCap;
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
- * @param tag The tag of the packet; for on(tag) and send(tag, ...)
34
- * @param type The packet type; defaults to none
35
- * @param dataCap The data cap (amount of values that can be sent); defaults to 1
36
- * @param dontSpread If true, the values will be kept in an array instead of spread
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(tag, type, dataCap, dontSpread) {
40
- if (type === void 0) { type = PacketType_1.PacketType.NONE; }
41
- if (dataCap === void 0) { dataCap = 1; }
42
- if (dontSpread === void 0) { dontSpread = false; }
43
- if (!isValidType(type))
44
- throw new Error("Invalid packet type: " + type);
45
- return new Packets_1.Packet(tag, Packets_1.PacketSchema.single(type, clampDataCap(dataCap), dontSpread));
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
- * @param tag The tag of the packet; for on(tag) and send(tag, ...)
50
- * @param types The types in the packet, in order
51
- * @param dataCaps The data cap (amount of values that can be sent) for each type, in order
52
- * @param dontSpread If true, the values will be kept in an array instead of spread
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(tag, types, dataCaps, dontSpread) {
56
- if (dontSpread === void 0) { dontSpread = false; }
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: " + invalid);
60
- dataCaps = dataCaps.map(clampDataCap);
61
- return new Packets_1.Packet(tag, Packets_1.PacketSchema.object(types, dataCaps, dontSpread));
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. In an object, you can do DefineEnum() for the type
65
- * @param packetTag The tag of the packet; for on(tag) and send(tag, ...)
66
- * @param enumTag The tag of the enum; for send(tag, WrapEnum(enumTag, value))
67
- * @param strings The possible values of the enum
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(packetTag, enumTag, strings, dataCap, dontSpread) {
72
- if (dataCap === void 0) { dataCap = 1; }
73
- if (dontSpread === void 0) { dontSpread = false; }
74
- return CreatePacket(packetTag, (0, EnumHandler_1.DefineEnum)(enumTag, strings), dataCap, dontSpread);
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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sonic-ws",
3
- "version": "1.0.0-beta.9",
3
+ "version": "1.0.0-rc.0-patch",
4
4
  "description": "A bandwidth efficient websocket library",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",