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