From 15e00747702a9971d4c441af5b73164bdce47361 Mon Sep 17 00:00:00 2001 From: Lloyd Brookes Date: Fri, 13 Nov 2015 15:33:19 +0000 Subject: [PATCH] path blacklist.. etags.. --- .travis.yml | 1 - lib/local-web-server.js | 28 +++++++++++++++++++++++----- package.json | 14 +++++++++++++- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index e4f4703..faf9484 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,4 +2,3 @@ language: node_js node_js: - '5.0' - '4.2' -- '0.12' diff --git a/lib/local-web-server.js b/lib/local-web-server.js index 552bfc6..e9ac2cc 100644 --- a/lib/local-web-server.js +++ b/lib/local-web-server.js @@ -8,16 +8,14 @@ const morgan = require('koa-morgan') const compress = require('koa-compress') const streamLogStats = require('stream-log-stats') const cors = require('kcors') +const conditional = require('koa-conditional-get'); +const etag = require('koa-etag'); /** * @module local-web-server */ module.exports = getApp -process.on('unhandledRejection', (reason, p) => { - throw reason -}) - function getApp (options) { options = Object.assign({ static: {}, @@ -26,7 +24,6 @@ function getApp (options) { compress: false }, options) - const log = options.log log.options = log.options || {} @@ -35,6 +32,19 @@ function getApp (options) { /* CORS: allow from any origin */ app.use(convert(cors())) + /* path blacklist */ + app.use(function pathBlacklist (ctx, next) { + if (/css$/.test(ctx.path)) { + ctx.throw(403, 'FUCK NO.') + } else { + return next() + } + }) + + app.use(convert(conditional())) + app.use(convert(etag())) + + /* mime-type overrides */ if (options.mime) { app.use((ctx, next) => { return next().then(() => { @@ -47,6 +57,7 @@ function getApp (options) { }) } + /* compress response */ if (options.compress) { app.use(convert(compress())) } @@ -66,9 +77,12 @@ function getApp (options) { } if (log.format) app.use(convert(morgan.middleware(log.format, log.options))) + /* serve static files */ if (options.static.root) { app.use(convert(serve(options.static.root, options.static.options))) } + + /* serve directory index */ if (options.serveIndex.path) { app.use(convert(serveIndex(options.serveIndex.path, options.serveIndex.options))) } @@ -82,3 +96,7 @@ function logstalgiaDate () { .replace('GMT', '') .replace(' (BST)', '') } + +process.on('unhandledRejection', (reason, p) => { + throw reason +}) diff --git a/package.json b/package.json index 878c447..0052f8c 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,17 @@ }, "main": "lib/local-web-server.js", "license": "MIT", - "keywords": [ "dev", "server", "web", "tool", "front-end", "development", "cors", "mime", "rest" ], + "keywords": [ + "dev", + "server", + "web", + "tool", + "front-end", + "development", + "cors", + "mime", + "rest" + ], "engines": { "node": ">=4.0.0" }, @@ -28,7 +38,9 @@ "koa": "^2.0.0-alpha.3", "koa-charset": "^1.1.4", "koa-compress": "^1.0.8", + "koa-conditional-get": "^1.0.3", "koa-convert": "^1.1.0", + "koa-etag": "^2.1.0", "koa-json": "^1.1.1", "koa-morgan": "^0.4.0", "koa-rewrite": "^1.1.1",