dsh-pdf-workbench 0.0.0-stage → 1.3.1

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.
Files changed (278) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +192 -3
  3. package/assets/screenshot-bilingual.jpg +0 -0
  4. package/assets/screenshot-hero.jpg +0 -0
  5. package/assets/screenshot-overlay.jpg +0 -0
  6. package/assets/screenshot-qa.jpg +0 -0
  7. package/assets/screenshot-reader.jpg +0 -0
  8. package/assets/screenshot-shelf.jpg +0 -0
  9. package/client.js +14126 -0
  10. package/cordis.patch.yml +13 -0
  11. package/docs/DETAIL.md +69 -0
  12. package/docs/READER-REWRITE.md +95 -0
  13. package/docs/RELEASE.md +110 -0
  14. package/docs/ROADMAP.md +172 -0
  15. package/docs/SIDE-PREVIEW.md +271 -0
  16. package/docs/TESTING.md +109 -0
  17. package/docs/TRANSLATION.md +167 -0
  18. package/icon.svg +6 -0
  19. package/index.js +210 -0
  20. package/locale/en.json +7 -0
  21. package/locale/zh.json +7 -0
  22. package/package.json +64 -6
  23. package/samples/manifest.json +52 -0
  24. package/samples/test/serre_c01_p2-3.pdf +0 -0
  25. package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c00_/345/211/215/347/275/256_/345/260/201/351/235/242/347/233/256/345/275/225.pdf +0 -0
  26. package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c01_/347/254/2540/347/253/240_/345/257/274/350/257/273.pdf +0 -0
  27. package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c02_/347/254/2541/347/253/240_/345/217/244/351/242/230_/345/255/231/345/255/220/347/256/227/347/273/217/345/210/260/347/247/246/344/271/235/351/237/266.pdf +0 -0
  28. package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c03_/347/254/2542/347/253/240_/346/225/264/346/225/260/347/211/210/346/234/254.pdf +0 -0
  29. package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c04_/347/254/2543/347/253/240_/347/216/257/350/256/272/347/211/210/346/234/254.pdf +0 -0
  30. package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c05_/347/254/2544/347/253/240_/345/244/232/351/241/271/345/274/217/347/211/210/346/234/254.pdf +0 -0
  31. package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c06_/347/254/2545/347/253/240_/347/276/244/350/256/272/347/211/210/346/234/254.pdf +0 -0
  32. package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c07_/347/254/2546/347/253/240_/347/273/237/344/270/200/347/273/223/346/236/204.pdf +0 -0
  33. package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c08_/347/254/2547/347/253/240_/345/233/236/346/234/233.pdf +0 -0
  34. package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c90_/351/231/204/345/275/225_/346/225/231/346/235/220/351/241/265/347/240/201/347/254/246/345/217/267/350/241/250/350/207/252/346/265/213.pdf +0 -0
  35. package/screenshots.json +8 -0
  36. package/src/client/viewer.js +80 -0
  37. package/src/host/annotations.js +145 -0
  38. package/src/host/artifacts.js +317 -0
  39. package/src/host/blocks.js +44 -0
  40. package/src/host/config.js +112 -0
  41. package/src/host/digest.js +156 -0
  42. package/src/host/environment.js +42 -0
  43. package/src/host/http.js +124 -0
  44. package/src/host/index-source.js +488 -0
  45. package/src/host/libraries.js +221 -0
  46. package/src/host/links.js +201 -0
  47. package/src/host/pdf-source.js +122 -0
  48. package/src/host/pending.js +166 -0
  49. package/src/host/pressure.js +137 -0
  50. package/src/host/purge.js +100 -0
  51. package/src/host/render.js +43 -0
  52. package/src/host/routes.js +1778 -0
  53. package/src/host/shelf-groups.js +109 -0
  54. package/src/host/state.js +128 -0
  55. package/src/host/toolkit.js +54 -0
  56. package/src/host/translate-blocks.js +132 -0
  57. package/src/host/translate-job.js +316 -0
  58. package/src/host/translate.js +94 -0
  59. package/src/host/update.js +134 -0
  60. package/tools/AGENTS.md +33 -0
  61. package/tools/README.md +138 -0
  62. package/tools/auto_chapters.py +122 -0
  63. package/tools/dsfile.mjs +198 -0
  64. package/tools/export_chapter.py +108 -0
  65. package/tools/export_page.py +91 -0
  66. package/tools/md2overlay.py +200 -0
  67. package/tools/md2tex.py +422 -0
  68. package/tools/pdf_blocks.py +110 -0
  69. package/tools/pdf_fontsize.py +20 -0
  70. package/tools/pdf_merge.py +15 -0
  71. package/tools/pdf_pagesizes.py +8 -0
  72. package/tools/pdf_raster.py +21 -0
  73. package/tools/pdfkit.py +896 -0
  74. package/tools/realign_overlay.py +248 -0
  75. package/tools/rebuild_overlay.py +55 -0
  76. package/tools/rename_chapters.py +93 -0
  77. package/tools//350/247/204/350/214/203/00_/345/205/261/345/220/214/347/272/246/345/256/232.md +142 -0
  78. package/tools//350/247/204/350/214/203/01_/346/226/207/346/234/254/346/217/220/345/217/226.md +204 -0
  79. package/tools//350/247/204/350/214/203/02_/347/255/224/347/226/221.md +61 -0
  80. package/tools//350/247/204/350/214/203/03_/344/271/240/351/242/230/350/247/243/347/255/224.md +77 -0
  81. package/tools//350/247/204/350/214/203/api/346/217/220/347/244/272/350/257/215_/346/217/220/345/217/226.md +11 -0
  82. package/vendor/cmaps/78-EUC-H.bcmap +0 -0
  83. package/vendor/cmaps/78-EUC-V.bcmap +0 -0
  84. package/vendor/cmaps/78-H.bcmap +0 -0
  85. package/vendor/cmaps/78-RKSJ-H.bcmap +0 -0
  86. package/vendor/cmaps/78-RKSJ-V.bcmap +0 -0
  87. package/vendor/cmaps/78-V.bcmap +0 -0
  88. package/vendor/cmaps/78ms-RKSJ-H.bcmap +0 -0
  89. package/vendor/cmaps/78ms-RKSJ-V.bcmap +0 -0
  90. package/vendor/cmaps/83pv-RKSJ-H.bcmap +0 -0
  91. package/vendor/cmaps/90ms-RKSJ-H.bcmap +0 -0
  92. package/vendor/cmaps/90ms-RKSJ-V.bcmap +0 -0
  93. package/vendor/cmaps/90msp-RKSJ-H.bcmap +0 -0
  94. package/vendor/cmaps/90msp-RKSJ-V.bcmap +0 -0
  95. package/vendor/cmaps/90pv-RKSJ-H.bcmap +0 -0
  96. package/vendor/cmaps/90pv-RKSJ-V.bcmap +0 -0
  97. package/vendor/cmaps/Add-H.bcmap +0 -0
  98. package/vendor/cmaps/Add-RKSJ-H.bcmap +0 -0
  99. package/vendor/cmaps/Add-RKSJ-V.bcmap +0 -0
  100. package/vendor/cmaps/Add-V.bcmap +0 -0
  101. package/vendor/cmaps/Adobe-CNS1-0.bcmap +0 -0
  102. package/vendor/cmaps/Adobe-CNS1-1.bcmap +0 -0
  103. package/vendor/cmaps/Adobe-CNS1-2.bcmap +0 -0
  104. package/vendor/cmaps/Adobe-CNS1-3.bcmap +0 -0
  105. package/vendor/cmaps/Adobe-CNS1-4.bcmap +0 -0
  106. package/vendor/cmaps/Adobe-CNS1-5.bcmap +0 -0
  107. package/vendor/cmaps/Adobe-CNS1-6.bcmap +0 -0
  108. package/vendor/cmaps/Adobe-CNS1-UCS2.bcmap +0 -0
  109. package/vendor/cmaps/Adobe-GB1-0.bcmap +0 -0
  110. package/vendor/cmaps/Adobe-GB1-1.bcmap +0 -0
  111. package/vendor/cmaps/Adobe-GB1-2.bcmap +0 -0
  112. package/vendor/cmaps/Adobe-GB1-3.bcmap +0 -0
  113. package/vendor/cmaps/Adobe-GB1-4.bcmap +0 -0
  114. package/vendor/cmaps/Adobe-GB1-5.bcmap +0 -0
  115. package/vendor/cmaps/Adobe-GB1-UCS2.bcmap +0 -0
  116. package/vendor/cmaps/Adobe-Japan1-0.bcmap +0 -0
  117. package/vendor/cmaps/Adobe-Japan1-1.bcmap +0 -0
  118. package/vendor/cmaps/Adobe-Japan1-2.bcmap +0 -0
  119. package/vendor/cmaps/Adobe-Japan1-3.bcmap +0 -0
  120. package/vendor/cmaps/Adobe-Japan1-4.bcmap +0 -0
  121. package/vendor/cmaps/Adobe-Japan1-5.bcmap +0 -0
  122. package/vendor/cmaps/Adobe-Japan1-6.bcmap +0 -0
  123. package/vendor/cmaps/Adobe-Japan1-UCS2.bcmap +0 -0
  124. package/vendor/cmaps/Adobe-Korea1-0.bcmap +0 -0
  125. package/vendor/cmaps/Adobe-Korea1-1.bcmap +0 -0
  126. package/vendor/cmaps/Adobe-Korea1-2.bcmap +0 -0
  127. package/vendor/cmaps/Adobe-Korea1-UCS2.bcmap +0 -0
  128. package/vendor/cmaps/B5-H.bcmap +0 -0
  129. package/vendor/cmaps/B5-V.bcmap +0 -0
  130. package/vendor/cmaps/B5pc-H.bcmap +0 -0
  131. package/vendor/cmaps/B5pc-V.bcmap +0 -0
  132. package/vendor/cmaps/CNS-EUC-H.bcmap +0 -0
  133. package/vendor/cmaps/CNS-EUC-V.bcmap +0 -0
  134. package/vendor/cmaps/CNS1-H.bcmap +0 -0
  135. package/vendor/cmaps/CNS1-V.bcmap +0 -0
  136. package/vendor/cmaps/CNS2-H.bcmap +0 -0
  137. package/vendor/cmaps/CNS2-V.bcmap +3 -0
  138. package/vendor/cmaps/ETHK-B5-H.bcmap +0 -0
  139. package/vendor/cmaps/ETHK-B5-V.bcmap +0 -0
  140. package/vendor/cmaps/ETen-B5-H.bcmap +0 -0
  141. package/vendor/cmaps/ETen-B5-V.bcmap +0 -0
  142. package/vendor/cmaps/ETenms-B5-H.bcmap +3 -0
  143. package/vendor/cmaps/ETenms-B5-V.bcmap +0 -0
  144. package/vendor/cmaps/EUC-H.bcmap +0 -0
  145. package/vendor/cmaps/EUC-V.bcmap +0 -0
  146. package/vendor/cmaps/Ext-H.bcmap +0 -0
  147. package/vendor/cmaps/Ext-RKSJ-H.bcmap +0 -0
  148. package/vendor/cmaps/Ext-RKSJ-V.bcmap +0 -0
  149. package/vendor/cmaps/Ext-V.bcmap +0 -0
  150. package/vendor/cmaps/GB-EUC-H.bcmap +0 -0
  151. package/vendor/cmaps/GB-EUC-V.bcmap +0 -0
  152. package/vendor/cmaps/GB-H.bcmap +4 -0
  153. package/vendor/cmaps/GB-V.bcmap +0 -0
  154. package/vendor/cmaps/GBK-EUC-H.bcmap +0 -0
  155. package/vendor/cmaps/GBK-EUC-V.bcmap +0 -0
  156. package/vendor/cmaps/GBK2K-H.bcmap +0 -0
  157. package/vendor/cmaps/GBK2K-V.bcmap +0 -0
  158. package/vendor/cmaps/GBKp-EUC-H.bcmap +0 -0
  159. package/vendor/cmaps/GBKp-EUC-V.bcmap +0 -0
  160. package/vendor/cmaps/GBT-EUC-H.bcmap +0 -0
  161. package/vendor/cmaps/GBT-EUC-V.bcmap +0 -0
  162. package/vendor/cmaps/GBT-H.bcmap +0 -0
  163. package/vendor/cmaps/GBT-V.bcmap +0 -0
  164. package/vendor/cmaps/GBTpc-EUC-H.bcmap +0 -0
  165. package/vendor/cmaps/GBTpc-EUC-V.bcmap +0 -0
  166. package/vendor/cmaps/GBpc-EUC-H.bcmap +0 -0
  167. package/vendor/cmaps/GBpc-EUC-V.bcmap +0 -0
  168. package/vendor/cmaps/H.bcmap +0 -0
  169. package/vendor/cmaps/HKdla-B5-H.bcmap +0 -0
  170. package/vendor/cmaps/HKdla-B5-V.bcmap +0 -0
  171. package/vendor/cmaps/HKdlb-B5-H.bcmap +0 -0
  172. package/vendor/cmaps/HKdlb-B5-V.bcmap +0 -0
  173. package/vendor/cmaps/HKgccs-B5-H.bcmap +0 -0
  174. package/vendor/cmaps/HKgccs-B5-V.bcmap +0 -0
  175. package/vendor/cmaps/HKm314-B5-H.bcmap +0 -0
  176. package/vendor/cmaps/HKm314-B5-V.bcmap +0 -0
  177. package/vendor/cmaps/HKm471-B5-H.bcmap +0 -0
  178. package/vendor/cmaps/HKm471-B5-V.bcmap +0 -0
  179. package/vendor/cmaps/HKscs-B5-H.bcmap +0 -0
  180. package/vendor/cmaps/HKscs-B5-V.bcmap +0 -0
  181. package/vendor/cmaps/Hankaku.bcmap +0 -0
  182. package/vendor/cmaps/Hiragana.bcmap +0 -0
  183. package/vendor/cmaps/KSC-EUC-H.bcmap +0 -0
  184. package/vendor/cmaps/KSC-EUC-V.bcmap +0 -0
  185. package/vendor/cmaps/KSC-H.bcmap +0 -0
  186. package/vendor/cmaps/KSC-Johab-H.bcmap +0 -0
  187. package/vendor/cmaps/KSC-Johab-V.bcmap +0 -0
  188. package/vendor/cmaps/KSC-V.bcmap +0 -0
  189. package/vendor/cmaps/KSCms-UHC-H.bcmap +0 -0
  190. package/vendor/cmaps/KSCms-UHC-HW-H.bcmap +0 -0
  191. package/vendor/cmaps/KSCms-UHC-HW-V.bcmap +0 -0
  192. package/vendor/cmaps/KSCms-UHC-V.bcmap +0 -0
  193. package/vendor/cmaps/KSCpc-EUC-H.bcmap +0 -0
  194. package/vendor/cmaps/KSCpc-EUC-V.bcmap +0 -0
  195. package/vendor/cmaps/Katakana.bcmap +0 -0
  196. package/vendor/cmaps/LICENSE.txt +36 -0
  197. package/vendor/cmaps/NWP-H.bcmap +0 -0
  198. package/vendor/cmaps/NWP-V.bcmap +0 -0
  199. package/vendor/cmaps/RKSJ-H.bcmap +0 -0
  200. package/vendor/cmaps/RKSJ-V.bcmap +0 -0
  201. package/vendor/cmaps/Roman.bcmap +0 -0
  202. package/vendor/cmaps/UniCNS-UCS2-H.bcmap +0 -0
  203. package/vendor/cmaps/UniCNS-UCS2-V.bcmap +0 -0
  204. package/vendor/cmaps/UniCNS-UTF16-H.bcmap +0 -0
  205. package/vendor/cmaps/UniCNS-UTF16-V.bcmap +0 -0
  206. package/vendor/cmaps/UniCNS-UTF32-H.bcmap +0 -0
  207. package/vendor/cmaps/UniCNS-UTF32-V.bcmap +0 -0
  208. package/vendor/cmaps/UniCNS-UTF8-H.bcmap +0 -0
  209. package/vendor/cmaps/UniCNS-UTF8-V.bcmap +0 -0
  210. package/vendor/cmaps/UniGB-UCS2-H.bcmap +0 -0
  211. package/vendor/cmaps/UniGB-UCS2-V.bcmap +0 -0
  212. package/vendor/cmaps/UniGB-UTF16-H.bcmap +0 -0
  213. package/vendor/cmaps/UniGB-UTF16-V.bcmap +0 -0
  214. package/vendor/cmaps/UniGB-UTF32-H.bcmap +0 -0
  215. package/vendor/cmaps/UniGB-UTF32-V.bcmap +0 -0
  216. package/vendor/cmaps/UniGB-UTF8-H.bcmap +0 -0
  217. package/vendor/cmaps/UniGB-UTF8-V.bcmap +0 -0
  218. package/vendor/cmaps/UniJIS-UCS2-H.bcmap +0 -0
  219. package/vendor/cmaps/UniJIS-UCS2-HW-H.bcmap +0 -0
  220. package/vendor/cmaps/UniJIS-UCS2-HW-V.bcmap +0 -0
  221. package/vendor/cmaps/UniJIS-UCS2-V.bcmap +0 -0
  222. package/vendor/cmaps/UniJIS-UTF16-H.bcmap +0 -0
  223. package/vendor/cmaps/UniJIS-UTF16-V.bcmap +0 -0
  224. package/vendor/cmaps/UniJIS-UTF32-H.bcmap +0 -0
  225. package/vendor/cmaps/UniJIS-UTF32-V.bcmap +0 -0
  226. package/vendor/cmaps/UniJIS-UTF8-H.bcmap +0 -0
  227. package/vendor/cmaps/UniJIS-UTF8-V.bcmap +0 -0
  228. package/vendor/cmaps/UniJIS2004-UTF16-H.bcmap +0 -0
  229. package/vendor/cmaps/UniJIS2004-UTF16-V.bcmap +0 -0
  230. package/vendor/cmaps/UniJIS2004-UTF32-H.bcmap +0 -0
  231. package/vendor/cmaps/UniJIS2004-UTF32-V.bcmap +0 -0
  232. package/vendor/cmaps/UniJIS2004-UTF8-H.bcmap +0 -0
  233. package/vendor/cmaps/UniJIS2004-UTF8-V.bcmap +0 -0
  234. package/vendor/cmaps/UniJISPro-UCS2-HW-V.bcmap +0 -0
  235. package/vendor/cmaps/UniJISPro-UCS2-V.bcmap +0 -0
  236. package/vendor/cmaps/UniJISPro-UTF8-V.bcmap +0 -0
  237. package/vendor/cmaps/UniJISX0213-UTF32-H.bcmap +0 -0
  238. package/vendor/cmaps/UniJISX0213-UTF32-V.bcmap +0 -0
  239. package/vendor/cmaps/UniJISX02132004-UTF32-H.bcmap +0 -0
  240. package/vendor/cmaps/UniJISX02132004-UTF32-V.bcmap +0 -0
  241. package/vendor/cmaps/UniKS-UCS2-H.bcmap +0 -0
  242. package/vendor/cmaps/UniKS-UCS2-V.bcmap +0 -0
  243. package/vendor/cmaps/UniKS-UTF16-H.bcmap +0 -0
  244. package/vendor/cmaps/UniKS-UTF16-V.bcmap +0 -0
  245. package/vendor/cmaps/UniKS-UTF32-H.bcmap +0 -0
  246. package/vendor/cmaps/UniKS-UTF32-V.bcmap +0 -0
  247. package/vendor/cmaps/UniKS-UTF8-H.bcmap +0 -0
  248. package/vendor/cmaps/UniKS-UTF8-V.bcmap +0 -0
  249. package/vendor/cmaps/V.bcmap +0 -0
  250. package/vendor/cmaps/WP-Symbol.bcmap +0 -0
  251. package/vendor/fonts/excalifont/Excalifont-Regular-349fac6ca4700ffec595a7150a0d1e1d.woff2 +0 -0
  252. package/vendor/fonts/excalifont/Excalifont-Regular-3f2c5db56cc93c5a6873b1361d730c16.woff2 +0 -0
  253. package/vendor/fonts/excalifont/Excalifont-Regular-41b173a47b57366892116a575a43e2b6.woff2 +0 -0
  254. package/vendor/fonts/excalifont/Excalifont-Regular-623ccf21b21ef6b3a0d87738f77eb071.woff2 +0 -0
  255. package/vendor/fonts/excalifont/Excalifont-Regular-a88b72a24fb54c9f94e3b5fdaa7481c9.woff2 +0 -0
  256. package/vendor/fonts/excalifont/Excalifont-Regular-b9dcf9d2e50a1eaf42fc664b50a3fd0d.woff2 +0 -0
  257. package/vendor/fonts/excalifont/Excalifont-Regular-be310b9bcd4f1a43f571c46df7809174.woff2 +0 -0
  258. package/vendor/fonts/excalifont/OFL.txt +98 -0
  259. package/vendor/fonts/zhhand/OFL.txt +99 -0
  260. package/vendor/fonts/zhhand/Xiaolai-Regular.ttf +0 -0
  261. package/vendor/pdf.min.mjs +29 -0
  262. package/vendor/pdf.worker.min.mjs +29 -0
  263. package/vendor/pdfjs-LICENSE.txt +177 -0
  264. package/vendor/rough/LICENSE +21 -0
  265. package/vendor/rough/rough.mjs +1 -0
  266. package/vendor/wasm/LICENSE_JBIG2 +196 -0
  267. package/vendor/wasm/LICENSE_OPENJPEG +39 -0
  268. package/vendor/wasm/LICENSE_PDFJS_JBIG2 +13 -0
  269. package/vendor/wasm/LICENSE_PDFJS_OPENJPEG +22 -0
  270. package/vendor/wasm/LICENSE_PDFJS_QCMS +19 -0
  271. package/vendor/wasm/LICENSE_QCMS +21 -0
  272. package/vendor/wasm/jbig2.wasm +0 -0
  273. package/vendor/wasm/jbig2_nowasm_fallback.js +15 -0
  274. package/vendor/wasm/openjpeg.wasm +0 -0
  275. package/vendor/wasm/openjpeg_nowasm_fallback.js +17 -0
  276. package/vendor/wasm/qcms_bg.wasm +0 -0
  277. package/vendor/wasm/quickjs-eval.wasm +0 -0
  278. package//345/256/211/350/243/205/344/270/216/345/210/206/345/217/221.md +54 -0
@@ -0,0 +1,124 @@
1
+ /**
2
+ * HTTP plumbing for the `/pdfdesk/*` surface.
3
+ *
4
+ * Mirrors the guards an installed plugin uses: loopback-only reads, same-origin
5
+ * mutations, a tiny JSON body cap, and `{ ok, error }` envelopes.
6
+ *
7
+ * @module dsh-pdf-workbench/http
8
+ */
9
+
10
+ /** Request bodies are a position, not data; anything larger is a client bug. */
11
+ const MAX_BODY_BYTES = 16 * 1024
12
+
13
+ /**
14
+ * Whether the request came from the local machine.
15
+ * @param {import('node:http').IncomingMessage} request - the inbound request.
16
+ * @returns {boolean} true for loopback peers.
17
+ */
18
+ export function isLoopback(request) {
19
+ const address = request.socket?.remoteAddress
20
+ if (address === undefined) return true
21
+ return address === '127.0.0.1'
22
+ || address === '::1'
23
+ || address === '::ffff:127.0.0.1'
24
+ || address.startsWith('127.')
25
+ }
26
+
27
+ /**
28
+ * Same-origin guard for mutations. A missing Origin is allowed (the Desktop
29
+ * shell strips it); a present Origin must match the Host header.
30
+ * @param {import('node:http').IncomingMessage} request - the inbound request.
31
+ * @returns {boolean} true when the request may mutate.
32
+ */
33
+ export function isSameOrigin(request) {
34
+ const origin = request.headers.origin
35
+ if (typeof origin !== 'string' || origin === '') return true
36
+ const host = request.headers.host
37
+ if (typeof host !== 'string' || host === '') return false
38
+ try {
39
+ return new URL(origin).host === host
40
+ } catch {
41
+ return false
42
+ }
43
+ }
44
+
45
+ /**
46
+ * Send one JSON response.
47
+ * @param {import('node:http').ServerResponse} response - the outbound response.
48
+ * @param {number} status - HTTP status.
49
+ * @param {unknown} payload - JSON-serializable body.
50
+ * @returns {void}
51
+ */
52
+ export function sendJson(response, status, payload) {
53
+ if (response.writableEnded || response.destroyed) return
54
+ const body = JSON.stringify(payload)
55
+ response.writeHead(status, {
56
+ 'content-type': 'application/json; charset=utf-8',
57
+ 'cache-control': 'no-store',
58
+ 'content-length': Buffer.byteLength(body),
59
+ })
60
+ response.end(body)
61
+ }
62
+
63
+ /**
64
+ * Send one binary response (the reader's PDF bytes, the toolkit archive).
65
+ * @param {import('node:http').ServerResponse} response - the outbound response.
66
+ * @param {number} status - HTTP status.
67
+ * @param {Buffer} body - the bytes.
68
+ * @param {string} contentType - the media type.
69
+ * @param {string} [disposition] - optional `content-disposition` value.
70
+ * @returns {void}
71
+ */
72
+ export function sendBytes(response, status, body, contentType, disposition) {
73
+ if (response.writableEnded || response.destroyed) return
74
+ const headers = {
75
+ 'content-type': contentType,
76
+ 'cache-control': 'no-store',
77
+ 'accept-ranges': 'none',
78
+ 'content-length': body.length,
79
+ }
80
+ if (disposition !== undefined) headers['content-disposition'] = disposition
81
+ response.writeHead(status, headers)
82
+ response.end(body)
83
+ }
84
+
85
+ /**
86
+ * Send one failure envelope.
87
+ * @param {import('node:http').ServerResponse} response - the outbound response.
88
+ * @param {number} status - HTTP status.
89
+ * @param {string} code - stable machine code.
90
+ * @param {string} message - human-readable message.
91
+ * @returns {void}
92
+ */
93
+ export function fail(response, status, code, message) {
94
+ sendJson(response, status, { ok: false, error: { code, message } })
95
+ }
96
+
97
+ /**
98
+ * Read a bounded JSON request body.
99
+ * @param {import('node:http').IncomingMessage} request - the inbound request.
100
+ * @returns {Promise<unknown>} the parsed body.
101
+ */
102
+ export function readJson(request, limit = MAX_BODY_BYTES) {
103
+ return new Promise((resolve, reject) => {
104
+ const chunks = []
105
+ let size = 0
106
+ request.on('data', chunk => {
107
+ size += chunk.length
108
+ if (size > limit) {
109
+ reject(new Error('请求体过大'))
110
+ request.destroy()
111
+ return
112
+ }
113
+ chunks.push(chunk)
114
+ })
115
+ request.on('end', () => {
116
+ try {
117
+ resolve(JSON.parse(Buffer.concat(chunks).toString('utf8')))
118
+ } catch {
119
+ reject(new Error('请求体不是合法 JSON'))
120
+ }
121
+ })
122
+ request.on('error', reject)
123
+ })
124
+ }
@@ -0,0 +1,488 @@
1
+ /**
2
+ * The book → chapter catalog.
3
+ *
4
+ * `索引.json` is the authored source when it exists; otherwise the catalog is
5
+ * derived from the chapter-split PDF file names (`<short>_cNN_<title>.pdf`).
6
+ * Either way the browser receives one canonical shape and never sees paths it
7
+ * cannot use. A catalog that could not be read at all reports a `problem`
8
+ * instead of looking like an empty library.
9
+ *
10
+ * @module dsh-pdf-workbench/index-source
11
+ */
12
+ import { readdir, readFile } from 'node:fs/promises'
13
+ import { basename, join } from 'node:path'
14
+
15
+ /** A chapter code is `c` plus two or three digits; the title part is optional. */
16
+ const CHAPTER_FILE = /^(?<short>.+?)_(?<code>c\d{2,3})(?:_(?<rest>.*))?\.pdf$/i
17
+
18
+ /** A split file the splitter named with a page range but no `cNN` marker. */
19
+ const RANGE_FILE = /^(?<short>[^_]+?)_(?:(?:第[^_]*?章)_)?(?<rest>.*?)(?:_p|_)?(?<from>\d{1,4})-(?<to>\d{1,4})\.pdf$/i
20
+
21
+ /** Canonical chapter code, lowercase and exactly `c` + two or three digits. */
22
+ const CODE_SHAPE = /^c\d{2,3}$/
23
+
24
+ /**
25
+ * Canonicalize one chapter code.
26
+ * @param {unknown} raw - code from a file name or the authored index.
27
+ * @returns {string | null} the lowercase code, or null when it is not chapter-shaped.
28
+ */
29
+ function normalizeCode(raw) {
30
+ if (typeof raw !== 'string') return null
31
+ const code = raw.trim().toLowerCase()
32
+ return CODE_SHAPE.test(code) ? code : null
33
+ }
34
+
35
+ /** A trailing (or final) `_p123-456` page range; anything after it joins the title. */
36
+ const PAGE_RANGE = /^(?<title>.*?)_p(?<from>\d+)-(?<to>\d+)(?<tail>.*)$/i
37
+
38
+ /** A parenthetical group, half or full width. */
39
+ const PAREN_GROUP = /[((]([^(())]*)[))]/g
40
+
41
+ /** Parentheticals containing these are notes, not authors. */
42
+ const NOTE_WORDS = /第|修订|版|卷|册|学习|指导/
43
+
44
+ /**
45
+ * Classify one chapter code.
46
+ * @param {string} code - `cNN`.
47
+ * @returns {'preface' | 'appendix' | 'bibliography' | 'chapter'} the kind.
48
+ */
49
+ function kindOf(code) {
50
+ if (code === 'c00') return 'preface'
51
+ if (code === 'c99') return 'bibliography'
52
+ if (/^c9\d$/.test(code)) return 'appendix'
53
+ return 'chapter'
54
+ }
55
+
56
+ /**
57
+ * Numeric order of a chapter code, so `c100` sorts after `c99`.
58
+ * @param {string} code - `cNN`.
59
+ * @returns {number} the numeric part, or a large number for foreign codes.
60
+ */
61
+ function codeOrder(code) {
62
+ const digits = Number(code.slice(1))
63
+ return Number.isFinite(digits) ? digits : Number.MAX_SAFE_INTEGER
64
+ }
65
+
66
+ /**
67
+ * Turn a file-name fragment into a readable chapter title.
68
+ * @param {string} raw - the part between the code and the page range.
69
+ * @returns {string} the title.
70
+ */
71
+ function tidyChapterTitle(raw) {
72
+ return raw
73
+ .replace(/(Chapter)([IVXLCDM]+)\b/gi, '$1 $2')
74
+ .replace(/_/g, ' ')
75
+ .replace(/\s{2,}/g, ' ')
76
+ .trim()
77
+ }
78
+
79
+ /**
80
+ * Parse one split PDF file name.
81
+ * @param {string} fileName - base name of the PDF.
82
+ * @returns {{ short: string, code: string, title: string, pages: string, kind: string } | null} the chapter, or null when the name is not chapter-shaped.
83
+ */
84
+ export function parseChapterFile(fileName) {
85
+ const match = CHAPTER_FILE.exec(fileName)
86
+ if (match === null) {
87
+ // Splitters commonly emit `<book>_<title>_<from>-<to>.pdf` without a code:
88
+ // accept it and let the scan number the chapters by page order.
89
+ const loose = RANGE_FILE.exec(fileName)
90
+ if (loose === null) return null
91
+ const from = Number(loose.groups.from)
92
+ const to = Number(loose.groups.to)
93
+ if (!Number.isFinite(from) || !Number.isFinite(to) || from <= 0 || to < from) return null
94
+ return {
95
+ short: '',
96
+ code: '',
97
+ title: tidyChapterTitle(loose.groups.rest ?? ''),
98
+ pages: `${from}-${to}`,
99
+ kind: 'body',
100
+ from,
101
+ }
102
+ }
103
+ const code = normalizeCode(match.groups.code)
104
+ if (code === null) return null
105
+ let rest = match.groups.rest ?? ''
106
+ let pages = ''
107
+ const range = PAGE_RANGE.exec(rest)
108
+ if (range !== null) {
109
+ pages = `${Number(range.groups.from)}-${Number(range.groups.to)}`
110
+ rest = `${range.groups.title}${range.groups.tail ?? ''}`
111
+ }
112
+ return { short: match.groups.short.trim(), code, title: tidyChapterTitle(rest), pages, kind: kindOf(code) }
113
+ }
114
+
115
+ /**
116
+ * Drop vendor/scanning parentheticals, bare numeric markers and stray punctuation.
117
+ * @param {string} dirName - the book directory name.
118
+ * @returns {string} the cleaned directory name.
119
+ */
120
+ function cleanBookDirectoryName(dirName) {
121
+ let text = dirName.trim()
122
+ for (let round = 0; round < 6; round += 1) {
123
+ const next = text.replace(PAREN_GROUP, (all, inner) => {
124
+ const content = String(inner).trim()
125
+ if (/^(z-library|1lib|z-lib|zlib)/i.test(content)) return ''
126
+ if (/^Z-Library$/i.test(content)) return ''
127
+ if (/^OCR$/i.test(content)) return ''
128
+ if (/^\d{1,2}$/.test(content)) return ''
129
+ return all
130
+ })
131
+ if (next === text) break
132
+ text = next
133
+ }
134
+ text = text.replace(/\s{2,}/g, ' ').trim()
135
+ const opens = (text.match(/[((]/g) ?? []).length
136
+ const closes = (text.match(/[))]/g) ?? []).length
137
+ if (opens > closes) text = text.replace(/[((][^(())]*$/, '').trim()
138
+ return text.replace(/[\s,,]+$/g, '').trim()
139
+ }
140
+
141
+ /**
142
+ * Whether a parenthetical reads like an author list rather than a note.
143
+ * @param {string} text - the parenthetical content.
144
+ * @returns {boolean} true when it can be shown as the author.
145
+ */
146
+ function looksLikeAuthor(text) {
147
+ if (!/^[\p{L}][\p{L}\p{N}.\s·\-]{1,39}$/u.test(text)) return false
148
+ if (/[,,、;;::]/.test(text)) return false
149
+ if (NOTE_WORDS.test(text)) return false
150
+ return true
151
+ }
152
+
153
+ /**
154
+ * Split a book directory name into a display title and an author.
155
+ * @param {string} dirName - the book directory name.
156
+ * @returns {{ title: string, author: string }} display fields.
157
+ */
158
+ export function splitBookTitle(dirName) {
159
+ const cleaned = cleanBookDirectoryName(dirName)
160
+ const groups = [...cleaned.matchAll(/[((]([^(())]+)[))]/g)]
161
+ for (let index = groups.length - 1; index >= 0; index -= 1) {
162
+ const inner = groups[index][1].trim()
163
+ if (!looksLikeAuthor(inner)) continue
164
+ const start = groups[index].index ?? 0
165
+ const title = (cleaned.slice(0, start) + cleaned.slice(start + groups[index][0].length))
166
+ .replace(/\s{2,}/g, ' ')
167
+ .trim()
168
+ return { title, author: inner }
169
+ }
170
+ return { title: cleaned, author: '' }
171
+ }
172
+
173
+ /**
174
+ * Normalize, de-duplicate and order one book's chapters.
175
+ * @param {object[]} chapters - candidate chapters in file order.
176
+ * @returns {{ chapters: object[], duplicates: number }} the rows and how many duplicate codes were dropped.
177
+ */
178
+ function normalizeChapters(chapters) {
179
+ const byCode = new Map()
180
+ let duplicates = 0
181
+ for (const chapter of chapters) {
182
+ if (chapter === null || typeof chapter !== 'object') continue
183
+ if (typeof chapter.code !== 'string' || chapter.code === '') continue
184
+ const row = {
185
+ code: chapter.code,
186
+ title: typeof chapter.title === 'string' ? chapter.title : '',
187
+ kind: typeof chapter.kind === 'string' && chapter.kind !== '' ? chapter.kind : kindOf(chapter.code),
188
+ pages: typeof chapter.pages === 'string' ? chapter.pages : '',
189
+ file: typeof chapter.file === 'string' ? chapter.file : '',
190
+ }
191
+ if (byCode.has(row.code)) duplicates += 1
192
+ byCode.set(row.code, row)
193
+ }
194
+ const rows = [...byCode.values()].sort((left, right) => codeOrder(left.code) - codeOrder(right.code))
195
+ return { chapters: rows, duplicates }
196
+ }
197
+
198
+ /**
199
+ * Normalize one book row into the shape the browser renders.
200
+ * @param {{ id: string, title: string, author?: string, dir?: string, cover?: string, chapters: object[], unparsed?: number }} book - assembled fields.
201
+ * @returns {object} the row.
202
+ */
203
+ function bookRow(book) {
204
+ const { chapters, duplicates } = normalizeChapters(book.chapters)
205
+ return {
206
+ id: book.id,
207
+ title: book.title === '' ? book.id : book.title,
208
+ author: typeof book.author === 'string' ? book.author : '',
209
+ dir: typeof book.dir === 'string' ? book.dir : '',
210
+ cover: typeof book.cover === 'string' ? book.cover : '',
211
+ unparsed: typeof book.unparsed === 'number' ? book.unparsed : 0,
212
+ duplicates,
213
+ chapters,
214
+ }
215
+ }
216
+
217
+ /** Short stable hash used to disambiguate two books that share a file-name short name. */
218
+ function shortHash(text) {
219
+ let hash = 0x811c9dc5
220
+ for (let index = 0; index < text.length; index += 1) {
221
+ hash ^= text.charCodeAt(index)
222
+ hash = Math.imul(hash, 0x01000193) >>> 0
223
+ }
224
+ return hash.toString(36).slice(0, 6)
225
+ }
226
+
227
+ /**
228
+ * Keep book ids unique; a later book that repeats a short name gets a stable
229
+ * directory hash, while the first keeps the plain id so an existing catalog
230
+ * does not change ids when an unrelated duplicate folder appears.
231
+ * @param {object[]} books - rows built from either source.
232
+ * @returns {object[]} rows with unique ids.
233
+ */
234
+ function assignUniqueIds(books) {
235
+ const seen = new Set()
236
+ return books.map(book => {
237
+ if (!seen.has(book.id)) {
238
+ seen.add(book.id)
239
+ return book
240
+ }
241
+ const unique = `${book.id}@${shortHash(book.dir === '' ? book.title : book.dir)}`
242
+ seen.add(unique)
243
+ return { ...book, id: unique }
244
+ })
245
+ }
246
+
247
+ /**
248
+ * Read and normalize an authored index file.
249
+ * @param {string} indexPath - absolute path of `索引.json`.
250
+ * @returns {Promise<{ status: 'ok', books: object[], warnings: string[] } | { status: 'missing' } | { status: 'invalid', warnings: string[] }>} the outcome.
251
+ */
252
+ async function readIndexFile(indexPath) {
253
+ let text
254
+ try {
255
+ text = await readFile(indexPath, 'utf8')
256
+ } catch {
257
+ return { status: 'missing' }
258
+ }
259
+ let parsed
260
+ try {
261
+ parsed = JSON.parse(text)
262
+ } catch (error) {
263
+ return { status: 'invalid', warnings: [`索引.json 无法解析,已改为按文件名推导:${String(error)}`] }
264
+ }
265
+ if (parsed === null || typeof parsed !== 'object' || !Array.isArray(parsed.books)) {
266
+ return { status: 'invalid', warnings: ['索引.json 缺少 books 数组,已改为按文件名推导。'] }
267
+ }
268
+ const warnings = []
269
+ const books = []
270
+ for (const entry of parsed.books) {
271
+ if (entry === null || typeof entry !== 'object' || typeof entry.id !== 'string' || entry.id === '') {
272
+ warnings.push('索引.json 里有一本书缺少 id,已跳过。')
273
+ continue
274
+ }
275
+ const chapters = Array.isArray(entry.chapters) ? entry.chapters : []
276
+ const normalizedChapters = []
277
+ for (const chapter of chapters) {
278
+ const code = normalizeCode(chapter?.code)
279
+ if (code === null) {
280
+ warnings.push(`索引.json 里 ${entry.id} 有一个章号不是 cNN,已跳过:${String(chapter?.code)}`)
281
+ continue
282
+ }
283
+ const pdf = chapter !== null && typeof chapter === 'object' && typeof chapter.pdf === 'string' ? chapter.pdf : ''
284
+ normalizedChapters.push({
285
+ code,
286
+ title: chapter?.title,
287
+ pages: chapter?.pages,
288
+ file: pdf === '' ? '' : basename(pdf).replace(/\.pdf$/i, ''),
289
+ })
290
+ }
291
+ books.push(bookRow({
292
+ id: entry.id,
293
+ title: typeof entry.title === 'string' ? entry.title : '',
294
+ author: entry.author,
295
+ dir: entry.dir,
296
+ cover: entry.cover,
297
+ chapters: normalizedChapters,
298
+ }))
299
+ }
300
+ return { status: 'ok', books: assignUniqueIds(books), warnings }
301
+ }
302
+
303
+ /**
304
+ * Derive the catalog from the split library on disk.
305
+ * @param {string} fragmentDir - absolute path of the `分片` folder.
306
+ * @returns {Promise<{ books: object[], skipped: object[], warnings: string[], readable: boolean }>} the derived catalog.
307
+ */
308
+ async function scanFragments(fragmentDir) {
309
+ const warnings = []
310
+ const skipped = []
311
+ let entries
312
+ try {
313
+ entries = await readdir(fragmentDir, { withFileTypes: true })
314
+ } catch (error) {
315
+ const missing = error !== null && typeof error === 'object' && error.code === 'ENOENT'
316
+ return {
317
+ books: [],
318
+ skipped,
319
+ readable: false,
320
+ missing,
321
+ warnings: missing ? [] : [`读不到分片目录 ${fragmentDir}:${String(error && error.message ? error.message : error)}`],
322
+ }
323
+ }
324
+ const books = []
325
+ for (const entry of entries) {
326
+ if (!entry.isDirectory() && !entry.isSymbolicLink()) continue
327
+ const directory = join(fragmentDir, entry.name)
328
+ let files
329
+ try {
330
+ files = await readdir(directory)
331
+ } catch {
332
+ warnings.push(`跳过无法读取的目录:${entry.name}`)
333
+ continue
334
+ }
335
+ const chapters = []
336
+ let short = ''
337
+ let pdfCount = 0
338
+ let unparsed = 0
339
+ for (const file of files) {
340
+ if (!file.toLowerCase().endsWith('.pdf')) continue
341
+ // Translation output lives beside the chapter PDF; it is not a chapter.
342
+ if (/_translated\.pdf$/i.test(file) || /_overlay\.pdf$/i.test(file)) continue
343
+ pdfCount += 1
344
+ const parsed = parseChapterFile(file)
345
+ if (parsed === null) {
346
+ unparsed += 1
347
+ continue
348
+ }
349
+ if (short === '') short = parsed.short
350
+ chapters.push({
351
+ code: parsed.code,
352
+ title: parsed.title,
353
+ kind: parsed.kind,
354
+ pages: parsed.pages,
355
+ file: file.replace(/\.pdf$/i, ''),
356
+ from: parsed.from,
357
+ })
358
+ }
359
+ if (chapters.length === 0) {
360
+ if (pdfCount > 0) skipped.push({ dir: entry.name, pdfCount })
361
+ continue
362
+ }
363
+ // Files the splitter left without `cNN`: number them by page order, with the
364
+ // cover/contents pair going to c00 and appendices to c90+.
365
+ const taken = new Set(chapters.filter(chapter => chapter.code !== '').map(chapter => chapter.code))
366
+ let bodyIndex = 1
367
+ const loose = chapters.filter(chapter => chapter.code === '')
368
+ loose.sort((left, right) => (left.from ?? 0) - (right.from ?? 0))
369
+ for (const chapter of loose) {
370
+ const text = `${chapter.title} ${chapter.file}`
371
+ let code
372
+ if (/附录|appendix/i.test(text)) {
373
+ let index = 90
374
+ while (taken.has(`c${index}`)) index += 1
375
+ code = `c${index}`
376
+ } else if (/目录|前言|封面|序言|序$|索引|参考/i.test(text)) {
377
+ code = 'c00'
378
+ } else {
379
+ while (taken.has(`c${String(bodyIndex).padStart(2, '0')}`)) bodyIndex += 1
380
+ code = `c${String(bodyIndex).padStart(2, '0')}`
381
+ bodyIndex += 1
382
+ }
383
+ chapter.code = code
384
+ chapter.kind = kindOf(code)
385
+ taken.add(code)
386
+ }
387
+ for (const chapter of chapters) delete chapter.from
388
+ chapters.sort((left, right) => left.code.localeCompare(right.code))
389
+ const { title, author } = splitBookTitle(entry.name)
390
+ books.push(bookRow({
391
+ id: short === '' ? entry.name : short,
392
+ title,
393
+ author,
394
+ dir: entry.name,
395
+ unparsed,
396
+ chapters,
397
+ }))
398
+ }
399
+ books.sort((left, right) => left.title.localeCompare(right.title, 'zh'))
400
+ return { books: assignUniqueIds(books), skipped, warnings, readable: true }
401
+ }
402
+
403
+ /**
404
+ * Whole-book PDFs: a library may also carry unsplit books under `整本\`, for
405
+ * people who cannot run the splitter. Each file is one book with a single
406
+ * synthetic chapter (`c00`), so reading works but there are no chapter rows.
407
+ * @param {string} libraryDir - the library root.
408
+ * @returns {Promise<{ books: object[], readable: boolean }>} one book per whole PDF.
409
+ */
410
+ async function scanWholeBooks(libraryDir) {
411
+ const found = []
412
+ let readable = false
413
+ // `整本\` first so it wins when the same stem exists at the root too.
414
+ for (const [dir, at] of [[join(libraryDir, '整本'), 'whole'], [libraryDir, 'root']]) {
415
+ let names
416
+ try {
417
+ names = await readdir(dir)
418
+ } catch {
419
+ continue
420
+ }
421
+ readable = true
422
+ for (const name of names) {
423
+ if (!/\.pdf$/i.test(name)) continue
424
+ const stem = name.replace(/\.pdf$/i, '').trim()
425
+ if (stem === '') continue
426
+ found.push({ stem, at })
427
+ }
428
+ }
429
+ const books = []
430
+ const seen = new Set()
431
+ for (const { stem, at } of found) {
432
+ if (seen.has(stem)) continue
433
+ seen.add(stem)
434
+ books.push({
435
+ id: stem,
436
+ title: stem,
437
+ author: '',
438
+ whole: true,
439
+ chapters: [{
440
+ code: 'c00',
441
+ title: '整本',
442
+ file: stem,
443
+ whole: true,
444
+ wholeAt: at,
445
+ pages: '',
446
+ kind: 'chapter',
447
+ }],
448
+ })
449
+ }
450
+ return { books, readable }
451
+ }
452
+
453
+ /**
454
+ * Load the current catalog: authored index first, file-name derivation second.
455
+ * @param {{ indexPath: string, fragmentDir: string, libraryDir?: string }} config - resolved paths.
456
+ * @returns {Promise<{ source: 'index.json' | 'scan', indexIgnored: boolean, books: object[], skipped: object[], warnings: string[], problem: { code: string, message: string } | null }>} the catalog, how it was obtained, and any blocking problem.
457
+ */
458
+ export async function loadCatalog(config) {
459
+ const fromIndex = await readIndexFile(config.indexPath)
460
+ const scanned = await scanFragments(config.fragmentDir)
461
+ const whole = config.libraryDir === undefined ? { books: [], readable: false } : await scanWholeBooks(config.libraryDir)
462
+ // A library may legitimately hold only whole-book PDFs: `分片` missing is not
463
+ // an error then. It is only a problem when neither layout is readable.
464
+ const readable = scanned.readable || whole.readable
465
+ const warnings = [
466
+ ...(fromIndex.status === 'invalid' ? fromIndex.warnings : []),
467
+ ...scanned.warnings,
468
+ ...(scanned.missing === true && whole.readable ? ['没有分片目录,只显示整本 PDF。'] : []),
469
+ ]
470
+ if (fromIndex.status === 'ok') {
471
+ return {
472
+ source: 'index.json',
473
+ indexIgnored: false,
474
+ books: assignUniqueIds([...fromIndex.books, ...whole.books]),
475
+ skipped: scanned.skipped,
476
+ warnings: [...fromIndex.warnings, ...warnings],
477
+ problem: null,
478
+ }
479
+ }
480
+ return {
481
+ source: 'scan',
482
+ indexIgnored: fromIndex.status === 'invalid',
483
+ books: assignUniqueIds([...scanned.books, ...whole.books]),
484
+ skipped: scanned.skipped,
485
+ warnings,
486
+ problem: readable ? null : { code: 'fragment-dir-unreadable', message: scanned.warnings[0] ?? `读不到分片目录 ${config.fragmentDir}` },
487
+ }
488
+ }