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 CHANGED
@@ -4,11 +4,11 @@
4
4
 
5
5
  WebSocket library focused on bandwidth efficiency and security.
6
6
 
7
- It can reduce packet size by up to 70% or more and process large packets in microseconds.
7
+ It can reduce packet size by up to 70% or more and validate and process large packets in microseconds.
8
8
 
9
9
  It has low latency and optimized data transfer.
10
10
 
11
- While still simple and efficient, SonicWS provides efficient packet transfers.
11
+ Helper functions for frequent cases are included to reduce dev boilerplate.
12
12
 
13
13
  ## SAMPLES
14
14
 
@@ -26,8 +26,8 @@ Browser (Client):
26
26
  ### Server:
27
27
  ```js
28
28
  const wss = new SonicWSServer(
29
- [CreatePacket("pong", PacketType.INTS_D, 1)], // client-sent packets
30
- [CreatePacket("ping", PacketType.INTS_D, 1), CreateObjPacket("data", [PacketType.INTS_A, PacketTypes.STRING], [2, 3])], // server-sent packets
29
+ [CreatePacket({tag: "pong", type: PacketType.INTS_D, dataMax: 1})], // client-sent packets
30
+ [CreatePacket({tag: "ping", type: PacketType.INTS_D, dataMax: 1}), CreateObjPacket({tag: "data", types: [PacketType.INTS_A, PacketTypes.STRING], dataMaxes: [2, 3]})], // server-sent packets
31
31
  { port: 1234 }
32
32
  );
33
33
 
@@ -11,12 +11,16 @@ export declare abstract class SonicWSCore {
11
11
  };
12
12
  protected preListen: {
13
13
  [key: string]: Array<(value: string) => void>;
14
- };
14
+ } | null;
15
15
  protected clientPackets: PacketHolder;
16
16
  protected serverPackets: PacketHolder;
17
17
  private pastKeys;
18
18
  private readyListeners;
19
19
  private keyHandler;
20
+ private rateLimitTimeout;
21
+ private rateLimit;
22
+ private sentPackets;
23
+ private sendQueue;
20
24
  constructor(ws: WebSocket);
21
25
  private serverKeyHandler;
22
26
  private messageHandler;
@@ -1,4 +1,13 @@
1
1
  "use strict";
2
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
3
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
4
+ if (ar || !(i in from)) {
5
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
6
+ ar[i] = from[i];
7
+ }
8
+ }
9
+ return to.concat(ar || Array.prototype.slice.call(from));
10
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
12
  exports.SonicWSCore = void 0;
4
13
  var PacketHolder_1 = require("../../util/PacketHolder");
@@ -14,6 +23,10 @@ var SonicWSCore = /** @class */ (function () {
14
23
  this.serverPackets = PacketHolder_1.PacketHolder.empty();
15
24
  this.pastKeys = false;
16
25
  this.readyListeners = [];
26
+ this.rateLimitTimeout = -1;
27
+ this.rateLimit = -1;
28
+ this.sentPackets = 0;
29
+ this.sendQueue = [];
17
30
  this.ws = ws;
18
31
  this.listeners = {
19
32
  message: [],
@@ -25,6 +38,8 @@ var SonicWSCore = /** @class */ (function () {
25
38
  this.ws.addEventListener('message', this.keyHandler); // lambda to persist 'this'
26
39
  this.ws.addEventListener('close', function (event) {
27
40
  _this.listeners.close.forEach(function (listener) { return listener(event); });
41
+ if (_this.rateLimit != 0 && _this.rateLimitTimeout != -1)
42
+ clearInterval(_this.rateLimitTimeout);
28
43
  });
29
44
  }
30
45
  SonicWSCore.prototype.serverKeyHandler = function (event) {
@@ -39,9 +54,18 @@ var SonicWSCore = /** @class */ (function () {
39
54
  this.ws.close(1003);
40
55
  throw new Error("Version mismatch: ".concat(version > version_1.VERSION ? "client" : "server", " is outdated (server: ").concat(version, ", client: ").concat(version_1.VERSION, ")"));
41
56
  }
42
- var _a = data.substring(4).split(CodePointUtil_1.NULL), ckData = _a[0], skData = _a[1];
43
- this.clientPackets.createPackets(Packets_1.Packet.deserializeAll(ckData));
44
- this.serverPackets.createPackets(Packets_1.Packet.deserializeAll(skData));
57
+ var _a = data.substring(4).split(CodePointUtil_1.NULL), ckData = _a[0], skData = _a[1], rateLimit = _a[2];
58
+ this.clientPackets.createPackets(Packets_1.Packet.deserializeAll(ckData, true));
59
+ this.serverPackets.createPackets(Packets_1.Packet.deserializeAll(skData, true));
60
+ this.rateLimit = rateLimit.charCodeAt(0);
61
+ if (this.rateLimit != 0) {
62
+ this.rateLimitTimeout = setInterval(function () {
63
+ _this.sentPackets = 0;
64
+ var toSend = __spreadArray([], _this.sendQueue, true);
65
+ _this.sendQueue = [];
66
+ toSend.forEach(function (p) { return _this.raw_send(p); });
67
+ }, 1000);
68
+ }
45
69
  Object.keys(this.preListen).forEach(function (tag) { return _this.preListen[tag].forEach(function (listener) {
46
70
  var key = _this.serverPackets.get(tag);
47
71
  // print the error to console without halting execution
@@ -51,10 +75,13 @@ var SonicWSCore = /** @class */ (function () {
51
75
  var packetListener = new PacketListener_1.PacketListener(packet, listener);
52
76
  _this.listen(tag, packetListener);
53
77
  }); });
78
+ this.preListen = null; // clear
54
79
  this.pastKeys = true;
55
80
  this.readyListeners.forEach(function (l) { return l(); });
81
+ this.readyListeners = null; // clear
56
82
  this.ws.removeEventListener('message', this.keyHandler);
57
83
  this.ws.addEventListener('message', function (event) { return _this.messageHandler(event); }); // lambda to persist 'this'
84
+ this.keyHandler = null;
58
85
  };
59
86
  SonicWSCore.prototype.messageHandler = function (event) {
60
87
  var _a;
@@ -65,14 +92,14 @@ var SonicWSCore = /** @class */ (function () {
65
92
  var key = data.substring(0, 1);
66
93
  var value = data.substring(1);
67
94
  var code = key.charCodeAt(0);
68
- if (code != null) {
69
- (_a = this.listeners.event[code]) === null || _a === void 0 ? void 0 : _a.forEach(function (l) {
70
- var result = l.listen(value);
71
- if (!result) {
72
- throw new Error("An error occured with data from the server!! This is probably my fault.. make an issue at https://github.com/cutelittlelily/sonic-ws");
73
- }
74
- });
95
+ if (code == null)
96
+ return;
97
+ var result = this.serverPackets.getPacket(this.serverPackets.getTag(key)).listen(value);
98
+ if (result == null) {
99
+ throw new Error("An error occured with data from the server!! This is probably my fault.. make an issue at https://github.com/cutelittlelily/sonic-ws");
75
100
  }
101
+ var processed = result[0], isArray = result[1];
102
+ (_a = this.listeners.event[code]) === null || _a === void 0 ? void 0 : _a.forEach(function (l) { return l.listen(processed, isArray); });
76
103
  };
77
104
  SonicWSCore.prototype.listen = function (key, listener) {
78
105
  var skey = this.serverPackets.get(key);
@@ -88,6 +115,12 @@ var SonicWSCore = /** @class */ (function () {
88
115
  this.listeners.message.push(listener);
89
116
  };
90
117
  SonicWSCore.prototype.raw_send = function (data) {
118
+ if (this.rateLimit == -1)
119
+ return console.error("A rate limit has not been received by the server!");
120
+ if (this.rateLimit != 0 && ++this.sentPackets > this.rateLimit) {
121
+ this.sendQueue.push(data);
122
+ return console.warn("Client is emitting more packets than the rate limit! Current queue size: ".concat(this.sendQueue.length));
123
+ }
91
124
  this.ws.send(data);
92
125
  };
93
126
  SonicWSCore.prototype.send = function (tag) {
@@ -1,20 +1,20 @@
1
- import { EnumPackage, EnumValue } from "./EnumType";
1
+ import { EnumPackage } from "./EnumType";
2
2
  export declare const MAX_ENUM_SIZE = 128;
3
- export declare const ENUM_TAG_TO_KEY: Record<string, Record<string, number>>;
4
- export declare const ENUM_KEY_TO_TAG: Record<string, Record<number, string>>;
3
+ export declare const ENUM_TAG_TO_KEY: Record<string, Record<any, number>>;
4
+ export declare const ENUM_KEY_TO_TAG: Record<string, Record<number, any>>;
5
5
  /**
6
6
  * Defines an enum with its tag and values
7
7
  * @param tag The tag of the enum; used for WrapEnum(tag, ...)
8
8
  * @param values The possible values of the enum
9
9
  * @returns A packaged enum
10
10
  */
11
- export declare function DefineEnum(tag: string, values: string[]): EnumPackage;
11
+ export declare function DefineEnum(tag: string, values: any[]): EnumPackage;
12
12
  /**
13
13
  * Wraps an enum into a transmittable format
14
14
  * @param tag The tag of the enum
15
15
  * @param value The value to send
16
16
  * @returns A transmittable enum value
17
17
  */
18
- export declare function WrapEnum(tag: string, value: string): EnumValue;
18
+ export declare function WrapEnum(tag: string, value: any): string;
19
19
  export declare function fromIndex(tag: string, index: number): string;
20
20
  export declare function fromEncoded(tag: string, encoded: string): string;
@@ -31,7 +31,7 @@ function DefineEnum(tag, values) {
31
31
  function WrapEnum(tag, value) {
32
32
  if (!(value in exports.ENUM_TAG_TO_KEY[tag]))
33
33
  throw new Error("Value \"".concat(value, "\" does not exist in enum \"").concat(tag, "\""));
34
- return new EnumType_1.EnumValue(tag, exports.ENUM_TAG_TO_KEY[tag][value]);
34
+ return String.fromCharCode(exports.ENUM_TAG_TO_KEY[tag][value]);
35
35
  }
36
36
  function fromIndex(tag, index) {
37
37
  return exports.ENUM_KEY_TO_TAG[tag][index];
@@ -1,12 +1,7 @@
1
- export declare class EnumValue {
2
- tag: string;
3
- index: number;
4
- encoded: string;
5
- constructor(tag: string, index: number);
6
- }
1
+ export declare const TYPE_CONVERSION_MAP: Record<number, (data: string) => string | number | boolean | undefined | null>;
7
2
  export declare class EnumPackage {
8
3
  tag: string;
9
- values: string[];
10
- constructor(tag: string, values: string[]);
4
+ values: any[];
5
+ constructor(tag: string, values: any[]);
11
6
  serialize(): string;
12
7
  }
@@ -1,22 +1,41 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.EnumPackage = exports.EnumValue = void 0;
4
- var EnumValue = /** @class */ (function () {
5
- function EnumValue(tag, index) {
6
- this.tag = tag;
7
- this.index = index;
8
- this.encoded = String.fromCharCode(index);
9
- }
10
- return EnumValue;
11
- }());
12
- exports.EnumValue = EnumValue;
3
+ exports.EnumPackage = exports.TYPE_CONVERSION_MAP = void 0;
4
+ var TYPE_INDEX_MAP = {
5
+ 'string': 0,
6
+ 'number': 1,
7
+ 'boolean': 2,
8
+ 'undefined': 3,
9
+ 'object': 4, // to handle null bug
10
+ };
11
+ exports.TYPE_CONVERSION_MAP = {
12
+ 0: function (d) { return d; },
13
+ 1: function (d) { return parseFloat(d); },
14
+ 2: function (d) { return d == 'true'; },
15
+ 3: function () { return undefined; },
16
+ 4: function () { return null; },
17
+ };
18
+ function getTypedIndex(data) {
19
+ var type = typeof data;
20
+ if (!(type in TYPE_INDEX_MAP) && data != null)
21
+ throw new Error("Cannot serialize type \"".concat(type, "\" in an enum!"));
22
+ return TYPE_INDEX_MAP[type];
23
+ }
13
24
  var EnumPackage = /** @class */ (function () {
14
25
  function EnumPackage(tag, values) {
15
26
  this.tag = tag;
16
27
  this.values = values;
17
28
  }
18
29
  EnumPackage.prototype.serialize = function () {
19
- return String.fromCharCode(this.tag.length + 1) + this.tag + String.fromCharCode(this.values.length + 1) + this.values.map(function (v) { return String.fromCharCode(v.length + 1) + v; }).join("");
30
+ return String.fromCharCode(this.tag.length + 1) + // tag length
31
+ this.tag + // tag
32
+ String.fromCharCode(this.values.length + 1) + // value count
33
+ this.values.map(function (v) {
34
+ return String.fromCharCode(String(v).length + 1) + // value length
35
+ String.fromCharCode(getTypedIndex(v) + 1) + // value type
36
+ v;
37
+ } // value
38
+ ).join("");
20
39
  };
21
40
  return EnumPackage;
22
41
  }());
@@ -3,5 +3,5 @@ export declare class PacketListener {
3
3
  private listener;
4
4
  private packet;
5
5
  constructor(packet: Packet, listener: (...data: any[]) => void);
6
- listen(value: string): boolean;
6
+ listen(processed: any, isArray: boolean): boolean;
7
7
  }
@@ -6,18 +6,8 @@ var PacketListener = /** @class */ (function () {
6
6
  this.listener = listener;
7
7
  this.packet = packet;
8
8
  }
9
- PacketListener.prototype.listen = function (value) {
10
- var processed;
11
- try {
12
- if (!this.packet.validate(value))
13
- return false;
14
- processed = this.packet.processReceive(value);
15
- }
16
- catch (err) {
17
- console.error("There was an error processing the packet! This is probably my fault... report at https://github.com/cutelittlelily/sonic-ws", err);
18
- return false;
19
- }
20
- if (Array.isArray(processed) && !this.packet.dontSpread)
9
+ PacketListener.prototype.listen = function (processed, isArray) {
10
+ if (isArray && !this.packet.dontSpread)
21
11
  this.listener.apply(this, processed);
22
12
  else
23
13
  this.listener(processed);
@@ -22,7 +22,7 @@ var LEN_DELIMIT = function (data, cap) {
22
22
  return true;
23
23
  };
24
24
  exports.PacketValidityProcessors = (_a = {},
25
- _a[PacketType_1.PacketType.NONE] = function (data) { return data == ""; },
25
+ _a[PacketType_1.PacketType.NONE] = function (data) { return data.length == 0; },
26
26
  _a[PacketType_1.PacketType.RAW] = function () { return true; },
27
27
  _a[PacketType_1.PacketType.STRINGS] = LEN_DELIMIT,
28
28
  _a[PacketType_1.PacketType.ENUMS] = function (data, cap, packet, index) {
@@ -40,15 +40,16 @@ exports.PacketValidityProcessors = (_a = {},
40
40
  _a[PacketType_1.PacketType.INTS_A] = LEN_DELIMIT,
41
41
  _a[PacketType_1.PacketType.DECIMALS] = function (data, cap) {
42
42
  var sectors = 0;
43
- for (var i = 0; i < data.length; i++) {
43
+ for (var i = 0; i < data.length;) {
44
44
  sectors++;
45
45
  if (sectors > cap)
46
46
  return false;
47
- var len = data.charCodeAt(i);
48
- i += len + 1;
47
+ var sectorBits = data.charCodeAt(i++);
48
+ var len = sectorBits >> 7;
49
+ var len2 = sectorBits & 0x7F;
50
+ i += len;
49
51
  if (i > data.length)
50
52
  return false;
51
- var len2 = data.charCodeAt(i);
52
53
  i += len2;
53
54
  if (i > data.length)
54
55
  return false;
@@ -91,10 +92,11 @@ exports.PacketReceiveProcessors = (_b = {},
91
92
  var points = (0, CodePointUtil_1.processCharCodes)(data);
92
93
  var numbers = [];
93
94
  for (var i = 0; i < points.length;) {
94
- var wholeSS = points[i++];
95
+ var sectorBits = points[i++];
96
+ var wholeSS = sectorBits >> 7;
97
+ var decimalSS = sectorBits & 0x7F;
95
98
  var whole = (0, CodePointUtil_1.deconvertINT_DCodes)(points.slice(i, i + wholeSS));
96
99
  i += wholeSS;
97
- var decimalSS = points[i++];
98
100
  var decimal = (0, CodePointUtil_1.deconvertINT_DCodes)(points.slice(i, i + decimalSS));
99
101
  i += decimalSS;
100
102
  numbers.push(parseFloat(whole + "." + decimal));
@@ -119,7 +121,7 @@ exports.PacketSendProcessors = (_c = {},
119
121
  for (var _i = 0; _i < arguments.length; _i++) {
120
122
  enums[_i] = arguments[_i];
121
123
  }
122
- return enums.map(function (v) { return v.encoded; }).join("");
124
+ return enums.join("");
123
125
  },
124
126
  _c[PacketType_1.PacketType.INTS_C] = function () {
125
127
  var numbers = [];
@@ -158,7 +160,8 @@ exports.PacketSendProcessors = (_c = {},
158
160
  var decimal = split.length > 1 ? parseFloat(split[1]) || 0 : 0;
159
161
  var wholeSS = (0, CodePointUtil_1.sectorSize)(whole);
160
162
  var decimalSS = (0, CodePointUtil_1.sectorSize)(decimal);
161
- return String.fromCharCode(wholeSS) + (0, CodePointUtil_1.convertINT_D)(whole, wholeSS) + String.fromCharCode(decimalSS) + (0, CodePointUtil_1.convertINT_D)(decimal, decimalSS);
163
+ var num = (wholeSS << 7) | decimalSS;
164
+ return String.fromCharCode(num) + (0, CodePointUtil_1.convertINT_D)(whole, wholeSS) + (0, CodePointUtil_1.convertINT_D)(decimal, decimalSS);
162
165
  }).join("");
163
166
  },
164
167
  _c[PacketType_1.PacketType.BOOLEANS] = function () {
@@ -1,3 +1,4 @@
1
+ /** All different packet types. Use ENUMS for any constant primitive data */
1
2
  export declare enum PacketType {
2
3
  /** No data */
3
4
  NONE = 0,
@@ -5,7 +6,7 @@ export declare enum PacketType {
5
6
  RAW = 1,
6
7
  /** String data; use ENUMS if the values are constant */
7
8
  STRINGS = 2,
8
- /** Constant strings */
9
+ /** Constant primitive data; strings, numbers, booleans, null, undefined */
9
10
  ENUMS = 3,
10
11
  /** One or more numbers from -27,648 to 27,647 */
11
12
  INTS_C = 4,
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
+ // separated file to allow imports
2
3
  Object.defineProperty(exports, "__esModule", { value: true });
3
4
  exports.PacketType = void 0;
4
- // separated file to allow imports
5
+ /** All different packet types. Use ENUMS for any constant primitive data */
5
6
  var PacketType;
6
7
  (function (PacketType) {
7
8
  /** No data */
@@ -10,7 +11,7 @@ var PacketType;
10
11
  PacketType[PacketType["RAW"] = 1] = "RAW";
11
12
  /** String data; use ENUMS if the values are constant */
12
13
  PacketType[PacketType["STRINGS"] = 2] = "STRINGS";
13
- /** Constant strings */
14
+ /** Constant primitive data; strings, numbers, booleans, null, undefined */
14
15
  PacketType[PacketType["ENUMS"] = 3] = "ENUMS";
15
16
  /** One or more numbers from -27,648 to 27,647 */
16
17
  PacketType[PacketType["INTS_C"] = 4] = "INTS_C";
@@ -2,11 +2,13 @@ import { EnumPackage } from "../enums/EnumType";
2
2
  import { PacketType } from "./PacketType";
3
3
  export declare class Packet {
4
4
  tag: string;
5
- size: number;
5
+ maxSize: number;
6
+ minSize: number;
6
7
  type: PacketType | PacketType[];
7
- dataCap: number | number[];
8
- dontSpread: boolean;
9
8
  enumData: EnumPackage[];
9
+ dataMax: number | number[];
10
+ dataMin: number | number[];
11
+ dontSpread: boolean;
10
12
  object: boolean;
11
13
  private receiveProcessor;
12
14
  private sendProcessor;
@@ -14,20 +16,23 @@ export declare class Packet {
14
16
  processReceive: (data: string) => any;
15
17
  processSend: (data: any[]) => string;
16
18
  validate: (data: string) => boolean;
17
- constructor(tag: string, schema: PacketSchema);
19
+ constructor(tag: string, schema: PacketSchema, client: boolean);
20
+ listen(value: string): [processed: any, isArray: boolean] | null;
18
21
  serialize(): string;
19
- static deserialize(text: string, offset: number): [packet: Packet, offset: number];
20
- static deserializeAll(text: string): Packet[];
22
+ static deserialize(text: string, offset: number, client: boolean): [packet: Packet, offset: number];
23
+ static deserializeAll(text: string, client: boolean): Packet[];
21
24
  }
22
25
  export declare class PacketSchema {
23
26
  types: PacketType[];
24
- dataCaps: number[];
25
- enumData: EnumPackage[];
27
+ dataMaxes: number[];
28
+ dataMins: number[];
26
29
  type: PacketType;
27
- dataCap: number;
30
+ dataMax: number;
31
+ dataMin: number;
32
+ enumData: EnumPackage[];
28
33
  dontSpread: boolean;
29
34
  object: boolean;
30
35
  constructor(object: boolean);
31
- static single(type: PacketType | EnumPackage, dataCap: number, dontSpread: boolean): PacketSchema;
32
- static object(types: (PacketType | EnumPackage)[], dataCaps: number[], dontSpread: boolean): PacketSchema;
36
+ static single(type: PacketType | EnumPackage, dataMax: number, dataMin: number, dontSpread: boolean): PacketSchema;
37
+ static object(types: (PacketType | EnumPackage)[], dataMaxes: number[], dataMins: number[], dontSpread: boolean): PacketSchema;
33
38
  }
@@ -2,17 +2,20 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PacketSchema = exports.Packet = void 0;
4
4
  var EnumHandler_1 = require("../enums/EnumHandler");
5
+ var EnumType_1 = require("../enums/EnumType");
5
6
  var CodePointUtil_1 = require("../util/CodePointUtil");
6
7
  var PacketProcessors_1 = require("./PacketProcessors");
7
8
  var PacketType_1 = require("./PacketType");
8
9
  var Packet = /** @class */ (function () {
9
- function Packet(tag, schema) {
10
+ function Packet(tag, schema, client) {
10
11
  var _this = this;
11
12
  this.tag = tag;
12
13
  if (schema.object) {
13
14
  this.type = schema.types;
14
- this.dataCap = schema.dataCaps;
15
- this.size = this.type.length;
15
+ this.dataMax = schema.dataMaxes;
16
+ this.dataMin = schema.dataMins;
17
+ this.maxSize = this.type.length;
18
+ this.minSize = this.type.length;
16
19
  this.object = true;
17
20
  this.receiveProcessor = (0, PacketProcessors_1.createObjReceiveProcesor)(this.type);
18
21
  this.validifier = (0, PacketProcessors_1.createObjValidator)(this.type);
@@ -27,8 +30,10 @@ var Packet = /** @class */ (function () {
27
30
  }
28
31
  else {
29
32
  this.type = schema.type;
30
- this.dataCap = schema.dataCap;
31
- this.size = this.dataCap;
33
+ this.dataMax = schema.dataMax;
34
+ this.dataMin = schema.dataMin;
35
+ this.maxSize = this.dataMax;
36
+ this.minSize = this.dataMin;
32
37
  this.object = false;
33
38
  this.receiveProcessor = PacketProcessors_1.PacketReceiveProcessors[this.type];
34
39
  this.validifier = PacketProcessors_1.PacketValidityProcessors[this.type];
@@ -41,11 +46,27 @@ var Packet = /** @class */ (function () {
41
46
  return _this.sendProcessor.apply(_this, data.flat());
42
47
  };
43
48
  }
44
- this.processReceive = function (data) { return _this.receiveProcessor(data, _this.dataCap, _this, 0); };
45
- this.validate = function (data) { return _this.validifier(data, _this.dataCap, _this, 0); };
49
+ this.processReceive = function (data) { return _this.receiveProcessor(data, _this.dataMax, _this, 0); };
50
+ this.validate = client ? function () { return true; } : function (data) { return _this.validifier(data, _this.dataMax, _this, 0); };
46
51
  this.enumData = schema.enumData;
47
52
  this.dontSpread = schema.dontSpread;
48
53
  }
54
+ Packet.prototype.listen = function (value) {
55
+ var processed, isArray;
56
+ try {
57
+ if (!this.validate(value))
58
+ return null;
59
+ processed = this.processReceive(value);
60
+ isArray = Array.isArray(processed);
61
+ if (isArray && processed.length < this.dataMin)
62
+ return null;
63
+ }
64
+ catch (err) {
65
+ console.error("There was an error processing the packet! This is probably my fault... report at https://github.com/cutelittlelily/sonic-ws", err);
66
+ return null;
67
+ }
68
+ return [processed, isArray];
69
+ };
49
70
  Packet.prototype.serialize = function () {
50
71
  // spread flag; ETX for "2", STX for "1", avoid NULL for delimiting
51
72
  var spreadFlag = this.dontSpread ? CodePointUtil_1.ETX : CodePointUtil_1.STX;
@@ -55,23 +76,26 @@ var Packet = /** @class */ (function () {
55
76
  if (!this.object) {
56
77
  return spreadFlag + enumData +
57
78
  CodePointUtil_1.STX + // dummy byte flag for consistent deserialization; becomes -1 to indicate single
58
- String.fromCharCode(this.dataCap + 1) + // the data cap, offset by 1 for NULL
79
+ String.fromCharCode(this.dataMax + 1) + // the data max, offset by 1 for NULL
80
+ String.fromCharCode(this.dataMin + 1) + // the data min, offset by 1 for NULL
59
81
  String.fromCharCode(this.type + 1) + // the type, offset by 1 for NULL
60
82
  String.fromCharCode(this.tag.length + 1) + // tag length, offset by 1 for NULL
61
83
  this.tag; // the tag
62
84
  }
63
85
  // object packet
64
86
  return spreadFlag + enumData +
65
- String.fromCharCode(this.size + 2) + // size, and +2 because of NULL and STX (STX is for single)
87
+ String.fromCharCode(this.maxSize + 2) + // size, and +2 because of NULL and STX (STX is for single)
66
88
  String.fromCharCode.apply(// size, and +2 because of NULL and STX (STX is for single)
67
- String, this.dataCap.map(function (x) { return x + 1; })) + // all data caps, offset by 1 for NULL
68
- String.fromCharCode.apply(// all data caps, offset by 1 for NULL
89
+ String, this.dataMax.map(function (x) { return x + 1; })) + // all data maxes, offset by 1 for NULL
90
+ String.fromCharCode.apply(// all data maxes, offset by 1 for NULL
91
+ String, this.dataMin.map(function (x) { return x + 1; })) + // all data mins, offset by 1 for NULL
92
+ String.fromCharCode.apply(// all data mins, offset by 1 for NULL
69
93
  String, this.type.map(function (x) { return x + 1; })) + // all types, offset by 1 for NULL
70
94
  String.fromCharCode(this.tag.length + 1) + // tag length, offset by 1 for NULL
71
95
  this.tag; // the tag
72
96
  };
73
97
  // i think i was high when i made these
74
- Packet.deserialize = function (text, offset) {
98
+ Packet.deserialize = function (text, offset, client) {
75
99
  var beginningOffset = offset;
76
100
  var dontSpread = text[offset] == CodePointUtil_1.ETX;
77
101
  var enumLength = text.charCodeAt(++offset) - 1;
@@ -84,9 +108,10 @@ var Packet = /** @class */ (function () {
84
108
  var values = [];
85
109
  for (var j = 0; j < valueCount; j++) {
86
110
  var valueLength = text.charCodeAt(++offset) - 1;
111
+ var valueType = text.charCodeAt(++offset) - 1;
87
112
  var value = text.slice(++offset, offset += valueLength);
88
113
  offset--;
89
- values.push(value);
114
+ values.push(EnumType_1.TYPE_CONVERSION_MAP[valueType](value));
90
115
  }
91
116
  enums.push((0, EnumHandler_1.DefineEnum)(enumTag, values));
92
117
  }
@@ -94,39 +119,43 @@ var Packet = /** @class */ (function () {
94
119
  // objects
95
120
  // the single packet is STX so STX - 2 = -1
96
121
  if (size != -1) {
97
- var dcStart = ++offset; // data caps section start
98
- var dcEnd = dcStart + size; // data caps section end
99
- var tStart = dcEnd; // types section start
122
+ var dcStart = ++offset; // data maxes section start
123
+ var dcEnd = dcStart + size; // data maxes section end
124
+ var dmStart = dcEnd; // data mins section start
125
+ var dmEnd = dmStart + size; // data mins section end
126
+ var tStart = dmEnd; // types section start
100
127
  var tEnd = tStart + size; // types section end
101
128
  var tagStart_1 = tEnd + 1; // tag string starts after tag length byte
102
- var dataCaps = (0, CodePointUtil_1.processCharCodes)(text.substring(dcStart, dcEnd)).map(function (x) { return x - 1; }); // subtract 1 to reverse
129
+ var dataMaxes = (0, CodePointUtil_1.processCharCodes)(text.substring(dcStart, dcEnd)).map(function (x) { return x - 1; }); // subtract 1 to reverse
130
+ var dataMins = (0, CodePointUtil_1.processCharCodes)(text.substring(dmStart, dmEnd)).map(function (x) { return x - 1; }); // subtract 1 to reverse
103
131
  var types = (0, CodePointUtil_1.processCharCodes)(text.substring(tStart, tEnd)).map(function (x) { return x - 1; }); // subtract 1 to reverse
104
132
  var index_1 = 0;
105
133
  var finalTypes = types.map(function (x) { return x == PacketType_1.PacketType.ENUMS ? enums[index_1++] : x; }); // convert enums to their enum packages
106
134
  var tagLength_1 = text.charCodeAt(tagStart_1 - 1) - 1; // tag length is right behind tag, subtracting 1 to reverse
107
135
  var tag_1 = text.substring(tagStart_1, tagStart_1 + tagLength_1); // tag is tag length long. yeah
108
136
  return [
109
- new Packet(tag_1, PacketSchema.object(finalTypes, dataCaps, dontSpread)),
110
- (offset - beginningOffset) + 1 + size + size + tagLength_1
137
+ new Packet(tag_1, PacketSchema.object(finalTypes, dataMaxes, dataMins, dontSpread), client),
138
+ (offset - beginningOffset) + 1 + size + size + size + tagLength_1
111
139
  ];
112
140
  }
113
141
  // single packet; subtracting 1 to revere.
114
- var dataCap = text.charCodeAt(++offset) - 1;
142
+ var dataMax = text.charCodeAt(++offset) - 1;
143
+ var dataMin = text.charCodeAt(++offset) - 1;
115
144
  var type = (text.charCodeAt(++offset) - 1);
116
145
  var finalType = type == PacketType_1.PacketType.ENUMS ? enums[0] : type; // convert enum to enum package
117
146
  var tagStart = ++offset;
118
147
  var tagLength = text.charCodeAt(tagStart) - 1;
119
148
  var tag = text.substring(tagStart + 1, tagStart + 1 + tagLength);
120
149
  return [
121
- new Packet(tag, PacketSchema.single(finalType, dataCap, dontSpread)),
150
+ new Packet(tag, PacketSchema.single(finalType, dataMax, dataMin, dontSpread), client),
122
151
  (offset - beginningOffset) + 1 + tagLength
123
152
  ];
124
153
  };
125
- Packet.deserializeAll = function (text) {
154
+ Packet.deserializeAll = function (text, client) {
126
155
  var arr = [];
127
156
  var offset = 0;
128
157
  while (offset < text.length) {
129
- var _a = this.deserialize(text, offset), packet = _a[0], len = _a[1];
158
+ var _a = this.deserialize(text, offset, client), packet = _a[0], len = _a[1];
130
159
  arr.push(packet);
131
160
  offset += len;
132
161
  }
@@ -138,14 +167,16 @@ exports.Packet = Packet;
138
167
  var PacketSchema = /** @class */ (function () {
139
168
  function PacketSchema(object) {
140
169
  this.types = [];
141
- this.dataCaps = [];
142
- this.enumData = [];
170
+ this.dataMaxes = [];
171
+ this.dataMins = [];
143
172
  this.type = PacketType_1.PacketType.NONE;
144
- this.dataCap = -1;
173
+ this.dataMax = -1;
174
+ this.dataMin = -1;
175
+ this.enumData = [];
145
176
  this.dontSpread = false;
146
177
  this.object = object;
147
178
  }
148
- PacketSchema.single = function (type, dataCap, dontSpread) {
179
+ PacketSchema.single = function (type, dataMax, dataMin, dontSpread) {
149
180
  var schema = new PacketSchema(false);
150
181
  if (typeof type == 'number') {
151
182
  schema.type = type;
@@ -154,13 +185,14 @@ var PacketSchema = /** @class */ (function () {
154
185
  schema.type = PacketType_1.PacketType.ENUMS;
155
186
  schema.enumData = [type];
156
187
  }
157
- schema.dataCap = dataCap;
188
+ schema.dataMax = dataMax;
189
+ schema.dataMin = dataMin;
158
190
  schema.dontSpread = dontSpread;
159
191
  return schema;
160
192
  };
161
- PacketSchema.object = function (types, dataCaps, dontSpread) {
162
- if (types.length != dataCaps.length)
163
- throw new Error("There is an inbalance between types and datacaps!");
193
+ PacketSchema.object = function (types, dataMaxes, dataMins, dontSpread) {
194
+ if (types.length != dataMaxes.length || types.length != dataMins.length)
195
+ throw new Error("There is an inbalance between the amount of types, data maxes, and data mins!");
164
196
  var schema = new PacketSchema(true);
165
197
  types.forEach(function (type) {
166
198
  if (typeof type == 'number') {
@@ -171,7 +203,8 @@ var PacketSchema = /** @class */ (function () {
171
203
  schema.enumData.push(type);
172
204
  }
173
205
  });
174
- schema.dataCaps = dataCaps;
206
+ schema.dataMaxes = dataMaxes;
207
+ schema.dataMins = dataMins;
175
208
  schema.dontSpread = dontSpread;
176
209
  return schema;
177
210
  };