@prose-reader/streamer 1.117.0 → 1.118.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,277 +1,277 @@
1
- import x, { XmlDocument as ce } from "xmldoc";
2
- import { urlJoin as T, isXmlBasedMimeType as de, detectMimeTypeFromName as X } from "@prose-reader/shared";
3
- import { Subject as P, mergeMap as C, EMPTY as U, from as N, map as $, catchError as W, shareReplay as H, switchMap as R, startWith as le, distinctUntilChanged as pe, withLatestFrom as fe, NEVER as me, timer as ue, of as I, tap as _, merge as q, takeUntil as he, BehaviorSubject as ge, filter as be, ignoreElements as ye, first as we, finalize as G, lastValueFrom as J } from "rxjs";
4
- let S = !1;
5
- const w = {
1
+ import F, { XmlDocument as de } from "xmldoc";
2
+ import { urlJoin as W, isXmlBasedMimeType as le, detectMimeTypeFromName as V } from "@prose-reader/shared";
3
+ import { Subject as R, mergeMap as O, EMPTY as _, pairwise as pe, filter as j, startWith as fe, from as L, tap as D, catchError as P, switchMap as S, merge as q, takeUntil as ue, map as A, ignoreElements as me, first as he, BehaviorSubject as ge, distinctUntilChanged as ye, shareReplay as be, NEVER as G, timer as we, finalize as K, of as J, lastValueFrom as Y } from "rxjs";
4
+ let k = !1;
5
+ const v = {
6
6
  enable: (e) => {
7
- S = e;
7
+ k = e;
8
8
  },
9
9
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
10
10
  log: (...e) => {
11
- S && console.log("[prose-reader-streamer]", ...e);
11
+ k && console.log("[prose-reader-streamer]", ...e);
12
12
  },
13
13
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
14
  warn: (...e) => {
15
- S && console.warn("[prose-reader-streamer]", ...e);
15
+ k && console.warn("[prose-reader-streamer]", ...e);
16
16
  },
17
17
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
18
18
  error: (...e) => {
19
19
  console.error(...e);
20
20
  },
21
21
  time: (e) => {
22
- S && console.time(`[prose-reader-streamer] [metric] ${e}`);
22
+ k && console.time(`[prose-reader-streamer] [metric] ${e}`);
23
23
  },
24
24
  timeEnd: (e) => {
25
- S && console.timeEnd(`[prose-reader-streamer] [metric] ${e}`);
25
+ k && console.timeEnd(`[prose-reader-streamer] [metric] ${e}`);
26
26
  },
27
- metric: (e, n = 1 / 0) => {
28
- const a = typeof e == "number" ? e : e.duration;
29
- S && (e.duration <= n ? console.log(
27
+ metric: (e, t = 1 / 0) => {
28
+ const i = typeof e == "number" ? e : e.duration;
29
+ k && (e.duration <= t ? console.log(
30
30
  "[prose-reader-streamer] [metric] ",
31
- `${e.name} took ${a}ms`
31
+ `${e.name} took ${i}ms`
32
32
  ) : console.warn(
33
33
  "[prose-reader-streamer] [metric] ",
34
- `${e.name} took ${e.duration}ms which is above the ${n}ms target for this function`
34
+ `${e.name} took ${e.duration}ms which is above the ${t}ms target for this function`
35
35
  ));
36
36
  },
37
37
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
38
- measurePerformance: (e, n = 10, a) => (...r) => {
39
- const t = performance.now(), s = a(...r);
38
+ measurePerformance: (e, t = 10, i) => (...r) => {
39
+ const n = performance.now(), s = i(...r);
40
40
  if (s && s.then)
41
41
  return s.then((o) => {
42
- const l = performance.now();
43
- return w.metric({ name: e, duration: l - t }, n), o;
42
+ const d = performance.now();
43
+ return v.metric({ name: e, duration: d - n }, t), o;
44
44
  });
45
- const i = performance.now();
46
- return w.metric({ name: e, duration: i - t }, n), s;
45
+ const a = performance.now();
46
+ return v.metric({ name: e, duration: a - n }, t), s;
47
47
  }
48
48
  }, ve = (e) => {
49
- var a;
50
- const n = (a = e.descendantWithPath("head")) == null ? void 0 : a.childrenNamed("meta").find((r) => r.attr.name === "calibre:cover");
51
- return !!(n && n.attr.name === "calibre:cover");
49
+ var i;
50
+ const t = (i = e.descendantWithPath("head")) == null ? void 0 : i.childrenNamed("meta").find((r) => r.attr.name === "calibre:cover");
51
+ return !!(t && t.attr.name === "calibre:cover");
52
52
  }, $e = (e) => {
53
- var n, a, r;
54
- return (r = (a = (n = e.descendantWithPath("body")) == null ? void 0 : n.descendantWithPath("div")) == null ? void 0 : a.childrenNamed("svg")) == null ? void 0 : r.find(
55
- (t) => t.attr.width === "100%" && t.attr.preserveAspectRatio === "none"
53
+ var t, i, r;
54
+ return (r = (i = (t = e.descendantWithPath("body")) == null ? void 0 : t.descendantWithPath("div")) == null ? void 0 : i.childrenNamed("svg")) == null ? void 0 : r.find(
55
+ (n) => n.attr.width === "100%" && n.attr.preserveAspectRatio === "none"
56
56
  );
57
- }, xe = ({ archive: e, resourcePath: n }) => async (a) => {
57
+ }, xe = ({ archive: e, resourcePath: t }) => async (i) => {
58
58
  const r = Object.values(e.files).find(
59
- (t) => t.uri === n
59
+ (n) => n.uri === t
60
60
  );
61
61
  if (r != null && r.basename.endsWith(".xhtml")) {
62
- const t = a.body ?? await r.string(), s = new ce(t);
62
+ const n = i.body ?? await r.string(), s = new de(n);
63
63
  if (ve(s)) {
64
- const i = $e(s);
65
- return i && delete i.attr.preserveAspectRatio, {
66
- ...a,
64
+ const a = $e(s);
65
+ return a && delete a.attr.preserveAspectRatio, {
66
+ ...i,
67
67
  body: s == null ? void 0 : s.toString()
68
68
  };
69
69
  }
70
70
  }
71
- return a;
72
- }, Fe = ({ archive: e, resourcePath: n }) => async (a) => xe({ archive: e, resourcePath: n })(a), Se = ({ archive: e, resourcePath: n }) => async (a) => {
71
+ return i;
72
+ }, Fe = ({ archive: e, resourcePath: t }) => async (i) => xe({ archive: e, resourcePath: t })(i), Ae = ({ archive: e, resourcePath: t }) => async (i) => {
73
73
  const r = Object.values(e.files).find(
74
- (t) => t.uri === n
74
+ (n) => n.uri === t
75
75
  );
76
76
  if (r != null && r.basename.endsWith(".css")) {
77
- const s = (a.body ?? await r.string()).replaceAll(
77
+ const s = (i.body ?? await r.string()).replaceAll(
78
78
  "-webkit-writing-mode",
79
79
  "writing-mode"
80
80
  );
81
81
  return {
82
- ...a,
82
+ ...i,
83
83
  body: s
84
84
  };
85
85
  }
86
- return a;
87
- }, k = (e) => {
88
- const a = Object.values(e.files).filter((r) => !r.dir).find((r) => r.uri.endsWith(".opf"));
86
+ return i;
87
+ }, C = (e) => {
88
+ const i = Object.values(e.files).filter((r) => !r.dir).find((r) => r.uri.endsWith(".opf"));
89
89
  return {
90
- data: a,
91
- basePath: (a == null ? void 0 : a.uri.substring(0, a.uri.lastIndexOf("/"))) || ""
90
+ data: i,
91
+ basePath: (i == null ? void 0 : i.uri.substring(0, i.uri.lastIndexOf("/"))) || ""
92
92
  };
93
- }, Q = (e, { opfBasePath: n, baseUrl: a }) => {
93
+ }, E = (e, { opfBasePath: t, baseUrl: i }) => {
94
94
  const r = {
95
95
  contents: [],
96
96
  path: "",
97
97
  href: "",
98
98
  title: ""
99
99
  };
100
- let t = e.childNamed("span") || e.childNamed("a");
101
- r.title = (t == null ? void 0 : t.attr.title) || (t == null ? void 0 : t.val.trim()) || r.title;
102
- let s = t == null ? void 0 : t.name;
103
- s !== "a" && (t = e.descendantWithPath(`${s}.a`), t && (s = t.name.toLowerCase())), s === "a" && (t != null && t.attr.href) && (r.path = T(n, t.attr.href), r.href = T(a, n, t.attr.href));
104
- const i = e.childNamed("ol");
105
- if (i) {
106
- const o = i.childrenNamed("li");
100
+ let n = e.childNamed("span") || e.childNamed("a");
101
+ r.title = (n == null ? void 0 : n.attr.title) || (n == null ? void 0 : n.val.trim()) || r.title;
102
+ let s = n == null ? void 0 : n.name;
103
+ s !== "a" && (n = e.descendantWithPath(`${s}.a`), n && (s = n.name.toLowerCase())), s === "a" && (n != null && n.attr.href) && (r.path = W(t, n.attr.href), r.href = W(i, t, n.attr.href));
104
+ const a = e.childNamed("ol");
105
+ if (a) {
106
+ const o = a.childrenNamed("li");
107
107
  o && o.length > 0 && (r.contents = o.map(
108
- (l) => Q(l, { opfBasePath: n, baseUrl: a })
108
+ (d) => E(d, { opfBasePath: t, baseUrl: i })
109
109
  ));
110
110
  }
111
111
  return r;
112
- }, Te = (e, { opfBasePath: n, baseUrl: a }) => {
113
- var s, i;
112
+ }, Te = (e, { opfBasePath: t, baseUrl: i }) => {
113
+ var s, a;
114
114
  const r = [];
115
- let t;
116
- return e.descendantWithPath("body.nav.ol") ? t = (s = e.descendantWithPath("body.nav.ol")) == null ? void 0 : s.children : e.descendantWithPath("body.section.nav.ol") && (t = (i = e.descendantWithPath("body.section.nav.ol")) == null ? void 0 : i.children), t && t.length > 0 && t.filter((o) => o.name === "li").forEach(
117
- (o) => r.push(Q(o, { opfBasePath: n, baseUrl: a }))
115
+ let n;
116
+ return e.descendantWithPath("body.nav.ol") ? n = (s = e.descendantWithPath("body.nav.ol")) == null ? void 0 : s.children : e.descendantWithPath("body.section.nav.ol") && (n = (a = e.descendantWithPath("body.section.nav.ol")) == null ? void 0 : a.children), n && n.length > 0 && n.filter((o) => o.name === "li").forEach(
117
+ (o) => r.push(E(o, { opfBasePath: t, baseUrl: i }))
118
118
  ), r;
119
- }, Ne = async (e, n, { opfBasePath: a, baseUrl: r }) => {
119
+ }, ke = async (e, t, { opfBasePath: i, baseUrl: r }) => {
120
120
  var s;
121
- const t = (s = e.childNamed("manifest")) == null ? void 0 : s.childrenNamed("item").find((i) => i.attr.properties === "nav");
122
- if (t) {
123
- const i = Object.values(n.files).find(
124
- (o) => o.uri.endsWith(t.attr.href || "")
121
+ const n = (s = e.childNamed("manifest")) == null ? void 0 : s.childrenNamed("item").find((a) => a.attr.properties === "nav");
122
+ if (n) {
123
+ const a = Object.values(t.files).find(
124
+ (o) => o.uri.endsWith(n.attr.href || "")
125
125
  );
126
- if (i) {
127
- const o = new x.XmlDocument(await i.string());
128
- return Te(o, { opfBasePath: a, baseUrl: r });
126
+ if (a) {
127
+ const o = new F.XmlDocument(await a.string());
128
+ return Te(o, { opfBasePath: i, baseUrl: r });
129
129
  }
130
130
  }
131
- }, Z = (e, {
132
- opfBasePath: n,
133
- baseUrl: a,
131
+ }, ee = (e, {
132
+ opfBasePath: t,
133
+ baseUrl: i,
134
134
  prefix: r
135
135
  }) => {
136
- var o, l;
137
- const t = ((o = e == null ? void 0 : e.childNamed(`${r}content`)) == null ? void 0 : o.attr.src) || "", s = {
138
- title: ((l = e == null ? void 0 : e.descendantWithPath(`${r}navLabel.${r}text`)) == null ? void 0 : l.val) || "",
139
- path: T(n, t),
140
- href: T(a, n, t),
136
+ var o, d;
137
+ const n = ((o = e == null ? void 0 : e.childNamed(`${r}content`)) == null ? void 0 : o.attr.src) || "", s = {
138
+ title: ((d = e == null ? void 0 : e.descendantWithPath(`${r}navLabel.${r}text`)) == null ? void 0 : d.val) || "",
139
+ path: W(t, n),
140
+ href: W(i, t, n),
141
141
  contents: []
142
- }, i = e.childrenNamed(`${r}navPoint`);
143
- return i && i.length > 0 && (s.contents = i.map(
144
- (m) => Z(m, { opfBasePath: n, baseUrl: a, prefix: r })
142
+ }, a = e.childrenNamed(`${r}navPoint`);
143
+ return a && a.length > 0 && (s.contents = a.map(
144
+ (p) => ee(p, { opfBasePath: t, baseUrl: i, prefix: r })
145
145
  )), s;
146
- }, We = (e, { opfBasePath: n, baseUrl: a }) => {
147
- var i;
148
- const r = [], t = e.name;
146
+ }, Se = (e, { opfBasePath: t, baseUrl: i }) => {
147
+ var a;
148
+ const r = [], n = e.name;
149
149
  let s = "";
150
- return t.indexOf(":") !== -1 && (s = t.split(":")[0] + ":"), (i = e.childNamed(`${s}navMap`)) == null || i.childrenNamed(`${s}navPoint`).forEach(
151
- (o) => r.push(Z(o, { opfBasePath: n, baseUrl: a, prefix: s }))
150
+ return n.indexOf(":") !== -1 && (s = n.split(":")[0] + ":"), (a = e.childNamed(`${s}navMap`)) == null || a.childrenNamed(`${s}navPoint`).forEach(
151
+ (o) => r.push(ee(o, { opfBasePath: t, baseUrl: i, prefix: s }))
152
152
  ), r;
153
- }, ke = async ({
153
+ }, We = async ({
154
154
  opfData: e,
155
- opfBasePath: n,
156
- baseUrl: a,
155
+ opfBasePath: t,
156
+ baseUrl: i,
157
157
  archive: r
158
158
  }) => {
159
- var i;
160
- const t = e.childNamed("spine"), s = t && t.attr.toc;
159
+ var a;
160
+ const n = e.childNamed("spine"), s = n && n.attr.toc;
161
161
  if (s) {
162
- const o = (i = e.childNamed("manifest")) == null ? void 0 : i.childrenNamed("item").find((l) => l.attr.id === s);
162
+ const o = (a = e.childNamed("manifest")) == null ? void 0 : a.childrenNamed("item").find((d) => d.attr.id === s);
163
163
  if (o) {
164
- const l = `${n}${n === "" ? "" : "/"}${o.attr.href}`, m = Object.values(r.files).find(
165
- (c) => c.uri.endsWith(l)
164
+ const d = `${t}${t === "" ? "" : "/"}${o.attr.href}`, p = Object.values(r.files).find(
165
+ (c) => c.uri.endsWith(d)
166
166
  );
167
- if (m) {
168
- const c = new x.XmlDocument(await m.string());
169
- return We(c, { opfBasePath: n, baseUrl: a });
167
+ if (p) {
168
+ const c = new F.XmlDocument(await p.string());
169
+ return Se(c, { opfBasePath: t, baseUrl: i });
170
170
  }
171
171
  }
172
172
  }
173
- }, Le = async (e, n, { baseUrl: a }) => {
174
- const { basePath: r } = k(n) || {}, t = await ke({
173
+ }, Ne = async (e, t, { baseUrl: i }) => {
174
+ const { basePath: r } = C(t) || {}, n = await We({
175
175
  opfData: e,
176
176
  opfBasePath: r,
177
- archive: n,
178
- baseUrl: a
177
+ archive: t,
178
+ baseUrl: i
179
179
  });
180
- return t || await Ne(e, n, { opfBasePath: r, baseUrl: a });
181
- }, Ae = async (e) => {
182
- const n = {
180
+ return n || await ke(e, t, { opfBasePath: r, baseUrl: i });
181
+ }, Le = async (e) => {
182
+ const t = {
183
183
  renditionLayout: void 0
184
184
  };
185
185
  return await Promise.all(
186
- e.files.map(async (a) => {
187
- var r, t;
188
- if (a.uri.endsWith("com.kobobooks.display-options.xml")) {
189
- const i = (r = new x.XmlDocument(await a.string()).childNamed("platform")) == null ? void 0 : r.childNamed("option");
190
- ((t = i == null ? void 0 : i.attr) == null ? void 0 : t.name) === "fixed-layout" && i.val === "true" && (n.renditionLayout = "pre-paginated");
186
+ e.files.map(async (i) => {
187
+ var r, n;
188
+ if (i.uri.endsWith("com.kobobooks.display-options.xml")) {
189
+ const a = (r = new F.XmlDocument(await i.string()).childNamed("platform")) == null ? void 0 : r.childNamed("option");
190
+ ((n = a == null ? void 0 : a.attr) == null ? void 0 : n.name) === "fixed-layout" && a.val === "true" && (t.renditionLayout = "pre-paginated");
191
191
  }
192
192
  })
193
- ), n;
194
- }, E = async ({
193
+ ), t;
194
+ }, te = async ({
195
195
  archive: e
196
196
  }) => {
197
- const { data: n, basePath: a } = k(e) || {}, r = await (n == null ? void 0 : n.string());
197
+ const { data: t, basePath: i } = C(e) || {}, r = await (t == null ? void 0 : t.string());
198
198
  if (!r) return [];
199
- const t = new x.XmlDocument(r), s = t.childNamed("manifest"), i = t.childNamed("spine"), o = i == null ? void 0 : i.childrenNamed("itemref").map((c) => c.attr.idref), l = (s == null ? void 0 : s.childrenNamed("item").filter((c) => o.includes(c.attr.id || ""))) || [];
200
- return e.files.filter((c) => l.find((d) => a ? `${a}/${d.attr.href}` === c.uri : `${d.attr.href}` === c.uri));
201
- }, ee = (e) => {
202
- var a;
203
- const n = e.childNamed("manifest");
204
- return ((a = n == null ? void 0 : n.childrenNamed("item")) == null ? void 0 : a.map((r) => ({
199
+ const n = new F.XmlDocument(r), s = n.childNamed("manifest"), a = n.childNamed("spine"), o = a == null ? void 0 : a.childrenNamed("itemref").map((c) => c.attr.idref), d = (s == null ? void 0 : s.childrenNamed("item").filter((c) => o.includes(c.attr.id || ""))) || [];
200
+ return e.files.filter((c) => d.find((l) => i ? `${i}/${l.attr.href}` === c.uri : `${l.attr.href}` === c.uri));
201
+ }, ne = (e) => {
202
+ var i;
203
+ const t = e.childNamed("manifest");
204
+ return ((i = t == null ? void 0 : t.childrenNamed("item")) == null ? void 0 : i.map((r) => ({
205
205
  href: r.attr.href || "",
206
206
  id: r.attr.id || "",
207
207
  mediaType: r.attr["media-type"]
208
208
  }))) || [];
209
- }, Pe = ({ archive: e, baseUrl: n }) => async (a) => {
210
- var O;
211
- const { data: r, basePath: t } = k(e) || {}, s = await Ae(e);
209
+ }, Pe = ({ archive: e, baseUrl: t }) => async (i) => {
210
+ var M;
211
+ const { data: r, basePath: n } = C(e) || {}, s = await Le(e);
212
212
  if (!r)
213
- return a;
214
- const i = await r.string();
215
- w.log(i, s);
216
- const o = new x.XmlDocument(i), l = await Le(o, e, { baseUrl: n }) || [], m = o.childNamed("metadata"), c = o.childNamed("manifest"), d = o.childNamed("spine"), p = o.childNamed("guide"), b = m == null ? void 0 : m.childNamed("dc:title"), y = (m == null ? void 0 : m.childrenNamed("meta")) || [], g = y.find(
217
- (h) => h.attr.property === "rendition:layout"
218
- ), u = y.find(
219
- (h) => h.attr.property === "rendition:flow"
220
- ), L = y.find(
221
- (h) => h.attr.property === "rendition:spread"
222
- ), D = g == null ? void 0 : g.val, te = u == null ? void 0 : u.val, ne = L == null ? void 0 : L.val, re = (b == null ? void 0 : b.val) || ((O = e.files.find(({ dir: h }) => h)) == null ? void 0 : O.basename) || "", se = d == null ? void 0 : d.attr["page-progression-direction"], ae = (await E({ archive: e })).reduce(
223
- (h, f) => f.size + h,
213
+ return i;
214
+ const a = await r.string();
215
+ v.log(a, s);
216
+ const o = new F.XmlDocument(a), d = await Ne(o, e, { baseUrl: t }) || [], p = o.childNamed("metadata"), c = o.childNamed("manifest"), l = o.childNamed("spine"), u = o.childNamed("guide"), g = p == null ? void 0 : p.childNamed("dc:title"), b = (p == null ? void 0 : p.childrenNamed("meta")) || [], h = b.find(
217
+ (m) => m.attr.property === "rendition:layout"
218
+ ), x = b.find(
219
+ (m) => m.attr.property === "rendition:flow"
220
+ ), y = b.find(
221
+ (m) => m.attr.property === "rendition:spread"
222
+ ), w = h == null ? void 0 : h.val, N = x == null ? void 0 : x.val, re = y == null ? void 0 : y.val, se = (g == null ? void 0 : g.val) || ((M = e.files.find(({ dir: m }) => m)) == null ? void 0 : M.basename) || "", ie = l == null ? void 0 : l.attr["page-progression-direction"], ae = (await te({ archive: e })).reduce(
223
+ (m, f) => f.size + m,
224
224
  0
225
225
  );
226
226
  return {
227
227
  filename: e.filename,
228
228
  nav: {
229
- toc: l
229
+ toc: d
230
230
  },
231
- renditionLayout: D || s.renditionLayout || "reflowable",
232
- renditionFlow: te || "auto",
233
- renditionSpread: ne,
234
- title: re,
235
- readingDirection: se || "ltr",
236
- spineItems: (d == null ? void 0 : d.childrenNamed("itemref").map((h) => {
237
- var M, V, B;
238
- const f = c == null ? void 0 : c.childrenNamed("item").find((v) => v.attr.id === (h == null ? void 0 : h.attr.idref)), ie = (f == null ? void 0 : f.attr.href) || "", A = ((M = h == null ? void 0 : h.attr.properties) == null ? void 0 : M.split(" ")) || [], oe = ((V = e.files.find((v) => v.uri.endsWith(ie))) == null ? void 0 : V.size) || 0, z = n ?? "";
231
+ renditionLayout: w || s.renditionLayout || "reflowable",
232
+ renditionFlow: N || "auto",
233
+ renditionSpread: re,
234
+ title: se,
235
+ readingDirection: ie || "ltr",
236
+ spineItems: (l == null ? void 0 : l.childrenNamed("itemref").map((m) => {
237
+ var X, B, H;
238
+ const f = c == null ? void 0 : c.childrenNamed("item").find(($) => $.attr.id === (m == null ? void 0 : m.attr.idref)), oe = (f == null ? void 0 : f.attr.href) || "", I = ((X = m == null ? void 0 : m.attr.properties) == null ? void 0 : X.split(" ")) || [], ce = ((B = e.files.find(($) => $.uri.endsWith(oe))) == null ? void 0 : B.size) || 0, U = t ?? "";
239
239
  return {
240
240
  id: (f == null ? void 0 : f.attr.id) || "",
241
- href: (B = f == null ? void 0 : f.attr.href) != null && B.startsWith("https://") ? f == null ? void 0 : f.attr.href : t ? `${z}${t}/${f == null ? void 0 : f.attr.href}` : `${z}${f == null ? void 0 : f.attr.href}`,
242
- renditionLayout: D || "reflowable",
243
- ...A.find(
244
- (v) => v === "rendition:layout-reflowable"
241
+ href: (H = f == null ? void 0 : f.attr.href) != null && H.startsWith("https://") ? f == null ? void 0 : f.attr.href : n ? `${U}${n}/${f == null ? void 0 : f.attr.href}` : `${U}${f == null ? void 0 : f.attr.href}`,
242
+ renditionLayout: w || "reflowable",
243
+ ...I.find(
244
+ ($) => $ === "rendition:layout-reflowable"
245
245
  ) && {
246
246
  renditionLayout: "reflowable"
247
247
  },
248
- progressionWeight: oe / ae,
249
- pageSpreadLeft: A.some((v) => v === "page-spread-left") || void 0,
250
- pageSpreadRight: A.some((v) => v === "page-spread-right") || void 0,
248
+ progressionWeight: ce / ae,
249
+ pageSpreadLeft: I.some(($) => $ === "page-spread-left") || void 0,
250
+ pageSpreadRight: I.some(($) => $ === "page-spread-right") || void 0,
251
251
  // size: itemSize
252
252
  mediaType: f == null ? void 0 : f.attr["media-type"]
253
253
  };
254
254
  })) || [],
255
- items: ee(o),
256
- guide: p == null ? void 0 : p.childrenNamed("reference").map((h) => ({
257
- href: h.attr.href || "",
258
- title: h.attr.title || "",
259
- type: h.attr.type
255
+ items: ne(o),
256
+ guide: u == null ? void 0 : u.childrenNamed("reference").map((m) => ({
257
+ href: m.attr.href || "",
258
+ title: m.attr.title || "",
259
+ type: m.attr.type
260
260
  }))
261
261
  };
262
- }, Ce = async (e, n) => {
263
- var t, s;
264
- const r = await ((t = k(e).data) == null ? void 0 : t.string());
262
+ }, Ce = async (e, t) => {
263
+ var n, s;
264
+ const r = await ((n = C(e).data) == null ? void 0 : n.string());
265
265
  if (r) {
266
- const i = new x.XmlDocument(r);
266
+ const a = new F.XmlDocument(r);
267
267
  return {
268
- mediaType: (s = ee(i).find((l) => n.endsWith(l.href))) == null ? void 0 : s.mediaType
268
+ mediaType: (s = ne(a).find((d) => t.endsWith(d.href))) == null ? void 0 : s.mediaType
269
269
  };
270
270
  }
271
271
  return {
272
- mediaType: Re(n)
272
+ mediaType: Ie(t)
273
273
  };
274
- }, Re = (e) => {
274
+ }, Ie = (e) => {
275
275
  if (e.endsWith(".css"))
276
276
  return "text/css; charset=UTF-8";
277
277
  if (e.endsWith(".jpg"))
@@ -282,25 +282,25 @@ const w = {
282
282
  return "video/mp4";
283
283
  if (e.endsWith(".svg"))
284
284
  return "image/svg+xml";
285
- }, Ie = ({ archive: e, resourcePath: n }) => async (a) => {
285
+ }, Re = ({ archive: e, resourcePath: t }) => async (i) => {
286
286
  const r = Object.values(e.files).find(
287
- (s) => s.uri === n
287
+ (s) => s.uri === t
288
288
  );
289
- if (!r) return a;
290
- const t = await Ce(e, n);
289
+ if (!r) return i;
290
+ const n = await Ce(e, t);
291
291
  return {
292
- ...a,
292
+ ...i,
293
293
  params: {
294
- ...a.params,
294
+ ...i.params,
295
295
  ...(r == null ? void 0 : r.encodingFormat) && {
296
296
  contentType: r.encodingFormat
297
297
  },
298
- ...t.mediaType && {
299
- contentType: t.mediaType
298
+ ...n.mediaType && {
299
+ contentType: n.mediaType
300
300
  }
301
301
  }
302
302
  };
303
- }, K = [
303
+ }, Q = [
304
304
  "div",
305
305
  "span",
306
306
  "p",
@@ -351,206 +351,206 @@ const w = {
351
351
  "canvas",
352
352
  "script",
353
353
  "style"
354
- ], je = ({ archive: e, resourcePath: n }) => async (a) => {
354
+ ], je = ({ archive: e, resourcePath: t }) => async (i) => {
355
355
  const r = Object.values(e.files).find(
356
- (t) => t.uri === n
356
+ (n) => n.uri === t
357
357
  );
358
358
  if (r != null && r.basename.endsWith(".xhtml")) {
359
- const t = a.body ?? await r.string();
359
+ const n = i.body ?? await r.string();
360
360
  if (!new RegExp(
361
- `<(${K.join("|")})[\\s/>]`,
361
+ `<(${Q.join("|")})[\\s/>]`,
362
362
  "i"
363
- ).test(t))
364
- return a;
365
- const i = new RegExp(
366
- `<(${K.join("|")})(\\s[^>]*)?\\s*/>`,
363
+ ).test(n))
364
+ return i;
365
+ const a = new RegExp(
366
+ `<(${Q.join("|")})(\\s[^>]*)?\\s*/>`,
367
367
  "gi"
368
- ), o = t.replace(
369
- i,
370
- (l, m, c = "") => `<${m} ${c.trim()}></${m}>`
368
+ ), o = n.replace(
369
+ a,
370
+ (d, p, c = "") => `<${p} ${c.trim()}></${p}>`
371
371
  );
372
372
  return {
373
- ...a,
373
+ ...i,
374
374
  body: o
375
375
  };
376
376
  }
377
- return a;
378
- }, De = async (e, n) => {
379
- const a = Object.values(e.files).find(
380
- (s) => s.uri === n
377
+ return i;
378
+ }, De = async (e, t) => {
379
+ const i = Object.values(e.files).find(
380
+ (s) => s.uri === t
381
381
  );
382
- if (!a)
382
+ if (!i)
383
383
  throw new Error("no file found");
384
384
  const r = {
385
385
  params: {}
386
- }, t = [
387
- Ie({ archive: e, resourcePath: n }),
388
- je({ archive: e, resourcePath: n }),
389
- Se({ archive: e, resourcePath: n }),
390
- Fe({ archive: e, resourcePath: n })
386
+ }, n = [
387
+ Re({ archive: e, resourcePath: t }),
388
+ je({ archive: e, resourcePath: t }),
389
+ Ae({ archive: e, resourcePath: t }),
390
+ Fe({ archive: e, resourcePath: t })
391
391
  ];
392
392
  try {
393
- const s = await t.reduce(async (i, o) => await o(await i), Promise.resolve(r));
394
- return w.log("Generated resource", n, s), {
393
+ const s = await n.reduce(async (a, o) => await o(await a), Promise.resolve(r));
394
+ return v.log("Generated resource", t, s), {
395
395
  ...s,
396
- body: s.body ?? await a.blob()
396
+ body: s.body ?? await i.blob()
397
397
  };
398
398
  } catch (s) {
399
- throw w.error(s), s;
399
+ throw v.error(s), s;
400
400
  }
401
- }, Oe = ({ archive: e, baseUrl: n }) => async () => {
401
+ }, Oe = ({ archive: e, baseUrl: t }) => async () => {
402
402
  var r;
403
- const a = Object.values(e.files).filter((t) => !t.dir);
403
+ const i = Object.values(e.files).filter((n) => !n.dir);
404
404
  return {
405
405
  filename: e.filename,
406
- title: ((r = e.files.find(({ dir: t }) => t)) == null ? void 0 : r.basename.replace(/\/$/, "")) || "",
406
+ title: ((r = e.files.find(({ dir: n }) => n)) == null ? void 0 : r.basename.replace(/\/$/, "")) || "",
407
407
  renditionLayout: "pre-paginated",
408
408
  renditionSpread: "auto",
409
409
  readingDirection: "ltr",
410
- spineItems: a.filter((t) => !t.basename.endsWith(".db")).map((t, s) => ({
410
+ spineItems: i.filter((n) => !n.basename.endsWith(".db")).map((n, s) => ({
411
411
  // some books such as cbz can have same basename inside different sub folder
412
412
  // we need to make sure to have unique index
413
413
  // /chap01/01.png, /chap02/01.png, etc
414
- id: `${s}.${t.basename}`,
415
- href: encodeURI(`${n}${t.uri}`),
414
+ id: `${s}.${n.basename}`,
415
+ href: encodeURI(`${t}${n.uri}`),
416
416
  renditionLayout: "pre-paginated",
417
- progressionWeight: 1 / a.length,
417
+ progressionWeight: 1 / i.length,
418
418
  pageSpreadLeft: void 0,
419
419
  pageSpreadRight: void 0,
420
- mediaType: t.encodingFormat
420
+ mediaType: n.encodingFormat
421
421
  })),
422
- items: a.map((t, s) => ({
423
- id: `${s}.${t.basename}`,
424
- href: `${n}${t.uri}`
422
+ items: i.map((n, s) => ({
423
+ id: `${s}.${n.basename}`,
424
+ href: `${t}${n.uri}`
425
425
  }))
426
426
  };
427
- }, ze = ({ archive: e }) => async (n) => {
427
+ }, ze = ({ archive: e }) => async (t) => {
428
428
  var s;
429
- const a = e.files.find(
430
- (i) => i.basename.toLowerCase() === "comicinfo.xml"
429
+ const i = e.files.find(
430
+ (a) => a.basename.toLowerCase() === "comicinfo.xml"
431
431
  );
432
- if (!a)
433
- return n;
432
+ if (!i)
433
+ return t;
434
434
  const r = {
435
- ...n,
436
- spineItems: n.spineItems.filter((i) => !i.id.toLowerCase().endsWith("comicinfo.xml")).map((i, o, l) => ({
437
- ...i,
438
- progressionWeight: 1 / l.length
435
+ ...t,
436
+ spineItems: t.spineItems.filter((a) => !a.id.toLowerCase().endsWith("comicinfo.xml")).map((a, o, d) => ({
437
+ ...a,
438
+ progressionWeight: 1 / d.length
439
439
  }))
440
- }, t = await a.string();
440
+ }, n = await i.string();
441
441
  try {
442
- const o = ((s = new x.XmlDocument(t).childNamed("Manga")) == null ? void 0 : s.val) || "unknown";
442
+ const o = ((s = new F.XmlDocument(n).childNamed("Manga")) == null ? void 0 : s.val) || "unknown";
443
443
  return {
444
444
  ...r,
445
445
  readingDirection: o === "YesAndRightToLeft" ? "rtl" : "ltr"
446
446
  };
447
- } catch (i) {
447
+ } catch (a) {
448
448
  return console.error(`Unable to parse comicinfo.xml for content
449
- `, t), console.error(i), r;
449
+ `, n), console.error(a), r;
450
450
  }
451
451
  }, Me = (e) => {
452
- var a;
453
- const n = (a = e.descendantWithPath("head")) == null ? void 0 : a.childrenNamed("meta").find((r) => r.attr.name === "viewport");
454
- return !!(n && n.attr.name === "viewport");
455
- }, Ve = (e) => e.reduce(async (n, a) => {
456
- if (!await n || !de({
457
- mimeType: a.encodingFormat,
458
- uri: a.uri
452
+ var i;
453
+ const t = (i = e.descendantWithPath("head")) == null ? void 0 : i.childrenNamed("meta").find((r) => r.attr.name === "viewport");
454
+ return !!(t && t.attr.name === "viewport");
455
+ }, Ue = (e) => e.reduce(async (t, i) => {
456
+ if (!await t || !le({
457
+ mimeType: i.encodingFormat,
458
+ uri: i.uri
459
459
  }))
460
460
  return !1;
461
- const t = await a.string();
462
- return t ? Me(new x.XmlDocument(t)) : !1;
463
- }, Promise.resolve(!0)), Be = ({ archive: e }) => async (n) => {
464
- if (n.renditionLayout === "reflowable" && n.spineItems.every((r) => r.renditionLayout === "reflowable")) {
465
- const r = await E({ archive: e });
466
- if (await Ve(r))
461
+ const n = await i.string();
462
+ return n ? Me(new F.XmlDocument(n)) : !1;
463
+ }, Promise.resolve(!0)), Xe = ({ archive: e }) => async (t) => {
464
+ if (t.renditionLayout === "reflowable" && t.spineItems.every((r) => r.renditionLayout === "reflowable")) {
465
+ const r = await te({ archive: e });
466
+ if (await Ue(r))
467
467
  return {
468
- ...n,
469
- spineItems: n.spineItems.map((s) => ({
468
+ ...t,
469
+ spineItems: t.spineItems.map((s) => ({
470
470
  ...s,
471
471
  renditionLayout: "pre-paginated"
472
472
  })),
473
473
  renditionLayout: "pre-paginated"
474
474
  };
475
475
  }
476
- return n;
477
- }, j = (e, n) => {
478
- var t;
479
- const a = e.split(/(\d+)/), r = n.split(/(\d+)/);
480
- for (let s = 0, i = a.length; s < i; s++)
481
- if (a[s] !== r[s])
482
- return (t = a[s]) != null && t.match(/\d/) ? +(a[s] || "") - +(r[s] || "") : (a[s] || "").localeCompare(r[s] || "");
476
+ return t;
477
+ }, z = (e, t) => {
478
+ var n;
479
+ const i = e.split(/(\d+)/), r = t.split(/(\d+)/);
480
+ for (let s = 0, a = i.length; s < a; s++)
481
+ if (i[s] !== r[s])
482
+ return (n = i[s]) != null && n.match(/\d/) ? +(i[s] || "") - +(r[s] || "") : (i[s] || "").localeCompare(r[s] || "");
483
483
  return 1;
484
- }, Xe = ({ archive: e, baseUrl: n }) => async (a) => {
485
- if (a.nav) return a;
484
+ }, Be = ({ archive: e, baseUrl: t }) => async (i) => {
485
+ if (i.nav) return i;
486
486
  const r = [...e.files].sort(
487
- (o, l) => j(o.uri, l.uri)
488
- ), t = Object.values(r), s = (o, l, m, c, d) => {
489
- const p = o.find((g) => g.title === l), [b, ...y] = m;
490
- return p ? b ? [
491
- ...o.filter((u) => u != p),
487
+ (o, d) => z(o.uri, d.uri)
488
+ ), n = Object.values(r), s = (o, d, p, c, l) => {
489
+ const u = o.find((h) => h.title === d), [g, ...b] = p;
490
+ return u ? g ? [
491
+ ...o.filter((x) => x != u),
492
492
  {
493
- ...p,
493
+ ...u,
494
494
  contents: [
495
- ...p.contents,
495
+ ...u.contents,
496
496
  ...s(
497
- p.contents,
497
+ u.contents,
498
+ g,
498
499
  b,
499
- y,
500
500
  c,
501
- d
501
+ l
502
502
  )
503
503
  ]
504
504
  }
505
- ] : p.path.split("/").length > d.split("/").length ? [
506
- ...o.filter((u) => u != p),
505
+ ] : u.path.split("/").length > l.split("/").length ? [
506
+ ...o.filter((x) => x != u),
507
507
  {
508
- ...p,
509
- path: d,
508
+ ...u,
509
+ path: l,
510
510
  href: c
511
511
  }
512
- ] : o : b ? [
512
+ ] : o : g ? [
513
513
  ...o,
514
514
  {
515
515
  contents: s(
516
516
  [],
517
+ g,
517
518
  b,
518
- y,
519
519
  c,
520
- d
520
+ l
521
521
  ),
522
522
  href: c,
523
- path: d,
524
- title: l
523
+ path: l,
524
+ title: d
525
525
  }
526
526
  ] : [
527
527
  ...o,
528
528
  {
529
529
  contents: [],
530
530
  href: c,
531
- path: d,
532
- title: l
531
+ path: l,
532
+ title: d
533
533
  }
534
534
  ];
535
- }, i = t.reduce(
536
- (o, l) => {
537
- if (l.dir) return o;
538
- const c = l.uri.split("/").slice(0, -1), [d, ...p] = c;
539
- if (d) {
540
- const b = T(n, encodeURI(l.uri)).replace(/\/$/, ""), y = l.uri.replace(/\/$/, "");
541
- return s(o, d, p, b, y);
535
+ }, a = n.reduce(
536
+ (o, d) => {
537
+ if (d.dir) return o;
538
+ const c = d.uri.split("/").slice(0, -1), [l, ...u] = c;
539
+ if (l) {
540
+ const g = W(t, encodeURI(d.uri)).replace(/\/$/, ""), b = d.uri.replace(/\/$/, "");
541
+ return s(o, l, u, g, b);
542
542
  }
543
543
  return o;
544
544
  },
545
545
  []
546
546
  );
547
- return i.length === 0 ? a : {
548
- ...a,
547
+ return a.length === 0 ? i : {
548
+ ...i,
549
549
  nav: {
550
- toc: i
550
+ toc: a
551
551
  }
552
552
  };
553
- }, Ue = {
553
+ }, He = {
554
554
  filename: "",
555
555
  items: [],
556
556
  nav: {
@@ -561,46 +561,45 @@ const w = {
561
561
  renditionSpread: "auto",
562
562
  spineItems: [],
563
563
  title: ""
564
- }, He = async (e, { baseUrl: n = "" } = {}) => {
565
- const a = [
566
- Oe({ archive: e, baseUrl: n }),
567
- Pe({ archive: e, baseUrl: n }),
568
- Be({ archive: e, baseUrl: n }),
569
- ze({ archive: e, baseUrl: n }),
570
- Xe({ archive: e, baseUrl: n })
564
+ }, Ve = async (e, { baseUrl: t = "" } = {}) => {
565
+ const i = [
566
+ Oe({ archive: e, baseUrl: t }),
567
+ Pe({ archive: e, baseUrl: t }),
568
+ Xe({ archive: e, baseUrl: t }),
569
+ ze({ archive: e, baseUrl: t }),
570
+ Be({ archive: e, baseUrl: t })
571
571
  ];
572
572
  try {
573
- const r = await a.reduce(async (t, s) => await s(await t), Promise.resolve(Ue));
574
- return w.log("Generated manifest", r), r;
573
+ const r = await i.reduce(async (n, s) => await s(await n), Promise.resolve(He));
574
+ return v.log("Generated manifest", r), r;
575
575
  } catch (r) {
576
- throw w.error(r), r;
576
+ throw v.error(r), r;
577
577
  }
578
- }, F = (e) => e.substring(e.lastIndexOf("/") + 1) || e, Y = (e) => e.endsWith("/") ? e.slice(0, -1) : e, Ze = async (e, n) => {
579
- const a = `
578
+ }, T = (e) => e.substring(e.lastIndexOf("/") + 1) || e, Z = (e) => e.endsWith("/") ? e.slice(0, -1) : e, Ze = async (e, t) => {
579
+ const i = `
580
580
  <?xml version="1.0" encoding="UTF-8"?><package xmlns="http://www.idpf.org/2007/opf" version="2.0" unique-identifier="bookid">
581
581
  <metadata>
582
- <meta property="rendition:layout">${n != null && n.useRenditionFlow ? "reflowable" : "pre-paginated"}</meta>
583
- ${n != null && n.useRenditionFlow ? '<meta property="rendition:flow">scrolled-continuous</meta>' : ""}
582
+ <meta property="rendition:layout">${t != null && t.useRenditionFlow ? "reflowable" : "pre-paginated"}</meta>
583
+ ${t != null && t.useRenditionFlow ? '<meta property="rendition:flow">scrolled-continuous</meta>' : ""}
584
584
  </metadata>
585
585
  <manifest>
586
586
  ${e.map(
587
- (s) => `<item id="${F(s)}" href="${s}" media-type="${X(s)}"/>`
587
+ (s) => `<item id="${T(s)}" href="${s}" media-type="${V(s)}"/>`
588
588
  ).join(`
589
589
  `)}
590
590
  </manifest>
591
591
  <spine>
592
- ${e.map((s) => `<itemref idref="${F(s)}" />`).join(`
592
+ ${e.map((s) => `<itemref idref="${T(s)}" />`).join(`
593
593
  `)}
594
594
  </spine>
595
595
  </package>
596
596
  `, r = e.map((s) => ({
597
597
  dir: !1,
598
- basename: F(s),
599
- encodingFormat: X(s),
598
+ basename: T(s),
599
+ encodingFormat: V(s),
600
600
  uri: s,
601
601
  size: 100 / e.length,
602
- base64: async () => "",
603
- blob: async () => new Blob(),
602
+ blob: async () => (await fetch(s)).blob(),
604
603
  string: async () => ""
605
604
  }));
606
605
  return {
@@ -610,21 +609,14 @@ const w = {
610
609
  basename: "content.opf",
611
610
  uri: "content.opf",
612
611
  size: 0,
613
- base64: async () => a,
614
612
  blob: async () => new Blob(),
615
- string: async () => a
613
+ string: async () => i
616
614
  }, ...r],
617
615
  close: () => Promise.resolve()
618
616
  };
619
- }, _e = async (e) => new Promise((n) => {
620
- const a = new FileReader();
621
- a.readAsDataURL(e), a.onloadend = function() {
622
- const r = a.result;
623
- n(r);
624
- };
625
- }), Ee = async (e, {
626
- mimeType: n,
627
- direction: a
617
+ }, Ee = async (e, {
618
+ mimeType: t,
619
+ direction: i
628
620
  } = { mimeType: "text/plain" }) => {
629
621
  const r = `
630
622
  <?xml version="1.0" encoding="UTF-8"?>
@@ -637,7 +629,7 @@ const w = {
637
629
  <manifest>
638
630
  <item id="p01" href="p01.txt" media-type="text/plain"/>
639
631
  </manifest>
640
- <spine page-progression-direction="${a ?? "ltr"}">
632
+ <spine page-progression-direction="${i ?? "ltr"}">
641
633
  <itemref idref="p01" />
642
634
  </spine>
643
635
  </package>
@@ -647,38 +639,35 @@ const w = {
647
639
  files: [
648
640
  {
649
641
  dir: !1,
650
- basename: F("generated.opf"),
642
+ basename: T("generated.opf"),
651
643
  uri: "generated.opf",
652
644
  blob: async () => new Blob([r]),
653
645
  string: async () => r,
654
- base64: async () => btoa(r),
655
646
  size: 0
656
647
  },
657
648
  {
658
649
  dir: !1,
659
- basename: F("p01.txt"),
650
+ basename: T("p01.txt"),
660
651
  uri: "p01.txt",
661
652
  blob: async () => typeof e == "string" ? new Blob([e]) : e,
662
653
  string: async () => typeof e == "string" ? e : e.text(),
663
- base64: async () => typeof e == "string" ? btoa(e) : _e(e),
664
654
  size: typeof e == "string" ? e.length : e.size,
665
- encodingFormat: n
655
+ encodingFormat: t
666
656
  }
667
657
  ],
668
658
  close: () => Promise.resolve()
669
659
  };
670
- }, et = async (e, { orderByAlpha: n, name: a } = {}) => {
660
+ }, et = async (e, { orderByAlpha: t, name: i } = {}) => {
671
661
  let r = Object.values(e.files);
672
- n && (r = r.slice().sort((s, i) => j(s.name, i.name)));
673
- const t = {
674
- filename: a || "",
662
+ t && (r = r.slice().sort((s, a) => z(s.name, a.name)));
663
+ const n = {
664
+ filename: i || "",
675
665
  files: r.map((s) => ({
676
666
  dir: s.dir,
677
- basename: F(s.name),
667
+ basename: T(s.name),
678
668
  uri: s.name,
679
669
  blob: () => s.async("blob"),
680
670
  string: () => s.async("string"),
681
- base64: () => s.async("base64"),
682
671
  ...s.internalStream && {
683
672
  stream: s.internalStream
684
673
  },
@@ -689,179 +678,212 @@ const w = {
689
678
  })),
690
679
  close: () => Promise.resolve()
691
680
  };
692
- return w.log("Generated archive", t), t;
693
- }, tt = async (e, { name: n } = {}) => {
694
- const a = await e.getFilesArray(), r = {
681
+ return v.log("Generated archive", n), n;
682
+ }, tt = async (e, { name: t } = {}) => {
683
+ const i = await e.getFilesArray(), r = {
695
684
  close: () => e.close(),
696
- filename: n ?? "",
697
- files: a.map((t) => ({
685
+ filename: t ?? "",
686
+ files: i.map((n) => ({
698
687
  dir: !1,
699
- basename: t.file.name,
700
- size: t.file.size,
701
- uri: `${t.path}${t.file.name}`,
702
- base64: async () => "",
703
- blob: async () => await t.file.extract(),
704
- string: async () => (await t.file.extract()).text()
688
+ basename: n.file.name,
689
+ size: n.file.size,
690
+ uri: `${n.path}${n.file.name}`,
691
+ blob: async () => await n.file.extract(),
692
+ string: async () => (await n.file.extract()).text()
705
693
  }))
706
694
  };
707
- return w.log("Generated archive", r), r;
708
- }, nt = async (e, { orderByAlpha: n, name: a } = {}) => {
695
+ return v.log("Generated archive", r), r;
696
+ }, nt = async (e, { orderByAlpha: t, name: i } = {}) => {
709
697
  let r = e;
710
- return n && (r = r.slice().sort((t, s) => j(t.name, s.name))), {
711
- filename: a || "",
712
- files: r.map((t) => ({
713
- dir: t.isDir,
714
- basename: F(t.name),
715
- uri: t.name,
716
- blob: async () => new Blob([await t.data()]),
698
+ return t && (r = r.slice().sort((n, s) => z(n.name, s.name))), {
699
+ filename: i || "",
700
+ files: r.map((n) => ({
701
+ dir: n.isDir,
702
+ basename: T(n.name),
703
+ uri: n.name,
704
+ blob: async () => new Blob([await n.data()]),
717
705
  string: async () => {
718
- const s = await t.data();
706
+ const s = await n.data();
719
707
  return String.fromCharCode.apply(
720
708
  null,
721
709
  Array.from(new Uint16Array(s))
722
710
  );
723
711
  },
724
- base64: async () => "",
725
- size: t.size
712
+ size: n.size
726
713
  })),
727
714
  close: () => Promise.resolve()
728
715
  };
729
716
  }, rt = ({
730
717
  enableReport: e
731
718
  } = {}) => {
732
- w.enable(!!e);
733
- }, qe = ({
719
+ v.enable(!!e);
720
+ };
721
+ class _e {
722
+ constructor(t) {
723
+ this.cleanArchiveAfter = t, this.state$ = new ge({
724
+ status: "idle",
725
+ locks: 0
726
+ });
727
+ }
728
+ update(t) {
729
+ this.state$.next({ ...this.state$.getValue(), ...t });
730
+ }
731
+ get locks$() {
732
+ return this.state$.pipe(A(({ locks: t }) => t));
733
+ }
734
+ get state() {
735
+ return this.state$.getValue();
736
+ }
737
+ get isUnlocked$() {
738
+ return this.locks$.pipe(
739
+ A((t) => t <= 0),
740
+ ye(),
741
+ be()
742
+ );
743
+ }
744
+ get overTTL$() {
745
+ return this.isUnlocked$.pipe(
746
+ S(
747
+ (t) => t ? this.cleanArchiveAfter === 1 / 0 ? G : we(this.cleanArchiveAfter) : G
748
+ )
749
+ );
750
+ }
751
+ }
752
+ const qe = ({
734
753
  getArchive: e,
735
- cleanArchiveAfter: n
754
+ cleanArchiveAfter: t = 5 * 60 * 1e3
755
+ // 5mn
736
756
  }) => {
737
- const a = new P(), r = new P(), t = new P(), s = {}, i = a.pipe(
738
- C((c) => {
739
- const d = s[c];
740
- return !d || d.getValue().status !== "idle" ? U : (d.next({
741
- ...d.getValue(),
757
+ const i = new R(), r = new R(), n = new R(), s = {}, a = i.pipe(
758
+ O((p) => {
759
+ const c = s[p];
760
+ if (!c || c.state.status !== "idle") return _;
761
+ let l = !1;
762
+ const u = (y) => {
763
+ var N;
764
+ const w = s[y];
765
+ delete s[y], l || ((N = w == null ? void 0 : w.state.archive) == null || N.close(), l = !0);
766
+ };
767
+ c.update({
742
768
  status: "loading"
743
- }), N(e(c)).pipe(
744
- $((p) => (d.next({
745
- ...d.getValue(),
746
- archive: p,
747
- status: "success"
748
- }), { key: c, archiveEntry: d })),
749
- W((p) => (d.next({
750
- ...d.getValue(),
751
- status: "error",
752
- error: p
753
- }), U))
754
- ));
755
- }),
756
- H()
757
- ), o = i.pipe(
758
- R(({ archiveEntry: c, key: d }) => {
759
- const p = c.pipe($(({ locks: g }) => g)), b = t.pipe(
760
- $(() => !0),
761
- le(!1),
762
- H()
769
+ });
770
+ const g = c.locks$, b = c.isUnlocked$, h = g.pipe(
771
+ pe(),
772
+ j(([y, w]) => w > y),
773
+ fe(!0)
763
774
  );
764
- return p.pipe(
765
- $((g) => g <= 0),
766
- pe()
767
- ).pipe(
768
- fe(b),
769
- R(
770
- ([g, u]) => g ? u ? I(null) : ue(n) : me
771
- ),
772
- _(() => {
773
- var g;
774
- delete s[d], (g = c.getValue().archive) == null || g.close();
775
+ return L(e(p)).pipe(
776
+ D((y) => {
777
+ c.update({
778
+ archive: y,
779
+ status: "success"
780
+ });
781
+ }),
782
+ P((y) => (u(p), c.update({
783
+ status: "error",
784
+ error: y
785
+ }), _)),
786
+ S(() => {
787
+ const y = h.pipe(
788
+ S(() => n),
789
+ S(() => b),
790
+ j((w) => w)
791
+ );
792
+ return q(y, c.overTTL$);
793
+ }),
794
+ D(() => {
795
+ u(p);
775
796
  })
776
797
  );
777
- })
778
- ), l = (c) => {
779
- let d = !1;
780
- const p = s[c] ?? new ge({
781
- archive: void 0,
782
- status: "idle",
783
- locks: 0,
784
- error: void 0
785
- });
786
- s[c] = p, p.next({
787
- ...p.getValue(),
788
- locks: p.getValue().locks + 1
798
+ }),
799
+ ue(r)
800
+ ), o = (p) => {
801
+ let c = !1;
802
+ const l = s[p] ?? new _e(t);
803
+ s[p] = l, l.update({
804
+ locks: l.state.locks + 1
789
805
  });
790
- const b = () => {
791
- d || (d = !0, p.next({
792
- ...p.getValue(),
793
- locks: p.getValue().locks - 1
806
+ const u = () => {
807
+ c || (c = !0, l.update({
808
+ locks: l.state.locks - 1
794
809
  }));
795
810
  };
796
- a.next(c);
797
- const y = p.pipe(
798
- $(({ archive: u }) => u),
799
- be((u) => !!u)
800
- ), g = p.pipe(
801
- _(({ error: u }) => {
802
- if (u)
803
- throw u;
811
+ i.next(p);
812
+ const g = l.state$.pipe(
813
+ A(({ archive: h }) => h),
814
+ j((h) => !!h)
815
+ ), b = l.state$.pipe(
816
+ D(({ error: h }) => {
817
+ if (h)
818
+ throw h;
804
819
  }),
805
- ye()
820
+ me()
806
821
  );
807
- return q(y, g).pipe(
808
- we(),
809
- $((u) => ({ archive: u, release: b })),
810
- W((u) => {
811
- throw b(), u;
822
+ return q(g, b).pipe(
823
+ he(),
824
+ A((h) => ({ archive: h, release: u })),
825
+ P((h) => {
826
+ throw u(), h;
812
827
  })
813
828
  );
814
- }, m = () => {
815
- Object.keys(s).forEach((c) => {
816
- delete s[c];
817
- }), t.next();
829
+ }, d = () => {
830
+ n.next();
818
831
  };
819
- return q(o, i).pipe(he(r)).subscribe(), {
820
- access: l,
821
- purge: m
832
+ return a.subscribe(), {
833
+ access: o,
834
+ purge: d,
835
+ archives: s
822
836
  };
823
837
  };
824
838
  class Ge {
825
839
  constructor({
826
- onError: n,
827
- onManifestSuccess: a,
840
+ onError: t,
841
+ onManifestSuccess: i,
828
842
  ...r
829
843
  }) {
830
- this.onError = (t) => new Response(String(t), { status: 500 }), this.epubLoader = qe(r), this.onManifestSuccess = a ?? (({ manifest: t }) => Promise.resolve(t)), this.onError = n ?? this.onError;
844
+ this.onError = (n) => (console.error(n), new Response(String(n), { status: 500 })), this.epubLoader = qe(r), this.onManifestSuccess = i ?? (({ manifest: n }) => Promise.resolve(n)), this.onError = t ?? this.onError;
845
+ }
846
+ prune() {
847
+ this.epubLoader.purge();
831
848
  }
832
- accessArchive(n) {
833
- return this.lastAccessedKey !== n && this.epubLoader.purge(), this.lastAccessedKey = n, this.epubLoader.access(n);
849
+ accessArchive(t) {
850
+ return this.lastAccessedKey !== void 0 && this.lastAccessedKey !== t && this.epubLoader.purge(), this.lastAccessedKey = t, this.epubLoader.access(t);
851
+ }
852
+ accessArchiveWithoutLock(t) {
853
+ return this.accessArchive(t).pipe(
854
+ A(({ archive: i, release: r }) => (r(), i))
855
+ );
834
856
  }
835
- fetchManifest({ key: n, baseUrl: a }) {
836
- const r = this.accessArchive(n).pipe(
837
- C(({ archive: t, release: s }) => N(
838
- He(t, { baseUrl: a })
857
+ fetchManifest({ key: t, baseUrl: i }) {
858
+ const r = this.accessArchive(t).pipe(
859
+ O(({ archive: n, release: s }) => L(
860
+ Ve(n, { baseUrl: i })
839
861
  ).pipe(
840
- R(
841
- (o) => N(this.onManifestSuccess({ manifest: o, archive: t }))
862
+ S(
863
+ (o) => L(this.onManifestSuccess({ manifest: o, archive: n }))
842
864
  ),
843
- $(
865
+ A(
844
866
  (o) => new Response(JSON.stringify(o), {
845
867
  status: 200
846
868
  })
847
869
  ),
848
- G(() => {
870
+ K(() => {
849
871
  s();
850
872
  })
851
873
  )),
852
- W((t) => I(this.onError(t)))
874
+ P((n) => J(this.onError(n)))
853
875
  );
854
- return J(r);
876
+ return Y(r);
855
877
  }
856
878
  fetchResource({
857
- key: n,
858
- resourcePath: a
879
+ key: t,
880
+ resourcePath: i
859
881
  }) {
860
- const r = this.accessArchive(n).pipe(
861
- C(({ archive: t, release: s }) => N(
862
- De(t, a)
882
+ const r = this.accessArchive(t).pipe(
883
+ O(({ archive: n, release: s }) => L(
884
+ De(n, i)
863
885
  ).pipe(
864
- $(
886
+ A(
865
887
  (o) => new Response(o.body, {
866
888
  status: 200,
867
889
  headers: {
@@ -871,36 +893,36 @@ class Ge {
871
893
  }
872
894
  })
873
895
  ),
874
- G(() => {
896
+ K(() => {
875
897
  s();
876
898
  })
877
899
  )),
878
- W((t) => I(this.onError(t)))
900
+ P((n) => J(this.onError(n)))
879
901
  );
880
- return J(r);
902
+ return Y(r);
881
903
  }
882
904
  }
883
905
  class st extends Ge {
884
906
  constructor({
885
- getUriInfo: n,
886
- ...a
907
+ getUriInfo: t,
908
+ ...i
887
909
  }) {
888
- super(a), this.getUriInfo = n, this.fetchEventListener = this.fetchEventListener.bind(this);
910
+ super(i), this.getUriInfo = t, this.fetchEventListener = this.fetchEventListener.bind(this);
889
911
  }
890
- fetchEventListener(n) {
912
+ fetchEventListener(t) {
891
913
  try {
892
- const a = this.getUriInfo(n);
893
- if (!a) return;
894
- const r = Y(a.baseUrl), t = n.request.url.substring(
914
+ const i = this.getUriInfo(t);
915
+ if (!i) return;
916
+ const r = Z(i.baseUrl), n = t.request.url.substring(
895
917
  r.length + 1
896
- ), [s = ""] = t.split("/"), i = decodeURIComponent(
897
- Y(t.substring(s.length + 1))
918
+ ), [s = ""] = n.split("/"), a = decodeURIComponent(
919
+ Z(n.substring(s.length + 1))
898
920
  );
899
- t.endsWith("/manifest") ? n.respondWith(
921
+ n.endsWith("/manifest") ? t.respondWith(
900
922
  this.fetchManifest({ key: s, baseUrl: `${r}/${s}/` })
901
- ) : n.respondWith(this.fetchResource({ key: s, resourcePath: i }));
902
- } catch (a) {
903
- n.respondWith(new Response(String(a), { status: 500 }));
923
+ ) : t.respondWith(this.fetchResource({ key: s, resourcePath: a }));
924
+ } catch (i) {
925
+ t.respondWith(new Response(String(i), { status: 500 }));
904
926
  }
905
927
  }
906
928
  }
@@ -913,8 +935,8 @@ export {
913
935
  tt as createArchiveFromLibArchive,
914
936
  Ee as createArchiveFromText,
915
937
  Ze as createArchiveFromUrls,
916
- He as generateManifestFromArchive,
938
+ Ve as generateManifestFromArchive,
917
939
  De as generateResourceFromArchive,
918
- k as getArchiveOpfInfo
940
+ C as getArchiveOpfInfo
919
941
  };
920
942
  //# sourceMappingURL=index.js.map