react-book-reader 1.0.0 → 1.0.1

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.
@@ -0,0 +1,737 @@
1
+ var Ht = Object.defineProperty;
2
+ var Dt = (r, t, e) => t in r ? Ht(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e;
3
+ var Nt = (r, t, e) => (Dt(r, typeof t != "symbol" ? t + "" : t, e), e), $t = (r, t, e) => {
4
+ if (!t.has(r))
5
+ throw TypeError("Cannot " + e);
6
+ };
7
+ var o = (r, t, e) => ($t(r, t, "read from private field"), e ? e.call(r) : t.get(r)), b = (r, t, e) => {
8
+ if (t.has(r))
9
+ throw TypeError("Cannot add the same private member more than once");
10
+ t instanceof WeakSet ? t.add(r) : t.set(r, e);
11
+ }, I = (r, t, e, s) => ($t(r, t, "write to private field"), s ? s.call(r, e) : t.set(r, e), e);
12
+ var Rt = (r, t, e, s) => ({
13
+ set _(n) {
14
+ I(r, t, n, e);
15
+ },
16
+ get _() {
17
+ return o(r, t, s);
18
+ }
19
+ }), w = (r, t, e) => ($t(r, t, "access private method"), e);
20
+ import { f as Xt, p as qt, t as Ut, a as _t } from "./index-CFQ7Jj2W.js";
21
+ const S = {
22
+ CONTAINER: "urn:oasis:names:tc:opendocument:xmlns:container",
23
+ XHTML: "http://www.w3.org/1999/xhtml",
24
+ OPF: "http://www.idpf.org/2007/opf",
25
+ EPUB: "http://www.idpf.org/2007/ops",
26
+ DC: "http://purl.org/dc/elements/1.1/",
27
+ DCTERMS: "http://purl.org/dc/terms/",
28
+ ENC: "http://www.w3.org/2001/04/xmlenc#",
29
+ NCX: "http://www.daisy.org/z3986/2005/ncx/",
30
+ XLINK: "http://www.w3.org/1999/xlink",
31
+ SMIL: "http://www.w3.org/ns/SMIL"
32
+ }, N = {
33
+ XML: "application/xml",
34
+ NCX: "application/x-dtbncx+xml",
35
+ XHTML: "application/xhtml+xml",
36
+ HTML: "text/html",
37
+ CSS: "text/css",
38
+ SVG: "image/svg+xml",
39
+ JS: /\/(x-)?(javascript|ecmascript)/
40
+ }, At = (r) => r.toLowerCase().replace(/[-:](.)/g, (t, e) => e.toUpperCase()), zt = (r) => r ? r.replace(/[\t\n\f\r ]+/g, " ").replace(/^[\t\n\f\r ]+/, "").replace(/[\t\n\f\r ]+$/, "") : "", ct = (r, t, e) => e ? (s) => {
41
+ var n, c;
42
+ return (c = (n = s.getAttribute(r)) == null ? void 0 : n.split(/\s/)) == null ? void 0 : c.includes(t);
43
+ } : typeof t == "function" ? (s) => t(s.getAttribute(r)) : (s) => s.getAttribute(r) === t, Et = (...r) => (t) => t ? Object.fromEntries(r.map((e) => [At(e), t.getAttribute(e)])) : null, B = (r) => zt(r == null ? void 0 : r.textContent), mt = (r, t) => {
44
+ const e = r.lookupNamespaceURI(null) === t || r.lookupPrefix(t), s = e ? (n, c) => (a) => a.namespaceURI === t && a.localName === c : (n, c) => (a) => a.localName === c;
45
+ return {
46
+ $: (n, c) => [...n.children].find(s(n, c)),
47
+ $$: (n, c) => [...n.children].filter(s(n, c)),
48
+ $$$: e ? (n, c) => [...n.getElementsByTagNameNS(t, c)] : (n, c) => [...n.getElementsByTagName(c)]
49
+ };
50
+ }, et = (r, t) => {
51
+ try {
52
+ if (t.includes(":"))
53
+ return new URL(r, t);
54
+ const e = "https://invalid.invalid/", s = new URL(r, e + t);
55
+ return s.search = "", decodeURI(s.href.replace(e, ""));
56
+ } catch (e) {
57
+ return console.warn(e), r;
58
+ }
59
+ }, Ot = (r) => /^(?!blob)\w+:/i.test(r), Gt = (r, t) => {
60
+ if (!r)
61
+ return t;
62
+ const e = r.replace(/\/$/, "").split("/"), s = t.replace(/\/$/, "").split("/"), n = (e.length > s.length ? e : s).findIndex((c, a) => e[a] !== s[a]);
63
+ return n < 0 ? "" : Array(e.length - n).fill("..").concat(s.slice(n)).join("/");
64
+ }, Vt = (r) => r.slice(0, r.lastIndexOf("/") + 1), St = async (r, t, e) => {
65
+ const s = [];
66
+ r.replace(t, (...c) => (s.push(c), null));
67
+ const n = [];
68
+ for (const c of s)
69
+ n.push(await e(...c));
70
+ return r.replace(t, () => n.shift());
71
+ }, Wt = (r) => r.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&"), z = { attrs: ["dir", "xml:lang"] }, J = { name: "alternate-script", many: !0, ...z, props: ["file-as"] }, Bt = {
72
+ many: !0,
73
+ ...z,
74
+ props: [{ name: "role", many: !0, attrs: ["scheme"] }, "file-as", J],
75
+ setLegacyAttrs: (r, t) => {
76
+ var e;
77
+ if (!((e = r.role) != null && e.length)) {
78
+ const s = t.getAttributeNS(S.OPF, "role");
79
+ s && (r.role = [{ value: s }]);
80
+ }
81
+ r.fileAs ?? (r.fileAs = t.getAttributeNS(S.OPF, "file-as"));
82
+ }
83
+ }, Jt = [
84
+ {
85
+ name: "title",
86
+ many: !0,
87
+ ...z,
88
+ props: ["title-type", "display-seq", "file-as", J]
89
+ },
90
+ {
91
+ name: "identifier",
92
+ many: !0,
93
+ props: [{ name: "identifier-type", attrs: ["scheme"] }],
94
+ setLegacyAttrs: (r, t) => {
95
+ if (!r.identifierType) {
96
+ const e = t.getAttributeNS(S.OPF, "scheme");
97
+ e && (r.identifierType = { value: e });
98
+ }
99
+ }
100
+ },
101
+ { name: "language", many: !0 },
102
+ { name: "creator", ...Bt },
103
+ { name: "contributor", ...Bt },
104
+ { name: "publisher", ...z, props: ["file-as", J] },
105
+ { name: "description", ...z, props: [J] },
106
+ { name: "rights", ...z, props: [J] },
107
+ { name: "date" },
108
+ { name: "dcterms:modified", type: "meta" },
109
+ {
110
+ name: "subject",
111
+ many: !0,
112
+ ...z,
113
+ props: ["term", "authority", J],
114
+ setLegacyAttrs: (r, t) => {
115
+ r.term ?? (r.term = t.getAttributeNS(S.OPF, "term")), r.authority ?? (r.authority = t.getAttributeNS(S.OPF, "authority"));
116
+ }
117
+ },
118
+ { name: "source", many: !0 },
119
+ {
120
+ name: "belongs-to-collection",
121
+ type: "meta",
122
+ many: !0,
123
+ ...z,
124
+ props: [
125
+ "collection-type",
126
+ "group-position",
127
+ "dcterms:identifier",
128
+ "file-as",
129
+ J,
130
+ { name: "belongs-to-collection", recursive: !0 }
131
+ ]
132
+ }
133
+ ], Kt = (r) => {
134
+ var g, A;
135
+ const { $: t, $$: e } = mt(r, S.OPF), s = t(r.documentElement, "metadata"), n = Array.from(s.children), c = (m, l) => {
136
+ var _;
137
+ if (!l)
138
+ return null;
139
+ const { props: f = [], attrs: v = [] } = m, k = B(l);
140
+ if (!f.length && !v.length)
141
+ return k;
142
+ const x = l.getAttribute("id"), j = x ? n.filter(ct("refines", "#" + x)) : [], q = Object.fromEntries([["value", k]].concat(f.map((M) => {
143
+ const { many: gt, recursive: yt } = M, W = typeof M == "string" ? M : M.name, ot = ct("property", W), at = yt ? m : M;
144
+ return [At(W), gt ? j.filter(ot).map((wt) => c(at, wt)) : c(at, j.find(ot))];
145
+ })).concat(v.map((M) => [At(M), l.getAttribute(M)])));
146
+ return (_ = m.setLegacyAttrs) == null || _.call(m, q, l), q;
147
+ }, a = n.filter(ct("refines", null)), u = Object.fromEntries(Jt.map((m) => {
148
+ const { type: l, name: f, many: v } = m, k = l === "meta" ? (x) => x.namespaceURI === S.OPF && x.getAttribute("property") === f : (x) => x.namespaceURI === S.DC && x.localName === f;
149
+ return [At(f), v ? a.filter(k).map((x) => c(m, x)) : c(m, a.find(k))];
150
+ })), i = e(s, "meta"), p = (m) => i.filter(ct("property", (l) => l == null ? void 0 : l.startsWith(m))).map((l) => [l.getAttribute("property").replace(m, ""), l]), d = Object.fromEntries(p("rendition:").map(([m, l]) => [m, B(l)])), h = { narrator: [], duration: {} };
151
+ for (const [m, l] of p("media:")) {
152
+ const f = B(l);
153
+ m === "duration" ? h.duration[((A = (g = l.getAttribute("refines")) == null ? void 0 : g.split("#")) == null ? void 0 : A[1]) ?? ""] = Ct(f) : m === "active-class" ? h.activeClass = f : m === "narrator" ? h.narrator.push(f) : m === "playback-active-class" && (h.playbackActiveClass = f);
154
+ }
155
+ return { metadata: u, rendition: d, media: h };
156
+ }, Qt = (r, t = (e) => e) => {
157
+ var m;
158
+ const { $: e, $$: s, $$$: n } = mt(r, S.XHTML), c = (l) => l ? decodeURI(t(l)) : null, a = (l) => (f) => {
159
+ var _;
160
+ const v = e(f, "a") ?? e(f, "span"), k = e(f, "ol"), x = c(v == null ? void 0 : v.getAttribute("href")), q = { label: B(v) || (v == null ? void 0 : v.getAttribute("title")), href: x, subitems: u(k) };
161
+ return l && (q.type = (_ = v == null ? void 0 : v.getAttributeNS(S.EPUB, "type")) == null ? void 0 : _.split(/\s/)), q;
162
+ }, u = (l, f) => l ? s(l, "li").map(a(f)) : null, i = (l, f) => u(e(l, "ol"), f), p = n(r, "nav");
163
+ let d = null, h = null, g = null, A = [];
164
+ for (const l of p) {
165
+ const f = ((m = l.getAttributeNS(S.EPUB, "type")) == null ? void 0 : m.split(/\s/)) ?? [];
166
+ f.includes("toc") ? d ?? (d = i(l)) : f.includes("page-list") ? h ?? (h = i(l)) : f.includes("landmarks") ? g ?? (g = i(l, !0)) : A.push({
167
+ label: B(l.firstElementChild),
168
+ type: f,
169
+ list: i(l)
170
+ });
171
+ }
172
+ return { toc: d, pageList: h, landmarks: g, others: A };
173
+ }, Yt = (r, t = (e) => e) => {
174
+ const { $: e, $$: s } = mt(r, S.NCX), n = (i) => i ? decodeURI(t(i)) : null, c = (i) => {
175
+ const p = e(i, "navLabel"), d = e(i, "content"), h = B(p), g = n(d.getAttribute("src"));
176
+ if (i.localName === "navPoint") {
177
+ const A = s(i, "navPoint");
178
+ return { label: h, href: g, subitems: A.length ? A.map(c) : null };
179
+ }
180
+ return { label: h, href: g };
181
+ }, a = (i, p) => s(i, p).map(c), u = (i, p) => {
182
+ const d = e(r.documentElement, i);
183
+ return d ? a(d, p) : null;
184
+ };
185
+ return {
186
+ toc: u("navMap", "navPoint"),
187
+ pageList: u("pageList", "pageTarget"),
188
+ others: s(r.documentElement, "navList").map((i) => ({
189
+ label: B(e(i, "navLabel")),
190
+ list: a(i, "navTarget")
191
+ }))
192
+ };
193
+ }, Ct = (r) => {
194
+ if (!r)
195
+ return;
196
+ const t = r.split(":").map((a) => parseFloat(a));
197
+ if (t.length === 3) {
198
+ const [a, u, i] = t;
199
+ return a * 60 * 60 + u * 60 + i;
200
+ }
201
+ if (t.length === 2) {
202
+ const [a, u] = t;
203
+ return a * 60 + u;
204
+ }
205
+ const [e, s] = r.split(/(?=[^\d.])/), n = parseFloat(e), c = s === "h" ? 60 * 60 : s === "min" ? 60 : s === "ms" ? 1e-3 : 1;
206
+ return n * c;
207
+ };
208
+ var G, lt, Y, C, $, T, ht, ut, Tt, Pt, st, Lt, V, tt, F, K, pt, Mt, rt, It, H, Q;
209
+ class Zt extends EventTarget {
210
+ constructor(e, s) {
211
+ super();
212
+ b(this, Tt);
213
+ b(this, st);
214
+ b(this, V);
215
+ b(this, F);
216
+ b(this, pt);
217
+ b(this, rt);
218
+ b(this, H);
219
+ b(this, G, void 0);
220
+ b(this, lt, void 0);
221
+ b(this, Y, void 0);
222
+ b(this, C, void 0);
223
+ b(this, $, void 0);
224
+ b(this, T, void 0);
225
+ b(this, ht, 1);
226
+ b(this, ut, 1);
227
+ this.book = e, this.loadXML = s;
228
+ }
229
+ async start(e, s = () => !0) {
230
+ var u;
231
+ (u = o(this, T)) == null || u.pause();
232
+ const n = this.book.sections[e], c = n == null ? void 0 : n.id;
233
+ if (!c)
234
+ return;
235
+ const { mediaOverlay: a } = n;
236
+ if (!a)
237
+ return this.start(e + 1);
238
+ I(this, Y, e), await w(this, Tt, Pt).call(this, a);
239
+ for (let i = 0; i < o(this, G).length; i++) {
240
+ const { items: p } = o(this, G)[i];
241
+ for (let d = 0; d < p.length; d++)
242
+ if (p[d].text.split("#")[0] === c && s(p[d], d, p))
243
+ return w(this, H, Q).call(this, i, d).catch((h) => w(this, F, K).call(this, h));
244
+ }
245
+ }
246
+ pause() {
247
+ var e;
248
+ (e = o(this, T)) == null || e.pause();
249
+ }
250
+ resume() {
251
+ var e;
252
+ (e = o(this, T)) == null || e.play().catch((s) => w(this, F, K).call(this, s));
253
+ }
254
+ prev() {
255
+ o(this, $) > 0 ? w(this, H, Q).call(this, o(this, C), o(this, $) - 1) : o(this, C) > 0 ? w(this, H, Q).call(this, o(this, C) - 1, o(this, G)[o(this, C) - 1].items.length - 1) : o(this, Y) > 0 && this.start(o(this, Y) - 1, (e, s, n) => s === n.length - 1);
256
+ }
257
+ next() {
258
+ w(this, H, Q).call(this, o(this, C), o(this, $) + 1);
259
+ }
260
+ setVolume(e) {
261
+ I(this, ht, e), o(this, T) && (o(this, T).volume = e);
262
+ }
263
+ setRate(e) {
264
+ I(this, ut, e), o(this, T) && (o(this, T).playbackRate = e);
265
+ }
266
+ }
267
+ G = new WeakMap(), lt = new WeakMap(), Y = new WeakMap(), C = new WeakMap(), $ = new WeakMap(), T = new WeakMap(), ht = new WeakMap(), ut = new WeakMap(), Tt = new WeakSet(), Pt = async function(e) {
268
+ if (o(this, lt) === e)
269
+ return;
270
+ const s = await this.loadXML(e.href), n = (u) => u ? et(u, e.href) : null, { $: c, $$$: a } = mt(s, S.SMIL);
271
+ I(this, C, -1), I(this, $, -1), I(this, G, a(s, "par").reduce((u, i) => {
272
+ var l;
273
+ const p = n((l = c(i, "text")) == null ? void 0 : l.getAttribute("src")), d = c(i, "audio");
274
+ if (!p || !d)
275
+ return u;
276
+ const h = n(d.getAttribute("src")), g = Ct(d.getAttribute("clipBegin")), A = Ct(d.getAttribute("clipEnd")), m = u.at(-1);
277
+ return (m == null ? void 0 : m.src) === h ? m.items.push({ text: p, begin: g, end: A }) : u.push({ src: h, items: [{ text: p, begin: g, end: A }] }), u;
278
+ }, [])), I(this, lt, e);
279
+ }, st = new WeakSet(), Lt = function() {
280
+ return o(this, G)[o(this, C)];
281
+ }, V = new WeakSet(), tt = function() {
282
+ var e, s;
283
+ return (s = (e = o(this, st, Lt)) == null ? void 0 : e.items) == null ? void 0 : s[o(this, $)];
284
+ }, F = new WeakSet(), K = function(e) {
285
+ console.error(e), this.dispatchEvent(new CustomEvent("error", { detail: e }));
286
+ }, pt = new WeakSet(), Mt = function() {
287
+ this.dispatchEvent(new CustomEvent("highlight", { detail: o(this, V, tt) }));
288
+ }, rt = new WeakSet(), It = function() {
289
+ this.dispatchEvent(new CustomEvent("unhighlight", { detail: o(this, V, tt) }));
290
+ }, H = new WeakSet(), Q = async function(e, s) {
291
+ var u;
292
+ o(this, T) && (o(this, T).pause(), URL.revokeObjectURL(o(this, T).src), I(this, T, null)), I(this, C, e), I(this, $, s);
293
+ const n = (u = o(this, st, Lt)) == null ? void 0 : u.src;
294
+ if (!n || !o(this, V, tt))
295
+ return this.start(o(this, Y) + 1);
296
+ const c = URL.createObjectURL(await this.book.loadBlob(n)), a = new Audio(c);
297
+ I(this, T, a), a.addEventListener("timeupdate", () => {
298
+ var h, g;
299
+ if (a.paused)
300
+ return;
301
+ const i = a.currentTime, { items: p } = o(this, st, Lt);
302
+ if (i > ((h = o(this, V, tt)) == null ? void 0 : h.end) && (w(this, rt, It).call(this), o(this, $) === p.length - 1)) {
303
+ w(this, H, Q).call(this, o(this, C) + 1, 0).catch((A) => w(this, F, K).call(this, A));
304
+ return;
305
+ }
306
+ const d = o(this, $);
307
+ for (; ((g = p[o(this, $) + 1]) == null ? void 0 : g.begin) <= i; )
308
+ Rt(this, $)._++;
309
+ o(this, $) !== d && w(this, pt, Mt).call(this);
310
+ }), a.addEventListener("error", () => w(this, F, K).call(this, new Error(`Failed to load ${n}`))), a.addEventListener("playing", () => w(this, pt, Mt).call(this)), a.addEventListener("pause", () => w(this, rt, It).call(this)), a.addEventListener("ended", () => {
311
+ w(this, rt, It).call(this), URL.revokeObjectURL(c), I(this, T, null), w(this, H, Q).call(this, e + 1, 0).catch((i) => w(this, F, K).call(this, i));
312
+ }), a.addEventListener("canplaythrough", () => {
313
+ a.currentTime = o(this, V, tt).begin ?? 0, a.volume = o(this, ht), a.playbackRate = o(this, ut), a.play().catch((i) => w(this, F, K).call(this, i));
314
+ });
315
+ };
316
+ const jt = /([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})/, te = (r) => {
317
+ for (const t of r.getElementsByTagNameNS(S.DC, "identifier")) {
318
+ const [e] = B(t).split(":").slice(-1);
319
+ if (jt.test(e))
320
+ return e;
321
+ }
322
+ return "";
323
+ }, Ft = (r) => B(
324
+ r.getElementById(r.documentElement.getAttribute("unique-identifier")) ?? r.getElementsByTagNameNS(S.DC, "identifier")[0]
325
+ ), kt = async (r, t, e) => {
326
+ const s = new Uint8Array(await e.slice(0, t).arrayBuffer());
327
+ t = Math.min(t, s.length);
328
+ for (var n = 0; n < t; n++)
329
+ s[n] = s[n] ^ r[n % r.length];
330
+ return new Blob([s, e.slice(t)], { type: e.type });
331
+ }, ee = async (r) => {
332
+ const t = new TextEncoder().encode(r), e = await globalThis.crypto.subtle.digest("SHA-1", t);
333
+ return new Uint8Array(e);
334
+ }, se = (r = ee) => ({
335
+ "http://www.idpf.org/2008/embedding": {
336
+ key: (t) => r(Ft(t).replaceAll(/[\u0020\u0009\u000d\u000a]/g, "")),
337
+ decode: (t, e) => kt(t, 1040, e)
338
+ },
339
+ "http://ns.adobe.com/pdf/enc#RC": {
340
+ key: (t) => {
341
+ const e = te(t).replaceAll("-", "");
342
+ return Uint8Array.from({ length: 16 }, (s, n) => parseInt(e.slice(n * 2, n * 2 + 2), 16));
343
+ },
344
+ decode: (t, e) => kt(t, 1024, e)
345
+ }
346
+ });
347
+ var dt, it, ft;
348
+ class re {
349
+ constructor(t) {
350
+ b(this, dt, /* @__PURE__ */ new Map());
351
+ b(this, it, /* @__PURE__ */ new Map());
352
+ b(this, ft, void 0);
353
+ I(this, ft, t);
354
+ }
355
+ async init(t, e) {
356
+ if (!t)
357
+ return;
358
+ const s = Array.from(
359
+ t.getElementsByTagNameNS(S.ENC, "EncryptedData"),
360
+ (n) => {
361
+ var c, a;
362
+ return {
363
+ algorithm: (c = n.getElementsByTagNameNS(S.ENC, "EncryptionMethod")[0]) == null ? void 0 : c.getAttribute("Algorithm"),
364
+ uri: (a = n.getElementsByTagNameNS(S.ENC, "CipherReference")[0]) == null ? void 0 : a.getAttribute("URI")
365
+ };
366
+ }
367
+ );
368
+ for (const { algorithm: n, uri: c } of s) {
369
+ if (!o(this, it).has(n)) {
370
+ const a = o(this, ft)[n];
371
+ if (!a) {
372
+ console.warn("Unknown encryption algorithm");
373
+ continue;
374
+ }
375
+ const u = await a.key(e);
376
+ o(this, it).set(n, (i) => a.decode(u, i));
377
+ }
378
+ o(this, dt).set(c, n);
379
+ }
380
+ }
381
+ getDecoder(t) {
382
+ return o(this, it).get(o(this, dt).get(t)) ?? ((e) => e);
383
+ }
384
+ }
385
+ dt = new WeakMap(), it = new WeakMap(), ft = new WeakMap();
386
+ class ie {
387
+ constructor({ opf: t, resolveHref: e }) {
388
+ var d, h, g, A, m;
389
+ this.opf = t;
390
+ const { $: s, $$: n, $$$: c } = mt(t, S.OPF), a = s(t.documentElement, "manifest"), u = s(t.documentElement, "spine"), i = n(u, "itemref");
391
+ this.manifest = n(a, "item").map(Et("href", "id", "media-type", "properties", "media-overlay")).map((l) => {
392
+ var f;
393
+ return l.href = e(l.href), l.properties = (f = l.properties) == null ? void 0 : f.split(/\s/), l;
394
+ }), this.spine = i.map(Et("idref", "id", "linear", "properties")).map((l) => {
395
+ var f;
396
+ return l.properties = (f = l.properties) == null ? void 0 : f.split(/\s/), l;
397
+ }), this.pageProgressionDirection = u.getAttribute("page-progression-direction"), this.navPath = (d = this.getItemByProperty("nav")) == null ? void 0 : d.href, this.ncxPath = (h = this.getItemByID(u.getAttribute("toc")) ?? this.manifest.find((l) => l.mediaType === N.NCX)) == null ? void 0 : h.href;
398
+ const p = s(t.documentElement, "guide");
399
+ p && (this.guide = n(p, "reference").map(Et("type", "title", "href")).map(({ type: l, title: f, href: v }) => ({
400
+ label: f,
401
+ type: l.split(/\s/),
402
+ href: e(v)
403
+ }))), this.cover = this.getItemByProperty("cover-image") ?? this.getItemByID((g = c(t, "meta").find(ct("name", "cover"))) == null ? void 0 : g.getAttribute("content")) ?? this.getItemByHref((m = (A = this.guide) == null ? void 0 : A.find((l) => l.type.includes("cover"))) == null ? void 0 : m.href), this.cfis = Xt(i);
404
+ }
405
+ getItemByID(t) {
406
+ return this.manifest.find((e) => e.id === t);
407
+ }
408
+ getItemByHref(t) {
409
+ return this.manifest.find((e) => e.href === t);
410
+ }
411
+ getItemByProperty(t) {
412
+ return this.manifest.find((e) => {
413
+ var s;
414
+ return (s = e.properties) == null ? void 0 : s.includes(t);
415
+ });
416
+ }
417
+ resolveCFI(t) {
418
+ const e = qt(t), s = (e.parent ?? e).shift();
419
+ let n = Ut(this.opf, s);
420
+ n && n.nodeName !== "idref" && (s.at(-1).id = null, n = Ut(this.opf, s));
421
+ const c = n == null ? void 0 : n.getAttribute("idref");
422
+ return { index: this.spine.findIndex((i) => i.idref === c), anchor: (i) => _t(i, e) };
423
+ }
424
+ }
425
+ var D, X, O;
426
+ class ne {
427
+ constructor({ loadText: t, loadBlob: e, resources: s }) {
428
+ b(this, D, /* @__PURE__ */ new Map());
429
+ b(this, X, /* @__PURE__ */ new Map());
430
+ b(this, O, /* @__PURE__ */ new Map());
431
+ Nt(this, "allowScript", !1);
432
+ this.loadText = t, this.loadBlob = e, this.manifest = s.manifest, this.assets = s.manifest;
433
+ }
434
+ createURL(t, e, s, n) {
435
+ if (!e)
436
+ return "";
437
+ const c = URL.createObjectURL(new Blob([e], { type: s }));
438
+ if (o(this, D).set(t, c), o(this, O).set(t, 1), n) {
439
+ const a = o(this, X).get(n);
440
+ a ? a.push(t) : o(this, X).set(n, [t]);
441
+ }
442
+ return c;
443
+ }
444
+ ref(t, e) {
445
+ const s = o(this, X).get(e);
446
+ return s != null && s.includes(t) || (o(this, O).set(t, o(this, O).get(t) + 1), s ? s.push(t) : o(this, X).set(e, [t])), o(this, D).get(t);
447
+ }
448
+ unref(t) {
449
+ if (!o(this, O).has(t))
450
+ return;
451
+ const e = o(this, O).get(t) - 1;
452
+ if (e < 1) {
453
+ URL.revokeObjectURL(o(this, D).get(t)), o(this, D).delete(t), o(this, O).delete(t);
454
+ const s = o(this, X).get(t);
455
+ if (s)
456
+ for (; s.length; )
457
+ this.unref(s.pop());
458
+ o(this, X).delete(t);
459
+ } else
460
+ o(this, O).set(t, e);
461
+ }
462
+ // load manifest item, recursively loading all resources as needed
463
+ async loadItem(t, e = []) {
464
+ if (!t)
465
+ return null;
466
+ const { href: s, mediaType: n } = t, c = N.JS.test(t.mediaType);
467
+ if (c && !this.allowScript)
468
+ return null;
469
+ const a = e.at(-1);
470
+ return o(this, D).has(s) ? this.ref(s, a) : (c || [N.XHTML, N.HTML, N.CSS, N.SVG].includes(n)) && e.every((i) => i !== s) ? this.loadReplaced(t, e) : this.createURL(s, await this.loadBlob(s), n, a);
471
+ }
472
+ async loadHref(t, e, s = []) {
473
+ if (Ot(t))
474
+ return t;
475
+ const n = et(t, e), c = this.manifest.find((a) => a.href === n);
476
+ return c ? this.loadItem(c, s.concat(e)) : t;
477
+ }
478
+ async loadReplaced(t, e = []) {
479
+ const { href: s, mediaType: n } = t, c = e.at(-1), a = await this.loadText(s);
480
+ if (!a)
481
+ return null;
482
+ if ([N.XHTML, N.HTML, N.SVG].includes(n)) {
483
+ let i = new DOMParser().parseFromString(a, n);
484
+ if (n === N.XHTML && i.querySelector("parsererror") && (console.warn(i.querySelector("parsererror").innerText), t.mediaType = N.HTML, i = new DOMParser().parseFromString(a, t.mediaType)), [N.XHTML, N.SVG].includes(t.mediaType)) {
485
+ let h = i.firstChild;
486
+ for (; h instanceof ProcessingInstruction; ) {
487
+ if (h.data) {
488
+ const g = await St(
489
+ h.data,
490
+ /(?:^|\s*)(href\s*=\s*['"])([^'"]*)(['"])/i,
491
+ (A, m, l, f) => this.loadHref(l, s, e).then((v) => `${m}${v}${f}`)
492
+ );
493
+ h.replaceWith(i.createProcessingInstruction(
494
+ h.target,
495
+ g
496
+ ));
497
+ }
498
+ h = h.nextSibling;
499
+ }
500
+ }
501
+ const p = async (h, g) => h.setAttribute(
502
+ g,
503
+ await this.loadHref(h.getAttribute(g), s, e)
504
+ );
505
+ for (const h of i.querySelectorAll("link[href]"))
506
+ await p(h, "href");
507
+ for (const h of i.querySelectorAll("[src]"))
508
+ await p(h, "src");
509
+ for (const h of i.querySelectorAll("[poster]"))
510
+ await p(h, "poster");
511
+ for (const h of i.querySelectorAll("object[data]"))
512
+ await p(h, "data");
513
+ for (const h of i.querySelectorAll("[*|href]:not([href])"))
514
+ h.setAttributeNS(S.XLINK, "href", await this.loadHref(
515
+ h.getAttributeNS(S.XLINK, "href"),
516
+ s,
517
+ e
518
+ ));
519
+ for (const h of i.querySelectorAll("style"))
520
+ h.textContent && (h.textContent = await this.replaceCSS(h.textContent, s, e));
521
+ for (const h of i.querySelectorAll("[style]"))
522
+ h.setAttribute(
523
+ "style",
524
+ await this.replaceCSS(h.getAttribute("style"), s, e)
525
+ );
526
+ const d = new XMLSerializer().serializeToString(i);
527
+ return this.createURL(s, d, t.mediaType, c);
528
+ }
529
+ const u = n === N.CSS ? await this.replaceCSS(a, s, e) : await this.replaceString(a, s, e);
530
+ return this.createURL(s, u, n, c);
531
+ }
532
+ async replaceCSS(t, e, s = []) {
533
+ const n = await St(
534
+ t,
535
+ /url\(\s*["']?([^'"\n]*?)\s*["']?\s*\)/gi,
536
+ (i, p) => this.loadHref(p, e, s).then((d) => `url("${d}")`)
537
+ ), c = await St(
538
+ n,
539
+ /@import\s*["']([^"'\n]*?)["']/gi,
540
+ (i, p) => this.loadHref(p, e, s).then((d) => `@import "${d}"`)
541
+ ), a = (window == null ? void 0 : window.innerWidth) ?? 800, u = (window == null ? void 0 : window.innerHeight) ?? 600;
542
+ return c.replace(new RegExp("(?<=[{\\s;])-epub-", "gi"), "").replace(/(\d*\.?\d+)vw/gi, (i, p) => parseFloat(p) * a / 100 + "px").replace(/(\d*\.?\d+)vh/gi, (i, p) => parseFloat(p) * u / 100 + "px").replace(/page-break-(after|before|inside)\s*:/gi, (i, p) => `-webkit-column-break-${p}:`).replace(/break-(after|before|inside)\s*:\s*(avoid-)?page/gi, (i, p, d) => `break-${p}: ${d ?? ""}column`);
543
+ }
544
+ // find & replace all possible relative paths for all assets without parsing
545
+ replaceString(t, e, s = []) {
546
+ const n = /* @__PURE__ */ new Map(), c = this.assets.map((u) => {
547
+ if (u.href === e)
548
+ return;
549
+ const i = Gt(Vt(e), u.href), p = encodeURI(i), d = "/" + u.href, h = encodeURI(d), g = /* @__PURE__ */ new Set([i, p, d, h]);
550
+ for (const A of g)
551
+ n.set(A, u);
552
+ return Array.from(g);
553
+ }).flat().filter((u) => u);
554
+ if (!c.length)
555
+ return t;
556
+ const a = new RegExp(c.map(Wt).join("|"), "g");
557
+ return St(t, a, async (u) => this.loadItem(
558
+ n.get(u.replace(/^\//, "")),
559
+ s.concat(e)
560
+ ));
561
+ }
562
+ unloadItem(t) {
563
+ this.unref(t == null ? void 0 : t.href);
564
+ }
565
+ destroy() {
566
+ for (const t of o(this, D).values())
567
+ URL.revokeObjectURL(t);
568
+ }
569
+ }
570
+ D = new WeakMap(), X = new WeakMap(), O = new WeakMap();
571
+ const oe = (r, t) => r.getElementById(t) ?? r.querySelector(`[name="${CSS.escape(t)}"]`), ae = (r) => {
572
+ for (const t of r) {
573
+ if (t === "page-spread-left" || t === "rendition:page-spread-left")
574
+ return "left";
575
+ if (t === "page-spread-right" || t === "rendition:page-spread-right")
576
+ return "right";
577
+ if (t === "rendition:page-spread-center")
578
+ return "center";
579
+ }
580
+ }, ce = (r) => r ? {
581
+ fixedLayout: B(r.querySelector('option[name="fixed-layout"]')),
582
+ openToSpread: B(r.querySelector('option[name="open-to-spread"]'))
583
+ } : null;
584
+ var Z, nt, U, P;
585
+ class ue {
586
+ constructor({ loadText: t, loadBlob: e, getSize: s, sha1: n }) {
587
+ b(this, U);
588
+ Nt(this, "parser", new DOMParser());
589
+ b(this, Z, void 0);
590
+ b(this, nt, void 0);
591
+ this.loadText = t, this.loadBlob = e, this.getSize = s, I(this, nt, new re(se(n)));
592
+ }
593
+ async init() {
594
+ var l, f, v, k, x, j, q, _, M, gt, yt, W, ot, at, wt;
595
+ const t = await w(this, U, P).call(this, "META-INF/container.xml");
596
+ if (!t)
597
+ throw new Error("Failed to load container file");
598
+ const e = Array.from(
599
+ t.getElementsByTagNameNS(S.CONTAINER, "rootfile"),
600
+ Et("full-path", "media-type")
601
+ ).filter((y) => y.mediaType === "application/oebps-package+xml");
602
+ if (!e.length)
603
+ throw new Error("No package document defined in container");
604
+ const s = e[0].fullPath, n = await w(this, U, P).call(this, s);
605
+ if (!n)
606
+ throw new Error("Failed to load package document");
607
+ const c = await w(this, U, P).call(this, "META-INF/encryption.xml");
608
+ await o(this, nt).init(c, n), this.resources = new ie({
609
+ opf: n,
610
+ resolveHref: (y) => et(y, s)
611
+ }), I(this, Z, new ne({
612
+ loadText: this.loadText,
613
+ loadBlob: (y) => Promise.resolve(this.loadBlob(y)).then(o(this, nt).getDecoder(y)),
614
+ resources: this.resources
615
+ })), this.sections = this.resources.spine.map((y, E) => {
616
+ const { idref: L, linear: xt, properties: bt = [] } = y, R = this.resources.getItemByID(L);
617
+ return R ? {
618
+ id: R.href,
619
+ load: () => o(this, Z).loadItem(R),
620
+ unload: () => o(this, Z).unloadItem(R),
621
+ createDocument: () => this.loadDocument(R),
622
+ size: this.getSize(R.href),
623
+ cfi: this.resources.cfis[E],
624
+ linear: xt,
625
+ pageSpread: ae(bt),
626
+ resolveHref: (vt) => et(vt, R.href),
627
+ mediaOverlay: R.mediaOverlay ? this.resources.getItemByID(R.mediaOverlay) : null
628
+ } : (console.warn(`Could not find item with ID "${L}" in manifest`), null);
629
+ }).filter((y) => y);
630
+ const { navPath: a, ncxPath: u } = this.resources;
631
+ if (a)
632
+ try {
633
+ const y = (L) => et(L, a), E = Qt(await w(this, U, P).call(this, a), y);
634
+ this.toc = E.toc, this.pageList = E.pageList, this.landmarks = E.landmarks;
635
+ } catch (y) {
636
+ console.warn(y);
637
+ }
638
+ if (!this.toc && u)
639
+ try {
640
+ const y = (L) => et(L, u), E = Yt(await w(this, U, P).call(this, u), y);
641
+ this.toc = E.toc, this.pageList = E.pageList;
642
+ } catch (y) {
643
+ console.warn(y);
644
+ }
645
+ this.landmarks ?? (this.landmarks = this.resources.guide);
646
+ const { metadata: i, rendition: p, media: d } = Kt(n);
647
+ this.rendition = p, this.media = d, this.dir = this.resources.pageProgressionDirection;
648
+ const h = ce(
649
+ await w(this, U, P).call(this, "META-INF/com.apple.ibooks.display-options.xml") ?? await w(this, U, P).call(this, "META-INF/com.kobobooks.display-options.xml")
650
+ );
651
+ h && (h.fixedLayout === "true" && ((l = this.rendition).layout ?? (l.layout = "pre-paginated")), h.openToSpread === "false" && ((f = this.sections.find((y) => y.linear !== "no")).pageSpread ?? (f.pageSpread = this.dir === "rtl" ? "left" : "right"))), this.parsedMetadata = i;
652
+ const g = (v = i == null ? void 0 : i.title) == null ? void 0 : v[0];
653
+ this.metadata = {
654
+ title: g == null ? void 0 : g.value,
655
+ subtitle: (x = (k = i == null ? void 0 : i.title) == null ? void 0 : k.find((y) => y.titleType === "subtitle")) == null ? void 0 : x.value,
656
+ sortAs: g == null ? void 0 : g.fileAs,
657
+ language: i == null ? void 0 : i.language,
658
+ identifier: Ft(n),
659
+ description: (j = i == null ? void 0 : i.description) == null ? void 0 : j.value,
660
+ publisher: (q = i == null ? void 0 : i.publisher) == null ? void 0 : q.value,
661
+ published: i == null ? void 0 : i.date,
662
+ modified: i == null ? void 0 : i.dctermsModified,
663
+ subject: (M = (_ = i == null ? void 0 : i.subject) == null ? void 0 : _.filter(({ value: y, term: E }) => y || E)) == null ? void 0 : M.map(({ value: y, term: E, authority: L }) => ({ name: y, code: E, scheme: L })),
664
+ rights: (gt = i == null ? void 0 : i.rights) == null ? void 0 : gt.value
665
+ };
666
+ const A = {
667
+ art: "artist",
668
+ aut: "author",
669
+ bkp: "producer",
670
+ clr: "colorist",
671
+ edt: "editor",
672
+ ill: "illustrator",
673
+ nrt: "narrator",
674
+ trl: "translator",
675
+ pbl: "publisher"
676
+ }, m = (y) => (E) => {
677
+ var bt;
678
+ const L = [...new Set((bt = E.role) == null ? void 0 : bt.map(({ value: R, scheme: vt }) => (!vt || vt === "marc:relators" ? A[R] : null) ?? y))], xt = { name: E.value, sortAs: E.fileAs };
679
+ return [L != null && L.length ? L : [y], xt];
680
+ };
681
+ return (wt = (at = (yt = i == null ? void 0 : i.creator) == null ? void 0 : yt.map(m("author"))) == null ? void 0 : at.concat((ot = (W = i == null ? void 0 : i.contributor) == null ? void 0 : W.map) == null ? void 0 : ot.call(W, m("contributor")))) == null || wt.forEach(([y, E]) => y.forEach((L) => {
682
+ this.metadata[L] ? this.metadata[L].push(E) : this.metadata[L] = [E];
683
+ })), this;
684
+ }
685
+ async loadDocument(t) {
686
+ const e = await this.loadText(t.href);
687
+ return this.parser.parseFromString(e, t.mediaType);
688
+ }
689
+ getMediaOverlay() {
690
+ return new Zt(this, w(this, U, P).bind(this));
691
+ }
692
+ resolveCFI(t) {
693
+ return this.resources.resolveCFI(t);
694
+ }
695
+ resolveHref(t) {
696
+ const [e, s] = t.split("#"), n = this.resources.getItemByHref(decodeURI(e));
697
+ return n ? { index: this.resources.spine.findIndex(({ idref: u }) => u === n.id), anchor: s ? (u) => oe(u, s) : () => 0 } : null;
698
+ }
699
+ splitTOCHref(t) {
700
+ return (t == null ? void 0 : t.split("#")) ?? [];
701
+ }
702
+ getTOCFragment(t, e) {
703
+ return t.getElementById(e) ?? t.querySelector(`[name="${CSS.escape(e)}"]`);
704
+ }
705
+ isExternal(t) {
706
+ return Ot(t);
707
+ }
708
+ async getCover() {
709
+ var e;
710
+ const t = (e = this.resources) == null ? void 0 : e.cover;
711
+ return t != null && t.href ? new Blob([await this.loadBlob(t.href)], { type: t.mediaType }) : null;
712
+ }
713
+ async getCalibreBookmarks() {
714
+ const t = await this.loadText("META-INF/calibre_bookmarks.txt"), e = "encoding=json+base64:";
715
+ if (t != null && t.startsWith(e)) {
716
+ const s = atob(t.slice(e.length));
717
+ return JSON.parse(s);
718
+ }
719
+ }
720
+ destroy() {
721
+ var t;
722
+ (t = o(this, Z)) == null || t.destroy();
723
+ }
724
+ }
725
+ Z = new WeakMap(), nt = new WeakMap(), U = new WeakSet(), P = async function(t) {
726
+ const e = await this.loadText(t);
727
+ if (!e)
728
+ return null;
729
+ const s = this.parser.parseFromString(e, N.XML);
730
+ if (s.querySelector("parsererror"))
731
+ throw new Error(`XML parsing error: ${t}
732
+ ${s.querySelector("parsererror").innerText}`);
733
+ return s;
734
+ };
735
+ export {
736
+ ue as EPUB
737
+ };