@php-wasm/logger 0.6.7 → 0.6.9
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.d.ts +1 -1
- package/index.js +1 -1
- package/index.mjs +136 -34
- package/{logger.d.ts → lib/logger.d.ts} +30 -6
- package/package.json +2 -2
package/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from './logger';
|
|
1
|
+
export * from './lib/logger';
|
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class i{constructor(e){this.
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=function(){var o;return typeof process<"u"&&((o=process.release)==null?void 0:o.name)==="node"?"NODE":typeof window<"u"?"WEB":typeof WorkerGlobalScope<"u"&&self instanceof WorkerGlobalScope?"WORKER":"NODE"}();if(u==="NODE"){let o=function(t){return new Promise(function(r,n){t.onload=t.onerror=function(s){t.onload=t.onerror=null,s.type==="load"?r(t.result):n(new Error("Failed to read the blob/file"))}})},e=function(){const t=new Uint8Array([1,2,3,4]),n=new File([t],"test").stream();try{return n.getReader({mode:"byob"}),!0}catch{return!1}};if(typeof File>"u"){class t extends Blob{constructor(n,s,a){super(n);let i;a!=null&&a.lastModified&&(i=new Date),(!i||isNaN(i.getFullYear()))&&(i=new Date),this.lastModifiedDate=i,this.lastModified=i.getMilliseconds(),this.name=s||""}}global.File=t}typeof Blob.prototype.arrayBuffer>"u"&&(Blob.prototype.arrayBuffer=function(){const r=new FileReader;return r.readAsArrayBuffer(this),o(r)}),typeof Blob.prototype.text>"u"&&(Blob.prototype.text=function(){const r=new FileReader;return r.readAsText(this),o(r)}),(typeof Blob.prototype.stream>"u"||!e())&&(Blob.prototype.stream=function(){let t=0;const r=this;return new ReadableStream({type:"bytes",autoAllocateChunkSize:512*1024,async pull(n){const s=n.byobRequest.view,i=await r.slice(t,t+s.byteLength).arrayBuffer(),d=new Uint8Array(i);new Uint8Array(s.buffer).set(d);const l=d.byteLength;n.byobRequest.respond(l),t+=l,t>=r.size&&n.close()}})})}if(u==="NODE"&&typeof CustomEvent>"u"){class o extends Event{constructor(t,r={}){super(t,r),this.detail=r.detail}initCustomEvent(){}}globalThis.CustomEvent=o}class f extends EventTarget{constructor(e){super(),this.fatalErrorEvent="playground-fatal-error",this.logs=[],this.windowConnected=!1,this.lastPHPLogLength=0,this.errorLogPath="/wordpress/wp-content/debug.log",e&&(this.errorLogPath=e)}async getRequestPhpErrorLog(e){return await e.fileExists(this.errorLogPath)?await e.readFileAsText(this.errorLogPath):""}logWindowError(e){this.log(`${e.message} in ${e.filename} on line ${e.lineno}:${e.colno}`,"Error")}logUnhandledRejection(e){if(!(e!=null&&e.reason))return;const t=(e==null?void 0:e.reason.stack)??e.reason;this.log(t,"Error")}addWindowErrorListener(){this.windowConnected||typeof window>"u"||(window.addEventListener("error",this.logWindowError.bind(this)),window.addEventListener("unhandledrejection",this.logUnhandledRejection.bind(this)),window.addEventListener("rejectionhandled",this.logUnhandledRejection.bind(this)),this.windowConnected=!0)}addPlaygroundRequestEndListener(e){e.addEventListener("request.end",async()=>{const t=await this.getRequestPhpErrorLog(e);t.length>this.lastPHPLogLength&&(this.logRaw(t.substring(this.lastPHPLogLength)),this.lastPHPLogLength=t.length)}),e.addEventListener("request.error",t=>{t=t,t.error&&(this.log(`${t.error.message} ${t.error.stack}`,"Fatal","PHP-WASM"),this.dispatchEvent(new CustomEvent(this.fatalErrorEvent,{detail:{logs:this.getLogs(),source:t.source}})))})}formatLogDate(e){const t=new Intl.DateTimeFormat("en-GB",{year:"numeric",month:"short",day:"2-digit",timeZone:"UTC"}).format(e).replace(/ /g,"-"),r=new Intl.DateTimeFormat("en-GB",{hour:"2-digit",minute:"2-digit",second:"2-digit",hour12:!1,timeZone:"UTC",timeZoneName:"short"}).format(e);return t+" "+r}formatMessage(e,t,r){return`[${this.formatLogDate(new Date)}] ${r} ${t}: ${e}`}log(e,t,r){t===void 0&&(t="Info");const n=this.formatMessage(e,t,r??"Playground");this.logRaw(n)}logRaw(e){this.logs.push(e),console.debug(e)}getLogs(){return this.logs}}const c=new f;function g(o){o.addWindowErrorListener()}function h(o,e){o.addPlaygroundRequestEndListener(e)}function w(o,e){o.addEventListener(o.fatalErrorEvent,e)}exports.Logger=f;exports.addFatalErrorListener=w;exports.collectPhpLogs=h;exports.collectWindowErrors=g;exports.logger=c;
|
package/index.mjs
CHANGED
|
@@ -1,6 +1,75 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
const u = function() {
|
|
2
|
+
var o;
|
|
3
|
+
return typeof process < "u" && ((o = process.release) == null ? void 0 : o.name) === "node" ? "NODE" : typeof window < "u" ? "WEB" : (
|
|
4
|
+
// @ts-ignore
|
|
5
|
+
typeof WorkerGlobalScope < "u" && // @ts-ignore
|
|
6
|
+
self instanceof WorkerGlobalScope ? "WORKER" : "NODE"
|
|
7
|
+
);
|
|
8
|
+
}();
|
|
9
|
+
if (u === "NODE") {
|
|
10
|
+
let o = function(t) {
|
|
11
|
+
return new Promise(function(r, n) {
|
|
12
|
+
t.onload = t.onerror = function(s) {
|
|
13
|
+
t.onload = t.onerror = null, s.type === "load" ? r(t.result) : n(new Error("Failed to read the blob/file"));
|
|
14
|
+
};
|
|
15
|
+
});
|
|
16
|
+
}, e = function() {
|
|
17
|
+
const t = new Uint8Array([1, 2, 3, 4]), n = new File([t], "test").stream();
|
|
18
|
+
try {
|
|
19
|
+
return n.getReader({ mode: "byob" }), !0;
|
|
20
|
+
} catch {
|
|
21
|
+
return !1;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
if (typeof File > "u") {
|
|
25
|
+
class t extends Blob {
|
|
26
|
+
constructor(n, s, a) {
|
|
27
|
+
super(n);
|
|
28
|
+
let i;
|
|
29
|
+
a != null && a.lastModified && (i = /* @__PURE__ */ new Date()), (!i || isNaN(i.getFullYear())) && (i = /* @__PURE__ */ new Date()), this.lastModifiedDate = i, this.lastModified = i.getMilliseconds(), this.name = s || "";
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
global.File = t;
|
|
33
|
+
}
|
|
34
|
+
typeof Blob.prototype.arrayBuffer > "u" && (Blob.prototype.arrayBuffer = function() {
|
|
35
|
+
const r = new FileReader();
|
|
36
|
+
return r.readAsArrayBuffer(this), o(r);
|
|
37
|
+
}), typeof Blob.prototype.text > "u" && (Blob.prototype.text = function() {
|
|
38
|
+
const r = new FileReader();
|
|
39
|
+
return r.readAsText(this), o(r);
|
|
40
|
+
}), (typeof Blob.prototype.stream > "u" || !e()) && (Blob.prototype.stream = function() {
|
|
41
|
+
let t = 0;
|
|
42
|
+
const r = this;
|
|
43
|
+
return new ReadableStream({
|
|
44
|
+
type: "bytes",
|
|
45
|
+
// 0.5 MB seems like a reasonable chunk size, let's adjust
|
|
46
|
+
// this if needed.
|
|
47
|
+
autoAllocateChunkSize: 512 * 1024,
|
|
48
|
+
async pull(n) {
|
|
49
|
+
const s = n.byobRequest.view, i = await r.slice(
|
|
50
|
+
t,
|
|
51
|
+
t + s.byteLength
|
|
52
|
+
).arrayBuffer(), d = new Uint8Array(i);
|
|
53
|
+
new Uint8Array(s.buffer).set(d);
|
|
54
|
+
const l = d.byteLength;
|
|
55
|
+
n.byobRequest.respond(l), t += l, t >= r.size && n.close();
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
if (u === "NODE" && typeof CustomEvent > "u") {
|
|
61
|
+
class o extends Event {
|
|
62
|
+
constructor(t, r = {}) {
|
|
63
|
+
super(t, r), this.detail = r.detail;
|
|
64
|
+
}
|
|
65
|
+
initCustomEvent() {
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
globalThis.CustomEvent = o;
|
|
69
|
+
}
|
|
70
|
+
class f extends EventTarget {
|
|
71
|
+
constructor(e) {
|
|
72
|
+
super(), this.fatalErrorEvent = "playground-fatal-error", this.logs = [], this.windowConnected = !1, this.lastPHPLogLength = 0, this.errorLogPath = "/wordpress/wp-content/debug.log", e && (this.errorLogPath = e);
|
|
4
73
|
}
|
|
5
74
|
/**
|
|
6
75
|
* Read the WordPress debug.log file and return its content.
|
|
@@ -8,18 +77,18 @@ class i {
|
|
|
8
77
|
* @param UniversalPHP playground instance
|
|
9
78
|
* @returns string The content of the debug.log file
|
|
10
79
|
*/
|
|
11
|
-
async getRequestPhpErrorLog(
|
|
12
|
-
return await
|
|
80
|
+
async getRequestPhpErrorLog(e) {
|
|
81
|
+
return await e.fileExists(this.errorLogPath) ? await e.readFileAsText(this.errorLogPath) : "";
|
|
13
82
|
}
|
|
14
83
|
/**
|
|
15
84
|
* Log Windows errors.
|
|
16
85
|
*
|
|
17
86
|
* @param ErrorEvent event
|
|
18
87
|
*/
|
|
19
|
-
logWindowError(
|
|
88
|
+
logWindowError(e) {
|
|
20
89
|
this.log(
|
|
21
|
-
`${
|
|
22
|
-
"
|
|
90
|
+
`${e.message} in ${e.filename} on line ${e.lineno}:${e.colno}`,
|
|
91
|
+
"Error"
|
|
23
92
|
);
|
|
24
93
|
}
|
|
25
94
|
/**
|
|
@@ -27,8 +96,11 @@ class i {
|
|
|
27
96
|
*
|
|
28
97
|
* @param PromiseRejectionEvent event
|
|
29
98
|
*/
|
|
30
|
-
logUnhandledRejection(
|
|
31
|
-
|
|
99
|
+
logUnhandledRejection(e) {
|
|
100
|
+
if (!(e != null && e.reason))
|
|
101
|
+
return;
|
|
102
|
+
const t = (e == null ? void 0 : e.reason.stack) ?? e.reason;
|
|
103
|
+
this.log(t, "Error");
|
|
32
104
|
}
|
|
33
105
|
/**
|
|
34
106
|
* Register a listener for the window error events and log the data.
|
|
@@ -46,10 +118,23 @@ class i {
|
|
|
46
118
|
* Register a listener for the request.end event and log the data.
|
|
47
119
|
* @param UniversalPHP playground instance
|
|
48
120
|
*/
|
|
49
|
-
addPlaygroundRequestEndListener(
|
|
50
|
-
|
|
51
|
-
const
|
|
52
|
-
|
|
121
|
+
addPlaygroundRequestEndListener(e) {
|
|
122
|
+
e.addEventListener("request.end", async () => {
|
|
123
|
+
const t = await this.getRequestPhpErrorLog(e);
|
|
124
|
+
t.length > this.lastPHPLogLength && (this.logRaw(t.substring(this.lastPHPLogLength)), this.lastPHPLogLength = t.length);
|
|
125
|
+
}), e.addEventListener("request.error", (t) => {
|
|
126
|
+
t = t, t.error && (this.log(
|
|
127
|
+
`${t.error.message} ${t.error.stack}`,
|
|
128
|
+
"Fatal",
|
|
129
|
+
"PHP-WASM"
|
|
130
|
+
), this.dispatchEvent(
|
|
131
|
+
new CustomEvent(this.fatalErrorEvent, {
|
|
132
|
+
detail: {
|
|
133
|
+
logs: this.getLogs(),
|
|
134
|
+
source: t.source
|
|
135
|
+
}
|
|
136
|
+
})
|
|
137
|
+
));
|
|
53
138
|
});
|
|
54
139
|
}
|
|
55
140
|
/**
|
|
@@ -58,58 +143,75 @@ class i {
|
|
|
58
143
|
* @param date
|
|
59
144
|
* @returns string
|
|
60
145
|
*/
|
|
61
|
-
formatLogDate(
|
|
62
|
-
const
|
|
146
|
+
formatLogDate(e) {
|
|
147
|
+
const t = new Intl.DateTimeFormat("en-GB", {
|
|
63
148
|
year: "numeric",
|
|
64
149
|
month: "short",
|
|
65
150
|
day: "2-digit",
|
|
66
151
|
timeZone: "UTC"
|
|
67
|
-
}).format(
|
|
152
|
+
}).format(e).replace(/ /g, "-"), r = new Intl.DateTimeFormat("en-GB", {
|
|
68
153
|
hour: "2-digit",
|
|
69
154
|
minute: "2-digit",
|
|
70
155
|
second: "2-digit",
|
|
71
156
|
hour12: !1,
|
|
72
157
|
timeZone: "UTC",
|
|
73
158
|
timeZoneName: "short"
|
|
74
|
-
}).format(
|
|
75
|
-
return
|
|
159
|
+
}).format(e);
|
|
160
|
+
return t + " " + r;
|
|
76
161
|
}
|
|
77
162
|
/**
|
|
78
163
|
* Format log message and severity and log it.
|
|
79
164
|
* @param string message
|
|
80
165
|
* @param LogSeverity severity
|
|
166
|
+
* @param string prefix
|
|
81
167
|
*/
|
|
82
|
-
formatMessage(t,
|
|
83
|
-
return `[${this.formatLogDate(/* @__PURE__ */ new Date())}] ${
|
|
168
|
+
formatMessage(e, t, r) {
|
|
169
|
+
return `[${this.formatLogDate(/* @__PURE__ */ new Date())}] ${r} ${t}: ${e}`;
|
|
84
170
|
}
|
|
85
171
|
/**
|
|
86
172
|
* Log message with severity and timestamp.
|
|
87
173
|
* @param string message
|
|
88
174
|
* @param LogSeverity severity
|
|
175
|
+
* @param string prefix
|
|
89
176
|
*/
|
|
90
|
-
log(t,
|
|
91
|
-
|
|
92
|
-
const n = this.formatMessage(
|
|
177
|
+
log(e, t, r) {
|
|
178
|
+
t === void 0 && (t = "Info");
|
|
179
|
+
const n = this.formatMessage(
|
|
180
|
+
e,
|
|
181
|
+
t,
|
|
182
|
+
r ?? "Playground"
|
|
183
|
+
);
|
|
93
184
|
this.logRaw(n);
|
|
94
185
|
}
|
|
95
186
|
/**
|
|
96
187
|
* Log message without severity and timestamp.
|
|
97
188
|
* @param string log
|
|
98
189
|
*/
|
|
99
|
-
logRaw(
|
|
100
|
-
console.debug(
|
|
190
|
+
logRaw(e) {
|
|
191
|
+
this.logs.push(e), console.debug(e);
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Get all logs.
|
|
195
|
+
* @returns string[]
|
|
196
|
+
*/
|
|
197
|
+
getLogs() {
|
|
198
|
+
return this.logs;
|
|
101
199
|
}
|
|
102
200
|
}
|
|
103
|
-
const
|
|
104
|
-
function
|
|
201
|
+
const c = new f();
|
|
202
|
+
function h(o) {
|
|
105
203
|
o.addWindowErrorListener();
|
|
106
204
|
}
|
|
107
|
-
function
|
|
108
|
-
o.addPlaygroundRequestEndListener(
|
|
205
|
+
function g(o, e) {
|
|
206
|
+
o.addPlaygroundRequestEndListener(e);
|
|
207
|
+
}
|
|
208
|
+
function w(o, e) {
|
|
209
|
+
o.addEventListener(o.fatalErrorEvent, e);
|
|
109
210
|
}
|
|
110
211
|
export {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
212
|
+
f as Logger,
|
|
213
|
+
w as addFatalErrorListener,
|
|
214
|
+
g as collectPhpLogs,
|
|
215
|
+
h as collectWindowErrors,
|
|
216
|
+
c as logger
|
|
115
217
|
};
|
|
@@ -1,13 +1,21 @@
|
|
|
1
|
-
import { UniversalPHP } from '
|
|
1
|
+
import { UniversalPHP } from '../../../universal/src/index.ts/src/lib/universal-php';
|
|
2
2
|
/**
|
|
3
3
|
* Log severity levels.
|
|
4
4
|
*/
|
|
5
|
-
export type LogSeverity = '
|
|
5
|
+
export type LogSeverity = 'Debug' | 'Info' | 'Warn' | 'Error' | 'Fatal';
|
|
6
|
+
/**
|
|
7
|
+
* Log prefix.
|
|
8
|
+
*/
|
|
9
|
+
export type LogPrefix = 'Playground' | 'PHP-WASM';
|
|
6
10
|
/**
|
|
7
11
|
* A logger for Playground.
|
|
8
12
|
*/
|
|
9
|
-
export declare class Logger {
|
|
10
|
-
|
|
13
|
+
export declare class Logger extends EventTarget {
|
|
14
|
+
readonly fatalErrorEvent = "playground-fatal-error";
|
|
15
|
+
/**
|
|
16
|
+
* Log messages
|
|
17
|
+
*/
|
|
18
|
+
private logs;
|
|
11
19
|
/**
|
|
12
20
|
* Whether the window events are connected.
|
|
13
21
|
*/
|
|
@@ -60,19 +68,26 @@ export declare class Logger {
|
|
|
60
68
|
* Format log message and severity and log it.
|
|
61
69
|
* @param string message
|
|
62
70
|
* @param LogSeverity severity
|
|
71
|
+
* @param string prefix
|
|
63
72
|
*/
|
|
64
|
-
formatMessage(message: string, severity: LogSeverity): string;
|
|
73
|
+
formatMessage(message: string, severity: LogSeverity, prefix: string): string;
|
|
65
74
|
/**
|
|
66
75
|
* Log message with severity and timestamp.
|
|
67
76
|
* @param string message
|
|
68
77
|
* @param LogSeverity severity
|
|
78
|
+
* @param string prefix
|
|
69
79
|
*/
|
|
70
|
-
log(message: string, severity?: LogSeverity): void;
|
|
80
|
+
log(message: string, severity?: LogSeverity, prefix?: LogPrefix): void;
|
|
71
81
|
/**
|
|
72
82
|
* Log message without severity and timestamp.
|
|
73
83
|
* @param string log
|
|
74
84
|
*/
|
|
75
85
|
logRaw(log: string): void;
|
|
86
|
+
/**
|
|
87
|
+
* Get all logs.
|
|
88
|
+
* @returns string[]
|
|
89
|
+
*/
|
|
90
|
+
getLogs(): string[];
|
|
76
91
|
}
|
|
77
92
|
/**
|
|
78
93
|
* The logger instance.
|
|
@@ -89,3 +104,12 @@ export declare function collectWindowErrors(loggerInstance: Logger): void;
|
|
|
89
104
|
* @param loggerInstance The logger instance
|
|
90
105
|
*/
|
|
91
106
|
export declare function collectPhpLogs(loggerInstance: Logger, playground: UniversalPHP): void;
|
|
107
|
+
/**
|
|
108
|
+
* Add a listener for the fatal Playground errors.
|
|
109
|
+
* These errors include Playground errors like Asyncify errors. PHP errors won't trigger this event.
|
|
110
|
+
* The callback function will receive an Event object with logs in the detail property.
|
|
111
|
+
*
|
|
112
|
+
* @param loggerInstance The logger instance
|
|
113
|
+
* @param callback The callback function
|
|
114
|
+
*/
|
|
115
|
+
export declare function addFatalErrorListener(loggerInstance: Logger, callback: EventListenerOrEventListenerObject): void;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@php-wasm/logger",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.9",
|
|
4
4
|
"description": "A logger for PHP-wasm clients like Playground and WP-now.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -25,5 +25,5 @@
|
|
|
25
25
|
"node": ">=18.18.2",
|
|
26
26
|
"npm": ">=8.11.0"
|
|
27
27
|
},
|
|
28
|
-
"gitHead": "
|
|
28
|
+
"gitHead": "f6b98ad6d05617ee8cb1234c21318fdde17a6e56"
|
|
29
29
|
}
|