@hytopia.com/server-protocol 1.0.10 → 1.0.12

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/exports.ts CHANGED
@@ -1,8 +1,7 @@
1
- import * as inboundPackets from './packets/inbound';
2
- import * as outboundPackets from './packets/outbound';
3
-
4
- export { inboundPackets, outboundPackets };
5
1
  export * from './packets/inbound';
6
2
  export * from './packets/outbound';
7
3
  export * from './packets/PacketCore';
4
+ export * from './packets/PacketDefinitions';
8
5
  export * from './schemas';
6
+
7
+
package/index.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  import * as protocol from './exports';
2
2
  export * from './exports';
3
- export default protocol;
3
+ export default protocol;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hytopia.com/server-protocol",
3
- "version": "1.0.10",
3
+ "version": "1.0.12",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -55,10 +55,10 @@ export type AnyPacket = IPacket<PacketId, unknown>;
55
55
 
56
56
  export type AnyPacketDefinition = IPacketDefinition<number, unknown>;
57
57
 
58
- export const createPacket = <TId extends number, TSchema>(
58
+ export function createPacket<TId extends number, TSchema>(
59
59
  packetDef: IPacketDefinition<TId, TSchema>,
60
60
  data: TSchema,
61
- ): IPacket<TId, TSchema> => {
61
+ ): IPacket<TId, TSchema> {
62
62
  if (!packetDef.validate(data)) {
63
63
  throw new Error(`createPacket(): Invalid payload for packet with id ${packetDef.id}, error: ${Ajv.instance.errorsText(packetDef.validate.errors)}`);
64
64
  }
@@ -66,11 +66,15 @@ export const createPacket = <TId extends number, TSchema>(
66
66
  return { i: packetDef.id, d: data };
67
67
  }
68
68
 
69
- export const definePacket = <TId extends PacketId, TSchema>(
69
+ export function definePacket<TId extends PacketId, TSchema>(
70
70
  id: TId,
71
71
  schema: JSONSchemaType<TSchema>,
72
- ): IPacketDefinition<TId, TSchema> => ({
73
- id,
74
- schema,
75
- validate: Ajv.instance.compile(schema),
76
- });
72
+ ): IPacketDefinition<TId, TSchema> {
73
+ return {
74
+ id,
75
+ schema,
76
+ validate: Ajv.instance.compile(schema),
77
+ };
78
+ };
79
+
80
+
@@ -0,0 +1,36 @@
1
+ import * as inboundPackets from './inbound';
2
+ import * as outboundPackets from './outbound';
3
+ import type { PacketId, AnyPacket, AnyPacketDefinition } from './PacketCore';
4
+
5
+ export { inboundPackets, outboundPackets };
6
+
7
+ export const registeredPackets = new Map<PacketId, AnyPacketDefinition>();
8
+
9
+ const allPackets = { ...inboundPackets, ...outboundPackets };
10
+
11
+ for (const packet of Object.values(allPackets)) {
12
+ if ('id' in packet && 'schema' in packet) {
13
+ const definition = packet as AnyPacketDefinition;
14
+
15
+ if (registeredPackets.has(definition.id)) {
16
+ throw new Error(`Packet with id ${definition.id} is already registered.`);
17
+ }
18
+
19
+ registeredPackets.set(definition.id, definition);
20
+ }
21
+ }
22
+
23
+ export function isValidPacket(packet: unknown): packet is AnyPacket {
24
+ if (
25
+ typeof packet !== 'object' ||
26
+ packet === null ||
27
+ !('i' in packet) ||
28
+ !('d' in packet) ||
29
+ typeof packet.i !== 'number'
30
+ ) {
31
+ return false;
32
+ }
33
+
34
+ const packetDef = registeredPackets.get(packet.i);
35
+ return !!packetDef && packetDef.validate(packet.d);
36
+ }