tlobject.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  1. const fs = require('fs');
  2. const util = require('util');
  3. const {crc32} = require('crc');
  4. const SourceBuilder = require('../sourcebuilder');
  5. const {snakeToCamelCase, variableSnakeToCamelCase} = require("../utils");
  6. const AUTO_GEN_NOTICE =
  7. "/*! File generated by TLObjects' generator. All changes will be ERASED !*/";
  8. const AUTO_CASTS = {
  9. InputPeer: 'utils.get_input_peer(await client.get_input_entity(%s))',
  10. InputChannel: 'utils.get_input_channel(await client.get_input_entity(%s))',
  11. InputUser: 'utils.get_input_user(await client.get_input_entity(%s))',
  12. InputDialogPeer: 'await client._get_input_dialog(%s)',
  13. InputNotifyPeer: 'await client._get_input_notify(%s)',
  14. InputMedia: 'utils.get_input_media(%s)',
  15. InputPhoto: 'utils.get_input_photo(%s)',
  16. InputMessage: 'utils.get_input_message(%s)',
  17. InputDocument: 'utils.get_input_document(%s)',
  18. InputChatPhoto: 'utils.get_input_chat_photo(%s)',
  19. };
  20. const NAMED_AUTO_CASTS = {
  21. 'chat_id,int': 'await client.get_peer_id(%s, add_mark=False)',
  22. };
  23. // Secret chats have a chat_id which may be negative.
  24. // With the named auto-cast above, we would break it.
  25. // However there are plenty of other legit requests
  26. // with `chat_id:int` where it is useful.
  27. //
  28. // NOTE: This works because the auto-cast is not recursive.
  29. // There are plenty of types that would break if we
  30. // did recurse into them to resolve them.
  31. const NAMED_BLACKLIST = new Set(['messages.discardEncryption']);
  32. const BASE_TYPES = [
  33. 'string',
  34. 'bytes',
  35. 'int',
  36. 'long',
  37. 'int128',
  38. 'int256',
  39. 'double',
  40. 'Bool',
  41. 'true',
  42. ];
  43. // Patched types {fullname: custom.ns.Name}
  44. const PATCHED_TYPES = {
  45. messageEmpty: 'message.Message',
  46. message: 'message.Message',
  47. messageService: 'message.Message',
  48. };
  49. const writeModules = (
  50. outDir,
  51. depth,
  52. kind,
  53. namespaceTlobjects,
  54. typeConstructors
  55. ) => {
  56. // namespace_tlobjects: {'namespace', [TLObject]}
  57. fs.mkdirSync(outDir, {recursive: true});
  58. for (const [ns, tlobjects] of Object.entries(namespaceTlobjects)) {
  59. const file = `${outDir}/${ns === 'null' ? 'index' : ns}.js`;
  60. const stream = fs.createWriteStream(file);
  61. const builder = new SourceBuilder(stream);
  62. const dotDepth = '.'.repeat(depth || 1);
  63. builder.writeln(AUTO_GEN_NOTICE);
  64. builder.writeln(
  65. `const { TLObject } = require('${dotDepth}/tlobject');`
  66. );
  67. if (kind !== 'TLObject') {
  68. builder.writeln(
  69. `const { ${kind} } = require('${dotDepth}/tlobject');`
  70. );
  71. }
  72. // Add the relative imports to the namespaces,
  73. // unless we already are in a namespace.
  74. if (!ns) {
  75. const imports = Object.keys(namespaceTlobjects)
  76. .filter(Boolean)
  77. .join(`, `);
  78. builder.writeln(`const { ${imports} } = require('.');`);
  79. }
  80. // Import struct for the .__bytes__(self) serialization
  81. builder.writeln("const struct = require('python-struct');");
  82. builder.writeln(`const Helpers = require('../../utils/Helpers');`);
  83. builder.writeln(`const BigIntBuffer = require("bigint-buffer");`);
  84. const typeNames = new Set();
  85. const typeDefs = [];
  86. /*
  87. // Find all the types in this file and generate type definitions
  88. // based on the types. The type definitions are written to the
  89. // file at the end.
  90. for (const t of tlobjects) {
  91. if (!t.isFunction) {
  92. let typeName = t.result;
  93. if (typeName.includes('.')) {
  94. typeName = typeName.slice(typeName.lastIndexOf('.'));
  95. }
  96. if (typeNames.has(typeName)) {
  97. continue;
  98. }
  99. typeNames.add(typeName);
  100. const constructors = typeConstructors[typeName];
  101. if (!constructors) {
  102. } else if (constructors.length === 1) {
  103. typeDefs.push(
  104. `Type${typeName} = ${constructors[0].className}`
  105. );
  106. } else {
  107. typeDefs.push(
  108. `Type${typeName} = Union[${constructors
  109. .map(x => constructors.className)
  110. .join(',')}]`
  111. );
  112. }
  113. }
  114. }*/
  115. const imports = {};
  116. const primitives = new Set([
  117. 'int',
  118. 'long',
  119. 'int128',
  120. 'int256',
  121. 'double',
  122. 'string',
  123. 'bytes',
  124. 'Bool',
  125. 'true',
  126. ]);
  127. // Find all the types in other files that are used in this file
  128. // and generate the information required to import those types.
  129. for (const t of tlobjects) {
  130. for (const arg of t.args) {
  131. let name = arg.type;
  132. if (!name || primitives.has(name)) {
  133. continue;
  134. }
  135. let importSpace = `${dotDepth}/tl/types`;
  136. if (name.includes('.')) {
  137. const [namespace] = name.split('.');
  138. name = name.split('.');
  139. importSpace += `/${namespace}`;
  140. }
  141. if (!typeNames.has(name)) {
  142. typeNames.add(name);
  143. if (name === 'date') {
  144. imports.datetime = ['datetime'];
  145. continue;
  146. } else if (!(importSpace in imports)) {
  147. imports[importSpace] = new Set();
  148. }
  149. imports[importSpace].add(`Type${name}`);
  150. }
  151. }
  152. }
  153. // Add imports required for type checking
  154. if (imports) {
  155. builder.writeln('if (false) { // TYPE_CHECKING {');
  156. for (const [namespace, names] of Object.entries(imports)) {
  157. builder.writeln(
  158. `const { ${[...names.values()].join(
  159. ', '
  160. )} } = require('${namespace}');`
  161. );
  162. }
  163. builder.endBlock();
  164. }
  165. // Generate the class for every TLObject
  166. for (const t of tlobjects) {
  167. if (t.fullname in PATCHED_TYPES) {
  168. builder.writeln(`const ${t.className} = null; // Patched`);
  169. } else {
  170. writeSourceCode(t, kind, builder, typeConstructors);
  171. builder.currentIndent = 0;
  172. }
  173. }
  174. // Write the type definitions generated earlier.
  175. builder.writeln();
  176. for (const line of typeDefs) {
  177. builder.writeln(line);
  178. }
  179. writeModuleExports(tlobjects, builder);
  180. }
  181. };
  182. const writeReadResult = (tlobject, builder) => {
  183. // Only requests can have a different response that's not their
  184. // serialized body, that is, we'll be setting their .result.
  185. //
  186. // The default behaviour is reading a TLObject too, so no need
  187. // to override it unless necessary.
  188. if (!tlobject.isFunction)
  189. return;
  190. // https://core.telegram.org/mtproto/serialize#boxed-and-bare-types
  191. // TL;DR; boxed types start with uppercase always, so we can use
  192. // this to check whether everything in it is boxed or not.
  193. //
  194. // Currently only un-boxed responses are Vector<int>/Vector<long>.
  195. // If this weren't the case, we should check upper case after
  196. // max(index('<'), index('.')) (and if it is, it's boxed, so return).
  197. let m = tlobject.result.match(/Vector<(int|long)>/);
  198. if (!m) {
  199. return
  200. }
  201. builder.endBlock();
  202. builder.writeln('static readResult(reader){');
  203. builder.writeln('reader.readInt() // Vector ID');
  204. builder.writeln('let temp = [];');
  205. builder.writeln('for (let i=0;i<reader.readInt();i++){');
  206. builder.writeln('temp.push(reader.read%s', m[0]);
  207. builder.writeln("}");
  208. builder.writeln('return temp');
  209. };
  210. /**
  211. * Writes the source code corresponding to the given TLObject
  212. * by making use of the ``builder`` `SourceBuilder`.
  213. *
  214. * Additional information such as file path depth and
  215. * the ``Type: [Constructors]`` must be given for proper
  216. * importing and documentation strings.
  217. */
  218. const writeSourceCode = (tlobject, kind, builder, typeConstructors) => {
  219. writeClassConstructor(tlobject, kind, typeConstructors, builder);
  220. writeResolve(tlobject, builder);
  221. //writeToJson(tlobject, builder);
  222. writeToBytes(tlobject, builder);
  223. builder.currentIndent--;
  224. writeFromReader(tlobject, builder);
  225. writeReadResult(tlobject, builder);
  226. builder.writeln('}');
  227. };
  228. const writeClassConstructor = (tlobject, kind, typeConstructors, builder) => {
  229. builder.writeln();
  230. builder.writeln();
  231. builder.writeln(`class ${tlobject.className} extends ${kind} {`);
  232. // Convert the args to string parameters, flags having =None
  233. const args = tlobject.realArgs.map(
  234. a =>
  235. `${a.name}: ${a.typeHint()}${
  236. a.isFlag || a.canBeInferred ? `=None` : ''
  237. }`
  238. );
  239. // Write the __init__ function if it has any argument
  240. if (!tlobject.realArgs.length) {
  241. return;
  242. }
  243. builder.writeln('/**');
  244. if (tlobject.isFunction) {
  245. builder.write(`:returns ${tlobject.result}: `);
  246. } else {
  247. builder.write(`Constructor for ${tlobject.result}: `);
  248. }
  249. const constructors = typeConstructors[tlobject.result];
  250. if (!constructors) {
  251. builder.writeln('This type has no constructors.');
  252. } else if (constructors.length === 1) {
  253. builder.writeln(`Instance of ${constructors[0].className}`);
  254. } else {
  255. builder.writeln(
  256. `Instance of either ${constructors
  257. .map(c => c.className)
  258. .join(', ')}`
  259. );
  260. }
  261. builder.writeln('*/');
  262. builder.writeln(`constructor(args) {`);
  263. builder.writeln(`super();`);
  264. // Class-level variable to store its Telegram's constructor ID
  265. builder.writeln(
  266. `this.CONSTRUCTOR_ID = 0x${tlobject.id.toString(16).padStart(8, '0')};`
  267. );
  268. builder.writeln(`this.SUBCLASS_OF_ID = 0x${crc32(tlobject.result)};`);
  269. builder.writeln();
  270. // Set the arguments
  271. for (const arg of tlobject.realArgs) {
  272. if (!arg.canBeInferred) {
  273. builder.writeln(`this.${variableSnakeToCamelCase(arg.name)} = args.${variableSnakeToCamelCase(arg.name)};`);
  274. }
  275. // Currently the only argument that can be
  276. // inferred are those called 'random_id'
  277. else if (arg.name === 'random_id') {
  278. // Endianness doesn't really matter, and 'big' is shorter
  279. let code = `int.from_bytes(Helpers.generateRandomBytes(${
  280. arg.type === 'long' ? 8 : 4
  281. }))`;
  282. if (arg.isVector) {
  283. // Currently for the case of "messages.forwardMessages"
  284. // Ensure we can infer the length from id:Vector<>
  285. if (!tlobject.realArgs.find(a => a.name === 'id').isVector) {
  286. throw new Error(
  287. `Cannot infer list of random ids for ${tlobject}`
  288. );
  289. }
  290. code = `new Array(id.length).fill().map(_ => ${code})`;
  291. }
  292. builder.writeln(
  293. `this.randomId = randomId !== null ? randomId : ${code}`
  294. );
  295. } else {
  296. throw new Error(`Cannot infer a value for ${arg}`);
  297. }
  298. }
  299. builder.endBlock();
  300. };
  301. const writeResolve = (tlobject, builder) => {
  302. if (
  303. tlobject.isFunction &&
  304. tlobject.realArgs.some(
  305. arg =>
  306. arg.type in AUTO_CASTS ||
  307. (`${arg.name},${arg.type}` in NAMED_AUTO_CASTS &&
  308. !NAMED_BLACKLIST.has(tlobject.fullname))
  309. )
  310. ) {
  311. builder.writeln('async resolve(client, utils) {');
  312. for (const arg of tlobject.realArgs) {
  313. let ac = AUTO_CASTS[arg.type];
  314. if (!ac) {
  315. ac = NAMED_AUTO_CASTS[`${arg.name},${arg.type}`];
  316. if (!ac) {
  317. continue;
  318. }
  319. }
  320. if (arg.isFlag) {
  321. builder.writeln(`if (this.${arg.name}) {`);
  322. }
  323. if (arg.isVector) {
  324. builder.write(`const _tmp = [];`);
  325. builder.writeln(`for (const _x of this.${arg.name}) {`);
  326. builder.writeln(`_tmp.push(%s);`, util.format(ac, '_x'));
  327. builder.endBlock();
  328. builder.writeln(`this.${arg.name} = _tmp;`);
  329. } else {
  330. builder.writeln(
  331. `this.${arg.name} = %s`,
  332. util.format(ac, `this.${arg.name}`)
  333. );
  334. }
  335. if (arg.isFlag) {
  336. builder.currentIndent--;
  337. builder.writeln('}');
  338. }
  339. }
  340. builder.endBlock();
  341. }
  342. };
  343. /**
  344. const writeToJson = (tlobject, builder) => {
  345. builder.writeln('toJson() {');
  346. builder.writeln('return {');
  347. builder.write("_: '%s'", tlobject.className);
  348. for (const arg of tlobject.realArgs) {
  349. builder.writeln(',');
  350. builder.write('%s: ', arg.name);
  351. if (BASE_TYPES.includes(arg.type)) {
  352. if (arg.isVector) {
  353. builder.write(
  354. 'this.%s === null ? [] : this.%s.slice()',
  355. arg.name,
  356. arg.name
  357. );
  358. } else {
  359. builder.write('this.%s', arg.name);
  360. }
  361. } else {
  362. if (arg.isVector) {
  363. builder.write(
  364. 'this.%s === null ? [] : this.%s.map(x => x instanceof TLObject ? x.toJson() : x)',
  365. arg.name,
  366. arg.name
  367. );
  368. } else {
  369. builder.write(
  370. 'this.%s instanceof TLObject ? this.%s.toJson() : this.%s',
  371. arg.name,
  372. arg.name,
  373. arg.name
  374. );
  375. }
  376. }
  377. }
  378. builder.writeln();
  379. builder.endBlock();
  380. builder.currentIndent--;
  381. builder.writeln('}');
  382. };
  383. */
  384. const writeToBytes = (tlobject, builder) => {
  385. builder.writeln('get bytes() {');
  386. // Some objects require more than one flag parameter to be set
  387. // at the same time. In this case, add an assertion.
  388. const repeatedArgs = {};
  389. for (let arg of tlobject.args) {
  390. if (arg.isFlag) {
  391. if (!repeatedArgs[arg.flagIndex]) {
  392. repeatedArgs[arg.flagIndex] = [];
  393. }
  394. repeatedArgs[arg.flagIndex].push(arg);
  395. }
  396. }
  397. for (let ra of Object.values(repeatedArgs)) {
  398. if (ra.length > 1) {
  399. let cnd1 = [];
  400. let cnd2 = [];
  401. let names = [];
  402. for (let a of ra) {
  403. cnd1.push(`this.${a.name} || this.${a.name}!==null`);
  404. cnd2.push(`this.${a.name}===null || this.${a.name}===false`);
  405. names.push(a.name);
  406. }
  407. builder.writeln("if (!((%s) && (%s)))\n\t throw new Error('%s paramaters must all"
  408. + " be false-y or all true')", cnd1.join(" && "), cnd2.join(" && "), names.join(", "));
  409. }
  410. }
  411. builder.writeln("return Buffer.concat([");
  412. builder.currentIndent++;
  413. let b = Buffer.alloc(4);
  414. b.writeUInt32LE(tlobject.id, 0);
  415. // First constructor code, we already know its bytes
  416. builder.writeln('Buffer.from("%s","hex"),', b.toString("hex"));
  417. for (let arg of tlobject.args) {
  418. console.log("ok");
  419. if (writeArgToBytes(builder, arg, tlobject.args)) {
  420. builder.writeln(',');
  421. }
  422. }
  423. builder.currentIndent--;
  424. builder.writeln("])");
  425. builder.endBlock();
  426. };
  427. // writeFromReader
  428. const writeFromReader = (tlobject, builder) => {
  429. builder.writeln("static fromReader(reader) {");
  430. for (const arg of tlobject.args) {
  431. if (arg.name !== "flag")
  432. builder.writeln("let %s", "_" + arg.name + ";");
  433. }
  434. builder.writeln("let _x;");
  435. for (const arg of tlobject.args) {
  436. writeArgReadCode(builder, arg, tlobject.args, "_" + arg.name);
  437. }
  438. let temp = [];
  439. for (let a of tlobject.realArgs) {
  440. temp.push(`${a.name}:_${a.name}`)
  441. }
  442. builder.writeln("return this({%s})", temp.join(",\n\t"));
  443. builder.writeln("}");
  444. };
  445. // writeReadResult
  446. /**
  447. * Writes the .__bytes__() code for the given argument
  448. * @param builder: The source code builder
  449. * @param arg: The argument to write
  450. * @param args: All the other arguments in TLObject same __bytes__.
  451. * This is required to determine the flags value
  452. * @param name: The name of the argument. Defaults to "self.argname"
  453. * This argument is an option because it's required when
  454. * writing Vectors<>
  455. */
  456. const writeArgToBytes = (builder, arg, args, name = null) => {
  457. if (arg.genericDefinition) {
  458. return; // Do nothing, this only specifies a later type
  459. }
  460. if (name === null) {
  461. name = `this.${arg.name}`;
  462. }
  463. name = variableSnakeToCamelCase(name);
  464. // The argument may be a flag, only write if it's not None AND
  465. // if it's not a True type.
  466. // True types are not actually sent, but instead only used to
  467. // determine the flags.
  468. if (arg.isFlag) {
  469. if (arg.type === 'true') {
  470. return; // Exit, since true type is never written
  471. } else if (arg.isVector) {
  472. // Vector flags are special since they consist of 3 values,
  473. // so we need an extra join here. Note that empty vector flags
  474. // should NOT be sent either!
  475. builder.write(
  476. "%s === null || %s === false ? Buffer.alloc(0) :Buffer.concat([",
  477. name,
  478. name
  479. );
  480. } else {
  481. builder.write("%s === null || %s === false ? Buffer.alloc(0) : [", name, name);
  482. }
  483. }
  484. if (arg.isVector) {
  485. if (arg.useVectorId) {
  486. builder.write("'15c4b51c',");
  487. }
  488. builder.write("struct.pack('<i', %s.length),", name);
  489. // Cannot unpack the values for the outer tuple through *[(
  490. // since that's a Python >3.5 feature, so add another join.
  491. builder.write('Buffer.concat([%s.map(x => ', name);
  492. // Temporary disable .is_vector, not to enter this if again
  493. // Also disable .is_flag since it's not needed per element
  494. const oldFlag = arg.isFlag;
  495. arg.isVector = arg.isFlag = false;
  496. writeArgToBytes(builder, arg, args, 'x');
  497. arg.isVector = true;
  498. arg.isFlag = oldFlag;
  499. builder.write(')]),', name);
  500. } else if (arg.flagIndicator) {
  501. // Calculate the flags with those items which are not None
  502. if (!args.some(f => f.isFlag)) {
  503. // There's a flag indicator, but no flag arguments so it's 0
  504. builder.write('Buffer.alloc(4)');
  505. } else {
  506. builder.write("struct.pack('<I', ");
  507. builder.write(
  508. args
  509. .filter(flag => flag.isFlag)
  510. .map(
  511. flag =>
  512. `(this.${flag.name} === null || this.${
  513. flag.name
  514. } === false ? 0 : ${1 << flag.flagIndex})`
  515. )
  516. .join(' | ')
  517. );
  518. builder.write(')');
  519. }
  520. } else if (arg.type === 'int') {
  521. builder.write("struct.pack('<i', %s)", name);
  522. } else if (arg.type === 'long') {
  523. builder.write("struct.pack('<q', %s)", name);
  524. } else if (arg.type === 'int128') {
  525. builder.write("BigIntBuffer.toBufferLE(BigInt(%s,16))", name);
  526. } else if (arg.type === 'int256') {
  527. builder.write("BigIntBuffer.toBufferLE(BigInt(%s,32))", name);
  528. } else if (arg.type === 'double') {
  529. builder.write("struct.pack('<d', %s)", name);
  530. } else if (arg.type === 'string') {
  531. builder.write('this.serializeBytes(%s)', name);
  532. } else if (arg.type === 'Bool') {
  533. builder.write('%s ? 0xb5757299 : 0x379779bc', name);
  534. } else if (arg.type === 'true') {
  535. // These are actually NOT written! Only used for flags
  536. } else if (arg.type === 'bytes') {
  537. builder.write('this.serializeBytes(%s)', name);
  538. } else if (arg.type === 'date') {
  539. builder.write('this.serializeDatetime(%s)', name);
  540. } else {
  541. // Else it may be a custom type
  542. builder.write('%s.bytes', name);
  543. // If the type is not boxed (i.e. starts with lowercase) we should
  544. // not serialize the constructor ID (so remove its first 4 bytes).
  545. let boxed = arg.type.charAt(arg.type.indexOf('.') + 1);
  546. boxed = boxed === boxed.toUpperCase();
  547. if (!boxed) {
  548. builder.write('.slice(4)');
  549. }
  550. }
  551. if (arg.isFlag) {
  552. builder.write(']');
  553. if (arg.isVector) {
  554. builder.write(')');
  555. }
  556. }
  557. return true;
  558. };
  559. /**
  560. * Writes the read code for the given argument, setting the
  561. * arg.name variable to its read value.
  562. * @param builder The source code builder
  563. * @param arg The argument to write
  564. * @param args All the other arguments in TLObject same on_send.
  565. * This is required to determine the flags value
  566. * @param name The name of the argument. Defaults to "self.argname"
  567. * This argument is an option because it's required when
  568. * writing Vectors<>
  569. */
  570. const writeArgReadCode = (builder, arg, args, name) => {
  571. if (arg.genericDefinition) {
  572. return // Do nothing, this only specifies a later type
  573. }
  574. //The argument may be a flag, only write that flag was given!
  575. let wasFlag = false;
  576. if (arg.isFlag) {
  577. // Treat 'true' flags as a special case, since they're true if
  578. // they're set, and nothing else needs to actually be read.
  579. if (arg.type === "true") {
  580. builder.writeln("%s = Boolean(flags & %s)", name, 1 << arg.flagIndex);
  581. return;
  582. }
  583. wasFlag = true;
  584. builder.writeln("if (flags & %s)", 1 << arg.flagIndex);
  585. // Temporary disable .is_flag not to enter this if
  586. // again when calling the method recursively
  587. arg.isFlag = false;
  588. }
  589. if (arg.isVector) {
  590. if (arg.useVectorId) {
  591. // We have to read the vector's constructor ID
  592. builder.writeln("reader.readInt();");
  593. }
  594. builder.writeln("%s = []", name);
  595. builder.writeln("for (let i=0;i<reader.readInt();i++){");
  596. // Temporary disable .is_vector, not to enter this if again
  597. arg.isVector = false;
  598. writeArgReadCode(builder, arg, args, "_x");
  599. builder.writeln("%s.push(_x)", name);
  600. arg.isVector = true;
  601. } else if (arg.flagIndicator) {
  602. //Read the flags, which will indicate what items we should read next
  603. builder.writeln("let flags = reader.readInt()");
  604. builder.writeln();
  605. } else if (arg.type === "int") {
  606. builder.writeln("%s = reader.readInt();", name)
  607. } else if (arg.type === "long") {
  608. builder.writeln("%s = reader.readInt();", name);
  609. } else if (arg.type === "int128") {
  610. builder.writeln('%s = reader.readLargeInt(128);', name);
  611. } else if (arg.type === "int256") {
  612. builder.writeln('%s = reader.readLargeInt(256);', name);
  613. } else if (arg.type === "double") {
  614. builder.writeln('%s = reader.readDouble();', name);
  615. } else if (arg.type === "string") {
  616. builder.writeln('%s = reader.tgReadString();', name);
  617. } else if (arg.type === "Bool") {
  618. builder.writeln('%s = reader.tgReadBool();', name);
  619. } else if (arg.type === "true") {
  620. builder.writeln('%s = true;', name);
  621. } else if (arg.type === "bytes") {
  622. builder.writeln('%s = reader.tgReadBytes();', name);
  623. } else if (arg.type === "date") {
  624. builder.writeln('%s = reader.tgReadDate();', name);
  625. } else {
  626. // Else it may be a custom type
  627. if (!arg.skipConstructorId) {
  628. builder.writeln('%s = reader.tgReadObject();', name);
  629. } else {
  630. // Import the correct type inline to avoid cyclic imports.
  631. // There may be better solutions so that we can just access
  632. // all the types before the files have been parsed, but I
  633. // don't know of any.
  634. let sepIndex = arg.type.indexOf(".");
  635. let ns, t;
  636. if (sepIndex === -1) {
  637. ns = ".";
  638. t = arg.type;
  639. } else {
  640. ns = "." + arg.type.slice(0, sepIndex);
  641. t = arg.type.slice(sepIndex + 1);
  642. }
  643. let className = snakeToCamelCase(t);
  644. // There would be no need to import the type if we're in the
  645. // file with the same namespace, but since it does no harm
  646. // and we don't have information about such thing in the
  647. // method we just ignore that case.
  648. builder.writeln('const %s = require("%S");', className, ns);
  649. builder.writeln("%s = %s.fromReader(reader);", name, className);
  650. }
  651. }
  652. // End vector and flag blocks if required (if we opened them before)
  653. if (arg.isVector) {
  654. builder.endBlock();
  655. }
  656. builder.currentIndent -= 1;
  657. if (wasFlag) {
  658. builder.currentIndent -= 1;
  659. builder.writeln("else");
  660. builder.currentIndent -= 1;
  661. builder.writeln("%s = null", name);
  662. builder.currentIndent -= 1;
  663. // Restore .isFlag;
  664. arg.isFlag = true;
  665. }
  666. };
  667. const writePatched = (outDir, namespaceTlobjects) => {
  668. fs.mkdirSync(outDir, {recursive: true});
  669. for (const [ns, tlobjects] of Object.entries(namespaceTlobjects)) {
  670. const file = `${outDir}/${ns === 'null' ? 'index' : ns}.js`;
  671. const stream = fs.createWriteStream(file);
  672. const builder = new SourceBuilder(stream);
  673. builder.writeln(AUTO_GEN_NOTICE);
  674. builder.writeln("const struct = require('python-struct');");
  675. builder.writeln(`const { TLObject, types, custom } = require('..');`);
  676. builder.writeln(`const Helpers = require('../../utils/Helpers');`);
  677. builder.writeln(`const BigIntBuffer = require("bigint-buffer");`);
  678. builder.writeln();
  679. for (const t of tlobjects) {
  680. builder.writeln(
  681. 'class %s extends custom.%s {',
  682. t.className,
  683. PATCHED_TYPES[t.fullname]
  684. );
  685. builder.writeln('constructor() {');
  686. builder.writeln('super();');
  687. builder.writeln(`this.CONSTRUCTOR_ID = 0x${t.id.toString(16)}`);
  688. builder.writeln(`this.SUBCLASS_OF_ID = 0x${crc32(t.result)}`);
  689. builder.endBlock();
  690. //writeToJson(t, builder);
  691. writeToBytes(t, builder);
  692. writeFromReader(t, builder);
  693. builder.writeln();
  694. builder.writeln(
  695. 'types.%s%s = %s',
  696. t.namespace ? `${t.namespace}.` : '',
  697. t.className,
  698. t.className
  699. );
  700. builder.writeln();
  701. }
  702. }
  703. };
  704. const writeAllTLObjects = (tlobjects, layer, builder) => {
  705. builder.writeln(AUTO_GEN_NOTICE);
  706. builder.writeln();
  707. builder.writeln("const { types, functions, patched } = require('.');");
  708. builder.writeln();
  709. // Create a constant variable to indicate which layer this is
  710. builder.writeln(`const LAYER = %s;`, layer);
  711. builder.writeln();
  712. // Then create the dictionary containing constructor_id: class
  713. builder.writeln('const tlobjects = {');
  714. // Fill the dictionary (0x1a2b3c4f: tl.full.type.path.Class)
  715. for (const tlobject of tlobjects) {
  716. builder.write('0x0%s: ', tlobject.id.toString(16).padStart(8, '0'));
  717. if (tlobject.fullname in PATCHED_TYPES) {
  718. builder.write('patched');
  719. } else {
  720. builder.write(tlobject.isFunction ? 'functions' : 'types');
  721. }
  722. if (tlobject.namespace) {
  723. builder.write('.%s', tlobject.namespace);
  724. }
  725. builder.writeln('.%s,', tlobject.className);
  726. }
  727. builder.endBlock(true);
  728. builder.writeln('');
  729. builder.writeln('module.exports = {');
  730. builder.writeln('LAYER,');
  731. builder.writeln('tlobjects');
  732. builder.endBlock(true);
  733. };
  734. const generateTLObjects = (tlobjects, layer, importDepth, outputDir) => {
  735. // Group everything by {namespace :[tlobjects]} to generate index.js
  736. const namespaceFunctions = {};
  737. const namespaceTypes = {};
  738. const namespacePatched = {};
  739. // Group {type: [constructors]} to generate the documentation
  740. const typeConstructors = {};
  741. for (const tlobject of tlobjects) {
  742. if (tlobject.isFunction) {
  743. if (!namespaceFunctions[tlobject.namespace]) {
  744. namespaceFunctions[tlobject.namespace] = [];
  745. }
  746. namespaceFunctions[tlobject.namespace].push(tlobject);
  747. } else {
  748. if (!namespaceTypes[tlobject.namespace]) {
  749. namespaceTypes[tlobject.namespace] = [];
  750. }
  751. if (!typeConstructors[tlobject.result]) {
  752. typeConstructors[tlobject.result] = [];
  753. }
  754. namespaceTypes[tlobject.namespace].push(tlobject);
  755. typeConstructors[tlobject.result].push(tlobject);
  756. if (tlobject.fullname in PATCHED_TYPES) {
  757. if (!namespacePatched[tlobject.namespace]) {
  758. namespacePatched[tlobject.namespace] = [];
  759. }
  760. namespacePatched[tlobject.namespace].push(tlobject);
  761. }
  762. }
  763. }
  764. writeModules(
  765. `${outputDir}/functions`,
  766. importDepth,
  767. 'TLRequest',
  768. namespaceFunctions,
  769. typeConstructors
  770. );
  771. writeModules(
  772. `${outputDir}/types`,
  773. importDepth,
  774. 'TLObject',
  775. namespaceTypes,
  776. typeConstructors
  777. );
  778. writePatched(`${outputDir}/patched`, namespacePatched);
  779. const filename = `${outputDir}/alltlobjects.js`;
  780. const stream = fs.createWriteStream(filename);
  781. const builder = new SourceBuilder(stream);
  782. writeAllTLObjects(tlobjects, layer, builder);
  783. };
  784. const cleanTLObjects = outputDir => {
  785. for (let d in ['functions', 'types', 'patched']) {
  786. d = `${outputDir}/d`;
  787. if (fs.statSync(d).isDirectory()) {
  788. fs.rmdirSync(d);
  789. }
  790. }
  791. const tl = `${outputDir}/alltlobjects.js`;
  792. if (fs.statSync(tl).isFile()) {
  793. fs.unlinkSync(tl);
  794. }
  795. };
  796. const writeModuleExports = (tlobjects, builder) => {
  797. builder.writeln('module.exports = {');
  798. for (const t of tlobjects) {
  799. builder.writeln(`${t.className},`);
  800. }
  801. builder.currentIndent--;
  802. builder.writeln('};');
  803. };
  804. module.exports = {
  805. generateTLObjects,
  806. cleanTLObjects,
  807. };