diff --git a/gramjs/client/TelegramClient.ts b/gramjs/client/TelegramClient.ts index ca8cff73..ecea5e7f 100644 --- a/gramjs/client/TelegramClient.ts +++ b/gramjs/client/TelegramClient.ts @@ -1298,11 +1298,11 @@ export class TelegramClient extends TelegramBaseClient { * @example * ```ts * const me = await client.getEntity("me"); - * console.log("My name is",utils.getDisplayName(me)); + * console.log("My name is", utils.getDisplayName(me)); * * const chat = await client.getInputEntity("username"); - * for await (const message of client.iterMessages(chat){ - * console.log("Message text is",message.text); + * for await (const message of client.iterMessages(chat)) { + * console.log("Message text is", message.text); * } * * // Note that you could have used the username directly, but it's diff --git a/gramjs/client/telegramBaseClient.ts b/gramjs/client/telegramBaseClient.ts index bd611119..fb5c893c 100644 --- a/gramjs/client/telegramBaseClient.ts +++ b/gramjs/client/telegramBaseClient.ts @@ -284,11 +284,11 @@ export abstract class TelegramBaseClient { } this._connection = clientParams.connection; let initProxy; - if (this._proxy?.MTProxy) { + if (this._proxy && 'MTProxy' in this._proxy) { this._connection = ConnectionTCPMTProxyAbridged; initProxy = new Api.InputClientProxy({ - address: this._proxy!.ip, - port: this._proxy!.port, + address: this._proxy.ip, + port: this._proxy.port, }); } this._initRequest = new Api.InitConnection({ diff --git a/gramjs/extensions/PromisedNetSockets.ts b/gramjs/extensions/PromisedNetSockets.ts index e1f0c405..35d92783 100644 --- a/gramjs/extensions/PromisedNetSockets.ts +++ b/gramjs/extensions/PromisedNetSockets.ts @@ -2,7 +2,7 @@ import * as net from "./net"; import { SocksClient } from "./socks"; import { Mutex } from "async-mutex"; -import { ProxyInterface } from "../network/connection/TCPMTProxy"; +import { ProxyInterface, SocksProxyType } from "../network/connection/TCPMTProxy"; const mutex = new Mutex(); @@ -14,22 +14,22 @@ export class PromisedNetSockets { private stream: Buffer; private canRead?: boolean | Promise; private resolveRead: ((value?: any) => void) | undefined; - private proxy?: ProxyInterface; + private proxy?: SocksProxyType; constructor(proxy?: ProxyInterface) { this.client = undefined; this.closed = true; this.stream = Buffer.alloc(0); - if (!proxy?.MTProxy) { + if (proxy) { // we only want to use this when it's not an MTProto proxy. - if (proxy) { + if (!('MTProxy' in proxy)) { if (!proxy.ip || !proxy.port || !proxy.socksType) { throw new Error( - `Invalid sockets params. ${proxy.ip}, ${proxy.port}, ${proxy.socksType}` + `Invalid sockets params: ip=${proxy.ip}, port=${proxy.port}, socksType=${proxy.socksType}` ); } + this.proxy = proxy; } - this.proxy = proxy; } } @@ -90,10 +90,7 @@ export class PromisedNetSockets { proxy: { host: this.proxy.ip, port: this.proxy.port, - type: - this.proxy.socksType != undefined - ? this.proxy.socksType - : 5, // Proxy version (4 or 5) + type: this.proxy.socksType, userId: this.proxy.username, password: this.proxy.password, }, diff --git a/gramjs/extensions/markdown.ts b/gramjs/extensions/markdown.ts index cc8345e3..50241980 100644 --- a/gramjs/extensions/markdown.ts +++ b/gramjs/extensions/markdown.ts @@ -38,7 +38,7 @@ export class MarkdownParser { foundIndex - tempEntities[foundDelim].offset; entities.push(tempEntities[foundDelim]); } - message = message.replace(foundDelim, ""); + message = message.toString().replace(foundDelim, ""); i = foundIndex; } return [message, entities]; diff --git a/gramjs/network/connection/TCPMTProxy.ts b/gramjs/network/connection/TCPMTProxy.ts index 71d6d184..f5584784 100644 --- a/gramjs/network/connection/TCPMTProxy.ts +++ b/gramjs/network/connection/TCPMTProxy.ts @@ -8,16 +8,22 @@ import { } from "../../extensions"; import { CTR } from "../../crypto/CTR"; -export interface ProxyInterface { - socksType?: 4 | 5; +interface BasicProxyInterface { ip: string; port: number; - secret?: string; - MTProxy?: boolean; timeout?: number; username?: string; password?: string; } +export type MTProxyType = BasicProxyInterface & { + secret: string; + MTProxy: true; +} +export type SocksProxyType = BasicProxyInterface & { + socksType: 4 | 5; +} + +export type ProxyInterface = MTProxyType | SocksProxyType; class MTProxyIO { header?: Buffer = undefined; @@ -154,7 +160,7 @@ export class TCPMTProxy extends ObfuscatedConnection { proxy: proxy, testServers: testServers, }); - if (!proxy.MTProxy) { + if (!('MTProxy' in proxy)) { throw new Error("This connection only supports MPTProxies"); } if (!proxy.secret) {