Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access-Control-Allow-Origin is not working when streaming the data #259

Closed
2 tasks done
iserioton opened this issue Jul 21, 2023 · 6 comments · Fixed by #260
Closed
2 tasks done

Access-Control-Allow-Origin is not working when streaming the data #259

iserioton opened this issue Jul 21, 2023 · 6 comments · Fixed by #260

Comments

@iserioton
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

8.2.0

Plugin version

No response

Node.js version

18.16.1

Operating system

Windows

Operating system version (i.e. 20.04, 11.3, 10)

Window 10

Description

I set up the cors like this: server.register(cors, { origin: '*', });

It works for normal APIs, but when I stream the data, it does not work.
const stream = fs.createReadStream(filePath); reply.type('application/json').header('Content-Length', fs.statSync(filePath).size); return stream.pipe(reply.raw);

Steps to Reproduce

Follow the description!

Expected Behavior

It should set "Access-Control-Allow-Origin" to "*" for the response header!

@Uzlopak
Copy link
Contributor

Uzlopak commented Jul 21, 2023

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

@VityaSchel
Copy link

VityaSchel commented Jul 22, 2023

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

@Uzlopak
Copy link
Contributor

Uzlopak commented Jul 22, 2023

Why cant you use reply.send(stream)?

@mcollina
Copy link
Member

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

@iserioton
Copy link
Author

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);

@climba03003
Copy link
Member

climba03003 commented Jul 24, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants