Skip to content

Commit 3a281e7

Browse files
author
Beatriz Rizental
authored
Merge pull request #430 from brizental/1716322-qml-gzip
Bug 1716322 - Enable fflate and ping payload gzipping in QML
2 parents e93806e + 141c5fa commit 3a281e7

File tree

5 files changed

+33
-29
lines changed

5 files changed

+33
-29
lines changed

CHANGELOG.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
* [#411](https://github.com/mozilla/glean.js/pull/411): Tag all messages logged by Glean with the component they are coming from.
66
* [#399](https://github.com/mozilla/glean.js/pull/399): Check if there are ping data before attempting to delete it.
77
* This change lowers the amount of log messages related to attempting to delete inexistent data.
8-
* [#415](https://github.com/mozilla/glean.js/pull/415): Gzip ping paylod before upload
8+
* [#415](https://github.com/mozilla/glean.js/pull/415), [#430](https://github.com/mozilla/glean.js/pull/430): Gzip ping paylod before upload
99
* This changes the signature of `Uploader.post` to accept `string | Uint8Array` for the `body` parameter, instead of only `string`.
10-
* The gizpping functionality is disabled in Qt/QML environments. Follow [Bug 1716322](https://bugzilla.mozilla.org/show_bug.cgi?id=1716322) for updates on the status of this feature on that platform.
1110
* [#431](https://github.com/mozilla/glean.js/pull/431): BUGFIX: Record the timestamp for events before dispatching to the internal task queue.
1211

1312
# v0.15.0 (2021-06-03)

glean/src/core/upload/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
import { gzipSync } from "fflate";
5+
import { gzipSync, strToU8 } from "fflate";
66

77
import type Platform from "../../platform/index.js";
88
import type { Configuration } from "../config.js";
@@ -140,8 +140,7 @@ class PingUploader implements PingsDatabaseObserver {
140140

141141
const stringifiedBody = JSON.stringify(ping.payload);
142142
try {
143-
const encoder = new TextEncoder();
144-
const compressedBody = gzipSync(encoder.encode(stringifiedBody));
143+
const compressedBody = gzipSync(strToU8(stringifiedBody));
145144
headers["Content-Encoding"] = "gzip";
146145
headers["Content-Length"] = compressedBody.length.toString();
147146
return {

glean/src/platform/qt/fflate.stub.js

Lines changed: 0 additions & 17 deletions
This file was deleted.

glean/src/platform/qt/uploader.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import log, { LoggingLevel } from "../../core/log.js";
66
import type Uploader from "../../core/upload/uploader.js";
77
import type { UploadResult } from "../../core/upload/uploader.js";
88
import { DEFAULT_UPLOAD_TIMEOUT_MS, UploadResultStatus } from "../../core/upload/uploader.js";
9+
import { isString } from "../../core/utils.js";
910

1011
const LOG_TAG = "platform.qt.Uploader";
1112

1213
class QtUploader implements Uploader {
13-
async post(url: string, body: string, headers: Record<string, string> = {}): Promise<UploadResult> {
14+
async post(url: string, body: string | Uint8Array, headers: Record<string, string> = {}): Promise<UploadResult> {
1415
return new Promise((resolve) => {
1516
const xhr = new XMLHttpRequest();
1617
xhr.timeout = DEFAULT_UPLOAD_TIMEOUT_MS;
@@ -48,7 +49,11 @@ class QtUploader implements Uploader {
4849
});
4950
};
5051

51-
xhr.send(body);
52+
if (!isString(body)) {
53+
xhr.send(body.buffer);
54+
} else {
55+
xhr.send(body);
56+
}
5257
});
5358
}
5459
}

glean/webpack.config.qt.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { fileURLToPath } from "url";
99
const __filename = fileURLToPath(import.meta.url);
1010
const __dirname = dirname(__filename);
1111

12+
const OUTPUT = "glean.lib.js";
13+
1214
/**
1315
* Hacky plugin that removes ".js" extensions from imports before resolving.
1416
*
@@ -30,6 +32,22 @@ class TsResolvePlugin {
3032
}
3133
}
3234

35+
/**
36+
* Plugin to remove the "use strict" statement added by Webpack before emmiting final bundle.
37+
*
38+
* Context on why this is necessary: https://github.com/101arrowz/fflate/pull/75#issuecomment-865016941,
39+
* tl;dr; "use strict" makes accessing a negative index throw an error in QML,
40+
* and not anywhere else.
41+
*/
42+
class RemoveUseStrictPlugin {
43+
apply(compiler) {
44+
compiler.hooks.shouldEmit.tap("RemoveUseStrictPlugin", compilation => {
45+
compilation.assets[OUTPUT]._value = compilation.assets[OUTPUT]._value.replace("\"use strict\";", "");
46+
return true;
47+
});
48+
}
49+
}
50+
3351
export default {
3452
entry: "./src/index/qt.ts",
3553
mode: "production",
@@ -58,14 +76,14 @@ export default {
5876
extensions: [ ".tsx", ".ts", ".js" ],
5977
plugins: [
6078
new TsResolvePlugin()
61-
],
62-
alias: {
63-
"fflate": path.resolve(__dirname, "src/platform/qt/fflate.stub.js")
64-
}
79+
]
6580
},
81+
plugins: [
82+
new RemoveUseStrictPlugin(),
83+
],
6684
output: {
6785
path: path.resolve(__dirname, "dist/qt/org/mozilla/Glean"),
68-
filename: "glean.lib.js",
86+
filename: OUTPUT,
6987
libraryTarget: "var",
7088
library: "Glean",
7189
}

0 commit comments

Comments
 (0)