cors/History.md000066400000005370151113247740007511 0ustar00 3.1.0 / 2020-05-17 ================== **features** * [[`013662a`](http://github.com/koajs/cors/commit/013662ae1ab65c4af230c17dfa1044609502b15b)] - feat: add support for using a function to determine whether or not to allow credentials. (#68) (mcohen75 <>) **others** * [[`da84dec`](http://github.com/koajs/cors/commit/da84dec7fa16af95d157a549bd473e7bfa4aa152)] - docs: update install script (dead-horse <>) * [[`eba3b44`](http://github.com/koajs/cors/commit/eba3b446055bd14b86d19dfc81d8ed5f83a8a534)] - chore: ES6 Object spread (#66) (Alpha <>) 3.0.0 / 2019-03-11 ================== **others** * [[`369d31d`](http://github.com/koajs/cors/commit/369d31db7835ed344011706f9506d45a44638017)] - refactor: use async function, support options.origin return promise (#59) (Yiyu He <>) 2.2.3 / 2018-12-19 ================== **fixes** * [[`12ae730`](http://github.com/koajs/cors/commit/12ae7306e8055322e6c5d29319330da52ba0e126)] - fix: set `Vary: Origin` header on error responses (#55) (Erik Fried <>) 2.2.2 / 2018-07-11 ================== **others** * [[`019ec40`](http://github.com/koajs/cors/commit/019ec403be573177e8ed6ad3ef4077b82b5ea934)] - travis: test node@10 and drop test node@4 (#51) (fengmk2 <>) * [[`6e22833`](http://github.com/koajs/cors/commit/6e22833ce125ca334b68980372065867eda892b0)] - doc: update outdated options doc (Xingan Wang <>) * [[`c982530`](http://github.com/koajs/cors/commit/c9825308ce1c76810468bdf5a404b838206fba22)] - travis: test node@8 (jongleberry <>) * [[`b4f65b3`](http://github.com/koajs/cors/commit/b4f65b39b558b870521e6613aee58898e88196f9)] - npm: remove tag (jongleberry <>) * [[`878ae9b`](http://github.com/koajs/cors/commit/878ae9b0c99fb6da8d3840e502d4968a65089e28)] - package: rename to @koa/cors (jongleberry <>) 2.2.1 / 2017-02-12 ================== * fix: always set "Vary: Origin" header (#31) 2.2.0 / 2016-09-26 ================== * feat: add PATCH to default methods 2.1.1 / 2016-05-14 ================== * fix: keepHeadersOnError won't affect OPTIONS request (#17) 2.1.0 / 2016-04-29 ================== * feat: Keep headers after an error (#13) * chore: use eslint instead of jshint (#10) * test: use codecov instead of coveralls 2.0.0 / 2016-02-20 ================== * chore: make node engines >= 4.3.1 * doc: update example * test: only test on node 4+ * refactor: src,test: update to (ctx, next) -> Promise middleware contract * chore: base on koa@2 1.0.1 / 2015-02-11 ================== * fix: make more spec-compliant 1.0.0 / 2015-02-09 ================== * first release cors/LICENSE000066400000002151151113247740006525 0ustar00This software is licensed under the MIT License. Copyright (c) 2015 - 2018 koajs and other contributors Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. cors/README.md000066400000004021151113247740006775 0ustar00@koa/cors ======= [![NPM version][npm-image]][npm-url] [![build status][travis-image]][travis-url] [![Test coverage][codecov-image]][codecov-url] [![David deps][david-image]][david-url] [![npm download][download-image]][download-url] [npm-image]: https://img.shields.io/npm/v/@koa/cors.svg?style=flat-square [npm-url]: https://npmjs.org/package/@koa/cors [travis-image]: https://img.shields.io/travis/koajs/cors.svg?style=flat-square [travis-url]: https://travis-ci.org/koajs/cors [codecov-image]: https://codecov.io/github/koajs/cors/coverage.svg?branch=v2.x [codecov-url]: https://codecov.io/github/koajs/cors?branch=v2.x [david-image]: https://img.shields.io/david/koajs/cors.svg?style=flat-square [david-url]: https://david-dm.org/koajs/cors [download-image]: https://img.shields.io/npm/dm/@koa/cors.svg?style=flat-square [download-url]: https://npmjs.org/package/@koa/cors [Cross-Origin Resource Sharing(CORS)](https://developer.mozilla.org/en/docs/Web/HTTP/Access_control_CORS) for koa ## Installation ```bash $ npm install @koa/cors --save ``` ## Quick start Enable cors with default options: - origin: request Origin header - allowMethods: GET,HEAD,PUT,POST,DELETE,PATCH ```js const Koa = require('koa'); const cors = require('@koa/cors'); const app = new Koa(); app.use(cors()); ``` ## cors(options) ```js /** * CORS middleware * * @param {Object} [options] * - {String|Function(ctx)} origin `Access-Control-Allow-Origin`, default is request Origin header * - {String|Array} allowMethods `Access-Control-Allow-Methods`, default is 'GET,HEAD,PUT,POST,DELETE,PATCH' * - {String|Array} exposeHeaders `Access-Control-Expose-Headers` * - {String|Array} allowHeaders `Access-Control-Allow-Headers` * - {String|Number} maxAge `Access-Control-Max-Age` in seconds * - {Boolean|Function(ctx)} credentials `Access-Control-Allow-Credentials`, default is false. * - {Boolean} keepHeadersOnError Add set headers to `err.header` if an error is thrown * @return {Function} cors middleware * @api public */ ``` ## License [MIT](./LICENSE) cors/index.js000066400000010344151113247740007170 0ustar00'use strict'; const vary = require('vary'); /** * CORS middleware * * @param {Object} [options] * - {String|Function(ctx)} origin `Access-Control-Allow-Origin`, default is request Origin header * - {String|Array} allowMethods `Access-Control-Allow-Methods`, default is 'GET,HEAD,PUT,POST,DELETE,PATCH' * - {String|Array} exposeHeaders `Access-Control-Expose-Headers` * - {String|Array} allowHeaders `Access-Control-Allow-Headers` * - {String|Number} maxAge `Access-Control-Max-Age` in seconds * - {Boolean} credentials `Access-Control-Allow-Credentials` * - {Boolean} keepHeadersOnError Add set headers to `err.header` if an error is thrown * @return {Function} cors middleware * @api public */ module.exports = function(options) { const defaults = { allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH', }; options = { ...defaults, ...options, }; if (Array.isArray(options.exposeHeaders)) { options.exposeHeaders = options.exposeHeaders.join(','); } if (Array.isArray(options.allowMethods)) { options.allowMethods = options.allowMethods.join(','); } if (Array.isArray(options.allowHeaders)) { options.allowHeaders = options.allowHeaders.join(','); } if (options.maxAge) { options.maxAge = String(options.maxAge); } options.keepHeadersOnError = options.keepHeadersOnError === undefined || !!options.keepHeadersOnError; return async function cors(ctx, next) { // If the Origin header is not present terminate this set of steps. // The request is outside the scope of this specification. const requestOrigin = ctx.get('Origin'); // Always set Vary header // https://github.com/rs/cors/issues/10 ctx.vary('Origin'); if (!requestOrigin) return await next(); let origin; if (typeof options.origin === 'function') { origin = options.origin(ctx); if (origin instanceof Promise) origin = await origin; if (!origin) return await next(); } else { origin = options.origin || requestOrigin; } let credentials; if (typeof options.credentials === 'function') { credentials = options.credentials(ctx); if (credentials instanceof Promise) credentials = await credentials; } else { credentials = !!options.credentials; } const headersSet = {}; function set(key, value) { ctx.set(key, value); headersSet[key] = value; } if (ctx.method !== 'OPTIONS') { // Simple Cross-Origin Request, Actual Request, and Redirects set('Access-Control-Allow-Origin', origin); if (credentials === true) { set('Access-Control-Allow-Credentials', 'true'); } if (options.exposeHeaders) { set('Access-Control-Expose-Headers', options.exposeHeaders); } if (!options.keepHeadersOnError) { return await next(); } try { return await next(); } catch (err) { const errHeadersSet = err.headers || {}; const varyWithOrigin = vary.append(errHeadersSet.vary || errHeadersSet.Vary || '', 'Origin'); delete errHeadersSet.Vary; err.headers = { ...errHeadersSet, ...headersSet, ...{ vary: varyWithOrigin }, }; throw err; } } else { // Preflight Request // If there is no Access-Control-Request-Method header or if parsing failed, // do not set any additional headers and terminate this set of steps. // The request is outside the scope of this specification. if (!ctx.get('Access-Control-Request-Method')) { // this not preflight request, ignore it return await next(); } ctx.set('Access-Control-Allow-Origin', origin); if (credentials === true) { ctx.set('Access-Control-Allow-Credentials', 'true'); } if (options.maxAge) { ctx.set('Access-Control-Max-Age', options.maxAge); } if (options.allowMethods) { ctx.set('Access-Control-Allow-Methods', options.allowMethods); } let allowHeaders = options.allowHeaders; if (!allowHeaders) { allowHeaders = ctx.get('Access-Control-Request-Headers'); } if (allowHeaders) { ctx.set('Access-Control-Allow-Headers', allowHeaders); } ctx.status = 204; } }; }; cors/package.json000066400000003724151113247740010015 0ustar00{ "_args": [ [ "@koa/cors@3.1.0", "/home/freeclou/app.optimyar.com/backend" ] ], "_from": "@koa/cors@3.1.0", "_id": "@koa/cors@3.1.0", "_inBundle": false, "_integrity": "sha512-7ulRC1da/rBa6kj6P4g2aJfnET3z8Uf3SWu60cjbtxTA5g8lxRdX/Bd2P92EagGwwAhANeNw8T8if99rJliR6Q==", "_location": "/@koa/cors", "_phantomChildren": {}, "_requested": { "type": "version", "registry": true, "raw": "@koa/cors@3.1.0", "name": "@koa/cors", "escapedName": "@koa%2fcors", "scope": "@koa", "rawSpec": "3.1.0", "saveSpec": null, "fetchSpec": "3.1.0" }, "_requiredBy": [ "/strapi" ], "_resolved": "https://registry.npmjs.org/@koa/cors/-/cors-3.1.0.tgz", "_spec": "3.1.0", "_where": "/home/freeclou/app.optimyar.com/backend", "author": { "name": "fengmk2", "email": "fengmk2@gmail.com", "url": "http://fengmk2.com" }, "bugs": { "url": "https://github.com/koajs/cors/issues" }, "dependencies": { "vary": "^1.1.2" }, "description": "Cross-Origin Resource Sharing(CORS) for koa", "devDependencies": { "autod": "*", "eslint": "^5.15.1", "eslint-config-egg": "^7.1.0", "istanbul": "*", "koa": "^2.5.1", "mocha": "3", "supertest": "^3.1.0" }, "engines": { "node": ">= 8.0.0" }, "files": [ "index.js" ], "homepage": "https://github.com/koajs/cors", "keywords": [ "cors", "koa-cors", "Cross-Origin Resource Sharing", "@koa/cors", "koa", "koajs" ], "license": "MIT", "main": "index.js", "name": "@koa/cors", "repository": { "type": "git", "url": "git://github.com/koajs/cors.git" }, "scripts": { "autod": "autod -w --prefix '^'", "ci": "npm run lint && npm run test-cov", "lint": "eslint index.js test", "test": "NODE_ENV=test mocha --check-leaks -R spec -t 5000 test/*.test.js", "test-cov": "NODE_ENV=test istanbul cover _mocha -- --check-leaks -t 5000 test/*.test.js" }, "version": "3.1.0" }