renew module work
This commit is contained in:
parent
f139f01b83
commit
11552df76f
9 changed files with 116 additions and 73 deletions
123
app.js
123
app.js
|
|
@ -16,14 +16,12 @@ const version = license.version || "rogue";
|
||||||
const netdb = require('@gridspace/net-level-client');
|
const netdb = require('@gridspace/net-level-client');
|
||||||
const PATH = require('path');
|
const PATH = require('path');
|
||||||
|
|
||||||
const fileCache = {};
|
const append = { mesh:'', kiri:'' };
|
||||||
const code_src = {};
|
|
||||||
const code = {};
|
const code = {};
|
||||||
const mods = {};
|
const mods = {};
|
||||||
const load = [];
|
const load = [];
|
||||||
const synth = {};
|
const synth = {};
|
||||||
const api = {};
|
const api = {};
|
||||||
const wrap = {};
|
|
||||||
|
|
||||||
let forceUseCache = false;
|
let forceUseCache = false;
|
||||||
let serviceWorker = true;
|
let serviceWorker = true;
|
||||||
|
|
@ -36,7 +34,6 @@ let dversion;
|
||||||
let lastmod;
|
let lastmod;
|
||||||
let logger;
|
let logger;
|
||||||
let debug;
|
let debug;
|
||||||
let over;
|
|
||||||
let http;
|
let http;
|
||||||
let util;
|
let util;
|
||||||
let dir;
|
let dir;
|
||||||
|
|
@ -100,7 +97,8 @@ function init(mod) {
|
||||||
|
|
||||||
mod.add(handleSetup);
|
mod.add(handleSetup);
|
||||||
mod.add(handleOptions);
|
mod.add(handleOptions);
|
||||||
mod.add(handleWasm);
|
mod.add(serveWasm);
|
||||||
|
mod.add(serveCode);
|
||||||
mod.add(fullpath({
|
mod.add(fullpath({
|
||||||
"/kiri" : redir("/kiri/", 301),
|
"/kiri" : redir("/kiri/", 301),
|
||||||
"/mesh" : redir("/mesh/", 301),
|
"/mesh" : redir("/mesh/", 301),
|
||||||
|
|
@ -120,6 +118,8 @@ function init(mod) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
mod.add(rewriteHtmlVersion);
|
mod.add(rewriteHtmlVersion);
|
||||||
|
// example of how to use the new middleware
|
||||||
|
// mod.add(appendContent('/kiri/index.html', '<script>console.log("hello")</script>'));
|
||||||
mod.static("/lib/", "src");
|
mod.static("/lib/", "src");
|
||||||
mod.static("/obj/", "web/obj");
|
mod.static("/obj/", "web/obj");
|
||||||
mod.static("/font/", "web/font");
|
mod.static("/font/", "web/font");
|
||||||
|
|
@ -149,10 +149,10 @@ function init(mod) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// load development and 3rd party modules
|
// load development and 3rd party modules
|
||||||
// load_modules('mod');
|
load_modules('mod');
|
||||||
|
|
||||||
// load optional local modules
|
// load optional local modules
|
||||||
// load_modules('mods');
|
load_modules('mods');
|
||||||
|
|
||||||
// run loads injected by modules
|
// run loads injected by modules
|
||||||
while (load.length) {
|
while (load.length) {
|
||||||
|
|
@ -162,6 +162,13 @@ function init(mod) {
|
||||||
logger.log({on_load_fail: e});
|
logger.log({on_load_fail: e});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// minify appends
|
||||||
|
if (!debug) {
|
||||||
|
for (let key of Object.keys(append)) {
|
||||||
|
append[key] = minify(append[key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// either add module assets to path or require(init.js)
|
// either add module assets to path or require(init.js)
|
||||||
|
|
@ -179,7 +186,7 @@ function loadModule(mod, dir) {
|
||||||
|
|
||||||
// load module and call returned function with helper object
|
// load module and call returned function with helper object
|
||||||
function initModule(mod, file, dir) {
|
function initModule(mod, file, dir) {
|
||||||
logger.log({module: file});
|
logger.log({ module: file, dir });
|
||||||
require_fresh(file)({
|
require_fresh(file)({
|
||||||
// express functions added here show up at "/api/" url root
|
// express functions added here show up at "/api/" url root
|
||||||
api: api,
|
api: api,
|
||||||
|
|
@ -192,7 +199,6 @@ function initModule(mod, file, dir) {
|
||||||
args: {},
|
args: {},
|
||||||
meta: mod.meta,
|
meta: mod.meta,
|
||||||
debug: debug,
|
debug: debug,
|
||||||
script: script,
|
|
||||||
moddir: dir,
|
moddir: dir,
|
||||||
rootdir: mod.dir,
|
rootdir: mod.dir,
|
||||||
version: oversion || version
|
version: oversion || version
|
||||||
|
|
@ -219,28 +225,28 @@ function initModule(mod, file, dir) {
|
||||||
logger: log.new
|
logger: log.new
|
||||||
},
|
},
|
||||||
inject: (code, file, opt = {}) => {
|
inject: (code, file, opt = {}) => {
|
||||||
// console.log({ inject: opt, code, file });
|
const body = fs.readFileSync(dir + '/' + file);
|
||||||
let codelist = script[code];
|
if (opt.first) {
|
||||||
if (!codelist) {
|
append[code] = body.toString() + '\n' + append[code];
|
||||||
return logger.log(`inject missing target "${code}"`);
|
} else {
|
||||||
}
|
append[code] += body.toString() + '\n';
|
||||||
const path = `${dir}/${file}`;
|
|
||||||
codelist.push(path);
|
|
||||||
if (opt.cachever) {
|
|
||||||
cachever[path] = opt.cachever;
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
path: {
|
path: {
|
||||||
any: arg => { mod.add(arg) },
|
any: arg => { mod.add(arg) },
|
||||||
pre: arg => { mod.add(prepath(arg)) },
|
code() {
|
||||||
map: arg => { mod.add(fixedmap(arg)) },
|
const [ path, file ] = [ ...arguments ];
|
||||||
|
code[path] = fs.readFileSync(file);
|
||||||
|
},
|
||||||
full: arg => { mod.add(fullpath(arg)) },
|
full: arg => { mod.add(fullpath(arg)) },
|
||||||
|
map: arg => { mod.add(fixedmap(arg)) },
|
||||||
|
pre: arg => { mod.add(prepath(arg)) },
|
||||||
|
redir: redir,
|
||||||
|
remap: remap,
|
||||||
|
setup: fn => { setupFn = fn },
|
||||||
static: (root, pre) => {
|
static: (root, pre) => {
|
||||||
mod.static(pre || "/", root);
|
mod.static(pre || "/", root);
|
||||||
},
|
},
|
||||||
redir: redir,
|
|
||||||
remap: remap,
|
|
||||||
setup: fn => { setupFn = fn }
|
|
||||||
},
|
},
|
||||||
handler: {
|
handler: {
|
||||||
addCORS: addCorsHeaders,
|
addCORS: addCorsHeaders,
|
||||||
|
|
@ -344,7 +350,20 @@ function handleOptions(req, res, next) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleWasm(req, res, next) {
|
function serveCode(req, res, next) {
|
||||||
|
let { path } = req.app;
|
||||||
|
if (path.startsWith("/code/")) {
|
||||||
|
path = path.split('/')[2].split('.')[0];
|
||||||
|
if (code[path]) {
|
||||||
|
res.setHeader('Content-Type', 'application/javascript');
|
||||||
|
res.setHeader('Cache-Control', 'public, max-age=600');
|
||||||
|
return res.end(code[path]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
next();
|
||||||
|
}
|
||||||
|
|
||||||
|
function serveWasm(req, res, next) {
|
||||||
let file = req.app.path.split('/').pop();
|
let file = req.app.path.split('/').pop();
|
||||||
let ext = (file || '').split('.')[1];
|
let ext = (file || '').split('.')[1];
|
||||||
let path = PATH.join(dir,"src","wasm",file);
|
let path = PATH.join(dir,"src","wasm",file);
|
||||||
|
|
@ -385,8 +404,7 @@ function generateDevices() {
|
||||||
fs.writeFileSync(PATH.join(dir,"src","pack","devices.js"), `export const devices = ${dstr};`);
|
fs.writeFileSync(PATH.join(dir,"src","pack","devices.js"), `export const devices = ${dstr};`);
|
||||||
}
|
}
|
||||||
|
|
||||||
function minify(path) {
|
function minify(code) {
|
||||||
let code = fs.readFileSync(path);
|
|
||||||
let mini = uglify.minify(code.toString(), {
|
let mini = uglify.minify(code.toString(), {
|
||||||
compress: {
|
compress: {
|
||||||
merge_vars: false,
|
merge_vars: false,
|
||||||
|
|
@ -512,39 +530,32 @@ function rewriteHtmlVersion(req, res, next) {
|
||||||
"/lib/kiri-run/minion.js"
|
"/lib/kiri-run/minion.js"
|
||||||
].indexOf(req.app.path) >= 0) {
|
].indexOf(req.app.path) >= 0) {
|
||||||
addCorsHeaders(req, res);
|
addCorsHeaders(req, res);
|
||||||
} else if ([
|
}
|
||||||
"/kiri/",
|
if ([
|
||||||
"/kiri/engine.html",
|
"/lib/main/kiri.js",
|
||||||
"/kiri/frame.html",
|
"/lib/main/mesh.js",
|
||||||
"/mesh/",
|
|
||||||
"/meta/",
|
|
||||||
].indexOf(req.app.path) >= 0) {
|
].indexOf(req.app.path) >= 0) {
|
||||||
addCorsHeaders(req, res);
|
const data = append[req.app.path.split('/')[3].split('.')[0]];
|
||||||
let real_write = res.write;
|
// console.log({ append: req.app.path, data: data?.length });
|
||||||
let real_end = res.end;
|
|
||||||
let mlen = '{{version}}'.length;
|
if (!data) return next();
|
||||||
let vstr = oversion || dversion || version;
|
|
||||||
if (vstr.length < mlen) {
|
const real_write = res.write;
|
||||||
vstr = vstr.padStart(mlen,0);
|
const real_end = res.end;
|
||||||
} else if (vstr.length > mlen) {
|
let body = '';
|
||||||
vstr = vstr.substring(0,mlen);
|
|
||||||
}
|
res.write = function (chunk, encoding) {
|
||||||
res.write = function() {
|
body += chunk.toString(encoding);
|
||||||
try {
|
|
||||||
// console.log('res.write', req.app.path, arguments[0].length);
|
|
||||||
arguments[0] = arguments[0].toString().replace(/{{version}}/g,vstr);
|
|
||||||
// console.log('new length', arguments[0].length);
|
|
||||||
real_write.apply(res, arguments);
|
|
||||||
} catch (err) {
|
|
||||||
console.log({ rewrite_error: err });
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
res.end = function() {
|
|
||||||
// console.log('res.write', req.app.path, arguments[0]?.length);
|
res.end = function (chunk, encoding) {
|
||||||
if (arguments[0]) {
|
if (chunk) {
|
||||||
arguments[0] = arguments[0].toString().replace(/{{version}}/g,vstr);
|
body += chunk.toString(encoding);
|
||||||
}
|
}
|
||||||
real_end.apply(res, arguments);
|
body += data;
|
||||||
|
res.setHeader('Content-Length', Buffer.byteLength(body));
|
||||||
|
real_write.call(res, body);
|
||||||
|
real_end.call(res);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,8 @@ self.kiri.load(api => {
|
||||||
|
|
||||||
console.log('BAMBU MODULE RUNNING');
|
console.log('BAMBU MODULE RUNNING');
|
||||||
|
|
||||||
const { kiri, moto } = self;
|
const { uc: ui } = api;
|
||||||
const { ui } = kiri;
|
const { $, h } = api.web;
|
||||||
const h = moto.webui;
|
|
||||||
const defams = ";; DEFINE BAMBU-AMS ";
|
const defams = ";; DEFINE BAMBU-AMS ";
|
||||||
const readonly = true;
|
const readonly = true;
|
||||||
const stock_colors = Object.values({
|
const stock_colors = Object.values({
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ self.kiri.load(api => {
|
||||||
});
|
});
|
||||||
api.stats.set('kiri', self.kiri.version + 'e');
|
api.stats.set('kiri', self.kiri.version + 'e');
|
||||||
});
|
});
|
||||||
|
|
||||||
if (self.mesh && self.mesh.api) {
|
if (self.mesh && self.mesh.api) {
|
||||||
self.mesh.api.electron = {};
|
self.mesh.api.electron = {};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,11 +18,15 @@ import { selection } from './selection.js';
|
||||||
import { stats } from '../kiri/stats.js';
|
import { stats } from '../kiri/stats.js';
|
||||||
import { widgets } from './widgets.js';
|
import { widgets } from './widgets.js';
|
||||||
import { updateTool } from '../kiri-mode/cam/tools.js';
|
import { updateTool } from '../kiri-mode/cam/tools.js';
|
||||||
|
import { version } from '../moto/license.js';
|
||||||
|
import { space as SPACE } from '../moto/space.js';
|
||||||
|
|
||||||
import { LANG } from './lang.js';
|
import { LANG } from './lang.js';
|
||||||
import { LOCAL, SETUP, SECURE } from './main.js';
|
import { LOCAL, SETUP, SECURE } from './main.js';
|
||||||
import { UI } from './ui.js';
|
import { UI } from './ui.js';
|
||||||
|
|
||||||
|
import web from '../moto/webui.js';
|
||||||
|
|
||||||
let UC = UI.prefix('kiri').inputAction(settings.conf.update),
|
let UC = UI.prefix('kiri').inputAction(settings.conf.update),
|
||||||
und = undefined,
|
und = undefined,
|
||||||
clone = Object.clone,
|
clone = Object.clone,
|
||||||
|
|
@ -80,6 +84,7 @@ let UC = UI.prefix('kiri').inputAction(settings.conf.update),
|
||||||
},
|
},
|
||||||
local = {
|
local = {
|
||||||
get: (key) => localGet(key),
|
get: (key) => localGet(key),
|
||||||
|
getItem: (key) => localGet(key),
|
||||||
getInt: (key) => parseInt(localGet(key)),
|
getInt: (key) => parseInt(localGet(key)),
|
||||||
getFloat: (key) => parseFloat(localGet(key)),
|
getFloat: (key) => parseFloat(localGet(key)),
|
||||||
getBoolean: (key, def = true) => {
|
getBoolean: (key, def = true) => {
|
||||||
|
|
@ -89,6 +94,7 @@ let UC = UI.prefix('kiri').inputAction(settings.conf.update),
|
||||||
toggle: (key, val, def) => localSet(key, val ?? !api.local.getBoolean(key, def)),
|
toggle: (key, val, def) => localSet(key, val ?? !api.local.getBoolean(key, def)),
|
||||||
put: (key, val) => localSet(key, val),
|
put: (key, val) => localSet(key, val),
|
||||||
set: (key, val) => localSet(key, val),
|
set: (key, val) => localSet(key, val),
|
||||||
|
setItem: (key, val) => localSet(key, val),
|
||||||
},
|
},
|
||||||
tweak = {
|
tweak = {
|
||||||
line_precision(v) { api.work.config({ base: { clipperClean: v } }) },
|
line_precision(v) { api.work.config({ base: { clipperClean: v } }) },
|
||||||
|
|
@ -152,6 +158,7 @@ export const api = {
|
||||||
settings,
|
settings,
|
||||||
show,
|
show,
|
||||||
space,
|
space,
|
||||||
|
SPACE,
|
||||||
stacks: STACKS,
|
stacks: STACKS,
|
||||||
stats,
|
stats,
|
||||||
tool: {
|
tool: {
|
||||||
|
|
@ -166,7 +173,9 @@ export const api = {
|
||||||
layer_hi: 0,
|
layer_hi: 0,
|
||||||
layer_max: 0
|
layer_max: 0
|
||||||
},
|
},
|
||||||
|
version,
|
||||||
view,
|
view,
|
||||||
|
web,
|
||||||
widgets,
|
widgets,
|
||||||
work,
|
work,
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ import { init as initWEDM } from '../kiri-mode/wedm/client.js';
|
||||||
import { init as initWJET } from '../kiri-mode/wjet/client.js';
|
import { init as initWJET } from '../kiri-mode/wjet/client.js';
|
||||||
|
|
||||||
let { CAM, SLA, FDM, LASER, DRAG, WJET, WEDM } = MODES,
|
let { CAM, SLA, FDM, LASER, DRAG, WJET, WEDM } = MODES,
|
||||||
{ client, catalog, platform, selection } = api,
|
{ client, catalog, platform, selection, stats } = api,
|
||||||
LANG = api.language.current,
|
LANG = api.language.current,
|
||||||
WIN = self.window,
|
WIN = self.window,
|
||||||
DOC = self.document,
|
DOC = self.document,
|
||||||
|
|
@ -1859,8 +1859,9 @@ function init_two() {
|
||||||
// load script extensions
|
// load script extensions
|
||||||
if (SETUP.s) SETUP.s.forEach(function(lib) {
|
if (SETUP.s) SETUP.s.forEach(function(lib) {
|
||||||
let scr = DOC.createElement('script');
|
let scr = DOC.createElement('script');
|
||||||
scr.setAttribute('defer',true);
|
scr.setAttribute('async', true);
|
||||||
scr.setAttribute('src',`/code/${lib}.js?${version}`);
|
scr.setAttribute('defer', true);
|
||||||
|
scr.setAttribute('src',`/code/${lib}.js?${version}}`);
|
||||||
DOC.body.appendChild(scr);
|
DOC.body.appendChild(scr);
|
||||||
stats.add('load_'+lib);
|
stats.add('load_'+lib);
|
||||||
api.event.emit('load.lib', lib);
|
api.event.emit('load.lib', lib);
|
||||||
|
|
|
||||||
|
|
@ -774,6 +774,7 @@ function showHelpFile(local,then) {
|
||||||
WIN.open("//docs.grid.space/", "_help");
|
WIN.open("//docs.grid.space/", "_help");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const LANG = api.language.current;
|
||||||
$('kiri-version').innerHTML = `${LANG.version} ${version}`;
|
$('kiri-version').innerHTML = `${LANG.version} ${version}`;
|
||||||
showModal('help');
|
showModal('help');
|
||||||
api.event.emit('help.show', local);
|
api.event.emit('help.show', local);
|
||||||
|
|
|
||||||
|
|
@ -8,26 +8,34 @@ import '../kiri/lang-en.js';
|
||||||
|
|
||||||
import { run } from '../kiri/init.js';
|
import { run } from '../kiri/init.js';
|
||||||
|
|
||||||
const load = [];
|
let load = [];
|
||||||
|
|
||||||
|
function safeExec(fn) {
|
||||||
|
try {
|
||||||
|
fn(kiri.api);
|
||||||
|
} catch (error) {
|
||||||
|
console.log('load error', fn, error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function checkReady() {
|
function checkReady() {
|
||||||
if (document.readyState === 'complete') {
|
if (document.readyState === 'complete') {
|
||||||
let api = run();
|
kiri.api = run();
|
||||||
kiri.api = api;
|
|
||||||
for (let fn of load) {
|
for (let fn of load) {
|
||||||
try {
|
safeExec(fn);
|
||||||
fn(api);
|
|
||||||
} catch (error) {
|
|
||||||
console.log('load error', fn, error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
load = undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.kiri = {
|
self.kiri = {
|
||||||
load(fn) {
|
load(fn) {
|
||||||
console.log('KIRI LOAD', [...arguments]);
|
// console.log('KIRI LOAD', [...arguments]);
|
||||||
load.push(fn);
|
if (load) {
|
||||||
|
load.push(fn);
|
||||||
|
} else {
|
||||||
|
safeExec(fn);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -92,6 +92,9 @@ function init() {
|
||||||
|
|
||||||
// hide url params
|
// hide url params
|
||||||
history.replaceState({}, '', '/mesh/');
|
history.replaceState({}, '', '/mesh/');
|
||||||
|
|
||||||
|
// for electron
|
||||||
|
self.mesh = { api };
|
||||||
}
|
}
|
||||||
|
|
||||||
// restore space layout and view from previous session
|
// restore space layout and view from previous session
|
||||||
|
|
|
||||||
|
|
@ -168,4 +168,14 @@ export {
|
||||||
$C,
|
$C,
|
||||||
h,
|
h,
|
||||||
estop
|
estop
|
||||||
};
|
}
|
||||||
|
|
||||||
|
export default {
|
||||||
|
$,
|
||||||
|
$d,
|
||||||
|
$h,
|
||||||
|
$c,
|
||||||
|
$C,
|
||||||
|
h,
|
||||||
|
estop
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue