Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 01a7a69

Browse files
committed
Add a rageshake function to download the logs locally
For element-hq/element-web#3304 This generates something similar to what the rageshake server does, just in an easy package for people to use. tar-js was chosen over zip or anything else because it's small, simple, and stable. Note: this doesn't work in Chrome, but firefox seems to be okay with it. Chrome appears to be blocking the download for some reason - the very first download was fine, but none afterwards
1 parent cad9562 commit 01a7a69

File tree

4 files changed

+95
-20
lines changed

4 files changed

+95
-20
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
"react-gemini-scrollbar": "github:matrix-org/react-gemini-scrollbar#9cf17f63b7c0b0ec5f31df27da0f82f7238dc594",
9898
"resize-observer-polyfill": "^1.5.0",
9999
"sanitize-html": "^1.18.4",
100+
"tar-js": "^0.3.0",
100101
"text-encoding-utf-8": "^1.0.1",
101102
"url": "^0.11.0",
102103
"velocity-animate": "^1.5.2",

src/i18n/strings/en_EN.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@
418418
"Collecting app version information": "Collecting app version information",
419419
"Collecting logs": "Collecting logs",
420420
"Uploading report": "Uploading report",
421+
"Downloading report": "Downloading report",
421422
"Waiting for response from server": "Waiting for response from server",
422423
"Messages containing my display name": "Messages containing my display name",
423424
"Messages containing my username": "Messages containing my username",

src/rageshake/submit-rageshake.js

Lines changed: 88 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import pako from 'pako';
2121
import {MatrixClientPeg} from '../MatrixClientPeg';
2222
import PlatformPeg from '../PlatformPeg';
2323
import { _t } from '../languageHandler';
24+
import Tar from "tar-js";
2425

2526
import * as rageshake from './rageshake';
2627

@@ -33,26 +34,7 @@ if (!TextEncoder) {
3334
TextEncoder = TextEncodingUtf8.TextEncoder;
3435
}
3536

36-
/**
37-
* Send a bug report.
38-
*
39-
* @param {string} bugReportEndpoint HTTP url to send the report to
40-
*
41-
* @param {object} opts optional dictionary of options
42-
*
43-
* @param {string} opts.userText Any additional user input.
44-
*
45-
* @param {boolean} opts.sendLogs True to send logs
46-
*
47-
* @param {function(string)} opts.progressCallback Callback to call with progress updates
48-
*
49-
* @return {Promise} Resolved when the bug report is sent.
50-
*/
51-
export default async function sendBugReport(bugReportEndpoint, opts) {
52-
if (!bugReportEndpoint) {
53-
throw new Error("No bug report endpoint has been set.");
54-
}
55-
37+
async function collectBugReport(opts) {
5638
opts = opts || {};
5739
const progressCallback = opts.progressCallback || (() => {});
5840

@@ -106,10 +88,96 @@ export default async function sendBugReport(bugReportEndpoint, opts) {
10688
}
10789
}
10890

91+
return body;
92+
}
93+
94+
/**
95+
* Send a bug report.
96+
*
97+
* @param {string} bugReportEndpoint HTTP url to send the report to
98+
*
99+
* @param {object} opts optional dictionary of options
100+
*
101+
* @param {string} opts.userText Any additional user input.
102+
*
103+
* @param {boolean} opts.sendLogs True to send logs
104+
*
105+
* @param {function(string)} opts.progressCallback Callback to call with progress updates
106+
*
107+
* @return {Promise} Resolved when the bug report is sent.
108+
*/
109+
export default async function sendBugReport(bugReportEndpoint, opts) {
110+
if (!bugReportEndpoint) {
111+
throw new Error("No bug report endpoint has been set.");
112+
}
113+
114+
opts = opts || {};
115+
const progressCallback = opts.progressCallback || (() => {});
116+
const body = await collectBugReport(opts);
117+
109118
progressCallback(_t("Uploading report"));
110119
await _submitReport(bugReportEndpoint, body, progressCallback);
111120
}
112121

122+
/**
123+
* Downloads the files from a bug report. This is the same as sendBugReport,
124+
* but instead causes the browser to download the files locally.
125+
*
126+
* @param {object} opts optional dictionary of options
127+
*
128+
* @param {string} opts.userText Any additional user input.
129+
*
130+
* @param {boolean} opts.sendLogs True to send logs
131+
*
132+
* @param {function(string)} opts.progressCallback Callback to call with progress updates
133+
*
134+
* @return {Promise} Resolved when the bug report is downloaded (or started).
135+
*/
136+
export async function downloadBugReport(opts) {
137+
opts = opts || {};
138+
const progressCallback = opts.progressCallback || (() => {});
139+
const body = await collectBugReport(opts);
140+
141+
progressCallback(_t("Downloading report"));
142+
let metadata = "";
143+
const tape = new Tar();
144+
let i = 0;
145+
for (const e of body.entries()) {
146+
if (e[0] === 'compressed-log') {
147+
await new Promise((resolve => {
148+
const reader = new FileReader();
149+
reader.addEventListener('loadend', ev => {
150+
tape.append(`log-${i++}.log`, pako.ungzip(ev.target.result));
151+
resolve();
152+
});
153+
reader.readAsArrayBuffer(e[1]);
154+
}))
155+
} else {
156+
metadata += `${e[0]} = ${e[1]}\n`;
157+
}
158+
}
159+
tape.append('issue.txt', metadata);
160+
161+
// We have to create a new anchor to download if we want a filename. Otherwise we could
162+
// just use window.open.
163+
const dl = document.createElement('a');
164+
dl.href = `data:application/octet-stream;base64,${btoa(uint8ToString(tape.out))}`;
165+
dl.download = 'rageshake.tar';
166+
document.body.appendChild(dl);
167+
dl.click();
168+
document.body.removeChild(dl);
169+
}
170+
171+
// Source: https://github.com/beatgammit/tar-js/blob/master/examples/main.js
172+
function uint8ToString(buf) {
173+
let i, length, out = '';
174+
for (i = 0, length = buf.length; i < length; i += 1) {
175+
out += String.fromCharCode(buf[i]);
176+
}
177+
178+
return out;
179+
}
180+
113181
function _submitReport(endpoint, body, progressCallback) {
114182
return new Promise((resolve, reject) => {
115183
const req = new XMLHttpRequest();

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8251,6 +8251,11 @@ tapable@^1.0.0, tapable@^1.1.3:
82518251
resolved "https://registry.yarnpkg.com/tapable/-/tapable-1.1.3.tgz#a1fccc06b58db61fd7a45da2da44f5f3a3e67ba2"
82528252
integrity sha512-4WK/bYZmj8xLr+HUCODHGF1ZFzsYffasLUgEiMBY4fgtltdO6B4WJtlSbPaDTLpYTcGVwM2qLnFTICEcNxs3kA==
82538253

8254+
tar-js@^0.3.0:
8255+
version "0.3.0"
8256+
resolved "https://registry.yarnpkg.com/tar-js/-/tar-js-0.3.0.tgz#6949aabfb0ba18bb1562ae51a439fd0f30183a17"
8257+
integrity sha1-aUmqv7C6GLsVYq5RpDn9DzAYOhc=
8258+
82548259
tar@^4:
82558260
version "4.4.13"
82568261
resolved "https://registry.yarnpkg.com/tar/-/tar-4.4.13.tgz#43b364bc52888d555298637b10d60790254ab525"

0 commit comments

Comments
 (0)