-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'release-v4.0.0-pre.1' into release
- Loading branch information
Showing
18 changed files
with
263 additions
and
71 deletions.
There are no files selected for viewing
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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 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 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 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 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 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 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 | ||
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 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.