tlobject.js 31 KB

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