@prose-reader/streamer 1.282.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.
@@ -1,7 +1,7 @@
1
- (function(p,h){typeof exports=="object"&&typeof module<"u"?h(exports,require("@prose-reader/shared"),require("xmldoc"),require("rxjs")):typeof define=="function"&&define.amd?define(["exports","@prose-reader/shared","xmldoc","rxjs"],h):(p=typeof globalThis<"u"?globalThis:p||self,h(p["prose-streamer"]={},p.shared,p.xmldoc,p.rxjs))})(this,(function(p,h,b,s){"use strict";const I=(n,t)=>{const o=n.split(/(\d+)/),r=t.split(/(\d+)/);for(let e=0,i=o.length;e<i;e++)if(o[e]!==r[e])return o[e]?.match(/\d/)?+(o[e]||"")-+(r[e]||""):(o[e]||"").localeCompare(r[e]||"");return 1},$=n=>n.substring(n.lastIndexOf("/")+1)||n,W=n=>n.endsWith("/")?n.slice(0,-1):n,P=n=>{const t=n.lastIndexOf("/");return t>=0?n.substring(0,t):""},G=async(n,{orderByAlpha:t,name:o}={})=>{let r=n;return t&&(r=r.slice().sort((e,i)=>I(e.name,i.name))),{filename:o||"",records:r.map(e=>{const i=e.size,a=$(e.name);return e.isDir?{dir:!0,basename:a,uri:e.name,size:i}:{dir:e.isDir,basename:a,encodingFormat:h.detectMimeTypeFromName(e.name),uri:e.name,blob:async()=>new Blob([await e.data()],{type:h.detectMimeTypeFromName(e.name)??""}),string:async()=>{const c=await e.data();return String.fromCharCode.apply(null,Array.from(new Uint16Array(c)))},size:i}}),close:()=>Promise.resolve()}},u=h.Report.namespace("@prose-reader/streamer",!1,{color:"#ffae42"}),q=n=>{const t={};for(const r of n){const e=r.split("/");let i=t;for(let a=0;a<e.length;a++){const c=e[a];c!==void 0&&(i[c]||(i[c]={}),i=i[c])}}const o=(r,e="")=>Object.keys(r).sort().map((i,a,c)=>{const m=a===c.length-1,d=e+(m?"└── ":"├── "),l=e+(m?" ":"│ "),f=r[i];return f&&Object.keys(f).length>0?`${d}${i}/
2
- ${o(f,l)}`:`${d}${i}`}).join(`
3
- `);return o(t)},K=async(n,{orderByAlpha:t,name:o}={})=>{let r=Object.values(n.files);t&&(r=r.slice().sort((i,a)=>I(i.name,a.name)));const e={filename:o||"",records:r.map(i=>{const a=i._data.uncompressedSize,c=$(i.name);return i.dir?{dir:!0,basename:c,uri:i.name,size:a}:{dir:!1,basename:$(i.name),uri:i.name,encodingFormat:h.detectMimeTypeFromName(i.name),blob:()=>i.async("blob"),string:()=>i.async("string"),...i.internalStream&&{stream:i.internalStream},size:i._data.uncompressedSize}}),close:()=>Promise.resolve()};if(u.log("Generated archive",e),process.env.NODE_ENV==="development"&&u.isEnabled()){const i=q(r.map(a=>a.name));u.groupCollapsed(...u.getGroupArgs("Archive folder structure")),u.log(`
4
- ${i}`),u.groupEnd()}return e},Y=async(n,{name:t}={})=>{const o=await n.getFilesArray(),r={close:()=>n.close(),filename:t??"",records:o.map(e=>({dir:!1,basename:e.file.name,encodingFormat:h.detectMimeTypeFromName(e.file.name),size:e.file.size,uri:`${e.path}${e.file.name}`,blob:async()=>await e.file.extract(),string:async()=>(await e.file.extract()).text()}))};return u.log("Generated archive",r),r},x=async(n,{mimeType:t,direction:o}={mimeType:"text/plain"})=>{const r=`
1
+ (function(p,u){typeof exports=="object"&&typeof module<"u"?u(exports,require("@prose-reader/shared"),require("xmldoc"),require("rxjs")):typeof define=="function"&&define.amd?define(["exports","@prose-reader/shared","xmldoc","rxjs"],u):(p=typeof globalThis<"u"?globalThis:p||self,u(p["prose-streamer"]={},p.shared,p.xmldoc,p.rxjs))})(this,(function(p,u,b,c){"use strict";const R=(e,n)=>{const o=e.split(/(\d+)/),r=n.split(/(\d+)/);for(let t=0,i=o.length;t<i;t++)if(o[t]!==r[t])return o[t]?.match(/\d/)?+(o[t]||"")-+(r[t]||""):(o[t]||"").localeCompare(r[t]||"");return 1},$=e=>e.substring(e.lastIndexOf("/")+1)||e,I=e=>e.endsWith("/")?e.slice(0,-1):e,C=e=>{const n=e.lastIndexOf("/");return n>=0?e.substring(0,n):""},J=async(e,{orderByAlpha:n,name:o}={})=>{let r=e;return n&&(r=r.slice().sort((t,i)=>R(t.name,i.name))),{filename:o||"",records:r.map(t=>{const i=t.size,s=$(t.name);return t.isDir?{dir:!0,basename:s,uri:t.name,size:i}:{dir:t.isDir,basename:s,encodingFormat:u.detectMimeTypeFromName(t.name),uri:t.name,blob:async()=>new Blob([await t.data()],{type:u.detectMimeTypeFromName(t.name)??""}),string:async()=>{const a=await t.data();return String.fromCharCode.apply(null,Array.from(new Uint16Array(a)))},size:i}}),close:()=>Promise.resolve()}},f=u.Report.namespace("@prose-reader/streamer",!1,{color:"#ffae42"}),G=e=>{const n={};for(const r of e){const t=r.split("/");let i=n;for(let s=0;s<t.length;s++){const a=t[s];a!==void 0&&(i[a]||(i[a]={}),i=i[a])}}const o=(r,t="")=>Object.keys(r).sort().map((i,s,a)=>{const d=s===a.length-1,l=t+(d?"└── ":"├── "),m=t+(d?" ":"│ "),h=r[i];return h&&Object.keys(h).length>0?`${l}${i}/
2
+ ${o(h,m)}`:`${l}${i}`}).join(`
3
+ `);return o(n)},K=async(e,{orderByAlpha:n,name:o}={})=>{let r=Object.values(e.files);n&&(r=r.slice().sort((i,s)=>R(i.name,s.name)));const t={filename:o||"",records:r.map(i=>{const s=i._data.uncompressedSize,a=$(i.name);return i.dir?{dir:!0,basename:a,uri:i.name,size:s}:{dir:!1,basename:$(i.name),uri:i.name,encodingFormat:u.detectMimeTypeFromName(i.name),blob:()=>i.async("blob"),string:()=>i.async("string"),...i.internalStream&&{stream:i.internalStream},size:i._data.uncompressedSize}}),close:()=>Promise.resolve()};if(f.log("Generated archive",t),process.env.NODE_ENV==="development"&&f.isEnabled()){const i=G(r.map(s=>s.name));f.groupCollapsed(...f.getGroupArgs("Archive folder structure")),f.log(`
4
+ ${i}`),f.groupEnd()}return t},Y=async(e,{name:n}={})=>{const o=await e.getFilesArray(),r={close:()=>e.close(),filename:n??"",records:o.map(t=>({dir:!1,basename:t.file.name,encodingFormat:u.detectMimeTypeFromName(t.file.name),size:t.file.size,uri:`${t.path}${t.file.name}`,blob:async()=>await t.file.extract(),string:async()=>(await t.file.extract()).text()}))};return f.log("Generated archive",r),r},Q=async(e,{mimeType:n,direction:o}={mimeType:"text/plain"})=>{const r=`
5
5
  <?xml version="1.0" encoding="UTF-8"?>
6
6
  <package xmlns="http://www.idpf.org/2007/opf" version="3.0" xml:lang="ja" prefix="rendition: http://www.idpf.org/vocab/rendition/#"
7
7
  unique-identifier="ootuya-id">
@@ -16,23 +16,23 @@ ${i}`),u.groupEnd()}return e},Y=async(n,{name:t}={})=>{const o=await n.getFilesA
16
16
  <itemref idref="p01" />
17
17
  </spine>
18
18
  </package>
19
- `;return{filename:"content.txt",records:[{dir:!1,basename:$("generated.opf"),uri:"generated.opf",blob:async()=>new Blob([r]),string:async()=>r,size:0},{dir:!1,basename:$("p01.txt"),uri:"p01.txt",blob:async()=>typeof n=="string"?new Blob([n]):n,string:async()=>typeof n=="string"?n:n.text(),size:typeof n=="string"?n.length:n.size,encodingFormat:t}],close:()=>Promise.resolve()}},Q=async(n,t)=>{const o=`
19
+ `;return{filename:"content.txt",records:[{dir:!1,basename:$("generated.opf"),uri:"generated.opf",blob:async()=>new Blob([r]),string:async()=>r,size:0},{dir:!1,basename:$("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:n}],close:()=>Promise.resolve()}},Z=async(e,n)=>{const o=`
20
20
  <?xml version="1.0" encoding="UTF-8"?><package xmlns="http://www.idpf.org/2007/opf" version="2.0" unique-identifier="bookid">
21
21
  <metadata>
22
- <meta property="rendition:layout">${t?.useRenditionFlow?"reflowable":"pre-paginated"}</meta>
23
- ${t?.useRenditionFlow?'<meta property="rendition:flow">scrolled-continuous</meta>':""}
22
+ <meta property="rendition:layout">${n?.useRenditionFlow?"reflowable":"pre-paginated"}</meta>
23
+ ${n?.useRenditionFlow?'<meta property="rendition:flow">scrolled-continuous</meta>':""}
24
24
  </metadata>
25
25
  <manifest>
26
- ${n.map(i=>`<item id="${$(i)}" href="${i}" media-type="${h.detectMimeTypeFromName(i)}"/>`).join(`
26
+ ${e.map(i=>`<item id="${$(i)}" href="${i}" media-type="${u.detectMimeTypeFromName(i)}"/>`).join(`
27
27
  `)}
28
28
  </manifest>
29
29
  <spine>
30
- ${n.map(i=>`<itemref idref="${$(i)}" />`).join(`
30
+ ${e.map(i=>`<itemref idref="${$(i)}" />`).join(`
31
31
  `)}
32
32
  </spine>
33
33
  </package>
34
- `,r=n.map(i=>({dir:!1,basename:$(i),encodingFormat:h.detectMimeTypeFromName(i),uri:i,size:100/n.length,blob:async()=>(await fetch(i)).blob(),string:async()=>""}));return{filename:"",records:[{dir:!1,basename:"content.opf",uri:"content.opf",size:0,blob:async()=>new Blob,string:async()=>o},...r],close:()=>Promise.resolve()}},Z=({enableReport:n}={})=>{u.enable(!!n)},T=n=>{const o=Object.values(n.records).filter(r=>!r.dir).find(r=>r.uri.endsWith(".opf"));return{data:o,basePath:o?.uri.substring(0,o.uri.lastIndexOf("/"))||""}},j=({archive:n})=>async t=>{const o=n.records.find(e=>e.basename.toLowerCase()==="com.apple.ibooks.display-options.xml");if(!o||o.dir)return t;const r=await(await o.blob()).text();try{const a=new b.XmlDocument(r).childNamed("platform")?.childrenNamed("option")?.find(c=>c.attr.name==="fixed-layout")?.val||"false";return{...t,renditionLayout:a==="true"?"pre-paginated":t.renditionLayout}}catch(e){return console.error(`Unable to parse com.apple.ibooks.display-options.xml for content
35
- `,r),console.error(e),t}},ee=async n=>{const t={renditionLayout:void 0};return await Promise.all(n.records.map(async o=>{if(o.uri.endsWith("com.kobobooks.display-options.xml")&&!o.dir){const e=new b.XmlDocument(await o.string()).childNamed("platform")?.childNamed("option");e?.attr?.name==="fixed-layout"&&e.val==="true"&&(t.renditionLayout="pre-paginated")}})),t},te=({archive:n})=>async t=>{const o=await ee(n);return{...t,renditionLayout:t.renditionLayout??o.renditionLayout}},ne=({archive:n,baseUrl:t})=>async()=>{const o=Object.values(n.records).filter(r=>!r.dir);return{filename:n.filename,title:n.records.find(({dir:r})=>r)?.basename.replace(/\/$/,"")||n.filename,renditionLayout:void 0,renditionSpread:"auto",readingDirection:"ltr",spineItems:o.filter(r=>!r.basename.endsWith(".db")).map((r,e)=>{const i=t||(/^https?:\/\//.test(r.uri)?"":"file://");return{id:`${e}.${r.basename}`,index:e,href:encodeURI(`${i}${r.uri}`),renditionLayout:void 0,progressionWeight:1/o.length,pageSpreadLeft:void 0,pageSpreadRight:void 0,mediaType:r.encodingFormat}}),items:o.map((r,e)=>({id:`${e}.${r.basename}`,href:encodeURI(`${t}${r.uri}`)}))}},R=async({archive:n})=>{const{data:t,basePath:o}=T(n)||{},r=await t?.string();if(!r)return[];const e=new b.XmlDocument(r),i=e.childNamed("manifest"),c=e.childNamed("spine")?.childrenNamed("itemref").map(l=>l.attr.idref),m=i?.childrenNamed("item").filter(l=>c.includes(l.attr.id||""))||[];return n.records.filter(l=>m.find(f=>o?`${o}/${f.attr.href}`===l.uri:`${f.attr.href}`===l.uri))},re=n=>{const t=n.attr.properties?.split(" ")||[];let o;return t.find(r=>r==="rendition:layout-reflowable")&&(o="reflowable"),t.find(r=>r==="rendition:layout-pre-paginated")&&(o="pre-paginated"),{renditionLayout:o,pageSpreadLeft:t.some(r=>r==="page-spread-left")||void 0,pageSpreadRight:t.some(r=>r==="page-spread-right")||void 0}},ie=(n,t,o)=>{const r=n.attr.href||"",e=o?.(n);return{href:t?`${e}${t}/${r}`:`${e}${r}`,id:n.attr.id||"",mediaType:n.attr["media-type"]}},M=(n,t,o)=>{const r=n.childNamed("manifest"),{basePath:e}=T(t)||{};return r?.childrenNamed("item")?.map(i=>ie(i,e,o))||[]},oe=({archive:n,baseUrl:t})=>async o=>{const{data:r,basePath:e}=T(n)||{};if(!r)return o;const i=await r.string();u.groupCollapsed(...u.getGroupArgs("OPF data")),u.log("data",i),u.groupEnd();const a=new b.XmlDocument(i),c=a.childNamed("metadata"),m=a.childNamed("manifest"),d=a.childNamed("spine"),l=a.childNamed("guide"),f=c?.childNamed("dc:title"),w=c?.childrenNamed("meta")||[],F=w.find(g=>g.attr.property==="rendition:layout"),N=w.find(g=>g.attr.property==="rendition:flow"),y=w.find(g=>g.attr.property==="rendition:spread"),L=F?.val,v=N?.val,E=y?.val,k=f?.val||n.records.find(({dir:g})=>g)?.basename||"",Ce=d?.attr["page-progression-direction"],Pe=(await R({archive:n})).reduce((g,S)=>S.size+g,0);return{filename:n.filename,renditionLayout:L,renditionFlow:v||"auto",renditionSpread:E,title:k,readingDirection:Ce||"ltr",spineItems:d?.childrenNamed("itemref").map((g,S)=>{const A=m?.childrenNamed("item").find(C=>C.attr.id===g?.attr.idref),H=A?.attr.href||"",Re=n.records.find(C=>C.uri.endsWith(H))?.size||0,_=t||(/^https?:\/\//.test(H)?"":"file://"),J=re(g);return{...J,id:A?.attr.id||"",index:S,href:A?.attr.href?.startsWith("https://")?A?.attr.href:e?`${_}${e}/${A?.attr.href}`:`${_}${A?.attr.href}`,renditionLayout:J.renditionLayout??L,progressionWeight:Re/Pe,mediaType:A?.attr["media-type"]}})||[],items:M(a,n,g=>{const S=g.attr.href||"";return/^https?:\/\//.test(S)?"":t||"file://"}),guide:l?.childrenNamed("reference").map(g=>({href:g.attr.href||"",title:g.attr.title||"",type:g.attr.type}))}},ae=n=>{const t=n.descendantWithPath("head")?.childrenNamed("meta").find(o=>o.attr.name==="viewport");return!!(t&&t.attr.name==="viewport")},se=n=>n.reduce(async(t,o)=>{if(!await t||!h.isXmlBasedMimeType({mimeType:o.encodingFormat,uri:o.uri}))return!1;const e=o.dir?null:await o.string();return e?ae(new b.XmlDocument(e)):!1},Promise.resolve(!0)),ce=({archive:n})=>async t=>{if(t.renditionLayout==="reflowable"&&t.spineItems.every(r=>r.renditionLayout==="reflowable")){const r=await R({archive:n});if(await se(r))return{...t,spineItems:t.spineItems.map(i=>({...i,renditionLayout:"pre-paginated"})),renditionLayout:"pre-paginated"}}return t},de=({archive:n})=>async t=>{const o=n.records.find(i=>i.basename.toLowerCase()==="comicinfo.xml"&&!i.dir);if(!o||o.dir)return t;const r={...t,spineItems:t.spineItems.filter(i=>!i.id.toLowerCase().endsWith("comicinfo.xml")).map((i,a,c)=>({...i,progressionWeight:1/c.length}))},e=await o.string();try{const a=new b.XmlDocument(e).childNamed("Manga")?.val||"unknown";return{...r,readingDirection:a==="YesAndRightToLeft"?"rtl":"ltr"}}catch(i){return console.error(`Unable to parse comicinfo.xml for content
36
- `,e),console.error(i),r}},le=n=>n.records.some(t=>t.basename.endsWith(".opf")),me=({archive:n})=>async t=>le(n)?t:{...t,spineItems:t.spineItems.map(r=>{const e=n.records.find(a=>decodeURI(r.href).endsWith(a.uri)),i=h.parseContentType(e?.encodingFormat??"")||h.detectMimeTypeFromName(e?.basename??"");return{...r,renditionLayout:i?.startsWith("image/")?"pre-paginated":r.renditionLayout}})},D=n=>n?n.children.map(t=>t instanceof b.XmlTextNode?t.text:t instanceof b.XmlElement?D(t):"").join("").trim():"",O=(n,{basePath:t,baseUrl:o})=>{const r={contents:[],path:"",href:"",title:""};let e=n.childNamed("span")||n.childNamed("a");r.title=(e?.attr.title||e?.val.trim()||D(e))??"";let i=e?.name;i!=="a"&&(e=n.descendantWithPath(`${i}.a`),e&&(i=e.name.toLowerCase())),i==="a"&&e?.attr.href&&(r.path=h.urlJoin(t,e.attr.href),r.href=h.urlJoin(o,t,e.attr.href));const a=n.childNamed("ol");if(a){const c=a.childrenNamed("li");c&&c.length>0&&(r.contents=c.map(m=>O(m,{basePath:t,baseUrl:o})))}return r},pe=(n,{basePath:t,baseUrl:o})=>{const r=[];let e;return n.descendantWithPath("body.nav.ol")?e=n.descendantWithPath("body.nav.ol")?.children:n.descendantWithPath("body.section.nav.ol")&&(e=n.descendantWithPath("body.section.nav.ol")?.children),e&&e.length>0&&e.filter(i=>i.name==="li").forEach(i=>{r.push(O(i,{basePath:t,baseUrl:o}))}),r},ue=async(n,t,{baseUrl:o})=>{const r=n.childNamed("manifest")?.childrenNamed("item").find(e=>e.attr.properties==="nav");if(r){const e=Object.values(t.records).find(i=>i.uri.endsWith(r.attr.href||""));if(e&&!e.dir){const i=new b.XmlDocument(await e.string()),a=P(e.uri);return pe(i,{basePath:a,baseUrl:o})}}},z=(n,{opfBasePath:t,baseUrl:o,prefix:r})=>{const e=n?.childNamed(`${r}content`)?.attr.src||"",i={title:n?.descendantWithPath(`${r}navLabel.${r}text`)?.val||"",path:h.urlJoin(t,e),href:h.urlJoin(o,t,e),contents:[]},a=n.childrenNamed(`${r}navPoint`);return a&&a.length>0&&(i.contents=a.map(c=>z(c,{opfBasePath:t,baseUrl:o,prefix:r}))),i},fe=(n,{opfBasePath:t,baseUrl:o})=>{const r=[],e=n.name;let i="";return e.indexOf(":")!==-1&&(i=`${e.split(":")[0]}:`),n.childNamed(`${i}navMap`)?.childrenNamed(`${i}navPoint`).forEach(a=>{r.push(z(a,{opfBasePath:t,baseUrl:o,prefix:i}))}),r},he=async({opfData:n,opfBasePath:t,baseUrl:o,archive:r})=>{const i=n.childNamed("spine")?.attr.toc;if(i){const a=n.childNamed("manifest")?.childrenNamed("item").find(c=>c.attr.id===i);if(a){const c=`${t}${t===""?"":"/"}${a.attr.href}`,m=Object.values(r.records).find(d=>d.uri.endsWith(c));if(m&&!m.dir){const d=new b.XmlDocument(await m.string());return fe(d,{opfBasePath:t,baseUrl:o})}}}},ge=async(n,t,{baseUrl:o})=>{const{basePath:r}=T(t)||{},e=await ue(n,t,{baseUrl:o});if(e)return e;const i=await he({opfData:n,opfBasePath:r,archive:t,baseUrl:o});if(i)return i},ye=(n,{baseUrl:t})=>{const o=[...n.records].sort((e,i)=>I(e.uri,i.uri)),r=(e,i,a,c,m)=>{const d=e.find(w=>w.title===i),[l,...f]=a;return d?l?[...e.filter(F=>F!==d),{...d,contents:[...d.contents,...r(d.contents,l,f,c,m)]}]:d.path.split("/").length>m.split("/").length?[...e.filter(F=>F!==d),{...d,path:m,href:c}]:e:l?[...e,{contents:r([],l,f,c,m),href:c,path:m,title:i}]:[...e,{contents:[],href:c,path:m,title:i}]};return o.reduce((e,i)=>{if(i.dir)return e;const a=i.uri.split("/").slice(0,-1),[c,...m]=a;if(!c)return e;const d=h.urlJoin(t,encodeURI(i.uri)).replace(/\/$/,""),l=i.uri.replace(/\/$/,"");return r(e,c,m,d,l)},[])},be=async(n,{baseUrl:t})=>{const{data:o}=T(n)||{};if(o&&!o.dir){const e=new b.XmlDocument(await o.string());return await ge(e,n,{baseUrl:t})||[]}const r=ye(n,{baseUrl:t});if(r.length!==0)return r},ve=({archive:n,baseUrl:t})=>async o=>{if(o.nav)return o;const r=await be(n,{baseUrl:t});return r?{...o,nav:{toc:r}}:o},U=async(n,{baseUrl:t=""}={})=>{const o=[oe({archive:n,baseUrl:t}),de({archive:n}),j({archive:n}),me({archive:n}),ce({archive:n}),te({archive:n}),ve({archive:n,baseUrl:t})];try{const r=ne({archive:n,baseUrl:t})(),e=await o.reduce(async(i,a)=>await a(await i),r);if(u.log("Generated manifest",e),process.env.NODE_ENV==="development"&&u.isEnabled()){const i=JSON.stringify(e,null,2);u.groupCollapsed(...u.getGroupArgs("Generated manifest")),u.log(`
37
- ${i}`),u.groupEnd()}return e}catch(r){throw u.error(r),r}},we=n=>{const t=n.descendantWithPath("head")?.childrenNamed("meta").find(o=>o.attr.name==="calibre:cover");return!!(t&&t.attr.name==="calibre:cover")},$e=n=>n.descendantWithPath("body")?.descendantWithPath("div")?.childrenNamed("svg")?.find(t=>t.attr.width==="100%"&&t.attr.preserveAspectRatio==="none"),Fe=({archive:n,resourcePath:t})=>async o=>{const r=Object.values(n.records).find(e=>e.uri===t&&!e.dir);if(r&&!r.dir&&r.basename.endsWith(".xhtml")){const e=o.body??await r.string(),i=new b.XmlDocument(e);if(we(i)){const a=$e(i);return a&&delete a.attr.preserveAspectRatio,{...o,body:i?.toString()}}}return o},Te=({archive:n,resourcePath:t})=>async o=>Fe({archive:n,resourcePath:t})(o),Ae=({archive:n,resourcePath:t})=>async o=>{const r=Object.values(n.records).find(e=>e.uri===t&&!e.dir);if(r&&!r.dir&&r.basename.endsWith(".css")){const i=(o.body??await r.string()).replaceAll("-webkit-writing-mode","writing-mode");return{...o,body:i}}return o},Ee=async(n,t)=>{const r=await T(n).data?.string();if(r){const e=new b.XmlDocument(r),i=M(e,n,()=>"");if(i.find(c=>t.endsWith(c.href))?.mediaType)return{mediaType:i.find(c=>t.endsWith(c.href))?.mediaType}}return{mediaType:Ne(t)}},Ne=n=>{if(n.endsWith(".css"))return"text/css; charset=UTF-8";if(n.endsWith(".jpg"))return"image/jpg";if(n.endsWith(".xhtml"))return"application/xhtml+xml";if(n.endsWith(".mp4"))return"video/mp4";if(n.endsWith(".svg"))return"image/svg+xml"},Se=({archive:n,resourcePath:t})=>async o=>{const r=Object.values(n.records).find(i=>i.uri===t&&!i.dir);if(!r||r.dir)return o;const e=await Ee(n,t);return{...o,params:{...o.params,...r?.encodingFormat&&{contentType:r.encodingFormat},...e.mediaType&&{contentType:e.mediaType}}}},X=["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"],Ie=({archive:n,resourcePath:t})=>async o=>{const r=Object.values(n.records).find(e=>e.uri===t&&!e.dir);if(r&&!r.dir&&r.basename.endsWith(".xhtml")){const e=o.body??await r.string();if(!new RegExp(`<(${X.join("|")})[\\s/>]`,"i").test(e))return o;const a=new RegExp(`<(${X.join("|")})(\\s[^>]*)?\\s*/>`,"gi"),c=e.replace(a,(m,d,l="")=>`<${d} ${l.trim()}></${d}>`);return{...o,body:c}}return o},B=async(n,t)=>{const o=Object.values(n.records).find(i=>i.uri===t&&!i.dir);if(!o||o.dir)throw new Error(`no file found for resourcePath:${t}`);const r={params:{}},e=[Se({archive:n,resourcePath:t}),Ie({archive:n,resourcePath:t}),Ae({archive:n,resourcePath:t}),Te({archive:n,resourcePath:t})];try{const i=await e.reduce(async(a,c)=>await c(await a),Promise.resolve(r));return u.log("Generated resource",t,i),{...i,body:i.body??await o.blob()}}catch(i){throw u.error(i),i}};class We{constructor(t){this.cleanArchiveAfter=t,this.state$=new s.BehaviorSubject({status:"idle",locks:0})}update(t){this.state$.next({...this.state$.getValue(),...t})}get locks$(){return this.state$.pipe(s.map(({locks:t})=>t))}get state(){return this.state$.getValue()}get isUnlocked$(){return this.locks$.pipe(s.map(t=>t<=0),s.distinctUntilChanged(),s.shareReplay())}get overTTL$(){return this.isUnlocked$.pipe(s.switchMap(t=>t?this.cleanArchiveAfter===1/0?s.NEVER:s.timer(this.cleanArchiveAfter):s.NEVER))}}const Le=({getArchive:n,cleanArchiveAfter:t=300*1e3})=>{const o=new s.Subject,r=new s.Subject,e=new s.Subject,i={},a=o.pipe(s.mergeMap(d=>{const l=i[d];if(!l||l.state.status!=="idle")return s.EMPTY;let f=!1;const w=v=>{u.debug(`Cleaning up archive with key: ${v}`);const E=i[v];delete i[v],f||(E?.state.archive?.close(),f=!0)};l.update({status:"loading"});const F=l.locks$,N=l.isUnlocked$,y=F.pipe(s.pairwise(),s.filter(([v,E])=>E>v),s.startWith(!0));return s.from(n(d)).pipe(s.tap(v=>{l.update({archive:v,status:"success"})}),s.catchError(v=>(w(d),l.update({status:"error",error:v}),s.EMPTY)),s.switchMap(()=>{const v=y.pipe(s.switchMap(()=>e),s.switchMap(()=>N),s.filter(k=>k));return s.merge(v,l.overTTL$).pipe(s.first(),s.tap(()=>{w(d)}))}))}),s.takeUntil(r)),c=d=>{let l=!1;const f=i[d]??new We(t);i[d]=f,f.update({locks:f.state.locks+1});const w=()=>{l||(l=!0,f.update({locks:f.state.locks-1}))};o.next(d);const F=f.state$.pipe(s.map(({archive:y})=>y),s.filter(y=>!!y)),N=f.state$.pipe(s.tap(({error:y})=>{if(y)throw y}),s.ignoreElements());return s.merge(F,N).pipe(s.first(),s.map(y=>({archive:y,release:w})),s.catchError(y=>{throw w(),y}))},m=()=>{e.next()};return a.subscribe(),{access:c,purge:m,_archives:i}};class V{constructor({onError:t,onManifestSuccess:o,...r}){this.onError=e=>(console.error(e),new Response(String(e),{status:500})),this.epubLoader=Le(r),this.onManifestSuccess=o??(({manifest:e})=>Promise.resolve(e)),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(s.map(({archive:o,release:r})=>(r(),o)))}fetchManifest({key:t,baseUrl:o}){const r=this.accessArchive(t).pipe(s.mergeMap(({archive:e,release:i})=>s.from(U(e,{baseUrl:o})).pipe(s.switchMap(c=>s.from(this.onManifestSuccess({manifest:c,archive:e}))),s.map(c=>new Response(JSON.stringify(c),{status:200})),s.finalize(()=>{i()}))),s.catchError(e=>s.of(this.onError(e))));return s.lastValueFrom(r)}fetchResource({key:t,resourcePath:o}){const r=this.accessArchive(t).pipe(s.mergeMap(({archive:e,release:i})=>{const a=o.replaceAll("file://","");return s.from(B(e,a)).pipe(s.map(m=>new Response(m.body,{status:200,headers:{...m.params.contentType&&{"Content-Type":m.params.contentType}}})),s.finalize(()=>{i()}))}),s.catchError(e=>s.of(this.onError(e))));return s.lastValueFrom(r)}}class ke 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 r=W(o.baseUrl),e=t.request.url.substring(r.length+1),[i=""]=e.split("/"),a=decodeURIComponent(W(e.substring(i.length+1)));e.endsWith("/manifest")?t.respondWith(this.fetchManifest({key:i,baseUrl:`${r}/${i}/`})):t.respondWith(this.fetchResource({key:i,resourcePath:a}))}catch(o){t.respondWith(new Response(String(o),{status:500}))}}}p.ServiceWorkerStreamer=ke,p.Streamer=V,p.configure=Z,p.createArchiveFromArrayBufferList=G,p.createArchiveFromJszip=K,p.createArchiveFromLibArchive=Y,p.createArchiveFromText=x,p.createArchiveFromUrls=Q,p.generateManifestFromArchive=U,p.generateResourceFromArchive=B,p.getArchiveOpfInfo=T,p.getUriBasePath=P,p.getUriBasename=$,p.removeTrailingSlash=W,p.sortByTitleComparator=I,Object.defineProperty(p,Symbol.toStringTag,{value:"Module"})}));
34
+ `,r=e.map(i=>({dir:!1,basename:$(i),encodingFormat:u.detectMimeTypeFromName(i),uri:i,size:100/e.length,blob:async()=>(await fetch(i)).blob(),string:async()=>""}));return{filename:"",records:[{dir:!1,basename:"content.opf",uri:"content.opf",size:0,blob:async()=>new Blob,string:async()=>o},...r],close:()=>Promise.resolve()}},j=({enableReport:e}={})=>{f.enable(!!e)},T=e=>{const o=Object.values(e.records).filter(r=>!r.dir).find(r=>r.uri.endsWith(".opf"));return{data:o,basePath:o?.uri.substring(0,o.uri.lastIndexOf("/"))||""}},ee=({archive:e})=>async n=>{const o=e.records.find(t=>t.basename.toLowerCase()==="com.apple.ibooks.display-options.xml");if(!o||o.dir)return n;const r=await(await o.blob()).text();try{const s=new b.XmlDocument(r).childNamed("platform")?.childrenNamed("option")?.find(a=>a.attr.name==="fixed-layout")?.val||"false";return{...n,renditionLayout:s==="true"?"pre-paginated":n.renditionLayout}}catch(t){return console.error(`Unable to parse com.apple.ibooks.display-options.xml for content
35
+ `,r),console.error(t),n}},te=async e=>{const n={renditionLayout:void 0};return await Promise.all(e.records.map(async o=>{if(o.uri.endsWith("com.kobobooks.display-options.xml")&&!o.dir){const t=new b.XmlDocument(await o.string()).childNamed("platform")?.childNamed("option");t?.attr?.name==="fixed-layout"&&t.val==="true"&&(n.renditionLayout="pre-paginated")}})),n},ne=({archive:e})=>async n=>{const o=await te(e);return{...n,renditionLayout:n.renditionLayout??o.renditionLayout}},re=({archive:e,baseUrl:n})=>async()=>{const o=Object.values(e.records).filter(r=>!r.dir);return{filename:e.filename,title:e.records.find(({dir:r})=>r)?.basename.replace(/\/$/,"")||e.filename,renditionLayout:void 0,renditionSpread:"auto",readingDirection:"ltr",spineItems:o.filter(r=>!r.basename.endsWith(".db")).map((r,t)=>{const i=n||(/^https?:\/\//.test(r.uri)?"":"file://");return{id:`${t}.${r.basename}`,index:t,href:encodeURI(`${i}${r.uri}`),renditionLayout:void 0,progressionWeight:1/o.length,pageSpreadLeft:void 0,pageSpreadRight:void 0,mediaType:r.encodingFormat}}),items:o.map((r,t)=>({id:`${t}.${r.basename}`,href:encodeURI(`${n}${r.uri}`)}))}},P=async({archive:e})=>{const{data:n,basePath:o}=T(e)||{},r=await n?.string();if(!r)return[];const t=new b.XmlDocument(r),i=t.childNamed("manifest"),a=t.childNamed("spine")?.childrenNamed("itemref").map(m=>m.attr.idref),d=i?.childrenNamed("item").filter(m=>a.includes(m.attr.id||""))||[];return e.records.filter(m=>d.find(h=>o?`${o}/${h.attr.href}`===m.uri:`${h.attr.href}`===m.uri))},ie=e=>{const n=e.attr.properties?.split(" ")||[];let o;return n.find(r=>r==="rendition:layout-reflowable")&&(o="reflowable"),n.find(r=>r==="rendition:layout-pre-paginated")&&(o="pre-paginated"),{renditionLayout:o,pageSpreadLeft:n.some(r=>r==="page-spread-left")||void 0,pageSpreadRight:n.some(r=>r==="page-spread-right")||void 0}},oe=(e,n,o)=>{const r=e.attr.href||"",t=o?.(e);return{href:n?`${t}${n}/${r}`:`${t}${r}`,id:e.attr.id||"",mediaType:e.attr["media-type"]}},M=(e,n,o)=>{const r=e.childNamed("manifest"),{basePath:t}=T(n)||{};return r?.childrenNamed("item")?.map(i=>oe(i,t,o))||[]},se=({archive:e,baseUrl:n})=>async o=>{const{data:r,basePath:t}=T(e)||{};if(!r)return o;const i=await r.string();f.groupCollapsed(...f.getGroupArgs("OPF data")),f.log("data",i),f.groupEnd();const s=new b.XmlDocument(i),a=s.childNamed("metadata"),d=s.childNamed("manifest"),l=s.childNamed("spine"),m=s.childNamed("guide"),h=a?.childNamed("dc:title"),w=a?.childrenNamed("meta")||[],F=w.find(g=>g.attr.property==="rendition:layout"),E=w.find(g=>g.attr.property==="rendition:flow"),y=w.find(g=>g.attr.property==="rendition:spread"),L=F?.val,v=E?.val,N=y?.val,k=h?.val||e.records.find(({dir:g})=>g)?.basename||"",Oe=l?.attr["page-progression-direction"],Be=(await P({archive:e})).reduce((g,S)=>S.size+g,0);return{filename:e.filename,renditionLayout:L,renditionFlow:v||"auto",renditionSpread:N,title:k,readingDirection:Oe||"ltr",spineItems:l?.childrenNamed("itemref").map((g,S)=>{const A=d?.childrenNamed("item").find(W=>W.attr.id===g?.attr.idref),x=A?.attr.href||"",Ue=e.records.find(W=>W.uri.endsWith(x))?.size||0,H=n||(/^https?:\/\//.test(x)?"":"file://"),q=ie(g);return{...q,id:A?.attr.id||"",index:S,href:A?.attr.href?.startsWith("https://")?A?.attr.href:t?`${H}${t}/${A?.attr.href}`:`${H}${A?.attr.href}`,renditionLayout:q.renditionLayout??L,progressionWeight:Ue/Be,mediaType:A?.attr["media-type"]}})||[],items:M(s,e,g=>{const S=g.attr.href||"";return/^https?:\/\//.test(S)?"":n||"file://"}),guide:m?.childrenNamed("reference").map(g=>({href:g.attr.href||"",title:g.attr.title||"",type:g.attr.type}))}},ae=e=>{const n=e.descendantWithPath("head")?.childrenNamed("meta").find(o=>o.attr.name==="viewport");return!!(n&&n.attr.name==="viewport")},ce=e=>e.reduce(async(n,o)=>{if(!await n||!u.isXmlBasedMimeType({mimeType:o.encodingFormat,uri:o.uri}))return!1;const t=o.dir?null:await o.string();return t?ae(new b.XmlDocument(t)):!1},Promise.resolve(!0)),de=({archive:e})=>async n=>{if(n.renditionLayout==="reflowable"&&n.spineItems.every(r=>r.renditionLayout==="reflowable")){const r=await P({archive:e});if(await ce(r))return{...n,spineItems:n.spineItems.map(i=>({...i,renditionLayout:"pre-paginated"})),renditionLayout:"pre-paginated"}}return n},le=({archive:e})=>async n=>{const o=e.records.find(i=>i.basename.toLowerCase()==="comicinfo.xml"&&!i.dir);if(!o||o.dir)return n;const r={...n,spineItems:n.spineItems.filter(i=>!i.id.toLowerCase().endsWith("comicinfo.xml")).map((i,s,a)=>({...i,progressionWeight:1/a.length}))},t=await o.string();try{const s=new b.XmlDocument(t).childNamed("Manga")?.val||"unknown";return{...r,readingDirection:s==="YesAndRightToLeft"?"rtl":"ltr"}}catch(i){return console.error(`Unable to parse comicinfo.xml for content
36
+ `,t),console.error(i),r}},me=e=>e.records.some(n=>n.basename.endsWith(".opf")),pe=({archive:e})=>async n=>me(e)?n:{...n,spineItems:n.spineItems.map(r=>{const t=e.records.find(s=>decodeURI(r.href).endsWith(s.uri)),i=u.parseContentType(t?.encodingFormat??"")||u.detectMimeTypeFromName(t?.basename??"");return{...r,renditionLayout:i&&u.isMediaContentMimeType(i)?"pre-paginated":r.renditionLayout}})},D=e=>e?e.children.map(n=>n instanceof b.XmlTextNode?n.text:n instanceof b.XmlElement?D(n):"").join("").trim():"",z=(e,{basePath:n,baseUrl:o})=>{const r={contents:[],path:"",href:"",title:""};let t=e.childNamed("span")||e.childNamed("a");r.title=(t?.attr.title||t?.val.trim()||D(t))??"";let i=t?.name;i!=="a"&&(t=e.descendantWithPath(`${i}.a`),t&&(i=t.name.toLowerCase())),i==="a"&&t?.attr.href&&(r.path=u.urlJoin(n,t.attr.href),r.href=u.urlJoin(o,n,t.attr.href));const s=e.childNamed("ol");if(s){const a=s.childrenNamed("li");a&&a.length>0&&(r.contents=a.map(d=>z(d,{basePath:n,baseUrl:o})))}return r},ue=(e,{basePath:n,baseUrl:o})=>{const r=[];let t;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(i=>i.name==="li").forEach(i=>{r.push(z(i,{basePath:n,baseUrl:o}))}),r},fe=async(e,n,{baseUrl:o})=>{const r=e.childNamed("manifest")?.childrenNamed("item").find(t=>t.attr.properties==="nav");if(r){const t=Object.values(n.records).find(i=>i.uri.endsWith(r.attr.href||""));if(t&&!t.dir){const i=new b.XmlDocument(await t.string()),s=C(t.uri);return ue(i,{basePath:s,baseUrl:o})}}},O=(e,{opfBasePath:n,baseUrl:o,prefix:r})=>{const t=e?.childNamed(`${r}content`)?.attr.src||"",i={title:e?.descendantWithPath(`${r}navLabel.${r}text`)?.val||"",path:u.urlJoin(n,t),href:u.urlJoin(o,n,t),contents:[]},s=e.childrenNamed(`${r}navPoint`);return s&&s.length>0&&(i.contents=s.map(a=>O(a,{opfBasePath:n,baseUrl:o,prefix:r}))),i},he=(e,{opfBasePath:n,baseUrl:o})=>{const r=[],t=e.name;let i="";return t.indexOf(":")!==-1&&(i=`${t.split(":")[0]}:`),e.childNamed(`${i}navMap`)?.childrenNamed(`${i}navPoint`).forEach(s=>{r.push(O(s,{opfBasePath:n,baseUrl:o,prefix:i}))}),r},ge=async({opfData:e,opfBasePath:n,baseUrl:o,archive:r})=>{const i=e.childNamed("spine")?.attr.toc;if(i){const s=e.childNamed("manifest")?.childrenNamed("item").find(a=>a.attr.id===i);if(s){const a=`${n}${n===""?"":"/"}${s.attr.href}`,d=Object.values(r.records).find(l=>l.uri.endsWith(a));if(d&&!d.dir){const l=new b.XmlDocument(await d.string());return he(l,{opfBasePath:n,baseUrl:o})}}}},ye=async(e,n,{baseUrl:o})=>{const{basePath:r}=T(n)||{},t=await fe(e,n,{baseUrl:o});if(t)return t;const i=await ge({opfData:e,opfBasePath:r,archive:n,baseUrl:o});if(i)return i},be=e=>e.replace(/\.[^.]+$/,"").replace(/[_-]/g," ").replace(/\s+/g," ").trim(),ve=(e,n)=>{if(!(e.spineItems.length===0||!e.spineItems.every(r=>(u.parseContentType(r.mediaType??"")||u.detectMimeTypeFromName(r.href))?.startsWith("audio/"))))return e.spineItems.map(r=>{const t=n.records.find(i=>!i.dir&&decodeURI(r.href).endsWith(i.uri));return{title:be(t?.basename??r.href),href:r.href,path:t?.uri??r.href,contents:[]}})},we=(e,{baseUrl:n})=>{const o=[...e.records].sort((t,i)=>R(t.uri,i.uri)),r=(t,i,s,a,d)=>{const l=t.find(w=>w.title===i),[m,...h]=s;return l?m?[...t.filter(F=>F!==l),{...l,contents:[...l.contents,...r(l.contents,m,h,a,d)]}]:l.path.split("/").length>d.split("/").length?[...t.filter(F=>F!==l),{...l,path:d,href:a}]:t:m?[...t,{contents:r([],m,h,a,d),href:a,path:d,title:i}]:[...t,{contents:[],href:a,path:d,title:i}]};return o.reduce((t,i)=>{if(i.dir)return t;const s=i.uri.split("/").slice(0,-1),[a,...d]=s;if(!a)return t;const l=u.urlJoin(n,encodeURI(i.uri)).replace(/\/$/,""),m=i.uri.replace(/\/$/,"");return r(t,a,d,l,m)},[])},$e=async(e,n,{baseUrl:o})=>{const{data:r}=T(e)||{};if(r&&!r.dir){const s=new b.XmlDocument(await r.string());return await ye(s,e,{baseUrl:o})||[]}const t=ve(n,e);if(t)return t;const i=we(e,{baseUrl:o});if(i.length!==0)return i},Fe=({archive:e,baseUrl:n})=>async o=>{if(o.nav)return o;const r=await $e(e,o,{baseUrl:n});return r?{...o,nav:{toc:r}}:o},B=async(e,{baseUrl:n=""}={})=>{const o=[se({archive:e,baseUrl:n}),le({archive:e}),ee({archive:e}),pe({archive:e}),de({archive:e}),ne({archive:e}),Fe({archive:e,baseUrl:n})];try{const r=re({archive:e,baseUrl:n})(),t=await o.reduce(async(i,s)=>await s(await i),r);if(f.log("Generated manifest",t),process.env.NODE_ENV==="development"&&f.isEnabled()){const i=JSON.stringify(t,null,2);f.groupCollapsed(...f.getGroupArgs("Generated manifest")),f.log(`
37
+ ${i}`),f.groupEnd()}return t}catch(r){throw f.error(r),r}},Te=e=>{const n=e.descendantWithPath("head")?.childrenNamed("meta").find(o=>o.attr.name==="calibre:cover");return!!(n&&n.attr.name==="calibre:cover")},Ae=e=>e.descendantWithPath("body")?.descendantWithPath("div")?.childrenNamed("svg")?.find(n=>n.attr.width==="100%"&&n.attr.preserveAspectRatio==="none"),Ne=({archive:e,resourcePath:n})=>async o=>{const r=Object.values(e.records).find(t=>t.uri===n&&!t.dir);if(r&&!r.dir&&r.basename.endsWith(".xhtml")){const t=o.body??await r.string(),i=new b.XmlDocument(t);if(Te(i)){const s=Ae(i);return s&&delete s.attr.preserveAspectRatio,{...o,body:i?.toString()}}}return o},Ee=({archive:e,resourcePath:n})=>async o=>Ne({archive:e,resourcePath:n})(o),Se=({archive:e,resourcePath:n})=>async o=>{const r=Object.values(e.records).find(t=>t.uri===n&&!t.dir);if(r&&!r.dir&&r.basename.endsWith(".css")){const i=(o.body??await r.string()).replaceAll("-webkit-writing-mode","writing-mode");return{...o,body:i}}return o},Re=async(e,n)=>{const r=await T(e).data?.string();if(r){const t=new b.XmlDocument(r),i=M(t,e,()=>"");if(i.find(a=>n.endsWith(a.href))?.mediaType)return{mediaType:i.find(a=>n.endsWith(a.href))?.mediaType}}return{mediaType:Ie(n)}},Ie=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"},Le=({archive:e,resourcePath:n})=>async o=>{const r=Object.values(e.records).find(i=>i.uri===n&&!i.dir);if(!r||r.dir)return o;const t=await Re(e,n);return{...o,params:{...o.params,...r?.encodingFormat&&{contentType:r.encodingFormat},...t.mediaType&&{contentType:t.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"],ke=({archive:e,resourcePath:n})=>async o=>{const r=Object.values(e.records).find(t=>t.uri===n&&!t.dir);if(r&&!r.dir&&r.basename.endsWith(".xhtml")){const t=o.body??await r.string();if(!new RegExp(`<(${U.join("|")})[\\s/>]`,"i").test(t))return o;const s=new RegExp(`<(${U.join("|")})(\\s[^>]*)?\\s*/>`,"gi"),a=t.replace(s,(d,l,m="")=>`<${l} ${m.trim()}></${l}>`);return{...o,body:a}}return o},X=async(e,n)=>{const o=Object.values(e.records).find(i=>i.uri===n&&!i.dir);if(!o||o.dir)throw new Error(`no file found for resourcePath:${n}`);const r={params:{}},t=[Le({archive:e,resourcePath:n}),ke({archive:e,resourcePath:n}),Se({archive:e,resourcePath:n}),Ee({archive:e,resourcePath:n})];try{const i=await t.reduce(async(s,a)=>await a(await s),Promise.resolve(r));return f.log("Generated resource",n,i),{...i,body:i.body??await o.blob()}}catch(i){throw f.error(i),i}};class We{constructor(n){this.cleanArchiveAfter=n,this.state$=new c.BehaviorSubject({status:"idle",locks:0})}update(n){this.state$.next({...this.state$.getValue(),...n})}get locks$(){return this.state$.pipe(c.map(({locks:n})=>n))}get state(){return this.state$.getValue()}get isUnlocked$(){return this.locks$.pipe(c.map(n=>n<=0),c.distinctUntilChanged(),c.shareReplay())}get overTTL$(){return this.isUnlocked$.pipe(c.switchMap(n=>n?this.cleanArchiveAfter===1/0?c.NEVER:c.timer(this.cleanArchiveAfter):c.NEVER))}}const Ce=({getArchive:e,cleanArchiveAfter:n=300*1e3})=>{const o=new c.Subject,r=new c.Subject,t=new c.Subject,i={},s=o.pipe(c.mergeMap(l=>{const m=i[l];if(!m||m.state.status!=="idle")return c.EMPTY;let h=!1;const w=v=>{f.debug(`Cleaning up archive with key: ${v}`);const N=i[v];delete i[v],h||(N?.state.archive?.close(),h=!0)};m.update({status:"loading"});const F=m.locks$,E=m.isUnlocked$,y=F.pipe(c.pairwise(),c.filter(([v,N])=>N>v),c.startWith(!0));return c.from(e(l)).pipe(c.tap(v=>{m.update({archive:v,status:"success"})}),c.catchError(v=>(w(l),m.update({status:"error",error:v}),c.EMPTY)),c.switchMap(()=>{const v=y.pipe(c.switchMap(()=>t),c.switchMap(()=>E),c.filter(k=>k));return c.merge(v,m.overTTL$).pipe(c.first(),c.tap(()=>{w(l)}))}))}),c.takeUntil(r)),a=l=>{let m=!1;const h=i[l]??new We(n);i[l]=h,h.update({locks:h.state.locks+1});const w=()=>{m||(m=!0,h.update({locks:h.state.locks-1}))};o.next(l);const F=h.state$.pipe(c.map(({archive:y})=>y),c.filter(y=>!!y)),E=h.state$.pipe(c.tap(({error:y})=>{if(y)throw y}),c.ignoreElements());return c.merge(F,E).pipe(c.first(),c.map(y=>({archive:y,release:w})),c.catchError(y=>{throw w(),y}))},d=()=>{t.next()};return s.subscribe(),{access:a,purge:d,_archives:i}},V=e=>e?/^\d+$/.test(e)?{valid:!0,value:Number.parseInt(e,10)}:{valid:!1,value:void 0}:{valid:!0,value:void 0},Pe=e=>{if(!e.toLowerCase().startsWith("bytes="))return{kind:"missing"};const n=e.slice(6).trim();if(!n)return{kind:"invalid"};if(n.includes(","))return{kind:"multi"};const o=/^(\d*)-(\d*)$/.exec(n);if(!o)return{kind:"invalid"};const[,r="",t=""]=o,i=V(r.trim()),s=V(t.trim());return!i.valid||!s.valid?{kind:"invalid"}:{kind:"single",start:i.value,end:s.value}},Me=e=>{if(e instanceof Blob)return{size:e.size,slice:(o,r)=>{const t=e.slice(o,r);return{content:t,length:t.size}}};const n=new TextEncoder().encode(e);return{size:n.byteLength,slice:(o,r)=>{const t=n.slice(o,r);return{content:t,length:t.byteLength}}}},De=({body:e,contentType:n,rangeHeader:o})=>{const r=new Headers;if(n&&r.set("Content-Type",n),r.set("Accept-Ranges","bytes"),!o)return e instanceof Blob&&r.set("Content-Length",String(e.size)),new Response(e,{status:200,headers:r});const t=Pe(o);if(t.kind==="missing"||t.kind==="multi")return e instanceof Blob&&r.set("Content-Length",String(e.size)),new Response(e,{status:200,headers:r});const i=Me(e),s=i.size;if(t.kind==="invalid")return new Response(null,{status:416,headers:{"Content-Range":`bytes */${s}`}});let a=t.start,d=t.end;if(a===void 0&&d===void 0)return new Response(null,{status:416,headers:{"Content-Range":`bytes */${s}`}});if(a===void 0){const m=Math.min(d??0,s);a=Math.max(0,s-m),d=s-1}else(d===void 0||d>=s)&&(d=s-1);if(a<0||d<0||a>=s||d>=s||a>d)return new Response(null,{status:416,headers:{"Content-Range":`bytes */${s}`}});const l=i.slice(a,d+1);return r.set("Content-Length",String(l.length)),r.set("Content-Range",`bytes ${a}-${d}/${s}`),new Response(l.content,{status:206,headers:r})};class _{constructor({onError:n,onManifestSuccess:o,...r}){this.onError=t=>(console.error(t),new Response(String(t),{status:500})),this.epubLoader=Ce(r),this.onManifestSuccess=o??(({manifest:t})=>Promise.resolve(t)),this.onError=n??this.onError}prune(){this.epubLoader.purge()}accessArchive(n){return this.lastAccessedKey!==void 0&&this.lastAccessedKey!==n&&this.epubLoader.purge(),this.lastAccessedKey=n,this.epubLoader.access(n)}accessArchiveWithoutLock(n){return this.accessArchive(n).pipe(c.map(({archive:o,release:r})=>(r(),o)))}withArchiveResponse({key:n,getResponse:o}){const r=this.accessArchive(n).pipe(c.mergeMap(({archive:t,release:i})=>c.from(o(t)).pipe(c.finalize(()=>{i()}))),c.catchError(t=>c.of(this.onError(t))));return c.lastValueFrom(r)}fetchManifest({key:n,baseUrl:o}){return this.withArchiveResponse({key:n,getResponse:r=>c.from(B(r,{baseUrl:o})).pipe(c.switchMap(i=>c.from(this.onManifestSuccess({manifest:i,archive:r}))),c.map(i=>new Response(JSON.stringify(i),{status:200})))})}fetchResource({key:n,resourcePath:o,request:r}){return this.withArchiveResponse({key:n,getResponse:t=>{const i=o.replaceAll("file://","");return c.from(X(t,i)).pipe(c.map(a=>De({body:a.body??"",contentType:a.params.contentType,rangeHeader:r?.headers.get("range")})))}})}}class ze extends _{constructor({getUriInfo:n,...o}){super(o),this.getUriInfo=n,this.fetchEventListener=this.fetchEventListener.bind(this)}fetchEventListener(n){try{const o=this.getUriInfo(n);if(!o)return;const r=I(o.baseUrl),t=n.request.url.substring(r.length+1),[i=""]=t.split("/"),s=decodeURIComponent(I(t.substring(i.length+1)));t.endsWith("/manifest")?n.respondWith(this.fetchManifest({key:i,baseUrl:`${r}/${i}/`})):n.respondWith(this.fetchResource({key:i,resourcePath:s,request:n.request}))}catch(o){n.respondWith(new Response(String(o),{status:500}))}}}p.ServiceWorkerStreamer=ze,p.Streamer=_,p.configure=j,p.createArchiveFromArrayBufferList=J,p.createArchiveFromJszip=K,p.createArchiveFromLibArchive=Y,p.createArchiveFromText=Q,p.createArchiveFromUrls=Z,p.generateManifestFromArchive=B,p.generateResourceFromArchive=X,p.getArchiveOpfInfo=T,p.getUriBasePath=C,p.getUriBasename=$,p.removeTrailingSlash=I,p.sortByTitleComparator=R,Object.defineProperty(p,Symbol.toStringTag,{value:"Module"})}));
38
38
  //# sourceMappingURL=index.umd.cjs.map