This commit is contained in:
cha0s 2020-12-11 07:52:18 -06:00
parent 18e941d3cb
commit 121dca5ff0
3 changed files with 46 additions and 16 deletions

View File

@ -1,7 +1,7 @@
import {createDatabaseConnection} from './db';
import createDockerContainer from './docker';
export {DataTypes as Types, Op} from 'sequelize';
export {DataTypes as Types, Op, default as Sequelize} from 'sequelize';
export {ModelMap} from './db';
export {default as Model} from './model';

View File

@ -30,26 +30,42 @@ export const createHttpServer = async (latus) => {
// eslint-disable-next-line no-param-reassign
latus.config['%http'] = httpServer;
httpServer.app = app;
app.use((req, res, next) => {
const requestMiddleware = (req, res, next) => {
latus.invokeMiddleware('@latus/http/request', req, res, next);
});
};
const routes = arrayFlatten(latus.invokeFlat('@latus/http/routes'));
routes.forEach(({method, path, handler}) => app[method](path, handler));
routes.forEach(({method, path, handler}) => app[method](path, requestMiddleware, handler));
// Serve latus.
app.use(configMiddleware(latus));
app.use(modulesMiddleware(latus));
app.get('/latus.config.js', requestMiddleware, configMiddleware(latus));
app.get('/latus.js', requestMiddleware, modulesMiddleware(latus));
// eslint-disable-next-line no-eval
if ('production' !== eval('process.env.NODE_ENV')) {
const proxy = httpProxy.createProxyServer({
secure: false,
target: `http://127.0.0.1:${devPort}`,
});
proxy.on('proxyRes', async (proxyRes, req, res) => {
if ('text/html; charset=UTF-8' === proxyRes.headers['content-type']) {
requestMiddleware(req, res, () => {
res.setHeader('Content-Type', proxyRes.headers['content-type']);
proxyRes.pipe(res);
});
}
else {
res.setHeader(
'Content-Type',
// ???
'/__webpack_hmr' === req.url ? 'text/event-stream' : proxyRes.headers['content-type'],
);
proxyRes.pipe(res);
}
});
proxy.on('error', (err, req, res) => {
if (res instanceof ServerResponse) {
res.status(502).end('Bad Gateway (WDS)');
}
});
app.all('*', (req, res) => proxy.web(req, res));
app.all('*', (req, res) => proxy.web(req, res, {selfHandleResponse: true}));
httpServer.on('upgrade', (req, socket, head) => proxy.ws(req, socket, head));
httpServer.on('close', () => proxy.close());
}

View File

@ -42,8 +42,15 @@ const Encoder = (latus) => class Encoder {
pack(packet) {
const packetId = packet.data[0];
const Packet = fromId(latus, packetId);
try {
return deflate(Packet.pack(packet));
}
catch (error) {
// eslint-disable-next-line no-console
console.error(`Couldn't pack ${Packet.name}(${JSON.stringify(packet.data[1], null, 2)})`);
throw error;
}
}
};
@ -70,6 +77,7 @@ const Decoder = (latus) => class Decoder extends decorate(class {}) {
const view = new Uint8Array(packet);
const packetId = view[0];
const Packet = fromId(latus, packetId);
try {
const unpacked = Packet.unpack(packet);
const {data} = unpacked;
this.emit('decoded', {
@ -79,6 +87,12 @@ const Decoder = (latus) => class Decoder extends decorate(class {}) {
nsp: unpacked.nsp,
});
}
catch (error) {
// eslint-disable-next-line no-console
console.error(`Couldn't unpack ${Packet.name}(${view})`);
throw error;
}
}
parseJSON(obj) {
this.emit('decoded', JSON.parse(obj));