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