sonic-ws 1.0.0-rc.0-patch → 1.0.0-rc.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 CHANGED
@@ -25,11 +25,16 @@ Browser (Client):
25
25
 
26
26
  ### Server:
27
27
  ```js
28
- const wss = new SonicWSServer(
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
- { port: 1234 }
32
- );
28
+ const wss = new SonicWSServer({
29
+ clientPackets: [
30
+ CreatePacket({tag: "pong", type: PacketType.INTS_D, dataMax: 1})
31
+ ],
32
+ serverPackets: [
33
+ CreatePacket({tag: "ping", type: PacketType.INTS_D, dataMax: 1}),
34
+ CreateObjPacket({tag: "data", types: [PacketType.INTS_A, PacketTypes.STRING], dataMaxes: [2, 3]})
35
+ ],
36
+ websocketOptions: { port: 1234 }
37
+ });
33
38
 
34
39
  wss.on_connect((socket) => {
35
40
 
@@ -75,12 +80,12 @@ ws.on_close((event) => {
75
80
 
76
81
  ## KNOWN ISSUES
77
82
 
78
- Can't send 0 values through stuff like STRINGS; processes as [""] instead of []
79
-
80
83
  ## PLANNED FEATURES
81
84
 
82
85
  More data checking and better error handling
83
86
 
84
87
  Layered object packets
85
88
 
86
- JSDoc'ing stuff
89
+ JSDoc'ing stuff
90
+
91
+ Possibilities of nested objects; however these are inefficient to do than just a flat one
@@ -1,16 +1,15 @@
1
1
  import { PacketHolder } from '../../util/PacketHolder';
2
- import { PacketListener } from '../../packets/PacketListener';
3
2
  export declare abstract class SonicWSCore {
4
3
  protected ws: WebSocket;
5
4
  protected listeners: {
6
5
  message: Array<(data: string) => void>;
7
6
  close: Array<(event: CloseEvent) => void>;
8
7
  event: {
9
- [key: string]: Array<PacketListener>;
8
+ [key: string]: Array<(...data: any[]) => void>;
10
9
  };
11
10
  };
12
11
  protected preListen: {
13
- [key: string]: Array<(value: string) => void>;
12
+ [key: string]: Array<(data: any[]) => void>;
14
13
  } | null;
15
14
  protected clientPackets: PacketHolder;
16
15
  protected serverPackets: PacketHolder;
@@ -24,11 +23,11 @@ export declare abstract class SonicWSCore {
24
23
  constructor(ws: WebSocket);
25
24
  private serverKeyHandler;
26
25
  private messageHandler;
27
- protected listen(key: string, listener: PacketListener): void;
26
+ protected listen(key: string, listener: (data: any[]) => void): void;
28
27
  raw_onmessage(listener: (data: string) => void): void;
29
28
  raw_send(data: string): void;
30
29
  send(tag: string, ...values: any[]): void;
31
30
  on_ready(listener: () => void): void;
32
31
  on_close(listener: (event: CloseEvent) => void): void;
33
- on(tag: string, listener: (value: string) => void): void;
32
+ on(tag: string, listener: (value: any[]) => void): void;
34
33
  }
@@ -11,7 +11,6 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.SonicWSCore = void 0;
13
13
  var PacketHolder_1 = require("../../util/PacketHolder");
14
- var PacketListener_1 = require("../../packets/PacketListener");
15
14
  var CodePointUtil_1 = require("../../util/CodePointUtil");
16
15
  var PacketUtils_1 = require("../../util/PacketUtils");
17
16
  var version_1 = require("../../../version");
@@ -71,9 +70,7 @@ var SonicWSCore = /** @class */ (function () {
71
70
  // print the error to console without halting execution
72
71
  if (key == null)
73
72
  return console.error(new Error("The server does not send the packet with tag \"".concat(tag, "\"!")));
74
- var packet = _this.serverPackets.getPacket(tag);
75
- var packetListener = new PacketListener_1.PacketListener(packet, listener);
76
- _this.listen(tag, packetListener);
73
+ _this.listen(tag, listener);
77
74
  }); });
78
75
  this.preListen = null; // clear
79
76
  this.pastKeys = true;
@@ -84,7 +81,7 @@ var SonicWSCore = /** @class */ (function () {
84
81
  this.keyHandler = null;
85
82
  };
86
83
  SonicWSCore.prototype.messageHandler = function (event) {
87
- var _a;
84
+ var _a, _b;
88
85
  var data = event.data.toString();
89
86
  this.listeners.message.forEach(function (listener) { return listener(data); });
90
87
  if (data.length < 1)
@@ -94,12 +91,16 @@ var SonicWSCore = /** @class */ (function () {
94
91
  var code = key.charCodeAt(0);
95
92
  if (code == null)
96
93
  return;
97
- var result = this.serverPackets.getPacket(this.serverPackets.getTag(key)).listen(value);
94
+ var packet = this.serverPackets.getPacket(this.serverPackets.getTag(key));
95
+ var result = packet.listen(value);
98
96
  if (result == null) {
99
97
  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");
100
98
  }
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); });
99
+ var processed = result[0], flatten = result[1];
100
+ if (flatten)
101
+ (_a = this.listeners.event[code]) === null || _a === void 0 ? void 0 : _a.forEach(function (l) { return l.apply(void 0, processed); });
102
+ else
103
+ (_b = this.listeners.event[code]) === null || _b === void 0 ? void 0 : _b.forEach(function (l) { return l(processed); });
103
104
  };
104
105
  SonicWSCore.prototype.listen = function (key, listener) {
105
106
  var skey = this.serverPackets.get(key);
@@ -148,8 +149,7 @@ var SonicWSCore = /** @class */ (function () {
148
149
  return;
149
150
  }
150
151
  var packet = this.serverPackets.getPacket(tag);
151
- var packetListener = new PacketListener_1.PacketListener(packet, listener);
152
- this.listen(tag, packetListener);
152
+ this.listen(tag, listener);
153
153
  };
154
154
  return SonicWSCore;
155
155
  }());
@@ -35,7 +35,8 @@ exports.PacketValidityProcessors = (_a = {},
35
35
  }
36
36
  return true;
37
37
  },
38
- _a[PacketType_1.PacketType.INTS_C] = function (data, cap) { return data.length == cap; },
38
+ _a[PacketType_1.PacketType.INTS_C] = function (data, cap) { return data.length <= cap; },
39
+ _a[PacketType_1.PacketType.UINTS_C] = function (data, cap) { return data.length <= cap; },
39
40
  _a[PacketType_1.PacketType.INTS_D] = function (data, cap) { return data.length > 0 && ((0, CodePointUtil_1.processCharCodes)(data).length - 1) % data[0].charCodeAt(0) <= cap; },
40
41
  _a[PacketType_1.PacketType.INTS_A] = LEN_DELIMIT,
41
42
  _a[PacketType_1.PacketType.DECIMALS] = function (data, cap) {
@@ -78,6 +79,7 @@ exports.PacketReceiveProcessors = (_b = {},
78
79
  return (0, CodePointUtil_1.processCharCodes)(data).map(function (code) { return pkg.values[code]; });
79
80
  },
80
81
  _b[PacketType_1.PacketType.INTS_C] = function (data) { return (0, CodePointUtil_1.processCharCodes)(data).map(CodePointUtil_1.fromSignedINT_C); },
82
+ _b[PacketType_1.PacketType.UINTS_C] = function (data) { return (0, CodePointUtil_1.processCharCodes)(data); },
81
83
  _b[PacketType_1.PacketType.INTS_D] = function (data) { return (0, ArrayUtil_1.splitArray)((0, CodePointUtil_1.processCharCodes)(data.substring(1)), data.charCodeAt(0)).map(function (arr) { return String.fromCharCode.apply(String, arr); }).map(CodePointUtil_1.deconvertINT_D); },
82
84
  _b[PacketType_1.PacketType.INTS_A] = function (data) {
83
85
  var numbers = [];
@@ -130,6 +132,13 @@ exports.PacketSendProcessors = (_c = {},
130
132
  }
131
133
  return numbers.map(CodePointUtil_1.stringedINT_C).join("");
132
134
  },
135
+ _c[PacketType_1.PacketType.UINTS_C] = function () {
136
+ var numbers = [];
137
+ for (var _i = 0; _i < arguments.length; _i++) {
138
+ numbers[_i] = arguments[_i];
139
+ }
140
+ return String.fromCharCode.apply(String, numbers);
141
+ },
133
142
  _c[PacketType_1.PacketType.INTS_D] = function () {
134
143
  var numbers = [];
135
144
  for (var _i = 0; _i < arguments.length; _i++) {
@@ -10,12 +10,14 @@ export declare enum PacketType {
10
10
  ENUMS = 3,
11
11
  /** One or more numbers from -27,648 to 27,647 */
12
12
  INTS_C = 4,
13
+ /** One or more positive numbers from 0 to 55,295 */
14
+ UINTS_C = 5,
13
15
  /** One or more numbers of any size. Similar maximum size will produce maximum efficiency */
14
- INTS_D = 5,
16
+ INTS_D = 6,
15
17
  /** One or more numbers of any size. More efficient for differently sized numbers, worse than INTS_D for similar sized numbers. */
16
- INTS_A = 6,
18
+ INTS_A = 7,
17
19
  /** One or more decimal numbers of any size */
18
- DECIMALS = 7,
20
+ DECIMALS = 8,
19
21
  /** One or more true/false */
20
- BOOLEANS = 8
22
+ BOOLEANS = 9
21
23
  }
@@ -15,12 +15,14 @@ var PacketType;
15
15
  PacketType[PacketType["ENUMS"] = 3] = "ENUMS";
16
16
  /** One or more numbers from -27,648 to 27,647 */
17
17
  PacketType[PacketType["INTS_C"] = 4] = "INTS_C";
18
+ /** One or more positive numbers from 0 to 55,295 */
19
+ PacketType[PacketType["UINTS_C"] = 5] = "UINTS_C";
18
20
  /** One or more numbers of any size. Similar maximum size will produce maximum efficiency */
19
- PacketType[PacketType["INTS_D"] = 5] = "INTS_D";
21
+ PacketType[PacketType["INTS_D"] = 6] = "INTS_D";
20
22
  /** One or more numbers of any size. More efficient for differently sized numbers, worse than INTS_D for similar sized numbers. */
21
- PacketType[PacketType["INTS_A"] = 6] = "INTS_A";
23
+ PacketType[PacketType["INTS_A"] = 7] = "INTS_A";
22
24
  /** One or more decimal numbers of any size */
23
- PacketType[PacketType["DECIMALS"] = 7] = "DECIMALS";
25
+ PacketType[PacketType["DECIMALS"] = 8] = "DECIMALS";
24
26
  /** One or more true/false */
25
- PacketType[PacketType["BOOLEANS"] = 8] = "BOOLEANS";
27
+ PacketType[PacketType["BOOLEANS"] = 9] = "BOOLEANS";
26
28
  })(PacketType || (exports.PacketType = PacketType = {}));
@@ -16,8 +16,9 @@ export declare class Packet {
16
16
  processReceive: (data: string) => any;
17
17
  processSend: (data: any[]) => string;
18
18
  validate: (data: string) => boolean;
19
- constructor(tag: string, schema: PacketSchema, client: boolean);
20
- listen(value: string): [processed: any, isArray: boolean] | null;
19
+ customValidator: ((...values: any[]) => boolean) | null;
20
+ constructor(tag: string, schema: PacketSchema, customValidator: ((values: any[]) => boolean) | null, client: boolean);
21
+ listen(value: string): [processed: any, flatten: boolean] | null;
21
22
  serialize(): string;
22
23
  static deserialize(text: string, offset: number, client: boolean): [packet: Packet, offset: number];
23
24
  static deserializeAll(text: string, client: boolean): Packet[];
@@ -7,7 +7,7 @@ var CodePointUtil_1 = require("../util/CodePointUtil");
7
7
  var PacketProcessors_1 = require("./PacketProcessors");
8
8
  var PacketType_1 = require("./PacketType");
9
9
  var Packet = /** @class */ (function () {
10
- function Packet(tag, schema, client) {
10
+ function Packet(tag, schema, customValidator, client) {
11
11
  var _this = this;
12
12
  this.tag = tag;
13
13
  if (schema.object) {
@@ -48,24 +48,35 @@ var Packet = /** @class */ (function () {
48
48
  }
49
49
  this.processReceive = function (data) { return _this.receiveProcessor(data, _this.dataMax, _this, 0); };
50
50
  this.validate = client ? function () { return true; } : function (data) { return _this.validifier(data, _this.dataMax, _this, 0); };
51
+ this.customValidator = customValidator;
51
52
  this.enumData = schema.enumData;
52
53
  this.dontSpread = schema.dontSpread;
53
54
  }
54
55
  Packet.prototype.listen = function (value) {
55
- var processed, isArray;
56
56
  try {
57
57
  if (!this.validate(value))
58
58
  return null;
59
- processed = this.processReceive(value);
60
- isArray = Array.isArray(processed);
59
+ var processed = this.processReceive(value);
60
+ var isArray = Array.isArray(processed);
61
61
  if (isArray && processed.length < this.dataMin)
62
62
  return null;
63
+ var flatten = isArray && !this.dontSpread;
64
+ if (this.customValidator != null) {
65
+ if (flatten) {
66
+ if (!this.customValidator.apply(this, processed))
67
+ return null;
68
+ }
69
+ else {
70
+ if (!this.customValidator(processed))
71
+ return null;
72
+ }
73
+ }
74
+ return [processed, flatten];
63
75
  }
64
76
  catch (err) {
65
77
  console.error("There was an error processing the packet! This is probably my fault... report at https://github.com/cutelittlelily/sonic-ws", err);
66
78
  return null;
67
79
  }
68
- return [processed, isArray];
69
80
  };
70
81
  Packet.prototype.serialize = function () {
71
82
  // spread flag; ETX for "2", STX for "1", avoid NULL for delimiting
@@ -134,7 +145,7 @@ var Packet = /** @class */ (function () {
134
145
  var tagLength_1 = text.charCodeAt(tagStart_1 - 1) - 1; // tag length is right behind tag, subtracting 1 to reverse
135
146
  var tag_1 = text.substring(tagStart_1, tagStart_1 + tagLength_1); // tag is tag length long. yeah
136
147
  return [
137
- new Packet(tag_1, PacketSchema.object(finalTypes, dataMaxes, dataMins, dontSpread), client),
148
+ new Packet(tag_1, PacketSchema.object(finalTypes, dataMaxes, dataMins, dontSpread), null, client),
138
149
  (offset - beginningOffset) + 1 + size + size + size + tagLength_1
139
150
  ];
140
151
  }
@@ -147,7 +158,7 @@ var Packet = /** @class */ (function () {
147
158
  var tagLength = text.charCodeAt(tagStart) - 1;
148
159
  var tag = text.substring(tagStart + 1, tagStart + 1 + tagLength);
149
160
  return [
150
- new Packet(tag, PacketSchema.single(finalType, dataMax, dataMin, dontSpread), client),
161
+ new Packet(tag, PacketSchema.single(finalType, dataMax, dataMin, dontSpread), null, client),
151
162
  (offset - beginningOffset) + 1 + tagLength
152
163
  ];
153
164
  };
@@ -19,7 +19,7 @@ export declare class SonicWSConnection {
19
19
  id: number;
20
20
  /** The indexed character of the connection. Smaller data packet in strings. */
21
21
  code: string;
22
- constructor(socket: WS.WebSocket, host: SonicWSServer, id: number, handshakePacket: string | null);
22
+ constructor(socket: WS.WebSocket, host: SonicWSServer, id: number, handshakePacket: string | null, rateLimit: number);
23
23
  private parseData;
24
24
  private handshakeHandler;
25
25
  private messageHandler;
@@ -44,14 +44,12 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
44
44
  Object.defineProperty(exports, "__esModule", { value: true });
45
45
  exports.SonicWSConnection = void 0;
46
46
  var WS = __importStar(require("ws"));
47
- var PacketListener_1 = require("../packets/PacketListener");
48
47
  var CodePointUtil_1 = require("../util/CodePointUtil");
49
48
  var PacketUtils_1 = require("../util/PacketUtils");
50
49
  var SonicWSConnection = /** @class */ (function () {
51
- function SonicWSConnection(socket, host, id, handshakePacket) {
50
+ function SonicWSConnection(socket, host, id, handshakePacket, rateLimit) {
52
51
  var _this = this;
53
52
  this.print = false;
54
- this.rateLimit = 50;
55
53
  this.received = 0;
56
54
  this.timers = [];
57
55
  this.messageLambda = function (data) { return _this.messageHandler(_this.parseData(data)); };
@@ -87,6 +85,7 @@ var SonicWSConnection = /** @class */ (function () {
87
85
  clearInterval(_this.rateLimitInterval);
88
86
  _this.timers.forEach(clearTimeout);
89
87
  });
88
+ this.rateLimit = rateLimit;
90
89
  if (this.rateLimit != 0)
91
90
  this.rateLimitInterval = setInterval(function () { return _this.received = 0; }, 1000);
92
91
  }
@@ -128,18 +127,18 @@ var SonicWSConnection = /** @class */ (function () {
128
127
  if (data == null)
129
128
  return;
130
129
  var tag = data[0], value = data[1];
131
- var listened = this.host.clientPackets.getPacket(tag).listen(value);
130
+ var packet = this.host.clientPackets.getPacket(tag);
131
+ var listened = packet.listen(value);
132
132
  // if invalid then ignore it
133
133
  if (listened == null) {
134
134
  this.socket.close(4003);
135
135
  return;
136
136
  }
137
- var processed = listened[0], isArray = listened[1];
138
- for (var _i = 0, _a = this.listeners[tag]; _i < _a.length; _i++) {
139
- var listener = _a[_i];
140
- listener.listen(processed, isArray);
141
- }
142
- ;
137
+ var processed = listened[0], flatten = listened[1];
138
+ if (flatten)
139
+ this.listeners[tag].forEach(function (l) { return l.apply(void 0, processed); });
140
+ else
141
+ this.listeners[tag].forEach(function (l) { return l(processed); });
143
142
  };
144
143
  SonicWSConnection.prototype.hideNewLines = function (str) {
145
144
  return str.split("\n").join("☺");
@@ -190,7 +189,7 @@ var SonicWSConnection = /** @class */ (function () {
190
189
  var packet = this.host.clientPackets.getPacket(tag);
191
190
  if (!this.listeners[tag])
192
191
  this.listeners[tag] = [];
193
- this.listeners[tag].push(new PacketListener_1.PacketListener(packet, listener));
192
+ this.listeners[tag].push(listener);
194
193
  };
195
194
  /**
196
195
  * Sends a packet with the tag and values
@@ -74,7 +74,7 @@ var SonicWSServer = /** @class */ (function () {
74
74
  var s_serverPackets = this.serverPackets.serialize();
75
75
  var keyData = "SWS" + version_1.VERSION_CHAR + s_clientPackets + CodePointUtil_1.NULL + s_serverPackets;
76
76
  this.wss.on('connection', function (socket) {
77
- var sonicConnection = new SonicWSConnection_1.SonicWSConnection(socket, _this, _this.generateSocketID(), _this.handshakePacket);
77
+ var sonicConnection = new SonicWSConnection_1.SonicWSConnection(socket, _this, _this.generateSocketID(), _this.handshakePacket, _this.rateLimit);
78
78
  // send tags to the client so it doesn't have to hard code them in
79
79
  socket.send(keyData + CodePointUtil_1.NULL + String.fromCharCode(_this.rateLimit));
80
80
  _this.connections.push(sonicConnection);
@@ -54,7 +54,7 @@ function toSignedINT_C(number) {
54
54
  }
55
55
  // just conversion and checks lol
56
56
  function stringedINT_C(number) {
57
- if (number >= exports.NEGATIVE_C || number < -exports.NEGATIVE_C - 1)
57
+ if (number > exports.NEGATIVE_C || number < -exports.NEGATIVE_C - 1)
58
58
  throw new Error("INT_C Numbers must be within range -".concat(exports.NEGATIVE_C + 1, " and ").concat(exports.NEGATIVE_C));
59
59
  return String.fromCharCode(toSignedINT_C(number));
60
60
  }
@@ -16,6 +16,8 @@ export type SinglePacketSettings = {
16
16
  dataMin?: number;
17
17
  /** If the values should be kept in an array or spread along the listener; defaults to false */
18
18
  dontSpread?: boolean;
19
+ /** A validation function that is called whenever data is received. Return true for success, return false to kick socket. */
20
+ validator?: ((values: any[]) => boolean) | null;
19
21
  };
20
22
  /** Settings for multi-typed packets */
21
23
  export type MultiPacketSettings = {
@@ -29,6 +31,8 @@ export type MultiPacketSettings = {
29
31
  dataMins?: number[];
30
32
  /** If the values should be kept in an array or spread along the listener; defaults to false */
31
33
  dontSpread?: boolean;
34
+ /** A validation function that is called whenever data is received. Return true for success, return false to kick socket. */
35
+ validator?: ((values: any[]) => boolean) | null;
32
36
  };
33
37
  /** Settings for single-typed enum packets */
34
38
  export type EnumPacketSettings = {
@@ -44,6 +48,8 @@ export type EnumPacketSettings = {
44
48
  dataMin?: number;
45
49
  /** If the values should be kept in an array or spread along the listener; defaults to false */
46
50
  dontSpread?: boolean;
51
+ /** A validation function that is called whenever data is received. Return true for success, return false to kick socket. */
52
+ validator?: ((values: any[]) => boolean) | null;
47
53
  };
48
54
  /**
49
55
  * Creates a structure for a simple single-typed packet.
@@ -23,6 +23,19 @@ function emitPacket(packets, send, tag, values) {
23
23
  if (found)
24
24
  console.warn("Passing an array will result in undefined behavior (".concat(JSON.stringify(found), "). Spread the array with ...arr"));
25
25
  }
26
+ else {
27
+ // also map non arrays to arrays to keep some code cleaner
28
+ values = values.map(function (x) { return !Array.isArray(x) ? [x] : x; });
29
+ var dataMins = packet.dataMin;
30
+ var dataMaxes = packet.dataMax;
31
+ for (var i = 0; i < dataMins.length; i++) {
32
+ // these will be the same length
33
+ if (values[i].length < dataMins[i])
34
+ throw new Error("Section ".concat(i + 1, " of packet \"").concat(tag, "\" requires at least ").concat(dataMins[i], " values!"));
35
+ if (values[i].length > dataMaxes[i])
36
+ throw new Error("Section ".concat(i + 1, " of packet \"").concat(tag, "\" only allows ").concat(dataMaxes[i], " values!"));
37
+ }
38
+ }
26
39
  send(code + (values.length > 0 ? packet.processSend(values) : ""));
27
40
  }
28
41
  function isValidType(type) {
@@ -55,13 +68,13 @@ function clampDataMin(dataMin, dataMax) {
55
68
  * @throws {Error} If the `type` is invalid.
56
69
  */
57
70
  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;
71
+ 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, _d = settings.validator, validator = _d === void 0 ? null : _d;
59
72
  if (dataMin == undefined)
60
73
  dataMin = type == PacketType_1.PacketType.NONE ? 0 : dataMax;
61
74
  if (!isValidType(type)) {
62
75
  throw new Error("Invalid packet type: ".concat(type));
63
76
  }
64
- return new Packets_1.Packet(tag, Packets_1.PacketSchema.single(type, clampDataMax(dataMax), clampDataMin(dataMin, dataMax), dontSpread), false);
77
+ return new Packets_1.Packet(tag, Packets_1.PacketSchema.single(type, clampDataMax(dataMax), clampDataMin(dataMin, dataMax), dontSpread), validator, false);
65
78
  }
66
79
  /**
67
80
  * Creates a structure for an object (multi-typed) packet.
@@ -71,7 +84,7 @@ function CreatePacket(settings) {
71
84
  * @throws {Error} If any type in `types` is invalid.
72
85
  */
73
86
  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;
87
+ var tag = settings.tag, types = settings.types, dataMaxes = settings.dataMaxes, dataMins = settings.dataMins, _a = settings.dontSpread, dontSpread = _a === void 0 ? false : _a, _b = settings.validator, validator = _b === void 0 ? null : _b;
75
88
  var invalid = types.find(function (type) { return !isValidType(type); });
76
89
  if (invalid) {
77
90
  throw new Error("Invalid packet type: ".concat(invalid));
@@ -82,7 +95,7 @@ function CreateObjPacket(settings) {
82
95
  dataMins = Array.from({ length: types.length }).map(function (_, i) { return dataMaxes[i]; });
83
96
  var clampedDataMaxes = dataMaxes.map(clampDataMax);
84
97
  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);
98
+ return new Packets_1.Packet(tag, Packets_1.PacketSchema.object(types, clampedDataMaxes, clampedDataMins, dontSpread), validator, false);
86
99
  }
87
100
  /**
88
101
  * Creates and defines an enum packet. This can be used to create an enum-based packet
@@ -91,12 +104,13 @@ function CreateObjPacket(settings) {
91
104
  * @returns The constructed packet structure data.
92
105
  */
93
106
  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;
107
+ 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, _d = settings.validator, validator = _d === void 0 ? null : _d;
95
108
  return CreatePacket({
96
109
  tag: packetTag,
97
110
  type: (0, EnumHandler_1.DefineEnum)(enumTag, values),
98
111
  dataMax: dataMax,
99
112
  dataMin: dataMin,
100
- dontSpread: dontSpread
113
+ dontSpread: dontSpread,
114
+ validator: validator
101
115
  });
102
116
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sonic-ws",
3
- "version": "1.0.0-rc.0-patch",
3
+ "version": "1.0.0-rc.2",
4
4
  "description": "A bandwidth efficient websocket library",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,7 +0,0 @@
1
- import { Packet } from "./Packets";
2
- export declare class PacketListener {
3
- private listener;
4
- private packet;
5
- constructor(packet: Packet, listener: (...data: any[]) => void);
6
- listen(processed: any, isArray: boolean): boolean;
7
- }
@@ -1,18 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PacketListener = void 0;
4
- var PacketListener = /** @class */ (function () {
5
- function PacketListener(packet, listener) {
6
- this.listener = listener;
7
- this.packet = packet;
8
- }
9
- PacketListener.prototype.listen = function (processed, isArray) {
10
- if (isArray && !this.packet.dontSpread)
11
- this.listener.apply(this, processed);
12
- else
13
- this.listener(processed);
14
- return true;
15
- };
16
- return PacketListener;
17
- }());
18
- exports.PacketListener = PacketListener;