Browse Source

path blacklist.. etags..

master
Lloyd Brookes 9 years ago
parent
commit
15e0074770
  1. 1
      .travis.yml
  2. 28
      lib/local-web-server.js
  3. 14
      package.json

1
.travis.yml

@ -2,4 +2,3 @@ language: node_js
node_js: node_js:
- '5.0' - '5.0'
- '4.2' - '4.2'
- '0.12'

28
lib/local-web-server.js

@ -8,16 +8,14 @@ const morgan = require('koa-morgan')
const compress = require('koa-compress') const compress = require('koa-compress')
const streamLogStats = require('stream-log-stats') const streamLogStats = require('stream-log-stats')
const cors = require('kcors') const cors = require('kcors')
const conditional = require('koa-conditional-get');
const etag = require('koa-etag');
/** /**
* @module local-web-server * @module local-web-server
*/ */
module.exports = getApp module.exports = getApp
process.on('unhandledRejection', (reason, p) => {
throw reason
})
function getApp (options) { function getApp (options) {
options = Object.assign({ options = Object.assign({
static: {}, static: {},
@ -26,7 +24,6 @@ function getApp (options) {
compress: false compress: false
}, options) }, options)
const log = options.log const log = options.log
log.options = log.options || {} log.options = log.options || {}
@ -35,6 +32,19 @@ function getApp (options) {
/* CORS: allow from any origin */ /* CORS: allow from any origin */
app.use(convert(cors())) 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) { if (options.mime) {
app.use((ctx, next) => { app.use((ctx, next) => {
return next().then(() => { return next().then(() => {
@ -47,6 +57,7 @@ function getApp (options) {
}) })
} }
/* compress response */
if (options.compress) { if (options.compress) {
app.use(convert(compress())) app.use(convert(compress()))
} }
@ -66,9 +77,12 @@ function getApp (options) {
} }
if (log.format) app.use(convert(morgan.middleware(log.format, log.options))) if (log.format) app.use(convert(morgan.middleware(log.format, log.options)))
/* serve static files */
if (options.static.root) { if (options.static.root) {
app.use(convert(serve(options.static.root, options.static.options))) app.use(convert(serve(options.static.root, options.static.options)))
} }
/* serve directory index */
if (options.serveIndex.path) { if (options.serveIndex.path) {
app.use(convert(serveIndex(options.serveIndex.path, options.serveIndex.options))) app.use(convert(serveIndex(options.serveIndex.path, options.serveIndex.options)))
} }
@ -82,3 +96,7 @@ function logstalgiaDate () {
.replace('GMT', '') .replace('GMT', '')
.replace(' (BST)', '') .replace(' (BST)', '')
} }
process.on('unhandledRejection', (reason, p) => {
throw reason
})

14
package.json

@ -7,7 +7,17 @@
}, },
"main": "lib/local-web-server.js", "main": "lib/local-web-server.js",
"license": "MIT", "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": { "engines": {
"node": ">=4.0.0" "node": ">=4.0.0"
}, },
@ -28,7 +38,9 @@
"koa": "^2.0.0-alpha.3", "koa": "^2.0.0-alpha.3",
"koa-charset": "^1.1.4", "koa-charset": "^1.1.4",
"koa-compress": "^1.0.8", "koa-compress": "^1.0.8",
"koa-conditional-get": "^1.0.3",
"koa-convert": "^1.1.0", "koa-convert": "^1.1.0",
"koa-etag": "^2.1.0",
"koa-json": "^1.1.1", "koa-json": "^1.1.1",
"koa-morgan": "^0.4.0", "koa-morgan": "^0.4.0",
"koa-rewrite": "^1.1.1", "koa-rewrite": "^1.1.1",

Loading…
Cancel
Save