Skip to content

Commit a34b5ab

Browse files
authored
Use multipart-form encoding for rageshake uploads (element-hq#3646)
This is a more sensible encoding for uploading logfiles, and will allow us to compress the logfiles in future. browser-request doesn't give us enough flexibility to do this properly, so we use XMLHttpRequest directly.
1 parent 062cf47 commit a34b5ab

1 file changed

Lines changed: 45 additions & 30 deletions

File tree

‎src/vector/submit-rageshake.js‎

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,19 @@ limitations under the License.
1515
*/
1616

1717
import q from "q";
18-
import request from "browser-request";
1918

2019
import PlatformPeg from 'matrix-react-sdk/lib/PlatformPeg';
2120

2221
import rageshake from './rageshake'
2322

23+
24+
// polyfill textencoder if necessary
25+
import * as TextEncodingUtf8 from 'text-encoding-utf-8';
26+
let TextEncoder = window.TextEncoder;
27+
if (!TextEncoder) {
28+
TextEncoder = TextEncodingUtf8.TextEncoder;
29+
}
30+
2431
/**
2532
* Send a bug report.
2633
* @param {string} bugReportEndpoint HTTP url to send the report to
@@ -49,36 +56,44 @@ export default async function sendBugReport(bugReportEndpoint, opts) {
4956

5057
console.log("Sending bug report.");
5158

52-
let logs = [];
59+
const body = new FormData();
60+
body.append('text', opts.userText || "User did not supply any additional text.");
61+
body.append('app', 'riot-web');
62+
body.append('version', version);
63+
body.append('user_agent', userAgent);
64+
5365
if (opts.sendLogs) {
54-
logs = await rageshake.getLogsForReport();
66+
const logs = await rageshake.getLogsForReport();
67+
for (let entry of logs) {
68+
// encode as UTF-8
69+
const buf = new TextEncoder().encode(entry.lines);
70+
71+
body.append('log', new Blob([buf]), entry.id);
72+
}
5573
}
5674

57-
await q.Promise((resolve, reject) => {
58-
request({
59-
method: "POST",
60-
url: bugReportEndpoint,
61-
body: {
62-
logs: logs,
63-
text: (
64-
opts.userText || "User did not supply any additional text."
65-
),
66-
app: 'riot-web',
67-
version: version,
68-
user_agent: userAgent,
69-
},
70-
json: true,
71-
timeout: 5 * 60 * 1000,
72-
}, (err, res) => {
73-
if (err) {
74-
reject(err);
75-
return;
76-
}
77-
if (res.status < 200 || res.status >= 400) {
78-
reject(new Error(`HTTP ${res.status}`));
79-
return;
80-
}
81-
resolve();
82-
})
83-
});
75+
await _submitReport(bugReportEndpoint, body);
76+
}
77+
78+
function _submitReport(endpoint, body) {
79+
const deferred = q.defer();
80+
81+
const req = new XMLHttpRequest();
82+
req.open("POST", endpoint);
83+
req.timeout = 5 * 60 * 1000;
84+
req.onreadystatechange = function() {
85+
if (req.readyState === XMLHttpRequest.DONE) {
86+
on_done();
87+
}
88+
};
89+
req.send(body);
90+
return deferred.promise;
91+
92+
function on_done() {
93+
if (req.status < 200 || req.status >= 400) {
94+
deferred.reject(new Error(`HTTP ${req.status}`));
95+
return;
96+
}
97+
deferred.resolve();
98+
}
8499
}

0 commit comments

Comments
 (0)