react-book-reader 1.0.1 → 1.1.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.
@@ -1,1036 +0,0 @@
1
- var Ft = Object.defineProperty;
2
- var _t = (r, e, t) => e in r ? Ft(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t;
3
- var q = (r, e, t) => (_t(r, typeof e != "symbol" ? e + "" : e, t), t), Rt = (r, e, t) => {
4
- if (!e.has(r))
5
- throw TypeError("Cannot " + t);
6
- };
7
- var c = (r, e, t) => (Rt(r, e, "read from private field"), t ? t.call(r) : e.get(r)), w = (r, e, t) => {
8
- if (e.has(r))
9
- throw TypeError("Cannot add the same private member more than once");
10
- e instanceof WeakSet ? e.add(r) : e.set(r, t);
11
- }, S = (r, e, t, s) => (Rt(r, e, "write to private field"), s ? s.call(r, t) : e.set(r, t), t);
12
- var Et = (r, e, t, s) => ({
13
- set _(n) {
14
- S(r, e, n, t);
15
- },
16
- get _() {
17
- return c(r, e, s);
18
- }
19
- }), V = (r, e, t) => (Rt(r, e, "access private method"), t);
20
- const M = (r) => {
21
- if (!r)
22
- return "";
23
- const e = document.createElement("textarea");
24
- return e.innerHTML = r, e.value;
25
- }, I = {
26
- XML: "application/xml",
27
- XHTML: "application/xhtml+xml",
28
- HTML: "text/html",
29
- CSS: "text/css",
30
- SVG: "image/svg+xml"
31
- }, Nt = {
32
- name: [0, 32, "string"],
33
- type: [60, 4, "string"],
34
- creator: [64, 4, "string"],
35
- numRecords: [76, 2, "uint"]
36
- }, zt = {
37
- compression: [0, 2, "uint"],
38
- numTextRecords: [8, 2, "uint"],
39
- recordSize: [10, 2, "uint"],
40
- encryption: [12, 2, "uint"]
41
- }, Xt = {
42
- magic: [16, 4, "string"],
43
- length: [20, 4, "uint"],
44
- type: [24, 4, "uint"],
45
- encoding: [28, 4, "uint"],
46
- uid: [32, 4, "uint"],
47
- version: [36, 4, "uint"],
48
- titleOffset: [84, 4, "uint"],
49
- titleLength: [88, 4, "uint"],
50
- localeRegion: [94, 1, "uint"],
51
- localeLanguage: [95, 1, "uint"],
52
- resourceStart: [108, 4, "uint"],
53
- huffcdic: [112, 4, "uint"],
54
- numHuffcdic: [116, 4, "uint"],
55
- exthFlag: [128, 4, "uint"],
56
- trailingFlags: [240, 4, "uint"],
57
- indx: [244, 4, "uint"]
58
- }, Pt = {
59
- resourceStart: [108, 4, "uint"],
60
- fdst: [192, 4, "uint"],
61
- numFdst: [196, 4, "uint"],
62
- frag: [248, 4, "uint"],
63
- skel: [252, 4, "uint"],
64
- guide: [260, 4, "uint"]
65
- }, Gt = {
66
- magic: [0, 4, "string"],
67
- length: [4, 4, "uint"],
68
- count: [8, 4, "uint"]
69
- }, vt = {
70
- magic: [0, 4, "string"],
71
- length: [4, 4, "uint"],
72
- type: [8, 4, "uint"],
73
- idxt: [20, 4, "uint"],
74
- numRecords: [24, 4, "uint"],
75
- encoding: [28, 4, "uint"],
76
- language: [32, 4, "uint"],
77
- total: [36, 4, "uint"],
78
- ordt: [40, 4, "uint"],
79
- ligt: [44, 4, "uint"],
80
- numLigt: [48, 4, "uint"],
81
- numCncx: [52, 4, "uint"]
82
- }, jt = {
83
- magic: [0, 4, "string"],
84
- length: [4, 4, "uint"],
85
- numControlBytes: [8, 4, "uint"]
86
- }, $t = {
87
- magic: [0, 4, "string"],
88
- offset1: [8, 4, "uint"],
89
- offset2: [12, 4, "uint"]
90
- }, qt = {
91
- magic: [0, 4, "string"],
92
- length: [4, 4, "uint"],
93
- numEntries: [8, 4, "uint"],
94
- codeLength: [12, 4, "uint"]
95
- }, Vt = {
96
- magic: [0, 4, "string"],
97
- numEntries: [8, 4, "uint"]
98
- }, Kt = {
99
- flags: [8, 4, "uint"],
100
- dataStart: [12, 4, "uint"],
101
- keyLength: [16, 4, "uint"],
102
- keyStart: [20, 4, "uint"]
103
- }, Yt = {
104
- 1252: "windows-1252",
105
- 65001: "utf-8"
106
- }, It = {
107
- 100: ["creator", "string", !0],
108
- 101: ["publisher"],
109
- 103: ["description"],
110
- 104: ["isbn"],
111
- 105: ["subject", "string", !0],
112
- 106: ["date"],
113
- 108: ["contributor", "string", !0],
114
- 109: ["rights"],
115
- 110: ["subjectCode", "string", !0],
116
- 112: ["source", "string", !0],
117
- 113: ["asin"],
118
- 121: ["boundary", "uint"],
119
- 122: ["fixedLayout"],
120
- 125: ["numResources", "uint"],
121
- 126: ["originalResolution"],
122
- 127: ["zeroGutter"],
123
- 128: ["zeroMargin"],
124
- 129: ["coverURI"],
125
- 132: ["regionMagnification"],
126
- 201: ["coverOffset", "uint"],
127
- 202: ["thumbnailOffset", "uint"],
128
- 503: ["title"],
129
- 524: ["language", "string", !0],
130
- 527: ["pageProgressionDirection"]
131
- }, Zt = {
132
- 1: [
133
- "ar",
134
- "ar-SA",
135
- "ar-IQ",
136
- "ar-EG",
137
- "ar-LY",
138
- "ar-DZ",
139
- "ar-MA",
140
- "ar-TN",
141
- "ar-OM",
142
- "ar-YE",
143
- "ar-SY",
144
- "ar-JO",
145
- "ar-LB",
146
- "ar-KW",
147
- "ar-AE",
148
- "ar-BH",
149
- "ar-QA"
150
- ],
151
- 2: ["bg"],
152
- 3: ["ca"],
153
- 4: ["zh", "zh-TW", "zh-CN", "zh-HK", "zh-SG"],
154
- 5: ["cs"],
155
- 6: ["da"],
156
- 7: ["de", "de-DE", "de-CH", "de-AT", "de-LU", "de-LI"],
157
- 8: ["el"],
158
- 9: [
159
- "en",
160
- "en-US",
161
- "en-GB",
162
- "en-AU",
163
- "en-CA",
164
- "en-NZ",
165
- "en-IE",
166
- "en-ZA",
167
- "en-JM",
168
- null,
169
- "en-BZ",
170
- "en-TT",
171
- "en-ZW",
172
- "en-PH"
173
- ],
174
- 10: [
175
- "es",
176
- "es-ES",
177
- "es-MX",
178
- null,
179
- "es-GT",
180
- "es-CR",
181
- "es-PA",
182
- "es-DO",
183
- "es-VE",
184
- "es-CO",
185
- "es-PE",
186
- "es-AR",
187
- "es-EC",
188
- "es-CL",
189
- "es-UY",
190
- "es-PY",
191
- "es-BO",
192
- "es-SV",
193
- "es-HN",
194
- "es-NI",
195
- "es-PR"
196
- ],
197
- 11: ["fi"],
198
- 12: ["fr", "fr-FR", "fr-BE", "fr-CA", "fr-CH", "fr-LU", "fr-MC"],
199
- 13: ["he"],
200
- 14: ["hu"],
201
- 15: ["is"],
202
- 16: ["it", "it-IT", "it-CH"],
203
- 17: ["ja"],
204
- 18: ["ko"],
205
- 19: ["nl", "nl-NL", "nl-BE"],
206
- 20: ["no", "nb", "nn"],
207
- 21: ["pl"],
208
- 22: ["pt", "pt-BR", "pt-PT"],
209
- 23: ["rm"],
210
- 24: ["ro"],
211
- 25: ["ru"],
212
- 26: ["hr", null, "sr"],
213
- 27: ["sk"],
214
- 28: ["sq"],
215
- 29: ["sv", "sv-SE", "sv-FI"],
216
- 30: ["th"],
217
- 31: ["tr"],
218
- 32: ["ur"],
219
- 33: ["id"],
220
- 34: ["uk"],
221
- 35: ["be"],
222
- 36: ["sl"],
223
- 37: ["et"],
224
- 38: ["lv"],
225
- 39: ["lt"],
226
- 41: ["fa"],
227
- 42: ["vi"],
228
- 43: ["hy"],
229
- 44: ["az"],
230
- 45: ["eu"],
231
- 46: ["hsb"],
232
- 47: ["mk"],
233
- 48: ["st"],
234
- 49: ["ts"],
235
- 50: ["tn"],
236
- 52: ["xh"],
237
- 53: ["zu"],
238
- 54: ["af"],
239
- 55: ["ka"],
240
- 56: ["fo"],
241
- 57: ["hi"],
242
- 58: ["mt"],
243
- 59: ["se"],
244
- 62: ["ms"],
245
- 63: ["kk"],
246
- 65: ["sw"],
247
- 67: ["uz", null, "uz-UZ"],
248
- 68: ["tt"],
249
- 69: ["bn"],
250
- 70: ["pa"],
251
- 71: ["gu"],
252
- 72: ["or"],
253
- 73: ["ta"],
254
- 74: ["te"],
255
- 75: ["kn"],
256
- 76: ["ml"],
257
- 77: ["as"],
258
- 78: ["mr"],
259
- 79: ["sa"],
260
- 82: ["cy", "cy-GB"],
261
- 83: ["gl", "gl-ES"],
262
- 87: ["kok"],
263
- 97: ["ne"],
264
- 98: ["fy"]
265
- }, mt = (r, e) => {
266
- const t = new r.constructor(r.length + e.length);
267
- return t.set(r), t.set(e, r.length), t;
268
- }, kt = (r, e, t) => {
269
- const s = new r.constructor(r.length + e.length + t.length);
270
- return s.set(r), s.set(e, r.length), s.set(t, r.length + e.length), s;
271
- }, Wt = new TextDecoder(), ft = (r) => Wt.decode(r), A = (r) => {
272
- if (!r)
273
- return;
274
- const e = r.byteLength, t = e === 4 ? "getUint32" : e === 2 ? "getUint16" : "getUint8";
275
- return new DataView(r)[t](0);
276
- }, T = (r, e) => Object.fromEntries(Array.from(Object.entries(r)).map(([t, [s, n, i]]) => [
277
- t,
278
- (i === "string" ? ft : A)(e.slice(s, s + n))
279
- ])), Tt = (r) => new TextDecoder(Yt[r]), dt = (r, e = 0) => {
280
- let t = 0, s = 0;
281
- for (const n of r.subarray(e, e + 4))
282
- if (t = t << 7 | (n & 127) >>> 0, s++, n & 128)
283
- break;
284
- return { value: t, length: s };
285
- }, Jt = (r) => {
286
- let e = 0;
287
- for (const t of r.subarray(-4))
288
- t & 128 && (e = 0), e = e << 7 | t & 127;
289
- return e;
290
- }, Ut = (r) => {
291
- let e = 0;
292
- for (; r > 0; r = r >> 1)
293
- (r & 1) === 1 && e++;
294
- return e;
295
- }, Qt = (r) => {
296
- let e = 0;
297
- for (; !(r & 1); )
298
- r = r >> 1, e++;
299
- return e;
300
- }, te = (r) => {
301
- let e = [];
302
- for (let t = 0; t < r.length; t++) {
303
- const s = r[t];
304
- if (s === 0)
305
- e.push(0);
306
- else if (s <= 8)
307
- for (const n of r.subarray(t + 1, (t += s) + 1))
308
- e.push(n);
309
- else if (s <= 127)
310
- e.push(s);
311
- else if (s <= 191) {
312
- const n = s << 8 | r[t++ + 1], i = (n & 16383) >>> 3, o = (n & 7) + 3;
313
- for (let a = 0; a < o; a++)
314
- e.push(e[e.length - i]);
315
- } else
316
- e.push(32, s ^ 128);
317
- }
318
- return Uint8Array.from(e);
319
- }, ee = (r, e) => {
320
- const t = e >> 3, s = e + 32, n = s >> 3;
321
- let i = 0n;
322
- for (let o = t; o <= n; o++)
323
- i = i << 8n | BigInt(r[o] ?? 0);
324
- return i >> 8n - BigInt(s & 7) & 0xffffffffn;
325
- }, se = async (r, e) => {
326
- const t = await e(r.huffcdic), { magic: s, offset1: n, offset2: i } = T($t, t);
327
- if (s !== "HUFF")
328
- throw new Error("Invalid HUFF record");
329
- const o = Array.from({ length: 256 }, (h, b) => n + b * 4).map((h) => A(t.slice(h, h + 4))).map((h) => [h & 128, h & 31, h >>> 8]), a = [null].concat(Array.from({ length: 32 }, (h, b) => i + b * 8).map((h) => [
330
- A(t.slice(h, h + 4)),
331
- A(t.slice(h + 4, h + 8))
332
- ])), u = [];
333
- for (let h = 1; h < r.numHuffcdic; h++) {
334
- const b = await e(r.huffcdic + h), p = T(qt, b);
335
- if (p.magic !== "CDIC")
336
- throw new Error("Invalid CDIC record");
337
- const l = Math.min(1 << p.codeLength, p.numEntries - u.length), f = b.slice(p.length);
338
- for (let d = 0; d < l; d++) {
339
- const m = A(f.slice(d * 2, d * 2 + 2)), y = A(f.slice(m, m + 2)), R = y & 32767, E = y & 32768, v = new Uint8Array(
340
- f.slice(m + 2, m + 2 + R)
341
- );
342
- u.push([v, E]);
343
- }
344
- }
345
- const g = (h) => {
346
- let b = new Uint8Array();
347
- const p = h.byteLength * 8;
348
- for (let l = 0; l < p; ) {
349
- const f = Number(ee(h, l));
350
- let [d, m, y] = o[f >>> 24];
351
- if (!d) {
352
- for (; f >>> 32 - m < a[m][0]; )
353
- m += 1;
354
- y = a[m][1];
355
- }
356
- if ((l += m) > p)
357
- break;
358
- const R = y - (f >>> 32 - m);
359
- let [E, v] = u[R];
360
- v || (E = g(E), u[R] = [E, !0]), b = mt(b, E);
361
- }
362
- return b;
363
- };
364
- return g;
365
- }, gt = async (r, e) => {
366
- const t = await e(r), s = T(vt, t);
367
- if (s.magic !== "INDX")
368
- throw new Error("Invalid INDX record");
369
- const n = Tt(s.encoding), i = t.slice(s.length), o = T(jt, i);
370
- if (o.magic !== "TAGX")
371
- throw new Error("Invalid TAGX section");
372
- const a = (o.length - 12) / 4, u = Array.from({ length: a }, (p, l) => new Uint8Array(i.slice(12 + l * 4, 12 + l * 4 + 4))), g = {};
373
- let h = 0;
374
- for (let p = 0; p < s.numCncx; p++) {
375
- const l = await e(r + s.numRecords + p + 1), f = new Uint8Array(l);
376
- for (let d = 0; d < f.byteLength; ) {
377
- const m = d, { value: y, length: R } = dt(f, d);
378
- d += R;
379
- const E = l.slice(d, d + y);
380
- d += y, g[h + m] = n.decode(E);
381
- }
382
- h += 65536;
383
- }
384
- const b = [];
385
- for (let p = 0; p < s.numRecords; p++) {
386
- const l = await e(r + 1 + p), f = new Uint8Array(l), d = T(vt, l);
387
- if (d.magic !== "INDX")
388
- throw new Error("Invalid INDX record");
389
- for (let m = 0; m < d.numRecords; m++) {
390
- const y = d.idxt + 4 + 2 * m, R = A(l.slice(y, y + 2)), E = A(l.slice(R, R + 1)), v = ft(l.slice(R + 1, R + 1 + E)), x = [], ht = R + 1 + E;
391
- let xt = 0, X = ht + o.numControlBytes;
392
- for (const [tt, P, G, yt] of u) {
393
- if (yt & 1) {
394
- xt++;
395
- continue;
396
- }
397
- const j = ht + xt, B = A(l.slice(j, j + 1)) & G;
398
- if (B === G)
399
- if (Ut(G) > 1) {
400
- const { value: et, length: $ } = dt(f, X);
401
- x.push([tt, null, et, P]), X += $;
402
- } else
403
- x.push([tt, 1, null, P]);
404
- else
405
- x.push([tt, B >> Qt(G), null, P]);
406
- }
407
- const Lt = {};
408
- for (const [tt, P, G, yt] of x) {
409
- const j = [];
410
- if (P != null)
411
- for (let B = 0; B < P * yt; B++) {
412
- const { value: et, length: $ } = dt(f, X);
413
- j.push(et), X += $;
414
- }
415
- else {
416
- let B = 0;
417
- for (; B < G; ) {
418
- const { value: et, length: $ } = dt(f, X);
419
- j.push(et), X += $, B += $;
420
- }
421
- }
422
- Lt[tt] = j;
423
- }
424
- b.push({ name: v, tagMap: Lt });
425
- }
426
- }
427
- return { table: b, cncx: g };
428
- }, ne = async (r, e) => {
429
- const { table: t, cncx: s } = await gt(r, e), n = t.map(({ tagMap: o }, a) => {
430
- var u, g, h, b, p, l;
431
- return {
432
- index: a,
433
- offset: (u = o[1]) == null ? void 0 : u[0],
434
- size: (g = o[2]) == null ? void 0 : g[0],
435
- label: s[o[3]] ?? "",
436
- headingLevel: (h = o[4]) == null ? void 0 : h[0],
437
- pos: o[6],
438
- parent: (b = o[21]) == null ? void 0 : b[0],
439
- firstChild: (p = o[22]) == null ? void 0 : p[0],
440
- lastChild: (l = o[23]) == null ? void 0 : l[0]
441
- };
442
- }), i = (o) => (o.firstChild == null || (o.children = n.filter((a) => a.parent === o.index).map(i)), o);
443
- return n.filter((o) => o.headingLevel === 0).map(i);
444
- }, re = (r, e) => {
445
- const { magic: t, count: s } = T(Gt, r);
446
- if (t !== "EXTH")
447
- throw new Error("Invalid EXTH header");
448
- const n = Tt(e), i = {};
449
- let o = 12;
450
- for (let a = 0; a < s; a++) {
451
- const u = A(r.slice(o, o + 4)), g = A(r.slice(o + 4, o + 8));
452
- if (u in It) {
453
- const [h, b, p] = It[u], l = r.slice(o + 8, o + g), f = b === "uint" ? A(l) : n.decode(l);
454
- p ? (i[h] ?? (i[h] = []), i[h].push(f)) : i[h] = f;
455
- }
456
- o += g;
457
- }
458
- return i;
459
- }, ie = async (r, e) => {
460
- const { flags: t, dataStart: s, keyLength: n, keyStart: i } = T(Kt, r), o = new Uint8Array(r.slice(s));
461
- if (t & 2) {
462
- const u = n === 16 ? 1024 : 1040, g = new Uint8Array(r.slice(i, i + n)), h = Math.min(u, o.length);
463
- for (var a = 0; a < h; a++)
464
- o[a] = o[a] ^ g[a % g.length];
465
- }
466
- if (t & 1)
467
- try {
468
- return await e(o);
469
- } catch (u) {
470
- console.warn(u), console.warn("Failed to decompress font");
471
- }
472
- return o;
473
- }, pe = async (r) => ft(await r.slice(60, 68).arrayBuffer()) === "BOOKMOBI";
474
- var K, Y;
475
- class oe {
476
- constructor() {
477
- w(this, K, void 0);
478
- w(this, Y, void 0);
479
- q(this, "pdb");
480
- }
481
- async open(e) {
482
- S(this, K, e);
483
- const t = T(Nt, await e.slice(0, 78).arrayBuffer());
484
- this.pdb = t;
485
- const s = await e.slice(78, 78 + t.numRecords * 8).arrayBuffer();
486
- S(this, Y, Array.from(
487
- { length: t.numRecords },
488
- (n, i) => A(s.slice(i * 8, i * 8 + 4))
489
- ).map((n, i, o) => [n, o[i + 1]]));
490
- }
491
- loadRecord(e) {
492
- const t = c(this, Y)[e];
493
- if (!t)
494
- throw new RangeError("Record index out of bounds");
495
- return c(this, K).slice(...t).arrayBuffer();
496
- }
497
- async loadMagic(e) {
498
- const t = c(this, Y)[e][0];
499
- return ft(await c(this, K).slice(t, t + 4).arrayBuffer());
500
- }
501
- }
502
- K = new WeakMap(), Y = new WeakMap();
503
- var Z, st, nt, rt, W, it, ot, St, pt, Mt;
504
- class be extends oe {
505
- constructor({ unzlib: t }) {
506
- super();
507
- w(this, ot);
508
- w(this, pt);
509
- w(this, Z, 0);
510
- w(this, st, void 0);
511
- w(this, nt, void 0);
512
- w(this, rt, void 0);
513
- w(this, W, void 0);
514
- w(this, it, void 0);
515
- this.unzlib = t;
516
- }
517
- async open(t) {
518
- var n;
519
- await super.open(t), this.headers = V(this, ot, St).call(this, await super.loadRecord(0)), S(this, st, this.headers.mobi.resourceStart);
520
- let s = this.headers.mobi.version >= 8;
521
- if (!s) {
522
- const i = (n = this.headers.exth) == null ? void 0 : n.boundary;
523
- if (i < 4294967295)
524
- try {
525
- this.headers = V(this, ot, St).call(this, await super.loadRecord(i)), S(this, Z, i), s = !0;
526
- } catch (o) {
527
- console.warn(o), console.warn("Failed to open KF8; falling back to MOBI");
528
- }
529
- }
530
- return await V(this, pt, Mt).call(this), s ? new ge(this).init() : new le(this).init();
531
- }
532
- decode(...t) {
533
- return c(this, nt).decode(...t);
534
- }
535
- encode(...t) {
536
- return c(this, rt).encode(...t);
537
- }
538
- loadRecord(t) {
539
- return super.loadRecord(c(this, Z) + t);
540
- }
541
- loadMagic(t) {
542
- return super.loadMagic(c(this, Z) + t);
543
- }
544
- loadText(t) {
545
- return this.loadRecord(t + 1).then((s) => new Uint8Array(s)).then(c(this, it)).then(c(this, W));
546
- }
547
- async loadResource(t) {
548
- const s = await super.loadRecord(c(this, st) + t), n = ft(s.slice(0, 4));
549
- return n === "FONT" ? ie(s, this.unzlib) : n === "VIDE" || n === "AUDI" ? s.slice(12) : s;
550
- }
551
- getNCX() {
552
- const t = this.headers.mobi.indx;
553
- if (t < 4294967295)
554
- return ne(t, this.loadRecord.bind(this));
555
- }
556
- getMetadata() {
557
- var n, i;
558
- const { mobi: t, exth: s } = this.headers;
559
- return {
560
- identifier: t.uid.toString(),
561
- title: M((s == null ? void 0 : s.title) || this.decode(t.title)),
562
- author: (n = s == null ? void 0 : s.creator) == null ? void 0 : n.map(M),
563
- publisher: M(s == null ? void 0 : s.publisher),
564
- language: (s == null ? void 0 : s.language) ?? t.language,
565
- published: s == null ? void 0 : s.date,
566
- description: M(s == null ? void 0 : s.description),
567
- subject: (i = s == null ? void 0 : s.subject) == null ? void 0 : i.map(M),
568
- rights: M(s == null ? void 0 : s.rights)
569
- };
570
- }
571
- async getCover() {
572
- const { exth: t } = this.headers, s = (t == null ? void 0 : t.coverOffset) < 4294967295 ? t == null ? void 0 : t.coverOffset : (t == null ? void 0 : t.thumbnailOffset) < 4294967295 ? t == null ? void 0 : t.thumbnailOffset : null;
573
- if (s != null) {
574
- const n = await this.loadResource(s);
575
- return new Blob([n]);
576
- }
577
- }
578
- }
579
- Z = new WeakMap(), st = new WeakMap(), nt = new WeakMap(), rt = new WeakMap(), W = new WeakMap(), it = new WeakMap(), ot = new WeakSet(), St = function(t) {
580
- const s = T(zt, t), n = T(Xt, t);
581
- if (n.magic !== "MOBI")
582
- throw new Error("Missing MOBI header");
583
- const { titleOffset: i, titleLength: o, localeLanguage: a, localeRegion: u } = n;
584
- n.title = t.slice(i, i + o);
585
- const g = Zt[a];
586
- n.language = (g == null ? void 0 : g[u >> 2]) ?? (g == null ? void 0 : g[0]);
587
- const h = n.exthFlag & 64 ? re(t.slice(n.length + 16), n.encoding) : null, b = n.version >= 8 ? T(Pt, t) : null;
588
- return { palmdoc: s, mobi: n, exth: h, kf8: b };
589
- }, pt = new WeakSet(), Mt = async function() {
590
- const { palmdoc: t, mobi: s } = this.headers;
591
- S(this, nt, Tt(s.encoding)), S(this, rt, new TextEncoder());
592
- const { compression: n } = t;
593
- if (S(this, W, n === 1 ? (u) => u : n === 2 ? te : n === 17480 ? await se(s, this.loadRecord.bind(this)) : null), !c(this, W))
594
- throw new Error("Unknown compression type");
595
- const { trailingFlags: i } = s, o = i & 1, a = Ut(i >>> 1);
596
- S(this, it, (u) => {
597
- for (let g = 0; g < a; g++) {
598
- const h = Jt(u);
599
- u = u.subarray(0, -h);
600
- }
601
- if (o) {
602
- const g = (u[u.length - 1] & 3) + 1;
603
- u = u.subarray(0, -g);
604
- }
605
- return u;
606
- });
607
- };
608
- const Ct = /<\s*(?:mbp:)?pagebreak[^>]*>/gi, ce = /<[^<>]+filepos=['"]{0,1}(\d+)[^<>]*>/gi, ae = (r) => {
609
- let e = 0;
610
- for (; r; ) {
611
- const t = r.parentElement;
612
- if (t) {
613
- const s = t.tagName.toLowerCase();
614
- s === "p" ? e += 1.5 : s === "blockquote" && (e += 2);
615
- }
616
- r = t;
617
- }
618
- return e;
619
- };
620
- var H, J, F, D, ct, at;
621
- class le {
622
- constructor(e) {
623
- q(this, "parser", new DOMParser());
624
- q(this, "serializer", new XMLSerializer());
625
- w(this, H, /* @__PURE__ */ new Map());
626
- w(this, J, /* @__PURE__ */ new Map());
627
- w(this, F, /* @__PURE__ */ new Map());
628
- w(this, D, void 0);
629
- w(this, ct, []);
630
- w(this, at, I.HTML);
631
- this.mobi = e;
632
- }
633
- async init() {
634
- var s;
635
- let e = new Uint8Array();
636
- for (let n = 0; n < this.mobi.headers.palmdoc.numTextRecords; n++)
637
- e = mt(e, await this.mobi.loadText(n));
638
- const t = Array.from(
639
- new Uint8Array(e),
640
- (n) => String.fromCharCode(n)
641
- ).join("");
642
- S(this, D, [0].concat(Array.from(t.matchAll(Ct), (n) => n.index)).map((n, i, o) => t.slice(n, o[i + 1])).map((n) => Uint8Array.from(n, (i) => i.charCodeAt(0))).map((n) => ({ book: this, raw: n })).reduce((n, i) => {
643
- const o = n[n.length - 1];
644
- return i.start = (o == null ? void 0 : o.end) ?? 0, i.end = i.start + i.raw.byteLength, n.concat(i);
645
- }, [])), this.sections = c(this, D).map((n, i) => ({
646
- id: i,
647
- load: () => this.loadSection(n),
648
- createDocument: () => this.createDocument(n),
649
- size: n.end - n.start
650
- }));
651
- try {
652
- this.landmarks = await this.getGuide();
653
- const n = (s = this.landmarks.find(({ type: i }) => i == null ? void 0 : i.includes("toc"))) == null ? void 0 : s.href;
654
- if (n) {
655
- const { index: i } = this.resolveHref(n), o = await this.sections[i].createDocument();
656
- let a, u = 0, g = 0;
657
- const h = /* @__PURE__ */ new Map(), b = /* @__PURE__ */ new Map();
658
- this.toc = Array.from(o.querySelectorAll("a[filepos]")).reduce((p, l) => {
659
- var y;
660
- const f = ae(l), d = {
661
- label: (y = l.innerText) == null ? void 0 : y.trim(),
662
- href: `filepos:${l.getAttribute("filepos")}`
663
- }, m = f > g ? u + 1 : f === g ? u : h.get(f) ?? Math.max(0, u - 1);
664
- if (m > u)
665
- a ? (a.subitems ?? (a.subitems = []), a.subitems.push(d), b.set(m, a)) : p.push(d);
666
- else {
667
- const R = b.get(m);
668
- R ? R.subitems.push(d) : p.push(d);
669
- }
670
- return a = d, u = m, g = f, h.set(f, m), p;
671
- }, []);
672
- }
673
- } catch (n) {
674
- console.warn(n);
675
- }
676
- return S(this, ct, [...new Set(
677
- Array.from(t.matchAll(ce), (n) => n[1])
678
- )].map((n) => ({ filepos: n, number: Number(n) })).sort((n, i) => n.number - i.number)), this.metadata = this.mobi.getMetadata(), this.getCover = this.mobi.getCover.bind(this.mobi), this;
679
- }
680
- async getGuide() {
681
- const e = await this.createDocument(c(this, D)[0]);
682
- return Array.from(e.getElementsByTagName("reference"), (t) => {
683
- var s;
684
- return {
685
- label: t.getAttribute("title"),
686
- type: (s = t.getAttribute("type")) == null ? void 0 : s.split(/\s/),
687
- href: `filepos:${t.getAttribute("filepos")}`
688
- };
689
- });
690
- }
691
- async loadResource(e) {
692
- if (c(this, H).has(e))
693
- return c(this, H).get(e);
694
- const t = await this.mobi.loadResource(e), s = URL.createObjectURL(new Blob([t]));
695
- return c(this, H).set(e, s), s;
696
- }
697
- async loadRecindex(e) {
698
- return this.loadResource(Number(e) - 1);
699
- }
700
- async replaceResources(e) {
701
- for (const t of e.querySelectorAll("img[recindex]")) {
702
- const s = t.getAttribute("recindex");
703
- try {
704
- t.src = await this.loadRecindex(s);
705
- } catch {
706
- console.warn(`Failed to load image ${s}`);
707
- }
708
- }
709
- for (const t of e.querySelectorAll("[mediarecindex]")) {
710
- const s = t.getAttribute("mediarecindex"), n = t.getAttribute("recindex");
711
- try {
712
- t.src = await this.loadRecindex(s), n && (t.poster = await this.loadRecindex(n));
713
- } catch {
714
- console.warn(`Failed to load media ${s}`);
715
- }
716
- }
717
- for (const t of e.querySelectorAll("[filepos]")) {
718
- const s = t.getAttribute("filepos");
719
- t.href = `filepos:${s}`;
720
- }
721
- }
722
- async loadText(e) {
723
- if (c(this, J).has(e))
724
- return c(this, J).get(e);
725
- const { raw: t } = e, s = c(this, ct).filter(({ number: o }) => o >= e.start && o < e.end).map((o) => ({ ...o, offset: o.number - e.start }));
726
- let n = t;
727
- s.length && (n = t.subarray(0, s[0].offset), s.forEach(({ filepos: o, offset: a }, u) => {
728
- const g = s[u + 1], h = this.mobi.encode(`<a id="filepos${o}"></a>`);
729
- n = kt(n, h, t.subarray(a, g == null ? void 0 : g.offset));
730
- }));
731
- const i = this.mobi.decode(n).replaceAll(Ct, "");
732
- return c(this, J).set(e, i), i;
733
- }
734
- async createDocument(e) {
735
- const t = await this.loadText(e);
736
- return this.parser.parseFromString(t, c(this, at));
737
- }
738
- async loadSection(e) {
739
- if (c(this, F).has(e))
740
- return c(this, F).get(e);
741
- const t = await this.createDocument(e), s = t.createElement("style");
742
- t.head.append(s), s.append(t.createTextNode(`blockquote {
743
- margin-block-start: 0;
744
- margin-block-end: 0;
745
- margin-inline-start: 1em;
746
- margin-inline-end: 0;
747
- }`)), await this.replaceResources(t);
748
- const n = this.serializer.serializeToString(t), i = URL.createObjectURL(new Blob([n], { type: c(this, at) }));
749
- return c(this, F).set(e, i), i;
750
- }
751
- resolveHref(e) {
752
- const t = e.match(/filepos:(.*)/)[1], s = Number(t);
753
- return { index: c(this, D).findIndex((o) => o.end > s), anchor: (o) => o.getElementById(`filepos${t}`) };
754
- }
755
- splitTOCHref(e) {
756
- const t = e.match(/filepos:(.*)/)[1], s = Number(t);
757
- return [c(this, D).findIndex((i) => i.end > s), `filepos${t}`];
758
- }
759
- getTOCFragment(e, t) {
760
- return e.getElementById(t);
761
- }
762
- isExternal(e) {
763
- return /^(?!blob|filepos)\w+:/i.test(e);
764
- }
765
- destroy() {
766
- for (const e of c(this, H).values())
767
- URL.revokeObjectURL(e);
768
- for (const e of c(this, F).values())
769
- URL.revokeObjectURL(e);
770
- }
771
- }
772
- H = new WeakMap(), J = new WeakMap(), F = new WeakMap(), D = new WeakMap(), ct = new WeakMap(), at = new WeakMap();
773
- const Ht = /kindle:(flow|embed):(\w+)(?:\?mime=(\w+\/[-+.\w]+))?/, ue = /kindle:pos:fid:(\w+):off:(\w+)/, fe = (r) => {
774
- const [e, t, s] = r.match(Ht).slice(1);
775
- return { resourceType: e, id: parseInt(t, 32), type: s };
776
- }, Bt = (r) => {
777
- const [e, t] = r.match(ue).slice(1);
778
- return { fid: parseInt(e, 32), off: parseInt(t, 32) };
779
- }, Dt = (r = 0, e = 0) => `kindle:pos:fid:${r.toString(32).toUpperCase().padStart(4, "0")}:off:${e.toString(32).toUpperCase().padStart(10, "0")}`, Ot = (r) => {
780
- const e = r.match(/\s(id|name|aid)\s*=\s*['"]([^'"]*)['"]/i);
781
- if (!e)
782
- return;
783
- const [, t, s] = e;
784
- return `[${t}="${CSS.escape(s)}"]`;
785
- }, he = async (r, e, t) => {
786
- const s = [];
787
- r.replace(e, (...i) => (s.push(i), null));
788
- const n = [];
789
- for (const i of s)
790
- n.push(await t(...i));
791
- return r.replace(e, () => n.shift());
792
- }, de = (r) => {
793
- for (const e of r) {
794
- if (e === "page-spread-left" || e === "rendition:page-spread-left")
795
- return "left";
796
- if (e === "page-spread-right" || e === "rendition:page-spread-right")
797
- return "right";
798
- if (e === "rendition:page-spread-center")
799
- return "center";
800
- }
801
- };
802
- var L, Q, _, N, z, O, k, C, bt, wt, U, lt, ut, At;
803
- class ge {
804
- constructor(e) {
805
- w(this, ut);
806
- q(this, "parser", new DOMParser());
807
- q(this, "serializer", new XMLSerializer());
808
- w(this, L, /* @__PURE__ */ new Map());
809
- w(this, Q, /* @__PURE__ */ new Map());
810
- w(this, _, /* @__PURE__ */ new Map());
811
- w(this, N, {});
812
- w(this, z, void 0);
813
- w(this, O, void 0);
814
- w(this, k, new Uint8Array());
815
- w(this, C, new Uint8Array());
816
- w(this, bt, -1);
817
- w(this, wt, -1);
818
- w(this, U, I.XHTML);
819
- w(this, lt, /* @__PURE__ */ new Map());
820
- this.mobi = e;
821
- }
822
- async init() {
823
- var g, h, b, p;
824
- const e = this.mobi.loadRecord.bind(this.mobi), { kf8: t } = this.mobi.headers;
825
- try {
826
- const l = await e(t.fdst), f = T(Vt, l);
827
- if (f.magic !== "FDST")
828
- throw new Error("Missing FDST record");
829
- const d = Array.from(
830
- { length: f.numEntries },
831
- (m, y) => 12 + y * 8
832
- ).map((m) => [
833
- A(l.slice(m, m + 4)),
834
- A(l.slice(m + 4, m + 8))
835
- ]);
836
- c(this, N).fdstTable = d, S(this, O, d[d.length - 1][1]);
837
- } catch {
838
- }
839
- const s = (await gt(t.skel, e)).table.map(({ name: l, tagMap: f }, d) => ({
840
- index: d,
841
- name: l,
842
- numFrag: f[1][0],
843
- offset: f[6][0],
844
- length: f[6][1]
845
- })), n = await gt(t.frag, e), i = n.table.map(({ name: l, tagMap: f }) => ({
846
- insertOffset: parseInt(l),
847
- selector: n.cncx[f[2][0]],
848
- index: f[4][0],
849
- offset: f[6][0],
850
- length: f[6][1]
851
- }));
852
- c(this, N).skelTable = s, c(this, N).fragTable = i, S(this, z, s.reduce((l, f) => {
853
- const d = l[l.length - 1], m = (d == null ? void 0 : d.fragEnd) ?? 0, y = m + f.numFrag, R = i.slice(m, y), E = f.length + R.map((x) => x.length).reduce((x, ht) => x + ht), v = ((d == null ? void 0 : d.totalLength) ?? 0) + E;
854
- return l.concat({ skel: f, frags: R, fragEnd: y, length: E, totalLength: v });
855
- }, []));
856
- const o = await this.getResourcesByMagic(["RESC", "PAGE"]), a = /* @__PURE__ */ new Map();
857
- if (o.RESC) {
858
- const l = await this.mobi.loadRecord(o.RESC), f = this.mobi.decode(l.slice(16)).replace(/\0/g, ""), d = f.search(/\?>/), m = `<package>${f.slice(d)}</package>`, y = this.parser.parseFromString(m, I.XML);
859
- for (const R of y.querySelectorAll("spine > itemref")) {
860
- const E = parseInt(R.getAttribute("skelid"));
861
- a.set(E, de(
862
- ((g = R.getAttribute("properties")) == null ? void 0 : g.split(" ")) ?? []
863
- ));
864
- }
865
- }
866
- this.sections = c(this, z).map((l, f) => l.frags.length ? {
867
- id: f,
868
- load: () => this.loadSection(l),
869
- createDocument: () => this.createDocument(l),
870
- size: l.length,
871
- pageSpread: a.get(f)
872
- } : { linear: "no" });
873
- try {
874
- const l = await this.mobi.getNCX(), f = ({ label: d, pos: m, children: y }) => {
875
- const [R, E] = m, v = Dt(R, E), x = c(this, Q).get(R);
876
- return x ? x.push(E) : c(this, Q).set(R, [E]), { label: M(d), href: v, subitems: y == null ? void 0 : y.map(f) };
877
- };
878
- this.toc = l == null ? void 0 : l.map(f), this.landmarks = await this.getGuide();
879
- } catch (l) {
880
- console.warn(l);
881
- }
882
- const { exth: u } = this.mobi.headers;
883
- return this.dir = u.pageProgressionDirection, this.rendition = {
884
- layout: u.fixedLayout === "true" ? "pre-paginated" : "reflowable",
885
- viewport: Object.fromEntries(((p = (b = (h = u.originalResolution) == null ? void 0 : h.split("x")) == null ? void 0 : b.slice(0, 2)) == null ? void 0 : p.map((l, f) => [f ? "height" : "width", l])) ?? [])
886
- }, this.metadata = this.mobi.getMetadata(), this.getCover = this.mobi.getCover.bind(this.mobi), this;
887
- }
888
- // is this really the only way of getting to RESC, PAGE, etc.?
889
- async getResourcesByMagic(e) {
890
- const t = {}, s = this.mobi.headers.kf8.resourceStart, n = this.mobi.pdb.numRecords;
891
- for (let i = s; i < n; i++)
892
- try {
893
- const o = await this.mobi.loadMagic(i), a = e.find((u) => u === o);
894
- a && (t[a] = i);
895
- } catch {
896
- }
897
- return t;
898
- }
899
- async getGuide() {
900
- const e = this.mobi.headers.kf8.guide;
901
- if (e < 4294967295) {
902
- const t = this.mobi.loadRecord.bind(this.mobi), { table: s, cncx: n } = await gt(e, t);
903
- return s.map(({ name: i, tagMap: o }) => {
904
- var a, u;
905
- return {
906
- label: n[o[1][0]] ?? "",
907
- type: i == null ? void 0 : i.split(/\s/),
908
- href: Dt(((a = o[6]) == null ? void 0 : a[0]) ?? ((u = o[3]) == null ? void 0 : u[0]))
909
- };
910
- });
911
- }
912
- }
913
- async loadResourceBlob(e) {
914
- var u;
915
- const { resourceType: t, id: s, type: n } = fe(e), i = t === "flow" ? await this.loadFlow(s) : await this.mobi.loadResource(s - 1), o = [I.XHTML, I.HTML, I.CSS, I.SVG].includes(n) ? await this.replaceResources(this.mobi.decode(i)) : i, a = n === I.SVG ? this.parser.parseFromString(o, n) : null;
916
- return [
917
- new Blob([o], { type: n }),
918
- // SVG wrappers need to be inlined
919
- // as browsers don't allow external resources when loading SVG as an image
920
- (u = a == null ? void 0 : a.getElementsByTagNameNS("http://www.w3.org/2000/svg", "image")) != null && u.length ? a.documentElement : null
921
- ];
922
- }
923
- async loadResource(e) {
924
- if (c(this, L).has(e))
925
- return c(this, L).get(e);
926
- const [t, s] = await this.loadResourceBlob(e), n = s ? e : URL.createObjectURL(t);
927
- return s && c(this, lt).set(n, s), c(this, L).set(e, n), n;
928
- }
929
- replaceResources(e) {
930
- const t = new RegExp(Ht, "g");
931
- return he(e, t, this.loadResource.bind(this));
932
- }
933
- // NOTE: there doesn't seem to be a way to access text randomly?
934
- // how to know the decompressed size of the records without decompressing?
935
- // 4096 is just the maximum size
936
- async loadRaw(e, t) {
937
- const s = t - c(this, k).length, n = c(this, O) == null ? 1 / 0 : c(this, O) - c(this, C).length - e;
938
- if (s < 0 || s < n) {
939
- for (; c(this, k).length < t; ) {
940
- const o = ++Et(this, bt)._, a = await this.mobi.loadText(o);
941
- S(this, k, mt(c(this, k), a));
942
- }
943
- return c(this, k).slice(e, t);
944
- }
945
- for (; c(this, O) - c(this, C).length > e; ) {
946
- const o = this.mobi.headers.palmdoc.numTextRecords - 1 - ++Et(this, wt)._, a = await this.mobi.loadText(o);
947
- S(this, C, mt(a, c(this, C)));
948
- }
949
- const i = c(this, O) - c(this, C).length;
950
- return c(this, C).slice(e - i, t - i);
951
- }
952
- loadFlow(e) {
953
- if (e < 4294967295)
954
- return this.loadRaw(...c(this, N).fdstTable[e]);
955
- }
956
- async loadText(e) {
957
- const { skel: t, frags: s, length: n } = e, i = await this.loadRaw(t.offset, t.offset + n);
958
- let o = i.slice(0, t.length);
959
- for (const a of s) {
960
- const u = a.insertOffset - t.offset, g = t.length + a.offset, h = i.slice(g, g + a.length);
961
- o = kt(
962
- o.slice(0, u),
963
- h,
964
- o.slice(u)
965
- );
966
- const b = c(this, Q).get(a.index);
967
- if (b)
968
- for (const p of b) {
969
- const l = this.mobi.decode(h).slice(p), f = Ot(l);
970
- V(this, ut, At).call(this, a.index, p, f);
971
- }
972
- }
973
- return this.mobi.decode(o);
974
- }
975
- async createDocument(e) {
976
- const t = await this.loadText(e);
977
- return this.parser.parseFromString(t, c(this, U));
978
- }
979
- async loadSection(e) {
980
- if (c(this, L).has(e))
981
- return c(this, L).get(e);
982
- const t = await this.loadText(e), s = await this.replaceResources(t);
983
- let n = this.parser.parseFromString(s, c(this, U));
984
- n.querySelector("parsererror") && (S(this, U, I.HTML), n = this.parser.parseFromString(s, c(this, U)));
985
- for (const [o, a] of c(this, lt))
986
- for (const u of n.querySelectorAll(`img[src="${o}"]`))
987
- u.replaceWith(a);
988
- const i = URL.createObjectURL(
989
- new Blob([this.serializer.serializeToString(n)], { type: c(this, U) })
990
- );
991
- return c(this, L).set(e, i), i;
992
- }
993
- getIndexByFID(e) {
994
- return c(this, z).findIndex((t) => t.frags.some((s) => s.index === e));
995
- }
996
- async resolveHref(e) {
997
- var f;
998
- const { fid: t, off: s } = Bt(e), n = this.getIndexByFID(t);
999
- if (n < 0)
1000
- return;
1001
- const i = (f = c(this, _).get(t)) == null ? void 0 : f.get(s);
1002
- if (i)
1003
- return { index: n, anchor: (d) => d.querySelector(i) };
1004
- const { skel: o, frags: a } = c(this, z)[n], u = a.find((d) => d.index === t), g = o.offset + o.length + u.offset, h = await this.loadRaw(g, g + u.length), b = this.mobi.decode(h).slice(s), p = Ot(b);
1005
- return V(this, ut, At).call(this, t, s, p), { index: n, anchor: (d) => d.querySelector(p) };
1006
- }
1007
- splitTOCHref(e) {
1008
- const t = Bt(e);
1009
- return [this.getIndexByFID(t.fid), t];
1010
- }
1011
- getTOCFragment(e, { fid: t, off: s }) {
1012
- var i;
1013
- const n = (i = c(this, _).get(t)) == null ? void 0 : i.get(s);
1014
- return e.querySelector(n);
1015
- }
1016
- isExternal(e) {
1017
- return /^(?!blob|kindle)\w+:/i.test(e);
1018
- }
1019
- destroy() {
1020
- for (const e of c(this, L).values())
1021
- URL.revokeObjectURL(e);
1022
- }
1023
- }
1024
- L = new WeakMap(), Q = new WeakMap(), _ = new WeakMap(), N = new WeakMap(), z = new WeakMap(), O = new WeakMap(), k = new WeakMap(), C = new WeakMap(), bt = new WeakMap(), wt = new WeakMap(), U = new WeakMap(), lt = new WeakMap(), ut = new WeakSet(), At = function(e, t, s) {
1025
- const n = c(this, _).get(e);
1026
- if (n)
1027
- n.set(t, s);
1028
- else {
1029
- const i = /* @__PURE__ */ new Map();
1030
- c(this, _).set(e, i), i.set(t, s);
1031
- }
1032
- };
1033
- export {
1034
- be as MOBI,
1035
- pe as isMOBI
1036
- };