tlobject.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  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. if (writeArgToBytes(builder, arg, tlobject.args)) {
  419. builder.writeln(',');
  420. }
  421. }
  422. builder.currentIndent--;
  423. builder.writeln("])");
  424. builder.endBlock();
  425. };
  426. // writeFromReader
  427. const writeFromReader = (tlobject, builder) => {
  428. builder.writeln("static fromReader(reader) {");
  429. for (const arg of tlobject.args) {
  430. if (arg.name !== "flag") {
  431. if (arg.name !== "x") {
  432. builder.writeln("let %s", "_" + arg.name + ";");
  433. }
  434. }
  435. }
  436. builder.writeln("let _x;");
  437. for (const arg of tlobject.args) {
  438. writeArgReadCode(builder, arg, tlobject.args, "_" + arg.name);
  439. }
  440. let temp = [];
  441. for (let a of tlobject.realArgs) {
  442. temp.push(`${a.name}:_${a.name}`)
  443. }
  444. builder.writeln("return this({%s})", temp.join(",\n\t"));
  445. builder.writeln("}");
  446. };
  447. // writeReadResult
  448. /**
  449. * Writes the .__bytes__() code for the given argument
  450. * @param builder: The source code builder
  451. * @param arg: The argument to write
  452. * @param args: All the other arguments in TLObject same __bytes__.
  453. * This is required to determine the flags value
  454. * @param name: The name of the argument. Defaults to "self.argname"
  455. * This argument is an option because it's required when
  456. * writing Vectors<>
  457. */
  458. const writeArgToBytes = (builder, arg, args, name = null) => {
  459. if (arg.genericDefinition) {
  460. return; // Do nothing, this only specifies a later type
  461. }
  462. if (name === null) {
  463. name = `this.${arg.name}`;
  464. }
  465. name = variableSnakeToCamelCase(name);
  466. // The argument may be a flag, only write if it's not None AND
  467. // if it's not a True type.
  468. // True types are not actually sent, but instead only used to
  469. // determine the flags.
  470. if (arg.isFlag) {
  471. if (arg.type === 'true') {
  472. return; // Exit, since true type is never written
  473. } else if (arg.isVector) {
  474. // Vector flags are special since they consist of 3 values,
  475. // so we need an extra join here. Note that empty vector flags
  476. // should NOT be sent either!
  477. builder.write(
  478. "%s === null || %s === false ? Buffer.alloc(0) :Buffer.concat([",
  479. name,
  480. name
  481. );
  482. } else {
  483. builder.write("%s === null || %s === false ? Buffer.alloc(0) : [", name, name);
  484. }
  485. }
  486. if (arg.isVector) {
  487. if (arg.useVectorId) {
  488. builder.write("'15c4b51c',");
  489. }
  490. builder.write("struct.pack('<i', %s.length),", name);
  491. // Cannot unpack the values for the outer tuple through *[(
  492. // since that's a Python >3.5 feature, so add another join.
  493. builder.write('Buffer.concat([%s.map(x => ', name);
  494. // Temporary disable .is_vector, not to enter this if again
  495. // Also disable .is_flag since it's not needed per element
  496. const oldFlag = arg.isFlag;
  497. arg.isVector = arg.isFlag = false;
  498. writeArgToBytes(builder, arg, args, 'x');
  499. arg.isVector = true;
  500. arg.isFlag = oldFlag;
  501. builder.write(')]),', name);
  502. } else if (arg.flagIndicator) {
  503. // Calculate the flags with those items which are not None
  504. if (!args.some(f => f.isFlag)) {
  505. // There's a flag indicator, but no flag arguments so it's 0
  506. builder.write('Buffer.alloc(4)');
  507. } else {
  508. builder.write("struct.pack('<I', ");
  509. builder.write(
  510. args
  511. .filter(flag => flag.isFlag)
  512. .map(
  513. flag =>
  514. `(this.${flag.name} === null || this.${
  515. flag.name
  516. } === false ? 0 : ${1 << flag.flagIndex})`
  517. )
  518. .join(' | ')
  519. );
  520. builder.write(')');
  521. }
  522. } else if (arg.type === 'int') {
  523. builder.write("struct.pack('<i', %s)", name);
  524. } else if (arg.type === 'long') {
  525. builder.write("struct.pack('<q', %s)", name);
  526. } else if (arg.type === 'int128') {
  527. builder.write("BigIntBuffer.toBufferLE(BigInt(%s,16))", name);
  528. } else if (arg.type === 'int256') {
  529. builder.write("BigIntBuffer.toBufferLE(BigInt(%s,32))", name);
  530. } else if (arg.type === 'double') {
  531. builder.write("struct.pack('<d', %s)", name);
  532. } else if (arg.type === 'string') {
  533. builder.write('this.serializeBytes(%s)', name);
  534. } else if (arg.type === 'Bool') {
  535. builder.write('%s ? 0xb5757299 : 0x379779bc', name);
  536. } else if (arg.type === 'true') {
  537. // These are actually NOT written! Only used for flags
  538. } else if (arg.type === 'bytes') {
  539. builder.write('this.serializeBytes(%s)', name);
  540. } else if (arg.type === 'date') {
  541. builder.write('this.serializeDatetime(%s)', name);
  542. } else {
  543. // Else it may be a custom type
  544. builder.write('%s.bytes', name);
  545. // If the type is not boxed (i.e. starts with lowercase) we should
  546. // not serialize the constructor ID (so remove its first 4 bytes).
  547. let boxed = arg.type.charAt(arg.type.indexOf('.') + 1);
  548. boxed = boxed === boxed.toUpperCase();
  549. if (!boxed) {
  550. builder.write('.slice(4)');
  551. }
  552. }
  553. if (arg.isFlag) {
  554. builder.write(']');
  555. if (arg.isVector) {
  556. builder.write(')');
  557. }
  558. }
  559. return true;
  560. };
  561. /**
  562. * Writes the read code for the given argument, setting the
  563. * arg.name variable to its read value.
  564. * @param builder The source code builder
  565. * @param arg The argument to write
  566. * @param args All the other arguments in TLObject same on_send.
  567. * This is required to determine the flags value
  568. * @param name The name of the argument. Defaults to "self.argname"
  569. * This argument is an option because it's required when
  570. * writing Vectors<>
  571. */
  572. const writeArgReadCode = (builder, arg, args, name) => {
  573. if (arg.genericDefinition) {
  574. return // Do nothing, this only specifies a later type
  575. }
  576. //The argument may be a flag, only write that flag was given!
  577. let wasFlag = false;
  578. if (arg.isFlag) {
  579. // Treat 'true' flags as a special case, since they're true if
  580. // they're set, and nothing else needs to actually be read.
  581. if (arg.type === "true") {
  582. builder.writeln("%s = Boolean(flags & %s);", name, 1 << arg.flagIndex);
  583. return;
  584. }
  585. wasFlag = true;
  586. builder.writeln("if (flags & %s) {", 1 << arg.flagIndex);
  587. // Temporary disable .is_flag not to enter this if
  588. // again when calling the method recursively
  589. arg.isFlag = false;
  590. }
  591. if (arg.isVector) {
  592. if (arg.useVectorId) {
  593. // We have to read the vector's constructor ID
  594. builder.writeln("reader.readInt();");
  595. }
  596. builder.writeln("%s = [];", name);
  597. builder.writeln("for (let i=0;i<reader.readInt();i++){");
  598. // Temporary disable .is_vector, not to enter this if again
  599. arg.isVector = false;
  600. writeArgReadCode(builder, arg, args, "_x");
  601. builder.writeln("%s.push(_x);", name);
  602. arg.isVector = true;
  603. } else if (arg.flagIndicator) {
  604. //Read the flags, which will indicate what items we should read next
  605. builder.writeln("let flags = reader.readInt();");
  606. builder.writeln();
  607. } else if (arg.type === "int") {
  608. builder.writeln("%s = reader.readInt();", name)
  609. } else if (arg.type === "long") {
  610. builder.writeln("%s = reader.readInt();", name);
  611. } else if (arg.type === "int128") {
  612. builder.writeln('%s = reader.readLargeInt(128);', name);
  613. } else if (arg.type === "int256") {
  614. builder.writeln('%s = reader.readLargeInt(256);', name);
  615. } else if (arg.type === "double") {
  616. builder.writeln('%s = reader.readDouble();', name);
  617. } else if (arg.type === "string") {
  618. builder.writeln('%s = reader.tgReadString();', name);
  619. } else if (arg.type === "Bool") {
  620. builder.writeln('%s = reader.tgReadBool();', name);
  621. } else if (arg.type === "true") {
  622. builder.writeln('%s = true;', name);
  623. } else if (arg.type === "bytes") {
  624. builder.writeln('%s = reader.tgReadBytes();', name);
  625. } else if (arg.type === "date") {
  626. builder.writeln('%s = reader.tgReadDate();', name);
  627. } else {
  628. // Else it may be a custom type
  629. if (!arg.skipConstructorId) {
  630. builder.writeln('%s = reader.tgReadObject();', name);
  631. } else {
  632. // Import the correct type inline to avoid cyclic imports.
  633. // There may be better solutions so that we can just access
  634. // all the types before the files have been parsed, but I
  635. // don't know of any.
  636. let sepIndex = arg.type.indexOf(".");
  637. let ns, t;
  638. if (sepIndex === -1) {
  639. ns = ".";
  640. t = arg.type;
  641. } else {
  642. ns = "." + arg.type.slice(0, sepIndex);
  643. t = arg.type.slice(sepIndex + 1);
  644. }
  645. let className = snakeToCamelCase(t);
  646. // There would be no need to import the type if we're in the
  647. // file with the same namespace, but since it does no harm
  648. // and we don't have information about such thing in the
  649. // method we just ignore that case.
  650. builder.writeln('let %s = require("%s");', className, ns);
  651. builder.writeln("%s = %s.fromReader(reader);", name, className);
  652. }
  653. }
  654. // End vector and flag blocks if required (if we opened them before)
  655. if (arg.isVector) {
  656. builder.writeln("}");
  657. }
  658. if (wasFlag) {
  659. builder.endBlock();
  660. builder.writeln("else {");
  661. builder.writeln("%s = null", name);
  662. builder.endBlock();
  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.endBlock();
  695. builder.currentIndent = 0;
  696. builder.writeln(
  697. 'types.%s%s = %s',
  698. t.namespace ? `${t.namespace}.` : '',
  699. t.className,
  700. t.className
  701. );
  702. builder.writeln();
  703. }
  704. }
  705. };
  706. const writeAllTLObjects = (tlobjects, layer, builder) => {
  707. builder.writeln(AUTO_GEN_NOTICE);
  708. builder.writeln();
  709. builder.writeln("const { types, functions, patched } = require('.');");
  710. builder.writeln();
  711. // Create a constant variable to indicate which layer this is
  712. builder.writeln(`const LAYER = %s;`, layer);
  713. builder.writeln();
  714. // Then create the dictionary containing constructor_id: class
  715. builder.writeln('const tlobjects = {');
  716. // Fill the dictionary (0x1a2b3c4f: tl.full.type.path.Class)
  717. for (const tlobject of tlobjects) {
  718. builder.write('0x0%s: ', tlobject.id.toString(16).padStart(8, '0'));
  719. if (tlobject.fullname in PATCHED_TYPES) {
  720. builder.write('patched');
  721. } else {
  722. builder.write(tlobject.isFunction ? 'functions' : 'types');
  723. }
  724. if (tlobject.namespace) {
  725. builder.write('.%s', tlobject.namespace);
  726. }
  727. builder.writeln('.%s,', tlobject.className);
  728. }
  729. builder.endBlock(true);
  730. builder.writeln('');
  731. builder.writeln('module.exports = {');
  732. builder.writeln('LAYER,');
  733. builder.writeln('tlobjects');
  734. builder.endBlock(true);
  735. };
  736. const generateTLObjects = (tlobjects, layer, importDepth, outputDir) => {
  737. // Group everything by {namespace :[tlobjects]} to generate index.js
  738. const namespaceFunctions = {};
  739. const namespaceTypes = {};
  740. const namespacePatched = {};
  741. // Group {type: [constructors]} to generate the documentation
  742. const typeConstructors = {};
  743. for (const tlobject of tlobjects) {
  744. if (tlobject.isFunction) {
  745. if (!namespaceFunctions[tlobject.namespace]) {
  746. namespaceFunctions[tlobject.namespace] = [];
  747. }
  748. namespaceFunctions[tlobject.namespace].push(tlobject);
  749. } else {
  750. if (!namespaceTypes[tlobject.namespace]) {
  751. namespaceTypes[tlobject.namespace] = [];
  752. }
  753. if (!typeConstructors[tlobject.result]) {
  754. typeConstructors[tlobject.result] = [];
  755. }
  756. namespaceTypes[tlobject.namespace].push(tlobject);
  757. typeConstructors[tlobject.result].push(tlobject);
  758. if (tlobject.fullname in PATCHED_TYPES) {
  759. if (!namespacePatched[tlobject.namespace]) {
  760. namespacePatched[tlobject.namespace] = [];
  761. }
  762. namespacePatched[tlobject.namespace].push(tlobject);
  763. }
  764. }
  765. }
  766. writeModules(
  767. `${outputDir}/functions`,
  768. importDepth,
  769. 'TLRequest',
  770. namespaceFunctions,
  771. typeConstructors
  772. );
  773. writeModules(
  774. `${outputDir}/types`,
  775. importDepth,
  776. 'TLObject',
  777. namespaceTypes,
  778. typeConstructors
  779. );
  780. writePatched(`${outputDir}/patched`, namespacePatched);
  781. const filename = `${outputDir}/alltlobjects.js`;
  782. const stream = fs.createWriteStream(filename);
  783. const builder = new SourceBuilder(stream);
  784. writeAllTLObjects(tlobjects, layer, builder);
  785. };
  786. const cleanTLObjects = outputDir => {
  787. for (let d in ['functions', 'types', 'patched']) {
  788. d = `${outputDir}/d`;
  789. if (fs.statSync(d).isDirectory()) {
  790. fs.rmdirSync(d);
  791. }
  792. }
  793. const tl = `${outputDir}/alltlobjects.js`;
  794. if (fs.statSync(tl).isFile()) {
  795. fs.unlinkSync(tl);
  796. }
  797. };
  798. const writeModuleExports = (tlobjects, builder) => {
  799. builder.writeln('module.exports = {');
  800. for (const t of tlobjects) {
  801. builder.writeln(`${t.className},`);
  802. }
  803. builder.currentIndent--;
  804. builder.writeln('};');
  805. };
  806. module.exports = {
  807. generateTLObjects,
  808. cleanTLObjects,
  809. };