@wp-playground/client 0.1.6 → 0.1.7

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 (2) hide show
  1. package/index.js +499 -214
  2. package/package.json +2 -6
package/index.js CHANGED
@@ -1,15 +1,257 @@
1
- import * as m from "comlink";
2
- import { cloneResponseMonitorProgress as U } from "@php-wasm/progress";
3
1
  (function() {
4
2
  return typeof window < "u" && !{}.TEST ? "WEB" : typeof WorkerGlobalScope < "u" && self instanceof WorkerGlobalScope ? "WORKER" : "NODE";
5
3
  })();
6
- function S(e) {
7
- A();
8
- const o = e instanceof Worker ? e : m.windowEndpoint(e);
9
- return m.wrap(o);
4
+ /**
5
+ * @license
6
+ * Copyright 2019 Google LLC
7
+ * SPDX-License-Identifier: Apache-2.0
8
+ */
9
+ const M = Symbol("Comlink.proxy"), Z = Symbol("Comlink.endpoint"), G = Symbol("Comlink.releaseProxy"), C = Symbol("Comlink.finalizer"), T = Symbol("Comlink.thrown"), _ = (e) => typeof e == "object" && e !== null || typeof e == "function", K = {
10
+ canHandle: (e) => _(e) && e[M],
11
+ serialize(e) {
12
+ const { port1: t, port2: r } = new MessageChannel();
13
+ return z(e, t), [r, [r]];
14
+ },
15
+ deserialize(e) {
16
+ return e.start(), q(e);
17
+ }
18
+ }, X = {
19
+ canHandle: (e) => _(e) && T in e,
20
+ serialize({ value: e }) {
21
+ let t;
22
+ return e instanceof Error ? t = {
23
+ isError: !0,
24
+ value: {
25
+ message: e.message,
26
+ name: e.name,
27
+ stack: e.stack
28
+ }
29
+ } : t = { isError: !1, value: e }, [t, []];
30
+ },
31
+ deserialize(e) {
32
+ throw e.isError ? Object.assign(new Error(e.value.message), e.value) : e.value;
33
+ }
34
+ }, F = /* @__PURE__ */ new Map([
35
+ ["proxy", K],
36
+ ["throw", X]
37
+ ]);
38
+ function Y(e, t) {
39
+ for (const r of e)
40
+ if (t === r || r === "*" || r instanceof RegExp && r.test(t))
41
+ return !0;
42
+ return !1;
43
+ }
44
+ function z(e, t = globalThis, r = ["*"]) {
45
+ t.addEventListener("message", function l(s) {
46
+ if (!s || !s.data)
47
+ return;
48
+ if (!Y(r, s.origin)) {
49
+ console.warn(`Invalid origin '${s.origin}' for comlink proxy`);
50
+ return;
51
+ }
52
+ const { id: u, type: i, path: p } = Object.assign({ path: [] }, s.data), c = (s.data.argumentList || []).map(w);
53
+ let n;
54
+ try {
55
+ const o = p.slice(0, -1).reduce((a, f) => a[f], e), d = p.reduce((a, f) => a[f], e);
56
+ switch (i) {
57
+ case "GET":
58
+ n = d;
59
+ break;
60
+ case "SET":
61
+ o[p.slice(-1)[0]] = w(s.data.value), n = !0;
62
+ break;
63
+ case "APPLY":
64
+ n = d.apply(o, c);
65
+ break;
66
+ case "CONSTRUCT":
67
+ {
68
+ const a = new d(...c);
69
+ n = re(a);
70
+ }
71
+ break;
72
+ case "ENDPOINT":
73
+ {
74
+ const { port1: a, port2: f } = new MessageChannel();
75
+ z(e, f), n = ne(a, [a]);
76
+ }
77
+ break;
78
+ case "RELEASE":
79
+ n = void 0;
80
+ break;
81
+ default:
82
+ return;
83
+ }
84
+ } catch (o) {
85
+ n = { value: o, [T]: 0 };
86
+ }
87
+ Promise.resolve(n).catch((o) => ({ value: o, [T]: 0 })).then((o) => {
88
+ const [d, a] = A(o);
89
+ t.postMessage(Object.assign(Object.assign({}, d), { id: u }), a), i === "RELEASE" && (t.removeEventListener("message", l), j(t), C in e && typeof e[C] == "function" && e[C]());
90
+ }).catch((o) => {
91
+ const [d, a] = A({
92
+ value: new TypeError("Unserializable return value"),
93
+ [T]: 0
94
+ });
95
+ t.postMessage(Object.assign(Object.assign({}, d), { id: u }), a);
96
+ });
97
+ }), t.start && t.start();
98
+ }
99
+ function Q(e) {
100
+ return e.constructor.name === "MessagePort";
101
+ }
102
+ function j(e) {
103
+ Q(e) && e.close();
104
+ }
105
+ function q(e, t) {
106
+ return O(e, [], t);
107
+ }
108
+ function P(e) {
109
+ if (e)
110
+ throw new Error("Proxy has been released and is not useable");
111
+ }
112
+ function H(e) {
113
+ return h(e, {
114
+ type: "RELEASE"
115
+ }).then(() => {
116
+ j(e);
117
+ });
118
+ }
119
+ const R = /* @__PURE__ */ new WeakMap(), S = "FinalizationRegistry" in globalThis && new FinalizationRegistry((e) => {
120
+ const t = (R.get(e) || 0) - 1;
121
+ R.set(e, t), t === 0 && H(e);
122
+ });
123
+ function J(e, t) {
124
+ const r = (R.get(t) || 0) + 1;
125
+ R.set(t, r), S && S.register(e, t, e);
126
+ }
127
+ function ee(e) {
128
+ S && S.unregister(e);
129
+ }
130
+ function O(e, t = [], r = function() {
131
+ }) {
132
+ let l = !1;
133
+ const s = new Proxy(r, {
134
+ get(u, i) {
135
+ if (P(l), i === G)
136
+ return () => {
137
+ ee(s), H(e), l = !0;
138
+ };
139
+ if (i === "then") {
140
+ if (t.length === 0)
141
+ return { then: () => s };
142
+ const p = h(e, {
143
+ type: "GET",
144
+ path: t.map((c) => c.toString())
145
+ }).then(w);
146
+ return p.then.bind(p);
147
+ }
148
+ return O(e, [...t, i]);
149
+ },
150
+ set(u, i, p) {
151
+ P(l);
152
+ const [c, n] = A(p);
153
+ return h(e, {
154
+ type: "SET",
155
+ path: [...t, i].map((o) => o.toString()),
156
+ value: c
157
+ }, n).then(w);
158
+ },
159
+ apply(u, i, p) {
160
+ P(l);
161
+ const c = t[t.length - 1];
162
+ if (c === Z)
163
+ return h(e, {
164
+ type: "ENDPOINT"
165
+ }).then(w);
166
+ if (c === "bind")
167
+ return O(e, t.slice(0, -1));
168
+ const [n, o] = D(p);
169
+ return h(e, {
170
+ type: "APPLY",
171
+ path: t.map((d) => d.toString()),
172
+ argumentList: n
173
+ }, o).then(w);
174
+ },
175
+ construct(u, i) {
176
+ P(l);
177
+ const [p, c] = D(i);
178
+ return h(e, {
179
+ type: "CONSTRUCT",
180
+ path: t.map((n) => n.toString()),
181
+ argumentList: p
182
+ }, c).then(w);
183
+ }
184
+ });
185
+ return J(s, e), s;
186
+ }
187
+ function te(e) {
188
+ return Array.prototype.concat.apply([], e);
189
+ }
190
+ function D(e) {
191
+ const t = e.map(A);
192
+ return [t.map((r) => r[0]), te(t.map((r) => r[1]))];
193
+ }
194
+ const B = /* @__PURE__ */ new WeakMap();
195
+ function ne(e, t) {
196
+ return B.set(e, t), e;
197
+ }
198
+ function re(e) {
199
+ return Object.assign(e, { [M]: !0 });
200
+ }
201
+ function oe(e, t = globalThis, r = "*") {
202
+ return {
203
+ postMessage: (l, s) => e.postMessage(l, r, s),
204
+ addEventListener: t.addEventListener.bind(t),
205
+ removeEventListener: t.removeEventListener.bind(t)
206
+ };
207
+ }
208
+ function A(e) {
209
+ for (const [t, r] of F)
210
+ if (r.canHandle(e)) {
211
+ const [l, s] = r.serialize(e);
212
+ return [
213
+ {
214
+ type: "HANDLER",
215
+ name: t,
216
+ value: l
217
+ },
218
+ s
219
+ ];
220
+ }
221
+ return [
222
+ {
223
+ type: "RAW",
224
+ value: e
225
+ },
226
+ B.get(e) || []
227
+ ];
228
+ }
229
+ function w(e) {
230
+ switch (e.type) {
231
+ case "HANDLER":
232
+ return F.get(e.name).deserialize(e.value);
233
+ case "RAW":
234
+ return e.value;
235
+ }
236
+ }
237
+ function h(e, t, r) {
238
+ return new Promise((l) => {
239
+ const s = ie();
240
+ e.addEventListener("message", function u(i) {
241
+ !i.data || !i.data.id || i.data.id !== s || (e.removeEventListener("message", u), l(i.data));
242
+ }), e.start && e.start(), e.postMessage(Object.assign({ id: s }, t), r);
243
+ });
244
+ }
245
+ function ie() {
246
+ return new Array(4).fill(0).map(() => Math.floor(Math.random() * Number.MAX_SAFE_INTEGER).toString(16)).join("-");
247
+ }
248
+ function ae(e) {
249
+ se();
250
+ const t = e instanceof Worker ? e : oe(e);
251
+ return q(t);
10
252
  }
11
- function A() {
12
- m.transferHandlers.set("EVENT", {
253
+ function se() {
254
+ F.set("EVENT", {
13
255
  canHandle: (e) => e instanceof CustomEvent,
14
256
  serialize: (e) => [
15
257
  {
@@ -18,101 +260,101 @@ function A() {
18
260
  []
19
261
  ],
20
262
  deserialize: (e) => e
21
- }), m.transferHandlers.set("FUNCTION", {
263
+ }), F.set("FUNCTION", {
22
264
  canHandle: (e) => typeof e == "function",
23
265
  serialize(e) {
24
266
  console.debug("[Comlink][Performance] Proxying a function");
25
- const { port1: o, port2: i } = new MessageChannel();
26
- return m.expose(e, o), [i, [i]];
267
+ const { port1: t, port2: r } = new MessageChannel();
268
+ return z(e, t), [r, [r]];
27
269
  },
28
270
  deserialize(e) {
29
- return e.start(), m.wrap(e);
271
+ return e.start(), q(e);
30
272
  }
31
273
  });
32
274
  }
33
275
  (function() {
34
276
  return navigator.userAgent.toLowerCase().indexOf("firefox") > -1 ? "iframe" : "webworker";
35
277
  })();
36
- var g = typeof globalThis < "u" ? globalThis : typeof window < "u" ? window : typeof global < "u" ? global : typeof self < "u" ? self : {}, F = {}, C = {
278
+ var v = typeof globalThis < "u" ? globalThis : typeof window < "u" ? window : typeof global < "u" ? global : typeof self < "u" ? self : {}, L = {}, le = {
37
279
  get exports() {
38
- return F;
280
+ return L;
39
281
  },
40
282
  set exports(e) {
41
- F = e;
283
+ L = e;
42
284
  }
43
285
  };
44
- (function(e, o) {
45
- (function(i, s) {
46
- s();
47
- })(g, function() {
48
- function i(t, n) {
49
- return typeof n > "u" ? n = { autoBom: !1 } : typeof n != "object" && (console.warn("Deprecated: Expected third argument to be a object"), n = { autoBom: !n }), n.autoBom && /^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(t.type) ? new Blob(["\uFEFF", t], { type: t.type }) : t;
286
+ (function(e, t) {
287
+ (function(r, l) {
288
+ l();
289
+ })(v, function() {
290
+ function r(n, o) {
291
+ return typeof o > "u" ? o = { autoBom: !1 } : typeof o != "object" && (console.warn("Deprecated: Expected third argument to be a object"), o = { autoBom: !o }), o.autoBom && /^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(n.type) ? new Blob(["\uFEFF", n], { type: n.type }) : n;
50
292
  }
51
- function s(t, n, c) {
52
- var r = new XMLHttpRequest();
53
- r.open("GET", t), r.responseType = "blob", r.onload = function() {
54
- l(r.response, n, c);
55
- }, r.onerror = function() {
293
+ function l(n, o, d) {
294
+ var a = new XMLHttpRequest();
295
+ a.open("GET", n), a.responseType = "blob", a.onload = function() {
296
+ c(a.response, o, d);
297
+ }, a.onerror = function() {
56
298
  console.error("could not download file");
57
- }, r.send();
299
+ }, a.send();
58
300
  }
59
- function p(t) {
60
- var n = new XMLHttpRequest();
61
- n.open("HEAD", t, !1);
301
+ function s(n) {
302
+ var o = new XMLHttpRequest();
303
+ o.open("HEAD", n, !1);
62
304
  try {
63
- n.send();
305
+ o.send();
64
306
  } catch {
65
307
  }
66
- return 200 <= n.status && 299 >= n.status;
308
+ return 200 <= o.status && 299 >= o.status;
67
309
  }
68
- function u(t) {
310
+ function u(n) {
69
311
  try {
70
- t.dispatchEvent(new MouseEvent("click"));
312
+ n.dispatchEvent(new MouseEvent("click"));
71
313
  } catch {
72
- var n = document.createEvent("MouseEvents");
73
- n.initMouseEvent("click", !0, !0, window, 0, 0, 0, 80, 20, !1, !1, !1, !1, 0, null), t.dispatchEvent(n);
314
+ var o = document.createEvent("MouseEvents");
315
+ o.initMouseEvent("click", !0, !0, window, 0, 0, 0, 80, 20, !1, !1, !1, !1, 0, null), n.dispatchEvent(o);
74
316
  }
75
317
  }
76
- var a = typeof window == "object" && window.window === window ? window : typeof self == "object" && self.self === self ? self : typeof g == "object" && g.global === g ? g : void 0, f = a.navigator && /Macintosh/.test(navigator.userAgent) && /AppleWebKit/.test(navigator.userAgent) && !/Safari/.test(navigator.userAgent), l = a.saveAs || (typeof window != "object" || window !== a ? function() {
77
- } : "download" in HTMLAnchorElement.prototype && !f ? function(t, n, c) {
78
- var r = a.URL || a.webkitURL, d = document.createElement("a");
79
- n = n || t.name || "download", d.download = n, d.rel = "noopener", typeof t == "string" ? (d.href = t, d.origin === location.origin ? u(d) : p(d.href) ? s(t, n, c) : u(d, d.target = "_blank")) : (d.href = r.createObjectURL(t), setTimeout(function() {
80
- r.revokeObjectURL(d.href);
318
+ var i = typeof window == "object" && window.window === window ? window : typeof self == "object" && self.self === self ? self : typeof v == "object" && v.global === v ? v : void 0, p = i.navigator && /Macintosh/.test(navigator.userAgent) && /AppleWebKit/.test(navigator.userAgent) && !/Safari/.test(navigator.userAgent), c = i.saveAs || (typeof window != "object" || window !== i ? function() {
319
+ } : "download" in HTMLAnchorElement.prototype && !p ? function(n, o, d) {
320
+ var a = i.URL || i.webkitURL, f = document.createElement("a");
321
+ o = o || n.name || "download", f.download = o, f.rel = "noopener", typeof n == "string" ? (f.href = n, f.origin === location.origin ? u(f) : s(f.href) ? l(n, o, d) : u(f, f.target = "_blank")) : (f.href = a.createObjectURL(n), setTimeout(function() {
322
+ a.revokeObjectURL(f.href);
81
323
  }, 4e4), setTimeout(function() {
82
- u(d);
324
+ u(f);
83
325
  }, 0));
84
- } : "msSaveOrOpenBlob" in navigator ? function(t, n, c) {
85
- if (n = n || t.name || "download", typeof t != "string")
86
- navigator.msSaveOrOpenBlob(i(t, c), n);
87
- else if (p(t))
88
- s(t, n, c);
326
+ } : "msSaveOrOpenBlob" in navigator ? function(n, o, d) {
327
+ if (o = o || n.name || "download", typeof n != "string")
328
+ navigator.msSaveOrOpenBlob(r(n, d), o);
329
+ else if (s(n))
330
+ l(n, o, d);
89
331
  else {
90
- var r = document.createElement("a");
91
- r.href = t, r.target = "_blank", setTimeout(function() {
92
- u(r);
332
+ var a = document.createElement("a");
333
+ a.href = n, a.target = "_blank", setTimeout(function() {
334
+ u(a);
93
335
  });
94
336
  }
95
- } : function(t, n, c, r) {
96
- if (r = r || open("", "_blank"), r && (r.document.title = r.document.body.innerText = "downloading..."), typeof t == "string")
97
- return s(t, n, c);
98
- var d = t.type === "application/octet-stream", h = /constructor/i.test(a.HTMLElement) || a.safari, b = /CriOS\/[\d]+/.test(navigator.userAgent);
99
- if ((b || d && h || f) && typeof FileReader < "u") {
100
- var v = new FileReader();
101
- v.onloadend = function() {
102
- var $ = v.result;
103
- $ = b ? $ : $.replace(/^data:[^;]*;/, "data:attachment/file;"), r ? r.location.href = $ : location = $, r = null;
104
- }, v.readAsDataURL(t);
337
+ } : function(n, o, d, a) {
338
+ if (a = a || open("", "_blank"), a && (a.document.title = a.document.body.innerText = "downloading..."), typeof n == "string")
339
+ return l(n, o, d);
340
+ var f = n.type === "application/octet-stream", g = /constructor/i.test(i.HTMLElement) || i.safari, E = /CriOS\/[\d]+/.test(navigator.userAgent);
341
+ if ((E || f && g || p) && typeof FileReader < "u") {
342
+ var y = new FileReader();
343
+ y.onloadend = function() {
344
+ var b = y.result;
345
+ b = E ? b : b.replace(/^data:[^;]*;/, "data:attachment/file;"), a ? a.location.href = b : location = b, a = null;
346
+ }, y.readAsDataURL(n);
105
347
  } else {
106
- var x = a.URL || a.webkitURL, w = x.createObjectURL(t);
107
- r ? r.location = w : location.href = w, r = null, setTimeout(function() {
108
- x.revokeObjectURL(w);
348
+ var x = i.URL || i.webkitURL, m = x.createObjectURL(n);
349
+ a ? a.location = m : location.href = m, a = null, setTimeout(function() {
350
+ x.revokeObjectURL(m);
109
351
  }, 4e4);
110
352
  }
111
353
  });
112
- a.saveAs = l.saveAs = l, e.exports = l;
354
+ i.saveAs = c.saveAs = c, e.exports = c;
113
355
  });
114
- })(C);
115
- const P = `<?php
356
+ })(le);
357
+ const U = `<?php
116
358
 
117
359
  function generateZipFile($exportPath, $databasePath, $docRoot) {
118
360
  $zip = new ZipArchive;
@@ -180,23 +422,23 @@ function importZipFile($pathToZip) {
180
422
  $zip->close();
181
423
  }
182
424
  }
183
- `, R = "databaseExport.xml", T = "/" + R;
184
- async function _(e) {
185
- const o = await e.request({
425
+ `, N = "databaseExport.xml", k = "/" + N;
426
+ async function fe(e) {
427
+ const t = await e.request({
186
428
  relativeUrl: "/wp-admin/export.php?download=true&&content=all"
187
- }), i = new TextDecoder().decode(
188
- o.body
429
+ }), r = new TextDecoder().decode(
430
+ t.body
189
431
  );
190
- await e.writeFile(T, i);
191
- const s = await e.wordPressVersion, p = await e.phpVersion, u = await e.documentRoot, a = `wordpress-playground--wp${s}--php${p}.zip`, f = `/${a}`, l = await e.run({
192
- code: P + ` generateZipFile('${f}', '${T}', '${u}');`
432
+ await e.writeFile(k, r);
433
+ const l = await e.wordPressVersion, s = await e.phpVersion, u = await e.documentRoot, i = `wordpress-playground--wp${l}--php${s}.zip`, p = `/${i}`, c = await e.run({
434
+ code: U + ` generateZipFile('${p}', '${k}', '${u}');`
193
435
  });
194
- if (l.exitCode !== 0)
195
- throw l.errors;
196
- const t = await e.readFileAsBuffer(a), n = new File([t], a);
197
- F.saveAs(n);
436
+ if (c.exitCode !== 0)
437
+ throw c.errors;
438
+ const n = await e.readFileAsBuffer(i), o = new File([n], i);
439
+ L.saveAs(o);
198
440
  }
199
- async function k(e, o) {
441
+ async function we(e, t) {
200
442
  if (
201
443
  // eslint-disable-next-line no-alert
202
444
  !confirm(
@@ -204,253 +446,296 @@ async function k(e, o) {
204
446
  )
205
447
  )
206
448
  return !1;
207
- const i = await o.arrayBuffer(), s = new Uint8Array(i), p = "/import.zip";
208
- await e.writeFile(p, s);
449
+ const r = await t.arrayBuffer(), l = new Uint8Array(r), s = "/import.zip";
450
+ await e.writeFile(s, l);
209
451
  const u = await e.run({
210
- code: P + ` readFileFromZipArchive('${p}', '${T}');`
452
+ code: U + ` readFileFromZipArchive('${s}', '${k}');`
211
453
  });
212
454
  if (u.exitCode !== 0)
213
455
  throw u.errors;
214
- const a = new TextDecoder().decode(
456
+ const i = new TextDecoder().decode(
215
457
  u.body
216
- ), f = new File(
217
- [a],
218
- R
219
- ), l = await e.request({
458
+ ), p = new File(
459
+ [i],
460
+ N
461
+ ), c = await e.request({
220
462
  relativeUrl: "/wp-admin/admin.php?import=wordpress"
221
- }), n = new DOMParser().parseFromString(
222
- new TextDecoder().decode(l.body),
463
+ }), o = new DOMParser().parseFromString(
464
+ new TextDecoder().decode(c.body),
223
465
  "text/html"
224
- ).getElementById("import-upload-form")?.getAttribute("action"), c = await e.request({
225
- relativeUrl: `/wp-admin/${n}`,
466
+ ).getElementById("import-upload-form")?.getAttribute("action"), d = await e.request({
467
+ relativeUrl: `/wp-admin/${o}`,
226
468
  method: "POST",
227
- files: { import: f }
228
- }), d = new DOMParser().parseFromString(
229
- new TextDecoder().decode(c.body),
469
+ files: { import: p }
470
+ }), f = new DOMParser().parseFromString(
471
+ new TextDecoder().decode(d.body),
230
472
  "text/html"
231
473
  ).querySelector(
232
474
  "#wpbody-content form"
233
- ), h = d?.getAttribute(
475
+ ), g = f?.getAttribute(
234
476
  "action"
235
- ), b = (d?.querySelector(
477
+ ), E = (f?.querySelector(
236
478
  "input[name='_wpnonce']"
237
- )).value, v = (d?.querySelector(
479
+ )).value, y = (f?.querySelector(
238
480
  "input[name='_wp_http_referer']"
239
- )).value, x = (d?.querySelector(
481
+ )).value, x = (f?.querySelector(
240
482
  "input[name='import_id']"
241
483
  )).value;
242
484
  await e.request({
243
- relativeUrl: h,
485
+ relativeUrl: g,
244
486
  method: "POST",
245
487
  formData: {
246
- _wpnonce: b,
247
- _wp_http_referer: v,
488
+ _wpnonce: E,
489
+ _wp_http_referer: y,
248
490
  import_id: x
249
491
  }
250
492
  });
251
- const w = await e.run({
252
- code: P + ` importZipFile('${p}');`
493
+ const m = await e.run({
494
+ code: U + ` importZipFile('${s}');`
253
495
  });
254
- if (w.exitCode !== 0)
255
- throw w.errors;
496
+ if (m.exitCode !== 0)
497
+ throw m.errors;
256
498
  return !0;
257
499
  }
258
- async function L(e, o = "admin", i = "password") {
500
+ async function me(e, t = "admin", r = "password") {
259
501
  await e.request({
260
502
  relativeUrl: "/wp-login.php"
261
503
  }), await e.request({
262
504
  relativeUrl: "/wp-login.php",
263
505
  method: "POST",
264
506
  formData: {
265
- log: o,
266
- pwd: i,
507
+ log: t,
508
+ pwd: r,
267
509
  rememberme: "forever"
268
510
  }
269
511
  });
270
512
  }
271
- function y(e) {
272
- return new DOMParser().parseFromString(O(e), "text/html");
513
+ function $(e) {
514
+ return new DOMParser().parseFromString(ce(e), "text/html");
273
515
  }
274
- function O(e) {
516
+ function ce(e) {
275
517
  return new TextDecoder().decode(e.body);
276
518
  }
277
- function q(e) {
278
- const o = e.split(".").shift().replace("-", " ");
279
- return o.charAt(0).toUpperCase() + o.slice(1).toLowerCase();
519
+ function W(e) {
520
+ const t = e.split(".").shift().replace("-", " ");
521
+ return t.charAt(0).toUpperCase() + t.slice(1).toLowerCase();
280
522
  }
281
- async function z(e, o, i = {}) {
282
- const s = "activate" in i ? i.activate : !0, p = await e.request({
523
+ async function ue(e, t, r = {}) {
524
+ const l = "activate" in r ? r.activate : !0, s = await e.request({
283
525
  relativeUrl: "/wp-admin/theme-install.php"
284
- }), u = y(p), a = new FormData(
526
+ }), u = $(s), i = new FormData(
285
527
  u.querySelector(".wp-upload-form")
286
- ), { themezip: f, ...l } = Object.fromEntries(
287
- a.entries()
288
- ), t = await e.request({
528
+ ), { themezip: p, ...c } = Object.fromEntries(
529
+ i.entries()
530
+ ), n = await e.request({
289
531
  relativeUrl: "/wp-admin/update.php?action=upload-theme",
290
532
  method: "POST",
291
- formData: l,
292
- files: { themezip: o }
533
+ formData: c,
534
+ files: { themezip: t }
293
535
  });
294
- if (s) {
295
- const n = y(t), c = n.querySelector(
536
+ if (l) {
537
+ const o = $(n), d = o.querySelector(
296
538
  "#wpbody-content > .wrap"
297
539
  );
298
- if (c?.textContent?.includes(
540
+ if (d?.textContent?.includes(
299
541
  "Theme installation failed."
300
542
  )) {
301
- console.error(c?.textContent);
543
+ console.error(d?.textContent);
302
544
  return;
303
545
  }
304
- const r = n.querySelector(
546
+ const a = o.querySelector(
305
547
  "#wpbody-content .activatelink, .update-from-upload-actions .button.button-primary"
306
548
  );
307
- if (!r) {
549
+ if (!a) {
308
550
  console.error('The "activate" button was not found.');
309
551
  return;
310
552
  }
311
- const d = r.attributes.getNamedItem("href").value, h = new URL(
312
- d,
553
+ const f = a.attributes.getNamedItem("href").value, g = new URL(
554
+ f,
313
555
  await e.pathToInternalUrl("/wp-admin/")
314
556
  ).toString();
315
557
  await e.request({
316
- absoluteUrl: h
558
+ absoluteUrl: g
317
559
  });
318
560
  }
319
561
  }
320
- async function D(e, o, i = {}) {
321
- const s = "activate" in i ? i.activate : !0, p = await e.request({
562
+ async function de(e, t, r = {}) {
563
+ const l = "activate" in r ? r.activate : !0, s = await e.request({
322
564
  relativeUrl: "/wp-admin/plugin-install.php?tab=upload"
323
- }), u = y(p), a = new FormData(
565
+ }), u = $(s), i = new FormData(
324
566
  u.querySelector(".wp-upload-form")
325
- ), { pluginzip: f, ...l } = Object.fromEntries(
326
- a.entries()
327
- ), t = await e.request({
567
+ ), { pluginzip: p, ...c } = Object.fromEntries(
568
+ i.entries()
569
+ ), n = await e.request({
328
570
  relativeUrl: "/wp-admin/update.php?action=upload-plugin",
329
571
  method: "POST",
330
- formData: l,
331
- files: { pluginzip: o }
572
+ formData: c,
573
+ files: { pluginzip: t }
332
574
  });
333
- if (s) {
334
- const r = y(t).querySelector("#wpbody-content .button.button-primary").attributes.getNamedItem("href").value, d = new URL(
335
- r,
575
+ if (l) {
576
+ const a = $(n).querySelector("#wpbody-content .button.button-primary").attributes.getNamedItem("href").value, f = new URL(
577
+ a,
336
578
  await e.pathToInternalUrl("/wp-admin/")
337
579
  ).toString();
338
580
  await e.request({
339
- absoluteUrl: d
581
+ absoluteUrl: f
340
582
  });
341
583
  }
342
- async function n(c, r) {
584
+ async function o(d, a) {
343
585
  return await e.writeFile(
344
- c,
345
- r(await e.readFileAsText(c))
586
+ d,
587
+ a(await e.readFileAsText(d))
346
588
  );
347
589
  }
348
- await e.isDir("/wordpress/wp-content/plugins/gutenberg") && !await e.fileExists("/wordpress/.gutenberg-patched") && (await e.writeFile("/wordpress/.gutenberg-patched", "1"), await n(
590
+ await e.isDir("/wordpress/wp-content/plugins/gutenberg") && !await e.fileExists("/wordpress/.gutenberg-patched") && (await e.writeFile("/wordpress/.gutenberg-patched", "1"), await o(
349
591
  "/wordpress/wp-content/plugins/gutenberg/build/block-editor/index.js",
350
- (c) => c.replace(
592
+ (d) => d.replace(
351
593
  /srcDoc:("[^"]+"|[^,]+)/g,
352
594
  'src:"/wp-includes/empty.html"'
353
595
  )
354
- ), await n(
596
+ ), await o(
355
597
  "/wordpress/wp-content/plugins/gutenberg/build/block-editor/index.min.js",
356
- (c) => c.replace(
598
+ (d) => d.replace(
357
599
  /srcDoc:("[^"]+"|[^,]+)/g,
358
600
  'src:"/wp-includes/empty.html"'
359
601
  )
360
602
  ));
361
603
  }
362
- async function j(e, o) {
363
- const p = y(
604
+ async function he(e, t) {
605
+ const s = $(
364
606
  await e.request({
365
607
  relativeUrl: "/wp-admin/plugins.php"
366
608
  })
367
609
  ).querySelector(
368
- `tr[data-slug="${o}"] a`
610
+ `tr[data-slug="${t}"] a`
369
611
  ).attributes.getNamedItem("href").value;
370
612
  await e.request({
371
- relativeUrl: "/wp-admin/" + p
613
+ relativeUrl: "/wp-admin/" + s
372
614
  });
373
615
  }
374
- async function B(e, o, i = 0, s) {
375
- let p = await fetch("/plugin-proxy?theme=" + o);
376
- if (s && (p = U(
377
- p,
378
- s.partialObserver(
379
- i / 2,
380
- `Installing ${q(o)} theme...`
616
+ const pe = 5 * 1024 * 1024;
617
+ function V(e, t) {
618
+ const r = e.headers.get("content-length") || "", l = parseInt(r, 10) || pe;
619
+ function s(u, i) {
620
+ t(
621
+ new CustomEvent("progress", {
622
+ detail: {
623
+ loaded: u,
624
+ total: i
625
+ }
626
+ })
627
+ );
628
+ }
629
+ return new Response(
630
+ new ReadableStream({
631
+ async start(u) {
632
+ if (!e.body) {
633
+ u.close();
634
+ return;
635
+ }
636
+ const i = e.body.getReader();
637
+ let p = 0;
638
+ for (; ; )
639
+ try {
640
+ const { done: c, value: n } = await i.read();
641
+ if (n && (p += n.byteLength), c) {
642
+ s(p, p), u.close();
643
+ break;
644
+ } else
645
+ s(p, l), u.enqueue(n);
646
+ } catch (c) {
647
+ console.error({ e: c }), u.error(c);
648
+ break;
649
+ }
650
+ }
651
+ }),
652
+ {
653
+ status: e.status,
654
+ statusText: e.statusText,
655
+ headers: e.headers
656
+ }
657
+ );
658
+ }
659
+ async function ge(e, t, r = 0, l) {
660
+ let s = await fetch("/plugin-proxy?theme=" + t);
661
+ if (l && (s = V(
662
+ s,
663
+ l.partialObserver(
664
+ r / 2,
665
+ `Installing ${W(t)} theme...`
381
666
  )
382
- ), s.slowlyIncrementBy(i / 2)), p.status === 200) {
383
- const u = new File([await p.blob()], o);
667
+ ), l.slowlyIncrementBy(r / 2)), s.status === 200) {
668
+ const u = new File([await s.blob()], t);
384
669
  try {
385
- await z(e, u);
386
- } catch (a) {
670
+ await ue(e, u);
671
+ } catch (i) {
387
672
  console.error(
388
- `Proceeding without the ${o} theme. Could not install it in wp-admin. The original error was: ${a}`
389
- ), console.error(a);
673
+ `Proceeding without the ${t} theme. Could not install it in wp-admin. The original error was: ${i}`
674
+ ), console.error(i);
390
675
  }
391
676
  } else
392
677
  console.error(
393
- `Proceeding without the ${o} theme. Could not download the zip bundle from https://downloads.wordpress.org/themes/${o} – Is the file name correct?`
678
+ `Proceeding without the ${t} theme. Could not download the zip bundle from https://downloads.wordpress.org/themes/${t} – Is the file name correct?`
394
679
  );
395
680
  }
396
- async function M(e, o, i = 0, s) {
397
- const p = new E(), u = new E(), a = i / o.length;
398
- await new Promise((f) => {
399
- for (const l of o)
400
- p.enqueue(async () => {
401
- let t = await fetch(
402
- "/plugin-proxy?plugin=" + l
681
+ async function ye(e, t, r = 0, l) {
682
+ const s = new I(), u = new I(), i = r / t.length;
683
+ await new Promise((p) => {
684
+ for (const c of t)
685
+ s.enqueue(async () => {
686
+ let n = await fetch(
687
+ "/plugin-proxy?plugin=" + c
403
688
  );
404
- return s && (t = U(
405
- t,
406
- s.partialObserver(
407
- a * 0.66,
408
- `Installing ${q(
409
- l
689
+ return l && (n = V(
690
+ n,
691
+ l.partialObserver(
692
+ i * 0.66,
693
+ `Installing ${W(
694
+ c
410
695
  )} plugin...`
411
696
  )
412
- )), t.status !== 200 ? (console.error(
413
- `Proceeding without the ${l} plugin. Could not download the zip bundle from https://downloads.wordpress.org/plugin/${l} – Is the file name correct?`
414
- ), null) : new File([await t.blob()], l);
697
+ )), n.status !== 200 ? (console.error(
698
+ `Proceeding without the ${c} plugin. Could not download the zip bundle from https://downloads.wordpress.org/plugin/${c} – Is the file name correct?`
699
+ ), null) : new File([await n.blob()], c);
415
700
  });
416
- p.addEventListener("resolved", (l) => {
701
+ s.addEventListener("resolved", (c) => {
417
702
  u.enqueue(async () => {
418
- if (l.detail) {
419
- s?.slowlyIncrementBy(a * 0.33);
703
+ if (c.detail) {
704
+ l?.slowlyIncrementBy(i * 0.33);
420
705
  try {
421
- await D(e, l.detail);
422
- } catch (t) {
706
+ await de(e, c.detail);
707
+ } catch (n) {
423
708
  console.error(
424
- `Proceeding without the ${l.detail.name} plugin. Could not install it in wp-admin. The original error was: ${t}`
425
- ), console.error(t);
709
+ `Proceeding without the ${c.detail.name} plugin. Could not install it in wp-admin. The original error was: ${n}`
710
+ ), console.error(n);
426
711
  }
427
712
  }
428
713
  });
429
714
  }), u.addEventListener("empty", () => {
430
- u.resolved === o.length && f(null);
715
+ u.resolved === t.length && p(null);
431
716
  });
432
717
  });
433
718
  }
434
- class E extends EventTarget {
719
+ class I extends EventTarget {
435
720
  #e = [];
436
721
  #t = !1;
437
722
  #n = 0;
438
723
  get resolved() {
439
724
  return this.#n;
440
725
  }
441
- async enqueue(o) {
442
- this.#e.push(o), this.#o();
726
+ async enqueue(t) {
727
+ this.#e.push(t), this.#r();
443
728
  }
444
- async #o() {
729
+ async #r() {
445
730
  if (!this.#t)
446
731
  try {
447
732
  for (this.#t = !0; this.#e.length; ) {
448
- const o = this.#e.shift();
449
- if (!o)
733
+ const t = this.#e.shift();
734
+ if (!t)
450
735
  break;
451
- const i = await o();
736
+ const r = await t();
452
737
  ++this.#n, this.dispatchEvent(
453
- new CustomEvent("resolved", { detail: i })
738
+ new CustomEvent("resolved", { detail: r })
454
739
  );
455
740
  }
456
741
  } finally {
@@ -458,21 +743,21 @@ class E extends EventTarget {
458
743
  }
459
744
  }
460
745
  }
461
- async function H(e, o) {
462
- e.src = o, await new Promise((s) => {
463
- e.addEventListener("load", s, !1);
746
+ async function be(e, t) {
747
+ e.src = t, await new Promise((l) => {
748
+ e.addEventListener("load", l, !1);
464
749
  });
465
- const i = S(e.contentWindow);
466
- return await i.absoluteUrl, i;
750
+ const r = ae(e.contentWindow);
751
+ return await r.absoluteUrl, r;
467
752
  }
468
753
  export {
469
- j as activatePlugin,
470
- H as connectPlayground,
471
- _ as exportFile,
472
- k as importFile,
473
- D as installPlugin,
474
- M as installPluginsFromDirectory,
475
- z as installTheme,
476
- B as installThemeFromDirectory,
477
- L as login
754
+ he as activatePlugin,
755
+ be as connectPlayground,
756
+ fe as exportFile,
757
+ we as importFile,
758
+ de as installPlugin,
759
+ ye as installPluginsFromDirectory,
760
+ ue as installTheme,
761
+ ge as installThemeFromDirectory,
762
+ me as login
478
763
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wp-playground/client",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "description": "WordPress Playground client",
5
5
  "repository": {
6
6
  "type": "git",
@@ -29,9 +29,5 @@
29
29
  "type": "module",
30
30
  "main": "index.js",
31
31
  "types": "index.d.ts",
32
- "dependencies": {
33
- "@php-wasm/progress": "0.0.1",
34
- "comlink": "4.4.1"
35
- },
36
- "gitHead": "87efbf6ada006edfb47fc16c081fafb05c555a93"
32
+ "gitHead": "629df0f3632cbd271f1fdbad2a1d0c0b22f40d4f"
37
33
  }