@colyseus/schema 1.0.38 → 1.0.40

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -926,7 +926,8 @@
926
926
  this.$indexes = new Map();
927
927
  this.$refId = 0;
928
928
  if (initialValues) {
929
- if (initialValues instanceof Map) {
929
+ if (initialValues instanceof Map ||
930
+ initialValues instanceof MapSchema) {
930
931
  initialValues.forEach(function (v, k) { return _this.set(k, v); });
931
932
  }
932
933
  else {
@@ -1,5 +1,12 @@
1
+ import * as ts from "typescript";
1
2
  import { Class, Interface, Context } from "./types";
2
3
  export declare function parseFiles(fileNames: string[], decoratorName?: string, context?: Context): {
3
4
  classes: Class[];
4
5
  interfaces: Interface[];
5
6
  };
7
+ /**
8
+ * TypeScript 4.8+ has introduced a change on how to access decorators.
9
+ * - https://github.com/microsoft/TypeScript/pull/49089
10
+ * - https://devblogs.microsoft.com/typescript/announcing-typescript-4-8/#decorators-are-placed-on-modifiers-on-typescripts-syntax-trees
11
+ */
12
+ export declare function getDecorators(node: ts.Node | null | undefined): undefined | ts.Decorator[];
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parseFiles = void 0;
3
+ exports.getDecorators = exports.parseFiles = void 0;
4
4
  var ts = require("typescript");
5
5
  var path = require("path");
6
6
  var fs_1 = require("fs");
@@ -82,7 +82,7 @@ function inspectNode(node, context, decoratorName) {
82
82
  }
83
83
  if (node.getText() === decoratorName) {
84
84
  var prop = (_b = (_a = node.parent) === null || _a === void 0 ? void 0 : _a.parent) === null || _b === void 0 ? void 0 : _b.parent;
85
- var propDecorator = prop === null || prop === void 0 ? void 0 : prop.decorators;
85
+ var propDecorator = getDecorators(prop);
86
86
  var hasExpression = (_c = prop === null || prop === void 0 ? void 0 : prop.expression) === null || _c === void 0 ? void 0 : _c.arguments;
87
87
  /**
88
88
  * neither a `@type()` decorator or `type()` call. skip.
@@ -193,4 +193,26 @@ function parseFiles(fileNames, decoratorName, context) {
193
193
  return context.getStructures();
194
194
  }
195
195
  exports.parseFiles = parseFiles;
196
+ /**
197
+ * TypeScript 4.8+ has introduced a change on how to access decorators.
198
+ * - https://github.com/microsoft/TypeScript/pull/49089
199
+ * - https://devblogs.microsoft.com/typescript/announcing-typescript-4-8/#decorators-are-placed-on-modifiers-on-typescripts-syntax-trees
200
+ */
201
+ function getDecorators(node) {
202
+ var _a;
203
+ if (node == undefined) {
204
+ return undefined;
205
+ }
206
+ // TypeScript 4.7 and below
207
+ if (node.decorators) {
208
+ return node.decorators;
209
+ }
210
+ // TypeScript 4.8 and above
211
+ if (ts.canHaveDecorators(node)) {
212
+ var decorators = ts.getDecorators(node);
213
+ return decorators ? Array.from(decorators) : undefined;
214
+ }
215
+ return (_a = node.modifiers) === null || _a === void 0 ? void 0 : _a.filter(ts.isDecorator);
216
+ }
217
+ exports.getDecorators = getDecorators;
196
218
  //# sourceMappingURL=parser.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"parser.js","sourceRoot":"","sources":["../../src/codegen/parser.ts"],"names":[],"mappings":";;;AAAA,+BAAiC;AACjC,2BAA6B;AAC7B,yBAAkC;AAClC,iCAA0E;AAE1E,IAAI,gBAA4B,CAAC;AACjC,IAAI,eAAyB,CAAC;AAE9B,IAAI,aAAsB,CAAC;AAE3B,SAAS,cAAc,CAAC,QAAkB,EAAE,WAAgB;IACxD,IAAI,EAAE,CAAC,YAAY,CAAC,WAAW,CAAC,EAAE;QAC9B,QAAQ,CAAC,IAAI,GAAG,KAAK,CAAC;QACtB,QAAQ,CAAC,SAAS,GAAG,WAAW,CAAC,IAAI,CAAC;KAEzC;SAAM,IAAI,WAAW,CAAC,IAAI,IAAI,EAAE,CAAC,UAAU,CAAC,uBAAuB,EAAE;QAClE,QAAQ,CAAC,IAAI,GAAG,WAAW,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC;QACpD,QAAQ,CAAC,SAAS,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CAAC,QAAQ,CAAC,KAAK,CAAC;YACzB,CAAC,QAAQ,CAAC,QAAQ,CAAC,MAAM,CAAC,EAC5B;YACE,oBAAoB,CAAC,IAAI,CAAC,UAAG,QAAQ,QAAK,CAAC,CAAC;YAC5C,oBAAoB,CAAC,IAAI,CAAC,UAAG,QAAQ,cAAW,CAAC,CAAC;SAErD;aAAM;YACH,oBAAoB,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;SACvC;QAED,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,oBAAoB,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;YAClD,IAAI;gBACA,cAAc,GAAG,IAAI,CAAC,OAAO,CAAC,oBAAoB,CAAC,CAAC,CAAC,CAAC,CAAC;gBAEvD,IAAI,WAAW,CAAC,cAAc,CAAC,EAAE;oBAC7B,MAAM;iBACT;gBAED,UAAU,GAAG,EAAE,CAAC,gBAAgB,CAC5B,cAAc,EACd,IAAA,iBAAY,EAAC,cAAc,CAAC,CAAC,QAAQ,EAAE,EACvC,EAAE,CAAC,YAAY,CAAC,MAAM,EACtB,IAAI,CACP,CAAC;gBAEF,WAAW,CAAC,cAAc,CAAC,GAAG,IAAI,CAAC;gBAEnC,MAAM;aACT;YAAC,OAAO,CAAC,EAAE;gBACR,6DAA6D;aAChE;SACJ;QAED,IAAI,UAAU,EAAE;YACZ,WAAW,CAAC,UAAU,EAAE,OAAO,EAAE,aAAa,CAAC,CAAC;SACnD;IACL,CAAC,CAAC,CAAC;IAEH,OAAO,OAAO,CAAC,aAAa,EAAE,CAAC;AACnC,CAAC;AA5DD,gCA4DC","sourcesContent":["import * as ts from \"typescript\";\nimport * as path from \"path\";\nimport { readFileSync } from \"fs\";\nimport { IStructure, Class, Interface, Property, Context } from \"./types\";\n\nlet currentStructure: IStructure;\nlet currentProperty: Property;\n\nlet globalContext: Context;\n\nfunction defineProperty(property: Property, initializer: any) {\n if (ts.isIdentifier(initializer)) {\n property.type = \"ref\";\n property.childType = initializer.text;\n\n } else if (initializer.kind == ts.SyntaxKind.ObjectLiteralExpression) {\n property.type = initializer.properties[0].name.text;\n property.childType = initializer.properties[0].initializer.text;\n\n } else if (initializer.kind == ts.SyntaxKind.ArrayLiteralExpression) {\n property.type = \"array\";\n property.childType = initializer.elements[0].text;\n\n } else {\n property.type = initializer.text;\n }\n}\n\nfunction inspectNode(node: ts.Node, context: Context, decoratorName: string) {\n switch (node.kind) {\n case ts.SyntaxKind.ImportClause:\n const specifier = (node.parent as any).moduleSpecifier;\n if (specifier && (specifier.text as string).startsWith('.')) {\n const currentDir = path.dirname(node.getSourceFile().fileName);\n const pathToImport = path.resolve(currentDir, specifier.text);\n parseFiles([pathToImport], decoratorName, globalContext);\n }\n break;\n\n case ts.SyntaxKind.ClassDeclaration:\n currentStructure = new Class();\n\n const heritageClauses = (node as ts.ClassLikeDeclarationBase).heritageClauses;\n if (heritageClauses && heritageClauses.length > 0) {\n (currentStructure as Class).extends = heritageClauses[0].types[0].expression.getText();\n }\n\n context.addStructure(currentStructure);\n break;\n\n case ts.SyntaxKind.InterfaceDeclaration:\n //\n // Only generate Interfaces if it has \"Message\" on its name.\n // Example: MyMessage\n //\n const interfaceName = (node as ts.TypeParameterDeclaration).name.escapedText.toString();\n if (interfaceName.indexOf(\"Message\") !== -1) {\n currentStructure = new Interface();\n currentStructure.name = interfaceName;\n\n context.addStructure(currentStructure);\n }\n break;\n\n case ts.SyntaxKind.ExtendsKeyword:\n // console.log(node.getText());\n break;\n\n case ts.SyntaxKind.PropertySignature:\n if (currentStructure instanceof Interface) {\n const interfaceDeclaration = node.parent;\n\n if (\n currentStructure.name !== (interfaceDeclaration as ts.TypeParameterDeclaration).name.escapedText.toString()\n ) {\n // skip if property if for a another interface than the one we're interested in.\n break;\n }\n\n // define a property of an interface\n const property = new Property();\n property.name = (node as any).name.escapedText.toString();\n property.type = (node as any).type.getText();\n currentStructure.addProperty(property);\n }\n break;\n\n case ts.SyntaxKind.Identifier:\n if (\n node.getText() === \"deprecated\" &&\n node.parent.kind !== ts.SyntaxKind.ImportSpecifier\n ) {\n currentProperty = new Property();\n currentProperty.deprecated = true;\n break;\n }\n\n if (node.getText() === decoratorName) {\n const prop: any = node.parent?.parent?.parent;\n const propDecorator = prop?.decorators;\n const hasExpression = prop?.expression?.arguments;\n\n /**\n * neither a `@type()` decorator or `type()` call. skip.\n */\n if (!propDecorator && !hasExpression) {\n break;\n }\n\n // using as decorator\n if (propDecorator) {\n /**\n * Calling `@type()` as decorator\n */\n const typeDecorator: any = propDecorator.find((decorator => {\n return (decorator.expression as any).expression.escapedText === decoratorName;\n })).expression;\n\n const property = currentProperty || new Property();\n property.name = prop.name.escapedText;\n currentStructure.addProperty(property);\n\n const typeArgument = typeDecorator.arguments[0];\n defineProperty(property, typeArgument);\n\n } else if (\n prop.expression.arguments?.[1] &&\n prop.expression.expression.arguments?.[0]\n ) {\n /**\n * Calling `type()` as a regular method\n */\n const property = currentProperty || new Property();\n property.name = prop.expression.arguments[1].text;\n currentStructure.addProperty(property);\n\n const typeArgument = prop.expression.expression.arguments[0];\n defineProperty(property, typeArgument);\n }\n\n\n } else if (\n node.getText() === \"defineTypes\" &&\n (\n node.parent.kind === ts.SyntaxKind.CallExpression ||\n node.parent.kind === ts.SyntaxKind.PropertyAccessExpression\n )\n ) {\n /**\n * JavaScript source file (`.js`)\n * Using `defineTypes()`\n */\n const callExpression = (node.parent.kind === ts.SyntaxKind.PropertyAccessExpression)\n ? node.parent.parent as ts.CallExpression\n : node.parent as ts.CallExpression;\n\n if (callExpression.kind !== ts.SyntaxKind.CallExpression) {\n break;\n }\n\n const className = callExpression.arguments[0].getText()\n currentStructure.name = className;\n\n const types = callExpression.arguments[1] as any;\n for (let i=0; i<types.properties.length; i++) {\n const prop = types.properties[i];\n\n const property = currentProperty || new Property();\n property.name = prop.name.escapedText;\n currentStructure.addProperty(property);\n\n defineProperty(property, prop.initializer);\n }\n\n }\n\n if (node.parent.kind === ts.SyntaxKind.ClassDeclaration) {\n currentStructure.name = node.getText();\n }\n\n currentProperty = undefined;\n\n break;\n }\n\n ts.forEachChild(node, (n) => inspectNode(n, context, decoratorName));\n}\n\nlet parsedFiles: { [filename: string]: boolean };\n\nexport function parseFiles(\n fileNames: string[],\n decoratorName: string = \"type\",\n context: Context = new Context()\n) {\n /**\n * Re-set globalContext for each test case\n */\n if (globalContext !== context) {\n parsedFiles = {};\n globalContext = context;\n }\n\n fileNames.forEach((fileName) => {\n let sourceFile: ts.Node;\n let sourceFileName: string;\n\n const fileNameAlternatives = [];\n\n if (\n !fileName.endsWith(\".ts\") &&\n !fileName.endsWith(\".js\") &&\n !fileName.endsWith(\".mjs\")\n ) {\n fileNameAlternatives.push(`${fileName}.ts`);\n fileNameAlternatives.push(`${fileName}/index.ts`);\n\n } else {\n fileNameAlternatives.push(fileName);\n }\n\n for (let i = 0; i < fileNameAlternatives.length; i++) {\n try {\n sourceFileName = path.resolve(fileNameAlternatives[i]);\n\n if (parsedFiles[sourceFileName]) {\n break;\n }\n\n sourceFile = ts.createSourceFile(\n sourceFileName,\n readFileSync(sourceFileName).toString(),\n ts.ScriptTarget.Latest,\n true\n );\n\n parsedFiles[sourceFileName] = true;\n\n break;\n } catch (e) {\n // console.log(`${fileNameAlternatives[i]} => ${e.message}`);\n }\n }\n\n if (sourceFile) {\n inspectNode(sourceFile, context, decoratorName);\n }\n });\n\n return context.getStructures();\n}"]}
1
+ {"version":3,"file":"parser.js","sourceRoot":"","sources":["../../src/codegen/parser.ts"],"names":[],"mappings":";;;AAAA,+BAAiC;AACjC,2BAA6B;AAC7B,yBAAkC;AAClC,iCAA0E;AAE1E,IAAI,gBAA4B,CAAC;AACjC,IAAI,eAAyB,CAAC;AAE9B,IAAI,aAAsB,CAAC;AAE3B,SAAS,cAAc,CAAC,QAAkB,EAAE,WAAgB;IACxD,IAAI,EAAE,CAAC,YAAY,CAAC,WAAW,CAAC,EAAE;QAC9B,QAAQ,CAAC,IAAI,GAAG,KAAK,CAAC;QACtB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,gCA4DC;AAED;;;;GAIG;AACH,SAAgB,aAAa,CAAC,IAAgC;;IAC1D,IAAI,IAAI,IAAI,SAAS,EAAE;QAAE,OAAO,SAAS,CAAC;KAAE;IAE5C,2BAA2B;IAC3B,IAAI,IAAI,CAAC,UAAU,EAAE;QAAE,OAAO,IAAI,CAAC,UAAU,CAAC;KAAE;IAEhD,2BAA2B;IAC3B,IAAI,EAAE,CAAC,iBAAiB,CAAC,IAAI,CAAC,EAAE;QAC5B,IAAM,UAAU,GAAG,EAAE,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC;QAC1C,OAAO,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;KAC1D;IAED,OAAO,MAAA,IAAI,CAAC,SAAS,0CAAE,MAAM,CAAC,EAAE,CAAC,WAAW,CAAC,CAAC;AAClD,CAAC;AAbD,sCAaC","sourcesContent":["import * as ts from \"typescript\";\nimport * as path from \"path\";\nimport { readFileSync } from \"fs\";\nimport { IStructure, Class, Interface, Property, Context } from \"./types\";\n\nlet currentStructure: IStructure;\nlet currentProperty: Property;\n\nlet globalContext: Context;\n\nfunction defineProperty(property: Property, initializer: any) {\n if (ts.isIdentifier(initializer)) {\n property.type = \"ref\";\n property.childType = initializer.text;\n\n } else if (initializer.kind == ts.SyntaxKind.ObjectLiteralExpression) {\n property.type = initializer.properties[0].name.text;\n property.childType = initializer.properties[0].initializer.text;\n\n } else if (initializer.kind == ts.SyntaxKind.ArrayLiteralExpression) {\n property.type = \"array\";\n property.childType = initializer.elements[0].text;\n\n } else {\n property.type = initializer.text;\n }\n}\n\nfunction inspectNode(node: ts.Node, context: Context, decoratorName: string) {\n switch (node.kind) {\n case ts.SyntaxKind.ImportClause:\n const specifier = (node.parent as any).moduleSpecifier;\n if (specifier && (specifier.text as string).startsWith('.')) {\n const currentDir = path.dirname(node.getSourceFile().fileName);\n const pathToImport = path.resolve(currentDir, specifier.text);\n parseFiles([pathToImport], decoratorName, globalContext);\n }\n break;\n\n case ts.SyntaxKind.ClassDeclaration:\n currentStructure = new Class();\n\n const heritageClauses = (node as ts.ClassLikeDeclarationBase).heritageClauses;\n if (heritageClauses && heritageClauses.length > 0) {\n (currentStructure as Class).extends = heritageClauses[0].types[0].expression.getText();\n }\n\n context.addStructure(currentStructure);\n break;\n\n case ts.SyntaxKind.InterfaceDeclaration:\n //\n // Only generate Interfaces if it has \"Message\" on its name.\n // Example: MyMessage\n //\n const interfaceName = (node as ts.TypeParameterDeclaration).name.escapedText.toString();\n if (interfaceName.indexOf(\"Message\") !== -1) {\n currentStructure = new Interface();\n currentStructure.name = interfaceName;\n\n context.addStructure(currentStructure);\n }\n break;\n\n case ts.SyntaxKind.ExtendsKeyword:\n // console.log(node.getText());\n break;\n\n case ts.SyntaxKind.PropertySignature:\n if (currentStructure instanceof Interface) {\n const interfaceDeclaration = node.parent;\n\n if (\n currentStructure.name !== (interfaceDeclaration as ts.TypeParameterDeclaration).name.escapedText.toString()\n ) {\n // skip if property if for a another interface than the one we're interested in.\n break;\n }\n\n // define a property of an interface\n const property = new Property();\n property.name = (node as any).name.escapedText.toString();\n property.type = (node as any).type.getText();\n currentStructure.addProperty(property);\n }\n break;\n\n case ts.SyntaxKind.Identifier:\n if (\n node.getText() === \"deprecated\" &&\n node.parent.kind !== ts.SyntaxKind.ImportSpecifier\n ) {\n currentProperty = new Property();\n currentProperty.deprecated = true;\n break;\n }\n\n if (node.getText() === decoratorName) {\n const prop: any = node.parent?.parent?.parent;\n const propDecorator = getDecorators(prop);\n const hasExpression = prop?.expression?.arguments;\n\n /**\n * neither a `@type()` decorator or `type()` call. skip.\n */\n if (!propDecorator && !hasExpression) {\n break;\n }\n\n // using as decorator\n if (propDecorator) {\n /**\n * Calling `@type()` as decorator\n */\n const typeDecorator: any = propDecorator.find((decorator => {\n return (decorator.expression as any).expression.escapedText === decoratorName;\n })).expression;\n\n const property = currentProperty || new Property();\n property.name = prop.name.escapedText;\n currentStructure.addProperty(property);\n\n const typeArgument = typeDecorator.arguments[0];\n defineProperty(property, typeArgument);\n\n } else if (\n prop.expression.arguments?.[1] &&\n prop.expression.expression.arguments?.[0]\n ) {\n /**\n * Calling `type()` as a regular method\n */\n const property = currentProperty || new Property();\n property.name = prop.expression.arguments[1].text;\n currentStructure.addProperty(property);\n\n const typeArgument = prop.expression.expression.arguments[0];\n defineProperty(property, typeArgument);\n }\n\n\n } else if (\n node.getText() === \"defineTypes\" &&\n (\n node.parent.kind === ts.SyntaxKind.CallExpression ||\n node.parent.kind === ts.SyntaxKind.PropertyAccessExpression\n )\n ) {\n /**\n * JavaScript source file (`.js`)\n * Using `defineTypes()`\n */\n const callExpression = (node.parent.kind === ts.SyntaxKind.PropertyAccessExpression)\n ? node.parent.parent as ts.CallExpression\n : node.parent as ts.CallExpression;\n\n if (callExpression.kind !== ts.SyntaxKind.CallExpression) {\n break;\n }\n\n const className = callExpression.arguments[0].getText()\n currentStructure.name = className;\n\n const types = callExpression.arguments[1] as any;\n for (let i=0; i<types.properties.length; i++) {\n const prop = types.properties[i];\n\n const property = currentProperty || new Property();\n property.name = prop.name.escapedText;\n currentStructure.addProperty(property);\n\n defineProperty(property, prop.initializer);\n }\n\n }\n\n if (node.parent.kind === ts.SyntaxKind.ClassDeclaration) {\n currentStructure.name = node.getText();\n }\n\n currentProperty = undefined;\n\n break;\n }\n\n ts.forEachChild(node, (n) => inspectNode(n, context, decoratorName));\n}\n\nlet parsedFiles: { [filename: string]: boolean };\n\nexport function parseFiles(\n fileNames: string[],\n decoratorName: string = \"type\",\n context: Context = new Context()\n) {\n /**\n * Re-set globalContext for each test case\n */\n if (globalContext !== context) {\n parsedFiles = {};\n globalContext = context;\n }\n\n fileNames.forEach((fileName) => {\n let sourceFile: ts.Node;\n let sourceFileName: string;\n\n const fileNameAlternatives = [];\n\n if (\n !fileName.endsWith(\".ts\") &&\n !fileName.endsWith(\".js\") &&\n !fileName.endsWith(\".mjs\")\n ) {\n fileNameAlternatives.push(`${fileName}.ts`);\n fileNameAlternatives.push(`${fileName}/index.ts`);\n\n } else {\n fileNameAlternatives.push(fileName);\n }\n\n for (let i = 0; i < fileNameAlternatives.length; i++) {\n try {\n sourceFileName = path.resolve(fileNameAlternatives[i]);\n\n if (parsedFiles[sourceFileName]) {\n break;\n }\n\n sourceFile = ts.createSourceFile(\n sourceFileName,\n readFileSync(sourceFileName).toString(),\n ts.ScriptTarget.Latest,\n true\n );\n\n parsedFiles[sourceFileName] = true;\n\n break;\n } catch (e) {\n // console.log(`${fileNameAlternatives[i]} => ${e.message}`);\n }\n }\n\n if (sourceFile) {\n inspectNode(sourceFile, context, decoratorName);\n }\n });\n\n return context.getStructures();\n}\n\n/**\n * TypeScript 4.8+ has introduced a change on how to access decorators.\n * - https://github.com/microsoft/TypeScript/pull/49089\n * - https://devblogs.microsoft.com/typescript/announcing-typescript-4-8/#decorators-are-placed-on-modifiers-on-typescripts-syntax-trees\n */\nexport function getDecorators(node: ts.Node | null | undefined,): undefined | ts.Decorator[] {\n if (node == undefined) { return undefined; }\n\n // TypeScript 4.7 and below\n if (node.decorators) { return node.decorators; }\n\n // TypeScript 4.8 and above\n if (ts.canHaveDecorators(node)) {\n const decorators = ts.getDecorators(node);\n return decorators ? Array.from(decorators) : undefined;\n }\n\n return node.modifiers?.filter(ts.isDecorator);\n}"]}
@@ -45,7 +45,8 @@ var MapSchema = /** @class */ (function () {
45
45
  this.$indexes = new Map();
46
46
  this.$refId = 0;
47
47
  if (initialValues) {
48
- if (initialValues instanceof Map) {
48
+ if (initialValues instanceof Map ||
49
+ initialValues instanceof MapSchema) {
49
50
  initialValues.forEach(function (v, k) { return _this.set(k, v); });
50
51
  }
51
52
  else {
@@ -1 +1 @@
1
- {"version":3,"file":"MapSchema.js","sourceRoot":"","sources":["../../src/types/MapSchema.ts"],"names":[],"mappings":";;;AAAA,oCAA2D;AAE3D,oDAAmD;AACnD,gCAAoC;AAEpC,SAAgB,WAAW,CAAC,KAAgB;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AAqB;IACrB,EAAE;IACF,yBAAK,GAAL,UAAM,UAAoB;QACtB,IAAI,MAAiB,CAAC;QAEtB,IAAI,UAAU,EAAE;YACZ,cAAc;YACd,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,SAAS,EAAE,EAAE,IAAI,CAAC,CAAC;SAEjD;aAAM;YACH,cAAc;YACd,MAAM,GAAG,IAAI,SAAS,EAAE,CAAC;YACzB,IAAI,CAAC,OAAO,CAAC,UAAC,KAAK,EAAE,GAAG;gBACpB,IAAI,KAAK,CAAC,UAAU,CAAC,EAAE;oBACnB,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;iBACrC;qBAAM;oBACH,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;iBAC1B;YACL,CAAC,CAAC,CAAA;SACL;QAED,OAAO,MAAM,CAAC;IAClB,CAAC;IAED,8BAAU,GAAV;QACI,eAAM,CAAC,SAAS,CAAC,UAAU,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAC5C,CAAC;IACL,gBAAC;AAAD,CAAC,AA9MD,IA8MC;AA9MY,8BAAS","sourcesContent":["import { Schema, SchemaDecoderCallbacks } from \"../Schema\";\n\nimport { ChangeTree } from \"../changes/ChangeTree\";\nimport { OPERATION } from \"../spec\";\n\nexport function getMapProxy(value: MapSchema) {\n value['$proxy'] = true;\n\n value = new Proxy(value, {\n get: (obj, prop) => {\n if (\n typeof (prop) !== \"symbol\" && // accessing properties\n typeof (obj[prop]) === \"undefined\"\n ) {\n return obj.get(prop as string);\n\n } else {\n return obj[prop];\n }\n },\n\n set: (obj, prop, setValue) => {\n if (\n typeof (prop) !== \"symbol\" &&\n (\n (prop as string).indexOf(\"$\") === -1 &&\n prop !== \"onAdd\" &&\n prop !== \"onRemove\" &&\n prop !== \"onChange\"\n )\n ) {\n obj.set(prop as string, setValue);\n\n } else {\n obj[prop] = setValue;\n }\n return true;\n },\n\n deleteProperty: (obj, prop) => {\n obj.delete(prop as string);\n return true;\n },\n });\n\n return value;\n}\n\nexport class MapSchema<V=any, K extends string = string> implements Map<K, V>, SchemaDecoderCallbacks {\n protected $changes: ChangeTree = new ChangeTree(this);\n\n protected $items: Map<K, V> = new Map<K, V>();\n protected $indexes: Map<number, K> = new Map<number, K>();\n\n protected $refId: number = 0;\n\n //\n // Decoding callbacks\n //\n public onAdd?: (item: V, key: K) => void;\n public onRemove?: (item: V, key: K) => void;\n public onChange?: (item: V, key: K) => void;\n\n static is(type: any) {\n return type['map'] !== undefined;\n }\n\n constructor (initialValues?: Map<K, V> | Record<K, V>) {\n if (initialValues) {\n if (initialValues instanceof Map) {\n initialValues.forEach((v, k) => this.set(k, v));\n\n } else {\n for (const k in initialValues) {\n this.set(k, initialValues[k]);\n }\n }\n }\n }\n\n /** Iterator */\n [Symbol.iterator](): IterableIterator<[K, V]> { return this.$items[Symbol.iterator](); }\n get [Symbol.toStringTag]() { return this.$items[Symbol.toStringTag] }\n\n set(key: K, value: V) {\n if (value === undefined || value === null) {\n throw new Error(`MapSchema#set('${key}', ${value}): trying to set ${value} value on '${key}'.`);\n }\n\n // get \"index\" for this value.\n const hasIndex = typeof(this.$changes.indexes[key]) !== \"undefined\";\n const index = (hasIndex)\n ? this.$changes.indexes[key]\n : this.$refId++;\n\n let operation: OPERATION = (hasIndex)\n ? OPERATION.REPLACE\n : OPERATION.ADD;\n\n const isRef = (value['$changes']) !== undefined;\n if (isRef) {\n (value['$changes'] as ChangeTree).setParent(\n this,\n this.$changes.root,\n index\n );\n }\n\n //\n // (encoding)\n // set a unique id to relate directly with this key/value.\n //\n if (!hasIndex) {\n this.$changes.indexes[key] = index;\n this.$indexes.set(index, key);\n\n } else if (\n isRef && // if is schema, force ADD operation if value differ from previous one.\n this.$items.get(key) !== value\n ) {\n operation = OPERATION.ADD;\n }\n\n this.$items.set(key, value);\n\n this.$changes.change(key, operation);\n\n return this;\n }\n\n get(key: K): V | undefined {\n return this.$items.get(key);\n }\n\n delete(key: K) {\n //\n // TODO: add a \"purge\" method after .encode() runs, to cleanup removed `$indexes`\n //\n // We don't remove $indexes to allow setting the same key in the same patch\n // (See \"should allow to remove and set an item in the same place\" test)\n //\n // // const index = this.$changes.indexes[key];\n // // this.$indexes.delete(index);\n\n this.$changes.delete(key);\n return this.$items.delete(key);\n }\n\n clear(isDecoding?: boolean) {\n // discard previous operations.\n this.$changes.discard(true, true);\n this.$changes.indexes = {};\n\n // clear previous indexes\n this.$indexes.clear();\n\n // flag child items for garbage collection.\n if (isDecoding && typeof (this.$changes.getType()) !== \"string\") {\n this.$items.forEach((item: V) => {\n this.$changes.root.removeRef(item['$changes'].refId);\n });\n }\n\n // clear items\n this.$items.clear();\n\n this.$changes.operation({ index: 0, op: OPERATION.CLEAR });\n\n // touch all structures until reach root\n this.$changes.touchParents();\n }\n\n has (key: K) {\n return this.$items.has(key);\n }\n\n forEach(callbackfn: (value: V, key: K, map: Map<K, V>) => void) {\n this.$items.forEach(callbackfn);\n }\n\n entries () {\n return this.$items.entries();\n }\n\n keys () {\n return this.$items.keys();\n }\n\n values() {\n return this.$items.values();\n }\n\n get size () {\n return this.$items.size;\n }\n\n protected setIndex(index: number, key: K) {\n this.$indexes.set(index, key);\n }\n\n protected getIndex(index: number) {\n return this.$indexes.get(index);\n }\n\n protected getByIndex(index: number) {\n return this.$items.get(this.$indexes.get(index));\n }\n\n protected deleteByIndex(index: number) {\n const key = this.$indexes.get(index);\n this.$items.delete(key);\n this.$indexes.delete(index);\n }\n\n toJSON() {\n const map: any = {};\n\n this.forEach((value, key) => {\n map[key] = (typeof (value['toJSON']) === \"function\")\n ? value['toJSON']()\n : value;\n });\n\n return map;\n }\n\n //\n // Decoding utilities\n //\n clone(isDecoding?: boolean): MapSchema<V> {\n let cloned: MapSchema;\n\n if (isDecoding) {\n // client-side\n cloned = Object.assign(new MapSchema(), this);\n\n } else {\n // server-side\n cloned = new MapSchema();\n this.forEach((value, key) => {\n if (value['$changes']) {\n cloned.set(key, value['clone']());\n } else {\n cloned.set(key, value);\n }\n })\n }\n\n return cloned;\n }\n\n triggerAll (): void {\n Schema.prototype.triggerAll.apply(this);\n }\n}\n"]}
1
+ {"version":3,"file":"MapSchema.js","sourceRoot":"","sources":["../../src/types/MapSchema.ts"],"names":[],"mappings":";;;AAAA,oCAA2D;AAE3D,oDAAmD;AACnD,gCAAoC;AAEpC,SAAgB,WAAW,CAAC,KAAgB;IACxC,KAAK,CAAC,QAAQ,CAAC,GAAG,IAAI,CAAC;IAEvB,KAAK,GAAG,IAAI,KAAK,CAAC,KAAK,EAAE;QACrB,GAAG,EAAE,UAAC,GAAG,EAAE,IAAI;YACX,IACI,OAAO,CAAC,IAAI,CAAC,KAAK,QAAQ,IAAI,uBAAuB;gBACrD,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,KAAK,WAAW,EACpC;gBACE,OAAO,GAAG,CAAC,GAAG,CAAC,IAAc,CAAC,CAAC;aAElC;iBAAM;gBACH,OAAO,GAAG,CAAC,IAAI,CAAC,CAAC;aACpB;QACL,CAAC;QAED,GAAG,EAAE,UAAC,GAAG,EAAE,IAAI,EAAE,QAAQ;YACrB,IACI,OAAO,CAAC,IAAI,CAAC,KAAK,QAAQ;gBAC1B,CACK,IAAe,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;oBACpC,IAAI,KAAK,OAAO;oBAChB,IAAI,KAAK,UAAU;oBACnB,IAAI,KAAK,UAAU,CACtB,EACH;gBACE,GAAG,CAAC,GAAG,CAAC,IAAc,EAAE,QAAQ,CAAC,CAAC;aAErC;iBAAM;gBACH,GAAG,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC;aACxB;YACD,OAAO,IAAI,CAAC;QAChB,CAAC;QAED,cAAc,EAAE,UAAC,GAAG,EAAE,IAAI;YACtB,GAAG,CAAC,MAAM,CAAC,IAAc,CAAC,CAAC;YAC3B,OAAO,IAAI,CAAC;QAChB,CAAC;K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B,IAAI,CAAC,OAAO,CAAC,UAAC,KAAK,EAAE,GAAG;YACpB,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,KAAK,UAAU,CAAC;gBAChD,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAC,EAAE;gBACnB,CAAC,CAAC,KAAK,CAAC;QAChB,CAAC,CAAC,CAAC;QAEH,OAAO,GAAG,CAAC;IACf,CAAC;IAED,EAAE;IACF,qBAAqB;IACrB,EAAE;IACF,yBAAK,GAAL,UAAM,UAAoB;QACtB,IAAI,MAAiB,CAAC;QAEtB,IAAI,UAAU,EAAE;YACZ,cAAc;YACd,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,SAAS,EAAE,EAAE,IAAI,CAAC,CAAC;SAEjD;aAAM;YACH,cAAc;YACd,MAAM,GAAG,IAAI,SAAS,EAAE,CAAC;YACzB,IAAI,CAAC,OAAO,CAAC,UAAC,KAAK,EAAE,GAAG;gBACpB,IAAI,KAAK,CAAC,UAAU,CAAC,EAAE;oBACnB,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;iBACrC;qBAAM;oBACH,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;iBAC1B;YACL,CAAC,CAAC,CAAA;SACL;QAED,OAAO,MAAM,CAAC;IAClB,CAAC;IAED,8BAAU,GAAV;QACI,eAAM,CAAC,SAAS,CAAC,UAAU,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAC5C,CAAC;IACL,gBAAC;AAAD,CAAC,AAjND,IAiNC;AAjNY,8BAAS","sourcesContent":["import { Schema, SchemaDecoderCallbacks } from \"../Schema\";\n\nimport { ChangeTree } from \"../changes/ChangeTree\";\nimport { OPERATION } from \"../spec\";\n\nexport function getMapProxy(value: MapSchema) {\n value['$proxy'] = true;\n\n value = new Proxy(value, {\n get: (obj, prop) => {\n if (\n typeof (prop) !== \"symbol\" && // accessing properties\n typeof (obj[prop]) === \"undefined\"\n ) {\n return obj.get(prop as string);\n\n } else {\n return obj[prop];\n }\n },\n\n set: (obj, prop, setValue) => {\n if (\n typeof (prop) !== \"symbol\" &&\n (\n (prop as string).indexOf(\"$\") === -1 &&\n prop !== \"onAdd\" &&\n prop !== \"onRemove\" &&\n prop !== \"onChange\"\n )\n ) {\n obj.set(prop as string, setValue);\n\n } else {\n obj[prop] = setValue;\n }\n return true;\n },\n\n deleteProperty: (obj, prop) => {\n obj.delete(prop as string);\n return true;\n },\n });\n\n return value;\n}\n\nexport class MapSchema<V=any, K extends string = string> implements Map<K, V>, SchemaDecoderCallbacks {\n protected $changes: ChangeTree = new ChangeTree(this);\n\n protected $items: Map<K, V> = new Map<K, V>();\n protected $indexes: Map<number, K> = new Map<number, K>();\n\n protected $refId: number = 0;\n\n //\n // Decoding callbacks\n //\n public onAdd?: (item: V, key: K) => void;\n public onRemove?: (item: V, key: K) => void;\n public onChange?: (item: V, key: K) => void;\n\n static is(type: any) {\n return type['map'] !== undefined;\n }\n\n constructor (initialValues?: Map<K, V> | Record<K, V>) {\n if (initialValues) {\n if (\n initialValues instanceof Map ||\n initialValues instanceof MapSchema\n ) {\n initialValues.forEach((v, k) => this.set(k, v));\n\n } else {\n for (const k in initialValues) {\n this.set(k, initialValues[k]);\n }\n }\n }\n }\n\n /** Iterator */\n [Symbol.iterator](): IterableIterator<[K, V]> { return this.$items[Symbol.iterator](); }\n get [Symbol.toStringTag]() { return this.$items[Symbol.toStringTag] }\n\n set(key: K, value: V) {\n if (value === undefined || value === null) {\n throw new Error(`MapSchema#set('${key}', ${value}): trying to set ${value} value on '${key}'.`);\n }\n\n // get \"index\" for this value.\n const hasIndex = typeof(this.$changes.indexes[key]) !== \"undefined\";\n const index = (hasIndex)\n ? this.$changes.indexes[key]\n : this.$refId++;\n\n let operation: OPERATION = (hasIndex)\n ? OPERATION.REPLACE\n : OPERATION.ADD;\n\n const isRef = (value['$changes']) !== undefined;\n if (isRef) {\n (value['$changes'] as ChangeTree).setParent(\n this,\n this.$changes.root,\n index\n );\n }\n\n //\n // (encoding)\n // set a unique id to relate directly with this key/value.\n //\n if (!hasIndex) {\n this.$changes.indexes[key] = index;\n this.$indexes.set(index, key);\n\n } else if (\n isRef && // if is schema, force ADD operation if value differ from previous one.\n this.$items.get(key) !== value\n ) {\n operation = OPERATION.ADD;\n }\n\n this.$items.set(key, value);\n\n this.$changes.change(key, operation);\n\n return this;\n }\n\n get(key: K): V | undefined {\n return this.$items.get(key);\n }\n\n delete(key: K) {\n //\n // TODO: add a \"purge\" method after .encode() runs, to cleanup removed `$indexes`\n //\n // We don't remove $indexes to allow setting the same key in the same patch\n // (See \"should allow to remove and set an item in the same place\" test)\n //\n // // const index = this.$changes.indexes[key];\n // // this.$indexes.delete(index);\n\n this.$changes.delete(key);\n return this.$items.delete(key);\n }\n\n clear(isDecoding?: boolean) {\n // discard previous operations.\n this.$changes.discard(true, true);\n this.$changes.indexes = {};\n\n // clear previous indexes\n this.$indexes.clear();\n\n // flag child items for garbage collection.\n if (isDecoding && typeof (this.$changes.getType()) !== \"string\") {\n this.$items.forEach((item: V) => {\n this.$changes.root.removeRef(item['$changes'].refId);\n });\n }\n\n // clear items\n this.$items.clear();\n\n this.$changes.operation({ index: 0, op: OPERATION.CLEAR });\n\n // touch all structures until reach root\n this.$changes.touchParents();\n }\n\n has (key: K) {\n return this.$items.has(key);\n }\n\n forEach(callbackfn: (value: V, key: K, map: Map<K, V>) => void) {\n this.$items.forEach(callbackfn);\n }\n\n entries () {\n return this.$items.entries();\n }\n\n keys () {\n return this.$items.keys();\n }\n\n values() {\n return this.$items.values();\n }\n\n get size () {\n return this.$items.size;\n }\n\n protected setIndex(index: number, key: K) {\n this.$indexes.set(index, key);\n }\n\n protected getIndex(index: number) {\n return this.$indexes.get(index);\n }\n\n protected getByIndex(index: number) {\n return this.$items.get(this.$indexes.get(index));\n }\n\n protected deleteByIndex(index: number) {\n const key = this.$indexes.get(index);\n this.$items.delete(key);\n this.$indexes.delete(index);\n }\n\n toJSON() {\n const map: any = {};\n\n this.forEach((value, key) => {\n map[key] = (typeof (value['toJSON']) === \"function\")\n ? value['toJSON']()\n : value;\n });\n\n return map;\n }\n\n //\n // Decoding utilities\n //\n clone(isDecoding?: boolean): MapSchema<V> {\n let cloned: MapSchema;\n\n if (isDecoding) {\n // client-side\n cloned = Object.assign(new MapSchema(), this);\n\n } else {\n // server-side\n cloned = new MapSchema();\n this.forEach((value, key) => {\n if (value['$changes']) {\n cloned.set(key, value['clone']());\n } else {\n cloned.set(key, value);\n }\n })\n }\n\n return cloned;\n }\n\n triggerAll (): void {\n Schema.prototype.triggerAll.apply(this);\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@colyseus/schema",
3
- "version": "1.0.38",
3
+ "version": "1.0.40",
4
4
  "description": "Binary state serializer with delta encoding for games",
5
5
  "bin": {
6
6
  "schema-codegen": "./bin/schema-codegen"
@@ -8,10 +8,8 @@
8
8
  "scripts": {
9
9
  "build": "tsc && rollup -c rollup.config.js",
10
10
  "watch": "tsc -w",
11
-
12
11
  "test": "mocha --require ts-node/register test/*Test.ts test/**/*Test.ts",
13
12
  "coverage": "nyc mocha --require ts-node/register --require source-map-support/register --recursive test/**Test.ts",
14
-
15
13
  "generate-test-1": "bin/schema-codegen test-external/PrimitiveTypes.ts --namespace SchemaTest.PrimitiveTypes --output ../colyseus-unity3d/Assets/Editor/ColyseusTests/Schema/PrimitiveTypes",
16
14
  "generate-test-2": "bin/schema-codegen test-external/ChildSchemaTypes.ts --namespace SchemaTest.ChildSchemaTypes --output ../colyseus-unity3d/Assets/Editor/ColyseusTests/Schema/ChildSchemaTypes",
17
15
  "generate-test-3": "bin/schema-codegen test-external/ArraySchemaTypes.ts --namespace SchemaTest.ArraySchemaTypes --output ../colyseus-unity3d/Assets/Editor/ColyseusTests/Schema/ArraySchemaTypes",
@@ -23,7 +21,6 @@
23
21
  "generate-test-9": "bin/schema-codegen test-external/InstanceSharingTypes.ts --namespace SchemaTest.InstanceSharing --output ../colyseus-unity3d/Assets/Editor/ColyseusTests/Schema/InstanceSharingTypes",
24
22
  "generate-test-10": "bin/schema-codegen test-external/Callbacks.ts --namespace SchemaTest.Callbacks --output ../colyseus-unity3d/Assets/Editor/ColyseusTests/Schema/Callbacks",
25
23
  "generate-test-11": "bin/schema-codegen test-external/MapSchemaMoveNullifyType.ts --namespace SchemaTest.MapSchemaMoveNullifyType --output ../colyseus-unity3d/Assets/Editor/ColyseusTests/Schema/MapSchemaMoveNullifyType",
26
-
27
24
  "prepublishOnly": "npm run build"
28
25
  },
29
26
  "files": [
@@ -78,7 +75,7 @@
78
75
  "source-map-support": "^0.5.13",
79
76
  "ts-node": "^7.0.1",
80
77
  "tslib": "^2.1.0",
81
- "typescript": "^4.1.3"
78
+ "typescript": "^4.8.4"
82
79
  },
83
80
  "nyc": {
84
81
  "extension": [
@@ -97,7 +97,7 @@ function inspectNode(node: ts.Node, context: Context, decoratorName: string) {
97
97
 
98
98
  if (node.getText() === decoratorName) {
99
99
  const prop: any = node.parent?.parent?.parent;
100
- const propDecorator = prop?.decorators;
100
+ const propDecorator = getDecorators(prop);
101
101
  const hasExpression = prop?.expression?.arguments;
102
102
 
103
103
  /**
@@ -248,4 +248,24 @@ export function parseFiles(
248
248
  });
249
249
 
250
250
  return context.getStructures();
251
+ }
252
+
253
+ /**
254
+ * TypeScript 4.8+ has introduced a change on how to access decorators.
255
+ * - https://github.com/microsoft/TypeScript/pull/49089
256
+ * - https://devblogs.microsoft.com/typescript/announcing-typescript-4-8/#decorators-are-placed-on-modifiers-on-typescripts-syntax-trees
257
+ */
258
+ export function getDecorators(node: ts.Node | null | undefined,): undefined | ts.Decorator[] {
259
+ if (node == undefined) { return undefined; }
260
+
261
+ // TypeScript 4.7 and below
262
+ if (node.decorators) { return node.decorators; }
263
+
264
+ // TypeScript 4.8 and above
265
+ if (ts.canHaveDecorators(node)) {
266
+ const decorators = ts.getDecorators(node);
267
+ return decorators ? Array.from(decorators) : undefined;
268
+ }
269
+
270
+ return node.modifiers?.filter(ts.isDecorator);
251
271
  }
@@ -67,7 +67,10 @@ export class MapSchema<V=any, K extends string = string> implements Map<K, V>, S
67
67
 
68
68
  constructor (initialValues?: Map<K, V> | Record<K, V>) {
69
69
  if (initialValues) {
70
- if (initialValues instanceof Map) {
70
+ if (
71
+ initialValues instanceof Map ||
72
+ initialValues instanceof MapSchema
73
+ ) {
71
74
  initialValues.forEach((v, k) => this.set(k, v));
72
75
 
73
76
  } else {