|
6 | 6 | return value !== undefined; |
7 | 7 | } |
8 | 8 |
|
| 9 | + /** |
| 10 | + * Convert an object to a single line string only displaying the top level keys and values. |
| 11 | + * This is meant as a compromise instead of displaying [object Object] |
| 12 | + * |
| 13 | + * This is handy for logging simple object values while also avoiding getting way out of hand |
| 14 | + * when logging large complex objects that would create a massive string using JSON.stringify directly. |
| 15 | + * This can still generate large strings for large objects like our Viewer but it's still shorter. |
| 16 | + * |
| 17 | + * @param {object} obj |
| 18 | + * @returns {string} |
| 19 | + */ |
| 20 | + function simpleStringify(obj) { |
| 21 | + if ("toString" in obj && obj.toString !== Object.prototype.toString) { |
| 22 | + // Use customized toString functions if they're specified |
| 23 | + // if it's the native function continue instead of getting [object Object] |
| 24 | + return obj.toString(); |
| 25 | + } |
| 26 | + |
| 27 | + const properties = Object.entries(obj); |
| 28 | + |
| 29 | + if (obj.constructor.name !== "Object") { |
| 30 | + // Iterate through the prototype's properties too to grab any extra getters |
| 31 | + // which are common across CesiumJS classes |
| 32 | + // https://stackoverflow.com/questions/60400066/how-to-enumerate-discover-getters-and-setters-in-javascript |
| 33 | + const prototypeProperties = Object.entries( |
| 34 | + Object.getOwnPropertyDescriptors(Reflect.getPrototypeOf(obj)), |
| 35 | + ); |
| 36 | + properties.push(...prototypeProperties); |
| 37 | + } |
| 38 | + |
| 39 | + const keyValueStrings = properties.map(([key, value]) => { |
| 40 | + let valueStr = value; |
| 41 | + if (typeof value === "string") { |
| 42 | + valueStr = `"${value}"`; |
| 43 | + } else if (typeof value === "function") { |
| 44 | + valueStr = functionString(value, true); |
| 45 | + } else if (Array.isArray(value)) { |
| 46 | + valueStr = arrayString(value); |
| 47 | + } |
| 48 | + return `${key}: ${valueStr}`; |
| 49 | + }); |
| 50 | + |
| 51 | + const className = |
| 52 | + obj.constructor.name !== "Object" ? `${obj.constructor.name} ` : ""; |
| 53 | + return `${className}{${keyValueStrings.join(", ")}}`; |
| 54 | + } |
| 55 | + |
| 56 | + function arrayString(arr) { |
| 57 | + return `[${arr.join(", ")}]`; |
| 58 | + } |
| 59 | + |
| 60 | + function functionString(func, signatureOnly) { |
| 61 | + const functionAsString = func.toString(); |
| 62 | + if (signatureOnly) { |
| 63 | + const signaturePattern = /function.*\)/; |
| 64 | + const functionSigMatch = functionAsString |
| 65 | + .toString() |
| 66 | + .match(signaturePattern); |
| 67 | + return functionSigMatch |
| 68 | + ? `${functionSigMatch[0].replace("function", "ƒ")} {...}` |
| 69 | + : "ƒ () {...}"; |
| 70 | + } |
| 71 | + |
| 72 | + const lineTruncatePattern = /function.*(?:\n.*){0,4}/; |
| 73 | + const linesTruncatedMatch = functionAsString.match(lineTruncatePattern); |
| 74 | + if (linesTruncatedMatch === null) { |
| 75 | + // unable to match and truncate by lines for some reason |
| 76 | + return linesTruncatedMatch.toString(); |
| 77 | + } |
| 78 | + let truncated = linesTruncatedMatch[0]; |
| 79 | + if (functionAsString.length > truncated.length) { |
| 80 | + truncated += "\n..."; |
| 81 | + } |
| 82 | + return truncated.replace("function", "ƒ"); |
| 83 | + } |
| 84 | + |
| 85 | + function errorLineNumber(error) { |
| 86 | + if (typeof error.stack !== "string") { |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + // Look for error.stack, "bucket.html:line:char" |
| 91 | + let lineNumber = -1; |
| 92 | + const stack = error.stack; |
| 93 | + let pos = stack.indexOf(bucket); |
| 94 | + if (pos < 0) { |
| 95 | + pos = stack.indexOf("<anonymous>"); |
| 96 | + } |
| 97 | + if (pos >= 0) { |
| 98 | + const lineStart = stack.indexOf(":", pos); |
| 99 | + if (lineStart > pos) { |
| 100 | + let lineEnd1 = stack.indexOf(":", lineStart + 1); |
| 101 | + const lineEnd2 = stack.indexOf("\n", lineStart + 1); |
| 102 | + if ( |
| 103 | + lineEnd2 > lineStart && |
| 104 | + (lineEnd2 < lineEnd1 || lineEnd1 < lineStart) |
| 105 | + ) { |
| 106 | + lineEnd1 = lineEnd2; |
| 107 | + } |
| 108 | + if (lineEnd1 > lineStart) { |
| 109 | + /*eslint-disable no-empty*/ |
| 110 | + try { |
| 111 | + lineNumber = parseInt(stack.substring(lineStart + 1, lineEnd1), 10); |
| 112 | + } catch (ex) {} |
| 113 | + /*eslint-enable no-empty*/ |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + return lineNumber; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Take a singular value and return the string representation for it. |
| 122 | + * Handles multiple types with specific handling for arrays, objects and functions. |
| 123 | + * Any value that is not specifically processed will get converted by `value.toString()` |
| 124 | + * |
| 125 | + * @param {any} value |
| 126 | + * @returns {string} |
| 127 | + */ |
9 | 128 | function print(value) { |
10 | 129 | if (value === null) { |
11 | 130 | return "null"; |
12 | | - } else if (defined(value)) { |
13 | | - return value.toString(); |
14 | 131 | } |
15 | | - return "undefined"; |
| 132 | + if (value === undefined) { |
| 133 | + return "undefined"; |
| 134 | + } |
| 135 | + if (Array.isArray(value)) { |
| 136 | + // there's a small chance this recurssion gets out of hand for nested arrays |
| 137 | + return arrayString( |
| 138 | + value.map((value) => { |
| 139 | + if (typeof value === "function") { |
| 140 | + return functionString(value, true); |
| 141 | + } |
| 142 | + return print(value); |
| 143 | + }), |
| 144 | + ); |
| 145 | + } |
| 146 | + if (typeof value.stack === "string") { |
| 147 | + // assume this is an Error object and attempt to extract the line number |
| 148 | + const lineNumber = errorLineNumber(value); |
| 149 | + if (lineNumber !== undefined) { |
| 150 | + return `${value.toString()} (on line ${lineNumber})`; |
| 151 | + } |
| 152 | + } |
| 153 | + if (typeof value === "function") { |
| 154 | + return functionString(value); |
| 155 | + } |
| 156 | + if (typeof value === "object") { |
| 157 | + return simpleStringify(value); |
| 158 | + } |
| 159 | + return value.toString(); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Combine any number of arguments into a single string converting them as needed. |
| 164 | + * |
| 165 | + * @param {any[]} args an array of any values, can be mixed types |
| 166 | + * @returns {string} |
| 167 | + */ |
| 168 | + function combineArguments(args) { |
| 169 | + return args.map(print).join(" "); |
16 | 170 | } |
17 | 171 |
|
| 172 | + const originalClear = console.clear; |
| 173 | + console.clear = function () { |
| 174 | + originalClear(); |
| 175 | + window.parent.postMessage( |
| 176 | + { |
| 177 | + type: "consoleClear", |
| 178 | + }, |
| 179 | + "*", |
| 180 | + ); |
| 181 | + }; |
| 182 | + |
18 | 183 | const originalLog = console.log; |
19 | | - console.log = function (d1) { |
20 | | - originalLog.apply(console, arguments); |
| 184 | + console.log = function (...args) { |
| 185 | + originalLog.apply(console, args); |
21 | 186 | window.parent.postMessage( |
22 | 187 | { |
23 | 188 | type: "consoleLog", |
24 | | - log: print(d1), |
| 189 | + log: combineArguments(args), |
25 | 190 | }, |
26 | 191 | "*", |
27 | 192 | ); |
28 | 193 | }; |
29 | 194 |
|
30 | 195 | const originalWarn = console.warn; |
31 | | - console.warn = function (d1) { |
32 | | - originalWarn.apply(console, arguments); |
| 196 | + console.warn = function (...args) { |
| 197 | + originalWarn.apply(console, args); |
33 | 198 | window.parent.postMessage( |
34 | 199 | { |
35 | 200 | type: "consoleWarn", |
36 | | - warn: defined(d1) ? d1.toString() : "undefined", |
| 201 | + warn: combineArguments(args), |
37 | 202 | }, |
38 | 203 | "*", |
39 | 204 | ); |
|
46 | 211 | } |
47 | 212 |
|
48 | 213 | const originalError = console.error; |
49 | | - console.error = function (d1) { |
50 | | - originalError.apply(console, arguments); |
51 | | - if (!defined(d1)) { |
| 214 | + console.error = function (...args) { |
| 215 | + originalError.apply(console, args); |
| 216 | + if (args.length === 0 || !defined(args[0])) { |
52 | 217 | window.parent.postMessage( |
53 | 218 | { |
54 | 219 | type: "consoleError", |
|
59 | 224 | return; |
60 | 225 | } |
61 | 226 |
|
62 | | - // Look for d1.stack, "bucket.html:line:char" |
63 | | - let lineNumber = -1; |
64 | | - const errorMsg = d1.toString(); |
65 | | - if (typeof d1.stack === "string") { |
66 | | - const stack = d1.stack; |
67 | | - let pos = stack.indexOf(bucket); |
68 | | - if (pos < 0) { |
69 | | - pos = stack.indexOf("<anonymous>"); |
70 | | - } |
71 | | - if (pos >= 0) { |
72 | | - const lineStart = stack.indexOf(":", pos); |
73 | | - if (lineStart > pos) { |
74 | | - let lineEnd1 = stack.indexOf(":", lineStart + 1); |
75 | | - const lineEnd2 = stack.indexOf("\n", lineStart + 1); |
76 | | - if ( |
77 | | - lineEnd2 > lineStart && |
78 | | - (lineEnd2 < lineEnd1 || lineEnd1 < lineStart) |
79 | | - ) { |
80 | | - lineEnd1 = lineEnd2; |
81 | | - } |
82 | | - if (lineEnd1 > lineStart) { |
83 | | - /*eslint-disable no-empty*/ |
84 | | - try { |
85 | | - lineNumber = parseInt( |
86 | | - stack.substring(lineStart + 1, lineEnd1), |
87 | | - 10, |
88 | | - ); |
89 | | - } catch (ex) {} |
90 | | - /*eslint-enable no-empty*/ |
91 | | - } |
92 | | - } |
93 | | - } |
94 | | - } |
95 | | - |
96 | | - if (lineNumber >= 0) { |
97 | | - window.parent.postMessage( |
98 | | - { |
99 | | - type: "consoleError", |
100 | | - error: errorMsg, |
101 | | - lineNumber: lineNumber, |
102 | | - }, |
103 | | - "*", |
104 | | - ); |
105 | | - } else { |
106 | | - window.parent.postMessage( |
107 | | - { |
108 | | - type: "consoleError", |
109 | | - error: errorMsg, |
110 | | - }, |
111 | | - "*", |
112 | | - ); |
113 | | - } |
| 227 | + window.parent.postMessage( |
| 228 | + { |
| 229 | + type: "consoleError", |
| 230 | + error: combineArguments(args), |
| 231 | + }, |
| 232 | + "*", |
| 233 | + ); |
114 | 234 | }; |
115 | 235 |
|
116 | 236 | window.onerror = function (errorMsg, url, lineNumber) { |
|
0 commit comments