Arch refactor #14

Merged
teidesu merged 7 commits from arch-refactor into master 2024-02-03 22:33:47 +03:00
14 changed files with 40 additions and 26 deletions
Showing only changes of commit 20a46c54a8 - Show all commits

View file

@ -1,4 +1,4 @@
const { MemoryStorage } = require('@mtcute/core/storage/memory.js') const { MemoryStorage } = require('@mtcute/core')
const { LogManager } = require('@mtcute/core/utils.js') const { LogManager } = require('@mtcute/core/utils.js')
exports.getApiParams = () => { exports.getApiParams = () => {

View file

@ -1,4 +1,4 @@
import { MemoryStorage } from '@mtcute/core/storage/memory.js' import { MemoryStorage } from '@mtcute/core'
import { LogManager } from '@mtcute/core/utils.js' import { LogManager } from '@mtcute/core/utils.js'
export const getApiParams = () => { export const getApiParams = () => {

View file

@ -2,7 +2,6 @@
"name": "mtcute-e2e", "name": "mtcute-e2e",
"private": true, "private": true,
"dependencies": { "dependencies": {
"@mtcute/client": "*",
"@mtcute/core": "*", "@mtcute/core": "*",
"@mtcute/crypto-node": "*", "@mtcute/crypto-node": "*",
"@mtcute/dispatcher": "*", "@mtcute/dispatcher": "*",

View file

@ -1,7 +1,7 @@
import { expect } from 'chai' import { expect } from 'chai'
import { describe, it } from 'mocha' import { describe, it } from 'mocha'
import { MtUnsupportedError, TelegramClient } from '@mtcute/client' import { BaseTelegramClient, MtUnsupportedError, TelegramClient } from '@mtcute/core'
import { getApiParams } from '../utils.js' import { getApiParams } from '../utils.js'
@ -14,11 +14,12 @@ describe('1. authorization', function () {
this.timeout(300_000) this.timeout(300_000)
it('should authorize in default dc', async () => { it('should authorize in default dc', async () => {
const tg = new TelegramClient(getApiParams('dc2.session')) const base = new BaseTelegramClient(getApiParams('dc2.session'))
const tg = new TelegramClient({ client: base })
// reset storage just in case // reset storage just in case
await tg.storage.load?.() await base.mt.storage.load()
await tg.storage.reset(true) await base.storage.clear(true)
while (true) { while (true) {
const phone = `999662${getAccountId()}` const phone = `999662${getAccountId()}`
@ -45,11 +46,12 @@ describe('1. authorization', function () {
}) })
it('should authorize in dc 1', async () => { it('should authorize in dc 1', async () => {
const tg = new TelegramClient(getApiParams('dc1.session')) const base = new BaseTelegramClient(getApiParams('dc1.session'))
const tg = new TelegramClient({ client: base })
// reset storage just in case // reset storage just in case
await tg.storage.load?.() await base.mt.storage.load()
await tg.storage.reset(true) await base.mt.storage.clear(true)
while (true) { while (true) {
const phone = `999661${getAccountId()}` const phone = `999661${getAccountId()}`

View file

@ -1,7 +1,7 @@
import { expect } from 'chai' import { expect } from 'chai'
import { describe, it } from 'mocha' import { describe, it } from 'mocha'
import { TelegramClient } from '@mtcute/client' import { TelegramClient } from '@mtcute/core'
import { getApiParams } from '../utils.js' import { getApiParams } from '../utils.js'

View file

@ -3,7 +3,7 @@ import { expect } from 'chai'
import { createHash } from 'crypto' import { createHash } from 'crypto'
import { describe, it } from 'mocha' import { describe, it } from 'mocha'
import { FileDownloadLocation, TelegramClient, Thumbnail } from '@mtcute/client' import { FileDownloadLocation, TelegramClient, Thumbnail } from '@mtcute/core'
import { sleep } from '@mtcute/core/utils.js' import { sleep } from '@mtcute/core/utils.js'
import { getApiParams } from '../utils.js' import { getApiParams } from '../utils.js'

View file

@ -1,7 +1,7 @@
import { expect } from 'chai' import { expect } from 'chai'
import { describe, it } from 'mocha' import { describe, it } from 'mocha'
import { Message, TelegramClient } from '@mtcute/client' import { Message, TelegramClient } from '@mtcute/core'
import { getApiParams, waitFor } from '../utils.js' import { getApiParams, waitFor } from '../utils.js'

View file

@ -1,12 +1,11 @@
// eslint-disable-next-line no-restricted-imports // eslint-disable-next-line no-restricted-imports
import { join } from 'path' import { join } from 'path'
import { BaseTelegramClientOptions, MaybeAsync } from '@mtcute/core' import { MaybeAsync, MemoryStorage } from '@mtcute/core'
import { MemoryStorage } from '@mtcute/core/storage/memory.js'
import { LogManager, sleep } from '@mtcute/core/utils.js' import { LogManager, sleep } from '@mtcute/core/utils.js'
import { SqliteStorage } from '@mtcute/sqlite' import { SqliteStorage } from '@mtcute/sqlite'
export const getApiParams = (storage?: string): BaseTelegramClientOptions => { export const getApiParams = (storage?: string) => {
if (!process.env.API_ID || !process.env.API_HASH) { if (!process.env.API_ID || !process.env.API_HASH) {
throw new Error('API_ID and API_HASH env variables must be set') throw new Error('API_ID and API_HASH env variables must be set')
} }

View file

@ -684,6 +684,8 @@ on(name: string, handler: (...args: any[]) => void): this\n`)
'notifyLoggedOut', 'notifyLoggedOut',
'notifyChannelOpened', 'notifyChannelOpened',
'notifyChannelClosed', 'notifyChannelClosed',
'startUpdatesLoop',
'stopUpdatesLoop',
'call', 'call',
'importSession', 'importSession',
'exportSession', 'exportSession',

View file

@ -132,6 +132,14 @@ export class BaseTelegramClient implements ITelegramClient {
return this.updates?.notifyChannelClosed(channelId) ?? false return this.updates?.notifyChannelClosed(channelId) ?? false
} }
async startUpdatesLoop(): Promise<void> {
await this.updates?.startLoop()
}
async stopUpdatesLoop(): Promise<void> {
this.updates?.stopLoop()
}
/** /**
* Make an RPC call * Make an RPC call
* *

View file

@ -5909,6 +5909,12 @@ TelegramClient.prototype.notifyChannelOpened = function (...args) {
TelegramClient.prototype.notifyChannelClosed = function (...args) { TelegramClient.prototype.notifyChannelClosed = function (...args) {
return this._client.notifyChannelClosed(...args) return this._client.notifyChannelClosed(...args)
} }
TelegramClient.prototype.startUpdatesLoop = function (...args) {
return this._client.startUpdatesLoop(...args)
}
TelegramClient.prototype.stopUpdatesLoop = function (...args) {
return this._client.stopUpdatesLoop(...args)
}
TelegramClient.prototype.call = function (...args) { TelegramClient.prototype.call = function (...args) {
return this._client.call(...args) return this._client.call(...args)
} }

View file

@ -22,6 +22,8 @@ export interface ITelegramClient {
notifyLoggedOut(): Promise<void> notifyLoggedOut(): Promise<void>
notifyChannelOpened(channelId: number, pts?: number): Promise<boolean> notifyChannelOpened(channelId: number, pts?: number): Promise<boolean>
notifyChannelClosed(channelId: number): Promise<boolean> notifyChannelClosed(channelId: number): Promise<boolean>
startUpdatesLoop(): Promise<void>
stopUpdatesLoop(): Promise<void>
call<T extends tl.RpcMethod>( call<T extends tl.RpcMethod>(
message: MustEqual<T, tl.RpcMethod>, message: MustEqual<T, tl.RpcMethod>,
params?: RpcCallOptions, params?: RpcCallOptions,
@ -42,12 +44,6 @@ export interface ITelegramClient {
getPoolSize(kind: ConnectionKind, dcId?: number): Promise<number> getPoolSize(kind: ConnectionKind, dcId?: number): Promise<number>
getPrimaryDcId(): Promise<number> getPrimaryDcId(): Promise<number>
computeSrpParams( computeSrpParams(request: tl.account.RawPassword, password: string): Promise<tl.RawInputCheckPasswordSRP>
request: tl.account.RawPassword, computeNewPasswordHash(algo: tl.TypePasswordKdfAlgo, password: string): Promise<Uint8Array>
password: string,
): Promise<tl.RawInputCheckPasswordSRP>
computeNewPasswordHash(
algo: tl.TypePasswordKdfAlgo,
password: string,
): Promise<Uint8Array>
} }

View file

@ -38,9 +38,9 @@ export class TelegramStorageManager {
this.mt._serviceOptions, this.mt._serviceOptions,
) )
async clear() { async clear(withAuthKeys = false) {
await this.provider.peers.deleteAll() await this.provider.peers.deleteAll()
await this.provider.refMessages.deleteAll() await this.provider.refMessages.deleteAll()
await this.mt.clear() await this.mt.clear(withAuthKeys)
} }
} }

View file

@ -100,4 +100,6 @@ export class TelegramWorkerPort<Custom extends WorkerCustomMethods> implements I
readonly getPrimaryDcId = this._bind('getPrimaryDcId') readonly getPrimaryDcId = this._bind('getPrimaryDcId')
readonly computeSrpParams = this._bind('computeSrpParams') readonly computeSrpParams = this._bind('computeSrpParams')
readonly computeNewPasswordHash = this._bind('computeNewPasswordHash') readonly computeNewPasswordHash = this._bind('computeNewPasswordHash')
readonly startUpdatesLoop = this._bind('startUpdatesLoop')
readonly stopUpdatesLoop = this._bind('stopUpdatesLoop')
} }