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