UNPKG

webcontainer-sandbox-react

Version:

A React component library for WebContainer-based code sandboxes

2 lines (1 loc) 7.79 kB
export declare const MONITOR_SCRIPT = "\n(function() {\n \"use strict\";\n\n // \u6570\u636E\u6E05\u7406\u51FD\u6570 - \u79FB\u9664\u4E0D\u53EF\u5E8F\u5217\u5316\u7684\u5BF9\u8C61\n function sanitizeData(obj, depth = 0) {\n if (depth > 10) return \"[\u6DF1\u5EA6\u9650\u5236]\";\n \n if (obj === null || obj === undefined) {\n return obj;\n }\n \n if (typeof obj === \"string\" || typeof obj === \"number\" || typeof obj === \"boolean\") {\n return obj;\n }\n \n if (obj instanceof Error) {\n return {\n name: obj.name,\n message: obj.message,\n stack: obj.stack\n };\n }\n \n if (obj instanceof Request || obj instanceof Response || obj instanceof ReadableStream) {\n return \"[\u4E0D\u53EF\u5E8F\u5217\u5316\u5BF9\u8C61: \" + obj.constructor.name + \"]\";\n }\n \n if (obj instanceof Event) {\n return {\n type: obj.type,\n target: obj.target ? obj.target.tagName || obj.target.constructor.name : null,\n timeStamp: obj.timeStamp\n };\n }\n \n if (Array.isArray(obj)) {\n return obj.map(item => sanitizeData(item, depth + 1));\n }\n \n if (typeof obj === \"object\") {\n const result = {};\n for (const key in obj) {\n try {\n if (obj.hasOwnProperty(key)) {\n const value = obj[key];\n if (typeof value === \"function\") {\n result[key] = \"[\u51FD\u6570]\";\n } else if (value && typeof value === \"object\") {\n // \u68C0\u67E5\u662F\u5426\u662F\u4E0D\u53EF\u5E8F\u5217\u5316\u7684\u5BF9\u8C61\n if (value instanceof Request || value instanceof Response || \n value instanceof ReadableStream || value instanceof AbortController ||\n value instanceof FormData || value instanceof File) {\n result[key] = \"[\u4E0D\u53EF\u5E8F\u5217\u5316\u5BF9\u8C61: \" + value.constructor.name + \"]\";\n } else {\n result[key] = sanitizeData(value, depth + 1);\n }\n } else {\n result[key] = value;\n }\n }\n } catch (e) {\n result[key] = \"[\u8BBF\u95EE\u9519\u8BEF: \" + e.message + \"]\";\n }\n }\n return result;\n }\n \n return String(obj);\n }\n\n function reportError(errorData) {\n console.log(\"\u51C6\u5907\u62A5\u544A\u9519\u8BEF:\", errorData);\n try {\n // \u6E05\u7406\u9519\u8BEF\u6570\u636E\n const cleanErrorData = sanitizeData(errorData);\n \n if (window.parent && window.parent !== window) {\n window.parent.postMessage({\n type: \"webcontainer-error\",\n error: cleanErrorData,\n source: \"webcontainer\",\n timestamp: Date.now()\n }, \"*\");\n console.log(\"\u9519\u8BEF\u5DF2\u53D1\u9001\u5230\u7236\u9875\u9762\");\n }else if (window.top && window.top !== window) {\n window.top.postMessage({\n type: \"webcontainer-error\",\n error: cleanErrorData,\n source: \"webcontainer\",\n timestamp: Date.now()\n }, \"*\");\n }\n } catch (e) {\n console.warn(\"\u65E0\u6CD5\u53D1\u9001\u9519\u8BEF\u62A5\u544A:\", e);\n // \u5C1D\u8BD5\u53D1\u9001\u7B80\u5316\u7248\u672C\u7684\u9519\u8BEF\u4FE1\u606F\n try {\n const simplifiedError = {\n type: errorData.type || \"unknown-error\",\n message: String(errorData.message || \"\u672A\u77E5\u9519\u8BEF\"),\n timestamp: Date.now()\n };\n \n if (window.parent && window.parent !== window) {\n window.parent.postMessage({\n type: \"webcontainer-error\",\n error: simplifiedError,\n source: \"webcontainer\"\n }, \"*\");\n }\n } catch (fallbackError) {\n console.error(\"\u8FDE\u7B80\u5316\u9519\u8BEF\u90FD\u65E0\u6CD5\u53D1\u9001:\", fallbackError);\n }\n }\n }\n\n // \u5168\u5C40\u9519\u8BEF\u6355\u83B7\n window.addEventListener(\"error\", function(event) {\n reportError({\n type: \"javascript-error\",\n message: event.message,\n filename: event.filename,\n lineno: event.lineno,\n colno: event.colno,\n stack: event.error ? event.error.stack : null,\n timestamp: Date.now()\n });\n }, true);\n\n // Promise rejection\n window.addEventListener(\"unhandledrejection\", function(event) {\n console.log(\"\u6355\u83B7\u5230Promise rejection:\", event);\n reportError({\n type: \"unhandled-rejection\",\n message: event.reason ? String(event.reason) : \"Unknown rejection\",\n stack: event.reason && event.reason.stack ? event.reason.stack : null,\n reason: event.reason,\n timestamp: Date.now()\n });\n });\n\n // // React\u9519\u8BEF\u8865\u5145\n // const originalConsoleError = console.error;\n // console.error = function(...args) {\n // const errorMessage = args.map(arg => {\n // if (typeof arg === \"object\") {\n // try {\n // return JSON.stringify(arg, null, 2);\n // } catch {\n // return String(arg);\n // }\n // }\n // return String(arg);\n // }).join(\" \");\n\n // if (errorMessage.includes(\"React\") || \n // errorMessage.includes(\"component\") || \n // errorMessage.includes(\"render\")) {\n // reportError({\n // type: \"react-error\",\n // message: errorMessage,\n // timestamp: Date.now()\n // });\n // }\n\n // originalConsoleError.apply(console, args);\n // };\n\n // // \u7F51\u7EDC\u9519\u8BEF\u76D1\u542C\n // const originalFetch = window.fetch;\n // window.fetch = function(...args) {\n // return originalFetch.apply(this, args).catch(error => {\n // reportError({\n // type: \"network-error\",\n // message: \"Fetch failed: \" + error.message,\n // url: args[0],\n // timestamp: Date.now()\n // });\n // throw error;\n // });\n // };\n\n // // \u8D44\u6E90\u52A0\u8F7D\u9519\u8BEF\n // window.addEventListener(\"error\", function(event) {\n // if (event.target !== window) {\n // reportError({\n // type: \"resource-error\",\n // message: \"Failed to load \" + event.target.tagName + \": \" + (event.target.src || event.target.href),\n // element: event.target.tagName,\n // source: event.target.src || event.target.href,\n // timestamp: Date.now()\n // });\n // }\n // }, true);\n\n})();\n";