2024-08-13 04:53:07 +03:00
|
|
|
import type { ITelegramStorageProvider, PartialOnly } from '@mtcute/core'
|
|
|
|
import type {
|
|
|
|
BaseTelegramClientOptions as BaseTelegramClientOptionsBase,
|
|
|
|
TelegramClientOptions,
|
|
|
|
} from '@mtcute/core/client.js'
|
2024-03-03 09:45:54 +03:00
|
|
|
import {
|
|
|
|
BaseTelegramClient as BaseTelegramClientBase,
|
|
|
|
TelegramClient as TelegramClientBase,
|
|
|
|
} from '@mtcute/core/client.js'
|
|
|
|
|
|
|
|
import { WebCryptoProvider } from './crypto.js'
|
|
|
|
import { IdbStorage } from './idb/index.js'
|
|
|
|
import { WebSocketTransport } from './websocket.js'
|
2024-09-18 02:10:36 +03:00
|
|
|
import { WebPlatform } from './platform.js'
|
2024-03-03 09:45:54 +03:00
|
|
|
|
|
|
|
export type { TelegramClientOptions }
|
|
|
|
|
|
|
|
export interface BaseTelegramClientOptions
|
2024-09-18 02:10:36 +03:00
|
|
|
extends PartialOnly<Omit<BaseTelegramClientOptionsBase, 'storage'>, 'transport' | 'crypto' | 'platform'> {
|
2024-03-03 09:45:54 +03:00
|
|
|
/**
|
|
|
|
* Storage to use for this client.
|
|
|
|
*
|
|
|
|
* If a string is passed, it will be used as
|
|
|
|
* a name for an IndexedDB database.
|
|
|
|
*
|
|
|
|
* @default `"client.session"`
|
|
|
|
*/
|
|
|
|
storage?: string | ITelegramStorageProvider
|
|
|
|
}
|
|
|
|
|
|
|
|
export class BaseTelegramClient extends BaseTelegramClientBase {
|
|
|
|
constructor(opts: BaseTelegramClientOptions) {
|
|
|
|
super({
|
|
|
|
crypto: new WebCryptoProvider(),
|
2024-08-28 19:10:53 +03:00
|
|
|
transport: new WebSocketTransport(),
|
2024-09-18 02:10:36 +03:00
|
|
|
platform: new WebPlatform(),
|
2024-03-03 09:45:54 +03:00
|
|
|
...opts,
|
|
|
|
storage:
|
2024-08-13 04:53:07 +03:00
|
|
|
typeof opts.storage === 'string'
|
|
|
|
? new IdbStorage(opts.storage)
|
|
|
|
: opts.storage ?? new IdbStorage('client.session'),
|
2024-03-03 09:45:54 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Telegram client for use in Node.js
|
|
|
|
*/
|
|
|
|
export class TelegramClient extends TelegramClientBase {
|
|
|
|
constructor(opts: TelegramClientOptions) {
|
|
|
|
if ('client' in opts) {
|
|
|
|
super(opts)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
super({
|
|
|
|
client: new BaseTelegramClient(opts),
|
2024-07-24 13:41:33 +03:00
|
|
|
disableUpdates: opts.disableUpdates,
|
2024-08-21 20:25:05 +03:00
|
|
|
skipConversationUpdates: opts.skipConversationUpdates,
|
|
|
|
updates: opts.updates,
|
2024-03-03 09:45:54 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|