128 lines
4.7 KiB
JavaScript
128 lines
4.7 KiB
JavaScript
/**
|
|
* Maestri Remote — Live OTA Bootstrapper
|
|
* Executa assets locais em cache e atualiza silenciosamente em background do Pod K3s.
|
|
*/
|
|
|
|
const REMOTE_SERVER = 'https://remote.winicomw.ddnsfree.com';
|
|
|
|
(function initLiveBootstrapper() {
|
|
// 1. Atualiza o DOM com o body mais recente se houver em cache
|
|
try {
|
|
const cachedBody = localStorage.getItem('maestri_ota_body');
|
|
if (cachedBody && cachedBody.length > 200) {
|
|
document.body.innerHTML = cachedBody;
|
|
}
|
|
} catch (e) {}
|
|
|
|
// 2. Executa o código da aplicação
|
|
const cachedJs = localStorage.getItem('maestri_ota_js');
|
|
if (cachedJs && cachedJs.length > 500) {
|
|
console.log('[OTA Bootstrapper] Carregando bundle atualizado do armazenamento local...');
|
|
try {
|
|
const script = document.createElement('script');
|
|
script.textContent = cachedJs;
|
|
document.body.appendChild(script);
|
|
} catch (err) {
|
|
console.error('[OTA Bootstrapper] Falha ao executar bundle em cache, usando fallback:', err);
|
|
loadFallbackScript();
|
|
}
|
|
} else {
|
|
loadFallbackScript();
|
|
}
|
|
|
|
function loadFallbackScript() {
|
|
const script = document.createElement('script');
|
|
const isApp = window.location.protocol === 'capacitor:' || window.location.protocol === 'file:' || !window.location.host || window.location.host === 'localhost';
|
|
script.src = isApp ? `${REMOTE_SERVER}/app.js?v=${Date.now()}` : `/app.js?v=${Date.now()}`;
|
|
document.body.appendChild(script);
|
|
}
|
|
|
|
// 3. Em background: Consulta se há atualização no servidor
|
|
setTimeout(() => checkForOtaUpdate(false), 1500);
|
|
setInterval(() => checkForOtaUpdate(false), 20000);
|
|
|
|
async function checkForOtaUpdate(force = false) {
|
|
try {
|
|
// Usa querystring com timestamp para evitar preflight OPTIONS e contornar CORS
|
|
const res = await fetch(`${REMOTE_SERVER}/api/version?src=ota&t=${Date.now()}`);
|
|
if (!res.ok) return false;
|
|
|
|
const manifest = await res.json();
|
|
const localBuild = localStorage.getItem('maestri_ota_build_id');
|
|
|
|
if (force || (manifest.buildId && manifest.buildId !== localBuild)) {
|
|
console.log(`[OTA Bootstrapper] Nova versão detectada: ${manifest.version} (${manifest.buildId}). Baixando...`);
|
|
|
|
// Baixa os assets atualizados do servidor (JS, CSS e HTML) com timestamp anti-cache
|
|
const [jsRes, cssRes, htmlRes] = await Promise.all([
|
|
fetch(`${REMOTE_SERVER}/app.js?b=${manifest.buildId}&t=${Date.now()}`),
|
|
fetch(`${REMOTE_SERVER}/style.css?b=${manifest.buildId}&t=${Date.now()}`),
|
|
fetch(`${REMOTE_SERVER}/index.html?b=${manifest.buildId}&t=${Date.now()}`)
|
|
]);
|
|
|
|
if (jsRes.ok && cssRes.ok) {
|
|
const newJs = await jsRes.text();
|
|
const newCss = await cssRes.text();
|
|
|
|
if (htmlRes.ok) {
|
|
const fullHtml = await htmlRes.text();
|
|
const bodyMatch = fullHtml.match(/<body[^>]*>([\s\S]*)<\/body>/i);
|
|
if (bodyMatch && bodyMatch[1]) {
|
|
const cleanBody = bodyMatch[1].replace(/<script[^>]*ota-bootstrapper\.js[^>]*><\/script>/gi, '');
|
|
localStorage.setItem('maestri_ota_body', cleanBody);
|
|
}
|
|
}
|
|
|
|
if (newJs.length > 500 && newCss.length > 200) {
|
|
localStorage.setItem('maestri_ota_js', newJs);
|
|
localStorage.setItem('maestri_ota_css', newCss);
|
|
localStorage.setItem('maestri_ota_build_id', manifest.buildId);
|
|
localStorage.setItem('maestri_ota_version', manifest.version);
|
|
|
|
console.log('[OTA Bootstrapper] Bundle completo (HTML+JS+CSS) atualizado no aparelho!');
|
|
showOtaToast(`Atualização v${manifest.version} aplicada! Reiniciando...`);
|
|
|
|
setTimeout(() => {
|
|
window.location.reload();
|
|
}, 1800);
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
} catch (err) {
|
|
console.warn('[OTA Bootstrapper] Checagem de atualização em background:', err.message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
window.checkForOtaUpdate = checkForOtaUpdate;
|
|
|
|
function showOtaToast(message) {
|
|
let toast = document.getElementById('ota-toast');
|
|
if (!toast) {
|
|
toast = document.createElement('div');
|
|
toast.id = 'ota-toast';
|
|
toast.style.cssText = `
|
|
position: fixed;
|
|
bottom: 20px;
|
|
left: 50%;
|
|
transform: translateX(-50%);
|
|
background: #0284c7;
|
|
color: white;
|
|
padding: 0.75rem 1.25rem;
|
|
border-radius: 9999px;
|
|
font-size: 0.85rem;
|
|
font-weight: 600;
|
|
z-index: 99999;
|
|
box-shadow: 0 4px 20px rgba(0,0,0,0.5);
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 0.5rem;
|
|
`;
|
|
document.body.appendChild(toast);
|
|
}
|
|
toast.innerText = message;
|
|
}
|
|
})();
|