Repository navigation
fix: make remote media downloads wait once and stop falling back on real errors #410
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
Open
cardoso
wants to merge
5
commits into
main
Choose a base branch
from
fix/CORE-2617
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
508aaa9
fix: wait for remote media that is still being uploaded
cardoso 2317136
fix: keep media timeout_ms within the federation transport timeout
cardoso 27a0b08
fix: only fall back to legacy media endpoints when the endpoint is un…
cardoso 09cfa6a
refactor: drop FEDERATION_MEDIA_DOWNLOAD_TIMEOUT_MS
cardoso b9ce195
fix: fail the request when a federation error body cannot be read
cardoso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
packages/federation-sdk/src/services/media.service.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| import { describe, expect, it, mock } from 'bun:test'; | ||
|
|
||
| import { FEDERATION_REQUEST_TIMEOUT_MS } from '@rocket.chat/federation-core'; | ||
| import type { FetchResponse } from '@rocket.chat/federation-core'; | ||
|
|
||
| import type { ConfigService } from './config.service'; | ||
| import { FederationRequestError } from './federation-request.service'; | ||
| import type { FederationRequestService } from './federation-request.service'; | ||
| import { MediaService, isUnknownEndpoint } from './media.service'; | ||
|
|
||
| const buildService = (requestBinaryData: unknown) => | ||
| new MediaService({} as ConfigService, { requestBinaryData } as unknown as FederationRequestService); | ||
|
|
||
| const federationError = (status: number, body = '') => new FederationRequestError({ status } as FetchResponse<unknown>, body); | ||
|
|
||
| const unknownEndpoint = () => federationError(404, JSON.stringify({ errcode: 'M_UNRECOGNIZED' })); | ||
|
|
||
| describe('MediaService.downloadFromRemoteServer', () => { | ||
| it('asks the origin to wait for an upload that is still being committed', async () => { | ||
| const calls: { endpoint: string; queryParams?: Record<string, string> }[] = []; | ||
| const requestBinaryData = mock(async (_method: string, _server: string, endpoint: string, queryParams?: Record<string, string>) => { | ||
| calls.push({ endpoint, queryParams }); | ||
| throw unknownEndpoint(); | ||
| }); | ||
|
|
||
| await buildService(requestBinaryData) | ||
| .downloadFromRemoteServer('remote.example', 'abc') | ||
| .catch(() => undefined); | ||
|
|
||
| expect(calls).toHaveLength(3); | ||
| for (const call of calls) { | ||
| // long enough for an upload to finish, short enough that the origin answers before we hang up | ||
| expect(Number(call.queryParams?.timeout_ms)).toBeGreaterThan(0); | ||
| expect(Number(call.queryParams?.timeout_ms)).toBeLessThan(FEDERATION_REQUEST_TIMEOUT_MS); | ||
| } | ||
| }); | ||
|
|
||
| it('does not let the origin fetch the file from a third server on our behalf', async () => { | ||
| const calls: { endpoint: string; queryParams?: Record<string, string> }[] = []; | ||
| const requestBinaryData = mock(async (_method: string, _server: string, endpoint: string, queryParams?: Record<string, string>) => { | ||
| calls.push({ endpoint, queryParams }); | ||
| throw unknownEndpoint(); | ||
| }); | ||
|
|
||
| await buildService(requestBinaryData) | ||
| .downloadFromRemoteServer('remote.example', 'abc') | ||
| .catch(() => undefined); | ||
|
|
||
| expect(calls[0]?.queryParams?.allow_remote).toBeUndefined(); | ||
| expect(calls[1]?.queryParams?.allow_remote).toBe('false'); | ||
| expect(calls[2]?.queryParams?.allow_remote).toBe('false'); | ||
| }); | ||
|
|
||
| it('stops at the first endpoint when the origin answers that the media is not uploaded yet', async () => { | ||
| const requestBinaryData = mock(async () => { | ||
| throw federationError(504, JSON.stringify({ errcode: 'M_NOT_YET_UPLOADED', error: 'Media has not been uploaded yet' })); | ||
| }); | ||
|
|
||
| await expect(buildService(requestBinaryData).downloadFromRemoteServer('remote.example', 'abc')).rejects.toThrow( | ||
| 'Failed to download media abc from remote.example', | ||
| ); | ||
| expect(requestBinaryData).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('stops at the first endpoint when the origin does not have the media', async () => { | ||
| const requestBinaryData = mock(async () => { | ||
| throw federationError(404, JSON.stringify({ errcode: 'M_NOT_FOUND', error: 'Not found' })); | ||
| }); | ||
|
|
||
| await buildService(requestBinaryData) | ||
| .downloadFromRemoteServer('remote.example', 'abc') | ||
| .catch(() => undefined); | ||
|
|
||
| expect(requestBinaryData).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('stops at the first endpoint when the origin could not be reached', async () => { | ||
| const requestBinaryData = mock(async () => { | ||
| throw new Error('Request timed out after 20000ms'); | ||
| }); | ||
|
|
||
| await buildService(requestBinaryData) | ||
| .downloadFromRemoteServer('remote.example', 'abc') | ||
| .catch(() => undefined); | ||
|
|
||
| expect(requestBinaryData).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('keeps the origin answer as the cause of the failure', async () => { | ||
| const answer = federationError(504, JSON.stringify({ errcode: 'M_NOT_YET_UPLOADED' })); | ||
| const requestBinaryData = mock(async () => { | ||
| throw answer; | ||
| }); | ||
|
|
||
| const failure = await buildService(requestBinaryData) | ||
| .downloadFromRemoteServer('remote.example', 'abc') | ||
| .catch((err: unknown) => err); | ||
|
|
||
| expect((failure as Error).cause).toBe(answer); | ||
| }); | ||
|
|
||
| it('moves on to the legacy endpoint when the federation one is not implemented', async () => { | ||
| const content = Buffer.from('file-bytes'); | ||
| const endpoints: string[] = []; | ||
| const requestBinaryData = mock(async (_method: string, _server: string, endpoint: string) => { | ||
| endpoints.push(endpoint); | ||
| if (endpoint.startsWith('/_matrix/federation/')) { | ||
| throw unknownEndpoint(); | ||
| } | ||
| return { content }; | ||
| }); | ||
|
|
||
| const result = await buildService(requestBinaryData).downloadFromRemoteServer('remote.example', 'abc'); | ||
|
|
||
| expect(result).toBe(content); | ||
| expect(endpoints).toEqual(['/_matrix/federation/v1/media/download/abc', '/_matrix/media/v3/download/remote.example/abc']); | ||
| }); | ||
|
|
||
| it('still returns the content from the first endpoint that answers', async () => { | ||
| const content = Buffer.from('file-bytes'); | ||
| const requestBinaryData = mock(async () => ({ content })); | ||
|
|
||
| const result = await buildService(requestBinaryData).downloadFromRemoteServer('remote.example', 'abc'); | ||
|
|
||
| expect(result).toBe(content); | ||
| expect(requestBinaryData).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); | ||
|
|
||
| describe('isUnknownEndpoint', () => { | ||
| it('recognises the ways a server says it does not implement an endpoint', () => { | ||
| expect(isUnknownEndpoint(federationError(404, JSON.stringify({ errcode: 'M_UNRECOGNIZED' })))).toBe(true); | ||
| expect(isUnknownEndpoint(federationError(405, JSON.stringify({ errcode: 'M_UNRECOGNIZED' })))).toBe(true); | ||
| expect(isUnknownEndpoint(federationError(404, ''))).toBe(true); | ||
| expect(isUnknownEndpoint(federationError(404, '<html>Not Found</html>'))).toBe(true); | ||
| expect(isUnknownEndpoint(federationError(400, JSON.stringify({ errcode: 'M_UNRECOGNIZED' })))).toBe(true); | ||
| }); | ||
|
|
||
| it('treats every other answer as the origin speaking about the media', () => { | ||
| expect(isUnknownEndpoint(federationError(404, JSON.stringify({ errcode: 'M_NOT_FOUND' })))).toBe(false); | ||
| expect(isUnknownEndpoint(federationError(504, JSON.stringify({ errcode: 'M_NOT_YET_UPLOADED' })))).toBe(false); | ||
| expect(isUnknownEndpoint(federationError(502, ''))).toBe(false); | ||
| expect(isUnknownEndpoint(federationError(400, JSON.stringify({ errcode: 'M_BAD_JSON' })))).toBe(false); | ||
| expect(isUnknownEndpoint(federationError(403, JSON.stringify({ errcode: 'M_FORBIDDEN' })))).toBe(false); | ||
| expect(isUnknownEndpoint(new Error('socket hang up'))).toBe(false); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.