Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #268 from cnpm/issue267-limit
Browse files Browse the repository at this point in the history
add koa-limit, fixed #267
  • Loading branch information
fengmk2 committed Mar 13, 2014
2 parents e497fb3 + 7aa2ea3 commit 8d8fd31
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 1 deletion.
37 changes: 37 additions & 0 deletions common/redis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*!
* cnpmjs.org - common/redis.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* dead_horse <dead_horse@qq.com> (http://deadhorse.me)
*/

'use strict';

/**
* Module dependencies.
*/

var config = require('../config');

// close redis by set config.redis to `null` or `{}`
if (!config.redis || !config.redis.host || !config.redis.port) {

var redis = require('redis');
var wrapper = require('co-redis');
var logger = require('./logger');

var _client = redis.createClient(config.redis);

_client.on('error', function (err) {
logger.error(err);
});

module.exports = wrapper(_client);

} else {
console.warn('can not found')
module.exports = null;
}
9 changes: 9 additions & 0 deletions config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ var config = {
syncModel: 'none', // 'none', 'all', 'exist'
syncConcurrency: 1,
maxDependencies: 200, // max handle number of package.json `dependencies` property

limit: {
enable: false,
token: 'koa-limit:download',
limit: 1000,
interval: 1000 * 60 * 60 * 24,
writeList: [],
blackList: []
}
};

// load config/config.js, everything in config.js will cover the same key in index.js
Expand Down
35 changes: 35 additions & 0 deletions middleware/limit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**!
* cnpmjs.org - middleware/limit.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* dead_horse <dead_horse@qq.com> (http://deadhorse.me)
*/

'use strict';

/**
* Module dependencies.
*/

var config = require('../config');
var limit = require('koa-limit');
var store = require('../common/redis');

var limitConfig = config.limit;

if (!limitConfig.enable) {
module.exports = function *ignoreLimit(next) {
yield *next;
}
} else {

if (!config.debug) {
limitConfig.store = store;
}

module.exports = limit(limitConfig);
}

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"co": "3.0.4",
"co-gather": "0.0.1",
"co-read": "0.0.1",
"co-redis": "1.1.0",
"co-write": "0.3.0",
"debug": "0.7.4",
"eventproxy": "0.3.0",
Expand All @@ -26,6 +27,7 @@
"gravatar": "1.0.6",
"humanize-number": "0.0.2",
"koa": "0.5.1",
"koa-limit": "0.0.2",
"koa-markdown": "0.0.3",
"koa-middlewares": "0.0.9",
"logfilestream": "0.1.0",
Expand All @@ -39,6 +41,7 @@
"nodemailer": "0.6.1",
"qn": "0.2.1",
"ready": "0.1.1",
"redis": "0.10.1",
"semver": "2.2.1",
"thunkify-wrap": "0.0.5",
"urllib": "0.5.10",
Expand Down
4 changes: 3 additions & 1 deletion routes/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

var middlewares = require('koa-middlewares');
var limit = require('../middleware/limit');
var login = require('../middleware/login');
var publishable = require('../middleware/publishable');
var syncByInstall = require('../middleware/sync_by_install');
Expand Down Expand Up @@ -44,7 +45,8 @@ function routes(app) {
app.put('/:name/sync', sync.sync);
app.get('/:name/sync/log/:id', sync.getSyncLog);

app.get('/:name/download/:filename', mod.download);
// need limit by ip
app.get('/:name/download/:filename', limit, mod.download);

// put tarball
// https://registry.npmjs.org/cnpmjs.org/-/cnpmjs.org-0.0.0.tgz/-rev/1-c85bc65e8d2470cc4d82b8f40da65b8e
Expand Down

0 comments on commit 8d8fd31

Please sign in to comment.