@hytopia.com/server-protocol 1.4.28 → 1.4.30

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hytopia.com/server-protocol",
3
- "version": "1.4.28",
3
+ "version": "1.4.30",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -1,6 +1,8 @@
1
1
  import Ajv from '../shared/Ajv';
2
2
  import type { JSONSchemaType, ValidateFunction } from 'ajv';
3
3
 
4
+ const FRAME_HEADER_SIZE = 4;
5
+
4
6
  /*
5
7
  * Packet types are numerically ordered relative to their use.
6
8
  *
@@ -101,6 +103,47 @@ export function createPacket<TId extends PacketId, TSchema>(
101
103
  return packet;
102
104
  }
103
105
 
106
+ export function createPacketBufferUnframer(): (chunk: Uint8Array) => Uint8Array[] {
107
+ let buffer = new Uint8Array(512 * 1024); // 512KB initial buffer
108
+ let used = 0;
109
+
110
+ return (chunk: Uint8Array): Uint8Array[] => {
111
+ const messages: Uint8Array[] = [];
112
+
113
+ // Grow buffer if needed
114
+ if (used + chunk.length > buffer.length) {
115
+ const grown = new Uint8Array(Math.max(buffer.length * 2, used + chunk.length));
116
+ grown.set(buffer.subarray(0, used));
117
+ buffer = grown;
118
+ }
119
+
120
+ // Append new chunk
121
+ buffer.set(chunk, used);
122
+ used += chunk.length;
123
+
124
+ // Extract complete messages
125
+ while (used >= FRAME_HEADER_SIZE) {
126
+ const view = new DataView(buffer.buffer, buffer.byteOffset);
127
+ const length = view.getUint32(0, false); // false = big-endian
128
+
129
+ // Wait for complete message
130
+ if (used < FRAME_HEADER_SIZE + length) {
131
+ break;
132
+ }
133
+
134
+ // Extract message (skip 4-byte header)
135
+ const message = buffer.slice(FRAME_HEADER_SIZE, FRAME_HEADER_SIZE + length);
136
+ messages.push(message);
137
+
138
+ // Shift buffer
139
+ buffer.copyWithin(0, FRAME_HEADER_SIZE + length, used);
140
+ used -= FRAME_HEADER_SIZE + length;
141
+ }
142
+
143
+ return messages;
144
+ };
145
+ }
146
+
104
147
  export function definePacket<TId extends PacketId, TSchema>(
105
148
  id: TId,
106
149
  schema: JSONSchemaType<TSchema>,
@@ -112,4 +155,15 @@ export function definePacket<TId extends PacketId, TSchema>(
112
155
  };
113
156
  };
114
157
 
115
-
158
+ export function framePacketBuffer(buffer: Uint8Array): Uint8Array {
159
+ const framed = new Uint8Array(FRAME_HEADER_SIZE + buffer.length);
160
+ const view = new DataView(framed.buffer);
161
+
162
+ // Write length as big-endian uint32 in first 4 bytes
163
+ view.setUint32(0, buffer.length, false);
164
+
165
+ // Copy data after header
166
+ framed.set(buffer, FRAME_HEADER_SIZE);
167
+
168
+ return framed;
169
+ }
@@ -3,8 +3,6 @@ import { JSONSchemaType } from 'ajv';
3
3
  export type ConnectionSchema = {
4
4
  i?: string; // connection id | server -> client | on initial WS connection
5
5
  k?: boolean; // kill connection | server -> client | tells client to close connection & not reconnect
6
- ts?: string; // transport session token | client -> server | can be sent with 1st webtransport packet to server
7
- tc?: string; // transport connection id | client -> server | can be sent with 1st webtransport packet to server
8
6
  };
9
7
 
10
8
  export const connectionSchema: JSONSchemaType<ConnectionSchema> = {
@@ -12,8 +10,6 @@ export const connectionSchema: JSONSchemaType<ConnectionSchema> = {
12
10
  properties: {
13
11
  i: { type: 'string', nullable: true },
14
12
  k: { type: 'boolean', nullable: true },
15
- ts: { type: 'string', nullable: true },
16
- tc: { type: 'string', nullable: true },
17
13
  },
18
14
  additionalProperties: false,
19
15
  };