dsh-pdf-workbench 0.0.0-stage → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +162 -2
- package/assets/screenshot-bilingual.jpg +0 -0
- package/assets/screenshot-hero.jpg +0 -0
- package/assets/screenshot-overlay.jpg +0 -0
- package/assets/screenshot-qa.jpg +0 -0
- package/assets/screenshot-reader.jpg +0 -0
- package/assets/screenshot-shelf.jpg +0 -0
- package/client.js +13438 -0
- package/cordis.patch.yml +13 -0
- package/docs/DETAIL.md +69 -0
- package/docs/READER-REWRITE.md +95 -0
- package/docs/ROADMAP.md +172 -0
- package/docs/SIDE-PREVIEW.md +271 -0
- package/docs/TESTING.md +109 -0
- package/docs/TRANSLATION.md +167 -0
- package/icon.svg +6 -0
- package/index.js +192 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +64 -6
- package/samples/manifest.json +52 -0
- package/samples/test/serre_c01_p2-3.pdf +0 -0
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- package/screenshots.json +8 -0
- package/src/client/viewer.js +80 -0
- package/src/host/annotations.js +145 -0
- package/src/host/artifacts.js +317 -0
- package/src/host/blocks.js +44 -0
- package/src/host/config.js +112 -0
- package/src/host/digest.js +156 -0
- package/src/host/environment.js +42 -0
- package/src/host/http.js +124 -0
- package/src/host/index-source.js +488 -0
- package/src/host/libraries.js +224 -0
- package/src/host/links.js +201 -0
- package/src/host/pdf-source.js +122 -0
- package/src/host/pending.js +166 -0
- package/src/host/pressure.js +137 -0
- package/src/host/purge.js +100 -0
- package/src/host/render.js +43 -0
- package/src/host/routes.js +1732 -0
- package/src/host/shelf-groups.js +109 -0
- package/src/host/state.js +128 -0
- package/src/host/toolkit.js +54 -0
- package/src/host/translate-blocks.js +132 -0
- package/src/host/translate-job.js +316 -0
- package/src/host/translate.js +94 -0
- package/src/host/update.js +134 -0
- package/tools/AGENTS.md +33 -0
- package/tools/README.md +138 -0
- package/tools/auto_chapters.py +122 -0
- package/tools/dsfile.mjs +198 -0
- package/tools/export_chapter.py +108 -0
- package/tools/export_page.py +91 -0
- package/tools/md2overlay.py +200 -0
- package/tools/md2tex.py +422 -0
- package/tools/pdf_blocks.py +110 -0
- package/tools/pdf_fontsize.py +20 -0
- package/tools/pdf_merge.py +15 -0
- package/tools/pdf_pagesizes.py +8 -0
- package/tools/pdf_raster.py +21 -0
- package/tools/pdfkit.py +896 -0
- package/tools/realign_overlay.py +248 -0
- package/tools/rebuild_overlay.py +55 -0
- package/tools/rename_chapters.py +93 -0
- package/tools//350/247/204/350/214/203/00_/345/205/261/345/220/214/347/272/246/345/256/232.md +142 -0
- package/tools//350/247/204/350/214/203/01_/346/226/207/346/234/254/346/217/220/345/217/226.md +204 -0
- package/tools//350/247/204/350/214/203/02_/347/255/224/347/226/221.md +61 -0
- package/tools//350/247/204/350/214/203/03_/344/271/240/351/242/230/350/247/243/347/255/224.md +77 -0
- 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
- package/vendor/cmaps/78-EUC-H.bcmap +0 -0
- package/vendor/cmaps/78-EUC-V.bcmap +0 -0
- package/vendor/cmaps/78-H.bcmap +0 -0
- package/vendor/cmaps/78-RKSJ-H.bcmap +0 -0
- package/vendor/cmaps/78-RKSJ-V.bcmap +0 -0
- package/vendor/cmaps/78-V.bcmap +0 -0
- package/vendor/cmaps/78ms-RKSJ-H.bcmap +0 -0
- package/vendor/cmaps/78ms-RKSJ-V.bcmap +0 -0
- package/vendor/cmaps/83pv-RKSJ-H.bcmap +0 -0
- package/vendor/cmaps/90ms-RKSJ-H.bcmap +0 -0
- package/vendor/cmaps/90ms-RKSJ-V.bcmap +0 -0
- package/vendor/cmaps/90msp-RKSJ-H.bcmap +0 -0
- package/vendor/cmaps/90msp-RKSJ-V.bcmap +0 -0
- package/vendor/cmaps/90pv-RKSJ-H.bcmap +0 -0
- package/vendor/cmaps/90pv-RKSJ-V.bcmap +0 -0
- package/vendor/cmaps/Add-H.bcmap +0 -0
- package/vendor/cmaps/Add-RKSJ-H.bcmap +0 -0
- package/vendor/cmaps/Add-RKSJ-V.bcmap +0 -0
- package/vendor/cmaps/Add-V.bcmap +0 -0
- package/vendor/cmaps/Adobe-CNS1-0.bcmap +0 -0
- package/vendor/cmaps/Adobe-CNS1-1.bcmap +0 -0
- package/vendor/cmaps/Adobe-CNS1-2.bcmap +0 -0
- package/vendor/cmaps/Adobe-CNS1-3.bcmap +0 -0
- package/vendor/cmaps/Adobe-CNS1-4.bcmap +0 -0
- package/vendor/cmaps/Adobe-CNS1-5.bcmap +0 -0
- package/vendor/cmaps/Adobe-CNS1-6.bcmap +0 -0
- package/vendor/cmaps/Adobe-CNS1-UCS2.bcmap +0 -0
- package/vendor/cmaps/Adobe-GB1-0.bcmap +0 -0
- package/vendor/cmaps/Adobe-GB1-1.bcmap +0 -0
- package/vendor/cmaps/Adobe-GB1-2.bcmap +0 -0
- package/vendor/cmaps/Adobe-GB1-3.bcmap +0 -0
- package/vendor/cmaps/Adobe-GB1-4.bcmap +0 -0
- package/vendor/cmaps/Adobe-GB1-5.bcmap +0 -0
- package/vendor/cmaps/Adobe-GB1-UCS2.bcmap +0 -0
- package/vendor/cmaps/Adobe-Japan1-0.bcmap +0 -0
- package/vendor/cmaps/Adobe-Japan1-1.bcmap +0 -0
- package/vendor/cmaps/Adobe-Japan1-2.bcmap +0 -0
- package/vendor/cmaps/Adobe-Japan1-3.bcmap +0 -0
- package/vendor/cmaps/Adobe-Japan1-4.bcmap +0 -0
- package/vendor/cmaps/Adobe-Japan1-5.bcmap +0 -0
- package/vendor/cmaps/Adobe-Japan1-6.bcmap +0 -0
- package/vendor/cmaps/Adobe-Japan1-UCS2.bcmap +0 -0
- package/vendor/cmaps/Adobe-Korea1-0.bcmap +0 -0
- package/vendor/cmaps/Adobe-Korea1-1.bcmap +0 -0
- package/vendor/cmaps/Adobe-Korea1-2.bcmap +0 -0
- package/vendor/cmaps/Adobe-Korea1-UCS2.bcmap +0 -0
- package/vendor/cmaps/B5-H.bcmap +0 -0
- package/vendor/cmaps/B5-V.bcmap +0 -0
- package/vendor/cmaps/B5pc-H.bcmap +0 -0
- package/vendor/cmaps/B5pc-V.bcmap +0 -0
- package/vendor/cmaps/CNS-EUC-H.bcmap +0 -0
- package/vendor/cmaps/CNS-EUC-V.bcmap +0 -0
- package/vendor/cmaps/CNS1-H.bcmap +0 -0
- package/vendor/cmaps/CNS1-V.bcmap +0 -0
- package/vendor/cmaps/CNS2-H.bcmap +0 -0
- package/vendor/cmaps/CNS2-V.bcmap +3 -0
- package/vendor/cmaps/ETHK-B5-H.bcmap +0 -0
- package/vendor/cmaps/ETHK-B5-V.bcmap +0 -0
- package/vendor/cmaps/ETen-B5-H.bcmap +0 -0
- package/vendor/cmaps/ETen-B5-V.bcmap +0 -0
- package/vendor/cmaps/ETenms-B5-H.bcmap +3 -0
- package/vendor/cmaps/ETenms-B5-V.bcmap +0 -0
- package/vendor/cmaps/EUC-H.bcmap +0 -0
- package/vendor/cmaps/EUC-V.bcmap +0 -0
- package/vendor/cmaps/Ext-H.bcmap +0 -0
- package/vendor/cmaps/Ext-RKSJ-H.bcmap +0 -0
- package/vendor/cmaps/Ext-RKSJ-V.bcmap +0 -0
- package/vendor/cmaps/Ext-V.bcmap +0 -0
- package/vendor/cmaps/GB-EUC-H.bcmap +0 -0
- package/vendor/cmaps/GB-EUC-V.bcmap +0 -0
- package/vendor/cmaps/GB-H.bcmap +4 -0
- package/vendor/cmaps/GB-V.bcmap +0 -0
- package/vendor/cmaps/GBK-EUC-H.bcmap +0 -0
- package/vendor/cmaps/GBK-EUC-V.bcmap +0 -0
- package/vendor/cmaps/GBK2K-H.bcmap +0 -0
- package/vendor/cmaps/GBK2K-V.bcmap +0 -0
- package/vendor/cmaps/GBKp-EUC-H.bcmap +0 -0
- package/vendor/cmaps/GBKp-EUC-V.bcmap +0 -0
- package/vendor/cmaps/GBT-EUC-H.bcmap +0 -0
- package/vendor/cmaps/GBT-EUC-V.bcmap +0 -0
- package/vendor/cmaps/GBT-H.bcmap +0 -0
- package/vendor/cmaps/GBT-V.bcmap +0 -0
- package/vendor/cmaps/GBTpc-EUC-H.bcmap +0 -0
- package/vendor/cmaps/GBTpc-EUC-V.bcmap +0 -0
- package/vendor/cmaps/GBpc-EUC-H.bcmap +0 -0
- package/vendor/cmaps/GBpc-EUC-V.bcmap +0 -0
- package/vendor/cmaps/H.bcmap +0 -0
- package/vendor/cmaps/HKdla-B5-H.bcmap +0 -0
- package/vendor/cmaps/HKdla-B5-V.bcmap +0 -0
- package/vendor/cmaps/HKdlb-B5-H.bcmap +0 -0
- package/vendor/cmaps/HKdlb-B5-V.bcmap +0 -0
- package/vendor/cmaps/HKgccs-B5-H.bcmap +0 -0
- package/vendor/cmaps/HKgccs-B5-V.bcmap +0 -0
- package/vendor/cmaps/HKm314-B5-H.bcmap +0 -0
- package/vendor/cmaps/HKm314-B5-V.bcmap +0 -0
- package/vendor/cmaps/HKm471-B5-H.bcmap +0 -0
- package/vendor/cmaps/HKm471-B5-V.bcmap +0 -0
- package/vendor/cmaps/HKscs-B5-H.bcmap +0 -0
- package/vendor/cmaps/HKscs-B5-V.bcmap +0 -0
- package/vendor/cmaps/Hankaku.bcmap +0 -0
- package/vendor/cmaps/Hiragana.bcmap +0 -0
- package/vendor/cmaps/KSC-EUC-H.bcmap +0 -0
- package/vendor/cmaps/KSC-EUC-V.bcmap +0 -0
- package/vendor/cmaps/KSC-H.bcmap +0 -0
- package/vendor/cmaps/KSC-Johab-H.bcmap +0 -0
- package/vendor/cmaps/KSC-Johab-V.bcmap +0 -0
- package/vendor/cmaps/KSC-V.bcmap +0 -0
- package/vendor/cmaps/KSCms-UHC-H.bcmap +0 -0
- package/vendor/cmaps/KSCms-UHC-HW-H.bcmap +0 -0
- package/vendor/cmaps/KSCms-UHC-HW-V.bcmap +0 -0
- package/vendor/cmaps/KSCms-UHC-V.bcmap +0 -0
- package/vendor/cmaps/KSCpc-EUC-H.bcmap +0 -0
- package/vendor/cmaps/KSCpc-EUC-V.bcmap +0 -0
- package/vendor/cmaps/Katakana.bcmap +0 -0
- package/vendor/cmaps/LICENSE.txt +36 -0
- package/vendor/cmaps/NWP-H.bcmap +0 -0
- package/vendor/cmaps/NWP-V.bcmap +0 -0
- package/vendor/cmaps/RKSJ-H.bcmap +0 -0
- package/vendor/cmaps/RKSJ-V.bcmap +0 -0
- package/vendor/cmaps/Roman.bcmap +0 -0
- package/vendor/cmaps/UniCNS-UCS2-H.bcmap +0 -0
- package/vendor/cmaps/UniCNS-UCS2-V.bcmap +0 -0
- package/vendor/cmaps/UniCNS-UTF16-H.bcmap +0 -0
- package/vendor/cmaps/UniCNS-UTF16-V.bcmap +0 -0
- package/vendor/cmaps/UniCNS-UTF32-H.bcmap +0 -0
- package/vendor/cmaps/UniCNS-UTF32-V.bcmap +0 -0
- package/vendor/cmaps/UniCNS-UTF8-H.bcmap +0 -0
- package/vendor/cmaps/UniCNS-UTF8-V.bcmap +0 -0
- package/vendor/cmaps/UniGB-UCS2-H.bcmap +0 -0
- package/vendor/cmaps/UniGB-UCS2-V.bcmap +0 -0
- package/vendor/cmaps/UniGB-UTF16-H.bcmap +0 -0
- package/vendor/cmaps/UniGB-UTF16-V.bcmap +0 -0
- package/vendor/cmaps/UniGB-UTF32-H.bcmap +0 -0
- package/vendor/cmaps/UniGB-UTF32-V.bcmap +0 -0
- package/vendor/cmaps/UniGB-UTF8-H.bcmap +0 -0
- package/vendor/cmaps/UniGB-UTF8-V.bcmap +0 -0
- package/vendor/cmaps/UniJIS-UCS2-H.bcmap +0 -0
- package/vendor/cmaps/UniJIS-UCS2-HW-H.bcmap +0 -0
- package/vendor/cmaps/UniJIS-UCS2-HW-V.bcmap +0 -0
- package/vendor/cmaps/UniJIS-UCS2-V.bcmap +0 -0
- package/vendor/cmaps/UniJIS-UTF16-H.bcmap +0 -0
- package/vendor/cmaps/UniJIS-UTF16-V.bcmap +0 -0
- package/vendor/cmaps/UniJIS-UTF32-H.bcmap +0 -0
- package/vendor/cmaps/UniJIS-UTF32-V.bcmap +0 -0
- package/vendor/cmaps/UniJIS-UTF8-H.bcmap +0 -0
- package/vendor/cmaps/UniJIS-UTF8-V.bcmap +0 -0
- package/vendor/cmaps/UniJIS2004-UTF16-H.bcmap +0 -0
- package/vendor/cmaps/UniJIS2004-UTF16-V.bcmap +0 -0
- package/vendor/cmaps/UniJIS2004-UTF32-H.bcmap +0 -0
- package/vendor/cmaps/UniJIS2004-UTF32-V.bcmap +0 -0
- package/vendor/cmaps/UniJIS2004-UTF8-H.bcmap +0 -0
- package/vendor/cmaps/UniJIS2004-UTF8-V.bcmap +0 -0
- package/vendor/cmaps/UniJISPro-UCS2-HW-V.bcmap +0 -0
- package/vendor/cmaps/UniJISPro-UCS2-V.bcmap +0 -0
- package/vendor/cmaps/UniJISPro-UTF8-V.bcmap +0 -0
- package/vendor/cmaps/UniJISX0213-UTF32-H.bcmap +0 -0
- package/vendor/cmaps/UniJISX0213-UTF32-V.bcmap +0 -0
- package/vendor/cmaps/UniJISX02132004-UTF32-H.bcmap +0 -0
- package/vendor/cmaps/UniJISX02132004-UTF32-V.bcmap +0 -0
- package/vendor/cmaps/UniKS-UCS2-H.bcmap +0 -0
- package/vendor/cmaps/UniKS-UCS2-V.bcmap +0 -0
- package/vendor/cmaps/UniKS-UTF16-H.bcmap +0 -0
- package/vendor/cmaps/UniKS-UTF16-V.bcmap +0 -0
- package/vendor/cmaps/UniKS-UTF32-H.bcmap +0 -0
- package/vendor/cmaps/UniKS-UTF32-V.bcmap +0 -0
- package/vendor/cmaps/UniKS-UTF8-H.bcmap +0 -0
- package/vendor/cmaps/UniKS-UTF8-V.bcmap +0 -0
- package/vendor/cmaps/V.bcmap +0 -0
- package/vendor/cmaps/WP-Symbol.bcmap +0 -0
- package/vendor/fonts/excalifont/Excalifont-Regular-349fac6ca4700ffec595a7150a0d1e1d.woff2 +0 -0
- package/vendor/fonts/excalifont/Excalifont-Regular-3f2c5db56cc93c5a6873b1361d730c16.woff2 +0 -0
- package/vendor/fonts/excalifont/Excalifont-Regular-41b173a47b57366892116a575a43e2b6.woff2 +0 -0
- package/vendor/fonts/excalifont/Excalifont-Regular-623ccf21b21ef6b3a0d87738f77eb071.woff2 +0 -0
- package/vendor/fonts/excalifont/Excalifont-Regular-a88b72a24fb54c9f94e3b5fdaa7481c9.woff2 +0 -0
- package/vendor/fonts/excalifont/Excalifont-Regular-b9dcf9d2e50a1eaf42fc664b50a3fd0d.woff2 +0 -0
- package/vendor/fonts/excalifont/Excalifont-Regular-be310b9bcd4f1a43f571c46df7809174.woff2 +0 -0
- package/vendor/fonts/excalifont/OFL.txt +98 -0
- package/vendor/fonts/zhhand/OFL.txt +99 -0
- package/vendor/fonts/zhhand/Xiaolai-Regular.ttf +0 -0
- package/vendor/pdf.min.mjs +29 -0
- package/vendor/pdf.worker.min.mjs +29 -0
- package/vendor/pdfjs-LICENSE.txt +177 -0
- package/vendor/rough/LICENSE +21 -0
- package/vendor/rough/rough.mjs +1 -0
- package/vendor/wasm/LICENSE_JBIG2 +196 -0
- package/vendor/wasm/LICENSE_OPENJPEG +39 -0
- package/vendor/wasm/LICENSE_PDFJS_JBIG2 +13 -0
- package/vendor/wasm/LICENSE_PDFJS_OPENJPEG +22 -0
- package/vendor/wasm/LICENSE_PDFJS_QCMS +19 -0
- package/vendor/wasm/LICENSE_QCMS +21 -0
- package/vendor/wasm/jbig2.wasm +0 -0
- package/vendor/wasm/jbig2_nowasm_fallback.js +15 -0
- package/vendor/wasm/openjpeg.wasm +0 -0
- package/vendor/wasm/openjpeg_nowasm_fallback.js +17 -0
- package/vendor/wasm/qcms_bg.wasm +0 -0
- package/vendor/wasm/quickjs-eval.wasm +0 -0
- package//345/256/211/350/243/205/344/270/216/345/210/206/345/217/221.md +54 -0
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The libraries this plugin knows about.
|
|
3
|
+
*
|
|
4
|
+
* One computer may hold several split libraries (different subjects, different
|
|
5
|
+
* drives). Each entry is just the paths the rest of the plugin needs, kept in
|
|
6
|
+
* `$DSH_HOME/pdfdesk/libraries.json` together with which one is active. The
|
|
7
|
+
* profile config still supplies the first library, so an existing install keeps
|
|
8
|
+
* working without touching its patch.
|
|
9
|
+
*
|
|
10
|
+
* Only the math layout is supported for now: the manual in the workbench tells a
|
|
11
|
+
* user how a library must be split and named, and `mode` records that so a later
|
|
12
|
+
* subject layout can be added without migrating.
|
|
13
|
+
*
|
|
14
|
+
* @module dsh-pdf-workbench/libraries
|
|
15
|
+
*/
|
|
16
|
+
import { mkdir, readFile, rename, stat, writeFile } from 'node:fs/promises'
|
|
17
|
+
import { basename, dirname, join } from 'node:path'
|
|
18
|
+
|
|
19
|
+
/** File format version. */
|
|
20
|
+
const VERSION = 1
|
|
21
|
+
|
|
22
|
+
/** The only layout this version understands. */
|
|
23
|
+
export const MODE_MATH = 'math'
|
|
24
|
+
|
|
25
|
+
/** Entries kept at most. */
|
|
26
|
+
const MAX_LIBRARIES = 24
|
|
27
|
+
|
|
28
|
+
/** A generated id: no spaces, no Chinese, stable in URLs. */
|
|
29
|
+
const ID_SHAPE = /^lib-[a-z0-9-]{1,40}$/
|
|
30
|
+
|
|
31
|
+
/** A safe name for display. */
|
|
32
|
+
const MAX_NAME = 120
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Derive the standard layout of one library root.
|
|
36
|
+
* @param {string} libraryDir - the library root (holds 分片/, 产物/, 封面/, 索引.json).
|
|
37
|
+
* @returns {{ libraryDir: string, fragmentDir: string, artifactDir: string, coverDir: string, indexPath: string }} the paths.
|
|
38
|
+
*/
|
|
39
|
+
export function deriveLayout(libraryDir) {
|
|
40
|
+
return {
|
|
41
|
+
libraryDir,
|
|
42
|
+
fragmentDir: join(libraryDir, '分片'),
|
|
43
|
+
artifactDir: join(libraryDir, '产物'),
|
|
44
|
+
coverDir: join(libraryDir, '封面'),
|
|
45
|
+
indexPath: join(libraryDir, '索引.json'),
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Keep one stored entry if it is usable.
|
|
51
|
+
* @param {unknown} value - stored entry.
|
|
52
|
+
* @returns {{ id: string, name: string, mode: string, libraryDir: string, fragmentDir: string, artifactDir: string, coverDir: string, indexPath: string, addedAt: number } | null} the entry.
|
|
53
|
+
*/
|
|
54
|
+
function sanitizeEntry(value) {
|
|
55
|
+
if (value === null || typeof value !== 'object') return null
|
|
56
|
+
const id = typeof value.id === 'string' && ID_SHAPE.test(value.id) ? value.id : ''
|
|
57
|
+
const libraryDir = typeof value.libraryDir === 'string' && value.libraryDir !== '' ? value.libraryDir : ''
|
|
58
|
+
if (id === '' || libraryDir === '') return null
|
|
59
|
+
const name = typeof value.name === 'string' && value.name.trim() !== ''
|
|
60
|
+
? value.name.trim().slice(0, MAX_NAME)
|
|
61
|
+
: basename(libraryDir)
|
|
62
|
+
const layout = deriveLayout(libraryDir)
|
|
63
|
+
return {
|
|
64
|
+
id,
|
|
65
|
+
name,
|
|
66
|
+
mode: typeof value.mode === 'string' && value.mode !== '' ? value.mode : MODE_MATH,
|
|
67
|
+
libraryDir,
|
|
68
|
+
fragmentDir: typeof value.fragmentDir === 'string' && value.fragmentDir !== '' ? value.fragmentDir : layout.fragmentDir,
|
|
69
|
+
artifactDir: typeof value.artifactDir === 'string' && value.artifactDir !== '' ? value.artifactDir : layout.artifactDir,
|
|
70
|
+
coverDir: typeof value.coverDir === 'string' && value.coverDir !== '' ? value.coverDir : layout.coverDir,
|
|
71
|
+
indexPath: typeof value.indexPath === 'string' && value.indexPath !== '' ? value.indexPath : layout.indexPath,
|
|
72
|
+
addedAt: typeof value.addedAt === 'number' && Number.isFinite(value.addedAt) ? value.addedAt : 0,
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Durable list of libraries plus the active one.
|
|
78
|
+
*/
|
|
79
|
+
export class LibrariesStore {
|
|
80
|
+
/**
|
|
81
|
+
* @param {string} file - absolute path of `libraries.json`.
|
|
82
|
+
* @param {{ libraryDir: string, fragmentDir: string, artifactDir: string, coverDir: string, indexPath: string, name?: string }} fallback - the profile's configured library.
|
|
83
|
+
*/
|
|
84
|
+
constructor(file, fallback) {
|
|
85
|
+
this.file = file
|
|
86
|
+
this.fallback = fallback
|
|
87
|
+
this.items = []
|
|
88
|
+
this.activeId = null
|
|
89
|
+
this.ready = this.load()
|
|
90
|
+
this.queue = Promise.resolve()
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Load once; a damaged file falls back to the configured library. */
|
|
94
|
+
async load() {
|
|
95
|
+
let parsed = null
|
|
96
|
+
try {
|
|
97
|
+
parsed = JSON.parse(await readFile(this.file, 'utf8'))
|
|
98
|
+
} catch {
|
|
99
|
+
parsed = null
|
|
100
|
+
}
|
|
101
|
+
const raw = parsed !== null && typeof parsed === 'object' && Array.isArray(parsed.items) ? parsed.items : []
|
|
102
|
+
const items = []
|
|
103
|
+
for (const value of raw) {
|
|
104
|
+
const entry = sanitizeEntry(value)
|
|
105
|
+
if (entry !== null && !items.some(item => item.id === entry.id)) items.push(entry)
|
|
106
|
+
if (items.length >= MAX_LIBRARIES) break
|
|
107
|
+
}
|
|
108
|
+
if (items.length === 0) {
|
|
109
|
+
items.push(this.fallbackEntry())
|
|
110
|
+
}
|
|
111
|
+
this.items = items
|
|
112
|
+
const storedActive = parsed !== null && typeof parsed === 'object' && typeof parsed.activeId === 'string' ? parsed.activeId : null
|
|
113
|
+
this.activeId = items.some(item => item.id === storedActive) ? storedActive : items[0].id
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** The profile's configured library, as the first entry. */
|
|
117
|
+
fallbackEntry() {
|
|
118
|
+
return {
|
|
119
|
+
id: 'lib-default',
|
|
120
|
+
name: this.fallback.name !== undefined && this.fallback.name !== '' ? this.fallback.name : basename(this.fallback.libraryDir),
|
|
121
|
+
mode: MODE_MATH,
|
|
122
|
+
libraryDir: this.fallback.libraryDir,
|
|
123
|
+
fragmentDir: this.fallback.fragmentDir,
|
|
124
|
+
artifactDir: this.fallback.artifactDir,
|
|
125
|
+
coverDir: this.fallback.coverDir,
|
|
126
|
+
indexPath: this.fallback.indexPath,
|
|
127
|
+
addedAt: Date.now(),
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Snapshot for bootstrap.
|
|
133
|
+
* @returns {Promise<{ items: object[], activeId: string }>} the list and the active id.
|
|
134
|
+
*/
|
|
135
|
+
async list() {
|
|
136
|
+
await this.ready
|
|
137
|
+
return { items: this.items.map(item => ({ ...item })), activeId: this.activeId }
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* The active library.
|
|
142
|
+
* @returns {Promise<object | null>} the active entry, or null.
|
|
143
|
+
*/
|
|
144
|
+
async active() {
|
|
145
|
+
await this.ready
|
|
146
|
+
return this.items.find(item => item.id === this.activeId) ?? null
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Add one library root. Validates that the directory exists.
|
|
151
|
+
* @param {{ libraryDir: string, name?: string }} input - the root and an optional display name.
|
|
152
|
+
* @returns {Promise<object>} the added entry.
|
|
153
|
+
*/
|
|
154
|
+
async add(input) {
|
|
155
|
+
const libraryDir = typeof input.libraryDir === 'string' ? input.libraryDir.trim() : ''
|
|
156
|
+
if (libraryDir === '') throw new Error('书库目录不能为空')
|
|
157
|
+
let info
|
|
158
|
+
try {
|
|
159
|
+
info = await stat(libraryDir)
|
|
160
|
+
} catch {
|
|
161
|
+
throw new Error(`目录不存在:${libraryDir}`)
|
|
162
|
+
}
|
|
163
|
+
if (!info.isDirectory()) throw new Error(`不是目录:${libraryDir}`)
|
|
164
|
+
await this.ready
|
|
165
|
+
const duplicate = this.items.find(item => item.libraryDir.toLowerCase() === libraryDir.toLowerCase())
|
|
166
|
+
if (duplicate !== undefined) {
|
|
167
|
+
this.activeId = duplicate.id
|
|
168
|
+
await this.flush()
|
|
169
|
+
return duplicate
|
|
170
|
+
}
|
|
171
|
+
if (this.items.length >= MAX_LIBRARIES) throw new Error('书库数量已达上限')
|
|
172
|
+
const layout = deriveLayout(libraryDir)
|
|
173
|
+
const entry = {
|
|
174
|
+
id: `lib-${Date.now().toString(36)}${Math.floor(Math.random() * 1296).toString(36)}`,
|
|
175
|
+
name: typeof input.name === 'string' && input.name.trim() !== ''
|
|
176
|
+
? input.name.trim().slice(0, MAX_NAME)
|
|
177
|
+
: basename(libraryDir),
|
|
178
|
+
mode: MODE_MATH,
|
|
179
|
+
...layout,
|
|
180
|
+
addedAt: Date.now(),
|
|
181
|
+
}
|
|
182
|
+
this.items = [...this.items, entry]
|
|
183
|
+
this.activeId = entry.id
|
|
184
|
+
await this.flush()
|
|
185
|
+
return entry
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Choose one library.
|
|
190
|
+
* @param {string} id - the entry id.
|
|
191
|
+
* @returns {Promise<boolean>} whether the id was known.
|
|
192
|
+
*/
|
|
193
|
+
async setActive(id) {
|
|
194
|
+
await this.ready
|
|
195
|
+
if (!this.items.some(item => item.id === id)) return false
|
|
196
|
+
this.activeId = id
|
|
197
|
+
await this.flush()
|
|
198
|
+
return true
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Forget one library (files stay on disk).
|
|
203
|
+
* @param {string} id - the entry id.
|
|
204
|
+
* @returns {Promise<boolean>} whether it was removed.
|
|
205
|
+
*/
|
|
206
|
+
async remove(id) {
|
|
207
|
+
await this.ready
|
|
208
|
+
const next = this.items.filter(item => item.id !== id)
|
|
209
|
+
if (next.length === this.items.length) return false
|
|
210
|
+
this.items = next.length === 0 ? [this.fallbackEntry()] : next
|
|
211
|
+
if (!this.items.some(item => item.id === this.activeId)) this.activeId = this.items[0].id
|
|
212
|
+
await this.flush()
|
|
213
|
+
return true
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** Persist atomically. */
|
|
217
|
+
async flush() {
|
|
218
|
+
await mkdir(dirname(this.file), { recursive: true })
|
|
219
|
+
const payload = { version: VERSION, activeId: this.activeId, items: this.items }
|
|
220
|
+
const temporary = `${this.file}.${process.pid}.tmp`
|
|
221
|
+
await writeFile(temporary, `${JSON.stringify(payload, null, 2)}\n`, 'utf8')
|
|
222
|
+
await rename(temporary, this.file)
|
|
223
|
+
}
|
|
224
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extra chapter placements for a session.
|
|
3
|
+
*
|
|
4
|
+
* A session's own title is its primary binding: `[<book id> cNN] <mode>` is
|
|
5
|
+
* what the workbench classifies on. That gives exactly one chapter per session.
|
|
6
|
+
* Two needs contradict that: an older session written before this convention,
|
|
7
|
+
* and one conversation that belongs under two chapters. Both are solved here by
|
|
8
|
+
* a link list instead of by rewriting the title — the conversation keeps its
|
|
9
|
+
* name, and the workbench shows it under the union of its title and its links.
|
|
10
|
+
*
|
|
11
|
+
* Links are deliberately tiny: the browser sends `(sessionId, bookId, code,
|
|
12
|
+
* mode)`; the Host validates the chapter against the live catalog before saving.
|
|
13
|
+
* A link whose session disappears is pruned, never fatal.
|
|
14
|
+
*
|
|
15
|
+
* @module dsh-pdf-workbench/links
|
|
16
|
+
*/
|
|
17
|
+
import { readFile, rename, mkdir, writeFile } from 'node:fs/promises'
|
|
18
|
+
import { dirname } from 'node:path'
|
|
19
|
+
|
|
20
|
+
/** File format version. */
|
|
21
|
+
const VERSION = 1
|
|
22
|
+
|
|
23
|
+
/** Links one session may hold, so a stray loop cannot grow the file forever. */
|
|
24
|
+
const MAX_PER_SESSION = 40
|
|
25
|
+
|
|
26
|
+
/** A session id: the only shape this store accepts. */
|
|
27
|
+
const SESSION_ID = /^[\w-]{1,80}$/
|
|
28
|
+
|
|
29
|
+
/** One path-safe book id. */
|
|
30
|
+
const BOOK_ID = /^[^\\/:*?"<>|]{1,200}$/
|
|
31
|
+
|
|
32
|
+
/** A chapter code, canonical form. */
|
|
33
|
+
const CODE = /^c\d{2,3}$/
|
|
34
|
+
|
|
35
|
+
/** Modes a chapter groups its sessions by. */
|
|
36
|
+
const MODES = new Set(['extract', 'qa', 'exercise'])
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Keep one link if it is usable.
|
|
40
|
+
* @param {unknown} value - stored or submitted link.
|
|
41
|
+
* @returns {{ bookId: string, code: string, mode: string, createdAt: number } | null} the link.
|
|
42
|
+
*/
|
|
43
|
+
function sanitizeLink(value) {
|
|
44
|
+
if (value === null || typeof value !== 'object') return null
|
|
45
|
+
const bookId = typeof value.bookId === 'string' ? value.bookId : ''
|
|
46
|
+
const code = typeof value.code === 'string' ? value.code.trim().toLowerCase() : ''
|
|
47
|
+
const mode = typeof value.mode === 'string' ? value.mode : ''
|
|
48
|
+
if (!BOOK_ID.test(bookId) || bookId === '.' || bookId === '..') return null
|
|
49
|
+
if (!CODE.test(code)) return null
|
|
50
|
+
if (!MODES.has(mode)) return null
|
|
51
|
+
const createdAt = typeof value.createdAt === 'number' && Number.isFinite(value.createdAt) ? value.createdAt : 0
|
|
52
|
+
return { bookId, code, mode, createdAt }
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Read the link file; anything damaged reads as empty.
|
|
57
|
+
* @param {string} file - absolute path of `links.json`.
|
|
58
|
+
* @returns {Promise<Record<string, object[]>>} links by session id.
|
|
59
|
+
*/
|
|
60
|
+
async function readLinkFile(file) {
|
|
61
|
+
let text
|
|
62
|
+
try {
|
|
63
|
+
text = await readFile(file, 'utf8')
|
|
64
|
+
} catch {
|
|
65
|
+
return {}
|
|
66
|
+
}
|
|
67
|
+
let parsed
|
|
68
|
+
try {
|
|
69
|
+
parsed = JSON.parse(text)
|
|
70
|
+
} catch {
|
|
71
|
+
return {}
|
|
72
|
+
}
|
|
73
|
+
const raw = parsed !== null && typeof parsed === 'object' && parsed.links !== null && typeof parsed.links === 'object'
|
|
74
|
+
? parsed.links
|
|
75
|
+
: {}
|
|
76
|
+
const links = {}
|
|
77
|
+
for (const [sessionId, entries] of Object.entries(raw)) {
|
|
78
|
+
if (!SESSION_ID.test(sessionId) || !Array.isArray(entries)) continue
|
|
79
|
+
const list = []
|
|
80
|
+
const seen = new Set()
|
|
81
|
+
for (const entry of entries) {
|
|
82
|
+
const link = sanitizeLink(entry)
|
|
83
|
+
if (link === null) continue
|
|
84
|
+
const key = `${link.bookId}|${link.code}`
|
|
85
|
+
if (seen.has(key)) continue
|
|
86
|
+
seen.add(key)
|
|
87
|
+
list.push(link)
|
|
88
|
+
if (list.length >= MAX_PER_SESSION) break
|
|
89
|
+
}
|
|
90
|
+
if (list.length > 0) links[sessionId] = list
|
|
91
|
+
}
|
|
92
|
+
return links
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Durable extra placements for sessions; the session title stays untouched.
|
|
97
|
+
*/
|
|
98
|
+
export class LinksStore {
|
|
99
|
+
/**
|
|
100
|
+
* @param {string} file - absolute path of `links.json`.
|
|
101
|
+
*/
|
|
102
|
+
constructor(file) {
|
|
103
|
+
this.file = file
|
|
104
|
+
this.links = {}
|
|
105
|
+
this.ready = this.load()
|
|
106
|
+
this.queue = Promise.resolve()
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Load once; later failures read as empty. */
|
|
110
|
+
async load() {
|
|
111
|
+
this.links = await readLinkFile(this.file)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Snapshot for one bootstrap response.
|
|
116
|
+
* @returns {Promise<Record<string, object[]>>} links by session id.
|
|
117
|
+
*/
|
|
118
|
+
async list() {
|
|
119
|
+
await this.ready
|
|
120
|
+
return { ...this.links }
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Links of one session.
|
|
125
|
+
* @param {string} sessionId - the session.
|
|
126
|
+
* @returns {Promise<object[]>} its links.
|
|
127
|
+
*/
|
|
128
|
+
async forSession(sessionId) {
|
|
129
|
+
await this.ready
|
|
130
|
+
return [...(this.links[sessionId] ?? [])]
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Attach one session to one chapter. An existing entry for the same chapter
|
|
135
|
+
* is replaced (its mode is updated) instead of duplicated.
|
|
136
|
+
* @param {string} sessionId - the session.
|
|
137
|
+
* @param {{ bookId: string, code: string, mode: string }} input - the placement.
|
|
138
|
+
* @returns {Promise<object[]>} the session's links after the change.
|
|
139
|
+
*/
|
|
140
|
+
async add(sessionId, input) {
|
|
141
|
+
if (!SESSION_ID.test(sessionId)) throw new Error('sessionId 不合法')
|
|
142
|
+
const link = sanitizeLink({ ...input, createdAt: Date.now() })
|
|
143
|
+
if (link === null) throw new Error('挂载参数不完整')
|
|
144
|
+
await this.ready
|
|
145
|
+
const existing = this.links[sessionId] ?? []
|
|
146
|
+
const key = `${link.bookId}|${link.code}`
|
|
147
|
+
const next = existing.filter(entry => `${entry.bookId}|${entry.code}` !== key)
|
|
148
|
+
next.push(link)
|
|
149
|
+
this.links = { ...this.links, [sessionId]: next.slice(-MAX_PER_SESSION) }
|
|
150
|
+
this.queue = this.queue.then(() => this.flush()).catch(() => {})
|
|
151
|
+
await this.queue
|
|
152
|
+
return this.forSession(sessionId)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Detach one chapter from one session; other chapters are untouched.
|
|
157
|
+
* @param {string} sessionId - the session.
|
|
158
|
+
* @param {string} bookId - the chapter's book.
|
|
159
|
+
* @param {string} code - the chapter code.
|
|
160
|
+
* @returns {Promise<object[]>} the session's remaining links.
|
|
161
|
+
*/
|
|
162
|
+
async remove(sessionId, bookId, code) {
|
|
163
|
+
if (!SESSION_ID.test(sessionId)) throw new Error('sessionId 不合法')
|
|
164
|
+
await this.ready
|
|
165
|
+
const existing = this.links[sessionId]
|
|
166
|
+
if (existing === undefined) return []
|
|
167
|
+
const key = `${bookId}|${code}`
|
|
168
|
+
const next = existing.filter(entry => `${entry.bookId}|${entry.code}` !== key)
|
|
169
|
+
const links = { ...this.links }
|
|
170
|
+
if (next.length === 0) delete links[sessionId]
|
|
171
|
+
else links[sessionId] = next
|
|
172
|
+
this.links = links
|
|
173
|
+
this.queue = this.queue.then(() => this.flush()).catch(() => {})
|
|
174
|
+
await this.queue
|
|
175
|
+
return next
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Forget every link of a session that no longer exists.
|
|
180
|
+
* @param {string} sessionId - the session.
|
|
181
|
+
* @returns {Promise<void>} completion.
|
|
182
|
+
*/
|
|
183
|
+
async forget(sessionId) {
|
|
184
|
+
if (!SESSION_ID.test(sessionId)) return
|
|
185
|
+
await this.ready
|
|
186
|
+
if (this.links[sessionId] === undefined) return
|
|
187
|
+
const links = { ...this.links }
|
|
188
|
+
delete links[sessionId]
|
|
189
|
+
this.links = links
|
|
190
|
+
this.queue = this.queue.then(() => this.flush()).catch(() => {})
|
|
191
|
+
await this.queue
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** Persist atomically. */
|
|
195
|
+
async flush() {
|
|
196
|
+
await mkdir(dirname(this.file), { recursive: true })
|
|
197
|
+
const temporary = `${this.file}.${process.pid}.tmp`
|
|
198
|
+
await writeFile(temporary, `${JSON.stringify({ version: VERSION, links: this.links }, null, 2)}\n`, 'utf8')
|
|
199
|
+
await rename(temporary, this.file)
|
|
200
|
+
}
|
|
201
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a chapter's PDF or a produced PDF actually lives.
|
|
3
|
+
*
|
|
4
|
+
* The catalog only carries file *names* (a chapter keeps its split file name, a
|
|
5
|
+
* produced file keeps its own name inside the chapter folder), so the browser
|
|
6
|
+
* asks for a (book, chapter) pair and the Host resolves the bytes. Both paths are
|
|
7
|
+
* built from validated segments and then checked for containment: a catalog that
|
|
8
|
+
* was edited by hand must not be able to point the reader outside the library.
|
|
9
|
+
*
|
|
10
|
+
* @module dsh-pdf-workbench/pdf-source
|
|
11
|
+
*/
|
|
12
|
+
import { open, stat } from 'node:fs/promises'
|
|
13
|
+
import { basename, join, resolve, sep } from 'node:path'
|
|
14
|
+
|
|
15
|
+
/** One path segment: no separators, no traversal, no drive colon. */
|
|
16
|
+
const SEGMENT = /^[^\\/:*?"<>|]{1,255}$/
|
|
17
|
+
|
|
18
|
+
/** A chapter code, canonical form. */
|
|
19
|
+
const CODE = /^c\d{2,3}$/
|
|
20
|
+
|
|
21
|
+
/** Bytes served for one PDF; the split files are a few MB at most. */
|
|
22
|
+
export const MAX_PDF_BYTES = 64 * 1024 * 1024
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Whether a value can be one directory segment.
|
|
26
|
+
* @param {unknown} value - candidate.
|
|
27
|
+
* @returns {boolean} true when safe.
|
|
28
|
+
*/
|
|
29
|
+
function isSegment(value) {
|
|
30
|
+
return typeof value === 'string' && SEGMENT.test(value) && value !== '.' && value !== '..'
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Whether `target` really lives under `root`.
|
|
35
|
+
* @param {string} root - directory that must contain the target.
|
|
36
|
+
* @param {string} target - resolved candidate.
|
|
37
|
+
* @returns {boolean} true when contained.
|
|
38
|
+
*/
|
|
39
|
+
function isInside(root, target) {
|
|
40
|
+
const normalize = value => value.replace(/[\\/]+$/, '').replace(/\//g, '\\').toLowerCase()
|
|
41
|
+
const parent = normalize(root)
|
|
42
|
+
const child = normalize(target)
|
|
43
|
+
return child === parent || child.startsWith(`${parent}${sep}`)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The split PDF of one chapter.
|
|
48
|
+
* @param {{ fragmentDir: string }} config - resolved paths.
|
|
49
|
+
* @param {object} book - catalog book row.
|
|
50
|
+
* @param {object} chapter - catalog chapter row.
|
|
51
|
+
* @returns {{ ok: true, path: string } | { ok: false, reason: 'bad-name' | 'not-found' }} the file, or why it cannot be served.
|
|
52
|
+
*/
|
|
53
|
+
export function chapterPdfPath(config, book, chapter) {
|
|
54
|
+
// A whole-book library entry: one unsplit PDF under `<libraryDir>\整本\`.
|
|
55
|
+
if (chapter.whole === true) {
|
|
56
|
+
if (typeof book.id !== 'string' || !isSegment(book.id) || !isSegment(chapter.file)) {
|
|
57
|
+
return { ok: false, reason: 'bad-name' }
|
|
58
|
+
}
|
|
59
|
+
const wholeAt = chapter.wholeAt === 'root' ? '' : '整本'
|
|
60
|
+
const base = wholeAt === '' ? config.libraryDir : join(config.libraryDir, wholeAt)
|
|
61
|
+
const target = resolve(base, `${chapter.file}.pdf`)
|
|
62
|
+
if (!isInside(base, target)) return { ok: false, reason: 'bad-name' }
|
|
63
|
+
return { ok: true, path: target }
|
|
64
|
+
}
|
|
65
|
+
if (!isSegment(book.dir) || !isSegment(chapter.file) || !CODE.test(chapter.code)) return { ok: false, reason: 'bad-name' }
|
|
66
|
+
const target = resolve(config.fragmentDir, book.dir, `${chapter.file}.pdf`)
|
|
67
|
+
if (!isInside(config.fragmentDir, target)) return { ok: false, reason: 'bad-name' }
|
|
68
|
+
return { ok: true, path: target }
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* A produced PDF inside one chapter's artifact folder.
|
|
73
|
+
* @param {{ artifactDir: string }} config - resolved paths.
|
|
74
|
+
* @param {object} book - catalog book row.
|
|
75
|
+
* @param {object} chapter - catalog chapter row.
|
|
76
|
+
* @param {string} name - file name inside the chapter folder.
|
|
77
|
+
* @returns {{ ok: true, path: string } | { ok: false, reason: 'bad-name' | 'not-found' }} the file, or why it cannot be served.
|
|
78
|
+
*/
|
|
79
|
+
export function artifactPdfPath(config, book, chapter, name) {
|
|
80
|
+
if (!isSegment(book.id) || !CODE.test(chapter.code)) return { ok: false, reason: 'bad-name' }
|
|
81
|
+
if (!isSegment(name) || basename(name) !== name || !name.toLowerCase().endsWith('.pdf')) return { ok: false, reason: 'bad-name' }
|
|
82
|
+
const dir = join(config.artifactDir, book.id, chapter.code)
|
|
83
|
+
const target = resolve(dir, name)
|
|
84
|
+
if (!isInside(config.artifactDir, target) || !isInside(dir, target)) return { ok: false, reason: 'bad-name' }
|
|
85
|
+
return { ok: true, path: target }
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Read one PDF for the reader, bounded so a stray huge file cannot exhaust memory.
|
|
90
|
+
* @param {string} path - absolute file path.
|
|
91
|
+
* @returns {Promise<{ ok: true, bytes: Buffer, size: number } | { ok: false, reason: 'not-found' | 'too-large' }>} the bytes.
|
|
92
|
+
*/
|
|
93
|
+
export async function readPdf(path) {
|
|
94
|
+
let info
|
|
95
|
+
try {
|
|
96
|
+
info = await stat(path)
|
|
97
|
+
} catch {
|
|
98
|
+
return { ok: false, reason: 'not-found' }
|
|
99
|
+
}
|
|
100
|
+
if (!info.isFile()) return { ok: false, reason: 'not-found' }
|
|
101
|
+
if (info.size > MAX_PDF_BYTES) return { ok: false, reason: 'too-large' }
|
|
102
|
+
let handle
|
|
103
|
+
try {
|
|
104
|
+
handle = await open(path, 'r')
|
|
105
|
+
} catch {
|
|
106
|
+
return { ok: false, reason: 'not-found' }
|
|
107
|
+
}
|
|
108
|
+
try {
|
|
109
|
+
const buffer = Buffer.alloc(info.size)
|
|
110
|
+
let offset = 0
|
|
111
|
+
while (offset < info.size) {
|
|
112
|
+
const { bytesRead } = await handle.read(buffer, offset, info.size - offset, offset)
|
|
113
|
+
if (bytesRead === 0) break
|
|
114
|
+
offset += bytesRead
|
|
115
|
+
}
|
|
116
|
+
return { ok: true, bytes: buffer.subarray(0, offset), size: info.size }
|
|
117
|
+
} catch {
|
|
118
|
+
return { ok: false, reason: 'not-found' }
|
|
119
|
+
} finally {
|
|
120
|
+
await handle.close().catch(() => {})
|
|
121
|
+
}
|
|
122
|
+
}
|