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

chore (ts-client): validate required Electric headers on response #1957

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

kevin-dp
Copy link
Contributor

@kevin-dp kevin-dp commented Nov 7, 2024

Fixes #1950

Questions:

  • Are the headers only required on 2XX responses or also on other responses?
    They are currently only being checked on 2XX responses.
  • Are we checking all required headers?
    Currently we check electric-offset, electric-handle, and electric-schema

Copy link
Contributor

@msfstef msfstef left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, I like the reuse of the fetch wrapper pattern!

I think possibly only 409 responses would also require an electric-handle to ensure it is possible to switch to the "right" shape, but I think doing just 200s is good enough to solve the issue of potentially misconfigured proxies

@thruflo
Copy link
Contributor

thruflo commented Nov 7, 2024

Can we also validate an electric-cursor header when we receive a 204 in response to a live request?

Not having the cursor param can result in re-requesting to the same URL.

@thruflo
Copy link
Contributor

thruflo commented Nov 7, 2024

Plus can we please write a section in the https://electric-sql.com/docs/guides/troubleshooting guide about this and include a link to it in the error message?

Basically we would want to explain that if headers are missing then your proxy needs to make sure it includes them, maybe with a code sample showing how (Kyle has some proxy code that clones the headers).

I can write this if you'd like.

Copy link
Contributor

@KyleAMathews KyleAMathews left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What did y'all think also about the idea of also tracking which URLs we request and check that we don't request the same URL twice (within a given session)?

This would catch any other issue we haven't thought of.

The only tricky thing here I can think of is we'd need to wipe the Set on shape rotation and not do the check obviously if we're retrying on errors.

missingHeaders.forEach((h) => {
msg += `- ${h}\n`
})
msg += `\nThis is often due to a proxy not allowing all headers to be returned.`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
msg += `\nThis is often due to a proxy not allowing all headers to be returned.`
msg += `\nThis is often due to a proxy not setting CORS correctly so that all Electric headers can be read by the client.`

export const requiredElectricResponseHeaders = [
`electric-offset`,
`electric-handle`,
`electric-schema`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

electric-cursor & electric-up-to-date are also required.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our API spec says that the cursor is optional on the response, is that wrong?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above I believe we only return a cursor when returning a 204 to a live request.

Copy link
Contributor

@msfstef msfstef Nov 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We actually include electric-cursor for all live requests, 200 or 204 - but only live requests

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah ok it's a bit more tricky to check for these but it's still really bad if they're not available so it it's hard to check that'd be good. But with docs on what headers CORS allows through, probably just checking the minimal always sent set is enough

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The point is the dev would see an error while developing and fix things.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, the docs are correct and electric-cursor is optional as it is only provided in live mode. I could modify the check such that it checks if the original URL contains the live query param and only then check for the presence of electric-cursor.

@KyleAMathews Regarding electric-up-to-date, it is only present on the last chunk we will get from the sync service, so we don't want to check this on every message?

@KyleAMathews
Copy link
Contributor

Checking just 200s for headers seems fine as it covers all the headers.

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 this pull request may close these issues.

Throw error if a shape response doesn't include necessary headers
4 participants