refactor: sane middleware API

This commit is contained in:
cha0s 2020-12-04 01:56:19 -06:00
parent c0af9ba70d
commit 48232aea3f
11 changed files with 59 additions and 45 deletions

View File

@ -24,5 +24,6 @@ export default {
id,
},
}),
'@latus/repl/context': (latus) => ({latus}),
},
};

View File

@ -79,24 +79,19 @@ export default class Plugins {
}
invokeMiddleware(hook, ...args) {
debug('invokeMiddleware(...): %s', hook);
if (!this.hooks[hook]) {
return Promise.resolve();
}
const parts = hook.split('/');
const key = parts.pop();
const plugins = this.config[parts.join('/')][key];
debug('middleware: %O', plugins);
const fns = plugins
.map((plugin) => this.hooks[hook].find(({plugin: candidate}) => plugin === candidate).fn);
const middleware = new Middleware(fns);
return new Promise((resolve, reject) => {
middleware.dispatch(...(args.concat(this).concat((error) => {
if (error) {
reject(error);
return;
}
resolve();
})));
});
const middleware = new Middleware(fns.map((fn) => fn(this)));
return middleware.promise(args.concat(this));
}
invokePlugin(hook, plugin, ...args) {

View File

@ -32,6 +32,18 @@ export default class Middleware {
invoke();
}
promise(...args) {
return new Promise((resolve, reject) => {
this.dispatch(...(args.concat((error) => {
if (error) {
reject(error);
return;
}
resolve();
})));
});
}
use(fn) {
this.middleware.push(fn);
}

View File

@ -4,9 +4,9 @@ import {createHttpServer} from './server';
export default {
hooks: {
'@latus/core/config': () => ({
middleware: [],
host: '0.0.0.0',
port: 32340,
request: [],
}),
'@latus/core/build': (configs) => {
// eslint-disable-next-line global-require, no-param-reassign

View File

@ -12,18 +12,15 @@ const {
} = process.env;
export const createHttpServer = async (latus) => {
const {config: {'@latus/http': {host, middleware, port}}} = latus;
const {config: {'@latus/http': {host, port}}} = latus;
const app = express();
const httpServer = createServer(app);
// eslint-disable-next-line no-param-reassign
latus.config['%http'] = httpServer;
httpServer.app = app;
// Request middleware.
for (let i = 0; i < middleware.length; i++) {
app.use(
latus.invokePlugin('@latus/http/middleware', middleware[i], httpServer),
);
}
app.use((req, res, next) => {
latus.invokeMiddleware('@latus/http/request', req, res).then(next).catch(next);
});
// Serve latus.
app.use(latusMiddleware(latus));
app.use(express.static(join(__dirname, 'client')));

View File

@ -3,7 +3,7 @@ import {createSocketServer} from './lifecycle';
export default {
hooks: {
'@latus/core/config': () => ({
middleware: [],
connection: [],
}),
'@latus/http/plugins': () => ({
'@latus/socket/client': {},

View File

@ -19,7 +19,6 @@ export default class SocketServer extends decorate(Class) {
const {
config: {
'@latus/core': {id},
'@latus/socket': {middleware},
},
} = latus;
this.io = SocketIoServer(httpServer, {
@ -30,12 +29,9 @@ export default class SocketServer extends decorate(Class) {
...latus.invokeReduce('@latus/socket/server'),
});
this.io.on('connect', this.onConnect);
for (let i = 0; i < middleware.length; i++) {
const fn = latus.invokePlugin('@latus/socket/middleware', middleware[i], this);
if (fn) {
this.io.use(fn);
}
}
this.io.use((socket, next) => {
latus.invokeMiddleware('@latus/socket/connection', socket).then(next).catch(next);
});
const Packets = latus.invokeReduce('@latus/socket/packets');
// eslint-disable-next-line no-param-reassign
latus.config['%packets'] = latus.invokeComposed('@latus/socket/packets/decorate', Packets);

View File

@ -1,15 +1,22 @@
import D from 'debug';
import passport from 'passport';
import LogOps from 'passport/lib/http/request';
const debug = D('@latus/user/passport');
export default {
hooks: {
'@latus/http/middleware': () => (req, res, next) => {
'@latus/http/request': () => (req, res, next) => {
debug('@latus/http/request: passport.initialize()');
passport.initialize()(req, res, () => {
debug('@latus/http/request: passport.session()');
passport.session()(req, res, next);
});
},
'@latus/socket/middleware': () => (socket, next) => {
'@latus/socket/connection': () => (socket, next) => {
debug('@latus/socket/connection: passport.initialize()');
passport.initialize()(socket.handshake, undefined, () => {
debug('@latus/socket/connection: passport.session()');
passport.session()(socket.handshake, undefined, () => {
/* eslint-disable no-param-reassign */
socket.handshake.login = LogOps.logIn;

View File

@ -1,7 +1,10 @@
import D from 'debug';
import express from 'express';
import expressSession from 'express-session';
import socketSession from 'express-socket.io-session';
const debug = D('@latus/user/session');
export const session = (options) => expressSession({
resave: false,
sameSite: true,
@ -12,17 +15,20 @@ export const session = (options) => expressSession({
export default {
hooks: {
'@latus/http/middleware': (httpServer, latus) => (req, res, next) => {
'@latus/http/request': (latus) => (req, res, next) => {
debug('@latus/http/request: express.urlencoded()');
express.urlencoded({extended: true})(req, res, () => {
debug('@latus/http/request: session()');
session(
latus.invokeReduce('@latus/http/session'),
)(req, res, next);
});
},
'@latus/socket/middleware': (socketServer, latus) => (
'@latus/socket/connection': (latus) => (socket, next) => {
debug('@latus/socket/connection: session()');
socketSession(session(
latus.invokeReduce('@latus/socket/session'),
))
),
))(socket, next);
},
},
};

View File

@ -17,7 +17,7 @@
"@latus/redis": "1.0.0",
"@latus/repl": "1.0.0",
"@latus/socket": "1.0.0",
"@latus/user": "1.0.0",
"@latus/user": "^1.0.0",
"dotenv": "8.2.0",
"react": "17.0.1",
"react-hot-loader": "4.13.0"

View File

@ -913,8 +913,8 @@
"@latus/core@1.0.0", "@latus/core@^1.0.0":
version "1.0.0"
resolved "https://npm.i12e.cha0s.io/@latus%2fcore/-/core-1.0.0.tgz#f34fae3aada01a49c0ed4a6d3685fc0a78a6add9"
integrity sha512-y7FM+7G/8WGWJkrRkvEfEC+v5BtYhl20Yd+pEWEMqlYG0HCERiJBviJzGG9ihdUIUa8Mnf0rS6JCIknZ5/1sng==
resolved "https://npm.i12e.cha0s.io/@latus%2fcore/-/core-1.0.0.tgz#c732ccacc0d32c11914403ec99a226ae1f94a507"
integrity sha512-uZxiZOddljBiETmSKWH/CtOCRoCZm9p6oZ2jXpnjng34jhaEXDVp2YqOcLbVu7hgiBfaEaXOY53hPSHcMu9YmQ==
dependencies:
debug "4.3.1"
js-yaml "3.14.0"
@ -933,8 +933,8 @@
"@latus/http@1.0.0", "@latus/http@^1.0.0":
version "1.0.0"
resolved "https://npm.i12e.cha0s.io/@latus%2fhttp/-/http-1.0.0.tgz#f2c6ebf9e423bb0e285ec30c8e15625c1351f0d9"
integrity sha512-+o+eQtoE+cbpR9/x+lMU/9gLZwrfFxjZj/cDkeDJAOjIjZhTxsbewWXjmx1KVdUppOAMCu5Y87l1mWBSXHTaDQ==
resolved "https://npm.i12e.cha0s.io/@latus%2fhttp/-/http-1.0.0.tgz#f91fa418b3f60c6ba736b515e531f6c47d5442d6"
integrity sha512-1/XFf53b1CGix8ieq0Og5iGKbUYXADOgBh5EJBil1g2vbxcoss+BcclDE9wceSTxwm310/1kxK/wED9xLAAdNg==
dependencies:
"@latus/core" "1.0.0"
"@neutrinojs/web" "^9.1.0"
@ -978,8 +978,8 @@
"@latus/socket@1.0.0":
version "1.0.0"
resolved "https://npm.i12e.cha0s.io/@latus%2fsocket/-/socket-1.0.0.tgz#53ff67747637c8da8ac9e9fbf34b0014c9307790"
integrity sha512-KqWklsEql2lTWSoCXf/xulNmnOrMsyHWNm/8ui2pcA4nZ132RNtGM1kQbTUd/qdCQD2itaZ64mkVGHWwsgVSwA==
resolved "https://npm.i12e.cha0s.io/@latus%2fsocket/-/socket-1.0.0.tgz#470f51e7d22acc3ef36a3da923337c5998879f63"
integrity sha512-ZqqFVMWojuEnu9S44miva+8n+INMn6LEQ459MSSVHyfrX10d2EFQjSo8NAGEx/0FH8H0gQcPsEc0Gz2DHkBY3Q==
dependencies:
"@latus/core" "^1.0.0"
"@latus/http" "^1.0.0"
@ -988,10 +988,10 @@
socket.io "2.3.0"
socket.io-client "2.3.0"
"@latus/user@1.0.0":
"@latus/user@^1.0.0":
version "1.0.0"
resolved "https://npm.i12e.cha0s.io/@latus%2fuser/-/user-1.0.0.tgz#8ed5a1196c8328af42d5ac04eeb2562a28fa57de"
integrity sha512-5gK6JUnQFteLKXuek6ypWcCbtvkqkkUjefeLk/7DIiuh+W/02RNp8XH/j4zOCbr1b4oN/J7j6xvIYCfZeayGhg==
resolved "https://npm.i12e.cha0s.io/@latus%2fuser/-/user-1.0.0.tgz#0a7329cfeb447d1f677991f3b4816986a451d252"
integrity sha512-/iaEwBv0f1aVky6O9AXWHPyfZXMVb14Jd/0DlMEN/xSmhQb/txmGdUsfzYcU+lcA5LBPaRTfAahaOo8UDeefdg==
dependencies:
"@latus/db" "^1.0.0"
bcrypt "^5.0.0"
@ -2068,9 +2068,9 @@ camelcase@^5.0.0, camelcase@^5.3.1:
integrity sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==
caniuse-lite@^1.0.30001164:
version "1.0.30001164"
resolved "https://npm.i12e.cha0s.io/caniuse-lite/-/caniuse-lite-1.0.30001164.tgz#5bbfd64ca605d43132f13cc7fdabb17c3036bfdc"
integrity sha512-G+A/tkf4bu0dSp9+duNiXc7bGds35DioCyC6vgK2m/rjA4Krpy5WeZgZyfH2f0wj2kI6yAWWucyap6oOwmY1mg==
version "1.0.30001165"
resolved "https://npm.i12e.cha0s.io/caniuse-lite/-/caniuse-lite-1.0.30001165.tgz#32955490d2f60290bb186bb754f2981917fa744f"
integrity sha512-8cEsSMwXfx7lWSUMA2s08z9dIgsnR5NAqjXP23stdsU3AUWkCr/rr4s4OFtHXn5XXr6+7kam3QFVoYyXNPdJPA==
chalk@^2.0.0, chalk@^2.1.0, chalk@^2.4.2:
version "2.4.2"