@php-wasm/fs-journal 0.6.2 → 0.6.4

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.cjs +12 -8
  2. package/index.js +331 -315
  3. package/package.json +2 -2
package/index.js CHANGED
@@ -1,19 +1,19 @@
1
- var Z = (e, t, r) => {
1
+ var Y = (e, t, r) => {
2
2
  if (!t.has(e))
3
3
  throw TypeError("Cannot " + r);
4
4
  };
5
- var a = (e, t, r) => (Z(e, t, "read from private field"), r ? r.call(e) : t.get(e)), l = (e, t, r) => {
5
+ var a = (e, t, r) => (Y(e, t, "read from private field"), r ? r.call(e) : t.get(e)), u = (e, t, r) => {
6
6
  if (t.has(e))
7
7
  throw TypeError("Cannot add the same private member more than once");
8
8
  t instanceof WeakSet ? t.add(e) : t.set(e, r);
9
- }, d = (e, t, r, s) => (Z(e, t, "write to private field"), s ? s.call(e, r) : t.set(e, r), r);
10
- var p = (e, t, r) => (Z(e, t, "access private method"), r);
9
+ }, h = (e, t, r, s) => (Y(e, t, "write to private field"), s ? s.call(e, r) : t.set(e, r), r);
10
+ var f = (e, t, r) => (Y(e, t, "access private method"), r);
11
11
  if (typeof File > "u") {
12
12
  class e extends Blob {
13
- constructor(r, s, i) {
13
+ constructor(r, s, n) {
14
14
  super(r);
15
- let n;
16
- i != null && i.lastModified && (n = /* @__PURE__ */ new Date()), (!n || isNaN(n.getFullYear())) && (n = /* @__PURE__ */ new Date()), this.lastModifiedDate = n, this.lastModified = n.getMilliseconds(), this.name = s || "";
15
+ let i;
16
+ n != null && n.lastModified && (i = /* @__PURE__ */ new Date()), (!i || isNaN(i.getFullYear())) && (i = /* @__PURE__ */ new Date()), this.lastModifiedDate = i, this.lastModified = i.getMilliseconds(), this.name = s || "";
17
17
  }
18
18
  }
19
19
  global.File = e;
@@ -50,10 +50,10 @@ function isByobSupported() {
50
50
  // this if needed.
51
51
  autoAllocateChunkSize: 512 * 1024,
52
52
  async pull(r) {
53
- const s = r.byobRequest.view, n = await t.slice(e, e + s.byteLength).arrayBuffer(), o = new Uint8Array(n);
53
+ const s = r.byobRequest.view, i = await t.slice(e, e + s.byteLength).arrayBuffer(), o = new Uint8Array(i);
54
54
  new Uint8Array(s.buffer).set(o);
55
- const c = o.byteLength;
56
- r.byobRequest.respond(c), e += c, e >= t.size && r.close();
55
+ const l = o.byteLength;
56
+ r.byobRequest.respond(l), e += l, e >= t.size && r.close();
57
57
  }
58
58
  });
59
59
  });
@@ -114,27 +114,27 @@ function improveWASMErrorReporting(e) {
114
114
  for (const r in e.asm)
115
115
  if (typeof e.asm[r] == "function") {
116
116
  const s = e.asm[r];
117
- e.asm[r] = function(...i) {
118
- var n;
117
+ e.asm[r] = function(...n) {
118
+ var i;
119
119
  try {
120
- return s(...i);
120
+ return s(...n);
121
121
  } catch (o) {
122
122
  if (!(o instanceof Error))
123
123
  throw o;
124
- const c = clarifyErrorMessage(
124
+ const l = clarifyErrorMessage(
125
125
  o,
126
- (n = e.lastAsyncifyStackSource) == null ? void 0 : n.stack
126
+ (i = e.lastAsyncifyStackSource) == null ? void 0 : i.stack
127
127
  );
128
128
  if (e.lastAsyncifyStackSource && (o.cause = e.lastAsyncifyStackSource), t.hasListeners()) {
129
129
  t.dispatchEvent(
130
130
  new ErrorEvent("error", {
131
131
  error: o,
132
- message: c
132
+ message: l
133
133
  })
134
134
  );
135
135
  return;
136
136
  }
137
- throw isExitCodeZero(o) || showCriticalErrorBox(c), o;
137
+ throw isExitCodeZero(o) || showCriticalErrorBox(l), o;
138
138
  }
139
139
  };
140
140
  }
@@ -266,20 +266,39 @@ function normalizePath$1(e) {
266
266
  function normalizePathsArray(e, t) {
267
267
  let r = 0;
268
268
  for (let s = e.length - 1; s >= 0; s--) {
269
- const i = e[s];
270
- i === "." ? e.splice(s, 1) : i === ".." ? (e.splice(s, 1), r++) : r && (e.splice(s, 1), r--);
269
+ const n = e[s];
270
+ n === "." ? e.splice(s, 1) : n === ".." ? (e.splice(s, 1), r++) : r && (e.splice(s, 1), r--);
271
271
  }
272
272
  if (t)
273
273
  for (; r; r--)
274
274
  e.unshift("..");
275
275
  return e;
276
276
  }
277
+ function splitShellCommand(e) {
278
+ let s = 0, n = "";
279
+ const i = [];
280
+ let o = "";
281
+ for (let l = 0; l < e.length; l++) {
282
+ const c = e[l];
283
+ c === "\\" ? ((e[l + 1] === '"' || e[l + 1] === "'") && l++, o += e[l]) : s === 0 ? c === '"' || c === "'" ? (s = 1, n = c) : c.match(/\s/) ? (o.trim().length && i.push(o.trim()), o = c) : i.length && !o ? o = i.pop() + c : o += c : s === 1 && (c === n ? (s = 0, n = "") : o += c);
284
+ }
285
+ return o && i.push(o.trim()), i;
286
+ }
277
287
  function createSpawnHandler(e) {
278
- return function(t) {
279
- const r = new ChildProcess(), s = new ProcessApi(r);
288
+ return function(t, r = [], s = {}) {
289
+ const n = new ChildProcess(), i = new ProcessApi(n);
280
290
  return setTimeout(async () => {
281
- await e(t, s), r.emit("spawn", !0);
282
- }), r;
291
+ let o = [];
292
+ if (r.length)
293
+ o = [t, ...r];
294
+ else if (typeof t == "string")
295
+ o = splitShellCommand(t);
296
+ else if (Array.isArray(t))
297
+ o = t;
298
+ else
299
+ throw new Error("Invalid command ", t);
300
+ await e(o, i, s), n.emit("spawn", !0);
301
+ }), n;
283
302
  };
284
303
  }
285
304
  class EventEmitter {
@@ -304,9 +323,15 @@ class ProcessApi extends EventEmitter {
304
323
  stdout(t) {
305
324
  typeof t == "string" && (t = new TextEncoder().encode(t)), this.childProcess.stdout.emit("data", t);
306
325
  }
326
+ stdoutEnd() {
327
+ this.childProcess.stdout.emit("end", {});
328
+ }
307
329
  stderr(t) {
308
330
  typeof t == "string" && (t = new TextEncoder().encode(t)), this.childProcess.stderr.emit("data", t);
309
331
  }
332
+ stderrEnd() {
333
+ this.childProcess.stderr.emit("end", {});
334
+ }
310
335
  exit(t) {
311
336
  this.exited || (this.exited = !0, this.childProcess.emit("exit", t));
312
337
  }
@@ -344,8 +369,8 @@ ReadableStream.prototype[Symbol.asyncIterator] || (ReadableStream.prototype[Symb
344
369
  }, ReadableStream.prototype.iterate = // @ts-ignore
345
370
  ReadableStream.prototype[Symbol.asyncIterator]);
346
371
  class PHPResponse {
347
- constructor(t, r, s, i = "", n = 0) {
348
- this.httpStatusCode = t, this.headers = r, this.bytes = s, this.exitCode = n, this.errors = i;
372
+ constructor(t, r, s, n = "", i = 0) {
373
+ this.httpStatusCode = t, this.headers = r, this.bytes = s, this.exitCode = i, this.errors = n;
349
374
  }
350
375
  static fromRawData(t) {
351
376
  return new PHPResponse(
@@ -378,16 +403,16 @@ class PHPResponse {
378
403
  return new TextDecoder().decode(this.bytes);
379
404
  }
380
405
  }
381
- var b, F;
406
+ var v, T;
382
407
  class PHPBrowser {
383
408
  /**
384
409
  * @param server - The PHP server to browse.
385
410
  * @param config - The browser configuration.
386
411
  */
387
412
  constructor(t, r = {}) {
388
- l(this, b, void 0);
389
- l(this, F, void 0);
390
- this.requestHandler = t, d(this, b, {}), d(this, F, {
413
+ u(this, v, void 0);
414
+ u(this, T, void 0);
415
+ this.requestHandler = t, h(this, v, {}), h(this, T, {
391
416
  handleRedirects: !1,
392
417
  maxRedirects: 4,
393
418
  ...r
@@ -415,14 +440,14 @@ class PHPBrowser {
415
440
  cookie: this.serializeCookies()
416
441
  }
417
442
  });
418
- if (s.headers["set-cookie"] && this.setCookies(s.headers["set-cookie"]), a(this, F).handleRedirects && s.headers.location && r < a(this, F).maxRedirects) {
419
- const i = new URL(
443
+ if (s.headers["set-cookie"] && this.setCookies(s.headers["set-cookie"]), a(this, T).handleRedirects && s.headers.location && r < a(this, T).maxRedirects) {
444
+ const n = new URL(
420
445
  s.headers.location[0],
421
446
  this.requestHandler.absoluteUrl
422
447
  );
423
448
  return this.request(
424
449
  {
425
- url: i.toString(),
450
+ url: n.toString(),
426
451
  method: "GET",
427
452
  headers: {}
428
453
  },
@@ -452,20 +477,20 @@ class PHPBrowser {
452
477
  try {
453
478
  if (!r.includes("="))
454
479
  continue;
455
- const s = r.indexOf("="), i = r.substring(0, s), n = r.substring(s + 1).split(";")[0];
456
- a(this, b)[i] = n;
480
+ const s = r.indexOf("="), n = r.substring(0, s), i = r.substring(s + 1).split(";")[0];
481
+ a(this, v)[n] = i;
457
482
  } catch (s) {
458
483
  console.error(s);
459
484
  }
460
485
  }
461
486
  serializeCookies() {
462
487
  const t = [];
463
- for (const r in a(this, b))
464
- t.push(`${r}=${a(this, b)[r]}`);
488
+ for (const r in a(this, v))
489
+ t.push(`${r}=${a(this, v)[r]}`);
465
490
  return t.join("; ");
466
491
  }
467
492
  }
468
- b = new WeakMap(), F = new WeakMap();
493
+ v = new WeakMap(), T = new WeakMap();
469
494
  const DEFAULT_BASE_URL = "http://example.com";
470
495
  function toRelativeUrl(e) {
471
496
  return e.toString().substring(e.origin.length);
@@ -476,7 +501,35 @@ function removePathPrefix(e, t) {
476
501
  function ensurePathPrefix(e, t) {
477
502
  return !t || e.startsWith(t) ? e : t + e;
478
503
  }
479
- var E, x, N, R, k, m, C, S, B, Q, L, X, O, ee;
504
+ async function encodeAsMultipart(e) {
505
+ const t = `----${Math.random().toString(36).slice(2)}`, r = `multipart/form-data; boundary=${t}`, s = new TextEncoder(), n = [];
506
+ for (const [c, p] of Object.entries(e))
507
+ n.push(`--${t}\r
508
+ `), n.push(`Content-Disposition: form-data; name="${c}"`), p instanceof File && n.push(`; filename="${p.name}"`), n.push(`\r
509
+ `), p instanceof File && (n.push("Content-Type: application/octet-stream"), n.push(`\r
510
+ `)), n.push(`\r
511
+ `), p instanceof File ? n.push(await fileToUint8Array(p)) : n.push(p), n.push(`\r
512
+ `);
513
+ n.push(`--${t}--\r
514
+ `);
515
+ const i = n.reduce((c, p) => c + p.length, 0), o = new Uint8Array(i);
516
+ let l = 0;
517
+ for (const c of n)
518
+ o.set(
519
+ typeof c == "string" ? s.encode(c) : c,
520
+ l
521
+ ), l += c.length;
522
+ return { bytes: o, contentType: r };
523
+ }
524
+ function fileToUint8Array(e) {
525
+ return new Promise((t) => {
526
+ const r = new FileReader();
527
+ r.onload = () => {
528
+ t(new Uint8Array(r.result));
529
+ }, r.readAsArrayBuffer(e);
530
+ });
531
+ }
532
+ var y, F, I, b, x, m, C, R, N, K, O, Z, U, X;
480
533
  class PHPRequestHandler {
481
534
  /**
482
535
  * @param php - The PHP instance.
@@ -489,7 +542,7 @@ class PHPRequestHandler {
489
542
  * @param fsPath - Absolute path of the static file to serve.
490
543
  * @returns The response.
491
544
  */
492
- l(this, B);
545
+ u(this, N);
493
546
  /**
494
547
  * Runs the requested PHP file with all the request and $_SERVER
495
548
  * superglobals populated.
@@ -497,7 +550,7 @@ class PHPRequestHandler {
497
550
  * @param request - The request.
498
551
  * @returns The response.
499
552
  */
500
- l(this, L);
553
+ u(this, O);
501
554
  /**
502
555
  * Resolve the requested path to the filesystem path of the requested PHP file.
503
556
  *
@@ -507,30 +560,30 @@ class PHPRequestHandler {
507
560
  * @throws {Error} If the requested path doesn't exist.
508
561
  * @returns The resolved filesystem path.
509
562
  */
510
- l(this, O);
511
- l(this, E, void 0);
512
- l(this, x, void 0);
513
- l(this, N, void 0);
514
- l(this, R, void 0);
515
- l(this, k, void 0);
516
- l(this, m, void 0);
517
- l(this, C, void 0);
518
- l(this, S, void 0);
519
- d(this, S, new Semaphore({ concurrency: 1 }));
563
+ u(this, U);
564
+ u(this, y, void 0);
565
+ u(this, F, void 0);
566
+ u(this, I, void 0);
567
+ u(this, b, void 0);
568
+ u(this, x, void 0);
569
+ u(this, m, void 0);
570
+ u(this, C, void 0);
571
+ u(this, R, void 0);
572
+ h(this, R, new Semaphore({ concurrency: 1 }));
520
573
  const {
521
574
  documentRoot: s = "/www/",
522
- absoluteUrl: i = typeof location == "object" ? location == null ? void 0 : location.href : ""
575
+ absoluteUrl: n = typeof location == "object" ? location == null ? void 0 : location.href : ""
523
576
  } = r;
524
- this.php = t, d(this, E, s);
525
- const n = new URL(i);
526
- d(this, N, n.hostname), d(this, R, n.port ? Number(n.port) : n.protocol === "https:" ? 443 : 80), d(this, x, (n.protocol || "").replace(":", ""));
527
- const o = a(this, R) !== 443 && a(this, R) !== 80;
528
- d(this, k, [
529
- a(this, N),
530
- o ? `:${a(this, R)}` : ""
531
- ].join("")), d(this, m, n.pathname.replace(/\/+$/, "")), d(this, C, [
532
- `${a(this, x)}://`,
533
- a(this, k),
577
+ this.php = t, h(this, y, s);
578
+ const i = new URL(n);
579
+ h(this, I, i.hostname), h(this, b, i.port ? Number(i.port) : i.protocol === "https:" ? 443 : 80), h(this, F, (i.protocol || "").replace(":", ""));
580
+ const o = a(this, b) !== 443 && a(this, b) !== 80;
581
+ h(this, x, [
582
+ a(this, I),
583
+ o ? `:${a(this, b)}` : ""
584
+ ].join("")), h(this, m, i.pathname.replace(/\/+$/, "")), h(this, C, [
585
+ `${a(this, F)}://`,
586
+ a(this, x),
534
587
  a(this, m)
535
588
  ].join(""));
536
589
  }
@@ -544,7 +597,7 @@ class PHPRequestHandler {
544
597
  return r.pathname.startsWith(a(this, m)) && (r.pathname = r.pathname.slice(a(this, m).length)), toRelativeUrl(r);
545
598
  }
546
599
  get isRequestRunning() {
547
- return a(this, S).running > 0;
600
+ return a(this, R).running > 0;
548
601
  }
549
602
  /** @inheritDoc */
550
603
  get absoluteUrl() {
@@ -552,21 +605,21 @@ class PHPRequestHandler {
552
605
  }
553
606
  /** @inheritDoc */
554
607
  get documentRoot() {
555
- return a(this, E);
608
+ return a(this, y);
556
609
  }
557
610
  /** @inheritDoc */
558
611
  async request(t) {
559
612
  const r = t.url.startsWith("http://") || t.url.startsWith("https://"), s = new URL(
560
613
  t.url,
561
614
  r ? void 0 : DEFAULT_BASE_URL
562
- ), i = removePathPrefix(
615
+ ), n = removePathPrefix(
563
616
  s.pathname,
564
617
  a(this, m)
565
- ), n = `${a(this, E)}${i}`;
566
- return seemsLikeAPHPRequestHandlerPath(n) ? await p(this, L, X).call(this, t, s) : p(this, B, Q).call(this, n);
618
+ ), i = `${a(this, y)}${n}`;
619
+ return seemsLikeAPHPRequestHandlerPath(i) ? await f(this, O, Z).call(this, t, s) : f(this, N, K).call(this, i);
567
620
  }
568
621
  }
569
- E = new WeakMap(), x = new WeakMap(), N = new WeakMap(), R = new WeakMap(), k = new WeakMap(), m = new WeakMap(), C = new WeakMap(), S = new WeakMap(), B = new WeakSet(), Q = function(t) {
622
+ y = new WeakMap(), F = new WeakMap(), I = new WeakMap(), b = new WeakMap(), x = new WeakMap(), m = new WeakMap(), C = new WeakMap(), R = new WeakMap(), N = new WeakSet(), K = function(t) {
570
623
  if (!this.php.fileExists(t))
571
624
  return new PHPResponse(
572
625
  404,
@@ -591,9 +644,9 @@ E = new WeakMap(), x = new WeakMap(), N = new WeakMap(), R = new WeakMap(), k =
591
644
  },
592
645
  r
593
646
  );
594
- }, L = new WeakSet(), X = async function(t, r) {
595
- var i, n, o;
596
- if (a(this, S).running > 0 && ((i = t.headers) == null ? void 0 : i["x-request-issuer"]) === "php")
647
+ }, O = new WeakSet(), Z = async function(t, r) {
648
+ var n, i;
649
+ if (a(this, R).running > 0 && ((n = t.headers) == null ? void 0 : n["x-request-issuer"]) === "php")
597
650
  return console.warn(
598
651
  "Possible deadlock: Called request() before the previous request() have finished. PHP likely issued an HTTP call to itself. Normally this would lead to infinite waiting as Request 1 holds the lock that the Request 2 is waiting to acquire. That's not useful, so PHPRequestHandler will return error 502 instead."
599
652
  ), new PHPResponse(
@@ -601,47 +654,34 @@ E = new WeakMap(), x = new WeakMap(), N = new WeakMap(), R = new WeakMap(), k =
601
654
  {},
602
655
  new TextEncoder().encode("502 Bad Gateway")
603
656
  );
604
- const s = await a(this, S).acquire();
657
+ const s = await a(this, R).acquire();
605
658
  try {
606
- this.php.addServerGlobalEntry("REMOTE_ADDR", "127.0.0.1"), this.php.addServerGlobalEntry("DOCUMENT_ROOT", a(this, E)), this.php.addServerGlobalEntry(
659
+ this.php.addServerGlobalEntry("REMOTE_ADDR", "127.0.0.1"), this.php.addServerGlobalEntry("DOCUMENT_ROOT", a(this, y)), this.php.addServerGlobalEntry(
607
660
  "HTTPS",
608
661
  a(this, C).startsWith("https://") ? "on" : ""
609
662
  );
610
- let c = "GET";
611
- const h = {
612
- host: a(this, k),
663
+ let o = "GET";
664
+ const l = {
665
+ host: a(this, x),
613
666
  ...normalizeHeaders(t.headers || {})
614
- }, f = [];
615
- if (t.files && Object.keys(t.files).length) {
616
- c = "POST";
617
- for (const y in t.files) {
618
- const M = t.files[y];
619
- f.push({
620
- key: y,
621
- name: M.name,
622
- type: M.type,
623
- data: new Uint8Array(await M.arrayBuffer())
624
- });
625
- }
626
- (n = h["content-type"]) != null && n.startsWith("multipart/form-data") && (t.formData = parseMultipartFormDataString(
627
- t.body || ""
628
- ), h["content-type"] = "application/x-www-form-urlencoded", delete t.body);
667
+ };
668
+ let c = t.body;
669
+ if (typeof c == "object" && !(c instanceof Uint8Array)) {
670
+ o = "POST";
671
+ const { bytes: d, contentType: _ } = await encodeAsMultipart(c);
672
+ c = d, l["content-type"] = _;
629
673
  }
630
- let u;
631
- t.formData !== void 0 ? (c = "POST", h["content-type"] = h["content-type"] || "application/x-www-form-urlencoded", u = new URLSearchParams(
632
- t.formData
633
- ).toString()) : u = t.body;
634
- let _;
674
+ let p;
635
675
  try {
636
- let y = r.pathname;
637
- if ((o = t.headers) != null && o["x-rewrite-url"])
676
+ let d = r.pathname;
677
+ if ((i = t.headers) != null && i["x-rewrite-url"])
638
678
  try {
639
- y = new URL(
679
+ d = new URL(
640
680
  t.headers["x-rewrite-url"]
641
681
  ).pathname;
642
682
  } catch {
643
683
  }
644
- _ = p(this, O, ee).call(this, y);
684
+ p = f(this, U, X).call(this, d);
645
685
  } catch {
646
686
  return new PHPResponse(
647
687
  404,
@@ -654,39 +694,23 @@ E = new WeakMap(), x = new WeakMap(), N = new WeakMap(), R = new WeakMap(), k =
654
694
  toRelativeUrl(r),
655
695
  a(this, m)
656
696
  ),
657
- protocol: a(this, x),
658
- method: t.method || c,
659
- body: u,
660
- fileInfos: f,
661
- scriptPath: _,
662
- headers: h
697
+ protocol: a(this, F),
698
+ method: t.method || o,
699
+ body: c,
700
+ scriptPath: p,
701
+ headers: l
663
702
  });
664
703
  } finally {
665
704
  s();
666
705
  }
667
- }, O = new WeakSet(), ee = function(t) {
706
+ }, U = new WeakSet(), X = function(t) {
668
707
  let r = removePathPrefix(t, a(this, m));
669
- r.includes(".php") ? r = r.split(".php")[0] + ".php" : (r.endsWith("/") || (r += "/"), r.endsWith("index.php") || (r += "index.php"));
670
- const s = `${a(this, E)}${r}`;
708
+ r.includes(".php") ? r = r.split(".php")[0] + ".php" : this.php.isDir(`${a(this, y)}${r}`) ? (r.endsWith("/") || (r = `${r}/`), r = `${r}index.php`) : r = "/index.php";
709
+ const s = `${a(this, y)}${r}`;
671
710
  if (this.php.fileExists(s))
672
711
  return s;
673
712
  throw new Error(`File not found: ${s}`);
674
713
  };
675
- function parseMultipartFormDataString(e) {
676
- const t = {}, r = e.match(/--(.*)\r\n/);
677
- if (!r)
678
- return t;
679
- const s = r[1], i = e.split(`--${s}`);
680
- return i.shift(), i.pop(), i.forEach((n) => {
681
- const o = n.indexOf(`\r
682
- \r
683
- `), c = n.substring(0, o).trim(), h = n.substring(o + 4).trim(), f = c.match(/name="([^"]+)"/);
684
- if (f) {
685
- const u = f[1];
686
- t[u] = h;
687
- }
688
- }), t;
689
- }
690
714
  function inferMimeType(e) {
691
715
  switch (e.split(".").pop()) {
692
716
  case "css":
@@ -821,20 +845,20 @@ function getEmscriptenFsError(e) {
821
845
  return FileErrorCodes[t];
822
846
  }
823
847
  function rethrowFileSystemError(e = "") {
824
- return function(r, s, i) {
825
- const n = i.value;
826
- i.value = function(...o) {
848
+ return function(r, s, n) {
849
+ const i = n.value;
850
+ n.value = function(...o) {
827
851
  try {
828
- return n.apply(this, o);
829
- } catch (c) {
830
- const h = typeof c == "object" ? c == null ? void 0 : c.errno : null;
831
- if (h in FileErrorCodes) {
832
- const f = FileErrorCodes[h], u = typeof o[0] == "string" ? o[0] : null, _ = u !== null ? e.replaceAll("{path}", u) : e;
833
- throw new Error(`${_}: ${f}`, {
834
- cause: c
852
+ return i.apply(this, o);
853
+ } catch (l) {
854
+ const c = typeof l == "object" ? l == null ? void 0 : l.errno : null;
855
+ if (c in FileErrorCodes) {
856
+ const p = FileErrorCodes[c], d = typeof o[0] == "string" ? o[0] : null, _ = d !== null ? e.replaceAll("{path}", d) : e;
857
+ throw new Error(`${_}: ${p}`, {
858
+ cause: l
835
859
  });
836
860
  }
837
- throw c;
861
+ throw l;
838
862
  }
839
863
  };
840
864
  };
@@ -848,12 +872,12 @@ function getLoadedRuntime(e) {
848
872
  return typeof process < "u" && ((e = process.release) == null ? void 0 : e.name) === "node" ? "NODE" : typeof window < "u" ? "WEB" : typeof WorkerGlobalScope < "u" && self instanceof WorkerGlobalScope ? "WORKER" : "NODE";
849
873
  })();
850
874
  var __defProp = Object.defineProperty, __getOwnPropDesc = Object.getOwnPropertyDescriptor, __decorateClass = (e, t, r, s) => {
851
- for (var i = s > 1 ? void 0 : s ? __getOwnPropDesc(t, r) : t, n = e.length - 1, o; n >= 0; n--)
852
- (o = e[n]) && (i = (s ? o(t, r, i) : o(i)) || i);
853
- return s && i && __defProp(t, r, i), i;
875
+ for (var n = s > 1 ? void 0 : s ? __getOwnPropDesc(t, r) : t, i = e.length - 1, o; i >= 0; i--)
876
+ (o = e[i]) && (n = (s ? o(t, r, n) : o(n)) || n);
877
+ return s && n && __defProp(t, r, n), n;
854
878
  };
855
879
  const STRING = "string", NUMBER = "number", __private__dont__use = Symbol("__private__dont__use");
856
- var T, H, A, w, P, v, g, I, U, te, D, re, $, se, q, ne, W, ie, j, oe, z, ae, G, le, J, ce, Y, ue, V, de, K, he;
880
+ var S, A, k, E, g, P, w, H, M, ee, B, te, L, re, $, se, D, ne, q, ie, W, oe, j, ae, z, le, G, ce, J, ue, Q, de;
857
881
  class BasePHP {
858
882
  /**
859
883
  * Initializes a PHP runtime.
@@ -863,55 +887,46 @@ class BasePHP {
863
887
  * @param serverOptions - Optional. Options for the PHPRequestHandler. If undefined, no request handler will be initialized.
864
888
  */
865
889
  constructor(e, t) {
866
- l(this, U);
867
- l(this, D);
868
- l(this, $);
869
- l(this, q);
870
- l(this, W);
871
- l(this, j);
872
- l(this, z);
873
- l(this, G);
874
- l(this, J);
875
- /**
876
- * Adds file information to $_FILES superglobal in PHP.
877
- *
878
- * In particular:
879
- * * Creates the file data in the filesystem
880
- * * Registers the file details in PHP
881
- *
882
- * @param fileInfo - File details
883
- */
884
- l(this, Y);
885
- l(this, V);
886
- l(this, K);
887
- l(this, T, void 0);
888
- l(this, H, void 0);
889
- l(this, A, void 0);
890
- l(this, w, void 0);
891
- l(this, P, void 0);
892
- l(this, v, void 0);
893
- l(this, g, void 0);
894
- l(this, I, void 0);
895
- d(this, T, []), d(this, w, !1), d(this, P, null), d(this, v, {}), d(this, g, /* @__PURE__ */ new Map()), d(this, I, []), this.semaphore = new Semaphore({ concurrency: 1 }), e !== void 0 && this.initializeRuntime(e), t && (this.requestHandler = new PHPBrowser(
890
+ u(this, M);
891
+ u(this, B);
892
+ u(this, L);
893
+ u(this, $);
894
+ u(this, D);
895
+ u(this, q);
896
+ u(this, W);
897
+ u(this, j);
898
+ u(this, z);
899
+ u(this, G);
900
+ u(this, J);
901
+ u(this, Q);
902
+ u(this, S, void 0);
903
+ u(this, A, void 0);
904
+ u(this, k, void 0);
905
+ u(this, E, void 0);
906
+ u(this, g, void 0);
907
+ u(this, P, void 0);
908
+ u(this, w, void 0);
909
+ u(this, H, void 0);
910
+ h(this, S, []), h(this, E, !1), h(this, g, null), h(this, P, {}), h(this, w, /* @__PURE__ */ new Map()), h(this, H, []), this.semaphore = new Semaphore({ concurrency: 1 }), e !== void 0 && this.initializeRuntime(e), t && (this.requestHandler = new PHPBrowser(
896
911
  new PHPRequestHandler(this, t)
897
912
  ));
898
913
  }
899
914
  addEventListener(e, t) {
900
- a(this, g).has(e) || a(this, g).set(e, /* @__PURE__ */ new Set()), a(this, g).get(e).add(t);
915
+ a(this, w).has(e) || a(this, w).set(e, /* @__PURE__ */ new Set()), a(this, w).get(e).add(t);
901
916
  }
902
917
  removeEventListener(e, t) {
903
918
  var r;
904
- (r = a(this, g).get(e)) == null || r.delete(t);
919
+ (r = a(this, w).get(e)) == null || r.delete(t);
905
920
  }
906
921
  dispatchEvent(e) {
907
- const t = a(this, g).get(e.type);
922
+ const t = a(this, w).get(e.type);
908
923
  if (t)
909
924
  for (const r of t)
910
925
  r(e);
911
926
  }
912
927
  /** @inheritDoc */
913
928
  async onMessage(e) {
914
- a(this, I).push(e);
929
+ a(this, H).push(e);
915
930
  }
916
931
  /** @inheritDoc */
917
932
  async setSpawnHandler(handler) {
@@ -942,13 +957,13 @@ class BasePHP {
942
957
  if (!t)
943
958
  throw new Error("Invalid PHP runtime id.");
944
959
  this[__private__dont__use] = t, t.onMessage = async (r) => {
945
- for (const s of a(this, I)) {
946
- const i = await s(r);
947
- if (i)
948
- return i;
960
+ for (const s of a(this, H)) {
961
+ const n = await s(r);
962
+ if (n)
963
+ return n;
949
964
  }
950
965
  return "";
951
- }, d(this, P, improveWASMErrorReporting(t)), this.dispatchEvent({
966
+ }, h(this, g, improveWASMErrorReporting(t)), this.dispatchEvent({
952
967
  type: "runtime.initialized"
953
968
  });
954
969
  }
@@ -963,13 +978,13 @@ class BasePHP {
963
978
  throw new Error(
964
979
  "Could not set SAPI name. This can only be done before the PHP WASM module is initialized.Did you already dispatch any requests?"
965
980
  );
966
- d(this, A, e);
981
+ h(this, k, e);
967
982
  }
968
983
  /** @inheritDoc */
969
984
  setPhpIniPath(e) {
970
- if (a(this, w))
985
+ if (a(this, E))
971
986
  throw new Error("Cannot set PHP ini path after calling run().");
972
- d(this, H, e), this[__private__dont__use].ccall(
987
+ h(this, A, e), this[__private__dont__use].ccall(
973
988
  "wasm_set_phpini_path",
974
989
  null,
975
990
  ["string"],
@@ -978,9 +993,9 @@ class BasePHP {
978
993
  }
979
994
  /** @inheritDoc */
980
995
  setPhpIniEntry(e, t) {
981
- if (a(this, w))
996
+ if (a(this, E))
982
997
  throw new Error("Cannot set PHP ini entries after calling run().");
983
- a(this, T).push([e, t]);
998
+ a(this, S).push([e, t]);
984
999
  }
985
1000
  /** @inheritDoc */
986
1001
  chdir(e) {
@@ -997,25 +1012,29 @@ class BasePHP {
997
1012
  const t = await this.semaphore.acquire();
998
1013
  let r;
999
1014
  try {
1000
- a(this, w) || (p(this, U, te).call(this), d(this, w, !0)), p(this, G, le).call(this, e.scriptPath || ""), p(this, $, se).call(this, e.relativeUri || ""), p(this, W, ie).call(this, e.method || "GET");
1001
- const s = normalizeHeaders(e.headers || {}), i = s.host || "example.com:443";
1002
- if (p(this, q, ne).call(this, i, e.protocol || "http"), p(this, j, oe).call(this, s), e.body && (r = p(this, z, ae).call(this, e.body)), e.fileInfos)
1003
- for (const o of e.fileInfos)
1004
- p(this, Y, ue).call(this, o);
1005
- typeof e.code == "string" && p(this, V, de).call(this, " ?>" + e.code), p(this, J, ce).call(this);
1006
- const n = await p(this, K, he).call(this);
1007
- if (e.throwOnError && n.exitCode !== 0) {
1008
- const o = {
1009
- stdout: n.text,
1010
- stderr: n.errors
1015
+ if (a(this, E) || (f(this, M, ee).call(this), h(this, E, !0)), e.scriptPath && !this.fileExists(e.scriptPath))
1016
+ throw new Error(
1017
+ `The script path "${e.scriptPath}" does not exist.`
1018
+ );
1019
+ f(this, j, ae).call(this, e.scriptPath || ""), f(this, L, re).call(this, e.relativeUri || ""), f(this, D, ne).call(this, e.method || "GET");
1020
+ const s = normalizeHeaders(e.headers || {}), n = s.host || "example.com:443";
1021
+ f(this, $, se).call(this, n, e.protocol || "http"), f(this, q, ie).call(this, s), e.body && (r = f(this, W, oe).call(this, e.body)), typeof e.code == "string" && f(this, J, ue).call(this, " ?>" + e.code), f(this, z, le).call(this);
1022
+ const i = e.env || {};
1023
+ for (const l in i)
1024
+ f(this, G, ce).call(this, l, i[l]);
1025
+ const o = await f(this, Q, de).call(this);
1026
+ if (e.throwOnError && o.exitCode !== 0) {
1027
+ const l = {
1028
+ stdout: o.text,
1029
+ stderr: o.errors
1011
1030
  };
1012
- console.warn("PHP.run() output was:", o);
1031
+ console.warn("PHP.run() output was:", l);
1013
1032
  const c = new Error(
1014
- `PHP.run() failed with exit code ${n.exitCode} and the following output`
1033
+ `PHP.run() failed with exit code ${o.exitCode} and the following output: ` + o.errors
1015
1034
  );
1016
- throw c.output = o, c;
1035
+ throw c.output = l, console.error(c), c;
1017
1036
  }
1018
- return n;
1037
+ return o;
1019
1038
  } finally {
1020
1039
  try {
1021
1040
  r && this[__private__dont__use].free(r);
@@ -1027,18 +1046,18 @@ class BasePHP {
1027
1046
  }
1028
1047
  }
1029
1048
  addServerGlobalEntry(e, t) {
1030
- a(this, v)[e] = t;
1049
+ a(this, P)[e] = t;
1031
1050
  }
1032
1051
  defineConstant(e, t) {
1033
1052
  let r = {};
1034
1053
  try {
1035
1054
  r = JSON.parse(
1036
- this.fileExists("/tmp/consts.json") && this.readFileAsText("/tmp/consts.json") || "{}"
1055
+ this.fileExists("/internal/consts.json") && this.readFileAsText("/internal/consts.json") || "{}"
1037
1056
  );
1038
1057
  } catch {
1039
1058
  }
1040
1059
  this.writeFile(
1041
- "/tmp/consts.json",
1060
+ "/internal/consts.json",
1042
1061
  JSON.stringify({
1043
1062
  ...r,
1044
1063
  [e]: t
@@ -1092,7 +1111,7 @@ class BasePHP {
1092
1111
  );
1093
1112
  if (t.prependPath) {
1094
1113
  const s = e.replace(/\/$/, "");
1095
- return r.map((i) => `${s}/${i}`);
1114
+ return r.map((n) => `${s}/${n}`);
1096
1115
  }
1097
1116
  return r;
1098
1117
  } catch (r) {
@@ -1123,9 +1142,9 @@ class BasePHP {
1123
1142
  this.exit();
1124
1143
  } catch {
1125
1144
  }
1126
- if (this.initializeRuntime(e), a(this, H) && this.setPhpIniPath(a(this, H)), a(this, A) && this.setSapiName(a(this, A)), this.requestHandler) {
1145
+ if (this.initializeRuntime(e), a(this, A) && this.setPhpIniPath(a(this, A)), a(this, k) && this.setSapiName(a(this, k)), this.requestHandler) {
1127
1146
  const r = this.documentRoot;
1128
- recreateMemFS(this[__private__dont__use].FS, t, r);
1147
+ copyFS(t, this[__private__dont__use].FS, r);
1129
1148
  }
1130
1149
  }
1131
1150
  exit(e = 0) {
@@ -1136,23 +1155,23 @@ class BasePHP {
1136
1155
  this[__private__dont__use]._exit(e);
1137
1156
  } catch {
1138
1157
  }
1139
- d(this, w, !1), d(this, P, null), delete this[__private__dont__use].onMessage, delete this[__private__dont__use];
1158
+ h(this, E, !1), h(this, g, null), delete this[__private__dont__use].onMessage, delete this[__private__dont__use];
1140
1159
  }
1141
1160
  }
1142
- T = new WeakMap(), H = new WeakMap(), A = new WeakMap(), w = new WeakMap(), P = new WeakMap(), v = new WeakMap(), g = new WeakMap(), I = new WeakMap(), U = new WeakSet(), te = function() {
1143
- if (this.setPhpIniEntry("auto_prepend_file", "/tmp/consts.php"), this.fileExists("/tmp/consts.php") || this.writeFile(
1144
- "/tmp/consts.php",
1161
+ S = new WeakMap(), A = new WeakMap(), k = new WeakMap(), E = new WeakMap(), g = new WeakMap(), P = new WeakMap(), w = new WeakMap(), H = new WeakMap(), M = new WeakSet(), ee = function() {
1162
+ if (this.setPhpIniEntry("auto_prepend_file", "/internal/consts.php"), this.fileExists("/internal/consts.php") || this.writeFile(
1163
+ "/internal/consts.php",
1145
1164
  `<?php
1146
- if(file_exists('/tmp/consts.json')) {
1147
- $consts = json_decode(file_get_contents('/tmp/consts.json'), true);
1165
+ if(file_exists('/internal/consts.json')) {
1166
+ $consts = json_decode(file_get_contents('/internal/consts.json'), true);
1148
1167
  foreach ($consts as $const => $value) {
1149
1168
  if (!defined($const) && is_scalar($value)) {
1150
1169
  define($const, $value);
1151
1170
  }
1152
1171
  }
1153
1172
  }`
1154
- ), a(this, T).length > 0) {
1155
- const e = a(this, T).map(([t, r]) => `${t}=${r}`).join(`
1173
+ ), a(this, S).length > 0) {
1174
+ const e = a(this, S).map(([t, r]) => `${t}=${r}`).join(`
1156
1175
  `) + `
1157
1176
 
1158
1177
  `;
@@ -1164,8 +1183,8 @@ T = new WeakMap(), H = new WeakMap(), A = new WeakMap(), w = new WeakMap(), P =
1164
1183
  );
1165
1184
  }
1166
1185
  this[__private__dont__use].ccall("php_wasm_init", null, [], []);
1167
- }, D = new WeakSet(), re = function() {
1168
- const e = "/tmp/headers.json";
1186
+ }, B = new WeakSet(), te = function() {
1187
+ const e = "/internal/headers.json";
1169
1188
  if (!this.fileExists(e))
1170
1189
  throw new Error(
1171
1190
  "SAPI Error: Could not find response headers file."
@@ -1174,14 +1193,14 @@ T = new WeakMap(), H = new WeakMap(), A = new WeakMap(), w = new WeakMap(), P =
1174
1193
  for (const s of t.headers) {
1175
1194
  if (!s.includes(": "))
1176
1195
  continue;
1177
- const i = s.indexOf(": "), n = s.substring(0, i).toLowerCase(), o = s.substring(i + 2);
1178
- n in r || (r[n] = []), r[n].push(o);
1196
+ const n = s.indexOf(": "), i = s.substring(0, n).toLowerCase(), o = s.substring(n + 2);
1197
+ i in r || (r[i] = []), r[i].push(o);
1179
1198
  }
1180
1199
  return {
1181
1200
  headers: r,
1182
1201
  httpStatusCode: t.status
1183
1202
  };
1184
- }, $ = new WeakSet(), se = function(e) {
1203
+ }, L = new WeakSet(), re = function(e) {
1185
1204
  if (this[__private__dont__use].ccall(
1186
1205
  "wasm_set_request_uri",
1187
1206
  null,
@@ -1196,7 +1215,7 @@ T = new WeakMap(), H = new WeakMap(), A = new WeakMap(), w = new WeakMap(), P =
1196
1215
  [t]
1197
1216
  );
1198
1217
  }
1199
- }, q = new WeakSet(), ne = function(e, t) {
1218
+ }, $ = new WeakSet(), se = function(e, t) {
1200
1219
  this[__private__dont__use].ccall(
1201
1220
  "wasm_set_request_host",
1202
1221
  null,
@@ -1214,14 +1233,14 @@ T = new WeakMap(), H = new WeakMap(), A = new WeakMap(), w = new WeakMap(), P =
1214
1233
  [NUMBER],
1215
1234
  [r]
1216
1235
  ), (t === "https" || !t && r === 443) && this.addServerGlobalEntry("HTTPS", "on");
1217
- }, W = new WeakSet(), ie = function(e) {
1236
+ }, D = new WeakSet(), ne = function(e) {
1218
1237
  this[__private__dont__use].ccall(
1219
1238
  "wasm_set_request_method",
1220
1239
  null,
1221
1240
  [STRING],
1222
1241
  [e]
1223
1242
  );
1224
- }, j = new WeakSet(), oe = function(e) {
1243
+ }, q = new WeakSet(), ie = function(e) {
1225
1244
  e.cookie && this[__private__dont__use].ccall(
1226
1245
  "wasm_set_cookies",
1227
1246
  null,
@@ -1245,98 +1264,100 @@ T = new WeakMap(), H = new WeakMap(), A = new WeakMap(), w = new WeakMap(), P =
1245
1264
  e[t]
1246
1265
  );
1247
1266
  }
1248
- }, z = new WeakSet(), ae = function(e) {
1249
- const t = this[__private__dont__use].lengthBytesUTF8(e), r = this[__private__dont__use].malloc(t + 1);
1250
- if (!r)
1267
+ }, W = new WeakSet(), oe = function(e) {
1268
+ let t, r;
1269
+ typeof e == "string" ? (console.warn(
1270
+ "Passing a string as the request body is deprecated. Please use a Uint8Array instead. See https://github.com/WordPress/wordpress-playground/issues/997 for more details"
1271
+ ), r = this[__private__dont__use].lengthBytesUTF8(e), t = r + 1) : (r = e.byteLength, t = e.byteLength);
1272
+ const s = this[__private__dont__use].malloc(t);
1273
+ if (!s)
1251
1274
  throw new Error("Could not allocate memory for the request body.");
1252
- return this[__private__dont__use].stringToUTF8(
1275
+ return typeof e == "string" ? this[__private__dont__use].stringToUTF8(
1253
1276
  e,
1254
- r,
1277
+ s,
1255
1278
  t + 1
1256
- ), this[__private__dont__use].ccall(
1279
+ ) : this[__private__dont__use].HEAPU8.set(e, s), this[__private__dont__use].ccall(
1257
1280
  "wasm_set_request_body",
1258
1281
  null,
1259
1282
  [NUMBER],
1260
- [r]
1283
+ [s]
1261
1284
  ), this[__private__dont__use].ccall(
1262
1285
  "wasm_set_content_length",
1263
1286
  null,
1264
1287
  [NUMBER],
1265
- [new TextEncoder().encode(e).length]
1266
- ), r;
1267
- }, G = new WeakSet(), le = function(e) {
1288
+ [r]
1289
+ ), s;
1290
+ }, j = new WeakSet(), ae = function(e) {
1268
1291
  this[__private__dont__use].ccall(
1269
1292
  "wasm_set_path_translated",
1270
1293
  null,
1271
1294
  [STRING],
1272
1295
  [e]
1273
1296
  );
1274
- }, J = new WeakSet(), ce = function() {
1275
- for (const e in a(this, v))
1297
+ }, z = new WeakSet(), le = function() {
1298
+ for (const e in a(this, P))
1276
1299
  this[__private__dont__use].ccall(
1277
1300
  "wasm_add_SERVER_entry",
1278
1301
  null,
1279
1302
  [STRING, STRING],
1280
- [e, a(this, v)[e]]
1303
+ [e, a(this, P)[e]]
1281
1304
  );
1282
- }, Y = new WeakSet(), ue = function(e) {
1283
- const { key: t, name: r, type: s, data: i } = e, n = `/tmp/${Math.random().toFixed(20)}`;
1284
- this.writeFile(n, i);
1285
- const o = 0;
1305
+ }, G = new WeakSet(), ce = function(e, t) {
1286
1306
  this[__private__dont__use].ccall(
1287
- "wasm_add_uploaded_file",
1307
+ "wasm_add_ENV_entry",
1288
1308
  null,
1289
- [STRING, STRING, STRING, STRING, NUMBER, NUMBER],
1290
- [t, r, s, n, o, i.byteLength]
1309
+ [STRING, STRING],
1310
+ [e, t]
1291
1311
  );
1292
- }, V = new WeakSet(), de = function(e) {
1312
+ }, J = new WeakSet(), ue = function(e) {
1293
1313
  this[__private__dont__use].ccall(
1294
1314
  "wasm_set_php_code",
1295
1315
  null,
1296
1316
  [STRING],
1297
1317
  [e]
1298
1318
  );
1299
- }, K = new WeakSet(), he = async function() {
1300
- var i;
1319
+ }, Q = new WeakSet(), de = async function() {
1320
+ var n;
1301
1321
  let e, t;
1302
1322
  try {
1303
- e = await new Promise((n, o) => {
1304
- var h;
1305
- t = (f) => {
1306
- const u = new Error("Rethrown");
1307
- u.cause = f.error, u.betterMessage = f.message, o(u);
1308
- }, (h = a(this, P)) == null || h.addEventListener(
1323
+ e = await new Promise((i, o) => {
1324
+ var c;
1325
+ t = (p) => {
1326
+ console.error(p), console.error(p.error);
1327
+ const d = new Error("Rethrown");
1328
+ d.cause = p.error, d.betterMessage = p.message, o(d);
1329
+ }, (c = a(this, g)) == null || c.addEventListener(
1309
1330
  "error",
1310
1331
  t
1311
1332
  );
1312
- const c = this[__private__dont__use].ccall(
1333
+ const l = this[__private__dont__use].ccall(
1313
1334
  "wasm_sapi_handle_request",
1314
1335
  NUMBER,
1315
1336
  [],
1316
1337
  [],
1317
1338
  { async: !0 }
1318
1339
  );
1319
- return c instanceof Promise ? c.then(n, o) : n(c);
1340
+ return l instanceof Promise ? l.then(i, o) : i(l);
1320
1341
  });
1321
- } catch (n) {
1322
- for (const f in this)
1323
- typeof this[f] == "function" && (this[f] = () => {
1342
+ } catch (i) {
1343
+ for (const p in this)
1344
+ typeof this[p] == "function" && (this[p] = () => {
1324
1345
  throw new Error(
1325
1346
  "PHP runtime has crashed – see the earlier error for details."
1326
1347
  );
1327
1348
  });
1328
1349
  this.functionsMaybeMissingFromAsyncify = getFunctionsMaybeMissingFromAsyncify();
1329
- const o = n, c = "betterMessage" in o ? o.betterMessage : o.message, h = new Error(c);
1330
- throw h.cause = o, h;
1350
+ const o = i, l = "betterMessage" in o ? o.betterMessage : o.message, c = new Error(l);
1351
+ throw c.cause = o, console.error(c), c;
1331
1352
  } finally {
1332
- (i = a(this, P)) == null || i.removeEventListener("error", t), d(this, v, {});
1353
+ (n = a(this, g)) == null || n.removeEventListener("error", t), h(this, P, {});
1333
1354
  }
1334
- const { headers: r, httpStatusCode: s } = p(this, D, re).call(this);
1355
+ const { headers: r, httpStatusCode: s } = f(this, B, te).call(this);
1335
1356
  return new PHPResponse(
1336
1357
  s,
1337
1358
  r,
1338
- this.readFileAsBuffer("/tmp/stdout"),
1339
- this.readFileAsText("/tmp/stderr"),
1359
+ this.readFileAsBuffer("/internal/stdout"),
1360
+ this.readFileAsText("/internal/stderr"),
1340
1361
  e
1341
1362
  );
1342
1363
  };
@@ -1376,67 +1397,62 @@ function normalizeHeaders(e) {
1376
1397
  t[r.toLowerCase()] = e[r];
1377
1398
  return t;
1378
1399
  }
1379
- function recreateMemFS(e, t, r) {
1400
+ function copyFS(e, t, r) {
1380
1401
  let s;
1381
1402
  try {
1382
- s = t.lookupPath(r);
1403
+ s = e.lookupPath(r);
1383
1404
  } catch {
1384
1405
  return;
1385
1406
  }
1386
1407
  if (!("contents" in s.node))
1387
1408
  return;
1388
- try {
1389
- e = e.lookupPath(r);
1390
- return;
1391
- } catch {
1392
- }
1393
- if (!t.isDir(s.node.mode)) {
1394
- e.writeFile(r, t.readFile(r));
1409
+ if (!e.isDir(s.node.mode)) {
1410
+ t.writeFile(r, e.readFile(r));
1395
1411
  return;
1396
1412
  }
1397
- e.mkdirTree(r);
1398
- const i = t.readdir(r).filter((n) => n !== "." && n !== "..");
1399
- for (const n of i)
1400
- recreateMemFS(e, t, joinPaths(r, n));
1413
+ t.mkdirTree(r);
1414
+ const n = e.readdir(r).filter((i) => i !== "." && i !== "..");
1415
+ for (const i of n)
1416
+ copyFS(e, t, joinPaths(r, i));
1401
1417
  }
1402
1418
  function journalFSEvents(e, t, r = () => {
1403
1419
  }) {
1404
1420
  function s() {
1405
1421
  t = normalizePath(t);
1406
- const n = e[__private__dont__use].FS, o = createFSHooks(n, (u) => {
1407
- if (u.path.startsWith(t))
1408
- r(u);
1409
- else if (u.operation === "RENAME" && u.toPath.startsWith(t))
1422
+ const i = e[__private__dont__use].FS, o = createFSHooks(i, (d) => {
1423
+ if (d.path.startsWith(t))
1424
+ r(d);
1425
+ else if (d.operation === "RENAME" && d.toPath.startsWith(t))
1410
1426
  for (const _ of recordExistingPath(
1411
1427
  e,
1412
- u.path,
1413
- u.toPath
1428
+ d.path,
1429
+ d.toPath
1414
1430
  ))
1415
1431
  r(_);
1416
- }), c = {};
1417
- for (const [u] of Object.entries(o))
1418
- c[u] = n[u];
1419
- function h() {
1420
- for (const [u, _] of Object.entries(o))
1421
- n[u] = function(...y) {
1422
- return _(...y), c[u].apply(this, y);
1432
+ }), l = {};
1433
+ for (const [d] of Object.entries(o))
1434
+ l[d] = i[d];
1435
+ function c() {
1436
+ for (const [d, _] of Object.entries(o))
1437
+ i[d] = function(...V) {
1438
+ return _(...V), l[d].apply(this, V);
1423
1439
  };
1424
1440
  }
1425
- function f() {
1426
- for (const [u, _] of Object.entries(c))
1427
- e[__private__dont__use].FS[u] = _;
1441
+ function p() {
1442
+ for (const [d, _] of Object.entries(l))
1443
+ e[__private__dont__use].FS[d] = _;
1428
1444
  }
1429
1445
  e[__private__dont__use].journal = {
1430
- bind: h,
1431
- unbind: f
1432
- }, h();
1446
+ bind: c,
1447
+ unbind: p
1448
+ }, c();
1433
1449
  }
1434
1450
  e.addEventListener("runtime.initialized", s), e[__private__dont__use] && s();
1435
- function i() {
1451
+ function n() {
1436
1452
  e[__private__dont__use].journal.unbind(), delete e[__private__dont__use].journal;
1437
1453
  }
1438
- return e.addEventListener("runtime.beforedestroy", i), function() {
1439
- return e.removeEventListener("runtime.initialized", s), e.removeEventListener("runtime.beforedestroy", i), e[__private__dont__use].journal.unbind();
1454
+ return e.addEventListener("runtime.beforedestroy", n), function() {
1455
+ return e.removeEventListener("runtime.initialized", s), e.removeEventListener("runtime.beforedestroy", n), e[__private__dont__use].journal.unbind();
1440
1456
  };
1441
1457
  }
1442
1458
  const createFSHooks = (e, t = () => {
@@ -1488,16 +1504,16 @@ const createFSHooks = (e, t = () => {
1488
1504
  },
1489
1505
  rename(r, s) {
1490
1506
  try {
1491
- const i = e.lookupPath(r, {
1507
+ const n = e.lookupPath(r, {
1492
1508
  follow: !0
1493
- }), n = e.lookupPath(s, {
1509
+ }), i = e.lookupPath(s, {
1494
1510
  parent: !0
1495
1511
  }).path;
1496
1512
  t({
1497
1513
  operation: "RENAME",
1498
- nodeType: e.isDir(i.node.mode) ? "directory" : "file",
1499
- path: i.path,
1500
- toPath: joinPaths(n, basename(s))
1514
+ nodeType: e.isDir(n.node.mode) ? "directory" : "file",
1515
+ path: n.path,
1516
+ toPath: joinPaths(i, basename(s))
1501
1517
  });
1502
1518
  } catch {
1503
1519
  }
@@ -1543,52 +1559,52 @@ function normalizeFilesystemOperations(e) {
1543
1559
  const t = {};
1544
1560
  for (let r = e.length - 1; r >= 0; r--) {
1545
1561
  for (let s = r - 1; s >= 0; s--) {
1546
- const i = checkRelationship(e[r], e[s]);
1547
- if (i === "none")
1562
+ const n = checkRelationship(e[r], e[s]);
1563
+ if (n === "none")
1548
1564
  continue;
1549
- const n = e[r], o = e[s];
1550
- if (n.operation === "RENAME" && o.operation === "RENAME") {
1565
+ const i = e[r], o = e[s];
1566
+ if (i.operation === "RENAME" && o.operation === "RENAME") {
1551
1567
  console.warn(
1552
1568
  "[FS Journal] Normalizing a double rename is not yet supported:",
1553
1569
  {
1554
- current: n,
1570
+ current: i,
1555
1571
  last: o
1556
1572
  }
1557
1573
  );
1558
1574
  continue;
1559
1575
  }
1560
- (o.operation === "CREATE" || o.operation === "WRITE") && (n.operation === "RENAME" ? i === "same_node" ? (t[s] = [], t[r] = [
1576
+ (o.operation === "CREATE" || o.operation === "WRITE") && (i.operation === "RENAME" ? n === "same_node" ? (t[s] = [], t[r] = [
1561
1577
  {
1562
1578
  ...o,
1563
- path: n.toPath
1579
+ path: i.toPath
1564
1580
  },
1565
1581
  ...t[r] || []
1566
- ]) : i === "descendant" && (t[s] = [], t[r] = [
1582
+ ]) : n === "descendant" && (t[s] = [], t[r] = [
1567
1583
  {
1568
1584
  ...o,
1569
1585
  path: joinPaths(
1570
- n.toPath,
1571
- o.path.substring(n.path.length)
1586
+ i.toPath,
1587
+ o.path.substring(i.path.length)
1572
1588
  )
1573
1589
  },
1574
1590
  ...t[r] || []
1575
- ]) : n.operation === "WRITE" && i === "same_node" ? t[s] = [] : n.operation === "DELETE" && i === "same_node" && (t[s] = [], t[r] = []));
1591
+ ]) : i.operation === "WRITE" && n === "same_node" ? t[s] = [] : i.operation === "DELETE" && n === "same_node" && (t[s] = [], t[r] = []));
1576
1592
  }
1577
1593
  if (Object.entries(t).length > 0) {
1578
- const s = e.flatMap((i, n) => n in t ? t[n] : [i]);
1594
+ const s = e.flatMap((n, i) => i in t ? t[i] : [n]);
1579
1595
  return normalizeFilesystemOperations(s);
1580
1596
  }
1581
1597
  }
1582
1598
  return e;
1583
1599
  }
1584
1600
  function checkRelationship(e, t) {
1585
- const r = e.path, s = e.operation !== "WRITE" && e.nodeType === "directory", i = t.operation !== "WRITE" && t.nodeType === "directory", n = t.operation === "RENAME" ? t.toPath : t.path;
1586
- return n === r ? "same_node" : i && r.startsWith(n + "/") ? "ancestor" : s && n.startsWith(r + "/") ? "descendant" : "none";
1601
+ const r = e.path, s = e.operation !== "WRITE" && e.nodeType === "directory", n = t.operation !== "WRITE" && t.nodeType === "directory", i = t.operation === "RENAME" ? t.toPath : t.path;
1602
+ return i === r ? "same_node" : n && r.startsWith(i + "/") ? "ancestor" : s && i.startsWith(r + "/") ? "descendant" : "none";
1587
1603
  }
1588
1604
  async function hydrateUpdateFileOps(e, t) {
1589
1605
  const s = t.filter(
1590
- (i) => i.operation === "WRITE"
1591
- ).map((i) => hydrateOp(e, i));
1606
+ (n) => n.operation === "WRITE"
1607
+ ).map((n) => hydrateOp(e, n));
1592
1608
  return await Promise.all(s), t;
1593
1609
  }
1594
1610
  const hydrateLock = new Semaphore({ concurrency: 15 });