Skip to content

Commit 61e36b5

Browse files
committed
add local files into the logs when required
1 parent 5602951 commit 61e36b5

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

src/lib/pm-log-capture.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import JSZip from 'jszip';
2+
import uid from './uid';
3+
14
/**
25
* String.prototype.indexOf, but it returns NaN not -1 on failure
36
* @param {string} str The string to check in
@@ -143,9 +146,27 @@ const parseStack = (stack, url, line, column) => {
143146
return _parseChromeStack(stack);
144147
};
145148
const downloadLogs = async () => {
146-
const str = JSON.stringify(consoleLogs);
147-
let blob = new Blob([str]);
148-
let filename = 'pm-error-download.json';
149+
const files = new JSZip();
150+
files.file('logs.json', JSON.stringify(consoleLogs));
151+
const index = {};
152+
// get files
153+
// sadly, this may just dead ass fail to get files due to blob lifecycle
154+
// and i dont want to make these files get stored at runtime, cause poopy doo doo ram
155+
for (const log of consoleLogs) {
156+
for (const trace of log.trace) {
157+
if (index[trace.url]) continue;
158+
const id = uid();
159+
const content = await fetch(trace.url)
160+
.then(res => res.ok ? res.text() : null)
161+
.catch(() => {});
162+
if (!content) continue;
163+
files.file(id, content);
164+
index[trace.url] = id;
165+
}
166+
}
167+
files.file('index.json', JSON.stringify(index));
168+
let blob = await files.generateAsync({ type: 'blob', compression: 'DEFLATE' });
169+
let filename = 'pm-error-download.pml';
149170
/* actually, this is a bad idea
150171
// if we can, include the project
151172
if (vm) {

src/lib/uid.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @fileoverview UID generator, from Blockly.
3+
*/
4+
5+
/**
6+
* Legal characters for the unique ID.
7+
* Should be all on a US keyboard. No XML special characters or control codes.
8+
* Removed $ due to issue 251.
9+
* Removed all symbols due to use in files
10+
* @private
11+
*/
12+
const soup_ = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
13+
14+
/**
15+
* Generate a unique ID, from Blockly. This should be globally unique.
16+
* 87 characters ^ 20 length > 128 bits (better than a UUID).
17+
* @return {string} A globally unique ID string.
18+
*/
19+
const uid = function () {
20+
const length = 20;
21+
const soupLength = soup_.length;
22+
const id = [];
23+
for (let i = 0; i < length; i++) {
24+
id[i] = soup_.charAt(Math.random() * soupLength);
25+
}
26+
return id.join('');
27+
};
28+
29+
export default uid;

0 commit comments

Comments
 (0)