2024-12-06 00:14:29 +03:00
|
|
|
import type { BaseTelegramClientOptions, MaybePromise } from 'mtcute'
|
2024-12-03 09:55:37 +03:00
|
|
|
|
|
|
|
import { join } from 'node:path'
|
2024-12-06 00:14:29 +03:00
|
|
|
import { fileURLToPath } from 'node:url'
|
|
|
|
import { sleep } from '@fuman/utils'
|
|
|
|
import { MemoryStorage, SqliteStorage } from 'mtcute'
|
|
|
|
import { LogManager } from 'mtcute/utils.js'
|
|
|
|
|
|
|
|
export const RUNTIME_DIR: string = fileURLToPath(new URL('../_runtime', import.meta.url))
|
|
|
|
export const IS_DENO = 'Deno' in globalThis
|
|
|
|
export const IS_BUN = 'Bun' in globalThis
|
|
|
|
export const IS_NODE = !IS_DENO && !IS_BUN
|
2023-10-16 19:23:53 +03:00
|
|
|
|
2024-08-21 11:05:07 +03:00
|
|
|
export function getApiParams(storage?: string): BaseTelegramClientOptions {
|
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')
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2024-08-13 04:53:07 +03:00
|
|
|
apiId: Number.parseInt(process.env.API_ID),
|
2023-10-16 19:23:53 +03:00
|
|
|
apiHash: process.env.API_HASH,
|
|
|
|
testMode: true,
|
2024-12-06 00:14:29 +03:00
|
|
|
storage: storage ? new SqliteStorage(join(RUNTIME_DIR, storage)) : new MemoryStorage(),
|
2023-12-23 22:00:20 +03:00
|
|
|
logLevel: LogManager.VERBOSE,
|
2023-10-16 19:23:53 +03:00
|
|
|
}
|
|
|
|
}
|
2023-12-23 22:00:20 +03:00
|
|
|
|
2025-01-10 05:43:34 +03:00
|
|
|
export async function waitFor(condition: () => MaybePromise<void>, timeout = 15000): 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
|
|
|
|
}
|