From ea3beceef7ac4a4ca4ee89c97854d7fd1d366c18 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Tue, 26 Jul 2016 18:32:33 +0800 Subject: [PATCH] feat: enable maxrequests middleware (#1003) --- Makefile | 20 ++++++-------------- package.json | 1 + servers/registry.js | 27 +++++++-------------------- servers/web.js | 19 +++---------------- worker.js | 13 ------------- 5 files changed, 17 insertions(+), 63 deletions(-) diff --git a/Makefile b/Makefile index 8c6a319..115e158 100644 --- a/Makefile +++ b/Makefile @@ -4,14 +4,7 @@ TIMEOUT = 30000 MOCHA_OPTS = DB = sqlite -install: - @npm install --build-from-source --registry=https://registry.npm.taobao.org \ - --disturl=https://npm.taobao.org/mirrors/node - -install-production production: - @NODE_ENV=production $(MAKE) install - -jshint: install +jshint: @node_modules/.bin/jshint . init-database: @@ -25,7 +18,7 @@ init-pg: @psql -c 'DROP DATABASE IF EXISTS cnpmjs_test;' @psql -c 'CREATE DATABASE cnpmjs_test;' -test: install init-database +test: init-database @NODE_ENV=test DB=${DB} node_modules/.bin/mocha \ --reporter $(REPORTER) \ --timeout $(TIMEOUT) \ @@ -47,7 +40,7 @@ test-pg: init-pg test-all: test-sqlite test-mysql -test-cov cov: install init-database +test-cov cov: init-database @NODE_ENV=test DB=${DB} node \ node_modules/.bin/istanbul cover \ node_modules/.bin/_mocha \ @@ -67,7 +60,7 @@ test-cov-sqlite: test-cov-mysql: init-mysql @$(MAKE) test-cov DB=mysql -test-travis: install init-database +test-travis: init-database @NODE_ENV=test DB=${DB} CNPM_SOURCE_NPM=https://registry.npmjs.com CNPM_SOURCE_NPM_ISCNPM=false \ node \ node_modules/.bin/istanbul cover \ @@ -98,15 +91,14 @@ test-travis-all: jshint test-travis-sqlite test-travis-mysql test-travis-pg dev: @NODE_ENV=development node node_modules/.bin/node-dev dispatch.js -contributors: install +contributors: @node_modules/.bin/contributors -f plain -o AUTHORS -autod: install +autod: @node_modules/.bin/autod -w \ --prefix "~" \ --exclude public,view,docs,backup,coverage \ --dep mysql \ --keep should,supertest,should-http,chunkstream,mm,pedding - @$(MAKE) install .PHONY: test diff --git a/package.json b/package.json index f77a4e5..a7be9d3 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "koa": "^1.2.0", "koa-limit": "^1.0.2", "koa-markdown": "^2.0.1", + "koa-maxrequests": "^1.0.0", "koa-middlewares": "^2.1.0", "koa-mock": "^1.6.1", "koa-safe-jsonp": "^0.3.1", diff --git a/servers/registry.js b/servers/registry.js index bb0e957..8b8a65e 100644 --- a/servers/registry.js +++ b/servers/registry.js @@ -1,19 +1,4 @@ -/**! - * cnpmjs.org - servers/registry.js - * - * Copyright(c) cnpmjs.org and other contributors. - * MIT Licensed - * - * Authors: - * dead_horse - * fengmk2 (http://fengmk2.github.com) - */ - -"use strict"; - -/** - * Module dependencies. - */ +'use strict'; var koa = require('koa'); var app = module.exports = koa(); @@ -28,25 +13,27 @@ var staticCache = require('../middleware/static'); var notFound = require('../middleware/registry_not_found'); var cors = require('kcors'); var proxyToNpm = require('../middleware/proxy_to_npm'); +var maxrequests = require('koa-maxrequests'); +app.use(maxrequests()); app.use(block()); middlewares.jsonp(app); -app.use(middlewares.rt({headerName: 'X-ReadTime'})); +app.use(middlewares.rt({ headerName: 'X-ReadTime' })); app.use(middlewares.rewrite('/favicon.ico', '/favicon.png')); app.use(staticCache); app.keys = ['todokey', config.sessionSecret]; app.proxy = true; -app.use(middlewares.bodyParser({jsonLimit: config.jsonLimit})); +app.use(middlewares.bodyParser({ jsonLimit: config.jsonLimit })); app.use(cors({ - allowMethods: 'GET,HEAD' + allowMethods: 'GET,HEAD', })); app.use(auth()); app.use(proxyToNpm()); app.use(notFound); if (config.enableCompress) { - app.use(middlewares.compress({threshold: 150})); + app.use(middlewares.compress({ threshold: 150 })); } app.use(middlewares.conditional()); app.use(middlewares.etag()); diff --git a/servers/web.js b/servers/web.js index 5d487d8..9bdbc8f 100644 --- a/servers/web.js +++ b/servers/web.js @@ -1,19 +1,4 @@ -/**! - * cnpmjs.org - servers/web.js - * - * Copyright(c) cnpmjs.org and other contributors. - * MIT Licensed - * - * Authors: - * dead_horse - * fengmk2 (http://fengmk2.com) - */ - -"use strict"; - -/** - * Module dependencies. - */ +'use strict'; var opensearch = require('../middleware/opensearch'); var notFound = require('../middleware/web_not_found'); @@ -32,6 +17,7 @@ var path = require('path'); var http = require('http'); var koa = require('koa'); var fs = require('fs'); +var maxrequests = require('koa-maxrequests'); var app = koa(); @@ -39,6 +25,7 @@ jsonp(app); var rootdir = path.dirname(__dirname); +app.use(maxrequests()); app.use(block()); app.use(middlewares.rt({headerName: 'X-ReadTime'})); app.use(middlewares.rewrite('/favicon.ico', '/favicon.png')); diff --git a/worker.js b/worker.js index 919b242..ba02f6d 100644 --- a/worker.js +++ b/worker.js @@ -1,18 +1,5 @@ -/**! - * Copyright(c) cnpmjs.org and other contributors. - * MIT Licensed - * - * Authors: - * dead_horse - * fengmk2 (http://fengmk2.com) - */ - 'use strict'; -/** - * Module dependencies. - */ - var graceful = require('graceful'); var registry = require('./servers/registry'); var web = require('./servers/web');