diff --git a/eslint.config.js b/eslint.config.js index 08ce1d20..e21d2cd5 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -74,18 +74,14 @@ export default antfu({ 'jsdoc/check-param-names': 'off', // todo: will fix in another iteration 'jsdoc/require-returns-description': 'off', // todo: will fix in another iteration 'ts/no-unsafe-member-access': 'off', - 'ts/no-dynamic-delete': 'off', - 'ts/strict-boolean-expressions': 'off', 'ts/unbound-method': 'off', - 'ts/no-invalid-void-type': 'off', - 'ts/no-unsafe-enum-comparison': 'off', + 'ts/strict-boolean-expressions': 'off', 'ts/promise-function-async': 'off', 'dot-notation': 'off', 'ts/dot-notation': 'off', 'ts/switch-exhaustiveness-check': 'off', 'ts/restrict-template-expressions': 'off', 'ts/method-signature-style': 'off', - 'style/indent-binary-ops': 'off', 'antfu/no-top-level-await': 'off', 'import/extensions': ['error', 'always', { ignorePackages: true, diff --git a/packages/core/src/highlevel/base.ts b/packages/core/src/highlevel/base.ts index 891e886f..d16f877f 100644 --- a/packages/core/src/highlevel/base.ts +++ b/packages/core/src/highlevel/base.ts @@ -8,8 +8,8 @@ import type { ICorePlatform } from '../types/platform.js' import type { MustEqual } from '../types/utils.js' import type { ICryptoProvider, + InputStringSessionData, Logger, - StringSessionData, } from '../utils/index.js' import type { ConnectionState, ITelegramClient } from './client.types.js' import type { ITelegramStorageProvider } from './storage/provider.js' @@ -234,14 +234,14 @@ export class BaseTelegramClient implements ITelegramClient { * @param session Session string to import * @param force Whether to overwrite existing session */ - async importSession(session: string | StringSessionData, force = false): Promise { + async importSession(session: string | InputStringSessionData, force = false): Promise { await this.prepare() const defaultDcAuthKey = await this.mt.storage.provider.authKeys.get(this.mt._defaultDcs.main.id) if (defaultDcAuthKey && !force) return - const data = typeof session === 'string' ? readStringSession(session) : session + const data = readStringSession(session) const testMode = data.primaryDcs.main.testMode if (testMode && !this.params.testMode) { diff --git a/packages/core/src/highlevel/client.ts b/packages/core/src/highlevel/client.ts index f7ce6043..31ee3b18 100644 --- a/packages/core/src/highlevel/client.ts +++ b/packages/core/src/highlevel/client.ts @@ -3,11 +3,9 @@ import type { tdFileId } from '@mtcute/file-id' import type { tl } from '@mtcute/tl' import type Long from 'long' import type { RpcCallOptions } from '../network/index.js' - import type { MaybeArray, MaybePromise, PartialExcept, PartialOnly } from '../types/index.js' import type { BaseTelegramClientOptions } from './base.js' import type { ITelegramClient } from './client.types.js' - import type { LogOutResult } from './methods/auth/log-out.js' import type { CreateGroupResult } from './methods/chats/create-group.js' import type { GetForumTopicsOffset } from './methods/forums/get-forum-topics.js' @@ -29,7 +27,7 @@ import type { ITelegramStorageProvider } from './storage/provider.js' import type { AllStories, ArrayPaginated, ArrayWithTotal, Boost, BoostSlot, BoostStats, BotChatJoinRequestUpdate, BotCommands, BotReactionCountUpdate, BotReactionUpdate, BotStoppedUpdate, BusinessCallbackQuery, BusinessChatLink, BusinessConnection, BusinessMessage, BusinessWorkHoursDay, CallbackQuery, Chat, ChatEvent, ChatInviteLink, ChatInviteLinkMember, ChatJoinRequestUpdate, ChatlistPreview, ChatMember, ChatMemberUpdate, ChatPreview, ChosenInlineResult, CollectibleInfo, DeleteBusinessMessageUpdate, DeleteMessageUpdate, DeleteStoryUpdate, Dialog, FactCheck, FileDownloadLocation, FileDownloadParameters, ForumTopic, FullChat, GameHighScore, HistoryReadUpdate, InlineCallbackQuery, InlineQuery, InputChatEventFilters, InputDialogFolder, InputFileLike, InputInlineResult, InputMediaLike, InputMediaSticker, InputMessageId, InputPeerLike, InputPrivacyRule, InputReaction, InputStickerSet, InputStickerSetItem, InputText, InputWebview, MaybeDynamic, Message, MessageEffect, MessageMedia, MessageReactions, ParametersSkip2, ParsedUpdate, PeerReaction, PeerStories, Photo, Poll, PollUpdate, PollVoteUpdate, PreCheckoutQuery, RawDocument, ReplyMarkup, SentCode, StarGift, StarsStatus, StarsTransaction, Sticker, StickerSet, StickerType, StoriesStealthMode, Story, StoryInteractions, StoryUpdate, StoryViewer, StoryViewersList, TakeoutSession, TextWithEntities, TypingStatus, UploadedFile, UploadFileLike, User, UserStarGift, UserStatusUpdate, UserTypingUpdate, WebviewResult } from './types/index.js' import type { ParsedUpdateHandlerParams } from './updates/parsed.js' import type { RawUpdateInfo } from './updates/types.js' -import type { StringSessionData } from './utils/string-session.js' +import type { InputStringSessionData } from './utils/string-session.js' /* THIS FILE WAS AUTO-GENERATED */ import { Emitter } from '@fuman/utils' import { MtUnsupportedError } from '../types/index.js' @@ -620,7 +618,7 @@ export interface TelegramClient extends ITelegramClient { * Note that passed session will be ignored in case storage already * contains authorization. */ - session?: string | StringSessionData + session?: string | InputStringSessionData /** * Whether to overwrite existing session. @@ -1059,12 +1057,12 @@ export interface TelegramClient extends ITelegramClient { * - `unigram` - Unigram */ platform: - | 'android' - | 'ios' - | 'tdesktop' - | 'macos' - | 'unigram' - | (string & {}) + | 'android' + | 'ios' + | 'tdesktop' + | 'macos' + | 'unigram' + | (string & {}) }): Promise /** * Close a webview previously opened by {@link openWebview} method. @@ -2448,7 +2446,7 @@ export interface TelegramClient extends ITelegramClient { location: FileDownloadLocation, params?: FileDownloadParameters): import('node:stream').Readable /** - * Download a file and return it as a readable stream, + * Download a file and return it as a `@fuman/io` stream, * streaming file contents. * * **Available**: ✅ both users and bots @@ -3362,13 +3360,13 @@ export interface TelegramClient extends ITelegramClient { */ getCallbackQueryMessage( id: - | CallbackQuery - | tl.RawUpdateBotCallbackQuery - | { - messageId: number - queryId: tl.Long - peer: InputPeerLike - }): Promise + | CallbackQuery + | tl.RawUpdateBotCallbackQuery + | { + messageId: number + queryId: tl.Long + peer: InputPeerLike + }): Promise // public version of the same method because why not /** * Get discussion message for some channel post. @@ -4040,14 +4038,14 @@ export interface TelegramClient extends ITelegramClient { */ sendCopyGroup( params: SendCopyGroupParams & - ( - | { - /** Source chat ID */ - fromChatId: InputPeerLike - /** Message IDs to forward */ - messages: number[] - } - | { messages: Message[] } + ( + | { + /** Source chat ID */ + fromChatId: InputPeerLike + /** Message IDs to forward */ + messages: number[] + } + | { messages: Message[] } )): Promise /** * Copy a message (i.e. send the same message, but do not forward it). @@ -4061,14 +4059,14 @@ export interface TelegramClient extends ITelegramClient { */ sendCopy( params: SendCopyParams & - ( - | { - /** Source chat ID */ - fromChatId: InputPeerLike - /** Message ID to forward */ - message: number - } - | { message: Message } + ( + | { + /** Source chat ID */ + fromChatId: InputPeerLike + /** Message ID to forward */ + message: number + } + | { message: Message } )): Promise /** * Send a group of media. diff --git a/packages/core/src/highlevel/client.types.ts b/packages/core/src/highlevel/client.types.ts index 06b2a06d..221993d1 100644 --- a/packages/core/src/highlevel/client.types.ts +++ b/packages/core/src/highlevel/client.types.ts @@ -11,7 +11,7 @@ import type { AppConfigManager } from './managers/app-config-manager.js' import type { TimersManager } from './managers/timers.js' import type { TelegramStorageManager } from './storage/storage.js' import type { RawUpdateInfo } from './updates/types.js' -import type { StringSessionData } from './utils/string-session.js' +import type { InputStringSessionData } from './utils/string-session.js' /** * Connection state of the client @@ -52,7 +52,7 @@ export interface ITelegramClient { message: MustEqual, params?: RpcCallOptions, ): Promise - importSession(session: string | StringSessionData, force?: boolean): Promise + importSession(session: string | InputStringSessionData, force?: boolean): Promise exportSession(): Promise handleClientUpdate(updates: tl.TypeUpdates, noDispatch?: boolean): void diff --git a/packages/core/src/highlevel/methods/_imports.ts b/packages/core/src/highlevel/methods/_imports.ts index 460bfa4d..f75bbcea 100644 --- a/packages/core/src/highlevel/methods/_imports.ts +++ b/packages/core/src/highlevel/methods/_imports.ts @@ -115,4 +115,4 @@ import { // @copy import { RawUpdateInfo } from '../updates/types.js' // @copy -import { StringSessionData } from '../utils/string-session.js' +import { InputStringSessionData } from '../utils/string-session.js' diff --git a/packages/core/src/highlevel/methods/auth/start.ts b/packages/core/src/highlevel/methods/auth/start.ts index 9fe3bb80..33588906 100644 --- a/packages/core/src/highlevel/methods/auth/start.ts +++ b/packages/core/src/highlevel/methods/auth/start.ts @@ -5,12 +5,12 @@ import type { ITelegramClient } from '../../client.types.js' import type { SentCode } from '../../types/auth/sent-code.js' import type { User } from '../../types/peers/user.js' import type { MaybeDynamic } from '../../types/utils.js' -import type { StringSessionData } from '../../utils/string-session.js' +import type { InputStringSessionData } from '../../utils/string-session.js' import { tl } from '@mtcute/tl' import { MtArgumentError, MtcuteError } from '../../../types/errors.js' import { normalizePhoneNumber, resolveMaybeDynamic } from '../../utils/misc-utils.js' -import { getMe } from '../users/get-me.js' +import { getMe } from '../users/get-me.js' import { checkPassword } from './check-password.js' import { resendCode } from './resend-code.js' import { sendCode } from './send-code.js' @@ -43,7 +43,7 @@ export async function start( * Note that passed session will be ignored in case storage already * contains authorization. */ - session?: string | StringSessionData + session?: string | InputStringSessionData /** * Whether to overwrite existing session. diff --git a/packages/core/src/highlevel/utils/string-session.ts b/packages/core/src/highlevel/utils/string-session.ts index ae09d48c..83b1e3de 100644 --- a/packages/core/src/highlevel/utils/string-session.ts +++ b/packages/core/src/highlevel/utils/string-session.ts @@ -4,7 +4,14 @@ import type { CurrentUserInfo } from '../storage/service/current-user.js' import { base64 } from '@fuman/utils' import { TlBinaryReader, TlBinaryWriter } from '@mtcute/tl-runtime' import { MtArgumentError } from '../../types/index.js' -import { parseBasicDcOption, serializeBasicDcOption } from '../../utils/dcs.js' +import { defaultProductionDc, defaultTestDc, parseBasicDcOption, serializeBasicDcOption } from '../../utils/dcs.js' + +export interface InputStringSessionData { + primaryDcs?: DcOptions + testMode?: boolean + self?: CurrentUserInfo | null + authKey: Uint8Array +} export interface StringSessionData { version: number @@ -77,7 +84,16 @@ export function writeStringSession(data: StringSessionData): string { return base64.encode(writer.result(), true) } -export function readStringSession(data: string): StringSessionData { +export function readStringSession(data: string | InputStringSessionData): StringSessionData { + if (typeof data !== 'string') { + return { + version: 3, + primaryDcs: data.primaryDcs ?? (data.testMode ? defaultTestDc : defaultProductionDc), + self: data.self ?? null, + authKey: data.authKey, + } + } + const buf = base64.decode(data, true) const version = buf[0]