@colyseus/schema 3.0.0-alpha.1 → 3.0.0-alpha.10

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.
Files changed (40) hide show
  1. package/build/cjs/index.js +85 -64
  2. package/build/cjs/index.js.map +1 -1
  3. package/build/esm/index.mjs +85 -64
  4. package/build/esm/index.mjs.map +1 -1
  5. package/build/umd/index.js +85 -64
  6. package/lib/Reflection.d.ts +1 -1
  7. package/lib/Reflection.js +1 -2
  8. package/lib/Reflection.js.map +1 -1
  9. package/lib/decoder/DecodeOperation.js +2 -2
  10. package/lib/decoder/DecodeOperation.js.map +1 -1
  11. package/lib/decoder/Decoder.d.ts +1 -1
  12. package/lib/decoder/Decoder.js +4 -4
  13. package/lib/decoder/Decoder.js.map +1 -1
  14. package/lib/decoder/strategy/StateCallbacks.js +1 -1
  15. package/lib/decoder/strategy/StateCallbacks.js.map +1 -1
  16. package/lib/encoder/ChangeTree.d.ts +0 -2
  17. package/lib/encoder/ChangeTree.js +2 -6
  18. package/lib/encoder/ChangeTree.js.map +1 -1
  19. package/lib/encoder/Encoder.d.ts +5 -4
  20. package/lib/encoder/Encoder.js +46 -43
  21. package/lib/encoder/Encoder.js.map +1 -1
  22. package/lib/encoder/StateView.d.ts +2 -0
  23. package/lib/encoder/StateView.js +4 -1
  24. package/lib/encoder/StateView.js.map +1 -1
  25. package/lib/encoding/decode.d.ts +21 -19
  26. package/lib/encoding/decode.js +6 -6
  27. package/lib/encoding/decode.js.map +1 -1
  28. package/lib/encoding/encode.d.ts +3 -2
  29. package/lib/encoding/encode.js +24 -22
  30. package/lib/encoding/encode.js.map +1 -1
  31. package/package.json +1 -1
  32. package/src/Reflection.ts +1 -2
  33. package/src/decoder/DecodeOperation.ts +3 -3
  34. package/src/decoder/Decoder.ts +5 -5
  35. package/src/decoder/strategy/StateCallbacks.ts +1 -1
  36. package/src/encoder/ChangeTree.ts +2 -6
  37. package/src/encoder/Encoder.ts +51 -47
  38. package/src/encoder/StateView.ts +4 -0
  39. package/src/encoding/decode.ts +24 -25
  40. package/src/encoding/encode.ts +26 -23
@@ -8,8 +8,7 @@ import type { Iterator } from "../encoding/decode";
8
8
  import { OPERATION, SWITCH_TO_STRUCTURE, TYPE_ID } from '../encoding/spec';
9
9
  import { Root } from "./ChangeTree";
10
10
  import { getNextPowerOf2 } from "../utils";
11
- import { StateView } from "./StateView";
12
- import { Metadata } from "../Metadata";
11
+ import type { StateView } from "./StateView";
13
12
 
14
13
  export class Encoder<T extends Schema = any> {
15
14
  static BUFFER_SIZE = 8 * 1024;// 8KB
@@ -18,7 +17,7 @@ export class Encoder<T extends Schema = any> {
18
17
  context: TypeContext;
19
18
  state: T;
20
19
 
21
- $root: Root;
20
+ root: Root;
22
21
 
23
22
  constructor(root: T) {
24
23
  this.setRoot(root);
@@ -36,20 +35,20 @@ export class Encoder<T extends Schema = any> {
36
35
  }
37
36
 
38
37
  protected setRoot(state: T) {
39
- this.$root = new Root();
38
+ this.root = new Root();
40
39
  this.state = state;
41
- state[$changes].setRoot(this.$root);
40
+ state[$changes].setRoot(this.root);
42
41
  }
43
42
 
44
43
  encode(
45
44
  it: Iterator = { offset: 0 },
46
45
  view?: StateView,
47
- bytes = this.sharedBuffer,
48
- changeTrees = this.$root.changes
46
+ buffer = this.sharedBuffer,
47
+ changeTrees = this.root.changes
49
48
  ): Buffer {
50
49
  const initialOffset = it.offset; // cache current offset in case we need to resize the buffer
51
50
 
52
- const isEncodeAll = this.$root.allChanges === changeTrees;
51
+ const isEncodeAll = this.root.allChanges === changeTrees;
53
52
  const hasView = (view !== undefined);
54
53
  const rootChangeTree = this.state[$changes];
55
54
 
@@ -74,8 +73,8 @@ export class Encoder<T extends Schema = any> {
74
73
 
75
74
  // skip root `refId` if it's the first change tree
76
75
  if (it.offset !== initialOffset || changeTree !== rootChangeTree) {
77
- bytes[it.offset++] = SWITCH_TO_STRUCTURE & 255;
78
- encode.number(bytes, changeTree.refId, it);
76
+ buffer[it.offset++] = SWITCH_TO_STRUCTURE & 255;
77
+ encode.number(buffer, changeTree.refId, it);
79
78
  }
80
79
 
81
80
  const changesIterator = changes.entries();
@@ -102,19 +101,25 @@ export class Encoder<T extends Schema = any> {
102
101
  // operation: OPERATION[operation],
103
102
  // });
104
103
 
105
- encoder(this, bytes, changeTree, fieldIndex, operation, it, isEncodeAll, hasView);
104
+ encoder(this, buffer, changeTree, fieldIndex, operation, it, isEncodeAll, hasView);
106
105
  }
107
106
  }
108
107
 
109
- if (it.offset > bytes.byteLength) {
110
- const newSize = getNextPowerOf2(this.sharedBuffer.byteLength * 2);
111
- console.warn("@colyseus/schema encode buffer overflow. Current buffer size: " + bytes.byteLength + ", encoding offset: " + it.offset + ", new size: " + newSize);
108
+ if (it.offset > buffer.byteLength) {
109
+ const newSize = getNextPowerOf2(buffer.byteLength * 2);
110
+ console.warn("@colyseus/schema encode buffer overflow. Current buffer size: " + buffer.byteLength + ", encoding offset: " + it.offset + ", new size: " + newSize);
112
111
 
113
112
  //
114
113
  // resize buffer and re-encode (TODO: can we avoid re-encoding here?)
115
114
  //
116
- this.sharedBuffer = Buffer.allocUnsafeSlow(newSize);
117
- return this.encode({ offset: initialOffset }, view);
115
+ buffer = Buffer.allocUnsafeSlow(newSize);
116
+
117
+ // assign resized buffer to local sharedBuffer
118
+ if (buffer === this.sharedBuffer) {
119
+ this.sharedBuffer = buffer;
120
+ }
121
+
122
+ return this.encode({ offset: initialOffset }, view, buffer);
118
123
 
119
124
  } else {
120
125
  //
@@ -127,53 +132,52 @@ export class Encoder<T extends Schema = any> {
127
132
  this.onEndEncode(changeTrees);
128
133
  }
129
134
 
130
- // return bytes;
131
- return bytes.slice(0, it.offset);
135
+ return buffer.subarray(0, it.offset);
132
136
  }
133
137
  }
134
138
 
135
- encodeAll(it: Iterator = { offset: 0 }) {
136
- // console.log(`encodeAll(), this.$root.allChanges (${this.$root.allChanges.size})`);
139
+ encodeAll(it: Iterator = { offset: 0 }, buffer: Buffer = this.sharedBuffer) {
140
+ // console.log(`encodeAll(), this.root.allChanges (${this.root.allChanges.size})`);
137
141
 
138
- // Array.from(this.$root.allChanges.entries()).map((item) => {
139
- // console.log("->", item[0].refId, item[0].ref.toJSON());
142
+ // Array.from(this.root.allChanges.entries()).map((item) => {
143
+ // console.log("->", { ref: item[0].ref.constructor.name, refId: item[0].refId, changes: item[1].size });
140
144
  // });
141
145
 
142
- return this.encode(it, undefined, this.sharedBuffer, this.$root.allChanges);
146
+ return this.encode(it, undefined, buffer, this.root.allChanges);
143
147
  }
144
148
 
145
149
  encodeAllView(view: StateView, sharedOffset: number, it: Iterator, bytes = this.sharedBuffer) {
146
150
  const viewOffset = it.offset;
147
151
 
148
- // console.log(`encodeAllView(), this.$root.allFilteredChanges (${this.$root.allFilteredChanges.size})`);
152
+ // console.log(`encodeAllView(), this.root.allFilteredChanges (${this.root.allFilteredChanges.size})`);
149
153
  // this.debugAllFilteredChanges();
150
154
 
151
155
  // try to encode "filtered" changes
152
- this.encode(it, view, bytes, this.$root.allFilteredChanges);
156
+ this.encode(it, view, bytes, this.root.allFilteredChanges);
153
157
 
154
158
  return Buffer.concat([
155
- bytes.slice(0, sharedOffset),
156
- bytes.slice(viewOffset, it.offset)
159
+ bytes.subarray(0, sharedOffset),
160
+ bytes.subarray(viewOffset, it.offset)
157
161
  ]);
158
162
  }
159
163
 
160
164
 
161
- // debugAllFilteredChanges() {
162
- // Array.from(this.$root.allFilteredChanges.entries()).map((item) => {
163
- // console.log("->", { refId: item[0].refId }, item[0].ref.toJSON());
164
- // if (Array.isArray(item[0].ref.toJSON())) {
165
- // item[1].forEach((op, key) => {
166
- // console.log(" ->", { key, op: OPERATION[op] });
167
- // })
168
- // }
169
- // });
170
- // }
165
+ debugAllFilteredChanges() {
166
+ Array.from(this.root.allFilteredChanges.entries()).map((item) => {
167
+ console.log("->", { refId: item[0].refId, changes: item[1].size }, item[0].ref.toJSON());
168
+ if (Array.isArray(item[0].ref.toJSON())) {
169
+ item[1].forEach((op, key) => {
170
+ console.log(" ->", { key, op: OPERATION[op] });
171
+ })
172
+ }
173
+ });
174
+ }
171
175
 
172
176
  encodeView(view: StateView, sharedOffset: number, it: Iterator, bytes = this.sharedBuffer) {
173
177
  const viewOffset = it.offset;
174
178
 
175
179
  // try to encode "filtered" changes
176
- this.encode(it, view, bytes, this.$root.filteredChanges);
180
+ this.encode(it, view, bytes, this.root.filteredChanges);
177
181
 
178
182
  // encode visibility changes (add/remove for this view)
179
183
  const viewChangesIterator = view.changes.entries();
@@ -209,12 +213,12 @@ export class Encoder<T extends Schema = any> {
209
213
  view.changes.clear();
210
214
 
211
215
  return Buffer.concat([
212
- bytes.slice(0, sharedOffset),
213
- bytes.slice(viewOffset, it.offset)
216
+ bytes.subarray(0, sharedOffset),
217
+ bytes.subarray(viewOffset, it.offset)
214
218
  ]);
215
219
  }
216
220
 
217
- onEndEncode(changeTrees = this.$root.changes) {
221
+ onEndEncode(changeTrees = this.root.changes) {
218
222
  const changeTreesIterator = changeTrees.entries();
219
223
  for (const [changeTree, _] of changeTreesIterator) {
220
224
  changeTree.endEncode();
@@ -223,14 +227,14 @@ export class Encoder<T extends Schema = any> {
223
227
 
224
228
  discardChanges() {
225
229
  // discard shared changes
226
- if (this.$root.changes.size > 0) {
227
- this.onEndEncode(this.$root.changes);
228
- this.$root.changes.clear();
230
+ if (this.root.changes.size > 0) {
231
+ this.onEndEncode(this.root.changes);
232
+ this.root.changes.clear();
229
233
  }
230
234
  // discard filtered changes
231
- if (this.$root.filteredChanges.size > 0) {
232
- this.onEndEncode(this.$root.filteredChanges);
233
- this.$root.filteredChanges.clear();
235
+ if (this.root.filteredChanges.size > 0) {
236
+ this.onEndEncode(this.root.filteredChanges);
237
+ this.root.filteredChanges.clear();
234
238
  }
235
239
  }
236
240
 
@@ -3,6 +3,10 @@ import { $changes } from "../types/symbols";
3
3
  import { DEFAULT_VIEW_TAG } from "../annotations";
4
4
  import { OPERATION } from "../encoding/spec";
5
5
  import { Metadata } from "../Metadata";
6
+ import type { Schema } from "../Schema";
7
+
8
+ export function createView(root: Schema) {
9
+ }
6
10
 
7
11
  export class StateView {
8
12
  /**
@@ -22,6 +22,7 @@
22
22
  */
23
23
 
24
24
  import { SWITCH_TO_STRUCTURE } from "./spec";
25
+ import type { BufferLike } from "./encode";
25
26
 
26
27
  /**
27
28
  * msgpack implementation highly based on notepack.io
@@ -30,9 +31,9 @@ import { SWITCH_TO_STRUCTURE } from "./spec";
30
31
 
31
32
  export interface Iterator { offset: number; }
32
33
 
33
- function utf8Read(bytes, offset, length) {
34
+ export function utf8Read(bytes: BufferLike, it: Iterator, length: number) {
34
35
  var string = '', chr = 0;
35
- for (var i = offset, end = offset + length; i < end; i++) {
36
+ for (var i = it.offset, end = it.offset + length; i < end; i++) {
36
37
  var byte = bytes[i];
37
38
  if ((byte & 0x80) === 0x00) {
38
39
  string += String.fromCharCode(byte);
@@ -71,48 +72,49 @@ function utf8Read(bytes, offset, length) {
71
72
  // (do not throw error to avoid server/client from crashing due to hack attemps)
72
73
  // throw new Error('Invalid byte ' + byte.toString(16));
73
74
  }
75
+ it.offset += length;
74
76
  return string;
75
77
  }
76
78
 
77
- export function int8 (bytes: number[], it: Iterator) {
79
+ export function int8 (bytes: BufferLike, it: Iterator) {
78
80
  return uint8(bytes, it) << 24 >> 24;
79
81
  };
80
82
 
81
- export function uint8 (bytes: number[], it: Iterator) {
83
+ export function uint8 (bytes: BufferLike, it: Iterator) {
82
84
  return bytes[it.offset++];
83
85
  };
84
86
 
85
- export function int16 (bytes: number[], it: Iterator) {
87
+ export function int16 (bytes: BufferLike, it: Iterator) {
86
88
  return uint16(bytes, it) << 16 >> 16;
87
89
  };
88
90
 
89
- export function uint16 (bytes: number[], it: Iterator) {
91
+ export function uint16 (bytes: BufferLike, it: Iterator) {
90
92
  return bytes[it.offset++] | bytes[it.offset++] << 8;
91
93
  };
92
94
 
93
- export function int32 (bytes: number[], it: Iterator) {
95
+ export function int32 (bytes: BufferLike, it: Iterator) {
94
96
  return bytes[it.offset++] | bytes[it.offset++] << 8 | bytes[it.offset++] << 16 | bytes[it.offset++] << 24;
95
97
  };
96
98
 
97
- export function uint32 (bytes: number[], it: Iterator) {
99
+ export function uint32 (bytes: BufferLike, it: Iterator) {
98
100
  return int32(bytes, it) >>> 0;
99
101
  };
100
102
 
101
- export function float32(bytes: number[], it: Iterator) {
103
+ export function float32(bytes: BufferLike, it: Iterator) {
102
104
  return readFloat32(bytes, it);
103
105
  }
104
106
 
105
- export function float64(bytes: number[], it: Iterator) {
107
+ export function float64(bytes: BufferLike, it: Iterator) {
106
108
  return readFloat64(bytes, it);
107
109
  }
108
110
 
109
- export function int64(bytes: number[], it: Iterator) {
111
+ export function int64(bytes: BufferLike, it: Iterator) {
110
112
  const low = uint32(bytes, it);
111
113
  const high = int32(bytes, it) * Math.pow(2, 32);
112
114
  return high + low;
113
115
  };
114
116
 
115
- export function uint64(bytes: number[], it: Iterator) {
117
+ export function uint64(bytes: BufferLike, it: Iterator) {
116
118
  const low = uint32(bytes, it);
117
119
  const high = uint32(bytes, it) * Math.pow(2, 32);
118
120
  return high + low;
@@ -124,22 +126,22 @@ const _int32 = new Int32Array(2);
124
126
  const _float32 = new Float32Array(_int32.buffer);
125
127
  const _float64 = new Float64Array(_int32.buffer);
126
128
 
127
- export function readFloat32 (bytes: number[], it: Iterator) {
129
+ export function readFloat32 (bytes: BufferLike, it: Iterator) {
128
130
  _int32[0] = int32(bytes, it);
129
131
  return _float32[0];
130
132
  };
131
133
 
132
- export function readFloat64 (bytes: number[], it: Iterator) {
134
+ export function readFloat64 (bytes: BufferLike, it: Iterator) {
133
135
  _int32[_isLittleEndian ? 0 : 1] = int32(bytes, it);
134
136
  _int32[_isLittleEndian ? 1 : 0] = int32(bytes, it);
135
137
  return _float64[0];
136
138
  };
137
139
 
138
- export function boolean (bytes: number[], it: Iterator) {
140
+ export function boolean (bytes: BufferLike, it: Iterator) {
139
141
  return uint8(bytes, it) > 0;
140
142
  };
141
143
 
142
- export function string (bytes, it: Iterator) {
144
+ export function string (bytes: BufferLike, it: Iterator) {
143
145
  const prefix = bytes[it.offset++];
144
146
  let length: number;
145
147
 
@@ -157,13 +159,10 @@ export function string (bytes, it: Iterator) {
157
159
  length = uint32(bytes, it);
158
160
  }
159
161
 
160
- const value = utf8Read(bytes, it.offset, length);
161
- it.offset += length;
162
-
163
- return value;
162
+ return utf8Read(bytes, it, length);
164
163
  }
165
164
 
166
- export function stringCheck(bytes, it: Iterator) {
165
+ export function stringCheck(bytes: BufferLike, it: Iterator) {
167
166
  const prefix = bytes[it.offset];
168
167
  return (
169
168
  // fixstr
@@ -177,7 +176,7 @@ export function stringCheck(bytes, it: Iterator) {
177
176
  );
178
177
  }
179
178
 
180
- export function number (bytes, it: Iterator) {
179
+ export function number (bytes: BufferLike, it: Iterator) {
181
180
  const prefix = bytes[it.offset++];
182
181
 
183
182
  if (prefix < 0x80) {
@@ -230,7 +229,7 @@ export function number (bytes, it: Iterator) {
230
229
  }
231
230
  };
232
231
 
233
- export function numberCheck (bytes, it: Iterator) {
232
+ export function numberCheck (bytes: BufferLike, it: Iterator) {
234
233
  const prefix = bytes[it.offset];
235
234
  // positive fixint - 0x00 - 0x7f
236
235
  // float 32 - 0xca
@@ -249,7 +248,7 @@ export function numberCheck (bytes, it: Iterator) {
249
248
  );
250
249
  }
251
250
 
252
- export function arrayCheck (bytes, it: Iterator) {
251
+ export function arrayCheck (bytes: BufferLike, it: Iterator) {
253
252
  return bytes[it.offset] < 0xa0;
254
253
 
255
254
  // const prefix = bytes[it.offset] ;
@@ -268,7 +267,7 @@ export function arrayCheck (bytes, it: Iterator) {
268
267
  // return prefix;
269
268
  }
270
269
 
271
- export function switchStructureCheck(bytes, it: Iterator) {
270
+ export function switchStructureCheck(bytes: BufferLike, it: Iterator) {
272
271
  return (
273
272
  // previous byte should be `SWITCH_TO_STRUCTURE`
274
273
  bytes[it.offset - 1] === SWITCH_TO_STRUCTURE &&
@@ -35,28 +35,32 @@ let textEncoder: TextEncoder;
35
35
  // @ts-ignore
36
36
  try { textEncoder = new TextEncoder(); } catch (e) { }
37
37
 
38
- function utf8Length(str) {
39
- var c = 0, length = 0;
40
- for (var i = 0, l = str.length; i < l; i++) {
41
- c = str.charCodeAt(i);
42
- if (c < 0x80) {
43
- length += 1;
44
- }
45
- else if (c < 0x800) {
46
- length += 2;
47
- }
48
- else if (c < 0xd800 || c >= 0xe000) {
49
- length += 3;
50
- }
51
- else {
52
- i++;
53
- length += 4;
38
+ const hasBufferByteLength = (typeof Buffer !== 'undefined' && Buffer.byteLength);
39
+
40
+ export const utf8Length = (hasBufferByteLength)
41
+ ? Buffer.byteLength // node
42
+ : function (str: string, _?: any) {
43
+ var c = 0, length = 0;
44
+ for (var i = 0, l = str.length; i < l; i++) {
45
+ c = str.charCodeAt(i);
46
+ if (c < 0x80) {
47
+ length += 1;
48
+ }
49
+ else if (c < 0x800) {
50
+ length += 2;
51
+ }
52
+ else if (c < 0xd800 || c >= 0xe000) {
53
+ length += 3;
54
+ }
55
+ else {
56
+ i++;
57
+ length += 4;
58
+ }
59
+ }
60
+ return length;
54
61
  }
55
- }
56
- return length;
57
- }
58
62
 
59
- export function utf8Write(view, str, it) {
63
+ export function utf8Write(view: BufferLike, str: string, it: Iterator) {
60
64
  var c = 0;
61
65
  for (var i = 0, l = str.length; i < l; i++) {
62
66
  c = str.charCodeAt(i);
@@ -162,12 +166,11 @@ export function boolean(bytes: BufferLike, value: number, it: Iterator) {
162
166
  bytes[it.offset++] = value ? 1 : 0; // uint8
163
167
  };
164
168
 
165
- export function string(bytes: Buffer, value: string, it: Iterator) {
169
+ export function string(bytes: BufferLike, value: string, it: Iterator) {
166
170
  // encode `null` strings as empty.
167
171
  if (!value) { value = ""; }
168
172
 
169
- // let length = utf8Length(value);
170
- let length = Buffer.byteLength(value, "utf8");
173
+ let length = utf8Length(value, "utf8");
171
174
  let size = 0;
172
175
 
173
176
  // fixstr