@php-wasm/logger 0.6.8 → 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.
Files changed (3) hide show
  1. package/index.js +1 -1
  2. package/index.mjs +72 -68
  3. 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(e){return new Promise(function(r,n){e.onload=e.onerror=function(s){e.onload=e.onerror=null,s.type==="load"?r(e.result):n(new Error("Failed to read the blob/file"))}})},t=function(){const e=new Uint8Array([1,2,3,4]),n=new File([e],"test").stream();try{return n.getReader({mode:"byob"}),!0}catch{return!1}};if(typeof File>"u"){class e 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=e}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"||!t())&&(Blob.prototype.stream=function(){let e=0;const r=this;return new ReadableStream({type:"bytes",autoAllocateChunkSize:512*1024,async pull(n){const s=n.byobRequest.view,i=await r.slice(e,e+s.byteLength).arrayBuffer(),d=new Uint8Array(i);new Uint8Array(s.buffer).set(d);const l=d.byteLength;n.byobRequest.respond(l),e+=l,e>=r.size&&n.close()}})})}if(u==="NODE"&&typeof CustomEvent>"u"){class o extends Event{constructor(e,r={}){super(e,r),this.detail=r.detail}initCustomEvent(){}}globalThis.CustomEvent=o}class f extends EventTarget{constructor(t){super(),this.fatalErrorEvent="playground-fatal-error",this.logs=[],this.windowConnected=!1,this.lastPHPLogLength=0,this.errorLogPath="/wordpress/wp-content/debug.log",t&&(this.errorLogPath=t)}async getRequestPhpErrorLog(t){return await t.fileExists(this.errorLogPath)?await t.readFileAsText(this.errorLogPath):""}logWindowError(t){this.log(`${t.message} in ${t.filename} on line ${t.lineno}:${t.colno}`,"Error")}logUnhandledRejection(t){this.log(`${t.reason.stack}`,"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(t){t.addEventListener("request.end",async()=>{const e=await this.getRequestPhpErrorLog(t);e.length>this.lastPHPLogLength&&(this.logRaw(e.substring(this.lastPHPLogLength)),this.lastPHPLogLength=e.length)}),t.addEventListener("request.error",e=>{e=e,e.error&&(this.log(`${e.error.message} ${e.error.stack}`,"Fatal","PHP-WASM"),this.dispatchEvent(new CustomEvent(this.fatalErrorEvent,{detail:{logs:this.getLogs()}})))})}formatLogDate(t){const e=new Intl.DateTimeFormat("en-GB",{year:"numeric",month:"short",day:"2-digit",timeZone:"UTC"}).format(t).replace(/ /g,"-"),r=new Intl.DateTimeFormat("en-GB",{hour:"2-digit",minute:"2-digit",second:"2-digit",hour12:!1,timeZone:"UTC",timeZoneName:"short"}).format(t);return e+" "+r}formatMessage(t,e,r){return`[${this.formatLogDate(new Date)}] ${r} ${e}: ${t}`}log(t,e,r){e===void 0&&(e="Info");const n=this.formatMessage(t,e,r??"Playground");this.logRaw(n)}logRaw(t){this.logs.push(t),console.debug(t)}getLogs(){return this.logs}}const c=new f;function g(o){o.addWindowErrorListener()}function h(o,t){o.addPlaygroundRequestEndListener(t)}function w(o,t){o.addEventListener(o.fatalErrorEvent,t)}exports.Logger=f;exports.addFatalErrorListener=w;exports.collectPhpLogs=h;exports.collectWindowErrors=g;exports.logger=c;
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 f = function() {
2
- var n;
3
- return typeof process < "u" && ((n = process.release) == null ? void 0 : n.name) === "node" ? "NODE" : typeof window < "u" ? "WEB" : (
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 (f === "NODE") {
10
- let n = function(e) {
11
- return new Promise(function(r, o) {
12
- e.onload = e.onerror = function(s) {
13
- e.onload = e.onerror = null, s.type === "load" ? r(e.result) : o(new Error("Failed to read the blob/file"));
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
- }, t = function() {
17
- const e = new Uint8Array([1, 2, 3, 4]), o = new File([e], "test").stream();
16
+ }, e = function() {
17
+ const t = new Uint8Array([1, 2, 3, 4]), n = new File([t], "test").stream();
18
18
  try {
19
- return o.getReader({ mode: "byob" }), !0;
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 e extends Blob {
26
- constructor(o, s, a) {
27
- super(o);
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 = e;
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), n(r);
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), n(r);
40
- }), (typeof Blob.prototype.stream > "u" || !t()) && (Blob.prototype.stream = function() {
41
- let e = 0;
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(o) {
49
- const s = o.byobRequest.view, i = await r.slice(
50
- e,
51
- e + s.byteLength
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
- o.byobRequest.respond(l), e += l, e >= r.size && o.close();
55
+ n.byobRequest.respond(l), t += l, t >= r.size && n.close();
56
56
  }
57
57
  });
58
58
  });
59
59
  }
60
- if (f === "NODE" && typeof CustomEvent > "u") {
61
- class n extends Event {
62
- constructor(e, r = {}) {
63
- super(e, r), this.detail = r.detail;
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 = n;
68
+ globalThis.CustomEvent = o;
69
69
  }
70
- class u extends EventTarget {
71
- constructor(t) {
72
- super(), this.fatalErrorEvent = "playground-fatal-error", this.logs = [], this.windowConnected = !1, this.lastPHPLogLength = 0, this.errorLogPath = "/wordpress/wp-content/debug.log", t && (this.errorLogPath = t);
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(t) {
81
- return await t.fileExists(this.errorLogPath) ? await t.readFileAsText(this.errorLogPath) : "";
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(t) {
88
+ logWindowError(e) {
89
89
  this.log(
90
- `${t.message} in ${t.filename} on line ${t.lineno}:${t.colno}`,
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(t) {
100
- this.log(`${t.reason.stack}`, "Error");
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(t) {
119
- t.addEventListener("request.end", async () => {
120
- const e = await this.getRequestPhpErrorLog(t);
121
- e.length > this.lastPHPLogLength && (this.logRaw(e.substring(this.lastPHPLogLength)), this.lastPHPLogLength = e.length);
122
- }), t.addEventListener("request.error", (e) => {
123
- e = e, e.error && (this.log(
124
- `${e.error.message} ${e.error.stack}`,
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(t) {
143
- const e = new Intl.DateTimeFormat("en-GB", {
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(t).replace(/ /g, "-"), r = new Intl.DateTimeFormat("en-GB", {
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(t);
156
- return e + " " + r;
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(t, e, r) {
165
- return `[${this.formatLogDate(/* @__PURE__ */ new Date())}] ${r} ${e}: ${t}`;
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(t, e, r) {
174
- e === void 0 && (e = "Info");
175
- const o = this.formatMessage(
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(o);
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(t) {
187
- this.logs.push(t), console.debug(t);
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 u();
198
- function h(n) {
199
- n.addWindowErrorListener();
201
+ const c = new f();
202
+ function h(o) {
203
+ o.addWindowErrorListener();
200
204
  }
201
- function g(n, t) {
202
- n.addPlaygroundRequestEndListener(t);
205
+ function g(o, e) {
206
+ o.addPlaygroundRequestEndListener(e);
203
207
  }
204
- function w(n, t) {
205
- n.addEventListener(n.fatalErrorEvent, t);
208
+ function w(o, e) {
209
+ o.addEventListener(o.fatalErrorEvent, e);
206
210
  }
207
211
  export {
208
- u as Logger,
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.8",
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": "cb7b1af260489022238495a2fc0d96f767a1294a"
28
+ "gitHead": "f6b98ad6d05617ee8cb1234c21318fdde17a6e56"
29
29
  }