@colyseus/schema 3.0.0-alpha.19 → 3.0.0-alpha.22
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/build/cjs/index.js +94 -63
- package/build/cjs/index.js.map +1 -1
- package/build/esm/index.mjs +94 -61
- package/build/esm/index.mjs.map +1 -1
- package/build/umd/index.js +94 -63
- package/lib/Metadata.d.ts +1 -0
- package/lib/Metadata.js +32 -1
- package/lib/Metadata.js.map +1 -1
- package/lib/Reflection.d.ts +0 -1
- package/lib/Reflection.js +27 -20
- package/lib/Reflection.js.map +1 -1
- package/lib/Schema.d.ts +1 -1
- package/lib/annotations.js +12 -10
- package/lib/annotations.js.map +1 -1
- package/lib/codegen/api.js +1 -2
- package/lib/codegen/api.js.map +1 -1
- package/lib/codegen/languages/cpp.js +1 -2
- package/lib/codegen/languages/cpp.js.map +1 -1
- package/lib/codegen/languages/csharp.js +1 -2
- package/lib/codegen/languages/csharp.js.map +1 -1
- package/lib/codegen/languages/haxe.js +1 -2
- package/lib/codegen/languages/haxe.js.map +1 -1
- package/lib/codegen/languages/java.js +1 -2
- package/lib/codegen/languages/java.js.map +1 -1
- package/lib/codegen/languages/js.js +1 -2
- package/lib/codegen/languages/js.js.map +1 -1
- package/lib/codegen/languages/lua.js +1 -2
- package/lib/codegen/languages/lua.js.map +1 -1
- package/lib/codegen/languages/ts.js +1 -2
- package/lib/codegen/languages/ts.js.map +1 -1
- package/lib/codegen/parser.js +2 -3
- package/lib/codegen/parser.js.map +1 -1
- package/lib/codegen/types.js +3 -3
- package/lib/codegen/types.js.map +1 -1
- package/lib/decoder/DecodeOperation.d.ts +0 -1
- package/lib/decoder/DecodeOperation.js +2 -2
- package/lib/decoder/DecodeOperation.js.map +1 -1
- package/lib/decoder/Decoder.d.ts +0 -1
- package/lib/decoder/strategy/RawChanges.js +1 -2
- package/lib/decoder/strategy/RawChanges.js.map +1 -1
- package/lib/decoder/strategy/StateCallbacks.js +1 -2
- package/lib/decoder/strategy/StateCallbacks.js.map +1 -1
- package/lib/encoder/EncodeOperation.d.ts +0 -1
- package/lib/encoder/EncodeOperation.js +3 -3
- package/lib/encoder/EncodeOperation.js.map +1 -1
- package/lib/encoder/Encoder.d.ts +0 -1
- package/lib/encoder/Encoder.js +5 -5
- package/lib/encoder/Encoder.js.map +1 -1
- package/lib/encoder/StateView.js +2 -2
- package/lib/encoder/StateView.js.map +1 -1
- package/lib/encoding/assert.js +3 -3
- package/lib/encoding/assert.js.map +1 -1
- package/lib/encoding/decode.js +20 -21
- package/lib/encoding/decode.js.map +1 -1
- package/lib/encoding/encode.d.ts +0 -1
- package/lib/encoding/encode.js +17 -17
- package/lib/encoding/encode.js.map +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.js.map +1 -1
- package/lib/types/custom/ArraySchema.d.ts +2 -2
- package/lib/types/custom/ArraySchema.js +0 -8
- package/lib/types/custom/ArraySchema.js.map +1 -1
- package/lib/types/registry.js +3 -4
- package/lib/types/registry.js.map +1 -1
- package/lib/types/utils.js +1 -2
- package/lib/types/utils.js.map +1 -1
- package/lib/utils.js +3 -4
- package/lib/utils.js.map +1 -1
- package/package.json +5 -5
- package/src/Metadata.ts +39 -1
- package/src/Reflection.ts +30 -20
- package/src/annotations.ts +6 -2
- package/src/encoder/Encoder.ts +5 -5
- package/src/index.ts +2 -0
- package/src/types/custom/ArraySchema.ts +2 -1
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@colyseus/schema",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.22",
|
|
4
4
|
"description": "Binary state serializer with delta encoding for games",
|
|
5
5
|
"bin": {
|
|
6
6
|
"schema-codegen": "./bin/schema-codegen"
|
|
7
7
|
},
|
|
8
8
|
"scripts": {
|
|
9
|
-
"build": "tsc && rollup -c rollup.config.
|
|
9
|
+
"build": "tsc && rollup -c rollup.config.mjs",
|
|
10
10
|
"watch": "tsc -w",
|
|
11
11
|
"test": "mocha --require ts-node/register test/*.test.ts test/**/*.test.ts",
|
|
12
12
|
"coverage": "nyc mocha --require ts-node/register --require source-map-support/register --recursive test/**Test.ts",
|
|
@@ -73,13 +73,13 @@
|
|
|
73
73
|
"nanoid": "^3.1.10",
|
|
74
74
|
"nyc": "^14.1.1",
|
|
75
75
|
"rimraf": "^3.0.0",
|
|
76
|
-
"rollup": "^
|
|
76
|
+
"rollup": "^4.18.0",
|
|
77
77
|
"sinon": "^7.2.2",
|
|
78
78
|
"source-map-support": "^0.5.13",
|
|
79
79
|
"ts-node": "^10.9.2",
|
|
80
80
|
"tslib": "^2.1.0",
|
|
81
|
-
"tsx": "^
|
|
82
|
-
"typescript": "^5.
|
|
81
|
+
"tsx": "^3.13.0",
|
|
82
|
+
"typescript": "^5.4.5"
|
|
83
83
|
},
|
|
84
84
|
"nyc": {
|
|
85
85
|
"extension": [
|
package/src/Metadata.ts
CHANGED
|
@@ -120,10 +120,48 @@ export const Metadata = {
|
|
|
120
120
|
init(klass: any) {
|
|
121
121
|
//
|
|
122
122
|
// Used only to initialize an empty Schema (Encoder#constructor)
|
|
123
|
+
// TODO: remove/refactor this...
|
|
123
124
|
//
|
|
124
125
|
const metadata = {};
|
|
125
126
|
klass.constructor[Symbol.metadata] = metadata;
|
|
126
|
-
Object.defineProperty(metadata, -1, {
|
|
127
|
+
Object.defineProperty(metadata, -1, {
|
|
128
|
+
value: 0,
|
|
129
|
+
enumerable: false,
|
|
130
|
+
configurable: true,
|
|
131
|
+
});
|
|
132
|
+
},
|
|
133
|
+
|
|
134
|
+
initialize(constructor: any, parentMetadata?: any) {
|
|
135
|
+
let metadata: Metadata = constructor[Symbol.metadata] ?? Object.create(null);
|
|
136
|
+
|
|
137
|
+
// make sure inherited classes have their own metadata object.
|
|
138
|
+
if (constructor[Symbol.metadata] === parentMetadata) {
|
|
139
|
+
metadata = Object.create(null);
|
|
140
|
+
|
|
141
|
+
if (parentMetadata) {
|
|
142
|
+
// assign parent metadata to current
|
|
143
|
+
Object.assign(metadata, parentMetadata);
|
|
144
|
+
|
|
145
|
+
for (let i = 0; i <= parentMetadata[-1]; i++) {
|
|
146
|
+
Object.defineProperty(metadata, i, {
|
|
147
|
+
value: parentMetadata[i],
|
|
148
|
+
enumerable: false,
|
|
149
|
+
configurable: true,
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
Object.defineProperty(metadata, -1, {
|
|
154
|
+
value: parentMetadata[-1],
|
|
155
|
+
enumerable: false,
|
|
156
|
+
configurable: true,
|
|
157
|
+
writable: true,
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
constructor[Symbol.metadata] = metadata;
|
|
163
|
+
|
|
164
|
+
return metadata;
|
|
127
165
|
},
|
|
128
166
|
|
|
129
167
|
isValidInstance(klass: any) {
|
package/src/Reflection.ts
CHANGED
|
@@ -106,62 +106,72 @@ export class Reflection extends Schema {
|
|
|
106
106
|
const reflectionDecoder = new Decoder(reflection);
|
|
107
107
|
reflectionDecoder.decode(bytes, it);
|
|
108
108
|
|
|
109
|
-
const
|
|
109
|
+
const typeContext = new TypeContext();
|
|
110
110
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
const
|
|
111
|
+
// 1st pass, initialize metadata + inheritance
|
|
112
|
+
reflection.types.forEach((reflectionType) => {
|
|
113
|
+
const parentClass: typeof Schema = typeContext.get(reflectionType.extendsId) ?? Schema;
|
|
114
|
+
const schema: typeof Schema = class _ extends parentClass {};
|
|
114
115
|
|
|
115
|
-
|
|
116
|
-
const _metadata = parentKlass && parentKlass[Symbol.metadata] || Object.create(null);
|
|
117
|
-
Object.defineProperty(schema, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata })
|
|
116
|
+
const parentMetadata = parentClass[Symbol.metadata];
|
|
118
117
|
|
|
119
118
|
// register for inheritance support
|
|
120
119
|
TypeContext.register(schema);
|
|
121
120
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
121
|
+
// for inheritance support
|
|
122
|
+
Metadata.initialize(schema, parentMetadata);
|
|
123
|
+
|
|
124
|
+
typeContext.add(schema, reflectionType.id);
|
|
126
125
|
}, {});
|
|
127
126
|
|
|
127
|
+
// 2nd pass, set fields
|
|
128
128
|
reflection.types.forEach((reflectionType) => {
|
|
129
|
-
const schemaType =
|
|
129
|
+
const schemaType = typeContext.get(reflectionType.id);
|
|
130
130
|
const metadata = schemaType[Symbol.metadata];
|
|
131
131
|
|
|
132
|
-
|
|
133
|
-
const parentFieldIndex =
|
|
132
|
+
// FIXME: use metadata[-1] to get field count
|
|
133
|
+
const parentFieldIndex = 0;
|
|
134
|
+
|
|
135
|
+
// console.log("--------------------");
|
|
136
|
+
// // console.log("reflectionType", reflectionType.toJSON());
|
|
137
|
+
// console.log("reflectionType.fields", reflectionType.fields.toJSON());
|
|
138
|
+
// console.log("parentFieldIndex", parentFieldIndex);
|
|
139
|
+
|
|
140
|
+
//
|
|
141
|
+
// FIXME: set fields using parentKlass as well
|
|
142
|
+
// currently the fields are duplicated on inherited classes
|
|
143
|
+
//
|
|
144
|
+
// // const parentKlass = reflection.types[reflectionType.extendsId];
|
|
145
|
+
// // parentKlass.fields
|
|
134
146
|
|
|
135
147
|
reflectionType.fields.forEach((field, i) => {
|
|
136
148
|
const fieldIndex = parentFieldIndex + i;
|
|
137
149
|
|
|
138
150
|
if (field.referencedType !== undefined) {
|
|
139
151
|
let fieldType = field.type;
|
|
140
|
-
let refType =
|
|
152
|
+
let refType: PrimitiveType = typeContext.get(field.referencedType);
|
|
141
153
|
|
|
142
154
|
// map or array of primitive type (-1)
|
|
143
155
|
if (!refType) {
|
|
144
156
|
const typeInfo = field.type.split(":");
|
|
145
157
|
fieldType = typeInfo[0];
|
|
146
|
-
refType = typeInfo[1];
|
|
158
|
+
refType = typeInfo[1] as PrimitiveType; // string
|
|
147
159
|
}
|
|
148
160
|
|
|
149
161
|
if (fieldType === "ref") {
|
|
150
|
-
// type(refType)(schemaType.prototype, field.name);
|
|
151
162
|
Metadata.addField(metadata, fieldIndex, field.name, refType);
|
|
152
163
|
|
|
153
164
|
} else {
|
|
154
|
-
// type({ [fieldType]: refType } as DefinitionType)(schemaType.prototype, field.name);
|
|
155
165
|
Metadata.addField(metadata, fieldIndex, field.name, { [fieldType]: refType } as DefinitionType);
|
|
156
166
|
}
|
|
157
167
|
|
|
158
168
|
} else {
|
|
159
|
-
// type(field.type as PrimitiveType)(schemaType.prototype, field.name);
|
|
160
169
|
Metadata.addField(metadata, fieldIndex, field.name, field.type as PrimitiveType);
|
|
161
170
|
}
|
|
162
171
|
});
|
|
163
172
|
});
|
|
164
173
|
|
|
165
|
-
|
|
174
|
+
// @ts-ignore
|
|
175
|
+
return new (typeContext.get(0))();
|
|
166
176
|
}
|
|
167
177
|
}
|
package/src/annotations.ts
CHANGED
|
@@ -330,6 +330,8 @@ export function view<T> (tag: number = DEFAULT_VIEW_TAG) {
|
|
|
330
330
|
|
|
331
331
|
const parentClass = Object.getPrototypeOf(constructor);
|
|
332
332
|
const parentMetadata = parentClass[Symbol.metadata];
|
|
333
|
+
|
|
334
|
+
// TODO: use Metadata.initialize()
|
|
333
335
|
const metadata: Metadata = (constructor[Symbol.metadata] ??= Object.assign({}, constructor[Symbol.metadata], parentMetadata ?? Object.create(null)));
|
|
334
336
|
|
|
335
337
|
if (!metadata[fieldName]) {
|
|
@@ -356,6 +358,8 @@ export function unreliable<T> (target: T, field: string) {
|
|
|
356
358
|
|
|
357
359
|
const parentClass = Object.getPrototypeOf(constructor);
|
|
358
360
|
const parentMetadata = parentClass[Symbol.metadata];
|
|
361
|
+
|
|
362
|
+
// TODO: use Metadata.initialize()
|
|
359
363
|
const metadata: Metadata = (constructor[Symbol.metadata] ??= Object.assign({}, constructor[Symbol.metadata], parentMetadata ?? Object.create(null)));
|
|
360
364
|
|
|
361
365
|
if (!metadata[field]) {
|
|
@@ -389,8 +393,8 @@ export function type (
|
|
|
389
393
|
TypeContext.register(constructor);
|
|
390
394
|
|
|
391
395
|
const parentClass = Object.getPrototypeOf(constructor);
|
|
392
|
-
const parentMetadata = parentClass[Symbol.metadata];
|
|
393
|
-
const metadata
|
|
396
|
+
const parentMetadata = parentClass && parentClass[Symbol.metadata];
|
|
397
|
+
const metadata = Metadata.initialize(constructor, parentMetadata);
|
|
394
398
|
|
|
395
399
|
let fieldIndex: number;
|
|
396
400
|
|
package/src/encoder/Encoder.ts
CHANGED
|
@@ -112,11 +112,11 @@ export class Encoder<T extends Schema = any> {
|
|
|
112
112
|
// try { throw new Error(); } catch (e) {
|
|
113
113
|
// // only print if not coming from Reflection.ts
|
|
114
114
|
// if (!e.stack.includes("src/Reflection.ts")) {
|
|
115
|
-
//
|
|
116
|
-
//
|
|
117
|
-
//
|
|
118
|
-
//
|
|
119
|
-
//
|
|
115
|
+
// console.log("WILL ENCODE", {
|
|
116
|
+
// ref: changeTree.ref.constructor.name,
|
|
117
|
+
// fieldIndex,
|
|
118
|
+
// operation: OPERATION[operation],
|
|
119
|
+
// });
|
|
120
120
|
// }
|
|
121
121
|
// }
|
|
122
122
|
|
package/src/index.ts
CHANGED
|
@@ -4,6 +4,8 @@ export type { DataChange } from "./decoder/DecodeOperation";
|
|
|
4
4
|
import { $track, $encoder, $decoder, $filter, $getByIndex, $deleteByIndex, $changes, $childType } from "./types/symbols";
|
|
5
5
|
export { $track, $encoder, $decoder, $filter, $getByIndex, $deleteByIndex, $changes, $childType };
|
|
6
6
|
|
|
7
|
+
export type { ToJSON } from "./types/HelperTypes";
|
|
8
|
+
|
|
7
9
|
import { MapSchema } from "./types/custom/MapSchema"
|
|
8
10
|
export { MapSchema };
|
|
9
11
|
|
|
@@ -471,6 +471,7 @@ export class ArraySchema<V = any> implements Array<V>, Collection<number, V> {
|
|
|
471
471
|
* @param thisArg An object to which the this keyword can refer in the callbackfn function.
|
|
472
472
|
* If thisArg is omitted, undefined is used as the this value.
|
|
473
473
|
*/
|
|
474
|
+
every<S extends V>(predicate: (value: V, index: number, array: V[]) => value is S, thisArg?: any): this is S[];
|
|
474
475
|
every(callbackfn: (value: V, index: number, array: V[]) => unknown, thisArg?: any): boolean {
|
|
475
476
|
return this.items.every(callbackfn, thisArg);
|
|
476
477
|
}
|
|
@@ -690,7 +691,7 @@ export class ArraySchema<V = any> implements Array<V>, Collection<number, V> {
|
|
|
690
691
|
//
|
|
691
692
|
// ES2023
|
|
692
693
|
//
|
|
693
|
-
with(index: number, value: V): V
|
|
694
|
+
with(index: number, value: V): ArraySchema<V> {
|
|
694
695
|
const copy = this.items.slice();
|
|
695
696
|
copy[index] = value;
|
|
696
697
|
return new ArraySchema(...copy);
|