From a183214689a471200b9a81c65705f41dbe528611 Mon Sep 17 00:00:00 2001 From: Karl Morrison Date: Wed, 19 Oct 2016 04:24:53 +0200 Subject: [PATCH 1/3] Fixed Koa compatibility for both Koa & Koa@2 --- middleware/koa/index.js | 1 + middleware/koa/serveStatic.js | 47 +++++++++++++++++++---------------- package.json | 6 ++++- 3 files changed, 31 insertions(+), 23 deletions(-) create mode 100644 middleware/koa/index.js diff --git a/middleware/koa/index.js b/middleware/koa/index.js new file mode 100644 index 00000000..f7f2f5b1 --- /dev/null +++ b/middleware/koa/index.js @@ -0,0 +1 @@ +exports.serveStatic = require('./serveStatic'); \ No newline at end of file diff --git a/middleware/koa/serveStatic.js b/middleware/koa/serveStatic.js index 4c146246..93754ca8 100644 --- a/middleware/koa/serveStatic.js +++ b/middleware/koa/serveStatic.js @@ -1,7 +1,7 @@ require('raptor-polyfill/string/startsWith'); require('raptor-polyfill/string/endsWith'); -var lasso = require('../'); +var lasso = require('../../'); var send = require('send'); var extend = require('raptor-util/extend'); @@ -9,7 +9,7 @@ function notFound() { this.error(404); } -module.exports = function(options) { +module.exports = function (options) { options = options || {}; var myLasso = options.lasso || lasso.getDefaultLasso(); @@ -23,7 +23,7 @@ module.exports = function(options) { } if (!outputDir || !urlPrefix) { - return function(req, res, next) { + return function (req, res, next) { return next(); }; } @@ -40,31 +40,34 @@ module.exports = function(options) { sendOptions.root = outputDir; + return function* (next) { - return function(ctx, next) { - var req = ctx.request, - res = ctx.response; + var ctx = this; - var path = req.path; - if (!path.startsWith(routePrefix) || (req.method !== 'GET' && req.method !== 'HEAD')) { - return next(); + var path = ctx.request.path; + if (!path.startsWith(routePrefix) || (ctx.request.method !== 'GET' && ctx.request.method !== 'HEAD')) { + return yield next; } - var filePath = path.substring(routePrefix.length); - - // create send stream - var stream = send(req, filePath, sendOptions); + ctx.respond = false; - // add directory handler - stream.on('directory', notFound); + var filePath = path.substring(routePrefix.length); - // forward errors - stream.on('error', function error(err) { - res.statusCode = err.statusCode || 500; - res.end('Not found: ' + filePath); + yield new Promise(function (resolve, reject) { + send(ctx.req, filePath, sendOptions) + .on('error', function(err) { + ctx.res.statusCode = err.statusCode || 500; + ctx.res.end('Not found: ' + filePath); + reject(err); + }) + .on('directory', notFound) + .on('headers', function(req, path, stat) { + ctx.status = 200; + }) + .on('end', function() { + resolve(); + }) + .pipe(ctx.res); }); - - // pipe - stream.pipe(res); }; }; diff --git a/package.json b/package.json index e3d7b079..028a8463 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,10 @@ }, "author": "Patrick Steele-Idem ", "maintainers": "Patrick Steele-Idem ", + "contributors": [{ + "name": "Karl Morrison", + "email": "basickarl@gmail.com" + }], "dependencies": { "app-module-path": "^1.0.0", "app-root-dir": "^1.0.0", @@ -68,5 +72,5 @@ "concat", "minify" ], - "version": "2.6.0" + "version": "2.6.2" } From 7894a284870b803695f6127531bc0897d5038bc4 Mon Sep 17 00:00:00 2001 From: Karl Morrison Date: Wed, 19 Oct 2016 04:34:41 +0200 Subject: [PATCH 2/3] Fixed Koa compatibility for both Koa & Koa@2 --- middleware/koa/index.js | 1 + middleware/koa/serveStatic.js | 47 +++++++++++++++++++---------------- package.json | 5 +++- 3 files changed, 30 insertions(+), 23 deletions(-) create mode 100644 middleware/koa/index.js diff --git a/middleware/koa/index.js b/middleware/koa/index.js new file mode 100644 index 00000000..f7f2f5b1 --- /dev/null +++ b/middleware/koa/index.js @@ -0,0 +1 @@ +exports.serveStatic = require('./serveStatic'); \ No newline at end of file diff --git a/middleware/koa/serveStatic.js b/middleware/koa/serveStatic.js index 4c146246..93754ca8 100644 --- a/middleware/koa/serveStatic.js +++ b/middleware/koa/serveStatic.js @@ -1,7 +1,7 @@ require('raptor-polyfill/string/startsWith'); require('raptor-polyfill/string/endsWith'); -var lasso = require('../'); +var lasso = require('../../'); var send = require('send'); var extend = require('raptor-util/extend'); @@ -9,7 +9,7 @@ function notFound() { this.error(404); } -module.exports = function(options) { +module.exports = function (options) { options = options || {}; var myLasso = options.lasso || lasso.getDefaultLasso(); @@ -23,7 +23,7 @@ module.exports = function(options) { } if (!outputDir || !urlPrefix) { - return function(req, res, next) { + return function (req, res, next) { return next(); }; } @@ -40,31 +40,34 @@ module.exports = function(options) { sendOptions.root = outputDir; + return function* (next) { - return function(ctx, next) { - var req = ctx.request, - res = ctx.response; + var ctx = this; - var path = req.path; - if (!path.startsWith(routePrefix) || (req.method !== 'GET' && req.method !== 'HEAD')) { - return next(); + var path = ctx.request.path; + if (!path.startsWith(routePrefix) || (ctx.request.method !== 'GET' && ctx.request.method !== 'HEAD')) { + return yield next; } - var filePath = path.substring(routePrefix.length); - - // create send stream - var stream = send(req, filePath, sendOptions); + ctx.respond = false; - // add directory handler - stream.on('directory', notFound); + var filePath = path.substring(routePrefix.length); - // forward errors - stream.on('error', function error(err) { - res.statusCode = err.statusCode || 500; - res.end('Not found: ' + filePath); + yield new Promise(function (resolve, reject) { + send(ctx.req, filePath, sendOptions) + .on('error', function(err) { + ctx.res.statusCode = err.statusCode || 500; + ctx.res.end('Not found: ' + filePath); + reject(err); + }) + .on('directory', notFound) + .on('headers', function(req, path, stat) { + ctx.status = 200; + }) + .on('end', function() { + resolve(); + }) + .pipe(ctx.res); }); - - // pipe - stream.pipe(res); }; }; diff --git a/package.json b/package.json index e3d7b079..b38e47e7 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,9 @@ }, "author": "Patrick Steele-Idem ", "maintainers": "Patrick Steele-Idem ", + "contributors": [ + "Karl Morrison " + ], "dependencies": { "app-module-path": "^1.0.0", "app-root-dir": "^1.0.0", @@ -68,5 +71,5 @@ "concat", "minify" ], - "version": "2.6.0" + "version": "2.6.2" } From bbb0c99402bf52cbd902a1c96fb21e6bca1de04a Mon Sep 17 00:00:00 2001 From: Karl Morrison Date: Wed, 19 Oct 2016 04:42:21 +0200 Subject: [PATCH 3/3] Fixed versioning --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 6672f8b6..d740b244 100644 --- a/package.json +++ b/package.json @@ -71,5 +71,5 @@ "concat", "minify" ], - "version": "2.6.3" + "version": "2.6.1" }