refactor: sane middleware API
This commit is contained in:
parent
c0af9ba70d
commit
48232aea3f
|
@ -24,5 +24,6 @@ export default {
|
||||||
id,
|
id,
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
|
'@latus/repl/context': (latus) => ({latus}),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -79,24 +79,19 @@ export default class Plugins {
|
||||||
}
|
}
|
||||||
|
|
||||||
invokeMiddleware(hook, ...args) {
|
invokeMiddleware(hook, ...args) {
|
||||||
|
debug('invokeMiddleware(...): %s', hook);
|
||||||
if (!this.hooks[hook]) {
|
if (!this.hooks[hook]) {
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
}
|
}
|
||||||
const parts = hook.split('/');
|
const parts = hook.split('/');
|
||||||
const key = parts.pop();
|
const key = parts.pop();
|
||||||
const plugins = this.config[parts.join('/')][key];
|
const plugins = this.config[parts.join('/')][key];
|
||||||
|
debug('middleware: %O', plugins);
|
||||||
|
|
||||||
const fns = plugins
|
const fns = plugins
|
||||||
.map((plugin) => this.hooks[hook].find(({plugin: candidate}) => plugin === candidate).fn);
|
.map((plugin) => this.hooks[hook].find(({plugin: candidate}) => plugin === candidate).fn);
|
||||||
const middleware = new Middleware(fns);
|
const middleware = new Middleware(fns.map((fn) => fn(this)));
|
||||||
return new Promise((resolve, reject) => {
|
return middleware.promise(args.concat(this));
|
||||||
middleware.dispatch(...(args.concat(this).concat((error) => {
|
|
||||||
if (error) {
|
|
||||||
reject(error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
resolve();
|
|
||||||
})));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
invokePlugin(hook, plugin, ...args) {
|
invokePlugin(hook, plugin, ...args) {
|
||||||
|
|
|
@ -32,6 +32,18 @@ export default class Middleware {
|
||||||
invoke();
|
invoke();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
promise(...args) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
this.dispatch(...(args.concat((error) => {
|
||||||
|
if (error) {
|
||||||
|
reject(error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
resolve();
|
||||||
|
})));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
use(fn) {
|
use(fn) {
|
||||||
this.middleware.push(fn);
|
this.middleware.push(fn);
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,9 +4,9 @@ import {createHttpServer} from './server';
|
||||||
export default {
|
export default {
|
||||||
hooks: {
|
hooks: {
|
||||||
'@latus/core/config': () => ({
|
'@latus/core/config': () => ({
|
||||||
middleware: [],
|
|
||||||
host: '0.0.0.0',
|
host: '0.0.0.0',
|
||||||
port: 32340,
|
port: 32340,
|
||||||
|
request: [],
|
||||||
}),
|
}),
|
||||||
'@latus/core/build': (configs) => {
|
'@latus/core/build': (configs) => {
|
||||||
// eslint-disable-next-line global-require, no-param-reassign
|
// eslint-disable-next-line global-require, no-param-reassign
|
||||||
|
|
|
@ -12,18 +12,15 @@ const {
|
||||||
} = process.env;
|
} = process.env;
|
||||||
|
|
||||||
export const createHttpServer = async (latus) => {
|
export const createHttpServer = async (latus) => {
|
||||||
const {config: {'@latus/http': {host, middleware, port}}} = latus;
|
const {config: {'@latus/http': {host, port}}} = latus;
|
||||||
const app = express();
|
const app = express();
|
||||||
const httpServer = createServer(app);
|
const httpServer = createServer(app);
|
||||||
// eslint-disable-next-line no-param-reassign
|
// eslint-disable-next-line no-param-reassign
|
||||||
latus.config['%http'] = httpServer;
|
latus.config['%http'] = httpServer;
|
||||||
httpServer.app = app;
|
httpServer.app = app;
|
||||||
// Request middleware.
|
app.use((req, res, next) => {
|
||||||
for (let i = 0; i < middleware.length; i++) {
|
latus.invokeMiddleware('@latus/http/request', req, res).then(next).catch(next);
|
||||||
app.use(
|
});
|
||||||
latus.invokePlugin('@latus/http/middleware', middleware[i], httpServer),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
// Serve latus.
|
// Serve latus.
|
||||||
app.use(latusMiddleware(latus));
|
app.use(latusMiddleware(latus));
|
||||||
app.use(express.static(join(__dirname, 'client')));
|
app.use(express.static(join(__dirname, 'client')));
|
||||||
|
|
|
@ -3,7 +3,7 @@ import {createSocketServer} from './lifecycle';
|
||||||
export default {
|
export default {
|
||||||
hooks: {
|
hooks: {
|
||||||
'@latus/core/config': () => ({
|
'@latus/core/config': () => ({
|
||||||
middleware: [],
|
connection: [],
|
||||||
}),
|
}),
|
||||||
'@latus/http/plugins': () => ({
|
'@latus/http/plugins': () => ({
|
||||||
'@latus/socket/client': {},
|
'@latus/socket/client': {},
|
||||||
|
|
|
@ -19,7 +19,6 @@ export default class SocketServer extends decorate(Class) {
|
||||||
const {
|
const {
|
||||||
config: {
|
config: {
|
||||||
'@latus/core': {id},
|
'@latus/core': {id},
|
||||||
'@latus/socket': {middleware},
|
|
||||||
},
|
},
|
||||||
} = latus;
|
} = latus;
|
||||||
this.io = SocketIoServer(httpServer, {
|
this.io = SocketIoServer(httpServer, {
|
||||||
|
@ -30,12 +29,9 @@ export default class SocketServer extends decorate(Class) {
|
||||||
...latus.invokeReduce('@latus/socket/server'),
|
...latus.invokeReduce('@latus/socket/server'),
|
||||||
});
|
});
|
||||||
this.io.on('connect', this.onConnect);
|
this.io.on('connect', this.onConnect);
|
||||||
for (let i = 0; i < middleware.length; i++) {
|
this.io.use((socket, next) => {
|
||||||
const fn = latus.invokePlugin('@latus/socket/middleware', middleware[i], this);
|
latus.invokeMiddleware('@latus/socket/connection', socket).then(next).catch(next);
|
||||||
if (fn) {
|
});
|
||||||
this.io.use(fn);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const Packets = latus.invokeReduce('@latus/socket/packets');
|
const Packets = latus.invokeReduce('@latus/socket/packets');
|
||||||
// eslint-disable-next-line no-param-reassign
|
// eslint-disable-next-line no-param-reassign
|
||||||
latus.config['%packets'] = latus.invokeComposed('@latus/socket/packets/decorate', Packets);
|
latus.config['%packets'] = latus.invokeComposed('@latus/socket/packets/decorate', Packets);
|
||||||
|
|
|
@ -1,15 +1,22 @@
|
||||||
|
import D from 'debug';
|
||||||
import passport from 'passport';
|
import passport from 'passport';
|
||||||
import LogOps from 'passport/lib/http/request';
|
import LogOps from 'passport/lib/http/request';
|
||||||
|
|
||||||
|
const debug = D('@latus/user/passport');
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
hooks: {
|
hooks: {
|
||||||
'@latus/http/middleware': () => (req, res, next) => {
|
'@latus/http/request': () => (req, res, next) => {
|
||||||
|
debug('@latus/http/request: passport.initialize()');
|
||||||
passport.initialize()(req, res, () => {
|
passport.initialize()(req, res, () => {
|
||||||
|
debug('@latus/http/request: passport.session()');
|
||||||
passport.session()(req, res, next);
|
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, () => {
|
passport.initialize()(socket.handshake, undefined, () => {
|
||||||
|
debug('@latus/socket/connection: passport.session()');
|
||||||
passport.session()(socket.handshake, undefined, () => {
|
passport.session()(socket.handshake, undefined, () => {
|
||||||
/* eslint-disable no-param-reassign */
|
/* eslint-disable no-param-reassign */
|
||||||
socket.handshake.login = LogOps.logIn;
|
socket.handshake.login = LogOps.logIn;
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
|
import D from 'debug';
|
||||||
import express from 'express';
|
import express from 'express';
|
||||||
import expressSession from 'express-session';
|
import expressSession from 'express-session';
|
||||||
import socketSession from 'express-socket.io-session';
|
import socketSession from 'express-socket.io-session';
|
||||||
|
|
||||||
|
const debug = D('@latus/user/session');
|
||||||
|
|
||||||
export const session = (options) => expressSession({
|
export const session = (options) => expressSession({
|
||||||
resave: false,
|
resave: false,
|
||||||
sameSite: true,
|
sameSite: true,
|
||||||
|
@ -12,17 +15,20 @@ export const session = (options) => expressSession({
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
hooks: {
|
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, () => {
|
express.urlencoded({extended: true})(req, res, () => {
|
||||||
|
debug('@latus/http/request: session()');
|
||||||
session(
|
session(
|
||||||
latus.invokeReduce('@latus/http/session'),
|
latus.invokeReduce('@latus/http/session'),
|
||||||
)(req, res, next);
|
)(req, res, next);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
'@latus/socket/middleware': (socketServer, latus) => (
|
'@latus/socket/connection': (latus) => (socket, next) => {
|
||||||
|
debug('@latus/socket/connection: session()');
|
||||||
socketSession(session(
|
socketSession(session(
|
||||||
latus.invokeReduce('@latus/socket/session'),
|
latus.invokeReduce('@latus/socket/session'),
|
||||||
))
|
))(socket, next);
|
||||||
),
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
"@latus/redis": "1.0.0",
|
"@latus/redis": "1.0.0",
|
||||||
"@latus/repl": "1.0.0",
|
"@latus/repl": "1.0.0",
|
||||||
"@latus/socket": "1.0.0",
|
"@latus/socket": "1.0.0",
|
||||||
"@latus/user": "1.0.0",
|
"@latus/user": "^1.0.0",
|
||||||
"dotenv": "8.2.0",
|
"dotenv": "8.2.0",
|
||||||
"react": "17.0.1",
|
"react": "17.0.1",
|
||||||
"react-hot-loader": "4.13.0"
|
"react-hot-loader": "4.13.0"
|
||||||
|
|
|
@ -913,8 +913,8 @@
|
||||||
|
|
||||||
"@latus/core@1.0.0", "@latus/core@^1.0.0":
|
"@latus/core@1.0.0", "@latus/core@^1.0.0":
|
||||||
version "1.0.0"
|
version "1.0.0"
|
||||||
resolved "https://npm.i12e.cha0s.io/@latus%2fcore/-/core-1.0.0.tgz#f34fae3aada01a49c0ed4a6d3685fc0a78a6add9"
|
resolved "https://npm.i12e.cha0s.io/@latus%2fcore/-/core-1.0.0.tgz#c732ccacc0d32c11914403ec99a226ae1f94a507"
|
||||||
integrity sha512-y7FM+7G/8WGWJkrRkvEfEC+v5BtYhl20Yd+pEWEMqlYG0HCERiJBviJzGG9ihdUIUa8Mnf0rS6JCIknZ5/1sng==
|
integrity sha512-uZxiZOddljBiETmSKWH/CtOCRoCZm9p6oZ2jXpnjng34jhaEXDVp2YqOcLbVu7hgiBfaEaXOY53hPSHcMu9YmQ==
|
||||||
dependencies:
|
dependencies:
|
||||||
debug "4.3.1"
|
debug "4.3.1"
|
||||||
js-yaml "3.14.0"
|
js-yaml "3.14.0"
|
||||||
|
@ -933,8 +933,8 @@
|
||||||
|
|
||||||
"@latus/http@1.0.0", "@latus/http@^1.0.0":
|
"@latus/http@1.0.0", "@latus/http@^1.0.0":
|
||||||
version "1.0.0"
|
version "1.0.0"
|
||||||
resolved "https://npm.i12e.cha0s.io/@latus%2fhttp/-/http-1.0.0.tgz#f2c6ebf9e423bb0e285ec30c8e15625c1351f0d9"
|
resolved "https://npm.i12e.cha0s.io/@latus%2fhttp/-/http-1.0.0.tgz#f91fa418b3f60c6ba736b515e531f6c47d5442d6"
|
||||||
integrity sha512-+o+eQtoE+cbpR9/x+lMU/9gLZwrfFxjZj/cDkeDJAOjIjZhTxsbewWXjmx1KVdUppOAMCu5Y87l1mWBSXHTaDQ==
|
integrity sha512-1/XFf53b1CGix8ieq0Og5iGKbUYXADOgBh5EJBil1g2vbxcoss+BcclDE9wceSTxwm310/1kxK/wED9xLAAdNg==
|
||||||
dependencies:
|
dependencies:
|
||||||
"@latus/core" "1.0.0"
|
"@latus/core" "1.0.0"
|
||||||
"@neutrinojs/web" "^9.1.0"
|
"@neutrinojs/web" "^9.1.0"
|
||||||
|
@ -978,8 +978,8 @@
|
||||||
|
|
||||||
"@latus/socket@1.0.0":
|
"@latus/socket@1.0.0":
|
||||||
version "1.0.0"
|
version "1.0.0"
|
||||||
resolved "https://npm.i12e.cha0s.io/@latus%2fsocket/-/socket-1.0.0.tgz#53ff67747637c8da8ac9e9fbf34b0014c9307790"
|
resolved "https://npm.i12e.cha0s.io/@latus%2fsocket/-/socket-1.0.0.tgz#470f51e7d22acc3ef36a3da923337c5998879f63"
|
||||||
integrity sha512-KqWklsEql2lTWSoCXf/xulNmnOrMsyHWNm/8ui2pcA4nZ132RNtGM1kQbTUd/qdCQD2itaZ64mkVGHWwsgVSwA==
|
integrity sha512-ZqqFVMWojuEnu9S44miva+8n+INMn6LEQ459MSSVHyfrX10d2EFQjSo8NAGEx/0FH8H0gQcPsEc0Gz2DHkBY3Q==
|
||||||
dependencies:
|
dependencies:
|
||||||
"@latus/core" "^1.0.0"
|
"@latus/core" "^1.0.0"
|
||||||
"@latus/http" "^1.0.0"
|
"@latus/http" "^1.0.0"
|
||||||
|
@ -988,10 +988,10 @@
|
||||||
socket.io "2.3.0"
|
socket.io "2.3.0"
|
||||||
socket.io-client "2.3.0"
|
socket.io-client "2.3.0"
|
||||||
|
|
||||||
"@latus/user@1.0.0":
|
"@latus/user@^1.0.0":
|
||||||
version "1.0.0"
|
version "1.0.0"
|
||||||
resolved "https://npm.i12e.cha0s.io/@latus%2fuser/-/user-1.0.0.tgz#8ed5a1196c8328af42d5ac04eeb2562a28fa57de"
|
resolved "https://npm.i12e.cha0s.io/@latus%2fuser/-/user-1.0.0.tgz#0a7329cfeb447d1f677991f3b4816986a451d252"
|
||||||
integrity sha512-5gK6JUnQFteLKXuek6ypWcCbtvkqkkUjefeLk/7DIiuh+W/02RNp8XH/j4zOCbr1b4oN/J7j6xvIYCfZeayGhg==
|
integrity sha512-/iaEwBv0f1aVky6O9AXWHPyfZXMVb14Jd/0DlMEN/xSmhQb/txmGdUsfzYcU+lcA5LBPaRTfAahaOo8UDeefdg==
|
||||||
dependencies:
|
dependencies:
|
||||||
"@latus/db" "^1.0.0"
|
"@latus/db" "^1.0.0"
|
||||||
bcrypt "^5.0.0"
|
bcrypt "^5.0.0"
|
||||||
|
@ -2068,9 +2068,9 @@ camelcase@^5.0.0, camelcase@^5.3.1:
|
||||||
integrity sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==
|
integrity sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==
|
||||||
|
|
||||||
caniuse-lite@^1.0.30001164:
|
caniuse-lite@^1.0.30001164:
|
||||||
version "1.0.30001164"
|
version "1.0.30001165"
|
||||||
resolved "https://npm.i12e.cha0s.io/caniuse-lite/-/caniuse-lite-1.0.30001164.tgz#5bbfd64ca605d43132f13cc7fdabb17c3036bfdc"
|
resolved "https://npm.i12e.cha0s.io/caniuse-lite/-/caniuse-lite-1.0.30001165.tgz#32955490d2f60290bb186bb754f2981917fa744f"
|
||||||
integrity sha512-G+A/tkf4bu0dSp9+duNiXc7bGds35DioCyC6vgK2m/rjA4Krpy5WeZgZyfH2f0wj2kI6yAWWucyap6oOwmY1mg==
|
integrity sha512-8cEsSMwXfx7lWSUMA2s08z9dIgsnR5NAqjXP23stdsU3AUWkCr/rr4s4OFtHXn5XXr6+7kam3QFVoYyXNPdJPA==
|
||||||
|
|
||||||
chalk@^2.0.0, chalk@^2.1.0, chalk@^2.4.2:
|
chalk@^2.0.0, chalk@^2.1.0, chalk@^2.4.2:
|
||||||
version "2.4.2"
|
version "2.4.2"
|
||||||
|
|
Loading…
Reference in New Issue
Block a user