@aiquants/markdown-explorer 0.14.0 → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +111 -0
- package/README.md +60 -12
- package/dist/EditSurface-CwKsTS11.cjs +2 -0
- package/dist/EditSurface-DGMLY7Gh.js +786 -0
- package/dist/ExplorerSplit-BLOaU5fE.cjs +1 -0
- package/dist/{ExplorerSplit-BPLECNpv.js → ExplorerSplit-tUCp0XdJ.js} +613 -592
- package/dist/MultiLayout-BBP2ajz8.cjs +1 -0
- package/dist/MultiLayout-DYy66vcO.js +963 -0
- package/dist/{NativeScrollBar-BUWXPvTV.cjs → NativeScrollBar-BkFg1EcW.cjs} +1 -1
- package/dist/{NativeScrollBar-BFF12oEd.js → NativeScrollBar-Dazd542j.js} +1 -1
- package/dist/TreeLayout-C8s5lBH_.cjs +1 -0
- package/dist/TreeLayout-CXX-rFO6.js +72 -0
- package/dist/client/storage.d.cts +1 -0
- package/dist/client/storage.d.ts +1 -0
- package/dist/core/geometry.d.cts +20 -1
- package/dist/core/geometry.d.ts +20 -1
- package/dist/core/labels.d.cts +7 -0
- package/dist/core/labels.d.ts +7 -0
- package/dist/core/location.d.cts +3 -0
- package/dist/core/location.d.ts +3 -0
- package/dist/core/treeSettings.d.cts +3 -1
- package/dist/core/treeSettings.d.ts +3 -1
- package/dist/index-DTKGgaok.js +3613 -0
- package/dist/index-DffJZ8-y.cjs +13 -0
- package/dist/index.cjs +1 -1
- package/dist/index.js +25 -25
- package/dist/location-D85EXiPt.cjs +1 -0
- package/dist/location-G7psbmv_.js +499 -0
- package/dist/{merge-Bi2VedIr.js → merge-BkFru0eg.js} +5 -5
- package/dist/{merge-BFk7nT59.cjs → merge-SRws1-Zj.cjs} +1 -1
- package/dist/server/ports.d.cts +8 -1
- package/dist/server/ports.d.ts +8 -1
- package/dist/server.cjs +1 -1
- package/dist/server.d.cts +1 -1
- package/dist/server.d.ts +1 -1
- package/dist/server.js +512 -499
- package/dist/{storage-BSthKLSG.js → storage-CmGHg7OA.js} +11 -10
- package/dist/storage-DM0EvkVf.cjs +1 -0
- package/dist/storage.cjs +1 -1
- package/dist/storage.js +1 -1
- package/dist/styles/markdown-explorer.css +1 -1
- package/dist/useSplitPersistence-B1b5Lg5E.js +42 -0
- package/dist/useSplitPersistence-DRjsPZdH.cjs +1 -0
- package/docs/behavior/dom-hooks.md +15 -10
- package/docs/behavior/layout.md +53 -14
- package/docs/behavior/localization.md +2 -1
- package/docs/behavior/url-contract.md +18 -6
- package/package.json +12 -10
- package/dist/EditSurface-BLlu5zEt.cjs +0 -2
- package/dist/EditSurface-CF2io496.js +0 -705
- package/dist/ExplorerSplit-Cjvy0JHP.cjs +0 -1
- package/dist/MultiLayout-BjA0k-5n.cjs +0 -1
- package/dist/MultiLayout-D8F0BfxA.js +0 -947
- package/dist/TreeLayout-CQUpgsxz.cjs +0 -1
- package/dist/TreeLayout-rhyVTfrI.js +0 -29
- package/dist/index-Dn7cYy-4.cjs +0 -13
- package/dist/index-v997iKxe.js +0 -3464
- package/dist/location-D6RE3wAN.cjs +0 -1
- package/dist/location-IRD9h42r.js +0 -444
- package/dist/storage-Dgi4JXaB.cjs +0 -1
package/dist/server.js
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import { b as Tt, c as nr, f as ke, g as or, h as sr, j as ir, k as ar, E as kt, l as he, r as cr, m as ur, i as q, n as dr, o as lr, p as fr, q as St, s as mr, t as pr, u as
|
|
2
|
-
import { D as
|
|
1
|
+
import { b as Tt, c as nr, f as ke, g as or, h as sr, j as ir, k as ar, E as kt, l as he, r as cr, m as ur, i as q, n as dr, o as lr, p as fr, q as St, s as mr, t as pr, u as je, v as wr, w as hr, x as yr, y as ae, z as Q, e as Ue, A as gr, B as Er } from "./dataRoute-CObbjNxG.js";
|
|
2
|
+
import { D as is, M as as, C as cs } from "./dataRoute-CObbjNxG.js";
|
|
3
3
|
import { createHash as br, createHmac as Tr } from "node:crypto";
|
|
4
4
|
import { createLinkCardHandler as kr } from "@aiquants/markdown/server";
|
|
5
5
|
import { crossOriginRefusal as xt, NO_CROSS_ORIGIN_NAVIGATION as Ot } from "@aiquants/request-isolation";
|
|
6
|
-
import { data as
|
|
7
|
-
import { w as xr,
|
|
8
|
-
import { existsSync as Mr, statSync as
|
|
9
|
-
import { extname as jr, isAbsolute as
|
|
6
|
+
import { data as Xe, redirect as Sr } from "react-router";
|
|
7
|
+
import { w as xr, f as Or, r as Rr, i as vr, g as _r, p as Ar, h as Nr, j as Ir, N as Pr } from "./location-G7psbmv_.js";
|
|
8
|
+
import { existsSync as Mr, statSync as Lr, realpathSync as $r, constants as me } from "node:fs";
|
|
9
|
+
import { extname as jr, isAbsolute as Je, relative as Ge, sep as Ye, join as We, resolve as zr } from "node:path";
|
|
10
10
|
import { fileURLToPath as Re } from "node:url";
|
|
11
11
|
import { Worker as Dr } from "node:worker_threads";
|
|
12
12
|
import { open as Rt, lstat as vt, realpath as Cr, readdir as Hr } from "node:fs/promises";
|
|
13
13
|
import { Readable as Br } from "node:stream";
|
|
14
|
-
const
|
|
15
|
-
authenticate: async () => ({ status: "signed-in", principal:
|
|
16
|
-
principalKey: () =>
|
|
14
|
+
const Ke = Object.freeze({ kind: "anonymous" }), ts = Object.freeze({
|
|
15
|
+
authenticate: async () => ({ status: "signed-in", principal: Ke }),
|
|
16
|
+
principalKey: () => Ke.kind,
|
|
17
17
|
signedOutPage: (e, t) => new Response(null, { status: 401, headers: t })
|
|
18
18
|
}), Fr = "application/octet-stream", _t = "application/pdf", Ur = /* @__PURE__ */ new Set([
|
|
19
19
|
"image/png",
|
|
@@ -42,17 +42,17 @@ const We = Object.freeze({ kind: "anonymous" }), Qo = Object.freeze({
|
|
|
42
42
|
"video/ogg",
|
|
43
43
|
"video/quicktime",
|
|
44
44
|
_t
|
|
45
|
-
]), At = "default-src 'none'; media-src 'self'; style-src 'unsafe-inline'", Nt = `${At}; sandbox`, Xr = `${At}; sandbox allow-same-origin`,
|
|
45
|
+
]), At = "default-src 'none'; media-src 'self'; style-src 'unsafe-inline'", Nt = `${At}; sandbox`, Xr = `${At}; sandbox allow-same-origin`, Ve = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/u, Jr = /^[\t\n\r ]+|[\t\n\r ]+$/gu, Me = /^[\t ]+|[\t ]+$/gu, Gr = (e) => {
|
|
46
46
|
const t = [];
|
|
47
47
|
let r = "", n = 0;
|
|
48
48
|
for (; ; ) {
|
|
49
49
|
for (; n < e.length && e[n] !== '"' && e[n] !== ","; )
|
|
50
50
|
r += e[n], n += 1;
|
|
51
51
|
if (n < e.length && e[n] === '"') {
|
|
52
|
-
const
|
|
52
|
+
const i = n;
|
|
53
53
|
for (n += 1; n < e.length && e[n] !== '"'; )
|
|
54
54
|
n += e[n] === "\\" ? 2 : 1;
|
|
55
|
-
if (n = Math.min(n + 1, e.length), r += e.slice(
|
|
55
|
+
if (n = Math.min(n + 1, e.length), r += e.slice(i, n), n < e.length) continue;
|
|
56
56
|
}
|
|
57
57
|
if (t.push(r.replace(Me, "")), r = "", n >= e.length) return t;
|
|
58
58
|
n += 1;
|
|
@@ -60,8 +60,8 @@ const We = Object.freeze({ kind: "anonymous" }), Qo = Object.freeze({
|
|
|
60
60
|
}, Yr = (e) => {
|
|
61
61
|
const t = e.replace(Jr, ""), r = t.indexOf("/");
|
|
62
62
|
if (r < 0) return null;
|
|
63
|
-
const n = t.slice(0, r),
|
|
64
|
-
return
|
|
63
|
+
const n = t.slice(0, r), i = t.indexOf(";", r + 1), a = t.slice(r + 1, i < 0 ? t.length : i).replace(/[\t\n\r ]+$/u, "");
|
|
64
|
+
return Ve.test(n) && Ve.test(a) ? `${n}/${a}`.toLowerCase() : null;
|
|
65
65
|
}, Wr = (e) => {
|
|
66
66
|
let t = null;
|
|
67
67
|
for (const r of Gr(e)) {
|
|
@@ -73,14 +73,14 @@ const We = Object.freeze({ kind: "anonymous" }), Qo = Object.freeze({
|
|
|
73
73
|
"Cache-Control": "private, max-age=60, stale-while-revalidate=600, no-transform",
|
|
74
74
|
Vary: "Cookie"
|
|
75
75
|
}), Zr = (e, t) => {
|
|
76
|
-
const r = Wr(e), n = r !== null && Ur.has(r),
|
|
76
|
+
const r = Wr(e), n = r !== null && Ur.has(r), i = n && (r.startsWith("audio/") || r.startsWith("video/"));
|
|
77
77
|
return {
|
|
78
78
|
contentType: r === null ? Fr : r,
|
|
79
79
|
inline: n,
|
|
80
80
|
headers: {
|
|
81
81
|
"X-Content-Type-Options": "nosniff",
|
|
82
82
|
"Content-Disposition": `${n ? "inline" : "attachment"}; filename*=UTF-8''${It(t)}`,
|
|
83
|
-
...r === _t ? {} : { "Content-Security-Policy":
|
|
83
|
+
...r === _t ? {} : { "Content-Security-Policy": i ? Xr : Nt },
|
|
84
84
|
"Cross-Origin-Resource-Policy": "same-origin"
|
|
85
85
|
}
|
|
86
86
|
};
|
|
@@ -93,87 +93,94 @@ const We = Object.freeze({ kind: "anonymous" }), Qo = Object.freeze({
|
|
|
93
93
|
"Content-Security-Policy": Nt,
|
|
94
94
|
"Cross-Origin-Resource-Policy": "same-origin"
|
|
95
95
|
}
|
|
96
|
-
}), en = /^bytes=(?:(\d+)-(\d*)|-(\d+))$/iu, ge = Object.freeze({ kind: "whole" }),
|
|
96
|
+
}), en = /^bytes=(?:(\d+)-(\d*)|-(\d+))$/iu, ge = Object.freeze({ kind: "whole" }), Ze = Object.freeze({ kind: "unsatisfiable" }), tn = (e, t, r) => {
|
|
97
97
|
const n = e.get("Range");
|
|
98
98
|
if (n === null) return ge;
|
|
99
|
-
const
|
|
100
|
-
if (
|
|
101
|
-
const
|
|
102
|
-
if (
|
|
103
|
-
const [, c, m, h] =
|
|
99
|
+
const i = e.get("If-Range");
|
|
100
|
+
if (i !== null && i !== r) return ge;
|
|
101
|
+
const a = en.exec(n);
|
|
102
|
+
if (a === null) return ge;
|
|
103
|
+
const [, c, m, h] = a;
|
|
104
104
|
if (h !== void 0) {
|
|
105
105
|
const y = Number(h);
|
|
106
|
-
if (y === 0) return
|
|
106
|
+
if (y === 0) return Ze;
|
|
107
107
|
const k = Math.max(0, t - y);
|
|
108
108
|
return { kind: "partial", range: { offset: k, length: t - k } };
|
|
109
109
|
}
|
|
110
|
-
const
|
|
111
|
-
return w <
|
|
112
|
-
},
|
|
110
|
+
const x = Number(c), w = m === "" ? Number.POSITIVE_INFINITY : Number(m);
|
|
111
|
+
return w < x ? ge : x >= t ? Ze : { kind: "partial", range: { offset: x, length: Math.min(w, t - 1) - x + 1 } };
|
|
112
|
+
}, Qe = "W/", rn = (e) => {
|
|
113
113
|
const t = [];
|
|
114
114
|
let r = 0, n = !1;
|
|
115
|
-
for (let
|
|
116
|
-
e[
|
|
115
|
+
for (let i = 0; i < e.length; i += 1)
|
|
116
|
+
e[i] === '"' ? n = !n : e[i] === "," && !n && (t.push(e.slice(r, i).replace(Me, "")), r = i + 1);
|
|
117
117
|
return t.push(e.slice(r).replace(Me, "")), t;
|
|
118
|
-
},
|
|
118
|
+
}, qe = (e) => e.startsWith(Qe) ? e.slice(Qe.length) : e, nn = (e, t) => {
|
|
119
119
|
if (e === null) return !1;
|
|
120
120
|
const r = rn(e);
|
|
121
121
|
if (r.length === 1 && r[0] === "*") return !0;
|
|
122
|
-
const n =
|
|
123
|
-
return r.some((
|
|
122
|
+
const n = qe(t);
|
|
123
|
+
return r.some((i) => qe(i) === n);
|
|
124
124
|
}, on = (e, t) => {
|
|
125
125
|
let r = t;
|
|
126
126
|
return e.pipeThrough(
|
|
127
127
|
new TransformStream({
|
|
128
|
-
transform: (n,
|
|
129
|
-
const
|
|
130
|
-
r -=
|
|
128
|
+
transform: (n, i) => {
|
|
129
|
+
const a = n.byteLength > r ? n.subarray(0, r) : n;
|
|
130
|
+
r -= a.byteLength, i.enqueue(a), r === 0 && i.terminate();
|
|
131
131
|
},
|
|
132
132
|
flush: (n) => {
|
|
133
133
|
r > 0 && n.error(new RangeError("[markdown-explorer] the asset ended before its declared length"));
|
|
134
134
|
}
|
|
135
135
|
})
|
|
136
136
|
);
|
|
137
|
-
}, ve = async (e, t, r, n,
|
|
137
|
+
}, ve = async (e, t, r, n, i) => {
|
|
138
138
|
if (n !== null && r.set("Content-Length", String(n)), e.method.toUpperCase() === "HEAD") return new Response(null, { status: t, headers: r });
|
|
139
|
-
const
|
|
140
|
-
return new Response(n === null ?
|
|
139
|
+
const a = await i();
|
|
140
|
+
return new Response(n === null ? a : on(a, n), { status: t, headers: r });
|
|
141
141
|
}, sn = (e, t) => {
|
|
142
142
|
const r = new Headers(e);
|
|
143
|
-
for (const [n,
|
|
143
|
+
for (const [n, i] of Object.entries({ "Accept-Ranges": "bytes", "Content-Range": `bytes */${t}`, "Cache-Control": "private, no-store", "X-Content-Type-Options": "nosniff" })) r.set(n, i);
|
|
144
144
|
return new Response(null, { status: 416, headers: r });
|
|
145
145
|
}, an = async (e, t, r, n) => {
|
|
146
|
-
const
|
|
147
|
-
for (const [
|
|
148
|
-
if (nn(e.headers.get("If-None-Match"),
|
|
149
|
-
if (c.set("Content-Type",
|
|
146
|
+
const i = `"${t.version}"`, a = n === "text" ? qr(t.name) : Zr(t.contentType, t.name), c = new Headers(r);
|
|
147
|
+
for (const [x, w] of Object.entries({ ...a.headers, ...Vr, ETag: i })) c.set(x, w);
|
|
148
|
+
if (nn(e.headers.get("If-None-Match"), i)) return new Response(null, { status: 304, headers: c });
|
|
149
|
+
if (c.set("Content-Type", a.contentType), t.size === null) return ve(e, 200, c, null, () => t.open(null));
|
|
150
150
|
if (t.size === 0)
|
|
151
151
|
return c.set("Content-Length", "0"), new Response(null, { status: 200, headers: c });
|
|
152
152
|
c.set("Accept-Ranges", "bytes");
|
|
153
|
-
const m = tn(e.headers, t.size,
|
|
153
|
+
const m = tn(e.headers, t.size, i);
|
|
154
154
|
if (m.kind === "unsatisfiable") return sn(r, t.size);
|
|
155
155
|
if (m.kind === "whole") return ve(e, 200, c, t.size, () => t.open(null));
|
|
156
156
|
const { range: h } = m;
|
|
157
157
|
return c.set("Content-Range", `bytes ${h.offset}-${h.offset + h.length - 1}/${t.size}`), ve(e, 206, c, h.length, () => t.open(h));
|
|
158
|
+
}, cn = Object.freeze({
|
|
159
|
+
"Cache-Control": "private, no-store",
|
|
160
|
+
"Referrer-Policy": "no-referrer"
|
|
161
|
+
}), un = (e, t) => {
|
|
162
|
+
const r = new Headers(t);
|
|
163
|
+
for (const [n, i] of Object.entries(cn)) r.set(n, i);
|
|
164
|
+
return r.set("Location", e), new Response(null, { status: 302, headers: r });
|
|
158
165
|
}, _e = (e, t) => {
|
|
159
166
|
const r = new AbortController(), n = t(AbortSignal.any([e.signal, r.signal]));
|
|
160
|
-
return new Promise((
|
|
161
|
-
let
|
|
167
|
+
return new Promise((i) => {
|
|
168
|
+
let a;
|
|
162
169
|
const c = setTimeout(() => {
|
|
163
|
-
|
|
170
|
+
i(e.late), a = setTimeout(() => r.abort(), e.handOverMs);
|
|
164
171
|
}, e.timeoutMs), m = () => {
|
|
165
|
-
clearTimeout(c), clearTimeout(
|
|
172
|
+
clearTimeout(c), clearTimeout(a);
|
|
166
173
|
};
|
|
167
174
|
n.then(
|
|
168
175
|
(h) => {
|
|
169
|
-
m(),
|
|
176
|
+
m(), i(h);
|
|
170
177
|
},
|
|
171
178
|
() => {
|
|
172
|
-
m(),
|
|
179
|
+
m(), i(e.late);
|
|
173
180
|
}
|
|
174
181
|
);
|
|
175
182
|
});
|
|
176
|
-
},
|
|
183
|
+
}, dn = Object.freeze({
|
|
177
184
|
error: (e, t) => {
|
|
178
185
|
console.error(e, t);
|
|
179
186
|
}
|
|
@@ -187,113 +194,113 @@ class Se extends Error {
|
|
|
187
194
|
super(`[markdown-explorer] ${t}`, r.cause === void 0 ? void 0 : { cause: r.cause }), this.name = "ExplorerError", this.reason = t, this.code = r.code, Object.defineProperty(this, Pt, { value: !0 });
|
|
188
195
|
}
|
|
189
196
|
}
|
|
190
|
-
const
|
|
197
|
+
const ln = (e) => e instanceof Error && Object.hasOwn(e, Pt) && Tt(e.reason), fn = (e) => typeof e == "object" && e !== null && e.name === "AbortError", ze = (e, t, r) => {
|
|
191
198
|
try {
|
|
192
199
|
e.error(t, r);
|
|
193
200
|
} catch (n) {
|
|
194
201
|
console.error("[markdown-explorer] the logger threw while reporting a failure", { operation: r.operation, error: n });
|
|
195
202
|
}
|
|
196
|
-
}, oe = (e, t) =>
|
|
203
|
+
}, oe = (e, t) => ze(e, `[markdown-explorer] ${t.operation} failed`, t), mn = (e, t) => ze(e, `[markdown-explorer] ${t.operation} left out an entry`, t), Ae = (e, t) => ze(e, `[markdown-explorer] ${t.operation} refused a path the deny rules match`, { ...t, error: new RangeError("[markdown-explorer] the path matches a deny rule") }), et = (e, t) => {
|
|
197
204
|
e === null || typeof e != "object" && typeof e != "function" || Promise.resolve(e).then(void 0, t);
|
|
198
|
-
}, G = (e, t, r, n) =>
|
|
205
|
+
}, G = (e, t, r, n) => ln(e) ? ke(e.reason, e.code) : (fn(e) || r.aborted || oe(n, { ...t, error: e }), { reason: "failed" }), tt = 2 * 1024 * 1024, rt = 256 * 1024 * 1024, pn = async (e, t) => {
|
|
199
206
|
const r = e.getReader(), n = new Uint8Array(t);
|
|
200
|
-
let
|
|
201
|
-
for (;
|
|
202
|
-
const { done:
|
|
203
|
-
if (
|
|
204
|
-
const m = c.subarray(0, t -
|
|
205
|
-
n.set(m,
|
|
207
|
+
let i = 0;
|
|
208
|
+
for (; i < t; ) {
|
|
209
|
+
const { done: a, value: c } = await r.read();
|
|
210
|
+
if (a) return n.subarray(0, i);
|
|
211
|
+
const m = c.subarray(0, t - i);
|
|
212
|
+
n.set(m, i), i += m.byteLength;
|
|
206
213
|
}
|
|
207
214
|
return r.cancel().catch(() => {
|
|
208
215
|
}), n;
|
|
209
|
-
},
|
|
216
|
+
}, wn = async (e, t) => {
|
|
210
217
|
if (e.size === 0) return { text: "", truncated: !1 };
|
|
211
|
-
const r = e.size !== null && e.size > t, n = e.size === null ? t + 1 : Math.min(e.size, t),
|
|
212
|
-
if (e.size !== null &&
|
|
213
|
-
const
|
|
214
|
-
return c.includes(0) ? null : { text: new TextDecoder("utf-8").decode(c, { stream:
|
|
215
|
-
},
|
|
218
|
+
const r = e.size !== null && e.size > t, n = e.size === null ? t + 1 : Math.min(e.size, t), i = await pn(await e.open(r ? { offset: 0, length: t } : null), n);
|
|
219
|
+
if (e.size !== null && i.byteLength < n) throw new RangeError("[markdown-explorer] the file ended before the length its asset port reported");
|
|
220
|
+
const a = r || i.byteLength > t, c = a ? i.subarray(0, t) : i;
|
|
221
|
+
return c.includes(0) ? null : { text: new TextDecoder("utf-8").decode(c, { stream: a }), truncated: a };
|
|
222
|
+
}, nt = (e, t) => {
|
|
216
223
|
if (!Number.isSafeInteger(t) || t <= 0) throw new RangeError(`[markdown-explorer] a cache ${e} must be a positive integer; got ${String(t)}`);
|
|
217
224
|
}, Mt = (e, t) => {
|
|
218
|
-
|
|
225
|
+
nt("capacity", e), t !== void 0 && nt("weight budget", t.max);
|
|
219
226
|
const r = /* @__PURE__ */ new Map();
|
|
220
227
|
let n = 0;
|
|
221
|
-
const
|
|
222
|
-
const c = r.get(
|
|
223
|
-
c !== void 0 && (r.delete(
|
|
228
|
+
const i = (a) => {
|
|
229
|
+
const c = r.get(a);
|
|
230
|
+
c !== void 0 && (r.delete(a), n -= c.weight);
|
|
224
231
|
};
|
|
225
232
|
return {
|
|
226
|
-
get: (
|
|
227
|
-
const c = r.get(
|
|
233
|
+
get: (a) => {
|
|
234
|
+
const c = r.get(a);
|
|
228
235
|
if (c !== void 0)
|
|
229
|
-
return r.delete(
|
|
236
|
+
return r.delete(a), r.set(a, c), c.value;
|
|
230
237
|
},
|
|
231
|
-
set: (
|
|
232
|
-
a
|
|
233
|
-
const m = t === void 0 ? 0 : t.of(
|
|
238
|
+
set: (a, c) => {
|
|
239
|
+
i(a);
|
|
240
|
+
const m = t === void 0 ? 0 : t.of(a, c);
|
|
234
241
|
if (!(t !== void 0 && m > t.max)) {
|
|
235
|
-
r.set(
|
|
242
|
+
r.set(a, { value: c, weight: m }), n += m;
|
|
236
243
|
for (const h of r.keys()) {
|
|
237
244
|
if (r.size <= e && (t === void 0 || n <= t.max)) break;
|
|
238
|
-
|
|
245
|
+
i(h);
|
|
239
246
|
}
|
|
240
247
|
}
|
|
241
248
|
},
|
|
242
|
-
delete:
|
|
249
|
+
delete: i,
|
|
243
250
|
size: () => r.size,
|
|
244
251
|
weight: () => n
|
|
245
252
|
};
|
|
246
|
-
},
|
|
253
|
+
}, Lt = (e) => Math.max(1, e - 1), hn = 2147483647, ot = Object.freeze({ status: "aborted" }), ue = (e, t, r) => {
|
|
247
254
|
const n = (e.get(t) ?? 0) + r;
|
|
248
255
|
n === 0 ? e.delete(t) : e.set(t, n);
|
|
249
256
|
}, le = (e, t, r) => {
|
|
250
257
|
if (!Number.isSafeInteger(t) || t <= 0 || t > r) throw new RangeError(`[markdown-explorer] a worker pool's ${e} must be an integer in 1..${String(r)}; got ${String(t)}`);
|
|
251
|
-
},
|
|
252
|
-
le("size", t, Number.MAX_SAFE_INTEGER), le("deadline", r,
|
|
253
|
-
const c = Object.freeze({ maxOldGenerationSizeMb:
|
|
258
|
+
}, yn = (e) => e !== null && typeof e == "object" && e.ready === !0 && !("id" in e), gn = (e, t) => e !== null && typeof e == "object" && e.id === t && "output" in e, En = ({ url: e, size: t, deadlineMs: r, jobsPerScope: n, maxQueuedJobs: i, heapLimits: a }) => {
|
|
259
|
+
le("size", t, Number.MAX_SAFE_INTEGER), le("deadline", r, hn), le("jobsPerScope", n, Number.MAX_SAFE_INTEGER), le("maxQueuedJobs", i, Number.MAX_SAFE_INTEGER), le("maxOldGenerationSizeMb", a.maxOldGenerationSizeMb, Number.MAX_SAFE_INTEGER), le("maxYoungGenerationSizeMb", a.maxYoungGenerationSizeMb, Number.MAX_SAFE_INTEGER);
|
|
260
|
+
const c = Object.freeze({ maxOldGenerationSizeMb: a.maxOldGenerationSizeMb, maxYoungGenerationSizeMb: a.maxYoungGenerationSizeMb }), m = /* @__PURE__ */ new Set(), h = [], x = /* @__PURE__ */ new Map(), w = /* @__PURE__ */ new Map(), y = Lt(t);
|
|
254
261
|
let k = 0;
|
|
255
262
|
const v = (p, g) => {
|
|
256
263
|
const { job: E } = p;
|
|
257
|
-
p.timer !== null && clearTimeout(p.timer), p.timer = null, p.job = null, p.sent = !1, E !== null && (ue(
|
|
258
|
-
},
|
|
264
|
+
p.timer !== null && clearTimeout(p.timer), p.timer = null, p.job = null, p.sent = !1, E !== null && (ue(x, E.scope, -1), ue(w, E.scope, -1), E.settle(g));
|
|
265
|
+
}, $ = (p) => {
|
|
259
266
|
const [g] = h.splice(p, 1);
|
|
260
267
|
g?.detach();
|
|
261
268
|
}, A = () => {
|
|
262
269
|
let p = -1, g = y, E = Number.POSITIVE_INFINITY;
|
|
263
270
|
for (const [O, T] of h.entries()) {
|
|
264
|
-
const
|
|
265
|
-
(
|
|
271
|
+
const I = x.get(T.scope) ?? 0, M = w.get(T.scope) ?? 0;
|
|
272
|
+
(I < g || I === g && p >= 0 && M < E) && (p = O, g = I, E = M);
|
|
266
273
|
}
|
|
267
274
|
return p;
|
|
268
275
|
}, R = (p, g) => {
|
|
269
276
|
m.has(p) && (m.delete(p), v(p, g), p.worker.terminate().catch(() => {
|
|
270
|
-
}),
|
|
271
|
-
},
|
|
277
|
+
}), F());
|
|
278
|
+
}, D = (p) => {
|
|
272
279
|
p.timer = setTimeout(() => {
|
|
273
280
|
R(p, { status: "timeout" });
|
|
274
281
|
}, r);
|
|
275
|
-
}, _ = (p, g) => p.sent ? { status: "crashed", error: g } : { status: "unavailable", error: g },
|
|
276
|
-
p.sent = !0,
|
|
282
|
+
}, _ = (p, g) => p.sent ? { status: "crashed", error: g } : { status: "unavailable", error: g }, P = (p, g) => {
|
|
283
|
+
p.sent = !0, D(p);
|
|
277
284
|
try {
|
|
278
285
|
p.worker.postMessage({ id: g.id, input: g.input });
|
|
279
286
|
} catch (E) {
|
|
280
287
|
v(p, { status: "crashed", error: E }), p.worker.unref();
|
|
281
288
|
}
|
|
282
|
-
},
|
|
289
|
+
}, C = () => {
|
|
283
290
|
const p = { worker: new Dr(e, { resourceLimits: c }), job: null, sent: !1, timer: null, ready: !1 };
|
|
284
291
|
return p.worker.unref(), p.timer = setTimeout(() => {
|
|
285
292
|
R(p, { status: "unavailable", error: new Error(`[markdown-explorer] a worker did not start within ${String(r)} ms`) });
|
|
286
293
|
}, r), p.worker.on("message", (g) => {
|
|
287
|
-
if (!p.ready &&
|
|
288
|
-
p.ready = !0, p.timer !== null && clearTimeout(p.timer), p.timer = null, p.job === null ? p.worker.unref() :
|
|
294
|
+
if (!p.ready && yn(g)) {
|
|
295
|
+
p.ready = !0, p.timer !== null && clearTimeout(p.timer), p.timer = null, p.job === null ? p.worker.unref() : P(p, p.job), p.job === null && F();
|
|
289
296
|
return;
|
|
290
297
|
}
|
|
291
298
|
const { job: E } = p;
|
|
292
|
-
if (E === null || !
|
|
299
|
+
if (E === null || !gn(g, E.id)) {
|
|
293
300
|
R(p, _(p, new Error("[markdown-explorer] a worker sent a message that answers no job")));
|
|
294
301
|
return;
|
|
295
302
|
}
|
|
296
|
-
v(p, { status: "done", output: g.output }), p.worker.unref(),
|
|
303
|
+
v(p, { status: "done", output: g.output }), p.worker.unref(), F();
|
|
297
304
|
}), p.worker.on("error", (g) => {
|
|
298
305
|
R(p, _(p, g));
|
|
299
306
|
}), p.worker.on("messageerror", (g) => {
|
|
@@ -301,7 +308,7 @@ const un = (e) => e instanceof Error && Object.hasOwn(e, Pt) && Tt(e.reason), dn
|
|
|
301
308
|
}), p.worker.on("exit", (g) => {
|
|
302
309
|
R(p, _(p, new Error(`[markdown-explorer] a worker exited with code ${String(g)}`)));
|
|
303
310
|
}), m.add(p), p;
|
|
304
|
-
},
|
|
311
|
+
}, F = () => {
|
|
305
312
|
for (; ; ) {
|
|
306
313
|
const p = A(), g = h[p];
|
|
307
314
|
if (g === void 0) return;
|
|
@@ -313,21 +320,21 @@ const un = (e) => e instanceof Error && Object.hasOwn(e, Pt) && Tt(e.reason), dn
|
|
|
313
320
|
}
|
|
314
321
|
if (E === void 0 && m.size < t)
|
|
315
322
|
try {
|
|
316
|
-
E =
|
|
323
|
+
E = C();
|
|
317
324
|
} catch (T) {
|
|
318
|
-
|
|
325
|
+
$(p), ue(w, g.scope, -1), g.settle({ status: "unavailable", error: T });
|
|
319
326
|
continue;
|
|
320
327
|
}
|
|
321
328
|
if (E === void 0) return;
|
|
322
|
-
|
|
329
|
+
$(p), ue(x, g.scope, 1);
|
|
323
330
|
const O = E;
|
|
324
|
-
O.job = g, O.worker.ref(), O.ready &&
|
|
331
|
+
O.job = g, O.worker.ref(), O.ready && P(O, g);
|
|
325
332
|
}
|
|
326
333
|
};
|
|
327
334
|
return {
|
|
328
335
|
run: (p, { scope: g, signal: E }) => new Promise((O) => {
|
|
329
336
|
if (E.aborted) {
|
|
330
|
-
O(
|
|
337
|
+
O(ot);
|
|
331
338
|
return;
|
|
332
339
|
}
|
|
333
340
|
if ((w.get(g) ?? 0) >= n) {
|
|
@@ -337,16 +344,16 @@ const un = (e) => e instanceof Error && Object.hasOwn(e, Pt) && Tt(e.reason), dn
|
|
|
337
344
|
k += 1;
|
|
338
345
|
const T = { id: k, input: p, scope: g, settle: O, detach: () => {
|
|
339
346
|
} };
|
|
340
|
-
h.push(T), ue(w, g, 1),
|
|
341
|
-
const
|
|
342
|
-
if (
|
|
343
|
-
if (h.length >
|
|
344
|
-
|
|
347
|
+
h.push(T), ue(w, g, 1), F();
|
|
348
|
+
const I = h.indexOf(T);
|
|
349
|
+
if (I < 0) return;
|
|
350
|
+
if (h.length > i) {
|
|
351
|
+
$(I), ue(w, g, -1), O({ status: "refused", limit: "queue" });
|
|
345
352
|
return;
|
|
346
353
|
}
|
|
347
354
|
const M = () => {
|
|
348
355
|
const U = h.indexOf(T);
|
|
349
|
-
U < 0 || (
|
|
356
|
+
U < 0 || ($(U), ue(w, g, -1), O(ot));
|
|
350
357
|
};
|
|
351
358
|
E.addEventListener("abort", M, { once: !0 }), T.detach = () => {
|
|
352
359
|
E.removeEventListener("abort", M);
|
|
@@ -354,246 +361,246 @@ const un = (e) => e instanceof Error && Object.hasOwn(e, Pt) && Tt(e.reason), dn
|
|
|
354
361
|
}),
|
|
355
362
|
workers: () => m.size
|
|
356
363
|
};
|
|
357
|
-
},
|
|
364
|
+
}, De = 1024 * 1024, bn = 3e4, Tn = 4 * De, kn = 64 * De, Sn = 3, xn = 2, On = (e, t) => e + Lt(t) + Sn, Rn = 4, vn = 32, _n = 512, An = 64, Nn = 4096, In = 32, st = 4096, Pn = 4, $t = ({ workerHeapMb: e, maxHtmlBytes: t }) => Math.floor(((e - An) * De - t * In) / Nn), Mn = ({ maxHtmlBytes: e }) => e, Ln = ({ maxHtmlBytes: e }) => Pn * e, $n = 1024, jn = 2147483647, zn = 64, Dn = 2 ** 29, Cn = 1024 * 1024, Hn = 64, it = Object.freeze({ status: "rejected", reason: "too-large", cause: null }), Ne = Object.freeze({ status: "rejected", reason: "failed", cause: null }), de = (e, t, r, n) => {
|
|
358
365
|
if (t === void 0) return r;
|
|
359
366
|
if (typeof t != "number") throw new TypeError(`[markdown-explorer] sanitizer.${e} must be a number; got ${typeof t}`);
|
|
360
367
|
if (!Number.isSafeInteger(t) || t <= 0 || t > n) throw new RangeError(`[markdown-explorer] sanitizer.${e} must be an integer in 1..${String(n)}; got ${String(t)}`);
|
|
361
368
|
return t;
|
|
362
|
-
},
|
|
369
|
+
}, Bn = (e, t) => {
|
|
363
370
|
if (e !== void 0 && (e === null || typeof e != "object")) throw new TypeError("[markdown-explorer] sanitizer must be an object of { workers, deadlineMs, maxHtmlBytes, cacheBytes, workerHeapMb, jobsPerScope, maxQueuedJobs }");
|
|
364
|
-
const { workers: r, deadlineMs: n, maxHtmlBytes:
|
|
365
|
-
workers:
|
|
366
|
-
deadlineMs: de("deadlineMs", n,
|
|
367
|
-
maxHtmlBytes: de("maxHtmlBytes",
|
|
368
|
-
cacheBytes: de("cacheBytes",
|
|
369
|
-
workerHeapMb: de("workerHeapMb", c,
|
|
371
|
+
const { workers: r, deadlineMs: n, maxHtmlBytes: i, cacheBytes: a, workerHeapMb: c, jobsPerScope: m, maxQueuedJobs: h } = e ?? {}, x = de("workers", r, xn, zn), w = de("jobsPerScope", m, On(t, x), Number.MAX_SAFE_INTEGER), y = Object.freeze({
|
|
372
|
+
workers: x,
|
|
373
|
+
deadlineMs: de("deadlineMs", n, bn, jn),
|
|
374
|
+
maxHtmlBytes: de("maxHtmlBytes", i, Tn, Dn),
|
|
375
|
+
cacheBytes: de("cacheBytes", a, kn, Number.MAX_SAFE_INTEGER),
|
|
376
|
+
workerHeapMb: de("workerHeapMb", c, _n, Cn),
|
|
370
377
|
jobsPerScope: w,
|
|
371
|
-
maxQueuedJobs: de("maxQueuedJobs", h,
|
|
378
|
+
maxQueuedJobs: de("maxQueuedJobs", h, Rn * w, Number.MAX_SAFE_INTEGER)
|
|
372
379
|
});
|
|
373
380
|
if (y.jobsPerScope < t)
|
|
374
381
|
throw new RangeError(`[markdown-explorer] sanitizer.jobsPerScope must be at least multiView.maxPanels (${String(t)}), because a page sends the document of every panel to the sanitizer at once for one reader; got ${String(y.jobsPerScope)}`);
|
|
375
382
|
if (y.maxQueuedJobs < t)
|
|
376
383
|
throw new RangeError(`[markdown-explorer] sanitizer.maxQueuedJobs must be at least multiView.maxPanels (${String(t)}), because a reader whose page finds every worker busy waits with the document of every panel; got ${String(y.maxQueuedJobs)}`);
|
|
377
|
-
if (
|
|
384
|
+
if ($t(y) < st)
|
|
378
385
|
throw new RangeError(
|
|
379
|
-
`[markdown-explorer] sanitizer.workerHeapMb (${String(y.workerHeapMb)} MiB) must leave room for ${String(
|
|
386
|
+
`[markdown-explorer] sanitizer.workerHeapMb (${String(y.workerHeapMb)} MiB) must leave room for ${String(st)} parsed nodes beside HTML of sanitizer.maxHtmlBytes (${String(y.maxHtmlBytes)} bytes); raise workerHeapMb or lower maxHtmlBytes`
|
|
380
387
|
);
|
|
381
388
|
return y;
|
|
382
|
-
},
|
|
389
|
+
}, Fn = () => {
|
|
383
390
|
const e = import.meta.url, t = new URL(`./sanitizeWorker${jr(Re(e))}`, e);
|
|
384
391
|
if (!Mr(Re(t))) throw new Error(`[markdown-explorer] the HTML sanitizer worker is missing: ${Re(t)}`);
|
|
385
392
|
return t;
|
|
386
|
-
},
|
|
393
|
+
}, at = /* @__PURE__ */ new Map(), Un = (e, t) => {
|
|
387
394
|
const r = [e.href, t.workers, t.deadlineMs, t.workerHeapMb, t.jobsPerScope, t.maxQueuedJobs].map(String).join("\0");
|
|
388
|
-
let n =
|
|
389
|
-
return n === void 0 && (n =
|
|
395
|
+
let n = at.get(r);
|
|
396
|
+
return n === void 0 && (n = En({
|
|
390
397
|
url: e,
|
|
391
398
|
size: t.workers,
|
|
392
399
|
deadlineMs: t.deadlineMs,
|
|
393
400
|
jobsPerScope: t.jobsPerScope,
|
|
394
401
|
maxQueuedJobs: t.maxQueuedJobs,
|
|
395
|
-
heapLimits: { maxOldGenerationSizeMb: t.workerHeapMb, maxYoungGenerationSizeMb:
|
|
396
|
-
}),
|
|
397
|
-
},
|
|
402
|
+
heapLimits: { maxOldGenerationSizeMb: t.workerHeapMb, maxYoungGenerationSizeMb: vn }
|
|
403
|
+
}), at.set(r, n)), n;
|
|
404
|
+
}, Xn = (e) => {
|
|
398
405
|
if (e === null || typeof e != "object") return !1;
|
|
399
406
|
const { status: t, html: r, message: n } = e;
|
|
400
407
|
return t === "sanitized" && typeof r == "string" || t === "too-large" || t === "error" && typeof n == "string";
|
|
401
|
-
},
|
|
402
|
-
const
|
|
408
|
+
}, Jn = (e) => e instanceof Error && e.code === "ERR_WORKER_OUT_OF_MEMORY", Gn = ({ iframeHosts: e, dataPath: t, options: r, pool: n }) => {
|
|
409
|
+
const i = Object.freeze([...e]), a = $t(r), c = Mn(r), m = Ln(r), h = Mt($n, {
|
|
403
410
|
max: r.cacheBytes,
|
|
404
|
-
of: (A, R) => 2 * A.length + (R.status === "sanitized" ? 2 * R.html.length :
|
|
405
|
-
}),
|
|
406
|
-
|
|
411
|
+
of: (A, R) => 2 * A.length + (R.status === "sanitized" ? 2 * R.html.length : Hn)
|
|
412
|
+
}), x = /* @__PURE__ */ new Map(), w = /* @__PURE__ */ new Map(), y = (A) => {
|
|
413
|
+
x.get(A.key) === A && x.delete(A.key);
|
|
407
414
|
const R = w.get(A.hash);
|
|
408
415
|
R?.delete(A), R?.size === 0 && w.delete(A.hash);
|
|
409
|
-
}, k = async (A, R,
|
|
410
|
-
const _ = await n.run({ html: A, iframeHosts:
|
|
416
|
+
}, k = async (A, R, D) => {
|
|
417
|
+
const _ = await n.run({ html: A, iframeHosts: i, dataPath: t, maxNodes: a, maxAttributeChars: c, maxSanitizedChars: m }, { scope: R, signal: D });
|
|
411
418
|
if (_.status === "refused") {
|
|
412
|
-
const
|
|
413
|
-
return { outcome: { status: "rejected", reason: "failed", cause: new Error(`[markdown-explorer] the HTML sanitizer refused a document: ${
|
|
419
|
+
const C = _.limit === "scope" ? `its reader already has ${String(r.jobsPerScope)} documents being sanitized or waiting` : `${String(r.maxQueuedJobs)} documents are already waiting`;
|
|
420
|
+
return { outcome: { status: "rejected", reason: "failed", cause: new Error(`[markdown-explorer] the HTML sanitizer refused a document: ${C}`) }, lasting: !1 };
|
|
414
421
|
}
|
|
415
422
|
if (_.status === "aborted") return { outcome: Ne, lasting: !1 };
|
|
416
423
|
if (_.status === "timeout") return { outcome: { status: "rejected", reason: "too-large", cause: new Error(`[markdown-explorer] the HTML sanitizer did not finish within ${String(r.deadlineMs)} ms`) }, lasting: !0 };
|
|
417
424
|
if (_.status === "unavailable") return { outcome: { status: "rejected", reason: "failed", cause: new Error("[markdown-explorer] the HTML sanitizer worker could not start", { cause: _.error }) }, lasting: !1 };
|
|
418
|
-
if (_.status === "crashed" &&
|
|
425
|
+
if (_.status === "crashed" && Jn(_.error))
|
|
419
426
|
return { outcome: { status: "rejected", reason: "too-large", cause: new Error(`[markdown-explorer] the HTML sanitizer ran out of its ${String(r.workerHeapMb)} MiB worker heap`, { cause: _.error }) }, lasting: !0 };
|
|
420
427
|
if (_.status === "crashed") return { outcome: { status: "rejected", reason: "failed", cause: new Error("[markdown-explorer] the HTML sanitizer worker failed", { cause: _.error }) }, lasting: !0 };
|
|
421
|
-
const { output:
|
|
422
|
-
return
|
|
423
|
-
}, v = (A, R,
|
|
424
|
-
let
|
|
428
|
+
const { output: P } = _;
|
|
429
|
+
return Xn(P) ? P.status === "too-large" ? { outcome: it, lasting: !0 } : P.status === "error" ? { outcome: { status: "rejected", reason: "failed", cause: new Error(`[markdown-explorer] the HTML sanitizer failed: ${P.message}`) }, lasting: !0 } : P.html.length > m ? { outcome: { status: "rejected", reason: "too-large", cause: new Error(`[markdown-explorer] the HTML sanitizer's output of ${String(P.html.length)} characters exceeds its limit of ${String(m)}`) }, lasting: !0 } : { outcome: { status: "sanitized", html: P.html }, lasting: !0 } : { outcome: { status: "rejected", reason: "failed", cause: new Error("[markdown-explorer] the HTML sanitizer worker answered something that is not an outcome") }, lasting: !0 };
|
|
430
|
+
}, v = (A, R, D, _) => {
|
|
431
|
+
let P = () => {
|
|
425
432
|
};
|
|
426
|
-
const
|
|
427
|
-
|
|
428
|
-
}),
|
|
429
|
-
|
|
433
|
+
const C = new Promise((g) => {
|
|
434
|
+
P = g;
|
|
435
|
+
}), F = { hash: R, key: D, outcome: C, answer: P, controller: new AbortController(), waiters: 0 };
|
|
436
|
+
x.set(D, F);
|
|
430
437
|
const p = w.get(R);
|
|
431
|
-
return p === void 0 ? w.set(R, /* @__PURE__ */ new Set([
|
|
432
|
-
if (y(
|
|
438
|
+
return p === void 0 ? w.set(R, /* @__PURE__ */ new Set([F])) : p.add(F), k(A, _, F.controller.signal).catch((g) => ({ outcome: { status: "rejected", reason: "failed", cause: g instanceof Error ? g : new Error(String(g)) }, lasting: !0 })).then(({ outcome: g, lasting: E }) => {
|
|
439
|
+
if (y(F), F.answer(g), !E) return;
|
|
433
440
|
const O = h.get(R), T = O ?? (g.status === "rejected" ? { ...g, cause: null } : g);
|
|
434
441
|
O === void 0 && h.set(R, T);
|
|
435
|
-
for (const
|
|
436
|
-
y(
|
|
437
|
-
}),
|
|
438
|
-
},
|
|
442
|
+
for (const I of w.get(R) ?? [])
|
|
443
|
+
y(I), I.answer(T), I.controller.abort();
|
|
444
|
+
}), F;
|
|
445
|
+
}, $ = (A, R) => new Promise((D) => {
|
|
439
446
|
A.waiters += 1;
|
|
440
447
|
const _ = () => {
|
|
441
|
-
A.waiters -= 1,
|
|
448
|
+
A.waiters -= 1, D(Ne), !(A.waiters > 0) && (y(A), A.controller.abort());
|
|
442
449
|
};
|
|
443
|
-
R.addEventListener("abort", _, { once: !0 }), A.outcome.then((
|
|
444
|
-
R.removeEventListener("abort", _),
|
|
450
|
+
R.addEventListener("abort", _, { once: !0 }), A.outcome.then((P) => {
|
|
451
|
+
R.removeEventListener("abort", _), D(P);
|
|
445
452
|
});
|
|
446
453
|
});
|
|
447
|
-
return (A, { scope: R, signal:
|
|
448
|
-
if (A.length > r.maxHtmlBytes || Buffer.byteLength(A, "utf8") > r.maxHtmlBytes) return Promise.resolve(
|
|
449
|
-
const _ = br("sha256").update(A, "utf8").digest("base64"),
|
|
450
|
-
if (
|
|
451
|
-
if (
|
|
452
|
-
const
|
|
453
|
-
return
|
|
454
|
+
return (A, { scope: R, signal: D }) => {
|
|
455
|
+
if (A.length > r.maxHtmlBytes || Buffer.byteLength(A, "utf8") > r.maxHtmlBytes) return Promise.resolve(it);
|
|
456
|
+
const _ = br("sha256").update(A, "utf8").digest("base64"), P = h.get(_);
|
|
457
|
+
if (P !== void 0) return Promise.resolve(P);
|
|
458
|
+
if (D.aborted) return Promise.resolve(Ne);
|
|
459
|
+
const C = `${_}\0${R}`, F = x.get(C);
|
|
460
|
+
return $(F === void 0 ? v(A, _, C, R) : F, D);
|
|
454
461
|
};
|
|
455
|
-
}, xe = (e) => ({ result: e, json: JSON.stringify(e) }), Ie = Object.freeze(xe(Object.freeze({ status: "error", failure: Object.freeze({ reason: "failed" }) }))),
|
|
456
|
-
const
|
|
457
|
-
const k = new AbortController(), v = { waiters: 0, superseded: !1, started: !1, loadTree: y },
|
|
458
|
-
const p = xe(
|
|
459
|
-
return !(v.superseded || k.signal.aborted) &&
|
|
460
|
-
},
|
|
461
|
-
if (
|
|
462
|
-
const
|
|
463
|
-
return v.started = !0, A(await v.loadTree(k.signal,
|
|
462
|
+
}, xe = (e) => ({ result: e, json: JSON.stringify(e) }), Ie = Object.freeze(xe(Object.freeze({ status: "error", failure: Object.freeze({ reason: "failed" }) }))), Yn = (e) => e.status === "ok" && e.entries.length > 0, Wn = ({ ttlMs: e, maxEntries: t, maxBytes: r, now: n }) => {
|
|
463
|
+
const i = Mt(t, { max: r, of: (w, y) => 2 * y.tree.json.length }), a = /* @__PURE__ */ new Map(), c = /* @__PURE__ */ new Map(), m = (w, y) => {
|
|
464
|
+
const k = new AbortController(), v = { waiters: 0, superseded: !1, started: !1, loadTree: y }, $ = c.get(w), A = (C, F) => {
|
|
465
|
+
const p = xe(C);
|
|
466
|
+
return !(v.superseded || k.signal.aborted) && Yn(C) && i.set(w, { tree: p, loadedAt: F }), p;
|
|
467
|
+
}, D = (async () => {
|
|
468
|
+
if ($ !== void 0 && await $, k.signal.aborted) return Ie;
|
|
469
|
+
const C = n();
|
|
470
|
+
return v.started = !0, A(await v.loadTree(k.signal, C), C);
|
|
464
471
|
})().finally(() => {
|
|
465
|
-
|
|
466
|
-
}), _ = { promise:
|
|
467
|
-
|
|
468
|
-
const
|
|
472
|
+
a.get(w) === _ && a.delete(w);
|
|
473
|
+
}), _ = { promise: D, controller: k, state: v };
|
|
474
|
+
a.set(w, _);
|
|
475
|
+
const P = D.then(
|
|
469
476
|
() => {
|
|
470
477
|
},
|
|
471
478
|
() => {
|
|
472
479
|
}
|
|
473
480
|
);
|
|
474
|
-
return c.set(w,
|
|
475
|
-
c.get(w) ===
|
|
481
|
+
return c.set(w, P), P.then(() => {
|
|
482
|
+
c.get(w) === P && c.delete(w);
|
|
476
483
|
}), _;
|
|
477
484
|
}, h = (w, y, k) => {
|
|
478
485
|
y.state.waiters += 1;
|
|
479
486
|
const v = () => {
|
|
480
|
-
y.state.waiters -= 1, !(y.state.waiters > 0) && (
|
|
487
|
+
y.state.waiters -= 1, !(y.state.waiters > 0) && (a.get(w) === y && a.delete(w), y.controller.abort());
|
|
481
488
|
};
|
|
482
489
|
return k.addEventListener("abort", v, { once: !0 }), y.promise.finally(() => {
|
|
483
490
|
k.removeEventListener("abort", v);
|
|
484
491
|
});
|
|
485
|
-
},
|
|
486
|
-
|
|
487
|
-
const y =
|
|
488
|
-
y?.state.started && (y.state.superseded = !0,
|
|
492
|
+
}, x = (w) => {
|
|
493
|
+
i.delete(w);
|
|
494
|
+
const y = a.get(w);
|
|
495
|
+
y?.state.started && (y.state.superseded = !0, a.delete(w));
|
|
489
496
|
};
|
|
490
497
|
return {
|
|
491
498
|
load: (w, y, k) => {
|
|
492
499
|
if (y.aborted) return Promise.resolve(Ie);
|
|
493
|
-
const v =
|
|
500
|
+
const v = i.get(w);
|
|
494
501
|
if (v !== void 0) {
|
|
495
502
|
if (n() - v.loadedAt < e) return Promise.resolve(v.tree);
|
|
496
|
-
|
|
503
|
+
i.delete(w);
|
|
497
504
|
}
|
|
498
|
-
const
|
|
499
|
-
return h(w,
|
|
505
|
+
const $ = a.get(w);
|
|
506
|
+
return h(w, $ === void 0 ? m(w, k) : $, y);
|
|
500
507
|
},
|
|
501
508
|
refresh: (w, y, k) => {
|
|
502
509
|
if (y.aborted) return Promise.resolve(Ie);
|
|
503
|
-
|
|
504
|
-
const v =
|
|
510
|
+
x(w);
|
|
511
|
+
const v = a.get(w);
|
|
505
512
|
return v === void 0 ? h(w, m(w, k), y) : (v.state.loadTree = k, h(w, v, y));
|
|
506
513
|
},
|
|
507
|
-
forget:
|
|
514
|
+
forget: x
|
|
508
515
|
};
|
|
509
|
-
},
|
|
516
|
+
}, Kn = (e) => ({
|
|
510
517
|
load: (t, r, n) => n(r, e()).then(xe),
|
|
511
518
|
refresh: (t, r, n) => n(r, e()).then(xe),
|
|
512
519
|
forget: () => {
|
|
513
520
|
}
|
|
514
|
-
}),
|
|
521
|
+
}), S = (e) => {
|
|
515
522
|
throw new TypeError(`[markdown-explorer] ${e}`);
|
|
516
|
-
},
|
|
523
|
+
}, Vn = (e) => {
|
|
517
524
|
if (e === null || typeof e != "object") return null;
|
|
518
525
|
const { name: t, path: r } = e;
|
|
519
526
|
return typeof t != "string" || typeof r != "string" ? null : t === "" ? { path: r, reason: "an empty name" } : q(r) ? null : { path: r, reason: "a path the browser cannot address (empty, too long, a lone surrogate, a control character or a backslash)" };
|
|
520
|
-
},
|
|
527
|
+
}, Zn = (e, t, r, n) => {
|
|
521
528
|
if (e === null) return null;
|
|
522
529
|
if (!Array.isArray(e)) throw new TypeError(`[markdown-explorer] a source's ancestors must be null or an array of folder paths; got ${typeof e}`);
|
|
523
|
-
const
|
|
524
|
-
for (const
|
|
525
|
-
if (typeof
|
|
526
|
-
if (r.deniesPath(
|
|
527
|
-
if (!n(
|
|
528
|
-
if (
|
|
529
|
-
if (
|
|
530
|
-
|
|
530
|
+
const i = /* @__PURE__ */ new Set();
|
|
531
|
+
for (const a of e) {
|
|
532
|
+
if (typeof a != "string" || !q(a)) throw new TypeError("[markdown-explorer] a source's ancestors held a path the explorer does not accept");
|
|
533
|
+
if (r.deniesPath(a)) throw new TypeError("[markdown-explorer] a source's ancestors held a denied path");
|
|
534
|
+
if (!n(a)) throw new TypeError("[markdown-explorer] a source's ancestors held a path outside the source's boundary");
|
|
535
|
+
if (a === t) throw new TypeError("[markdown-explorer] a source's ancestors held the document itself");
|
|
536
|
+
if (i.has(a)) throw new TypeError("[markdown-explorer] a source's ancestors held a path twice");
|
|
537
|
+
i.add(a);
|
|
531
538
|
}
|
|
532
|
-
return Object.freeze([...
|
|
533
|
-
},
|
|
534
|
-
const
|
|
535
|
-
if (!Array.isArray(h)) return
|
|
536
|
-
const
|
|
539
|
+
return Object.freeze([...i]);
|
|
540
|
+
}, Qn = (e, t, r, n, i) => {
|
|
541
|
+
const a = /* @__PURE__ */ new Set(), c = (h) => {
|
|
542
|
+
if (!Array.isArray(h)) return S("a source returned entries that are not an array");
|
|
543
|
+
const x = [];
|
|
537
544
|
for (const w of h) {
|
|
538
545
|
const y = m(w);
|
|
539
|
-
y !== null &&
|
|
546
|
+
y !== null && x.push(y);
|
|
540
547
|
}
|
|
541
|
-
return
|
|
548
|
+
return x;
|
|
542
549
|
}, m = (h) => {
|
|
543
|
-
const
|
|
544
|
-
if (
|
|
545
|
-
return n(
|
|
550
|
+
const x = Vn(h);
|
|
551
|
+
if (x !== null)
|
|
552
|
+
return n(x), null;
|
|
546
553
|
const w = lr(h);
|
|
547
|
-
if (w !== null) return
|
|
554
|
+
if (w !== null) return S(w);
|
|
548
555
|
const y = h;
|
|
549
|
-
if (
|
|
550
|
-
const { name: k, path: v, type:
|
|
556
|
+
if (a.has(y)) return S("a source returned entries that contain themselves");
|
|
557
|
+
const { name: k, path: v, type: $, mimeType: A, modifiedAt: R, children: D, lazy: _ } = y;
|
|
551
558
|
if (t.deniesName(k) || t.deniesPath(v)) return null;
|
|
552
|
-
if (!
|
|
559
|
+
if (!i(v))
|
|
553
560
|
return n({ path: v, reason: "a path outside the source's boundary" }), null;
|
|
554
|
-
const
|
|
555
|
-
if (
|
|
561
|
+
const P = { name: k, path: v, ...A === void 0 ? {} : { mimeType: A }, ...R === void 0 ? {} : { modifiedAt: R } };
|
|
562
|
+
if ($ === "file") return { ...P, type: $ };
|
|
556
563
|
if (_ === !0)
|
|
557
|
-
return r ? { ...
|
|
558
|
-
if (
|
|
559
|
-
|
|
560
|
-
const
|
|
561
|
-
return
|
|
564
|
+
return r ? { ...P, type: $, lazy: !0, children: [] } : S("a lazy directory came from a source without a children function");
|
|
565
|
+
if (D === void 0) return { ...P, type: $ };
|
|
566
|
+
a.add(y);
|
|
567
|
+
const C = c(D);
|
|
568
|
+
return a.delete(y), { ...P, type: $, children: C };
|
|
562
569
|
};
|
|
563
570
|
return c(e);
|
|
564
|
-
},
|
|
571
|
+
}, qn = /^[\x21\x23-\x7E]{1,256}$/u, jt = (e) => typeof e == "string" && qn.test(e), eo = /* @__PURE__ */ new Set(["text", "image", "media", "file"]), to = (e) => typeof e == "string" && eo.has(e), ro = (e, t, r) => {
|
|
565
572
|
if (e !== null && typeof e == "object") {
|
|
566
|
-
const
|
|
567
|
-
if (
|
|
568
|
-
if (Object.hasOwn(
|
|
569
|
-
if (typeof
|
|
570
|
-
if (!jt(
|
|
571
|
-
const m = t(
|
|
573
|
+
const a = e, { kind: c } = a;
|
|
574
|
+
if (to(c)) {
|
|
575
|
+
if (Object.hasOwn(a, "url")) return S(`a ${c} document carries its version, not a url`);
|
|
576
|
+
if (typeof a.name != "string") return S("a document needs a name string");
|
|
577
|
+
if (!jt(a.version)) return S(`a ${c} document needs a version of 1 to 256 visible ASCII characters other than '"'`);
|
|
578
|
+
const m = t(a.version);
|
|
572
579
|
switch (c) {
|
|
573
580
|
case "image":
|
|
574
581
|
case "file":
|
|
575
|
-
return { kind: c, name:
|
|
582
|
+
return { kind: c, name: a.name, url: m };
|
|
576
583
|
case "media": {
|
|
577
|
-
const { mediaType: h } =
|
|
578
|
-
return ir(h) ? { kind: c, name:
|
|
584
|
+
const { mediaType: h } = a;
|
|
585
|
+
return ir(h) ? { kind: c, name: a.name, url: m, mediaType: h } : S(`unknown media type ${typeof h == "string" ? JSON.stringify(h.slice(0, 64)) : typeof h}`);
|
|
579
586
|
}
|
|
580
587
|
case "text":
|
|
581
|
-
return typeof
|
|
588
|
+
return typeof a.text != "string" || typeof a.truncated != "boolean" ? S("a text document needs text and truncated") : { kind: c, name: a.name, text: a.text, truncated: a.truncated, url: m };
|
|
582
589
|
}
|
|
583
590
|
}
|
|
584
591
|
}
|
|
585
592
|
const n = ar(e);
|
|
586
|
-
if (n !== null) return
|
|
587
|
-
const
|
|
588
|
-
switch (
|
|
593
|
+
if (n !== null) return S(n);
|
|
594
|
+
const i = e;
|
|
595
|
+
switch (i.kind) {
|
|
589
596
|
case "failure": {
|
|
590
|
-
const { reason:
|
|
591
|
-
return { kind: "failure", failure: ke(
|
|
597
|
+
const { reason: a, code: c } = i.failure;
|
|
598
|
+
return { kind: "failure", failure: ke(a, c) };
|
|
592
599
|
}
|
|
593
600
|
case "markdown":
|
|
594
|
-
return
|
|
601
|
+
return i.headings.length > kt ? { kind: "failure", failure: ke("too-large") } : (i.edit?.kind === "inline" || i.edit?.kind === "draft") && !r ? S(`a markdown document offers ${i.edit.kind} editing, but the server has no edit ports`) : { kind: "unsanitized-markdown", name: i.name, html: i.htmlContent, headings: i.headings.map(({ text: a, depth: c, id: m }) => ({ text: a, depth: c, id: m })), edit: no(i.edit) };
|
|
595
602
|
}
|
|
596
|
-
},
|
|
603
|
+
}, no = (e) => {
|
|
597
604
|
if (e === null) return null;
|
|
598
605
|
switch (e.kind) {
|
|
599
606
|
case "inline":
|
|
@@ -603,71 +610,77 @@ const un = (e) => e instanceof Error && Object.hasOwn(e, Pt) && Tt(e.reason), dn
|
|
|
603
610
|
case "external":
|
|
604
611
|
return { kind: "external", href: e.href };
|
|
605
612
|
}
|
|
606
|
-
}, Oe = (e, t, r) => typeof e != "string" ?
|
|
607
|
-
if (e === null || typeof e != "object") return
|
|
613
|
+
}, Oe = (e, t, r) => typeof e != "string" ? S(`${r} needs a text string`) : e.includes("\0") ? S(`${r} holds NUL, which no text editor can keep`) : new TextEncoder().encode(e).byteLength > t ? S(`${r} is over editMaxBytes (${t} bytes); refuse a longer source with ExplorerError "too-large"`) : e, zt = (e) => dr(e.href) ? { kind: "grant", href: e.href } : S("a grant needs a root-relative href"), oo = (e, t) => {
|
|
614
|
+
if (e === null || typeof e != "object") return S("an edit source must be an object");
|
|
608
615
|
const r = e;
|
|
609
|
-
return r.kind === "grant" ? zt(r) : r.kind === "draft" ? { kind: "draft", text: Oe(r.text, t, "a draft source") } : r.kind !== "edit" ?
|
|
610
|
-
},
|
|
611
|
-
if (e === null || typeof e != "object") return
|
|
616
|
+
return r.kind === "grant" ? zt(r) : r.kind === "draft" ? { kind: "draft", text: Oe(r.text, t, "a draft source") } : r.kind !== "edit" ? S(`an edit source's kind must be "edit", "draft" or "grant"`) : he(r.version) ? typeof r.history != "boolean" ? S("an edit source needs history") : { kind: "edit", text: Oe(r.text, t, "an edit source"), version: r.version, history: r.history } : S("an edit source needs a version of 1 to 1024 characters");
|
|
617
|
+
}, so = (e, t) => {
|
|
618
|
+
if (e === null || typeof e != "object") return S("a save result must be an object");
|
|
612
619
|
const r = e;
|
|
613
620
|
switch (r.kind) {
|
|
614
621
|
case "grant":
|
|
615
622
|
return zt(r);
|
|
616
623
|
case "saved":
|
|
617
|
-
return he(r.version) ? { kind: "saved", version: r.version } :
|
|
624
|
+
return he(r.version) ? { kind: "saved", version: r.version } : S("a saved result needs a version of 1 to 1024 characters");
|
|
618
625
|
case "conflict":
|
|
619
|
-
return he(r.version) ? { kind: "conflict", version: r.version, text: Oe(r.text, t, "a conflict's current text") } :
|
|
626
|
+
return he(r.version) ? { kind: "conflict", version: r.version, text: Oe(r.text, t, "a conflict's current text") } : S("a conflict needs the current version of 1 to 1024 characters");
|
|
620
627
|
default:
|
|
621
|
-
return
|
|
628
|
+
return S(`a save result's kind must be "saved", "conflict" or "grant"`);
|
|
622
629
|
}
|
|
623
|
-
},
|
|
624
|
-
if (e === null || typeof e != "object") return
|
|
630
|
+
}, io = (e) => {
|
|
631
|
+
if (e === null || typeof e != "object") return S("a preview must be an object");
|
|
625
632
|
const { htmlContent: t, headings: r } = e;
|
|
626
|
-
if (typeof t != "string" || !Array.isArray(r)) return
|
|
633
|
+
if (typeof t != "string" || !Array.isArray(r)) return S("a preview needs htmlContent and headings");
|
|
627
634
|
if (r.length > kt) return { kind: "failure", failure: ke("too-large") };
|
|
628
635
|
for (const n of r) {
|
|
629
|
-
const
|
|
630
|
-
if (
|
|
636
|
+
const i = ur(n);
|
|
637
|
+
if (i !== null) return S(i);
|
|
631
638
|
}
|
|
632
|
-
return { html: t, headings: r.map(({ text: n, depth:
|
|
633
|
-
},
|
|
634
|
-
if (!Array.isArray(e)) return
|
|
639
|
+
return { html: t, headings: r.map(({ text: n, depth: i, id: a }) => ({ text: n, depth: i, id: a })) };
|
|
640
|
+
}, ao = (e) => {
|
|
641
|
+
if (!Array.isArray(e)) return S("revisions must be an array");
|
|
635
642
|
const t = /* @__PURE__ */ new Set();
|
|
636
643
|
return e.map((r) => {
|
|
637
644
|
const n = cr(r);
|
|
638
|
-
if (n !== null) return
|
|
639
|
-
const { id:
|
|
640
|
-
return t.has(
|
|
645
|
+
if (n !== null) return S(n);
|
|
646
|
+
const { id: i, modifiedAt: a, size: c, author: m, kept: h, current: x } = r;
|
|
647
|
+
return t.has(i) ? S(`two revisions share the id ${JSON.stringify(i.slice(0, 64))}`) : (t.add(i), { id: i, modifiedAt: a, size: c, author: m, kept: h, current: x });
|
|
641
648
|
});
|
|
642
|
-
},
|
|
643
|
-
if (e === null || typeof e != "object") return
|
|
644
|
-
const { name: t, contentType: r, size: n, version:
|
|
645
|
-
return typeof t != "string" || t === "" ?
|
|
646
|
-
},
|
|
647
|
-
if (
|
|
648
|
-
const {
|
|
649
|
-
if (typeof
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
649
|
+
}, co = (e, t) => Oe(e, t, "a revision"), Le = (e) => {
|
|
650
|
+
if (e === null || typeof e != "object") return S("an asset file must be an object");
|
|
651
|
+
const { name: t, contentType: r, size: n, version: i, open: a, kind: c } = e;
|
|
652
|
+
return c !== void 0 ? S(c === "redirect" ? "an asset redirect answers only a request for the stored file (options.redirect), never one that reads the bytes" : 'an asset file has no kind (the only kind is "redirect")') : typeof t != "string" || t === "" ? S("an asset file needs a non-empty name") : typeof r != "string" ? S("an asset file needs a contentType string ('' when unknown)") : n !== null && !(typeof n == "number" && Number.isSafeInteger(n) && n >= 0) ? S("an asset file's size must be null or a non-negative safe integer") : jt(i) ? typeof a != "function" ? S("an asset file needs an open function") : Object.freeze({ name: t, contentType: r, size: n, version: i, open: a }) : S(`an asset file needs a version of 1 to 256 visible ASCII characters other than '"'`);
|
|
653
|
+
}, uo = (e) => {
|
|
654
|
+
if (e === null || typeof e != "object" || e.kind !== "redirect") return Le(e);
|
|
655
|
+
const { url: t } = e;
|
|
656
|
+
if (typeof t != "string" || !URL.canParse(t)) return S("an asset redirect needs an absolute https URL");
|
|
657
|
+
const r = new URL(t);
|
|
658
|
+
return r.protocol !== "https:" || r.username !== "" || r.password !== "" ? S("an asset redirect's URL must be https and carry no credentials") : Object.freeze({ kind: "redirect", url: r.href });
|
|
659
|
+
}, lo = (e) => Array.isArray(e) ? e.map((t) => {
|
|
660
|
+
if (t === null || typeof t != "object") return S("a notice must be an object");
|
|
661
|
+
const { id: r, tone: n, title: i, body: a, action: c } = t;
|
|
662
|
+
if (typeof r != "string" || r === "" || typeof i != "string") return S("a notice needs an id and a title");
|
|
663
|
+
if (!or(n)) return S(`unknown notice tone ${typeof n == "string" ? JSON.stringify(n.slice(0, 64)) : typeof n}`);
|
|
664
|
+
if (a !== void 0 && typeof a != "string") return S("a notice body must be a string");
|
|
665
|
+
const h = { id: r, tone: n, title: i, ...a === void 0 ? {} : { body: a } };
|
|
653
666
|
if (c === void 0) return h;
|
|
654
|
-
if (c === null || typeof c != "object") return
|
|
655
|
-
const { label:
|
|
656
|
-
return typeof
|
|
657
|
-
}) :
|
|
667
|
+
if (c === null || typeof c != "object") return S("a notice action must be an object");
|
|
668
|
+
const { label: x, href: w } = c;
|
|
669
|
+
return typeof x != "string" || !sr(w, fr) ? S("a notice action needs a label and an http(s), mailto or relative href") : { ...h, action: { label: x, href: w } };
|
|
670
|
+
}) : S("notices must be an array"), fo = 4e3, Ee = Object.freeze({ ttlMs: 3e5, maxEntries: 64, maxBytes: 64 * 1024 * 1024 }), ct = 32, Dt = "private, no-store", mo = Object.freeze({
|
|
658
671
|
"Content-Type": "application/json; charset=utf-8",
|
|
659
672
|
"Cache-Control": Dt,
|
|
660
673
|
"X-Content-Type-Options": "nosniff",
|
|
661
674
|
"Content-Security-Policy": "default-src 'none'; frame-ancestors 'none'; sandbox",
|
|
662
675
|
"Cross-Origin-Resource-Policy": "same-origin"
|
|
663
|
-
}),
|
|
676
|
+
}), po = 22, wo = 2147483647, Pe = Object.freeze([]), ho = Object.freeze({ status: "ok", source: null, ancestors: null }), ut = Object.freeze(["contains", "confirm"]), dt = Object.freeze({ readOnly: !1 }), yo = Object.freeze({ readOnly: !0 }), K = (e, t, r) => {
|
|
664
677
|
if (!(r && t === void 0) && typeof t != "function")
|
|
665
678
|
throw new TypeError(`[markdown-explorer] ${e} must be a function; got ${typeof t}`);
|
|
666
679
|
}, fe = (e, t, r) => {
|
|
667
680
|
if (typeof t != "number") throw new TypeError(`[markdown-explorer] ${e} must be a number; got ${typeof t}`);
|
|
668
681
|
if (!Number.isSafeInteger(t) || t <= 0 || t > r) throw new RangeError(`[markdown-explorer] ${e} must be an integer in 1..${String(r)}; got ${String(t)}`);
|
|
669
682
|
return t;
|
|
670
|
-
},
|
|
683
|
+
}, go = (e) => {
|
|
671
684
|
if (e === null || typeof e != "object" || !Object.isFrozen(e)) throw new TypeError("[markdown-explorer] config must be the value returned by defineExplorerConfig");
|
|
672
685
|
return Rr(e);
|
|
673
686
|
}, lt = (e) => {
|
|
@@ -676,33 +689,33 @@ const un = (e) => e instanceof Error && Object.hasOwn(e, Pt) && Tt(e.reason), dn
|
|
|
676
689
|
} catch {
|
|
677
690
|
throw new TypeError("[markdown-explorer] authenticate returned headers that are not a valid headers init (their values are not logged)");
|
|
678
691
|
}
|
|
679
|
-
}, ft = (e) => e instanceof Response ? new TypeError(`[markdown-explorer] authenticate threw a Response (status ${String(e.status)}), which a data request does not follow`) : e,
|
|
692
|
+
}, ft = (e) => e instanceof Response ? new TypeError(`[markdown-explorer] authenticate threw a Response (status ${String(e.status)}), which a data request does not follow`) : e, Eo = (e) => {
|
|
680
693
|
if (typeof e != "string") throw new TypeError(`[markdown-explorer] scopeSecret must be a string; got ${typeof e}`);
|
|
681
694
|
if (e.length < ct) throw new RangeError(`[markdown-explorer] scopeSecret must have at least ${String(ct)} characters; got ${String(e.length)}`);
|
|
682
695
|
return e;
|
|
683
|
-
},
|
|
696
|
+
}, bo = (e, t) => {
|
|
684
697
|
if (!Ir(t)) throw new TypeError(`[markdown-explorer] ${e}.boundary must be a plain object { contains, confirm? }`);
|
|
685
698
|
for (const r of Object.keys(t))
|
|
686
699
|
if (!ut.includes(r)) throw new RangeError(`[markdown-explorer] ${e}.boundary.${r} is not an option; expected ${ut.join(", ")}`);
|
|
687
700
|
K(`${e}.boundary.contains`, t.contains, !1), K(`${e}.boundary.confirm`, t.confirm, !0);
|
|
688
|
-
},
|
|
701
|
+
}, To = (e) => {
|
|
689
702
|
if (!Array.isArray(e)) throw new TypeError("[markdown-explorer] sources must be an array");
|
|
690
703
|
const t = e;
|
|
691
704
|
if (t.length === 0) throw new RangeError("[markdown-explorer] sources must hold at least one source");
|
|
692
705
|
const r = /* @__PURE__ */ new Set();
|
|
693
|
-
for (const [n,
|
|
694
|
-
if (
|
|
695
|
-
const { id:
|
|
696
|
-
if (!vr(
|
|
697
|
-
throw new RangeError(`[markdown-explorer] sources[${String(n)}].id must use lowercase letters, digits and "-" and start with a letter or digit; got ${typeof
|
|
698
|
-
const k = `source ${JSON.stringify(
|
|
699
|
-
if (r.has(
|
|
700
|
-
if (r.add(
|
|
706
|
+
for (const [n, i] of t.entries()) {
|
|
707
|
+
if (i === null || typeof i != "object") throw new TypeError(`[markdown-explorer] sources[${String(n)}] must be an object`);
|
|
708
|
+
const { id: a, label: c, boundary: m, tree: h, children: x, ancestors: w, cacheScope: y } = i;
|
|
709
|
+
if (!vr(a))
|
|
710
|
+
throw new RangeError(`[markdown-explorer] sources[${String(n)}].id must use lowercase letters, digits and "-" and start with a letter or digit; got ${typeof a == "string" ? JSON.stringify(a) : typeof a}`);
|
|
711
|
+
const k = `source ${JSON.stringify(a)}`;
|
|
712
|
+
if (r.has(a)) throw new RangeError(`[markdown-explorer] source ids must be unique; ${JSON.stringify(a)} repeats at sources[${String(n)}]`);
|
|
713
|
+
if (r.add(a), typeof c != "string" && typeof c != "function") throw new TypeError(`[markdown-explorer] ${k}.label must be a string or a function`);
|
|
701
714
|
if (typeof c == "string" && c.trim() === "") throw new RangeError(`[markdown-explorer] ${k}.label must not be blank`);
|
|
702
|
-
if (
|
|
715
|
+
if (bo(k, m), K(`${k}.tree`, h, !1), K(`${k}.children`, x, !0), K(`${k}.ancestors`, w, !0), y !== void 0 && y !== "principal" && y !== "shared") throw new RangeError(`[markdown-explorer] ${k}.cacheScope must be "principal" or "shared"; got ${JSON.stringify(y)}`);
|
|
703
716
|
}
|
|
704
717
|
return t;
|
|
705
|
-
},
|
|
718
|
+
}, ko = (e) => {
|
|
706
719
|
if (e === !1) return !1;
|
|
707
720
|
if (e === void 0) return Ee;
|
|
708
721
|
if (e === null || typeof e != "object") throw new TypeError("[markdown-explorer] treeCache must be { ttlMs, maxEntries, maxBytes } or false");
|
|
@@ -722,22 +735,22 @@ const un = (e) => e instanceof Error && Object.hasOwn(e, Pt) && Tt(e.reason), dn
|
|
|
722
735
|
return t;
|
|
723
736
|
}, wt = (e, t) => {
|
|
724
737
|
if (typeof t != "string" || t === "") throw new TypeError("[markdown-explorer] principalKey must return a non-empty string");
|
|
725
|
-
return Tr("sha256", e).update(t, "utf8").digest("base64url").slice(0,
|
|
726
|
-
},
|
|
738
|
+
return Tr("sha256", e).update(t, "utf8").digest("base64url").slice(0, po);
|
|
739
|
+
}, So = (e) => e.view === "multi" ? e.docs : e.doc === null ? [] : [e.doc], ce = (e, t) => {
|
|
727
740
|
const r = e.getAll(t), [n] = r;
|
|
728
741
|
return r.length === 1 && n !== void 0 && n !== "" ? n : null;
|
|
729
742
|
}, W = (e, t, r, n = {}) => {
|
|
730
|
-
const
|
|
731
|
-
for (const [
|
|
732
|
-
return new Response(t, { status: e, headers:
|
|
733
|
-
}, j = (e, t, r) => W(pr[e.reason], JSON.stringify({ failure: e }), t, r),
|
|
734
|
-
const r =
|
|
743
|
+
const i = new Headers(r);
|
|
744
|
+
for (const [a, c] of Object.entries({ ...mo, ...n })) i.set(a, c);
|
|
745
|
+
return new Response(t, { status: e, headers: i });
|
|
746
|
+
}, j = (e, t, r) => W(pr[e.reason], JSON.stringify({ failure: e }), t, r), xo = /* @__PURE__ */ new Set(["refresh", "save", "preview"]), Oo = /* @__PURE__ */ new Set(["edit", "save", "preview", "revisions", "revision"]), ht = (e, t) => {
|
|
747
|
+
const r = xo.has(t) ? ["POST"] : ["GET", "HEAD"];
|
|
735
748
|
return r.includes(e.method.toUpperCase()) ? null : W(405, JSON.stringify({ failure: { reason: "invalid" } }), void 0, { Allow: r.join(", ") });
|
|
736
|
-
}, yt = (e) => e.headers.get(
|
|
737
|
-
const [r, ...n] = (e.headers.get("Content-Type") ?? "").split(";").map((k) => k.trim().toLowerCase()),
|
|
738
|
-
if (r !== Er ||
|
|
739
|
-
const
|
|
740
|
-
if (Number.isFinite(
|
|
749
|
+
}, yt = (e) => e.headers.get(je) === wr && xt(e, Ot) === void 0, Ro = (e) => e.headers.get(je) === hr && xt(e, Ot) === void 0, vo = async (e, t) => {
|
|
750
|
+
const [r, ...n] = (e.headers.get("Content-Type") ?? "").split(";").map((k) => k.trim().toLowerCase()), i = n.find((k) => k.startsWith("charset="));
|
|
751
|
+
if (r !== Er || i !== void 0 && i !== "charset=utf-8") return { status: "error", failure: { reason: "invalid" } };
|
|
752
|
+
const a = Number(e.headers.get("Content-Length") ?? "0");
|
|
753
|
+
if (Number.isFinite(a) && a > t) return { status: "error", failure: { reason: "too-large" } };
|
|
741
754
|
if (e.body === null) return { status: "ok", text: "" };
|
|
742
755
|
const c = e.body.getReader(), m = [];
|
|
743
756
|
let h = 0;
|
|
@@ -748,42 +761,42 @@ const un = (e) => e instanceof Error && Object.hasOwn(e, Pt) && Tt(e.reason), dn
|
|
|
748
761
|
return await c.cancel(), { status: "error", failure: { reason: "too-large" } };
|
|
749
762
|
m.push(v);
|
|
750
763
|
}
|
|
751
|
-
const
|
|
764
|
+
const x = new Uint8Array(h);
|
|
752
765
|
let w = 0;
|
|
753
766
|
for (const k of m)
|
|
754
|
-
|
|
767
|
+
x.set(k, w), w += k.byteLength;
|
|
755
768
|
let y;
|
|
756
769
|
try {
|
|
757
|
-
y = new TextDecoder("utf-8", { fatal: !0 }).decode(
|
|
770
|
+
y = new TextDecoder("utf-8", { fatal: !0 }).decode(x);
|
|
758
771
|
} catch {
|
|
759
772
|
return { status: "error", failure: { reason: "invalid" } };
|
|
760
773
|
}
|
|
761
774
|
return y.includes("\0") ? { status: "error", failure: { reason: "invalid" } } : { status: "ok", text: y };
|
|
762
|
-
},
|
|
775
|
+
}, _o = (e) => {
|
|
763
776
|
if (e === void 0) return;
|
|
764
777
|
if (e === null || typeof e != "object") throw new TypeError("[markdown-explorer] edit must be an object of the editor's ports");
|
|
765
778
|
const t = e;
|
|
766
779
|
for (const r of ["source", "save", "preview", "revisions", "revision"]) K(`edit.${r}`, t[r], !1);
|
|
767
780
|
return e;
|
|
768
|
-
},
|
|
769
|
-
const n = new Headers(),
|
|
770
|
-
for (const
|
|
771
|
-
for (const [c, m] of
|
|
781
|
+
}, Ao = ({ parentHeaders: e, loaderHeaders: t, errorHeaders: r }) => {
|
|
782
|
+
const n = new Headers(), i = r === void 0 ? [e, t] : [e, t, r];
|
|
783
|
+
for (const a of i)
|
|
784
|
+
for (const [c, m] of a)
|
|
772
785
|
c !== "set-cookie" && n.set(c, m);
|
|
773
786
|
return n;
|
|
774
|
-
},
|
|
787
|
+
}, rs = (e) => {
|
|
775
788
|
if (e === null || typeof e != "object") throw new TypeError("[markdown-explorer] the server configuration must be an object");
|
|
776
|
-
const t =
|
|
789
|
+
const t = go(e.config), r = Eo(e.scopeSecret);
|
|
777
790
|
K("authenticate", e.authenticate, !1), K("principalKey", e.principalKey, !1), K("document", e.document, !1), K("asset", e.asset, !1), K("signedOutPage", e.signedOutPage, !1), K("forbiddenPage", e.forbiddenPage, !0), K("notices", e.notices, !0), K("onTreeRefreshed", e.onTreeRefreshed, !0);
|
|
778
|
-
const n =
|
|
791
|
+
const n = To(e.sources), i = xr(
|
|
779
792
|
t,
|
|
780
793
|
n.map((o) => o.id)
|
|
781
|
-
),
|
|
782
|
-
if (
|
|
794
|
+
), a = e.logger;
|
|
795
|
+
if (a !== void 0 && (a === null || typeof a != "object" || typeof a.error != "function"))
|
|
783
796
|
throw new TypeError("[markdown-explorer] logger must be an object with an error function");
|
|
784
|
-
const c = e.logger === void 0 ?
|
|
797
|
+
const c = e.logger === void 0 ? dn : e.logger, m = St(e.deny), h = e.deferredTimeoutMs === void 0 ? fo : fe("deferredTimeoutMs", e.deferredTimeoutMs, wo), x = e.textMaxBytes === void 0 ? tt : fe("textMaxBytes", e.textMaxBytes, rt), w = _o(e.edit);
|
|
785
798
|
if (w === void 0 && e.editMaxBytes !== void 0) throw new TypeError("[markdown-explorer] editMaxBytes needs the edit ports");
|
|
786
|
-
const y = e.editMaxBytes === void 0 ?
|
|
799
|
+
const y = e.editMaxBytes === void 0 ? tt : fe("editMaxBytes", e.editMaxBytes, rt), k = ko(e.treeCache), v = () => Date.now(), $ = k === !1 ? Kn(v) : Wn({ ...k, now: v }), A = Bn(e.sanitizer, t.multiView.maxPanels), R = Gn({ iframeHosts: t.iframeHosts, dataPath: t.dataPath, options: A, pool: Un(Fn(), A) }), D = new Map(n.map((o) => [o.id, o])), _ = Or(t), { authenticate: P, principalKey: C, signedOutPage: F, forbiddenPage: p, onTreeRefreshed: g } = e, E = (o, u) => {
|
|
787
800
|
if (typeof o.label == "string") return o.label;
|
|
788
801
|
const s = o.label(u);
|
|
789
802
|
if (typeof s != "string" || s.trim() === "") throw new TypeError(`[markdown-explorer] the label of source ${JSON.stringify(o.id)} must be a non-blank string`);
|
|
@@ -796,39 +809,39 @@ const un = (e) => e instanceof Error && Object.hasOwn(e, Pt) && Tt(e.reason), dn
|
|
|
796
809
|
} catch (l) {
|
|
797
810
|
return s(l), !1;
|
|
798
811
|
}
|
|
799
|
-
return typeof d == "boolean" ? d : (
|
|
800
|
-
},
|
|
812
|
+
return typeof d == "boolean" ? d : (et(d, s), s(new TypeError(`[markdown-explorer] boundary.contains of source ${JSON.stringify(o.id)} must return a boolean; got ${typeof d}`)), !1);
|
|
813
|
+
}, I = async (o, u, s) => {
|
|
801
814
|
const { confirm: d } = o.boundary;
|
|
802
815
|
if (d === void 0) return { status: "inside", readPath: s };
|
|
803
816
|
let l;
|
|
804
817
|
try {
|
|
805
818
|
l = await d(u, s);
|
|
806
|
-
} catch (
|
|
807
|
-
return { status: "error", failure: G(
|
|
819
|
+
} catch (z) {
|
|
820
|
+
return { status: "error", failure: G(z, { operation: "boundary", sourceId: o.id, path: s }, u.signal, c) };
|
|
808
821
|
}
|
|
809
|
-
const f = (
|
|
822
|
+
const f = (z) => (oe(c, { operation: "boundary", sourceId: o.id, path: s, error: new TypeError(`[markdown-explorer] boundary.confirm of source ${JSON.stringify(o.id)} ${z}`) }), { status: "outside" });
|
|
810
823
|
if (l === null || typeof l != "object") return f(`must resolve to { inside, readPath? }; got ${l === null ? "null" : typeof l}`);
|
|
811
|
-
const { inside: b, readPath:
|
|
812
|
-
return b === !1 ? { status: "outside" } : b !== !0 || typeof
|
|
824
|
+
const { inside: b, readPath: N } = l;
|
|
825
|
+
return b === !1 ? { status: "outside" } : b !== !0 || typeof N != "string" ? f("must resolve to { inside: false } or { inside: true, readPath: string }") : !q(N) || m.deniesPath(N) ? (oe(c, { operation: "boundary", sourceId: o.id, path: s, error: new RangeError(`[markdown-explorer] boundary.confirm of source ${JSON.stringify(o.id)} answered a readPath the explorer does not serve (unacceptable or denied)`) }), { status: "error", failure: { reason: "failed" } }) : { status: "inside", readPath: N };
|
|
813
826
|
}, M = async (o, u) => {
|
|
814
827
|
const s = n.filter((l) => T(l, u));
|
|
815
828
|
if (s.length === 0) return { status: "error", failure: { reason: "forbidden" } };
|
|
816
829
|
if (s.some((l) => l.boundary.confirm === void 0)) return { status: "inside", readPath: u };
|
|
817
830
|
let d = null;
|
|
818
831
|
for (const l of s) {
|
|
819
|
-
const f = await
|
|
832
|
+
const f = await I(l, o, u);
|
|
820
833
|
if (f.status === "inside") return f;
|
|
821
834
|
f.status === "error" && d === null && (d = f.failure);
|
|
822
835
|
}
|
|
823
836
|
return { status: "error", failure: d === null ? { reason: "forbidden" } : d };
|
|
824
837
|
}, U = (o, u, s) => {
|
|
825
|
-
const d = ({ path: l, reason: f }) =>
|
|
838
|
+
const d = ({ path: l, reason: f }) => mn(c, { operation: s.operation, sourceId: s.sourceId, path: l, error: new TypeError(`[markdown-explorer] an entry with ${f} was left out`) });
|
|
826
839
|
try {
|
|
827
|
-
return { status: "ok", entries:
|
|
840
|
+
return { status: "ok", entries: Qn(o, m, u.children !== void 0, d, (l) => T(u, l)) };
|
|
828
841
|
} catch (l) {
|
|
829
842
|
return oe(c, { ...s, error: l }), { status: "error", failure: { reason: "failed" } };
|
|
830
843
|
}
|
|
831
|
-
},
|
|
844
|
+
}, L = async (o, u, s) => {
|
|
832
845
|
let d;
|
|
833
846
|
try {
|
|
834
847
|
d = await o.tree(u);
|
|
@@ -837,17 +850,17 @@ const un = (e) => e instanceof Error && Object.hasOwn(e, Pt) && Tt(e.reason), dn
|
|
|
837
850
|
}
|
|
838
851
|
const l = U(d, o, { operation: "tree", sourceId: o.id });
|
|
839
852
|
return l.status === "ok" ? { status: "ok", entries: l.entries, loadedAt: s } : l;
|
|
840
|
-
}, X = (o, u, s, d) => (l, f) =>
|
|
853
|
+
}, X = (o, u, s, d) => (l, f) => L(o, { principal: u, request: s, signal: l, refresh: d }, f), V = (o, u, s, d, l) => $.load(O(o, s), l, X(o, u, d, !1)), J = (o, u, s, d) => $.refresh(O(o, s), d.signal, X(o, u, d, !0)), se = (o, u) => {
|
|
841
854
|
if (g === void 0) return;
|
|
842
855
|
const s = (d) => oe(c, { operation: "refresh", sourceId: o.id, error: d });
|
|
843
856
|
try {
|
|
844
|
-
|
|
857
|
+
et(g(u), s);
|
|
845
858
|
} catch (d) {
|
|
846
859
|
s(d);
|
|
847
860
|
}
|
|
848
|
-
},
|
|
861
|
+
}, H = async (o, u, s, d) => {
|
|
849
862
|
if (m.deniesPath(d) || !T(o, d)) return { status: "error", failure: { reason: "forbidden" } };
|
|
850
|
-
const l = await
|
|
863
|
+
const l = await I(o, s, d);
|
|
851
864
|
if (l.status === "outside") return { status: "error", failure: { reason: "forbidden" } };
|
|
852
865
|
if (l.status === "error") return { status: "error", failure: l.failure };
|
|
853
866
|
let f;
|
|
@@ -858,7 +871,7 @@ const un = (e) => e instanceof Error && Object.hasOwn(e, Pt) && Tt(e.reason), dn
|
|
|
858
871
|
}
|
|
859
872
|
return U(f, o, { operation: "children", sourceId: o.id, path: d });
|
|
860
873
|
}, Z = async (o, u, s, d) => {
|
|
861
|
-
const l = await
|
|
874
|
+
const l = await I(o, s, d);
|
|
862
875
|
if (l.status === "outside") return { status: "ok", ancestors: null };
|
|
863
876
|
if (l.status === "error") return { status: "error", failure: l.failure };
|
|
864
877
|
let f;
|
|
@@ -869,7 +882,7 @@ const un = (e) => e instanceof Error && Object.hasOwn(e, Pt) && Tt(e.reason), dn
|
|
|
869
882
|
}
|
|
870
883
|
if (Array.isArray(f) && f.length > t.revealDocument.maxDepth) return { status: "ok", ancestors: null };
|
|
871
884
|
try {
|
|
872
|
-
return { status: "ok", ancestors:
|
|
885
|
+
return { status: "ok", ancestors: Zn(f, d, m, (b) => T(o, b)) };
|
|
873
886
|
} catch (b) {
|
|
874
887
|
return oe(c, { operation: "ancestors", sourceId: o.id, path: d, error: b }), { status: "error", failure: { reason: "failed" } };
|
|
875
888
|
}
|
|
@@ -882,7 +895,7 @@ const un = (e) => e instanceof Error && Object.hasOwn(e, Pt) && Tt(e.reason), dn
|
|
|
882
895
|
const f = await Z(d, l, u, s);
|
|
883
896
|
if (f.status === "ok" && f.ancestors !== null) return { status: "ok", source: d.id, ancestors: f.ancestors };
|
|
884
897
|
}
|
|
885
|
-
return
|
|
898
|
+
return ho;
|
|
886
899
|
}, ie = async (o, u, s) => {
|
|
887
900
|
if (m.deniesPath(s)) return Q("forbidden");
|
|
888
901
|
const d = await M(o, s);
|
|
@@ -890,14 +903,14 @@ const un = (e) => e instanceof Error && Object.hasOwn(e, Pt) && Tt(e.reason), dn
|
|
|
890
903
|
let l;
|
|
891
904
|
try {
|
|
892
905
|
l = await e.document(o, s, d.readPath);
|
|
893
|
-
} catch (
|
|
894
|
-
return { kind: "failure", failure: G(
|
|
906
|
+
} catch (N) {
|
|
907
|
+
return { kind: "failure", failure: G(N, { operation: "document", path: s }, o.signal, c) };
|
|
895
908
|
}
|
|
896
909
|
let f;
|
|
897
910
|
try {
|
|
898
|
-
f =
|
|
899
|
-
} catch (
|
|
900
|
-
return oe(c, { operation: "document", path: s, error:
|
|
911
|
+
f = ro(l, (N) => Ue(t.dataPath, s, N), w !== void 0);
|
|
912
|
+
} catch (N) {
|
|
913
|
+
return oe(c, { operation: "document", path: s, error: N }), Q("failed");
|
|
901
914
|
}
|
|
902
915
|
if (f.kind !== "unsanitized-markdown") return f;
|
|
903
916
|
const b = await R(f.html, { scope: u, signal: o.signal });
|
|
@@ -905,7 +918,7 @@ const un = (e) => e instanceof Error && Object.hasOwn(e, Pt) && Tt(e.reason), dn
|
|
|
905
918
|
}, ye = async (o) => {
|
|
906
919
|
if (e.notices === void 0) return Pe;
|
|
907
920
|
try {
|
|
908
|
-
return
|
|
921
|
+
return lo(await e.notices(o));
|
|
909
922
|
} catch (u) {
|
|
910
923
|
return G(u, { operation: "notices" }, o.signal, c), Pe;
|
|
911
924
|
}
|
|
@@ -919,8 +932,8 @@ const un = (e) => e instanceof Error && Object.hasOwn(e, Pt) && Tt(e.reason), dn
|
|
|
919
932
|
const f = await M(o, d);
|
|
920
933
|
if (f.status === "error") return j(f.failure, s);
|
|
921
934
|
try {
|
|
922
|
-
const b =
|
|
923
|
-
return await an(o.request,
|
|
935
|
+
const b = l === "file", N = await e.asset(o, d, f.readPath, { redirect: b }), z = b ? uo(N) : Le(N);
|
|
936
|
+
return "kind" in z ? un(z.url, s) : await an(o.request, z, s, l);
|
|
924
937
|
} catch (b) {
|
|
925
938
|
return j(G(b, { operation: "asset", path: d }, o.signal, c), s);
|
|
926
939
|
}
|
|
@@ -930,8 +943,8 @@ const un = (e) => e instanceof Error && Object.hasOwn(e, Pt) && Tt(e.reason), dn
|
|
|
930
943
|
const s = await M(o, u);
|
|
931
944
|
if (s.status === "error") return { kind: "failure", failure: s.failure };
|
|
932
945
|
try {
|
|
933
|
-
const d =
|
|
934
|
-
return l === null ? Q("unsupported") : { kind: "text", name: d.name, text: l.text, truncated: l.truncated, url:
|
|
946
|
+
const d = Le(await e.asset(o, u, s.readPath, { redirect: !1 })), l = await wn(d, x);
|
|
947
|
+
return l === null ? Q("unsupported") : { kind: "text", name: d.name, text: l.text, truncated: l.truncated, url: Ue(t.dataPath, u, d.version) };
|
|
935
948
|
} catch (d) {
|
|
936
949
|
return { kind: "failure", failure: G(d, { operation: "text", path: u }, o.signal, c) };
|
|
937
950
|
}
|
|
@@ -939,7 +952,7 @@ const un = (e) => e instanceof Error && Object.hasOwn(e, Pt) && Tt(e.reason), dn
|
|
|
939
952
|
const d = await pe(u, "edit", s);
|
|
940
953
|
if (d.status === "error") return { kind: "failure", failure: d.failure };
|
|
941
954
|
try {
|
|
942
|
-
return
|
|
955
|
+
return oo(await o.source(u, s, d.readPath), y);
|
|
943
956
|
} catch (l) {
|
|
944
957
|
return { kind: "failure", failure: G(l, { operation: "edit", path: s }, u.signal, c) };
|
|
945
958
|
}
|
|
@@ -947,7 +960,7 @@ const un = (e) => e instanceof Error && Object.hasOwn(e, Pt) && Tt(e.reason), dn
|
|
|
947
960
|
const f = await pe(u, "save", s);
|
|
948
961
|
if (f.status === "error") return { kind: "failure", failure: f.failure };
|
|
949
962
|
try {
|
|
950
|
-
return
|
|
963
|
+
return so(await o.save(u, s, f.readPath, { text: l, baseVersion: d }), y);
|
|
951
964
|
} catch (b) {
|
|
952
965
|
return { kind: "failure", failure: G(b, { operation: "save", path: s }, u.signal, c) };
|
|
953
966
|
}
|
|
@@ -956,18 +969,18 @@ const un = (e) => e instanceof Error && Object.hasOwn(e, Pt) && Tt(e.reason), dn
|
|
|
956
969
|
if (f.status === "error") return { kind: "failure", failure: f.failure };
|
|
957
970
|
let b;
|
|
958
971
|
try {
|
|
959
|
-
b =
|
|
960
|
-
} catch (
|
|
961
|
-
return { kind: "failure", failure: G(
|
|
972
|
+
b = io(await o.preview(u, d, f.readPath, l));
|
|
973
|
+
} catch (z) {
|
|
974
|
+
return { kind: "failure", failure: G(z, { operation: "preview", path: d }, u.signal, c) };
|
|
962
975
|
}
|
|
963
976
|
if ("kind" in b) return b.kind === "failure" ? b : Q("failed");
|
|
964
|
-
const
|
|
965
|
-
return
|
|
977
|
+
const N = await R(b.html, { scope: s, signal: u.signal });
|
|
978
|
+
return N.status === "sanitized" ? { kind: "preview", htmlContent: N.html, headings: b.headings } : (N.cause !== null && oe(c, { operation: "preview", path: d, error: N.cause }), Q(N.reason));
|
|
966
979
|
}, Vt = async (o, u, s) => {
|
|
967
980
|
const d = await pe(u, "revisions", s);
|
|
968
981
|
if (d.status === "error") return { kind: "failure", failure: d.failure };
|
|
969
982
|
try {
|
|
970
|
-
return { kind: "revisions", revisions:
|
|
983
|
+
return { kind: "revisions", revisions: ao(await o.revisions(u, s, d.readPath)) };
|
|
971
984
|
} catch (l) {
|
|
972
985
|
return { kind: "failure", failure: G(l, { operation: "revisions", path: s }, u.signal, c) };
|
|
973
986
|
}
|
|
@@ -975,7 +988,7 @@ const un = (e) => e instanceof Error && Object.hasOwn(e, Pt) && Tt(e.reason), dn
|
|
|
975
988
|
const l = await pe(u, "revision", s);
|
|
976
989
|
if (l.status === "error") return { kind: "failure", failure: l.failure };
|
|
977
990
|
try {
|
|
978
|
-
return { kind: "revision", id: d, text:
|
|
991
|
+
return { kind: "revision", id: d, text: co(await o.revision(u, s, l.readPath, d), y) };
|
|
979
992
|
} catch (f) {
|
|
980
993
|
return { kind: "failure", failure: G(f, { operation: "revision", path: s }, u.signal, c) };
|
|
981
994
|
}
|
|
@@ -988,37 +1001,37 @@ const un = (e) => e instanceof Error && Object.hasOwn(e, Pt) && Tt(e.reason), dn
|
|
|
988
1001
|
const re = ce(l, ae.revision);
|
|
989
1002
|
return he(re) ? W(200, JSON.stringify(await Zt(o, s, b, re)), f) : j({ reason: "invalid" }, f);
|
|
990
1003
|
}
|
|
991
|
-
const
|
|
992
|
-
if (u === "save" && !he(
|
|
993
|
-
const
|
|
994
|
-
return
|
|
1004
|
+
const N = ce(l, ae.base);
|
|
1005
|
+
if (u === "save" && !he(N)) return j({ reason: "invalid" }, f);
|
|
1006
|
+
const z = await vo(s.request, y);
|
|
1007
|
+
return z.status === "error" ? j(z.failure, f) : u === "preview" ? W(200, JSON.stringify(await Kt(o, s, d, b, z.text)), f) : W(200, JSON.stringify(await Wt(o, s, b, N, z.text)), f);
|
|
995
1008
|
}, qt = async ({ request: o, url: u, params: s }) => {
|
|
996
|
-
const d = mt(await
|
|
997
|
-
if (l.set("Cache-Control", Dt), d.status === "signed-out") throw pt("signedOutPage",
|
|
998
|
-
if (d.status === "forbidden") throw p === void 0 ?
|
|
999
|
-
const f = _r(
|
|
1009
|
+
const d = mt(await P(o, dt)), l = lt(d.headers);
|
|
1010
|
+
if (l.set("Cache-Control", Dt), d.status === "signed-out") throw pt("signedOutPage", F(o, l));
|
|
1011
|
+
if (d.status === "forbidden") throw p === void 0 ? Xe(null, { status: 403, headers: l }) : pt("forbiddenPage", p(o, l));
|
|
1012
|
+
const f = _r(i, s["*"], u.searchParams);
|
|
1000
1013
|
if (f.kind === "redirect") throw Sr(f.href, { headers: l });
|
|
1001
|
-
const { location: b } = f, { principal:
|
|
1014
|
+
const { location: b } = f, { principal: N } = d, z = wt(r, C(N)), re = D.get(b.source);
|
|
1002
1015
|
if (re === void 0) throw new RangeError(`[markdown-explorer] no source for ${JSON.stringify(b.source)}`);
|
|
1003
1016
|
const we = { signal: o.signal, timeoutMs: h, late: null, handOverMs: h }, ne = {
|
|
1004
|
-
scope:
|
|
1005
|
-
sources: n.map((
|
|
1017
|
+
scope: z,
|
|
1018
|
+
sources: n.map((B) => ({ id: B.id, label: E(B, N) })),
|
|
1006
1019
|
settings: Ar(Nr(o.headers.get("Cookie"), t.settingsCookie), _),
|
|
1007
1020
|
tree: b.view === "single" ? null : {
|
|
1008
1021
|
sourceId: b.source,
|
|
1009
|
-
result: _e(we, (
|
|
1022
|
+
result: _e(we, (B) => V(re, N, z, o, B).then((Y) => Y.result))
|
|
1010
1023
|
},
|
|
1011
|
-
documents:
|
|
1012
|
-
notices: _e({ signal: o.signal, timeoutMs: h, late: Pe, handOverMs: 0 }, (
|
|
1024
|
+
documents: So(b).map((B) => ({ path: B, document: _e(we, (Y) => ie({ principal: N, request: o, signal: Y, refresh: !1 }, z, B)) })),
|
|
1025
|
+
notices: _e({ signal: o.signal, timeoutMs: h, late: Pe, handOverMs: 0 }, (B) => ye({ principal: N, request: o, signal: B, refresh: !1 }))
|
|
1013
1026
|
};
|
|
1014
|
-
return
|
|
1015
|
-
},
|
|
1016
|
-
const s = mt(await
|
|
1027
|
+
return Xe(ne, { headers: l });
|
|
1028
|
+
}, Ce = async (o, u) => {
|
|
1029
|
+
const s = mt(await P(o, u));
|
|
1017
1030
|
return { authentication: s, headers: lt(s.headers) };
|
|
1018
1031
|
}, er = async (o, u, s) => {
|
|
1019
1032
|
let d;
|
|
1020
1033
|
try {
|
|
1021
|
-
d = await
|
|
1034
|
+
d = await Ce(u, dt);
|
|
1022
1035
|
} catch (Y) {
|
|
1023
1036
|
return j(G(ft(Y), { operation: "authenticate" }, u.signal, c));
|
|
1024
1037
|
}
|
|
@@ -1027,54 +1040,54 @@ const un = (e) => e instanceof Error && Object.hasOwn(e, Pt) && Tt(e.reason), dn
|
|
|
1027
1040
|
if (l.status === "forbidden") return j({ reason: "forbidden" }, f);
|
|
1028
1041
|
const { principal: b } = l;
|
|
1029
1042
|
if (o === "asset") return Jt({ principal: b, request: u, signal: u.signal, refresh: !1 }, s, f);
|
|
1030
|
-
const
|
|
1031
|
-
f.set(yr,
|
|
1032
|
-
const
|
|
1043
|
+
const N = wt(r, C(b));
|
|
1044
|
+
f.set(yr, N);
|
|
1045
|
+
const z = { principal: b, request: u, signal: u.signal, refresh: !1 }, re = () => j({ reason: "invalid" }, f);
|
|
1033
1046
|
if (o === "document") {
|
|
1034
1047
|
const Y = ce(s, ae.path);
|
|
1035
1048
|
if (Y === null || !q(Y)) return re();
|
|
1036
|
-
const
|
|
1037
|
-
return
|
|
1049
|
+
const Fe = u.headers.has(je);
|
|
1050
|
+
return Fe && !yt(u) ? j({ reason: "forbidden" }, f) : W(200, JSON.stringify(await ie({ ...z, refresh: Fe }, N, Y)), f);
|
|
1038
1051
|
}
|
|
1039
1052
|
if (o === "text") {
|
|
1040
1053
|
const Y = ce(s, ae.path);
|
|
1041
|
-
return Y === null || !q(Y) ? re() : W(200, JSON.stringify(await Gt(
|
|
1054
|
+
return Y === null || !q(Y) ? re() : W(200, JSON.stringify(await Gt(z, Y)), f);
|
|
1042
1055
|
}
|
|
1043
1056
|
if (o === "edit" || o === "save" || o === "preview" || o === "revisions" || o === "revision")
|
|
1044
|
-
return w === void 0 ? j({ reason: "not-found" }, f) : Qt(w, o,
|
|
1057
|
+
return w === void 0 ? j({ reason: "not-found" }, f) : Qt(w, o, z, N, s, f);
|
|
1045
1058
|
const we = ce(s, ae.source), ne = ce(s, ae.path);
|
|
1046
1059
|
if (we === null || (o === "children" || o === "ancestors" || o === "reveal") && (ne === null || !q(ne))) return re();
|
|
1047
|
-
const
|
|
1048
|
-
if (
|
|
1049
|
-
if (o === "tree") return W(200, (await V(
|
|
1060
|
+
const B = D.get(we);
|
|
1061
|
+
if (B === void 0) return j({ reason: "not-found" }, f);
|
|
1062
|
+
if (o === "tree") return W(200, (await V(B, b, N, u, u.signal)).json, f);
|
|
1050
1063
|
if (o === "refresh") {
|
|
1051
|
-
const Y = await J(
|
|
1052
|
-
return u.signal.aborted || se(
|
|
1064
|
+
const Y = await J(B, b, N, u);
|
|
1065
|
+
return u.signal.aborted || se(B, O(B, N)), W(200, Y.json, f);
|
|
1053
1066
|
}
|
|
1054
|
-
return o === "ancestors" ?
|
|
1067
|
+
return o === "ancestors" ? B.ancestors === void 0 || ne === null ? j({ reason: "not-found" }, f) : W(200, JSON.stringify(await ee(B, B.ancestors, z, ne)), f) : o === "reveal" ? ne === null ? re() : W(200, JSON.stringify(await te(B, z, ne)), f) : B.children === void 0 || ne === null ? j({ reason: "not-found" }, f) : W(200, JSON.stringify(await H(B, B.children, z, ne)), f);
|
|
1055
1068
|
}, tr = async (o) => {
|
|
1056
|
-
const { authentication: u, headers: s } = await
|
|
1069
|
+
const { authentication: u, headers: s } = await Ce(o, yo);
|
|
1057
1070
|
if (!s.keys().next().done) throw new TypeError("[markdown-explorer] authenticate returned headers in read-only mode; a read-only authentication must only read the session (none was forwarded)");
|
|
1058
1071
|
return u.status === "signed-in";
|
|
1059
|
-
},
|
|
1072
|
+
}, He = t.embeds.linkCardEndpoint === null ? null : kr({ authorize: tr }), rr = async (o, u) => {
|
|
1060
1073
|
try {
|
|
1061
1074
|
return await o(u);
|
|
1062
1075
|
} catch (s) {
|
|
1063
1076
|
return j(G(ft(s), { operation: "authenticate" }, u.signal, c));
|
|
1064
1077
|
}
|
|
1065
|
-
},
|
|
1078
|
+
}, Be = async (o, u, s) => {
|
|
1066
1079
|
if (new URL(o.url).pathname.endsWith(".data")) return j({ reason: "not-found" });
|
|
1067
1080
|
if (!mr(s)) return j({ reason: "not-found" });
|
|
1068
1081
|
if (s === "link-card") {
|
|
1069
|
-
if (
|
|
1082
|
+
if (He === null) return j({ reason: "not-found" });
|
|
1070
1083
|
const l = ht(o, s);
|
|
1071
|
-
return l !== null ? l : rr(
|
|
1084
|
+
return l !== null ? l : rr(He, o);
|
|
1072
1085
|
}
|
|
1073
|
-
if (
|
|
1086
|
+
if (Oo.has(s) && w === void 0) return j({ reason: "not-found" });
|
|
1074
1087
|
const d = ht(o, s);
|
|
1075
1088
|
if (d !== null) return d;
|
|
1076
1089
|
if (s === "refresh" && !yt(o)) return j({ reason: "forbidden" });
|
|
1077
|
-
if ((s === "save" || s === "preview") && !
|
|
1090
|
+
if ((s === "save" || s === "preview") && !Ro(o)) return j({ reason: "forbidden" });
|
|
1078
1091
|
try {
|
|
1079
1092
|
return await er(s, o, u.searchParams);
|
|
1080
1093
|
} catch (l) {
|
|
@@ -1083,15 +1096,15 @@ const un = (e) => e instanceof Error && Object.hasOwn(e, Pt) && Tt(e.reason), dn
|
|
|
1083
1096
|
};
|
|
1084
1097
|
return Object.freeze({
|
|
1085
1098
|
loader: qt,
|
|
1086
|
-
headers:
|
|
1087
|
-
dataLoader: ({ request: o, url: u, params: s }) =>
|
|
1088
|
-
dataAction: ({ request: o, url: u, params: s }) =>
|
|
1099
|
+
headers: Ao,
|
|
1100
|
+
dataLoader: ({ request: o, url: u, params: s }) => Be(o, u, s["*"]),
|
|
1101
|
+
dataAction: ({ request: o, url: u, params: s }) => Be(o, u, s["*"]),
|
|
1089
1102
|
forgetTree: (o) => {
|
|
1090
1103
|
if (typeof o != "string") throw new TypeError(`[markdown-explorer] forgetTree needs the string key onTreeRefreshed received; got ${typeof o}`);
|
|
1091
|
-
|
|
1104
|
+
$.forget(o);
|
|
1092
1105
|
}
|
|
1093
1106
|
});
|
|
1094
|
-
}, be = Object.freeze({ maxEntries: 2e4, maxDocumentBytes: 2 * 1024 * 1024, textPreviewBytes: 8 * 1024, walkConcurrency: 8 }),
|
|
1107
|
+
}, be = Object.freeze({ maxEntries: 2e4, maxDocumentBytes: 2 * 1024 * 1024, textPreviewBytes: 8 * 1024, walkConcurrency: 8 }), No = /* @__PURE__ */ new Set(["md", "markdown", "mdx"]), Ct = new Map([
|
|
1095
1108
|
...[
|
|
1096
1109
|
["png", "image/png"],
|
|
1097
1110
|
["jpg", "image/jpeg"],
|
|
@@ -1126,7 +1139,7 @@ const un = (e) => e instanceof Error && Object.hasOwn(e, Pt) && Tt(e.reason), dn
|
|
|
1126
1139
|
["wma", "audio/x-ms-wma"]
|
|
1127
1140
|
].map(([e, t]) => [e, { kind: "media", mediaType: "audio", contentType: t }]),
|
|
1128
1141
|
["pdf", { kind: "media", mediaType: "pdf", contentType: "application/pdf" }]
|
|
1129
|
-
]),
|
|
1142
|
+
]), Io = /* @__PURE__ */ new Set([
|
|
1130
1143
|
"txt",
|
|
1131
1144
|
"text",
|
|
1132
1145
|
"log",
|
|
@@ -1251,7 +1264,7 @@ const un = (e) => e instanceof Error && Object.hasOwn(e, Pt) && Tt(e.reason), dn
|
|
|
1251
1264
|
"eslintrc",
|
|
1252
1265
|
"babelrc",
|
|
1253
1266
|
"mailmap"
|
|
1254
|
-
]),
|
|
1267
|
+
]), Po = /* @__PURE__ */ new Set([
|
|
1255
1268
|
"readme",
|
|
1256
1269
|
"license",
|
|
1257
1270
|
"licence",
|
|
@@ -1272,16 +1285,16 @@ const un = (e) => e instanceof Error && Object.hasOwn(e, Pt) && Tt(e.reason), dn
|
|
|
1272
1285
|
"jenkinsfile",
|
|
1273
1286
|
"brewfile",
|
|
1274
1287
|
"codeowners"
|
|
1275
|
-
]), Ht = typeof me.O_NOFOLLOW == "number" ? me.O_NOFOLLOW : 0, Bt = typeof me.O_NONBLOCK == "number" ? me.O_NONBLOCK : 0, gt = (e) => `${e.size}-${e.mtimeNs}`,
|
|
1288
|
+
]), Ht = typeof me.O_NOFOLLOW == "number" ? me.O_NOFOLLOW : 0, Bt = typeof me.O_NONBLOCK == "number" ? me.O_NONBLOCK : 0, gt = (e) => `${e.size}-${e.mtimeNs}`, Mo = (e) => e !== "" && e !== "." && e !== ".." && !e.includes(":") && !e.endsWith(".") && !e.endsWith(" "), Et = (e) => q(e) && e === e.normalize("NFC") && e.split("/").every(Mo), Ft = (e) => {
|
|
1276
1289
|
const t = e.toLowerCase(), r = t.lastIndexOf(".");
|
|
1277
1290
|
return r < 0 ? "" : t.slice(r + 1);
|
|
1278
|
-
},
|
|
1291
|
+
}, Lo = (e) => {
|
|
1279
1292
|
const t = Ft(e);
|
|
1280
|
-
if (
|
|
1293
|
+
if (No.has(t)) return { kind: "markdown" };
|
|
1281
1294
|
const r = Ct.get(t);
|
|
1282
1295
|
if (r !== void 0) return r.kind === "image" ? { kind: "image" } : { kind: "media", mediaType: r.mediaType };
|
|
1283
1296
|
const n = e.toLowerCase();
|
|
1284
|
-
return
|
|
1297
|
+
return Io.has(t) || !n.includes(".") && Po.has(n) ? { kind: "text" } : { kind: "file" };
|
|
1285
1298
|
}, $e = (e) => {
|
|
1286
1299
|
switch (typeof e == "object" && e !== null ? e.code : void 0) {
|
|
1287
1300
|
case "ENOENT":
|
|
@@ -1304,7 +1317,7 @@ const un = (e) => e instanceof Error && Object.hasOwn(e, Pt) && Tt(e.reason), dn
|
|
|
1304
1317
|
}, Xt = (e) => {
|
|
1305
1318
|
const t = typeof e == "object" && e !== null ? e.code : void 0;
|
|
1306
1319
|
return t === "EACCES" || t === "EPERM" || t === "ENOENT" || t === "ENOTDIR";
|
|
1307
|
-
},
|
|
1320
|
+
}, $o = async (e) => {
|
|
1308
1321
|
try {
|
|
1309
1322
|
return Math.trunc((await vt(e)).mtimeMs);
|
|
1310
1323
|
} catch (t) {
|
|
@@ -1316,208 +1329,208 @@ const un = (e) => e instanceof Error && Object.hasOwn(e, Pt) && Tt(e.reason), dn
|
|
|
1316
1329
|
for await (const n of e.createReadStream({ start: 0, end: t - 1, autoClose: !1 }))
|
|
1317
1330
|
r.push(n);
|
|
1318
1331
|
return Buffer.concat(r);
|
|
1319
|
-
},
|
|
1320
|
-
let
|
|
1332
|
+
}, jo = (e, t) => e !== null ? { start: e.offset, end: e.offset + e.length - 1 } : t === 0n ? {} : { start: 0, end: Number(t) - 1 }, zo = async (e, t, r, n) => {
|
|
1333
|
+
let i;
|
|
1321
1334
|
try {
|
|
1322
|
-
|
|
1323
|
-
const h = await
|
|
1335
|
+
i = await Rt(e, me.O_RDONLY | Ht | Bt);
|
|
1336
|
+
const h = await i.stat({ bigint: !0 });
|
|
1324
1337
|
if (!h.isFile() || h.ino !== t.ino || h.dev !== t.dev || h.size !== t.size || h.mtimeNs !== t.mtimeNs) throw new Se("not-found");
|
|
1325
1338
|
} catch (h) {
|
|
1326
|
-
throw await
|
|
1339
|
+
throw await i?.close(), Ut(h);
|
|
1327
1340
|
}
|
|
1328
|
-
const
|
|
1329
|
-
|
|
1341
|
+
const a = i.createReadStream(jo(r, t.size)), c = Br.toWeb(a), m = () => {
|
|
1342
|
+
a.destroy(n.reason);
|
|
1330
1343
|
};
|
|
1331
1344
|
return n.aborted ? m() : n.addEventListener("abort", m, { once: !0 }), c;
|
|
1332
|
-
},
|
|
1345
|
+
}, Do = (e, t) => {
|
|
1333
1346
|
const r = Pr.compare(e, t);
|
|
1334
1347
|
return r !== 0 ? r : e === t ? 0 : e < t ? -1 : 1;
|
|
1335
|
-
},
|
|
1348
|
+
}, Co = (e) => {
|
|
1336
1349
|
let t = 0;
|
|
1337
1350
|
const r = [];
|
|
1338
1351
|
return async (n) => {
|
|
1339
|
-
t < e ? t += 1 : await new Promise((
|
|
1352
|
+
t < e ? t += 1 : await new Promise((i) => r.push(i));
|
|
1340
1353
|
try {
|
|
1341
1354
|
return await n();
|
|
1342
1355
|
} finally {
|
|
1343
|
-
const
|
|
1344
|
-
|
|
1356
|
+
const i = r.shift();
|
|
1357
|
+
i === void 0 ? t -= 1 : i();
|
|
1345
1358
|
}
|
|
1346
1359
|
};
|
|
1347
|
-
},
|
|
1360
|
+
}, Ho = (e, t) => {
|
|
1348
1361
|
if (e.isSymbolicLink()) return "symbolic-link";
|
|
1349
1362
|
if (!(e.isDirectory() || e.isFile())) return "special-file";
|
|
1350
1363
|
const { name: r } = e;
|
|
1351
1364
|
return r !== r.normalize("NFC") ? "not-nfc" : r.includes(":") ? "colon" : r.endsWith(".") || r.endsWith(" ") ? "trailing-dot-or-space" : q(r) ? q(t) ? null : "path-too-long" : "control-character-or-backslash";
|
|
1352
|
-
},
|
|
1365
|
+
}, Bo = /* @__PURE__ */ new Set(["id", "label", "root", "pathPrefix", "parse", "deny", "maxEntries", "maxDocumentBytes", "textPreviewBytes", "walkConcurrency", "cacheScope", "reportSkipped"]), Te = (e, t, r) => {
|
|
1353
1366
|
if (t === void 0) return r;
|
|
1354
1367
|
if (typeof t != "number" || !Number.isSafeInteger(t) || t <= 0) throw new RangeError(`[markdown-explorer] ${e} must be a positive integer; got ${String(t)}`);
|
|
1355
1368
|
return t;
|
|
1356
|
-
},
|
|
1369
|
+
}, ns = (e) => {
|
|
1357
1370
|
if (e === null || typeof e != "object") throw new TypeError("[markdown-explorer] createFileSystemSource needs an options object");
|
|
1358
1371
|
for (const E of Object.keys(e))
|
|
1359
|
-
if (!
|
|
1360
|
-
const { id: t, label: r, root: n, parse:
|
|
1372
|
+
if (!Bo.has(E)) throw new TypeError(`[markdown-explorer] createFileSystemSource does not know the option ${JSON.stringify(E)}`);
|
|
1373
|
+
const { id: t, label: r, root: n, parse: i } = e;
|
|
1361
1374
|
if (typeof t != "string" || t === "") throw new TypeError("[markdown-explorer] a file-system source needs a non-empty id");
|
|
1362
1375
|
if (typeof r != "string" || r.trim() === "") throw new TypeError("[markdown-explorer] a file-system source needs a non-blank label");
|
|
1363
|
-
if (typeof
|
|
1364
|
-
if (typeof n != "string" || !
|
|
1365
|
-
let
|
|
1376
|
+
if (typeof i != "function") throw new TypeError("[markdown-explorer] a file-system source needs a parse function");
|
|
1377
|
+
if (typeof n != "string" || !Je(n)) throw new RangeError(`[markdown-explorer] root must be an absolute path; got ${JSON.stringify(n)}`);
|
|
1378
|
+
let a;
|
|
1366
1379
|
try {
|
|
1367
|
-
|
|
1380
|
+
a = Lr(n).isDirectory();
|
|
1368
1381
|
} catch (E) {
|
|
1369
1382
|
throw new RangeError(`[markdown-explorer] root does not exist: ${n}`, { cause: E });
|
|
1370
1383
|
}
|
|
1371
|
-
if (!
|
|
1372
|
-
const c =
|
|
1384
|
+
if (!a) throw new RangeError(`[markdown-explorer] root is not a directory: ${n}`);
|
|
1385
|
+
const c = $r(n), m = e.pathPrefix;
|
|
1373
1386
|
if (typeof m != "string") throw new TypeError('[markdown-explorer] pathPrefix is required: "" for a source over the whole root, or relative segments ending with "/"');
|
|
1374
1387
|
if (m !== "" && !(m.endsWith("/") && Et(m.slice(0, -1))))
|
|
1375
1388
|
throw new RangeError(`[markdown-explorer] pathPrefix must be empty or relative segments ending with "/"; got ${JSON.stringify(m)}`);
|
|
1376
|
-
const h = St(e.deny),
|
|
1389
|
+
const h = St(e.deny), x = Te("maxEntries", e.maxEntries, be.maxEntries), w = Te("maxDocumentBytes", e.maxDocumentBytes, be.maxDocumentBytes), y = Te("textPreviewBytes", e.textPreviewBytes, be.textPreviewBytes), k = Te("walkConcurrency", e.walkConcurrency, be.walkConcurrency), v = e.cacheScope === void 0 ? "principal" : e.cacheScope;
|
|
1377
1390
|
if (v !== "principal" && v !== "shared") throw new RangeError(`[markdown-explorer] cacheScope must be "principal" or "shared"; got ${JSON.stringify(v)}`);
|
|
1378
|
-
const
|
|
1379
|
-
if (
|
|
1380
|
-
const A =
|
|
1391
|
+
const $ = e.reportSkipped;
|
|
1392
|
+
if ($ !== void 0 && typeof $ != "function") throw new TypeError("[markdown-explorer] reportSkipped must be a function");
|
|
1393
|
+
const A = $ === void 0 ? (E) => {
|
|
1381
1394
|
console.warn(`[markdown-explorer] the file-system source ${JSON.stringify(E.sourceId)} does not list ${JSON.stringify(E.path)}: ${E.reason}`);
|
|
1382
|
-
} :
|
|
1383
|
-
const O = zr(c, ...E.split("/")), T =
|
|
1384
|
-
return T === "" || T === ".." || T.startsWith(`..${
|
|
1395
|
+
} : $, R = /* @__PURE__ */ new Set(), D = (E) => {
|
|
1396
|
+
const O = zr(c, ...E.split("/")), T = Ge(c, O);
|
|
1397
|
+
return T === "" || T === ".." || T.startsWith(`..${Ye}`) || Je(T) ? null : O;
|
|
1385
1398
|
}, _ = (E, O) => {
|
|
1386
1399
|
const T = `${O}\0${E}`;
|
|
1387
|
-
R.has(T) || R.size >=
|
|
1388
|
-
},
|
|
1400
|
+
R.has(T) || R.size >= x || (R.add(T), A({ sourceId: t, path: E, reason: O }));
|
|
1401
|
+
}, P = async ({ signal: E }) => {
|
|
1389
1402
|
E.throwIfAborted();
|
|
1390
1403
|
const O = new AbortController(), T = () => {
|
|
1391
1404
|
O.abort(E.reason);
|
|
1392
1405
|
};
|
|
1393
1406
|
E.addEventListener("abort", T, { once: !0 });
|
|
1394
|
-
const
|
|
1407
|
+
const I = Co(k);
|
|
1395
1408
|
let M = 0;
|
|
1396
|
-
const U = async (
|
|
1397
|
-
const V = await
|
|
1409
|
+
const U = async (L, X) => {
|
|
1410
|
+
const V = await I(async () => {
|
|
1398
1411
|
O.signal.throwIfAborted();
|
|
1399
1412
|
try {
|
|
1400
|
-
return await Hr(
|
|
1401
|
-
} catch (
|
|
1402
|
-
if (X === "" || !Xt(
|
|
1413
|
+
return await Hr(L, { withFileTypes: !0 });
|
|
1414
|
+
} catch (H) {
|
|
1415
|
+
if (X === "" || !Xt(H)) throw H;
|
|
1403
1416
|
return null;
|
|
1404
1417
|
}
|
|
1405
1418
|
});
|
|
1406
1419
|
if (O.signal.throwIfAborted(), V === null)
|
|
1407
1420
|
return _(`${m}${X}`, "unreadable-directory"), null;
|
|
1408
1421
|
const J = [];
|
|
1409
|
-
for (const
|
|
1410
|
-
if (h.deniesName(
|
|
1411
|
-
const Z = X === "" ?
|
|
1422
|
+
for (const H of V) {
|
|
1423
|
+
if (h.deniesName(H.name)) continue;
|
|
1424
|
+
const Z = X === "" ? H.name : `${X}/${H.name}`, ee = `${m}${Z}`, te = Ho(H, ee);
|
|
1412
1425
|
if (te !== null) {
|
|
1413
1426
|
_(ee, te);
|
|
1414
1427
|
continue;
|
|
1415
1428
|
}
|
|
1416
|
-
J.push({ dirent:
|
|
1429
|
+
J.push({ dirent: H, relativePath: Z, path: ee });
|
|
1417
1430
|
}
|
|
1418
|
-
if (M += J.length, M >
|
|
1419
|
-
return J.sort((
|
|
1420
|
-
J.map(async ({ dirent:
|
|
1421
|
-
const te = await
|
|
1422
|
-
if (!
|
|
1423
|
-
const ye = await U(
|
|
1424
|
-
return ye === null ? null : { name:
|
|
1431
|
+
if (M += J.length, M > x) throw new Se("too-large");
|
|
1432
|
+
return J.sort((H, Z) => Do(H.dirent.name, Z.dirent.name)), (await Promise.all(
|
|
1433
|
+
J.map(async ({ dirent: H, relativePath: Z, path: ee }) => {
|
|
1434
|
+
const te = await $o(We(L, H.name)), ie = te === void 0 ? {} : { modifiedAt: te };
|
|
1435
|
+
if (!H.isDirectory()) return { name: H.name, path: ee, type: "file", ...ie };
|
|
1436
|
+
const ye = await U(We(L, H.name), Z);
|
|
1437
|
+
return ye === null ? null : { name: H.name, path: ee, type: "directory", ...ie, children: ye };
|
|
1425
1438
|
})
|
|
1426
|
-
)).filter((
|
|
1439
|
+
)).filter((H) => H !== null);
|
|
1427
1440
|
};
|
|
1428
1441
|
try {
|
|
1429
|
-
const
|
|
1430
|
-
if (
|
|
1431
|
-
return
|
|
1432
|
-
} catch (
|
|
1433
|
-
throw O.abort(),
|
|
1442
|
+
const L = await U(c, "");
|
|
1443
|
+
if (L === null) throw new Error("[markdown-explorer] the source's root could not be listed");
|
|
1444
|
+
return L;
|
|
1445
|
+
} catch (L) {
|
|
1446
|
+
throw O.abort(), L;
|
|
1434
1447
|
} finally {
|
|
1435
1448
|
E.removeEventListener("abort", T);
|
|
1436
1449
|
}
|
|
1437
|
-
},
|
|
1450
|
+
}, C = async (E) => {
|
|
1438
1451
|
if (!E.startsWith(m)) return { status: "refused", reason: "not-found" };
|
|
1439
1452
|
const O = E.slice(m.length);
|
|
1440
1453
|
if (!Et(O)) return { status: "refused", reason: "invalid" };
|
|
1441
1454
|
if (h.deniesPath(O)) return { status: "refused", reason: "forbidden" };
|
|
1442
|
-
const T =
|
|
1455
|
+
const T = D(O);
|
|
1443
1456
|
if (T === null) return { status: "refused", reason: "forbidden" };
|
|
1444
|
-
const
|
|
1445
|
-
if (
|
|
1446
|
-
if (!
|
|
1457
|
+
const I = await vt(T, { bigint: !0 });
|
|
1458
|
+
if (I.isSymbolicLink()) return { status: "refused", reason: "forbidden" };
|
|
1459
|
+
if (!I.isFile()) return { status: "refused", reason: "unsupported" };
|
|
1447
1460
|
const M = await Cr(T);
|
|
1448
|
-
return M !== T || h.deniesPath(
|
|
1449
|
-
},
|
|
1450
|
-
let
|
|
1461
|
+
return M !== T || h.deniesPath(Ge(c, M).split(Ye).join("/")) ? { status: "refused", reason: "forbidden" } : { status: "file", target: T, name: O.slice(O.lastIndexOf("/") + 1), stats: I };
|
|
1462
|
+
}, F = async (E, O, T) => {
|
|
1463
|
+
let I;
|
|
1451
1464
|
try {
|
|
1452
|
-
const M = await
|
|
1465
|
+
const M = await C(T);
|
|
1453
1466
|
if (M.status === "refused") return Q(M.reason);
|
|
1454
|
-
const { target: U, name:
|
|
1455
|
-
if (V.kind === "image") return { kind: "image", name:
|
|
1456
|
-
if (V.kind === "media") return { kind: "media", name:
|
|
1457
|
-
if (V.kind === "file") return { kind: "file", name:
|
|
1458
|
-
|
|
1459
|
-
const se = await
|
|
1467
|
+
const { target: U, name: L, stats: X } = M, V = Lo(L), J = gt(X);
|
|
1468
|
+
if (V.kind === "image") return { kind: "image", name: L, version: J };
|
|
1469
|
+
if (V.kind === "media") return { kind: "media", name: L, mediaType: V.mediaType, version: J };
|
|
1470
|
+
if (V.kind === "file") return { kind: "file", name: L, version: J };
|
|
1471
|
+
I = await Rt(U, me.O_RDONLY | Ht | Bt);
|
|
1472
|
+
const se = await I.stat({ bigint: !0 });
|
|
1460
1473
|
if (!se.isFile() || se.ino !== X.ino || se.dev !== X.dev) return Q("forbidden");
|
|
1461
1474
|
if (V.kind === "markdown") {
|
|
1462
1475
|
if (se.size > BigInt(w)) return Q("too-large");
|
|
1463
|
-
const te = await bt(
|
|
1476
|
+
const te = await bt(I, w + 1);
|
|
1464
1477
|
if (te.length > w) return Q("too-large");
|
|
1465
|
-
const ie = await
|
|
1466
|
-
return { kind: "markdown", name:
|
|
1478
|
+
const ie = await i(new TextDecoder("utf-8").decode(te), O);
|
|
1479
|
+
return { kind: "markdown", name: L, htmlContent: ie.htmlContent, headings: ie.headings, edit: null };
|
|
1467
1480
|
}
|
|
1468
|
-
const
|
|
1469
|
-
if (
|
|
1470
|
-
const Z = se.size > BigInt(y), ee = new TextDecoder("utf-8").decode(
|
|
1471
|
-
return { kind: "text", name:
|
|
1481
|
+
const H = await bt(I, y);
|
|
1482
|
+
if (H.includes(0)) return { kind: "file", name: L, version: J };
|
|
1483
|
+
const Z = se.size > BigInt(y), ee = new TextDecoder("utf-8").decode(H, { stream: Z });
|
|
1484
|
+
return { kind: "text", name: L, text: ee, truncated: Z, version: J };
|
|
1472
1485
|
} catch (M) {
|
|
1473
1486
|
const U = $e(M);
|
|
1474
1487
|
if (U === null) throw M;
|
|
1475
1488
|
return Q(U);
|
|
1476
1489
|
} finally {
|
|
1477
|
-
await
|
|
1490
|
+
await I?.close();
|
|
1478
1491
|
}
|
|
1479
1492
|
}, p = async (E, O, T) => {
|
|
1480
|
-
let
|
|
1493
|
+
let I;
|
|
1481
1494
|
try {
|
|
1482
|
-
|
|
1495
|
+
I = await C(T);
|
|
1483
1496
|
} catch (J) {
|
|
1484
1497
|
throw Ut(J);
|
|
1485
1498
|
}
|
|
1486
|
-
if (
|
|
1487
|
-
const { target: M, name: U, stats:
|
|
1488
|
-
return { name: U, contentType: V, size: Number(
|
|
1499
|
+
if (I.status === "refused") throw new Se(I.reason);
|
|
1500
|
+
const { target: M, name: U, stats: L } = I, X = Ct.get(Ft(U)), V = X === void 0 ? "" : X.contentType;
|
|
1501
|
+
return { name: U, contentType: V, size: Number(L.size), version: gt(L), open: (J) => zo(M, L, J, E.signal) };
|
|
1489
1502
|
}, g = async (E, O, T) => {
|
|
1490
|
-
let
|
|
1503
|
+
let I;
|
|
1491
1504
|
try {
|
|
1492
|
-
|
|
1493
|
-
} catch (
|
|
1494
|
-
if ($e(
|
|
1495
|
-
throw
|
|
1505
|
+
I = await C(T);
|
|
1506
|
+
} catch (L) {
|
|
1507
|
+
if ($e(L) !== null) return null;
|
|
1508
|
+
throw L;
|
|
1496
1509
|
}
|
|
1497
|
-
if (
|
|
1510
|
+
if (I.status === "refused") return null;
|
|
1498
1511
|
const M = T.slice(m.length).split("/");
|
|
1499
|
-
if (M.some((
|
|
1512
|
+
if (M.some((L) => h.deniesName(L))) return null;
|
|
1500
1513
|
const U = M.slice(0, -1);
|
|
1501
|
-
return U.map((
|
|
1514
|
+
return U.map((L, X) => `${m}${U.slice(0, X + 1).join("/")}`);
|
|
1502
1515
|
};
|
|
1503
1516
|
return Object.freeze({
|
|
1504
1517
|
id: t,
|
|
1505
1518
|
label: r,
|
|
1506
1519
|
cacheScope: v,
|
|
1507
1520
|
boundary: Object.freeze({ contains: (E) => E.startsWith(m) }),
|
|
1508
|
-
tree:
|
|
1521
|
+
tree: P,
|
|
1509
1522
|
ancestors: g,
|
|
1510
|
-
document:
|
|
1523
|
+
document: F,
|
|
1511
1524
|
asset: p
|
|
1512
1525
|
});
|
|
1513
1526
|
};
|
|
1514
1527
|
export {
|
|
1515
|
-
|
|
1528
|
+
is as DEFAULT_DENIED_PATH_SEGMENTS,
|
|
1516
1529
|
Se as ExplorerError,
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1530
|
+
as as MAX_DOCUMENT_PATH_LENGTH,
|
|
1531
|
+
ts as anonymousAccess,
|
|
1532
|
+
ns as createFileSystemSource,
|
|
1533
|
+
rs as createMarkdownExplorer,
|
|
1521
1534
|
q as isAcceptableDocumentPath,
|
|
1522
|
-
|
|
1535
|
+
cs as isDeniedPath
|
|
1523
1536
|
};
|