@cloud411716/fancy-webnovel 4.0.1 → 4.0.2

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/README.md CHANGED
@@ -37,6 +37,11 @@ allowBuilds:
37
37
 
38
38
  然后重新执行安装。`tesseract.js` 的 postinstall 只是 OpenCollective 提示;不允许构建时 OCR 模式不可用,但插件其余功能不受影响。
39
39
 
40
+ 番茄 OCR 模式会优先使用本地 `chi_sim.traineddata`,不会默认走 jsdelivr 下载:
41
+ 依次查找 `FANCY_TESSDATA_DIR`、`$DSH_HOME`、`~/.dsh`、工作区 `.fancy/tessdata`、
42
+ 工作区 `tessdata` 和系统 tessdata 目录。DSH 沙箱里外网请求经常是 `fetch failed`,
43
+ 把 `chi_sim.traineddata` 放到上述任一目录即可离线 OCR。
44
+
40
45
  ## 依赖解析
41
46
 
42
47
  依赖声明在 `dependencies` / `peerDependencies` 中。用 `dsh plugin add` 从 npm 安装时,
@@ -1,8 +1,11 @@
1
1
  /**
2
2
  * scraper-utils.js — 采集脚本公共工具
3
3
  *
4
- * 提供各平台 scraper 公用的纯函数,无外部依赖。
4
+ * 提供各平台 scraper 公用的纯函数;除 tesseract 本地语言包定位外不依赖外部服务。
5
5
  */
6
+ import fs from 'fs';
7
+ import os from 'os';
8
+ import path from 'path';
6
9
 
7
10
  export function getArg(args, name) {
8
11
  const i = args.indexOf(name);
@@ -13,6 +16,36 @@ export function getArg(args, name) {
13
16
  return inline === undefined ? null : String(inline).slice(prefix.length);
14
17
  }
15
18
 
19
+ /**
20
+ * 找本地 chi_sim.traineddata,避免 tesseract.js 默认从 jsdelivr 下载。
21
+ *
22
+ * DSH 的 bash 沙箱通常只放行工作区/本机文件,外网请求会直接 fetch failed。
23
+ * ~/.dsh 下已经有安装时带的 chi_sim.traineddata,直接把它作为 langPath +
24
+ * cachePath 即可:worker 先读 cache,读到了就不会再走网络。
25
+ *
26
+ * 支持 chi_sim.traineddata(未压缩)和 chi_sim.traineddata.gz(压缩)。
27
+ *
28
+ * @returns {{dir:string,gzip:boolean}|null}
29
+ */
30
+ export function resolveTessdata() {
31
+ const home = process.env.HOME || os.homedir();
32
+ const dshHome = process.env.DSH_HOME || path.join(home, '.dsh');
33
+ const candidates = [
34
+ process.env.FANCY_TESSDATA_DIR,
35
+ dshHome,
36
+ path.join(process.cwd(), '.fancy', 'tessdata'),
37
+ path.join(process.cwd(), 'tessdata'),
38
+ '/usr/share/tesseract-ocr/5/tessdata',
39
+ '/usr/share/tesseract-ocr/4.00/tessdata',
40
+ '/usr/share/tessdata',
41
+ ].filter(Boolean);
42
+ for (const dir of candidates) {
43
+ if (fs.existsSync(path.join(dir, 'chi_sim.traineddata'))) return { dir, gzip: false };
44
+ if (fs.existsSync(path.join(dir, 'chi_sim.traineddata.gz'))) return { dir, gzip: true };
45
+ }
46
+ return null;
47
+ }
48
+
16
49
  // ---------------------------------------------------------------------------
17
50
  // 扫描方式(取数路径)
18
51
  // ---------------------------------------------------------------------------
package/lib/workspace.js CHANGED
@@ -286,6 +286,34 @@ export function registerGlobalWorkspace(root, opts = {}) {
286
286
  return entry;
287
287
  }
288
288
 
289
+ /**
290
+ * Remove a workspace from the machine-level registry.
291
+ *
292
+ * Test helpers create temporary workspaces through `initWorkspace`; without
293
+ * unregistering, every run leaves a stale `/tmp/fancy-ws-test-*` entry behind.
294
+ * Also moves the legacy pointer if it still targets the removed workspace.
295
+ *
296
+ * @param {string} root workspace directory
297
+ * @returns {boolean} true when an entry was removed
298
+ */
299
+ export function unregisterGlobalWorkspace(root) {
300
+ const workspacePath = path.resolve(root);
301
+ const registry = readGlobalRegistry();
302
+ const remaining = registry.workspaces.filter((entry) => entry.workspace !== workspacePath);
303
+ const removed = remaining.length !== registry.workspaces.length;
304
+ if (removed) writeGlobalRegistry({ ...registry, workspaces: remaining });
305
+
306
+ const pointer = readGlobalPointer();
307
+ if (pointer && pointer.workspace === workspacePath) {
308
+ if (remaining.length) {
309
+ writeGlobalPointer(remaining[0].workspace);
310
+ } else {
311
+ try { fs.unlinkSync(globalPointerPath()); } catch { /* already gone */ }
312
+ }
313
+ }
314
+ return removed;
315
+ }
316
+
289
317
  export function listGlobalWorkspaces() {
290
318
  return readGlobalRegistry().workspaces
291
319
  .map((entry) => ({ ...entry, exists: isWorkspace(entry.workspace) }))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloud411716/fancy-webnovel",
3
- "version": "4.0.1",
3
+ "version": "4.0.2",
4
4
  "type": "module",
5
5
  "main": "index.js",
6
6
  "exports": {
@@ -26,7 +26,7 @@ import path from 'path';
26
26
  import { pathToFileURL } from 'url';
27
27
  import { chromium } from 'playwright-core';
28
28
  import Tesseract from 'tesseract.js';
29
- import { getArg, localDateStamp, runCli, normalizeScanMode, SCAN_MODE_API, renderIntroLines, sanitizeBookFields, sanitizeMarkdownInline } from '#scraper-utils';
29
+ import { getArg, localDateStamp, runCli, normalizeScanMode, SCAN_MODE_API, renderIntroLines, sanitizeBookFields, sanitizeMarkdownInline, resolveTessdata } from '#scraper-utils';
30
30
 
31
31
  // 捕获未处理的 promise 错误——只写 stderr,让上层重试逻辑处理,不要直接退出
32
32
  process.on('unhandledRejection', (reason) => {
@@ -181,7 +181,17 @@ async function resetOcrWorker() {
181
181
  async function getOcrWorker() {
182
182
  if (_ocrWorker) return _ocrWorker;
183
183
  if (_ocrInitPromise) return _ocrInitPromise;
184
- const init = Tesseract.createWorker('chi_sim');
184
+ const tessdata = resolveTessdata();
185
+ if (!tessdata) {
186
+ process.stderr.write('[ocr] 未找到本地 chi_sim.traineddata,将尝试在线下载;如果网络被沙箱拦截,请把文件放到 ~/.dsh/ 或设置 FANCY_TESSDATA_DIR\n');
187
+ }
188
+ const init = Tesseract.createWorker('chi_sim', 1, tessdata
189
+ ? {
190
+ langPath: tessdata.dir,
191
+ cachePath: tessdata.dir,
192
+ gzip: tessdata.gzip,
193
+ }
194
+ : {});
185
195
  _ocrInitPromise = (async () => {
186
196
  try {
187
197
  const w = await withTimeout(init, OCR_INIT_TIMEOUT_MS, 'OCR worker 初始化');