add maybe future code for loading workers from blobs

This commit is contained in:
Stewart Allen 2025-06-27 16:49:04 -04:00
commit 87137e44d1
2 changed files with 27 additions and 21 deletions

22
app.js
View file

@ -681,11 +681,12 @@ function generateDevices() {
function prepareScripts() { function prepareScripts() {
generateDevices(); generateDevices();
for (let key of Object.keys(script)) { for (let key of Object.keys(script)) {
code[key] = concatCode(script[key]); code[key] = concatCode(key);
} }
} }
function concatCode(array) { function concatCode(key) {
let array = script[key];
let code = []; let code = [];
let direct = array.filter(f => f.charAt(0) !== '@'); let direct = array.filter(f => f.charAt(0) !== '@');
let inject = array.filter(f => f.charAt(0) === '@').map(f => f.substring(1)); let inject = array.filter(f => f.charAt(0) === '@').map(f => f.substring(1));
@ -695,11 +696,11 @@ function concatCode(array) {
// in debug mode, the script should load dependent // in debug mode, the script should load dependent
// scripts instead of serving a complete bundle // scripts instead of serving a complete bundle
if (debug) { if (debug) {
const code = [ code.push(...[
oversion ? `self.debug_version='${oversion}';self.enable_service=${serviceWorker};` : '', oversion ? `self.debug_version='${oversion}';self.enable_service=${serviceWorker};` : '',
'self.debug=true;', 'self.debug=true;',
'(function() { let load = [ ' '(function() { let load = [ '
]; ]);
direct.forEach(file => { direct.forEach(file => {
const vers = cachever[file] || oversion || dversion || version; const vers = cachever[file] || oversion || dversion || version;
code.push(`"/${file.replace(/\\/g,'/')}?${vers}",`); code.push(`"/${file.replace(/\\/g,'/')}?${vers}",`);
@ -719,9 +720,8 @@ function concatCode(array) {
inject.forEach(key => { inject.forEach(key => {
code.push(synth[key]); code.push(synth[key]);
}); });
return code.join('\n'); code = code.join('\n');
} } else {
direct.forEach(file => { direct.forEach(file => {
let cached = getCachedFile(file, path => { let cached = getCachedFile(file, path => {
return minify(PATH.join(dir,file)); return minify(PATH.join(dir,file));
@ -731,12 +731,14 @@ function concatCode(array) {
} }
code.push(cached); code.push(cached);
}); });
inject.forEach(key => { inject.forEach(key => {
code.push(synth[key]); code.push(synth[key]);
}); });
code = code.join('');
return code.join(''); }
// console.log(`SETTING synth[${key}]`);
// synth[key] = `self.${key} = "${Buffer.from(code).toString('base64')}";\n`;
return code;
} }
function getCachedFile(file, fn) { function getCachedFile(file, fn) {

View file

@ -70,6 +70,10 @@ const client = exports({
newWorker() { newWorker() {
if (self.createWorker) { if (self.createWorker) {
return self.createWorker(); return self.createWorker();
} else if (self.kiri_work) {
const work = atob(self.kiri_work);
const blob = new Blob([ work ], { type: 'application/javascript' });
return new Worker(URL.createObjectURL(blob));
} else { } else {
let _ = debug ? '_' : ''; let _ = debug ? '_' : '';
return new Worker(`/code/kiri_work.js?${_}${gapp.version}`); return new Worker(`/code/kiri_work.js?${_}${gapp.version}`);