-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathpreviewEntry.js
More file actions
218 lines (207 loc) · 6 KB
/
Copy pathpreviewEntry.js
File metadata and controls
218 lines (207 loc) · 6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import { Hook, Decode, Encode } from 'console-feed';
import StackTrace from 'stacktrace-js';
import { evaluateExpression } from './evaluateExpression';
// should postMessage user the dispatcher? does the parent window need to
// be registered as a frame? or a just a listener?
// could maybe send these as a message idk
// const { editor } = window;
const editor = window.parent.parent;
const { editorOrigin } = window;
const htmlOffset = 12;
window.objectUrls[window.location.href] = '/index.html';
const blobPath = window.location.href.split('/').pop();
window.objectPaths[blobPath] = 'index.html';
let hitCount = 0;
let lastHitTime = 0;
let firstLine = null;
let stopTimeout = null;
window.loopProtect = {
hit: function handleLoopHit(line) {
const now = Date.now();
if (now - lastHitTime > 1000) {
hitCount = 0;
firstLine = null;
if (stopTimeout) {
clearTimeout(stopTimeout);
stopTimeout = null;
}
}
hitCount++;
lastHitTime = now;
if (hitCount === 1) {
firstLine = line;
stopTimeout = setTimeout(() => {
if (hitCount === 1) {
const msg = `Infinite loop detected at line ${firstLine}. Stopping execution.`;
throw new Error(msg);
}
}, 30);
}
if (hitCount > 1) {
if (stopTimeout) {
clearTimeout(stopTimeout);
stopTimeout = null;
}
const msg = 'Multiple infinite loops detected. Stopping execution.';
throw new Error(msg);
}
return true;
}
};
const consoleBuffer = [];
const LOGWAIT = 500;
Hook(window.console, (log) => {
consoleBuffer.push({
log
});
});
setInterval(() => {
if (consoleBuffer.length > 0) {
const message = {
messages: consoleBuffer,
source: 'sketch'
};
editor.postMessage(message, editorOrigin);
consoleBuffer.length = 0;
}
}, LOGWAIT);
function handleMessageEvent(e) {
// maybe don't need this?? idk!
if (window.origin !== e.origin) return;
const { data } = e;
const { source, messages } = data;
if (source === 'console' && Array.isArray(messages)) {
const decodedMessages = messages.map((message) => Decode(message.log));
decodedMessages.forEach((message) => {
const { data: args } = message;
const { result, error } = evaluateExpression(args);
const resultMessages = [
{ log: Encode({ method: error ? 'error' : 'result', data: [result] }) }
];
editor.postMessage(
{
messages: resultMessages,
source: 'sketch'
},
editorOrigin
);
});
}
}
window.addEventListener('message', handleMessageEvent);
// catch reference errors, via http://stackoverflow.com/a/12747364/2994108
window.onerror = async function onError(
msg,
source,
lineNumber,
columnNo,
error
) {
// maybe i can use error.stack sometime but i'm having a hard time triggering
// this function
let data;
if (!error) {
data = msg;
} else {
data = `${error.name}: ${error.message}`;
const resolvedFileName = window.objectUrls[source];
let resolvedLineNo = lineNumber;
if (window.objectUrls[source] === 'index.html') {
resolvedLineNo = lineNumber - htmlOffset;
}
const line = `\n at ${resolvedFileName}:${resolvedLineNo}:${columnNo}`;
data = data.concat(line);
}
editor.postMessage(
{
source: 'sketch',
messages: [
{
log: [
{
method: 'error',
data: [data],
id: Date.now().toString()
}
]
}
]
},
editorOrigin
);
return false;
};
// catch rejected promises
window.onunhandledrejection = async function onUnhandledRejection(event) {
if (event.reason && event.reason.message) {
let stackLines = [];
if (event.reason.stack) {
stackLines = await StackTrace.fromError(event.reason);
}
let data = `${event.reason.name}: ${event.reason.message}`;
stackLines.forEach((stackLine) => {
const { fileName, functionName, lineNumber, columnNumber } = stackLine;
const resolvedFileName = window.objectUrls[fileName] || fileName;
const resolvedFuncName = functionName || '(anonymous function)';
let line;
if (lineNumber && columnNumber) {
let resolvedLineNumber = lineNumber;
if (resolvedFileName === 'index.html') {
resolvedLineNumber = lineNumber - htmlOffset;
}
line = `\n at ${resolvedFuncName} (${resolvedFileName}:${resolvedLineNumber}:${columnNumber})`;
} else {
line = `\n at ${resolvedFuncName} (${resolvedFileName})`;
}
data = data.concat(line);
});
editor.postMessage(
{
source: 'sketch',
messages: [
{
log: [
{
method: 'error',
data: [data],
id: Date.now().toString()
}
]
}
]
},
editorOrigin
);
}
};
// Monkeypatch p5._friendlyError
const _report = window.p5?._report;
if (_report) {
window.p5._report = function resolvedReport(message, method, color) {
const urls = Object.keys(window.objectUrls);
const paths = Object.keys(window.objectPaths);
let newMessage = message;
urls.forEach((url) => {
newMessage = newMessage.replaceAll(url, window.objectUrls[url]);
if (newMessage.match('index.html')) {
const onLineRegex = /on line (?<lineNo>.\d) in/gm;
const lineNoRegex = /index\.html:(?<lineNo>.\d):/gm;
const match = onLineRegex.exec(newMessage);
const line = match.groups.lineNo;
const resolvedLine = parseInt(line, 10) - htmlOffset;
newMessage = newMessage.replace(
onLineRegex,
`on line ${resolvedLine} in`
);
newMessage = newMessage.replace(
lineNoRegex,
`index.html:${resolvedLine}:`
);
}
});
paths.forEach((path) => {
newMessage = newMessage.replaceAll(path, window.objectPaths[path]);
});
_report.apply(window.p5, [newMessage, method, color]);
};
}