Git Product home page Git Product logo

Comments (6)

Uzlopak avatar Uzlopak commented on June 28, 2024 2

Why cant you use reply.send(stream)?

from fastify-cors.

climba03003 avatar climba03003 commented on June 28, 2024 1

I believe nothing it can do on our side.
Do not use reply.raw directly since it bypass all logic we have.
Or do all the thing on your own if you really want to use reply.raw.

from fastify-cors.

Uzlopak avatar Uzlopak commented on June 28, 2024

See the test i added to the PR #260 on how to do it correct.

from fastify-cors.

VityaSchel avatar VityaSchel commented on June 28, 2024

Any workaround?

upd: Use custom preHandler hook:

fastify.addHook('preHandler', corsPreHandler)

export const corsPreHandler = (req: FastifyRequest, res: FastifyReply, done: HookHandlerDoneFunction) => {

  const blacklistedPaths = ['/page/:pageParam']
  if (!blacklistedPaths.includes(req.routerPath)) {
    const origin = req.headers.origin ?? ''
    res.header('Access-Control-Allow-Origin', origin)
    res.header('Access-Control-Allow-Methods', '*')
    res.header('Access-Control-Allow-Headers', '*')
  }

  const isPreflight = /options/i.test(req.method)
  if (isPreflight) {
    return res.send()
  }
      
  done()
}

taken from SO

from fastify-cors.

mcollina avatar mcollina commented on June 28, 2024

stream.pipe(reply.raw) bypasses all Fastify logic by using reply.raw. Use return stream in your route or reply.send(stream)

from fastify-cors.

iserioton avatar iserioton commented on June 28, 2024

Also worked when I did this:

reply
  .type("application/json")
  .header("Access-Control-Allow-Origin", "*")
  .header("Content-Length", fs.statSync(filePath).size);
reply.raw.writeHead(200, reply.getHeaders());
return stream.pipe(reply.raw);

from fastify-cors.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.