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