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