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

44
app.js
View file

@ -681,11 +681,12 @@ function generateDevices() {
function prepareScripts() {
generateDevices();
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 direct = array.filter(f => f.charAt(0) !== '@');
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
// scripts instead of serving a complete bundle
if (debug) {
const code = [
code.push(...[
oversion ? `self.debug_version='${oversion}';self.enable_service=${serviceWorker};` : '',
'self.debug=true;',
'(function() { let load = [ '
];
]);
direct.forEach(file => {
const vers = cachever[file] || oversion || dversion || version;
code.push(`"/${file.replace(/\\/g,'/')}?${vers}",`);
@ -719,24 +720,25 @@ function concatCode(array) {
inject.forEach(key => {
code.push(synth[key]);
});
return code.join('\n');
}
direct.forEach(file => {
let cached = getCachedFile(file, path => {
return minify(PATH.join(dir,file));
code = code.join('\n');
} else {
direct.forEach(file => {
let cached = getCachedFile(file, path => {
return minify(PATH.join(dir,file));
});
if (oversion) {
cached = `self.debug_version='${oversion}';self.enable_service=${serviceWorker};` + cached;
}
code.push(cached);
});
if (oversion) {
cached = `self.debug_version='${oversion}';self.enable_service=${serviceWorker};` + cached;
}
code.push(cached);
});
inject.forEach(key => {
code.push(synth[key]);
});
return code.join('');
inject.forEach(key => {
code.push(synth[key]);
});
code = code.join('');
}
// console.log(`SETTING synth[${key}]`);
// synth[key] = `self.${key} = "${Buffer.from(code).toString('base64')}";\n`;
return code;
}
function getCachedFile(file, fn) {

View file

@ -70,6 +70,10 @@ const client = exports({
newWorker() {
if (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 {
let _ = debug ? '_' : '';
return new Worker(`/code/kiri_work.js?${_}${gapp.version}`);