@prose-reader/streamer 1.283.0 → 1.284.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,120 +1,120 @@
1
- import { detectMimeTypeFromName as x, Report as re, isXmlBasedMimeType as se, parseContentType as ie, urlJoin as S } from "@prose-reader/shared";
2
- import { XmlDocument as y, XmlTextNode as oe, XmlElement as ae } from "xmldoc";
3
- import { Subject as L, mergeMap as q, EMPTY as M, pairwise as ce, filter as C, startWith as de, from as A, tap as P, catchError as j, switchMap as I, merge as U, first as V, takeUntil as le, map as $, ignoreElements as pe, BehaviorSubject as me, distinctUntilChanged as ue, shareReplay as fe, NEVER as X, timer as he, finalize as ge, of as ye, lastValueFrom as be } from "rxjs";
4
- const O = (t, e) => {
5
- const i = t.split(/(\d+)/), s = e.split(/(\d+)/);
6
- for (let n = 0, r = i.length; n < r; n++)
7
- if (i[n] !== s[n])
8
- return i[n]?.match(/\d/) ? +(i[n] || "") - +(s[n] || "") : (i[n] || "").localeCompare(s[n] || "");
1
+ import { detectMimeTypeFromName as w, Report as se, isXmlBasedMimeType as ie, parseContentType as q, isMediaContentMimeType as oe, urlJoin as N } from "@prose-reader/shared";
2
+ import { XmlDocument as y, XmlTextNode as ae, XmlElement as ce } from "xmldoc";
3
+ import { Subject as W, mergeMap as J, EMPTY as M, pairwise as de, filter as C, startWith as le, from as R, tap as P, catchError as z, switchMap as I, merge as U, first as V, takeUntil as pe, map as x, ignoreElements as ue, BehaviorSubject as me, distinctUntilChanged as fe, shareReplay as he, NEVER as X, timer as ge, finalize as ye, of as be, lastValueFrom as ve } from "rxjs";
4
+ const j = (e, n) => {
5
+ const i = e.split(/(\d+)/), r = n.split(/(\d+)/);
6
+ for (let t = 0, s = i.length; t < s; t++)
7
+ if (i[t] !== r[t])
8
+ return i[t]?.match(/\d/) ? +(i[t] || "") - +(r[t] || "") : (i[t] || "").localeCompare(r[t] || "");
9
9
  return 1;
10
- }, w = (t) => t.substring(t.lastIndexOf("/") + 1) || t, H = (t) => t.endsWith("/") ? t.slice(0, -1) : t, we = (t) => {
11
- const e = t.lastIndexOf("/");
12
- return e >= 0 ? t.substring(0, e) : "";
13
- }, dt = async (t, { orderByAlpha: e, name: i } = {}) => {
14
- let s = t;
15
- return e && (s = s.slice().sort((n, r) => O(n.name, r.name))), {
10
+ }, v = (e) => e.substring(e.lastIndexOf("/") + 1) || e, _ = (e) => e.endsWith("/") ? e.slice(0, -1) : e, we = (e) => {
11
+ const n = e.lastIndexOf("/");
12
+ return n >= 0 ? e.substring(0, n) : "";
13
+ }, mt = async (e, { orderByAlpha: n, name: i } = {}) => {
14
+ let r = e;
15
+ return n && (r = r.slice().sort((t, s) => j(t.name, s.name))), {
16
16
  filename: i || "",
17
- records: s.map((n) => {
18
- const r = n.size, o = w(n.name);
19
- return n.isDir ? {
17
+ records: r.map((t) => {
18
+ const s = t.size, o = v(t.name);
19
+ return t.isDir ? {
20
20
  dir: !0,
21
21
  basename: o,
22
- uri: n.name,
23
- size: r
22
+ uri: t.name,
23
+ size: s
24
24
  } : {
25
- dir: n.isDir,
25
+ dir: t.isDir,
26
26
  basename: o,
27
- encodingFormat: x(n.name),
28
- uri: n.name,
29
- blob: async () => new Blob([await n.data()], {
30
- type: x(n.name) ?? ""
27
+ encodingFormat: w(t.name),
28
+ uri: t.name,
29
+ blob: async () => new Blob([await t.data()], {
30
+ type: w(t.name) ?? ""
31
31
  }),
32
32
  string: async () => {
33
- const a = await n.data();
33
+ const a = await t.data();
34
34
  return String.fromCharCode.apply(
35
35
  null,
36
36
  Array.from(new Uint16Array(a))
37
37
  );
38
38
  },
39
- size: r
39
+ size: s
40
40
  };
41
41
  }),
42
42
  close: () => Promise.resolve()
43
43
  };
44
- }, ve = "@prose-reader/streamer", m = re.namespace(ve, !1, {
44
+ }, $e = "@prose-reader/streamer", u = se.namespace($e, !1, {
45
45
  color: "#ffae42"
46
- }), $e = (t) => {
47
- const e = {};
48
- for (const s of t) {
49
- const n = s.split("/");
50
- let r = e;
51
- for (let o = 0; o < n.length; o++) {
52
- const a = n[o];
53
- a !== void 0 && (r[a] || (r[a] = {}), r = r[a]);
46
+ }), xe = (e) => {
47
+ const n = {};
48
+ for (const r of e) {
49
+ const t = r.split("/");
50
+ let s = n;
51
+ for (let o = 0; o < t.length; o++) {
52
+ const a = t[o];
53
+ a !== void 0 && (s[a] || (s[a] = {}), s = s[a]);
54
54
  }
55
55
  }
56
- const i = (s, n = "") => Object.keys(s).sort().map((r, o, a) => {
57
- const c = o === a.length - 1, d = n + (c ? "└── " : "├── "), l = n + (c ? " " : "│ "), p = s[r];
58
- return p && Object.keys(p).length > 0 ? `${d}${r}/
59
- ${i(p, l)}` : `${d}${r}`;
56
+ const i = (r, t = "") => Object.keys(r).sort().map((s, o, a) => {
57
+ const c = o === a.length - 1, d = t + (c ? "└── " : "├── "), l = t + (c ? " " : "│ "), p = r[s];
58
+ return p && Object.keys(p).length > 0 ? `${d}${s}/
59
+ ${i(p, l)}` : `${d}${s}`;
60
60
  }).join(`
61
61
  `);
62
- return i(e);
63
- }, lt = async (t, { orderByAlpha: e, name: i } = {}) => {
64
- let s = Object.values(t.files);
65
- e && (s = s.slice().sort((r, o) => O(r.name, o.name)));
66
- const n = {
62
+ return i(n);
63
+ }, ft = async (e, { orderByAlpha: n, name: i } = {}) => {
64
+ let r = Object.values(e.files);
65
+ n && (r = r.slice().sort((s, o) => j(s.name, o.name)));
66
+ const t = {
67
67
  filename: i || "",
68
- records: s.map((r) => {
69
- const o = r._data.uncompressedSize, a = w(r.name);
70
- return r.dir ? {
68
+ records: r.map((s) => {
69
+ const o = s._data.uncompressedSize, a = v(s.name);
70
+ return s.dir ? {
71
71
  dir: !0,
72
72
  basename: a,
73
- uri: r.name,
73
+ uri: s.name,
74
74
  size: o
75
75
  } : {
76
76
  dir: !1,
77
- basename: w(r.name),
78
- uri: r.name,
79
- encodingFormat: x(r.name),
80
- blob: () => r.async("blob"),
81
- string: () => r.async("string"),
82
- ...r.internalStream && {
83
- stream: r.internalStream
77
+ basename: v(s.name),
78
+ uri: s.name,
79
+ encodingFormat: w(s.name),
80
+ blob: () => s.async("blob"),
81
+ string: () => s.async("string"),
82
+ ...s.internalStream && {
83
+ stream: s.internalStream
84
84
  },
85
85
  // this is private API
86
86
  // @ts-expect-error
87
- size: r._data.uncompressedSize
87
+ size: s._data.uncompressedSize
88
88
  };
89
89
  }),
90
90
  close: () => Promise.resolve()
91
91
  };
92
- if (m.log("Generated archive", n), process.env.NODE_ENV === "development" && m.isEnabled()) {
93
- const r = $e(s.map((o) => o.name));
94
- m.groupCollapsed(...m.getGroupArgs("Archive folder structure")), m.log(`
95
- ${r}`), m.groupEnd();
92
+ if (u.log("Generated archive", t), process.env.NODE_ENV === "development" && u.isEnabled()) {
93
+ const s = xe(r.map((o) => o.name));
94
+ u.groupCollapsed(...u.getGroupArgs("Archive folder structure")), u.log(`
95
+ ${s}`), u.groupEnd();
96
96
  }
97
- return n;
98
- }, pt = async (t, { name: e } = {}) => {
99
- const i = await t.getFilesArray(), s = {
100
- close: () => t.close(),
101
- filename: e ?? "",
102
- records: i.map((n) => ({
97
+ return t;
98
+ }, ht = async (e, { name: n } = {}) => {
99
+ const i = await e.getFilesArray(), r = {
100
+ close: () => e.close(),
101
+ filename: n ?? "",
102
+ records: i.map((t) => ({
103
103
  dir: !1,
104
- basename: n.file.name,
105
- encodingFormat: x(n.file.name),
106
- size: n.file.size,
107
- uri: `${n.path}${n.file.name}`,
108
- blob: async () => await n.file.extract(),
109
- string: async () => (await n.file.extract()).text()
104
+ basename: t.file.name,
105
+ encodingFormat: w(t.file.name),
106
+ size: t.file.size,
107
+ uri: `${t.path}${t.file.name}`,
108
+ blob: async () => await t.file.extract(),
109
+ string: async () => (await t.file.extract()).text()
110
110
  }))
111
111
  };
112
- return m.log("Generated archive", s), s;
113
- }, mt = async (t, {
114
- mimeType: e,
112
+ return u.log("Generated archive", r), r;
113
+ }, gt = async (e, {
114
+ mimeType: n,
115
115
  direction: i
116
116
  } = { mimeType: "text/plain" }) => {
117
- const s = `
117
+ const r = `
118
118
  <?xml version="1.0" encoding="UTF-8"?>
119
119
  <package xmlns="http://www.idpf.org/2007/opf" version="3.0" xml:lang="ja" prefix="rendition: http://www.idpf.org/vocab/rendition/#"
120
120
  unique-identifier="ootuya-id">
@@ -135,49 +135,49 @@ ${r}`), m.groupEnd();
135
135
  records: [
136
136
  {
137
137
  dir: !1,
138
- basename: w("generated.opf"),
138
+ basename: v("generated.opf"),
139
139
  uri: "generated.opf",
140
- blob: async () => new Blob([s]),
141
- string: async () => s,
140
+ blob: async () => new Blob([r]),
141
+ string: async () => r,
142
142
  size: 0
143
143
  },
144
144
  {
145
145
  dir: !1,
146
- basename: w("p01.txt"),
146
+ basename: v("p01.txt"),
147
147
  uri: "p01.txt",
148
- blob: async () => typeof t == "string" ? new Blob([t]) : t,
149
- string: async () => typeof t == "string" ? t : t.text(),
150
- size: typeof t == "string" ? t.length : t.size,
151
- encodingFormat: e
148
+ blob: async () => typeof e == "string" ? new Blob([e]) : e,
149
+ string: async () => typeof e == "string" ? e : e.text(),
150
+ size: typeof e == "string" ? e.length : e.size,
151
+ encodingFormat: n
152
152
  }
153
153
  ],
154
154
  close: () => Promise.resolve()
155
155
  };
156
- }, ut = async (t, e) => {
156
+ }, yt = async (e, n) => {
157
157
  const i = `
158
158
  <?xml version="1.0" encoding="UTF-8"?><package xmlns="http://www.idpf.org/2007/opf" version="2.0" unique-identifier="bookid">
159
159
  <metadata>
160
- <meta property="rendition:layout">${e?.useRenditionFlow ? "reflowable" : "pre-paginated"}</meta>
161
- ${e?.useRenditionFlow ? '<meta property="rendition:flow">scrolled-continuous</meta>' : ""}
160
+ <meta property="rendition:layout">${n?.useRenditionFlow ? "reflowable" : "pre-paginated"}</meta>
161
+ ${n?.useRenditionFlow ? '<meta property="rendition:flow">scrolled-continuous</meta>' : ""}
162
162
  </metadata>
163
163
  <manifest>
164
- ${t.map(
165
- (r) => `<item id="${w(r)}" href="${r}" media-type="${x(r)}"/>`
164
+ ${e.map(
165
+ (s) => `<item id="${v(s)}" href="${s}" media-type="${w(s)}"/>`
166
166
  ).join(`
167
167
  `)}
168
168
  </manifest>
169
169
  <spine>
170
- ${t.map((r) => `<itemref idref="${w(r)}" />`).join(`
170
+ ${e.map((s) => `<itemref idref="${v(s)}" />`).join(`
171
171
  `)}
172
172
  </spine>
173
173
  </package>
174
- `, s = t.map((r) => ({
174
+ `, r = e.map((s) => ({
175
175
  dir: !1,
176
- basename: w(r),
177
- encodingFormat: x(r),
178
- uri: r,
179
- size: 100 / t.length,
180
- blob: async () => (await fetch(r)).blob(),
176
+ basename: v(s),
177
+ encodingFormat: w(s),
178
+ uri: s,
179
+ size: 100 / e.length,
180
+ blob: async () => (await fetch(s)).blob(),
181
181
  string: async () => ""
182
182
  }));
183
183
  return {
@@ -189,322 +189,335 @@ ${r}`), m.groupEnd();
189
189
  size: 0,
190
190
  blob: async () => new Blob(),
191
191
  string: async () => i
192
- }, ...s],
192
+ }, ...r],
193
193
  close: () => Promise.resolve()
194
194
  };
195
- }, ft = ({
196
- enableReport: t
195
+ }, bt = ({
196
+ enableReport: e
197
197
  } = {}) => {
198
- m.enable(!!t);
199
- }, E = (t) => {
200
- const i = Object.values(t.records).filter(
201
- (s) => !s.dir
202
- ).find((s) => s.uri.endsWith(".opf"));
198
+ u.enable(!!e);
199
+ }, A = (e) => {
200
+ const i = Object.values(e.records).filter(
201
+ (r) => !r.dir
202
+ ).find((r) => r.uri.endsWith(".opf"));
203
203
  return {
204
204
  data: i,
205
205
  basePath: i?.uri.substring(0, i.uri.lastIndexOf("/")) || ""
206
206
  };
207
- }, xe = ({ archive: t }) => async (e) => {
208
- const i = t.records.find(
209
- (n) => n.basename.toLowerCase() === "com.apple.ibooks.display-options.xml"
207
+ }, Fe = ({ archive: e }) => async (n) => {
208
+ const i = e.records.find(
209
+ (t) => t.basename.toLowerCase() === "com.apple.ibooks.display-options.xml"
210
210
  );
211
211
  if (!i || i.dir)
212
- return e;
213
- const s = await (await i.blob()).text();
212
+ return n;
213
+ const r = await (await i.blob()).text();
214
214
  try {
215
- const o = new y(s).childNamed("platform")?.childrenNamed("option")?.find((a) => a.attr.name === "fixed-layout")?.val || "false";
215
+ const o = new y(r).childNamed("platform")?.childrenNamed("option")?.find((a) => a.attr.name === "fixed-layout")?.val || "false";
216
216
  return {
217
- ...e,
218
- renditionLayout: o === "true" ? "pre-paginated" : e.renditionLayout
217
+ ...n,
218
+ renditionLayout: o === "true" ? "pre-paginated" : n.renditionLayout
219
219
  };
220
- } catch (n) {
220
+ } catch (t) {
221
221
  return console.error(
222
222
  `Unable to parse com.apple.ibooks.display-options.xml for content
223
223
  `,
224
- s
225
- ), console.error(n), e;
224
+ r
225
+ ), console.error(t), n;
226
226
  }
227
- }, Fe = async (t) => {
228
- const e = {
227
+ }, Ae = async (e) => {
228
+ const n = {
229
229
  renditionLayout: void 0
230
230
  };
231
231
  return await Promise.all(
232
- t.records.map(async (i) => {
232
+ e.records.map(async (i) => {
233
233
  if (i.uri.endsWith("com.kobobooks.display-options.xml") && !i.dir) {
234
- const n = new y(await i.string()).childNamed("platform")?.childNamed("option");
235
- n?.attr?.name === "fixed-layout" && n.val === "true" && (e.renditionLayout = "pre-paginated");
234
+ const t = new y(await i.string()).childNamed("platform")?.childNamed("option");
235
+ t?.attr?.name === "fixed-layout" && t.val === "true" && (n.renditionLayout = "pre-paginated");
236
236
  }
237
237
  })
238
- ), e;
239
- }, Ee = ({ archive: t }) => async (e) => {
240
- const i = await Fe(t);
238
+ ), n;
239
+ }, Te = ({ archive: e }) => async (n) => {
240
+ const i = await Ae(e);
241
241
  return {
242
- ...e,
243
- renditionLayout: e.renditionLayout ?? i.renditionLayout
242
+ ...n,
243
+ renditionLayout: n.renditionLayout ?? i.renditionLayout
244
244
  };
245
- }, Re = ({ archive: t, baseUrl: e }) => async () => {
246
- const i = Object.values(t.records).filter((s) => !s.dir);
245
+ }, Ee = ({ archive: e, baseUrl: n }) => async () => {
246
+ const i = Object.values(e.records).filter((r) => !r.dir);
247
247
  return {
248
- filename: t.filename,
249
- title: t.records.find(({ dir: s }) => s)?.basename.replace(/\/$/, "") || t.filename,
248
+ filename: e.filename,
249
+ title: e.records.find(({ dir: r }) => r)?.basename.replace(/\/$/, "") || e.filename,
250
250
  renditionLayout: void 0,
251
251
  renditionSpread: "auto",
252
252
  readingDirection: "ltr",
253
- spineItems: i.filter((s) => !s.basename.endsWith(".db")).map((s, n) => {
254
- const r = e || (/^https?:\/\//.test(s.uri) ? "" : "file://");
253
+ spineItems: i.filter((r) => !r.basename.endsWith(".db")).map((r, t) => {
254
+ const s = n || (/^https?:\/\//.test(r.uri) ? "" : "file://");
255
255
  return {
256
256
  // some books such as cbz can have same basename inside different sub folder
257
257
  // we need to make sure to have unique index
258
258
  // /chap01/01.png, /chap02/01.png, etc
259
- id: `${n}.${s.basename}`,
260
- index: n,
261
- href: encodeURI(`${r}${s.uri}`),
259
+ id: `${t}.${r.basename}`,
260
+ index: t,
261
+ href: encodeURI(`${s}${r.uri}`),
262
262
  renditionLayout: void 0,
263
263
  progressionWeight: 1 / i.length,
264
264
  pageSpreadLeft: void 0,
265
265
  pageSpreadRight: void 0,
266
- mediaType: s.encodingFormat
266
+ mediaType: r.encodingFormat
267
267
  };
268
268
  }),
269
- items: i.map((s, n) => ({
270
- id: `${n}.${s.basename}`,
271
- href: encodeURI(`${e}${s.uri}`)
269
+ items: i.map((r, t) => ({
270
+ id: `${t}.${r.basename}`,
271
+ href: encodeURI(`${n}${r.uri}`)
272
272
  }))
273
273
  };
274
- }, J = async ({
275
- archive: t
274
+ }, K = async ({
275
+ archive: e
276
276
  }) => {
277
- const { data: e, basePath: i } = E(t) || {}, s = await e?.string();
278
- if (!s) return [];
279
- const n = new y(s), r = n.childNamed("manifest"), a = n.childNamed("spine")?.childrenNamed("itemref").map((l) => l.attr.idref), c = r?.childrenNamed("item").filter((l) => a.includes(l.attr.id || "")) || [];
280
- return t.records.filter((l) => c.find((p) => i ? `${i}/${p.attr.href}` === l.uri : `${p.attr.href}` === l.uri));
281
- }, Ne = (t) => {
282
- const e = t.attr.properties?.split(" ") || [];
277
+ const { data: n, basePath: i } = A(e) || {}, r = await n?.string();
278
+ if (!r) return [];
279
+ const t = new y(r), s = t.childNamed("manifest"), a = t.childNamed("spine")?.childrenNamed("itemref").map((l) => l.attr.idref), c = s?.childrenNamed("item").filter((l) => a.includes(l.attr.id || "")) || [];
280
+ return e.records.filter((l) => c.find((p) => i ? `${i}/${p.attr.href}` === l.uri : `${p.attr.href}` === l.uri));
281
+ }, Re = (e) => {
282
+ const n = e.attr.properties?.split(" ") || [];
283
283
  let i;
284
- return e.find((s) => s === "rendition:layout-reflowable") && (i = "reflowable"), e.find((s) => s === "rendition:layout-pre-paginated") && (i = "pre-paginated"), {
284
+ return n.find((r) => r === "rendition:layout-reflowable") && (i = "reflowable"), n.find((r) => r === "rendition:layout-pre-paginated") && (i = "pre-paginated"), {
285
285
  renditionLayout: i,
286
- pageSpreadLeft: e.some((s) => s === "page-spread-left") || void 0,
287
- pageSpreadRight: e.some((s) => s === "page-spread-right") || void 0
286
+ pageSpreadLeft: n.some((r) => r === "page-spread-left") || void 0,
287
+ pageSpreadRight: n.some((r) => r === "page-spread-right") || void 0
288
288
  };
289
- }, Ae = (t, e, i) => {
290
- const s = t.attr.href || "", n = i?.(t);
289
+ }, Ie = (e, n, i) => {
290
+ const r = e.attr.href || "", t = i?.(e);
291
291
  return {
292
- href: e ? `${n}${e}/${s}` : `${n}${s}`,
293
- id: t.attr.id || "",
294
- mediaType: t.attr["media-type"]
292
+ href: n ? `${t}${n}/${r}` : `${t}${r}`,
293
+ id: e.attr.id || "",
294
+ mediaType: e.attr["media-type"]
295
295
  };
296
- }, K = (t, e, i) => {
297
- const s = t.childNamed("manifest"), { basePath: n } = E(e) || {};
298
- return s?.childrenNamed("item")?.map((r) => Ae(r, n, i)) || [];
299
- }, Ie = ({ archive: t, baseUrl: e }) => async (i) => {
300
- const { data: s, basePath: n } = E(t) || {};
301
- if (!s)
296
+ }, Y = (e, n, i) => {
297
+ const r = e.childNamed("manifest"), { basePath: t } = A(n) || {};
298
+ return r?.childrenNamed("item")?.map((s) => Ie(s, t, i)) || [];
299
+ }, Ne = ({ archive: e, baseUrl: n }) => async (i) => {
300
+ const { data: r, basePath: t } = A(e) || {};
301
+ if (!r)
302
302
  return i;
303
- const r = await s.string();
304
- m.groupCollapsed(...m.getGroupArgs("OPF data")), m.log("data", r), m.groupEnd();
305
- const o = new y(r), a = o.childNamed("metadata"), c = o.childNamed("manifest"), d = o.childNamed("spine"), l = o.childNamed("guide"), p = a?.childNamed("dc:title"), g = a?.childrenNamed("meta") || [], b = g.find(
306
- (u) => u.attr.property === "rendition:layout"
307
- ), R = g.find(
308
- (u) => u.attr.property === "rendition:flow"
303
+ const s = await r.string();
304
+ u.groupCollapsed(...u.getGroupArgs("OPF data")), u.log("data", s), u.groupEnd();
305
+ const o = new y(s), a = o.childNamed("metadata"), c = o.childNamed("manifest"), d = o.childNamed("spine"), l = o.childNamed("guide"), p = a?.childNamed("dc:title"), g = a?.childrenNamed("meta") || [], b = g.find(
306
+ (m) => m.attr.property === "rendition:layout"
307
+ ), T = g.find(
308
+ (m) => m.attr.property === "rendition:flow"
309
309
  ), f = g.find(
310
- (u) => u.attr.property === "rendition:spread"
311
- ), T = b?.val, h = R?.val, F = f?.val, W = p?.val || t.records.find(({ dir: u }) => u)?.basename || "", ee = d?.attr["page-progression-direction"], te = (await J({ archive: t })).reduce(
312
- (u, N) => N.size + u,
310
+ (m) => m.attr.property === "rendition:spread"
311
+ ), S = b?.val, h = T?.val, F = f?.val, L = p?.val || e.records.find(({ dir: m }) => m)?.basename || "", te = d?.attr["page-progression-direction"], ne = (await K({ archive: e })).reduce(
312
+ (m, E) => E.size + m,
313
313
  0
314
314
  );
315
315
  return {
316
- filename: t.filename,
317
- renditionLayout: T,
316
+ filename: e.filename,
317
+ renditionLayout: S,
318
318
  renditionFlow: h || "auto",
319
319
  renditionSpread: F,
320
- title: W,
321
- readingDirection: ee || "ltr",
320
+ title: L,
321
+ readingDirection: te || "ltr",
322
322
  /**
323
323
  * @see https://www.w3.org/TR/epub/#sec-itemref-elem
324
324
  */
325
- spineItems: d?.childrenNamed("itemref").map((u, N) => {
326
- const v = c?.childrenNamed("item").find((k) => k.attr.id === u?.attr.idref), z = v?.attr.href || "", ne = t.records.find((k) => k.uri.endsWith(z))?.size || 0, B = e || (/^https?:\/\//.test(z) ? "" : "file://"), D = Ne(u);
325
+ spineItems: d?.childrenNamed("itemref").map((m, E) => {
326
+ const $ = c?.childrenNamed("item").find((k) => k.attr.id === m?.attr.idref), O = $?.attr.href || "", re = e.records.find((k) => k.uri.endsWith(O))?.size || 0, B = n || (/^https?:\/\//.test(O) ? "" : "file://"), D = Re(m);
327
327
  return {
328
328
  ...D,
329
- id: v?.attr.id || "",
330
- index: N,
331
- href: v?.attr.href?.startsWith("https://") ? v?.attr.href : n ? `${B}${n}/${v?.attr.href}` : `${B}${v?.attr.href}`,
332
- renditionLayout: D.renditionLayout ?? T,
333
- progressionWeight: ne / te,
329
+ id: $?.attr.id || "",
330
+ index: E,
331
+ href: $?.attr.href?.startsWith("https://") ? $?.attr.href : t ? `${B}${t}/${$?.attr.href}` : `${B}${$?.attr.href}`,
332
+ renditionLayout: D.renditionLayout ?? S,
333
+ progressionWeight: re / ne,
334
334
  // size: itemSize
335
- mediaType: v?.attr["media-type"]
335
+ mediaType: $?.attr["media-type"]
336
336
  };
337
337
  }) || [],
338
- items: K(o, t, (u) => {
339
- const N = u.attr.href || "";
340
- return /^https?:\/\//.test(N) ? "" : e || "file://";
338
+ items: Y(o, e, (m) => {
339
+ const E = m.attr.href || "";
340
+ return /^https?:\/\//.test(E) ? "" : n || "file://";
341
341
  }),
342
- guide: l?.childrenNamed("reference").map((u) => ({
343
- href: u.attr.href || "",
344
- title: u.attr.title || "",
345
- type: u.attr.type
342
+ guide: l?.childrenNamed("reference").map((m) => ({
343
+ href: m.attr.href || "",
344
+ title: m.attr.title || "",
345
+ type: m.attr.type
346
346
  }))
347
347
  };
348
- }, Se = (t) => {
349
- const e = t.descendantWithPath("head")?.childrenNamed("meta").find((i) => i.attr.name === "viewport");
350
- return !!(e && e.attr.name === "viewport");
351
- }, Te = (t) => t.reduce(async (e, i) => {
352
- if (!await e || !se({
348
+ }, Se = (e) => {
349
+ const n = e.descendantWithPath("head")?.childrenNamed("meta").find((i) => i.attr.name === "viewport");
350
+ return !!(n && n.attr.name === "viewport");
351
+ }, Le = (e) => e.reduce(async (n, i) => {
352
+ if (!await n || !ie({
353
353
  mimeType: i.encodingFormat,
354
354
  uri: i.uri
355
355
  }))
356
356
  return !1;
357
- const n = i.dir ? null : await i.string();
358
- return n ? Se(new y(n)) : !1;
359
- }, Promise.resolve(!0)), We = ({ archive: t }) => async (e) => {
360
- if (e.renditionLayout === "reflowable" && e.spineItems.every((s) => s.renditionLayout === "reflowable")) {
361
- const s = await J({ archive: t });
362
- if (await Te(s))
357
+ const t = i.dir ? null : await i.string();
358
+ return t ? Se(new y(t)) : !1;
359
+ }, Promise.resolve(!0)), ke = ({ archive: e }) => async (n) => {
360
+ if (n.renditionLayout === "reflowable" && n.spineItems.every((r) => r.renditionLayout === "reflowable")) {
361
+ const r = await K({ archive: e });
362
+ if (await Le(r))
363
363
  return {
364
- ...e,
365
- spineItems: e.spineItems.map((r) => ({
366
- ...r,
364
+ ...n,
365
+ spineItems: n.spineItems.map((s) => ({
366
+ ...s,
367
367
  renditionLayout: "pre-paginated"
368
368
  })),
369
369
  renditionLayout: "pre-paginated"
370
370
  };
371
371
  }
372
- return e;
373
- }, ke = ({ archive: t }) => async (e) => {
374
- const i = t.records.find(
375
- (r) => r.basename.toLowerCase() === "comicinfo.xml" && !r.dir
372
+ return n;
373
+ }, We = ({ archive: e }) => async (n) => {
374
+ const i = e.records.find(
375
+ (s) => s.basename.toLowerCase() === "comicinfo.xml" && !s.dir
376
376
  );
377
377
  if (!i || i.dir)
378
- return e;
379
- const s = {
380
- ...e,
381
- spineItems: e.spineItems.filter((r) => !r.id.toLowerCase().endsWith("comicinfo.xml")).map((r, o, a) => ({
382
- ...r,
378
+ return n;
379
+ const r = {
380
+ ...n,
381
+ spineItems: n.spineItems.filter((s) => !s.id.toLowerCase().endsWith("comicinfo.xml")).map((s, o, a) => ({
382
+ ...s,
383
383
  progressionWeight: 1 / a.length
384
384
  }))
385
- }, n = await i.string();
385
+ }, t = await i.string();
386
386
  try {
387
- const o = new y(n).childNamed("Manga")?.val || "unknown";
387
+ const o = new y(t).childNamed("Manga")?.val || "unknown";
388
388
  return {
389
- ...s,
389
+ ...r,
390
390
  readingDirection: o === "YesAndRightToLeft" ? "rtl" : "ltr"
391
391
  };
392
- } catch (r) {
392
+ } catch (s) {
393
393
  return console.error(`Unable to parse comicinfo.xml for content
394
- `, n), console.error(r), s;
394
+ `, t), console.error(s), r;
395
395
  }
396
- }, Le = (t) => t.records.some((e) => e.basename.endsWith(".opf")), Ce = ({ archive: t }) => async (e) => Le(t) ? e : {
397
- ...e,
398
- spineItems: e.spineItems.map((s) => {
399
- const n = t.records.find(
400
- (o) => decodeURI(s.href).endsWith(o.uri)
401
- ), r = ie(n?.encodingFormat ?? "") || x(n?.basename ?? "");
396
+ }, Ce = (e) => e.records.some((n) => n.basename.endsWith(".opf")), Pe = ({ archive: e }) => async (n) => Ce(e) ? n : {
397
+ ...n,
398
+ spineItems: n.spineItems.map((r) => {
399
+ const t = e.records.find(
400
+ (o) => decodeURI(r.href).endsWith(o.uri)
401
+ ), s = q(t?.encodingFormat ?? "") || w(t?.basename ?? "");
402
402
  return {
403
- ...s,
404
- renditionLayout: r?.startsWith("image/") ? "pre-paginated" : s.renditionLayout
403
+ ...r,
404
+ renditionLayout: s && oe(s) ? "pre-paginated" : r.renditionLayout
405
405
  };
406
406
  })
407
- }, Y = (t) => t ? t.children.map((e) => e instanceof oe ? e.text : e instanceof ae ? Y(e) : "").join("").trim() : "", Q = (t, { basePath: e, baseUrl: i }) => {
408
- const s = {
407
+ }, Q = (e) => e ? e.children.map((n) => n instanceof ae ? n.text : n instanceof ce ? Q(n) : "").join("").trim() : "", Z = (e, { basePath: n, baseUrl: i }) => {
408
+ const r = {
409
409
  contents: [],
410
410
  path: "",
411
411
  href: "",
412
412
  title: ""
413
413
  };
414
- let n = t.childNamed("span") || t.childNamed("a");
415
- s.title = (n?.attr.title || n?.val.trim() || Y(n)) ?? "";
416
- let r = n?.name;
417
- r !== "a" && (n = t.descendantWithPath(`${r}.a`), n && (r = n.name.toLowerCase())), r === "a" && n?.attr.href && (s.path = S(e, n.attr.href), s.href = S(i, e, n.attr.href));
418
- const o = t.childNamed("ol");
414
+ let t = e.childNamed("span") || e.childNamed("a");
415
+ r.title = (t?.attr.title || t?.val.trim() || Q(t)) ?? "";
416
+ let s = t?.name;
417
+ s !== "a" && (t = e.descendantWithPath(`${s}.a`), t && (s = t.name.toLowerCase())), s === "a" && t?.attr.href && (r.path = N(n, t.attr.href), r.href = N(i, n, t.attr.href));
418
+ const o = e.childNamed("ol");
419
419
  if (o) {
420
420
  const a = o.childrenNamed("li");
421
- a && a.length > 0 && (s.contents = a.map(
422
- (c) => Q(c, { basePath: e, baseUrl: i })
421
+ a && a.length > 0 && (r.contents = a.map(
422
+ (c) => Z(c, { basePath: n, baseUrl: i })
423
423
  ));
424
424
  }
425
- return s;
426
- }, Pe = (t, { basePath: e, baseUrl: i }) => {
427
- const s = [];
428
- let n;
429
- return t.descendantWithPath("body.nav.ol") ? n = t.descendantWithPath("body.nav.ol")?.children : t.descendantWithPath("body.section.nav.ol") && (n = t.descendantWithPath("body.section.nav.ol")?.children), n && n.length > 0 && n.filter((r) => r.name === "li").forEach((r) => {
430
- s.push(Q(r, { basePath: e, baseUrl: i }));
431
- }), s;
432
- }, je = async (t, e, { baseUrl: i }) => {
433
- const s = t.childNamed("manifest")?.childrenNamed("item").find((n) => n.attr.properties === "nav");
434
- if (s) {
435
- const n = Object.values(e.records).find(
436
- (r) => r.uri.endsWith(s.attr.href || "")
425
+ return r;
426
+ }, ze = (e, { basePath: n, baseUrl: i }) => {
427
+ const r = [];
428
+ let t;
429
+ return e.descendantWithPath("body.nav.ol") ? t = e.descendantWithPath("body.nav.ol")?.children : e.descendantWithPath("body.section.nav.ol") && (t = e.descendantWithPath("body.section.nav.ol")?.children), t && t.length > 0 && t.filter((s) => s.name === "li").forEach((s) => {
430
+ r.push(Z(s, { basePath: n, baseUrl: i }));
431
+ }), r;
432
+ }, je = async (e, n, { baseUrl: i }) => {
433
+ const r = e.childNamed("manifest")?.childrenNamed("item").find((t) => t.attr.properties === "nav");
434
+ if (r) {
435
+ const t = Object.values(n.records).find(
436
+ (s) => s.uri.endsWith(r.attr.href || "")
437
437
  );
438
- if (n && !n.dir) {
439
- const r = new y(await n.string()), o = we(n.uri);
440
- return Pe(r, { basePath: o, baseUrl: i });
438
+ if (t && !t.dir) {
439
+ const s = new y(await t.string()), o = we(t.uri);
440
+ return ze(s, { basePath: o, baseUrl: i });
441
441
  }
442
442
  }
443
- }, Z = (t, {
444
- opfBasePath: e,
443
+ }, ee = (e, {
444
+ opfBasePath: n,
445
445
  baseUrl: i,
446
- prefix: s
446
+ prefix: r
447
447
  }) => {
448
- const n = t?.childNamed(`${s}content`)?.attr.src || "", r = {
449
- title: t?.descendantWithPath(`${s}navLabel.${s}text`)?.val || "",
450
- path: S(e, n),
451
- href: S(i, e, n),
448
+ const t = e?.childNamed(`${r}content`)?.attr.src || "", s = {
449
+ title: e?.descendantWithPath(`${r}navLabel.${r}text`)?.val || "",
450
+ path: N(n, t),
451
+ href: N(i, n, t),
452
452
  contents: []
453
- }, o = t.childrenNamed(`${s}navPoint`);
454
- return o && o.length > 0 && (r.contents = o.map(
455
- (a) => Z(a, { opfBasePath: e, baseUrl: i, prefix: s })
456
- )), r;
457
- }, Oe = (t, { opfBasePath: e, baseUrl: i }) => {
458
- const s = [], n = t.name;
459
- let r = "";
460
- return n.indexOf(":") !== -1 && (r = `${n.split(":")[0]}:`), t.childNamed(`${r}navMap`)?.childrenNamed(`${r}navPoint`).forEach((o) => {
461
- s.push(Z(o, { opfBasePath: e, baseUrl: i, prefix: r }));
462
- }), s;
463
- }, ze = async ({
464
- opfData: t,
465
- opfBasePath: e,
453
+ }, o = e.childrenNamed(`${r}navPoint`);
454
+ return o && o.length > 0 && (s.contents = o.map(
455
+ (a) => ee(a, { opfBasePath: n, baseUrl: i, prefix: r })
456
+ )), s;
457
+ }, Oe = (e, { opfBasePath: n, baseUrl: i }) => {
458
+ const r = [], t = e.name;
459
+ let s = "";
460
+ return t.indexOf(":") !== -1 && (s = `${t.split(":")[0]}:`), e.childNamed(`${s}navMap`)?.childrenNamed(`${s}navPoint`).forEach((o) => {
461
+ r.push(ee(o, { opfBasePath: n, baseUrl: i, prefix: s }));
462
+ }), r;
463
+ }, Be = async ({
464
+ opfData: e,
465
+ opfBasePath: n,
466
466
  baseUrl: i,
467
- archive: s
467
+ archive: r
468
468
  }) => {
469
- const r = t.childNamed("spine")?.attr.toc;
470
- if (r) {
471
- const o = t.childNamed("manifest")?.childrenNamed("item").find((a) => a.attr.id === r);
469
+ const s = e.childNamed("spine")?.attr.toc;
470
+ if (s) {
471
+ const o = e.childNamed("manifest")?.childrenNamed("item").find((a) => a.attr.id === s);
472
472
  if (o) {
473
- const a = `${e}${e === "" ? "" : "/"}${o.attr.href}`, c = Object.values(s.records).find(
473
+ const a = `${n}${n === "" ? "" : "/"}${o.attr.href}`, c = Object.values(r.records).find(
474
474
  (d) => d.uri.endsWith(a)
475
475
  );
476
476
  if (c && !c.dir) {
477
477
  const d = new y(await c.string());
478
- return Oe(d, { opfBasePath: e, baseUrl: i });
478
+ return Oe(d, { opfBasePath: n, baseUrl: i });
479
479
  }
480
480
  }
481
481
  }
482
- }, Be = async (t, e, { baseUrl: i }) => {
483
- const { basePath: s } = E(e) || {}, n = await je(t, e, {
482
+ }, De = async (e, n, { baseUrl: i }) => {
483
+ const { basePath: r } = A(n) || {}, t = await je(e, n, {
484
484
  baseUrl: i
485
485
  });
486
- if (n)
487
- return n;
488
- const r = await ze({
489
- opfData: t,
490
- opfBasePath: s,
491
- archive: e,
486
+ if (t)
487
+ return t;
488
+ const s = await Be({
489
+ opfData: e,
490
+ opfBasePath: r,
491
+ archive: n,
492
492
  baseUrl: i
493
493
  });
494
- if (r)
495
- return r;
496
- }, De = (t, { baseUrl: e }) => {
497
- const i = [...t.records].sort(
498
- (n, r) => O(n.uri, r.uri)
499
- ), s = (n, r, o, a, c) => {
500
- const d = n.find((g) => g.title === r), [l, ...p] = o;
494
+ if (s)
495
+ return s;
496
+ }, Me = (e) => e.replace(/\.[^.]+$/, "").replace(/[_-]/g, " ").replace(/\s+/g, " ").trim(), Ue = (e, n) => {
497
+ if (!(e.spineItems.length === 0 || !e.spineItems.every((r) => (q(r.mediaType ?? "") || w(r.href))?.startsWith("audio/"))))
498
+ return e.spineItems.map((r) => {
499
+ const t = n.records.find(
500
+ (s) => !s.dir && decodeURI(r.href).endsWith(s.uri)
501
+ );
502
+ return {
503
+ title: Me(t?.basename ?? r.href),
504
+ href: r.href,
505
+ path: t?.uri ?? r.href,
506
+ contents: []
507
+ };
508
+ });
509
+ }, Ve = (e, { baseUrl: n }) => {
510
+ const i = [...e.records].sort(
511
+ (t, s) => j(t.uri, s.uri)
512
+ ), r = (t, s, o, a, c) => {
513
+ const d = t.find((g) => g.title === s), [l, ...p] = o;
501
514
  return d ? l ? [
502
- ...n.filter((b) => b !== d),
515
+ ...t.filter((b) => b !== d),
503
516
  {
504
517
  ...d,
505
518
  contents: [
506
519
  ...d.contents,
507
- ...s(
520
+ ...r(
508
521
  d.contents,
509
522
  l,
510
523
  p,
@@ -514,16 +527,16 @@ ${r}`), m.groupEnd();
514
527
  ]
515
528
  }
516
529
  ] : d.path.split("/").length > c.split("/").length ? [
517
- ...n.filter((b) => b !== d),
530
+ ...t.filter((b) => b !== d),
518
531
  {
519
532
  ...d,
520
533
  path: c,
521
534
  href: a
522
535
  }
523
- ] : n : l ? [
524
- ...n,
536
+ ] : t : l ? [
537
+ ...t,
525
538
  {
526
- contents: s(
539
+ contents: r(
527
540
  [],
528
541
  l,
529
542
  p,
@@ -532,143 +545,145 @@ ${r}`), m.groupEnd();
532
545
  ),
533
546
  href: a,
534
547
  path: c,
535
- title: r
548
+ title: s
536
549
  }
537
550
  ] : [
538
- ...n,
551
+ ...t,
539
552
  {
540
553
  contents: [],
541
554
  href: a,
542
555
  path: c,
543
- title: r
556
+ title: s
544
557
  }
545
558
  ];
546
559
  };
547
- return i.reduce((n, r) => {
548
- if (r.dir) return n;
549
- const o = r.uri.split("/").slice(0, -1), [a, ...c] = o;
550
- if (!a) return n;
551
- const d = S(e, encodeURI(r.uri)).replace(/\/$/, ""), l = r.uri.replace(/\/$/, "");
552
- return s(n, a, c, d, l);
560
+ return i.reduce((t, s) => {
561
+ if (s.dir) return t;
562
+ const o = s.uri.split("/").slice(0, -1), [a, ...c] = o;
563
+ if (!a) return t;
564
+ const d = N(n, encodeURI(s.uri)).replace(/\/$/, ""), l = s.uri.replace(/\/$/, "");
565
+ return r(t, a, c, d, l);
553
566
  }, []);
554
- }, Me = async (t, { baseUrl: e }) => {
555
- const { data: i } = E(t) || {};
556
- if (i && !i.dir) {
557
- const n = new y(await i.string());
558
- return await Be(n, t, { baseUrl: e }) || [];
567
+ }, Xe = async (e, n, { baseUrl: i }) => {
568
+ const { data: r } = A(e) || {};
569
+ if (r && !r.dir) {
570
+ const o = new y(await r.string());
571
+ return await De(o, e, { baseUrl: i }) || [];
559
572
  }
560
- const s = De(t, { baseUrl: e });
573
+ const t = Ue(n, e);
574
+ if (t) return t;
575
+ const s = Ve(e, { baseUrl: i });
561
576
  if (s.length !== 0)
562
577
  return s;
563
- }, Ue = ({ archive: t, baseUrl: e }) => async (i) => {
578
+ }, _e = ({ archive: e, baseUrl: n }) => async (i) => {
564
579
  if (i.nav) return i;
565
- const s = await Me(t, { baseUrl: e });
566
- return s ? {
580
+ const r = await Xe(e, i, { baseUrl: n });
581
+ return r ? {
567
582
  ...i,
568
583
  nav: {
569
- toc: s
584
+ toc: r
570
585
  }
571
586
  } : i;
572
- }, Ve = async (t, { baseUrl: e = "" } = {}) => {
587
+ }, He = async (e, { baseUrl: n = "" } = {}) => {
573
588
  const i = [
574
- Ie({ archive: t, baseUrl: e }),
575
- ke({ archive: t }),
576
- xe({ archive: t }),
577
- Ce({ archive: t }),
578
- We({ archive: t }),
579
- Ee({ archive: t }),
580
- Ue({ archive: t, baseUrl: e })
589
+ Ne({ archive: e, baseUrl: n }),
590
+ We({ archive: e }),
591
+ Fe({ archive: e }),
592
+ Pe({ archive: e }),
593
+ ke({ archive: e }),
594
+ Te({ archive: e }),
595
+ _e({ archive: e, baseUrl: n })
581
596
  ];
582
597
  try {
583
- const s = Re({ archive: t, baseUrl: e })(), n = await i.reduce(async (r, o) => await o(await r), s);
584
- if (m.log("Generated manifest", n), process.env.NODE_ENV === "development" && m.isEnabled()) {
585
- const r = JSON.stringify(n, null, 2);
586
- m.groupCollapsed(...m.getGroupArgs("Generated manifest")), m.log(`
587
- ${r}`), m.groupEnd();
598
+ const r = Ee({ archive: e, baseUrl: n })(), t = await i.reduce(async (s, o) => await o(await s), r);
599
+ if (u.log("Generated manifest", t), process.env.NODE_ENV === "development" && u.isEnabled()) {
600
+ const s = JSON.stringify(t, null, 2);
601
+ u.groupCollapsed(...u.getGroupArgs("Generated manifest")), u.log(`
602
+ ${s}`), u.groupEnd();
588
603
  }
589
- return n;
590
- } catch (s) {
591
- throw m.error(s), s;
604
+ return t;
605
+ } catch (r) {
606
+ throw u.error(r), r;
592
607
  }
593
- }, Xe = (t) => {
594
- const e = t.descendantWithPath("head")?.childrenNamed("meta").find((i) => i.attr.name === "calibre:cover");
595
- return !!(e && e.attr.name === "calibre:cover");
596
- }, He = (t) => t.descendantWithPath("body")?.descendantWithPath("div")?.childrenNamed("svg")?.find(
597
- (e) => e.attr.width === "100%" && e.attr.preserveAspectRatio === "none"
598
- ), _e = ({ archive: t, resourcePath: e }) => async (i) => {
599
- const s = Object.values(t.records).find(
600
- (n) => n.uri === e && !n.dir
608
+ }, Ge = (e) => {
609
+ const n = e.descendantWithPath("head")?.childrenNamed("meta").find((i) => i.attr.name === "calibre:cover");
610
+ return !!(n && n.attr.name === "calibre:cover");
611
+ }, qe = (e) => e.descendantWithPath("body")?.descendantWithPath("div")?.childrenNamed("svg")?.find(
612
+ (n) => n.attr.width === "100%" && n.attr.preserveAspectRatio === "none"
613
+ ), Je = ({ archive: e, resourcePath: n }) => async (i) => {
614
+ const r = Object.values(e.records).find(
615
+ (t) => t.uri === n && !t.dir
601
616
  );
602
- if (s && !s.dir && s.basename.endsWith(".xhtml")) {
603
- const n = i.body ?? await s.string(), r = new y(n);
604
- if (Xe(r)) {
605
- const o = He(r);
617
+ if (r && !r.dir && r.basename.endsWith(".xhtml")) {
618
+ const t = i.body ?? await r.string(), s = new y(t);
619
+ if (Ge(s)) {
620
+ const o = qe(s);
606
621
  return o && delete o.attr.preserveAspectRatio, {
607
622
  ...i,
608
- body: r?.toString()
623
+ body: s?.toString()
609
624
  };
610
625
  }
611
626
  }
612
627
  return i;
613
- }, Ge = ({ archive: t, resourcePath: e }) => async (i) => _e({ archive: t, resourcePath: e })(i), qe = ({ archive: t, resourcePath: e }) => async (i) => {
614
- const s = Object.values(t.records).find(
615
- (n) => n.uri === e && !n.dir
628
+ }, Ke = ({ archive: e, resourcePath: n }) => async (i) => Je({ archive: e, resourcePath: n })(i), Ye = ({ archive: e, resourcePath: n }) => async (i) => {
629
+ const r = Object.values(e.records).find(
630
+ (t) => t.uri === n && !t.dir
616
631
  );
617
- if (s && !s.dir && s.basename.endsWith(".css")) {
618
- const r = (i.body ?? await s.string()).replaceAll(
632
+ if (r && !r.dir && r.basename.endsWith(".css")) {
633
+ const s = (i.body ?? await r.string()).replaceAll(
619
634
  "-webkit-writing-mode",
620
635
  "writing-mode"
621
636
  );
622
637
  return {
623
638
  ...i,
624
- body: r
639
+ body: s
625
640
  };
626
641
  }
627
642
  return i;
628
- }, Je = async (t, e) => {
629
- const s = await E(t).data?.string();
630
- if (s) {
631
- const n = new y(s), r = K(n, t, () => "");
632
- if (r.find(
633
- (a) => e.endsWith(a.href)
643
+ }, Qe = async (e, n) => {
644
+ const r = await A(e).data?.string();
645
+ if (r) {
646
+ const t = new y(r), s = Y(t, e, () => "");
647
+ if (s.find(
648
+ (a) => n.endsWith(a.href)
634
649
  )?.mediaType)
635
650
  return {
636
- mediaType: r.find((a) => e.endsWith(a.href))?.mediaType
651
+ mediaType: s.find((a) => n.endsWith(a.href))?.mediaType
637
652
  };
638
653
  }
639
654
  return {
640
- mediaType: Ke(e)
655
+ mediaType: Ze(n)
641
656
  };
642
- }, Ke = (t) => {
643
- if (t.endsWith(".css"))
657
+ }, Ze = (e) => {
658
+ if (e.endsWith(".css"))
644
659
  return "text/css; charset=UTF-8";
645
- if (t.endsWith(".jpg"))
660
+ if (e.endsWith(".jpg"))
646
661
  return "image/jpg";
647
- if (t.endsWith(".xhtml"))
662
+ if (e.endsWith(".xhtml"))
648
663
  return "application/xhtml+xml";
649
- if (t.endsWith(".mp4"))
664
+ if (e.endsWith(".mp4"))
650
665
  return "video/mp4";
651
- if (t.endsWith(".svg"))
666
+ if (e.endsWith(".svg"))
652
667
  return "image/svg+xml";
653
- }, Ye = ({ archive: t, resourcePath: e }) => async (i) => {
654
- const s = Object.values(t.records).find(
655
- (r) => r.uri === e && !r.dir
668
+ }, et = ({ archive: e, resourcePath: n }) => async (i) => {
669
+ const r = Object.values(e.records).find(
670
+ (s) => s.uri === n && !s.dir
656
671
  );
657
- if (!s || s.dir) return i;
658
- const n = await Je(t, e);
672
+ if (!r || r.dir) return i;
673
+ const t = await Qe(e, n);
659
674
  return {
660
675
  ...i,
661
676
  params: {
662
677
  ...i.params,
663
- ...s?.encodingFormat && {
664
- contentType: s.encodingFormat
678
+ ...r?.encodingFormat && {
679
+ contentType: r.encodingFormat
665
680
  },
666
- ...n.mediaType && {
667
- contentType: n.mediaType
681
+ ...t.mediaType && {
682
+ contentType: t.mediaType
668
683
  }
669
684
  }
670
685
  };
671
- }, _ = [
686
+ }, H = [
672
687
  "div",
673
688
  "span",
674
689
  "p",
@@ -719,21 +734,21 @@ ${r}`), m.groupEnd();
719
734
  "canvas",
720
735
  "script",
721
736
  "style"
722
- ], Qe = ({ archive: t, resourcePath: e }) => async (i) => {
723
- const s = Object.values(t.records).find(
724
- (n) => n.uri === e && !n.dir
737
+ ], tt = ({ archive: e, resourcePath: n }) => async (i) => {
738
+ const r = Object.values(e.records).find(
739
+ (t) => t.uri === n && !t.dir
725
740
  );
726
- if (s && !s.dir && s.basename.endsWith(".xhtml")) {
727
- const n = i.body ?? await s.string();
741
+ if (r && !r.dir && r.basename.endsWith(".xhtml")) {
742
+ const t = i.body ?? await r.string();
728
743
  if (!new RegExp(
729
- `<(${_.join("|")})[\\s/>]`,
744
+ `<(${H.join("|")})[\\s/>]`,
730
745
  "i"
731
- ).test(n))
746
+ ).test(t))
732
747
  return i;
733
748
  const o = new RegExp(
734
- `<(${_.join("|")})(\\s[^>]*)?\\s*/>`,
749
+ `<(${H.join("|")})(\\s[^>]*)?\\s*/>`,
735
750
  "gi"
736
- ), a = n.replace(
751
+ ), a = t.replace(
737
752
  o,
738
753
  (c, d, l = "") => `<${d} ${l.trim()}></${d}>`
739
754
  );
@@ -743,100 +758,100 @@ ${r}`), m.groupEnd();
743
758
  };
744
759
  }
745
760
  return i;
746
- }, Ze = async (t, e) => {
747
- const i = Object.values(t.records).find(
748
- (r) => r.uri === e && !r.dir
761
+ }, nt = async (e, n) => {
762
+ const i = Object.values(e.records).find(
763
+ (s) => s.uri === n && !s.dir
749
764
  );
750
765
  if (!i || i.dir)
751
- throw new Error(`no file found for resourcePath:${e}`);
752
- const s = {
766
+ throw new Error(`no file found for resourcePath:${n}`);
767
+ const r = {
753
768
  params: {}
754
- }, n = [
755
- Ye({ archive: t, resourcePath: e }),
756
- Qe({ archive: t, resourcePath: e }),
757
- qe({ archive: t, resourcePath: e }),
758
- Ge({ archive: t, resourcePath: e })
769
+ }, t = [
770
+ et({ archive: e, resourcePath: n }),
771
+ tt({ archive: e, resourcePath: n }),
772
+ Ye({ archive: e, resourcePath: n }),
773
+ Ke({ archive: e, resourcePath: n })
759
774
  ];
760
775
  try {
761
- const r = await n.reduce(async (o, a) => await a(await o), Promise.resolve(s));
762
- return m.log("Generated resource", e, r), {
763
- ...r,
764
- body: r.body ?? await i.blob()
776
+ const s = await t.reduce(async (o, a) => await a(await o), Promise.resolve(r));
777
+ return u.log("Generated resource", n, s), {
778
+ ...s,
779
+ body: s.body ?? await i.blob()
765
780
  };
766
- } catch (r) {
767
- throw m.error(r), r;
781
+ } catch (s) {
782
+ throw u.error(s), s;
768
783
  }
769
784
  };
770
- class et {
771
- constructor(e) {
772
- this.cleanArchiveAfter = e, this.state$ = new me({
785
+ class rt {
786
+ constructor(n) {
787
+ this.cleanArchiveAfter = n, this.state$ = new me({
773
788
  status: "idle",
774
789
  locks: 0
775
790
  });
776
791
  }
777
- update(e) {
778
- this.state$.next({ ...this.state$.getValue(), ...e });
792
+ update(n) {
793
+ this.state$.next({ ...this.state$.getValue(), ...n });
779
794
  }
780
795
  get locks$() {
781
- return this.state$.pipe($(({ locks: e }) => e));
796
+ return this.state$.pipe(x(({ locks: n }) => n));
782
797
  }
783
798
  get state() {
784
799
  return this.state$.getValue();
785
800
  }
786
801
  get isUnlocked$() {
787
802
  return this.locks$.pipe(
788
- $((e) => e <= 0),
789
- ue(),
790
- fe()
803
+ x((n) => n <= 0),
804
+ fe(),
805
+ he()
791
806
  );
792
807
  }
793
808
  get overTTL$() {
794
809
  return this.isUnlocked$.pipe(
795
810
  I(
796
- (e) => e ? this.cleanArchiveAfter === 1 / 0 ? X : he(this.cleanArchiveAfter) : X
811
+ (n) => n ? this.cleanArchiveAfter === 1 / 0 ? X : ge(this.cleanArchiveAfter) : X
797
812
  )
798
813
  );
799
814
  }
800
815
  }
801
- const tt = ({
802
- getArchive: t,
803
- cleanArchiveAfter: e = 300 * 1e3
816
+ const st = ({
817
+ getArchive: e,
818
+ cleanArchiveAfter: n = 300 * 1e3
804
819
  // 5mn
805
820
  }) => {
806
- const i = new L(), s = new L(), n = new L(), r = {}, o = i.pipe(
807
- q((d) => {
808
- const l = r[d];
821
+ const i = new W(), r = new W(), t = new W(), s = {}, o = i.pipe(
822
+ J((d) => {
823
+ const l = s[d];
809
824
  if (!l || l.state.status !== "idle") return M;
810
825
  let p = !1;
811
826
  const g = (h) => {
812
- m.debug(`Cleaning up archive with key: ${h}`);
813
- const F = r[h];
814
- delete r[h], p || (F?.state.archive?.close(), p = !0);
827
+ u.debug(`Cleaning up archive with key: ${h}`);
828
+ const F = s[h];
829
+ delete s[h], p || (F?.state.archive?.close(), p = !0);
815
830
  };
816
831
  l.update({
817
832
  status: "loading"
818
833
  });
819
- const b = l.locks$, R = l.isUnlocked$, f = b.pipe(
820
- ce(),
834
+ const b = l.locks$, T = l.isUnlocked$, f = b.pipe(
835
+ de(),
821
836
  C(([h, F]) => F > h),
822
- de(!0)
837
+ le(!0)
823
838
  );
824
- return A(t(d)).pipe(
839
+ return R(e(d)).pipe(
825
840
  P((h) => {
826
841
  l.update({
827
842
  archive: h,
828
843
  status: "success"
829
844
  });
830
845
  }),
831
- j((h) => (g(d), l.update({
846
+ z((h) => (g(d), l.update({
832
847
  status: "error",
833
848
  error: h
834
849
  }), M)),
835
850
  I(() => {
836
851
  const h = f.pipe(
837
- I(() => n),
838
- I(() => R),
839
- C((W) => W)
852
+ I(() => t),
853
+ I(() => T),
854
+ C((L) => L)
840
855
  );
841
856
  return U(h, l.overTTL$).pipe(
842
857
  V(),
@@ -847,11 +862,11 @@ const tt = ({
847
862
  })
848
863
  );
849
864
  }),
850
- le(s)
865
+ pe(r)
851
866
  ), a = (d) => {
852
867
  let l = !1;
853
- const p = r[d] ?? new et(e);
854
- r[d] = p, p.update({
868
+ const p = s[d] ?? new rt(n);
869
+ s[d] = p, p.update({
855
870
  locks: p.state.locks + 1
856
871
  });
857
872
  const g = () => {
@@ -861,161 +876,179 @@ const tt = ({
861
876
  };
862
877
  i.next(d);
863
878
  const b = p.state$.pipe(
864
- $(({ archive: f }) => f),
879
+ x(({ archive: f }) => f),
865
880
  C((f) => !!f)
866
- ), R = p.state$.pipe(
881
+ ), T = p.state$.pipe(
867
882
  P(({ error: f }) => {
868
883
  if (f)
869
884
  throw f;
870
885
  }),
871
- pe()
886
+ ue()
872
887
  );
873
- return U(b, R).pipe(
888
+ return U(b, T).pipe(
874
889
  V(),
875
- $((f) => ({ archive: f, release: g })),
876
- j((f) => {
890
+ x((f) => ({ archive: f, release: g })),
891
+ z((f) => {
877
892
  throw g(), f;
878
893
  })
879
894
  );
880
895
  }, c = () => {
881
- n.next();
896
+ t.next();
882
897
  };
883
898
  return o.subscribe(), {
884
899
  access: a,
885
900
  purge: c,
886
- _archives: r
901
+ _archives: s
887
902
  };
888
- }, G = (t) => t ? /^\d+$/.test(t) ? {
903
+ }, G = (e) => e ? /^\d+$/.test(e) ? {
889
904
  valid: !0,
890
- value: Number.parseInt(t, 10)
905
+ value: Number.parseInt(e, 10)
891
906
  } : {
892
907
  valid: !1,
893
908
  value: void 0
894
909
  } : {
895
910
  valid: !0,
896
911
  value: void 0
897
- }, nt = (t) => {
898
- if (!t.toLowerCase().startsWith("bytes="))
912
+ }, it = (e) => {
913
+ if (!e.toLowerCase().startsWith("bytes="))
899
914
  return {
900
915
  kind: "missing"
901
916
  };
902
- const e = t.slice(6).trim();
903
- if (!e)
917
+ const n = e.slice(6).trim();
918
+ if (!n)
904
919
  return {
905
920
  kind: "invalid"
906
921
  };
907
- if (e.includes(","))
922
+ if (n.includes(","))
908
923
  return {
909
924
  kind: "multi"
910
925
  };
911
- const i = /^(\d*)-(\d*)$/.exec(e);
926
+ const i = /^(\d*)-(\d*)$/.exec(n);
912
927
  if (!i)
913
928
  return {
914
929
  kind: "invalid"
915
930
  };
916
- const [, s = "", n = ""] = i, r = G(s.trim()), o = G(n.trim());
917
- return !r.valid || !o.valid ? {
931
+ const [, r = "", t = ""] = i, s = G(r.trim()), o = G(t.trim());
932
+ return !s.valid || !o.valid ? {
918
933
  kind: "invalid"
919
934
  } : {
920
935
  kind: "single",
921
- start: r.value,
936
+ start: s.value,
922
937
  end: o.value
923
938
  };
924
- }, rt = ({
925
- body: t,
926
- contentType: e,
939
+ }, ot = (e) => {
940
+ if (e instanceof Blob)
941
+ return {
942
+ size: e.size,
943
+ slice: (i, r) => {
944
+ const t = e.slice(i, r);
945
+ return { content: t, length: t.size };
946
+ }
947
+ };
948
+ const n = new TextEncoder().encode(e);
949
+ return {
950
+ size: n.byteLength,
951
+ slice: (i, r) => {
952
+ const t = n.slice(i, r);
953
+ return { content: t, length: t.byteLength };
954
+ }
955
+ };
956
+ }, at = ({
957
+ body: e,
958
+ contentType: n,
927
959
  rangeHeader: i
928
960
  }) => {
929
- const s = new Headers(), n = t instanceof Blob ? t : new Blob([t]), r = n.size;
930
- if (e && s.set("Content-Type", e), s.set("Accept-Ranges", "bytes"), s.set("Content-Length", String(r)), !i)
931
- return new Response(t, {
961
+ const r = new Headers();
962
+ if (n && r.set("Content-Type", n), r.set("Accept-Ranges", "bytes"), !i)
963
+ return e instanceof Blob && r.set("Content-Length", String(e.size)), new Response(e, {
932
964
  status: 200,
933
- headers: s
965
+ headers: r
934
966
  });
935
- const o = nt(i);
936
- if (o.kind === "missing" || o.kind === "multi")
937
- return new Response(t, {
967
+ const t = it(i);
968
+ if (t.kind === "missing" || t.kind === "multi")
969
+ return e instanceof Blob && r.set("Content-Length", String(e.size)), new Response(e, {
938
970
  status: 200,
939
- headers: s
971
+ headers: r
940
972
  });
941
- if (o.kind === "invalid")
973
+ const s = ot(e), o = s.size;
974
+ if (t.kind === "invalid")
942
975
  return new Response(null, {
943
976
  status: 416,
944
977
  headers: {
945
- "Content-Range": `bytes */${r}`
978
+ "Content-Range": `bytes */${o}`
946
979
  }
947
980
  });
948
- let a = o.start, c = o.end;
981
+ let a = t.start, c = t.end;
949
982
  if (a === void 0 && c === void 0)
950
983
  return new Response(null, {
951
984
  status: 416,
952
985
  headers: {
953
- "Content-Range": `bytes */${r}`
986
+ "Content-Range": `bytes */${o}`
954
987
  }
955
988
  });
956
989
  if (a === void 0) {
957
- const l = Math.min(c ?? 0, r);
958
- a = Math.max(0, r - l), c = r - 1;
959
- } else (c === void 0 || c >= r) && (c = r - 1);
960
- if (a < 0 || c < 0 || a >= r || c >= r || a > c)
990
+ const l = Math.min(c ?? 0, o);
991
+ a = Math.max(0, o - l), c = o - 1;
992
+ } else (c === void 0 || c >= o) && (c = o - 1);
993
+ if (a < 0 || c < 0 || a >= o || c >= o || a > c)
961
994
  return new Response(null, {
962
995
  status: 416,
963
996
  headers: {
964
- "Content-Range": `bytes */${r}`
997
+ "Content-Range": `bytes */${o}`
965
998
  }
966
999
  });
967
- const d = n.slice(a, c + 1);
968
- return s.set("Content-Length", String(d.size)), s.set("Content-Range", `bytes ${a}-${c}/${r}`), new Response(d, {
1000
+ const d = s.slice(a, c + 1);
1001
+ return r.set("Content-Length", String(d.length)), r.set("Content-Range", `bytes ${a}-${c}/${o}`), new Response(d.content, {
969
1002
  status: 206,
970
- headers: s
1003
+ headers: r
971
1004
  });
972
1005
  };
973
- class st {
1006
+ class ct {
974
1007
  constructor({
975
- onError: e,
1008
+ onError: n,
976
1009
  onManifestSuccess: i,
977
- ...s
1010
+ ...r
978
1011
  }) {
979
- this.onError = (n) => (console.error(n), new Response(String(n), { status: 500 })), this.epubLoader = tt(s), this.onManifestSuccess = i ?? (({ manifest: n }) => Promise.resolve(n)), this.onError = e ?? this.onError;
1012
+ this.onError = (t) => (console.error(t), new Response(String(t), { status: 500 })), this.epubLoader = st(r), this.onManifestSuccess = i ?? (({ manifest: t }) => Promise.resolve(t)), this.onError = n ?? this.onError;
980
1013
  }
981
1014
  prune() {
982
1015
  this.epubLoader.purge();
983
1016
  }
984
- accessArchive(e) {
985
- return this.lastAccessedKey !== void 0 && this.lastAccessedKey !== e && this.epubLoader.purge(), this.lastAccessedKey = e, this.epubLoader.access(e);
1017
+ accessArchive(n) {
1018
+ return this.lastAccessedKey !== void 0 && this.lastAccessedKey !== n && this.epubLoader.purge(), this.lastAccessedKey = n, this.epubLoader.access(n);
986
1019
  }
987
- accessArchiveWithoutLock(e) {
988
- return this.accessArchive(e).pipe(
989
- $(({ archive: i, release: s }) => (s(), i))
1020
+ accessArchiveWithoutLock(n) {
1021
+ return this.accessArchive(n).pipe(
1022
+ x(({ archive: i, release: r }) => (r(), i))
990
1023
  );
991
1024
  }
992
1025
  withArchiveResponse({
993
- key: e,
1026
+ key: n,
994
1027
  getResponse: i
995
1028
  }) {
996
- const s = this.accessArchive(e).pipe(
997
- q(
998
- ({ archive: n, release: r }) => A(i(n)).pipe(
999
- ge(() => {
1000
- r();
1029
+ const r = this.accessArchive(n).pipe(
1030
+ J(
1031
+ ({ archive: t, release: s }) => R(i(t)).pipe(
1032
+ ye(() => {
1033
+ s();
1001
1034
  })
1002
1035
  )
1003
1036
  ),
1004
- j((n) => ye(this.onError(n)))
1037
+ z((t) => be(this.onError(t)))
1005
1038
  );
1006
- return be(s);
1039
+ return ve(r);
1007
1040
  }
1008
- fetchManifest({ key: e, baseUrl: i }) {
1041
+ fetchManifest({ key: n, baseUrl: i }) {
1009
1042
  return this.withArchiveResponse({
1010
- key: e,
1011
- getResponse: (s) => A(
1012
- Ve(s, { baseUrl: i })
1043
+ key: n,
1044
+ getResponse: (r) => R(
1045
+ He(r, { baseUrl: i })
1013
1046
  ).pipe(
1014
1047
  I(
1015
- (r) => A(this.onManifestSuccess({ manifest: r, archive: s }))
1048
+ (s) => R(this.onManifestSuccess({ manifest: s, archive: r }))
1016
1049
  ),
1017
- $(
1018
- (r) => new Response(JSON.stringify(r), {
1050
+ x(
1051
+ (s) => new Response(JSON.stringify(s), {
1019
1052
  status: 200
1020
1053
  })
1021
1054
  )
@@ -1023,22 +1056,22 @@ class st {
1023
1056
  });
1024
1057
  }
1025
1058
  fetchResource({
1026
- key: e,
1059
+ key: n,
1027
1060
  resourcePath: i,
1028
- request: s
1061
+ request: r
1029
1062
  }) {
1030
1063
  return this.withArchiveResponse({
1031
- key: e,
1032
- getResponse: (n) => {
1033
- const r = i.replaceAll("file://", "");
1034
- return A(
1035
- Ze(n, r)
1064
+ key: n,
1065
+ getResponse: (t) => {
1066
+ const s = i.replaceAll("file://", "");
1067
+ return R(
1068
+ nt(t, s)
1036
1069
  ).pipe(
1037
- $(
1038
- (a) => rt({
1070
+ x(
1071
+ (a) => at({
1039
1072
  body: a.body ?? "",
1040
1073
  contentType: a.params.contentType,
1041
- rangeHeader: s?.headers.get("range")
1074
+ rangeHeader: r?.headers.get("range")
1042
1075
  })
1043
1076
  )
1044
1077
  );
@@ -1046,51 +1079,51 @@ class st {
1046
1079
  });
1047
1080
  }
1048
1081
  }
1049
- class ht extends st {
1082
+ class vt extends ct {
1050
1083
  constructor({
1051
- getUriInfo: e,
1084
+ getUriInfo: n,
1052
1085
  ...i
1053
1086
  }) {
1054
- super(i), this.getUriInfo = e, this.fetchEventListener = this.fetchEventListener.bind(this);
1087
+ super(i), this.getUriInfo = n, this.fetchEventListener = this.fetchEventListener.bind(this);
1055
1088
  }
1056
- fetchEventListener(e) {
1089
+ fetchEventListener(n) {
1057
1090
  try {
1058
- const i = this.getUriInfo(e);
1091
+ const i = this.getUriInfo(n);
1059
1092
  if (!i) return;
1060
- const s = H(i.baseUrl), n = e.request.url.substring(
1061
- s.length + 1
1062
- ), [r = ""] = n.split("/"), o = decodeURIComponent(
1063
- H(n.substring(r.length + 1))
1093
+ const r = _(i.baseUrl), t = n.request.url.substring(
1094
+ r.length + 1
1095
+ ), [s = ""] = t.split("/"), o = decodeURIComponent(
1096
+ _(t.substring(s.length + 1))
1064
1097
  );
1065
- n.endsWith("/manifest") ? e.respondWith(
1066
- this.fetchManifest({ key: r, baseUrl: `${s}/${r}/` })
1067
- ) : e.respondWith(
1098
+ t.endsWith("/manifest") ? n.respondWith(
1099
+ this.fetchManifest({ key: s, baseUrl: `${r}/${s}/` })
1100
+ ) : n.respondWith(
1068
1101
  this.fetchResource({
1069
- key: r,
1102
+ key: s,
1070
1103
  resourcePath: o,
1071
- request: e.request
1104
+ request: n.request
1072
1105
  })
1073
1106
  );
1074
1107
  } catch (i) {
1075
- e.respondWith(new Response(String(i), { status: 500 }));
1108
+ n.respondWith(new Response(String(i), { status: 500 }));
1076
1109
  }
1077
1110
  }
1078
1111
  }
1079
1112
  export {
1080
- ht as ServiceWorkerStreamer,
1081
- st as Streamer,
1082
- ft as configure,
1083
- dt as createArchiveFromArrayBufferList,
1084
- lt as createArchiveFromJszip,
1085
- pt as createArchiveFromLibArchive,
1086
- mt as createArchiveFromText,
1087
- ut as createArchiveFromUrls,
1088
- Ve as generateManifestFromArchive,
1089
- Ze as generateResourceFromArchive,
1090
- E as getArchiveOpfInfo,
1113
+ vt as ServiceWorkerStreamer,
1114
+ ct as Streamer,
1115
+ bt as configure,
1116
+ mt as createArchiveFromArrayBufferList,
1117
+ ft as createArchiveFromJszip,
1118
+ ht as createArchiveFromLibArchive,
1119
+ gt as createArchiveFromText,
1120
+ yt as createArchiveFromUrls,
1121
+ He as generateManifestFromArchive,
1122
+ nt as generateResourceFromArchive,
1123
+ A as getArchiveOpfInfo,
1091
1124
  we as getUriBasePath,
1092
- w as getUriBasename,
1093
- H as removeTrailingSlash,
1094
- O as sortByTitleComparator
1125
+ v as getUriBasename,
1126
+ _ as removeTrailingSlash,
1127
+ j as sortByTitleComparator
1095
1128
  };
1096
1129
  //# sourceMappingURL=index.js.map