From e18e1df29f053e02d70e31f6571b58fb8ee76f68 Mon Sep 17 00:00:00 2001 From: Ian Macalinao Date: Wed, 18 Nov 2015 18:22:01 -0600 Subject: [PATCH] add support for koa v2 Use const Update for Koa 2 Use strict Update example --- .travis.yml | 3 ++- example.js | 16 +++++++------ index.js | 37 ++++++++++++++--------------- package.json | 7 ++++-- test/test.js | 67 +++++++++++++++++++++++----------------------------- 5 files changed, 64 insertions(+), 66 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9887221..501b6f5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,4 @@ node_js: -- "0.11" +- "4" +- "5" language: node_js diff --git a/example.js b/example.js index 9b9e991..7b57e2e 100644 --- a/example.js +++ b/example.js @@ -1,8 +1,9 @@ +'use strict'; -var rewrite = require('./'); -var koa = require('koa'); +const rewrite = require('./'); +const Koa = require('koa'); -var app = koa(); +const app = new Koa(); // GET /i124 app.use(rewrite(/^\/i(\w+)/, '/items/$1')); @@ -14,9 +15,10 @@ app.use(rewrite('/:src..:dst', '/commits/:src/to/:dst')); // GET /js/jquery.js app.use(rewrite('/js/*', '/public/assets/js/$1')); -app.use(function*(){ - this.body = this.url + '\n'; +app.use(function(ctx) { + ctx.body = ctx.url + '\n'; }); -app.listen(3000); -console.log('listening on port 3000'); \ No newline at end of file +app.listen(3000, () => { + console.log('listening on port 3000'); +}); diff --git a/index.js b/index.js index 6327f79..9c642dd 100644 --- a/index.js +++ b/index.js @@ -1,10 +1,10 @@ - +'use strict'; /** * Module dependencies. */ - var debug = require('debug')('koa-rewrite'); - var toRegexp = require('path-to-regexp'); + const debug = require('debug')('koa-rewrite'); + const toRegexp = require('path-to-regexp'); /** * Expose `expose`. @@ -22,31 +22,30 @@ module.exports = rewrite; */ function rewrite(src, dst) { - var keys = []; - var re = toRegexp(src, keys); - var map = toMap(keys); + const keys = []; + const re = toRegexp(src, keys); + const map = toMap(keys); debug('rewrite %s -> %s %s', src, dst, re); - return function*(next){ - var orig = this.path; - var m = re.exec(orig); - + return function(ctx, next) { + const orig = ctx.path; + const m = re.exec(orig); + if (m) { - this.path = dst.replace(/\$(\d+)|(?::(\w+))/g, function(_, n, name){ + ctx.path = dst.replace(/\$(\d+)|(?::(\w+))/g, function(_, n, name){ if (name) return m[map[name].index + 1]; return m[n]; }); - debug('rewrite %s -> %s', orig, this.path); - - yield* next; + debug('rewrite %s -> %s', orig, ctx.path); - this.path = orig; - return; + return next().then(function() { + ctx.path = orig; + }); } - yield* next; + return next(); } } @@ -59,7 +58,7 @@ function rewrite(src, dst) { */ function toMap(params) { - var map = {}; + const map = {}; params.forEach(function(param, i){ param.index = i; @@ -67,4 +66,4 @@ function toMap(params) { }); return map; -} \ No newline at end of file +} diff --git a/package.json b/package.json index e52dd58..06093b7 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "repository": "koajs/rewrite", "version": "1.1.0", "scripts": { - "test": "./node_modules/mocha/bin/mocha --harmony test/test" + "test": "mocha test/test" }, "keywords": [ "koa", @@ -17,7 +17,7 @@ "index.js" ], "devDependencies": { - "koa": "~0.2.1", + "koa": "^2.0.0-alpha.3", "mocha": "^2.2.4", "supertest": "^0.15.0" }, @@ -25,5 +25,8 @@ "dependencies": { "debug": "*", "path-to-regexp": "0.0.2" + }, + "engines": { + "node": ">= 4" } } diff --git a/test/test.js b/test/test.js index da5fa97..27c9a15 100644 --- a/test/test.js +++ b/test/test.js @@ -1,18 +1,23 @@ -var request = require('supertest'); -var koa = require('koa'); -var rewrite = require('..'); +'use strict'; -describe('koa-rewrite', function () { +const request = require('supertest'); +const Koa = require('koa'); +const rewrite = require('..'); + +function differentPathHelper(ctx, next) { + const orig = ctx.path; + return next().then(() => { + if (orig !== ctx.path) ctx.throw(ctx.path + ' not equal to original path ' + orig); + }); +} + +describe('new Koa-rewrite', function () { it('rewrite /^\/i(\w+)/ -> /items/$1', function (done) { - var app = koa(); - app.use(function* (next) { - var orig = this.path; - yield* next; - if (orig !== this.path) this.throw(this.path + ' not equal to original path ' + orig); - }); + const app = new Koa(); + app.use(differentPathHelper); app.use(rewrite(/^\/i(\w+)/, '/items/$1')); - app.use(function* () { - this.body = this.path; + app.use(function(ctx) { + ctx.body = ctx.path; }); request(app.callback()) @@ -21,15 +26,11 @@ describe('koa-rewrite', function () { }); it('rewrite /:src..:dst -> /commits/$1/to/$2', function (done) { - var app = koa(); - app.use(function* (next) { - var orig = this.path; - yield* next; - if (orig !== this.path) this.throw(this.path + ' not equal to original path ' + orig); - }); + const app = new Koa(); + app.use(differentPathHelper); app.use(rewrite('/:src..:dst', '/commits/$1/to/$2')); - app.use(function* () { - this.body = this.path; + app.use(function(ctx) { + ctx.body = ctx.path; }); request(app.callback()) @@ -38,15 +39,11 @@ describe('koa-rewrite', function () { }); it('rewrite /:src..:dst -> /commits/:src/to/:dst', function (done) { - var app = koa(); - app.use(function* (next) { - var orig = this.path; - yield* next; - if (orig !== this.path) this.throw(this.path + ' not equal to original path ' + orig); - }); + const app = new Koa(); + app.use(differentPathHelper); app.use(rewrite('/:src..:dst', '/commits/:src/to/:dst')); - app.use(function* () { - this.body = this.path; + app.use(function(ctx) { + ctx.body = ctx.path; }); request(app.callback()) @@ -55,19 +52,15 @@ describe('koa-rewrite', function () { }); it('rewrite /js/* -> /public/assets/js/$1', function (done) { - var app = koa(); - app.use(function* (next) { - var orig = this.path; - yield* next; - if (orig !== this.path) this.throw(this.path + ' not equal to original path ' + orig); - }); + const app = new Koa(); + app.use(differentPathHelper); app.use(rewrite('/js/*', '/public/assets/js/$1')); - app.use(function* () { - this.body = this.path; + app.use(function(ctx) { + ctx.body = ctx.path; }); request(app.callback()) .get('/js/jquery.js') .expect('/public/assets/js/jquery.js', done); }); -}); \ No newline at end of file +});