fix(core): payload size limit

This commit is contained in:
teidesu 2021-05-09 19:34:25 +03:00
parent df84137391
commit cae7f90c57
2 changed files with 29 additions and 10 deletions

View file

@ -1,6 +1,9 @@
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'
import { MaybeArray } from '@mtcute/core' import { MaybeArray } from '@mtcute/core'
import { createUsersChatsIndex, normalizeToInputChannel } from '../../utils/peer-utils' import {
createUsersChatsIndex,
normalizeToInputChannel,
} from '../../utils/peer-utils'
import { tl } from '@mtcute/tl' import { tl } from '@mtcute/tl'
import { Message, InputPeerLike, MtCuteTypeAssertionError } from '../../types' import { Message, InputPeerLike, MtCuteTypeAssertionError } from '../../types'
@ -53,16 +56,17 @@ export async function getMessages(
const isSingle = !Array.isArray(messageIds) const isSingle = !Array.isArray(messageIds)
if (isSingle) messageIds = [messageIds as number] if (isSingle) messageIds = [messageIds as number]
const ids = (messageIds as number[]).map( const type = fromReply ? 'inputMessageReplyTo' : 'inputMessageID'
(it) => const ids: tl.TypeInputMessage[] = (messageIds as number[]).map((it) => ({
({ _: type,
_: fromReply ? 'inputMessageReplyTo' : 'inputMessageID',
id: it, id: it,
} as tl.TypeInputMessage) }))
)
const res = await this.call( const res = await this.call(
peer._ === 'inputPeerChannel' || peer._ === 'inputChannel' peer._ === 'inputPeerChannel' ||
peer._ === 'inputChannel' ||
peer._ === 'inputPeerChannelFromMessage' ||
peer._ === 'inputChannelFromMessage'
? { ? {
_: 'channels.getMessages', _: 'channels.getMessages',
id: ids, id: ids,

View file

@ -551,6 +551,21 @@ export class TelegramConnection extends PersistentConnection {
stack = new Error().stack stack = new Error().stack
} }
const content = typeof method === 'string' ? message! : method.message
if (content.length > 1044404) {
// if you send larger payloads, telegram will just close connection,
// and since we resend them, it will get resent after reconnection and
// that will be an endless loop of reconnections. we don't want that,
// and payloads this large are usually a sign of an error in the code.
const err = new Error(`Payload is too big (${content.length} > 1044404)`)
if (typeof method === 'string') {
throw err
} else {
// shouldn't happen, but whatever
method.promise.reject(err)
}
}
const promise = const promise =
typeof method === 'string' typeof method === 'string'
? createControllablePromise() ? createControllablePromise()
@ -572,7 +587,7 @@ export class TelegramConnection extends PersistentConnection {
this._pendingRpcCalls[messageId.toString(16)] = pending this._pendingRpcCalls[messageId.toString(16)] = pending
const encrypted = await this._mtproto.encryptMessage( const encrypted = await this._mtproto.encryptMessage(
typeof method === 'string' ? message! : method.message, content,
messageId, messageId,
seqNo seqNo
) )