@colyseus/schema 3.0.39 → 3.0.40

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.
@@ -58,16 +58,23 @@ export class Decoder<T extends Schema = any> {
58
58
  if (bytes[it.offset] == SWITCH_TO_STRUCTURE) {
59
59
  it.offset++;
60
60
 
61
- this.currentRefId = decode.number(bytes, it);
62
- const nextRef = $root.refs.get(this.currentRefId) as Schema;
61
+ const nextRefId = decode.number(bytes, it);
62
+ const nextRef = $root.refs.get(nextRefId);
63
63
 
64
64
  //
65
65
  // Trying to access a reference that haven't been decoded yet.
66
66
  //
67
- if (!nextRef) { throw new Error(`"refId" not found: ${this.currentRefId}`); }
67
+ if (!nextRef) {
68
+ console.error(`"refId" not found: ${nextRefId}`, { previousRef: this, previousRefId: this.currentRefId });
69
+ console.warn("Please report this to the developers. All refIds =>");
70
+ console.warn(Schema.debugRefIdsDecoder(this));
71
+ this.skipCurrentStructure(bytes, it, totalBytes);
72
+ }
68
73
  ref[$onDecodeEnd]?.()
69
- ref = nextRef;
70
74
 
75
+ this.currentRefId = nextRefId;
76
+
77
+ ref = nextRef;
71
78
  decoder = ref.constructor[$decoder];
72
79
 
73
80
  continue;
@@ -77,22 +84,7 @@ export class Decoder<T extends Schema = any> {
77
84
 
78
85
  if (result === DEFINITION_MISMATCH) {
79
86
  console.warn("@colyseus/schema: definition mismatch");
80
-
81
- //
82
- // keep skipping next bytes until reaches a known structure
83
- // by local decoder.
84
- //
85
- const nextIterator: Iterator = { offset: it.offset };
86
- while (it.offset < totalBytes) {
87
- if (bytes[it.offset] === SWITCH_TO_STRUCTURE) {
88
- nextIterator.offset = it.offset + 1;
89
- if ($root.refs.has(decode.number(bytes, nextIterator))) {
90
- break;
91
- }
92
- }
93
-
94
- it.offset++;
95
- }
87
+ this.skipCurrentStructure(bytes, it, totalBytes);
96
88
  continue;
97
89
  }
98
90
  }
@@ -109,6 +101,23 @@ export class Decoder<T extends Schema = any> {
109
101
  return allChanges;
110
102
  }
111
103
 
104
+ skipCurrentStructure(bytes: Buffer, it: Iterator, totalBytes: number) {
105
+ //
106
+ // keep skipping next bytes until reaches a known structure
107
+ // by local decoder.
108
+ //
109
+ const nextIterator: Iterator = { offset: it.offset };
110
+ while (it.offset < totalBytes) {
111
+ if (bytes[it.offset] === SWITCH_TO_STRUCTURE) {
112
+ nextIterator.offset = it.offset + 1;
113
+ if (this.root.refs.has(decode.number(bytes, nextIterator))) {
114
+ break;
115
+ }
116
+ }
117
+ it.offset++;
118
+ }
119
+ }
120
+
112
121
  getInstanceType(bytes: Buffer, it: Iterator, defaultType: typeof Schema): typeof Schema {
113
122
  let type: typeof Schema;
114
123
 
@@ -26,7 +26,7 @@ export class ReferenceTracker {
26
26
  public refs = new Map<number, Ref>();
27
27
  public refIds = new WeakMap<Ref, number>();
28
28
 
29
- public refCounts: { [refId: number]: number; } = {};
29
+ public refCount: { [refId: number]: number; } = {};
30
30
  public deletedRefs = new Set<number>();
31
31
 
32
32
  public callbacks: { [refId: number]: SchemaCallbacks } = {};
@@ -42,7 +42,7 @@ export class ReferenceTracker {
42
42
  this.refIds.set(ref, refId);
43
43
 
44
44
  if (incrementCount) {
45
- this.refCounts[refId] = (this.refCounts[refId] || 0) + 1;
45
+ this.refCount[refId] = (this.refCount[refId] || 0) + 1;
46
46
  }
47
47
 
48
48
  if (this.deletedRefs.has(refId)) {
@@ -52,7 +52,7 @@ export class ReferenceTracker {
52
52
 
53
53
  // for decoding
54
54
  removeRef(refId: number) {
55
- const refCount = this.refCounts[refId];
55
+ const refCount = this.refCount[refId];
56
56
 
57
57
  if (refCount === undefined) {
58
58
  try {
@@ -73,7 +73,7 @@ export class ReferenceTracker {
73
73
  return;
74
74
  }
75
75
 
76
- if ((this.refCounts[refId] = refCount - 1) <= 0) {
76
+ if ((this.refCount[refId] = refCount - 1) <= 0) {
77
77
  this.deletedRefs.add(refId);
78
78
  }
79
79
  }
@@ -82,7 +82,7 @@ export class ReferenceTracker {
82
82
  this.refs.clear();
83
83
  this.deletedRefs.clear();
84
84
  this.callbacks = {};
85
- this.refCounts = {};
85
+ this.refCount = {};
86
86
  }
87
87
 
88
88
  // for decoding
@@ -91,7 +91,7 @@ export class ReferenceTracker {
91
91
  //
92
92
  // Skip active references.
93
93
  //
94
- if (this.refCounts[refId] > 0) { return; }
94
+ if (this.refCount[refId] > 0) { return; }
95
95
 
96
96
  const ref = this.refs.get(refId);
97
97
 
@@ -121,7 +121,7 @@ export class ReferenceTracker {
121
121
  }
122
122
 
123
123
  this.refs.delete(refId); // remove ref
124
- delete this.refCounts[refId]; // remove ref count
124
+ delete this.refCount[refId]; // remove ref count
125
125
  delete this.callbacks[refId]; // remove callbacks
126
126
  });
127
127