From 374a5e8e7b9db0de84390a155a58d2a365cd2f41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ha=CC=8Akan=20Canberger?= Date: Tue, 24 Oct 2017 16:02:08 +0200 Subject: [PATCH] feat: Add middleware plugins from fl4re/sinopia This is basically the PR fl4re/sinopia#18 by @fredr --- conf/full.yaml | 5 +++++ src/api/index.js | 12 ++++++++++++ .../fixtures/plugins/middlewares.js | 17 +++++++++++++++++ test/functional/index.js | 1 + test/functional/plugins/middleware.spec.js | 19 +++++++++++++++++++ test/functional/store/config-2.yaml | 4 ++++ 6 files changed, 58 insertions(+) create mode 100644 test/functional/fixtures/plugins/middlewares.js create mode 100644 test/functional/plugins/middleware.spec.js diff --git a/conf/full.yaml b/conf/full.yaml index 6ce606060..c966dcf51 100644 --- a/conf/full.yaml +++ b/conf/full.yaml @@ -186,3 +186,8 @@ notify: content: ' {{ handlebar-expression }}' # For Slack, follow the following format: # content: '{ "text": "Package *{{ name }}* published to version *{{ dist-tags.latest }}*", "username": "Verdaccio", "icon_emoji": ":package:" }' + +# Configure plugins that can register custom middlewares +#middlewares: +# plugin-name: +# setting: true diff --git a/src/api/index.js b/src/api/index.js index a72c49c36..abb0b25f6 100644 --- a/src/api/index.js +++ b/src/api/index.js @@ -11,6 +11,7 @@ const Cats = require('../lib/status-cats'); const Storage = require('../lib/storage'); const _ = require('lodash'); const cors = require('cors'); +const load_plugins = require('../lib/plugin-loader').load_plugins; module.exports = function(config_hash) { // Config @@ -80,6 +81,17 @@ module.exports = function(config_hash) { }); }); } + // register middleware plugins + const plugin_params = { + config: config, + logger: Logger.logger, + }; + const plugins = load_plugins(config, config.middlewares, plugin_params, function(p) { + return p.register_middlewares; + }); + plugins.forEach(function(p) { + p.register_middlewares(app, auth, storage); + }); // For npm request app.use(require('./endpoint')(config, auth, storage)); diff --git a/test/functional/fixtures/plugins/middlewares.js b/test/functional/fixtures/plugins/middlewares.js new file mode 100644 index 000000000..cf31aeb3c --- /dev/null +++ b/test/functional/fixtures/plugins/middlewares.js @@ -0,0 +1,17 @@ + +module.exports = Plugin + +function Plugin(config, stuff) { + var self = Object.create(Plugin.prototype) + self._config = config + return self +} + +Plugin.prototype.register_middlewares = function (app, auth, storage) { + var message = this._config.message + + app.get('/test/route', function (req, res, next) { + res.status(200) + return next({ ok: message }) + }); +} diff --git a/test/functional/index.js b/test/functional/index.js index 9958d9d57..1ac863f13 100644 --- a/test/functional/index.js +++ b/test/functional/index.js @@ -74,6 +74,7 @@ describe('Create registry servers', function() { require('./adduser/logout')(); require('./tags/addtag.spec')(); require('./plugins/auth.spec')(); + require('./plugins/middleware.spec')(); require('./notifications/notify')(); // requires packages published to server1/server2 require('./uplink.cache.spec')(); diff --git a/test/functional/plugins/middleware.spec.js b/test/functional/plugins/middleware.spec.js new file mode 100644 index 000000000..54f53a826 --- /dev/null +++ b/test/functional/plugins/middleware.spec.js @@ -0,0 +1,19 @@ +'use strict'; + +require('../lib/startup'); + + +module.exports = function () { + const server2 = process.server2; + + describe('middlewares', function() { + it('should serve the registered route', function() { + return server2.request({ + uri: '/test/route', + method: 'GET' + }) + .status(200) + .body_ok('this is a custom route') + }) + }) +} diff --git a/test/functional/store/config-2.yaml b/test/functional/store/config-2.yaml index 005128426..a49d4fd80 100644 --- a/test/functional/store/config-2.yaml +++ b/test/functional/store/config-2.yaml @@ -14,6 +14,10 @@ uplinks: web: enable: true +middlewares: + ../fixtures/plugins/middlewares: + message: this is a custom route + auth: ../fixtures/plugins/authenticate: accept_user: authtest2