将内置UI封包

This commit is contained in:
undefined committed 2020-05-10 17:46:43 +08:00
1 parent 9156fa3a9b
commit 88fc0606ee
5 files changed
+74 -52

No files matched your search

+30 -12
View File
@@ -20,6 +20,13 @@ function ensureDir(dir) {
}
const active = [];
try {
const f = require('../.build/builtin.json');
const m = { ...yaml.safeLoad(zlib.gunzipSync(f.data)), id: 'builtin' };
active.push(m);
} catch (e) {
/* Ignore */
}
async function preload() {
const files = fs.readdirSync(root('.build/module'));
@@ -28,24 +35,35 @@ async function preload() {
try {
const f = fs.readFileSync(root(`.build/module/${file}`));
const m = { ...yaml.safeLoad(zlib.gunzipSync(f)), id: file.split('.')[0] };
if (m.os) {
if (!m.os.includes(os.platform())) throw new Error('Unsupported OS');
}
if (m.file) {
m.files = {};
ensureDir(path.resolve(os.tmpdir(), 'hydro', file));
for (const n in m.file) {
const e = path.resolve(os.tmpdir(), 'hydro', file, n);
fs.writeFileSync(e, Buffer.from(m.file[n], 'base64'), { mode: 755 });
m.files[n] = e;
}
}
active.push(m);
} catch (e) {
console.error(`Module Load Fail: ${file}`);
}
}
}
for (const i of active) {
try {
if (i.os) {
if (!i.os.includes(os.platform())) throw new Error('Unsupported OS');
}
if (i.file) {
i.files = {};
ensureDir(path.resolve(os.tmpdir(), 'hydro', i.id));
for (const n in i.file) {
if (i.file[n] === null) {
ensureDir(path.resolve(os.tmpdir(), 'hydro', i.id, n));
} else {
const e = path.resolve(os.tmpdir(), 'hydro', i.id, n);
fs.writeFileSync(e, Buffer.from(i.file[n], 'base64'), { mode: 755 });
i.files[n] = e;
}
}
}
} catch (e) {
console.error(`Module Load Fail: ${i.id}`);
console.error(e);
}
}
}
async function handler() {
+7 -2
View File
@@ -1,4 +1,5 @@
const path = require('path');
const os = require('os');
const { ObjectID } = require('bson');
const Koa = require('koa');
const morgan = require('koa-morgan');
@@ -28,7 +29,7 @@ async function prepare() {
const useHttps = await system.get('listen.https');
server = (useHttps ? https : http).createServer(app.callback());
app.keys = await system.get('session.keys');
app.use(cache(path.join(process.cwd(), '.uibuild'), {
app.use(cache(path.join(os.tmpdir(), 'hydro', 'builtin'), {
maxAge: 365 * 24 * 60 * 60,
}));
app.use(Body({
@@ -395,6 +396,10 @@ function Connection(prefix, RouteConnHandler) {
sock.installHandlers(server);
}
function Middleware(middleware) {
app.use(middleware);
}
async function start() {
app.use(morgan(':method :url :status :res[content-length] - :response-time ms'));
app.use(router.routes()).use(router.allowedMethods());
@@ -404,5 +409,5 @@ async function start() {
}
global.Hydro.service.server = module.exports = {
Handler, ConnectionHandler, Route, Connection, prepare, start,
Handler, ConnectionHandler, Route, Connection, Middleware, prepare, start,
};