@colyseus/schema 3.0.0-alpha.40 → 3.0.0-alpha.41

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 (50) hide show
  1. package/README.md +19 -3
  2. package/build/cjs/index.js +1205 -1223
  3. package/build/cjs/index.js.map +1 -1
  4. package/build/esm/index.mjs +1205 -1224
  5. package/build/esm/index.mjs.map +1 -1
  6. package/build/umd/index.js +1205 -1223
  7. package/lib/annotations.d.ts +1 -1
  8. package/lib/annotations.js.map +1 -1
  9. package/lib/codegen/languages/csharp.js +1 -44
  10. package/lib/codegen/languages/csharp.js.map +1 -1
  11. package/lib/decoder/DecodeOperation.d.ts +3 -3
  12. package/lib/decoder/DecodeOperation.js +9 -9
  13. package/lib/decoder/DecodeOperation.js.map +1 -1
  14. package/lib/decoder/Decoder.js +5 -5
  15. package/lib/decoder/Decoder.js.map +1 -1
  16. package/lib/encoder/EncodeOperation.js +7 -7
  17. package/lib/encoder/EncodeOperation.js.map +1 -1
  18. package/lib/encoder/Encoder.js +4 -4
  19. package/lib/encoder/Encoder.js.map +1 -1
  20. package/lib/encoding/assert.d.ts +5 -5
  21. package/lib/encoding/assert.js +8 -0
  22. package/lib/encoding/assert.js.map +1 -1
  23. package/lib/encoding/decode.d.ts +35 -20
  24. package/lib/encoding/decode.js +43 -86
  25. package/lib/encoding/decode.js.map +1 -1
  26. package/lib/encoding/encode.d.ts +36 -17
  27. package/lib/encoding/encode.js +49 -39
  28. package/lib/encoding/encode.js.map +1 -1
  29. package/lib/encoding/spec.d.ts +2 -4
  30. package/lib/encoding/spec.js +0 -2
  31. package/lib/encoding/spec.js.map +1 -1
  32. package/lib/index.d.ts +4 -6
  33. package/lib/index.js +6 -5
  34. package/lib/index.js.map +1 -1
  35. package/lib/types/registry.d.ts +8 -1
  36. package/lib/types/registry.js +20 -2
  37. package/lib/types/registry.js.map +1 -1
  38. package/package.json +1 -1
  39. package/src/annotations.ts +3 -1
  40. package/src/codegen/languages/csharp.ts +1 -47
  41. package/src/decoder/DecodeOperation.ts +6 -6
  42. package/src/decoder/Decoder.ts +3 -3
  43. package/src/encoder/EncodeOperation.ts +1 -1
  44. package/src/encoder/Encoder.ts +1 -1
  45. package/src/encoding/assert.ts +13 -5
  46. package/src/encoding/decode.ts +62 -97
  47. package/src/encoding/encode.ts +64 -36
  48. package/src/encoding/spec.ts +2 -5
  49. package/src/index.ts +4 -6
  50. package/src/types/registry.ts +22 -3
@@ -24,8 +24,6 @@
24
24
  /**
25
25
  * ArraySchema operations
26
26
  */
27
- OPERATION[OPERATION["PUSH"] = 11] = "PUSH";
28
- OPERATION[OPERATION["UNSHIFT"] = 12] = "UNSHIFT";
29
27
  OPERATION[OPERATION["REVERSE"] = 15] = "REVERSE";
30
28
  OPERATION[OPERATION["MOVE"] = 32] = "MOVE";
31
29
  OPERATION[OPERATION["DELETE_BY_REFID"] = 33] = "DELETE_BY_REFID";
@@ -67,1055 +65,1283 @@
67
65
  const $viewFieldIndexes = "$__viewFieldIndexes";
68
66
  const $fieldIndexesByViewTag = "$__fieldIndexesByViewTag";
69
67
 
70
- const registeredTypes = {};
71
- const identifiers = new Map();
72
- function registerType(identifier, definition) {
73
- identifiers.set(definition.constructor, identifier);
74
- registeredTypes[identifier] = definition;
75
- }
76
- function getType(identifier) {
77
- return registeredTypes[identifier];
68
+ /**
69
+ * Copyright (c) 2018 Endel Dreyer
70
+ * Copyright (c) 2014 Ion Drive Software Ltd.
71
+ *
72
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
73
+ * of this software and associated documentation files (the "Software"), to deal
74
+ * in the Software without restriction, including without limitation the rights
75
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
76
+ * copies of the Software, and to permit persons to whom the Software is
77
+ * furnished to do so, subject to the following conditions:
78
+ *
79
+ * The above copyright notice and this permission notice shall be included in all
80
+ * copies or substantial portions of the Software.
81
+ *
82
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
83
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
84
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
85
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
86
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
87
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
88
+ * SOFTWARE
89
+ */
90
+ /**
91
+ * msgpack implementation highly based on notepack.io
92
+ * https://github.com/darrachequesne/notepack
93
+ */
94
+ let textEncoder;
95
+ // @ts-ignore
96
+ try {
97
+ textEncoder = new TextEncoder();
78
98
  }
79
-
80
- class TypeContext {
81
- /**
82
- * For inheritance support
83
- * Keeps track of which classes extends which. (parent -> children)
84
- */
85
- static { this.inheritedTypes = new Map(); }
86
- static register(target) {
87
- const parent = Object.getPrototypeOf(target);
88
- if (parent !== Schema) {
89
- let inherits = TypeContext.inheritedTypes.get(parent);
90
- if (!inherits) {
91
- inherits = new Set();
92
- TypeContext.inheritedTypes.set(parent, inherits);
93
- }
94
- inherits.add(target);
95
- }
96
- }
97
- constructor(rootClass) {
98
- this.types = {};
99
- this.schemas = new Map();
100
- this.hasFilters = false;
101
- this.parentFiltered = {};
102
- if (rootClass) {
103
- this.discoverTypes(rootClass);
104
- }
105
- }
106
- has(schema) {
107
- return this.schemas.has(schema);
108
- }
109
- get(typeid) {
110
- return this.types[typeid];
111
- }
112
- add(schema, typeid = this.schemas.size) {
113
- // skip if already registered
114
- if (this.schemas.has(schema)) {
115
- return false;
116
- }
117
- this.types[typeid] = schema;
118
- //
119
- // Workaround to allow using an empty Schema (with no `@type()` fields)
120
- //
121
- if (schema[Symbol.metadata] === undefined) {
122
- Metadata.initialize(schema);
123
- }
124
- this.schemas.set(schema, typeid);
125
- return true;
126
- }
127
- getTypeId(klass) {
128
- return this.schemas.get(klass);
129
- }
130
- discoverTypes(klass, parentIndex, parentFieldViewTag) {
131
- if (!this.add(klass)) {
132
- return;
133
- }
134
- // add classes inherited from this base class
135
- TypeContext.inheritedTypes.get(klass)?.forEach((child) => {
136
- this.discoverTypes(child, parentIndex, parentFieldViewTag);
137
- });
138
- // add parent classes
139
- let parent = klass;
140
- while ((parent = Object.getPrototypeOf(parent)) &&
141
- parent !== Schema && // stop at root (Schema)
142
- parent !== Function.prototype // stop at root (non-Schema)
143
- ) {
144
- this.discoverTypes(parent);
145
- }
146
- const metadata = (klass[Symbol.metadata] ??= {});
147
- // if any schema/field has filters, mark "context" as having filters.
148
- if (metadata[$viewFieldIndexes]) {
149
- this.hasFilters = true;
150
- }
151
- if (parentFieldViewTag !== undefined) {
152
- this.parentFiltered[`${this.schemas.get(klass)}-${parentIndex}`] = true;
153
- }
154
- for (const fieldIndex in metadata) {
155
- const index = fieldIndex;
156
- const fieldType = metadata[index].type;
157
- const viewTag = metadata[index].tag;
158
- if (typeof (fieldType) === "string") {
159
- continue;
99
+ catch (e) { }
100
+ const _convoBuffer$1 = new ArrayBuffer(8);
101
+ const _int32$1 = new Int32Array(_convoBuffer$1);
102
+ const _float32$1 = new Float32Array(_convoBuffer$1);
103
+ const _float64$1 = new Float64Array(_convoBuffer$1);
104
+ const _int64$1 = new BigInt64Array(_convoBuffer$1);
105
+ const hasBufferByteLength = (typeof Buffer !== 'undefined' && Buffer.byteLength);
106
+ const utf8Length = (hasBufferByteLength)
107
+ ? Buffer.byteLength // node
108
+ : function (str, _) {
109
+ var c = 0, length = 0;
110
+ for (var i = 0, l = str.length; i < l; i++) {
111
+ c = str.charCodeAt(i);
112
+ if (c < 0x80) {
113
+ length += 1;
160
114
  }
161
- if (Array.isArray(fieldType)) {
162
- const type = fieldType[0];
163
- // skip primitive types
164
- if (type === "string") {
165
- continue;
166
- }
167
- this.discoverTypes(type, index, viewTag);
115
+ else if (c < 0x800) {
116
+ length += 2;
168
117
  }
169
- else if (typeof (fieldType) === "function") {
170
- this.discoverTypes(fieldType, viewTag);
118
+ else if (c < 0xd800 || c >= 0xe000) {
119
+ length += 3;
171
120
  }
172
121
  else {
173
- const type = Object.values(fieldType)[0];
174
- // skip primitive types
175
- if (typeof (type) === "string") {
176
- continue;
177
- }
178
- this.discoverTypes(type, index, viewTag);
122
+ i++;
123
+ length += 4;
179
124
  }
180
125
  }
181
- }
182
- }
183
-
184
- function getNormalizedType(type) {
185
- return (Array.isArray(type))
186
- ? { array: type[0] }
187
- : (typeof (type['type']) !== "undefined")
188
- ? type['type']
189
- : type;
190
- }
191
- const Metadata = {
192
- addField(metadata, index, name, type, descriptor) {
193
- if (index > 64) {
194
- throw new Error(`Can't define field '${name}'.\nSchema instances may only have up to 64 fields.`);
126
+ return length;
127
+ };
128
+ function utf8Write(view, str, it) {
129
+ var c = 0;
130
+ for (var i = 0, l = str.length; i < l; i++) {
131
+ c = str.charCodeAt(i);
132
+ if (c < 0x80) {
133
+ view[it.offset++] = c;
195
134
  }
196
- metadata[index] = Object.assign(metadata[index] || {}, // avoid overwriting previous field metadata (@owned / @deprecated)
197
- {
198
- type: getNormalizedType(type),
199
- index,
200
- name,
201
- });
202
- // create "descriptors" map
203
- Object.defineProperty(metadata, $descriptors, {
204
- value: metadata[$descriptors] || {},
205
- enumerable: false,
206
- configurable: true,
207
- });
208
- if (descriptor) {
209
- // for encoder
210
- metadata[$descriptors][name] = descriptor;
211
- metadata[$descriptors][`_${name}`] = {
212
- value: undefined,
213
- writable: true,
214
- enumerable: false,
215
- configurable: true,
216
- };
135
+ else if (c < 0x800) {
136
+ view[it.offset] = 0xc0 | (c >> 6);
137
+ view[it.offset + 1] = 0x80 | (c & 0x3f);
138
+ it.offset += 2;
217
139
  }
218
- else {
219
- // for decoder
220
- metadata[$descriptors][name] = {
221
- value: undefined,
222
- writable: true,
223
- enumerable: true,
224
- configurable: true,
225
- };
140
+ else if (c < 0xd800 || c >= 0xe000) {
141
+ view[it.offset] = 0xe0 | (c >> 12);
142
+ view[it.offset + 1] = 0x80 | (c >> 6 & 0x3f);
143
+ view[it.offset + 2] = 0x80 | (c & 0x3f);
144
+ it.offset += 3;
226
145
  }
227
- // map -1 as last field index
228
- Object.defineProperty(metadata, $numFields, {
229
- value: index,
230
- enumerable: false,
231
- configurable: true
232
- });
233
- // map field name => index (non enumerable)
234
- Object.defineProperty(metadata, name, {
235
- value: index,
236
- enumerable: false,
237
- configurable: true,
238
- });
239
- // if child Ref/complex type, add to -4
240
- if (typeof (metadata[index].type) !== "string") {
241
- if (metadata[$refTypeFieldIndexes] === undefined) {
242
- Object.defineProperty(metadata, $refTypeFieldIndexes, {
243
- value: [],
244
- enumerable: false,
245
- configurable: true,
246
- });
247
- }
248
- metadata[$refTypeFieldIndexes].push(index);
146
+ else {
147
+ i++;
148
+ c = 0x10000 + (((c & 0x3ff) << 10) | (str.charCodeAt(i) & 0x3ff));
149
+ view[it.offset] = 0xf0 | (c >> 18);
150
+ view[it.offset + 1] = 0x80 | (c >> 12 & 0x3f);
151
+ view[it.offset + 2] = 0x80 | (c >> 6 & 0x3f);
152
+ view[it.offset + 3] = 0x80 | (c & 0x3f);
153
+ it.offset += 4;
249
154
  }
250
- },
251
- setTag(metadata, fieldName, tag) {
252
- const index = metadata[fieldName];
253
- const field = metadata[index];
254
- // add 'tag' to the field
255
- field.tag = tag;
256
- if (!metadata[$viewFieldIndexes]) {
257
- // -2: all field indexes with "view" tag
258
- Object.defineProperty(metadata, $viewFieldIndexes, {
259
- value: [],
260
- enumerable: false,
261
- configurable: true
262
- });
263
- // -3: field indexes by "view" tag
264
- Object.defineProperty(metadata, $fieldIndexesByViewTag, {
265
- value: {},
266
- enumerable: false,
267
- configurable: true
268
- });
155
+ }
156
+ }
157
+ function int8$1(bytes, value, it) {
158
+ bytes[it.offset++] = value & 255;
159
+ }
160
+ function uint8$1(bytes, value, it) {
161
+ bytes[it.offset++] = value & 255;
162
+ }
163
+ function int16$1(bytes, value, it) {
164
+ bytes[it.offset++] = value & 255;
165
+ bytes[it.offset++] = (value >> 8) & 255;
166
+ }
167
+ function uint16$1(bytes, value, it) {
168
+ bytes[it.offset++] = value & 255;
169
+ bytes[it.offset++] = (value >> 8) & 255;
170
+ }
171
+ function int32$1(bytes, value, it) {
172
+ bytes[it.offset++] = value & 255;
173
+ bytes[it.offset++] = (value >> 8) & 255;
174
+ bytes[it.offset++] = (value >> 16) & 255;
175
+ bytes[it.offset++] = (value >> 24) & 255;
176
+ }
177
+ function uint32$1(bytes, value, it) {
178
+ const b4 = value >> 24;
179
+ const b3 = value >> 16;
180
+ const b2 = value >> 8;
181
+ const b1 = value;
182
+ bytes[it.offset++] = b1 & 255;
183
+ bytes[it.offset++] = b2 & 255;
184
+ bytes[it.offset++] = b3 & 255;
185
+ bytes[it.offset++] = b4 & 255;
186
+ }
187
+ function int64$1(bytes, value, it) {
188
+ const high = Math.floor(value / Math.pow(2, 32));
189
+ const low = value >>> 0;
190
+ uint32$1(bytes, low, it);
191
+ uint32$1(bytes, high, it);
192
+ }
193
+ function uint64$1(bytes, value, it) {
194
+ const high = (value / Math.pow(2, 32)) >> 0;
195
+ const low = value >>> 0;
196
+ uint32$1(bytes, low, it);
197
+ uint32$1(bytes, high, it);
198
+ }
199
+ function bigint64$1(bytes, value, it) {
200
+ _int64$1[0] = BigInt.asIntN(64, value);
201
+ int32$1(bytes, _int32$1[0], it);
202
+ int32$1(bytes, _int32$1[1], it);
203
+ }
204
+ function biguint64$1(bytes, value, it) {
205
+ _int64$1[0] = BigInt.asIntN(64, value);
206
+ int32$1(bytes, _int32$1[0], it);
207
+ int32$1(bytes, _int32$1[1], it);
208
+ }
209
+ function float32$1(bytes, value, it) {
210
+ _float32$1[0] = value;
211
+ int32$1(bytes, _int32$1[0], it);
212
+ }
213
+ function float64$1(bytes, value, it) {
214
+ _float64$1[0] = value;
215
+ int32$1(bytes, _int32$1[0 ], it);
216
+ int32$1(bytes, _int32$1[1 ], it);
217
+ }
218
+ function boolean$1(bytes, value, it) {
219
+ bytes[it.offset++] = value ? 1 : 0; // uint8
220
+ }
221
+ function string$1(bytes, value, it) {
222
+ // encode `null` strings as empty.
223
+ if (!value) {
224
+ value = "";
225
+ }
226
+ let length = utf8Length(value, "utf8");
227
+ let size = 0;
228
+ // fixstr
229
+ if (length < 0x20) {
230
+ bytes[it.offset++] = length | 0xa0;
231
+ size = 1;
232
+ }
233
+ // str 8
234
+ else if (length < 0x100) {
235
+ bytes[it.offset++] = 0xd9;
236
+ bytes[it.offset++] = length % 255;
237
+ size = 2;
238
+ }
239
+ // str 16
240
+ else if (length < 0x10000) {
241
+ bytes[it.offset++] = 0xda;
242
+ uint16$1(bytes, length, it);
243
+ size = 3;
244
+ }
245
+ // str 32
246
+ else if (length < 0x100000000) {
247
+ bytes[it.offset++] = 0xdb;
248
+ uint32$1(bytes, length, it);
249
+ size = 5;
250
+ }
251
+ else {
252
+ throw new Error('String too long');
253
+ }
254
+ utf8Write(bytes, value, it);
255
+ return size + length;
256
+ }
257
+ function number$1(bytes, value, it) {
258
+ if (isNaN(value)) {
259
+ return number$1(bytes, 0, it);
260
+ }
261
+ else if (!isFinite(value)) {
262
+ return number$1(bytes, (value > 0) ? Number.MAX_SAFE_INTEGER : -Number.MAX_SAFE_INTEGER, it);
263
+ }
264
+ else if (value !== (value | 0)) {
265
+ if (Math.abs(value) <= 3.4028235e+38) { // range check
266
+ _float32$1[0] = value;
267
+ if (Math.abs(Math.abs(_float32$1[0]) - Math.abs(value)) < 1e-4) { // precision check; adjust 1e-n (n = precision) to in-/decrease acceptable precision loss
268
+ // now we know value is in range for f32 and has acceptable precision for f32
269
+ bytes[it.offset++] = 0xca;
270
+ float32$1(bytes, value, it);
271
+ return 5;
272
+ }
269
273
  }
270
- metadata[$viewFieldIndexes].push(index);
271
- if (!metadata[$fieldIndexesByViewTag][tag]) {
272
- metadata[$fieldIndexesByViewTag][tag] = [];
274
+ bytes[it.offset++] = 0xcb;
275
+ float64$1(bytes, value, it);
276
+ return 9;
277
+ }
278
+ if (value >= 0) {
279
+ // positive fixnum
280
+ if (value < 0x80) {
281
+ bytes[it.offset++] = value & 255; // uint8
282
+ return 1;
273
283
  }
274
- metadata[$fieldIndexesByViewTag][tag].push(index);
275
- },
276
- setFields(target, fields) {
277
- // for inheritance support
278
- const constructor = target.prototype.constructor;
279
- TypeContext.register(constructor);
280
- const parentClass = Object.getPrototypeOf(constructor);
281
- const parentMetadata = parentClass && parentClass[Symbol.metadata];
282
- const metadata = Metadata.initialize(constructor);
283
- // Use Schema's methods if not defined in the class
284
- if (!constructor[$track]) {
285
- constructor[$track] = Schema[$track];
284
+ // uint 8
285
+ if (value < 0x100) {
286
+ bytes[it.offset++] = 0xcc;
287
+ bytes[it.offset++] = value & 255; // uint8
288
+ return 2;
286
289
  }
287
- if (!constructor[$encoder]) {
288
- constructor[$encoder] = Schema[$encoder];
290
+ // uint 16
291
+ if (value < 0x10000) {
292
+ bytes[it.offset++] = 0xcd;
293
+ uint16$1(bytes, value, it);
294
+ return 3;
289
295
  }
290
- if (!constructor[$decoder]) {
291
- constructor[$decoder] = Schema[$decoder];
296
+ // uint 32
297
+ if (value < 0x100000000) {
298
+ bytes[it.offset++] = 0xce;
299
+ uint32$1(bytes, value, it);
300
+ return 5;
292
301
  }
293
- if (!constructor.prototype.toJSON) {
294
- constructor.prototype.toJSON = Schema.prototype.toJSON;
302
+ // uint 64
303
+ bytes[it.offset++] = 0xcf;
304
+ uint64$1(bytes, value, it);
305
+ return 9;
306
+ }
307
+ else {
308
+ // negative fixnum
309
+ if (value >= -0x20) {
310
+ bytes[it.offset++] = 0xe0 | (value + 0x20);
311
+ return 1;
295
312
  }
296
- //
297
- // detect index for this field, considering inheritance
298
- //
299
- let fieldIndex = metadata[$numFields] // current structure already has fields defined
300
- ?? (parentMetadata && parentMetadata[$numFields]) // parent structure has fields defined
301
- ?? -1; // no fields defined
302
- fieldIndex++;
303
- for (const field in fields) {
304
- const type = fields[field];
305
- const normalizedType = getNormalizedType(type);
306
- // FIXME: this code is duplicated from @type() annotation
307
- const complexTypeKlass = (Array.isArray(type))
308
- ? getType("array")
309
- : (typeof (Object.keys(type)[0]) === "string") && getType(Object.keys(type)[0]);
310
- const childType = (complexTypeKlass)
311
- ? Object.values(type)[0]
312
- : normalizedType;
313
- Metadata.addField(metadata, fieldIndex, field, type, getPropertyDescriptor(`_${field}`, fieldIndex, childType, complexTypeKlass));
314
- fieldIndex++;
313
+ // int 8
314
+ if (value >= -0x80) {
315
+ bytes[it.offset++] = 0xd0;
316
+ int8$1(bytes, value, it);
317
+ return 2;
315
318
  }
316
- return target;
317
- },
318
- isDeprecated(metadata, field) {
319
- return metadata[field].deprecated === true;
320
- },
321
- init(klass) {
322
- //
323
- // Used only to initialize an empty Schema (Encoder#constructor)
324
- // TODO: remove/refactor this...
325
- //
326
- const metadata = {};
327
- klass[Symbol.metadata] = metadata;
328
- Object.defineProperty(metadata, $numFields, {
329
- value: 0,
330
- enumerable: false,
331
- configurable: true,
332
- });
333
- },
334
- initialize(constructor) {
335
- const parentClass = Object.getPrototypeOf(constructor);
336
- const parentMetadata = parentClass[Symbol.metadata];
337
- let metadata = constructor[Symbol.metadata] ?? Object.create(null);
338
- // make sure inherited classes have their own metadata object.
339
- if (parentClass !== Schema && metadata === parentMetadata) {
340
- metadata = Object.create(null);
341
- if (parentMetadata) {
342
- //
343
- // assign parent metadata to current
344
- //
345
- Object.setPrototypeOf(metadata, parentMetadata);
346
- // $numFields
347
- Object.defineProperty(metadata, $numFields, {
348
- value: parentMetadata[$numFields],
349
- enumerable: false,
350
- configurable: true,
351
- writable: true,
352
- });
353
- // $viewFieldIndexes / $fieldIndexesByViewTag
354
- if (parentMetadata[$viewFieldIndexes] !== undefined) {
355
- Object.defineProperty(metadata, $viewFieldIndexes, {
356
- value: [...parentMetadata[$viewFieldIndexes]],
357
- enumerable: false,
358
- configurable: true,
359
- writable: true,
360
- });
361
- Object.defineProperty(metadata, $fieldIndexesByViewTag, {
362
- value: { ...parentMetadata[$fieldIndexesByViewTag] },
363
- enumerable: false,
364
- configurable: true,
365
- writable: true,
366
- });
367
- }
368
- // $refTypeFieldIndexes
369
- if (parentMetadata[$refTypeFieldIndexes] !== undefined) {
370
- Object.defineProperty(metadata, $refTypeFieldIndexes, {
371
- value: [...parentMetadata[$refTypeFieldIndexes]],
372
- enumerable: false,
373
- configurable: true,
374
- writable: true,
375
- });
376
- }
377
- // $descriptors
378
- Object.defineProperty(metadata, $descriptors, {
379
- value: { ...parentMetadata[$descriptors] },
380
- enumerable: false,
381
- configurable: true,
382
- writable: true,
383
- });
384
- }
319
+ // int 16
320
+ if (value >= -0x8000) {
321
+ bytes[it.offset++] = 0xd1;
322
+ int16$1(bytes, value, it);
323
+ return 3;
385
324
  }
386
- constructor[Symbol.metadata] = metadata;
387
- return metadata;
388
- },
389
- isValidInstance(klass) {
390
- return (klass.constructor[Symbol.metadata] &&
391
- Object.prototype.hasOwnProperty.call(klass.constructor[Symbol.metadata], $numFields));
392
- },
393
- getFields(klass) {
394
- const metadata = klass[Symbol.metadata];
395
- const fields = {};
396
- for (let i = 0; i <= metadata[$numFields]; i++) {
397
- fields[metadata[i].name] = metadata[i].type;
325
+ // int 32
326
+ if (value >= -0x80000000) {
327
+ bytes[it.offset++] = 0xd2;
328
+ int32$1(bytes, value, it);
329
+ return 5;
398
330
  }
399
- return fields;
331
+ // int 64
332
+ bytes[it.offset++] = 0xd3;
333
+ int64$1(bytes, value, it);
334
+ return 9;
335
+ }
336
+ }
337
+ const encode = {
338
+ int8: int8$1,
339
+ uint8: uint8$1,
340
+ int16: int16$1,
341
+ uint16: uint16$1,
342
+ int32: int32$1,
343
+ uint32: uint32$1,
344
+ int64: int64$1,
345
+ uint64: uint64$1,
346
+ bigint64: bigint64$1,
347
+ biguint64: biguint64$1,
348
+ float32: float32$1,
349
+ float64: float64$1,
350
+ boolean: boolean$1,
351
+ string: string$1,
352
+ number: number$1,
353
+ utf8Write,
354
+ utf8Length,
355
+ };
356
+
357
+ /**
358
+ * Copyright (c) 2018 Endel Dreyer
359
+ * Copyright (c) 2014 Ion Drive Software Ltd.
360
+ *
361
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
362
+ * of this software and associated documentation files (the "Software"), to deal
363
+ * in the Software without restriction, including without limitation the rights
364
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
365
+ * copies of the Software, and to permit persons to whom the Software is
366
+ * furnished to do so, subject to the following conditions:
367
+ *
368
+ * The above copyright notice and this permission notice shall be included in all
369
+ * copies or substantial portions of the Software.
370
+ *
371
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
372
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
373
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
374
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
375
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
376
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
377
+ * SOFTWARE
378
+ */
379
+ // force little endian to facilitate decoding on multiple implementations
380
+ const _convoBuffer = new ArrayBuffer(8);
381
+ const _int32 = new Int32Array(_convoBuffer);
382
+ const _float32 = new Float32Array(_convoBuffer);
383
+ const _float64 = new Float64Array(_convoBuffer);
384
+ const _uint64 = new BigUint64Array(_convoBuffer);
385
+ const _int64 = new BigInt64Array(_convoBuffer);
386
+ function utf8Read(bytes, it, length) {
387
+ var string = '', chr = 0;
388
+ for (var i = it.offset, end = it.offset + length; i < end; i++) {
389
+ var byte = bytes[i];
390
+ if ((byte & 0x80) === 0x00) {
391
+ string += String.fromCharCode(byte);
392
+ continue;
393
+ }
394
+ if ((byte & 0xe0) === 0xc0) {
395
+ string += String.fromCharCode(((byte & 0x1f) << 6) |
396
+ (bytes[++i] & 0x3f));
397
+ continue;
398
+ }
399
+ if ((byte & 0xf0) === 0xe0) {
400
+ string += String.fromCharCode(((byte & 0x0f) << 12) |
401
+ ((bytes[++i] & 0x3f) << 6) |
402
+ ((bytes[++i] & 0x3f) << 0));
403
+ continue;
404
+ }
405
+ if ((byte & 0xf8) === 0xf0) {
406
+ chr = ((byte & 0x07) << 18) |
407
+ ((bytes[++i] & 0x3f) << 12) |
408
+ ((bytes[++i] & 0x3f) << 6) |
409
+ ((bytes[++i] & 0x3f) << 0);
410
+ if (chr >= 0x010000) { // surrogate pair
411
+ chr -= 0x010000;
412
+ string += String.fromCharCode((chr >>> 10) + 0xD800, (chr & 0x3FF) + 0xDC00);
413
+ }
414
+ else {
415
+ string += String.fromCharCode(chr);
416
+ }
417
+ continue;
418
+ }
419
+ console.error('Invalid byte ' + byte.toString(16));
420
+ // (do not throw error to avoid server/client from crashing due to hack attemps)
421
+ // throw new Error('Invalid byte ' + byte.toString(16));
422
+ }
423
+ it.offset += length;
424
+ return string;
425
+ }
426
+ function int8(bytes, it) {
427
+ return uint8(bytes, it) << 24 >> 24;
428
+ }
429
+ function uint8(bytes, it) {
430
+ return bytes[it.offset++];
431
+ }
432
+ function int16(bytes, it) {
433
+ return uint16(bytes, it) << 16 >> 16;
434
+ }
435
+ function uint16(bytes, it) {
436
+ return bytes[it.offset++] | bytes[it.offset++] << 8;
437
+ }
438
+ function int32(bytes, it) {
439
+ return bytes[it.offset++] | bytes[it.offset++] << 8 | bytes[it.offset++] << 16 | bytes[it.offset++] << 24;
440
+ }
441
+ function uint32(bytes, it) {
442
+ return int32(bytes, it) >>> 0;
443
+ }
444
+ function float32(bytes, it) {
445
+ _int32[0] = int32(bytes, it);
446
+ return _float32[0];
447
+ }
448
+ function float64(bytes, it) {
449
+ _int32[0 ] = int32(bytes, it);
450
+ _int32[1 ] = int32(bytes, it);
451
+ return _float64[0];
452
+ }
453
+ function int64(bytes, it) {
454
+ const low = uint32(bytes, it);
455
+ const high = int32(bytes, it) * Math.pow(2, 32);
456
+ return high + low;
457
+ }
458
+ function uint64(bytes, it) {
459
+ const low = uint32(bytes, it);
460
+ const high = uint32(bytes, it) * Math.pow(2, 32);
461
+ return high + low;
462
+ }
463
+ function bigint64(bytes, it) {
464
+ _int32[0] = int32(bytes, it);
465
+ _int32[1] = int32(bytes, it);
466
+ return _int64[0];
467
+ }
468
+ function biguint64(bytes, it) {
469
+ _int32[0] = int32(bytes, it);
470
+ _int32[1] = int32(bytes, it);
471
+ return _uint64[0];
472
+ }
473
+ function boolean(bytes, it) {
474
+ return uint8(bytes, it) > 0;
475
+ }
476
+ function string(bytes, it) {
477
+ const prefix = bytes[it.offset++];
478
+ let length;
479
+ if (prefix < 0xc0) {
480
+ // fixstr
481
+ length = prefix & 0x1f;
482
+ }
483
+ else if (prefix === 0xd9) {
484
+ length = uint8(bytes, it);
485
+ }
486
+ else if (prefix === 0xda) {
487
+ length = uint16(bytes, it);
488
+ }
489
+ else if (prefix === 0xdb) {
490
+ length = uint32(bytes, it);
491
+ }
492
+ return utf8Read(bytes, it, length);
493
+ }
494
+ function number(bytes, it) {
495
+ const prefix = bytes[it.offset++];
496
+ if (prefix < 0x80) {
497
+ // positive fixint
498
+ return prefix;
400
499
  }
401
- };
402
-
403
- function setOperationAtIndex(changeSet, index) {
404
- const operationsIndex = changeSet.indexes[index];
405
- if (operationsIndex === undefined) {
406
- changeSet.indexes[index] = changeSet.operations.push(index) - 1;
500
+ else if (prefix === 0xca) {
501
+ // float 32
502
+ return float32(bytes, it);
407
503
  }
408
- else {
409
- changeSet.operations[operationsIndex] = index;
504
+ else if (prefix === 0xcb) {
505
+ // float 64
506
+ return float64(bytes, it);
410
507
  }
411
- }
412
- function deleteOperationAtIndex(changeSet, index) {
413
- const operationsIndex = changeSet.indexes[index];
414
- if (operationsIndex !== undefined) {
415
- changeSet.operations[operationsIndex] = undefined;
508
+ else if (prefix === 0xcc) {
509
+ // uint 8
510
+ return uint8(bytes, it);
416
511
  }
417
- delete changeSet.indexes[index];
418
- }
419
- function enqueueChangeTree(root, changeTree, changeSet, queueRootIndex = changeTree[changeSet].queueRootIndex) {
420
- if (root && root[changeSet][queueRootIndex] !== changeTree) {
421
- changeTree[changeSet].queueRootIndex = root[changeSet].push(changeTree) - 1;
512
+ else if (prefix === 0xcd) {
513
+ // uint 16
514
+ return uint16(bytes, it);
422
515
  }
423
- }
424
- class ChangeTree {
425
- constructor(ref) {
426
- this.isFiltered = false;
427
- this.isPartiallyFiltered = false;
428
- this.indexedOperations = {};
429
- //
430
- // TODO:
431
- // try storing the index + operation per item.
432
- // example: 1024 & 1025 => ADD, 1026 => DELETE
433
- //
434
- // => https://chatgpt.com/share/67107d0c-bc20-8004-8583-83b17dd7c196
435
- //
436
- this.changes = { indexes: {}, operations: [] };
437
- this.allChanges = { indexes: {}, operations: [] };
438
- /**
439
- * Is this a new instance? Used on ArraySchema to determine OPERATION.MOVE_AND_ADD operation.
440
- */
441
- this.isNew = true;
442
- this.ref = ref;
443
- //
444
- // Does this structure have "filters" declared?
445
- //
446
- if (ref.constructor[Symbol.metadata]?.[$viewFieldIndexes]) {
447
- this.allFilteredChanges = { indexes: {}, operations: [] };
448
- this.filteredChanges = { indexes: {}, operations: [] };
449
- }
516
+ else if (prefix === 0xce) {
517
+ // uint 32
518
+ return uint32(bytes, it);
450
519
  }
451
- setRoot(root) {
452
- this.root = root;
453
- const isNewChangeTree = this.root.add(this);
454
- const metadata = this.ref.constructor[Symbol.metadata];
455
- if (this.root.types.hasFilters) {
456
- //
457
- // At Schema initialization, the "root" structure might not be available
458
- // yet, as it only does once the "Encoder" has been set up.
459
- //
460
- // So the "parent" may be already set without a "root".
461
- //
462
- this.checkIsFiltered(metadata, this.parent, this.parentIndex);
463
- if (this.isFiltered || this.isPartiallyFiltered) {
464
- enqueueChangeTree(root, this, 'filteredChanges');
465
- if (isNewChangeTree) {
466
- this.root.allFilteredChanges.push(this);
467
- }
468
- }
469
- }
470
- if (!this.isFiltered) {
471
- enqueueChangeTree(root, this, 'changes');
472
- if (isNewChangeTree) {
473
- this.root.allChanges.push(this);
474
- }
475
- }
476
- // Recursively set root on child structures
477
- if (metadata) {
478
- metadata[$refTypeFieldIndexes]?.forEach((index) => {
479
- const field = metadata[index];
480
- const value = this.ref[field.name];
481
- value?.[$changes].setRoot(root);
482
- });
483
- }
484
- else if (this.ref[$childType] && typeof (this.ref[$childType]) !== "string") {
485
- // MapSchema / ArraySchema, etc.
486
- this.ref.forEach((value, key) => {
487
- value[$changes].setRoot(root);
488
- });
489
- }
520
+ else if (prefix === 0xcf) {
521
+ // uint 64
522
+ return uint64(bytes, it);
490
523
  }
491
- setParent(parent, root, parentIndex) {
492
- this.parent = parent;
493
- this.parentIndex = parentIndex;
494
- // avoid setting parents with empty `root`
495
- if (!root) {
496
- return;
497
- }
498
- const metadata = this.ref.constructor[Symbol.metadata];
499
- // skip if parent is already set
500
- if (root !== this.root) {
501
- this.root = root;
502
- const isNewChangeTree = root.add(this);
503
- if (root.types.hasFilters) {
504
- this.checkIsFiltered(metadata, parent, parentIndex);
505
- if (this.isFiltered || this.isPartiallyFiltered) {
506
- enqueueChangeTree(root, this, 'filteredChanges');
507
- if (isNewChangeTree) {
508
- this.root.allFilteredChanges.push(this);
509
- }
510
- }
511
- }
512
- if (!this.isFiltered) {
513
- enqueueChangeTree(root, this, 'changes');
514
- if (isNewChangeTree) {
515
- this.root.allChanges.push(this);
516
- }
517
- }
518
- }
519
- else {
520
- root.add(this);
521
- }
522
- // assign same parent on child structures
523
- if (metadata) {
524
- metadata[$refTypeFieldIndexes]?.forEach((index) => {
525
- const field = metadata[index];
526
- const value = this.ref[field.name];
527
- value?.[$changes].setParent(this.ref, root, index);
528
- // try { throw new Error(); } catch (e) {
529
- // console.log(e.stack);
530
- // }
531
- });
532
- }
533
- else if (this.ref[$childType] && typeof (this.ref[$childType]) !== "string") {
534
- // MapSchema / ArraySchema, etc.
535
- this.ref.forEach((value, key) => {
536
- value[$changes].setParent(this.ref, root, this.indexes[key] ?? key);
537
- });
538
- }
524
+ else if (prefix === 0xd0) {
525
+ // int 8
526
+ return int8(bytes, it);
539
527
  }
540
- forEachChild(callback) {
541
- //
542
- // assign same parent on child structures
543
- //
544
- const metadata = this.ref.constructor[Symbol.metadata];
545
- if (metadata) {
546
- metadata[$refTypeFieldIndexes]?.forEach((index) => {
547
- const field = metadata[index];
548
- const value = this.ref[field.name];
549
- if (value) {
550
- callback(value[$changes], index);
551
- }
552
- });
553
- }
554
- else if (this.ref[$childType] && typeof (this.ref[$childType]) !== "string") {
555
- // MapSchema / ArraySchema, etc.
556
- this.ref.forEach((value, key) => {
557
- callback(value[$changes], this.indexes[key] ?? key);
558
- });
559
- }
528
+ else if (prefix === 0xd1) {
529
+ // int 16
530
+ return int16(bytes, it);
560
531
  }
561
- operation(op) {
562
- // operations without index use negative values to represent them
563
- // this is checked during .encode() time.
564
- this.changes.operations.push(-op);
565
- enqueueChangeTree(this.root, this, 'changes');
532
+ else if (prefix === 0xd2) {
533
+ // int 32
534
+ return int32(bytes, it);
566
535
  }
567
- change(index, operation = exports.OPERATION.ADD) {
568
- const metadata = this.ref.constructor[Symbol.metadata];
569
- const isFiltered = this.isFiltered || (metadata?.[index]?.tag !== undefined);
570
- const changeSet = (isFiltered)
571
- ? this.filteredChanges
572
- : this.changes;
573
- const previousOperation = this.indexedOperations[index];
574
- if (!previousOperation || previousOperation === exports.OPERATION.DELETE) {
575
- const op = (!previousOperation)
576
- ? operation
577
- : (previousOperation === exports.OPERATION.DELETE)
578
- ? exports.OPERATION.DELETE_AND_ADD
579
- : operation;
580
- //
581
- // TODO: are DELETE operations being encoded as ADD here ??
582
- //
583
- this.indexedOperations[index] = op;
584
- }
585
- setOperationAtIndex(changeSet, index);
586
- if (isFiltered) {
587
- setOperationAtIndex(this.allFilteredChanges, index);
588
- if (this.root) {
589
- enqueueChangeTree(this.root, this, 'filteredChanges');
590
- enqueueChangeTree(this.root, this, 'allFilteredChanges');
536
+ else if (prefix === 0xd3) {
537
+ // int 64
538
+ return int64(bytes, it);
539
+ }
540
+ else if (prefix > 0xdf) {
541
+ // negative fixint
542
+ return (0xff - prefix + 1) * -1;
543
+ }
544
+ }
545
+ const decode = {
546
+ utf8Read,
547
+ int8,
548
+ uint8,
549
+ int16,
550
+ uint16,
551
+ int32,
552
+ uint32,
553
+ float32,
554
+ float64,
555
+ int64,
556
+ uint64,
557
+ bigint64,
558
+ biguint64,
559
+ boolean,
560
+ string,
561
+ number,
562
+ };
563
+
564
+ const registeredTypes = {};
565
+ const identifiers = new Map();
566
+ function registerType(identifier, definition) {
567
+ if (definition.constructor) {
568
+ identifiers.set(definition.constructor, identifier);
569
+ registeredTypes[identifier] = definition;
570
+ }
571
+ if (definition.encode) {
572
+ encode[identifier] = definition.encode;
573
+ }
574
+ if (definition.decode) {
575
+ decode[identifier] = definition.decode;
576
+ }
577
+ }
578
+ function getType(identifier) {
579
+ return registeredTypes[identifier];
580
+ }
581
+ function defineCustomTypes(types) {
582
+ for (const identifier in types) {
583
+ registerType(identifier, types[identifier]);
584
+ }
585
+ return (t) => type(t);
586
+ }
587
+
588
+ class TypeContext {
589
+ /**
590
+ * For inheritance support
591
+ * Keeps track of which classes extends which. (parent -> children)
592
+ */
593
+ static { this.inheritedTypes = new Map(); }
594
+ static register(target) {
595
+ const parent = Object.getPrototypeOf(target);
596
+ if (parent !== Schema) {
597
+ let inherits = TypeContext.inheritedTypes.get(parent);
598
+ if (!inherits) {
599
+ inherits = new Set();
600
+ TypeContext.inheritedTypes.set(parent, inherits);
591
601
  }
602
+ inherits.add(target);
592
603
  }
593
- else {
594
- setOperationAtIndex(this.allChanges, index);
595
- enqueueChangeTree(this.root, this, 'changes');
604
+ }
605
+ constructor(rootClass) {
606
+ this.types = {};
607
+ this.schemas = new Map();
608
+ this.hasFilters = false;
609
+ this.parentFiltered = {};
610
+ if (rootClass) {
611
+ this.discoverTypes(rootClass);
596
612
  }
597
613
  }
598
- shiftChangeIndexes(shiftIndex) {
599
- //
600
- // Used only during:
614
+ has(schema) {
615
+ return this.schemas.has(schema);
616
+ }
617
+ get(typeid) {
618
+ return this.types[typeid];
619
+ }
620
+ add(schema, typeid = this.schemas.size) {
621
+ // skip if already registered
622
+ if (this.schemas.has(schema)) {
623
+ return false;
624
+ }
625
+ this.types[typeid] = schema;
601
626
  //
602
- // - ArraySchema#unshift()
627
+ // Workaround to allow using an empty Schema (with no `@type()` fields)
603
628
  //
604
- const changeSet = (this.isFiltered)
605
- ? this.filteredChanges
606
- : this.changes;
607
- const newIndexedOperations = {};
608
- const newIndexes = {};
609
- for (const index in this.indexedOperations) {
610
- newIndexedOperations[Number(index) + shiftIndex] = this.indexedOperations[index];
611
- newIndexes[Number(index) + shiftIndex] = changeSet[index];
629
+ if (schema[Symbol.metadata] === undefined) {
630
+ Metadata.initialize(schema);
612
631
  }
613
- this.indexedOperations = newIndexedOperations;
614
- changeSet.indexes = newIndexes;
615
- changeSet.operations = changeSet.operations.map((index) => index + shiftIndex);
632
+ this.schemas.set(schema, typeid);
633
+ return true;
616
634
  }
617
- shiftAllChangeIndexes(shiftIndex, startIndex = 0) {
618
- //
619
- // Used only during:
620
- //
621
- // - ArraySchema#splice()
622
- //
623
- if (this.isFiltered || this.isPartiallyFiltered) {
624
- this._shiftAllChangeIndexes(shiftIndex, startIndex, this.allFilteredChanges);
625
- this._shiftAllChangeIndexes(shiftIndex, startIndex, this.allChanges);
635
+ getTypeId(klass) {
636
+ return this.schemas.get(klass);
637
+ }
638
+ discoverTypes(klass, parentIndex, parentFieldViewTag) {
639
+ if (!this.add(klass)) {
640
+ return;
626
641
  }
627
- else {
628
- this._shiftAllChangeIndexes(shiftIndex, startIndex, this.allChanges);
642
+ // add classes inherited from this base class
643
+ TypeContext.inheritedTypes.get(klass)?.forEach((child) => {
644
+ this.discoverTypes(child, parentIndex, parentFieldViewTag);
645
+ });
646
+ // add parent classes
647
+ let parent = klass;
648
+ while ((parent = Object.getPrototypeOf(parent)) &&
649
+ parent !== Schema && // stop at root (Schema)
650
+ parent !== Function.prototype // stop at root (non-Schema)
651
+ ) {
652
+ this.discoverTypes(parent);
629
653
  }
630
- }
631
- _shiftAllChangeIndexes(shiftIndex, startIndex = 0, changeSet) {
632
- const newIndexes = {};
633
- for (const key in changeSet.indexes) {
634
- const index = changeSet.indexes[key];
635
- if (index > startIndex) {
636
- newIndexes[Number(key) + shiftIndex] = index;
654
+ const metadata = (klass[Symbol.metadata] ??= {});
655
+ // if any schema/field has filters, mark "context" as having filters.
656
+ if (metadata[$viewFieldIndexes]) {
657
+ this.hasFilters = true;
658
+ }
659
+ if (parentFieldViewTag !== undefined) {
660
+ this.parentFiltered[`${this.schemas.get(klass)}-${parentIndex}`] = true;
661
+ }
662
+ for (const fieldIndex in metadata) {
663
+ const index = fieldIndex;
664
+ const fieldType = metadata[index].type;
665
+ const viewTag = metadata[index].tag;
666
+ if (typeof (fieldType) === "string") {
667
+ continue;
637
668
  }
638
- else {
639
- newIndexes[key] = index;
669
+ if (Array.isArray(fieldType)) {
670
+ const type = fieldType[0];
671
+ // skip primitive types
672
+ if (type === "string") {
673
+ continue;
674
+ }
675
+ this.discoverTypes(type, index, viewTag);
640
676
  }
641
- }
642
- changeSet.indexes = newIndexes;
643
- for (let i = 0; i < changeSet.operations.length; i++) {
644
- const index = changeSet.operations[i];
645
- if (index > startIndex) {
646
- changeSet.operations[i] = index + shiftIndex;
677
+ else if (typeof (fieldType) === "function") {
678
+ this.discoverTypes(fieldType, viewTag);
679
+ }
680
+ else {
681
+ const type = Object.values(fieldType)[0];
682
+ // skip primitive types
683
+ if (typeof (type) === "string") {
684
+ continue;
685
+ }
686
+ this.discoverTypes(type, index, viewTag);
647
687
  }
648
688
  }
649
689
  }
650
- indexedOperation(index, operation, allChangesIndex = index) {
651
- this.indexedOperations[index] = operation;
652
- if (this.filteredChanges) {
653
- setOperationAtIndex(this.allFilteredChanges, allChangesIndex);
654
- setOperationAtIndex(this.filteredChanges, index);
655
- enqueueChangeTree(this.root, this, 'filteredChanges');
656
- }
657
- else {
658
- setOperationAtIndex(this.allChanges, allChangesIndex);
659
- setOperationAtIndex(this.changes, index);
660
- enqueueChangeTree(this.root, this, 'changes');
690
+ }
691
+
692
+ function getNormalizedType(type) {
693
+ return (Array.isArray(type))
694
+ ? { array: type[0] }
695
+ : (typeof (type['type']) !== "undefined")
696
+ ? type['type']
697
+ : type;
698
+ }
699
+ const Metadata = {
700
+ addField(metadata, index, name, type, descriptor) {
701
+ if (index > 64) {
702
+ throw new Error(`Can't define field '${name}'.\nSchema instances may only have up to 64 fields.`);
661
703
  }
662
- }
663
- getType(index) {
664
- if (Metadata.isValidInstance(this.ref)) {
665
- const metadata = this.ref.constructor[Symbol.metadata];
666
- return metadata[index].type;
704
+ metadata[index] = Object.assign(metadata[index] || {}, // avoid overwriting previous field metadata (@owned / @deprecated)
705
+ {
706
+ type: getNormalizedType(type),
707
+ index,
708
+ name,
709
+ });
710
+ // create "descriptors" map
711
+ Object.defineProperty(metadata, $descriptors, {
712
+ value: metadata[$descriptors] || {},
713
+ enumerable: false,
714
+ configurable: true,
715
+ });
716
+ if (descriptor) {
717
+ // for encoder
718
+ metadata[$descriptors][name] = descriptor;
719
+ metadata[$descriptors][`_${name}`] = {
720
+ value: undefined,
721
+ writable: true,
722
+ enumerable: false,
723
+ configurable: true,
724
+ };
667
725
  }
668
726
  else {
669
- //
670
- // Get the child type from parent structure.
671
- // - ["string"] => "string"
672
- // - { map: "string" } => "string"
673
- // - { set: "string" } => "string"
674
- //
675
- return this.ref[$childType];
727
+ // for decoder
728
+ metadata[$descriptors][name] = {
729
+ value: undefined,
730
+ writable: true,
731
+ enumerable: true,
732
+ configurable: true,
733
+ };
676
734
  }
677
- }
678
- getChange(index) {
679
- return this.indexedOperations[index];
680
- }
681
- //
682
- // used during `.encode()`
683
- //
684
- getValue(index, isEncodeAll = false) {
685
- //
686
- // `isEncodeAll` param is only used by ArraySchema
687
- //
688
- return this.ref[$getByIndex](index, isEncodeAll);
689
- }
690
- delete(index, operation, allChangesIndex = index) {
691
- if (index === undefined) {
692
- try {
693
- throw new Error(`@colyseus/schema ${this.ref.constructor.name}: trying to delete non-existing index '${index}'`);
694
- }
695
- catch (e) {
696
- console.warn(e);
735
+ // map -1 as last field index
736
+ Object.defineProperty(metadata, $numFields, {
737
+ value: index,
738
+ enumerable: false,
739
+ configurable: true
740
+ });
741
+ // map field name => index (non enumerable)
742
+ Object.defineProperty(metadata, name, {
743
+ value: index,
744
+ enumerable: false,
745
+ configurable: true,
746
+ });
747
+ // if child Ref/complex type, add to -4
748
+ if (typeof (metadata[index].type) !== "string") {
749
+ if (metadata[$refTypeFieldIndexes] === undefined) {
750
+ Object.defineProperty(metadata, $refTypeFieldIndexes, {
751
+ value: [],
752
+ enumerable: false,
753
+ configurable: true,
754
+ });
697
755
  }
698
- return;
756
+ metadata[$refTypeFieldIndexes].push(index);
699
757
  }
700
- const changeSet = (this.filteredChanges)
701
- ? this.filteredChanges
702
- : this.changes;
703
- this.indexedOperations[index] = operation ?? exports.OPERATION.DELETE;
704
- setOperationAtIndex(changeSet, index);
705
- const previousValue = this.getValue(index);
706
- // remove `root` reference
707
- if (previousValue && previousValue[$changes]) {
708
- //
709
- // FIXME: this.root is "undefined"
710
- //
711
- // This method is being called at decoding time when a DELETE operation is found.
712
- //
713
- // - This is due to using the concrete Schema class at decoding time.
714
- // - "Reflected" structures do not have this problem.
715
- //
716
- // (the property descriptors should NOT be used at decoding time. only at encoding time.)
717
- //
718
- this.root?.remove(previousValue[$changes]);
758
+ },
759
+ setTag(metadata, fieldName, tag) {
760
+ const index = metadata[fieldName];
761
+ const field = metadata[index];
762
+ // add 'tag' to the field
763
+ field.tag = tag;
764
+ if (!metadata[$viewFieldIndexes]) {
765
+ // -2: all field indexes with "view" tag
766
+ Object.defineProperty(metadata, $viewFieldIndexes, {
767
+ value: [],
768
+ enumerable: false,
769
+ configurable: true
770
+ });
771
+ // -3: field indexes by "view" tag
772
+ Object.defineProperty(metadata, $fieldIndexesByViewTag, {
773
+ value: {},
774
+ enumerable: false,
775
+ configurable: true
776
+ });
777
+ }
778
+ metadata[$viewFieldIndexes].push(index);
779
+ if (!metadata[$fieldIndexesByViewTag][tag]) {
780
+ metadata[$fieldIndexesByViewTag][tag] = [];
781
+ }
782
+ metadata[$fieldIndexesByViewTag][tag].push(index);
783
+ },
784
+ setFields(target, fields) {
785
+ // for inheritance support
786
+ const constructor = target.prototype.constructor;
787
+ TypeContext.register(constructor);
788
+ const parentClass = Object.getPrototypeOf(constructor);
789
+ const parentMetadata = parentClass && parentClass[Symbol.metadata];
790
+ const metadata = Metadata.initialize(constructor);
791
+ // Use Schema's methods if not defined in the class
792
+ if (!constructor[$track]) {
793
+ constructor[$track] = Schema[$track];
719
794
  }
720
- //
721
- // FIXME: this is looking a ugly and repeated
722
- //
723
- if (this.filteredChanges) {
724
- deleteOperationAtIndex(this.allFilteredChanges, allChangesIndex);
725
- enqueueChangeTree(this.root, this, 'filteredChanges');
795
+ if (!constructor[$encoder]) {
796
+ constructor[$encoder] = Schema[$encoder];
726
797
  }
727
- else {
728
- deleteOperationAtIndex(this.allChanges, allChangesIndex);
729
- enqueueChangeTree(this.root, this, 'changes');
798
+ if (!constructor[$decoder]) {
799
+ constructor[$decoder] = Schema[$decoder];
800
+ }
801
+ if (!constructor.prototype.toJSON) {
802
+ constructor.prototype.toJSON = Schema.prototype.toJSON;
730
803
  }
731
- }
732
- endEncode() {
733
- this.indexedOperations = {};
734
- // // clear changes
735
- // this.changes.indexes = {};
736
- // this.changes.operations.length = 0;
737
- // ArraySchema and MapSchema have a custom "encode end" method
738
- this.ref[$onEncodeEnd]?.();
739
- // Not a new instance anymore
740
- this.isNew = false;
741
- }
742
- discard(discardAll = false) {
743
804
  //
744
- // > MapSchema:
745
- // Remove cached key to ensure ADD operations is unsed instead of
746
- // REPLACE in case same key is used on next patches.
805
+ // detect index for this field, considering inheritance
747
806
  //
748
- this.ref[$onEncodeEnd]?.();
749
- this.indexedOperations = {};
750
- this.changes.indexes = {};
751
- this.changes.operations.length = 0;
752
- this.changes.queueRootIndex = undefined;
753
- if (this.filteredChanges !== undefined) {
754
- this.filteredChanges.indexes = {};
755
- this.filteredChanges.operations.length = 0;
756
- this.filteredChanges.queueRootIndex = undefined;
807
+ let fieldIndex = metadata[$numFields] // current structure already has fields defined
808
+ ?? (parentMetadata && parentMetadata[$numFields]) // parent structure has fields defined
809
+ ?? -1; // no fields defined
810
+ fieldIndex++;
811
+ for (const field in fields) {
812
+ const type = fields[field];
813
+ const normalizedType = getNormalizedType(type);
814
+ // FIXME: this code is duplicated from @type() annotation
815
+ const complexTypeKlass = (Array.isArray(type))
816
+ ? getType("array")
817
+ : (typeof (Object.keys(type)[0]) === "string") && getType(Object.keys(type)[0]);
818
+ const childType = (complexTypeKlass)
819
+ ? Object.values(type)[0]
820
+ : normalizedType;
821
+ Metadata.addField(metadata, fieldIndex, field, type, getPropertyDescriptor(`_${field}`, fieldIndex, childType, complexTypeKlass));
822
+ fieldIndex++;
757
823
  }
758
- if (discardAll) {
759
- this.allChanges.indexes = {};
760
- this.allChanges.operations.length = 0;
761
- if (this.allFilteredChanges !== undefined) {
762
- this.allFilteredChanges.indexes = {};
763
- this.allFilteredChanges.operations.length = 0;
824
+ return target;
825
+ },
826
+ isDeprecated(metadata, field) {
827
+ return metadata[field].deprecated === true;
828
+ },
829
+ init(klass) {
830
+ //
831
+ // Used only to initialize an empty Schema (Encoder#constructor)
832
+ // TODO: remove/refactor this...
833
+ //
834
+ const metadata = {};
835
+ klass[Symbol.metadata] = metadata;
836
+ Object.defineProperty(metadata, $numFields, {
837
+ value: 0,
838
+ enumerable: false,
839
+ configurable: true,
840
+ });
841
+ },
842
+ initialize(constructor) {
843
+ const parentClass = Object.getPrototypeOf(constructor);
844
+ const parentMetadata = parentClass[Symbol.metadata];
845
+ let metadata = constructor[Symbol.metadata] ?? Object.create(null);
846
+ // make sure inherited classes have their own metadata object.
847
+ if (parentClass !== Schema && metadata === parentMetadata) {
848
+ metadata = Object.create(null);
849
+ if (parentMetadata) {
850
+ //
851
+ // assign parent metadata to current
852
+ //
853
+ Object.setPrototypeOf(metadata, parentMetadata);
854
+ // $numFields
855
+ Object.defineProperty(metadata, $numFields, {
856
+ value: parentMetadata[$numFields],
857
+ enumerable: false,
858
+ configurable: true,
859
+ writable: true,
860
+ });
861
+ // $viewFieldIndexes / $fieldIndexesByViewTag
862
+ if (parentMetadata[$viewFieldIndexes] !== undefined) {
863
+ Object.defineProperty(metadata, $viewFieldIndexes, {
864
+ value: [...parentMetadata[$viewFieldIndexes]],
865
+ enumerable: false,
866
+ configurable: true,
867
+ writable: true,
868
+ });
869
+ Object.defineProperty(metadata, $fieldIndexesByViewTag, {
870
+ value: { ...parentMetadata[$fieldIndexesByViewTag] },
871
+ enumerable: false,
872
+ configurable: true,
873
+ writable: true,
874
+ });
875
+ }
876
+ // $refTypeFieldIndexes
877
+ if (parentMetadata[$refTypeFieldIndexes] !== undefined) {
878
+ Object.defineProperty(metadata, $refTypeFieldIndexes, {
879
+ value: [...parentMetadata[$refTypeFieldIndexes]],
880
+ enumerable: false,
881
+ configurable: true,
882
+ writable: true,
883
+ });
884
+ }
885
+ // $descriptors
886
+ Object.defineProperty(metadata, $descriptors, {
887
+ value: { ...parentMetadata[$descriptors] },
888
+ enumerable: false,
889
+ configurable: true,
890
+ writable: true,
891
+ });
764
892
  }
765
- // remove children references
766
- this.forEachChild((changeTree, _) => this.root?.remove(changeTree));
767
893
  }
768
- }
769
- /**
770
- * Recursively discard all changes from this, and child structures.
771
- */
772
- discardAll() {
773
- const keys = Object.keys(this.indexedOperations);
774
- for (let i = 0, len = keys.length; i < len; i++) {
775
- const value = this.getValue(Number(keys[i]));
776
- if (value && value[$changes]) {
777
- value[$changes].discardAll();
778
- }
894
+ constructor[Symbol.metadata] = metadata;
895
+ return metadata;
896
+ },
897
+ isValidInstance(klass) {
898
+ return (klass.constructor[Symbol.metadata] &&
899
+ Object.prototype.hasOwnProperty.call(klass.constructor[Symbol.metadata], $numFields));
900
+ },
901
+ getFields(klass) {
902
+ const metadata = klass[Symbol.metadata];
903
+ const fields = {};
904
+ for (let i = 0; i <= metadata[$numFields]; i++) {
905
+ fields[metadata[i].name] = metadata[i].type;
779
906
  }
780
- this.discard();
907
+ return fields;
781
908
  }
782
- ensureRefId() {
783
- // skip if refId is already set.
784
- if (this.refId !== undefined) {
785
- return;
786
- }
787
- this.refId = this.root.getNextUniqueId();
909
+ };
910
+
911
+ function setOperationAtIndex(changeSet, index) {
912
+ const operationsIndex = changeSet.indexes[index];
913
+ if (operationsIndex === undefined) {
914
+ changeSet.indexes[index] = changeSet.operations.push(index) - 1;
788
915
  }
789
- get changed() {
790
- return (Object.entries(this.indexedOperations).length > 0);
916
+ else {
917
+ changeSet.operations[operationsIndex] = index;
791
918
  }
792
- checkIsFiltered(metadata, parent, parentIndex) {
793
- // Detect if current structure has "filters" declared
794
- this.isPartiallyFiltered = metadata?.[$viewFieldIndexes] !== undefined;
795
- if (this.isPartiallyFiltered) {
796
- this.filteredChanges = this.filteredChanges || { indexes: {}, operations: [] };
797
- this.allFilteredChanges = this.allFilteredChanges || { indexes: {}, operations: [] };
798
- }
799
- // skip if parent is not set
800
- if (!parent) {
801
- return;
802
- }
803
- if (!Metadata.isValidInstance(parent)) {
804
- const parentChangeTree = parent[$changes];
805
- parent = parentChangeTree.parent;
806
- parentIndex = parentChangeTree.parentIndex;
807
- }
808
- const parentMetadata = parent.constructor?.[Symbol.metadata];
809
- this.isFiltered = parentMetadata?.[$viewFieldIndexes]?.includes(parentIndex);
919
+ }
920
+ function deleteOperationAtIndex(changeSet, index) {
921
+ const operationsIndex = changeSet.indexes[index];
922
+ if (operationsIndex !== undefined) {
923
+ changeSet.operations[operationsIndex] = undefined;
924
+ }
925
+ delete changeSet.indexes[index];
926
+ }
927
+ function enqueueChangeTree(root, changeTree, changeSet, queueRootIndex = changeTree[changeSet].queueRootIndex) {
928
+ if (root && root[changeSet][queueRootIndex] !== changeTree) {
929
+ changeTree[changeSet].queueRootIndex = root[changeSet].push(changeTree) - 1;
930
+ }
931
+ }
932
+ class ChangeTree {
933
+ constructor(ref) {
934
+ this.isFiltered = false;
935
+ this.isPartiallyFiltered = false;
936
+ this.indexedOperations = {};
810
937
  //
811
- // TODO: refactor this!
938
+ // TODO:
939
+ // try storing the index + operation per item.
940
+ // example: 1024 & 1025 => ADD, 1026 => DELETE
812
941
  //
813
- // swapping `changes` and `filteredChanges` is required here
814
- // because "isFiltered" may not be imedialely available on `change()`
942
+ // => https://chatgpt.com/share/67107d0c-bc20-8004-8583-83b17dd7c196
815
943
  //
816
- if (this.isFiltered) {
817
- this.filteredChanges = { indexes: {}, operations: [] };
944
+ this.changes = { indexes: {}, operations: [] };
945
+ this.allChanges = { indexes: {}, operations: [] };
946
+ /**
947
+ * Is this a new instance? Used on ArraySchema to determine OPERATION.MOVE_AND_ADD operation.
948
+ */
949
+ this.isNew = true;
950
+ this.ref = ref;
951
+ //
952
+ // Does this structure have "filters" declared?
953
+ //
954
+ if (ref.constructor[Symbol.metadata]?.[$viewFieldIndexes]) {
818
955
  this.allFilteredChanges = { indexes: {}, operations: [] };
819
- if (this.changes.operations.length > 0) {
820
- // swap changes reference
821
- const changes = this.changes;
822
- this.changes = this.filteredChanges;
823
- this.filteredChanges = changes;
824
- // swap "all changes" reference
825
- const allFilteredChanges = this.allFilteredChanges;
826
- this.allFilteredChanges = this.allChanges;
827
- this.allChanges = allFilteredChanges;
828
- // console.log("SWAP =>", {
829
- // "this.allFilteredChanges": this.allFilteredChanges,
830
- // "this.allChanges": this.allChanges
831
- // })
832
- }
956
+ this.filteredChanges = { indexes: {}, operations: [] };
833
957
  }
834
958
  }
835
- }
836
-
837
- /**
838
- * Copyright (c) 2018 Endel Dreyer
839
- * Copyright (c) 2014 Ion Drive Software Ltd.
840
- *
841
- * Permission is hereby granted, free of charge, to any person obtaining a copy
842
- * of this software and associated documentation files (the "Software"), to deal
843
- * in the Software without restriction, including without limitation the rights
844
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
845
- * copies of the Software, and to permit persons to whom the Software is
846
- * furnished to do so, subject to the following conditions:
847
- *
848
- * The above copyright notice and this permission notice shall be included in all
849
- * copies or substantial portions of the Software.
850
- *
851
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
852
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
853
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
854
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
855
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
856
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
857
- * SOFTWARE
858
- */
859
- /**
860
- * msgpack implementation highly based on notepack.io
861
- * https://github.com/darrachequesne/notepack
862
- */
863
- let textEncoder;
864
- // @ts-ignore
865
- try {
866
- textEncoder = new TextEncoder();
867
- }
868
- catch (e) { }
869
- const hasBufferByteLength = (typeof Buffer !== 'undefined' && Buffer.byteLength);
870
- const utf8Length = (hasBufferByteLength)
871
- ? Buffer.byteLength // node
872
- : function (str, _) {
873
- var c = 0, length = 0;
874
- for (var i = 0, l = str.length; i < l; i++) {
875
- c = str.charCodeAt(i);
876
- if (c < 0x80) {
877
- length += 1;
959
+ setRoot(root) {
960
+ this.root = root;
961
+ const isNewChangeTree = this.root.add(this);
962
+ const metadata = this.ref.constructor[Symbol.metadata];
963
+ if (this.root.types.hasFilters) {
964
+ //
965
+ // At Schema initialization, the "root" structure might not be available
966
+ // yet, as it only does once the "Encoder" has been set up.
967
+ //
968
+ // So the "parent" may be already set without a "root".
969
+ //
970
+ this.checkIsFiltered(metadata, this.parent, this.parentIndex);
971
+ if (this.isFiltered || this.isPartiallyFiltered) {
972
+ enqueueChangeTree(root, this, 'filteredChanges');
973
+ if (isNewChangeTree) {
974
+ this.root.allFilteredChanges.push(this);
975
+ }
878
976
  }
879
- else if (c < 0x800) {
880
- length += 2;
977
+ }
978
+ if (!this.isFiltered) {
979
+ enqueueChangeTree(root, this, 'changes');
980
+ if (isNewChangeTree) {
981
+ this.root.allChanges.push(this);
881
982
  }
882
- else if (c < 0xd800 || c >= 0xe000) {
883
- length += 3;
983
+ }
984
+ // Recursively set root on child structures
985
+ if (metadata) {
986
+ metadata[$refTypeFieldIndexes]?.forEach((index) => {
987
+ const field = metadata[index];
988
+ const value = this.ref[field.name];
989
+ value?.[$changes].setRoot(root);
990
+ });
991
+ }
992
+ else if (this.ref[$childType] && typeof (this.ref[$childType]) !== "string") {
993
+ // MapSchema / ArraySchema, etc.
994
+ this.ref.forEach((value, key) => {
995
+ value[$changes].setRoot(root);
996
+ });
997
+ }
998
+ }
999
+ setParent(parent, root, parentIndex) {
1000
+ this.parent = parent;
1001
+ this.parentIndex = parentIndex;
1002
+ // avoid setting parents with empty `root`
1003
+ if (!root) {
1004
+ return;
1005
+ }
1006
+ const metadata = this.ref.constructor[Symbol.metadata];
1007
+ // skip if parent is already set
1008
+ if (root !== this.root) {
1009
+ this.root = root;
1010
+ const isNewChangeTree = root.add(this);
1011
+ if (root.types.hasFilters) {
1012
+ this.checkIsFiltered(metadata, parent, parentIndex);
1013
+ if (this.isFiltered || this.isPartiallyFiltered) {
1014
+ enqueueChangeTree(root, this, 'filteredChanges');
1015
+ if (isNewChangeTree) {
1016
+ this.root.allFilteredChanges.push(this);
1017
+ }
1018
+ }
884
1019
  }
885
- else {
886
- i++;
887
- length += 4;
1020
+ if (!this.isFiltered) {
1021
+ enqueueChangeTree(root, this, 'changes');
1022
+ if (isNewChangeTree) {
1023
+ this.root.allChanges.push(this);
1024
+ }
888
1025
  }
889
1026
  }
890
- return length;
891
- };
892
- function utf8Write(view, str, it) {
893
- var c = 0;
894
- for (var i = 0, l = str.length; i < l; i++) {
895
- c = str.charCodeAt(i);
896
- if (c < 0x80) {
897
- view[it.offset++] = c;
1027
+ else {
1028
+ root.add(this);
898
1029
  }
899
- else if (c < 0x800) {
900
- view[it.offset] = 0xc0 | (c >> 6);
901
- view[it.offset + 1] = 0x80 | (c & 0x3f);
902
- it.offset += 2;
1030
+ // assign same parent on child structures
1031
+ if (metadata) {
1032
+ metadata[$refTypeFieldIndexes]?.forEach((index) => {
1033
+ const field = metadata[index];
1034
+ const value = this.ref[field.name];
1035
+ value?.[$changes].setParent(this.ref, root, index);
1036
+ // try { throw new Error(); } catch (e) {
1037
+ // console.log(e.stack);
1038
+ // }
1039
+ });
903
1040
  }
904
- else if (c < 0xd800 || c >= 0xe000) {
905
- view[it.offset] = 0xe0 | (c >> 12);
906
- view[it.offset + 1] = 0x80 | (c >> 6 & 0x3f);
907
- view[it.offset + 2] = 0x80 | (c & 0x3f);
908
- it.offset += 3;
1041
+ else if (this.ref[$childType] && typeof (this.ref[$childType]) !== "string") {
1042
+ // MapSchema / ArraySchema, etc.
1043
+ this.ref.forEach((value, key) => {
1044
+ value[$changes].setParent(this.ref, root, this.indexes[key] ?? key);
1045
+ });
909
1046
  }
910
- else {
911
- i++;
912
- c = 0x10000 + (((c & 0x3ff) << 10) | (str.charCodeAt(i) & 0x3ff));
913
- view[it.offset] = 0xf0 | (c >> 18);
914
- view[it.offset + 1] = 0x80 | (c >> 12 & 0x3f);
915
- view[it.offset + 2] = 0x80 | (c >> 6 & 0x3f);
916
- view[it.offset + 3] = 0x80 | (c & 0x3f);
917
- it.offset += 4;
1047
+ }
1048
+ forEachChild(callback) {
1049
+ //
1050
+ // assign same parent on child structures
1051
+ //
1052
+ const metadata = this.ref.constructor[Symbol.metadata];
1053
+ if (metadata) {
1054
+ metadata[$refTypeFieldIndexes]?.forEach((index) => {
1055
+ const field = metadata[index];
1056
+ const value = this.ref[field.name];
1057
+ if (value) {
1058
+ callback(value[$changes], index);
1059
+ }
1060
+ });
1061
+ }
1062
+ else if (this.ref[$childType] && typeof (this.ref[$childType]) !== "string") {
1063
+ // MapSchema / ArraySchema, etc.
1064
+ this.ref.forEach((value, key) => {
1065
+ callback(value[$changes], this.indexes[key] ?? key);
1066
+ });
918
1067
  }
919
1068
  }
920
- }
921
- function int8$1(bytes, value, it) {
922
- bytes[it.offset++] = value & 255;
923
- }
924
- function uint8$1(bytes, value, it) {
925
- bytes[it.offset++] = value & 255;
926
- }
927
- function int16$1(bytes, value, it) {
928
- bytes[it.offset++] = value & 255;
929
- bytes[it.offset++] = (value >> 8) & 255;
930
- }
931
- function uint16$1(bytes, value, it) {
932
- bytes[it.offset++] = value & 255;
933
- bytes[it.offset++] = (value >> 8) & 255;
934
- }
935
- function int32$1(bytes, value, it) {
936
- bytes[it.offset++] = value & 255;
937
- bytes[it.offset++] = (value >> 8) & 255;
938
- bytes[it.offset++] = (value >> 16) & 255;
939
- bytes[it.offset++] = (value >> 24) & 255;
940
- }
941
- function uint32$1(bytes, value, it) {
942
- const b4 = value >> 24;
943
- const b3 = value >> 16;
944
- const b2 = value >> 8;
945
- const b1 = value;
946
- bytes[it.offset++] = b1 & 255;
947
- bytes[it.offset++] = b2 & 255;
948
- bytes[it.offset++] = b3 & 255;
949
- bytes[it.offset++] = b4 & 255;
950
- }
951
- function int64$1(bytes, value, it) {
952
- const high = Math.floor(value / Math.pow(2, 32));
953
- const low = value >>> 0;
954
- uint32$1(bytes, low, it);
955
- uint32$1(bytes, high, it);
956
- }
957
- function uint64$1(bytes, value, it) {
958
- const high = (value / Math.pow(2, 32)) >> 0;
959
- const low = value >>> 0;
960
- uint32$1(bytes, low, it);
961
- uint32$1(bytes, high, it);
962
- }
963
- function float32$1(bytes, value, it) {
964
- writeFloat32(bytes, value, it);
965
- }
966
- function float64$1(bytes, value, it) {
967
- writeFloat64(bytes, value, it);
968
- }
969
- const _int32$1 = new Int32Array(2);
970
- const _float32$1 = new Float32Array(_int32$1.buffer);
971
- const _float64$1 = new Float64Array(_int32$1.buffer);
972
- function writeFloat32(bytes, value, it) {
973
- _float32$1[0] = value;
974
- int32$1(bytes, _int32$1[0], it);
975
- }
976
- function writeFloat64(bytes, value, it) {
977
- _float64$1[0] = value;
978
- int32$1(bytes, _int32$1[0 ], it);
979
- int32$1(bytes, _int32$1[1 ], it);
980
- }
981
- function boolean$1(bytes, value, it) {
982
- bytes[it.offset++] = value ? 1 : 0; // uint8
983
- }
984
- function string$1(bytes, value, it) {
985
- // encode `null` strings as empty.
986
- if (!value) {
987
- value = "";
1069
+ operation(op) {
1070
+ // operations without index use negative values to represent them
1071
+ // this is checked during .encode() time.
1072
+ this.changes.operations.push(-op);
1073
+ enqueueChangeTree(this.root, this, 'changes');
988
1074
  }
989
- let length = utf8Length(value, "utf8");
990
- let size = 0;
991
- // fixstr
992
- if (length < 0x20) {
993
- bytes[it.offset++] = length | 0xa0;
994
- size = 1;
1075
+ change(index, operation = exports.OPERATION.ADD) {
1076
+ const metadata = this.ref.constructor[Symbol.metadata];
1077
+ const isFiltered = this.isFiltered || (metadata?.[index]?.tag !== undefined);
1078
+ const changeSet = (isFiltered)
1079
+ ? this.filteredChanges
1080
+ : this.changes;
1081
+ const previousOperation = this.indexedOperations[index];
1082
+ if (!previousOperation || previousOperation === exports.OPERATION.DELETE) {
1083
+ const op = (!previousOperation)
1084
+ ? operation
1085
+ : (previousOperation === exports.OPERATION.DELETE)
1086
+ ? exports.OPERATION.DELETE_AND_ADD
1087
+ : operation;
1088
+ //
1089
+ // TODO: are DELETE operations being encoded as ADD here ??
1090
+ //
1091
+ this.indexedOperations[index] = op;
1092
+ }
1093
+ setOperationAtIndex(changeSet, index);
1094
+ if (isFiltered) {
1095
+ setOperationAtIndex(this.allFilteredChanges, index);
1096
+ if (this.root) {
1097
+ enqueueChangeTree(this.root, this, 'filteredChanges');
1098
+ enqueueChangeTree(this.root, this, 'allFilteredChanges');
1099
+ }
1100
+ }
1101
+ else {
1102
+ setOperationAtIndex(this.allChanges, index);
1103
+ enqueueChangeTree(this.root, this, 'changes');
1104
+ }
995
1105
  }
996
- // str 8
997
- else if (length < 0x100) {
998
- bytes[it.offset++] = 0xd9;
999
- bytes[it.offset++] = length % 255;
1000
- size = 2;
1106
+ shiftChangeIndexes(shiftIndex) {
1107
+ //
1108
+ // Used only during:
1109
+ //
1110
+ // - ArraySchema#unshift()
1111
+ //
1112
+ const changeSet = (this.isFiltered)
1113
+ ? this.filteredChanges
1114
+ : this.changes;
1115
+ const newIndexedOperations = {};
1116
+ const newIndexes = {};
1117
+ for (const index in this.indexedOperations) {
1118
+ newIndexedOperations[Number(index) + shiftIndex] = this.indexedOperations[index];
1119
+ newIndexes[Number(index) + shiftIndex] = changeSet[index];
1120
+ }
1121
+ this.indexedOperations = newIndexedOperations;
1122
+ changeSet.indexes = newIndexes;
1123
+ changeSet.operations = changeSet.operations.map((index) => index + shiftIndex);
1001
1124
  }
1002
- // str 16
1003
- else if (length < 0x10000) {
1004
- bytes[it.offset++] = 0xda;
1005
- uint16$1(bytes, length, it);
1006
- size = 3;
1125
+ shiftAllChangeIndexes(shiftIndex, startIndex = 0) {
1126
+ //
1127
+ // Used only during:
1128
+ //
1129
+ // - ArraySchema#splice()
1130
+ //
1131
+ if (this.isFiltered || this.isPartiallyFiltered) {
1132
+ this._shiftAllChangeIndexes(shiftIndex, startIndex, this.allFilteredChanges);
1133
+ this._shiftAllChangeIndexes(shiftIndex, startIndex, this.allChanges);
1134
+ }
1135
+ else {
1136
+ this._shiftAllChangeIndexes(shiftIndex, startIndex, this.allChanges);
1137
+ }
1007
1138
  }
1008
- // str 32
1009
- else if (length < 0x100000000) {
1010
- bytes[it.offset++] = 0xdb;
1011
- uint32$1(bytes, length, it);
1012
- size = 5;
1139
+ _shiftAllChangeIndexes(shiftIndex, startIndex = 0, changeSet) {
1140
+ const newIndexes = {};
1141
+ for (const key in changeSet.indexes) {
1142
+ const index = changeSet.indexes[key];
1143
+ if (index > startIndex) {
1144
+ newIndexes[Number(key) + shiftIndex] = index;
1145
+ }
1146
+ else {
1147
+ newIndexes[key] = index;
1148
+ }
1149
+ }
1150
+ changeSet.indexes = newIndexes;
1151
+ for (let i = 0; i < changeSet.operations.length; i++) {
1152
+ const index = changeSet.operations[i];
1153
+ if (index > startIndex) {
1154
+ changeSet.operations[i] = index + shiftIndex;
1155
+ }
1156
+ }
1013
1157
  }
1014
- else {
1015
- throw new Error('String too long');
1158
+ indexedOperation(index, operation, allChangesIndex = index) {
1159
+ this.indexedOperations[index] = operation;
1160
+ if (this.filteredChanges) {
1161
+ setOperationAtIndex(this.allFilteredChanges, allChangesIndex);
1162
+ setOperationAtIndex(this.filteredChanges, index);
1163
+ enqueueChangeTree(this.root, this, 'filteredChanges');
1164
+ }
1165
+ else {
1166
+ setOperationAtIndex(this.allChanges, allChangesIndex);
1167
+ setOperationAtIndex(this.changes, index);
1168
+ enqueueChangeTree(this.root, this, 'changes');
1169
+ }
1016
1170
  }
1017
- utf8Write(bytes, value, it);
1018
- return size + length;
1019
- }
1020
- function number$1(bytes, value, it) {
1021
- if (isNaN(value)) {
1022
- return number$1(bytes, 0, it);
1171
+ getType(index) {
1172
+ if (Metadata.isValidInstance(this.ref)) {
1173
+ const metadata = this.ref.constructor[Symbol.metadata];
1174
+ return metadata[index].type;
1175
+ }
1176
+ else {
1177
+ //
1178
+ // Get the child type from parent structure.
1179
+ // - ["string"] => "string"
1180
+ // - { map: "string" } => "string"
1181
+ // - { set: "string" } => "string"
1182
+ //
1183
+ return this.ref[$childType];
1184
+ }
1023
1185
  }
1024
- else if (!isFinite(value)) {
1025
- return number$1(bytes, (value > 0) ? Number.MAX_SAFE_INTEGER : -Number.MAX_SAFE_INTEGER, it);
1186
+ getChange(index) {
1187
+ return this.indexedOperations[index];
1026
1188
  }
1027
- else if (value !== (value | 0)) {
1028
- bytes[it.offset++] = 0xcb;
1029
- writeFloat64(bytes, value, it);
1030
- return 9;
1031
- // TODO: encode float 32?
1032
- // is it possible to differentiate between float32 / float64 here?
1033
- // // float 32
1034
- // bytes.push(0xca);
1035
- // writeFloat32(bytes, value);
1036
- // return 5;
1189
+ //
1190
+ // used during `.encode()`
1191
+ //
1192
+ getValue(index, isEncodeAll = false) {
1193
+ //
1194
+ // `isEncodeAll` param is only used by ArraySchema
1195
+ //
1196
+ return this.ref[$getByIndex](index, isEncodeAll);
1037
1197
  }
1038
- if (value >= 0) {
1039
- // positive fixnum
1040
- if (value < 0x80) {
1041
- bytes[it.offset++] = value & 255; // uint8
1042
- return 1;
1198
+ delete(index, operation, allChangesIndex = index) {
1199
+ if (index === undefined) {
1200
+ try {
1201
+ throw new Error(`@colyseus/schema ${this.ref.constructor.name}: trying to delete non-existing index '${index}'`);
1202
+ }
1203
+ catch (e) {
1204
+ console.warn(e);
1205
+ }
1206
+ return;
1043
1207
  }
1044
- // uint 8
1045
- if (value < 0x100) {
1046
- bytes[it.offset++] = 0xcc;
1047
- bytes[it.offset++] = value & 255; // uint8
1048
- return 2;
1208
+ const changeSet = (this.filteredChanges)
1209
+ ? this.filteredChanges
1210
+ : this.changes;
1211
+ this.indexedOperations[index] = operation ?? exports.OPERATION.DELETE;
1212
+ setOperationAtIndex(changeSet, index);
1213
+ const previousValue = this.getValue(index);
1214
+ // remove `root` reference
1215
+ if (previousValue && previousValue[$changes]) {
1216
+ //
1217
+ // FIXME: this.root is "undefined"
1218
+ //
1219
+ // This method is being called at decoding time when a DELETE operation is found.
1220
+ //
1221
+ // - This is due to using the concrete Schema class at decoding time.
1222
+ // - "Reflected" structures do not have this problem.
1223
+ //
1224
+ // (the property descriptors should NOT be used at decoding time. only at encoding time.)
1225
+ //
1226
+ this.root?.remove(previousValue[$changes]);
1049
1227
  }
1050
- // uint 16
1051
- if (value < 0x10000) {
1052
- bytes[it.offset++] = 0xcd;
1053
- uint16$1(bytes, value, it);
1054
- return 3;
1228
+ //
1229
+ // FIXME: this is looking a ugly and repeated
1230
+ //
1231
+ if (this.filteredChanges) {
1232
+ deleteOperationAtIndex(this.allFilteredChanges, allChangesIndex);
1233
+ enqueueChangeTree(this.root, this, 'filteredChanges');
1055
1234
  }
1056
- // uint 32
1057
- if (value < 0x100000000) {
1058
- bytes[it.offset++] = 0xce;
1059
- uint32$1(bytes, value, it);
1060
- return 5;
1235
+ else {
1236
+ deleteOperationAtIndex(this.allChanges, allChangesIndex);
1237
+ enqueueChangeTree(this.root, this, 'changes');
1061
1238
  }
1062
- // uint 64
1063
- bytes[it.offset++] = 0xcf;
1064
- uint64$1(bytes, value, it);
1065
- return 9;
1066
1239
  }
1067
- else {
1068
- // negative fixnum
1069
- if (value >= -0x20) {
1070
- bytes[it.offset++] = 0xe0 | (value + 0x20);
1071
- return 1;
1240
+ endEncode() {
1241
+ this.indexedOperations = {};
1242
+ // // clear changes
1243
+ // this.changes.indexes = {};
1244
+ // this.changes.operations.length = 0;
1245
+ // ArraySchema and MapSchema have a custom "encode end" method
1246
+ this.ref[$onEncodeEnd]?.();
1247
+ // Not a new instance anymore
1248
+ this.isNew = false;
1249
+ }
1250
+ discard(discardAll = false) {
1251
+ //
1252
+ // > MapSchema:
1253
+ // Remove cached key to ensure ADD operations is unsed instead of
1254
+ // REPLACE in case same key is used on next patches.
1255
+ //
1256
+ this.ref[$onEncodeEnd]?.();
1257
+ this.indexedOperations = {};
1258
+ this.changes.indexes = {};
1259
+ this.changes.operations.length = 0;
1260
+ this.changes.queueRootIndex = undefined;
1261
+ if (this.filteredChanges !== undefined) {
1262
+ this.filteredChanges.indexes = {};
1263
+ this.filteredChanges.operations.length = 0;
1264
+ this.filteredChanges.queueRootIndex = undefined;
1265
+ }
1266
+ if (discardAll) {
1267
+ this.allChanges.indexes = {};
1268
+ this.allChanges.operations.length = 0;
1269
+ if (this.allFilteredChanges !== undefined) {
1270
+ this.allFilteredChanges.indexes = {};
1271
+ this.allFilteredChanges.operations.length = 0;
1272
+ }
1273
+ // remove children references
1274
+ this.forEachChild((changeTree, _) => this.root?.remove(changeTree));
1275
+ }
1276
+ }
1277
+ /**
1278
+ * Recursively discard all changes from this, and child structures.
1279
+ */
1280
+ discardAll() {
1281
+ const keys = Object.keys(this.indexedOperations);
1282
+ for (let i = 0, len = keys.length; i < len; i++) {
1283
+ const value = this.getValue(Number(keys[i]));
1284
+ if (value && value[$changes]) {
1285
+ value[$changes].discardAll();
1286
+ }
1287
+ }
1288
+ this.discard();
1289
+ }
1290
+ ensureRefId() {
1291
+ // skip if refId is already set.
1292
+ if (this.refId !== undefined) {
1293
+ return;
1294
+ }
1295
+ this.refId = this.root.getNextUniqueId();
1296
+ }
1297
+ get changed() {
1298
+ return (Object.entries(this.indexedOperations).length > 0);
1299
+ }
1300
+ checkIsFiltered(metadata, parent, parentIndex) {
1301
+ // Detect if current structure has "filters" declared
1302
+ this.isPartiallyFiltered = metadata?.[$viewFieldIndexes] !== undefined;
1303
+ if (this.isPartiallyFiltered) {
1304
+ this.filteredChanges = this.filteredChanges || { indexes: {}, operations: [] };
1305
+ this.allFilteredChanges = this.allFilteredChanges || { indexes: {}, operations: [] };
1072
1306
  }
1073
- // int 8
1074
- if (value >= -0x80) {
1075
- bytes[it.offset++] = 0xd0;
1076
- int8$1(bytes, value, it);
1077
- return 2;
1307
+ // skip if parent is not set
1308
+ if (!parent) {
1309
+ return;
1078
1310
  }
1079
- // int 16
1080
- if (value >= -0x8000) {
1081
- bytes[it.offset++] = 0xd1;
1082
- int16$1(bytes, value, it);
1083
- return 3;
1311
+ if (!Metadata.isValidInstance(parent)) {
1312
+ const parentChangeTree = parent[$changes];
1313
+ parent = parentChangeTree.parent;
1314
+ parentIndex = parentChangeTree.parentIndex;
1084
1315
  }
1085
- // int 32
1086
- if (value >= -0x80000000) {
1087
- bytes[it.offset++] = 0xd2;
1088
- int32$1(bytes, value, it);
1089
- return 5;
1316
+ const parentMetadata = parent.constructor?.[Symbol.metadata];
1317
+ this.isFiltered = parentMetadata?.[$viewFieldIndexes]?.includes(parentIndex);
1318
+ //
1319
+ // TODO: refactor this!
1320
+ //
1321
+ // swapping `changes` and `filteredChanges` is required here
1322
+ // because "isFiltered" may not be imedialely available on `change()`
1323
+ //
1324
+ if (this.isFiltered) {
1325
+ this.filteredChanges = { indexes: {}, operations: [] };
1326
+ this.allFilteredChanges = { indexes: {}, operations: [] };
1327
+ if (this.changes.operations.length > 0) {
1328
+ // swap changes reference
1329
+ const changes = this.changes;
1330
+ this.changes = this.filteredChanges;
1331
+ this.filteredChanges = changes;
1332
+ // swap "all changes" reference
1333
+ const allFilteredChanges = this.allFilteredChanges;
1334
+ this.allFilteredChanges = this.allChanges;
1335
+ this.allChanges = allFilteredChanges;
1336
+ // console.log("SWAP =>", {
1337
+ // "this.allFilteredChanges": this.allFilteredChanges,
1338
+ // "this.allChanges": this.allChanges
1339
+ // })
1340
+ }
1090
1341
  }
1091
- // int 64
1092
- bytes[it.offset++] = 0xd3;
1093
- int64$1(bytes, value, it);
1094
- return 9;
1095
1342
  }
1096
1343
  }
1097
1344
 
1098
- var encode = /*#__PURE__*/Object.freeze({
1099
- __proto__: null,
1100
- boolean: boolean$1,
1101
- float32: float32$1,
1102
- float64: float64$1,
1103
- int16: int16$1,
1104
- int32: int32$1,
1105
- int64: int64$1,
1106
- int8: int8$1,
1107
- number: number$1,
1108
- string: string$1,
1109
- uint16: uint16$1,
1110
- uint32: uint32$1,
1111
- uint64: uint64$1,
1112
- uint8: uint8$1,
1113
- utf8Length: utf8Length,
1114
- utf8Write: utf8Write,
1115
- writeFloat32: writeFloat32,
1116
- writeFloat64: writeFloat64
1117
- });
1118
-
1119
1345
  function encodeValue(encoder, bytes, type, value, operation, it) {
1120
1346
  if (typeof (type) === "string") {
1121
1347
  encode[type]?.(bytes, value, it);
@@ -1125,7 +1351,7 @@
1125
1351
  // Encode refId for this instance.
1126
1352
  // The actual instance is going to be encoded on next `changeTree` iteration.
1127
1353
  //
1128
- number$1(bytes, value[$changes].refId, it);
1354
+ encode.number(bytes, value[$changes].refId, it);
1129
1355
  // Try to encode inherited TYPE_ID if it's an ADD operation.
1130
1356
  if ((operation & exports.OPERATION.ADD) === exports.OPERATION.ADD) {
1131
1357
  encoder.tryEncodeTypeId(bytes, type, value.constructor, it);
@@ -1136,7 +1362,7 @@
1136
1362
  // Encode refId for this instance.
1137
1363
  // The actual instance is going to be encoded on next `changeTree` iteration.
1138
1364
  //
1139
- number$1(bytes, value[$changes].refId, it);
1365
+ encode.number(bytes, value[$changes].refId, it);
1140
1366
  }
1141
1367
  }
1142
1368
  /**
@@ -1167,7 +1393,7 @@
1167
1393
  return;
1168
1394
  }
1169
1395
  // encode index
1170
- number$1(bytes, index, it);
1396
+ encode.number(bytes, index, it);
1171
1397
  // Do not encode value for DELETE operations
1172
1398
  if (operation === exports.OPERATION.DELETE) {
1173
1399
  return;
@@ -1182,7 +1408,7 @@
1182
1408
  // MapSchema dynamic key
1183
1409
  //
1184
1410
  const dynamicIndex = changeTree.ref['$indexes'].get(index);
1185
- string$1(bytes, dynamicIndex, it);
1411
+ encode.string(bytes, dynamicIndex, it);
1186
1412
  }
1187
1413
  }
1188
1414
  const type = ref[$childType];
@@ -1230,7 +1456,7 @@
1230
1456
  return;
1231
1457
  }
1232
1458
  // encode index
1233
- number$1(bytes, refOrIndex, it);
1459
+ encode.number(bytes, refOrIndex, it);
1234
1460
  // Do not encode value for DELETE operations
1235
1461
  if (operation === exports.OPERATION.DELETE) {
1236
1462
  return;
@@ -1248,259 +1474,6 @@
1248
1474
  encodeValue(encoder, bytes, type, value, operation, it);
1249
1475
  };
1250
1476
 
1251
- /**
1252
- * Copyright (c) 2018 Endel Dreyer
1253
- * Copyright (c) 2014 Ion Drive Software Ltd.
1254
- *
1255
- * Permission is hereby granted, free of charge, to any person obtaining a copy
1256
- * of this software and associated documentation files (the "Software"), to deal
1257
- * in the Software without restriction, including without limitation the rights
1258
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1259
- * copies of the Software, and to permit persons to whom the Software is
1260
- * furnished to do so, subject to the following conditions:
1261
- *
1262
- * The above copyright notice and this permission notice shall be included in all
1263
- * copies or substantial portions of the Software.
1264
- *
1265
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1266
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1267
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1268
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1269
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1270
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1271
- * SOFTWARE
1272
- */
1273
- function utf8Read(bytes, it, length) {
1274
- var string = '', chr = 0;
1275
- for (var i = it.offset, end = it.offset + length; i < end; i++) {
1276
- var byte = bytes[i];
1277
- if ((byte & 0x80) === 0x00) {
1278
- string += String.fromCharCode(byte);
1279
- continue;
1280
- }
1281
- if ((byte & 0xe0) === 0xc0) {
1282
- string += String.fromCharCode(((byte & 0x1f) << 6) |
1283
- (bytes[++i] & 0x3f));
1284
- continue;
1285
- }
1286
- if ((byte & 0xf0) === 0xe0) {
1287
- string += String.fromCharCode(((byte & 0x0f) << 12) |
1288
- ((bytes[++i] & 0x3f) << 6) |
1289
- ((bytes[++i] & 0x3f) << 0));
1290
- continue;
1291
- }
1292
- if ((byte & 0xf8) === 0xf0) {
1293
- chr = ((byte & 0x07) << 18) |
1294
- ((bytes[++i] & 0x3f) << 12) |
1295
- ((bytes[++i] & 0x3f) << 6) |
1296
- ((bytes[++i] & 0x3f) << 0);
1297
- if (chr >= 0x010000) { // surrogate pair
1298
- chr -= 0x010000;
1299
- string += String.fromCharCode((chr >>> 10) + 0xD800, (chr & 0x3FF) + 0xDC00);
1300
- }
1301
- else {
1302
- string += String.fromCharCode(chr);
1303
- }
1304
- continue;
1305
- }
1306
- console.error('Invalid byte ' + byte.toString(16));
1307
- // (do not throw error to avoid server/client from crashing due to hack attemps)
1308
- // throw new Error('Invalid byte ' + byte.toString(16));
1309
- }
1310
- it.offset += length;
1311
- return string;
1312
- }
1313
- function int8(bytes, it) {
1314
- return uint8(bytes, it) << 24 >> 24;
1315
- }
1316
- function uint8(bytes, it) {
1317
- return bytes[it.offset++];
1318
- }
1319
- function int16(bytes, it) {
1320
- return uint16(bytes, it) << 16 >> 16;
1321
- }
1322
- function uint16(bytes, it) {
1323
- return bytes[it.offset++] | bytes[it.offset++] << 8;
1324
- }
1325
- function int32(bytes, it) {
1326
- return bytes[it.offset++] | bytes[it.offset++] << 8 | bytes[it.offset++] << 16 | bytes[it.offset++] << 24;
1327
- }
1328
- function uint32(bytes, it) {
1329
- return int32(bytes, it) >>> 0;
1330
- }
1331
- function float32(bytes, it) {
1332
- return readFloat32(bytes, it);
1333
- }
1334
- function float64(bytes, it) {
1335
- return readFloat64(bytes, it);
1336
- }
1337
- function int64(bytes, it) {
1338
- const low = uint32(bytes, it);
1339
- const high = int32(bytes, it) * Math.pow(2, 32);
1340
- return high + low;
1341
- }
1342
- function uint64(bytes, it) {
1343
- const low = uint32(bytes, it);
1344
- const high = uint32(bytes, it) * Math.pow(2, 32);
1345
- return high + low;
1346
- }
1347
- const _int32 = new Int32Array(2);
1348
- const _float32 = new Float32Array(_int32.buffer);
1349
- const _float64 = new Float64Array(_int32.buffer);
1350
- function readFloat32(bytes, it) {
1351
- _int32[0] = int32(bytes, it);
1352
- return _float32[0];
1353
- }
1354
- function readFloat64(bytes, it) {
1355
- _int32[0 ] = int32(bytes, it);
1356
- _int32[1 ] = int32(bytes, it);
1357
- return _float64[0];
1358
- }
1359
- function boolean(bytes, it) {
1360
- return uint8(bytes, it) > 0;
1361
- }
1362
- function string(bytes, it) {
1363
- const prefix = bytes[it.offset++];
1364
- let length;
1365
- if (prefix < 0xc0) {
1366
- // fixstr
1367
- length = prefix & 0x1f;
1368
- }
1369
- else if (prefix === 0xd9) {
1370
- length = uint8(bytes, it);
1371
- }
1372
- else if (prefix === 0xda) {
1373
- length = uint16(bytes, it);
1374
- }
1375
- else if (prefix === 0xdb) {
1376
- length = uint32(bytes, it);
1377
- }
1378
- return utf8Read(bytes, it, length);
1379
- }
1380
- function stringCheck(bytes, it) {
1381
- const prefix = bytes[it.offset];
1382
- return (
1383
- // fixstr
1384
- (prefix < 0xc0 && prefix > 0xa0) ||
1385
- // str 8
1386
- prefix === 0xd9 ||
1387
- // str 16
1388
- prefix === 0xda ||
1389
- // str 32
1390
- prefix === 0xdb);
1391
- }
1392
- function number(bytes, it) {
1393
- const prefix = bytes[it.offset++];
1394
- if (prefix < 0x80) {
1395
- // positive fixint
1396
- return prefix;
1397
- }
1398
- else if (prefix === 0xca) {
1399
- // float 32
1400
- return readFloat32(bytes, it);
1401
- }
1402
- else if (prefix === 0xcb) {
1403
- // float 64
1404
- return readFloat64(bytes, it);
1405
- }
1406
- else if (prefix === 0xcc) {
1407
- // uint 8
1408
- return uint8(bytes, it);
1409
- }
1410
- else if (prefix === 0xcd) {
1411
- // uint 16
1412
- return uint16(bytes, it);
1413
- }
1414
- else if (prefix === 0xce) {
1415
- // uint 32
1416
- return uint32(bytes, it);
1417
- }
1418
- else if (prefix === 0xcf) {
1419
- // uint 64
1420
- return uint64(bytes, it);
1421
- }
1422
- else if (prefix === 0xd0) {
1423
- // int 8
1424
- return int8(bytes, it);
1425
- }
1426
- else if (prefix === 0xd1) {
1427
- // int 16
1428
- return int16(bytes, it);
1429
- }
1430
- else if (prefix === 0xd2) {
1431
- // int 32
1432
- return int32(bytes, it);
1433
- }
1434
- else if (prefix === 0xd3) {
1435
- // int 64
1436
- return int64(bytes, it);
1437
- }
1438
- else if (prefix > 0xdf) {
1439
- // negative fixint
1440
- return (0xff - prefix + 1) * -1;
1441
- }
1442
- }
1443
- function numberCheck(bytes, it) {
1444
- const prefix = bytes[it.offset];
1445
- // positive fixint - 0x00 - 0x7f
1446
- // float 32 - 0xca
1447
- // float 64 - 0xcb
1448
- // uint 8 - 0xcc
1449
- // uint 16 - 0xcd
1450
- // uint 32 - 0xce
1451
- // uint 64 - 0xcf
1452
- // int 8 - 0xd0
1453
- // int 16 - 0xd1
1454
- // int 32 - 0xd2
1455
- // int 64 - 0xd3
1456
- return (prefix < 0x80 ||
1457
- (prefix >= 0xca && prefix <= 0xd3));
1458
- }
1459
- function arrayCheck(bytes, it) {
1460
- return bytes[it.offset] < 0xa0;
1461
- // const prefix = bytes[it.offset] ;
1462
- // if (prefix < 0xa0) {
1463
- // return prefix;
1464
- // // array
1465
- // } else if (prefix === 0xdc) {
1466
- // it.offset += 2;
1467
- // } else if (0xdd) {
1468
- // it.offset += 4;
1469
- // }
1470
- // return prefix;
1471
- }
1472
- function switchStructureCheck(bytes, it) {
1473
- return (
1474
- // previous byte should be `SWITCH_TO_STRUCTURE`
1475
- bytes[it.offset - 1] === SWITCH_TO_STRUCTURE &&
1476
- // next byte should be a number
1477
- (bytes[it.offset] < 0x80 || (bytes[it.offset] >= 0xca && bytes[it.offset] <= 0xd3)));
1478
- }
1479
-
1480
- var decode = /*#__PURE__*/Object.freeze({
1481
- __proto__: null,
1482
- arrayCheck: arrayCheck,
1483
- boolean: boolean,
1484
- float32: float32,
1485
- float64: float64,
1486
- int16: int16,
1487
- int32: int32,
1488
- int64: int64,
1489
- int8: int8,
1490
- number: number,
1491
- numberCheck: numberCheck,
1492
- readFloat32: readFloat32,
1493
- readFloat64: readFloat64,
1494
- string: string,
1495
- stringCheck: stringCheck,
1496
- switchStructureCheck: switchStructureCheck,
1497
- uint16: uint16,
1498
- uint32: uint32,
1499
- uint64: uint64,
1500
- uint8: uint8,
1501
- utf8Read: utf8Read
1502
- });
1503
-
1504
1477
  const DEFINITION_MISMATCH = -1;
1505
1478
  function decodeValue(decoder, operation, ref, index, type, bytes, it, allChanges) {
1506
1479
  const $root = decoder.root;
@@ -1535,7 +1508,7 @@
1535
1508
  }
1536
1509
  if (operation === exports.OPERATION.DELETE) ;
1537
1510
  else if (Schema.is(type)) {
1538
- const refId = number(bytes, it);
1511
+ const refId = decode.number(bytes, it);
1539
1512
  value = $root.refs.get(refId);
1540
1513
  if (previousValue) {
1541
1514
  const previousRefId = $root.refIds.get(previousValue);
@@ -1564,7 +1537,7 @@
1564
1537
  }
1565
1538
  else {
1566
1539
  const typeDef = getType(Object.keys(type)[0]);
1567
- const refId = number(bytes, it);
1540
+ const refId = decode.number(bytes, it);
1568
1541
  const valueRef = ($root.refs.has(refId))
1569
1542
  ? previousValue || $root.refs.get(refId)
1570
1543
  : new typeDef.constructor();
@@ -1643,12 +1616,12 @@
1643
1616
  ref.clear();
1644
1617
  return;
1645
1618
  }
1646
- const index = number(bytes, it);
1619
+ const index = decode.number(bytes, it);
1647
1620
  const type = ref[$childType];
1648
1621
  let dynamicIndex;
1649
1622
  if ((operation & exports.OPERATION.ADD) === exports.OPERATION.ADD) { // ADD or DELETE_AND_ADD
1650
1623
  if (typeof (ref['set']) === "function") {
1651
- dynamicIndex = string(bytes, it); // MapSchema
1624
+ dynamicIndex = decode.string(bytes, it); // MapSchema
1652
1625
  ref['setIndex'](index, dynamicIndex);
1653
1626
  }
1654
1627
  else {
@@ -1710,7 +1683,7 @@
1710
1683
  }
1711
1684
  else if (operation === exports.OPERATION.DELETE_BY_REFID) {
1712
1685
  // TODO: refactor here, try to follow same flow as below
1713
- const refId = number(bytes, it);
1686
+ const refId = decode.number(bytes, it);
1714
1687
  const previousValue = decoder.root.refs.get(refId);
1715
1688
  index = ref.findIndex((value) => value === previousValue);
1716
1689
  ref[$deleteByIndex](index);
@@ -1726,7 +1699,7 @@
1726
1699
  return;
1727
1700
  }
1728
1701
  else if (operation === exports.OPERATION.ADD_BY_REFID) {
1729
- const refId = number(bytes, it);
1702
+ const refId = decode.number(bytes, it);
1730
1703
  const itemByRefId = decoder.root.refs.get(refId);
1731
1704
  // use existing index, or push new value
1732
1705
  index = (itemByRefId)
@@ -1734,7 +1707,7 @@
1734
1707
  : ref.length;
1735
1708
  }
1736
1709
  else {
1737
- index = number(bytes, it);
1710
+ index = decode.number(bytes, it);
1738
1711
  }
1739
1712
  const type = ref[$childType];
1740
1713
  let dynamicIndex = index;
@@ -1781,6 +1754,10 @@
1781
1754
  console.log(`trying to encode "NaN" in ${klass.constructor.name}#${field}`);
1782
1755
  }
1783
1756
  break;
1757
+ case "bigint64":
1758
+ case "biguint64":
1759
+ typeofTarget = "bigint";
1760
+ break;
1784
1761
  case "string":
1785
1762
  typeofTarget = "string";
1786
1763
  allowNull = true;
@@ -1788,6 +1765,10 @@
1788
1765
  case "boolean":
1789
1766
  // boolean is always encoded as true/false based on truthiness
1790
1767
  return;
1768
+ default:
1769
+ // skip assertion for custom types
1770
+ // TODO: allow custom types to define their own assertions
1771
+ return;
1791
1772
  }
1792
1773
  if (typeof (value) !== typeofTarget && (!allowNull || (allowNull && value !== null))) {
1793
1774
  let foundValue = `'${JSON.stringify(value)}'${(value && value.constructor && ` (${value.constructor.name})`) || ''}`;
@@ -3792,7 +3773,7 @@
3792
3773
  // (unless it "hasView", which will need to revisit the root)
3793
3774
  if (hasView || it.offset > initialOffset || changeTree !== rootChangeTree) {
3794
3775
  buffer[it.offset++] = SWITCH_TO_STRUCTURE & 255;
3795
- number$1(buffer, changeTree.refId, it);
3776
+ encode.number(buffer, changeTree.refId, it);
3796
3777
  }
3797
3778
  for (let j = 0, numChanges = operations.operations.length; j < numChanges; j++) {
3798
3779
  const fieldIndex = operations.operations[j];
@@ -3921,7 +3902,7 @@
3921
3902
  const encoder = ctor[$encoder];
3922
3903
  const metadata = ctor[Symbol.metadata];
3923
3904
  bytes[it.offset++] = SWITCH_TO_STRUCTURE & 255;
3924
- number$1(bytes, changeTree.refId, it);
3905
+ encode.number(bytes, changeTree.refId, it);
3925
3906
  const keys = Object.keys(changes);
3926
3907
  for (let i = 0, numChanges = keys.length; i < numChanges; i++) {
3927
3908
  const key = keys[i];
@@ -3987,7 +3968,7 @@
3987
3968
  }
3988
3969
  if (baseTypeId !== targetTypeId) {
3989
3970
  bytes[it.offset++] = TYPE_ID & 255;
3990
- number$1(bytes, targetTypeId, it);
3971
+ encode.number(bytes, targetTypeId, it);
3991
3972
  }
3992
3973
  }
3993
3974
  get hasChanges() {
@@ -4147,7 +4128,7 @@
4147
4128
  //
4148
4129
  if (bytes[it.offset] == SWITCH_TO_STRUCTURE) {
4149
4130
  it.offset++;
4150
- this.currentRefId = number(bytes, it);
4131
+ this.currentRefId = decode.number(bytes, it);
4151
4132
  const nextRef = $root.refs.get(this.currentRefId);
4152
4133
  //
4153
4134
  // Trying to access a reference that haven't been decoded yet.
@@ -4169,9 +4150,9 @@
4169
4150
  //
4170
4151
  const nextIterator = { offset: it.offset };
4171
4152
  while (it.offset < totalBytes) {
4172
- if (switchStructureCheck(bytes, it)) {
4153
+ if (bytes[it.offset] === SWITCH_TO_STRUCTURE) {
4173
4154
  nextIterator.offset = it.offset + 1;
4174
- if ($root.refs.has(number(bytes, nextIterator))) {
4155
+ if ($root.refs.has(decode.number(bytes, nextIterator))) {
4175
4156
  break;
4176
4157
  }
4177
4158
  }
@@ -4192,7 +4173,7 @@
4192
4173
  let type;
4193
4174
  if (bytes[it.offset] === TYPE_ID) {
4194
4175
  it.offset++;
4195
- const type_id = number(bytes, it);
4176
+ const type_id = decode.number(bytes, it);
4196
4177
  type = this.context.get(type_id);
4197
4178
  }
4198
4179
  return type || defaultType;
@@ -4871,6 +4852,7 @@
4871
4852
  exports.decode = decode;
4872
4853
  exports.decodeKeyValueOperation = decodeKeyValueOperation;
4873
4854
  exports.decodeSchemaOperation = decodeSchemaOperation;
4855
+ exports.defineCustomTypes = defineCustomTypes;
4874
4856
  exports.defineTypes = defineTypes;
4875
4857
  exports.deprecated = deprecated;
4876
4858
  exports.dumpChanges = dumpChanges;