Skip to content

Bug: (@tgsnake/core) error - [32] Error when trying to make auth key: RangeError: Out of memory: BigInt generated from this operation is too big #9

@sudoskys

Description

@sudoskys

This is a standalone script, the purpose is to download the gift GIF

Log

(@tgsnake/core) error - [32] Error when trying to make auth key:  RangeError: Out of memory: BigInt generated from this operation is too big
    at decompose (/home/nano/Project/myapp/node_modules/.pnpm/@tgsnake+core@1.13.11/node_modules/@tgsnake/core/lib/src/crypto/Prime.js:47:95)
    at <anonymous> (/home/nano/Project/myapp/node_modules/.pnpm/@tgsnake+core@1.13.11/node_modules/@tgsnake/core/lib/src/session/Auth.js:55:73)
    at processTicksAndRejections (:12:39) {
  [message]: 'Out of memory: BigInt generated from this operation is too big',
  originalLine: 25,
  originalColumn: 15,
  [line]: 25,
  [column]: 15,
  [sourceURL]: '/home/nano/Project/myapp/node_modules/.pnpm/@tgsnake+core@1.13.11/node_modules/@tgsnake/core/lib/src/crypto/Prime.js',
  [stack]: 'RangeError: Out of memory: BigInt generated from this operation is too big\n' +
    '    at decompose (/home/nano/Project/myapp/node_modules/.pnpm/@tgsnake+core@1.13.11/node_modules/@tgsnake/core/lib/src/crypto/Prime.js:47:95)\n' +
    '    at <anonymous> (/home/nano/Project/myapp/node_modules/.pnpm/@tgsnake+core@1.13.11/node_modules/@tgsnake/core/lib/src/session/Auth.js:55:73)\n' +
    '    at processTicksAndRejections (:12:39)'
} 2/11/2024 20:9:29.620
(@tgsnake/core) error - [32] Error when trying to make auth key:  RangeError: Out of memory: BigInt generated from this operation is too big
    at decompose (/home/nano/Project/myapp/node_modules/.pnpm/@tgsnake+core@1.13.11/node_modules/@tgsnake/core/lib/src/crypto/Prime.js:47:95)
    at <anonymous> (/home/nano/Project/myapp/node_modules/.pnpm/@tgsnake+core@1.13.11/node_modules/@tgsnake/core/lib/src/session/Auth.js:55:73)
    at processTicksAndRejections (:12:39) {
  [message]: 'Out of memory: BigInt generated from this operation is too big',
  originalLine: 25,
  originalColumn: 15,
  [line]: 25,
  [column]: 15,
  [sourceURL]: '/home/nano/Project/myapp/node_modules/.pnpm/@tgsnake+core@1.13.11/node_modules/@tgsnake/core/lib/src/crypto/Prime.js',
  [stack]: 'RangeError: Out of memory: BigInt generated from this operation is too big\n' +
    '    at decompose (/home/nano/Project/myapp/node_modules/.pnpm/@tgsnake+core@1.13.11/node_modules/@tgsnake/core/lib/src/crypto/Prime.js:47:95)\n' +
    '    at <anonymous> (/home/nano/Project/myapp/node_modules/.pnpm/@tgsnake+core@1.13.11/node_modules/@tgsnake/core/lib/src/session/Auth.js:55:73)\n' +
    '    at processTicksAndRejections (:12:39)'
} 2/11/2024 20:9:32.475
42 |         }
43 |         let k = BigInt(0);
44 |         while (k < r && g === BigInt(1)) {
45 |             ys = y;
46 |             for (let i = 0; BigInt(i) < helpers_js_1.bigMath.min(m, r - k); i++) {
47 |                 y = (0, helpers_js_1.bigIntMod)((0, helpers_js_1.bigIntPow)(y, BigInt(2), pq) + c, pq);
                                                                                                   ^
RangeError: Out of memory: BigInt generated from this operation is too big
      at decompose (/home/nano/Project/myapp/node_modules/.pnpm/@tgsnake+core@1.13.11/node_modules/@tgsnake/core/lib/src/crypto/Prime.js:47:95)
      at /home/nano/Project/myapp/node_modules/.pnpm/@tgsnake+core@1.13.11/node_modules/@tgsnake/core/lib/src/session/Auth.js:94:31

Reproduce

import { Snake } from "tgsnake";
import { Raw } from "@tgsnake/core";
import { fs } from "tgsnake/lib/src/platform.node";
const client = new Snake();
// example start command
client.cmd('start', async (ctx) => {
  return ctx.message.reply('Alive!!');
});
client.run().then(async () => {
  try {
    const req = new Raw.payments.GetStarGifts({ hash: 0 });
    const response = await client.api.invoke(req, 1, 1000, 1000);

    if ('gifts' in response) {
      const gifts = response;
      for (const gift of gifts.gifts) {
        console.log(`
            Gift ID: ${gift.id.toString()}
            Stars: ${gift.stars.toString()}
            Limited: ${gift.limited ? 'Yes' : 'No'}
            Sold Out: ${gift.soldOut ? 'Yes' : 'No'}
            Remaining: ${gift.availabilityRemains}
            Total: ${gift.availabilityTotal}
            Required Stars: ${gift.convertStars.toString()}
            First Sale Date: ${new Date(gift.firstSaleDate * 1000).toLocaleString()}
            Last Sale Date: ${new Date(gift.lastSaleDate * 1000).toLocaleString()}
            Sticker: ${gift.sticker.toJSON()}
            `);
        // console.log(gift.sticker.toString());

        if ('size' in gift.sticker && 'id' in gift.sticker) {
          if (Number(gift.sticker.size) === 0) {
            console.log(`Skipping sticker with size 0: ${gift.id.toString()}`);
            continue;
          }

          const location = new Raw.InputDocumentFileLocation({
            id: gift.sticker.id,
            accessHash: gift.sticker.accessHash,
            fileReference: gift.sticker.fileReference,
            thumbSize: ""
          });

          const folderPath = './stickers';
          const extension = gift.sticker.mimeType === 'application/x-tgsticker' ? '.tgs' : '.webp';
          const filePath = `${folderPath}/${gift.id.toString()}${extension}`;
          try {
            if (!fs.existsSync(folderPath)) {
              fs.mkdirSync(folderPath, { recursive: true });
            }
            const writeStream = fs.createWriteStream(filePath);
            
            const filePart = 256 * 1024;
            const totalSize = Number(gift.sticker.size);
            let offset = 0;

            // Add progress display
            console.log(`Start downloading sticker ${gift.id.toString()}, total size: ${totalSize} bytes`);
            
            while (offset < totalSize) {
              try {
                const res = await client._client.download({
                  file: location,
                  offset: BigInt(offset),
                  limit: Math.min(filePart, totalSize - offset),
                  dcId: Number(gift.sticker.dcId)
                });
                
                writeStream.write(res);
                offset += filePart;
                
                // Show download progress
                console.log(`Download progress: ${Math.min(100, (offset / totalSize) * 100).toFixed(2)}%`);
              } catch (err) {
                console.error(`Failed to download chunk, retrying... offset: ${offset}`, err);
                await new Promise(resolve => setTimeout(resolve, 1000)); // Add retry delay
                continue;
              }
            }

            writeStream.end();
            console.log(`Sticker saved: ${gift.id.toString()}`);
          } catch (err) {
            console.error(`Failed to download sticker ${gift.id.toString()}:`, err);
          }
        }
      }
    }
  } catch (err) {
    console.log(err);
  }
});



Env

"scripts": {
    "start": "bun run src/index.ts"
  },
  "dependencies": {
    "tgsnake": "^3.3.2"
  },
  "overrides": {
    "tgsnake": {
      "@tgsnake/core": "latest"
    }
  }
  • Vscode
  • Archlinux
  • CMD bun run src/index.ts

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingbun platformThis issue only can be reproduce in bun platformcan be reproduceThis issue can be reproduce.help wantedHelp is needed to solved this issue

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions