From a3daff891f393f738308ec0d9d28e2ab103b5dff Mon Sep 17 00:00:00 2001 From: alina sireneva Date: Sat, 25 Jan 2025 09:19:21 +0300 Subject: [PATCH] fix: do not use service worker for avatar --- packages/worker/src/sw/avatar.ts | 55 -------------------------- packages/worker/src/sw/main.ts | 11 ------ packages/worker/src/worker/telegram.ts | 47 ++++++++++++++++------ 3 files changed, 36 insertions(+), 77 deletions(-) delete mode 100644 packages/worker/src/sw/avatar.ts diff --git a/packages/worker/src/sw/avatar.ts b/packages/worker/src/sw/avatar.ts deleted file mode 100644 index fa7bcbd..0000000 --- a/packages/worker/src/sw/avatar.ts +++ /dev/null @@ -1,55 +0,0 @@ -import type { BaseTelegramClient } from '@mtcute/web' -import { downloadAsBuffer, getMe } from '@mtcute/web/methods.js' -import { createInternalClient } from '../utils/telegram.ts' -import { timeout } from '../utils/timeout.ts' -import { getCacheStorage } from './cache.ts' - -const clients = new Map() - -export async function clearAvatarCache(accountId: string) { - const cacheKey = new URL(`/sw/avatar/${accountId}`, location.origin) - await (await getCacheStorage()).delete(cacheKey) -} - -export async function handleAvatarRequest(accountId: string) { - const cacheKey = new URL(`/sw/avatar/${accountId}`, location.origin) - - const cache = await getCacheStorage() - try { - const cachedRes = await timeout(cache.match(cacheKey), 10000) - if (cachedRes) { - return cachedRes - } - } catch {} - - let client = clients.get(accountId) - if (!client) { - client = createInternalClient(accountId) - await client.prepare() - clients.set(accountId, client) - } - - const self = await getMe(client) - - if (!self.photo) { - const res = new Response('No photo', { status: 404 }) - await client.close() - await cache.put(cacheKey, res.clone()) - return res - } - - const buf = await downloadAsBuffer(client, self.photo.big) - await client.close() - - const res = new Response(buf, { - headers: { - 'Content-Type': 'image/jpeg', - 'Cache-Control': 'public, max-age=86400', - 'Access-Control-Allow-Origin': '*', - }, - }) - - await cache.put(cacheKey, res.clone()) - - return res -} diff --git a/packages/worker/src/sw/main.ts b/packages/worker/src/sw/main.ts index 64f8ca7..5d55f42 100644 --- a/packages/worker/src/sw/main.ts +++ b/packages/worker/src/sw/main.ts @@ -1,6 +1,5 @@ import { unknownToError } from '@fuman/utils' import { IS_SAFARI } from '../utils/env.ts' -import { clearAvatarCache, handleAvatarRequest } from './avatar.ts' import { requestCache } from './cache.ts' import { type DownloadFileParams, handleDownload, handlePort } from './download/handler.ts' import { clearCache, handleRuntimeRequest } from './runtime.ts' @@ -9,11 +8,6 @@ import { forgetAllScripts, forgetScript, uploadScript } from './scripts.ts' declare const self: ServiceWorkerGlobalScope async function handleSwRequest(_req: Request, url: URL): Promise { - if (url.pathname.startsWith('/sw/avatar/')) { - const accountId = url.pathname.split('/')[3] - return handleAvatarRequest(accountId) - } - if (url.pathname.startsWith('/sw/runtime/')) { return handleRuntimeRequest(url) } @@ -62,7 +56,6 @@ self.onoffline = self.ononline = () => { export type SwMessage = | { event: 'UPLOAD_SCRIPT', name: string, files: Record } | { event: 'FORGET_SCRIPT', name: string } - | { event: 'CLEAR_AVATAR_CACHE', accountId: string } | { event: 'CLEAR_CACHE' } | { event: 'DOWNLOAD_FILE', id: string, params: DownloadFileParams, port: MessagePort } @@ -81,10 +74,6 @@ async function handleMessage(msg: SwMessage) { await forgetAllScripts() break } - case 'CLEAR_AVATAR_CACHE': { - clearAvatarCache(msg.accountId) - break - } } } diff --git a/packages/worker/src/worker/telegram.ts b/packages/worker/src/worker/telegram.ts index 1cb6f5e..db3133f 100644 --- a/packages/worker/src/worker/telegram.ts +++ b/packages/worker/src/worker/telegram.ts @@ -4,13 +4,11 @@ import type { CustomApiFields, TelegramAccount } from '../store/accounts.ts' import { assert, hex } from '@fuman/utils' import { DC_MAPPING_PROD, DC_MAPPING_TEST } from '@mtcute/convert' import { tl } from '@mtcute/web' -import { checkPassword, getMe, resendCode, sendCode, signIn, signInBot, signInQr } from '@mtcute/web/methods.js' +import { checkPassword, downloadAsBuffer, getMe, resendCode, sendCode, signIn, signInBot, signInQr } from '@mtcute/web/methods.js' import { readStringSession } from '@mtcute/web/utils.js' import { nanoid } from 'nanoid' import { renderSVG } from 'uqr' import { $accounts, $activeAccountId } from '../store/accounts.ts' -import { swInvokeMethod } from '../sw/client.ts' -import { waitForServiceWorkerInit } from '../sw/register.ts' import { createInternalClient, deleteAccount, importAccount } from '../utils/telegram.ts' import { emitEvent } from './utils.ts' @@ -40,6 +38,8 @@ function getTmpClient(accountId: string): [BaseTelegramClient, () => Promise { if (it.id === accountId) { return {