refactor(security): http

This commit is contained in:
cha0s 2021-01-19 10:40:04 -06:00
parent 73c18a4ad4
commit e56682d659
12 changed files with 47 additions and 41 deletions

View File

@ -23,6 +23,8 @@
'@latus/socket/client', '@latus/socket/client',
'@latus/react/client', '@latus/react/client',
], ],
}
'@latus/http/server': {
request: [ request: [
'@latus/user/session', '@latus/user/session',
'@latus/user/passport', '@latus/user/passport',

View File

@ -25,10 +25,10 @@ export default {
'@latus/db/server/models': gatherWithLatus( '@latus/db/server/models': gatherWithLatus(
require.context('./models', false, /\.js$/), require.context('./models', false, /\.js$/),
), ),
'@latus/http/request': (latus) => { '@latus/http/server/request': (latus) => {
const {config: {'@latus/governor/server': {http}}} = latus; const {config: {'@latus/governor/server': {http}}} = latus;
const limiter = createLimiter(latus, { const limiter = createLimiter(latus, {
keyPrefix: '@latus/http/request', keyPrefix: '@latus/http/server/request',
...http, ...http,
}); });
return async (req, res, next) => { return async (req, res, next) => {

View File

@ -3,7 +3,7 @@ const copy = require('@neutrinojs/copy');
module.exports = require('../../config/.neutrinorc'); module.exports = require('../../config/.neutrinorc');
module.exports.use.push(copy({ module.exports.use.push(copy({
patterns: ['index.js', 'index.ejs', 'tests.js'].map((path) => ({ patterns: ['entry.js', 'index.ejs', 'tests.js'].map((path) => ({
from: `src/client/${path}`, from: `src/client/${path}`,
to: 'client', to: 'client',
})).concat(['.neutrinorc.js', 'client-plugins.js', 'webpack.config.js'].map((path) => ({ })).concat(['.neutrinorc.js', 'client-plugins.js', 'webpack.config.js'].map((path) => ({

View File

@ -25,7 +25,7 @@ const client = {
const root = `${__dirname}/..`; const root = `${__dirname}/..`;
neutrino.options.root = fs.realpathSync(root); neutrino.options.root = fs.realpathSync(root);
neutrino.options.source = 'client'; neutrino.options.source = 'client';
neutrino.options.mains.index = 'index'; neutrino.options.mains.index = 'entry';
neutrino.options.mains.tests = { neutrino.options.mains.tests = {
entry: './client/tests', entry: './client/tests',
title: 'Mocha tests', title: 'Mocha tests',

View File

@ -0,0 +1,15 @@
import {Latus} from '@latus/core';
// eslint-disable-next-line import/no-unresolved
import '@latus/core/virtual';
(async () => {
if (!window.$$latus) {
return;
}
const latus = new Latus(window.$$latus);
await Promise.all(latus.invokeFlat('@latus/core/starting'));
await Promise.all(latus.invokeFlat('@latus/core/started'));
await latus.invokeSequential('@latus/http/client/up');
// eslint-disable-next-line no-console
console.log('Latus up!\n');
})();

View File

@ -1,15 +1,7 @@
import {Latus} from '@latus/core'; export default {
// eslint-disable-next-line import/no-unresolved hooks: {
import '@latus/core/virtual'; '@latus/core/config': () => ({
up: [],
(async () => { }),
if (!window.$$latus) { },
return; };
}
const latus = new Latus(window.$$latus);
await Promise.all(latus.invokeFlat('@latus/core/starting'));
await Promise.all(latus.invokeFlat('@latus/core/started'));
await latus.invokeSequential('@latus/http/client/up');
// eslint-disable-next-line no-console
console.log('Latus up!\n');
})();

View File

@ -17,7 +17,7 @@ const {
export const createHttpServer = async (latus) => { export const createHttpServer = async (latus) => {
const { const {
config: { config: {
'@latus/http': { '@latus/http/server': {
build, build,
devPort, devPort,
host, host,
@ -33,7 +33,7 @@ export const createHttpServer = async (latus) => {
latus.config['%http'] = httpServer; latus.config['%http'] = httpServer;
httpServer.app = app; httpServer.app = app;
const requestMiddleware = (req, res, next) => { const requestMiddleware = (req, res, next) => {
latus.invokeMiddleware('@latus/http/request', req, res, next); latus.invokeMiddleware('@latus/http/server/request', req, res, next);
}; };
const routes = arrayFlatten(latus.invokeFlat('@latus/http/routes')); const routes = arrayFlatten(latus.invokeFlat('@latus/http/routes'));
routes.forEach(({method, path, handler}) => app[method](path, requestMiddleware, handler)); routes.forEach(({method, path, handler}) => app[method](path, requestMiddleware, handler));

View File

@ -1,16 +1,7 @@
export default { export default {
hooks: { hooks: {
'@latus/core/config': () => ({ '@latus/core/config': () => ({
build: 'build/http',
'client.up': [],
devPort: 32341,
devPublic: undefined,
host: '0.0.0.0',
plugins: [],
port: 32340,
request: [],
title: 'Latus', title: 'Latus',
trust: false,
}), }),
}, },
}; };

View File

@ -7,7 +7,7 @@ export default {
'@latus/core/build': (configs, latus) => { '@latus/core/build': (configs, latus) => {
const { const {
config: { config: {
'@latus/http': { '@latus/http/server': {
devPort, devPort,
devPublic, devPublic,
host, host,
@ -34,10 +34,16 @@ export default {
spawn(`${binary} --hot --mode development --config ${config}`, options); spawn(`${binary} --hot --mode development --config ${config}`, options);
} }
}, },
'@latus/core/up': (latus) => createHttpServer(latus), '@latus/core/config': () => ({
'@latus/http/plugins': (req, {config: {'@latus/http': {'client.up': up}}}) => ({ build: 'build/http',
'@latus/http/client': {up}, devPort: 32341,
devPublic: undefined,
host: '0.0.0.0',
port: 32340,
request: [],
trust: false,
}), }),
'@latus/core/up': (latus) => createHttpServer(latus),
'@latus/repl/context': (latus) => ({ '@latus/repl/context': (latus) => ({
httpServer: latus.config['%http'], httpServer: latus.config['%http'],
}), }),

View File

@ -26,7 +26,7 @@ const adapterIntoReq = (path, adapter, req) => {
export default { export default {
hooks: { hooks: {
'@latus/http/request': ({config: {'%redisAdapter': adapter}}) => (req, res, next) => { '@latus/http/server/request': ({config: {'%redisAdapter': adapter}}) => (req, res, next) => {
adapterIntoReq('http', adapter, req); adapterIntoReq('http', adapter, req);
next(); next();
}, },

View File

@ -22,10 +22,10 @@ export default {
'@latus/db/server/models': gatherWithLatus( '@latus/db/server/models': gatherWithLatus(
require.context('./models', false, /\.js$/), require.context('./models', false, /\.js$/),
), ),
'@latus/http/request': (latus) => (req, res, next) => { '@latus/http/server/request': (latus) => (req, res, next) => {
debug('@latus/http/request: passport.initialize()'); debug('@latus/http/server/request: passport.initialize()');
passport.initialize()(req, res, () => { passport.initialize()(req, res, () => {
debug('@latus/http/request: passport.session()'); debug('@latus/http/server/request: passport.session()');
passport.session()(req, res, () => { passport.session()(req, res, () => {
if (!req.user) { if (!req.user) {
const {User} = latus.get('%models.fromName'); const {User} = latus.get('%models.fromName');

View File

@ -16,12 +16,12 @@ export default {
...latus.invokeReduce('@latus/user/session'), ...latus.invokeReduce('@latus/user/session'),
}); });
}, },
'@latus/http/request': (latus) => { '@latus/http/server/request': (latus) => {
const urle = express.urlencoded({extended: true}); const urle = express.urlencoded({extended: true});
return (req, res, next) => { return (req, res, next) => {
debug('@latus/http/request: express.urlencoded()'); debug('@latus/http/server/request: express.urlencoded()');
urle(req, res, () => { urle(req, res, () => {
debug('@latus/http/request: session()'); debug('@latus/http/server/request: session()');
latus.config['%session'](req, res, (error) => { latus.config['%session'](req, res, (error) => {
if (error) { if (error) {
next(error); next(error);