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