Skip to content

Commit

Permalink
feat: support customized middlewares (#1436)
Browse files Browse the repository at this point in the history
  • Loading branch information
XadillaX authored and fengmk2 committed Feb 19, 2019
1 parent 444b5d1 commit 99c4c3f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
8 changes: 8 additions & 0 deletions config/index.js
Expand Up @@ -50,6 +50,8 @@ var config = {
// view files directory
viewDir: path.join(root, 'view', 'web'),

customRegistryMiddlewares: [],

// config for koa-limit middleware
// for limit download rates
limit: {
Expand Down Expand Up @@ -268,6 +270,12 @@ var config = {

if (process.env.NODE_ENV === 'test') {
config.enableAbbreviatedMetadata = true;
config.customRegistryMiddlewares.push(() => {
return function* (next) {
this.set('x-custom-middleware', 'true');
yield next;
};
});
}

if (process.env.NODE_ENV !== 'test') {
Expand Down
6 changes: 5 additions & 1 deletion servers/registry.js
Expand Up @@ -23,7 +23,7 @@ var maxrequests = require('koa-maxrequests');
var proxy = require('koa-proxy');
app.use(proxy({
host: 'https://registry.npmjs.org',
match: /^\/\-\/npm\/v1\/security\/audits/
match: /^\/\-\/npm\/v1\/security\/audits/
}));


Expand Down Expand Up @@ -51,6 +51,10 @@ app.use(notFound);
app.use(conditional());
app.use(etag());

for (const middleware of config.customRegistryMiddlewares) {
app.use(middleware(app));
}

/**
* Routes
*/
Expand Down
12 changes: 12 additions & 0 deletions test/controllers/registry/package/list.test.js
Expand Up @@ -41,6 +41,18 @@ describe('test/controllers/registry/package/list.test.js', () => {
});
});

it('should use costomized registry middleware', done => {
request(app)
.get('/@cnpmtest/testmodule-list-1')
.expect(200, function (err, res) {
should.not.exist(err);
var data = res.body;
data.name.should.equal('@cnpmtest/testmodule-list-1');
assert(res.headers['x-custom-middleware'] === 'true');
done();
});
});

it('should return all versions', done => {
request(app)
.get('/@cnpmtest/testmodule-list-1')
Expand Down

0 comments on commit 99c4c3f

Please sign in to comment.