@php-wasm/logger 2.0.12 → 2.0.13
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.
- package/index.cjs +3 -3
- package/index.cjs.map +1 -1
- package/index.js +112 -89
- package/index.js.map +1 -1
- package/lib/collectors/collect-php-logs.d.ts +1 -1
- package/lib/collectors/collect-window-errors.d.ts +1 -1
- package/lib/logger.d.ts +44 -7
- package/package.json +3 -3
package/index.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});require("@php-wasm/node-polyfills");const p="playground-log",
|
|
2
|
-
`):JSON.stringify(e,null,2),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});require("@php-wasm/node-polyfills");const p="playground-log",f=(e,...r)=>{E.dispatchEvent(new CustomEvent(p,{detail:{log:e,args:r}}))},v=(e,...r)=>{switch(typeof e.message=="string"?Reflect.set(e,"message",l(e.message)):e.message.message&&typeof e.message.message=="string"&&Reflect.set(e.message,"message",l(e.message.message)),e.severity){case t.Debug:console.debug(e.message,...r);break;case t.Info:console.info(e.message,...r);break;case t.Warn:console.warn(e.message,...r);break;case t.Error:console.error(e.message,...r);break;case t.Fatal:console.error(e.message,...r);break;default:console.log(e.message,...r)}},P=e=>e instanceof Error?[e.message,e.stack].join(`
|
|
2
|
+
`):JSON.stringify(e,null,2),g=[],u=e=>{g.push(e)},i=e=>{if(e.raw===!0)u(e.message);else{const r=w(typeof e.message=="object"?P(e.message):e.message,e.severity,e.prefix??a.JS);u(r)}},T=()=>{g.length=0};let c=0;const m="/wordpress/wp-content/debug.log",S=async e=>await e.fileExists(m)?await e.readFileAsText(m):"",W=(e,r)=>{r.addEventListener("request.end",async()=>{const s=await S(r);if(s.length>c){const o=s.substring(c);e.logMessage({message:o,severity:t.Log,raw:!0}),c=s.length}}),r.addEventListener("request.error",s=>{s=s,s.error&&(e.logMessage({message:`${s.error.message} ${s.error.stack}`,severity:t.Fatal,prefix:s.source==="request"?a.PHP:a.WASM}),e.dispatchEvent(new CustomEvent(e.fatalErrorEvent,{detail:{logs:e.getLogs(),source:s.source}})))})},t={Fatal:{name:"fatal",level:0},Error:{name:"error",level:1},Warn:{name:"warn",level:2},Log:{name:"log",level:3},Info:{name:"info",level:4},Debug:{name:"debug",level:5}},a={WASM:"Wasm Crash",PHP:"PHP",JS:"JavaScript"};class L extends EventTarget{constructor(r=[]){super(),this.fatalErrorEvent="playground-fatal-error",this.severity=t.Info,this.handlers=r}getLogs(){return this.handlers.includes(i)?[...g]:(this.error(`Logs aren't stored because the logToMemory handler isn't registered.
|
|
3
3
|
If you're using a custom logger instance, make sure to register logToMemory handler.
|
|
4
|
-
`),[])}logMessage(r,...s){for(const
|
|
4
|
+
`),[])}logMessage(r,...s){const o={...r,severity:r.severity??t.Log};for(const n of this.handlers)o.severity.level<=this.severity.level&&n(o,...s)}setSeverityFilterLevel(r){this.severity=r}log(r,...s){this.logMessage({message:r,severity:t.Log,prefix:a.JS,raw:!1},...s)}debug(r,...s){this.logMessage({message:r,severity:t.Debug,prefix:a.JS,raw:!1},...s)}info(r,...s){this.logMessage({message:r,severity:t.Info,prefix:a.JS,raw:!1},...s)}warn(r,...s){this.logMessage({message:r,severity:t.Warn,prefix:a.JS,raw:!1},...s)}error(r,...s){this.logMessage({message:r,severity:t.Error,prefix:a.JS,raw:!1},...s)}}const O=()=>{try{if(process.env.NODE_ENV==="test")return[i,f]}catch{}return[i,v,f]},E=new L(O()),l=e=>e.replace(/\t/g,""),w=(e,r,s)=>{const o=new Date,n=new Intl.DateTimeFormat("en-GB",{year:"numeric",month:"short",day:"2-digit",timeZone:"UTC"}).format(o).replace(/ /g,"-"),b=new Intl.DateTimeFormat("en-GB",{hour:"2-digit",minute:"2-digit",second:"2-digit",hour12:!1,timeZone:"UTC",timeZoneName:"short"}).format(o),M=n+" "+b;return e=l(e),`[${M}] ${s} ${r.name}: ${e}`},k=(e,r)=>{e.addEventListener(e.fatalErrorEvent,r)},x=(e,r)=>{e.logMessage({message:`${r.message} in ${r.filename} on line ${r.lineno}:${r.colno}`,severity:t.Error})},y=(e,r)=>{if(!(r!=null&&r.reason))return;const s=(r==null?void 0:r.reason.stack)??r.reason;e.logMessage({message:s,severity:t.Error})};let d=0;const $=e=>{navigator.serviceWorker.addEventListener("message",r=>{var s,o,n;((s=r.data)==null?void 0:s.numberOfOpenPlaygroundTabs)!==void 0&&d!==((o=r.data)==null?void 0:o.numberOfOpenPlaygroundTabs)&&(d=(n=r.data)==null?void 0:n.numberOfOpenPlaygroundTabs,e.debug(`Number of open Playground tabs is: ${d}`))})};let h=!1;const C=e=>{h||($(e),!(typeof window>"u")&&(window.addEventListener("error",r=>x(e,r)),window.addEventListener("unhandledrejection",r=>y(e,r)),window.addEventListener("rejectionhandled",r=>y(e,r)),h=!0))},D=e=>{e.addEventListener("activate",()=>{e.clients.matchAll().then(r=>{const s={numberOfOpenPlaygroundTabs:r.filter(o=>o.frameType==="top-level").length};for(const o of r)o.postMessage(s)})})};exports.LogPrefix=a;exports.LogSeverity=t;exports.Logger=L;exports.addCrashListener=k;exports.clearMemoryLogs=T;exports.collectPhpLogs=W;exports.collectWindowErrors=C;exports.errorLogPath=m;exports.formatLogEntry=w;exports.logEvent=f;exports.logEventType=p;exports.logToConsole=v;exports.logToMemory=i;exports.logger=E;exports.logs=g;exports.prepareLogMessage=l;exports.reportServiceWorkerMetrics=D;
|
|
5
5
|
//# sourceMappingURL=index.cjs.map
|
package/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../../../../packages/php-wasm/logger/src/lib/handlers/log-event.ts","../../../../packages/php-wasm/logger/src/lib/handlers/log-to-console.ts","../../../../packages/php-wasm/logger/src/lib/handlers/log-to-memory.ts","../../../../packages/php-wasm/logger/src/lib/collectors/collect-php-logs.ts","../../../../packages/php-wasm/logger/src/lib/logger.ts","../../../../packages/php-wasm/logger/src/lib/collectors/collect-window-errors.ts","../../../../packages/php-wasm/logger/src/lib/collectors/collect-service-worker-metrics.ts"],"sourcesContent":["import type { LogHandler } from '../log-handlers';\nimport type { Log } from '../logger';\nimport { logger } from '../logger';\n\nexport const logEventType = 'playground-log';\n\nexport const logEvent: LogHandler = (log: Log, ...args: any[]): void => {\n\tlogger.dispatchEvent(\n\t\tnew CustomEvent(logEventType, {\n\t\t\tdetail: {\n\t\t\t\tlog,\n\t\t\t\targs,\n\t\t\t},\n\t\t})\n\t);\n};\n","import type { LogHandler } from '../log-handlers';\nimport type { Log } from '../logger';\nimport { prepareLogMessage } from '../logger';\n\n/**\n * Log message to the console.\n */\nexport const logToConsole: LogHandler = (log: Log, ...args: any[]): void => {\n\tif (typeof log.message === 'string') {\n\t\t// Some errors have a read-only message property where direct\n\t\t// assignment will throw an error. The assignment is merely for\n\t\t// formatting, so let's assign with Reflect.set and avoid the error.\n\t\tReflect.set(log, 'message', prepareLogMessage(log.message));\n\t} else if (log.message.message && typeof log.message.message === 'string') {\n\t\t// Some errors have a read-only message property where direct\n\t\t// assignment will throw an error. The assignment is merely for\n\t\t// formatting, so let's assign with Reflect.set and avoid the error.\n\t\tReflect.set(\n\t\t\tlog.message,\n\t\t\t'message',\n\t\t\tprepareLogMessage(log.message.message)\n\t\t);\n\t}\n\t/* eslint-disable no-console */\n\tswitch (log.severity) {\n\t\tcase 'Debug':\n\t\t\tconsole.debug(log.message, ...args);\n\t\t\tbreak;\n\t\tcase 'Info':\n\t\t\tconsole.info(log.message, ...args);\n\t\t\tbreak;\n\t\tcase 'Warn':\n\t\t\tconsole.warn(log.message, ...args);\n\t\t\tbreak;\n\t\tcase 'Error':\n\t\t\tconsole.error(log.message, ...args);\n\t\t\tbreak;\n\t\tcase 'Fatal':\n\t\t\tconsole.error(log.message, ...args);\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tconsole.log(log.message, ...args);\n\t}\n\t/* eslint-enable no-console */\n};\n","import type { LogHandler } from '../log-handlers';\nimport type { Log } from '../logger';\nimport { formatLogEntry } from '../logger';\n\nconst prepareLogMessage = (logMessage: object): string => {\n\tif (logMessage instanceof Error) {\n\t\treturn [logMessage.message, logMessage.stack].join('\\n');\n\t}\n\treturn JSON.stringify(logMessage, null, 2);\n};\n\nexport const logs: string[] = [];\n\nconst addToLogArray = (message: string): void => {\n\tlogs.push(message);\n};\n\n/**\n * Log to memory\n */\nexport const logToMemory: LogHandler = (log: Log): void => {\n\tif (log.raw === true) {\n\t\taddToLogArray(log.message);\n\t} else {\n\t\tconst message = formatLogEntry(\n\t\t\ttypeof log.message === 'object'\n\t\t\t\t? prepareLogMessage(log.message)\n\t\t\t\t: log.message,\n\t\t\tlog.severity ?? 'Info',\n\t\t\tlog.prefix ?? 'JavaScript'\n\t\t);\n\t\taddToLogArray(message);\n\t}\n};\n\nexport const clearMemoryLogs = (): void => {\n\tlogs.length = 0;\n};\n","import type { UniversalPHP, PHPRequestErrorEvent } from '../types';\nimport type { Logger } from '../logger';\n\nlet lastPHPLogLength = 0;\nexport const errorLogPath = '/wordpress/wp-content/debug.log';\n\n/**\n * Read the WordPress debug.log file and return its content.\n *\n * @param UniversalPHP playground instance\n * @returns string The content of the debug.log file\n */\nconst getRequestPhpErrorLog = async (playground: UniversalPHP) => {\n\tif (!(await playground.fileExists(errorLogPath))) {\n\t\treturn '';\n\t}\n\treturn await playground.readFileAsText(errorLogPath);\n};\n\n/**\n * Collect PHP logs from the error_log file and log them.\n * @param UniversalPHP playground instance\n * @param loggerInstance The logger instance\n */\nexport const collectPhpLogs = (\n\tloggerInstance: Logger,\n\tplayground: UniversalPHP\n) => {\n\tplayground.addEventListener('request.end', async () => {\n\t\tconst log = await getRequestPhpErrorLog(playground);\n\t\tif (log.length > lastPHPLogLength) {\n\t\t\tconst currentLog = log.substring(lastPHPLogLength);\n\t\t\tloggerInstance.logMessage({\n\t\t\t\tmessage: currentLog,\n\t\t\t\traw: true,\n\t\t\t});\n\t\t\tlastPHPLogLength = log.length;\n\t\t}\n\t});\n\tplayground.addEventListener('request.error', (event) => {\n\t\tevent = event as PHPRequestErrorEvent;\n\t\tif (event.error) {\n\t\t\tloggerInstance.logMessage({\n\t\t\t\tmessage: `${event.error.message} ${event.error.stack}`,\n\t\t\t\tseverity: 'Fatal',\n\t\t\t\tprefix: event.source === 'request' ? 'PHP' : 'WASM Crash',\n\t\t\t});\n\t\t\tloggerInstance.dispatchEvent(\n\t\t\t\tnew CustomEvent(loggerInstance.fatalErrorEvent, {\n\t\t\t\t\tdetail: {\n\t\t\t\t\t\tlogs: loggerInstance.getLogs(),\n\t\t\t\t\t\tsource: event.source,\n\t\t\t\t\t},\n\t\t\t\t})\n\t\t\t);\n\t\t}\n\t});\n};\n","import { logEvent } from './handlers/log-event';\nimport {\n\tlogToMemory,\n\tlogToConsole,\n\tlogs,\n\ttype LogHandler,\n} from './log-handlers';\n\nexport { logEventType } from './handlers/log-event';\n\nexport { errorLogPath } from './collectors/collect-php-logs';\n\nexport type Log = {\n\tmessage: any;\n\tseverity?: LogSeverity;\n\tprefix?: LogPrefix;\n\traw?: boolean;\n};\n\n/**\n * Log severity levels.\n */\nexport type LogSeverity = 'Debug' | 'Info' | 'Warn' | 'Error' | 'Fatal';\n\n/**\n * Log prefix.\n */\nexport type LogPrefix = 'WASM Crash' | 'PHP' | 'JavaScript';\n\n/**\n * A logger for Playground.\n */\nexport class Logger extends EventTarget {\n\tpublic readonly fatalErrorEvent = 'playground-fatal-error';\n\tprivate readonly handlers: LogHandler[];\n\n\t// constructor\n\tconstructor(\n\t\t// Log handlers\n\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type\n\t\thandlers: LogHandler[] = []\n\t) {\n\t\tsuper();\n\t\tthis.handlers = handlers;\n\t}\n\n\t/**\n\t * Get all logs.\n\t * @returns string[]\n\t */\n\tpublic getLogs(): string[] {\n\t\tif (!this.handlers.includes(logToMemory)) {\n\t\t\tthis\n\t\t\t\t.error(`Logs aren't stored because the logToMemory handler isn't registered.\n\t\t\t\tIf you're using a custom logger instance, make sure to register logToMemory handler.\n\t\t\t`);\n\t\t\treturn [];\n\t\t}\n\t\treturn [...logs];\n\t}\n\n\t/**\n\t * Log message with severity.\n\t *\n\t * @param message any\n\t * @param severity LogSeverity\n\t * @param raw boolean\n\t * @param args any\n\t */\n\tpublic logMessage(log: Log, ...args: any[]): void {\n\t\tfor (const handler of this.handlers) {\n\t\t\thandler(log, ...args);\n\t\t}\n\t}\n\n\t/**\n\t * Log message\n\t *\n\t * @param message any\n\t * @param args any\n\t */\n\tpublic log(message: any, ...args: any[]): void {\n\t\tthis.logMessage(\n\t\t\t{\n\t\t\t\tmessage,\n\t\t\t\tseverity: undefined,\n\t\t\t\tprefix: 'JavaScript',\n\t\t\t\traw: false,\n\t\t\t},\n\t\t\t...args\n\t\t);\n\t}\n\n\t/**\n\t * Log debug message\n\t *\n\t * @param message any\n\t * @param args any\n\t */\n\tpublic debug(message: any, ...args: any[]): void {\n\t\tthis.logMessage(\n\t\t\t{\n\t\t\t\tmessage,\n\t\t\t\tseverity: 'Debug',\n\t\t\t\tprefix: 'JavaScript',\n\t\t\t\traw: false,\n\t\t\t},\n\t\t\t...args\n\t\t);\n\t}\n\n\t/**\n\t * Log info message\n\t *\n\t * @param message any\n\t * @param args any\n\t */\n\tpublic info(message: any, ...args: any[]): void {\n\t\tthis.logMessage(\n\t\t\t{\n\t\t\t\tmessage,\n\t\t\t\tseverity: 'Info',\n\t\t\t\tprefix: 'JavaScript',\n\t\t\t\traw: false,\n\t\t\t},\n\t\t\t...args\n\t\t);\n\t}\n\n\t/**\n\t * Log warning message\n\t *\n\t * @param message any\n\t * @param args any\n\t */\n\tpublic warn(message: any, ...args: any[]): void {\n\t\tthis.logMessage(\n\t\t\t{\n\t\t\t\tmessage,\n\t\t\t\tseverity: 'Warn',\n\t\t\t\tprefix: 'JavaScript',\n\t\t\t\traw: false,\n\t\t\t},\n\t\t\t...args\n\t\t);\n\t}\n\n\t/**\n\t * Log error message\n\t *\n\t * @param message any\n\t * @param args any\n\t */\n\tpublic error(message: any, ...args: any[]): void {\n\t\tthis.logMessage(\n\t\t\t{\n\t\t\t\tmessage,\n\t\t\t\tseverity: 'Error',\n\t\t\t\tprefix: 'JavaScript',\n\t\t\t\traw: false,\n\t\t\t},\n\t\t\t...args\n\t\t);\n\t}\n}\n\nconst getDefaultHandlers = () => {\n\ttry {\n\t\tif (process.env['NODE_ENV'] === 'test') {\n\t\t\treturn [logToMemory, logEvent];\n\t\t}\n\t} catch {\n\t\t// Process.env is not available in the browser\n\t}\n\treturn [logToMemory, logToConsole, logEvent];\n};\n\n/**\n * The logger instance.\n */\nexport const logger: Logger = new Logger(getDefaultHandlers());\n\nexport const prepareLogMessage = (message: string) => {\n\treturn message.replace(/\\t/g, '');\n};\n\nexport const formatLogEntry = (\n\tmessage: string,\n\tseverity: LogSeverity,\n\tprefix: string\n): string => {\n\tconst date = new Date();\n\tconst formattedDate = new Intl.DateTimeFormat('en-GB', {\n\t\tyear: 'numeric',\n\t\tmonth: 'short',\n\t\tday: '2-digit',\n\t\ttimeZone: 'UTC',\n\t})\n\t\t.format(date)\n\t\t.replace(/ /g, '-');\n\n\tconst formattedTime = new Intl.DateTimeFormat('en-GB', {\n\t\thour: '2-digit',\n\t\tminute: '2-digit',\n\t\tsecond: '2-digit',\n\t\thour12: false,\n\t\ttimeZone: 'UTC',\n\t\ttimeZoneName: 'short',\n\t}).format(date);\n\tconst now = formattedDate + ' ' + formattedTime;\n\tmessage = prepareLogMessage(message);\n\treturn `[${now}] ${prefix} ${severity}: ${message}`;\n};\n\n/**\n * Add a listener for the Playground crashes.\n * These crashes include Playground errors like Asyncify errors.\n * The callback function will receive an Event object with logs in the detail\n * property.\n *\n * @param loggerInstance The logger instance\n * @param callback The callback function\n */\nexport const addCrashListener = (\n\tloggerInstance: Logger,\n\tcallback: EventListenerOrEventListenerObject\n) => {\n\tloggerInstance.addEventListener(loggerInstance.fatalErrorEvent, callback);\n};\n","import type { Logger } from '../logger';\n\n/**\n * Log Windows errors.\n *\n * @param loggerInstance The logger instance\n * @param ErrorEvent event\n */\nconst logWindowErrorEvent = (loggerInstance: Logger, event: ErrorEvent) => {\n\tloggerInstance.logMessage({\n\t\tmessage: `${event.message} in ${event.filename} on line ${event.lineno}:${event.colno}`,\n\t\tseverity: 'Error',\n\t});\n};\n\n/**\n * Log promise rejections.\n *\n * @param loggerInstance The logger instance\n * @param PromiseRejectionEvent event\n */\nconst logPromiseRejection = (\n\tloggerInstance: Logger,\n\tevent: PromiseRejectionEvent\n) => {\n\t// No reason was provided, so we can't log anything.\n\tif (!event?.reason) {\n\t\treturn;\n\t}\n\tconst message = event?.reason.stack ?? event.reason;\n\tloggerInstance.logMessage({\n\t\tmessage,\n\t\tseverity: 'Error',\n\t});\n};\n\n/**\n * The number of open Playground tabs.\n */\nlet numberOfOpenPlaygroundTabs = 0;\n\n/**\n * Register a listener for service worker messages and log the data.\n * The service worker will send the number of open Playground tabs.\n *\n * @param loggerInstance The logger instance\n */\nconst addServiceWorkerMessageListener = (loggerInstance: Logger) => {\n\tnavigator.serviceWorker.addEventListener('message', (event) => {\n\t\tif (event.data?.numberOfOpenPlaygroundTabs === undefined) {\n\t\t\treturn;\n\t\t}\n\t\t// Each tab sends an activate event on load. Prevent sending the same\n\t\t// metrics multiple times if a tab is reloaded.\n\t\tif (\n\t\t\tnumberOfOpenPlaygroundTabs ===\n\t\t\tevent.data?.numberOfOpenPlaygroundTabs\n\t\t) {\n\t\t\treturn;\n\t\t}\n\t\tnumberOfOpenPlaygroundTabs = event.data?.numberOfOpenPlaygroundTabs;\n\t\tloggerInstance.debug(\n\t\t\t`Number of open Playground tabs is: ${numberOfOpenPlaygroundTabs}`\n\t\t);\n\t});\n};\n\n// If the window events are already connected.\nlet windowConnected = false;\n\n/**\n * Collect errors from JavaScript window events like error and log them.\n * @param loggerInstance The logger instance\n */\nexport const collectWindowErrors = (loggerInstance: Logger) => {\n\t// Ensure that the window events are connected only once.\n\tif (windowConnected) {\n\t\treturn;\n\t}\n\taddServiceWorkerMessageListener(loggerInstance);\n\tif (typeof window === 'undefined') {\n\t\treturn;\n\t}\n\n\twindow.addEventListener('error', (event) =>\n\t\tlogWindowErrorEvent(loggerInstance, event as ErrorEvent)\n\t);\n\twindow.addEventListener('unhandledrejection', (event) =>\n\t\tlogPromiseRejection(loggerInstance, event as PromiseRejectionEvent)\n\t);\n\twindow.addEventListener('rejectionhandled', (event) =>\n\t\tlogPromiseRejection(loggerInstance, event as PromiseRejectionEvent)\n\t);\n\twindowConnected = true;\n};\n","/// <reference lib=\"WebWorker\" />\n\n/**\n * **Call this inside a service worker.**\n * These errors include Playground errors like Asyncify errors. PHP errors\n * won't trigger this event.\n *\n * Reports service worker metrics.\n * Allows the logger to request metrics from the service worker by sending a\n * message. The service worker will respond with the number of open Playground\n * tabs.\n *\n * @param worker The service worker\n */\nexport const reportServiceWorkerMetrics = (\n\tworker: ServiceWorkerGlobalScope\n) => {\n\tworker.addEventListener('activate', () => {\n\t\tworker.clients.matchAll().then((clients) => {\n\t\t\tconst metrics = {\n\t\t\t\tnumberOfOpenPlaygroundTabs: clients.filter(\n\t\t\t\t\t// Only count top-level frames to get the number of tabs.\n\t\t\t\t\t(c) => c.frameType === 'top-level'\n\t\t\t\t).length,\n\t\t\t};\n\t\t\tfor (const client of clients) {\n\t\t\t\tclient.postMessage(metrics);\n\t\t\t}\n\t\t});\n\t});\n};\n"],"names":["logEventType","logEvent","log","args","logger","logToConsole","prepareLogMessage","logMessage","logs","addToLogArray","message","logToMemory","formatLogEntry","clearMemoryLogs","lastPHPLogLength","errorLogPath","getRequestPhpErrorLog","playground","collectPhpLogs","loggerInstance","currentLog","event","Logger","handlers","handler","getDefaultHandlers","severity","prefix","date","formattedDate","formattedTime","now","addCrashListener","callback","logWindowErrorEvent","logPromiseRejection","numberOfOpenPlaygroundTabs","addServiceWorkerMessageListener","_a","_b","_c","windowConnected","collectWindowErrors","reportServiceWorkerMetrics","worker","cl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Q,SAAW,EAAA,KAAMC,GAAY,CAC3C,MAAMC,EAAU,CACf,2BAA4BD,EAAQ,OAElCE,GAAMA,EAAE,YAAc,WAAA,EACtB,MACH,EACA,UAAWC,KAAUH,EACpBG,EAAO,YAAYF,CAAO,CAC3B,CACA,CAAA,CACD,CACF"}
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../../../../packages/php-wasm/logger/src/lib/handlers/log-event.ts","../../../../packages/php-wasm/logger/src/lib/handlers/log-to-console.ts","../../../../packages/php-wasm/logger/src/lib/handlers/log-to-memory.ts","../../../../packages/php-wasm/logger/src/lib/collectors/collect-php-logs.ts","../../../../packages/php-wasm/logger/src/lib/logger.ts","../../../../packages/php-wasm/logger/src/lib/collectors/collect-window-errors.ts","../../../../packages/php-wasm/logger/src/lib/collectors/collect-service-worker-metrics.ts"],"sourcesContent":["import type { LogHandler } from '../log-handlers';\nimport { type Log, logger } from '../logger';\n\nexport const logEventType = 'playground-log';\n\nexport const logEvent: LogHandler = (log: Log, ...args: any[]): void => {\n\tlogger.dispatchEvent(\n\t\tnew CustomEvent(logEventType, {\n\t\t\tdetail: {\n\t\t\t\tlog,\n\t\t\t\targs,\n\t\t\t},\n\t\t})\n\t);\n};\n","import type { LogHandler } from '../log-handlers';\nimport { type Log, LogSeverity, prepareLogMessage } from '../logger';\n\n/**\n * Log message to the console.\n */\nexport const logToConsole: LogHandler = (log: Log, ...args: any[]): void => {\n\tif (typeof log.message === 'string') {\n\t\t// Some errors have a read-only message property where direct\n\t\t// assignment will throw an error. The assignment is merely for\n\t\t// formatting, so let's assign with Reflect.set and avoid the error.\n\t\tReflect.set(log, 'message', prepareLogMessage(log.message));\n\t} else if (log.message.message && typeof log.message.message === 'string') {\n\t\t// Some errors have a read-only message property where direct\n\t\t// assignment will throw an error. The assignment is merely for\n\t\t// formatting, so let's assign with Reflect.set and avoid the error.\n\t\tReflect.set(\n\t\t\tlog.message,\n\t\t\t'message',\n\t\t\tprepareLogMessage(log.message.message)\n\t\t);\n\t}\n\t/* eslint-disable no-console */\n\tswitch (log.severity) {\n\t\tcase LogSeverity.Debug:\n\t\t\tconsole.debug(log.message, ...args);\n\t\t\tbreak;\n\t\tcase LogSeverity.Info:\n\t\t\tconsole.info(log.message, ...args);\n\t\t\tbreak;\n\t\tcase LogSeverity.Warn:\n\t\t\tconsole.warn(log.message, ...args);\n\t\t\tbreak;\n\t\tcase LogSeverity.Error:\n\t\t\tconsole.error(log.message, ...args);\n\t\t\tbreak;\n\t\tcase LogSeverity.Fatal:\n\t\t\tconsole.error(log.message, ...args);\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tconsole.log(log.message, ...args);\n\t}\n\t/* eslint-enable no-console */\n};\n","import type { LogHandler } from '../log-handlers';\nimport { formatLogEntry, type Log, LogPrefix } from '../logger';\n\nconst prepareLogMessage = (logMessage: object): string => {\n\tif (logMessage instanceof Error) {\n\t\treturn [logMessage.message, logMessage.stack].join('\\n');\n\t}\n\treturn JSON.stringify(logMessage, null, 2);\n};\n\nexport const logs: string[] = [];\n\nconst addToLogArray = (message: string): void => {\n\tlogs.push(message);\n};\n\n/**\n * Log to memory\n */\nexport const logToMemory: LogHandler = (log: Log): void => {\n\tif (log.raw === true) {\n\t\taddToLogArray(log.message);\n\t} else {\n\t\tconst message = formatLogEntry(\n\t\t\ttypeof log.message === 'object'\n\t\t\t\t? prepareLogMessage(log.message)\n\t\t\t\t: log.message,\n\t\t\tlog.severity,\n\t\t\tlog.prefix ?? LogPrefix.JS\n\t\t);\n\t\taddToLogArray(message);\n\t}\n};\n\nexport const clearMemoryLogs = (): void => {\n\tlogs.length = 0;\n};\n","import type { UniversalPHP, PHPRequestErrorEvent } from '../types';\nimport { type Logger, LogPrefix, LogSeverity } from '../logger';\n\nlet lastPHPLogLength = 0;\nexport const errorLogPath = '/wordpress/wp-content/debug.log';\n\n/**\n * Read the WordPress debug.log file and return its content.\n *\n * @param UniversalPHP playground instance\n * @returns string The content of the debug.log file\n */\nconst getRequestPhpErrorLog = async (playground: UniversalPHP) => {\n\tif (!(await playground.fileExists(errorLogPath))) {\n\t\treturn '';\n\t}\n\treturn await playground.readFileAsText(errorLogPath);\n};\n\n/**\n * Collect PHP logs from the error_log file and log them.\n * @param UniversalPHP playground instance\n * @param loggerInstance The logger instance\n */\nexport const collectPhpLogs = (\n\tloggerInstance: Logger,\n\tplayground: UniversalPHP\n) => {\n\tplayground.addEventListener('request.end', async () => {\n\t\tconst log = await getRequestPhpErrorLog(playground);\n\t\tif (log.length > lastPHPLogLength) {\n\t\t\tconst currentLog = log.substring(lastPHPLogLength);\n\t\t\tloggerInstance.logMessage({\n\t\t\t\tmessage: currentLog,\n\t\t\t\tseverity: LogSeverity.Log,\n\t\t\t\traw: true,\n\t\t\t});\n\t\t\tlastPHPLogLength = log.length;\n\t\t}\n\t});\n\tplayground.addEventListener('request.error', (event) => {\n\t\tevent = event as PHPRequestErrorEvent;\n\t\tif (event.error) {\n\t\t\tloggerInstance.logMessage({\n\t\t\t\tmessage: `${event.error.message} ${event.error.stack}`,\n\t\t\t\tseverity: LogSeverity.Fatal,\n\t\t\t\tprefix:\n\t\t\t\t\tevent.source === 'request' ? LogPrefix.PHP : LogPrefix.WASM,\n\t\t\t});\n\t\t\tloggerInstance.dispatchEvent(\n\t\t\t\tnew CustomEvent(loggerInstance.fatalErrorEvent, {\n\t\t\t\t\tdetail: {\n\t\t\t\t\t\tlogs: loggerInstance.getLogs(),\n\t\t\t\t\t\tsource: event.source,\n\t\t\t\t\t},\n\t\t\t\t})\n\t\t\t);\n\t\t}\n\t});\n};\n","import { logEvent } from './handlers/log-event';\nimport {\n\tlogToMemory,\n\tlogToConsole,\n\tlogs,\n\ttype LogHandler,\n} from './log-handlers';\n\nexport { logEventType } from './handlers/log-event';\n\nexport { errorLogPath } from './collectors/collect-php-logs';\n\nexport type Log = {\n\tmessage: any;\n\tseverity: LogSeverity;\n\tprefix?: LogPrefix;\n\traw?: boolean;\n};\n\n/**\n * Log severity levels.\n */\nexport const LogSeverity = {\n\tFatal: { name: 'fatal', level: 0 },\n\tError: { name: 'error', level: 1 },\n\tWarn: { name: 'warn', level: 2 },\n\tLog: { name: 'log', level: 3 },\n\tInfo: { name: 'info', level: 4 },\n\tDebug: { name: 'debug', level: 5 },\n} as const;\n\nexport type LogSeverity = (typeof LogSeverity)[keyof typeof LogSeverity];\n\n/**\n * Log prefix.\n */\nexport const LogPrefix = {\n\tWASM: 'Wasm Crash',\n\tPHP: 'PHP',\n\tJS: 'JavaScript',\n} as const;\n\nexport type LogPrefix = (typeof LogPrefix)[keyof typeof LogPrefix];\n\n/**\n * A logger for Playground.\n */\nexport class Logger extends EventTarget {\n\tpublic readonly fatalErrorEvent = 'playground-fatal-error';\n\tprivate readonly handlers: LogHandler[];\n\tprivate severity: LogSeverity = LogSeverity.Info;\n\n\t// constructor\n\tconstructor(\n\t\t// Log handlers\n\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type\n\t\thandlers: LogHandler[] = []\n\t) {\n\t\tsuper();\n\t\tthis.handlers = handlers;\n\t}\n\n\t/**\n\t * Get all logs.\n\t * @returns string[]\n\t */\n\tpublic getLogs(): string[] {\n\t\tif (!this.handlers.includes(logToMemory)) {\n\t\t\tthis\n\t\t\t\t.error(`Logs aren't stored because the logToMemory handler isn't registered.\n\t\t\t\tIf you're using a custom logger instance, make sure to register logToMemory handler.\n\t\t\t`);\n\t\t\treturn [];\n\t\t}\n\t\treturn [...logs];\n\t}\n\n\t/**\n\t * Log message with severity.\n\t *\n\t * @param log Log\n\t * @param args any\n\t */\n\tpublic logMessage(\n\t\tlog: Omit<Log, 'severity'> & { severity?: LogSeverity },\n\t\t...args: any[]\n\t): void {\n\t\tconst logWithSeverity: Log = {\n\t\t\t...log,\n\t\t\tseverity: log.severity ?? LogSeverity.Log,\n\t\t};\n\t\tfor (const handler of this.handlers) {\n\t\t\tif (logWithSeverity.severity.level <= this.severity.level) {\n\t\t\t\thandler(logWithSeverity, ...args);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Filter message based on severity\n\t * @param severity LogSeverity\n\t */\n\tpublic setSeverityFilterLevel(severity: LogSeverity): void {\n\t\tthis.severity = severity;\n\t}\n\n\t/**\n\t * Log message\n\t *\n\t * @param message any\n\t * @param args any\n\t */\n\tpublic log(message: any, ...args: any[]): void {\n\t\tthis.logMessage(\n\t\t\t{\n\t\t\t\tmessage,\n\t\t\t\tseverity: LogSeverity.Log,\n\t\t\t\tprefix: LogPrefix.JS,\n\t\t\t\traw: false,\n\t\t\t},\n\t\t\t...args\n\t\t);\n\t}\n\n\t/**\n\t * Log debug message\n\t *\n\t * @param message any\n\t * @param args any\n\t */\n\tpublic debug(message: any, ...args: any[]): void {\n\t\tthis.logMessage(\n\t\t\t{\n\t\t\t\tmessage,\n\t\t\t\tseverity: LogSeverity.Debug,\n\t\t\t\tprefix: LogPrefix.JS,\n\t\t\t\traw: false,\n\t\t\t},\n\t\t\t...args\n\t\t);\n\t}\n\n\t/**\n\t * Log info message\n\t *\n\t * @param message any\n\t * @param args any\n\t */\n\tpublic info(message: any, ...args: any[]): void {\n\t\tthis.logMessage(\n\t\t\t{\n\t\t\t\tmessage,\n\t\t\t\tseverity: LogSeverity.Info,\n\t\t\t\tprefix: LogPrefix.JS,\n\t\t\t\traw: false,\n\t\t\t},\n\t\t\t...args\n\t\t);\n\t}\n\n\t/**\n\t * Log warning message\n\t *\n\t * @param message any\n\t * @param args any\n\t */\n\tpublic warn(message: any, ...args: any[]): void {\n\t\tthis.logMessage(\n\t\t\t{\n\t\t\t\tmessage,\n\t\t\t\tseverity: LogSeverity.Warn,\n\t\t\t\tprefix: LogPrefix.JS,\n\t\t\t\traw: false,\n\t\t\t},\n\t\t\t...args\n\t\t);\n\t}\n\n\t/**\n\t * Log error message\n\t *\n\t * @param message any\n\t * @param args any\n\t */\n\tpublic error(message: any, ...args: any[]): void {\n\t\tthis.logMessage(\n\t\t\t{\n\t\t\t\tmessage,\n\t\t\t\tseverity: LogSeverity.Error,\n\t\t\t\tprefix: LogPrefix.JS,\n\t\t\t\traw: false,\n\t\t\t},\n\t\t\t...args\n\t\t);\n\t}\n}\n\nconst getDefaultHandlers = () => {\n\ttry {\n\t\tif (process.env['NODE_ENV'] === 'test') {\n\t\t\treturn [logToMemory, logEvent];\n\t\t}\n\t} catch {\n\t\t// Process.env is not available in the browser\n\t}\n\treturn [logToMemory, logToConsole, logEvent];\n};\n\n/**\n * The logger instance.\n */\nexport const logger: Logger = new Logger(getDefaultHandlers());\n\nexport const prepareLogMessage = (message: string) => {\n\treturn message.replace(/\\t/g, '');\n};\n\nexport const formatLogEntry = (\n\tmessage: string,\n\tseverity: LogSeverity,\n\tprefix: string\n): string => {\n\tconst date = new Date();\n\tconst formattedDate = new Intl.DateTimeFormat('en-GB', {\n\t\tyear: 'numeric',\n\t\tmonth: 'short',\n\t\tday: '2-digit',\n\t\ttimeZone: 'UTC',\n\t})\n\t\t.format(date)\n\t\t.replace(/ /g, '-');\n\n\tconst formattedTime = new Intl.DateTimeFormat('en-GB', {\n\t\thour: '2-digit',\n\t\tminute: '2-digit',\n\t\tsecond: '2-digit',\n\t\thour12: false,\n\t\ttimeZone: 'UTC',\n\t\ttimeZoneName: 'short',\n\t}).format(date);\n\tconst now = formattedDate + ' ' + formattedTime;\n\tmessage = prepareLogMessage(message);\n\treturn `[${now}] ${prefix} ${severity.name}: ${message}`;\n};\n\n/**\n * Add a listener for the Playground crashes.\n * These crashes include Playground errors like Asyncify errors.\n * The callback function will receive an Event object with logs in the detail\n * property.\n *\n * @param loggerInstance The logger instance\n * @param callback The callback function\n */\nexport const addCrashListener = (\n\tloggerInstance: Logger,\n\tcallback: EventListenerOrEventListenerObject\n) => {\n\tloggerInstance.addEventListener(loggerInstance.fatalErrorEvent, callback);\n};\n","import { type Logger, LogSeverity } from '../logger';\n\n/**\n * Log Windows errors.\n *\n * @param loggerInstance The logger instance\n * @param ErrorEvent event\n */\nconst logWindowErrorEvent = (loggerInstance: Logger, event: ErrorEvent) => {\n\tloggerInstance.logMessage({\n\t\tmessage: `${event.message} in ${event.filename} on line ${event.lineno}:${event.colno}`,\n\t\tseverity: LogSeverity.Error,\n\t});\n};\n\n/**\n * Log promise rejections.\n *\n * @param loggerInstance The logger instance\n * @param PromiseRejectionEvent event\n */\nconst logPromiseRejection = (\n\tloggerInstance: Logger,\n\tevent: PromiseRejectionEvent\n) => {\n\t// No reason was provided, so we can't log anything.\n\tif (!event?.reason) {\n\t\treturn;\n\t}\n\tconst message = event?.reason.stack ?? event.reason;\n\tloggerInstance.logMessage({\n\t\tmessage,\n\t\tseverity: LogSeverity.Error,\n\t});\n};\n\n/**\n * The number of open Playground tabs.\n */\nlet numberOfOpenPlaygroundTabs = 0;\n\n/**\n * Register a listener for service worker messages and log the data.\n * The service worker will send the number of open Playground tabs.\n *\n * @param loggerInstance The logger instance\n */\nconst addServiceWorkerMessageListener = (loggerInstance: Logger) => {\n\tnavigator.serviceWorker.addEventListener('message', (event) => {\n\t\tif (event.data?.numberOfOpenPlaygroundTabs === undefined) {\n\t\t\treturn;\n\t\t}\n\t\t// Each tab sends an activate event on load. Prevent sending the same\n\t\t// metrics multiple times if a tab is reloaded.\n\t\tif (\n\t\t\tnumberOfOpenPlaygroundTabs ===\n\t\t\tevent.data?.numberOfOpenPlaygroundTabs\n\t\t) {\n\t\t\treturn;\n\t\t}\n\t\tnumberOfOpenPlaygroundTabs = event.data?.numberOfOpenPlaygroundTabs;\n\t\tloggerInstance.debug(\n\t\t\t`Number of open Playground tabs is: ${numberOfOpenPlaygroundTabs}`\n\t\t);\n\t});\n};\n\n// If the window events are already connected.\nlet windowConnected = false;\n\n/**\n * Collect errors from JavaScript window events like error and log them.\n * @param loggerInstance The logger instance\n */\nexport const collectWindowErrors = (loggerInstance: Logger) => {\n\t// Ensure that the window events are connected only once.\n\tif (windowConnected) {\n\t\treturn;\n\t}\n\taddServiceWorkerMessageListener(loggerInstance);\n\tif (typeof window === 'undefined') {\n\t\treturn;\n\t}\n\n\twindow.addEventListener('error', (event) =>\n\t\tlogWindowErrorEvent(loggerInstance, event as ErrorEvent)\n\t);\n\twindow.addEventListener('unhandledrejection', (event) =>\n\t\tlogPromiseRejection(loggerInstance, event as PromiseRejectionEvent)\n\t);\n\twindow.addEventListener('rejectionhandled', (event) =>\n\t\tlogPromiseRejection(loggerInstance, event as PromiseRejectionEvent)\n\t);\n\twindowConnected = true;\n};\n","/// <reference lib=\"WebWorker\" />\n\n/**\n * **Call this inside a service worker.**\n * These errors include Playground errors like Asyncify errors. PHP errors\n * won't trigger this event.\n *\n * Reports service worker metrics.\n * Allows the logger to request metrics from the service worker by sending a\n * message. The service worker will respond with the number of open Playground\n * tabs.\n *\n * @param worker The service worker\n */\nexport const reportServiceWorkerMetrics = (\n\tworker: ServiceWorkerGlobalScope\n) => {\n\tworker.addEventListener('activate', () => {\n\t\tworker.clients.matchAll().then((clients) => {\n\t\t\tconst metrics = {\n\t\t\t\tnumberOfOpenPlaygroundTabs: clients.filter(\n\t\t\t\t\t// Only count top-level frames to get the number of tabs.\n\t\t\t\t\t(c) => c.frameType === 'top-level'\n\t\t\t\t).length,\n\t\t\t};\n\t\t\tfor (const client of clients) {\n\t\t\t\tclient.postMessage(metrics);\n\t\t\t}\n\t\t});\n\t});\n};\n"],"names":["logEventType","logEvent","log","args","logger","logToConsole","prepareLogMessage","LogSeverity","logMessage","logs","addToLogArray","message","logToMemory","formatLogEntry","LogPrefix","clearMemoryLogs","lastPHPLogLength","errorLogPath","getRequestPhpErrorLog","playground","collectPhpLogs","loggerInstance","currentLog","event","Logger","handlers","logWithSeverity","handler","severity","getDefaultHandlers","prefix","date","formattedDate","formattedTime","now","addCrashListener","callback","logWindowErrorEvent","logPromiseRejection","numberOfOpenPlaygroundTabs","addServiceWorkerMessageListener","_a","_b","_c","windowConnected","collectWindowErrors","reportServiceWorkerMetrics","worker","clients","metrics","c","client"],"mappings":"oHAGO,MAAMA,EAAe,iBAEfC,EAAuB,CAACC,KAAaC,IAAsB,CAChEC,EAAA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AAc,iBAAiB,UAAYE,GAAU,aAC1DkB,EAAAlB,EAAM,OAAN,YAAAkB,EAAY,8BAA+B,QAM9CF,MACAG,EAAAnB,EAAM,OAAN,YAAAmB,EAAY,8BAIbH,GAA6BI,EAAApB,EAAM,OAAN,YAAAoB,EAAY,2BAC1BtB,EAAA,MACd,sCAAsCkB,CAA0B,EACjE,EAAA,CACA,CACF,EAGA,IAAIK,EAAkB,GAMT,MAAAC,EAAuBxB,GAA2B,CAE1DuB,IAGJJ,EAAgCnB,CAAc,EAC1C,SAAO,OAAW,OAIf,OAAA,iBAAiB,QAAUE,GACjCc,EAAoBhB,EAAgBE,CAAmB,CACxD,EACO,OAAA,iBAAiB,qBAAuBA,GAC9Ce,EAAoBjB,EAAgBE,CAA8B,CACnE,EACO,OAAA,iBAAiB,mBAAqBA,GAC5Ce,EAAoBjB,EAAgBE,CAA8B,CACnE,EACkBqB,EAAA,IACnB,EChFaE,EACZC,GACI,CACGA,EAAA,iBAAiB,WAAY,IAAM,CACzCA,EAAO,QAAQ,SAAW,EAAA,KAAMC,GAAY,CAC3C,MAAMC,EAAU,CACf,2BAA4BD,EAAQ,OAElCE,GAAMA,EAAE,YAAc,WAAA,EACtB,MACH,EACA,UAAWC,KAAUH,EACpBG,EAAO,YAAYF,CAAO,CAC3B,CACA,CAAA,CACD,CACF"}
|
package/index.js
CHANGED
|
@@ -1,70 +1,71 @@
|
|
|
1
1
|
import "@php-wasm/node-polyfills";
|
|
2
|
-
const
|
|
3
|
-
|
|
4
|
-
new CustomEvent(
|
|
2
|
+
const E = "playground-log", f = (e, ...r) => {
|
|
3
|
+
M.dispatchEvent(
|
|
4
|
+
new CustomEvent(E, {
|
|
5
5
|
detail: {
|
|
6
6
|
log: e,
|
|
7
7
|
args: r
|
|
8
8
|
}
|
|
9
9
|
})
|
|
10
10
|
);
|
|
11
|
-
},
|
|
12
|
-
switch (typeof e.message == "string" ? Reflect.set(e, "message",
|
|
11
|
+
}, w = (e, ...r) => {
|
|
12
|
+
switch (typeof e.message == "string" ? Reflect.set(e, "message", g(e.message)) : e.message.message && typeof e.message.message == "string" && Reflect.set(
|
|
13
13
|
e.message,
|
|
14
14
|
"message",
|
|
15
|
-
|
|
15
|
+
g(e.message.message)
|
|
16
16
|
), e.severity) {
|
|
17
|
-
case
|
|
17
|
+
case t.Debug:
|
|
18
18
|
console.debug(e.message, ...r);
|
|
19
19
|
break;
|
|
20
|
-
case
|
|
20
|
+
case t.Info:
|
|
21
21
|
console.info(e.message, ...r);
|
|
22
22
|
break;
|
|
23
|
-
case
|
|
23
|
+
case t.Warn:
|
|
24
24
|
console.warn(e.message, ...r);
|
|
25
25
|
break;
|
|
26
|
-
case
|
|
26
|
+
case t.Error:
|
|
27
27
|
console.error(e.message, ...r);
|
|
28
28
|
break;
|
|
29
|
-
case
|
|
29
|
+
case t.Fatal:
|
|
30
30
|
console.error(e.message, ...r);
|
|
31
31
|
break;
|
|
32
32
|
default:
|
|
33
33
|
console.log(e.message, ...r);
|
|
34
34
|
}
|
|
35
|
-
},
|
|
36
|
-
`) : JSON.stringify(e, null, 2), d = [],
|
|
35
|
+
}, L = (e) => e instanceof Error ? [e.message, e.stack].join(`
|
|
36
|
+
`) : JSON.stringify(e, null, 2), d = [], m = (e) => {
|
|
37
37
|
d.push(e);
|
|
38
|
-
},
|
|
38
|
+
}, c = (e) => {
|
|
39
39
|
if (e.raw === !0)
|
|
40
|
-
|
|
40
|
+
m(e.message);
|
|
41
41
|
else {
|
|
42
|
-
const r =
|
|
43
|
-
typeof e.message == "object" ?
|
|
44
|
-
e.severity
|
|
45
|
-
e.prefix ??
|
|
42
|
+
const r = S(
|
|
43
|
+
typeof e.message == "object" ? L(e.message) : e.message,
|
|
44
|
+
e.severity,
|
|
45
|
+
e.prefix ?? a.JS
|
|
46
46
|
);
|
|
47
|
-
|
|
47
|
+
m(r);
|
|
48
48
|
}
|
|
49
|
-
},
|
|
49
|
+
}, $ = () => {
|
|
50
50
|
d.length = 0;
|
|
51
51
|
};
|
|
52
|
-
let
|
|
53
|
-
const
|
|
52
|
+
let i = 0;
|
|
53
|
+
const u = "/wordpress/wp-content/debug.log", b = async (e) => await e.fileExists(u) ? await e.readFileAsText(u) : "", k = (e, r) => {
|
|
54
54
|
r.addEventListener("request.end", async () => {
|
|
55
55
|
const s = await b(r);
|
|
56
|
-
if (s.length >
|
|
57
|
-
const
|
|
56
|
+
if (s.length > i) {
|
|
57
|
+
const o = s.substring(i);
|
|
58
58
|
e.logMessage({
|
|
59
|
-
message:
|
|
59
|
+
message: o,
|
|
60
|
+
severity: t.Log,
|
|
60
61
|
raw: !0
|
|
61
|
-
}),
|
|
62
|
+
}), i = s.length;
|
|
62
63
|
}
|
|
63
64
|
}), r.addEventListener("request.error", (s) => {
|
|
64
65
|
s = s, s.error && (e.logMessage({
|
|
65
66
|
message: `${s.error.message} ${s.error.stack}`,
|
|
66
|
-
severity:
|
|
67
|
-
prefix: s.source === "request" ?
|
|
67
|
+
severity: t.Fatal,
|
|
68
|
+
prefix: s.source === "request" ? a.PHP : a.WASM
|
|
68
69
|
}), e.dispatchEvent(
|
|
69
70
|
new CustomEvent(e.fatalErrorEvent, {
|
|
70
71
|
detail: {
|
|
@@ -74,32 +75,52 @@ const f = "/wordpress/wp-content/debug.log", b = async (e) => await e.fileExists
|
|
|
74
75
|
})
|
|
75
76
|
));
|
|
76
77
|
});
|
|
78
|
+
}, t = {
|
|
79
|
+
Fatal: { name: "fatal", level: 0 },
|
|
80
|
+
Error: { name: "error", level: 1 },
|
|
81
|
+
Warn: { name: "warn", level: 2 },
|
|
82
|
+
Log: { name: "log", level: 3 },
|
|
83
|
+
Info: { name: "info", level: 4 },
|
|
84
|
+
Debug: { name: "debug", level: 5 }
|
|
85
|
+
}, a = {
|
|
86
|
+
WASM: "Wasm Crash",
|
|
87
|
+
PHP: "PHP",
|
|
88
|
+
JS: "JavaScript"
|
|
77
89
|
};
|
|
78
|
-
class
|
|
90
|
+
class P extends EventTarget {
|
|
79
91
|
// constructor
|
|
80
92
|
constructor(r = []) {
|
|
81
|
-
super(), this.fatalErrorEvent = "playground-fatal-error", this.handlers = r;
|
|
93
|
+
super(), this.fatalErrorEvent = "playground-fatal-error", this.severity = t.Info, this.handlers = r;
|
|
82
94
|
}
|
|
83
95
|
/**
|
|
84
96
|
* Get all logs.
|
|
85
97
|
* @returns string[]
|
|
86
98
|
*/
|
|
87
99
|
getLogs() {
|
|
88
|
-
return this.handlers.includes(
|
|
100
|
+
return this.handlers.includes(c) ? [...d] : (this.error(`Logs aren't stored because the logToMemory handler isn't registered.
|
|
89
101
|
If you're using a custom logger instance, make sure to register logToMemory handler.
|
|
90
102
|
`), []);
|
|
91
103
|
}
|
|
92
104
|
/**
|
|
93
105
|
* Log message with severity.
|
|
94
106
|
*
|
|
95
|
-
* @param
|
|
96
|
-
* @param severity LogSeverity
|
|
97
|
-
* @param raw boolean
|
|
107
|
+
* @param log Log
|
|
98
108
|
* @param args any
|
|
99
109
|
*/
|
|
100
110
|
logMessage(r, ...s) {
|
|
101
|
-
|
|
102
|
-
|
|
111
|
+
const o = {
|
|
112
|
+
...r,
|
|
113
|
+
severity: r.severity ?? t.Log
|
|
114
|
+
};
|
|
115
|
+
for (const n of this.handlers)
|
|
116
|
+
o.severity.level <= this.severity.level && n(o, ...s);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Filter message based on severity
|
|
120
|
+
* @param severity LogSeverity
|
|
121
|
+
*/
|
|
122
|
+
setSeverityFilterLevel(r) {
|
|
123
|
+
this.severity = r;
|
|
103
124
|
}
|
|
104
125
|
/**
|
|
105
126
|
* Log message
|
|
@@ -111,8 +132,8 @@ class L extends EventTarget {
|
|
|
111
132
|
this.logMessage(
|
|
112
133
|
{
|
|
113
134
|
message: r,
|
|
114
|
-
severity:
|
|
115
|
-
prefix:
|
|
135
|
+
severity: t.Log,
|
|
136
|
+
prefix: a.JS,
|
|
116
137
|
raw: !1
|
|
117
138
|
},
|
|
118
139
|
...s
|
|
@@ -128,8 +149,8 @@ class L extends EventTarget {
|
|
|
128
149
|
this.logMessage(
|
|
129
150
|
{
|
|
130
151
|
message: r,
|
|
131
|
-
severity:
|
|
132
|
-
prefix:
|
|
152
|
+
severity: t.Debug,
|
|
153
|
+
prefix: a.JS,
|
|
133
154
|
raw: !1
|
|
134
155
|
},
|
|
135
156
|
...s
|
|
@@ -145,8 +166,8 @@ class L extends EventTarget {
|
|
|
145
166
|
this.logMessage(
|
|
146
167
|
{
|
|
147
168
|
message: r,
|
|
148
|
-
severity:
|
|
149
|
-
prefix:
|
|
169
|
+
severity: t.Info,
|
|
170
|
+
prefix: a.JS,
|
|
150
171
|
raw: !1
|
|
151
172
|
},
|
|
152
173
|
...s
|
|
@@ -162,8 +183,8 @@ class L extends EventTarget {
|
|
|
162
183
|
this.logMessage(
|
|
163
184
|
{
|
|
164
185
|
message: r,
|
|
165
|
-
severity:
|
|
166
|
-
prefix:
|
|
186
|
+
severity: t.Warn,
|
|
187
|
+
prefix: a.JS,
|
|
167
188
|
raw: !1
|
|
168
189
|
},
|
|
169
190
|
...s
|
|
@@ -179,102 +200,104 @@ class L extends EventTarget {
|
|
|
179
200
|
this.logMessage(
|
|
180
201
|
{
|
|
181
202
|
message: r,
|
|
182
|
-
severity:
|
|
183
|
-
prefix:
|
|
203
|
+
severity: t.Error,
|
|
204
|
+
prefix: a.JS,
|
|
184
205
|
raw: !1
|
|
185
206
|
},
|
|
186
207
|
...s
|
|
187
208
|
);
|
|
188
209
|
}
|
|
189
210
|
}
|
|
190
|
-
const
|
|
211
|
+
const T = () => {
|
|
191
212
|
try {
|
|
192
213
|
if (process.env.NODE_ENV === "test")
|
|
193
|
-
return [
|
|
214
|
+
return [c, f];
|
|
194
215
|
} catch {
|
|
195
216
|
}
|
|
196
|
-
return [
|
|
197
|
-
},
|
|
198
|
-
const
|
|
217
|
+
return [c, w, f];
|
|
218
|
+
}, M = new P(T()), g = (e) => e.replace(/\t/g, ""), S = (e, r, s) => {
|
|
219
|
+
const o = /* @__PURE__ */ new Date(), n = new Intl.DateTimeFormat("en-GB", {
|
|
199
220
|
year: "numeric",
|
|
200
221
|
month: "short",
|
|
201
222
|
day: "2-digit",
|
|
202
223
|
timeZone: "UTC"
|
|
203
|
-
}).format(
|
|
224
|
+
}).format(o).replace(/ /g, "-"), p = new Intl.DateTimeFormat("en-GB", {
|
|
204
225
|
hour: "2-digit",
|
|
205
226
|
minute: "2-digit",
|
|
206
227
|
second: "2-digit",
|
|
207
228
|
hour12: !1,
|
|
208
229
|
timeZone: "UTC",
|
|
209
230
|
timeZoneName: "short"
|
|
210
|
-
}).format(
|
|
211
|
-
return e =
|
|
212
|
-
},
|
|
231
|
+
}).format(o), v = n + " " + p;
|
|
232
|
+
return e = g(e), `[${v}] ${s} ${r.name}: ${e}`;
|
|
233
|
+
}, D = (e, r) => {
|
|
213
234
|
e.addEventListener(e.fatalErrorEvent, r);
|
|
214
|
-
},
|
|
235
|
+
}, x = (e, r) => {
|
|
215
236
|
e.logMessage({
|
|
216
237
|
message: `${r.message} in ${r.filename} on line ${r.lineno}:${r.colno}`,
|
|
217
|
-
severity:
|
|
238
|
+
severity: t.Error
|
|
218
239
|
});
|
|
219
|
-
},
|
|
240
|
+
}, h = (e, r) => {
|
|
220
241
|
if (!(r != null && r.reason))
|
|
221
242
|
return;
|
|
222
243
|
const s = (r == null ? void 0 : r.reason.stack) ?? r.reason;
|
|
223
244
|
e.logMessage({
|
|
224
245
|
message: s,
|
|
225
|
-
severity:
|
|
246
|
+
severity: t.Error
|
|
226
247
|
});
|
|
227
248
|
};
|
|
228
|
-
let
|
|
249
|
+
let l = 0;
|
|
229
250
|
const O = (e) => {
|
|
230
251
|
navigator.serviceWorker.addEventListener("message", (r) => {
|
|
231
|
-
var s,
|
|
232
|
-
((s = r.data) == null ? void 0 : s.numberOfOpenPlaygroundTabs) !== void 0 &&
|
|
233
|
-
`Number of open Playground tabs is: ${
|
|
252
|
+
var s, o, n;
|
|
253
|
+
((s = r.data) == null ? void 0 : s.numberOfOpenPlaygroundTabs) !== void 0 && l !== ((o = r.data) == null ? void 0 : o.numberOfOpenPlaygroundTabs) && (l = (n = r.data) == null ? void 0 : n.numberOfOpenPlaygroundTabs, e.debug(
|
|
254
|
+
`Number of open Playground tabs is: ${l}`
|
|
234
255
|
));
|
|
235
256
|
});
|
|
236
257
|
};
|
|
237
|
-
let
|
|
238
|
-
const
|
|
239
|
-
|
|
258
|
+
let y = !1;
|
|
259
|
+
const J = (e) => {
|
|
260
|
+
y || (O(e), !(typeof window > "u") && (window.addEventListener(
|
|
240
261
|
"error",
|
|
241
|
-
(r) =>
|
|
262
|
+
(r) => x(e, r)
|
|
242
263
|
), window.addEventListener(
|
|
243
264
|
"unhandledrejection",
|
|
244
|
-
(r) =>
|
|
265
|
+
(r) => h(e, r)
|
|
245
266
|
), window.addEventListener(
|
|
246
267
|
"rejectionhandled",
|
|
247
|
-
(r) =>
|
|
248
|
-
),
|
|
249
|
-
},
|
|
268
|
+
(r) => h(e, r)
|
|
269
|
+
), y = !0));
|
|
270
|
+
}, C = (e) => {
|
|
250
271
|
e.addEventListener("activate", () => {
|
|
251
272
|
e.clients.matchAll().then((r) => {
|
|
252
273
|
const s = {
|
|
253
274
|
numberOfOpenPlaygroundTabs: r.filter(
|
|
254
275
|
// Only count top-level frames to get the number of tabs.
|
|
255
|
-
(
|
|
276
|
+
(o) => o.frameType === "top-level"
|
|
256
277
|
).length
|
|
257
278
|
};
|
|
258
|
-
for (const
|
|
259
|
-
|
|
279
|
+
for (const o of r)
|
|
280
|
+
o.postMessage(s);
|
|
260
281
|
});
|
|
261
282
|
});
|
|
262
283
|
};
|
|
263
284
|
export {
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
285
|
+
a as LogPrefix,
|
|
286
|
+
t as LogSeverity,
|
|
287
|
+
P as Logger,
|
|
288
|
+
D as addCrashListener,
|
|
289
|
+
$ as clearMemoryLogs,
|
|
290
|
+
k as collectPhpLogs,
|
|
291
|
+
J as collectWindowErrors,
|
|
292
|
+
u as errorLogPath,
|
|
293
|
+
S as formatLogEntry,
|
|
294
|
+
f as logEvent,
|
|
295
|
+
E as logEventType,
|
|
296
|
+
w as logToConsole,
|
|
297
|
+
c as logToMemory,
|
|
298
|
+
M as logger,
|
|
276
299
|
d as logs,
|
|
277
|
-
|
|
278
|
-
|
|
300
|
+
g as prepareLogMessage,
|
|
301
|
+
C as reportServiceWorkerMetrics
|
|
279
302
|
};
|
|
280
303
|
//# sourceMappingURL=index.js.map
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../../../packages/php-wasm/logger/src/lib/handlers/log-event.ts","../../../../packages/php-wasm/logger/src/lib/handlers/log-to-console.ts","../../../../packages/php-wasm/logger/src/lib/handlers/log-to-memory.ts","../../../../packages/php-wasm/logger/src/lib/collectors/collect-php-logs.ts","../../../../packages/php-wasm/logger/src/lib/logger.ts","../../../../packages/php-wasm/logger/src/lib/collectors/collect-window-errors.ts","../../../../packages/php-wasm/logger/src/lib/collectors/collect-service-worker-metrics.ts"],"sourcesContent":["import type { LogHandler } from '../log-handlers';\nimport type { Log } from '../logger';\nimport { logger } from '../logger';\n\nexport const logEventType = 'playground-log';\n\nexport const logEvent: LogHandler = (log: Log, ...args: any[]): void => {\n\tlogger.dispatchEvent(\n\t\tnew CustomEvent(logEventType, {\n\t\t\tdetail: {\n\t\t\t\tlog,\n\t\t\t\targs,\n\t\t\t},\n\t\t})\n\t);\n};\n","import type { LogHandler } from '../log-handlers';\nimport type { Log } from '../logger';\nimport { prepareLogMessage } from '../logger';\n\n/**\n * Log message to the console.\n */\nexport const logToConsole: LogHandler = (log: Log, ...args: any[]): void => {\n\tif (typeof log.message === 'string') {\n\t\t// Some errors have a read-only message property where direct\n\t\t// assignment will throw an error. The assignment is merely for\n\t\t// formatting, so let's assign with Reflect.set and avoid the error.\n\t\tReflect.set(log, 'message', prepareLogMessage(log.message));\n\t} else if (log.message.message && typeof log.message.message === 'string') {\n\t\t// Some errors have a read-only message property where direct\n\t\t// assignment will throw an error. The assignment is merely for\n\t\t// formatting, so let's assign with Reflect.set and avoid the error.\n\t\tReflect.set(\n\t\t\tlog.message,\n\t\t\t'message',\n\t\t\tprepareLogMessage(log.message.message)\n\t\t);\n\t}\n\t/* eslint-disable no-console */\n\tswitch (log.severity) {\n\t\tcase 'Debug':\n\t\t\tconsole.debug(log.message, ...args);\n\t\t\tbreak;\n\t\tcase 'Info':\n\t\t\tconsole.info(log.message, ...args);\n\t\t\tbreak;\n\t\tcase 'Warn':\n\t\t\tconsole.warn(log.message, ...args);\n\t\t\tbreak;\n\t\tcase 'Error':\n\t\t\tconsole.error(log.message, ...args);\n\t\t\tbreak;\n\t\tcase 'Fatal':\n\t\t\tconsole.error(log.message, ...args);\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tconsole.log(log.message, ...args);\n\t}\n\t/* eslint-enable no-console */\n};\n","import type { LogHandler } from '../log-handlers';\nimport type { Log } from '../logger';\nimport { formatLogEntry } from '../logger';\n\nconst prepareLogMessage = (logMessage: object): string => {\n\tif (logMessage instanceof Error) {\n\t\treturn [logMessage.message, logMessage.stack].join('\\n');\n\t}\n\treturn JSON.stringify(logMessage, null, 2);\n};\n\nexport const logs: string[] = [];\n\nconst addToLogArray = (message: string): void => {\n\tlogs.push(message);\n};\n\n/**\n * Log to memory\n */\nexport const logToMemory: LogHandler = (log: Log): void => {\n\tif (log.raw === true) {\n\t\taddToLogArray(log.message);\n\t} else {\n\t\tconst message = formatLogEntry(\n\t\t\ttypeof log.message === 'object'\n\t\t\t\t? prepareLogMessage(log.message)\n\t\t\t\t: log.message,\n\t\t\tlog.severity ?? 'Info',\n\t\t\tlog.prefix ?? 'JavaScript'\n\t\t);\n\t\taddToLogArray(message);\n\t}\n};\n\nexport const clearMemoryLogs = (): void => {\n\tlogs.length = 0;\n};\n","import type { UniversalPHP, PHPRequestErrorEvent } from '../types';\nimport type { Logger } from '../logger';\n\nlet lastPHPLogLength = 0;\nexport const errorLogPath = '/wordpress/wp-content/debug.log';\n\n/**\n * Read the WordPress debug.log file and return its content.\n *\n * @param UniversalPHP playground instance\n * @returns string The content of the debug.log file\n */\nconst getRequestPhpErrorLog = async (playground: UniversalPHP) => {\n\tif (!(await playground.fileExists(errorLogPath))) {\n\t\treturn '';\n\t}\n\treturn await playground.readFileAsText(errorLogPath);\n};\n\n/**\n * Collect PHP logs from the error_log file and log them.\n * @param UniversalPHP playground instance\n * @param loggerInstance The logger instance\n */\nexport const collectPhpLogs = (\n\tloggerInstance: Logger,\n\tplayground: UniversalPHP\n) => {\n\tplayground.addEventListener('request.end', async () => {\n\t\tconst log = await getRequestPhpErrorLog(playground);\n\t\tif (log.length > lastPHPLogLength) {\n\t\t\tconst currentLog = log.substring(lastPHPLogLength);\n\t\t\tloggerInstance.logMessage({\n\t\t\t\tmessage: currentLog,\n\t\t\t\traw: true,\n\t\t\t});\n\t\t\tlastPHPLogLength = log.length;\n\t\t}\n\t});\n\tplayground.addEventListener('request.error', (event) => {\n\t\tevent = event as PHPRequestErrorEvent;\n\t\tif (event.error) {\n\t\t\tloggerInstance.logMessage({\n\t\t\t\tmessage: `${event.error.message} ${event.error.stack}`,\n\t\t\t\tseverity: 'Fatal',\n\t\t\t\tprefix: event.source === 'request' ? 'PHP' : 'WASM Crash',\n\t\t\t});\n\t\t\tloggerInstance.dispatchEvent(\n\t\t\t\tnew CustomEvent(loggerInstance.fatalErrorEvent, {\n\t\t\t\t\tdetail: {\n\t\t\t\t\t\tlogs: loggerInstance.getLogs(),\n\t\t\t\t\t\tsource: event.source,\n\t\t\t\t\t},\n\t\t\t\t})\n\t\t\t);\n\t\t}\n\t});\n};\n","import { logEvent } from './handlers/log-event';\nimport {\n\tlogToMemory,\n\tlogToConsole,\n\tlogs,\n\ttype LogHandler,\n} from './log-handlers';\n\nexport { logEventType } from './handlers/log-event';\n\nexport { errorLogPath } from './collectors/collect-php-logs';\n\nexport type Log = {\n\tmessage: any;\n\tseverity?: LogSeverity;\n\tprefix?: LogPrefix;\n\traw?: boolean;\n};\n\n/**\n * Log severity levels.\n */\nexport type LogSeverity = 'Debug' | 'Info' | 'Warn' | 'Error' | 'Fatal';\n\n/**\n * Log prefix.\n */\nexport type LogPrefix = 'WASM Crash' | 'PHP' | 'JavaScript';\n\n/**\n * A logger for Playground.\n */\nexport class Logger extends EventTarget {\n\tpublic readonly fatalErrorEvent = 'playground-fatal-error';\n\tprivate readonly handlers: LogHandler[];\n\n\t// constructor\n\tconstructor(\n\t\t// Log handlers\n\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type\n\t\thandlers: LogHandler[] = []\n\t) {\n\t\tsuper();\n\t\tthis.handlers = handlers;\n\t}\n\n\t/**\n\t * Get all logs.\n\t * @returns string[]\n\t */\n\tpublic getLogs(): string[] {\n\t\tif (!this.handlers.includes(logToMemory)) {\n\t\t\tthis\n\t\t\t\t.error(`Logs aren't stored because the logToMemory handler isn't registered.\n\t\t\t\tIf you're using a custom logger instance, make sure to register logToMemory handler.\n\t\t\t`);\n\t\t\treturn [];\n\t\t}\n\t\treturn [...logs];\n\t}\n\n\t/**\n\t * Log message with severity.\n\t *\n\t * @param message any\n\t * @param severity LogSeverity\n\t * @param raw boolean\n\t * @param args any\n\t */\n\tpublic logMessage(log: Log, ...args: any[]): void {\n\t\tfor (const handler of this.handlers) {\n\t\t\thandler(log, ...args);\n\t\t}\n\t}\n\n\t/**\n\t * Log message\n\t *\n\t * @param message any\n\t * @param args any\n\t */\n\tpublic log(message: any, ...args: any[]): void {\n\t\tthis.logMessage(\n\t\t\t{\n\t\t\t\tmessage,\n\t\t\t\tseverity: undefined,\n\t\t\t\tprefix: 'JavaScript',\n\t\t\t\traw: false,\n\t\t\t},\n\t\t\t...args\n\t\t);\n\t}\n\n\t/**\n\t * Log debug message\n\t *\n\t * @param message any\n\t * @param args any\n\t */\n\tpublic debug(message: any, ...args: any[]): void {\n\t\tthis.logMessage(\n\t\t\t{\n\t\t\t\tmessage,\n\t\t\t\tseverity: 'Debug',\n\t\t\t\tprefix: 'JavaScript',\n\t\t\t\traw: false,\n\t\t\t},\n\t\t\t...args\n\t\t);\n\t}\n\n\t/**\n\t * Log info message\n\t *\n\t * @param message any\n\t * @param args any\n\t */\n\tpublic info(message: any, ...args: any[]): void {\n\t\tthis.logMessage(\n\t\t\t{\n\t\t\t\tmessage,\n\t\t\t\tseverity: 'Info',\n\t\t\t\tprefix: 'JavaScript',\n\t\t\t\traw: false,\n\t\t\t},\n\t\t\t...args\n\t\t);\n\t}\n\n\t/**\n\t * Log warning message\n\t *\n\t * @param message any\n\t * @param args any\n\t */\n\tpublic warn(message: any, ...args: any[]): void {\n\t\tthis.logMessage(\n\t\t\t{\n\t\t\t\tmessage,\n\t\t\t\tseverity: 'Warn',\n\t\t\t\tprefix: 'JavaScript',\n\t\t\t\traw: false,\n\t\t\t},\n\t\t\t...args\n\t\t);\n\t}\n\n\t/**\n\t * Log error message\n\t *\n\t * @param message any\n\t * @param args any\n\t */\n\tpublic error(message: any, ...args: any[]): void {\n\t\tthis.logMessage(\n\t\t\t{\n\t\t\t\tmessage,\n\t\t\t\tseverity: 'Error',\n\t\t\t\tprefix: 'JavaScript',\n\t\t\t\traw: false,\n\t\t\t},\n\t\t\t...args\n\t\t);\n\t}\n}\n\nconst getDefaultHandlers = () => {\n\ttry {\n\t\tif (process.env['NODE_ENV'] === 'test') {\n\t\t\treturn [logToMemory, logEvent];\n\t\t}\n\t} catch {\n\t\t// Process.env is not available in the browser\n\t}\n\treturn [logToMemory, logToConsole, logEvent];\n};\n\n/**\n * The logger instance.\n */\nexport const logger: Logger = new Logger(getDefaultHandlers());\n\nexport const prepareLogMessage = (message: string) => {\n\treturn message.replace(/\\t/g, '');\n};\n\nexport const formatLogEntry = (\n\tmessage: string,\n\tseverity: LogSeverity,\n\tprefix: string\n): string => {\n\tconst date = new Date();\n\tconst formattedDate = new Intl.DateTimeFormat('en-GB', {\n\t\tyear: 'numeric',\n\t\tmonth: 'short',\n\t\tday: '2-digit',\n\t\ttimeZone: 'UTC',\n\t})\n\t\t.format(date)\n\t\t.replace(/ /g, '-');\n\n\tconst formattedTime = new Intl.DateTimeFormat('en-GB', {\n\t\thour: '2-digit',\n\t\tminute: '2-digit',\n\t\tsecond: '2-digit',\n\t\thour12: false,\n\t\ttimeZone: 'UTC',\n\t\ttimeZoneName: 'short',\n\t}).format(date);\n\tconst now = formattedDate + ' ' + formattedTime;\n\tmessage = prepareLogMessage(message);\n\treturn `[${now}] ${prefix} ${severity}: ${message}`;\n};\n\n/**\n * Add a listener for the Playground crashes.\n * These crashes include Playground errors like Asyncify errors.\n * The callback function will receive an Event object with logs in the detail\n * property.\n *\n * @param loggerInstance The logger instance\n * @param callback The callback function\n */\nexport const addCrashListener = (\n\tloggerInstance: Logger,\n\tcallback: EventListenerOrEventListenerObject\n) => {\n\tloggerInstance.addEventListener(loggerInstance.fatalErrorEvent, callback);\n};\n","import type { Logger } from '../logger';\n\n/**\n * Log Windows errors.\n *\n * @param loggerInstance The logger instance\n * @param ErrorEvent event\n */\nconst logWindowErrorEvent = (loggerInstance: Logger, event: ErrorEvent) => {\n\tloggerInstance.logMessage({\n\t\tmessage: `${event.message} in ${event.filename} on line ${event.lineno}:${event.colno}`,\n\t\tseverity: 'Error',\n\t});\n};\n\n/**\n * Log promise rejections.\n *\n * @param loggerInstance The logger instance\n * @param PromiseRejectionEvent event\n */\nconst logPromiseRejection = (\n\tloggerInstance: Logger,\n\tevent: PromiseRejectionEvent\n) => {\n\t// No reason was provided, so we can't log anything.\n\tif (!event?.reason) {\n\t\treturn;\n\t}\n\tconst message = event?.reason.stack ?? event.reason;\n\tloggerInstance.logMessage({\n\t\tmessage,\n\t\tseverity: 'Error',\n\t});\n};\n\n/**\n * The number of open Playground tabs.\n */\nlet numberOfOpenPlaygroundTabs = 0;\n\n/**\n * Register a listener for service worker messages and log the data.\n * The service worker will send the number of open Playground tabs.\n *\n * @param loggerInstance The logger instance\n */\nconst addServiceWorkerMessageListener = (loggerInstance: Logger) => {\n\tnavigator.serviceWorker.addEventListener('message', (event) => {\n\t\tif (event.data?.numberOfOpenPlaygroundTabs === undefined) {\n\t\t\treturn;\n\t\t}\n\t\t// Each tab sends an activate event on load. Prevent sending the same\n\t\t// metrics multiple times if a tab is reloaded.\n\t\tif (\n\t\t\tnumberOfOpenPlaygroundTabs ===\n\t\t\tevent.data?.numberOfOpenPlaygroundTabs\n\t\t) {\n\t\t\treturn;\n\t\t}\n\t\tnumberOfOpenPlaygroundTabs = event.data?.numberOfOpenPlaygroundTabs;\n\t\tloggerInstance.debug(\n\t\t\t`Number of open Playground tabs is: ${numberOfOpenPlaygroundTabs}`\n\t\t);\n\t});\n};\n\n// If the window events are already connected.\nlet windowConnected = false;\n\n/**\n * Collect errors from JavaScript window events like error and log them.\n * @param loggerInstance The logger instance\n */\nexport const collectWindowErrors = (loggerInstance: Logger) => {\n\t// Ensure that the window events are connected only once.\n\tif (windowConnected) {\n\t\treturn;\n\t}\n\taddServiceWorkerMessageListener(loggerInstance);\n\tif (typeof window === 'undefined') {\n\t\treturn;\n\t}\n\n\twindow.addEventListener('error', (event) =>\n\t\tlogWindowErrorEvent(loggerInstance, event as ErrorEvent)\n\t);\n\twindow.addEventListener('unhandledrejection', (event) =>\n\t\tlogPromiseRejection(loggerInstance, event as PromiseRejectionEvent)\n\t);\n\twindow.addEventListener('rejectionhandled', (event) =>\n\t\tlogPromiseRejection(loggerInstance, event as PromiseRejectionEvent)\n\t);\n\twindowConnected = true;\n};\n","/// <reference lib=\"WebWorker\" />\n\n/**\n * **Call this inside a service worker.**\n * These errors include Playground errors like Asyncify errors. PHP errors\n * won't trigger this event.\n *\n * Reports service worker metrics.\n * Allows the logger to request metrics from the service worker by sending a\n * message. The service worker will respond with the number of open Playground\n * tabs.\n *\n * @param worker The service worker\n */\nexport const reportServiceWorkerMetrics = (\n\tworker: ServiceWorkerGlobalScope\n) => {\n\tworker.addEventListener('activate', () => {\n\t\tworker.clients.matchAll().then((clients) => {\n\t\t\tconst metrics = {\n\t\t\t\tnumberOfOpenPlaygroundTabs: clients.filter(\n\t\t\t\t\t// Only count top-level frames to get the number of tabs.\n\t\t\t\t\t(c) => c.frameType === 'top-level'\n\t\t\t\t).length,\n\t\t\t};\n\t\t\tfor (const client of clients) {\n\t\t\t\tclient.postMessage(metrics);\n\t\t\t}\n\t\t});\n\t});\n};\n"],"names":["logEventType","logEvent","log","args","logger","logToConsole","prepareLogMessage","logMessage","logs","addToLogArray","message","logToMemory","formatLogEntry","clearMemoryLogs","lastPHPLogLength","errorLogPath","getRequestPhpErrorLog","playground","collectPhpLogs","loggerInstance","currentLog","event","Logger","handlers","handler","getDefaultHandlers","severity","prefix","date","formattedDate","formattedTime","now","addCrashListener","callback","logWindowErrorEvent","logPromiseRejection","numberOfOpenPlaygroundTabs","addServiceWorkerMessageListener","_a","_b","_c","windowConnected","collectWindowErrors","reportServiceWorkerMetrics","worker","clients","metrics","c","client"],"mappings":";AAIO,MAAMA,IAAe,kBAEfC,IAAuB,CAACC,MAAaC,MAAsB;AAChE,EAAAC,EAAA;AAAA,IACN,IAAI,YAAYJ,GAAc;AAAA,MAC7B,QAAQ;AAAA,QACP,KAAAE;AAAA,QACA,MAAAC;AAAA,MAAA;AAAA,IAED,CAAA;AAAA,EACF;AACD,GCRaE,IAA2B,CAACH,MAAaC,MAAsB;AAiB3E,UAhBI,OAAOD,EAAI,WAAY,WAI1B,QAAQ,IAAIA,GAAK,WAAWI,EAAkBJ,EAAI,OAAO,CAAC,IAChDA,EAAI,QAAQ,WAAW,OAAOA,EAAI,QAAQ,WAAY,YAIxD,QAAA;AAAA,IACPA,EAAI;AAAA,IACJ;AAAA,IACAI,EAAkBJ,EAAI,QAAQ,OAAO;AAAA,EACtC,GAGOA,EAAI,UAAU;AAAA,IACrB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ACAG,IAAAlB,EAAM,SAAN,gBAAAkB,EAAY,gCAIbH,KAA6BI,IAAAnB,EAAM,SAAN,gBAAAmB,EAAY,4BAC1BrB,EAAA;AAAA,MACd,sCAAsCiB,CAA0B;AAAA,IACjE;AAAA,EAAA,CACA;AACF;AAGA,IAAIK,IAAkB;AAMT,MAAAC,IAAsB,CAACvB,MAA2B;AAE9D,EAAIsB,MAGJJ,EAAgClB,CAAc,GAC1C,SAAO,SAAW,SAIf,OAAA;AAAA,IAAiB;AAAA,IAAS,CAACE,MACjCa,EAAoBf,GAAgBE,CAAmB;AAAA,EACxD,GACO,OAAA;AAAA,IAAiB;AAAA,IAAsB,CAACA,MAC9Cc,EAAoBhB,GAAgBE,CAA8B;AAAA,EACnE,GACO,OAAA;AAAA,IAAiB;AAAA,IAAoB,CAACA,MAC5Cc,EAAoBhB,GAAgBE,CAA8B;AAAA,EACnE,GACkBoB,IAAA;AACnB,GChFaE,IAA6B,CACzCC,MACI;AACG,EAAAA,EAAA,iBAAiB,YAAY,MAAM;AACzC,IAAAA,EAAO,QAAQ,SAAW,EAAA,KAAK,CAACC,MAAY;AAC3C,YAAMC,IAAU;AAAA,QACf,4BAA4BD,EAAQ;AAAA;AAAA,UAEnC,CAACE,MAAMA,EAAE,cAAc;AAAA,QAAA,EACtB;AAAA,MACH;AACA,iBAAWC,KAAUH;AACpB,QAAAG,EAAO,YAAYF,CAAO;AAAA,IAC3B,CACA;AAAA,EAAA,CACD;AACF;"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../../../packages/php-wasm/logger/src/lib/handlers/log-event.ts","../../../../packages/php-wasm/logger/src/lib/handlers/log-to-console.ts","../../../../packages/php-wasm/logger/src/lib/handlers/log-to-memory.ts","../../../../packages/php-wasm/logger/src/lib/collectors/collect-php-logs.ts","../../../../packages/php-wasm/logger/src/lib/logger.ts","../../../../packages/php-wasm/logger/src/lib/collectors/collect-window-errors.ts","../../../../packages/php-wasm/logger/src/lib/collectors/collect-service-worker-metrics.ts"],"sourcesContent":["import type { LogHandler } from '../log-handlers';\nimport { type Log, logger } from '../logger';\n\nexport const logEventType = 'playground-log';\n\nexport const logEvent: LogHandler = (log: Log, ...args: any[]): void => {\n\tlogger.dispatchEvent(\n\t\tnew CustomEvent(logEventType, {\n\t\t\tdetail: {\n\t\t\t\tlog,\n\t\t\t\targs,\n\t\t\t},\n\t\t})\n\t);\n};\n","import type { LogHandler } from '../log-handlers';\nimport { type Log, LogSeverity, prepareLogMessage } from '../logger';\n\n/**\n * Log message to the console.\n */\nexport const logToConsole: LogHandler = (log: Log, ...args: any[]): void => {\n\tif (typeof log.message === 'string') {\n\t\t// Some errors have a read-only message property where direct\n\t\t// assignment will throw an error. The assignment is merely for\n\t\t// formatting, so let's assign with Reflect.set and avoid the error.\n\t\tReflect.set(log, 'message', prepareLogMessage(log.message));\n\t} else if (log.message.message && typeof log.message.message === 'string') {\n\t\t// Some errors have a read-only message property where direct\n\t\t// assignment will throw an error. The assignment is merely for\n\t\t// formatting, so let's assign with Reflect.set and avoid the error.\n\t\tReflect.set(\n\t\t\tlog.message,\n\t\t\t'message',\n\t\t\tprepareLogMessage(log.message.message)\n\t\t);\n\t}\n\t/* eslint-disable no-console */\n\tswitch (log.severity) {\n\t\tcase LogSeverity.Debug:\n\t\t\tconsole.debug(log.message, ...args);\n\t\t\tbreak;\n\t\tcase LogSeverity.Info:\n\t\t\tconsole.info(log.message, ...args);\n\t\t\tbreak;\n\t\tcase LogSeverity.Warn:\n\t\t\tconsole.warn(log.message, ...args);\n\t\t\tbreak;\n\t\tcase LogSeverity.Error:\n\t\t\tconsole.error(log.message, ...args);\n\t\t\tbreak;\n\t\tcase LogSeverity.Fatal:\n\t\t\tconsole.error(log.message, ...args);\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tconsole.log(log.message, ...args);\n\t}\n\t/* eslint-enable no-console */\n};\n","import type { LogHandler } from '../log-handlers';\nimport { formatLogEntry, type Log, LogPrefix } from '../logger';\n\nconst prepareLogMessage = (logMessage: object): string => {\n\tif (logMessage instanceof Error) {\n\t\treturn [logMessage.message, logMessage.stack].join('\\n');\n\t}\n\treturn JSON.stringify(logMessage, null, 2);\n};\n\nexport const logs: string[] = [];\n\nconst addToLogArray = (message: string): void => {\n\tlogs.push(message);\n};\n\n/**\n * Log to memory\n */\nexport const logToMemory: LogHandler = (log: Log): void => {\n\tif (log.raw === true) {\n\t\taddToLogArray(log.message);\n\t} else {\n\t\tconst message = formatLogEntry(\n\t\t\ttypeof log.message === 'object'\n\t\t\t\t? prepareLogMessage(log.message)\n\t\t\t\t: log.message,\n\t\t\tlog.severity,\n\t\t\tlog.prefix ?? LogPrefix.JS\n\t\t);\n\t\taddToLogArray(message);\n\t}\n};\n\nexport const clearMemoryLogs = (): void => {\n\tlogs.length = 0;\n};\n","import type { UniversalPHP, PHPRequestErrorEvent } from '../types';\nimport { type Logger, LogPrefix, LogSeverity } from '../logger';\n\nlet lastPHPLogLength = 0;\nexport const errorLogPath = '/wordpress/wp-content/debug.log';\n\n/**\n * Read the WordPress debug.log file and return its content.\n *\n * @param UniversalPHP playground instance\n * @returns string The content of the debug.log file\n */\nconst getRequestPhpErrorLog = async (playground: UniversalPHP) => {\n\tif (!(await playground.fileExists(errorLogPath))) {\n\t\treturn '';\n\t}\n\treturn await playground.readFileAsText(errorLogPath);\n};\n\n/**\n * Collect PHP logs from the error_log file and log them.\n * @param UniversalPHP playground instance\n * @param loggerInstance The logger instance\n */\nexport const collectPhpLogs = (\n\tloggerInstance: Logger,\n\tplayground: UniversalPHP\n) => {\n\tplayground.addEventListener('request.end', async () => {\n\t\tconst log = await getRequestPhpErrorLog(playground);\n\t\tif (log.length > lastPHPLogLength) {\n\t\t\tconst currentLog = log.substring(lastPHPLogLength);\n\t\t\tloggerInstance.logMessage({\n\t\t\t\tmessage: currentLog,\n\t\t\t\tseverity: LogSeverity.Log,\n\t\t\t\traw: true,\n\t\t\t});\n\t\t\tlastPHPLogLength = log.length;\n\t\t}\n\t});\n\tplayground.addEventListener('request.error', (event) => {\n\t\tevent = event as PHPRequestErrorEvent;\n\t\tif (event.error) {\n\t\t\tloggerInstance.logMessage({\n\t\t\t\tmessage: `${event.error.message} ${event.error.stack}`,\n\t\t\t\tseverity: LogSeverity.Fatal,\n\t\t\t\tprefix:\n\t\t\t\t\tevent.source === 'request' ? LogPrefix.PHP : LogPrefix.WASM,\n\t\t\t});\n\t\t\tloggerInstance.dispatchEvent(\n\t\t\t\tnew CustomEvent(loggerInstance.fatalErrorEvent, {\n\t\t\t\t\tdetail: {\n\t\t\t\t\t\tlogs: loggerInstance.getLogs(),\n\t\t\t\t\t\tsource: event.source,\n\t\t\t\t\t},\n\t\t\t\t})\n\t\t\t);\n\t\t}\n\t});\n};\n","import { logEvent } from './handlers/log-event';\nimport {\n\tlogToMemory,\n\tlogToConsole,\n\tlogs,\n\ttype LogHandler,\n} from './log-handlers';\n\nexport { logEventType } from './handlers/log-event';\n\nexport { errorLogPath } from './collectors/collect-php-logs';\n\nexport type Log = {\n\tmessage: any;\n\tseverity: LogSeverity;\n\tprefix?: LogPrefix;\n\traw?: boolean;\n};\n\n/**\n * Log severity levels.\n */\nexport const LogSeverity = {\n\tFatal: { name: 'fatal', level: 0 },\n\tError: { name: 'error', level: 1 },\n\tWarn: { name: 'warn', level: 2 },\n\tLog: { name: 'log', level: 3 },\n\tInfo: { name: 'info', level: 4 },\n\tDebug: { name: 'debug', level: 5 },\n} as const;\n\nexport type LogSeverity = (typeof LogSeverity)[keyof typeof LogSeverity];\n\n/**\n * Log prefix.\n */\nexport const LogPrefix = {\n\tWASM: 'Wasm Crash',\n\tPHP: 'PHP',\n\tJS: 'JavaScript',\n} as const;\n\nexport type LogPrefix = (typeof LogPrefix)[keyof typeof LogPrefix];\n\n/**\n * A logger for Playground.\n */\nexport class Logger extends EventTarget {\n\tpublic readonly fatalErrorEvent = 'playground-fatal-error';\n\tprivate readonly handlers: LogHandler[];\n\tprivate severity: LogSeverity = LogSeverity.Info;\n\n\t// constructor\n\tconstructor(\n\t\t// Log handlers\n\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type\n\t\thandlers: LogHandler[] = []\n\t) {\n\t\tsuper();\n\t\tthis.handlers = handlers;\n\t}\n\n\t/**\n\t * Get all logs.\n\t * @returns string[]\n\t */\n\tpublic getLogs(): string[] {\n\t\tif (!this.handlers.includes(logToMemory)) {\n\t\t\tthis\n\t\t\t\t.error(`Logs aren't stored because the logToMemory handler isn't registered.\n\t\t\t\tIf you're using a custom logger instance, make sure to register logToMemory handler.\n\t\t\t`);\n\t\t\treturn [];\n\t\t}\n\t\treturn [...logs];\n\t}\n\n\t/**\n\t * Log message with severity.\n\t *\n\t * @param log Log\n\t * @param args any\n\t */\n\tpublic logMessage(\n\t\tlog: Omit<Log, 'severity'> & { severity?: LogSeverity },\n\t\t...args: any[]\n\t): void {\n\t\tconst logWithSeverity: Log = {\n\t\t\t...log,\n\t\t\tseverity: log.severity ?? LogSeverity.Log,\n\t\t};\n\t\tfor (const handler of this.handlers) {\n\t\t\tif (logWithSeverity.severity.level <= this.severity.level) {\n\t\t\t\thandler(logWithSeverity, ...args);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Filter message based on severity\n\t * @param severity LogSeverity\n\t */\n\tpublic setSeverityFilterLevel(severity: LogSeverity): void {\n\t\tthis.severity = severity;\n\t}\n\n\t/**\n\t * Log message\n\t *\n\t * @param message any\n\t * @param args any\n\t */\n\tpublic log(message: any, ...args: any[]): void {\n\t\tthis.logMessage(\n\t\t\t{\n\t\t\t\tmessage,\n\t\t\t\tseverity: LogSeverity.Log,\n\t\t\t\tprefix: LogPrefix.JS,\n\t\t\t\traw: false,\n\t\t\t},\n\t\t\t...args\n\t\t);\n\t}\n\n\t/**\n\t * Log debug message\n\t *\n\t * @param message any\n\t * @param args any\n\t */\n\tpublic debug(message: any, ...args: any[]): void {\n\t\tthis.logMessage(\n\t\t\t{\n\t\t\t\tmessage,\n\t\t\t\tseverity: LogSeverity.Debug,\n\t\t\t\tprefix: LogPrefix.JS,\n\t\t\t\traw: false,\n\t\t\t},\n\t\t\t...args\n\t\t);\n\t}\n\n\t/**\n\t * Log info message\n\t *\n\t * @param message any\n\t * @param args any\n\t */\n\tpublic info(message: any, ...args: any[]): void {\n\t\tthis.logMessage(\n\t\t\t{\n\t\t\t\tmessage,\n\t\t\t\tseverity: LogSeverity.Info,\n\t\t\t\tprefix: LogPrefix.JS,\n\t\t\t\traw: false,\n\t\t\t},\n\t\t\t...args\n\t\t);\n\t}\n\n\t/**\n\t * Log warning message\n\t *\n\t * @param message any\n\t * @param args any\n\t */\n\tpublic warn(message: any, ...args: any[]): void {\n\t\tthis.logMessage(\n\t\t\t{\n\t\t\t\tmessage,\n\t\t\t\tseverity: LogSeverity.Warn,\n\t\t\t\tprefix: LogPrefix.JS,\n\t\t\t\traw: false,\n\t\t\t},\n\t\t\t...args\n\t\t);\n\t}\n\n\t/**\n\t * Log error message\n\t *\n\t * @param message any\n\t * @param args any\n\t */\n\tpublic error(message: any, ...args: any[]): void {\n\t\tthis.logMessage(\n\t\t\t{\n\t\t\t\tmessage,\n\t\t\t\tseverity: LogSeverity.Error,\n\t\t\t\tprefix: LogPrefix.JS,\n\t\t\t\traw: false,\n\t\t\t},\n\t\t\t...args\n\t\t);\n\t}\n}\n\nconst getDefaultHandlers = () => {\n\ttry {\n\t\tif (process.env['NODE_ENV'] === 'test') {\n\t\t\treturn [logToMemory, logEvent];\n\t\t}\n\t} catch {\n\t\t// Process.env is not available in the browser\n\t}\n\treturn [logToMemory, logToConsole, logEvent];\n};\n\n/**\n * The logger instance.\n */\nexport const logger: Logger = new Logger(getDefaultHandlers());\n\nexport const prepareLogMessage = (message: string) => {\n\treturn message.replace(/\\t/g, '');\n};\n\nexport const formatLogEntry = (\n\tmessage: string,\n\tseverity: LogSeverity,\n\tprefix: string\n): string => {\n\tconst date = new Date();\n\tconst formattedDate = new Intl.DateTimeFormat('en-GB', {\n\t\tyear: 'numeric',\n\t\tmonth: 'short',\n\t\tday: '2-digit',\n\t\ttimeZone: 'UTC',\n\t})\n\t\t.format(date)\n\t\t.replace(/ /g, '-');\n\n\tconst formattedTime = new Intl.DateTimeFormat('en-GB', {\n\t\thour: '2-digit',\n\t\tminute: '2-digit',\n\t\tsecond: '2-digit',\n\t\thour12: false,\n\t\ttimeZone: 'UTC',\n\t\ttimeZoneName: 'short',\n\t}).format(date);\n\tconst now = formattedDate + ' ' + formattedTime;\n\tmessage = prepareLogMessage(message);\n\treturn `[${now}] ${prefix} ${severity.name}: ${message}`;\n};\n\n/**\n * Add a listener for the Playground crashes.\n * These crashes include Playground errors like Asyncify errors.\n * The callback function will receive an Event object with logs in the detail\n * property.\n *\n * @param loggerInstance The logger instance\n * @param callback The callback function\n */\nexport const addCrashListener = (\n\tloggerInstance: Logger,\n\tcallback: EventListenerOrEventListenerObject\n) => {\n\tloggerInstance.addEventListener(loggerInstance.fatalErrorEvent, callback);\n};\n","import { type Logger, LogSeverity } from '../logger';\n\n/**\n * Log Windows errors.\n *\n * @param loggerInstance The logger instance\n * @param ErrorEvent event\n */\nconst logWindowErrorEvent = (loggerInstance: Logger, event: ErrorEvent) => {\n\tloggerInstance.logMessage({\n\t\tmessage: `${event.message} in ${event.filename} on line ${event.lineno}:${event.colno}`,\n\t\tseverity: LogSeverity.Error,\n\t});\n};\n\n/**\n * Log promise rejections.\n *\n * @param loggerInstance The logger instance\n * @param PromiseRejectionEvent event\n */\nconst logPromiseRejection = (\n\tloggerInstance: Logger,\n\tevent: PromiseRejectionEvent\n) => {\n\t// No reason was provided, so we can't log anything.\n\tif (!event?.reason) {\n\t\treturn;\n\t}\n\tconst message = event?.reason.stack ?? event.reason;\n\tloggerInstance.logMessage({\n\t\tmessage,\n\t\tseverity: LogSeverity.Error,\n\t});\n};\n\n/**\n * The number of open Playground tabs.\n */\nlet numberOfOpenPlaygroundTabs = 0;\n\n/**\n * Register a listener for service worker messages and log the data.\n * The service worker will send the number of open Playground tabs.\n *\n * @param loggerInstance The logger instance\n */\nconst addServiceWorkerMessageListener = (loggerInstance: Logger) => {\n\tnavigator.serviceWorker.addEventListener('message', (event) => {\n\t\tif (event.data?.numberOfOpenPlaygroundTabs === undefined) {\n\t\t\treturn;\n\t\t}\n\t\t// Each tab sends an activate event on load. Prevent sending the same\n\t\t// metrics multiple times if a tab is reloaded.\n\t\tif (\n\t\t\tnumberOfOpenPlaygroundTabs ===\n\t\t\tevent.data?.numberOfOpenPlaygroundTabs\n\t\t) {\n\t\t\treturn;\n\t\t}\n\t\tnumberOfOpenPlaygroundTabs = event.data?.numberOfOpenPlaygroundTabs;\n\t\tloggerInstance.debug(\n\t\t\t`Number of open Playground tabs is: ${numberOfOpenPlaygroundTabs}`\n\t\t);\n\t});\n};\n\n// If the window events are already connected.\nlet windowConnected = false;\n\n/**\n * Collect errors from JavaScript window events like error and log them.\n * @param loggerInstance The logger instance\n */\nexport const collectWindowErrors = (loggerInstance: Logger) => {\n\t// Ensure that the window events are connected only once.\n\tif (windowConnected) {\n\t\treturn;\n\t}\n\taddServiceWorkerMessageListener(loggerInstance);\n\tif (typeof window === 'undefined') {\n\t\treturn;\n\t}\n\n\twindow.addEventListener('error', (event) =>\n\t\tlogWindowErrorEvent(loggerInstance, event as ErrorEvent)\n\t);\n\twindow.addEventListener('unhandledrejection', (event) =>\n\t\tlogPromiseRejection(loggerInstance, event as PromiseRejectionEvent)\n\t);\n\twindow.addEventListener('rejectionhandled', (event) =>\n\t\tlogPromiseRejection(loggerInstance, event as PromiseRejectionEvent)\n\t);\n\twindowConnected = true;\n};\n","/// <reference lib=\"WebWorker\" />\n\n/**\n * **Call this inside a service worker.**\n * These errors include Playground errors like Asyncify errors. PHP errors\n * won't trigger this event.\n *\n * Reports service worker metrics.\n * Allows the logger to request metrics from the service worker by sending a\n * message. The service worker will respond with the number of open Playground\n * tabs.\n *\n * @param worker The service worker\n */\nexport const reportServiceWorkerMetrics = (\n\tworker: ServiceWorkerGlobalScope\n) => {\n\tworker.addEventListener('activate', () => {\n\t\tworker.clients.matchAll().then((clients) => {\n\t\t\tconst metrics = {\n\t\t\t\tnumberOfOpenPlaygroundTabs: clients.filter(\n\t\t\t\t\t// Only count top-level frames to get the number of tabs.\n\t\t\t\t\t(c) => c.frameType === 'top-level'\n\t\t\t\t).length,\n\t\t\t};\n\t\t\tfor (const client of clients) {\n\t\t\t\tclient.postMessage(metrics);\n\t\t\t}\n\t\t});\n\t});\n};\n"],"names":["logEventType","logEvent","log","args","logger","logToConsole","prepareLogMessage","LogSeverity","logMessage","logs","addToLogArray","message","logToMemory","formatLogEntry","LogPrefix","clearMemoryLogs","lastPHPLogLength","errorLogPath","getRequestPhpErrorLog","playground","collectPhpLogs","loggerInstance","currentLog","event","Logger","handlers","logWithSeverity","handler","severity","getDefaultHandlers","prefix","date","formattedDate","formattedTime","now","addCrashListener","callback","logWindowErrorEvent","logPromiseRejection","numberOfOpenPlaygroundTabs","addServiceWorkerMessageListener","_a","_b","_c","windowConnected","collectWindowErrors","reportServiceWorkerMetrics","worker","clients","metrics","c","client"],"mappings":";AAGO,MAAMA,IAAe,kBAEfC,IAAuB,CAACC,MAAaC,MAAsB;AAChE,EAAAC,EAAA;AAAA,IACN,IAAI,YAAYJ,GAAc;AAAA,MAC7B,QAAQ;AAAA,QACP,KAAAE;AAAA,QACA,MAAAC;AAAA,MAAA;AAAA,IAED,CAAA;AAAA,EACF;AACD,GCRaE,IAA2B,CAACH,MAAaC,MAAsB;AAiB3E,UAhBI,OAAOD,EAAI,WAAY,WAI1B,QAAQ,IAAIA,GAAK,WAAWI,EAAkBJ,EAAI,OAAO,CAAC,IAChDA,EAAI,QAAQ,WAAW,OAAOA,EAAI,QAAQ,WAAY,YAIxD,QAAA;AAAA,IACPA,EAAI;AAAA,IACJ;AAAA,IACAI,EAAkBJ,EAAI,QAAQ,OAAO;AAAA,EACtC,GAGOA,EAAI,UAAU;AAAA,IACrB,KAAKK,EAAY;AAChB,cAAQ,MAAML,EAAI,SAAS,GAAGC,CAAI;AA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O,OAAA;AAAA,IAAiB;AAAA,IAAoB,CAACA,MAC5Ce,EAAoBjB,GAAgBE,CAA8B;AAAA,EACnE,GACkBqB,IAAA;AACnB,GChFaE,IAA6B,CACzCC,MACI;AACG,EAAAA,EAAA,iBAAiB,YAAY,MAAM;AACzC,IAAAA,EAAO,QAAQ,SAAW,EAAA,KAAK,CAACC,MAAY;AAC3C,YAAMC,IAAU;AAAA,QACf,4BAA4BD,EAAQ;AAAA;AAAA,UAEnC,CAACE,MAAMA,EAAE,cAAc;AAAA,QAAA,EACtB;AAAA,MACH;AACA,iBAAWC,KAAUH;AACpB,QAAAG,EAAO,YAAYF,CAAO;AAAA,IAC3B,CACA;AAAA,EAAA,CACD;AACF;"}
|
package/lib/logger.d.ts
CHANGED
|
@@ -3,24 +3,56 @@ export { logEventType } from './handlers/log-event';
|
|
|
3
3
|
export { errorLogPath } from './collectors/collect-php-logs';
|
|
4
4
|
export type Log = {
|
|
5
5
|
message: any;
|
|
6
|
-
severity
|
|
6
|
+
severity: LogSeverity;
|
|
7
7
|
prefix?: LogPrefix;
|
|
8
8
|
raw?: boolean;
|
|
9
9
|
};
|
|
10
10
|
/**
|
|
11
11
|
* Log severity levels.
|
|
12
12
|
*/
|
|
13
|
-
export
|
|
13
|
+
export declare const LogSeverity: {
|
|
14
|
+
readonly Fatal: {
|
|
15
|
+
readonly name: "fatal";
|
|
16
|
+
readonly level: 0;
|
|
17
|
+
};
|
|
18
|
+
readonly Error: {
|
|
19
|
+
readonly name: "error";
|
|
20
|
+
readonly level: 1;
|
|
21
|
+
};
|
|
22
|
+
readonly Warn: {
|
|
23
|
+
readonly name: "warn";
|
|
24
|
+
readonly level: 2;
|
|
25
|
+
};
|
|
26
|
+
readonly Log: {
|
|
27
|
+
readonly name: "log";
|
|
28
|
+
readonly level: 3;
|
|
29
|
+
};
|
|
30
|
+
readonly Info: {
|
|
31
|
+
readonly name: "info";
|
|
32
|
+
readonly level: 4;
|
|
33
|
+
};
|
|
34
|
+
readonly Debug: {
|
|
35
|
+
readonly name: "debug";
|
|
36
|
+
readonly level: 5;
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
export type LogSeverity = (typeof LogSeverity)[keyof typeof LogSeverity];
|
|
14
40
|
/**
|
|
15
41
|
* Log prefix.
|
|
16
42
|
*/
|
|
17
|
-
export
|
|
43
|
+
export declare const LogPrefix: {
|
|
44
|
+
readonly WASM: "Wasm Crash";
|
|
45
|
+
readonly PHP: "PHP";
|
|
46
|
+
readonly JS: "JavaScript";
|
|
47
|
+
};
|
|
48
|
+
export type LogPrefix = (typeof LogPrefix)[keyof typeof LogPrefix];
|
|
18
49
|
/**
|
|
19
50
|
* A logger for Playground.
|
|
20
51
|
*/
|
|
21
52
|
export declare class Logger extends EventTarget {
|
|
22
53
|
readonly fatalErrorEvent = "playground-fatal-error";
|
|
23
54
|
private readonly handlers;
|
|
55
|
+
private severity;
|
|
24
56
|
constructor(handlers?: LogHandler[]);
|
|
25
57
|
/**
|
|
26
58
|
* Get all logs.
|
|
@@ -30,12 +62,17 @@ export declare class Logger extends EventTarget {
|
|
|
30
62
|
/**
|
|
31
63
|
* Log message with severity.
|
|
32
64
|
*
|
|
33
|
-
* @param
|
|
34
|
-
* @param severity LogSeverity
|
|
35
|
-
* @param raw boolean
|
|
65
|
+
* @param log Log
|
|
36
66
|
* @param args any
|
|
37
67
|
*/
|
|
38
|
-
logMessage(log: Log,
|
|
68
|
+
logMessage(log: Omit<Log, 'severity'> & {
|
|
69
|
+
severity?: LogSeverity;
|
|
70
|
+
}, ...args: any[]): void;
|
|
71
|
+
/**
|
|
72
|
+
* Filter message based on severity
|
|
73
|
+
* @param severity LogSeverity
|
|
74
|
+
*/
|
|
75
|
+
setSeverityFilterLevel(severity: LogSeverity): void;
|
|
39
76
|
/**
|
|
40
77
|
* Log message
|
|
41
78
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@php-wasm/logger",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.13",
|
|
4
4
|
"description": "A logger for PHP-wasm clients like Playground and WP-now.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"node": ">=20.18.3",
|
|
28
28
|
"npm": ">=10.1.0"
|
|
29
29
|
},
|
|
30
|
-
"gitHead": "
|
|
30
|
+
"gitHead": "c9d2979597806e5f14985a0607c9c61e48d54c9b",
|
|
31
31
|
"overrides": {
|
|
32
32
|
"rollup": "^4.34.6",
|
|
33
33
|
"react": "18.3.1",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"ws": "^8.18.0"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@php-wasm/node-polyfills": "2.0.
|
|
39
|
+
"@php-wasm/node-polyfills": "2.0.13"
|
|
40
40
|
},
|
|
41
41
|
"optionalDependencies": {
|
|
42
42
|
"fs-ext": "2.1.1"
|