2023-12-23 22:00:20 +03:00
|
|
|
// eslint-disable-next-line no-restricted-imports
|
|
|
|
import { join } from 'path'
|
|
|
|
|
2024-02-03 20:14:03 +03:00
|
|
|
import { MaybePromise, MemoryStorage } from '@mtcute/core'
|
2024-03-01 18:16:07 +03:00
|
|
|
import { setPlatform } from '@mtcute/core/platform.js'
|
2023-12-23 22:00:20 +03:00
|
|
|
import { LogManager, sleep } from '@mtcute/core/utils.js'
|
2024-04-25 15:37:04 +03:00
|
|
|
import { NodePlatform, SqliteStorage, TcpTransport } from '@mtcute/node'
|
|
|
|
import { NodeCryptoProvider } from '@mtcute/node/utils.js'
|
2023-10-16 19:23:53 +03:00
|
|
|
|
2024-02-03 13:26:21 +03:00
|
|
|
export const getApiParams = (storage?: string) => {
|
2023-10-16 19:23:53 +03:00
|
|
|
if (!process.env.API_ID || !process.env.API_HASH) {
|
|
|
|
throw new Error('API_ID and API_HASH env variables must be set')
|
|
|
|
}
|
|
|
|
|
2024-03-01 18:16:07 +03:00
|
|
|
setPlatform(new NodePlatform())
|
|
|
|
|
2023-10-16 19:23:53 +03:00
|
|
|
return {
|
|
|
|
apiId: parseInt(process.env.API_ID),
|
|
|
|
apiHash: process.env.API_HASH,
|
|
|
|
testMode: true,
|
2023-12-23 22:00:20 +03:00
|
|
|
storage: storage ? new SqliteStorage(join(__dirname, storage)) : new MemoryStorage(),
|
|
|
|
logLevel: LogManager.VERBOSE,
|
2024-03-01 18:16:07 +03:00
|
|
|
transport: () => new TcpTransport(),
|
|
|
|
crypto: new NodeCryptoProvider(),
|
2023-10-16 19:23:53 +03:00
|
|
|
}
|
|
|
|
}
|
2023-12-23 22:00:20 +03:00
|
|
|
|
2024-02-03 20:14:03 +03:00
|
|
|
export async function waitFor(condition: () => MaybePromise<void>, timeout = 5000): Promise<void> {
|
2023-12-23 22:00:20 +03:00
|
|
|
const start = Date.now()
|
|
|
|
let lastError
|
|
|
|
|
|
|
|
while (Date.now() - start < timeout) {
|
|
|
|
try {
|
|
|
|
await condition()
|
|
|
|
|
|
|
|
return
|
|
|
|
} catch (e) {
|
|
|
|
lastError = e
|
|
|
|
await sleep(100)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw lastError
|
|
|
|
}
|