@colyseus/schema 3.0.0-alpha.0 → 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 +1219 -1427
  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 +1219 -1427
  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 +19 -16
  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 +43 -37
@@ -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 &&
@@ -22,6 +22,9 @@
22
22
  */
23
23
 
24
24
  import type { TextEncoder } from "util";
25
+ import type { Iterator } from "./decode";
26
+
27
+ export type BufferLike = number[] | ArrayBufferLike;
25
28
 
26
29
  /**
27
30
  * msgpack implementation highly based on notepack.io
@@ -32,28 +35,32 @@ let textEncoder: TextEncoder;
32
35
  // @ts-ignore
33
36
  try { textEncoder = new TextEncoder(); } catch (e) { }
34
37
 
35
- function utf8Length(str) {
36
- var c = 0, length = 0;
37
- for (var i = 0, l = str.length; i < l; i++) {
38
- c = str.charCodeAt(i);
39
- if (c < 0x80) {
40
- length += 1;
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;
41
61
  }
42
- else if (c < 0x800) {
43
- length += 2;
44
- }
45
- else if (c < 0xd800 || c >= 0xe000) {
46
- length += 3;
47
- }
48
- else {
49
- i++;
50
- length += 4;
51
- }
52
- }
53
- return length;
54
- }
55
62
 
56
- export function utf8Write(view, str, it) {
63
+ export function utf8Write(view: BufferLike, str: string, it: Iterator) {
57
64
  var c = 0;
58
65
  for (var i = 0, l = str.length; i < l; i++) {
59
66
  c = str.charCodeAt(i);
@@ -80,32 +87,32 @@ export function utf8Write(view, str, it) {
80
87
  }
81
88
  }
82
89
 
83
- export function int8(bytes, value, it) {
90
+ export function int8(bytes: BufferLike, value: number, it: Iterator) {
84
91
  bytes[it.offset++] = value & 255;
85
92
  };
86
93
 
87
- export function uint8(bytes, value, it) {
94
+ export function uint8(bytes: BufferLike, value: number, it: Iterator) {
88
95
  bytes[it.offset++] = value & 255;
89
96
  };
90
97
 
91
- export function int16(bytes, value, it) {
98
+ export function int16(bytes: BufferLike, value: number, it: Iterator) {
92
99
  bytes[it.offset++] = value & 255;
93
100
  bytes[it.offset++] = (value >> 8) & 255;
94
101
  };
95
102
 
96
- export function uint16(bytes, value, it) {
103
+ export function uint16(bytes: BufferLike, value: number, it: Iterator) {
97
104
  bytes[it.offset++] = value & 255;
98
105
  bytes[it.offset++] = (value >> 8) & 255;
99
106
  };
100
107
 
101
- export function int32(bytes, value, it) {
108
+ export function int32(bytes: BufferLike, value: number, it: Iterator) {
102
109
  bytes[it.offset++] = value & 255;
103
110
  bytes[it.offset++] = (value >> 8) & 255;
104
111
  bytes[it.offset++] = (value >> 16) & 255;
105
112
  bytes[it.offset++] = (value >> 24) & 255;
106
113
  };
107
114
 
108
- export function uint32(bytes, value, it) {
115
+ export function uint32(bytes: BufferLike, value: number, it: Iterator) {
109
116
  const b4 = value >> 24;
110
117
  const b3 = value >> 16;
111
118
  const b2 = value >> 8;
@@ -116,25 +123,25 @@ export function uint32(bytes, value, it) {
116
123
  bytes[it.offset++] = b4 & 255;
117
124
  };
118
125
 
119
- export function int64(bytes, value, it) {
126
+ export function int64(bytes: BufferLike, value: number, it: Iterator) {
120
127
  const high = Math.floor(value / Math.pow(2, 32));
121
128
  const low = value >>> 0;
122
129
  uint32(bytes, low, it);
123
130
  uint32(bytes, high, it);
124
131
  };
125
132
 
126
- export function uint64(bytes, value, it) {
133
+ export function uint64(bytes: BufferLike, value: number, it: Iterator) {
127
134
  const high = (value / Math.pow(2, 32)) >> 0;
128
135
  const low = value >>> 0;
129
136
  uint32(bytes, low, it);
130
137
  uint32(bytes, high, it);
131
138
  };
132
139
 
133
- export function float32(bytes, value, it) {
140
+ export function float32(bytes: BufferLike, value: number, it: Iterator) {
134
141
  writeFloat32(bytes, value, it);
135
142
  }
136
143
 
137
- export function float64(bytes, value, it) {
144
+ export function float64(bytes: BufferLike, value: number, it: Iterator) {
138
145
  writeFloat64(bytes, value, it);
139
146
  }
140
147
 
@@ -144,27 +151,26 @@ const _int32 = new Int32Array(2);
144
151
  const _float32 = new Float32Array(_int32.buffer);
145
152
  const _float64 = new Float64Array(_int32.buffer);
146
153
 
147
- export function writeFloat32(bytes, value, it) {
154
+ export function writeFloat32(bytes: BufferLike, value: number, it: Iterator) {
148
155
  _float32[0] = value;
149
156
  int32(bytes, _int32[0], it);
150
157
  };
151
158
 
152
- export function writeFloat64(bytes, value, it) {
159
+ export function writeFloat64(bytes: BufferLike, value: number, it: Iterator) {
153
160
  _float64[0] = value;
154
161
  int32(bytes, _int32[_isLittleEndian ? 0 : 1], it);
155
162
  int32(bytes, _int32[_isLittleEndian ? 1 : 0], it);
156
163
  };
157
164
 
158
- export function boolean(bytes, value, it) {
165
+ export function boolean(bytes: BufferLike, value: number, it: Iterator) {
159
166
  bytes[it.offset++] = value ? 1 : 0; // uint8
160
167
  };
161
168
 
162
- export function string(bytes: Buffer, value, it) {
169
+ export function string(bytes: BufferLike, value: string, it: Iterator) {
163
170
  // encode `null` strings as empty.
164
171
  if (!value) { value = ""; }
165
172
 
166
- // let length = utf8Length(value);
167
- let length = Buffer.byteLength(value, "utf8");
173
+ let length = utf8Length(value, "utf8");
168
174
  let size = 0;
169
175
 
170
176
  // fixstr
@@ -198,7 +204,7 @@ export function string(bytes: Buffer, value, it) {
198
204
  return size + length;
199
205
  }
200
206
 
201
- export function number(bytes, value, it) {
207
+ export function number(bytes: BufferLike, value: number, it: Iterator) {
202
208
  if (isNaN(value)) {
203
209
  return number(bytes, 0, it);
204
210