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/react/client',
],
}
'@latus/http/server': {
request: [
'@latus/user/session',
'@latus/user/passport',

View File

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

View File

@ -3,7 +3,7 @@ const copy = require('@neutrinojs/copy');
module.exports = require('../../config/.neutrinorc');
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}`,
to: 'client',
})).concat(['.neutrinorc.js', 'client-plugins.js', 'webpack.config.js'].map((path) => ({

View File

@ -25,7 +25,7 @@ const client = {
const root = `${__dirname}/..`;
neutrino.options.root = fs.realpathSync(root);
neutrino.options.source = 'client';
neutrino.options.mains.index = 'index';
neutrino.options.mains.index = 'entry';
neutrino.options.mains.tests = {
entry: './client/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';
// 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');
})();
export default {
hooks: {
'@latus/core/config': () => ({
up: [],
}),
},
};

View File

@ -17,7 +17,7 @@ const {
export const createHttpServer = async (latus) => {
const {
config: {
'@latus/http': {
'@latus/http/server': {
build,
devPort,
host,
@ -33,7 +33,7 @@ export const createHttpServer = async (latus) => {
latus.config['%http'] = httpServer;
httpServer.app = app;
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'));
routes.forEach(({method, path, handler}) => app[method](path, requestMiddleware, handler));

View File

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

View File

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

View File

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

View File

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

View File

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