mirror of
https://github.com/danog/telegram-tt.git
synced 2024-12-11 16:49:44 +01:00
2 lines
7.3 KiB
JavaScript
2 lines
7.3 KiB
JavaScript
(()=>{"use strict";const e="tt-media-progressive",t="tt-assets",n=("undefined"!=typeof window&&window.innerHeight,Math.round(425),new Set(["newMessage","newScheduledMessage","deleteMessages","deleteScheduledMessages","deleteHistory"]),new Set(["image/png","image/gif","image/jpeg"])),s=new Set(["video/mp4"]);new Set(["audio/mp3","audio/ogg","audio/wav","audio/mpeg","audio/flac","audio/aac","audio/m4a","audio/mp4","audio/x-m4a"]),new Set([...n,...s]),new Set(["AU","BD","CA","CO","EG","HN","IE","IN","JO","MX","MY","NI","NZ","PH","PK","SA","SV","US"]);const a=e=>new Promise((t=>{setTimeout((()=>t()),e)})),i=524288,o=new Map;async function r(e,t){const n=t.url.includes("/download/")?(await self.clients.matchAll()).find((e=>"window"===e.type&&"top-level"===e.frameType)):await self.clients.get(e.clientId);if(!n)return;const s=(e=>{let t;do{t=String(Math.random()).replace("0.","id")}while(e.hasOwnProperty(t));return t})(o),i={};let r=!1;const c=Promise.race([a(6e4).then((()=>r?void 0:Promise.reject(new Error("ERROR_PART_TIMEOUT")))),new Promise(((e,t)=>{Object.assign(i,{resolve:e,reject:t})}))]);return o.set(s,i),c.catch((()=>{})).finally((()=>{o.delete(s),r=!0})),n.postMessage({type:"requestPart",messageId:s,params:t}),c}self.addEventListener("message",(e=>{const{type:t,messageId:n,result:s}=e.data;if("partResponse"===t){const e=o.get(n);e&&e.resolve(s)}}));const c=1048576;class l{constructor(){var e,t;t=void 0,(e="queue")in this?Object.defineProperty(this,e,{value:t,enumerable:!0,configurable:!0,writable:!0}):this[e]=t,this.queue=[]}push(e){this.queue.push(e)}async pop(){return await this.queue.shift()}get size(){return this.queue.length}}async function d(e){const n=await async function(n,s){let i=!1;try{return await Promise.race([a(3e3).then((()=>i?void 0:Promise.reject(new Error("TIMEOUT")))),(async()=>{const n=await self.caches.open(t),s=await n.match(e.request);return{cache:n,cached:s}})()])}catch(e){return void console.error(e)}finally{i=!0}}(),{cache:s,cached:i}=n||{};if(s&&i){if(i.ok)return i;await s.delete(e.request)}const o=await fetch(e.request);return o.ok&&s&&s.put(e.request,o.clone()),o}var u,f;(f=u||(u={})).True="1",f.False="0";let p=(new Date).valueOf();const h=new Set,w={};function g(e){return e.custom.from_id?e.custom.from_id:e.custom.chat_id||e.custom.channel_id?`-${e.custom.chat_id||e.custom.channel_id}`:void 0}function m(e){if(e.custom.msg_id)return parseInt(e.custom.msg_id,10)}async function y(){const e=new URL(self.registration.scope).origin;return(await self.clients.matchAll({type:"window"})).filter((t=>new URL(t.url).origin===e))}async function v(e){const t=(await y())[0];t&&t.postMessage({type:"playNotificationSound",payload:{id:e}})}function I(e){let{chatId:t,messageId:n,body:s,title:a,icon:i,reaction:o,shouldReplaceHistory:r}=e;const c=(new Date).valueOf()-p<1e3,l={body:s,data:{chatId:t,messageId:n,reaction:o,count:1,shouldReplaceHistory:r},icon:i||"icon-192x192.png",badge:"icon-192x192.png",tag:String(c?0:t||0),vibrate:[200,100,200]};return Promise.all([o?void 0:v(String(n)||t||""),self.registration.showNotification(a,l)])}async function R(e,t){if(t.chatId&&(e.postMessage({type:"focusMessage",payload:t}),!e.focused))try{await e.focus()}catch(e){}}self.addEventListener("sync",(()=>{p=Date.now()}));const b=/[\da-f]{20}.*\.(js|css|woff2?|svg|png|jpg|jpeg|tgs|json|wasm)$/;self.addEventListener("install",(e=>{e.waitUntil(self.skipWaiting())})),self.addEventListener("activate",(e=>{e.waitUntil(Promise.race([a(3e3),Promise.all([self.caches.delete(t),self.clients.claim()])]))})),self.addEventListener("fetch",(t=>{const{url:n}=t.request;return n.includes("/progressive/")?(t.respondWith(async function(t){const{url:n}=t.request,s=t.request.headers.get("range"),a=/^bytes=(\d+)-(\d+)?$/g.exec(s||""),o=Number(a[1]);let c=Number(a[2]);if((!c||c-o+1>i)&&(c=o+i-1),0===o&&1===c){const e=t.request.url.match(/fileSize=(\d+)&mimeType=([\w/]+)/),n=e&&Number(e[1]),s=null==e?void 0:e[2];if(n&&s)return new Response(new Uint8Array(2).buffer,{status:206,statusText:"Partial Content",headers:[["Content-Range",`bytes 0-1/${n}`],["Accept-Ranges","bytes"],["Content-Length","2"],["Content-Type",s]]})}const l=`${n}?start=${o}&end=${c}`,[d,u]=await async function(t){const n=await self.caches.open(e);return Promise.all([n.match(`${t}&type=arrayBuffer`).then((e=>e?e.arrayBuffer():void 0)),n.match(`${t}&type=headers`).then((e=>e?e.json():void 0))])}(l);if(d)return new Response(d,{status:206,statusText:"Partial Content",headers:u});let f;try{f=await r(t,{url:n,start:o,end:c})}catch(e){}if(!f)return new Response("",{status:500,statusText:"Failed to fetch progressive part"});const{arrayBuffer:p,fullSize:h,mimeType:w}=f,g=Math.min(c-o+1,p.byteLength);c=o+g-1;const m=p.slice(0,g),y=[["Content-Range",`bytes ${o}-${c}/${h}`],["Accept-Ranges","bytes"],["Content-Length",String(g)],["Content-Type",w]];return g<=524288&&c<2097151&&async function(t,n,s){const a=await self.caches.open(e);Promise.all([a.put(new Request(`${t}&type=arrayBuffer`),new Response(n)),a.put(new Request(`${t}&type=headers`),new Response(JSON.stringify(s)))])}(l,m,y),new Response(m,{status:206,statusText:"Partial Content",headers:y})}(t)),!0):n.includes("/download/")?(t.respondWith(async function(e){const{url:t}=e.request;let n;try{n=await r(e,{url:t,start:0,end:65536})}catch(e){}if(!n)return new Response("",{status:500,statusText:"Failed to fetch file to download"});const s=e.request.url.match(/filename=(.*)/),a=s?`filename="${decodeURIComponent(s[1])}"`:"",{fullSize:i,mimeType:o}=n,d=[["Content-Length",String(i)],["Content-Type",o],["Content-Disposition",`attachment; ${a}`]],u=new l,f=n=>(u.push(r(e,{url:t,start:n,end:n+c-1}).then((e=>null==e?void 0:e.arrayBuffer))),n+c);let p=0;const h=new ReadableStream({start(){for(let e=0;e<8&&!(p>=i);e++)p=f(p)},async pull(e){const t=await u.pop();t?(e.enqueue(new Uint8Array(t)),t.byteLength<c?e.close():p<i&&(p=f(p))):e.close()}});return new Response(h,{status:200,statusText:"OK",headers:d})}(t)),!0):!(!n.startsWith("http")||!n.match(b)||(t.respondWith(d(t)),0))})),self.addEventListener("push",(function(e){const t=function(e){try{return e.data.json()}catch(e){return}}(e);if(!t||t.mute===u.True)return;const n=function(e){return{chatId:g(e),messageId:m(e),title:e.title||"Telegram WebZ",body:e.description}}(t);h.has(n.messageId)?h.delete(n.messageId):e.waitUntil(I(n))})),self.addEventListener("notificationclick",(function(e){const t=self.registration.scope;e.notification.close();const{data:n}=e.notification;e.waitUntil((async()=>{const e=await y();if(await Promise.all(e.map((e=>(w[e.id]=n,R(e,n))))),self.clients.openWindow&&!(e.length>0)){w[0]=n;try{const e=await self.clients.openWindow(t);e&&(w[e.id]=n)}catch(e){}}})())})),self.addEventListener("message",(function(e){if(!e.data)return;const t=e.source;if("clientReady"===e.data.type){const n=w[t.id]||w[0];n&&(delete w[t.id],delete w[0],e.waitUntil(R(t,n)))}if("showMessageNotification"===e.data.type){const t=e.data.payload;e.waitUntil((async()=>(t.chatId&&(await self.registration.getNotifications({tag:t.chatId})).forEach((e=>e.close())),h.add(t.messageId),I(t)))())}"closeMessageNotifications"===e.data.type&&e.waitUntil(async function(e){let{chatId:t,lastReadInboxMessageId:n}=e;const s=await self.registration.getNotifications(),a=n||Number.MAX_VALUE;s.forEach((e=>{("0"===e.tag||e.data.chatId===t&&e.data.messageId<=a)&&e.close()}))}(e.data.payload))}))})();
|
|
//# sourceMappingURL=1516.d65ebe46f9458ef62bd5.js.map
|