@dcl/ecs 7.0.6-3824930139.commit-9bd6c05 → 7.0.6-3832097301.commit-4e46b20

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.
@@ -1,3 +1,9 @@
1
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
2
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
3
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
4
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
5
+ };
6
+ var _ReadWriteByteBuffer_instances, _ReadWriteByteBuffer_woAdd, _ReadWriteByteBuffer_roAdd;
1
7
  import * as utf8 from '@protobufjs/utf8';
2
8
  /**
3
9
  * Take the max between currentSize and intendedSize and then plus 1024. Then,
@@ -18,240 +24,220 @@ const defaultInitialCapacity = 10240;
18
24
  * - Use read and write function to generate or consume data.
19
25
  * - Use set and get only if you are sure that you're doing.
20
26
  */
21
- export function createByteBuffer(options = {}) {
22
- const initialROffset = options.reading?.currentOffset || 0;
23
- let initialBuffer = null;
24
- let initialWOffset = 0;
25
- if (options.writing) {
26
- initialBuffer = options.writing.buffer;
27
- if (options.writing.currentOffset) {
28
- initialWOffset = options.writing.currentOffset;
29
- }
27
+ export class ReadWriteByteBuffer {
28
+ /**
29
+ * @param buffer - The initial buffer, provide a buffer if you need to set "initial capacity"
30
+ * @param readingOffset - Set the cursor where begins to read. Default 0
31
+ * @param writingOffset - Set the cursor to not start writing from the begin of it. Defaults to the buffer size
32
+ */
33
+ constructor(buffer, readingOffset, writingOffset) {
34
+ _ReadWriteByteBuffer_instances.add(this);
35
+ this._buffer = buffer || new Uint8Array(defaultInitialCapacity);
36
+ this.view = new DataView(this._buffer.buffer, this._buffer.byteOffset);
37
+ this.woffset = writingOffset ?? (buffer ? this._buffer.length : null) ?? 0;
38
+ this.roffset = readingOffset ?? 0;
30
39
  }
31
- else if (options.reading) {
32
- initialBuffer = options.reading.buffer;
33
- initialWOffset = options.reading.length || options.reading.buffer.length;
40
+ buffer() {
41
+ return this._buffer;
34
42
  }
35
- else {
36
- initialBuffer = new Uint8Array(options.initialCapacity || defaultInitialCapacity);
43
+ bufferLength() {
44
+ return this._buffer.length;
37
45
  }
38
- let buffer = initialBuffer;
39
- let view = new DataView(buffer.buffer, buffer.byteOffset);
40
- let woffset = initialWOffset;
41
- let roffset = initialROffset;
42
- /**
43
- * Increement the write offset and resize the buffer if it needs.
44
- */
45
- const woAdd = (amount) => {
46
- if (woffset + amount > buffer.byteLength) {
47
- const newsize = getNextSize(buffer.byteLength, woffset + amount);
48
- const newBuffer = new Uint8Array(newsize);
49
- newBuffer.set(buffer);
50
- buffer = newBuffer;
51
- view = new DataView(buffer.buffer);
52
- }
53
- woffset += amount;
54
- return woffset - amount;
55
- };
56
- /**
57
- * Increment the read offset and throw an error if it's trying to read
58
- * outside the bounds.
59
- */
60
- const roAdd = (amount) => {
61
- if (roffset + amount > woffset) {
62
- throw new Error('Outside of the bounds of writen data.');
46
+ resetBuffer() {
47
+ this.roffset = 0;
48
+ this.woffset = 0;
49
+ }
50
+ currentReadOffset() {
51
+ return this.roffset;
52
+ }
53
+ currentWriteOffset() {
54
+ return this.woffset;
55
+ }
56
+ incrementReadOffset(amount) {
57
+ return __classPrivateFieldGet(this, _ReadWriteByteBuffer_instances, "m", _ReadWriteByteBuffer_roAdd).call(this, amount);
58
+ }
59
+ remainingBytes() {
60
+ return this.woffset - this.roffset;
61
+ }
62
+ readFloat32() {
63
+ return this.view.getFloat32(__classPrivateFieldGet(this, _ReadWriteByteBuffer_instances, "m", _ReadWriteByteBuffer_roAdd).call(this, 4));
64
+ }
65
+ readFloat64() {
66
+ return this.view.getFloat64(__classPrivateFieldGet(this, _ReadWriteByteBuffer_instances, "m", _ReadWriteByteBuffer_roAdd).call(this, 8));
67
+ }
68
+ readInt8() {
69
+ return this.view.getInt8(__classPrivateFieldGet(this, _ReadWriteByteBuffer_instances, "m", _ReadWriteByteBuffer_roAdd).call(this, 1));
70
+ }
71
+ readInt16() {
72
+ return this.view.getInt16(__classPrivateFieldGet(this, _ReadWriteByteBuffer_instances, "m", _ReadWriteByteBuffer_roAdd).call(this, 2));
73
+ }
74
+ readInt32() {
75
+ return this.view.getInt32(__classPrivateFieldGet(this, _ReadWriteByteBuffer_instances, "m", _ReadWriteByteBuffer_roAdd).call(this, 4));
76
+ }
77
+ readInt64() {
78
+ return this.view.getBigInt64(__classPrivateFieldGet(this, _ReadWriteByteBuffer_instances, "m", _ReadWriteByteBuffer_roAdd).call(this, 8));
79
+ }
80
+ readUint8() {
81
+ return this.view.getUint8(__classPrivateFieldGet(this, _ReadWriteByteBuffer_instances, "m", _ReadWriteByteBuffer_roAdd).call(this, 1));
82
+ }
83
+ readUint16() {
84
+ return this.view.getUint16(__classPrivateFieldGet(this, _ReadWriteByteBuffer_instances, "m", _ReadWriteByteBuffer_roAdd).call(this, 2));
85
+ }
86
+ readUint32() {
87
+ return this.view.getUint32(__classPrivateFieldGet(this, _ReadWriteByteBuffer_instances, "m", _ReadWriteByteBuffer_roAdd).call(this, 4));
88
+ }
89
+ readUint64() {
90
+ return this.view.getBigUint64(__classPrivateFieldGet(this, _ReadWriteByteBuffer_instances, "m", _ReadWriteByteBuffer_roAdd).call(this, 8));
91
+ }
92
+ readBuffer() {
93
+ const length = this.view.getUint32(__classPrivateFieldGet(this, _ReadWriteByteBuffer_instances, "m", _ReadWriteByteBuffer_roAdd).call(this, 4));
94
+ return this._buffer.subarray(__classPrivateFieldGet(this, _ReadWriteByteBuffer_instances, "m", _ReadWriteByteBuffer_roAdd).call(this, length), __classPrivateFieldGet(this, _ReadWriteByteBuffer_instances, "m", _ReadWriteByteBuffer_roAdd).call(this, 0));
95
+ }
96
+ readUtf8String() {
97
+ const length = this.view.getUint32(__classPrivateFieldGet(this, _ReadWriteByteBuffer_instances, "m", _ReadWriteByteBuffer_roAdd).call(this, 4));
98
+ return utf8.read(this._buffer, __classPrivateFieldGet(this, _ReadWriteByteBuffer_instances, "m", _ReadWriteByteBuffer_roAdd).call(this, length), __classPrivateFieldGet(this, _ReadWriteByteBuffer_instances, "m", _ReadWriteByteBuffer_roAdd).call(this, 0));
99
+ }
100
+ incrementWriteOffset(amount) {
101
+ return __classPrivateFieldGet(this, _ReadWriteByteBuffer_instances, "m", _ReadWriteByteBuffer_woAdd).call(this, amount);
102
+ }
103
+ toBinary() {
104
+ return this._buffer.subarray(0, this.woffset);
105
+ }
106
+ toCopiedBinary() {
107
+ return new Uint8Array(this.toBinary());
108
+ }
109
+ writeBuffer(value, writeLength = true) {
110
+ if (writeLength) {
111
+ this.writeUint32(value.byteLength);
63
112
  }
64
- roffset += amount;
65
- return roffset - amount;
66
- };
67
- return {
68
- buffer() {
69
- return buffer;
70
- },
71
- bufferLength() {
72
- return buffer.length;
73
- },
74
- resetBuffer() {
75
- roffset = 0;
76
- woffset = 0;
77
- },
78
- currentReadOffset() {
79
- return roffset;
80
- },
81
- currentWriteOffset() {
82
- return woffset;
83
- },
84
- incrementReadOffset(amount) {
85
- return roAdd(amount);
86
- },
87
- remainingBytes() {
88
- return woffset - roffset;
89
- },
90
- readFloat32() {
91
- return view.getFloat32(roAdd(4));
92
- },
93
- readFloat64() {
94
- return view.getFloat64(roAdd(8));
95
- },
96
- readInt8() {
97
- return view.getInt8(roAdd(1));
98
- },
99
- readInt16() {
100
- return view.getInt16(roAdd(2));
101
- },
102
- readInt32() {
103
- return view.getInt32(roAdd(4));
104
- },
105
- readInt64() {
106
- return view.getBigInt64(roAdd(8));
107
- },
108
- readUint8() {
109
- return view.getUint8(roAdd(1));
110
- },
111
- readUint16() {
112
- return view.getUint16(roAdd(2));
113
- },
114
- readUint32() {
115
- return view.getUint32(roAdd(4));
116
- },
117
- readUint64() {
118
- return view.getBigUint64(roAdd(8));
119
- },
120
- readBuffer() {
121
- const length = view.getUint32(roAdd(4));
122
- return buffer.subarray(roAdd(length), roAdd(0));
123
- },
124
- readUtf8String() {
125
- const length = view.getUint32(roAdd(4));
126
- return utf8.read(buffer, roAdd(length), roAdd(0));
127
- },
128
- incrementWriteOffset(amount) {
129
- return woAdd(amount);
130
- },
131
- size() {
132
- return woffset;
133
- },
134
- toBinary() {
135
- return buffer.subarray(0, woffset);
136
- },
137
- toCopiedBinary() {
138
- return new Uint8Array(this.toBinary());
139
- },
140
- writeBuffer(value, writeLength = true) {
141
- if (writeLength) {
142
- this.writeUint32(value.byteLength);
143
- }
144
- const o = woAdd(value.byteLength);
145
- buffer.set(value, o);
146
- },
147
- writeUtf8String(value, writeLength = true) {
148
- const byteLength = utf8.length(value);
149
- if (writeLength) {
150
- this.writeUint32(byteLength);
151
- }
152
- const o = woAdd(byteLength);
153
- utf8.write(value, buffer, o);
154
- },
155
- writeFloat32(value) {
156
- const o = woAdd(4);
157
- view.setFloat32(o, value);
158
- },
159
- writeFloat64(value) {
160
- const o = woAdd(8);
161
- view.setFloat64(o, value);
162
- },
163
- writeInt8(value) {
164
- const o = woAdd(1);
165
- view.setInt8(o, value);
166
- },
167
- writeInt16(value) {
168
- const o = woAdd(2);
169
- view.setInt16(o, value);
170
- },
171
- writeInt32(value) {
172
- const o = woAdd(4);
173
- view.setInt32(o, value);
174
- },
175
- writeInt64(value) {
176
- const o = woAdd(8);
177
- view.setBigInt64(o, value);
178
- },
179
- writeUint8(value) {
180
- const o = woAdd(1);
181
- view.setUint8(o, value);
182
- },
183
- writeUint16(value) {
184
- const o = woAdd(2);
185
- view.setUint16(o, value);
186
- },
187
- writeUint32(value) {
188
- const o = woAdd(4);
189
- view.setUint32(o, value);
190
- },
191
- writeUint64(value) {
192
- const o = woAdd(8);
193
- view.setBigUint64(o, value);
194
- },
195
- // Dataview Proxy
196
- getFloat32(offset) {
197
- return view.getFloat32(offset);
198
- },
199
- getFloat64(offset) {
200
- return view.getFloat64(offset);
201
- },
202
- getInt8(offset) {
203
- return view.getInt8(offset);
204
- },
205
- getInt16(offset) {
206
- return view.getInt16(offset);
207
- },
208
- getInt32(offset) {
209
- return view.getInt32(offset);
210
- },
211
- getInt64(offset) {
212
- return view.getBigInt64(offset);
213
- },
214
- getUint8(offset) {
215
- return view.getUint8(offset);
216
- },
217
- getUint16(offset) {
218
- return view.getUint16(offset);
219
- },
220
- getUint32(offset) {
221
- return view.getUint32(offset);
222
- },
223
- getUint64(offset) {
224
- return view.getBigUint64(offset);
225
- },
226
- setFloat32(offset, value) {
227
- view.setFloat32(offset, value);
228
- },
229
- setFloat64(offset, value) {
230
- view.setFloat64(offset, value);
231
- },
232
- setInt8(offset, value) {
233
- view.setInt8(offset, value);
234
- },
235
- setInt16(offset, value) {
236
- view.setInt16(offset, value);
237
- },
238
- setInt32(offset, value) {
239
- view.setInt32(offset, value);
240
- },
241
- setInt64(offset, value) {
242
- view.setBigInt64(offset, value);
243
- },
244
- setUint8(offset, value) {
245
- view.setUint8(offset, value);
246
- },
247
- setUint16(offset, value) {
248
- view.setUint16(offset, value);
249
- },
250
- setUint32(offset, value) {
251
- view.setUint32(offset, value);
252
- },
253
- setUint64(offset, value) {
254
- view.setBigUint64(offset, value);
113
+ const o = __classPrivateFieldGet(this, _ReadWriteByteBuffer_instances, "m", _ReadWriteByteBuffer_woAdd).call(this, value.byteLength);
114
+ this._buffer.set(value, o);
115
+ }
116
+ writeUtf8String(value, writeLength = true) {
117
+ const byteLength = utf8.length(value);
118
+ if (writeLength) {
119
+ this.writeUint32(byteLength);
255
120
  }
256
- };
121
+ const o = __classPrivateFieldGet(this, _ReadWriteByteBuffer_instances, "m", _ReadWriteByteBuffer_woAdd).call(this, byteLength);
122
+ utf8.write(value, this._buffer, o);
123
+ }
124
+ writeFloat32(value) {
125
+ const o = __classPrivateFieldGet(this, _ReadWriteByteBuffer_instances, "m", _ReadWriteByteBuffer_woAdd).call(this, 4);
126
+ this.view.setFloat32(o, value);
127
+ }
128
+ writeFloat64(value) {
129
+ const o = __classPrivateFieldGet(this, _ReadWriteByteBuffer_instances, "m", _ReadWriteByteBuffer_woAdd).call(this, 8);
130
+ this.view.setFloat64(o, value);
131
+ }
132
+ writeInt8(value) {
133
+ const o = __classPrivateFieldGet(this, _ReadWriteByteBuffer_instances, "m", _ReadWriteByteBuffer_woAdd).call(this, 1);
134
+ this.view.setInt8(o, value);
135
+ }
136
+ writeInt16(value) {
137
+ const o = __classPrivateFieldGet(this, _ReadWriteByteBuffer_instances, "m", _ReadWriteByteBuffer_woAdd).call(this, 2);
138
+ this.view.setInt16(o, value);
139
+ }
140
+ writeInt32(value) {
141
+ const o = __classPrivateFieldGet(this, _ReadWriteByteBuffer_instances, "m", _ReadWriteByteBuffer_woAdd).call(this, 4);
142
+ this.view.setInt32(o, value);
143
+ }
144
+ writeInt64(value) {
145
+ const o = __classPrivateFieldGet(this, _ReadWriteByteBuffer_instances, "m", _ReadWriteByteBuffer_woAdd).call(this, 8);
146
+ this.view.setBigInt64(o, value);
147
+ }
148
+ writeUint8(value) {
149
+ const o = __classPrivateFieldGet(this, _ReadWriteByteBuffer_instances, "m", _ReadWriteByteBuffer_woAdd).call(this, 1);
150
+ this.view.setUint8(o, value);
151
+ }
152
+ writeUint16(value) {
153
+ const o = __classPrivateFieldGet(this, _ReadWriteByteBuffer_instances, "m", _ReadWriteByteBuffer_woAdd).call(this, 2);
154
+ this.view.setUint16(o, value);
155
+ }
156
+ writeUint32(value) {
157
+ const o = __classPrivateFieldGet(this, _ReadWriteByteBuffer_instances, "m", _ReadWriteByteBuffer_woAdd).call(this, 4);
158
+ this.view.setUint32(o, value);
159
+ }
160
+ writeUint64(value) {
161
+ const o = __classPrivateFieldGet(this, _ReadWriteByteBuffer_instances, "m", _ReadWriteByteBuffer_woAdd).call(this, 8);
162
+ this.view.setBigUint64(o, value);
163
+ }
164
+ // DataView Proxy
165
+ getFloat32(offset) {
166
+ return this.view.getFloat32(offset);
167
+ }
168
+ getFloat64(offset) {
169
+ return this.view.getFloat64(offset);
170
+ }
171
+ getInt8(offset) {
172
+ return this.view.getInt8(offset);
173
+ }
174
+ getInt16(offset) {
175
+ return this.view.getInt16(offset);
176
+ }
177
+ getInt32(offset) {
178
+ return this.view.getInt32(offset);
179
+ }
180
+ getInt64(offset) {
181
+ return this.view.getBigInt64(offset);
182
+ }
183
+ getUint8(offset) {
184
+ return this.view.getUint8(offset);
185
+ }
186
+ getUint16(offset) {
187
+ return this.view.getUint16(offset);
188
+ }
189
+ getUint32(offset) {
190
+ return this.view.getUint32(offset);
191
+ }
192
+ getUint64(offset) {
193
+ return this.view.getBigUint64(offset);
194
+ }
195
+ setFloat32(offset, value) {
196
+ this.view.setFloat32(offset, value);
197
+ }
198
+ setFloat64(offset, value) {
199
+ this.view.setFloat64(offset, value);
200
+ }
201
+ setInt8(offset, value) {
202
+ this.view.setInt8(offset, value);
203
+ }
204
+ setInt16(offset, value) {
205
+ this.view.setInt16(offset, value);
206
+ }
207
+ setInt32(offset, value) {
208
+ this.view.setInt32(offset, value);
209
+ }
210
+ setInt64(offset, value) {
211
+ this.view.setBigInt64(offset, value);
212
+ }
213
+ setUint8(offset, value) {
214
+ this.view.setUint8(offset, value);
215
+ }
216
+ setUint16(offset, value) {
217
+ this.view.setUint16(offset, value);
218
+ }
219
+ setUint32(offset, value) {
220
+ this.view.setUint32(offset, value);
221
+ }
222
+ setUint64(offset, value) {
223
+ this.view.setBigUint64(offset, value);
224
+ }
257
225
  }
226
+ _ReadWriteByteBuffer_instances = new WeakSet(), _ReadWriteByteBuffer_woAdd = function _ReadWriteByteBuffer_woAdd(amount) {
227
+ if (this.woffset + amount > this._buffer.byteLength) {
228
+ const newsize = getNextSize(this._buffer.byteLength, this.woffset + amount);
229
+ const newBuffer = new Uint8Array(newsize);
230
+ newBuffer.set(this._buffer);
231
+ const oldOffset = this._buffer.byteOffset;
232
+ this._buffer = newBuffer;
233
+ this.view = new DataView(this._buffer.buffer, oldOffset);
234
+ }
235
+ this.woffset += amount;
236
+ return this.woffset - amount;
237
+ }, _ReadWriteByteBuffer_roAdd = function _ReadWriteByteBuffer_roAdd(amount) {
238
+ if (this.roffset + amount > this.woffset) {
239
+ throw new Error('Outside of the bounds of writen data.');
240
+ }
241
+ this.roffset += amount;
242
+ return this.roffset - amount;
243
+ };
@@ -0,0 +1,11 @@
1
+ import { Entity } from '../../engine/entity';
2
+ import { ByteBuffer } from '../ByteBuffer';
3
+ import { DeleteComponentMessage } from './types';
4
+ export declare namespace DeleteComponent {
5
+ const MESSAGE_HEADER_LENGTH = 20;
6
+ /**
7
+ * Write DeleteComponent message
8
+ */
9
+ function write(entity: Entity, componentId: number, timestamp: number, buf: ByteBuffer): void;
10
+ function read(buf: ByteBuffer): DeleteComponentMessage | null;
11
+ }
@@ -0,0 +1,45 @@
1
+ import CrdtMessageProtocol from '.';
2
+ import { CrdtMessageType, CRDT_MESSAGE_HEADER_LENGTH } from './types';
3
+ export var DeleteComponent;
4
+ (function (DeleteComponent) {
5
+ // TODO: change timestamp to 32 bit and remove buffer length (-8 bytes)
6
+ DeleteComponent.MESSAGE_HEADER_LENGTH = 20;
7
+ /**
8
+ * Write DeleteComponent message
9
+ */
10
+ function write(entity, componentId, timestamp, buf) {
11
+ // reserve the beginning
12
+ const messageLength = CRDT_MESSAGE_HEADER_LENGTH + DeleteComponent.MESSAGE_HEADER_LENGTH;
13
+ const startMessageOffset = buf.incrementWriteOffset(messageLength);
14
+ // Write CrdtMessage header
15
+ buf.setUint32(startMessageOffset, messageLength);
16
+ buf.setUint32(startMessageOffset + 4, CrdtMessageType.DELETE_COMPONENT);
17
+ // Write ComponentOperation header
18
+ buf.setUint32(startMessageOffset + 8, entity);
19
+ buf.setUint32(startMessageOffset + 12, componentId);
20
+ // TODO: change timestamp to 32bit (-4 bytes)
21
+ buf.setUint64(startMessageOffset + 16, BigInt(timestamp));
22
+ // TODO: remove buffer length (-4 bytes)
23
+ buf.setUint32(startMessageOffset + 24, 0);
24
+ }
25
+ DeleteComponent.write = write;
26
+ function read(buf) {
27
+ const header = CrdtMessageProtocol.readHeader(buf);
28
+ if (!header) {
29
+ return null;
30
+ }
31
+ if (header.type !== CrdtMessageType.DELETE_COMPONENT) {
32
+ throw new Error('DeleteComponentOperation tried to read another message type.');
33
+ }
34
+ const msg = {
35
+ ...header,
36
+ entityId: buf.readUint32(),
37
+ componentId: buf.readInt32(),
38
+ timestamp: Number(buf.readUint64())
39
+ };
40
+ // TODO: remove buffer length
41
+ buf.incrementReadOffset(4);
42
+ return msg;
43
+ }
44
+ DeleteComponent.read = read;
45
+ })(DeleteComponent || (DeleteComponent = {}));
@@ -0,0 +1,8 @@
1
+ import { Entity } from '../../engine/entity';
2
+ import { ByteBuffer } from '../ByteBuffer';
3
+ import { DeleteEntityMessage } from './types';
4
+ export declare namespace DeleteEntity {
5
+ const MESSAGE_HEADER_LENGTH = 4;
6
+ function write(entity: Entity, buf: ByteBuffer): void;
7
+ function read(buf: ByteBuffer): DeleteEntityMessage | null;
8
+ }
@@ -0,0 +1,28 @@
1
+ import CrdtMessageProtocol from '.';
2
+ import { CrdtMessageType, CRDT_MESSAGE_HEADER_LENGTH } from './types';
3
+ export var DeleteEntity;
4
+ (function (DeleteEntity) {
5
+ DeleteEntity.MESSAGE_HEADER_LENGTH = 4;
6
+ function write(entity, buf) {
7
+ // Write CrdtMessage header
8
+ buf.writeUint32(CRDT_MESSAGE_HEADER_LENGTH + 4);
9
+ buf.writeUint32(CrdtMessageType.DELETE_ENTITY);
10
+ // body
11
+ buf.writeUint32(entity);
12
+ }
13
+ DeleteEntity.write = write;
14
+ function read(buf) {
15
+ const header = CrdtMessageProtocol.readHeader(buf);
16
+ if (!header) {
17
+ return null;
18
+ }
19
+ if (header.type !== CrdtMessageType.DELETE_ENTITY) {
20
+ throw new Error('DeleteComponentOperation tried to read another message type.');
21
+ }
22
+ return {
23
+ ...header,
24
+ entityId: buf.readUint32()
25
+ };
26
+ }
27
+ DeleteEntity.read = read;
28
+ })(DeleteEntity || (DeleteEntity = {}));
@@ -0,0 +1,32 @@
1
+ import { ByteBuffer } from '../ByteBuffer';
2
+ import { CrdtMessageHeader } from './types';
3
+ export * from './types';
4
+ export * from './deleteComponent';
5
+ export * from './putComponent';
6
+ export * from './deleteEntity';
7
+ export declare namespace CrdtMessageProtocol {
8
+ /**
9
+ * Validate if the message incoming is completed
10
+ * @param buf - ByteBuffer
11
+ */
12
+ function validate(buf: ByteBuffer): boolean;
13
+ /**
14
+ * Get the current header, consuming the bytes involved.
15
+ * @param buf - ByteBuffer
16
+ * @returns header or null if there is no validated message
17
+ */
18
+ function readHeader(buf: ByteBuffer): CrdtMessageHeader | null;
19
+ /**
20
+ * Get the current header, without consuming the bytes involved.
21
+ * @param buf - ByteBuffer
22
+ * @returns header or null if there is no validated message
23
+ */
24
+ function getHeader(buf: ByteBuffer): CrdtMessageHeader | null;
25
+ /**
26
+ * Consume the incoming message without processing it.
27
+ * @param buf - ByteBuffer
28
+ * @returns true in case of success or false if there is no valid message.
29
+ */
30
+ function consumeMessage(buf: ByteBuffer): boolean;
31
+ }
32
+ export default CrdtMessageProtocol;
@@ -0,0 +1,70 @@
1
+ import { CRDT_MESSAGE_HEADER_LENGTH } from './types';
2
+ export * from './types';
3
+ export * from './deleteComponent';
4
+ export * from './putComponent';
5
+ export * from './deleteEntity';
6
+ export var CrdtMessageProtocol;
7
+ (function (CrdtMessageProtocol) {
8
+ /**
9
+ * Validate if the message incoming is completed
10
+ * @param buf - ByteBuffer
11
+ */
12
+ function validate(buf) {
13
+ const rem = buf.remainingBytes();
14
+ if (rem < CRDT_MESSAGE_HEADER_LENGTH) {
15
+ return false;
16
+ }
17
+ const messageLength = buf.getUint32(buf.currentReadOffset());
18
+ if (rem < messageLength) {
19
+ return false;
20
+ }
21
+ return true;
22
+ }
23
+ CrdtMessageProtocol.validate = validate;
24
+ /**
25
+ * Get the current header, consuming the bytes involved.
26
+ * @param buf - ByteBuffer
27
+ * @returns header or null if there is no validated message
28
+ */
29
+ function readHeader(buf) {
30
+ if (!validate(buf)) {
31
+ return null;
32
+ }
33
+ return {
34
+ length: buf.readUint32(),
35
+ type: buf.readUint32()
36
+ };
37
+ }
38
+ CrdtMessageProtocol.readHeader = readHeader;
39
+ /**
40
+ * Get the current header, without consuming the bytes involved.
41
+ * @param buf - ByteBuffer
42
+ * @returns header or null if there is no validated message
43
+ */
44
+ function getHeader(buf) {
45
+ if (!validate(buf)) {
46
+ return null;
47
+ }
48
+ const currentOffset = buf.currentReadOffset();
49
+ return {
50
+ length: buf.getUint32(currentOffset),
51
+ type: buf.getUint32(currentOffset + 4)
52
+ };
53
+ }
54
+ CrdtMessageProtocol.getHeader = getHeader;
55
+ /**
56
+ * Consume the incoming message without processing it.
57
+ * @param buf - ByteBuffer
58
+ * @returns true in case of success or false if there is no valid message.
59
+ */
60
+ function consumeMessage(buf) {
61
+ const header = getHeader(buf);
62
+ if (!header) {
63
+ return false;
64
+ }
65
+ buf.incrementReadOffset(header.length);
66
+ return true;
67
+ }
68
+ CrdtMessageProtocol.consumeMessage = consumeMessage;
69
+ })(CrdtMessageProtocol || (CrdtMessageProtocol = {}));
70
+ export default CrdtMessageProtocol;
@@ -0,0 +1,3 @@
1
+ import { ByteBuffer } from '../ByteBuffer';
2
+ import { CrdtMessage } from './types';
3
+ export declare function readMessage(buf: ByteBuffer): CrdtMessage | null;
@@ -0,0 +1,17 @@
1
+ import CrdtMessageProtocol, { DeleteComponent, DeleteEntity, PutComponentOperation } from '.';
2
+ import { CrdtMessageType } from './types';
3
+ export function readMessage(buf) {
4
+ const header = CrdtMessageProtocol.getHeader(buf);
5
+ if (!header)
6
+ return null;
7
+ if (header.type === CrdtMessageType.PUT_COMPONENT) {
8
+ return PutComponentOperation.read(buf);
9
+ }
10
+ else if (header.type === CrdtMessageType.DELETE_COMPONENT) {
11
+ return DeleteComponent.read(buf);
12
+ }
13
+ else if (header.type === CrdtMessageType.DELETE_ENTITY) {
14
+ return DeleteEntity.read(buf);
15
+ }
16
+ return null;
17
+ }