change the way dependent scripts are loaded and how debug state propagates from server to client to worker

This commit is contained in:
Stewart Allen 2020-03-18 18:22:57 -04:00
commit 490cdcbce4
8 changed files with 93 additions and 182 deletions

View file

@ -1 +0,0 @@
../node_modules/n3d-threejs/index.js

View file

@ -1523,7 +1523,7 @@ var gs_kiri_init = exports;
UI.toolMetric.onclick = updateTool;
UI.toolType.onchange = updateTool;
$('kiri').onclick = API.help.show;
$('appname').onclick = API.help.show;
SPACE.platform.setSize(
settings().device.bedWidth,
@ -1694,36 +1694,40 @@ var gs_kiri_init = exports;
// if a language needs to load, the script is injected and loaded
// first. once this loads, or doesn't, the initialization begins
let lang_set = undefined;
let lang = SETUP.ln ? SETUP.ln[0] : SDB.getItem('kiri-lang');
// inject language script
if (SETUP.ln || SDB.getItem('kiri-lang')) {
let lang = SETUP.ln ? SETUP.ln[0] : SDB.getItem('kiri-lang');
if (lang !== 'en') {
lang_set = lang;
let scr = DOC.createElement('script');
// scr.setAttribute('defer',true);
scr.setAttribute('src',`/kiri/lang/${lang}.js`);
DOC.body.appendChild(scr);
STATS.set('ll',lang);
scr.onload = function() {
KIRI.lang.set(lang);
init_one();
};
scr.onerror = function(err) {
console.log({language_load_error: err, lang})
init_one();
}
// inject language script if not english
if (lang && lang !== 'en' && lang !== 'en-us') {
lang_set = lang;
let scr = DOC.createElement('script');
// scr.setAttribute('defer',true);
scr.setAttribute('src',`/kiri/lang/${lang}.js`);
(DOC.body || DOC.head).appendChild(scr);
STATS.set('ll',lang);
scr.onload = function() {
KIRI.lang.set(lang);
init_one();
};
scr.onerror = function(err) {
console.log({language_load_error: err, lang})
init_one();
}
} else {
// set to browser default
KIRI.lang.set();
}
// set to browser default which will be overridden
// by any future script loads (above)
KIRI.lang.set();
// schedule init_one to run after all page content is loaded
// unless a languge script is loading first, in which case it
// will call init once it's done (or failed)
if (!lang_set) {
SPACE.addEventListener(DOC, 'DOMContentLoaded', init_one, false);
if (document.readyState === 'loading') {
SPACE.addEventListener(DOC, 'DOMContentLoaded', init_one);
} else {
// happens in debug mode when scripts are chain loaded
init_one();
}
}
})();

View file

@ -8,9 +8,6 @@ let loc = self.location,
host = loc.hostname,
port = loc.port,
proto = loc.protocol,
pre = '',
// pre = host.indexOf(".space") > 0 || host === "localhost" ?
// proto + "//" + host + ":" + port : "",
time = function() { return new Date().getTime() },
KIRI = self.kiri,
BASE = self.base,
@ -19,13 +16,6 @@ let loc = self.location,
slicing = {},
worker = null;
// new moto.Ajax(function(body) {
// console.log({body:body});
// let blob = new Blob([body], {type : 'application/json'});
// worker = new Worker(URL.createObjectURL(blob));
// console.log({worker})
// }).request(pre + "/code/worker.js/123");
function send(fn, data, onreply, async, zerocopy) {
let seq = seqid++;
@ -59,7 +49,7 @@ KIRI.work = {
slicing = {};
running = {};
worker = new Worker(pre + "/code/worker.js/" + exports.VERSION);
worker = new Worker(`/code/worker.js`);
worker.onmessage = function(e) {
let now = time(),

View file

@ -1,69 +1,26 @@
/** Copyright Stewart Allen -- All Rights Reserved */
"use strict";
// this code runs in the web worker
module = { exports: {} };
importScripts(`/code/work.js`);
let loc = self.location,
ver = exports.VERSION,
time = function() { return new Date().getTime() };
host = loc.hostname,
debug = host === 'debug' || host === 'localhost';
console.log(`kiri | init work | ${ver}${debug ? ' | debug' : ''}`);
// when running the server in 'web' mode, the obfuscated code is served as a
// unified ball via /code/work.js -- otherwise, map "localhost" to "debug"
// to debug unobfuscated code. if not, /js/ paths will 404.
if (debug) {
try {
[
"license",
"ext-n3d", // for math/geometry only
"ext-clip2",
"add-array",
"add-three",
"geo",
"geo-point",
"geo-debug",
"geo-bounds",
"geo-line",
"geo-slope",
"geo-polygon",
"geo-polygons",
"geo-gyroid",
"kiri-fill",
"kiri-slice",
"kiri-slicer",
"kiri-driver-fdm",
"kiri-driver-cam",
"kiri-driver-laser",
"kiri-widget",
"kiri-pack",
"kiri-print",
"kiri-codec"
].forEach(function(scr) {
importScripts(`/js/${scr}.js/${ver}`);
})
} catch (e) {
console.log("unable to load worker scripts. server is in production mode.");
}
} else {
importScripts("/code/work.js/"+ver);
}
let base = self.base,
base = self.base,
moto = self.moto,
dbug = base.debug,
util = base.util,
time = util.time,
kiri = self.kiri,
Widget = kiri.Widget,
currentPrint,
cache = {};
if (!debug) {
base.debug.disable();
}
console.log(`kiri | init work | ${ver}`);
base.debug.disable();
// catch clipper alerts and convert to console messages
self.alert = function(o) {
console.log(o);
};
let dispatch = {
decimate: function(vertices, send) {
@ -108,7 +65,7 @@ let dispatch = {
slices.forEach(function(slice,index) {
send.data({index: index, slice: slice.encode()});
})
if (debug && widget.polish) {
if (self.debug && widget.polish) {
send.data({polish: kiri.codec.encode(widget.polish)});
}
send.data({send_end: time()});
@ -221,8 +178,3 @@ self.onmessage = function(e) {
console.log({kiri_msg:e});
}
};
// catch clipper alerts and convert to console messages
self.alert = function(o) {
console.log(o);
};

View file

@ -21,7 +21,7 @@ self.kiri.copyright = exports.COPYRIGHT;
DOC = self.document,
LOC = self.location,
HOST = LOC.host.split(':'),
LOCAL = HOST[0] === 'localhost' || HOST[0] === 'debug',
LOCAL = self.debug,
SETUP = parseOpt(LOC.search.substring(1)),
SECURE = isSecure(LOC.protocol),
SDB = MOTO.KV,

View file

@ -144,10 +144,6 @@ function prepareScripts() {
code.meta = concatCode(script.meta);
code.work = concatCode(script.work);
code.worker = concatCode(script.worker);
inject.kiri_local = htmlScript(codePrefix, script.kiri);
inject.meta_local = htmlScript(codePrefix, script.meta);
inject.kiri = htmlScript("code/", ["kiri"]);
inject.meta = htmlScript("code/", ["meta"]);
fs.readdir("./web/kiri/filter/FDM", function(err, files) {
filters_fdm = files || filters_fdm;
});
@ -184,6 +180,32 @@ function concatCode(array) {
cachepath,
filepath;
// in debug mode, the script should load dependent
// scripts instead of serving a complete bundle
if (debug) {
let code = [ `(function() { let load = [ `];
array.forEach(file => {
if (file.indexOf(".js") < 0) {
file = `/js/${file}.js`;
}
code.push(`"${file}",`);
});
code.push([
']; function load_next() {',
'let file = load.shift();',
'if (!file) return;',
'if (!self.document) { importScripts(file); return load_next() }',
'let s = document.createElement("script");',
's.type = "text/javascript";',
's.src = file;',
's.onload = load_next;',
'document.head.append(s);',
'}; load_next(); })();',
'self.debug=true;'
].join('\n'));
return code.join('\n');
}
array.forEach((file, index) => {
if (file.charAt(0) === "/" || file.indexOf("\\") > 0) {
filepath = file;
@ -202,6 +224,7 @@ function concatCode(array) {
});
code.push(cached);
});
return code.join('');
}
@ -420,6 +443,16 @@ function addCorsHeaders(req, res) {
res.setHeader("Allow", "GET,POST,OPTIONS");
}
/**
* in debug mode only, serve ANY file path beginning with / that exists
*/
function handleDebug(req, res, next) {
if (debug && lastmod(req.url)) {
return res.end(fs.readFileSync(req.url));
}
return next();
}
/**
* meta:moto data storage and retrieval url
*
@ -543,6 +576,9 @@ function handleData(req, res, next) {
function minify(path) {
let code = fs.readFileSync(path);
if (skip_minify.indexOf(path) >= 0) {
return code;
}
let mini = uglify.minify(code.toString());
if (mini.error) {
console.trace(mini.error);
@ -557,7 +593,7 @@ function minify(path) {
* @param {Function} next
*/
function handleJS(req, res, next) {
if (!(req.gs.local || debug || clearOK.indexOf(req.gs.path) >= 0)) {
if (!(req.gs.local || debug)) {
return reply404(req, res);
}
@ -613,10 +649,11 @@ function handleJS(req, res, next) {
* @param {Object} req
* @param {Object} res
* @param {Function} next
*
* expects paths in the format "/code/[token]/the/rest/is/ignored"
*/
function handleCode(req, res, next) {
let cookie = getCookieValue(req.headers.cookie),
key = req.gs.path.split('/')[2].split('.')[0],
let key = req.gs.path.split('/')[2].split('.')[0],
ck = code_src[key],
js = code[key];
@ -683,68 +720,6 @@ function serveCode(req, res, code) {
res.end(code.code);
}
/**
* @param {Object} req
* @param {Object} res
* @param {Function} next
*/
function rewriteHTML(req, res, next) {
function sendHTML(entry) {
let imd = ifModifiedDate(req);
if (imd && entry.mtime <= imd) {
res.writeHead(304, "Not Modified");
res.end();
return;
}
res.writeHead(200, {
'Last-Modified': new Date(entry.mtime).toGMTString(),
'Cache-Control': 'public, max-age=600',
'Content-Type': 'text/html'
});
res.write(entry.data);
res.end();
}
if (req.url.indexOf(".html") > 0) {
let local = req.gs.local,
key = local ? "local_" + req.url : req.url,
path = "web" + req.gs.path,
mtime = lastmod(path),
cached = fileCache[key],
replaced = false,
magic;
if (mtime === 0) return next();
if (cached && mtime === cached.mtime) return sendHTML(cached);
fs.readFile(path, (err, data) => {
if (!data) return next();
data = data.toString();
injectKeys.forEach(key => {
if (replaced) return;
magic = "<!--{"+key+"}-->";
if (data.indexOf(magic) > 0) {
data = data.replace(magic, inject[local ? key + "_local" : key]);
replaced = true;
}
});
if (replaced) {
cached = fileCache[key] = {
data: data,
mtime: mtime
};
sendHTML(cached);
} else {
next();
}
});
} else {
next();
}
}
/* *********************************************
* Setup / Global
********************************************* */
@ -791,13 +766,13 @@ let ver = require('../js/license.js'),
filters_laser = [],
modPaths = [],
ipCache = {},
clearOK = [
'/js/ext-three.js'
skip_minify = [
"js/ext-three.js"
],
script = {
kiri : [
"ext-three",
"license",
// "ext-clip2",
"ext-tween",
"ext-fsave",
"add-array",
@ -840,6 +815,7 @@ let ver = require('../js/license.js'),
"kiri-export"
],
meta : [
"ext-three",
"license",
"ext-tween",
"ext-fsave",
@ -856,8 +832,8 @@ let ver = require('../js/license.js'),
"meta"
],
work : [
"ext-three",
"license",
"ext-n3d",
"ext-clip2",
"add-array",
"add-three",
@ -870,7 +846,6 @@ let ver = require('../js/license.js'),
"geo-polygon",
"geo-polygons",
"geo-gyroid",
"kiri-lang",
"kiri-fill",
"kiri-slice",
"kiri-slicer",
@ -883,14 +858,11 @@ let ver = require('../js/license.js'),
"kiri-codec"
],
worker : [
"license",
"kiri-worker"
]
},
code_src = {},
code = {},
inject = {},
injectKeys = ["kiri", "meta"],
WS = require('ws'),
wss = new WS.Server({ noServer: true }),
wss_roots = {},
@ -1288,8 +1260,8 @@ handler.use(fullpath({
[ "/code/", handleCode ],
[ "/js/", handleJS ]
]))
.use(handleDebug)
.use(fixedmap("/api/", api))
.use(rewriteHTML)
.use(compression)
.use(handleStatic(currentDir + "/web/"))
.listen(port)

View file

@ -34,7 +34,6 @@
"leveldown": "^5.5.1",
"levelup": "^4.3.2",
"moment": "^2.24.0",
"n3d-threejs": "^72.0.0",
"pixi.js": "^4.8.9",
"serve-static": "^1.14.1",
"three": "^0.114.0",

View file

@ -17,17 +17,12 @@
<link rel="apple-touch-icon" href="/kiri/favicon-mobile.png">
<link rel="stylesheet" type="text/css" href="/moto/style.css">
<link rel="stylesheet" type="text/css" href="/kiri/style.css">
<script src="/js/ext-three.js"></script>
<script src="/code/kiri.js"></script>
</head>
<body>
<script>
/* because TWEEN complains */
module = {};
</script>
<!--{kiri}-->
<!-- app title home page links -->
<div id="appid">
<span><a href="#" id="kiri">Kiri:Moto</a> by <a href="/" target="home">Grid.Space</a></span>
<span><a href="#" id="appname">Kiri:Moto</a> by <a href="/" target="home">Grid.Space</a></span>
</div>
<!-- left control menu -->
<div id="control-left" class="control"><div id="assets"></div></div>