@aiquants/markdown-explorer 0.4.0 → 0.5.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 +12 -0
- package/README.md +20 -2
- package/dist/ExplorerSplit-CWcK1E4I.js +1495 -0
- package/dist/ExplorerSplit-ajN3GSdA.cjs +1 -0
- package/dist/{MultiLayout-8s7q3-Hm.js → MultiLayout-BtWtCzJV.js} +69 -66
- package/dist/MultiLayout-DvkcdO9c.cjs +1 -0
- package/dist/{NativeScrollBar-DqkutcGb.cjs → NativeScrollBar-DbzyQ85y.cjs} +1 -1
- package/dist/{NativeScrollBar-D3VDRQqf.js → NativeScrollBar-apXjIKRW.js} +1 -1
- package/dist/TreeLayout-BcHsv9G8.js +23 -0
- package/dist/TreeLayout-rwhJfLmU.cjs +1 -0
- package/dist/core/contracts.d.cts +13 -0
- package/dist/core/contracts.d.ts +13 -0
- package/dist/{dataRoute-Dl6xmEFs.js → dataRoute-Btg4w3vO.js} +70 -65
- package/dist/dataRoute-DHpu0fg2.cjs +1 -0
- package/dist/{index-Dv9lUZqD.js → index-CIZw73j9.js} +539 -519
- package/dist/index-Cie6WPiM.cjs +3 -0
- package/dist/index.cjs +1 -1
- package/dist/index.js +5 -5
- package/dist/{location-2QZADLiv.cjs → location-CSIhx-L4.cjs} +1 -1
- package/dist/{location-CaNtUswk.js → location-c1zdeySY.js} +1 -1
- package/dist/sanitizeWorker.cjs +1 -1
- package/dist/sanitizeWorker.js +1 -1
- package/dist/server/errors.d.cts +1 -1
- package/dist/server/errors.d.ts +1 -1
- package/dist/server.cjs +1 -1
- package/dist/server.js +597 -589
- package/docs/behavior/security.md +1 -1
- package/docs/behavior/url-contract.md +1 -0
- package/package.json +1 -1
- package/dist/ExplorerSplit-rUVsf5i-.cjs +0 -1
- package/dist/ExplorerSplit-uX2ojZe8.js +0 -1479
- package/dist/MultiLayout-ZK2V9nms.cjs +0 -1
- package/dist/TreeLayout-C7ZNnaOr.js +0 -22
- package/dist/TreeLayout-DkTVgxxb.cjs +0 -1
- package/dist/dataRoute-DYjYBiXF.cjs +0 -1
- package/dist/index-CLe6vRXs.cjs +0 -3
package/dist/server.js
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import { b as
|
|
2
|
-
import { D as
|
|
3
|
-
import { createHash as
|
|
4
|
-
import { createLinkCardHandler as
|
|
5
|
-
import { data as
|
|
6
|
-
import { w as
|
|
7
|
-
import { existsSync as
|
|
8
|
-
import { extname as
|
|
1
|
+
import { b as lt, c as It, f as _e, d as vt, g as Mt, h as Pt, j as $t, E as jt, i as te, k as Lt, l as zt, r as dt, m as Dt, n as Ct, o as ft, p as Ht, q as Bt, s as fe, t as oe, e as Ft } from "./dataRoute-Btg4w3vO.js";
|
|
2
|
+
import { D as ho, M as go, u as yo } from "./dataRoute-Btg4w3vO.js";
|
|
3
|
+
import { createHash as Ut, createHmac as Jt } from "node:crypto";
|
|
4
|
+
import { createLinkCardHandler as Xt } from "@aiquants/markdown/server";
|
|
5
|
+
import { data as Le, redirect as Gt } from "react-router";
|
|
6
|
+
import { w as Wt, d as Yt, r as Kt, i as Vt, a as Zt, p as Qt, b as qt, f as er, N as tr } from "./location-c1zdeySY.js";
|
|
7
|
+
import { existsSync as rr, statSync as nr, realpathSync as or, constants as ue } from "node:fs";
|
|
8
|
+
import { extname as sr, isAbsolute as ze, relative as De, sep as Ce, join as ar, resolve as ir } from "node:path";
|
|
9
9
|
import { fileURLToPath as Se } from "node:url";
|
|
10
|
-
import { Worker as
|
|
11
|
-
import { open as
|
|
12
|
-
import { Readable as
|
|
13
|
-
const
|
|
14
|
-
authenticate: async () => ({ status: "signed-in", principal:
|
|
15
|
-
principalKey: () =>
|
|
10
|
+
import { Worker as cr } from "node:worker_threads";
|
|
11
|
+
import { open as mt, lstat as ur, realpath as lr, readdir as dr } from "node:fs/promises";
|
|
12
|
+
import { Readable as fr } from "node:stream";
|
|
13
|
+
const He = Object.freeze({ kind: "anonymous" }), lo = Object.freeze({
|
|
14
|
+
authenticate: async () => ({ status: "signed-in", principal: He }),
|
|
15
|
+
principalKey: () => He.kind,
|
|
16
16
|
signedOutPage: (e, t) => new Response(null, { status: 401, headers: t })
|
|
17
|
-
}),
|
|
17
|
+
}), mr = "application/octet-stream", pt = "application/pdf", pr = /* @__PURE__ */ new Set([
|
|
18
18
|
"image/png",
|
|
19
19
|
"image/apng",
|
|
20
20
|
"image/jpeg",
|
|
@@ -40,8 +40,8 @@ const Ce = Object.freeze({ kind: "anonymous" }), io = Object.freeze({
|
|
|
40
40
|
"video/webm",
|
|
41
41
|
"video/ogg",
|
|
42
42
|
"video/quicktime",
|
|
43
|
-
|
|
44
|
-
]),
|
|
43
|
+
pt
|
|
44
|
+
]), wt = "default-src 'none'; media-src 'self'; style-src 'unsafe-inline'", wr = `${wt}; sandbox`, hr = `${wt}; sandbox allow-same-origin`, Be = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/u, gr = /^[\t\n\r ]+|[\t\n\r ]+$/gu, Ne = /^[\t ]+|[\t ]+$/gu, yr = (e) => {
|
|
45
45
|
const t = [];
|
|
46
46
|
let r = "", n = 0;
|
|
47
47
|
for (; ; ) {
|
|
@@ -56,68 +56,68 @@ const Ce = Object.freeze({ kind: "anonymous" }), io = Object.freeze({
|
|
|
56
56
|
if (t.push(r.replace(Ne, "")), r = "", n >= e.length) return t;
|
|
57
57
|
n += 1;
|
|
58
58
|
}
|
|
59
|
-
},
|
|
60
|
-
const t = e.replace(
|
|
59
|
+
}, Er = (e) => {
|
|
60
|
+
const t = e.replace(gr, ""), r = t.indexOf("/");
|
|
61
61
|
if (r < 0) return null;
|
|
62
|
-
const n = t.slice(0, r), a = t.indexOf(";", r + 1),
|
|
63
|
-
return
|
|
64
|
-
},
|
|
62
|
+
const n = t.slice(0, r), a = t.indexOf(";", r + 1), s = t.slice(r + 1, a < 0 ? t.length : a).replace(/[\t\n\r ]+$/u, "");
|
|
63
|
+
return Be.test(n) && Be.test(s) ? `${n}/${s}`.toLowerCase() : null;
|
|
64
|
+
}, br = (e) => {
|
|
65
65
|
let t = null;
|
|
66
|
-
for (const r of
|
|
67
|
-
const n =
|
|
66
|
+
for (const r of yr(e)) {
|
|
67
|
+
const n = Er(r);
|
|
68
68
|
n !== null && n !== "*/*" && (t = n);
|
|
69
69
|
}
|
|
70
70
|
return t;
|
|
71
|
-
},
|
|
71
|
+
}, Sr = /['()*]/gu, Tr = (e) => encodeURIComponent(e).replace(Sr, (t) => `%${t.charCodeAt(0).toString(16).toUpperCase()}`), xr = Object.freeze({
|
|
72
72
|
"Cache-Control": "private, max-age=60, stale-while-revalidate=600, no-transform",
|
|
73
73
|
Vary: "Cookie"
|
|
74
|
-
}),
|
|
75
|
-
const r =
|
|
74
|
+
}), kr = (e, t) => {
|
|
75
|
+
const r = br(e), n = r !== null && pr.has(r), a = n && (r.startsWith("audio/") || r.startsWith("video/"));
|
|
76
76
|
return {
|
|
77
|
-
contentType: r === null ?
|
|
77
|
+
contentType: r === null ? mr : r,
|
|
78
78
|
inline: n,
|
|
79
79
|
headers: {
|
|
80
80
|
"X-Content-Type-Options": "nosniff",
|
|
81
|
-
"Content-Disposition": `${n ? "inline" : "attachment"}; filename*=UTF-8''${
|
|
82
|
-
...r ===
|
|
81
|
+
"Content-Disposition": `${n ? "inline" : "attachment"}; filename*=UTF-8''${Tr(t)}`,
|
|
82
|
+
...r === pt ? {} : { "Content-Security-Policy": a ? hr : wr },
|
|
83
83
|
"Cross-Origin-Resource-Policy": "same-origin"
|
|
84
84
|
}
|
|
85
85
|
};
|
|
86
|
-
},
|
|
86
|
+
}, Or = /^bytes=(?:(\d+)-(\d*)|-(\d+))$/iu, me = Object.freeze({ kind: "whole" }), Fe = Object.freeze({ kind: "unsatisfiable" }), Rr = (e, t, r) => {
|
|
87
87
|
const n = e.get("Range");
|
|
88
88
|
if (n === null) return me;
|
|
89
89
|
const a = e.get("If-Range");
|
|
90
90
|
if (a !== null && a !== r) return me;
|
|
91
|
-
const
|
|
92
|
-
if (
|
|
93
|
-
const [,
|
|
94
|
-
if (
|
|
95
|
-
const w = Number(
|
|
96
|
-
if (w === 0) return
|
|
91
|
+
const s = Or.exec(n);
|
|
92
|
+
if (s === null) return me;
|
|
93
|
+
const [, c, f, E] = s;
|
|
94
|
+
if (E !== void 0) {
|
|
95
|
+
const w = Number(E);
|
|
96
|
+
if (w === 0) return Fe;
|
|
97
97
|
const O = Math.max(0, t - w);
|
|
98
98
|
return { kind: "partial", range: { offset: O, length: t - O } };
|
|
99
99
|
}
|
|
100
|
-
const x = Number(
|
|
101
|
-
return
|
|
102
|
-
},
|
|
100
|
+
const x = Number(c), p = f === "" ? Number.POSITIVE_INFINITY : Number(f);
|
|
101
|
+
return p < x ? me : x >= t ? Fe : { kind: "partial", range: { offset: x, length: Math.min(p, t - 1) - x + 1 } };
|
|
102
|
+
}, Ue = "W/", _r = (e) => {
|
|
103
103
|
const t = [];
|
|
104
104
|
let r = 0, n = !1;
|
|
105
105
|
for (let a = 0; a < e.length; a += 1)
|
|
106
106
|
e[a] === '"' ? n = !n : e[a] === "," && !n && (t.push(e.slice(r, a).replace(Ne, "")), r = a + 1);
|
|
107
107
|
return t.push(e.slice(r).replace(Ne, "")), t;
|
|
108
|
-
},
|
|
108
|
+
}, Je = (e) => e.startsWith(Ue) ? e.slice(Ue.length) : e, Nr = (e, t) => {
|
|
109
109
|
if (e === null) return !1;
|
|
110
|
-
const r =
|
|
110
|
+
const r = _r(e);
|
|
111
111
|
if (r.length === 1 && r[0] === "*") return !0;
|
|
112
|
-
const n =
|
|
113
|
-
return r.some((a) =>
|
|
114
|
-
},
|
|
112
|
+
const n = Je(t);
|
|
113
|
+
return r.some((a) => Je(a) === n);
|
|
114
|
+
}, Ar = (e, t) => {
|
|
115
115
|
let r = t;
|
|
116
116
|
return e.pipeThrough(
|
|
117
117
|
new TransformStream({
|
|
118
118
|
transform: (n, a) => {
|
|
119
|
-
const
|
|
120
|
-
r -=
|
|
119
|
+
const s = n.byteLength > r ? n.subarray(0, r) : n;
|
|
120
|
+
r -= s.byteLength, a.enqueue(s), r === 0 && a.terminate();
|
|
121
121
|
},
|
|
122
122
|
flush: (n) => {
|
|
123
123
|
r > 0 && n.error(new RangeError("[markdown-explorer] the asset ended before its declared length"));
|
|
@@ -126,89 +126,89 @@ const Ce = Object.freeze({ kind: "anonymous" }), io = Object.freeze({
|
|
|
126
126
|
);
|
|
127
127
|
}, Te = async (e, t, r, n, a) => {
|
|
128
128
|
if (n !== null && r.set("Content-Length", String(n)), e.method.toUpperCase() === "HEAD") return new Response(null, { status: t, headers: r });
|
|
129
|
-
const
|
|
130
|
-
return new Response(n === null ?
|
|
131
|
-
},
|
|
129
|
+
const s = await a();
|
|
130
|
+
return new Response(n === null ? s : Ar(s, n), { status: t, headers: r });
|
|
131
|
+
}, Ir = (e, t) => {
|
|
132
132
|
const r = new Headers(e);
|
|
133
133
|
for (const [n, a] of Object.entries({ "Accept-Ranges": "bytes", "Content-Range": `bytes */${t}`, "Cache-Control": "private, no-store", "X-Content-Type-Options": "nosniff" })) r.set(n, a);
|
|
134
134
|
return new Response(null, { status: 416, headers: r });
|
|
135
|
-
},
|
|
136
|
-
const n = `"${t.version}"`, a =
|
|
137
|
-
for (const [
|
|
138
|
-
if (
|
|
139
|
-
if (
|
|
135
|
+
}, vr = async (e, t, r) => {
|
|
136
|
+
const n = `"${t.version}"`, a = kr(t.contentType, t.name), s = new Headers(r);
|
|
137
|
+
for (const [E, x] of Object.entries({ ...a.headers, ...xr, ETag: n })) s.set(E, x);
|
|
138
|
+
if (Nr(e.headers.get("If-None-Match"), n)) return new Response(null, { status: 304, headers: s });
|
|
139
|
+
if (s.set("Content-Type", a.contentType), t.size === null) return Te(e, 200, s, null, () => t.open(null));
|
|
140
140
|
if (t.size === 0)
|
|
141
|
-
return
|
|
142
|
-
|
|
143
|
-
const
|
|
144
|
-
if (
|
|
145
|
-
if (
|
|
146
|
-
const { range:
|
|
147
|
-
return
|
|
141
|
+
return s.set("Content-Length", "0"), new Response(null, { status: 200, headers: s });
|
|
142
|
+
s.set("Accept-Ranges", "bytes");
|
|
143
|
+
const c = Rr(e.headers, t.size, n);
|
|
144
|
+
if (c.kind === "unsatisfiable") return Ir(r, t.size);
|
|
145
|
+
if (c.kind === "whole") return Te(e, 200, s, t.size, () => t.open(null));
|
|
146
|
+
const { range: f } = c;
|
|
147
|
+
return s.set("Content-Range", `bytes ${f.offset}-${f.offset + f.length - 1}/${t.size}`), Te(e, 206, s, f.length, () => t.open(f));
|
|
148
148
|
}, xe = (e, t) => {
|
|
149
149
|
const r = new AbortController(), n = t(AbortSignal.any([e.signal, r.signal]));
|
|
150
150
|
return new Promise((a) => {
|
|
151
|
-
let
|
|
152
|
-
const
|
|
153
|
-
a(e.late),
|
|
154
|
-
}, e.timeoutMs),
|
|
155
|
-
clearTimeout(
|
|
151
|
+
let s;
|
|
152
|
+
const c = setTimeout(() => {
|
|
153
|
+
a(e.late), s = setTimeout(() => r.abort(), e.handOverMs);
|
|
154
|
+
}, e.timeoutMs), f = () => {
|
|
155
|
+
clearTimeout(c), clearTimeout(s);
|
|
156
156
|
};
|
|
157
157
|
n.then(
|
|
158
|
-
(
|
|
159
|
-
|
|
158
|
+
(E) => {
|
|
159
|
+
f(), a(E);
|
|
160
160
|
},
|
|
161
161
|
() => {
|
|
162
|
-
|
|
162
|
+
f(), a(e.late);
|
|
163
163
|
}
|
|
164
164
|
);
|
|
165
165
|
});
|
|
166
|
-
},
|
|
166
|
+
}, Mr = Object.freeze({
|
|
167
167
|
error: (e, t) => {
|
|
168
168
|
console.error(e, t);
|
|
169
169
|
}
|
|
170
|
-
}),
|
|
170
|
+
}), ht = /* @__PURE__ */ Symbol.for("@aiquants/markdown-explorer:ExplorerError");
|
|
171
171
|
class Ee extends Error {
|
|
172
172
|
reason;
|
|
173
173
|
code;
|
|
174
174
|
constructor(t, r = {}) {
|
|
175
|
-
if (!
|
|
176
|
-
if (r.code !== void 0 && !
|
|
177
|
-
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,
|
|
175
|
+
if (!lt(t)) throw new RangeError(`[markdown-explorer] ExplorerError needs a failure reason; got ${JSON.stringify(t)}`);
|
|
176
|
+
if (r.code !== void 0 && !It(r.code)) throw new RangeError(`[markdown-explorer] an ExplorerError code must be 1 to 64 lowercase letters, digits or hyphens; got ${JSON.stringify(r.code)}`);
|
|
177
|
+
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, ht, { value: !0 });
|
|
178
178
|
}
|
|
179
179
|
}
|
|
180
|
-
const
|
|
180
|
+
const Pr = (e) => e instanceof Error && Object.hasOwn(e, ht) && lt(e.reason), $r = (e) => typeof e == "object" && e !== null && e.name === "AbortError", Ie = (e, t, r) => {
|
|
181
181
|
try {
|
|
182
182
|
e.error(t, r);
|
|
183
183
|
} catch (n) {
|
|
184
184
|
console.error("[markdown-explorer] the logger threw while reporting a failure", { operation: r.operation, error: n });
|
|
185
185
|
}
|
|
186
|
-
}, ee = (e, t) => Ie(e, `[markdown-explorer] ${t.operation} failed`, t),
|
|
186
|
+
}, ee = (e, t) => Ie(e, `[markdown-explorer] ${t.operation} failed`, t), jr = (e, t) => Ie(e, `[markdown-explorer] ${t.operation} left out an entry`, t), Lr = (e, t) => Ie(e, `[markdown-explorer] ${t.operation} refused a path the deny rules match`, { ...t, error: new RangeError("[markdown-explorer] the path matches a deny rule") }), Xe = (e, t) => {
|
|
187
187
|
e === null || typeof e != "object" && typeof e != "function" || Promise.resolve(e).then(void 0, t);
|
|
188
|
-
},
|
|
188
|
+
}, q = (e, t, r, n) => Pr(e) ? _e(e.reason, e.code) : ($r(e) || r.aborted || ee(n, { ...t, error: e }), { reason: "failed" }), Ge = (e, t) => {
|
|
189
189
|
if (!Number.isSafeInteger(t) || t <= 0) throw new RangeError(`[markdown-explorer] a cache ${e} must be a positive integer; got ${String(t)}`);
|
|
190
|
-
},
|
|
190
|
+
}, gt = (e, t) => {
|
|
191
191
|
Ge("capacity", e), t !== void 0 && Ge("weight budget", t.max);
|
|
192
192
|
const r = /* @__PURE__ */ new Map();
|
|
193
193
|
let n = 0;
|
|
194
|
-
const a = (
|
|
195
|
-
const
|
|
196
|
-
|
|
194
|
+
const a = (s) => {
|
|
195
|
+
const c = r.get(s);
|
|
196
|
+
c !== void 0 && (r.delete(s), n -= c.weight);
|
|
197
197
|
};
|
|
198
198
|
return {
|
|
199
|
-
get: (
|
|
200
|
-
const
|
|
201
|
-
if (
|
|
202
|
-
return r.delete(
|
|
199
|
+
get: (s) => {
|
|
200
|
+
const c = r.get(s);
|
|
201
|
+
if (c !== void 0)
|
|
202
|
+
return r.delete(s), r.set(s, c), c.value;
|
|
203
203
|
},
|
|
204
|
-
set: (
|
|
205
|
-
a(
|
|
206
|
-
const
|
|
207
|
-
if (!(t !== void 0 &&
|
|
208
|
-
r.set(
|
|
209
|
-
for (const
|
|
204
|
+
set: (s, c) => {
|
|
205
|
+
a(s);
|
|
206
|
+
const f = t === void 0 ? 0 : t.of(s, c);
|
|
207
|
+
if (!(t !== void 0 && f > t.max)) {
|
|
208
|
+
r.set(s, { value: c, weight: f }), n += f;
|
|
209
|
+
for (const E of r.keys()) {
|
|
210
210
|
if (r.size <= e && (t === void 0 || n <= t.max)) break;
|
|
211
|
-
a(
|
|
211
|
+
a(E);
|
|
212
212
|
}
|
|
213
213
|
}
|
|
214
214
|
},
|
|
@@ -216,31 +216,31 @@ const Mr = (e) => e instanceof Error && Object.hasOwn(e, wt) && ut(e.reason), vr
|
|
|
216
216
|
size: () => r.size,
|
|
217
217
|
weight: () => n
|
|
218
218
|
};
|
|
219
|
-
},
|
|
219
|
+
}, yt = (e) => Math.max(1, e - 1), zr = 2147483647, We = Object.freeze({ status: "aborted" }), se = (e, t, r) => {
|
|
220
220
|
const n = (e.get(t) ?? 0) + r;
|
|
221
221
|
n === 0 ? e.delete(t) : e.set(t, n);
|
|
222
|
-
},
|
|
222
|
+
}, ce = (e, t, r) => {
|
|
223
223
|
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)}`);
|
|
224
|
-
},
|
|
225
|
-
|
|
226
|
-
const
|
|
224
|
+
}, Dr = (e) => e !== null && typeof e == "object" && e.ready === !0 && !("id" in e), Cr = (e, t) => e !== null && typeof e == "object" && e.id === t && "output" in e, Hr = ({ url: e, size: t, deadlineMs: r, jobsPerScope: n, maxQueuedJobs: a, heapLimits: s }) => {
|
|
225
|
+
ce("size", t, Number.MAX_SAFE_INTEGER), ce("deadline", r, zr), ce("jobsPerScope", n, Number.MAX_SAFE_INTEGER), ce("maxQueuedJobs", a, Number.MAX_SAFE_INTEGER), ce("maxOldGenerationSizeMb", s.maxOldGenerationSizeMb, Number.MAX_SAFE_INTEGER), ce("maxYoungGenerationSizeMb", s.maxYoungGenerationSizeMb, Number.MAX_SAFE_INTEGER);
|
|
226
|
+
const c = Object.freeze({ maxOldGenerationSizeMb: s.maxOldGenerationSizeMb, maxYoungGenerationSizeMb: s.maxYoungGenerationSizeMb }), f = /* @__PURE__ */ new Set(), E = [], x = /* @__PURE__ */ new Map(), p = /* @__PURE__ */ new Map(), w = yt(t);
|
|
227
227
|
let O = 0;
|
|
228
228
|
const _ = (u, h) => {
|
|
229
229
|
const { job: g } = u;
|
|
230
|
-
u.timer !== null && clearTimeout(u.timer), u.timer = null, u.job = null, u.sent = !1, g !== null && (
|
|
231
|
-
},
|
|
232
|
-
const [h] =
|
|
230
|
+
u.timer !== null && clearTimeout(u.timer), u.timer = null, u.job = null, u.sent = !1, g !== null && (se(x, g.scope, -1), se(p, g.scope, -1), g.settle(h));
|
|
231
|
+
}, M = (u) => {
|
|
232
|
+
const [h] = E.splice(u, 1);
|
|
233
233
|
h?.detach();
|
|
234
234
|
}, R = () => {
|
|
235
235
|
let u = -1, h = w, g = Number.POSITIVE_INFINITY;
|
|
236
|
-
for (const [T, b] of
|
|
237
|
-
const N = x.get(b.scope) ?? 0, P =
|
|
236
|
+
for (const [T, b] of E.entries()) {
|
|
237
|
+
const N = x.get(b.scope) ?? 0, P = p.get(b.scope) ?? 0;
|
|
238
238
|
(N < h || N === h && u >= 0 && P < g) && (u = T, h = N, g = P);
|
|
239
239
|
}
|
|
240
240
|
return u;
|
|
241
241
|
}, k = (u, h) => {
|
|
242
|
-
|
|
243
|
-
}),
|
|
242
|
+
f.has(u) && (f.delete(u), _(u, h), u.worker.terminate().catch(() => {
|
|
243
|
+
}), H());
|
|
244
244
|
}, j = (u) => {
|
|
245
245
|
u.timer = setTimeout(() => {
|
|
246
246
|
k(u, { status: "timeout" });
|
|
@@ -252,47 +252,47 @@ const Mr = (e) => e instanceof Error && Object.hasOwn(e, wt) && ut(e.reason), vr
|
|
|
252
252
|
} catch (g) {
|
|
253
253
|
_(u, { status: "crashed", error: g }), u.worker.unref();
|
|
254
254
|
}
|
|
255
|
-
},
|
|
256
|
-
const u = { worker: new
|
|
255
|
+
}, C = () => {
|
|
256
|
+
const u = { worker: new cr(e, { resourceLimits: c }), job: null, sent: !1, timer: null, ready: !1 };
|
|
257
257
|
return u.worker.unref(), u.timer = setTimeout(() => {
|
|
258
258
|
k(u, { status: "unavailable", error: new Error(`[markdown-explorer] a worker did not start within ${String(r)} ms`) });
|
|
259
259
|
}, r), u.worker.on("message", (h) => {
|
|
260
|
-
if (!u.ready &&
|
|
261
|
-
u.ready = !0, u.timer !== null && clearTimeout(u.timer), u.timer = null, u.job === null ? u.worker.unref() : A(u, u.job), u.job === null &&
|
|
260
|
+
if (!u.ready && Dr(h)) {
|
|
261
|
+
u.ready = !0, u.timer !== null && clearTimeout(u.timer), u.timer = null, u.job === null ? u.worker.unref() : A(u, u.job), u.job === null && H();
|
|
262
262
|
return;
|
|
263
263
|
}
|
|
264
264
|
const { job: g } = u;
|
|
265
|
-
if (g === null || !
|
|
265
|
+
if (g === null || !Cr(h, g.id)) {
|
|
266
266
|
k(u, S(u, new Error("[markdown-explorer] a worker sent a message that answers no job")));
|
|
267
267
|
return;
|
|
268
268
|
}
|
|
269
|
-
_(u, { status: "done", output: h.output }), u.worker.unref(),
|
|
269
|
+
_(u, { status: "done", output: h.output }), u.worker.unref(), H();
|
|
270
270
|
}), u.worker.on("error", (h) => {
|
|
271
271
|
k(u, S(u, h));
|
|
272
272
|
}), u.worker.on("messageerror", (h) => {
|
|
273
273
|
k(u, S(u, h));
|
|
274
274
|
}), u.worker.on("exit", (h) => {
|
|
275
275
|
k(u, S(u, new Error(`[markdown-explorer] a worker exited with code ${String(h)}`)));
|
|
276
|
-
}),
|
|
277
|
-
},
|
|
276
|
+
}), f.add(u), u;
|
|
277
|
+
}, H = () => {
|
|
278
278
|
for (; ; ) {
|
|
279
|
-
const u = R(), h =
|
|
279
|
+
const u = R(), h = E[u];
|
|
280
280
|
if (h === void 0) return;
|
|
281
281
|
let g;
|
|
282
|
-
for (const b of
|
|
282
|
+
for (const b of f)
|
|
283
283
|
if (b.job === null) {
|
|
284
284
|
g = b;
|
|
285
285
|
break;
|
|
286
286
|
}
|
|
287
|
-
if (g === void 0 &&
|
|
287
|
+
if (g === void 0 && f.size < t)
|
|
288
288
|
try {
|
|
289
|
-
g =
|
|
289
|
+
g = C();
|
|
290
290
|
} catch (b) {
|
|
291
|
-
|
|
291
|
+
M(u), se(p, h.scope, -1), h.settle({ status: "unavailable", error: b });
|
|
292
292
|
continue;
|
|
293
293
|
}
|
|
294
294
|
if (g === void 0) return;
|
|
295
|
-
|
|
295
|
+
M(u), se(x, h.scope, 1);
|
|
296
296
|
const T = g;
|
|
297
297
|
T.job = h, T.worker.ref(), T.ready && A(T, h);
|
|
298
298
|
}
|
|
@@ -300,115 +300,115 @@ const Mr = (e) => e instanceof Error && Object.hasOwn(e, wt) && ut(e.reason), vr
|
|
|
300
300
|
return {
|
|
301
301
|
run: (u, { scope: h, signal: g }) => new Promise((T) => {
|
|
302
302
|
if (g.aborted) {
|
|
303
|
-
T(
|
|
303
|
+
T(We);
|
|
304
304
|
return;
|
|
305
305
|
}
|
|
306
|
-
if ((
|
|
306
|
+
if ((p.get(h) ?? 0) >= n) {
|
|
307
307
|
T({ status: "refused", limit: "scope" });
|
|
308
308
|
return;
|
|
309
309
|
}
|
|
310
310
|
O += 1;
|
|
311
311
|
const b = { id: O, input: u, scope: h, settle: T, detach: () => {
|
|
312
312
|
} };
|
|
313
|
-
|
|
314
|
-
const N =
|
|
313
|
+
E.push(b), se(p, h, 1), H();
|
|
314
|
+
const N = E.indexOf(b);
|
|
315
315
|
if (N < 0) return;
|
|
316
|
-
if (
|
|
317
|
-
|
|
316
|
+
if (E.length > a) {
|
|
317
|
+
M(N), se(p, h, -1), T({ status: "refused", limit: "queue" });
|
|
318
318
|
return;
|
|
319
319
|
}
|
|
320
320
|
const P = () => {
|
|
321
|
-
const B =
|
|
322
|
-
B < 0 || (
|
|
321
|
+
const B = E.indexOf(b);
|
|
322
|
+
B < 0 || (M(B), se(p, h, -1), T(We));
|
|
323
323
|
};
|
|
324
324
|
g.addEventListener("abort", P, { once: !0 }), b.detach = () => {
|
|
325
325
|
g.removeEventListener("abort", P);
|
|
326
326
|
};
|
|
327
327
|
}),
|
|
328
|
-
workers: () =>
|
|
328
|
+
workers: () => f.size
|
|
329
329
|
};
|
|
330
|
-
},
|
|
330
|
+
}, ve = 1024 * 1024, Br = 3e4, Fr = 4 * ve, Ur = 64 * ve, Jr = 3, Xr = 2, Gr = (e, t) => e + yt(t) + Jr, Wr = 4, Yr = 32, Kr = 512, Vr = 64, Zr = 4096, Qr = 32, Ye = 4096, qr = 4, Et = ({ workerHeapMb: e, maxHtmlBytes: t }) => Math.floor(((e - Vr) * ve - t * Qr) / Zr), en = ({ maxHtmlBytes: e }) => e, tn = ({ maxHtmlBytes: e }) => qr * e, rn = 1024, nn = 2147483647, on = 64, sn = 2 ** 29, an = 1024 * 1024, cn = 64, Ke = Object.freeze({ status: "rejected", reason: "too-large", cause: null }), ke = Object.freeze({ status: "rejected", reason: "failed", cause: null }), ae = (e, t, r, n) => {
|
|
331
331
|
if (t === void 0) return r;
|
|
332
332
|
if (typeof t != "number") throw new TypeError(`[markdown-explorer] sanitizer.${e} must be a number; got ${typeof t}`);
|
|
333
333
|
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)}`);
|
|
334
334
|
return t;
|
|
335
|
-
},
|
|
335
|
+
}, un = (e, t) => {
|
|
336
336
|
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 }");
|
|
337
|
-
const { workers: r, deadlineMs: n, maxHtmlBytes: a, cacheBytes:
|
|
337
|
+
const { workers: r, deadlineMs: n, maxHtmlBytes: a, cacheBytes: s, workerHeapMb: c, jobsPerScope: f, maxQueuedJobs: E } = e ?? {}, x = ae("workers", r, Xr, on), p = ae("jobsPerScope", f, Gr(t, x), Number.MAX_SAFE_INTEGER), w = Object.freeze({
|
|
338
338
|
workers: x,
|
|
339
|
-
deadlineMs:
|
|
340
|
-
maxHtmlBytes:
|
|
341
|
-
cacheBytes:
|
|
342
|
-
workerHeapMb:
|
|
343
|
-
jobsPerScope:
|
|
344
|
-
maxQueuedJobs:
|
|
339
|
+
deadlineMs: ae("deadlineMs", n, Br, nn),
|
|
340
|
+
maxHtmlBytes: ae("maxHtmlBytes", a, Fr, sn),
|
|
341
|
+
cacheBytes: ae("cacheBytes", s, Ur, Number.MAX_SAFE_INTEGER),
|
|
342
|
+
workerHeapMb: ae("workerHeapMb", c, Kr, an),
|
|
343
|
+
jobsPerScope: p,
|
|
344
|
+
maxQueuedJobs: ae("maxQueuedJobs", E, Wr * p, Number.MAX_SAFE_INTEGER)
|
|
345
345
|
});
|
|
346
346
|
if (w.jobsPerScope < t)
|
|
347
347
|
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(w.jobsPerScope)}`);
|
|
348
348
|
if (w.maxQueuedJobs < t)
|
|
349
349
|
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(w.maxQueuedJobs)}`);
|
|
350
|
-
if (
|
|
350
|
+
if (Et(w) < Ye)
|
|
351
351
|
throw new RangeError(
|
|
352
|
-
`[markdown-explorer] sanitizer.workerHeapMb (${String(w.workerHeapMb)} MiB) must leave room for ${String(
|
|
352
|
+
`[markdown-explorer] sanitizer.workerHeapMb (${String(w.workerHeapMb)} MiB) must leave room for ${String(Ye)} parsed nodes beside HTML of sanitizer.maxHtmlBytes (${String(w.maxHtmlBytes)} bytes); raise workerHeapMb or lower maxHtmlBytes`
|
|
353
353
|
);
|
|
354
354
|
return w;
|
|
355
|
-
},
|
|
356
|
-
const e = import.meta.url, t = new URL(`./sanitizeWorker${
|
|
357
|
-
if (!
|
|
355
|
+
}, ln = () => {
|
|
356
|
+
const e = import.meta.url, t = new URL(`./sanitizeWorker${sr(Se(e))}`, e);
|
|
357
|
+
if (!rr(Se(t))) throw new Error(`[markdown-explorer] the HTML sanitizer worker is missing: ${Se(t)}`);
|
|
358
358
|
return t;
|
|
359
|
-
},
|
|
359
|
+
}, Ve = /* @__PURE__ */ new Map(), dn = (e, t) => {
|
|
360
360
|
const r = [e.href, t.workers, t.deadlineMs, t.workerHeapMb, t.jobsPerScope, t.maxQueuedJobs].map(String).join("\0");
|
|
361
|
-
let n =
|
|
362
|
-
return n === void 0 && (n =
|
|
361
|
+
let n = Ve.get(r);
|
|
362
|
+
return n === void 0 && (n = Hr({
|
|
363
363
|
url: e,
|
|
364
364
|
size: t.workers,
|
|
365
365
|
deadlineMs: t.deadlineMs,
|
|
366
366
|
jobsPerScope: t.jobsPerScope,
|
|
367
367
|
maxQueuedJobs: t.maxQueuedJobs,
|
|
368
|
-
heapLimits: { maxOldGenerationSizeMb: t.workerHeapMb, maxYoungGenerationSizeMb:
|
|
369
|
-
}),
|
|
370
|
-
},
|
|
368
|
+
heapLimits: { maxOldGenerationSizeMb: t.workerHeapMb, maxYoungGenerationSizeMb: Yr }
|
|
369
|
+
}), Ve.set(r, n)), n;
|
|
370
|
+
}, fn = (e) => {
|
|
371
371
|
if (e === null || typeof e != "object") return !1;
|
|
372
372
|
const { status: t, html: r, message: n } = e;
|
|
373
373
|
return t === "sanitized" && typeof r == "string" || t === "too-large" || t === "error" && typeof n == "string";
|
|
374
|
-
},
|
|
375
|
-
const a = Object.freeze([...e]),
|
|
374
|
+
}, mn = (e) => e instanceof Error && e.code === "ERR_WORKER_OUT_OF_MEMORY", pn = ({ iframeHosts: e, dataPath: t, options: r, pool: n }) => {
|
|
375
|
+
const a = Object.freeze([...e]), s = Et(r), c = en(r), f = tn(r), E = gt(rn, {
|
|
376
376
|
max: r.cacheBytes,
|
|
377
|
-
of: (R, k) => 2 * R.length + (k.status === "sanitized" ? 2 * k.html.length :
|
|
378
|
-
}), x = /* @__PURE__ */ new Map(),
|
|
377
|
+
of: (R, k) => 2 * R.length + (k.status === "sanitized" ? 2 * k.html.length : cn)
|
|
378
|
+
}), x = /* @__PURE__ */ new Map(), p = /* @__PURE__ */ new Map(), w = (R) => {
|
|
379
379
|
x.get(R.key) === R && x.delete(R.key);
|
|
380
|
-
const k =
|
|
381
|
-
k?.delete(R), k?.size === 0 &&
|
|
380
|
+
const k = p.get(R.hash);
|
|
381
|
+
k?.delete(R), k?.size === 0 && p.delete(R.hash);
|
|
382
382
|
}, O = async (R, k, j) => {
|
|
383
|
-
const S = await n.run({ html: R, iframeHosts: a, dataPath: t, maxNodes:
|
|
383
|
+
const S = await n.run({ html: R, iframeHosts: a, dataPath: t, maxNodes: s, maxAttributeChars: c, maxSanitizedChars: f }, { scope: k, signal: j });
|
|
384
384
|
if (S.status === "refused") {
|
|
385
|
-
const
|
|
386
|
-
return { outcome: { status: "rejected", reason: "failed", cause: new Error(`[markdown-explorer] the HTML sanitizer refused a document: ${
|
|
385
|
+
const C = S.limit === "scope" ? `its reader already has ${String(r.jobsPerScope)} documents being sanitized or waiting` : `${String(r.maxQueuedJobs)} documents are already waiting`;
|
|
386
|
+
return { outcome: { status: "rejected", reason: "failed", cause: new Error(`[markdown-explorer] the HTML sanitizer refused a document: ${C}`) }, lasting: !1 };
|
|
387
387
|
}
|
|
388
388
|
if (S.status === "aborted") return { outcome: ke, lasting: !1 };
|
|
389
389
|
if (S.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 };
|
|
390
390
|
if (S.status === "unavailable") return { outcome: { status: "rejected", reason: "failed", cause: new Error("[markdown-explorer] the HTML sanitizer worker could not start", { cause: S.error }) }, lasting: !1 };
|
|
391
|
-
if (S.status === "crashed" &&
|
|
391
|
+
if (S.status === "crashed" && mn(S.error))
|
|
392
392
|
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: S.error }) }, lasting: !0 };
|
|
393
393
|
if (S.status === "crashed") return { outcome: { status: "rejected", reason: "failed", cause: new Error("[markdown-explorer] the HTML sanitizer worker failed", { cause: S.error }) }, lasting: !0 };
|
|
394
394
|
const { output: A } = S;
|
|
395
|
-
return
|
|
395
|
+
return fn(A) ? A.status === "too-large" ? { outcome: Ke, lasting: !0 } : A.status === "error" ? { outcome: { status: "rejected", reason: "failed", cause: new Error(`[markdown-explorer] the HTML sanitizer failed: ${A.message}`) }, lasting: !0 } : A.html.length > f ? { outcome: { status: "rejected", reason: "too-large", cause: new Error(`[markdown-explorer] the HTML sanitizer's output of ${String(A.html.length)} characters exceeds its limit of ${String(f)}`) }, lasting: !0 } : { outcome: { status: "sanitized", html: A.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 };
|
|
396
396
|
}, _ = (R, k, j, S) => {
|
|
397
397
|
let A = () => {
|
|
398
398
|
};
|
|
399
|
-
const
|
|
399
|
+
const C = new Promise((h) => {
|
|
400
400
|
A = h;
|
|
401
|
-
}),
|
|
402
|
-
x.set(j,
|
|
403
|
-
const u =
|
|
404
|
-
return u === void 0 ?
|
|
405
|
-
if (w(
|
|
406
|
-
const T =
|
|
407
|
-
T === void 0 &&
|
|
408
|
-
for (const N of
|
|
401
|
+
}), H = { hash: k, key: j, outcome: C, answer: A, controller: new AbortController(), waiters: 0 };
|
|
402
|
+
x.set(j, H);
|
|
403
|
+
const u = p.get(k);
|
|
404
|
+
return u === void 0 ? p.set(k, /* @__PURE__ */ new Set([H])) : u.add(H), O(R, S, H.controller.signal).catch((h) => ({ outcome: { status: "rejected", reason: "failed", cause: h instanceof Error ? h : new Error(String(h)) }, lasting: !0 })).then(({ outcome: h, lasting: g }) => {
|
|
405
|
+
if (w(H), H.answer(h), !g) return;
|
|
406
|
+
const T = E.get(k), b = T ?? (h.status === "rejected" ? { ...h, cause: null } : h);
|
|
407
|
+
T === void 0 && E.set(k, b);
|
|
408
|
+
for (const N of p.get(k) ?? [])
|
|
409
409
|
w(N), N.answer(b), N.controller.abort();
|
|
410
|
-
}),
|
|
411
|
-
},
|
|
410
|
+
}), H;
|
|
411
|
+
}, M = (R, k) => new Promise((j) => {
|
|
412
412
|
R.waiters += 1;
|
|
413
413
|
const S = () => {
|
|
414
414
|
R.waiters -= 1, j(ke), !(R.waiters > 0) && (w(R), R.controller.abort());
|
|
@@ -418,218 +418,218 @@ const Mr = (e) => e instanceof Error && Object.hasOwn(e, wt) && ut(e.reason), vr
|
|
|
418
418
|
});
|
|
419
419
|
});
|
|
420
420
|
return (R, { scope: k, signal: j }) => {
|
|
421
|
-
if (R.length > r.maxHtmlBytes || Buffer.byteLength(R, "utf8") > r.maxHtmlBytes) return Promise.resolve(
|
|
422
|
-
const S =
|
|
421
|
+
if (R.length > r.maxHtmlBytes || Buffer.byteLength(R, "utf8") > r.maxHtmlBytes) return Promise.resolve(Ke);
|
|
422
|
+
const S = Ut("sha256").update(R, "utf8").digest("base64"), A = E.get(S);
|
|
423
423
|
if (A !== void 0) return Promise.resolve(A);
|
|
424
424
|
if (j.aborted) return Promise.resolve(ke);
|
|
425
|
-
const
|
|
426
|
-
return
|
|
425
|
+
const C = `${S}\0${k}`, H = x.get(C);
|
|
426
|
+
return M(H === void 0 ? _(R, S, C, k) : H, j);
|
|
427
427
|
};
|
|
428
|
-
}, be = (e) => ({ result: e, json: JSON.stringify(e) }), Oe = Object.freeze(be(Object.freeze({ status: "error", failure: Object.freeze({ reason: "failed" }) }))),
|
|
429
|
-
const a =
|
|
430
|
-
const O = new AbortController(), _ = { waiters: 0, superseded: !1, started: !1, loadTree: w },
|
|
431
|
-
const u = be(
|
|
432
|
-
return !(_.superseded || O.signal.aborted) &&
|
|
428
|
+
}, be = (e) => ({ result: e, json: JSON.stringify(e) }), Oe = Object.freeze(be(Object.freeze({ status: "error", failure: Object.freeze({ reason: "failed" }) }))), wn = (e) => e.status === "ok" && e.entries.length > 0, hn = ({ ttlMs: e, maxEntries: t, maxBytes: r, now: n }) => {
|
|
429
|
+
const a = gt(t, { max: r, of: (p, w) => 2 * w.tree.json.length }), s = /* @__PURE__ */ new Map(), c = /* @__PURE__ */ new Map(), f = (p, w) => {
|
|
430
|
+
const O = new AbortController(), _ = { waiters: 0, superseded: !1, started: !1, loadTree: w }, M = c.get(p), R = (C, H) => {
|
|
431
|
+
const u = be(C);
|
|
432
|
+
return !(_.superseded || O.signal.aborted) && wn(C) && a.set(p, { tree: u, loadedAt: H }), u;
|
|
433
433
|
}, j = (async () => {
|
|
434
|
-
if (
|
|
435
|
-
const
|
|
436
|
-
return _.started = !0, R(await _.loadTree(O.signal,
|
|
434
|
+
if (M !== void 0 && await M, O.signal.aborted) return Oe;
|
|
435
|
+
const C = n();
|
|
436
|
+
return _.started = !0, R(await _.loadTree(O.signal, C), C);
|
|
437
437
|
})().finally(() => {
|
|
438
|
-
|
|
438
|
+
s.get(p) === S && s.delete(p);
|
|
439
439
|
}), S = { promise: j, controller: O, state: _ };
|
|
440
|
-
|
|
440
|
+
s.set(p, S);
|
|
441
441
|
const A = j.then(
|
|
442
442
|
() => {
|
|
443
443
|
},
|
|
444
444
|
() => {
|
|
445
445
|
}
|
|
446
446
|
);
|
|
447
|
-
return
|
|
448
|
-
|
|
447
|
+
return c.set(p, A), A.then(() => {
|
|
448
|
+
c.get(p) === A && c.delete(p);
|
|
449
449
|
}), S;
|
|
450
|
-
},
|
|
450
|
+
}, E = (p, w, O) => {
|
|
451
451
|
w.state.waiters += 1;
|
|
452
452
|
const _ = () => {
|
|
453
|
-
w.state.waiters -= 1, !(w.state.waiters > 0) && (
|
|
453
|
+
w.state.waiters -= 1, !(w.state.waiters > 0) && (s.get(p) === w && s.delete(p), w.controller.abort());
|
|
454
454
|
};
|
|
455
455
|
return O.addEventListener("abort", _, { once: !0 }), w.promise.finally(() => {
|
|
456
456
|
O.removeEventListener("abort", _);
|
|
457
457
|
});
|
|
458
|
-
}, x = (
|
|
459
|
-
a.delete(
|
|
460
|
-
const w =
|
|
461
|
-
w?.state.started && (w.state.superseded = !0,
|
|
458
|
+
}, x = (p) => {
|
|
459
|
+
a.delete(p);
|
|
460
|
+
const w = s.get(p);
|
|
461
|
+
w?.state.started && (w.state.superseded = !0, s.delete(p));
|
|
462
462
|
};
|
|
463
463
|
return {
|
|
464
|
-
load: (
|
|
464
|
+
load: (p, w, O) => {
|
|
465
465
|
if (w.aborted) return Promise.resolve(Oe);
|
|
466
|
-
const _ = a.get(
|
|
466
|
+
const _ = a.get(p);
|
|
467
467
|
if (_ !== void 0) {
|
|
468
468
|
if (n() - _.loadedAt < e) return Promise.resolve(_.tree);
|
|
469
|
-
a.delete(
|
|
469
|
+
a.delete(p);
|
|
470
470
|
}
|
|
471
|
-
const
|
|
472
|
-
return
|
|
471
|
+
const M = s.get(p);
|
|
472
|
+
return E(p, M === void 0 ? f(p, O) : M, w);
|
|
473
473
|
},
|
|
474
|
-
refresh: (
|
|
474
|
+
refresh: (p, w, O) => {
|
|
475
475
|
if (w.aborted) return Promise.resolve(Oe);
|
|
476
|
-
x(
|
|
477
|
-
const _ =
|
|
478
|
-
return _ === void 0 ?
|
|
476
|
+
x(p);
|
|
477
|
+
const _ = s.get(p);
|
|
478
|
+
return _ === void 0 ? E(p, f(p, O), w) : (_.state.loadTree = O, E(p, _, w));
|
|
479
479
|
},
|
|
480
480
|
forget: x
|
|
481
481
|
};
|
|
482
|
-
},
|
|
482
|
+
}, gn = (e) => ({
|
|
483
483
|
load: (t, r, n) => n(r, e()).then(be),
|
|
484
484
|
refresh: (t, r, n) => n(r, e()).then(be),
|
|
485
485
|
forget: () => {
|
|
486
486
|
}
|
|
487
487
|
}), $ = (e) => {
|
|
488
488
|
throw new TypeError(`[markdown-explorer] ${e}`);
|
|
489
|
-
},
|
|
489
|
+
}, yn = (e) => {
|
|
490
490
|
if (e === null || typeof e != "object") return null;
|
|
491
491
|
const { name: t, path: r } = e;
|
|
492
492
|
return typeof t != "string" || typeof r != "string" ? null : t === "" ? { path: r, reason: "an empty name" } : te(r) ? null : { path: r, reason: "a path the browser cannot address (empty, too long, a lone surrogate, a control character or a backslash)" };
|
|
493
|
-
},
|
|
493
|
+
}, En = (e, t, r, n) => {
|
|
494
494
|
if (e === null) return null;
|
|
495
495
|
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}`);
|
|
496
496
|
const a = /* @__PURE__ */ new Set();
|
|
497
|
-
for (const
|
|
498
|
-
if (typeof
|
|
499
|
-
if (r.deniesPath(
|
|
500
|
-
if (!n(
|
|
501
|
-
if (
|
|
502
|
-
if (a.has(
|
|
503
|
-
a.add(
|
|
497
|
+
for (const s of e) {
|
|
498
|
+
if (typeof s != "string" || !te(s)) throw new TypeError("[markdown-explorer] a source's ancestors held a path the explorer does not accept");
|
|
499
|
+
if (r.deniesPath(s)) throw new TypeError("[markdown-explorer] a source's ancestors held a denied path");
|
|
500
|
+
if (!n(s)) throw new TypeError("[markdown-explorer] a source's ancestors held a path outside the source's boundary");
|
|
501
|
+
if (s === t) throw new TypeError("[markdown-explorer] a source's ancestors held the document itself");
|
|
502
|
+
if (a.has(s)) throw new TypeError("[markdown-explorer] a source's ancestors held a path twice");
|
|
503
|
+
a.add(s);
|
|
504
504
|
}
|
|
505
505
|
return Object.freeze([...a]);
|
|
506
|
-
},
|
|
507
|
-
const
|
|
508
|
-
if (!Array.isArray(
|
|
506
|
+
}, bn = (e, t, r, n, a) => {
|
|
507
|
+
const s = /* @__PURE__ */ new Set(), c = (E) => {
|
|
508
|
+
if (!Array.isArray(E)) return $("a source returned entries that are not an array");
|
|
509
509
|
const x = [];
|
|
510
|
-
for (const
|
|
511
|
-
const w =
|
|
510
|
+
for (const p of E) {
|
|
511
|
+
const w = f(p);
|
|
512
512
|
w !== null && x.push(w);
|
|
513
513
|
}
|
|
514
514
|
return x;
|
|
515
|
-
},
|
|
516
|
-
const x =
|
|
515
|
+
}, f = (E) => {
|
|
516
|
+
const x = yn(E);
|
|
517
517
|
if (x !== null)
|
|
518
518
|
return n(x), null;
|
|
519
|
-
const
|
|
520
|
-
if (
|
|
521
|
-
const w =
|
|
522
|
-
if (
|
|
523
|
-
const { name: O, path: _, type:
|
|
519
|
+
const p = Lt(E);
|
|
520
|
+
if (p !== null) return $(p);
|
|
521
|
+
const w = E;
|
|
522
|
+
if (s.has(w)) return $("a source returned entries that contain themselves");
|
|
523
|
+
const { name: O, path: _, type: M, mimeType: R, children: k, lazy: j } = w;
|
|
524
524
|
if (t.deniesName(O) || t.deniesPath(_)) return null;
|
|
525
525
|
if (!a(_))
|
|
526
526
|
return n({ path: _, reason: "a path outside the source's boundary" }), null;
|
|
527
527
|
const S = R === void 0 ? { name: O, path: _ } : { name: O, path: _, mimeType: R };
|
|
528
|
-
if (
|
|
528
|
+
if (M === "file") return { ...S, type: M };
|
|
529
529
|
if (j === !0)
|
|
530
|
-
return r ? { ...S, type:
|
|
531
|
-
if (k === void 0) return { ...S, type:
|
|
532
|
-
|
|
533
|
-
const A =
|
|
534
|
-
return
|
|
530
|
+
return r ? { ...S, type: M, lazy: !0, children: [] } : $("a lazy directory came from a source without a children function");
|
|
531
|
+
if (k === void 0) return { ...S, type: M };
|
|
532
|
+
s.add(w);
|
|
533
|
+
const A = c(k);
|
|
534
|
+
return s.delete(w), { ...S, type: M, children: A };
|
|
535
535
|
};
|
|
536
|
-
return
|
|
537
|
-
},
|
|
536
|
+
return c(e);
|
|
537
|
+
}, Sn = /^[\x21\x23-\x7E]{1,256}$/u, bt = (e) => typeof e == "string" && Sn.test(e), Tn = /* @__PURE__ */ new Set(["text", "image", "media", "file"]), xn = (e) => typeof e == "string" && Tn.has(e), kn = (e, t) => {
|
|
538
538
|
if (e !== null && typeof e == "object") {
|
|
539
|
-
const a = e, { kind:
|
|
540
|
-
if (
|
|
541
|
-
if (Object.hasOwn(a, "url")) return $(`a ${
|
|
539
|
+
const a = e, { kind: s } = a;
|
|
540
|
+
if (xn(s)) {
|
|
541
|
+
if (Object.hasOwn(a, "url")) return $(`a ${s} document carries its version, not a url`);
|
|
542
542
|
if (typeof a.name != "string") return $("a document needs a name string");
|
|
543
|
-
if (!
|
|
544
|
-
const
|
|
545
|
-
switch (
|
|
543
|
+
if (!bt(a.version)) return $(`a ${s} document needs a version of 1 to 256 visible ASCII characters other than '"'`);
|
|
544
|
+
const c = t(a.version);
|
|
545
|
+
switch (s) {
|
|
546
546
|
case "image":
|
|
547
547
|
case "file":
|
|
548
|
-
return { kind:
|
|
548
|
+
return { kind: s, name: a.name, url: c };
|
|
549
549
|
case "media": {
|
|
550
|
-
const { mediaType:
|
|
551
|
-
return
|
|
550
|
+
const { mediaType: f } = a;
|
|
551
|
+
return Pt(f) ? { kind: s, name: a.name, url: c, mediaType: f } : $(`unknown media type ${typeof f == "string" ? JSON.stringify(f.slice(0, 64)) : typeof f}`);
|
|
552
552
|
}
|
|
553
553
|
case "text":
|
|
554
|
-
return typeof a.text != "string" || typeof a.truncated != "boolean" ? $("a text document needs text and truncated") : { kind:
|
|
554
|
+
return typeof a.text != "string" || typeof a.truncated != "boolean" ? $("a text document needs text and truncated") : { kind: s, name: a.name, text: a.text, truncated: a.truncated, url: c };
|
|
555
555
|
}
|
|
556
556
|
}
|
|
557
557
|
}
|
|
558
|
-
const r =
|
|
558
|
+
const r = $t(e);
|
|
559
559
|
if (r !== null) return $(r);
|
|
560
560
|
const n = e;
|
|
561
561
|
switch (n.kind) {
|
|
562
562
|
case "failure": {
|
|
563
|
-
const { reason: a, code:
|
|
564
|
-
return { kind: "failure", failure: _e(a,
|
|
563
|
+
const { reason: a, code: s } = n.failure;
|
|
564
|
+
return { kind: "failure", failure: _e(a, s) };
|
|
565
565
|
}
|
|
566
566
|
case "markdown":
|
|
567
|
-
return n.headings.length >
|
|
567
|
+
return n.headings.length > jt ? { kind: "failure", failure: _e("too-large") } : { kind: "unsanitized-markdown", name: n.name, html: n.htmlContent, headings: n.headings.map(({ text: a, depth: s, id: c }) => ({ text: a, depth: s, id: c })) };
|
|
568
568
|
}
|
|
569
|
-
},
|
|
569
|
+
}, On = (e) => {
|
|
570
570
|
if (e === null || typeof e != "object") return $("an asset file must be an object");
|
|
571
|
-
const { name: t, contentType: r, size: n, version: a, open:
|
|
572
|
-
return typeof t != "string" || t === "" ? $("an asset file needs a non-empty name") : typeof r != "string" ? $("an asset file needs a contentType string ('' when unknown)") : n !== null && !(typeof n == "number" && Number.isSafeInteger(n) && n >= 0) ? $("an asset file's size must be null or a non-negative safe integer") :
|
|
573
|
-
},
|
|
571
|
+
const { name: t, contentType: r, size: n, version: a, open: s } = e;
|
|
572
|
+
return typeof t != "string" || t === "" ? $("an asset file needs a non-empty name") : typeof r != "string" ? $("an asset file needs a contentType string ('' when unknown)") : n !== null && !(typeof n == "number" && Number.isSafeInteger(n) && n >= 0) ? $("an asset file's size must be null or a non-negative safe integer") : bt(a) ? typeof s != "function" ? $("an asset file needs an open function") : Object.freeze({ name: t, contentType: r, size: n, version: a, open: s }) : $(`an asset file needs a version of 1 to 256 visible ASCII characters other than '"'`);
|
|
573
|
+
}, Rn = (e) => Array.isArray(e) ? e.map((t) => {
|
|
574
574
|
if (t === null || typeof t != "object") return $("a notice must be an object");
|
|
575
|
-
const { id: r, tone: n, title: a, body:
|
|
575
|
+
const { id: r, tone: n, title: a, body: s, action: c } = t;
|
|
576
576
|
if (typeof r != "string" || r === "" || typeof a != "string") return $("a notice needs an id and a title");
|
|
577
|
-
if (!
|
|
578
|
-
if (
|
|
579
|
-
const
|
|
580
|
-
if (
|
|
581
|
-
if (
|
|
582
|
-
const { label: x, href:
|
|
583
|
-
return typeof x != "string" || !
|
|
584
|
-
}) : $("notices must be an array"),
|
|
577
|
+
if (!vt(n)) return $(`unknown notice tone ${typeof n == "string" ? JSON.stringify(n.slice(0, 64)) : typeof n}`);
|
|
578
|
+
if (s !== void 0 && typeof s != "string") return $("a notice body must be a string");
|
|
579
|
+
const E = { id: r, tone: n, title: a, ...s === void 0 ? {} : { body: s } };
|
|
580
|
+
if (c === void 0) return E;
|
|
581
|
+
if (c === null || typeof c != "object") return $("a notice action must be an object");
|
|
582
|
+
const { label: x, href: p } = c;
|
|
583
|
+
return typeof x != "string" || !Mt(p, zt) ? $("a notice action needs a label and an http(s), mailto or relative href") : { ...E, action: { label: x, href: p } };
|
|
584
|
+
}) : $("notices must be an array"), _n = 4e3, pe = Object.freeze({ ttlMs: 3e5, maxEntries: 64, maxBytes: 64 * 1024 * 1024 }), Ze = 32, St = "private, no-store", Nn = Object.freeze({
|
|
585
585
|
"Content-Type": "application/json; charset=utf-8",
|
|
586
|
-
"Cache-Control":
|
|
586
|
+
"Cache-Control": St,
|
|
587
587
|
"X-Content-Type-Options": "nosniff",
|
|
588
588
|
"Content-Security-Policy": "default-src 'none'; frame-ancestors 'none'; sandbox",
|
|
589
589
|
"Cross-Origin-Resource-Policy": "same-origin"
|
|
590
|
-
}),
|
|
590
|
+
}), An = 22, In = 2147483647, Re = Object.freeze([]), vn = Object.freeze({ status: "ok", source: null, ancestors: null }), Qe = Object.freeze(["contains", "confirm"]), qe = Object.freeze({ readOnly: !1 }), Mn = Object.freeze({ readOnly: !0 }), X = (e, t, r) => {
|
|
591
591
|
if (!(r && t === void 0) && typeof t != "function")
|
|
592
592
|
throw new TypeError(`[markdown-explorer] ${e} must be a function; got ${typeof t}`);
|
|
593
593
|
}, ye = (e, t, r) => {
|
|
594
594
|
if (typeof t != "number") throw new TypeError(`[markdown-explorer] ${e} must be a number; got ${typeof t}`);
|
|
595
595
|
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)}`);
|
|
596
596
|
return t;
|
|
597
|
-
},
|
|
597
|
+
}, Pn = (e) => {
|
|
598
598
|
if (e === null || typeof e != "object" || !Object.isFrozen(e)) throw new TypeError("[markdown-explorer] config must be the value returned by defineExplorerConfig");
|
|
599
|
-
return
|
|
600
|
-
},
|
|
599
|
+
return Kt(e);
|
|
600
|
+
}, et = (e) => {
|
|
601
601
|
try {
|
|
602
602
|
return new Headers(e);
|
|
603
603
|
} catch {
|
|
604
604
|
throw new TypeError("[markdown-explorer] authenticate returned headers that are not a valid headers init (their values are not logged)");
|
|
605
605
|
}
|
|
606
|
-
},
|
|
606
|
+
}, tt = (e) => e instanceof Response ? new TypeError(`[markdown-explorer] authenticate threw a Response (status ${String(e.status)}), which a data request does not follow`) : e, $n = (e) => {
|
|
607
607
|
if (typeof e != "string") throw new TypeError(`[markdown-explorer] scopeSecret must be a string; got ${typeof e}`);
|
|
608
|
-
if (e.length <
|
|
608
|
+
if (e.length < Ze) throw new RangeError(`[markdown-explorer] scopeSecret must have at least ${String(Ze)} characters; got ${String(e.length)}`);
|
|
609
609
|
return e;
|
|
610
|
-
},
|
|
611
|
-
if (!
|
|
610
|
+
}, jn = (e, t) => {
|
|
611
|
+
if (!er(t)) throw new TypeError(`[markdown-explorer] ${e}.boundary must be a plain object { contains, confirm? }`);
|
|
612
612
|
for (const r of Object.keys(t))
|
|
613
|
-
if (!
|
|
614
|
-
|
|
615
|
-
},
|
|
613
|
+
if (!Qe.includes(r)) throw new RangeError(`[markdown-explorer] ${e}.boundary.${r} is not an option; expected ${Qe.join(", ")}`);
|
|
614
|
+
X(`${e}.boundary.contains`, t.contains, !1), X(`${e}.boundary.confirm`, t.confirm, !0);
|
|
615
|
+
}, Ln = (e) => {
|
|
616
616
|
if (!Array.isArray(e)) throw new TypeError("[markdown-explorer] sources must be an array");
|
|
617
617
|
const t = e;
|
|
618
618
|
if (t.length === 0) throw new RangeError("[markdown-explorer] sources must hold at least one source");
|
|
619
619
|
const r = /* @__PURE__ */ new Set();
|
|
620
620
|
for (const [n, a] of t.entries()) {
|
|
621
621
|
if (a === null || typeof a != "object") throw new TypeError(`[markdown-explorer] sources[${String(n)}] must be an object`);
|
|
622
|
-
const { id:
|
|
623
|
-
if (!
|
|
624
|
-
throw new RangeError(`[markdown-explorer] sources[${String(n)}].id must use lowercase letters, digits and "-" and start with a letter or digit; got ${typeof
|
|
625
|
-
const O = `source ${JSON.stringify(
|
|
626
|
-
if (r.has(
|
|
627
|
-
if (r.add(
|
|
628
|
-
if (typeof
|
|
629
|
-
if (
|
|
622
|
+
const { id: s, label: c, boundary: f, tree: E, children: x, ancestors: p, cacheScope: w } = a;
|
|
623
|
+
if (!Vt(s))
|
|
624
|
+
throw new RangeError(`[markdown-explorer] sources[${String(n)}].id must use lowercase letters, digits and "-" and start with a letter or digit; got ${typeof s == "string" ? JSON.stringify(s) : typeof s}`);
|
|
625
|
+
const O = `source ${JSON.stringify(s)}`;
|
|
626
|
+
if (r.has(s)) throw new RangeError(`[markdown-explorer] source ids must be unique; ${JSON.stringify(s)} repeats at sources[${String(n)}]`);
|
|
627
|
+
if (r.add(s), typeof c != "string" && typeof c != "function") throw new TypeError(`[markdown-explorer] ${O}.label must be a string or a function`);
|
|
628
|
+
if (typeof c == "string" && c.trim() === "") throw new RangeError(`[markdown-explorer] ${O}.label must not be blank`);
|
|
629
|
+
if (jn(O, f), X(`${O}.tree`, E, !1), X(`${O}.children`, x, !0), X(`${O}.ancestors`, p, !0), w !== void 0 && w !== "principal" && w !== "shared") throw new RangeError(`[markdown-explorer] ${O}.cacheScope must be "principal" or "shared"; got ${JSON.stringify(w)}`);
|
|
630
630
|
}
|
|
631
631
|
return t;
|
|
632
|
-
},
|
|
632
|
+
}, zn = (e) => {
|
|
633
633
|
if (e === !1) return !1;
|
|
634
634
|
if (e === void 0) return pe;
|
|
635
635
|
if (e === null || typeof e != "object") throw new TypeError("[markdown-explorer] treeCache must be { ttlMs, maxEntries, maxBytes } or false");
|
|
@@ -639,270 +639,278 @@ const Mr = (e) => e instanceof Error && Object.hasOwn(e, wt) && ut(e.reason), vr
|
|
|
639
639
|
maxEntries: r === void 0 ? pe.maxEntries : ye("treeCache.maxEntries", r, Number.MAX_SAFE_INTEGER),
|
|
640
640
|
maxBytes: n === void 0 ? pe.maxBytes : ye("treeCache.maxBytes", n, Number.MAX_SAFE_INTEGER)
|
|
641
641
|
};
|
|
642
|
-
},
|
|
642
|
+
}, rt = (e) => {
|
|
643
643
|
if (e === null || typeof e != "object") throw new TypeError("[markdown-explorer] authenticate must resolve to { status, ... }");
|
|
644
644
|
const { status: t } = e;
|
|
645
645
|
if (t !== "signed-in" && t !== "signed-out" && t !== "forbidden") throw new TypeError(`[markdown-explorer] authenticate resolved to an unknown status ${JSON.stringify(t)}`);
|
|
646
646
|
return e;
|
|
647
|
-
},
|
|
647
|
+
}, nt = (e, t) => {
|
|
648
648
|
if (!(t instanceof Response)) throw new TypeError(`[markdown-explorer] ${e} must return a Response`);
|
|
649
649
|
return t;
|
|
650
|
-
},
|
|
650
|
+
}, ot = (e, t) => {
|
|
651
651
|
if (typeof t != "string" || t === "") throw new TypeError("[markdown-explorer] principalKey must return a non-empty string");
|
|
652
|
-
return
|
|
653
|
-
},
|
|
652
|
+
return Jt("sha256", e).update(t, "utf8").digest("base64url").slice(0, An);
|
|
653
|
+
}, Dn = (e) => e.view === "multi" ? e.docs : e.doc === null ? [] : [e.doc], we = (e, t) => {
|
|
654
654
|
const r = e.getAll(t), [n] = r;
|
|
655
655
|
return r.length === 1 && n !== void 0 && n !== "" ? n : null;
|
|
656
|
-
},
|
|
656
|
+
}, ne = (e, t, r, n = {}) => {
|
|
657
657
|
const a = new Headers(r);
|
|
658
|
-
for (const [
|
|
658
|
+
for (const [s, c] of Object.entries({ ...Nn, ...n })) a.set(s, c);
|
|
659
659
|
return new Response(t, { status: e, headers: a });
|
|
660
|
-
}, F = (e, t, r) =>
|
|
660
|
+
}, F = (e, t, r) => ne(Ct[e.reason], JSON.stringify({ failure: e }), t, r), st = (e, t) => {
|
|
661
661
|
const r = t === "refresh" ? ["POST"] : ["GET", "HEAD"];
|
|
662
|
-
return r.includes(e.method.toUpperCase()) ? null :
|
|
663
|
-
},
|
|
664
|
-
if (e.headers.get(
|
|
662
|
+
return r.includes(e.method.toUpperCase()) ? null : ne(405, JSON.stringify({ failure: { reason: "invalid" } }), void 0, { Allow: r.join(", ") });
|
|
663
|
+
}, at = (e) => {
|
|
664
|
+
if (e.headers.get(ft) !== Ht) return !1;
|
|
665
665
|
const t = e.headers.get("Sec-Fetch-Site");
|
|
666
666
|
return t === null || t === "same-origin";
|
|
667
|
-
},
|
|
667
|
+
}, Cn = ({ parentHeaders: e, loaderHeaders: t, errorHeaders: r }) => {
|
|
668
668
|
const n = new Headers(), a = r === void 0 ? [e, t] : [e, t, r];
|
|
669
|
-
for (const
|
|
670
|
-
for (const [
|
|
671
|
-
|
|
669
|
+
for (const s of a)
|
|
670
|
+
for (const [c, f] of s)
|
|
671
|
+
c !== "set-cookie" && n.set(c, f);
|
|
672
672
|
return n;
|
|
673
|
-
},
|
|
673
|
+
}, fo = (e) => {
|
|
674
674
|
if (e === null || typeof e != "object") throw new TypeError("[markdown-explorer] the server configuration must be an object");
|
|
675
|
-
const t =
|
|
676
|
-
|
|
677
|
-
const n =
|
|
675
|
+
const t = Pn(e.config), r = $n(e.scopeSecret);
|
|
676
|
+
X("authenticate", e.authenticate, !1), X("principalKey", e.principalKey, !1), X("document", e.document, !1), X("asset", e.asset, !1), X("signedOutPage", e.signedOutPage, !1), X("forbiddenPage", e.forbiddenPage, !0), X("notices", e.notices, !0), X("onTreeRefreshed", e.onTreeRefreshed, !0);
|
|
677
|
+
const n = Ln(e.sources), a = Wt(
|
|
678
678
|
t,
|
|
679
|
-
n.map((
|
|
680
|
-
),
|
|
681
|
-
if (
|
|
679
|
+
n.map((o) => o.id)
|
|
680
|
+
), s = e.logger;
|
|
681
|
+
if (s !== void 0 && (s === null || typeof s != "object" || typeof s.error != "function"))
|
|
682
682
|
throw new TypeError("[markdown-explorer] logger must be an object with an error function");
|
|
683
|
-
const
|
|
684
|
-
if (typeof
|
|
685
|
-
const
|
|
686
|
-
if (typeof
|
|
687
|
-
return
|
|
688
|
-
}, u = (
|
|
689
|
-
const
|
|
690
|
-
let
|
|
683
|
+
const c = e.logger === void 0 ? Mr : e.logger, f = dt(e.deny), E = e.deferredTimeoutMs === void 0 ? _n : ye("deferredTimeoutMs", e.deferredTimeoutMs, In), x = zn(e.treeCache), p = () => Date.now(), w = x === !1 ? gn(p) : hn({ ...x, now: p }), O = un(e.sanitizer, t.multiView.maxPanels), _ = pn({ iframeHosts: t.iframeHosts, dataPath: t.dataPath, options: O, pool: dn(ln(), O) }), M = new Map(n.map((o) => [o.id, o])), R = Yt(t), { authenticate: k, principalKey: j, signedOutPage: S, forbiddenPage: A, onTreeRefreshed: C } = e, H = (o, d) => {
|
|
684
|
+
if (typeof o.label == "string") return o.label;
|
|
685
|
+
const i = o.label(d);
|
|
686
|
+
if (typeof i != "string" || i.trim() === "") throw new TypeError(`[markdown-explorer] the label of source ${JSON.stringify(o.id)} must be a non-blank string`);
|
|
687
|
+
return i;
|
|
688
|
+
}, u = (o, d) => o.cacheScope === "shared" ? o.id : `${o.id}\0${d}`, h = (o, d) => {
|
|
689
|
+
const i = (m) => ee(c, { operation: "boundary", sourceId: o.id, path: d, error: m });
|
|
690
|
+
let l;
|
|
691
691
|
try {
|
|
692
|
-
|
|
693
|
-
} catch (
|
|
694
|
-
return
|
|
692
|
+
l = o.boundary.contains(d);
|
|
693
|
+
} catch (m) {
|
|
694
|
+
return i(m), !1;
|
|
695
695
|
}
|
|
696
|
-
return typeof
|
|
697
|
-
}, g = async (
|
|
698
|
-
const { confirm:
|
|
699
|
-
if (
|
|
700
|
-
let
|
|
696
|
+
return typeof l == "boolean" ? l : (Xe(l, i), i(new TypeError(`[markdown-explorer] boundary.contains of source ${JSON.stringify(o.id)} must return a boolean; got ${typeof l}`)), !1);
|
|
697
|
+
}, g = async (o, d, i) => {
|
|
698
|
+
const { confirm: l } = o.boundary;
|
|
699
|
+
if (l === void 0) return { status: "inside", readPath: i };
|
|
700
|
+
let m;
|
|
701
701
|
try {
|
|
702
|
-
|
|
703
|
-
} catch (
|
|
704
|
-
return { status: "error", failure:
|
|
702
|
+
m = await l(d, i);
|
|
703
|
+
} catch (Y) {
|
|
704
|
+
return { status: "error", failure: q(Y, { operation: "boundary", sourceId: o.id, path: i }, d.signal, c) };
|
|
705
705
|
}
|
|
706
|
-
const
|
|
707
|
-
if (
|
|
708
|
-
const { inside: I, readPath: z } =
|
|
709
|
-
return I === !1 ? { status: "outside" } : I !== !0 || typeof z != "string" ?
|
|
710
|
-
}, T = async (
|
|
711
|
-
const
|
|
712
|
-
if (
|
|
713
|
-
if (
|
|
714
|
-
let
|
|
715
|
-
for (const
|
|
716
|
-
const
|
|
717
|
-
if (
|
|
718
|
-
|
|
706
|
+
const y = (Y) => (ee(c, { operation: "boundary", sourceId: o.id, path: i, error: new TypeError(`[markdown-explorer] boundary.confirm of source ${JSON.stringify(o.id)} ${Y}`) }), { status: "outside" });
|
|
707
|
+
if (m === null || typeof m != "object") return y(`must resolve to { inside, readPath? }; got ${m === null ? "null" : typeof m}`);
|
|
708
|
+
const { inside: I, readPath: z } = m;
|
|
709
|
+
return I === !1 ? { status: "outside" } : I !== !0 || typeof z != "string" ? y("must resolve to { inside: false } or { inside: true, readPath: string }") : !te(z) || f.deniesPath(z) ? (ee(c, { operation: "boundary", sourceId: o.id, path: i, 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: z };
|
|
710
|
+
}, T = async (o, d) => {
|
|
711
|
+
const i = n.filter((m) => h(m, d));
|
|
712
|
+
if (i.length === 0) return { status: "error", failure: { reason: "forbidden" } };
|
|
713
|
+
if (i.some((m) => m.boundary.confirm === void 0)) return { status: "inside", readPath: d };
|
|
714
|
+
let l = null;
|
|
715
|
+
for (const m of i) {
|
|
716
|
+
const y = await g(m, o, d);
|
|
717
|
+
if (y.status === "inside") return y;
|
|
718
|
+
y.status === "error" && l === null && (l = y.failure);
|
|
719
719
|
}
|
|
720
|
-
return { status: "error", failure:
|
|
721
|
-
}, b = (
|
|
722
|
-
const
|
|
720
|
+
return { status: "error", failure: l === null ? { reason: "forbidden" } : l };
|
|
721
|
+
}, b = (o, d, i) => {
|
|
722
|
+
const l = ({ path: m, reason: y }) => jr(c, { operation: i.operation, sourceId: i.sourceId, path: m, error: new TypeError(`[markdown-explorer] an entry with ${y} was left out`) });
|
|
723
723
|
try {
|
|
724
|
-
return { status: "ok", entries:
|
|
725
|
-
} catch (
|
|
726
|
-
return ee(
|
|
724
|
+
return { status: "ok", entries: bn(o, f, d.children !== void 0, l, (m) => h(d, m)) };
|
|
725
|
+
} catch (m) {
|
|
726
|
+
return ee(c, { ...i, error: m }), { status: "error", failure: { reason: "failed" } };
|
|
727
727
|
}
|
|
728
|
-
}, N = async (
|
|
729
|
-
let
|
|
728
|
+
}, N = async (o, d, i) => {
|
|
729
|
+
let l;
|
|
730
730
|
try {
|
|
731
|
-
|
|
732
|
-
} catch (
|
|
733
|
-
return { status: "error", failure:
|
|
731
|
+
l = await o.tree(d);
|
|
732
|
+
} catch (y) {
|
|
733
|
+
return { status: "error", failure: q(y, { operation: "tree", sourceId: o.id }, d.signal, c) };
|
|
734
734
|
}
|
|
735
|
-
const
|
|
736
|
-
return
|
|
737
|
-
}, P = (
|
|
738
|
-
if (
|
|
739
|
-
const
|
|
735
|
+
const m = b(l, o, { operation: "tree", sourceId: o.id });
|
|
736
|
+
return m.status === "ok" ? { status: "ok", entries: m.entries, loadedAt: i } : m;
|
|
737
|
+
}, P = (o, d, i, l) => (m, y) => N(o, { principal: d, request: i, signal: m, refresh: l }, y), B = (o, d, i, l, m) => w.load(u(o, i), m, P(o, d, l, !1)), v = (o, d, i, l) => w.refresh(u(o, i), l.signal, P(o, d, l, !0)), J = (o, d) => {
|
|
738
|
+
if (C === void 0) return;
|
|
739
|
+
const i = (l) => ee(c, { operation: "refresh", sourceId: o.id, error: l });
|
|
740
740
|
try {
|
|
741
|
-
Xe(
|
|
742
|
-
} catch (
|
|
743
|
-
|
|
741
|
+
Xe(C(d), i);
|
|
742
|
+
} catch (l) {
|
|
743
|
+
i(l);
|
|
744
744
|
}
|
|
745
|
-
},
|
|
746
|
-
if (
|
|
747
|
-
const
|
|
748
|
-
if (
|
|
749
|
-
if (
|
|
750
|
-
let
|
|
745
|
+
}, G = async (o, d, i, l) => {
|
|
746
|
+
if (f.deniesPath(l) || !h(o, l)) return { status: "error", failure: { reason: "forbidden" } };
|
|
747
|
+
const m = await g(o, i, l);
|
|
748
|
+
if (m.status === "outside") return { status: "error", failure: { reason: "forbidden" } };
|
|
749
|
+
if (m.status === "error") return { status: "error", failure: m.failure };
|
|
750
|
+
let y;
|
|
751
751
|
try {
|
|
752
|
-
|
|
752
|
+
y = await d(i, l, m.readPath);
|
|
753
753
|
} catch (I) {
|
|
754
|
-
return { status: "error", failure:
|
|
754
|
+
return { status: "error", failure: q(I, { operation: "children", sourceId: o.id, path: l }, i.signal, c) };
|
|
755
755
|
}
|
|
756
|
-
return b(
|
|
757
|
-
}, U = async (
|
|
758
|
-
|
|
759
|
-
if (
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
if (p.status === "error") return { status: "error", failure: p.failure };
|
|
763
|
-
let E;
|
|
756
|
+
return b(y, o, { operation: "children", sourceId: o.id, path: l });
|
|
757
|
+
}, U = async (o, d, i, l) => {
|
|
758
|
+
const m = await g(o, i, l);
|
|
759
|
+
if (m.status === "outside") return { status: "ok", ancestors: null };
|
|
760
|
+
if (m.status === "error") return { status: "error", failure: m.failure };
|
|
761
|
+
let y;
|
|
764
762
|
try {
|
|
765
|
-
|
|
763
|
+
y = await d(i, l, m.readPath, { maxDepth: t.revealDocument.maxDepth });
|
|
766
764
|
} catch (I) {
|
|
767
|
-
return { status: "error", failure:
|
|
765
|
+
return { status: "error", failure: q(I, { operation: "ancestors", sourceId: o.id, path: l }, i.signal, c) };
|
|
768
766
|
}
|
|
769
|
-
if (Array.isArray(
|
|
767
|
+
if (Array.isArray(y) && y.length > t.revealDocument.maxDepth) return { status: "ok", ancestors: null };
|
|
770
768
|
try {
|
|
771
|
-
return { status: "ok", ancestors:
|
|
769
|
+
return { status: "ok", ancestors: En(y, l, f, (I) => h(o, I)) };
|
|
772
770
|
} catch (I) {
|
|
773
|
-
return ee(
|
|
771
|
+
return ee(c, { operation: "ancestors", sourceId: o.id, path: l, error: I }), { status: "error", failure: { reason: "failed" } };
|
|
772
|
+
}
|
|
773
|
+
}, re = async (o, d, i, l) => f.deniesPath(l) ? { status: "error", failure: { reason: "forbidden" } } : h(o, l) ? U(o, d, i, l) : { status: "ok", ancestors: null }, L = async (o, d, i) => {
|
|
774
|
+
if (f.deniesPath(i)) return { status: "error", failure: { reason: "forbidden" } };
|
|
775
|
+
for (const l of [o, ...n.filter((m) => m !== o)]) {
|
|
776
|
+
if (d.signal.aborted) break;
|
|
777
|
+
const m = l.ancestors;
|
|
778
|
+
if (m === void 0 || !h(l, i)) continue;
|
|
779
|
+
const y = await U(l, m, d, i);
|
|
780
|
+
if (y.status === "ok" && y.ancestors !== null) return { status: "ok", source: l.id, ancestors: y.ancestors };
|
|
774
781
|
}
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
782
|
+
return vn;
|
|
783
|
+
}, W = async (o, d, i) => {
|
|
784
|
+
if (f.deniesPath(i)) return oe("forbidden");
|
|
785
|
+
const l = await T(o, i);
|
|
786
|
+
if (l.status === "error") return { kind: "failure", failure: l.failure };
|
|
787
|
+
let m;
|
|
780
788
|
try {
|
|
781
|
-
|
|
789
|
+
m = await e.document(o, i, l.readPath);
|
|
782
790
|
} catch (z) {
|
|
783
|
-
return { kind: "failure", failure:
|
|
791
|
+
return { kind: "failure", failure: q(z, { operation: "document", path: i }, o.signal, c) };
|
|
784
792
|
}
|
|
785
|
-
let
|
|
793
|
+
let y;
|
|
786
794
|
try {
|
|
787
|
-
|
|
795
|
+
y = kn(m, (z) => Ft(t.dataPath, i, z));
|
|
788
796
|
} catch (z) {
|
|
789
|
-
return ee(
|
|
797
|
+
return ee(c, { operation: "document", path: i, error: z }), oe("failed");
|
|
790
798
|
}
|
|
791
|
-
if (
|
|
792
|
-
const I = await _(
|
|
793
|
-
return I.status === "sanitized" ? { kind: "markdown", name:
|
|
794
|
-
},
|
|
799
|
+
if (y.kind !== "unsanitized-markdown") return y;
|
|
800
|
+
const I = await _(y.html, { scope: d, signal: o.signal });
|
|
801
|
+
return I.status === "sanitized" ? { kind: "markdown", name: y.name, htmlContent: I.html, headings: y.headings } : (I.cause !== null && ee(c, { operation: "document", path: i, error: I.cause }), oe(I.reason));
|
|
802
|
+
}, V = async (o) => {
|
|
795
803
|
if (e.notices === void 0) return Re;
|
|
796
804
|
try {
|
|
797
|
-
return
|
|
798
|
-
} catch (
|
|
799
|
-
return
|
|
805
|
+
return Rn(await e.notices(o));
|
|
806
|
+
} catch (d) {
|
|
807
|
+
return q(d, { operation: "notices" }, o.signal, c), Re;
|
|
800
808
|
}
|
|
801
|
-
},
|
|
802
|
-
const
|
|
803
|
-
if (
|
|
804
|
-
if (
|
|
805
|
-
return
|
|
806
|
-
const
|
|
807
|
-
if (
|
|
809
|
+
}, Z = async (o, d, i) => {
|
|
810
|
+
const l = we(d, fe.path);
|
|
811
|
+
if (l === null || !te(l)) return F({ reason: "invalid" }, i);
|
|
812
|
+
if (f.deniesPath(l))
|
|
813
|
+
return Lr(c, { operation: "asset", path: l }), F({ reason: "forbidden" }, i);
|
|
814
|
+
const m = await T(o, l);
|
|
815
|
+
if (m.status === "error") return F(m.failure, i);
|
|
808
816
|
try {
|
|
809
|
-
const
|
|
810
|
-
return await
|
|
811
|
-
} catch (
|
|
812
|
-
return F(
|
|
817
|
+
const y = On(await e.asset(o, l, m.readPath));
|
|
818
|
+
return await vr(o.request, y, i);
|
|
819
|
+
} catch (y) {
|
|
820
|
+
return F(q(y, { operation: "asset", path: l }, o.signal, c), i);
|
|
813
821
|
}
|
|
814
|
-
},
|
|
815
|
-
const
|
|
816
|
-
if (
|
|
817
|
-
if (
|
|
818
|
-
const
|
|
819
|
-
if (
|
|
820
|
-
const { location: I } =
|
|
821
|
-
if (
|
|
822
|
-
const le = { signal:
|
|
823
|
-
scope:
|
|
824
|
-
sources: n.map((
|
|
825
|
-
settings:
|
|
822
|
+
}, de = async ({ request: o, url: d, params: i }) => {
|
|
823
|
+
const l = rt(await k(o, qe)), m = et(l.headers);
|
|
824
|
+
if (m.set("Cache-Control", St), l.status === "signed-out") throw nt("signedOutPage", S(o, m));
|
|
825
|
+
if (l.status === "forbidden") throw A === void 0 ? Le(null, { status: 403, headers: m }) : nt("forbiddenPage", A(o, m));
|
|
826
|
+
const y = Zt(a, i["*"], d.searchParams);
|
|
827
|
+
if (y.kind === "redirect") throw Gt(y.href, { headers: m });
|
|
828
|
+
const { location: I } = y, { principal: z } = l, Y = ot(r, j(z)), ie = M.get(I.source);
|
|
829
|
+
if (ie === void 0) throw new RangeError(`[markdown-explorer] no source for ${JSON.stringify(I.source)}`);
|
|
830
|
+
const le = { signal: o.signal, timeoutMs: E, late: null, handOverMs: E }, Q = {
|
|
831
|
+
scope: Y,
|
|
832
|
+
sources: n.map((D) => ({ id: D.id, label: H(D, z) })),
|
|
833
|
+
settings: Qt(qt(o.headers.get("Cookie"), t.settingsCookie), R),
|
|
826
834
|
tree: I.view === "single" ? null : {
|
|
827
835
|
sourceId: I.source,
|
|
828
|
-
result: xe(le, (
|
|
836
|
+
result: xe(le, (D) => B(ie, z, Y, o, D).then((K) => K.result))
|
|
829
837
|
},
|
|
830
|
-
documents:
|
|
831
|
-
notices: xe({ signal:
|
|
838
|
+
documents: Dn(I).map((D) => ({ path: D, document: xe(le, (K) => W({ principal: z, request: o, signal: K, refresh: !1 }, Y, D)) })),
|
|
839
|
+
notices: xe({ signal: o.signal, timeoutMs: E, late: Re, handOverMs: 0 }, (D) => V({ principal: z, request: o, signal: D, refresh: !1 }))
|
|
832
840
|
};
|
|
833
|
-
return
|
|
834
|
-
},
|
|
835
|
-
const
|
|
836
|
-
return { authentication:
|
|
837
|
-
},
|
|
838
|
-
let
|
|
841
|
+
return Le(Q, { headers: m });
|
|
842
|
+
}, Me = async (o, d) => {
|
|
843
|
+
const i = rt(await k(o, d));
|
|
844
|
+
return { authentication: i, headers: et(i.headers) };
|
|
845
|
+
}, _t = async (o, d, i) => {
|
|
846
|
+
let l;
|
|
839
847
|
try {
|
|
840
|
-
|
|
841
|
-
} catch (
|
|
842
|
-
return F(
|
|
848
|
+
l = await Me(d, qe);
|
|
849
|
+
} catch (K) {
|
|
850
|
+
return F(q(tt(K), { operation: "authenticate" }, d.signal, c));
|
|
843
851
|
}
|
|
844
|
-
const { authentication:
|
|
845
|
-
if (
|
|
846
|
-
if (
|
|
847
|
-
const { principal: I } =
|
|
848
|
-
if (
|
|
849
|
-
const z =
|
|
850
|
-
|
|
851
|
-
const
|
|
852
|
-
if (
|
|
853
|
-
const
|
|
854
|
-
if (
|
|
855
|
-
const
|
|
856
|
-
return
|
|
852
|
+
const { authentication: m, headers: y } = l;
|
|
853
|
+
if (m.status === "signed-out") return F({ reason: "unauthorized" }, y);
|
|
854
|
+
if (m.status === "forbidden") return F({ reason: "forbidden" }, y);
|
|
855
|
+
const { principal: I } = m;
|
|
856
|
+
if (o === "asset") return Z({ principal: I, request: d, signal: d.signal, refresh: !1 }, i, y);
|
|
857
|
+
const z = ot(r, j(I));
|
|
858
|
+
y.set(Bt, z);
|
|
859
|
+
const Y = { principal: I, request: d, signal: d.signal, refresh: !1 }, ie = () => F({ reason: "invalid" }, y);
|
|
860
|
+
if (o === "document") {
|
|
861
|
+
const K = we(i, fe.path);
|
|
862
|
+
if (K === null || !te(K)) return ie();
|
|
863
|
+
const je = d.headers.has(ft);
|
|
864
|
+
return je && !at(d) ? F({ reason: "forbidden" }, y) : ne(200, JSON.stringify(await W({ ...Y, refresh: je }, z, K)), y);
|
|
857
865
|
}
|
|
858
|
-
const le = we(
|
|
859
|
-
if (le === null || (
|
|
860
|
-
const
|
|
861
|
-
if (
|
|
862
|
-
if (
|
|
863
|
-
if (
|
|
864
|
-
const
|
|
865
|
-
return
|
|
866
|
+
const le = we(i, fe.source), Q = we(i, fe.path);
|
|
867
|
+
if (le === null || (o === "children" || o === "ancestors" || o === "reveal") && (Q === null || !te(Q))) return ie();
|
|
868
|
+
const D = M.get(le);
|
|
869
|
+
if (D === void 0) return F({ reason: "not-found" }, y);
|
|
870
|
+
if (o === "tree") return ne(200, (await B(D, I, z, d, d.signal)).json, y);
|
|
871
|
+
if (o === "refresh") {
|
|
872
|
+
const K = await v(D, I, z, d);
|
|
873
|
+
return d.signal.aborted || J(D, u(D, z)), ne(200, K.json, y);
|
|
866
874
|
}
|
|
867
|
-
return
|
|
868
|
-
},
|
|
869
|
-
const { authentication:
|
|
870
|
-
if (!
|
|
871
|
-
return
|
|
872
|
-
},
|
|
875
|
+
return o === "ancestors" ? D.ancestors === void 0 || Q === null ? F({ reason: "not-found" }, y) : ne(200, JSON.stringify(await re(D, D.ancestors, Y, Q)), y) : o === "reveal" ? Q === null ? ie() : ne(200, JSON.stringify(await L(D, Y, Q)), y) : D.children === void 0 || Q === null ? F({ reason: "not-found" }, y) : ne(200, JSON.stringify(await G(D, D.children, Y, Q)), y);
|
|
876
|
+
}, Nt = async (o) => {
|
|
877
|
+
const { authentication: d, headers: i } = await Me(o, Mn);
|
|
878
|
+
if (!i.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)");
|
|
879
|
+
return d.status === "signed-in";
|
|
880
|
+
}, Pe = t.embeds.linkCardEndpoint === null ? null : Xt({ authorize: Nt }), At = async (o, d) => {
|
|
873
881
|
try {
|
|
874
|
-
return await
|
|
875
|
-
} catch (
|
|
876
|
-
return F(
|
|
882
|
+
return await o(d);
|
|
883
|
+
} catch (i) {
|
|
884
|
+
return F(q(tt(i), { operation: "authenticate" }, d.signal, c));
|
|
877
885
|
}
|
|
878
|
-
},
|
|
879
|
-
if (new URL(
|
|
880
|
-
if (!
|
|
881
|
-
if (
|
|
882
|
-
if (
|
|
883
|
-
const
|
|
884
|
-
return
|
|
886
|
+
}, $e = async (o, d, i) => {
|
|
887
|
+
if (new URL(o.url).pathname.endsWith(".data")) return F({ reason: "not-found" });
|
|
888
|
+
if (!Dt(i)) return F({ reason: "not-found" });
|
|
889
|
+
if (i === "link-card") {
|
|
890
|
+
if (Pe === null) return F({ reason: "not-found" });
|
|
891
|
+
const m = st(o, i);
|
|
892
|
+
return m !== null ? m : At(Pe, o);
|
|
885
893
|
}
|
|
886
|
-
const
|
|
887
|
-
if (
|
|
888
|
-
if (
|
|
894
|
+
const l = st(o, i);
|
|
895
|
+
if (l !== null) return l;
|
|
896
|
+
if (i === "refresh" && !at(o)) return F({ reason: "forbidden" });
|
|
889
897
|
try {
|
|
890
|
-
return await
|
|
891
|
-
} catch (
|
|
892
|
-
return F(
|
|
898
|
+
return await _t(i, o, d.searchParams);
|
|
899
|
+
} catch (m) {
|
|
900
|
+
return F(q(m, { operation: i }, o.signal, c));
|
|
893
901
|
}
|
|
894
902
|
};
|
|
895
903
|
return Object.freeze({
|
|
896
|
-
loader:
|
|
897
|
-
headers:
|
|
898
|
-
dataLoader: ({ request:
|
|
899
|
-
dataAction: ({ request:
|
|
900
|
-
forgetTree: (
|
|
901
|
-
if (typeof
|
|
902
|
-
w.forget(
|
|
904
|
+
loader: de,
|
|
905
|
+
headers: Cn,
|
|
906
|
+
dataLoader: ({ request: o, url: d, params: i }) => $e(o, d, i["*"]),
|
|
907
|
+
dataAction: ({ request: o, url: d, params: i }) => $e(o, d, i["*"]),
|
|
908
|
+
forgetTree: (o) => {
|
|
909
|
+
if (typeof o != "string") throw new TypeError(`[markdown-explorer] forgetTree needs the string key onTreeRefreshed received; got ${typeof o}`);
|
|
910
|
+
w.forget(o);
|
|
903
911
|
}
|
|
904
912
|
});
|
|
905
|
-
}, he = Object.freeze({ maxEntries: 2e4, maxDocumentBytes: 2 * 1024 * 1024, textPreviewBytes: 8 * 1024, walkConcurrency: 8 }),
|
|
913
|
+
}, he = Object.freeze({ maxEntries: 2e4, maxDocumentBytes: 2 * 1024 * 1024, textPreviewBytes: 8 * 1024, walkConcurrency: 8 }), Hn = /* @__PURE__ */ new Set(["md", "markdown", "mdx"]), Tt = new Map([
|
|
906
914
|
...[
|
|
907
915
|
["png", "image/png"],
|
|
908
916
|
["jpg", "image/jpeg"],
|
|
@@ -937,7 +945,7 @@ const Mr = (e) => e instanceof Error && Object.hasOwn(e, wt) && ut(e.reason), vr
|
|
|
937
945
|
["wma", "audio/x-ms-wma"]
|
|
938
946
|
].map(([e, t]) => [e, { kind: "media", mediaType: "audio", contentType: t }]),
|
|
939
947
|
["pdf", { kind: "media", mediaType: "pdf", contentType: "application/pdf" }]
|
|
940
|
-
]),
|
|
948
|
+
]), Bn = /* @__PURE__ */ new Set([
|
|
941
949
|
"txt",
|
|
942
950
|
"text",
|
|
943
951
|
"log",
|
|
@@ -1062,7 +1070,7 @@ const Mr = (e) => e instanceof Error && Object.hasOwn(e, wt) && ut(e.reason), vr
|
|
|
1062
1070
|
"eslintrc",
|
|
1063
1071
|
"babelrc",
|
|
1064
1072
|
"mailmap"
|
|
1065
|
-
]),
|
|
1073
|
+
]), Fn = /* @__PURE__ */ new Set([
|
|
1066
1074
|
"readme",
|
|
1067
1075
|
"license",
|
|
1068
1076
|
"licence",
|
|
@@ -1083,16 +1091,16 @@ const Mr = (e) => e instanceof Error && Object.hasOwn(e, wt) && ut(e.reason), vr
|
|
|
1083
1091
|
"jenkinsfile",
|
|
1084
1092
|
"brewfile",
|
|
1085
1093
|
"codeowners"
|
|
1086
|
-
]),
|
|
1094
|
+
]), xt = typeof ue.O_NOFOLLOW == "number" ? ue.O_NOFOLLOW : 0, kt = typeof ue.O_NONBLOCK == "number" ? ue.O_NONBLOCK : 0, it = (e) => `${e.size}-${e.mtimeNs}`, Un = (e) => e !== "" && e !== "." && e !== ".." && !e.includes(":") && !e.endsWith(".") && !e.endsWith(" "), ct = (e) => te(e) && e === e.normalize("NFC") && e.split("/").every(Un), Ot = (e) => {
|
|
1087
1095
|
const t = e.toLowerCase(), r = t.lastIndexOf(".");
|
|
1088
1096
|
return r < 0 ? "" : t.slice(r + 1);
|
|
1089
|
-
},
|
|
1090
|
-
const t =
|
|
1091
|
-
if (
|
|
1092
|
-
const r =
|
|
1097
|
+
}, Jn = (e) => {
|
|
1098
|
+
const t = Ot(e);
|
|
1099
|
+
if (Hn.has(t)) return { kind: "markdown" };
|
|
1100
|
+
const r = Tt.get(t);
|
|
1093
1101
|
if (r !== void 0) return r.kind === "image" ? { kind: "image" } : { kind: "media", mediaType: r.mediaType };
|
|
1094
1102
|
const n = e.toLowerCase();
|
|
1095
|
-
return
|
|
1103
|
+
return Bn.has(t) || !n.includes(".") && Fn.has(n) ? { kind: "text" } : { kind: "file" };
|
|
1096
1104
|
}, Ae = (e) => {
|
|
1097
1105
|
switch (typeof e == "object" && e !== null ? e.code : void 0) {
|
|
1098
1106
|
case "ENOENT":
|
|
@@ -1109,34 +1117,34 @@ const Mr = (e) => e instanceof Error && Object.hasOwn(e, wt) && ut(e.reason), vr
|
|
|
1109
1117
|
default:
|
|
1110
1118
|
return null;
|
|
1111
1119
|
}
|
|
1112
|
-
},
|
|
1120
|
+
}, Rt = (e) => {
|
|
1113
1121
|
const t = Ae(e);
|
|
1114
1122
|
return t === null ? e : new Ee(t);
|
|
1115
|
-
},
|
|
1123
|
+
}, Xn = (e) => {
|
|
1116
1124
|
const t = typeof e == "object" && e !== null ? e.code : void 0;
|
|
1117
1125
|
return t === "EACCES" || t === "EPERM" || t === "ENOENT" || t === "ENOTDIR";
|
|
1118
|
-
},
|
|
1126
|
+
}, ut = async (e, t) => {
|
|
1119
1127
|
const r = [];
|
|
1120
1128
|
for await (const n of e.createReadStream({ start: 0, end: t - 1, autoClose: !1 }))
|
|
1121
1129
|
r.push(n);
|
|
1122
1130
|
return Buffer.concat(r);
|
|
1123
|
-
},
|
|
1131
|
+
}, Gn = (e, t) => e !== null ? { start: e.offset, end: e.offset + e.length - 1 } : t === 0n ? {} : { start: 0, end: Number(t) - 1 }, Wn = async (e, t, r, n) => {
|
|
1124
1132
|
let a;
|
|
1125
1133
|
try {
|
|
1126
|
-
a = await
|
|
1127
|
-
const
|
|
1128
|
-
if (!
|
|
1129
|
-
} catch (
|
|
1130
|
-
throw await a?.close(),
|
|
1134
|
+
a = await mt(e, ue.O_RDONLY | xt | kt);
|
|
1135
|
+
const E = await a.stat({ bigint: !0 });
|
|
1136
|
+
if (!E.isFile() || E.ino !== t.ino || E.dev !== t.dev || E.size !== t.size || E.mtimeNs !== t.mtimeNs) throw new Ee("not-found");
|
|
1137
|
+
} catch (E) {
|
|
1138
|
+
throw await a?.close(), Rt(E);
|
|
1131
1139
|
}
|
|
1132
|
-
const
|
|
1133
|
-
|
|
1140
|
+
const s = a.createReadStream(Gn(r, t.size)), c = fr.toWeb(s), f = () => {
|
|
1141
|
+
s.destroy(n.reason);
|
|
1134
1142
|
};
|
|
1135
|
-
return n.aborted ?
|
|
1136
|
-
},
|
|
1137
|
-
const r =
|
|
1143
|
+
return n.aborted ? f() : n.addEventListener("abort", f, { once: !0 }), c;
|
|
1144
|
+
}, Yn = (e, t) => {
|
|
1145
|
+
const r = tr.compare(e, t);
|
|
1138
1146
|
return r !== 0 ? r : e === t ? 0 : e < t ? -1 : 1;
|
|
1139
|
-
},
|
|
1147
|
+
}, Kn = (e) => {
|
|
1140
1148
|
let t = 0;
|
|
1141
1149
|
const r = [];
|
|
1142
1150
|
return async (n) => {
|
|
@@ -1148,44 +1156,44 @@ const Mr = (e) => e instanceof Error && Object.hasOwn(e, wt) && ut(e.reason), vr
|
|
|
1148
1156
|
a === void 0 ? t -= 1 : a();
|
|
1149
1157
|
}
|
|
1150
1158
|
};
|
|
1151
|
-
},
|
|
1159
|
+
}, Vn = (e, t) => {
|
|
1152
1160
|
if (e.isSymbolicLink()) return "symbolic-link";
|
|
1153
1161
|
if (!(e.isDirectory() || e.isFile())) return "special-file";
|
|
1154
1162
|
const { name: r } = e;
|
|
1155
1163
|
return r !== r.normalize("NFC") ? "not-nfc" : r.includes(":") ? "colon" : r.endsWith(".") || r.endsWith(" ") ? "trailing-dot-or-space" : te(r) ? te(t) ? null : "path-too-long" : "control-character-or-backslash";
|
|
1156
|
-
},
|
|
1164
|
+
}, Zn = /* @__PURE__ */ new Set(["id", "label", "root", "pathPrefix", "parse", "deny", "maxEntries", "maxDocumentBytes", "textPreviewBytes", "walkConcurrency", "cacheScope", "reportSkipped"]), ge = (e, t, r) => {
|
|
1157
1165
|
if (t === void 0) return r;
|
|
1158
1166
|
if (typeof t != "number" || !Number.isSafeInteger(t) || t <= 0) throw new RangeError(`[markdown-explorer] ${e} must be a positive integer; got ${String(t)}`);
|
|
1159
1167
|
return t;
|
|
1160
|
-
},
|
|
1168
|
+
}, mo = (e) => {
|
|
1161
1169
|
if (e === null || typeof e != "object") throw new TypeError("[markdown-explorer] createFileSystemSource needs an options object");
|
|
1162
1170
|
for (const g of Object.keys(e))
|
|
1163
|
-
if (!
|
|
1171
|
+
if (!Zn.has(g)) throw new TypeError(`[markdown-explorer] createFileSystemSource does not know the option ${JSON.stringify(g)}`);
|
|
1164
1172
|
const { id: t, label: r, root: n, parse: a } = e;
|
|
1165
1173
|
if (typeof t != "string" || t === "") throw new TypeError("[markdown-explorer] a file-system source needs a non-empty id");
|
|
1166
1174
|
if (typeof r != "string" || r.trim() === "") throw new TypeError("[markdown-explorer] a file-system source needs a non-blank label");
|
|
1167
1175
|
if (typeof a != "function") throw new TypeError("[markdown-explorer] a file-system source needs a parse function");
|
|
1168
|
-
if (typeof n != "string" || !
|
|
1169
|
-
let
|
|
1176
|
+
if (typeof n != "string" || !ze(n)) throw new RangeError(`[markdown-explorer] root must be an absolute path; got ${JSON.stringify(n)}`);
|
|
1177
|
+
let s;
|
|
1170
1178
|
try {
|
|
1171
|
-
|
|
1179
|
+
s = nr(n).isDirectory();
|
|
1172
1180
|
} catch (g) {
|
|
1173
1181
|
throw new RangeError(`[markdown-explorer] root does not exist: ${n}`, { cause: g });
|
|
1174
1182
|
}
|
|
1175
|
-
if (!
|
|
1176
|
-
const
|
|
1177
|
-
if (typeof
|
|
1178
|
-
if (
|
|
1179
|
-
throw new RangeError(`[markdown-explorer] pathPrefix must be empty or relative segments ending with "/"; got ${JSON.stringify(
|
|
1180
|
-
const
|
|
1183
|
+
if (!s) throw new RangeError(`[markdown-explorer] root is not a directory: ${n}`);
|
|
1184
|
+
const c = or(n), f = e.pathPrefix;
|
|
1185
|
+
if (typeof f != "string") throw new TypeError('[markdown-explorer] pathPrefix is required: "" for a source over the whole root, or relative segments ending with "/"');
|
|
1186
|
+
if (f !== "" && !(f.endsWith("/") && ct(f.slice(0, -1))))
|
|
1187
|
+
throw new RangeError(`[markdown-explorer] pathPrefix must be empty or relative segments ending with "/"; got ${JSON.stringify(f)}`);
|
|
1188
|
+
const E = dt(e.deny), x = ge("maxEntries", e.maxEntries, he.maxEntries), p = ge("maxDocumentBytes", e.maxDocumentBytes, he.maxDocumentBytes), w = ge("textPreviewBytes", e.textPreviewBytes, he.textPreviewBytes), O = ge("walkConcurrency", e.walkConcurrency, he.walkConcurrency), _ = e.cacheScope === void 0 ? "principal" : e.cacheScope;
|
|
1181
1189
|
if (_ !== "principal" && _ !== "shared") throw new RangeError(`[markdown-explorer] cacheScope must be "principal" or "shared"; got ${JSON.stringify(_)}`);
|
|
1182
|
-
const
|
|
1183
|
-
if (
|
|
1184
|
-
const R =
|
|
1190
|
+
const M = e.reportSkipped;
|
|
1191
|
+
if (M !== void 0 && typeof M != "function") throw new TypeError("[markdown-explorer] reportSkipped must be a function");
|
|
1192
|
+
const R = M === void 0 ? (g) => {
|
|
1185
1193
|
console.warn(`[markdown-explorer] the file-system source ${JSON.stringify(g.sourceId)} does not list ${JSON.stringify(g.path)}: ${g.reason}`);
|
|
1186
|
-
} :
|
|
1187
|
-
const T =
|
|
1188
|
-
return b === "" || b === ".." || b.startsWith(`..${
|
|
1194
|
+
} : M, k = /* @__PURE__ */ new Set(), j = (g) => {
|
|
1195
|
+
const T = ir(c, ...g.split("/")), b = De(c, T);
|
|
1196
|
+
return b === "" || b === ".." || b.startsWith(`..${Ce}`) || ze(b) ? null : T;
|
|
1189
1197
|
}, S = (g, T) => {
|
|
1190
1198
|
const b = `${T}\0${g}`;
|
|
1191
1199
|
k.has(b) || k.size >= x || (k.add(b), R({ sourceId: t, path: g, reason: T }));
|
|
@@ -1195,132 +1203,132 @@ const Mr = (e) => e instanceof Error && Object.hasOwn(e, wt) && ut(e.reason), vr
|
|
|
1195
1203
|
T.abort(g.reason);
|
|
1196
1204
|
};
|
|
1197
1205
|
g.addEventListener("abort", b, { once: !0 });
|
|
1198
|
-
const N =
|
|
1206
|
+
const N = Kn(O);
|
|
1199
1207
|
let P = 0;
|
|
1200
|
-
const B = async (
|
|
1201
|
-
const
|
|
1208
|
+
const B = async (v, J) => {
|
|
1209
|
+
const G = await N(async () => {
|
|
1202
1210
|
T.signal.throwIfAborted();
|
|
1203
1211
|
try {
|
|
1204
|
-
return await
|
|
1212
|
+
return await dr(v, { withFileTypes: !0 });
|
|
1205
1213
|
} catch (L) {
|
|
1206
|
-
if (
|
|
1214
|
+
if (J === "" || !Xn(L)) throw L;
|
|
1207
1215
|
return null;
|
|
1208
1216
|
}
|
|
1209
1217
|
});
|
|
1210
|
-
if (T.signal.throwIfAborted(),
|
|
1211
|
-
return S(`${
|
|
1218
|
+
if (T.signal.throwIfAborted(), G === null)
|
|
1219
|
+
return S(`${f}${J}`, "unreadable-directory"), null;
|
|
1212
1220
|
const U = [];
|
|
1213
|
-
for (const L of
|
|
1214
|
-
if (
|
|
1215
|
-
const W =
|
|
1216
|
-
if (
|
|
1217
|
-
S(
|
|
1221
|
+
for (const L of G) {
|
|
1222
|
+
if (E.deniesName(L.name)) continue;
|
|
1223
|
+
const W = J === "" ? L.name : `${J}/${L.name}`, V = `${f}${W}`, Z = Vn(L, V);
|
|
1224
|
+
if (Z !== null) {
|
|
1225
|
+
S(V, Z);
|
|
1218
1226
|
continue;
|
|
1219
1227
|
}
|
|
1220
|
-
U.push({ dirent: L, relativePath: W, path:
|
|
1228
|
+
U.push({ dirent: L, relativePath: W, path: V });
|
|
1221
1229
|
}
|
|
1222
1230
|
if (P += U.length, P > x) throw new Ee("too-large");
|
|
1223
|
-
return U.sort((L, W) =>
|
|
1224
|
-
U.map(async ({ dirent: L, relativePath: W, path:
|
|
1225
|
-
if (!L.isDirectory()) return { name: L.name, path:
|
|
1226
|
-
const
|
|
1227
|
-
return
|
|
1231
|
+
return U.sort((L, W) => Yn(L.dirent.name, W.dirent.name)), (await Promise.all(
|
|
1232
|
+
U.map(async ({ dirent: L, relativePath: W, path: V }) => {
|
|
1233
|
+
if (!L.isDirectory()) return { name: L.name, path: V, type: "file" };
|
|
1234
|
+
const Z = await B(ar(v, L.name), W);
|
|
1235
|
+
return Z === null ? null : { name: L.name, path: V, type: "directory", children: Z };
|
|
1228
1236
|
})
|
|
1229
1237
|
)).filter((L) => L !== null);
|
|
1230
1238
|
};
|
|
1231
1239
|
try {
|
|
1232
|
-
const
|
|
1233
|
-
if (
|
|
1234
|
-
return
|
|
1235
|
-
} catch (
|
|
1236
|
-
throw T.abort(),
|
|
1240
|
+
const v = await B(c, "");
|
|
1241
|
+
if (v === null) throw new Error("[markdown-explorer] the source's root could not be listed");
|
|
1242
|
+
return v;
|
|
1243
|
+
} catch (v) {
|
|
1244
|
+
throw T.abort(), v;
|
|
1237
1245
|
} finally {
|
|
1238
1246
|
g.removeEventListener("abort", b);
|
|
1239
1247
|
}
|
|
1240
|
-
},
|
|
1241
|
-
if (!g.startsWith(
|
|
1242
|
-
const T = g.slice(
|
|
1243
|
-
if (!
|
|
1244
|
-
if (
|
|
1248
|
+
}, C = async (g) => {
|
|
1249
|
+
if (!g.startsWith(f)) return { status: "refused", reason: "not-found" };
|
|
1250
|
+
const T = g.slice(f.length);
|
|
1251
|
+
if (!ct(T)) return { status: "refused", reason: "invalid" };
|
|
1252
|
+
if (E.deniesPath(T)) return { status: "refused", reason: "forbidden" };
|
|
1245
1253
|
const b = j(T);
|
|
1246
1254
|
if (b === null) return { status: "refused", reason: "forbidden" };
|
|
1247
|
-
const N = await
|
|
1255
|
+
const N = await ur(b, { bigint: !0 });
|
|
1248
1256
|
if (N.isSymbolicLink()) return { status: "refused", reason: "forbidden" };
|
|
1249
1257
|
if (!N.isFile()) return { status: "refused", reason: "unsupported" };
|
|
1250
|
-
const P = await
|
|
1251
|
-
return P !== b ||
|
|
1252
|
-
},
|
|
1258
|
+
const P = await lr(b);
|
|
1259
|
+
return P !== b || E.deniesPath(De(c, P).split(Ce).join("/")) ? { status: "refused", reason: "forbidden" } : { status: "file", target: b, name: T.slice(T.lastIndexOf("/") + 1), stats: N };
|
|
1260
|
+
}, H = async (g, T, b) => {
|
|
1253
1261
|
let N;
|
|
1254
1262
|
try {
|
|
1255
|
-
const P = await
|
|
1256
|
-
if (P.status === "refused") return
|
|
1257
|
-
const { target: B, name:
|
|
1258
|
-
if (
|
|
1259
|
-
if (
|
|
1260
|
-
if (
|
|
1261
|
-
N = await
|
|
1262
|
-
const
|
|
1263
|
-
if (!
|
|
1264
|
-
if (
|
|
1265
|
-
if (
|
|
1266
|
-
const
|
|
1267
|
-
if (
|
|
1268
|
-
const de = await a(new TextDecoder("utf-8").decode(
|
|
1269
|
-
return { kind: "markdown", name:
|
|
1263
|
+
const P = await C(b);
|
|
1264
|
+
if (P.status === "refused") return oe(P.reason);
|
|
1265
|
+
const { target: B, name: v, stats: J } = P, G = Jn(v), U = it(J);
|
|
1266
|
+
if (G.kind === "image") return { kind: "image", name: v, version: U };
|
|
1267
|
+
if (G.kind === "media") return { kind: "media", name: v, mediaType: G.mediaType, version: U };
|
|
1268
|
+
if (G.kind === "file") return { kind: "file", name: v, version: U };
|
|
1269
|
+
N = await mt(B, ue.O_RDONLY | xt | kt);
|
|
1270
|
+
const re = await N.stat({ bigint: !0 });
|
|
1271
|
+
if (!re.isFile() || re.ino !== J.ino || re.dev !== J.dev) return oe("forbidden");
|
|
1272
|
+
if (G.kind === "markdown") {
|
|
1273
|
+
if (re.size > BigInt(p)) return oe("too-large");
|
|
1274
|
+
const Z = await ut(N, p + 1);
|
|
1275
|
+
if (Z.length > p) return oe("too-large");
|
|
1276
|
+
const de = await a(new TextDecoder("utf-8").decode(Z), T);
|
|
1277
|
+
return { kind: "markdown", name: v, htmlContent: de.htmlContent, headings: de.headings };
|
|
1270
1278
|
}
|
|
1271
|
-
const L = await
|
|
1272
|
-
if (L.includes(0)) return { kind: "file", name:
|
|
1273
|
-
const W =
|
|
1274
|
-
return { kind: "text", name:
|
|
1279
|
+
const L = await ut(N, w);
|
|
1280
|
+
if (L.includes(0)) return { kind: "file", name: v, version: U };
|
|
1281
|
+
const W = re.size > BigInt(w), V = new TextDecoder("utf-8").decode(L, { stream: W });
|
|
1282
|
+
return { kind: "text", name: v, text: V, truncated: W, version: U };
|
|
1275
1283
|
} catch (P) {
|
|
1276
1284
|
const B = Ae(P);
|
|
1277
1285
|
if (B === null) throw P;
|
|
1278
|
-
return
|
|
1286
|
+
return oe(B);
|
|
1279
1287
|
} finally {
|
|
1280
1288
|
await N?.close();
|
|
1281
1289
|
}
|
|
1282
1290
|
}, u = async (g, T, b) => {
|
|
1283
1291
|
let N;
|
|
1284
1292
|
try {
|
|
1285
|
-
N = await
|
|
1293
|
+
N = await C(b);
|
|
1286
1294
|
} catch (U) {
|
|
1287
|
-
throw
|
|
1295
|
+
throw Rt(U);
|
|
1288
1296
|
}
|
|
1289
1297
|
if (N.status === "refused") throw new Ee(N.reason);
|
|
1290
|
-
const { target: P, name: B, stats:
|
|
1291
|
-
return { name: B, contentType:
|
|
1298
|
+
const { target: P, name: B, stats: v } = N, J = Tt.get(Ot(B)), G = J === void 0 ? "" : J.contentType;
|
|
1299
|
+
return { name: B, contentType: G, size: Number(v.size), version: it(v), open: (U) => Wn(P, v, U, g.signal) };
|
|
1292
1300
|
}, h = async (g, T, b) => {
|
|
1293
1301
|
let N;
|
|
1294
1302
|
try {
|
|
1295
|
-
N = await
|
|
1296
|
-
} catch (
|
|
1297
|
-
if (Ae(
|
|
1298
|
-
throw
|
|
1303
|
+
N = await C(b);
|
|
1304
|
+
} catch (v) {
|
|
1305
|
+
if (Ae(v) !== null) return null;
|
|
1306
|
+
throw v;
|
|
1299
1307
|
}
|
|
1300
1308
|
if (N.status === "refused") return null;
|
|
1301
|
-
const P = b.slice(
|
|
1302
|
-
if (P.some((
|
|
1309
|
+
const P = b.slice(f.length).split("/");
|
|
1310
|
+
if (P.some((v) => E.deniesName(v))) return null;
|
|
1303
1311
|
const B = P.slice(0, -1);
|
|
1304
|
-
return B.map((
|
|
1312
|
+
return B.map((v, J) => `${f}${B.slice(0, J + 1).join("/")}`);
|
|
1305
1313
|
};
|
|
1306
1314
|
return Object.freeze({
|
|
1307
1315
|
id: t,
|
|
1308
1316
|
label: r,
|
|
1309
1317
|
cacheScope: _,
|
|
1310
|
-
boundary: Object.freeze({ contains: (g) => g.startsWith(
|
|
1318
|
+
boundary: Object.freeze({ contains: (g) => g.startsWith(f) }),
|
|
1311
1319
|
tree: A,
|
|
1312
1320
|
ancestors: h,
|
|
1313
|
-
document:
|
|
1321
|
+
document: H,
|
|
1314
1322
|
asset: u
|
|
1315
1323
|
});
|
|
1316
1324
|
};
|
|
1317
1325
|
export {
|
|
1318
|
-
|
|
1326
|
+
ho as DEFAULT_DENIED_PATH_SEGMENTS,
|
|
1319
1327
|
Ee as ExplorerError,
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1328
|
+
go as MAX_DOCUMENT_PATH_LENGTH,
|
|
1329
|
+
lo as anonymousAccess,
|
|
1330
|
+
mo as createFileSystemSource,
|
|
1331
|
+
fo as createMarkdownExplorer,
|
|
1324
1332
|
te as isAcceptableDocumentPath,
|
|
1325
|
-
|
|
1333
|
+
yo as isDeniedPath
|
|
1326
1334
|
};
|