@fastify/cors
Advanced tools
Comparing version 8.3.0 to 8.4.0
@@ -140,3 +140,3 @@ 'use strict' | ||
function normalizeCorsOptions (opts) { | ||
const corsOptions = Object.assign({}, defaultOptions, opts) | ||
const corsOptions = { ...defaultOptions, ...opts } | ||
if (Array.isArray(opts.origin) && opts.origin.indexOf('*') !== -1) { | ||
@@ -143,0 +143,0 @@ corsOptions.origin = '*' |
{ | ||
"name": "@fastify/cors", | ||
"version": "8.3.0", | ||
"version": "8.4.0", | ||
"description": "Fastify CORS", | ||
@@ -35,4 +35,4 @@ "main": "index.js", | ||
"@types/node": "^20.1.0", | ||
"@typescript-eslint/eslint-plugin": "^5.12.1", | ||
"@typescript-eslint/parser": "^5.12.1", | ||
"@typescript-eslint/eslint-plugin": "^6.4.1", | ||
"@typescript-eslint/parser": "^6.4.0", | ||
"cors": "^2.8.5", | ||
@@ -42,3 +42,3 @@ "fastify": "^4.0.0-rc.2", | ||
"tap": "^16.0.0", | ||
"tsd": "^0.28.0", | ||
"tsd": "^0.29.0", | ||
"typescript": "^5.0.2" | ||
@@ -45,0 +45,0 @@ }, |
@@ -10,8 +10,2 @@ # @fastify/cors | ||
Supports Fastify versions `4.x`. | ||
- Please refer to [7.x](https://github.com/fastify/fastify-cors/tree/v7.0.0) for Fastify `^3.x` compatibility. | ||
- Please refer to [3.x](https://github.com/fastify/fastify-cors/tree/3.x) for Fastify `^2.x` compatibility. | ||
- Please refer to [1.x](https://github.com/fastify/fastify-cors/tree/v1.0.0) for Fastify `^1.x` compatibility. | ||
## Install | ||
@@ -22,4 +16,18 @@ ``` | ||
### Compatibility | ||
| Plugin version | Fastify version | | ||
| -------------- |---------------- | | ||
| `^8.0.0` | `^4.0.0` | | ||
| `^7.0.0` | `^3.0.0` | | ||
| `^3.0.0` | `^2.0.0` | | ||
| `^1.0.0` | `^1.0.0` | | ||
Please note that if a Fastify version is out of support, then so are the corresponding version(s) of this plugin | ||
in the table above. | ||
See [Fastify's LTS policy](https://github.com/fastify/fastify/blob/main/docs/Reference/LTS.md) for more details. | ||
## Usage | ||
Require `@fastify/cors` and register it as any other plugin, it will add a `onRequest` hook and a [wildcard options route](https://github.com/fastify/fastify/issues/326#issuecomment-411360862). | ||
Require `@fastify/cors` and register it as any other plugin, it will add an `onRequest` hook and a [wildcard options route](https://github.com/fastify/fastify/issues/326#issuecomment-411360862). | ||
```js | ||
@@ -61,3 +69,3 @@ import Fastify from 'fastify' | ||
* `methods`: Configures the **Access-Control-Allow-Methods** CORS header. Expects a comma-delimited string (ex: 'GET,PUT,POST') or an array (ex: `['GET', 'PUT', 'POST']`). | ||
* `hook`: See the section `Custom Fastify hook name` (default: `onResponse`) | ||
* `hook`: See the section `Custom Fastify hook name` (default: `onRequest`) | ||
* `allowedHeaders`: Configures the **Access-Control-Allow-Headers** CORS header. Expects a comma-delimited string (ex: `'Content-Type,Authorization'`) or an array (ex: `['Content-Type', 'Authorization']`). If not specified, defaults to reflecting the headers specified in the request's **Access-Control-Request-Headers** header. | ||
@@ -64,0 +72,0 @@ * `exposedHeaders`: Configures the **Access-Control-Expose-Headers** CORS header. Expects a comma-delimited string (ex: `'Content-Range,X-Content-Range'`) or an array (ex: `['Content-Range', 'X-Content-Range']`). If not specified, no custom headers are exposed. |
'use strict' | ||
const { test } = require('tap') | ||
const { createReadStream, statSync, readFileSync } = require('node:fs') | ||
const Fastify = require('fastify') | ||
const cors = require('../') | ||
const { resolve } = require('node:path') | ||
@@ -31,2 +33,34 @@ test('Should add cors headers', t => { | ||
test('Should add cors headers when payload is a stream', t => { | ||
t.plan(4) | ||
const fastify = Fastify() | ||
fastify.register(cors) | ||
const filePath = resolve(__dirname, __filename) | ||
fastify.get('/', (req, reply) => { | ||
const stream = createReadStream(filePath) | ||
reply | ||
.type('application/json') | ||
.header('Content-Length', statSync(filePath).size) | ||
.send(stream) | ||
}) | ||
const fileContent = readFileSync(filePath, 'utf-8') | ||
fastify.inject({ | ||
method: 'GET', | ||
url: '/' | ||
}, (err, res) => { | ||
t.error(err) | ||
delete res.headers.date | ||
t.equal(res.statusCode, 200) | ||
t.equal(res.payload, fileContent) | ||
t.match(res.headers, { | ||
'access-control-allow-origin': '*', | ||
'content-length': statSync(filePath).size | ||
}) | ||
}) | ||
}) | ||
test('Should add cors headers (custom values)', t => { | ||
@@ -33,0 +67,0 @@ t.plan(8) |
'use strict' | ||
const LRUCache = require('mnemonist').LRUCache | ||
const LRUCache = require('mnemonist/lru-cache') | ||
@@ -23,3 +23,3 @@ /** | ||
const validFieldnameRE = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/ | ||
const validFieldnameRE = /^[!#$%&'*+\-.^\w`|~]+$/u | ||
function validateFieldname (fieldname) { | ||
@@ -26,0 +26,0 @@ if (validFieldnameRE.test(fieldname) === false) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
93356
2781
170