WIP TOFU BOOTLOADER

This commit is contained in:
ansuz 2021-01-29 15:09:44 +05:30
parent 01cb64d07d
commit 65dd8f1d6a
6 changed files with 82 additions and 22 deletions

View file

@ -69,14 +69,48 @@ define([
require([document.querySelector('script[data-bootload]').getAttribute('data-bootload')]);
};
var urlArgs = RequireConfig().urlArgs;
var sw = window.navigator.serviceWorker;
// If the browser doesn't support service workers, just start loading normally
if (!sw) { return void load(); }
// TOFU
// calling back 'true' means it's safe to proceed
// otherwise load the old version
var TOFU_KEY = 'TOFU_URL_ARGS';
var checkVersion = function (msg, cb) {
var newUrlArgs = urlArgs;
var k = TOFU_KEY;
try {
var oldUrlArgs = localStorage.getItem(k);
if (!oldUrlArgs) {
//localStorage.setItem(k, newUrlArgs);
return void cb(true);
}
if (oldUrlArgs === newUrlArgs) {
return void cb(true);
}
// we don't have our fancy loading screen or other UI, so use window.confirm
return void setTimeout(function () {
var answer = window.confirm(msg);
//localStorage.setItem(k, newUrlArgs);
return void cb(answer);
});
} catch (err) {
localStorage.removeItem(k);
return void cb(false);
}
};
var loadServiceWorker = function (args, offline) {
var path = '/sw.js?' + args; // + (offline ? '&offline=1': ''); // XXX wrong way to do offline...
try {
sw
.register('/sw.js?'
+ RequireConfig().urlArgs
, { scope: '/' })
.register(path, { scope: '/' })
.then(function (reg) {
// XXX tell the service worker if it should stay offline...
localStorage.setItem(TOFU_KEY, args);
console.log("service-worker registered", reg);
load();
})
@ -88,5 +122,23 @@ define([
console.error(e);
load();
}
};
var msg = "ok to load a new version of cryptpad (" + urlArgs + ")?";
checkVersion(msg, function (consent) {
var consentfulWorker = urlArgs;
var offline = false;
//console.log(urlArgs);
if (!consent) {
consentfulWorker = localStorage.getItem(TOFU_KEY);
offline = true;
//return void loadServiceWorker(localStorage.getItem(TOFU_KEY), true);
} else {
console.error("THE USER CONSENTED TO THE UPDATE");
}
window.consentfulWorker = consentfulWorker;
loadServiceWorker(consentfulWorker, offline);
});
});

View file

@ -1932,7 +1932,7 @@ define([
var CRYPTPAD_VERSION = 'cryptpad-version';
var currentVersion = localStorage[CRYPTPAD_VERSION];
var updateLocalVersion = function (newUrlArgs) {
var updateLocalVersion = function (newUrlArgs) { // XXX
// Check for CryptPad updates
var urlArgs = newUrlArgs || (Config.requireConf ? Config.requireConf.urlArgs : null);
if (!urlArgs) { return; }
@ -2089,7 +2089,7 @@ define([
STORE_READY: onStoreReady,
// Network
NETWORK_DISCONNECT: common.onNetworkDisconnect.fire,
NETWORK_RECONNECT: function (data) {
NETWORK_RECONNECT: function (data) { // XXX reconnect
require(['/api/config?' + (+new Date())], function (NewConfig) {
var update = updateLocalVersion(NewConfig.requireConf && NewConfig.requireConf.urlArgs);
if (update) {

View file

@ -2659,7 +2659,7 @@ define([
if (typeof(cb) === 'function') { cb(returned); }
store.offline = false;
sendDriveEvent('NETWORK_RECONNECT'); // Tell inner that we're now online
sendDriveEvent('NETWORK_RECONNECT'); // Tell inner that we're now online // XXX
broadcast([], "UPDATE_METADATA");
broadcast([], "STORE_READY", returned);
@ -2826,7 +2826,7 @@ define([
});
rt.proxy.on('reconnect', function () {
store.offline = false;
sendDriveEvent('NETWORK_RECONNECT');
sendDriveEvent('NETWORK_RECONNECT'); // XXX reconnect
broadcast([], "UPDATE_METADATA");
});

View file

@ -60,7 +60,7 @@ define([
try {
sw
.register('/sw.js?'
+ RequireConfig().urlArgs
+ RequireConfig().urlArgs // XXX should match the query string provided by outer
, { scope: '/' })
.then(function (reg) {
console.log("service-worker registered", reg);

View file

@ -12,6 +12,7 @@ define([
var requireConfig = RequireConfig();
var lang = Messages._languageUsed;
var req = {
worker: window.consentfulWorker,
cfg: requireConfig,
req: [ '/common/loading.js' ],
pfx: window.location.origin,
@ -30,6 +31,7 @@ define([
}
}
// XXX load with cached URL args via window.consentfulWorker
document.getElementById('sbox-iframe').setAttribute('src',
ApiConfig.httpSafeOrigin + (pathname || window.location.pathname) + 'inner.html?' +
requireConfig.urlArgs + '#' + encodeURIComponent(JSON.stringify(req)));

View file

@ -1,6 +1,12 @@
/* jshint esversion: 6 */
var version = self.location.search || "DEFAULT";
//console.log('worker location:', self.location);
console.info('SW VERSION', version);
var params = new URLSearchParams(version);
var OFFLINE = params.has('offline');
console.info("SW OFFLINE?", OFFLINE);
var filesToCache = [
'/common/sframe-app-outer.js',