@swf2es/web 0.1.15 → 0.1.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,35 @@
1
+ // What the element says of an error a SWF's code threw and nothing caught.
2
+ // The release player's message is the error's number alone, so the report
3
+ // adds what the number stands for and where it was thrown; and a SWF that
4
+ // throws the same error every frame is told of once, then at its 10th,
5
+ // 100th and so on, rather than filling the console.
6
+
7
+ /** How many stack lines a report shows: the method that threw and its callers. */
8
+ const FRAMES = 4;
9
+
10
+ export class UncaughtReports {
11
+ /** How often each error was reported, by its text and where it was thrown. */
12
+ private readonly counts = new Map<string, number>();
13
+
14
+ /**
15
+ * The line to print for an error whose AS3 text is `text`, e.g.
16
+ * "TypeError: Error #1006": with `detail`, the message in full ("Error
17
+ * #1006: value is not a function."), and `stack`, where it was made, if
18
+ * the runtime has them. Null for a repeat that is not told.
19
+ */
20
+ line(text: string, detail: string | null, stack: string | null): string | null {
21
+ const frames = stack ? stack.split("\n").slice(0, FRAMES) : [];
22
+ const key = `${text}\n${frames[0] ?? ""}`;
23
+ const count = (this.counts.get(key) ?? 0) + 1;
24
+ this.counts.set(key, count);
25
+ // The text ends in the number where the message is the release player's: its words follow.
26
+ const number = detail?.match(/^Error #\d+/)?.[0];
27
+ const full =
28
+ detail && number && text.endsWith(number) ? text + detail.slice(number.length) : text;
29
+ if (count === 1) {
30
+ return [`swf2es: ${full}`, ...frames].join("\n");
31
+ }
32
+
33
+ return /^10*$/.test(String(count)) ? `swf2es: ${full} (${count} times)` : null;
34
+ }
35
+ }