dsh-pdf-workbench 1.4.1 → 1.5.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.
Files changed (78) hide show
  1. package/README.md +29 -7
  2. package/client.js +4106 -469
  3. package/docs/LAYOUT.md +56 -0
  4. package/docs/RELEASE.md +8 -0
  5. package/docs/TESTING.md +16 -0
  6. package/docs/ci.md +18 -13
  7. package/docs/copy/templates.md +4 -4
  8. package/docs/decisions.md +119 -0
  9. package/docs/features.md +14 -8
  10. package/docs/requirements/2026-11-25-v1.5/346/224/271/350/277/233/351/234/200/346/261/202.md +622 -0
  11. package/docs/roles/{release-manager.md → ci.md} +37 -47
  12. package/docs/roles/programmer.md +45 -59
  13. package/docs/roles/requirements.md +68 -0
  14. package/docs/todo.md +590 -32
  15. package/index.js +337 -1
  16. package/package.json +1 -1
  17. package/src/host/backup.js +172 -33
  18. package/src/host/book-wizard.js +454 -0
  19. package/src/host/digest.js +4 -0
  20. package/src/host/engine.js +88 -0
  21. package/src/host/exports.js +159 -0
  22. package/src/host/http.js +28 -1
  23. package/src/host/imports.js +650 -0
  24. package/src/host/index-source.js +117 -11
  25. package/src/host/library-export.js +162 -0
  26. package/src/host/pdf-source.js +6 -2
  27. package/src/host/routes.js +1020 -120
  28. package/src/host/shortcut.js +238 -0
  29. package/src/host/storage.js +421 -0
  30. package/src/host/translate-blocks.js +49 -45
  31. package/src/host/translate-job.js +37 -10
  32. package/src/host/translate.js +19 -45
  33. package/src/host/translation-cache.js +209 -0
  34. package/src/host/typst-install.js +132 -0
  35. package/src/host/zip.js +486 -4
  36. package/tools/latex_to_typst.py +1353 -0
  37. package/tools/md2overlay.py +147 -152
  38. package/tools/md2overlay_typst.py +312 -0
  39. package/tools/md2typst.py +818 -0
  40. package/tools/overlay_layout.py +332 -0
  41. package/tools/pdf_blocks.py +82 -10
  42. package/tools/realign_overlay.py +4 -1
  43. package/tools/rebuild_overlay.py +3 -1
  44. package/tools/split_pdf.py +158 -0
  45. package/tools/typst_runtime.py +98 -0
  46. package/vendor/katex/LICENSE +21 -0
  47. package/vendor/katex/fonts/KaTeX_AMS-Regular.woff2 +0 -0
  48. package/vendor/katex/fonts/KaTeX_Caligraphic-Bold.woff2 +0 -0
  49. package/vendor/katex/fonts/KaTeX_Caligraphic-Regular.woff2 +0 -0
  50. package/vendor/katex/fonts/KaTeX_Fraktur-Bold.woff2 +0 -0
  51. package/vendor/katex/fonts/KaTeX_Fraktur-Regular.woff2 +0 -0
  52. package/vendor/katex/fonts/KaTeX_Main-Bold.woff2 +0 -0
  53. package/vendor/katex/fonts/KaTeX_Main-BoldItalic.woff2 +0 -0
  54. package/vendor/katex/fonts/KaTeX_Main-Italic.woff2 +0 -0
  55. package/vendor/katex/fonts/KaTeX_Main-Regular.woff2 +0 -0
  56. package/vendor/katex/fonts/KaTeX_Math-BoldItalic.woff2 +0 -0
  57. package/vendor/katex/fonts/KaTeX_Math-Italic.woff2 +0 -0
  58. package/vendor/katex/fonts/KaTeX_SansSerif-Bold.woff2 +0 -0
  59. package/vendor/katex/fonts/KaTeX_SansSerif-Italic.woff2 +0 -0
  60. package/vendor/katex/fonts/KaTeX_SansSerif-Regular.woff2 +0 -0
  61. package/vendor/katex/fonts/KaTeX_Script-Regular.woff2 +0 -0
  62. package/vendor/katex/fonts/KaTeX_Size1-Regular.woff2 +0 -0
  63. package/vendor/katex/fonts/KaTeX_Size2-Regular.woff2 +0 -0
  64. package/vendor/katex/fonts/KaTeX_Size3-Regular.woff2 +0 -0
  65. package/vendor/katex/fonts/KaTeX_Size4-Regular.woff2 +0 -0
  66. package/vendor/katex/fonts/KaTeX_Typewriter-Regular.woff2 +0 -0
  67. package/vendor/katex/katex.min.css +1 -0
  68. package/vendor/katex/katex.min.js +1 -0
  69. package//345/256/211/350/243/205/344/270/216/345/210/206/345/217/221.md +9 -1
  70. package/docs/boss-handover.md +0 -84
  71. package/docs/roles/auditor.md +0 -123
  72. package/docs/roles/boss.md +0 -162
  73. package/docs/roles/code-reviewer.md +0 -132
  74. package/docs/roles/feature-inventory.md +0 -119
  75. package/docs/roles/feedback-collector.md +0 -105
  76. package/docs/roles/tester.md +0 -140
  77. package/docs/roles/ui-copywriter.md +0 -122
  78. package/src/host/book-import.js +0 -98
@@ -0,0 +1,159 @@
1
+ /**
2
+ * Packaging history and background packing jobs.
3
+ *
4
+ * A migration package of a real profile is large (sessions dominate: ~120 MB
5
+ * here), so packing takes long enough that a synchronous request can be cut by
6
+ * the page/transport timeout. Packing therefore runs as a background job: the
7
+ * click returns immediately and the settings page polls the job list, then the
8
+ * finished archive shows up in the history.
9
+ *
10
+ * History is derived from the folder rather than kept in a side file: the
11
+ * backup folder already holds exactly what exists, so a package moved or
12
+ * deleted by the user disappears from the list without a stale record, and
13
+ * nothing has to be excluded from a migration package (that package never
14
+ * includes the plugin backup folder).
15
+ *
16
+ * @module dsh-pdf-workbench/exports
17
+ */
18
+ import { spawn } from 'node:child_process'
19
+ import { readdir, stat } from 'node:fs/promises'
20
+ import { dirname, join } from 'node:path'
21
+
22
+ /** Both package prefixes, and nothing else, count as history. The library name
23
+ * (when present) sits between the prefix and the timestamp. */
24
+ const EXPORT_NAME = /^(?:dsh-workbench-backup|(library-export|pdf-pack)(?:-[^/\\:*?"<>|]+?)?)-\d{8}-\d{6}\.zip$/
25
+
26
+ /** At most this many finished jobs stay in the in-memory list. */
27
+ const MAX_JOBS = 20
28
+
29
+ /** In-memory packing jobs; they do not survive a Host restart. */
30
+ const jobs = new Map()
31
+
32
+ /**
33
+ * Whether a name is one of this plugin's packages.
34
+ * @param {string} name - file name.
35
+ * @returns {boolean} true for a migration backup, library export or PDF pack.
36
+ */
37
+ export function isExportName(name) {
38
+ return EXPORT_NAME.test(String(name))
39
+ }
40
+
41
+ /**
42
+ * Split a package name into kind, library label and timestamp.
43
+ * @param {string} name - file name.
44
+ * @returns {{ kind: 'backup' | 'library-export' | 'pdf-pack', library: string }} the label.
45
+ */
46
+ export function describeExportName(name) {
47
+ const value = String(name)
48
+ if (/^pdf-pack-/.test(value)) {
49
+ const match = /^pdf-pack-(.+?)-\d{8}-\d{6}\.zip$/.exec(value)
50
+ return { kind: 'pdf-pack', library: match === null ? '' : match[1] }
51
+ }
52
+ if (/^library-export-/.test(value)) {
53
+ const match = /^library-export-(.+?)-\d{8}-\d{6}\.zip$/.exec(value)
54
+ return { kind: 'library-export', library: match === null ? '' : match[1] }
55
+ }
56
+ return { kind: 'backup', library: '' }
57
+ }
58
+
59
+ /**
60
+ * The packages that currently exist in `<dataDir>/backup`, newest first.
61
+ * @param {string} dataDir - the plugin data folder.
62
+ * @returns {Promise<Array<{ name: string, kind: 'backup' | 'library-export', bytes: number, createdAt: number, path: string }>>} the records.
63
+ */
64
+ export async function listExports(dataDir) {
65
+ const dir = join(dataDir, 'backup')
66
+ const names = await readdir(dir).catch(() => [])
67
+ const items = []
68
+ for (const name of names) {
69
+ if (!isExportName(name)) continue
70
+ const path = join(dir, name)
71
+ const info = await stat(path).catch(() => null)
72
+ if (info === null || !info.isFile()) continue
73
+ items.push({
74
+ name,
75
+ ...describeExportName(name),
76
+ bytes: info.size,
77
+ createdAt: info.mtimeMs,
78
+ path,
79
+ })
80
+ }
81
+ items.sort((left, right) => (right.createdAt - left.createdAt) || right.name.localeCompare(left.name))
82
+ return items
83
+ }
84
+
85
+ /**
86
+ * Start one packing job. Only one runs at a time: two 100 MB archives at once
87
+ * would fight over the same disk for no gain.
88
+ * @param {'backup' | 'library-export'} kind - what is being packed.
89
+ * @param {() => Promise<object>} run - the packing work; its result is kept on the job.
90
+ * @returns {{ id: string, kind: string, status: 'running' | 'done' | 'error', startedAt: number, finishedAt: number, result: object | null, error: string | null }} the job (mutated as it runs).
91
+ */
92
+ export function startExportJob(kind, run) {
93
+ if (listJobs().some(job => job.status === 'running')) {
94
+ throw Object.assign(new Error('已有打包任务在进行,请等它完成'), { code: 'busy' })
95
+ }
96
+ const id = `ex-${Date.now().toString(36)}${Math.random().toString(36).slice(2, 6)}`
97
+ const job = { id, kind, status: 'running', startedAt: Date.now(), finishedAt: 0, result: null, error: null }
98
+ jobs.set(id, job)
99
+ void (async () => {
100
+ try {
101
+ job.result = await run()
102
+ job.status = 'done'
103
+ } catch (error) {
104
+ job.error = String(error && error.message ? error.message : error)
105
+ job.status = 'error'
106
+ } finally {
107
+ job.finishedAt = Date.now()
108
+ const finished = listJobs().filter(entry => entry.status !== 'running')
109
+ for (const stale of finished.slice(MAX_JOBS)) jobs.delete(stale.id)
110
+ }
111
+ })()
112
+ return job
113
+ }
114
+
115
+ /**
116
+ * @returns {Array<object>} jobs, newest first.
117
+ */
118
+ export function listJobs() {
119
+ return [...jobs.values()].sort((left, right) => right.startedAt - left.startedAt)
120
+ }
121
+
122
+ /**
123
+ * Show one file in the OS file manager. The caller validates that the path is
124
+ * inside the plugin's own backup folder.
125
+ * @param {string} path - file to reveal.
126
+ * @param {import('node:child_process').SpawnOptions} [options] - override (tests inject a recorder).
127
+ * @returns {void}
128
+ */
129
+ export function revealInFileManager(path) {
130
+ const spawnOptions = { detached: true, stdio: 'ignore', windowsHide: false }
131
+ if (process.platform === 'win32') {
132
+ spawn('explorer.exe', ['/select,', path], spawnOptions).unref()
133
+ return
134
+ }
135
+ if (process.platform === 'darwin') {
136
+ spawn('open', ['-R', path], spawnOptions).unref()
137
+ return
138
+ }
139
+ spawn('xdg-open', [dirname(path)], spawnOptions).unref()
140
+ }
141
+
142
+ /**
143
+ * Open a folder in the OS file manager. Used for the library root, where there
144
+ * is no single file to select.
145
+ * @param {string} path - folder to open.
146
+ * @returns {void}
147
+ */
148
+ export function revealDirectory(path) {
149
+ const spawnOptions = { detached: true, stdio: 'ignore', windowsHide: false }
150
+ if (process.platform === 'win32') {
151
+ spawn('explorer.exe', [path], spawnOptions).unref()
152
+ return
153
+ }
154
+ if (process.platform === 'darwin') {
155
+ spawn('open', [path], spawnOptions).unref()
156
+ return
157
+ }
158
+ spawn('xdg-open', [path], spawnOptions).unref()
159
+ }
package/src/host/http.js CHANGED
@@ -69,7 +69,7 @@ export function sendJson(response, status, payload) {
69
69
  * @param {string} [disposition] - optional `content-disposition` value.
70
70
  * @returns {void}
71
71
  */
72
- export function sendBytes(response, status, body, contentType, disposition) {
72
+ export function sendBytes(response, status, body, contentType, disposition, cache) {
73
73
  if (response.writableEnded || response.destroyed) return
74
74
  const headers = {
75
75
  'content-type': contentType,
@@ -78,10 +78,37 @@ export function sendBytes(response, status, body, contentType, disposition) {
78
78
  'content-length': body.length,
79
79
  }
80
80
  if (disposition !== undefined) headers['content-disposition'] = disposition
81
+ // A small, rarely-changing file (a cover image) may be reused: without an
82
+ // entity tag the browser re-downloads it on every mount, because the default
83
+ // for this server is `no-store`.
84
+ if (cache !== undefined && typeof cache.etag === 'string' && cache.etag !== '') {
85
+ headers.etag = cache.etag
86
+ headers['cache-control'] = `private, max-age=${Number(cache.maxAge) || 300}`
87
+ }
81
88
  response.writeHead(status, headers)
82
89
  response.end(body)
83
90
  }
84
91
 
92
+ /**
93
+ * Answer a conditional GET with `304 Not Modified` when the client already has
94
+ * this version, so a cached cover is not downloaded again.
95
+ * @param {import('node:http').IncomingMessage} request - inbound request.
96
+ * @param {import('node:http').ServerResponse} response - outbound response.
97
+ * @param {string} etag - current entity tag.
98
+ * @returns {boolean} true when a 304 was written (the caller must then stop).
99
+ */
100
+ export function sendNotModified(request, response, etag) {
101
+ const header = request.headers['if-none-match']
102
+ if (typeof header !== 'string' || header === '') return false
103
+ const wanted = header.trim()
104
+ const matched = wanted === '*' || wanted.split(',').some(part => part.trim() === etag)
105
+ if (!matched) return false
106
+ if (response.writableEnded || response.destroyed) return true
107
+ response.writeHead(304, { etag, 'cache-control': 'private, max-age=300' })
108
+ response.end()
109
+ return true
110
+ }
111
+
85
112
  /**
86
113
  * Send one failure envelope.
87
114
  * @param {import('node:http').ServerResponse} response - the outbound response.