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

feat(clients): Always order query params #2044

Merged
merged 6 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/selfish-rings-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@electric-sql/client": patch
"@core/elixir-client": patch
---

Always use sorted query parameters in official clients to ensure Shape URLs are cached consistently.
9 changes: 8 additions & 1 deletion packages/elixir-client/lib/electric/client/fetch/request.ex
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,14 @@ defmodule Electric.Client.Fetch.Request do
%{endpoint: endpoint} = request

if Keyword.get(opts, :query, true) do
query = request |> params() |> URI.encode_query(:rfc3986)
# Convert map to _ordered_ list of query parameters
# to ensure consistent caching
query =
request
|> params()
|> Map.to_list()
|> List.keysort(0)
|> URI.encode_query(:rfc3986)

URI.to_string(%{endpoint | query: query})
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ defmodule Electric.Client.Fetch.RequestTest do

params = URI.decode_query(query)

# should have sorted parameters
assert query == "cursor=123948&handle=my-shape&live=true&offset=1234_1&table=my_table"

assert %{
"table" => "my_table",
"cursor" => "123948",
Expand Down
3 changes: 3 additions & 0 deletions packages/typescript-client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,9 @@ export class ShapeStream<T extends Row<unknown> = Row>
fetchUrl.searchParams.set(REPLICA_PARAM, this.#replica as string)
}

// sort query params in-place for stable URLs and improved cache hits
fetchUrl.searchParams.sort()

let response!: Response
try {
response = await this.#fetchClient(fetchUrl.toString(), {
Expand Down
1 change: 1 addition & 0 deletions packages/typescript-client/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ function getNextChunkUrl(url: string, res: Response): string | void {

nextUrl.searchParams.set(SHAPE_HANDLE_QUERY_PARAM, shapeHandle)
nextUrl.searchParams.set(OFFSET_QUERY_PARAM, lastOffset)
nextUrl.searchParams.sort()
return nextUrl.toString()
}

Expand Down
27 changes: 17 additions & 10 deletions packages/typescript-client/test/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ describe(`createFetchWithChunkBuffer`, () => {
expect(result).toBe(initialResponse)

// Check if the next chunk was prefetched
const nextUrl = `${baseUrl}&handle=123&offset=456`
const nextUrl = sortUrlParams(`${baseUrl}&handle=123&offset=456`)
expect(mockFetch).toHaveBeenCalledWith(nextUrl, expect.anything())
})

Expand Down Expand Up @@ -250,23 +250,23 @@ describe(`createFetchWithChunkBuffer`, () => {
expect(mockFetch).toHaveBeenCalledTimes(1 + maxPrefetchNum)
expect(mockFetch).toHaveBeenNthCalledWith(
2,
`${baseUrl}&handle=123&offset=0`,
sortUrlParams(`${baseUrl}&handle=123&offset=0`),
expect.anything()
)
expect(mockFetch).toHaveBeenNthCalledWith(
3,
`${baseUrl}&handle=123&offset=1`,
sortUrlParams(`${baseUrl}&handle=123&offset=1`),
expect.anything()
)

// Second request consumes one of the prefetched responses and
// next one fires up
await fetchWrapper(`${baseUrl}&handle=123&offset=0`)
await fetchWrapper(sortUrlParams(`${baseUrl}&handle=123&offset=0`))
await sleep()
expect(mockFetch).toHaveBeenCalledTimes(1 + maxPrefetchNum + 1)
expect(mockFetch).toHaveBeenNthCalledWith(
4,
`${baseUrl}&handle=123&offset=2`,
sortUrlParams(`${baseUrl}&handle=123&offset=2`),
expect.anything()
)
})
Expand Down Expand Up @@ -297,7 +297,7 @@ describe(`createFetchWithChunkBuffer`, () => {
expect(result).toBe(initialResponse)

// fetch the next chunk as well
const nextUrl = `${baseUrl}&handle=123&offset=456`
const nextUrl = sortUrlParams(`${baseUrl}&handle=123&offset=456`)
const nextResult = await fetchWrapper(nextUrl)
expect(nextResult).toBe(nextResponse)

Expand Down Expand Up @@ -339,7 +339,8 @@ describe(`createFetchWithChunkBuffer`, () => {
expect(result).toBe(initialResponse)

// Prefetch should have been attempted but failed
const nextUrl = `${baseUrl}&handle=123&offset=456`
const nextUrl = sortUrlParams(`${baseUrl}&handle=123&offset=456`)

expect(mockFetch).toHaveBeenCalledWith(nextUrl, expect.anything())

// One for the main request, one for the prefetch
Expand Down Expand Up @@ -381,20 +382,20 @@ describe(`createFetchWithChunkBuffer`, () => {
expect(mockFetch).toHaveBeenNthCalledWith(1, baseUrl)
expect(mockFetch).toHaveBeenNthCalledWith(
2,
`${baseUrl}&handle=123&offset=0`,
sortUrlParams(`${baseUrl}&handle=123&offset=0`),
expect.anything()
)

// once interrupted it should have called the alt + the 2 prefetches
expect(mockFetch).toHaveBeenNthCalledWith(3, altUrl)
expect(mockFetch).toHaveBeenNthCalledWith(
4,
`${altUrl}&handle=123&offset=2`,
sortUrlParams(`${altUrl}&handle=123&offset=2`),
expect.anything()
)
expect(mockFetch).toHaveBeenNthCalledWith(
5,
`${altUrl}&handle=123&offset=3`,
sortUrlParams(`${altUrl}&handle=123&offset=3`),
expect.anything()
)
})
Expand Down Expand Up @@ -429,3 +430,9 @@ describe(`createFetchWithChunkBuffer`, () => {
expect(mockFetch).toHaveBeenCalledTimes(2)
})
})

function sortUrlParams(url: string): string {
const parsedUrl = new URL(url)
parsedUrl.searchParams.sort()
return parsedUrl.toString()
}
31 changes: 31 additions & 0 deletions packages/typescript-client/test/stream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,35 @@ describe(`ShapeStream`, () => {
},
})
})

it(`should sort query parameters for stable URLs`, async () => {
const eventTarget = new EventTarget()
const requestedUrls: Array<string> = []
const fetchWrapper = (
...args: Parameters<typeof fetch>
): Promise<Response> => {
requestedUrls.push(args[0].toString())
eventTarget.dispatchEvent(new Event(`fetch`))
return Promise.resolve(Response.error())
}

const aborter = new AbortController()
new ShapeStream({
url: shapeUrl,
table: `foo`,
where: `a=1`,
columns: [`id`],
handle: `potato`,
signal: aborter.signal,
fetchClient: fetchWrapper,
})

await new Promise((resolve) =>
eventTarget.addEventListener(`fetch`, resolve, { once: true })
)

expect(requestedUrls[0].split(`?`)[1]).toEqual(
`columns=id&handle=potato&offset=-1&table=foo&where=a%3D1`
)
})
})