Comments (5)
The line that adds some default options for the OPTIONS response seem to be affecting it - perhaps because it sets a default schema that isn't merged?
I'm just guessing because commenting out line 36 (snippet below) in fastify-cors' index.js seems to avoid this issue, and was introduced in 2.2.0.
if (preflight === true) {
fastify.options('*', { schema: { hide: hideOptionsRoute } }, (req, reply) => reply.send())
}
from fastify-cors.
We should investigate, but if you switch
fastify.register(sharedSchemas);
fastify.register(cors);
it seems to work
EDIT:
the register()
function doesn't return a promise, you don't need to await it
from fastify-cors.
Hmm OK, after some debugging I'm not sure if it's just my misunderstanding on the conceptual/documentation thing on fastify's schema, route, and plugin encapsulation.
Maybe it makes sense to move this to the fastify
repo instead because it's not really a cors plugin issue.
@Eomm you seem to be also doing some work on the schema compilation/registration so I guess you're the best person to ask about this.
Some things I found out:
- Registering a route with a schema option before registering the schema in a plugin, then using the schema. This causes the schema compiler to run twice, but on the second run it doesn't have the same list of schemas to compile, causing it to throw a reference error.
const fastify = require('fastify')({ logger: true });
const fp = require('fastify-plugin');
fastify.get('/', { schema: {} }, (req, reply) => reply.send());
fastify.register(fp(async f => f.addSchema({
$id: 'https://example.com/bson/objectId',
type: 'string',
pattern: '\\b[0-9A-Fa-f]{24}\\b',
})));
fastify.get('/:id', {
schema: {
params: {
type: 'object',
properties: {
id: { $ref: 'https://example.com/bson/objectId#' },
},
},
},
handler: async (req) => ({ hello: req.params.id }),
});
fastify.listen('1337', '127.0.0.1');
- Moving the schema registration out of the plugin makes it fine then. I'm not sure why? I thought running the plugin through
fastify-plugin
should also avail the schemas to whatever is in the registration scope, but it seems I'm wrong about this.
The server below runs fine, and schemas compile and throw the correct error if the routes do not fulfill the pattern.
const fastify = require('fastify')({ logger: true });
fastify.get('/', { schema: {} }, (req, reply) => reply.send());
fastify.addSchema({
$id: 'https://example.com/bson/objectId',
type: 'string',
pattern: '\\b[0-9A-Fa-f]{24}\\b',
});
fastify.get('/:id', {
schema: {
params: {
type: 'object',
properties: {
id: { $ref: 'https://example.com/bson/objectId#' },
},
},
},
handler: async (req) => ({ hello: req.params.id }),
});
fastify.listen('1337', '127.0.0.1');
- Not having any routes that do not use schema options before the schema is registered in a plugin also makes it work.
const fastify = require('fastify')({ logger: true });
const fp = require('fastify-plugin');
fastify.get('/', (req, reply) => reply.send());
fastify.register(fp(async f => f.addSchema({
$id: 'https://example.com/bson/objectId',
type: 'string',
pattern: '\\b[0-9A-Fa-f]{24}\\b',
})));
fastify.get('/:id', {
schema: {
params: {
type: 'object',
properties: {
id: { $ref: 'https://example.com/bson/objectId#' },
},
},
},
handler: async (req) => ({ hello: req.params.id }),
});
fastify.listen('1337', '127.0.0.1');
from fastify-cors.
I'm closing this and opening an issue in fastify
instead.
from fastify-cors.
but it seems I'm wrong about this.
No, you are right.
This causes the schema compiler to run twice
I think this is the error, my thought is that the schemas are consumed and the $id
is removed.
We will check in the other issue π
from fastify-cors.
Related Issues (20)
- Lack of support with Fastify 4.x HOT 4
- doesnt' work with Typescript HOT 1
- Origin returns undefined HOT 14
- Latency when using a large JWT token HOT 7
- UnhandledPromiseRejectionWarning: AvvioError: Plugin must be a function or a promise HOT 1
- Allow the special `*` value in `array` in `origin` option HOT 3
- Setting for `Cache-Control` to allow preflight requests to be cached in CDNs HOT 1
- Error(s) in doc. HOT 1
- cacheControl default value is missing HOT 1
- Access-Control-Allow-Origin is not working when streaming the data HOT 6
- AvvioError [Error]: Plugin did not start in time: '[object Promise]' HOT 2
- Support Private Network CORS HOT 8
- Options Request Returns 404 running in bun HOT 1
- Missing type declaration file for β@fastify/corsβ module HOT 2
- preflight 404 on prefixed index routes in nested/scopted cors setups
- Multiple @fastify/cors HOT 2
- Do not always define `Vary` header HOT 1
- `preHandler` called twice when rejecting CORS HOT 5
- Vary with dynamic config
- Credentials header is added regardless of origin HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fastify-cors.