@prose-reader/streamer 1.181.0 → 1.185.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 +34 -34
- package/dist/index.umd.cjs +3 -3
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import $, { XmlDocument as de, XmlTextNode as le, XmlElement as
|
|
2
|
-
import { urlJoin as k, isXmlBasedMimeType as
|
|
1
|
+
import $, { XmlDocument as de, XmlTextNode as le, XmlElement as pe } from "xmldoc";
|
|
2
|
+
import { urlJoin as k, isXmlBasedMimeType as fe, parseContentType as ue, detectMimeTypeFromName as O } from "@prose-reader/shared";
|
|
3
3
|
import { Subject as R, mergeMap as M, EMPTY as _, pairwise as me, filter as j, startWith as he, from as P, tap as D, catchError as I, switchMap as W, merge as q, takeUntil as ge, map as F, ignoreElements as ye, first as be, BehaviorSubject as we, distinctUntilChanged as ve, shareReplay as $e, NEVER as G, timer as xe, finalize as K, of as J, lastValueFrom as Y } from "rxjs";
|
|
4
4
|
let T = !1;
|
|
5
5
|
const w = {
|
|
@@ -84,7 +84,7 @@ const w = {
|
|
|
84
84
|
};
|
|
85
85
|
}
|
|
86
86
|
return i;
|
|
87
|
-
}, ee = (e) => e ? e.children.map((t) => t instanceof le ? t.text : t instanceof
|
|
87
|
+
}, ee = (e) => e ? e.children.map((t) => t instanceof le ? t.text : t instanceof pe ? ee(t) : "").join("").trim() : "", A = (e) => e.substring(e.lastIndexOf("/") + 1) || e, Q = (e) => e.endsWith("/") ? e.slice(0, -1) : e, ke = (e) => {
|
|
88
88
|
const t = e.lastIndexOf("/");
|
|
89
89
|
return t >= 0 ? e.substring(0, t) : "";
|
|
90
90
|
}, te = (e, { basePath: t, baseUrl: i }) => {
|
|
@@ -198,7 +198,7 @@ const w = {
|
|
|
198
198
|
const { data: t, basePath: i } = N(e) || {}, s = await (t == null ? void 0 : t.string());
|
|
199
199
|
if (!s) return [];
|
|
200
200
|
const n = new $.XmlDocument(s), r = n.childNamed("manifest"), o = n.childNamed("spine"), a = o == null ? void 0 : o.childrenNamed("itemref").map((d) => d.attr.idref), c = (r == null ? void 0 : r.childrenNamed("item").filter((d) => a.includes(d.attr.id || ""))) || [];
|
|
201
|
-
return e.files.filter((d) => c.find((
|
|
201
|
+
return e.files.filter((d) => c.find((p) => i ? `${i}/${p.attr.href}` === d.uri : `${p.attr.href}` === d.uri));
|
|
202
202
|
}, je = (e, t, i) => {
|
|
203
203
|
const s = e.attr.href || "", n = i == null ? void 0 : i(e);
|
|
204
204
|
return {
|
|
@@ -217,7 +217,7 @@ const w = {
|
|
|
217
217
|
return i;
|
|
218
218
|
const r = await s.string();
|
|
219
219
|
w.log("data", r);
|
|
220
|
-
const o = new $.XmlDocument(r), a = await Ce(o, e, { baseUrl: t }) || [], c = o.childNamed("metadata"), l = o.childNamed("manifest"), d = o.childNamed("spine"),
|
|
220
|
+
const o = new $.XmlDocument(r), a = await Ce(o, e, { baseUrl: t }) || [], c = o.childNamed("metadata"), l = o.childNamed("manifest"), d = o.childNamed("spine"), p = o.childNamed("guide"), u = c == null ? void 0 : c.childNamed("dc:title"), g = (c == null ? void 0 : c.childrenNamed("meta")) || [], b = g.find(
|
|
221
221
|
(m) => m.attr.property === "rendition:layout"
|
|
222
222
|
), h = g.find(
|
|
223
223
|
(m) => m.attr.property === "rendition:flow"
|
|
@@ -242,23 +242,23 @@ const w = {
|
|
|
242
242
|
*/
|
|
243
243
|
spineItems: (d == null ? void 0 : d.childrenNamed("itemref").map((m, L) => {
|
|
244
244
|
var V, E;
|
|
245
|
-
const
|
|
245
|
+
const f = l == null ? void 0 : l.childrenNamed("item").find((C) => C.attr.id === (m == null ? void 0 : m.attr.idref)), U = (f == null ? void 0 : f.attr.href) || "", ce = ((V = e.files.find((C) => C.uri.endsWith(U))) == null ? void 0 : V.size) || 0, B = t || (/^https?:\/\//.test(U) ? "" : "file://"), H = Re(m);
|
|
246
246
|
return {
|
|
247
247
|
...H,
|
|
248
|
-
id: (
|
|
248
|
+
id: (f == null ? void 0 : f.attr.id) || "",
|
|
249
249
|
index: L,
|
|
250
|
-
href: (E =
|
|
250
|
+
href: (E = f == null ? void 0 : f.attr.href) != null && E.startsWith("https://") ? f == null ? void 0 : f.attr.href : n ? `${B}${n}/${f == null ? void 0 : f.attr.href}` : `${B}${f == null ? void 0 : f.attr.href}`,
|
|
251
251
|
renditionLayout: H.renditionLayout ?? y,
|
|
252
252
|
progressionWeight: ce / ae,
|
|
253
253
|
// size: itemSize
|
|
254
|
-
mediaType:
|
|
254
|
+
mediaType: f == null ? void 0 : f.attr["media-type"]
|
|
255
255
|
};
|
|
256
256
|
})) || [],
|
|
257
257
|
items: se(o, e, (m) => {
|
|
258
258
|
const L = m.attr.href || "";
|
|
259
259
|
return /^https?:\/\//.test(L) ? "" : t || "file://";
|
|
260
260
|
}),
|
|
261
|
-
guide:
|
|
261
|
+
guide: p == null ? void 0 : p.childrenNamed("reference").map((m) => ({
|
|
262
262
|
href: m.attr.href || "",
|
|
263
263
|
title: m.attr.title || "",
|
|
264
264
|
type: m.attr.type
|
|
@@ -457,7 +457,7 @@ const w = {
|
|
|
457
457
|
const t = (i = e.descendantWithPath("head")) == null ? void 0 : i.childrenNamed("meta").find((s) => s.attr.name === "viewport");
|
|
458
458
|
return !!(t && t.attr.name === "viewport");
|
|
459
459
|
}, _e = (e) => e.reduce(async (t, i) => {
|
|
460
|
-
if (!await t || !
|
|
460
|
+
if (!await t || !fe({
|
|
461
461
|
mimeType: i.encodingFormat,
|
|
462
462
|
uri: i.uri
|
|
463
463
|
}))
|
|
@@ -489,7 +489,7 @@ const w = {
|
|
|
489
489
|
if (i.nav) return i;
|
|
490
490
|
const s = [...e.files].sort(
|
|
491
491
|
(a, c) => z(a.uri, c.uri)
|
|
492
|
-
), n = Object.values(s), r = (a, c, l, d,
|
|
492
|
+
), n = Object.values(s), r = (a, c, l, d, p) => {
|
|
493
493
|
const u = a.find((h) => h.title === c), [g, ...b] = l;
|
|
494
494
|
return u ? g ? [
|
|
495
495
|
...a.filter((x) => x !== u),
|
|
@@ -502,15 +502,15 @@ const w = {
|
|
|
502
502
|
g,
|
|
503
503
|
b,
|
|
504
504
|
d,
|
|
505
|
-
|
|
505
|
+
p
|
|
506
506
|
)
|
|
507
507
|
]
|
|
508
508
|
}
|
|
509
|
-
] : u.path.split("/").length >
|
|
509
|
+
] : u.path.split("/").length > p.split("/").length ? [
|
|
510
510
|
...a.filter((x) => x !== u),
|
|
511
511
|
{
|
|
512
512
|
...u,
|
|
513
|
-
path:
|
|
513
|
+
path: p,
|
|
514
514
|
href: d
|
|
515
515
|
}
|
|
516
516
|
] : a : g ? [
|
|
@@ -521,10 +521,10 @@ const w = {
|
|
|
521
521
|
g,
|
|
522
522
|
b,
|
|
523
523
|
d,
|
|
524
|
-
|
|
524
|
+
p
|
|
525
525
|
),
|
|
526
526
|
href: d,
|
|
527
|
-
path:
|
|
527
|
+
path: p,
|
|
528
528
|
title: c
|
|
529
529
|
}
|
|
530
530
|
] : [
|
|
@@ -532,17 +532,17 @@ const w = {
|
|
|
532
532
|
{
|
|
533
533
|
contents: [],
|
|
534
534
|
href: d,
|
|
535
|
-
path:
|
|
535
|
+
path: p,
|
|
536
536
|
title: c
|
|
537
537
|
}
|
|
538
538
|
];
|
|
539
539
|
}, o = n.reduce(
|
|
540
540
|
(a, c) => {
|
|
541
541
|
if (c.dir) return a;
|
|
542
|
-
const d = c.uri.split("/").slice(0, -1), [
|
|
543
|
-
if (
|
|
542
|
+
const d = c.uri.split("/").slice(0, -1), [p, ...u] = d;
|
|
543
|
+
if (p) {
|
|
544
544
|
const g = k(t, encodeURI(c.uri)).replace(/\/$/, ""), b = c.uri.replace(/\/$/, "");
|
|
545
|
-
return r(a,
|
|
545
|
+
return r(a, p, u, g, b);
|
|
546
546
|
}
|
|
547
547
|
return a;
|
|
548
548
|
},
|
|
@@ -743,7 +743,7 @@ const w = {
|
|
|
743
743
|
}))
|
|
744
744
|
};
|
|
745
745
|
return w.log("Generated archive", s), s;
|
|
746
|
-
},
|
|
746
|
+
}, pt = async (e, { orderByAlpha: t, name: i } = {}) => {
|
|
747
747
|
let s = e;
|
|
748
748
|
return t && (s = s.slice().sort((n, r) => z(n.name, r.name))), {
|
|
749
749
|
filename: i || "",
|
|
@@ -763,7 +763,7 @@ const w = {
|
|
|
763
763
|
})),
|
|
764
764
|
close: () => Promise.resolve()
|
|
765
765
|
};
|
|
766
|
-
},
|
|
766
|
+
}, ft = ({
|
|
767
767
|
enableReport: e
|
|
768
768
|
} = {}) => {
|
|
769
769
|
w.enable(!!e);
|
|
@@ -808,11 +808,11 @@ const tt = ({
|
|
|
808
808
|
M((l) => {
|
|
809
809
|
const d = r[l];
|
|
810
810
|
if (!d || d.state.status !== "idle") return _;
|
|
811
|
-
let
|
|
811
|
+
let p = !1;
|
|
812
812
|
const u = (y) => {
|
|
813
813
|
var S;
|
|
814
814
|
const v = r[y];
|
|
815
|
-
delete r[y],
|
|
815
|
+
delete r[y], p || ((S = v == null ? void 0 : v.state.archive) == null || S.close(), p = !0);
|
|
816
816
|
};
|
|
817
817
|
d.update({
|
|
818
818
|
status: "loading"
|
|
@@ -849,20 +849,20 @@ const tt = ({
|
|
|
849
849
|
ge(s)
|
|
850
850
|
), a = (l) => {
|
|
851
851
|
let d = !1;
|
|
852
|
-
const
|
|
853
|
-
r[l] =
|
|
854
|
-
locks:
|
|
852
|
+
const p = r[l] ?? new et(t);
|
|
853
|
+
r[l] = p, p.update({
|
|
854
|
+
locks: p.state.locks + 1
|
|
855
855
|
});
|
|
856
856
|
const u = () => {
|
|
857
|
-
d || (d = !0,
|
|
858
|
-
locks:
|
|
857
|
+
d || (d = !0, p.update({
|
|
858
|
+
locks: p.state.locks - 1
|
|
859
859
|
}));
|
|
860
860
|
};
|
|
861
861
|
i.next(l);
|
|
862
|
-
const g =
|
|
862
|
+
const g = p.state$.pipe(
|
|
863
863
|
F(({ archive: h }) => h),
|
|
864
864
|
j((h) => !!h)
|
|
865
|
-
), b =
|
|
865
|
+
), b = p.state$.pipe(
|
|
866
866
|
D(({ error: h }) => {
|
|
867
867
|
if (h)
|
|
868
868
|
throw h;
|
|
@@ -982,8 +982,8 @@ class ut extends nt {
|
|
|
982
982
|
export {
|
|
983
983
|
ut as ServiceWorkerStreamer,
|
|
984
984
|
nt as Streamer,
|
|
985
|
-
|
|
986
|
-
|
|
985
|
+
ft as configure,
|
|
986
|
+
pt as createArchiveFromArrayBufferList,
|
|
987
987
|
dt as createArchiveFromJszip,
|
|
988
988
|
lt as createArchiveFromLibArchive,
|
|
989
989
|
ct as createArchiveFromText,
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
(function(g,b){typeof exports=="object"&&typeof module<"u"?b(exports,require("xmldoc"),require("@prose-reader/shared"),require("rxjs")):typeof define=="function"&&define.amd?define(["exports","xmldoc","@prose-reader/shared","rxjs"],b):(g=typeof globalThis<"u"?globalThis:g||self,b(g["prose-streamer"]={},g.xmldoc,g.shared,g.rxjs))})(this,function(g,b,F,c){"use strict";let N=!1;const A={enable:e=>{N=e},log:(...e)=>{N&&console.log("[prose-reader-streamer]",...e)},warn:(...e)=>{N&&console.warn("[prose-reader-streamer]",...e)},error:(...e)=>{console.error(...e)},time:e=>{N&&console.time(`[prose-reader-streamer] [metric] ${e}`)},timeEnd:e=>{N&&console.timeEnd(`[prose-reader-streamer] [metric] ${e}`)},metric:(e,t=1/0)=>{const o=typeof e=="number"?e:e.duration;N&&(e.duration<=t?console.log("[prose-reader-streamer] [metric] ",`${e.name} took ${o}ms`):console.warn("[prose-reader-streamer] [metric] ",`${e.name} took ${e.duration}ms which is above the ${t}ms target for this function`))},measurePerformance:(e,t,o)=>(...i)=>{const n=performance.now(),r=o(...i);if(r!=null&&r.then)return r.then(s=>{const d=performance.now();return A.metric({name:e,duration:d-n},t),s});const a=performance.now();return A.metric({name:e,duration:a-n},t),r}},K=e=>{var o;const t=(o=e.descendantWithPath("head"))==null?void 0:o.childrenNamed("meta").find(i=>i.attr.name==="calibre:cover");return!!(t&&t.attr.name==="calibre:cover")},Y=e=>{var t,o,i;return(i=(o=(t=e.descendantWithPath("body"))==null?void 0:t.descendantWithPath("div"))==null?void 0:o.childrenNamed("svg"))==null?void 0:i.find(n=>n.attr.width==="100%"&&n.attr.preserveAspectRatio==="none")},Q=({archive:e,resourcePath:t})=>async o=>{const i=Object.values(e.files).find(n=>n.uri===t);if(i!=null&&i.basename.endsWith(".xhtml")){const n=o.body??await i.string(),r=new b.XmlDocument(n);if(K(r)){const a=Y(r);return a&&delete a.attr.preserveAspectRatio,{...o,body:r==null?void 0:r.toString()}}}return o},Z=({archive:e,resourcePath:t})=>async o=>Q({archive:e,resourcePath:t})(o),j=({archive:e,resourcePath:t})=>async o=>{const i=Object.values(e.files).find(n=>n.uri===t);if(i!=null&&i.basename.endsWith(".css")){const r=(o.body??await i.string()).replaceAll("-webkit-writing-mode","writing-mode");return{...o,body:r}}return o},R=e=>e?e.children.map(t=>t instanceof b.XmlTextNode?t.text:t instanceof b.XmlElement?R(t):"").join("").trim():"",L=e=>e.substring(e.lastIndexOf("/")+1)||e,M=e=>e.endsWith("/")?e.slice(0,-1):e,ee=e=>{const t=e.lastIndexOf("/");return t>=0?e.substring(0,t):""},D=(e,{basePath:t,baseUrl:o})=>{const i={contents:[],path:"",href:"",title:""};let n=e.childNamed("span")||e.childNamed("a");i.title=((n==null?void 0:n.attr.title)||(n==null?void 0:n.val.trim())||R(n))??"";let r=n==null?void 0:n.name;r!=="a"&&(n=e.descendantWithPath(`${r}.a`),n&&(r=n.name.toLowerCase())),r==="a"&&(n!=null&&n.attr.href)&&(i.path=F.urlJoin(t,n.attr.href),i.href=F.urlJoin(o,t,n.attr.href));const a=e.childNamed("ol");if(a){const s=a.childrenNamed("li");s&&s.length>0&&(i.contents=s.map(d=>D(d,{basePath:t,baseUrl:o})))}return i},te=(e,{basePath:t,baseUrl:o})=>{var r,a;const i=[];let n;return e.descendantWithPath("body.nav.ol")?n=(r=e.descendantWithPath("body.nav.ol"))==null?void 0:r.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(s=>s.name==="li").forEach(s=>i.push(D(s,{basePath:t,baseUrl:o}))),i},ne=async(e,t,{baseUrl:o})=>{var n;const i=(n=e.childNamed("manifest"))==null?void 0:n.childrenNamed("item").find(r=>r.attr.properties==="nav");if(i){const r=Object.values(t.files).find(a=>a.uri.endsWith(i.attr.href||""));if(r){const a=new b.XmlDocument(await r.string()),s=ee(r.uri);return te(a,{basePath:s,baseUrl:o})}}},O=(e,{opfBasePath:t,baseUrl:o,prefix:i})=>{var s,d;const n=((s=e==null?void 0:e.childNamed(`${i}content`))==null?void 0:s.attr.src)||"",r={title:((d=e==null?void 0:e.descendantWithPath(`${i}navLabel.${i}text`))==null?void 0:d.val)||"",path:F.urlJoin(t,n),href:F.urlJoin(o,t,n),contents:[]},a=e.childrenNamed(`${i}navPoint`);return a&&a.length>0&&(r.contents=a.map(f=>O(f,{opfBasePath:t,baseUrl:o,prefix:i}))),r},re=(e,{opfBasePath:t,baseUrl:o})=>{var a;const i=[],n=e.name;let r="";return n.indexOf(":")!==-1&&(r=`${n.split(":")[0]}:`),(a=e.childNamed(`${r}navMap`))==null||a.childrenNamed(`${r}navPoint`).forEach(s=>i.push(O(s,{opfBasePath:t,baseUrl:o,prefix:r}))),i},ie=async({opfData:e,opfBasePath:t,baseUrl:o,archive:i})=>{var a;const n=e.childNamed("spine"),r=n==null?void 0:n.attr.toc;if(r){const s=(a=e.childNamed("manifest"))==null?void 0:a.childrenNamed("item").find(d=>d.attr.id===r);if(s){const d=`${t}${t===""?"":"/"}${s.attr.href}`,f=Object.values(i.files).find(l=>l.uri.endsWith(d));if(f){const l=new b.XmlDocument(await f.string());return re(l,{opfBasePath:t,baseUrl:o})}}}},oe=async(e,t,{baseUrl:o})=>{const{basePath:i}=W(t)||{},n=await ie({opfData:e,opfBasePath:i,archive:t,baseUrl:o});return n||await ne(e,t,{baseUrl:o})},ae=e=>{var i;const t=((i=e.attr.properties)==null?void 0:i.split(" "))||[];let o;return t.find(n=>n==="rendition:layout-reflowable")&&(o="reflowable"),t.find(n=>n==="rendition:layout-pre-paginated")&&(o="pre-paginated"),{renditionLayout:o,pageSpreadLeft:t.some(n=>n==="page-spread-left")||void 0,pageSpreadRight:t.some(n=>n==="page-spread-right")||void 0}},W=e=>{const o=Object.values(e.files).filter(i=>!i.dir).find(i=>i.uri.endsWith(".opf"));return{data:o,basePath:(o==null?void 0:o.uri.substring(0,o.uri.lastIndexOf("/")))||""}},z=async({archive:e})=>{const{data:t,basePath:o}=W(e)||{},i=await(t==null?void 0:t.string());if(!i)return[];const n=new b.XmlDocument(i),r=n.childNamed("manifest"),a=n.childNamed("spine"),s=a==null?void 0:a.childrenNamed("itemref").map(l=>l.attr.idref),d=(r==null?void 0:r.childrenNamed("item").filter(l=>s.includes(l.attr.id||"")))||[];return e.files.filter(l=>d.find(u=>o?`${o}/${u.attr.href}`===l.uri:`${u.attr.href}`===l.uri))},se=(e,t,o)=>{const i=e.attr.href||"",n=o==null?void 0:o(e);return{href:t?`${n}${t}/${i}`:`${n}${i}`,id:e.attr.id||"",mediaType:e.attr["media-type"]}},X=(e,t,o)=>{var r;const i=e.childNamed("manifest"),{basePath:n}=W(t)||{};return((r=i==null?void 0:i.childrenNamed("item"))==null?void 0:r.map(a=>se(a,n,o)))||[]},ce=({archive:e,baseUrl:t})=>async o=>{var H;const{data:i,basePath:n}=W(e)||{};if(!i)return o;const r=await i.string();A.log("data",r);const a=new b.XmlDocument(r),s=await oe(a,e,{baseUrl:t})||[],d=a.childNamed("metadata"),f=a.childNamed("manifest"),l=a.childNamed("spine"),u=a.childNamed("guide"),m=d==null?void 0:d.childNamed("dc:title"),w=(d==null?void 0:d.childrenNamed("meta"))||[],$=w.find(h=>h.attr.property==="rendition:layout"),y=w.find(h=>h.attr.property==="rendition:flow"),S=w.find(h=>h.attr.property==="rendition:spread"),v=$==null?void 0:$.val,T=y==null?void 0:y.val,P=S==null?void 0:S.val,Re=(m==null?void 0:m.val)||((H=e.files.find(({dir:h})=>h))==null?void 0:H.basename)||"",Me=l==null?void 0:l.attr["page-progression-direction"],De=(await z({archive:e})).reduce((h,k)=>k.size+h,0);return{filename:e.filename,nav:{toc:s},renditionLayout:v,renditionFlow:T||"auto",renditionSpread:P,title:Re,readingDirection:Me||"ltr",spineItems:(l==null?void 0:l.childrenNamed("itemref").map((h,k)=>{var _,G;const p=f==null?void 0:f.childrenNamed("item").find(C=>C.attr.id===(h==null?void 0:h.attr.idref)),J=(p==null?void 0:p.attr.href)||"",Oe=((_=e.files.find(C=>C.uri.endsWith(J)))==null?void 0:_.size)||0,q=t||(/^https?:\/\//.test(J)?"":"file://"),x=ae(h);return{...x,id:(p==null?void 0:p.attr.id)||"",index:k,href:(G=p==null?void 0:p.attr.href)!=null&&G.startsWith("https://")?p==null?void 0:p.attr.href:n?`${q}${n}/${p==null?void 0:p.attr.href}`:`${q}${p==null?void 0:p.attr.href}`,renditionLayout:x.renditionLayout??v,progressionWeight:Oe/De,mediaType:p==null?void 0:p.attr["media-type"]}}))||[],items:X(a,e,h=>{const k=h.attr.href||"";return/^https?:\/\//.test(k)?"":t||"file://"}),guide:u==null?void 0:u.childrenNamed("reference").map(h=>({href:h.attr.href||"",title:h.attr.title||"",type:h.attr.type}))}},de=async(e,t)=>{var n,r,a;const i=await((n=W(e).data)==null?void 0:n.string());if(i){const s=new b.XmlDocument(i),d=X(s,e,()=>"");if((r=d.find(l=>t.endsWith(l.href)))==null?void 0:r.mediaType)return{mediaType:(a=d.find(l=>t.endsWith(l.href)))==null?void 0:a.mediaType}}return{mediaType:le(t)}},le=e=>{if(e.endsWith(".css"))return"text/css; charset=UTF-8";if(e.endsWith(".jpg"))return"image/jpg";if(e.endsWith(".xhtml"))return"application/xhtml+xml";if(e.endsWith(".mp4"))return"video/mp4";if(e.endsWith(".svg"))return"image/svg+xml"},fe=({archive:e,resourcePath:t})=>async o=>{const i=Object.values(e.files).find(r=>r.uri===t);if(!i)return o;const n=await de(e,t);return{...o,params:{...o.params,...(i==null?void 0:i.encodingFormat)&&{contentType:i.encodingFormat},...n.mediaType&&{contentType:n.mediaType}}}},U=["div","span","p","a","li","ul","ol","h1","h2","h3","h4","h5","h6","table","tr","td","th","thead","tbody","tfoot","section","article","header","footer","nav","aside","main","figure","figcaption","blockquote","pre","code","form","textarea","select","option","button","label","fieldset","legend","caption","dl","dt","dd","iframe","video","audio","canvas","script","style"],ue=({archive:e,resourcePath:t})=>async o=>{const i=Object.values(e.files).find(n=>n.uri===t);if(i!=null&&i.basename.endsWith(".xhtml")){const n=o.body??await i.string();if(!new RegExp(`<(${U.join("|")})[\\s/>]`,"i").test(n))return o;const a=new RegExp(`<(${U.join("|")})(\\s[^>]*)?\\s*/>`,"gi"),s=n.replace(a,(d,f,l="")=>`<${f} ${l.trim()}></${f}>`);return{...o,body:s}}return o},B=async(e,t)=>{const o=Object.values(e.files).find(r=>r.uri===t);if(!o)throw new Error("no file found");const i={params:{}},n=[fe({archive:e,resourcePath:t}),ue({archive:e,resourcePath:t}),j({archive:e,resourcePath:t}),Z({archive:e,resourcePath:t})];try{const r=await n.reduce(async(a,s)=>await s(await a),Promise.resolve(i));return A.log("Generated resource",t,r),{...r,body:r.body??await o.blob()}}catch(r){throw A.error(r),r}},pe=({archive:e,baseUrl:t})=>async()=>{var i;const o=Object.values(e.files).filter(n=>!n.dir);return{filename:e.filename,title:((i=e.files.find(({dir:n})=>n))==null?void 0:i.basename.replace(/\/$/,""))||"",renditionLayout:void 0,renditionSpread:"auto",readingDirection:"ltr",spineItems:o.filter(n=>!n.basename.endsWith(".db")).map((n,r)=>({id:`${r}.${n.basename}`,index:r,href:encodeURI(`${t}${n.uri}`),renditionLayout:void 0,progressionWeight:1/o.length,pageSpreadLeft:void 0,pageSpreadRight:void 0,mediaType:n.encodingFormat})),items:o.map((n,r)=>({id:`${r}.${n.basename}`,href:encodeURI(`${t}${n.uri}`)}))}},me=async e=>{const t={renditionLayout:void 0};return await Promise.all(e.files.map(async o=>{var i,n;if(o.uri.endsWith("com.kobobooks.display-options.xml")){const a=(i=new b.XmlDocument(await o.string()).childNamed("platform"))==null?void 0:i.childNamed("option");((n=a==null?void 0:a.attr)==null?void 0:n.name)==="fixed-layout"&&a.val==="true"&&(t.renditionLayout="pre-paginated")}})),t},he=({archive:e})=>async t=>{const o=await me(e);return{...t,renditionLayout:t.renditionLayout??o.renditionLayout}},ge=e=>{var o;const t=(o=e.descendantWithPath("head"))==null?void 0:o.childrenNamed("meta").find(i=>i.attr.name==="viewport");return!!(t&&t.attr.name==="viewport")},ye=e=>e.reduce(async(t,o)=>{if(!await t||!F.isXmlBasedMimeType({mimeType:o.encodingFormat,uri:o.uri}))return!1;const n=await o.string();return n?ge(new b.XmlDocument(n)):!1},Promise.resolve(!0)),be=({archive:e})=>async t=>{if(t.renditionLayout==="reflowable"&&t.spineItems.every(i=>i.renditionLayout==="reflowable")){const i=await z({archive:e});if(await ye(i))return{...t,spineItems:t.spineItems.map(r=>({...r,renditionLayout:"pre-paginated"})),renditionLayout:"pre-paginated"}}return t},I=(e,t)=>{var n;const o=e.split(/(\d+)/),i=t.split(/(\d+)/);for(let r=0,a=o.length;r<a;r++)if(o[r]!==i[r])return(n=o[r])!=null&&n.match(/\d/)?+(o[r]||"")-+(i[r]||""):(o[r]||"").localeCompare(i[r]||"");return 1},we=({archive:e,baseUrl:t})=>async o=>{if(o.nav)return o;const i=[...e.files].sort((s,d)=>I(s.uri,d.uri)),n=Object.values(i),r=(s,d,f,l,u)=>{const m=s.find(y=>y.title===d),[w,...$]=f;return m?w?[...s.filter(S=>S!==m),{...m,contents:[...m.contents,...r(m.contents,w,$,l,u)]}]:m.path.split("/").length>u.split("/").length?[...s.filter(S=>S!==m),{...m,path:u,href:l}]:s:w?[...s,{contents:r([],w,$,l,u),href:l,path:u,title:d}]:[...s,{contents:[],href:l,path:u,title:d}]},a=n.reduce((s,d)=>{if(d.dir)return s;const l=d.uri.split("/").slice(0,-1),[u,...m]=l;if(u){const w=F.urlJoin(t,encodeURI(d.uri)).replace(/\/$/,""),$=d.uri.replace(/\/$/,"");return r(s,u,m,w,$)}return s},[]);return a.length===0?o:{...o,nav:{toc:a}}},ve=({archive:e})=>async t=>{var r;const o=e.files.find(a=>a.basename.toLowerCase()==="comicinfo.xml");if(!o)return t;const i={...t,spineItems:t.spineItems.filter(a=>!a.id.toLowerCase().endsWith("comicinfo.xml")).map((a,s,d)=>({...a,progressionWeight:1/d.length}))},n=await o.string();try{const s=((r=new b.XmlDocument(n).childNamed("Manga"))==null?void 0:r.val)||"unknown";return{...i,readingDirection:s==="YesAndRightToLeft"?"rtl":"ltr"}}catch(a){return console.error(`Unable to parse comicinfo.xml for content
|
|
1
|
+
(function(g,b){typeof exports=="object"&&typeof module<"u"?b(exports,require("xmldoc"),require("@prose-reader/shared"),require("rxjs")):typeof define=="function"&&define.amd?define(["exports","xmldoc","@prose-reader/shared","rxjs"],b):(g=typeof globalThis<"u"?globalThis:g||self,b(g["prose-streamer"]={},g.xmldoc,g.shared,g.rxjs))})(this,function(g,b,F,c){"use strict";let N=!1;const A={enable:e=>{N=e},log:(...e)=>{N&&console.log("[prose-reader-streamer]",...e)},warn:(...e)=>{N&&console.warn("[prose-reader-streamer]",...e)},error:(...e)=>{console.error(...e)},time:e=>{N&&console.time(`[prose-reader-streamer] [metric] ${e}`)},timeEnd:e=>{N&&console.timeEnd(`[prose-reader-streamer] [metric] ${e}`)},metric:(e,t=1/0)=>{const o=typeof e=="number"?e:e.duration;N&&(e.duration<=t?console.log("[prose-reader-streamer] [metric] ",`${e.name} took ${o}ms`):console.warn("[prose-reader-streamer] [metric] ",`${e.name} took ${e.duration}ms which is above the ${t}ms target for this function`))},measurePerformance:(e,t,o)=>(...i)=>{const n=performance.now(),r=o(...i);if(r!=null&&r.then)return r.then(s=>{const d=performance.now();return A.metric({name:e,duration:d-n},t),s});const a=performance.now();return A.metric({name:e,duration:a-n},t),r}},K=e=>{var o;const t=(o=e.descendantWithPath("head"))==null?void 0:o.childrenNamed("meta").find(i=>i.attr.name==="calibre:cover");return!!(t&&t.attr.name==="calibre:cover")},Y=e=>{var t,o,i;return(i=(o=(t=e.descendantWithPath("body"))==null?void 0:t.descendantWithPath("div"))==null?void 0:o.childrenNamed("svg"))==null?void 0:i.find(n=>n.attr.width==="100%"&&n.attr.preserveAspectRatio==="none")},Q=({archive:e,resourcePath:t})=>async o=>{const i=Object.values(e.files).find(n=>n.uri===t);if(i!=null&&i.basename.endsWith(".xhtml")){const n=o.body??await i.string(),r=new b.XmlDocument(n);if(K(r)){const a=Y(r);return a&&delete a.attr.preserveAspectRatio,{...o,body:r==null?void 0:r.toString()}}}return o},Z=({archive:e,resourcePath:t})=>async o=>Q({archive:e,resourcePath:t})(o),j=({archive:e,resourcePath:t})=>async o=>{const i=Object.values(e.files).find(n=>n.uri===t);if(i!=null&&i.basename.endsWith(".css")){const r=(o.body??await i.string()).replaceAll("-webkit-writing-mode","writing-mode");return{...o,body:r}}return o},R=e=>e?e.children.map(t=>t instanceof b.XmlTextNode?t.text:t instanceof b.XmlElement?R(t):"").join("").trim():"",L=e=>e.substring(e.lastIndexOf("/")+1)||e,M=e=>e.endsWith("/")?e.slice(0,-1):e,ee=e=>{const t=e.lastIndexOf("/");return t>=0?e.substring(0,t):""},D=(e,{basePath:t,baseUrl:o})=>{const i={contents:[],path:"",href:"",title:""};let n=e.childNamed("span")||e.childNamed("a");i.title=((n==null?void 0:n.attr.title)||(n==null?void 0:n.val.trim())||R(n))??"";let r=n==null?void 0:n.name;r!=="a"&&(n=e.descendantWithPath(`${r}.a`),n&&(r=n.name.toLowerCase())),r==="a"&&(n!=null&&n.attr.href)&&(i.path=F.urlJoin(t,n.attr.href),i.href=F.urlJoin(o,t,n.attr.href));const a=e.childNamed("ol");if(a){const s=a.childrenNamed("li");s&&s.length>0&&(i.contents=s.map(d=>D(d,{basePath:t,baseUrl:o})))}return i},te=(e,{basePath:t,baseUrl:o})=>{var r,a;const i=[];let n;return e.descendantWithPath("body.nav.ol")?n=(r=e.descendantWithPath("body.nav.ol"))==null?void 0:r.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(s=>s.name==="li").forEach(s=>i.push(D(s,{basePath:t,baseUrl:o}))),i},ne=async(e,t,{baseUrl:o})=>{var n;const i=(n=e.childNamed("manifest"))==null?void 0:n.childrenNamed("item").find(r=>r.attr.properties==="nav");if(i){const r=Object.values(t.files).find(a=>a.uri.endsWith(i.attr.href||""));if(r){const a=new b.XmlDocument(await r.string()),s=ee(r.uri);return te(a,{basePath:s,baseUrl:o})}}},O=(e,{opfBasePath:t,baseUrl:o,prefix:i})=>{var s,d;const n=((s=e==null?void 0:e.childNamed(`${i}content`))==null?void 0:s.attr.src)||"",r={title:((d=e==null?void 0:e.descendantWithPath(`${i}navLabel.${i}text`))==null?void 0:d.val)||"",path:F.urlJoin(t,n),href:F.urlJoin(o,t,n),contents:[]},a=e.childrenNamed(`${i}navPoint`);return a&&a.length>0&&(r.contents=a.map(f=>O(f,{opfBasePath:t,baseUrl:o,prefix:i}))),r},re=(e,{opfBasePath:t,baseUrl:o})=>{var a;const i=[],n=e.name;let r="";return n.indexOf(":")!==-1&&(r=`${n.split(":")[0]}:`),(a=e.childNamed(`${r}navMap`))==null||a.childrenNamed(`${r}navPoint`).forEach(s=>i.push(O(s,{opfBasePath:t,baseUrl:o,prefix:r}))),i},ie=async({opfData:e,opfBasePath:t,baseUrl:o,archive:i})=>{var a;const n=e.childNamed("spine"),r=n==null?void 0:n.attr.toc;if(r){const s=(a=e.childNamed("manifest"))==null?void 0:a.childrenNamed("item").find(d=>d.attr.id===r);if(s){const d=`${t}${t===""?"":"/"}${s.attr.href}`,f=Object.values(i.files).find(l=>l.uri.endsWith(d));if(f){const l=new b.XmlDocument(await f.string());return re(l,{opfBasePath:t,baseUrl:o})}}}},oe=async(e,t,{baseUrl:o})=>{const{basePath:i}=W(t)||{},n=await ie({opfData:e,opfBasePath:i,archive:t,baseUrl:o});return n||await ne(e,t,{baseUrl:o})},ae=e=>{var i;const t=((i=e.attr.properties)==null?void 0:i.split(" "))||[];let o;return t.find(n=>n==="rendition:layout-reflowable")&&(o="reflowable"),t.find(n=>n==="rendition:layout-pre-paginated")&&(o="pre-paginated"),{renditionLayout:o,pageSpreadLeft:t.some(n=>n==="page-spread-left")||void 0,pageSpreadRight:t.some(n=>n==="page-spread-right")||void 0}},W=e=>{const o=Object.values(e.files).filter(i=>!i.dir).find(i=>i.uri.endsWith(".opf"));return{data:o,basePath:(o==null?void 0:o.uri.substring(0,o.uri.lastIndexOf("/")))||""}},z=async({archive:e})=>{const{data:t,basePath:o}=W(e)||{},i=await(t==null?void 0:t.string());if(!i)return[];const n=new b.XmlDocument(i),r=n.childNamed("manifest"),a=n.childNamed("spine"),s=a==null?void 0:a.childrenNamed("itemref").map(l=>l.attr.idref),d=(r==null?void 0:r.childrenNamed("item").filter(l=>s.includes(l.attr.id||"")))||[];return e.files.filter(l=>d.find(p=>o?`${o}/${p.attr.href}`===l.uri:`${p.attr.href}`===l.uri))},se=(e,t,o)=>{const i=e.attr.href||"",n=o==null?void 0:o(e);return{href:t?`${n}${t}/${i}`:`${n}${i}`,id:e.attr.id||"",mediaType:e.attr["media-type"]}},X=(e,t,o)=>{var r;const i=e.childNamed("manifest"),{basePath:n}=W(t)||{};return((r=i==null?void 0:i.childrenNamed("item"))==null?void 0:r.map(a=>se(a,n,o)))||[]},ce=({archive:e,baseUrl:t})=>async o=>{var H;const{data:i,basePath:n}=W(e)||{};if(!i)return o;const r=await i.string();A.log("data",r);const a=new b.XmlDocument(r),s=await oe(a,e,{baseUrl:t})||[],d=a.childNamed("metadata"),f=a.childNamed("manifest"),l=a.childNamed("spine"),p=a.childNamed("guide"),m=d==null?void 0:d.childNamed("dc:title"),w=(d==null?void 0:d.childrenNamed("meta"))||[],$=w.find(h=>h.attr.property==="rendition:layout"),y=w.find(h=>h.attr.property==="rendition:flow"),S=w.find(h=>h.attr.property==="rendition:spread"),v=$==null?void 0:$.val,T=y==null?void 0:y.val,P=S==null?void 0:S.val,Re=(m==null?void 0:m.val)||((H=e.files.find(({dir:h})=>h))==null?void 0:H.basename)||"",Me=l==null?void 0:l.attr["page-progression-direction"],De=(await z({archive:e})).reduce((h,k)=>k.size+h,0);return{filename:e.filename,nav:{toc:s},renditionLayout:v,renditionFlow:T||"auto",renditionSpread:P,title:Re,readingDirection:Me||"ltr",spineItems:(l==null?void 0:l.childrenNamed("itemref").map((h,k)=>{var _,G;const u=f==null?void 0:f.childrenNamed("item").find(C=>C.attr.id===(h==null?void 0:h.attr.idref)),J=(u==null?void 0:u.attr.href)||"",Oe=((_=e.files.find(C=>C.uri.endsWith(J)))==null?void 0:_.size)||0,q=t||(/^https?:\/\//.test(J)?"":"file://"),x=ae(h);return{...x,id:(u==null?void 0:u.attr.id)||"",index:k,href:(G=u==null?void 0:u.attr.href)!=null&&G.startsWith("https://")?u==null?void 0:u.attr.href:n?`${q}${n}/${u==null?void 0:u.attr.href}`:`${q}${u==null?void 0:u.attr.href}`,renditionLayout:x.renditionLayout??v,progressionWeight:Oe/De,mediaType:u==null?void 0:u.attr["media-type"]}}))||[],items:X(a,e,h=>{const k=h.attr.href||"";return/^https?:\/\//.test(k)?"":t||"file://"}),guide:p==null?void 0:p.childrenNamed("reference").map(h=>({href:h.attr.href||"",title:h.attr.title||"",type:h.attr.type}))}},de=async(e,t)=>{var n,r,a;const i=await((n=W(e).data)==null?void 0:n.string());if(i){const s=new b.XmlDocument(i),d=X(s,e,()=>"");if((r=d.find(l=>t.endsWith(l.href)))==null?void 0:r.mediaType)return{mediaType:(a=d.find(l=>t.endsWith(l.href)))==null?void 0:a.mediaType}}return{mediaType:le(t)}},le=e=>{if(e.endsWith(".css"))return"text/css; charset=UTF-8";if(e.endsWith(".jpg"))return"image/jpg";if(e.endsWith(".xhtml"))return"application/xhtml+xml";if(e.endsWith(".mp4"))return"video/mp4";if(e.endsWith(".svg"))return"image/svg+xml"},fe=({archive:e,resourcePath:t})=>async o=>{const i=Object.values(e.files).find(r=>r.uri===t);if(!i)return o;const n=await de(e,t);return{...o,params:{...o.params,...(i==null?void 0:i.encodingFormat)&&{contentType:i.encodingFormat},...n.mediaType&&{contentType:n.mediaType}}}},U=["div","span","p","a","li","ul","ol","h1","h2","h3","h4","h5","h6","table","tr","td","th","thead","tbody","tfoot","section","article","header","footer","nav","aside","main","figure","figcaption","blockquote","pre","code","form","textarea","select","option","button","label","fieldset","legend","caption","dl","dt","dd","iframe","video","audio","canvas","script","style"],pe=({archive:e,resourcePath:t})=>async o=>{const i=Object.values(e.files).find(n=>n.uri===t);if(i!=null&&i.basename.endsWith(".xhtml")){const n=o.body??await i.string();if(!new RegExp(`<(${U.join("|")})[\\s/>]`,"i").test(n))return o;const a=new RegExp(`<(${U.join("|")})(\\s[^>]*)?\\s*/>`,"gi"),s=n.replace(a,(d,f,l="")=>`<${f} ${l.trim()}></${f}>`);return{...o,body:s}}return o},B=async(e,t)=>{const o=Object.values(e.files).find(r=>r.uri===t);if(!o)throw new Error("no file found");const i={params:{}},n=[fe({archive:e,resourcePath:t}),pe({archive:e,resourcePath:t}),j({archive:e,resourcePath:t}),Z({archive:e,resourcePath:t})];try{const r=await n.reduce(async(a,s)=>await s(await a),Promise.resolve(i));return A.log("Generated resource",t,r),{...r,body:r.body??await o.blob()}}catch(r){throw A.error(r),r}},ue=({archive:e,baseUrl:t})=>async()=>{var i;const o=Object.values(e.files).filter(n=>!n.dir);return{filename:e.filename,title:((i=e.files.find(({dir:n})=>n))==null?void 0:i.basename.replace(/\/$/,""))||"",renditionLayout:void 0,renditionSpread:"auto",readingDirection:"ltr",spineItems:o.filter(n=>!n.basename.endsWith(".db")).map((n,r)=>({id:`${r}.${n.basename}`,index:r,href:encodeURI(`${t}${n.uri}`),renditionLayout:void 0,progressionWeight:1/o.length,pageSpreadLeft:void 0,pageSpreadRight:void 0,mediaType:n.encodingFormat})),items:o.map((n,r)=>({id:`${r}.${n.basename}`,href:encodeURI(`${t}${n.uri}`)}))}},me=async e=>{const t={renditionLayout:void 0};return await Promise.all(e.files.map(async o=>{var i,n;if(o.uri.endsWith("com.kobobooks.display-options.xml")){const a=(i=new b.XmlDocument(await o.string()).childNamed("platform"))==null?void 0:i.childNamed("option");((n=a==null?void 0:a.attr)==null?void 0:n.name)==="fixed-layout"&&a.val==="true"&&(t.renditionLayout="pre-paginated")}})),t},he=({archive:e})=>async t=>{const o=await me(e);return{...t,renditionLayout:t.renditionLayout??o.renditionLayout}},ge=e=>{var o;const t=(o=e.descendantWithPath("head"))==null?void 0:o.childrenNamed("meta").find(i=>i.attr.name==="viewport");return!!(t&&t.attr.name==="viewport")},ye=e=>e.reduce(async(t,o)=>{if(!await t||!F.isXmlBasedMimeType({mimeType:o.encodingFormat,uri:o.uri}))return!1;const n=await o.string();return n?ge(new b.XmlDocument(n)):!1},Promise.resolve(!0)),be=({archive:e})=>async t=>{if(t.renditionLayout==="reflowable"&&t.spineItems.every(i=>i.renditionLayout==="reflowable")){const i=await z({archive:e});if(await ye(i))return{...t,spineItems:t.spineItems.map(r=>({...r,renditionLayout:"pre-paginated"})),renditionLayout:"pre-paginated"}}return t},I=(e,t)=>{var n;const o=e.split(/(\d+)/),i=t.split(/(\d+)/);for(let r=0,a=o.length;r<a;r++)if(o[r]!==i[r])return(n=o[r])!=null&&n.match(/\d/)?+(o[r]||"")-+(i[r]||""):(o[r]||"").localeCompare(i[r]||"");return 1},we=({archive:e,baseUrl:t})=>async o=>{if(o.nav)return o;const i=[...e.files].sort((s,d)=>I(s.uri,d.uri)),n=Object.values(i),r=(s,d,f,l,p)=>{const m=s.find(y=>y.title===d),[w,...$]=f;return m?w?[...s.filter(S=>S!==m),{...m,contents:[...m.contents,...r(m.contents,w,$,l,p)]}]:m.path.split("/").length>p.split("/").length?[...s.filter(S=>S!==m),{...m,path:p,href:l}]:s:w?[...s,{contents:r([],w,$,l,p),href:l,path:p,title:d}]:[...s,{contents:[],href:l,path:p,title:d}]},a=n.reduce((s,d)=>{if(d.dir)return s;const l=d.uri.split("/").slice(0,-1),[p,...m]=l;if(p){const w=F.urlJoin(t,encodeURI(d.uri)).replace(/\/$/,""),$=d.uri.replace(/\/$/,"");return r(s,p,m,w,$)}return s},[]);return a.length===0?o:{...o,nav:{toc:a}}},ve=({archive:e})=>async t=>{var r;const o=e.files.find(a=>a.basename.toLowerCase()==="comicinfo.xml");if(!o)return t;const i={...t,spineItems:t.spineItems.filter(a=>!a.id.toLowerCase().endsWith("comicinfo.xml")).map((a,s,d)=>({...a,progressionWeight:1/d.length}))},n=await o.string();try{const s=((r=new b.XmlDocument(n).childNamed("Manga"))==null?void 0:r.val)||"unknown";return{...i,readingDirection:s==="YesAndRightToLeft"?"rtl":"ltr"}}catch(a){return console.error(`Unable to parse comicinfo.xml for content
|
|
2
2
|
`,n),console.error(a),i}},$e=e=>e.files.some(t=>t.basename.endsWith(".opf")),Fe=({archive:e})=>async t=>$e(e)?t:{...t,spineItems:t.spineItems.map(i=>{const n=e.files.find(a=>decodeURI(i.href).endsWith(a.uri)),r=F.parseContentType((n==null?void 0:n.encodingFormat)??"")??F.detectMimeTypeFromName((n==null?void 0:n.basename)??"");return{...i,renditionLayout:r!=null&&r.startsWith("image/")?"pre-paginated":i.renditionLayout}})},Ae=({archive:e})=>async t=>{var n,r;const o=e.files.find(a=>a.basename.toLowerCase()==="com.apple.ibooks.display-options.xml");if(!o)return t;const i=await(await o.blob()).text();try{const s=new b.XmlDocument(i).childNamed("platform"),d=((r=(n=s==null?void 0:s.childrenNamed("option"))==null?void 0:n.find(f=>f.attr.name==="fixed-layout"))==null?void 0:r.val)||"false";return{...t,renditionLayout:d==="true"?"pre-paginated":t.renditionLayout}}catch(a){return console.error(`Unable to parse com.apple.ibooks.display-options.xml for content
|
|
3
|
-
`,i),console.error(a),t}},E=async(e,{baseUrl:t=""}={})=>{const o=[ce({archive:e,baseUrl:t}),ve({archive:e}),Ae({archive:e}),Fe({archive:e}),be({archive:e}),he({archive:e}),we({archive:e,baseUrl:t})];try{const i=
|
|
3
|
+
`,i),console.error(a),t}},E=async(e,{baseUrl:t=""}={})=>{const o=[ce({archive:e,baseUrl:t}),ve({archive:e}),Ae({archive:e}),Fe({archive:e}),be({archive:e}),he({archive:e}),we({archive:e,baseUrl:t})];try{const i=ue({archive:e,baseUrl:t})(),n=await o.reduce(async(r,a)=>await a(await r),i);return A.log("Generated manifest",n),n}catch(i){throw A.error(i),i}},Te=async(e,t)=>{const o=`
|
|
4
4
|
<?xml version="1.0" encoding="UTF-8"?><package xmlns="http://www.idpf.org/2007/opf" version="2.0" unique-identifier="bookid">
|
|
5
5
|
<metadata>
|
|
6
6
|
<meta property="rendition:layout">${t!=null&&t.useRenditionFlow?"reflowable":"pre-paginated"}</meta>
|
|
@@ -30,5 +30,5 @@
|
|
|
30
30
|
<itemref idref="p01" />
|
|
31
31
|
</spine>
|
|
32
32
|
</package>
|
|
33
|
-
`;return{filename:"content.txt",files:[{dir:!1,basename:L("generated.opf"),uri:"generated.opf",blob:async()=>new Blob([i]),string:async()=>i,size:0},{dir:!1,basename:L("p01.txt"),uri:"p01.txt",blob:async()=>typeof e=="string"?new Blob([e]):e,string:async()=>typeof e=="string"?e:e.text(),size:typeof e=="string"?e.length:e.size,encodingFormat:t}],close:()=>Promise.resolve()}},Le=async(e,{orderByAlpha:t,name:o}={})=>{let i=Object.values(e.files);t&&(i=i.slice().sort((r,a)=>I(r.name,a.name)));const n={filename:o||"",files:i.map(r=>({dir:r.dir,basename:L(r.name),uri:r.name,blob:()=>r.async("blob"),string:()=>r.async("string"),...r.internalStream&&{stream:r.internalStream},size:r._data.uncompressedSize})),close:()=>Promise.resolve()};return A.log("Generated archive",n),n},Ne=async(e,{name:t}={})=>{const o=await e.getFilesArray(),i={close:()=>e.close(),filename:t??"",files:o.map(n=>({dir:!1,basename:n.file.name,size:n.file.size,uri:`${n.path}${n.file.name}`,blob:async()=>await n.file.extract(),string:async()=>(await n.file.extract()).text()}))};return A.log("Generated archive",i),i},We=async(e,{orderByAlpha:t,name:o}={})=>{let i=e;return t&&(i=i.slice().sort((n,r)=>I(n.name,r.name))),{filename:o||"",files:i.map(n=>({dir:n.isDir,basename:L(n.name),uri:n.name,blob:async()=>new Blob([await n.data()]),string:async()=>{const r=await n.data();return String.fromCharCode.apply(null,Array.from(new Uint16Array(r)))},size:n.size})),close:()=>Promise.resolve()}},ke=({enableReport:e}={})=>{A.enable(!!e)};class Pe{constructor(t){this.cleanArchiveAfter=t,this.state$=new c.BehaviorSubject({status:"idle",locks:0})}update(t){this.state$.next({...this.state$.getValue(),...t})}get locks$(){return this.state$.pipe(c.map(({locks:t})=>t))}get state(){return this.state$.getValue()}get isUnlocked$(){return this.locks$.pipe(c.map(t=>t<=0),c.distinctUntilChanged(),c.shareReplay())}get overTTL$(){return this.isUnlocked$.pipe(c.switchMap(t=>t?this.cleanArchiveAfter===1/0?c.NEVER:c.timer(this.cleanArchiveAfter):c.NEVER))}}const Ie=({getArchive:e,cleanArchiveAfter:t=5*60*1e3})=>{const o=new c.Subject,i=new c.Subject,n=new c.Subject,r={},a=o.pipe(c.mergeMap(f=>{const l=r[f];if(!l||l.state.status!=="idle")return c.EMPTY;let
|
|
33
|
+
`;return{filename:"content.txt",files:[{dir:!1,basename:L("generated.opf"),uri:"generated.opf",blob:async()=>new Blob([i]),string:async()=>i,size:0},{dir:!1,basename:L("p01.txt"),uri:"p01.txt",blob:async()=>typeof e=="string"?new Blob([e]):e,string:async()=>typeof e=="string"?e:e.text(),size:typeof e=="string"?e.length:e.size,encodingFormat:t}],close:()=>Promise.resolve()}},Le=async(e,{orderByAlpha:t,name:o}={})=>{let i=Object.values(e.files);t&&(i=i.slice().sort((r,a)=>I(r.name,a.name)));const n={filename:o||"",files:i.map(r=>({dir:r.dir,basename:L(r.name),uri:r.name,blob:()=>r.async("blob"),string:()=>r.async("string"),...r.internalStream&&{stream:r.internalStream},size:r._data.uncompressedSize})),close:()=>Promise.resolve()};return A.log("Generated archive",n),n},Ne=async(e,{name:t}={})=>{const o=await e.getFilesArray(),i={close:()=>e.close(),filename:t??"",files:o.map(n=>({dir:!1,basename:n.file.name,size:n.file.size,uri:`${n.path}${n.file.name}`,blob:async()=>await n.file.extract(),string:async()=>(await n.file.extract()).text()}))};return A.log("Generated archive",i),i},We=async(e,{orderByAlpha:t,name:o}={})=>{let i=e;return t&&(i=i.slice().sort((n,r)=>I(n.name,r.name))),{filename:o||"",files:i.map(n=>({dir:n.isDir,basename:L(n.name),uri:n.name,blob:async()=>new Blob([await n.data()]),string:async()=>{const r=await n.data();return String.fromCharCode.apply(null,Array.from(new Uint16Array(r)))},size:n.size})),close:()=>Promise.resolve()}},ke=({enableReport:e}={})=>{A.enable(!!e)};class Pe{constructor(t){this.cleanArchiveAfter=t,this.state$=new c.BehaviorSubject({status:"idle",locks:0})}update(t){this.state$.next({...this.state$.getValue(),...t})}get locks$(){return this.state$.pipe(c.map(({locks:t})=>t))}get state(){return this.state$.getValue()}get isUnlocked$(){return this.locks$.pipe(c.map(t=>t<=0),c.distinctUntilChanged(),c.shareReplay())}get overTTL$(){return this.isUnlocked$.pipe(c.switchMap(t=>t?this.cleanArchiveAfter===1/0?c.NEVER:c.timer(this.cleanArchiveAfter):c.NEVER))}}const Ie=({getArchive:e,cleanArchiveAfter:t=5*60*1e3})=>{const o=new c.Subject,i=new c.Subject,n=new c.Subject,r={},a=o.pipe(c.mergeMap(f=>{const l=r[f];if(!l||l.state.status!=="idle")return c.EMPTY;let p=!1;const m=v=>{var P;const T=r[v];delete r[v],p||((P=T==null?void 0:T.state.archive)==null||P.close(),p=!0)};l.update({status:"loading"});const w=l.locks$,$=l.isUnlocked$,y=w.pipe(c.pairwise(),c.filter(([v,T])=>T>v),c.startWith(!0));return c.from(e(f)).pipe(c.tap(v=>{l.update({archive:v,status:"success"})}),c.catchError(v=>(m(f),l.update({status:"error",error:v}),c.EMPTY)),c.switchMap(()=>{const v=y.pipe(c.switchMap(()=>n),c.switchMap(()=>$),c.filter(T=>T));return c.merge(v,l.overTTL$)}),c.tap(()=>{m(f)}))}),c.takeUntil(i)),s=f=>{let l=!1;const p=r[f]??new Pe(t);r[f]=p,p.update({locks:p.state.locks+1});const m=()=>{l||(l=!0,p.update({locks:p.state.locks-1}))};o.next(f);const w=p.state$.pipe(c.map(({archive:y})=>y),c.filter(y=>!!y)),$=p.state$.pipe(c.tap(({error:y})=>{if(y)throw y}),c.ignoreElements());return c.merge(w,$).pipe(c.first(),c.map(y=>({archive:y,release:m})),c.catchError(y=>{throw m(),y}))},d=()=>{n.next()};return a.subscribe(),{access:s,purge:d,archives:r}};class V{constructor({onError:t,onManifestSuccess:o,...i}){this.onError=n=>(console.error(n),new Response(String(n),{status:500})),this.epubLoader=Ie(i),this.onManifestSuccess=o??(({manifest:n})=>Promise.resolve(n)),this.onError=t??this.onError}prune(){this.epubLoader.purge()}accessArchive(t){return this.lastAccessedKey!==void 0&&this.lastAccessedKey!==t&&this.epubLoader.purge(),this.lastAccessedKey=t,this.epubLoader.access(t)}accessArchiveWithoutLock(t){return this.accessArchive(t).pipe(c.map(({archive:o,release:i})=>(i(),o)))}fetchManifest({key:t,baseUrl:o}){const i=this.accessArchive(t).pipe(c.mergeMap(({archive:n,release:r})=>c.from(E(n,{baseUrl:o})).pipe(c.switchMap(s=>c.from(this.onManifestSuccess({manifest:s,archive:n}))),c.map(s=>new Response(JSON.stringify(s),{status:200})),c.finalize(()=>{r()}))),c.catchError(n=>c.of(this.onError(n))));return c.lastValueFrom(i)}fetchResource({key:t,resourcePath:o}){const i=this.accessArchive(t).pipe(c.mergeMap(({archive:n,release:r})=>{const a=o.replaceAll("file://","");return c.from(B(n,a)).pipe(c.map(d=>new Response(d.body,{status:200,headers:{...d.params.contentType&&{"Content-Type":d.params.contentType}}})),c.finalize(()=>{r()}))}),c.catchError(n=>c.of(this.onError(n))));return c.lastValueFrom(i)}}class Ce extends V{constructor({getUriInfo:t,...o}){super(o),this.getUriInfo=t,this.fetchEventListener=this.fetchEventListener.bind(this)}fetchEventListener(t){try{const o=this.getUriInfo(t);if(!o)return;const i=M(o.baseUrl),n=t.request.url.substring(i.length+1),[r=""]=n.split("/"),a=decodeURIComponent(M(n.substring(r.length+1)));n.endsWith("/manifest")?t.respondWith(this.fetchManifest({key:r,baseUrl:`${i}/${r}/`})):t.respondWith(this.fetchResource({key:r,resourcePath:a}))}catch(o){t.respondWith(new Response(String(o),{status:500}))}}}g.ServiceWorkerStreamer=Ce,g.Streamer=V,g.configure=ke,g.createArchiveFromArrayBufferList=We,g.createArchiveFromJszip=Le,g.createArchiveFromLibArchive=Ne,g.createArchiveFromText=Se,g.createArchiveFromUrls=Te,g.generateManifestFromArchive=E,g.generateResourceFromArchive=B,g.getArchiveOpfInfo=W,Object.defineProperty(g,Symbol.toStringTag,{value:"Module"})});
|
|
34
34
|
//# sourceMappingURL=index.umd.cjs.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prose-reader/streamer",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.185.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.umd.cjs",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -23,14 +23,14 @@
|
|
|
23
23
|
"test:watch": "vitest watch"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@prose-reader/shared": "^1.
|
|
26
|
+
"@prose-reader/shared": "^1.185.0"
|
|
27
27
|
},
|
|
28
28
|
"peerDependencies": {
|
|
29
29
|
"buffer": "^6.0.3",
|
|
30
30
|
"rxjs": "*",
|
|
31
31
|
"xmldoc": "^1.1.2"
|
|
32
32
|
},
|
|
33
|
-
"gitHead": "
|
|
33
|
+
"gitHead": "8a1b7236ea277b81b31801ce31f3771fd2240048",
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"buffer": "^6.0.3",
|
|
36
36
|
"isomorphic-fetch": "^3.0.0",
|