@colyseus/schema 4.0.0 → 4.0.2

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.
Files changed (241) hide show
  1. package/build/cjs/{index.js → index.cjs} +1 -1
  2. package/build/cjs/index.cjs.map +1 -0
  3. package/build/esm/index.mjs.map +1 -1
  4. package/lib/Metadata.js +54 -58
  5. package/lib/Metadata.js.map +1 -1
  6. package/lib/Reflection.js +29 -32
  7. package/lib/Reflection.js.map +1 -1
  8. package/lib/Schema.js +41 -45
  9. package/lib/Schema.js.map +1 -1
  10. package/lib/annotations.d.ts +1 -1
  11. package/lib/annotations.js +58 -69
  12. package/lib/annotations.js.map +1 -1
  13. package/lib/bench_encode.js +24 -59
  14. package/lib/bench_encode.js.map +1 -1
  15. package/lib/benchmark.d.ts +1 -0
  16. package/lib/benchmark.js +218 -0
  17. package/lib/benchmark.js.map +1 -0
  18. package/lib/codegen/api.js +10 -41
  19. package/lib/codegen/api.js.map +1 -1
  20. package/lib/codegen/argv.js +1 -3
  21. package/lib/codegen/argv.js.map +1 -1
  22. package/lib/codegen/cli.js +4 -9
  23. package/lib/codegen/cli.js.map +1 -1
  24. package/lib/codegen/languages/cpp.js +5 -8
  25. package/lib/codegen/languages/cpp.js.map +1 -1
  26. package/lib/codegen/languages/csharp.js +5 -8
  27. package/lib/codegen/languages/csharp.js.map +1 -1
  28. package/lib/codegen/languages/haxe.js +3 -6
  29. package/lib/codegen/languages/haxe.js.map +1 -1
  30. package/lib/codegen/languages/java.js +3 -6
  31. package/lib/codegen/languages/java.js.map +1 -1
  32. package/lib/codegen/languages/js.js +4 -7
  33. package/lib/codegen/languages/js.js.map +1 -1
  34. package/lib/codegen/languages/lua.js +4 -7
  35. package/lib/codegen/languages/lua.js.map +1 -1
  36. package/lib/codegen/languages/ts.js +5 -8
  37. package/lib/codegen/languages/ts.js.map +1 -1
  38. package/lib/codegen/parser.js +22 -59
  39. package/lib/codegen/parser.js.map +1 -1
  40. package/lib/codegen/types.js +12 -51
  41. package/lib/codegen/types.js.map +1 -1
  42. package/lib/decoder/DecodeOperation.js +44 -51
  43. package/lib/decoder/DecodeOperation.js.map +1 -1
  44. package/lib/decoder/Decoder.js +24 -28
  45. package/lib/decoder/Decoder.js.map +1 -1
  46. package/lib/decoder/ReferenceTracker.js +10 -14
  47. package/lib/decoder/ReferenceTracker.js.map +1 -1
  48. package/lib/decoder/strategy/Callbacks.js +39 -43
  49. package/lib/decoder/strategy/Callbacks.js.map +1 -1
  50. package/lib/decoder/strategy/RawChanges.js +1 -4
  51. package/lib/decoder/strategy/RawChanges.js.map +1 -1
  52. package/lib/decoder/strategy/getDecoderStateCallbacks.js +24 -27
  53. package/lib/decoder/strategy/getDecoderStateCallbacks.js.map +1 -1
  54. package/lib/encoder/ChangeTree.js +36 -44
  55. package/lib/encoder/ChangeTree.js.map +1 -1
  56. package/lib/encoder/EncodeOperation.js +25 -32
  57. package/lib/encoder/EncodeOperation.js.map +1 -1
  58. package/lib/encoder/Encoder.d.ts +5 -5
  59. package/lib/encoder/Encoder.js +25 -29
  60. package/lib/encoder/Encoder.js.map +1 -1
  61. package/lib/encoder/Root.js +17 -21
  62. package/lib/encoder/Root.js.map +1 -1
  63. package/lib/encoder/StateView.js +40 -45
  64. package/lib/encoder/StateView.js.map +1 -1
  65. package/lib/encoding/assert.js +3 -9
  66. package/lib/encoding/assert.js.map +1 -1
  67. package/lib/encoding/decode.js +2 -6
  68. package/lib/encoding/decode.js.map +1 -1
  69. package/lib/encoding/encode.js +1 -4
  70. package/lib/encoding/encode.js.map +1 -1
  71. package/lib/encoding/spec.js +4 -7
  72. package/lib/encoding/spec.js.map +1 -1
  73. package/lib/index.d.ts +2 -2
  74. package/lib/index.js +33 -75
  75. package/lib/index.js.map +1 -1
  76. package/lib/src/Metadata.d.ts +49 -0
  77. package/lib/src/Metadata.js +256 -0
  78. package/lib/src/Metadata.js.map +1 -0
  79. package/lib/src/Reflection.d.ts +71 -0
  80. package/lib/src/Reflection.js +179 -0
  81. package/lib/src/Reflection.js.map +1 -0
  82. package/lib/src/Schema.d.ts +86 -0
  83. package/lib/src/Schema.js +352 -0
  84. package/lib/src/Schema.js.map +1 -0
  85. package/lib/src/annotations.d.ts +109 -0
  86. package/lib/src/annotations.js +473 -0
  87. package/lib/src/annotations.js.map +1 -0
  88. package/lib/src/bench_encode.d.ts +1 -0
  89. package/lib/src/bench_encode.js +91 -0
  90. package/lib/src/bench_encode.js.map +1 -0
  91. package/lib/src/codegen/api.d.ts +7 -0
  92. package/lib/src/codegen/api.js +56 -0
  93. package/lib/src/codegen/api.js.map +1 -0
  94. package/lib/src/codegen/argv.d.ts +6 -0
  95. package/lib/src/codegen/argv.js +39 -0
  96. package/lib/src/codegen/argv.js.map +1 -0
  97. package/lib/src/codegen/cli.d.ts +1 -0
  98. package/lib/src/codegen/cli.js +58 -0
  99. package/lib/src/codegen/cli.js.map +1 -0
  100. package/lib/src/codegen/languages/cpp.d.ts +3 -0
  101. package/lib/src/codegen/languages/cpp.js +258 -0
  102. package/lib/src/codegen/languages/cpp.js.map +1 -0
  103. package/lib/src/codegen/languages/csharp.d.ts +4 -0
  104. package/lib/src/codegen/languages/csharp.js +154 -0
  105. package/lib/src/codegen/languages/csharp.js.map +1 -0
  106. package/lib/src/codegen/languages/haxe.d.ts +3 -0
  107. package/lib/src/codegen/languages/haxe.js +100 -0
  108. package/lib/src/codegen/languages/haxe.js.map +1 -0
  109. package/lib/src/codegen/languages/java.d.ts +6 -0
  110. package/lib/src/codegen/languages/java.js +100 -0
  111. package/lib/src/codegen/languages/java.js.map +1 -0
  112. package/lib/src/codegen/languages/js.d.ts +3 -0
  113. package/lib/src/codegen/languages/js.js +101 -0
  114. package/lib/src/codegen/languages/js.js.map +1 -0
  115. package/lib/src/codegen/languages/lua.d.ts +3 -0
  116. package/lib/src/codegen/languages/lua.js +103 -0
  117. package/lib/src/codegen/languages/lua.js.map +1 -0
  118. package/lib/src/codegen/languages/ts.d.ts +3 -0
  119. package/lib/src/codegen/languages/ts.js +116 -0
  120. package/lib/src/codegen/languages/ts.js.map +1 -0
  121. package/lib/src/codegen/parser.d.ts +13 -0
  122. package/lib/src/codegen/parser.js +327 -0
  123. package/lib/src/codegen/parser.js.map +1 -0
  124. package/lib/src/codegen/types.d.ts +52 -0
  125. package/lib/src/codegen/types.js +142 -0
  126. package/lib/src/codegen/types.js.map +1 -0
  127. package/lib/src/decoder/DecodeOperation.d.ts +23 -0
  128. package/lib/src/decoder/DecodeOperation.js +248 -0
  129. package/lib/src/decoder/DecodeOperation.js.map +1 -0
  130. package/lib/src/decoder/Decoder.d.ts +21 -0
  131. package/lib/src/decoder/Decoder.js +114 -0
  132. package/lib/src/decoder/Decoder.js.map +1 -0
  133. package/lib/src/decoder/ReferenceTracker.d.ts +25 -0
  134. package/lib/src/decoder/ReferenceTracker.js +135 -0
  135. package/lib/src/decoder/ReferenceTracker.js.map +1 -0
  136. package/lib/src/decoder/strategy/Callbacks.d.ts +154 -0
  137. package/lib/src/decoder/strategy/Callbacks.js +336 -0
  138. package/lib/src/decoder/strategy/Callbacks.js.map +1 -0
  139. package/lib/src/decoder/strategy/RawChanges.d.ts +3 -0
  140. package/lib/src/decoder/strategy/RawChanges.js +4 -0
  141. package/lib/src/decoder/strategy/RawChanges.js.map +1 -0
  142. package/lib/src/decoder/strategy/getDecoderStateCallbacks.d.ts +76 -0
  143. package/lib/src/decoder/strategy/getDecoderStateCallbacks.js +274 -0
  144. package/lib/src/decoder/strategy/getDecoderStateCallbacks.js.map +1 -0
  145. package/lib/src/encoder/ChangeTree.d.ts +135 -0
  146. package/lib/src/encoder/ChangeTree.js +534 -0
  147. package/lib/src/encoder/ChangeTree.js.map +1 -0
  148. package/lib/src/encoder/EncodeOperation.d.ts +22 -0
  149. package/lib/src/encoder/EncodeOperation.js +132 -0
  150. package/lib/src/encoder/EncodeOperation.js.map +1 -0
  151. package/lib/src/encoder/Encoder.d.ts +22 -0
  152. package/lib/src/encoder/Encoder.js +204 -0
  153. package/lib/src/encoder/Encoder.js.map +1 -0
  154. package/lib/src/encoder/Root.d.ts +28 -0
  155. package/lib/src/encoder/Root.js +229 -0
  156. package/lib/src/encoder/Root.js.map +1 -0
  157. package/lib/src/encoder/StateView.d.ts +34 -0
  158. package/lib/src/encoder/StateView.js +279 -0
  159. package/lib/src/encoder/StateView.js.map +1 -0
  160. package/lib/src/encoding/assert.d.ts +10 -0
  161. package/lib/src/encoding/assert.js +49 -0
  162. package/lib/src/encoding/assert.js.map +1 -0
  163. package/lib/src/encoding/decode.d.ts +67 -0
  164. package/lib/src/encoding/decode.js +217 -0
  165. package/lib/src/encoding/decode.js.map +1 -0
  166. package/lib/src/encoding/encode.d.ts +40 -0
  167. package/lib/src/encoding/encode.js +279 -0
  168. package/lib/src/encoding/encode.js.map +1 -0
  169. package/lib/src/encoding/spec.d.ts +24 -0
  170. package/lib/src/encoding/spec.js +26 -0
  171. package/lib/src/encoding/spec.js.map +1 -0
  172. package/lib/src/index.d.ts +32 -0
  173. package/lib/src/index.js +39 -0
  174. package/lib/src/index.js.map +1 -0
  175. package/lib/src/symbol.shim.d.ts +6 -0
  176. package/lib/src/symbol.shim.js +3 -0
  177. package/lib/src/symbol.shim.js.map +1 -0
  178. package/lib/src/types/HelperTypes.d.ts +77 -0
  179. package/lib/src/types/HelperTypes.js +2 -0
  180. package/lib/src/types/HelperTypes.js.map +1 -0
  181. package/lib/src/types/TypeContext.d.ts +31 -0
  182. package/lib/src/types/TypeContext.js +143 -0
  183. package/lib/src/types/TypeContext.js.map +1 -0
  184. package/lib/src/types/custom/ArraySchema.d.ts +270 -0
  185. package/lib/src/types/custom/ArraySchema.js +733 -0
  186. package/lib/src/types/custom/ArraySchema.js.map +1 -0
  187. package/lib/src/types/custom/CollectionSchema.d.ts +51 -0
  188. package/lib/src/types/custom/CollectionSchema.js +170 -0
  189. package/lib/src/types/custom/CollectionSchema.js.map +1 -0
  190. package/lib/src/types/custom/MapSchema.d.ts +51 -0
  191. package/lib/src/types/custom/MapSchema.js +222 -0
  192. package/lib/src/types/custom/MapSchema.js.map +1 -0
  193. package/lib/src/types/custom/SetSchema.d.ts +48 -0
  194. package/lib/src/types/custom/SetSchema.js +178 -0
  195. package/lib/src/types/custom/SetSchema.js.map +1 -0
  196. package/lib/src/types/registry.d.ts +16 -0
  197. package/lib/src/types/registry.js +30 -0
  198. package/lib/src/types/registry.js.map +1 -0
  199. package/lib/src/types/symbols.d.ts +33 -0
  200. package/lib/src/types/symbols.js +34 -0
  201. package/lib/src/types/symbols.js.map +1 -0
  202. package/lib/src/types/utils.d.ts +1 -0
  203. package/lib/src/types/utils.js +13 -0
  204. package/lib/src/types/utils.js.map +1 -0
  205. package/lib/src/utils.d.ts +13 -0
  206. package/lib/src/utils.js +49 -0
  207. package/lib/src/utils.js.map +1 -0
  208. package/lib/src/v3_bench.d.ts +1 -0
  209. package/lib/src/v3_bench.js +128 -0
  210. package/lib/src/v3_bench.js.map +1 -0
  211. package/lib/symbol.shim.js +1 -2
  212. package/lib/symbol.shim.js.map +1 -1
  213. package/lib/types/HelperTypes.js +1 -2
  214. package/lib/types/TypeContext.js +8 -12
  215. package/lib/types/TypeContext.js.map +1 -1
  216. package/lib/types/custom/ArraySchema.js +63 -67
  217. package/lib/types/custom/ArraySchema.js.map +1 -1
  218. package/lib/types/custom/CollectionSchema.js +27 -31
  219. package/lib/types/custom/CollectionSchema.js.map +1 -1
  220. package/lib/types/custom/MapSchema.js +37 -41
  221. package/lib/types/custom/MapSchema.js.map +1 -1
  222. package/lib/types/custom/SetSchema.js +28 -32
  223. package/lib/types/custom/SetSchema.js.map +1 -1
  224. package/lib/types/registry.js +13 -20
  225. package/lib/types/registry.js.map +1 -1
  226. package/lib/types/symbols.js +16 -19
  227. package/lib/types/symbols.js.map +1 -1
  228. package/lib/types/utils.js +1 -4
  229. package/lib/types/utils.js.map +1 -1
  230. package/lib/utils.js +9 -14
  231. package/lib/utils.js.map +1 -1
  232. package/lib/v3_bench.js +17 -19
  233. package/lib/v3_bench.js.map +1 -1
  234. package/package.json +9 -7
  235. package/src/codegen/api.ts +7 -0
  236. package/src/codegen/parser.ts +2 -2
  237. package/src/codegen/types.ts +5 -0
  238. package/src/encoder/Encoder.ts +24 -7
  239. package/src/index.ts +2 -2
  240. package/build/cjs/index.js.map +0 -1
  241. package/src/bench_encode.ts +0 -64
@@ -0,0 +1,327 @@
1
+ import * as ts from "typescript";
2
+ import * as path from "path";
3
+ import { readFileSync } from "fs";
4
+ import { Class, Interface, Property, Context, Enum } from "./types";
5
+ let currentStructure;
6
+ let currentProperty;
7
+ let globalContext;
8
+ function defineProperty(property, initializer) {
9
+ if (ts.isIdentifier(initializer)) {
10
+ property.type = "ref";
11
+ property.childType = initializer.text;
12
+ }
13
+ else if (initializer.kind == ts.SyntaxKind.ObjectLiteralExpression) {
14
+ property.type = initializer.properties[0].name.text;
15
+ property.childType = initializer.properties[0].initializer.text;
16
+ }
17
+ else if (initializer.kind == ts.SyntaxKind.ArrayLiteralExpression) {
18
+ property.type = "array";
19
+ property.childType = initializer.elements[0].text;
20
+ }
21
+ else {
22
+ property.type = initializer.text;
23
+ }
24
+ }
25
+ function inspectNode(node, context, decoratorName) {
26
+ switch (node.kind) {
27
+ case ts.SyntaxKind.ImportClause:
28
+ const specifier = node.parent.moduleSpecifier;
29
+ if (specifier && specifier.text.startsWith('.')) {
30
+ const currentDir = path.dirname(node.getSourceFile().fileName);
31
+ const pathToImport = path.resolve(currentDir, specifier.text);
32
+ parseFiles([pathToImport], decoratorName, globalContext);
33
+ }
34
+ break;
35
+ case ts.SyntaxKind.ClassDeclaration:
36
+ currentStructure = new Class();
37
+ const heritageClauses = node.heritageClauses;
38
+ if (heritageClauses && heritageClauses.length > 0) {
39
+ currentStructure.extends = heritageClauses[0].types[0].expression.getText();
40
+ }
41
+ context.addStructure(currentStructure);
42
+ break;
43
+ case ts.SyntaxKind.InterfaceDeclaration:
44
+ //
45
+ // Only generate Interfaces if it has "Message" on its name.
46
+ // Example: MyMessage
47
+ //
48
+ const interfaceName = node.name.escapedText.toString();
49
+ if (interfaceName.indexOf("Message") !== -1) {
50
+ currentStructure = new Interface();
51
+ currentStructure.name = interfaceName;
52
+ context.addStructure(currentStructure);
53
+ }
54
+ break;
55
+ case ts.SyntaxKind.EnumDeclaration:
56
+ const enumName = node.name.escapedText.toString();
57
+ currentStructure = new Enum();
58
+ currentStructure.name = enumName;
59
+ context.addStructure(currentStructure);
60
+ break;
61
+ case ts.SyntaxKind.ExtendsKeyword:
62
+ // console.log(node.getText());
63
+ break;
64
+ case ts.SyntaxKind.PropertySignature:
65
+ if (currentStructure instanceof Interface) {
66
+ const interfaceDeclaration = node.parent;
67
+ if (currentStructure.name !== interfaceDeclaration.name.escapedText.toString()) {
68
+ // skip if property if for a another interface than the one we're interested in.
69
+ break;
70
+ }
71
+ // define a property of an interface
72
+ const property = new Property();
73
+ property.name = node.name.escapedText.toString();
74
+ property.type = node.type.getText();
75
+ currentStructure.addProperty(property);
76
+ }
77
+ break;
78
+ case ts.SyntaxKind.Identifier:
79
+ if (node.getText() === "deprecated" &&
80
+ node.parent.kind !== ts.SyntaxKind.ImportSpecifier) {
81
+ currentProperty = new Property();
82
+ currentProperty.deprecated = true;
83
+ break;
84
+ }
85
+ if (node.getText() === decoratorName) {
86
+ const prop = node.parent?.parent?.parent;
87
+ const propDecorator = getDecorators(prop);
88
+ const hasExpression = prop?.expression?.arguments;
89
+ const hasDecorator = (propDecorator?.length > 0);
90
+ /**
91
+ * neither a `@type()` decorator or `type()` call. skip.
92
+ */
93
+ if (!hasDecorator && !hasExpression) {
94
+ break;
95
+ }
96
+ // using as decorator
97
+ if (propDecorator) {
98
+ /**
99
+ * Calling `@type()` as decorator
100
+ */
101
+ const typeDecorator = propDecorator.find((decorator => {
102
+ return decorator.expression.expression.escapedText === decoratorName;
103
+ })).expression;
104
+ const property = currentProperty || new Property();
105
+ property.name = prop.name.escapedText;
106
+ currentStructure.addProperty(property);
107
+ const typeArgument = typeDecorator.arguments[0];
108
+ defineProperty(property, typeArgument);
109
+ }
110
+ else if (prop.expression.arguments?.[1] &&
111
+ prop.expression.expression.arguments?.[0]) {
112
+ /**
113
+ * Calling `type()` as a regular method
114
+ */
115
+ const property = currentProperty || new Property();
116
+ property.name = prop.expression.arguments[1].text;
117
+ currentStructure.addProperty(property);
118
+ const typeArgument = prop.expression.expression.arguments[0];
119
+ defineProperty(property, typeArgument);
120
+ }
121
+ }
122
+ else if (node.getText() === "setFields" &&
123
+ (node.parent.kind === ts.SyntaxKind.CallExpression ||
124
+ node.parent.kind === ts.SyntaxKind.PropertyAccessExpression)) {
125
+ /**
126
+ * Metadata.setFields(klassName, { ... })
127
+ */
128
+ const callExpression = (node.parent.kind === ts.SyntaxKind.PropertyAccessExpression)
129
+ ? node.parent.parent
130
+ : node.parent;
131
+ /**
132
+ * Skip if @codegen-ignore comment is found before the call expression
133
+ * TODO: currently, if @codegen-ignore is on the file, it will skip all the setFields calls.
134
+ */
135
+ const sourceFile = node.getSourceFile();
136
+ const fullText = sourceFile.getFullText();
137
+ const nodeStart = callExpression.getFullStart();
138
+ const textBeforeNode = fullText.substring(0, nodeStart);
139
+ if (textBeforeNode.includes('@codegen-ignore')) {
140
+ break;
141
+ }
142
+ if (callExpression.kind !== ts.SyntaxKind.CallExpression) {
143
+ break;
144
+ }
145
+ const classNameNode = callExpression.arguments[0];
146
+ const className = ts.isClassExpression(classNameNode)
147
+ ? classNameNode.name?.escapedText.toString()
148
+ : classNameNode.getText();
149
+ // skip if no className is provided
150
+ if (!className) {
151
+ break;
152
+ }
153
+ if (currentStructure?.name !== className) {
154
+ currentStructure = new Class();
155
+ }
156
+ context.addStructure(currentStructure);
157
+ currentStructure.extends = "Schema"; // force extends to Schema
158
+ currentStructure.name = className;
159
+ const types = callExpression.arguments[1];
160
+ for (let i = 0; i < types.properties.length; i++) {
161
+ const prop = types.properties[i];
162
+ const property = currentProperty || new Property();
163
+ property.name = prop.name.escapedText;
164
+ currentStructure.addProperty(property);
165
+ defineProperty(property, prop.initializer);
166
+ }
167
+ }
168
+ else if (node.getText() === "defineTypes" &&
169
+ (node.parent.kind === ts.SyntaxKind.CallExpression ||
170
+ node.parent.kind === ts.SyntaxKind.PropertyAccessExpression)) {
171
+ /**
172
+ * JavaScript source file (`.js`)
173
+ * Using `defineTypes()`
174
+ */
175
+ const callExpression = (node.parent.kind === ts.SyntaxKind.PropertyAccessExpression)
176
+ ? node.parent.parent
177
+ : node.parent;
178
+ if (callExpression.kind !== ts.SyntaxKind.CallExpression) {
179
+ break;
180
+ }
181
+ const className = callExpression.arguments[0].getText();
182
+ currentStructure.name = className;
183
+ const types = callExpression.arguments[1];
184
+ for (let i = 0; i < types.properties.length; i++) {
185
+ const prop = types.properties[i];
186
+ const property = currentProperty || new Property();
187
+ property.name = prop.name.escapedText;
188
+ currentStructure.addProperty(property);
189
+ defineProperty(property, prop.initializer);
190
+ }
191
+ }
192
+ if (node.parent.kind === ts.SyntaxKind.ClassDeclaration) {
193
+ currentStructure.name = node.getText();
194
+ }
195
+ currentProperty = undefined;
196
+ break;
197
+ case ts.SyntaxKind.CallExpression:
198
+ /**
199
+ * Defining schema via `schema.schema({ ... })`
200
+ * - schema.schema({})
201
+ * - schema({})
202
+ * - ClassName.extends({})
203
+ */
204
+ if (((node.expression?.getText() === "schema.schema" ||
205
+ node.expression?.getText() === "schema") ||
206
+ (node.expression?.getText().indexOf(".extends") !== -1)) &&
207
+ node.arguments[0].kind === ts.SyntaxKind.ObjectLiteralExpression) {
208
+ const callExpression = node;
209
+ let className = callExpression.arguments[1]?.getText();
210
+ if (!className && callExpression.parent.kind === ts.SyntaxKind.VariableDeclaration) {
211
+ className = callExpression.parent.name?.getText();
212
+ }
213
+ // skip if no className is provided
214
+ if (!className) {
215
+ break;
216
+ }
217
+ if (currentStructure?.name !== className) {
218
+ currentStructure = new Class();
219
+ context.addStructure(currentStructure);
220
+ }
221
+ if (node.expression?.getText().indexOf(".extends") !== -1) {
222
+ // if it's using `.extends({})`
223
+ const extendsClass = node.expression?.expression?.escapedText;
224
+ // skip if no extendsClass is provided
225
+ if (!extendsClass) {
226
+ break;
227
+ }
228
+ currentStructure.extends = extendsClass;
229
+ }
230
+ else {
231
+ // if it's using `schema({})`
232
+ currentStructure.extends = "Schema"; // force extends to Schema
233
+ }
234
+ currentStructure.name = className;
235
+ const types = callExpression.arguments[0];
236
+ for (let i = 0; i < types.properties.length; i++) {
237
+ const prop = types.properties[i];
238
+ const property = currentProperty || new Property();
239
+ property.name = prop.name.escapedText;
240
+ currentStructure.addProperty(property);
241
+ defineProperty(property, prop.initializer);
242
+ }
243
+ }
244
+ break;
245
+ case ts.SyntaxKind.EnumMember:
246
+ if (currentStructure instanceof Enum) {
247
+ const initializer = node.initializer?.text;
248
+ const name = node.getFirstToken().getText();
249
+ const property = currentProperty || new Property();
250
+ property.name = name;
251
+ if (initializer !== undefined) {
252
+ property.type = initializer;
253
+ }
254
+ currentStructure.addProperty(property);
255
+ currentProperty = undefined;
256
+ }
257
+ break;
258
+ }
259
+ ts.forEachChild(node, (n) => inspectNode(n, context, decoratorName));
260
+ }
261
+ let parsedFiles;
262
+ export function parseFiles(fileNames, decoratorName = "type", context = new Context()) {
263
+ /**
264
+ * Re-set globalContext for each test case
265
+ */
266
+ if (globalContext !== context) {
267
+ parsedFiles = {};
268
+ globalContext = context;
269
+ }
270
+ fileNames.forEach((fileName) => {
271
+ let sourceFile;
272
+ let sourceFileName;
273
+ const fileNameAlternatives = [];
274
+ if (!fileName.endsWith(".ts") &&
275
+ !fileName.endsWith(".js") &&
276
+ !fileName.endsWith(".mjs")) {
277
+ fileNameAlternatives.push(`${fileName}.ts`);
278
+ fileNameAlternatives.push(`${fileName}/index.ts`);
279
+ }
280
+ else {
281
+ fileNameAlternatives.push(fileName);
282
+ }
283
+ for (let i = 0; i < fileNameAlternatives.length; i++) {
284
+ try {
285
+ sourceFileName = path.resolve(fileNameAlternatives[i]);
286
+ if (parsedFiles[sourceFileName]) {
287
+ break;
288
+ }
289
+ sourceFile = ts.createSourceFile(sourceFileName, readFileSync(sourceFileName).toString(), ts.ScriptTarget.Latest, true);
290
+ parsedFiles[sourceFileName] = true;
291
+ break;
292
+ }
293
+ catch (e) {
294
+ // console.log(`${fileNameAlternatives[i]} => ${e.message}`);
295
+ }
296
+ }
297
+ if (sourceFile) {
298
+ inspectNode(sourceFile, context, decoratorName);
299
+ }
300
+ });
301
+ return context.getStructures();
302
+ }
303
+ /**
304
+ * TypeScript 4.8+ has introduced a change on how to access decorators.
305
+ * - https://github.com/microsoft/TypeScript/pull/49089
306
+ * - https://devblogs.microsoft.com/typescript/announcing-typescript-4-8/#decorators-are-placed-on-modifiers-on-typescripts-syntax-trees
307
+ */
308
+ export function getDecorators(node) {
309
+ if (node == undefined) {
310
+ return undefined;
311
+ }
312
+ // TypeScript 4.7 and below
313
+ // @ts-ignore
314
+ if (node.decorators) {
315
+ return node.decorators;
316
+ }
317
+ // TypeScript 4.8 and above
318
+ // @ts-ignore
319
+ if (ts.canHaveDecorators && ts.canHaveDecorators(node)) {
320
+ // @ts-ignore
321
+ const decorators = ts.getDecorators(node);
322
+ return decorators ? Array.from(decorators) : undefined;
323
+ }
324
+ // @ts-ignore
325
+ return node.modifiers?.filter(ts.isDecorator);
326
+ }
327
+ //# sourceMappingURL=parser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parser.js","sourceRoot":"","sources":["../../../src/codegen/parser.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,YAAY,CAAC;AACjC,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAC7B,OAAO,EAAC,YAAY,EAAC,MAAM,IAAI,CAAC;AAChC,OAAO,EAAa,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAC,MAAM,SAAS,CAAC;AAE9E,IAAI,gBAA4B,CAAC;AACjC,IAAI,eAAyB,CAAC;AAE9B,IAAI,aAAsB,CAAC;AAE3B,SAAS,cAAc,CA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AAC,MAAM,EACtB,IAAI,CACP,CAAC;gBAEF,WAAW,CAAC,cAAc,CAAC,GAAG,IAAI,CAAC;gBAEnC,MAAM;YACV,CAAC;YAAC,OAAO,CAAC,EAAE,CAAC;gBACT,6DAA6D;YACjE,CAAC;QACL,CAAC;QAED,IAAI,UAAU,EAAE,CAAC;YACb,WAAW,CAAC,UAAU,EAAE,OAAO,EAAE,aAAa,CAAC,CAAC;QACpD,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,OAAO,OAAO,CAAC,aAAa,EAAE,CAAC;AACnC,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,aAAa,CAAC,IAAgC;IAC1D,IAAI,IAAI,IAAI,SAAS,EAAE,CAAC;QAAC,OAAO,SAAS,CAAC;IAAC,CAAC;IAE5C,2BAA2B;IAC3B,aAAa;IACb,IAAI,IAAI,CAAC,UAAU,EAAE,CAAC;QAAC,OAAO,IAAI,CAAC,UAAU,CAAC;IAAC,CAAC;IAEhD,2BAA2B;IAC3B,aAAa;IACb,IAAI,EAAE,CAAC,iBAAiB,IAAI,EAAE,CAAC,iBAAiB,CAAC,IAAI,CAAC,EAAE,CAAC;QACrD,aAAa;QACb,MAAM,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;IAC3D,CAAC;IAED,aAAa;IACb,OAAO,IAAI,CAAC,SAAS,EAAE,MAAM,CAAC,EAAE,CAAC,WAAW,CAAC,CAAC;AAClD,CAAC","sourcesContent":["import * as ts from \"typescript\";\nimport * as path from \"path\";\nimport {readFileSync} from \"fs\";\nimport {IStructure, Class, Interface, Property, Context, Enum} 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.EnumDeclaration:\n const enumName = (\n node as ts.EnumDeclaration\n ).name.escapedText.toString();\n currentStructure = new Enum();\n currentStructure.name = enumName;\n context.addStructure(currentStructure);\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 const hasDecorator = (propDecorator?.length > 0);\n\n /**\n * neither a `@type()` decorator or `type()` call. skip.\n */\n if (!hasDecorator && !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 } else if (\n node.getText() === \"setFields\" &&\n (\n node.parent.kind === ts.SyntaxKind.CallExpression ||\n node.parent.kind === ts.SyntaxKind.PropertyAccessExpression\n )\n ) {\n /**\n * Metadata.setFields(klassName, { ... })\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 /**\n * Skip if @codegen-ignore comment is found before the call expression\n * TODO: currently, if @codegen-ignore is on the file, it will skip all the setFields calls.\n */\n const sourceFile = node.getSourceFile();\n const fullText = sourceFile.getFullText();\n const nodeStart = callExpression.getFullStart();\n const textBeforeNode = fullText.substring(0, nodeStart);\n if (textBeforeNode.includes('@codegen-ignore')) {\n break;\n }\n\n if (callExpression.kind !== ts.SyntaxKind.CallExpression) {\n break;\n }\n\n const classNameNode = callExpression.arguments[0];\n const className = ts.isClassExpression(classNameNode)\n ? classNameNode.name?.escapedText.toString()\n : classNameNode.getText();\n\n // skip if no className is provided\n if (!className) { break; }\n\n if (currentStructure?.name !== className) {\n currentStructure = new Class();\n }\n context.addStructure(currentStructure);\n (currentStructure as Class).extends = \"Schema\"; // force extends to Schema\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\n currentStructure.addProperty(property);\n defineProperty(property, prop.initializer);\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 case ts.SyntaxKind.CallExpression:\n /**\n * Defining schema via `schema.schema({ ... })`\n * - schema.schema({})\n * - schema({})\n * - ClassName.extends({})\n */\n if (\n (\n (\n (node as ts.CallExpression).expression?.getText() === \"schema.schema\" ||\n (node as ts.CallExpression).expression?.getText() === \"schema\"\n ) ||\n (\n (node as ts.CallExpression).expression?.getText().indexOf(\".extends\") !== -1\n )\n ) &&\n (node as ts.CallExpression).arguments[0].kind === ts.SyntaxKind.ObjectLiteralExpression\n ) {\n const callExpression = node as ts.CallExpression;\n\n let className = callExpression.arguments[1]?.getText();\n\n if (!className && callExpression.parent.kind === ts.SyntaxKind.VariableDeclaration) {\n className = (callExpression.parent as ts.VariableDeclaration).name?.getText();\n }\n\n // skip if no className is provided\n if (!className) { break; }\n\n if (currentStructure?.name !== className) {\n currentStructure = new Class();\n context.addStructure(currentStructure);\n }\n\n if ((node as ts.CallExpression).expression?.getText().indexOf(\".extends\") !== -1) {\n // if it's using `.extends({})`\n const extendsClass = (node as any).expression?.expression?.escapedText;\n\n // skip if no extendsClass is provided\n if (!extendsClass) { break; }\n (currentStructure as Class).extends = extendsClass;\n\n } else {\n // if it's using `schema({})`\n (currentStructure as Class).extends = \"Schema\"; // force extends to Schema\n }\n\n currentStructure.name = className;\n\n const types = callExpression.arguments[0] 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\n currentStructure.addProperty(property);\n defineProperty(property, prop.initializer);\n }\n }\n\n break;\n\n case ts.SyntaxKind.EnumMember:\n if (currentStructure instanceof Enum) {\n const initializer = (node as any).initializer?.text;\n const name = node.getFirstToken().getText();\n const property = currentProperty || new Property();\n property.name = name;\n if (initializer !== undefined) {\n property.type = initializer;\n }\n currentStructure.addProperty(property);\n currentProperty = undefined;\n }\n break;\n }\n\n ts.forEachChild(node, (n: ts.Node) => 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 // @ts-ignore\n if (node.decorators) { return node.decorators; }\n\n // TypeScript 4.8 and above\n // @ts-ignore\n if (ts.canHaveDecorators && ts.canHaveDecorators(node)) {\n // @ts-ignore\n const decorators = ts.getDecorators(node);\n return decorators ? Array.from(decorators) : undefined;\n }\n\n // @ts-ignore\n return node.modifiers?.filter(ts.isDecorator);\n}\n"]}
@@ -0,0 +1,52 @@
1
+ export declare function getCommentHeader(singleLineComment?: string): string;
2
+ export declare class Context {
3
+ classes: Class[];
4
+ interfaces: Interface[];
5
+ enums: Enum[];
6
+ getStructures(): {
7
+ classes: Class[];
8
+ interfaces: Interface[];
9
+ enums: Enum[];
10
+ };
11
+ addStructure(structure: IStructure): void;
12
+ private getParentClass;
13
+ private isSchemaClass;
14
+ }
15
+ export interface IStructure {
16
+ context: Context;
17
+ name: string;
18
+ properties: Property[];
19
+ addProperty(property: Property): void;
20
+ }
21
+ export declare class Interface implements IStructure {
22
+ context: Context;
23
+ name: string;
24
+ properties: Property[];
25
+ addProperty(property: Property): void;
26
+ }
27
+ export declare class Class implements IStructure {
28
+ context: Context;
29
+ name: string;
30
+ properties: Property[];
31
+ extends: string;
32
+ addProperty(property: Property): void;
33
+ postProcessing(): void;
34
+ }
35
+ export declare class Enum implements IStructure {
36
+ context: Context;
37
+ name: string;
38
+ properties: Property[];
39
+ addProperty(property: Property): void;
40
+ }
41
+ export declare class Property {
42
+ index: number;
43
+ name: string;
44
+ type: string;
45
+ childType: string;
46
+ deprecated?: boolean;
47
+ }
48
+ export interface File {
49
+ name: string;
50
+ content: string;
51
+ }
52
+ export declare function getInheritanceTree(klass: Class, allClasses: Class[], includeSelf?: boolean): Class[];
@@ -0,0 +1,142 @@
1
+ import * as fs from "fs";
2
+ import * as path from "path";
3
+ if (typeof (__dirname) === "undefined") {
4
+ global.__dirname = path.dirname(new URL(import.meta.url).pathname);
5
+ }
6
+ const VERSION = JSON.parse(fs.readFileSync(__dirname + "/../../package.json").toString()).version;
7
+ const COMMENT_HEADER = `
8
+ THIS FILE HAS BEEN GENERATED AUTOMATICALLY
9
+ DO NOT CHANGE IT MANUALLY UNLESS YOU KNOW WHAT YOU'RE DOING
10
+
11
+ GENERATED USING @colyseus/schema ${VERSION}
12
+ `;
13
+ export function getCommentHeader(singleLineComment = "//") {
14
+ return `${COMMENT_HEADER.split("\n").map(line => `${singleLineComment} ${line}`).join("\n")}`;
15
+ }
16
+ export class Context {
17
+ constructor() {
18
+ this.classes = [];
19
+ this.interfaces = [];
20
+ this.enums = [];
21
+ }
22
+ getStructures() {
23
+ return {
24
+ classes: this.classes.filter(klass => {
25
+ if (this.isSchemaClass(klass)) {
26
+ return true;
27
+ }
28
+ else {
29
+ let parentClass = klass;
30
+ while (parentClass = this.getParentClass(parentClass)) {
31
+ if (this.isSchemaClass(parentClass)) {
32
+ return true;
33
+ }
34
+ }
35
+ }
36
+ return false;
37
+ }),
38
+ interfaces: this.interfaces,
39
+ enums: this.enums,
40
+ };
41
+ }
42
+ addStructure(structure) {
43
+ if (structure.context === this) {
44
+ return;
45
+ } // skip if already added.
46
+ structure.context = this;
47
+ if (structure instanceof Class) {
48
+ this.classes.push(structure);
49
+ }
50
+ else if (structure instanceof Interface) {
51
+ this.interfaces.push(structure);
52
+ }
53
+ else if (structure instanceof Enum) {
54
+ this.enums.push(structure);
55
+ }
56
+ }
57
+ getParentClass(klass) {
58
+ return this.classes.find(c => c.name === klass.extends);
59
+ }
60
+ isSchemaClass(klass) {
61
+ let isSchema = false;
62
+ let currentClass = klass;
63
+ while (!isSchema && currentClass) {
64
+ //
65
+ // TODO: ideally we should check for actual @colyseus/schema module
66
+ // reference rather than arbitrary strings.
67
+ //
68
+ isSchema = (currentClass.extends === "Schema" ||
69
+ currentClass.extends === "schema.Schema" ||
70
+ currentClass.extends === "Schema.Schema");
71
+ //
72
+ // When extending from `schema.Schema`, it is required to
73
+ // normalize as "Schema" for code generation.
74
+ //
75
+ if (currentClass === klass && isSchema) {
76
+ klass.extends = "Schema";
77
+ }
78
+ currentClass = this.getParentClass(currentClass);
79
+ }
80
+ return isSchema;
81
+ }
82
+ }
83
+ export class Interface {
84
+ constructor() {
85
+ this.properties = [];
86
+ }
87
+ addProperty(property) {
88
+ if (property.type.indexOf("[]") >= 0) {
89
+ // is array!
90
+ property.childType = property.type.match(/([^\[]+)/i)[1];
91
+ property.type = "array";
92
+ this.properties.push(property);
93
+ }
94
+ else {
95
+ this.properties.push(property);
96
+ }
97
+ }
98
+ }
99
+ export class Class {
100
+ constructor() {
101
+ this.properties = [];
102
+ }
103
+ addProperty(property) {
104
+ property.index = this.properties.length;
105
+ this.properties.push(property);
106
+ }
107
+ postProcessing() {
108
+ /**
109
+ * Ensure the proprierties `index` are correct using inheritance
110
+ */
111
+ let parentKlass = this;
112
+ while (parentKlass &&
113
+ (parentKlass = this.context.classes.find(k => k.name === parentKlass.extends))) {
114
+ this.properties.forEach(prop => {
115
+ prop.index += parentKlass.properties.length;
116
+ });
117
+ }
118
+ }
119
+ }
120
+ export class Enum {
121
+ constructor() {
122
+ this.properties = [];
123
+ }
124
+ addProperty(property) {
125
+ this.properties.push(property);
126
+ }
127
+ }
128
+ export class Property {
129
+ }
130
+ export function getInheritanceTree(klass, allClasses, includeSelf = true) {
131
+ let currentClass = klass;
132
+ let inheritanceTree = [];
133
+ if (includeSelf) {
134
+ inheritanceTree.push(currentClass);
135
+ }
136
+ while (currentClass.extends !== "Schema") {
137
+ currentClass = allClasses.find(klass => klass.name == currentClass.extends);
138
+ inheritanceTree.push(currentClass);
139
+ }
140
+ return inheritanceTree;
141
+ }
142
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/codegen/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,IAAI,CAAC;AACzB,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAE7B,IAAI,OAAM,CAAC,SAAS,CAAC,KAAK,WAAW,EAAE,CAAC;IACpC,MAAM,CAAC,SAAS,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,QAAQ,CAAC,CAAC;AACvE,CAAC;AAED,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,YAAY,CAAC,SAAS,GAAG,qBAAqB,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC,OAAO,CAAC;AAClG,MAAM,cAAc,GAAG;;;;mCAIY,OAAO;CACzC,CAAC;AAEF,MAAM,UAAU,gBAAgB,CAAC,oBAA4B,IAAI;IAC7D,OAA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AC;IACzB,IAAI,eAAe,GAAY,EAAE,CAAC;IAElC,IAAI,WAAW,EAAE,CAAC;QACd,eAAe,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;IACvC,CAAC;IAED,OAAO,YAAY,CAAC,OAAO,KAAK,QAAQ,EAAE,CAAC;QACvC,YAAY,GAAG,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,IAAI,IAAI,YAAY,CAAC,OAAO,CAAC,CAAC;QAC5E,eAAe,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;IACvC,CAAC;IAED,OAAO,eAAe,CAAC;AAC3B,CAAC","sourcesContent":["import * as fs from \"fs\";\nimport * as path from \"path\";\n\nif (typeof(__dirname) === \"undefined\") {\n global.__dirname = path.dirname(new URL(import.meta.url).pathname);\n}\n\nconst VERSION = JSON.parse(fs.readFileSync(__dirname + \"/../../package.json\").toString()).version;\nconst COMMENT_HEADER = `\nTHIS FILE HAS BEEN GENERATED AUTOMATICALLY\nDO NOT CHANGE IT MANUALLY UNLESS YOU KNOW WHAT YOU'RE DOING\n\nGENERATED USING @colyseus/schema ${VERSION}\n`;\n\nexport function getCommentHeader(singleLineComment: string = \"//\") {\n return `${COMMENT_HEADER.split(\"\\n\").map(line => `${singleLineComment} ${line}`).join(\"\\n\")}`;\n}\n\nexport class Context {\n classes: Class[] = [];\n interfaces: Interface[] = [];\n enums: Enum[] = [];\n\n getStructures() {\n return {\n classes: this.classes.filter(klass => {\n if (this.isSchemaClass(klass)) {\n return true;\n\n } else {\n let parentClass = klass;\n while (parentClass = this.getParentClass(parentClass)) {\n if (this.isSchemaClass(parentClass)) {\n return true;\n }\n }\n }\n return false;\n }),\n interfaces: this.interfaces,\n enums: this.enums,\n };\n }\n\n addStructure(structure: IStructure) {\n if (structure.context === this) { return; } // skip if already added.\n structure.context = this;\n\n if (structure instanceof Class) {\n this.classes.push(structure);\n } else if (structure instanceof Interface) {\n this.interfaces.push(structure);\n } else if (structure instanceof Enum) {\n this.enums.push(structure);\n }\n }\n\n private getParentClass(klass: Class) {\n return this.classes.find(c => c.name === klass.extends);\n }\n\n private isSchemaClass(klass: Class) {\n let isSchema: boolean = false;\n\n let currentClass = klass;\n while (!isSchema && currentClass) {\n //\n // TODO: ideally we should check for actual @colyseus/schema module\n // reference rather than arbitrary strings.\n //\n isSchema = (\n currentClass.extends === \"Schema\" ||\n currentClass.extends === \"schema.Schema\" ||\n currentClass.extends === \"Schema.Schema\"\n );\n\n //\n // When extending from `schema.Schema`, it is required to\n // normalize as \"Schema\" for code generation.\n //\n if (currentClass === klass && isSchema) {\n klass.extends = \"Schema\";\n }\n\n currentClass = this.getParentClass(currentClass);\n }\n\n return isSchema;\n }\n}\n\nexport interface IStructure {\n context: Context;\n name: string;\n properties: Property[];\n addProperty(property: Property): void;\n}\n\nexport class Interface implements IStructure {\n context: Context;\n name: string;\n properties: Property[] = [];\n\n addProperty(property: Property): void {\n if (property.type.indexOf(\"[]\") >= 0) {\n // is array!\n property.childType = property.type.match(/([^\\[]+)/i)[1];\n property.type = \"array\";\n this.properties.push(property);\n\n } else {\n this.properties.push(property);\n }\n }\n}\n\nexport class Class implements IStructure {\n context: Context;\n name: string;\n properties: Property[] = [];\n extends: string;\n\n addProperty(property: Property) {\n property.index = this.properties.length;\n this.properties.push(property);\n }\n\n postProcessing() {\n /**\n * Ensure the proprierties `index` are correct using inheritance\n */\n let parentKlass: Class = this;\n\n while (\n parentKlass &&\n (parentKlass = this.context.classes.find(k => k.name === parentKlass.extends))\n ) {\n this.properties.forEach(prop => {\n prop.index += parentKlass.properties.length;\n });\n }\n }\n}\n\nexport class Enum implements IStructure {\n context: Context;\n name: string;\n properties: Property[] = [];\n\n addProperty(property: Property) {\n this.properties.push(property);\n }\n}\n\nexport class Property {\n index: number;\n name: string;\n type: string;\n childType: string;\n deprecated?: boolean;\n}\n\nexport interface File {\n name: string\n content: string;\n}\n\nexport function getInheritanceTree(klass: Class, allClasses: Class[], includeSelf: boolean = true) {\n let currentClass = klass;\n let inheritanceTree: Class[] = [];\n\n if (includeSelf) {\n inheritanceTree.push(currentClass);\n }\n\n while (currentClass.extends !== \"Schema\") {\n currentClass = allClasses.find(klass => klass.name == currentClass.extends);\n inheritanceTree.push(currentClass);\n }\n\n return inheritanceTree;\n}\n"]}
@@ -0,0 +1,23 @@
1
+ import { OPERATION } from "../encoding/spec";
2
+ import { Schema } from "../Schema";
3
+ import type { Ref } from "../encoder/ChangeTree";
4
+ import type { Decoder } from "./Decoder";
5
+ import { Iterator } from "../encoding/decode";
6
+ export interface DataChange<T = any, F = string> {
7
+ ref: Ref;
8
+ refId: number;
9
+ op: OPERATION;
10
+ field: F;
11
+ dynamicIndex?: number | string;
12
+ value: T;
13
+ previousValue: T;
14
+ }
15
+ export declare const DEFINITION_MISMATCH = -1;
16
+ export type DecodeOperation<T extends Schema = any> = (decoder: Decoder<T>, bytes: Uint8Array, it: Iterator, ref: Ref, allChanges: DataChange[]) => number | void;
17
+ export declare function decodeValue<T extends Ref>(decoder: Decoder, operation: OPERATION, ref: T, index: number, type: any, bytes: Uint8Array, it: Iterator, allChanges: DataChange[]): {
18
+ value: any;
19
+ previousValue: T;
20
+ };
21
+ export declare const decodeSchemaOperation: DecodeOperation;
22
+ export declare const decodeKeyValueOperation: DecodeOperation;
23
+ export declare const decodeArray: DecodeOperation;