@colyseus/schema 2.0.22 → 2.0.24

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.
@@ -3251,6 +3251,7 @@
3251
3251
  return _this;
3252
3252
  }
3253
3253
  Reflection.encode = function (instance) {
3254
+ var _a;
3254
3255
  var rootSchemaType = instance.constructor;
3255
3256
  var reflection = new Reflection();
3256
3257
  reflection.rootType = rootSchemaType._typeid;
@@ -3290,7 +3291,7 @@
3290
3291
  }
3291
3292
  reflection.types.push(currentType);
3292
3293
  };
3293
- var types = rootSchemaType._context.types;
3294
+ var types = (_a = rootSchemaType._context) === null || _a === void 0 ? void 0 : _a.types;
3294
3295
  for (var typeid in types) {
3295
3296
  var type_2 = new ReflectionType();
3296
3297
  type_2.id = Number(typeid);
package/lib/Reflection.js CHANGED
@@ -93,7 +93,7 @@ class Reflection extends Schema_1.Schema {
93
93
  }
94
94
  reflection.types.push(currentType);
95
95
  };
96
- const types = rootSchemaType._context.types;
96
+ const types = rootSchemaType._context?.types;
97
97
  for (let typeid in types) {
98
98
  const type = new ReflectionType();
99
99
  type.id = Number(typeid);
@@ -1 +1 @@
1
- {"version":3,"file":"Reflection.js","sourceRoot":"","sources":["../src/Reflection.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,+CAA6E;AAC7E,qCAAkC;AAClC,qDAAkD;AAClD,uDAA+C;AAG/C,MAAM,iBAAiB,GAAG,EAAE,OAAO,EAAE,IAAI,qBAAO,EAAE,EAAE,CAAC;AAErD;;GAEG;AACH,MAAa,eAAgB,SAAQ,eAAM;CAS1C;AATD,0CASC;AAPG;IADC,IAAA,kBAAI,EAAC,QAAQ,EAAE,iBAAiB,CAAC;;6CACrB;AAGb;IADC,IAAA,kBAAI,EAAC,QAAQ,EAAE,iBAAiB,CAAC;;6CACrB;AAGb;IADC,IAAA,kBAAI,EAAC,QAAQ,EAAE,iBAAiB,CAAC;;uDACX;AAG3B,MAAa,cAAe,SAAQ,eAAM;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AAM;oBACH,IAAA,kBAAI,EAAC,KAAK,CAAC,IAAqB,EAAE,EAAE,OAAO,EAAE,CAAC,CAAC,UAAU,CAAC,SAAS,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC;iBACpF;YACL,CAAC,CAAC,CAAC;QACP,CAAC,CAAC,CAAA;QAEF,MAAM,QAAQ,GAAQ,WAAW,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC;QACvD,MAAM,YAAY,GAAG,IAAI,QAAQ,EAAE,CAAC;QAEpC;;;WAGG;QACH,KAAK,IAAI,SAAS,IAAI,QAAQ,CAAC,WAAW,CAAC,MAAM,EAAE;YAC/C,MAAM,SAAS,GAAG,QAAQ,CAAC,WAAW,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;YAEzD,IAAI,OAAM,CAAC,SAAS,CAAC,KAAK,QAAQ,EAAE;gBAChC,YAAY,CAAC,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,SAAS,CAAC,KAAK,UAAU,CAAC;oBACzD,CAAC,CAAC,IAAK,SAAiB,EAAE,CAAC,wBAAwB;oBACnD,CAAC,CAAC,IAAI,CAAC,IAAA,sBAAO,EAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC,CAAC,oBAAoB;aACrF;SACJ;QAED,OAAO,YAAY,CAAC;IACxB,CAAC;CACJ;AAhID,gCAgIC;AA9HG;IADC,IAAA,kBAAI,EAAC,CAAE,cAAc,CAAE,EAAE,iBAAiB,CAAC;8BACrC,yBAAW;yCAAqD;AAGvE;IADC,IAAA,kBAAI,EAAC,QAAQ,EAAE,iBAAiB,CAAC;;4CACjB","sourcesContent":["import { type, PrimitiveType, Context, DefinitionType } from \"./annotations\";\nimport { Schema } from \"./Schema\";\nimport { ArraySchema } from \"./types/ArraySchema\";\nimport { getType } from \"./types/typeRegistry\";\nimport { Iterator } from \"./encoding/decode\";\n\nconst reflectionContext = { context: new Context() };\n\n/**\n * Reflection\n */\nexport class ReflectionField extends Schema {\n @type(\"string\", reflectionContext)\n name: string;\n\n @type(\"string\", reflectionContext)\n type: string;\n\n @type(\"number\", reflectionContext)\n referencedType: number;\n}\n\nexport class ReflectionType extends Schema {\n @type(\"number\", reflectionContext)\n id: number;\n\n @type([ ReflectionField ], reflectionContext)\n fields: ArraySchema<ReflectionField> = new ArraySchema<ReflectionField>();\n}\n\nexport class Reflection extends Schema {\n @type([ ReflectionType ], reflectionContext)\n types: ArraySchema<ReflectionType> = new ArraySchema<ReflectionType>();\n\n @type(\"number\", reflectionContext)\n rootType: number;\n\n static encode (instance: Schema) {\n const rootSchemaType = instance.constructor as typeof Schema;\n\n const reflection = new Reflection();\n reflection.rootType = rootSchemaType._typeid;\n\n const buildType = (currentType: ReflectionType, schema: any) => {\n for (let fieldName in schema) {\n const field = new ReflectionField();\n field.name = fieldName;\n\n let fieldType: string;\n\n if (typeof (schema[fieldName]) === \"string\") {\n fieldType = schema[fieldName];\n\n } else {\n const type = schema[fieldName];\n let childTypeSchema: typeof Schema;\n\n //\n // TODO: refactor below.\n //\n if (Schema.is(type)) {\n fieldType = \"ref\";\n childTypeSchema = schema[fieldName];\n\n } else {\n fieldType = Object.keys(type)[0];\n\n if (typeof(type[fieldType]) === \"string\") {\n fieldType += \":\" + type[fieldType]; // array:string\n\n } else {\n childTypeSchema = type[fieldType];\n }\n }\n\n field.referencedType = (childTypeSchema)\n ? childTypeSchema._typeid\n : -1;\n }\n\n field.type = fieldType;\n currentType.fields.push(field);\n }\n\n reflection.types.push(currentType);\n }\n\n const types = rootSchemaType._context.types;\n for (let typeid in types) {\n const type = new ReflectionType();\n type.id = Number(typeid);\n buildType(type, types[typeid]._definition.schema);\n }\n\n return reflection.encodeAll();\n }\n\n static decode<T extends Schema = Schema>(bytes: number[], it?: Iterator): T {\n const context = new Context();\n\n const reflection = new Reflection();\n reflection.decode(bytes, it);\n\n const schemaTypes = reflection.types.reduce((types, reflectionType) => {\n const schema: typeof Schema = class _ extends Schema {};\n const typeid = reflectionType.id;\n types[typeid] = schema\n context.add(schema, typeid);\n return types;\n }, {});\n\n reflection.types.forEach((reflectionType) => {\n const schemaType = schemaTypes[reflectionType.id];\n\n reflectionType.fields.forEach(field => {\n if (field.referencedType !== undefined) {\n let fieldType = field.type;\n let refType = schemaTypes[field.referencedType];\n\n // map or array of primitive type (-1)\n if (!refType) {\n const typeInfo = field.type.split(\":\");\n fieldType = typeInfo[0];\n refType = typeInfo[1];\n }\n\n if (fieldType === \"ref\") {\n type(refType, { context })(schemaType.prototype, field.name);\n\n } else {\n type({ [fieldType]: refType } as DefinitionType, { context })(schemaType.prototype, field.name);\n }\n\n } else {\n type(field.type as PrimitiveType, { context })(schemaType.prototype, field.name);\n }\n });\n })\n\n const rootType: any = schemaTypes[reflection.rootType];\n const rootInstance = new rootType();\n\n /**\n * auto-initialize referenced types on root type\n * to allow registering listeners immediatelly on client-side\n */\n for (let fieldName in rootType._definition.schema) {\n const fieldType = rootType._definition.schema[fieldName];\n\n if (typeof(fieldType) !== \"string\") {\n rootInstance[fieldName] = (typeof (fieldType) === \"function\")\n ? new (fieldType as any)() // is a schema reference\n : new (getType(Object.keys(fieldType)[0])).constructor(); // is a \"collection\"\n }\n }\n\n return rootInstance;\n }\n}"]}
1
+ {"version":3,"file":"Reflection.js","sourceRoot":"","sources":["../src/Reflection.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,+CAA6E;AAC7E,qCAAkC;AAClC,qDAAkD;AAClD,uDAA+C;AAG/C,MAAM,iBAAiB,GAAG,EAAE,OAAO,EAAE,IAAI,qBAAO,EAAE,EAAE,CAAC;AAErD;;GAEG;AACH,MAAa,eAAgB,SAAQ,eAAM;CAS1C;AATD,0CASC;AAPG;IADC,IAAA,kBAAI,EAAC,QAAQ,EAAE,iBAAiB,CAAC;;6CACrB;AAGb;IADC,IAAA,kBAAI,EAAC,QAAQ,EAAE,iBAAiB,CAAC;;6CACrB;AAGb;IADC,IAAA,kBA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AAC,UAAU,CAAC,SAAS,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC;qBACnG;iBAEJ;qBAAM;oBACH,IAAA,kBAAI,EAAC,KAAK,CAAC,IAAqB,EAAE,EAAE,OAAO,EAAE,CAAC,CAAC,UAAU,CAAC,SAAS,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC;iBACpF;YACL,CAAC,CAAC,CAAC;QACP,CAAC,CAAC,CAAA;QAEF,MAAM,QAAQ,GAAQ,WAAW,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC;QACvD,MAAM,YAAY,GAAG,IAAI,QAAQ,EAAE,CAAC;QAEpC;;;WAGG;QACH,KAAK,IAAI,SAAS,IAAI,QAAQ,CAAC,WAAW,CAAC,MAAM,EAAE;YAC/C,MAAM,SAAS,GAAG,QAAQ,CAAC,WAAW,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;YAEzD,IAAI,OAAM,CAAC,SAAS,CAAC,KAAK,QAAQ,EAAE;gBAChC,YAAY,CAAC,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,SAAS,CAAC,KAAK,UAAU,CAAC;oBACzD,CAAC,CAAC,IAAK,SAAiB,EAAE,CAAC,wBAAwB;oBACnD,CAAC,CAAC,IAAI,CAAC,IAAA,sBAAO,EAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC,CAAC,oBAAoB;aACrF;SACJ;QAED,OAAO,YAAY,CAAC;IACxB,CAAC;CACJ;AAhID,gCAgIC;AA9HG;IADC,IAAA,kBAAI,EAAC,CAAE,cAAc,CAAE,EAAE,iBAAiB,CAAC;8BACrC,yBAAW;yCAAqD;AAGvE;IADC,IAAA,kBAAI,EAAC,QAAQ,EAAE,iBAAiB,CAAC;;4CACjB","sourcesContent":["import { type, PrimitiveType, Context, DefinitionType } from \"./annotations\";\nimport { Schema } from \"./Schema\";\nimport { ArraySchema } from \"./types/ArraySchema\";\nimport { getType } from \"./types/typeRegistry\";\nimport { Iterator } from \"./encoding/decode\";\n\nconst reflectionContext = { context: new Context() };\n\n/**\n * Reflection\n */\nexport class ReflectionField extends Schema {\n @type(\"string\", reflectionContext)\n name: string;\n\n @type(\"string\", reflectionContext)\n type: string;\n\n @type(\"number\", reflectionContext)\n referencedType: number;\n}\n\nexport class ReflectionType extends Schema {\n @type(\"number\", reflectionContext)\n id: number;\n\n @type([ ReflectionField ], reflectionContext)\n fields: ArraySchema<ReflectionField> = new ArraySchema<ReflectionField>();\n}\n\nexport class Reflection extends Schema {\n @type([ ReflectionType ], reflectionContext)\n types: ArraySchema<ReflectionType> = new ArraySchema<ReflectionType>();\n\n @type(\"number\", reflectionContext)\n rootType: number;\n\n static encode (instance: Schema) {\n const rootSchemaType = instance.constructor as typeof Schema;\n\n const reflection = new Reflection();\n reflection.rootType = rootSchemaType._typeid;\n\n const buildType = (currentType: ReflectionType, schema: any) => {\n for (let fieldName in schema) {\n const field = new ReflectionField();\n field.name = fieldName;\n\n let fieldType: string;\n\n if (typeof (schema[fieldName]) === \"string\") {\n fieldType = schema[fieldName];\n\n } else {\n const type = schema[fieldName];\n let childTypeSchema: typeof Schema;\n\n //\n // TODO: refactor below.\n //\n if (Schema.is(type)) {\n fieldType = \"ref\";\n childTypeSchema = schema[fieldName];\n\n } else {\n fieldType = Object.keys(type)[0];\n\n if (typeof(type[fieldType]) === \"string\") {\n fieldType += \":\" + type[fieldType]; // array:string\n\n } else {\n childTypeSchema = type[fieldType];\n }\n }\n\n field.referencedType = (childTypeSchema)\n ? childTypeSchema._typeid\n : -1;\n }\n\n field.type = fieldType;\n currentType.fields.push(field);\n }\n\n reflection.types.push(currentType);\n }\n\n const types = rootSchemaType._context?.types;\n for (let typeid in types) {\n const type = new ReflectionType();\n type.id = Number(typeid);\n buildType(type, types[typeid]._definition.schema);\n }\n\n return reflection.encodeAll();\n }\n\n static decode<T extends Schema = Schema>(bytes: number[], it?: Iterator): T {\n const context = new Context();\n\n const reflection = new Reflection();\n reflection.decode(bytes, it);\n\n const schemaTypes = reflection.types.reduce((types, reflectionType) => {\n const schema: typeof Schema = class _ extends Schema {};\n const typeid = reflectionType.id;\n types[typeid] = schema\n context.add(schema, typeid);\n return types;\n }, {});\n\n reflection.types.forEach((reflectionType) => {\n const schemaType = schemaTypes[reflectionType.id];\n\n reflectionType.fields.forEach(field => {\n if (field.referencedType !== undefined) {\n let fieldType = field.type;\n let refType = schemaTypes[field.referencedType];\n\n // map or array of primitive type (-1)\n if (!refType) {\n const typeInfo = field.type.split(\":\");\n fieldType = typeInfo[0];\n refType = typeInfo[1];\n }\n\n if (fieldType === \"ref\") {\n type(refType, { context })(schemaType.prototype, field.name);\n\n } else {\n type({ [fieldType]: refType } as DefinitionType, { context })(schemaType.prototype, field.name);\n }\n\n } else {\n type(field.type as PrimitiveType, { context })(schemaType.prototype, field.name);\n }\n });\n })\n\n const rootType: any = schemaTypes[reflection.rootType];\n const rootInstance = new rootType();\n\n /**\n * auto-initialize referenced types on root type\n * to allow registering listeners immediatelly on client-side\n */\n for (let fieldName in rootType._definition.schema) {\n const fieldType = rootType._definition.schema[fieldName];\n\n if (typeof(fieldType) !== \"string\") {\n rootInstance[fieldName] = (typeof (fieldType) === \"function\")\n ? new (fieldType as any)() // is a schema reference\n : new (getType(Object.keys(fieldType)[0])).constructor(); // is a \"collection\"\n }\n }\n\n return rootInstance;\n }\n}"]}
@@ -7,5 +7,5 @@ export type NonFunctionPropNames<T> = {
7
7
  [K in keyof T]: T[K] extends Function ? never : K;
8
8
  }[keyof T];
9
9
  export type ToJSON<T> = NonFunctionProps<{
10
- [K in keyof T]: T[K] extends MapSchema<infer U> ? Record<string, U> : T[K] extends ArraySchema<infer U> ? U[] : T[K];
10
+ [K in keyof T]: T[K] extends MapSchema<infer U> ? Record<string, U> : T[K] extends Map<string, infer U> ? Record<string, U> : T[K] extends ArraySchema<infer U> ? U[] : T[K];
11
11
  }>;
@@ -1 +1 @@
1
- {"version":3,"file":"HelperTypes.js","sourceRoot":"","sources":["../../src/types/HelperTypes.ts"],"names":[],"mappings":"","sourcesContent":["import { ArraySchema } from \"./ArraySchema\";\nimport { MapSchema } from \"./MapSchema\";\n\nexport type NonFunctionProps<T> = Omit<T, {\n [K in keyof T]: T[K] extends Function ? K : never;\n}[keyof T]>;\n\nexport type NonFunctionPropNames<T> = {\n [K in keyof T]: T[K] extends Function ? never : K\n}[keyof T];\n\nexport type ToJSON<T> = NonFunctionProps<{\n [K in keyof T]: T[K] extends MapSchema<infer U>\n ? Record<string, U>\n : T[K] extends ArraySchema<infer U>\n ? U[]\n : T[K]\n}>;"]}
1
+ {"version":3,"file":"HelperTypes.js","sourceRoot":"","sources":["../../src/types/HelperTypes.ts"],"names":[],"mappings":"","sourcesContent":["import { ArraySchema } from \"./ArraySchema\";\nimport { MapSchema } from \"./MapSchema\";\n\nexport type NonFunctionProps<T> = Omit<T, {\n [K in keyof T]: T[K] extends Function ? K : never;\n}[keyof T]>;\n\nexport type NonFunctionPropNames<T> = {\n [K in keyof T]: T[K] extends Function ? never : K\n}[keyof T];\n\nexport type ToJSON<T> = NonFunctionProps<{\n [K in keyof T]: T[K] extends MapSchema<infer U>\n ? Record<string, U>\n : T[K] extends Map<string, infer U>\n ? Record<string, U>\n : T[K] extends ArraySchema<infer U>\n ? U[]\n : T[K]\n}>;"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@colyseus/schema",
3
- "version": "2.0.22",
3
+ "version": "2.0.24",
4
4
  "description": "Binary state serializer with delta encoding for games",
5
5
  "bin": {
6
6
  "schema-codegen": "./bin/schema-codegen"
package/src/Reflection.ts CHANGED
@@ -85,7 +85,7 @@ export class Reflection extends Schema {
85
85
  reflection.types.push(currentType);
86
86
  }
87
87
 
88
- const types = rootSchemaType._context.types;
88
+ const types = rootSchemaType._context?.types;
89
89
  for (let typeid in types) {
90
90
  const type = new ReflectionType();
91
91
  type.id = Number(typeid);
@@ -12,7 +12,9 @@ export type NonFunctionPropNames<T> = {
12
12
  export type ToJSON<T> = NonFunctionProps<{
13
13
  [K in keyof T]: T[K] extends MapSchema<infer U>
14
14
  ? Record<string, U>
15
- : T[K] extends ArraySchema<infer U>
16
- ? U[]
17
- : T[K]
15
+ : T[K] extends Map<string, infer U>
16
+ ? Record<string, U>
17
+ : T[K] extends ArraySchema<infer U>
18
+ ? U[]
19
+ : T[K]
18
20
  }>;