├── .eslintignore ├── .eslintrc ├── test ├── fixtures │ └── apps │ │ └── origin-test │ │ ├── config │ │ └── config.default.js │ │ ├── package.json │ │ └── app │ │ ├── router.js │ │ └── controller │ │ └── home.js └── origin.test.js ├── app.js ├── .gitignore ├── .travis.yml ├── config └── config.default.js ├── appveyor.yml ├── .autod.conf.js ├── app └── middleware │ └── origin.js ├── .github └── PULL_REQUEST_TEMPLATE.md ├── LICENSE ├── package.json ├── README.md ├── README.zh_CN.md └── .nyc_output └── ac0f27f6-68c1-4c1b-894f-c9cc26ac628e.json /.eslintignore: -------------------------------------------------------------------------------- 1 | coverage 2 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "eslint-config-egg" 3 | } 4 | -------------------------------------------------------------------------------- /test/fixtures/apps/origin-test/config/config.default.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | exports.keys = '123456'; 4 | -------------------------------------------------------------------------------- /test/fixtures/apps/origin-test/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "origin-test", 3 | "version": "0.0.1" 4 | } -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = app => { 4 | app.config.coreMiddlewares.push('origin'); 5 | }; 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | logs/ 2 | npm-debug.log 3 | node_modules/ 4 | coverage/ 5 | .idea/ 6 | run/ 7 | .DS_Store 8 | *.swp 9 | 10 | -------------------------------------------------------------------------------- /test/fixtures/apps/origin-test/app/router.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = app => { 4 | const { router, controller } = app; 5 | 6 | router.get('/', controller.home.index); 7 | }; 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - '8' 5 | - '9' 6 | install: 7 | - npm i npminstall && npminstall 8 | script: 9 | - npm run ci 10 | after_script: 11 | - npminstall codecov && codecov 12 | -------------------------------------------------------------------------------- /config/config.default.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /** 4 | * egg-origin default config 5 | * @member Config#eggOrigin 6 | * @property {String} SOME_KEY - some description 7 | */ 8 | exports.cors = { 9 | origin: [], 10 | }; 11 | 12 | exports.origin = { 13 | whiteList: [], 14 | }; 15 | -------------------------------------------------------------------------------- /test/fixtures/apps/origin-test/app/controller/home.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const Controller = require('egg').Controller; 4 | 5 | class HomeController extends Controller { 6 | async index() { 7 | this.ctx.body = 'hi, ' + this.app.plugins.eggOrigin.name; 8 | } 9 | } 10 | 11 | module.exports = HomeController; 12 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | environment: 2 | matrix: 3 | - nodejs_version: '8' 4 | - nodejs_version: '9' 5 | 6 | install: 7 | - ps: Install-Product node $env:nodejs_version 8 | - npm i npminstall && node_modules\.bin\npminstall 9 | 10 | test_script: 11 | - node --version 12 | - npm --version 13 | - npm run test 14 | 15 | build: off 16 | -------------------------------------------------------------------------------- /.autod.conf.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { 4 | write: true, 5 | prefix: '^', 6 | plugin: 'autod-egg', 7 | test: [ 8 | 'test', 9 | 'benchmark', 10 | ], 11 | devdep: [ 12 | 'egg', 13 | 'egg-ci', 14 | 'egg-bin', 15 | 'autod', 16 | 'autod-egg', 17 | 'eslint', 18 | 'eslint-config-egg', 19 | 'webstorm-disable-index', 20 | ], 21 | exclude: [ 22 | './test/fixtures', 23 | './docs', 24 | './coverage', 25 | ], 26 | }; 27 | -------------------------------------------------------------------------------- /test/origin.test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const mock = require('egg-mock'); 4 | 5 | describe('test/origin.test.js', () => { 6 | let app; 7 | before(() => { 8 | app = mock.app({ 9 | baseDir: 'apps/origin-test', 10 | }); 11 | return app.ready(); 12 | }); 13 | 14 | after(() => app.close()); 15 | afterEach(mock.restore); 16 | 17 | it('should GET /', () => { 18 | return app.httpRequest() 19 | .get('/') 20 | .expect('hi, eggOrigin') 21 | .expect(200); 22 | }); 23 | }); 24 | -------------------------------------------------------------------------------- /app/middleware/origin.js: -------------------------------------------------------------------------------- 1 | /** 2 | 3 | 【自定义允许跨域】:Access-Control-Allow-Origin 4 | 5 | */ 6 | 'use strict'; 7 | 8 | module.exports = options => { 9 | const { whiteList } = options; 10 | /** 11 | * 如果传入的不是数组,直接抛出错误 12 | * */ 13 | if (!Array.isArray(whiteList)) { 14 | throw Error('---------跨域白名单必须设置为数组----------'); 15 | } 16 | 17 | return async function setOrigin(ctx, next) { 18 | // 当前访问的域名 19 | const { origin } = ctx.request.header; 20 | // 如果设置成 '*',就给访问的域名设置允许跨域 21 | if (whiteList.indexOf('*') > -1) { 22 | ctx.response.set('Access-Control-Allow-Origin', origin); 23 | } else if (whiteList.indexOf(origin) > -1) { 24 | ctx.response.set('Access-Control-Allow-Origin', origin); 25 | } 26 | await next(); 27 | }; 28 | }; 29 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 10 | 11 | ##### Checklist 12 | 13 | 14 | - [ ] `npm test` passes 15 | - [ ] tests and/or benchmarks are included 16 | - [ ] documentation is changed or added 17 | - [ ] commit message follows commit guidelines 18 | 19 | ##### Affected core subsystem(s) 20 | 21 | 22 | 23 | ##### Description of change 24 | 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Alibaba Group Holding Limited and other contributors. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "egg-origin", 3 | "version": "1.0.3", 4 | "description": "an eggjs middleware of custom origin", 5 | "eggPlugin": { 6 | "name": "origin" 7 | }, 8 | "keywords": [ 9 | "egg", 10 | "eggPlugin", 11 | "egg-plugin", 12 | "eggjs", 13 | "egg origin", 14 | "egg-cors" 15 | ], 16 | "dependencies": { 17 | "egg-cors": "^2.1.2" 18 | }, 19 | "devDependencies": { 20 | "autod": "^3.0.0", 21 | "autod-egg": "^1.0.0", 22 | "egg": "^2.0.0", 23 | "egg-bin": "^4.3.0", 24 | "egg-ci": "^1.8.0", 25 | "egg-mock": "^3.13.0", 26 | "eslint": "^4.11.0", 27 | "eslint-config-egg": "^5.1.0", 28 | "webstorm-disable-index": "^1.2.0" 29 | }, 30 | "engines": { 31 | "node": ">=8.0.0" 32 | }, 33 | "scripts": { 34 | "test": "npm run lint -- --fix && egg-bin pkgfiles && npm run test-local", 35 | "test-local": "egg-bin test", 36 | "cov": "egg-bin cov", 37 | "lint": "eslint .", 38 | "ci": "egg-bin pkgfiles --check && npm run lint && npm run cov", 39 | "pkgfiles": "egg-bin pkgfiles", 40 | "autod": "autod" 41 | }, 42 | "files": [ 43 | "app", 44 | "config", 45 | "app.js" 46 | ], 47 | "ci": { 48 | "version": "8, 9" 49 | }, 50 | "repository": { 51 | "type": "git", 52 | "url": "git+https://github.com/temool/egg-origin.git" 53 | }, 54 | "bugs": { 55 | "url": "https://github.com/temool/egg-origin/issues" 56 | }, 57 | "homepage": "https://github.com/temool/egg-origin#README.zh_CN", 58 | "author": "temool", 59 | "license": "MIT" 60 | } 61 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # egg-origin 2 | 3 | [![NPM version][npm-image]][npm-url] 4 | [![build status][travis-image]][travis-url] 5 | [![Test coverage][codecov-image]][codecov-url] 6 | [![David deps][david-image]][david-url] 7 | [![Known Vulnerabilities][snyk-image]][snyk-url] 8 | [![npm download][download-image]][download-url] 9 | 10 | [npm-image]: https://img.shields.io/npm/v/egg-origin.svg?style=flat-square 11 | [npm-url]: https://npmjs.org/package/egg-origin 12 | [travis-image]: https://img.shields.io/travis/eggjs/egg-origin.svg?style=flat-square 13 | [travis-url]: https://travis-ci.org/eggjs/egg-origin 14 | [codecov-image]: https://img.shields.io/codecov/c/github/eggjs/egg-origin.svg?style=flat-square 15 | [codecov-url]: https://codecov.io/github/eggjs/egg-origin?branch=master 16 | [david-image]: https://img.shields.io/david/eggjs/egg-origin.svg?style=flat-square 17 | [david-url]: https://david-dm.org/eggjs/egg-origin 18 | [snyk-image]: https://snyk.io/test/npm/egg-origin/badge.svg?style=flat-square 19 | [snyk-url]: https://snyk.io/test/npm/egg-origin 20 | [download-image]: https://img.shields.io/npm/dm/egg-origin.svg?style=flat-square 21 | [download-url]: https://npmjs.org/package/egg-origin 22 | 23 | 26 | 27 | ## Install 28 | 29 | ```bash 30 | $ npm i egg-origin --save 31 | ``` 32 | 33 | ## Usage 34 | 35 | ```js 36 | // {app_root}/config/plugin.js 37 | exports.origin = { 38 | enable: true, 39 | package: 'egg-origin', 40 | }; 41 | ``` 42 | 43 | ## Configuration 44 | 45 | ```js 46 | // {app_root}/config/config.default.js 47 | exports.origin = { 48 | whiteList: [] 49 | }; 50 | ``` 51 | 52 | see [config/config.default.js](config/config.default.js) for more detail. 53 | 54 | ## Example 55 | ```js 56 | // {app_root}/config/config.default.js 57 | exports.origin = { 58 | whiteList: [http://foo.com', 'http://localhost:8080'] 59 | }; 60 | ``` 61 | 62 | 63 | ## Questions & Suggestions 64 | 65 | Please open an issue [here](https://github.com/temool/egg-origin/issues). 66 | 67 | ## License 68 | 69 | [MIT](LICENSE) 70 | -------------------------------------------------------------------------------- /README.zh_CN.md: -------------------------------------------------------------------------------- 1 | # egg-origin 2 | 3 | [![NPM version][npm-image]][npm-url] 4 | [![build status][travis-image]][travis-url] 5 | [![Test coverage][codecov-image]][codecov-url] 6 | [![David deps][david-image]][david-url] 7 | [![Known Vulnerabilities][snyk-image]][snyk-url] 8 | [![npm download][download-image]][download-url] 9 | 10 | [npm-image]: https://img.shields.io/npm/v/egg-origin.svg?style=flat-square 11 | [npm-url]: https://npmjs.org/package/egg-origin 12 | [travis-image]: https://img.shields.io/travis/eggjs/egg-origin.svg?style=flat-square 13 | [travis-url]: https://travis-ci.org/eggjs/egg-origin 14 | [codecov-image]: https://img.shields.io/codecov/c/github/eggjs/egg-origin.svg?style=flat-square 15 | [codecov-url]: https://codecov.io/github/eggjs/egg-origin?branch=master 16 | [david-image]: https://img.shields.io/david/eggjs/egg-origin.svg?style=flat-square 17 | [david-url]: https://david-dm.org/eggjs/egg-origin 18 | [snyk-image]: https://snyk.io/test/npm/egg-origin/badge.svg?style=flat-square 19 | [snyk-url]: https://snyk.io/test/npm/egg-origin 20 | [download-image]: https://img.shields.io/npm/dm/egg-origin.svg?style=flat-square 21 | [download-url]: https://npmjs.org/package/egg-origin 22 | 23 | 26 | 27 | ## 依赖说明 28 | 29 | ### 依赖的 egg 版本 30 | 31 | egg-origin 版本 | egg 1.x 32 | --- | --- 33 | 1.x | 😁 34 | 0.x | ❌ 35 | 36 | ### 依赖的插件 37 | 45 | 46 | ## 开启插件 47 | 48 | ```js 49 | // config/plugin.js 50 | exports.origin = { 51 | enable: true, 52 | package: 'egg-origin', 53 | }; 54 | ``` 55 | 56 | ## 配置插件 57 | ```js 58 | // {app_root}/config/config.default.js 59 | exports.origin = { 60 | whiteList: [http://foo.com', 'http://localhost:8080'] 61 | }; 62 | ``` 63 | ## 使用场景 64 | 65 | - 目前 egg-cors 存在一定的局限性: 66 | - 1、只能设置一个域名 或者 '*',不能指定多个域名,维护一个白名单; 67 | - 2、设置成'*'时,前端请求带 withCredentials: true 参数时,还是会跨域。 68 | egg-origin 解决了以上问题,后期将会添加更多配置,是配置更灵活,满足不同的需求。 69 | 70 | 71 | 74 | 75 | 76 | 77 | 78 | 79 | ## 提问交流 80 | 81 | 请到 [egg-origin issues](https://github.com/temool/egg-origin/issues) 异步交流。 82 | 83 | ## License 84 | 85 | [MIT](LICENSE) 86 | -------------------------------------------------------------------------------- /.nyc_output/ac0f27f6-68c1-4c1b-894f-c9cc26ac628e.json: -------------------------------------------------------------------------------- 1 | {"/Users/chenmin/Desktop/chenmin/egg-demo/egg-origin/config/config.default.js":{"path":"/Users/chenmin/Desktop/chenmin/egg-demo/egg-origin/config/config.default.js","statementMap":{"0":{"start":{"line":8,"column":0},"end":{"line":10,"column":2}}},"fnMap":{},"branchMap":{},"s":{"0":1},"f":{},"b":{},"_coverageSchema":"43e27e138ebf9cfc5966b082cf9a028302ed4184","hash":"712d5c085a3739f021854c82c87c7ffc6f27ec58","contentHash":"114ab7bf6a92f0ce87c8a809b9a142a1"},"/Users/chenmin/Desktop/chenmin/egg-demo/egg-origin/app.js":{"path":"/Users/chenmin/Desktop/chenmin/egg-demo/egg-origin/app.js","statementMap":{"0":{"start":{"line":3,"column":0},"end":{"line":5,"column":2}},"1":{"start":{"line":4,"column":2},"end":{"line":4,"column":44}}},"fnMap":{"0":{"name":"(anonymous_0)","decl":{"start":{"line":3,"column":17},"end":{"line":3,"column":18}},"loc":{"start":{"line":3,"column":24},"end":{"line":5,"column":1}},"line":3}},"branchMap":{},"s":{"0":1,"1":1},"f":{"0":1},"b":{},"_coverageSchema":"43e27e138ebf9cfc5966b082cf9a028302ed4184","hash":"93efc40440704170aff8211f9732e539d86ed779","contentHash":"d257bbc9b1a6511058a4dd27e705fbf1"},"/Users/chenmin/Desktop/chenmin/egg-demo/egg-origin/app/middleware/origin.js":{"path":"/Users/chenmin/Desktop/chenmin/egg-demo/egg-origin/app/middleware/origin.js","statementMap":{"0":{"start":{"line":22,"column":0},"end":{"line":38,"column":2}},"1":{"start":{"line":23,"column":24},"end":{"line":23,"column":31}},"2":{"start":{"line":27,"column":2},"end":{"line":29,"column":3}},"3":{"start":{"line":28,"column":4},"end":{"line":28,"column":51}},"4":{"start":{"line":31,"column":2},"end":{"line":37,"column":4}},"5":{"start":{"line":32,"column":23},"end":{"line":32,"column":41}},"6":{"start":{"line":33,"column":4},"end":{"line":35,"column":5}},"7":{"start":{"line":34,"column":6},"end":{"line":34,"column":62}},"8":{"start":{"line":36,"column":4},"end":{"line":36,"column":17}}},"fnMap":{"0":{"name":"(anonymous_0)","decl":{"start":{"line":22,"column":17},"end":{"line":22,"column":18}},"loc":{"start":{"line":22,"column":28},"end":{"line":38,"column":1}},"line":22},"1":{"name":"setOrigin","decl":{"start":{"line":31,"column":24},"end":{"line":31,"column":33}},"loc":{"start":{"line":31,"column":45},"end":{"line":37,"column":3}},"line":31}},"branchMap":{"0":{"loc":{"start":{"line":27,"column":2},"end":{"line":29,"column":3}},"type":"if","locations":[{"start":{"line":27,"column":2},"end":{"line":29,"column":3}},{"start":{"line":27,"column":2},"end":{"line":29,"column":3}}],"line":27},"1":{"loc":{"start":{"line":33,"column":4},"end":{"line":35,"column":5}},"type":"if","locations":[{"start":{"line":33,"column":4},"end":{"line":35,"column":5}},{"start":{"line":33,"column":4},"end":{"line":35,"column":5}}],"line":33}},"s":{"0":1,"1":1,"2":1,"3":1,"4":0,"5":0,"6":0,"7":0,"8":0},"f":{"0":1,"1":0},"b":{"0":[1,0],"1":[0,0]},"_coverageSchema":"43e27e138ebf9cfc5966b082cf9a028302ed4184","hash":"ce0f516e5722262aeb8163b39cecbb975fe43506","contentHash":"b4a5a2fa1113f778fb4490813d3f950c"}} --------------------------------------------------------------------------------