-
Notifications
You must be signed in to change notification settings - Fork 34
Bug 1777182 - Add a sendBeacon uploader
#1834
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
10 commits
Select commit
Hold shift + click to select a range
fc06a27
Use the correct content length
Dexterp37 682cca1
Refactor ping requests payloads
Dexterp37 c66fb91
Given a more specific name to the browser fetch uploader
Dexterp37 103c091
Add and expose a sendBeacon uploader
Dexterp37 02ed6eb
Add sendbeacon unit tests
Dexterp37 4f54291
Pass around the same config object
Dexterp37 d6e806b
Do not stringify twice when gzipping
Dexterp37 a97f3b6
Fix debugging when using sendBeacon
Dexterp37 5eb6019
Fix tests
Dexterp37 74508e5
Address review feedback
Dexterp37 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| /* 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/. */ | ||
|
|
||
| // Error to be thrown in case the final ping body is larger than MAX_PING_BODY_SIZE. | ||
| export class PingBodyOverflowError extends Error { | ||
| constructor(message?: string) { | ||
| super(message); | ||
| this.name = "PingBodyOverflow"; | ||
| } | ||
| } |
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,50 @@ | ||
| /* 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 { gzipSync, strToU8 } from "fflate"; | ||
| import { PingBodyOverflowError } from "./ping_body_overflow_error.js"; | ||
|
|
||
| /** | ||
| * Represents a payload to be transmitted by an uploading mechanism. | ||
| */ | ||
| export default class PingRequest<BodyType extends string | Uint8Array> { | ||
| constructor ( | ||
| readonly identifier: string, | ||
| readonly headers: Record<string, string>, | ||
| readonly payload: BodyType, | ||
| readonly maxBodySize: number | ||
| ) {} | ||
|
|
||
| asCompressedPayload(): PingRequest<string | Uint8Array> { | ||
| // If this is already gzipped, do nothing. | ||
| if (this.headers["Content-Encoding"] == "gzip") { | ||
| return this; | ||
| } | ||
|
|
||
| // We prefer using `strToU8` instead of TextEncoder directly, | ||
| // because it will polyfill TextEncoder if it's not present in the environment. | ||
| // For example, IE doesn't provide TextEncoder. | ||
| const encodedBody = strToU8(this.payload as string); | ||
|
|
||
| let finalBody: string | Uint8Array; | ||
| const headers = this.headers; | ||
| try { | ||
| finalBody = gzipSync(encodedBody); | ||
| headers["Content-Encoding"] = "gzip"; | ||
| headers["Content-Length"] = finalBody.length.toString(); | ||
| } catch { | ||
| // Fall back to whatever we had. | ||
| return this; | ||
| } | ||
|
|
||
| if (finalBody.length > this.maxBodySize) { | ||
| throw new PingBodyOverflowError( | ||
| `Body for ping ${this.identifier} exceeds ${this.maxBodySize} bytes. Discarding.` | ||
| ); | ||
| } | ||
|
|
||
| return new PingRequest<string | Uint8Array>(this.identifier, headers, finalBody, this.maxBodySize); | ||
| } | ||
| } | ||
|
|
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
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,42 @@ | ||
| /* 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 type PingRequest from "../../core/upload/ping_request.js"; | ||
|
|
||
| import log, { LoggingLevel } from "../../core/log.js"; | ||
| import Uploader from "../../core/upload/uploader.js"; | ||
| import { DEFAULT_UPLOAD_TIMEOUT_MS, UploadResultStatus, UploadResult } from "../../core/upload/uploader.js"; | ||
|
|
||
| const LOG_TAG = "platform.browser.SendBeaconUploader"; | ||
|
|
||
| class BrowserSendBeaconUploader extends Uploader { | ||
| timeoutMs: number = DEFAULT_UPLOAD_TIMEOUT_MS; | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/require-await | ||
rosahbruno marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| async post( | ||
| url: string, | ||
| pingRequest: PingRequest<string | Uint8Array> | ||
| ): Promise<UploadResult> { | ||
| // While the most appropriate type would be "application/json", | ||
| // using that would cause to send CORS preflight requests. We | ||
| // instead send the content as plain text and rely on the backend | ||
| // to do the appropriate validation/parsing. | ||
| const wasQueued = navigator.sendBeacon(url, pingRequest.payload); | ||
| if (wasQueued) { | ||
| // Unfortunately we don't know much more other than data was enqueued, | ||
| // it is the agent's responsibility to manage the submission. The only | ||
| // thing we can do is remove this from our internal queue. | ||
| return new UploadResult(UploadResultStatus.Success, 200); | ||
| } | ||
| log(LOG_TAG, "The `sendBeacon` call was not serviced by the browser. Deleting data.", LoggingLevel.Error); | ||
| // If the agent says there's a problem, there's not so much we can do. | ||
| return new UploadResult(UploadResultStatus.UnrecoverableFailure); | ||
| } | ||
|
|
||
| supportsCustomHeaders(): boolean { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| export default new BrowserSendBeaconUploader(); | ||
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
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.