-
Notifications
You must be signed in to change notification settings - Fork 34
Bug 1677440 - Implement a browser upload adapter #25
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
65653dd
Implement a browser upload adapter
9d91a72
Fix unrelated bug found in storage tests
bdcb176
Compile for webext with the upload adapter for browsers
e37ac8b
Mock fetch related stuff and add JSDOM
f93a594
UploadAapter -> Uploader
93fb853
Fix tests
768ddb6
Add comment about tests efault export
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
Large diffs are not rendered by default.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| import { UploadResult, UploadResultStatus } from "upload"; | ||
| import { Uploader } from "."; | ||
|
|
||
| class BrowserUploader extends Uploader { | ||
| async post(url: URL, body: string, headers: Record<string, string> = {}): Promise<UploadResult> { | ||
| const controller = new AbortController(); | ||
| const timeout = setTimeout(() => controller.abort(), this.defaultTimeout); | ||
|
|
||
| let response; | ||
| try { | ||
| response = await fetch(url.toString(), { | ||
| headers, | ||
| method: "POST", | ||
| body: body, | ||
| keepalive: true, | ||
| // Strips any cookies or authorization headers from the request. | ||
| credentials: "omit", | ||
| signal: controller.signal, | ||
| redirect: "error", | ||
| }); | ||
| } catch(e) { | ||
| // If we time out and call controller.abort, | ||
| // the fetch API will throw a DOMException with name "AbortError". | ||
| if (e instanceof DOMException) { | ||
| console.error("Timeout while attempting to upload ping.", e); | ||
| } else if (e instanceof TypeError) { | ||
| // From MDN: "A fetch() promise will reject with a TypeError | ||
| // when a network error is encountered or CORS is misconfigured on the server-side, | ||
| // although this usually means permission issues or similar" | ||
| // See: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#checking_that_the_fetch_was_successful | ||
| // | ||
| // We will treat this as we treat server / network errors in this case. | ||
| console.error("Network while attempting to upload ping.", e); | ||
| } else { | ||
| console.error("Unknown error while attempting to upload ping.", e); | ||
| } | ||
|
|
||
| clearTimeout(timeout); | ||
| return { result: UploadResultStatus.RecoverableFailure }; | ||
| } | ||
|
|
||
| clearTimeout(timeout); | ||
| return { | ||
| result: UploadResultStatus.Success, | ||
| status: (await response.json()).status | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| export default new BrowserUploader(); |
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,46 @@ | ||
| /* This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| import { UploadResult, UploadResultStatus } from "upload"; | ||
|
|
||
| /** | ||
| * Uploader interface, actualy uploading logic varies per platform. | ||
| */ | ||
| export abstract class Uploader { | ||
| // The timeout, in seconds, to use for all operations with the server. | ||
| protected defaultTimeout = 10_000; | ||
|
|
||
| /** | ||
| * Makes a POST request to a given url, with the given headers and body. | ||
| * | ||
| * @param url The URL to make the POST request | ||
| * @param body The stringified body of this post request | ||
| * @param headers Optional header to include in the request | ||
| * | ||
| * @returns The status code of the response. | ||
| */ | ||
| abstract post(url: URL, body: string, headers?: Record<string, string>): Promise<UploadResult>; | ||
| } | ||
|
|
||
| // Default export for tests sake. | ||
brizental marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // | ||
| // This is necessary, because when building for release we will not use this index file. | ||
| // Instead, each platform should have their own Uploader implementation and alias the "upload/adapter" | ||
| // import to point to the their specific file. | ||
| // | ||
| // The aliasing is done in the webpack configuration, but tests don't use webpack and expect | ||
| // this file to have a default export. | ||
| // | ||
| // TODO: remove this comment once Bug 1687516 is resolved. | ||
| class MockUploader extends Uploader { | ||
| post(): Promise<UploadResult> { | ||
| const result: UploadResult = { | ||
| result: UploadResultStatus.Success, | ||
| status: 200 | ||
| }; | ||
| return Promise.resolve(result); | ||
| } | ||
| } | ||
|
|
||
| export default new MockUploader(); | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I copied this from the python bindings, on the android bindings we have two different timeouts: one for connection timeout and another for request timeout. I don't know that there is a way to make this differentiation here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's fine to not have the differentiation if the uploader doesn't allow it :)