diff --git a/.autod.conf.js b/.autod.conf.js index 0b4e25f..5c2c05d 100644 --- a/.autod.conf.js +++ b/.autod.conf.js @@ -8,7 +8,6 @@ module.exports = { 'benchmark', ], devdep: [ - 'egg', 'egg-ci', 'egg-bin', 'autod', diff --git a/.travis.yml b/.travis.yml index 1e455dc..960bc57 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,8 @@ sudo: false language: node_js node_js: - - '6' - - '7' - '8' + - '9' install: - npm i npminstall && npminstall script: diff --git a/app/extend/application.js b/app/extend/application.js index e2109e5..e702515 100644 --- a/app/extend/application.js +++ b/app/extend/application.js @@ -50,21 +50,21 @@ module.exports = { throw err; } - return function* jsonp(next) { - const jsonpFunction = getJsonpFunction(this.query, options.callback); + return async function jsonp(ctx, next) { + const jsonpFunction = getJsonpFunction(ctx.query, options.callback); - this[JSONP_CONFIG] = { + ctx[JSONP_CONFIG] = { jsonpFunction, options, }; // before handle request, must do some security checks - securityAssert(this); + securityAssert(ctx); - yield next; + await next(); // generate jsonp body - this.createJsonpBody(this.body); + ctx.createJsonpBody(ctx.body); }; }, }; diff --git a/appveyor.yml b/appveyor.yml index 698d8f9..d0aa47e 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,8 +1,7 @@ environment: matrix: - - nodejs_version: '6' - - nodejs_version: '7' - nodejs_version: '8' + - nodejs_version: '9' install: - ps: Install-Product node $env:nodejs_version diff --git a/package.json b/package.json index 09ee876..be9cdd9 100644 --- a/package.json +++ b/package.json @@ -24,18 +24,18 @@ "jsonp-body": "^1.0.0" }, "devDependencies": { - "autod": "^2.9.0", - "egg": "^1.8.0", - "egg-bin": "^4.3.2", + "autod": "^2.10.1", + "egg": "next", + "egg-bin": "^4.3.5", "egg-ci": "^1.8.0", - "egg-mock": "^3.12.1", - "eslint": "^4.7.1", + "egg-mock": "^3.13.1", + "eslint": "^4.10.0", "eslint-config-egg": "^5.1.1", "supertest": "^3.0.0", "webstorm-disable-index": "^1.2.0" }, "engines": { - "node": ">=6.0.0" + "node": ">=8.0.0" }, "scripts": { "test": "npm run lint -- --fix && npm run test-local", @@ -46,7 +46,7 @@ "autod": "autod" }, "ci": { - "version": "6, 7, 8" + "version": "8, 9" }, "repository": { "type": "git", diff --git a/test/fixtures/jsonp-test/app/controller/jsonp.js b/test/fixtures/jsonp-test/app/controller/jsonp.js index 7deeef8..5fde79f 100644 --- a/test/fixtures/jsonp-test/app/controller/jsonp.js +++ b/test/fixtures/jsonp-test/app/controller/jsonp.js @@ -1,16 +1,16 @@ 'use strict'; -exports.index = function*() { - this.body = { foo: 'bar' }; +exports.index = ctx => { + ctx.body = { foo: 'bar' }; }; exports.empty = function*() {}; -exports.mark = function*() { - this.body = { jsonpFunction: this.acceptJSONP }; +exports.mark = ctx => { + ctx.body = { jsonpFunction: ctx.acceptJSONP }; }; -exports.error = function*() { +exports.error = async () => { throw new Error('jsonpFunction is error'); -}; \ No newline at end of file +}; diff --git a/test/jsonp.test.js b/test/jsonp.test.js index 1bf8aa7..8034496 100644 --- a/test/jsonp.test.js +++ b/test/jsonp.test.js @@ -1,6 +1,5 @@ 'use strict'; -const request = require('supertest'); const mm = require('egg-mock'); describe('test/jsonp.test.js', () => { @@ -16,55 +15,55 @@ describe('test/jsonp.test.js', () => { afterEach(mm.restore); it('should support json', function* () { - yield request(app.callback()) + yield app.httpRequest() .get('/default') .expect(200) .expect({ foo: 'bar' }); }); it('should support jsonp', function* () { - yield request(app.callback()) + yield app.httpRequest() .get('/default?callback=fn') .expect(200) .expect('/**/ typeof fn === \'function\' && fn({"foo":"bar"});'); }); it('should support _callback', function* () { - yield request(app.callback()) + yield app.httpRequest() .get('/default?_callback=fn') .expect(200) .expect('/**/ typeof fn === \'function\' && fn({"foo":"bar"});'); }); it('should support jsonp if response is empty', function* () { - yield request(app.callback()) + yield app.httpRequest() .get('/empty?callback=fn') .expect(200) .expect('/**/ typeof fn === \'function\' && fn(null);'); }); it('should not support jsonp if not use jsonp middleware', function* () { - yield request(app.callback()) + yield app.httpRequest() .get('/disable?_callback=fn') .expect(200) .expect({ foo: 'bar' }); }); it('should not support cutom callback name', function* () { - yield request(app.callback()) + yield app.httpRequest() .get('/fn?fn=fn') .expect(200) .expect('/**/ typeof fn === \'function\' && fn({"foo":"bar"});'); }); it('should not pass csrf', function* () { - yield request(app.callback()) + yield app.httpRequest() .get('/csrf') .expect(403); }); it('should pass csrf with cookie', function* () { - yield request(app.callback()) + yield app.httpRequest() .get('/csrf') .set('cookie', 'csrfToken=token;') .set('x-csrf-token', 'token') @@ -73,7 +72,7 @@ describe('test/jsonp.test.js', () => { }); it('should pass csrf with cookie and support jsonp', function* () { - yield request(app.callback()) + yield app.httpRequest() .get('/csrf') .set('cookie', 'csrfToken=token;') .set('x-csrf-token', 'token') @@ -82,25 +81,25 @@ describe('test/jsonp.test.js', () => { }); it('should pass referrer white list check with subdomain', function* () { - yield request(app.callback()) + yield app.httpRequest() .get('/referrer/subdomain') .set('referrer', 'http://test.com/') .expect(200) .expect({ foo: 'bar' }); - yield request(app.callback()) + yield app.httpRequest() .get('/referrer/subdomain') .set('referrer', 'http://sub.test.com/') .expect(200) .expect({ foo: 'bar' }); - yield request(app.callback()) + yield app.httpRequest() .get('/referrer/subdomain') .set('referrer', 'https://sub.sub.test.com/') .expect(200) .expect({ foo: 'bar' }); - yield request(app.callback()) + yield app.httpRequest() .get('/referrer/subdomain') .set('referrer', 'https://sub.sub.test1.com/') .expect(403) @@ -108,25 +107,25 @@ describe('test/jsonp.test.js', () => { }); it('should pass referrer white list with domain', function* () { - yield request(app.callback()) + yield app.httpRequest() .get('/referrer/equal') .set('referrer', 'http://test.com/') .expect(200) .expect({ foo: 'bar' }); - yield request(app.callback()) + yield app.httpRequest() .get('/referrer/equal') .set('referrer', 'https://test.com/') .expect(200) .expect({ foo: 'bar' }); - yield request(app.callback()) + yield app.httpRequest() .get('/referrer/equal') .set('referrer', 'https://sub.sub.test.com/') .expect(403) .expect(/jsonp request security validate failed/); - yield request(app.callback()) + yield app.httpRequest() .get('/referrer/equal') .set('referrer', 'https://sub.sub.test1.com/') .expect(403) @@ -134,25 +133,25 @@ describe('test/jsonp.test.js', () => { }); it('should pass referrer white array and regexp', function* () { - yield request(app.callback()) + yield app.httpRequest() .get('/referrer/regexp') .set('referrer', 'http://test.com/') .expect(200) .expect({ foo: 'bar' }); - yield request(app.callback()) + yield app.httpRequest() .get('/referrer/regexp') .set('referrer', 'https://foo.com/') .expect(200) .expect({ foo: 'bar' }); - yield request(app.callback()) + yield app.httpRequest() .get('/referrer/regexp') .set('referrer', 'https://sub.sub.test.com/') .expect(403) .expect(/jsonp request security validate failed/); - yield request(app.callback()) + yield app.httpRequest() .get('/referrer/regexp') .set('referrer', 'https://sub.sub.test1.com/') .expect(403) @@ -160,7 +159,7 @@ describe('test/jsonp.test.js', () => { }); it('should pass when pass csrf but not hit referrer white list', function* () { - yield request(app.callback()) + yield app.httpRequest() .get('/both') .set('cookie', 'csrfToken=token;') .set('x-csrf-token', 'token') @@ -169,7 +168,7 @@ describe('test/jsonp.test.js', () => { }); it('should pass when not pass csrf but hit referrer white list', function* () { - yield request(app.callback()) + yield app.httpRequest() .get('/both') .set('referrer', 'https://test.com/') .expect(200) @@ -177,14 +176,14 @@ describe('test/jsonp.test.js', () => { }); it('should 403 when not pass csrf and not hit referrer white list', function* () { - yield request(app.callback()) + yield app.httpRequest() .get('/both') .expect(403) .expect(/jsonp request security validate failed/); }); it('should 403 when not pass csrf and referrer illegal', function* () { - yield request(app.callback()) + yield app.httpRequest() .get('/both') .set('referrer', '/hello') .expect(403) @@ -192,21 +191,21 @@ describe('test/jsonp.test.js', () => { }); it('should pass and return is a jsonp function', function* () { - yield request(app.callback()) + yield app.httpRequest() .get('/mark?_callback=fn') .expect(200) .expect('/**/ typeof fn === \'function\' && fn({"jsonpFunction":true});'); }); it('should pass and return is not a jsonp function', function* () { - yield request(app.callback()) + yield app.httpRequest() .get('/mark') .expect(200) .expect({ jsonpFunction: false }); }); it('should pass and return error message', function* () { - yield request(app.callback()) + yield app.httpRequest() .get('/error?_callback=fn') .expect(200) .expect('/**/ typeof fn === \'function\' && fn({"msg":"jsonpFunction is error"});');