@capyclaw/offline-ddr0 0.0.0-stage → 1.9.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 +441 -2
- package/client/client.js +603 -0
- package/cordis.patch.yml +13 -0
- package/docs/STORAGE-MIGRATION.md +136 -0
- package/lib/atomic.mjs +40 -0
- package/lib/http-cache.mjs +42 -0
- package/lib/inbox.mjs +64 -0
- package/lib/media.mjs +242 -0
- package/lib/migrate-json.mjs +101 -0
- package/lib/mirror-index.mjs +105 -0
- package/lib/mirror.mjs +123 -0
- package/lib/proxy.mjs +188 -0
- package/lib/remote.mjs +158 -0
- package/lib/render.mjs +93 -0
- package/lib/repository.mjs +71 -0
- package/lib/store.mjs +610 -0
- package/lib/surface.mjs +74 -0
- package/lib/sync-ui.mjs +108 -0
- package/lib/sync.mjs +433 -0
- package/lib/wire.mjs +211 -0
- package/lib/workstore-repository.mjs +316 -0
- package/lib/workstore.mjs +338 -0
- package/mirror/files/00001.html +2045 -0
- package/mirror/files/00002.json +1 -0
- package/mirror/files/00003.json +1 -0
- package/mirror/files/00004.json +1 -0
- package/mirror/files/00005.json +1 -0
- package/mirror/files/00006.json +1 -0
- package/mirror/files/00007.json +1 -0
- package/mirror/files/00008.json +1 -0
- package/mirror/files/00009.json +1 -0
- package/mirror/files/00010.json +1 -0
- package/mirror/files/00011.json +1 -0
- package/mirror/files/00012.json +1 -0
- package/mirror/files/00013.json +1 -0
- package/mirror/files/00014.json +1 -0
- package/mirror/files/00015.json +1 -0
- package/mirror/files/00016.json +1 -0
- package/mirror/files/00017.json +1 -0
- package/mirror/files/00018.json +1 -0
- package/mirror/files/00019.json +1 -0
- package/mirror/files/00020.json +1 -0
- package/mirror/files/00021.json +1 -0
- package/mirror/files/00022.json +1 -0
- package/mirror/files/00023.js +205 -0
- package/mirror/files/00024.json +1 -0
- package/mirror/files/00025.json +1 -0
- package/mirror/files/00026.json +1 -0
- package/mirror/files/00027.js +51 -0
- package/mirror/files/00028.json +1 -0
- package/mirror/files/00029.json +1 -0
- package/mirror/files/00030.json +1 -0
- package/mirror/files/00031.json +1 -0
- package/mirror/files/00032.json +1 -0
- package/mirror/files/00033.json +1 -0
- package/mirror/files/00034.json +1 -0
- package/mirror/files/00035.json +1 -0
- package/mirror/files/00036.json +1 -0
- package/mirror/files/00037.json +1 -0
- package/mirror/files/00038.json +1 -0
- package/mirror/files/00039.json +1 -0
- package/mirror/files/00040.js +2311 -0
- package/mirror/files/00041.json +1 -0
- package/mirror/files/00042.json +1 -0
- package/mirror/files/00043.json +1 -0
- package/mirror/files/00044.json +1 -0
- package/mirror/files/00045.js +575 -0
- package/mirror/files/00046.json +1 -0
- package/mirror/files/00047.js +575 -0
- package/mirror/files/00048.js +575 -0
- package/mirror/files/00049.json +1 -0
- package/mirror/files/00050.json +1 -0
- package/mirror/files/00051.json +1 -0
- package/mirror/files/00052.json +1 -0
- package/mirror/files/00053.js +575 -0
- package/mirror/files/00054.json +1 -0
- package/mirror/files/00055.json +1 -0
- package/mirror/files/00056.js +575 -0
- package/mirror/files/00057.json +1 -0
- package/mirror/files/00058.json +1 -0
- package/mirror/files/00059.js +360 -0
- package/mirror/files/00060.json +1 -0
- package/mirror/files/00061.json +1 -0
- package/mirror/files/00062.json +1 -0
- package/mirror/files/00063.json +1 -0
- package/mirror/files/00064.json +1 -0
- package/mirror/files/00065.js +144 -0
- package/mirror/files/00066.js +7 -0
- package/mirror/files/00067.json +1 -0
- package/mirror/files/00068.json +1 -0
- package/mirror/files/00069.json +1 -0
- package/mirror/files/00070.json +1 -0
- package/mirror/files/00071.json +1 -0
- package/mirror/files/00072.json +1 -0
- package/mirror/files/00073.js +1 -0
- package/mirror/files/00074.json +1 -0
- package/mirror/files/00075.json +1 -0
- package/mirror/files/00076.json +1 -0
- package/mirror/files/00077.js +45 -0
- package/mirror/files/00078.js +1348 -0
- package/mirror/files/00079.json +1 -0
- package/mirror/files/00080.json +1 -0
- package/mirror/files/00081.js +1 -0
- package/mirror/files/00082.json +1 -0
- package/mirror/files/00083.json +1 -0
- package/mirror/files/00084.js +7 -0
- package/mirror/files/00085.json +1 -0
- package/mirror/files/00086.json +1 -0
- package/mirror/files/00087.js +7 -0
- package/mirror/files/00088.json +1 -0
- package/mirror/files/00089.json +1 -0
- package/mirror/files/00090.js +7 -0
- package/mirror/files/00091.json +1 -0
- package/mirror/files/00092.json +1 -0
- package/mirror/files/00093.js +102 -0
- package/mirror/files/00094.json +1 -0
- package/mirror/files/00095.json +1 -0
- package/mirror/files/00096.json +1 -0
- package/mirror/files/00097.json +1 -0
- package/mirror/files/00098.json +1 -0
- package/mirror/files/00099.json +1 -0
- package/mirror/files/00100.json +1 -0
- package/mirror/files/00101.js +102 -0
- package/mirror/files/00102.json +1 -0
- package/mirror/files/00103.json +1 -0
- package/mirror/files/00104.json +1 -0
- package/mirror/files/00105.json +1 -0
- package/mirror/files/00106.json +1 -0
- package/mirror/files/00107.js +110 -0
- package/mirror/files/00108.json +1 -0
- package/mirror/files/00109.json +1 -0
- package/mirror/files/00110.json +1 -0
- package/mirror/files/00111.json +1 -0
- package/mirror/files/00112.json +1 -0
- package/mirror/files/00113.json +1 -0
- package/mirror/files/00114.json +1 -0
- package/mirror/files/00115.json +1 -0
- package/mirror/files/00116.json +1 -0
- package/mirror/files/00117.json +1 -0
- package/mirror/files/00118.json +1 -0
- package/mirror/files/00119.json +1 -0
- package/mirror/files/00120.json +1 -0
- package/mirror/files/00121.json +1 -0
- package/mirror/files/00122.json +1 -0
- package/mirror/files/00123.json +1 -0
- package/mirror/files/00124.json +1 -0
- package/mirror/files/00125.json +1 -0
- package/mirror/files/00126.json +1 -0
- package/mirror/files/00127.json +1 -0
- package/mirror/files/00128.json +1 -0
- package/mirror/files/00129.json +1 -0
- package/mirror/files/00130.json +1 -0
- package/mirror/files/00131.json +1 -0
- package/mirror/files/00132.js +110 -0
- package/mirror/files/00133.json +1 -0
- package/mirror/files/00134.json +1 -0
- package/mirror/files/00135.json +1 -0
- package/mirror/files/00136.json +1 -0
- package/mirror/files/00137.js +26 -0
- package/mirror/files/00138.json +1 -0
- package/mirror/files/00139.json +1 -0
- package/mirror/files/00140.json +1 -0
- package/mirror/files/00141.json +1 -0
- package/mirror/files/00142.json +1 -0
- package/mirror/files/00143.json +1 -0
- package/mirror/files/00144.js +26 -0
- package/mirror/files/00145.json +1 -0
- package/mirror/files/00146.json +1 -0
- package/mirror/files/00147.js +29 -0
- package/mirror/files/00148.json +1 -0
- package/mirror/files/00149.json +1 -0
- package/mirror/files/00150.js +23 -0
- package/mirror/files/00151.json +1 -0
- package/mirror/files/00152.json +1 -0
- package/mirror/files/00153.js +719 -0
- package/mirror/files/00154.js +9575 -0
- package/mirror/files/00155.js +108 -0
- package/mirror/files/00156.js +379 -0
- package/mirror/files/00157.js +48 -0
- package/mirror/files/00158.json +16 -0
- package/mirror/manifest.json +1582 -0
- package/node_modules/@capyclaw/asset-service/README.md +48 -0
- package/node_modules/@capyclaw/asset-service/cordis.patch.yml +4 -0
- package/node_modules/@capyclaw/asset-service/dsh-suite.json +29 -0
- package/node_modules/@capyclaw/asset-service/lib/host.mjs +14 -0
- package/node_modules/@capyclaw/asset-service/package.json +59 -0
- package/node_modules/@capyclaw/asset-service/src/index.mjs +223 -0
- package/node_modules/@capyclaw/foundation/README.md +51 -0
- package/node_modules/@capyclaw/foundation/abi/bridge.mjs +86 -0
- package/node_modules/@capyclaw/foundation/abi/errors.mjs +63 -0
- package/node_modules/@capyclaw/foundation/abi/index.mjs +35 -0
- package/node_modules/@capyclaw/foundation/abi/kinds.mjs +271 -0
- package/node_modules/@capyclaw/foundation/abi/loader.mjs +345 -0
- package/node_modules/@capyclaw/foundation/abi/schema.mjs +98 -0
- package/node_modules/@capyclaw/foundation/abi/spec.mjs +255 -0
- package/node_modules/@capyclaw/foundation/abi/validate.mjs +24 -0
- package/node_modules/@capyclaw/foundation/abi/version.mjs +82 -0
- package/node_modules/@capyclaw/foundation/cordis.patch.yml +5 -0
- package/node_modules/@capyclaw/foundation/kit/index.mjs +6 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/client-panel.mjs +98 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/host-event.mjs +35 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/http-router.mjs +69 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/lifecycle-hook.mjs +55 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/own-server.mjs +113 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/raw.mjs +68 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/runtime-driver.mjs +113 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/service-provider.mjs +77 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/skill-provider.mjs +56 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/tool-provider.mjs +50 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/ui-panel.mjs +64 -0
- package/node_modules/@capyclaw/foundation/lib/ctx-service.mjs +42 -0
- package/node_modules/@capyclaw/foundation/lib/host/caps.mjs +34 -0
- package/node_modules/@capyclaw/foundation/lib/host/contract.mjs +85 -0
- package/node_modules/@capyclaw/foundation/lib/host/dsh.mjs +50 -0
- package/node_modules/@capyclaw/foundation/lib/host/memory.mjs +160 -0
- package/node_modules/@capyclaw/foundation/lib/host.mjs +23 -0
- package/node_modules/@capyclaw/foundation/lib/kernel.mjs +185 -0
- package/node_modules/@capyclaw/foundation/lib/skill/scan.mjs +87 -0
- package/node_modules/@capyclaw/foundation/lib/subsystems/events.mjs +60 -0
- package/node_modules/@capyclaw/foundation/lib/subsystems/log.mjs +51 -0
- package/node_modules/@capyclaw/foundation/lib/subsystems/net.mjs +188 -0
- package/node_modules/@capyclaw/foundation/package.json +53 -0
- package/node_modules/@capyclaw/foundation/runtime/index.mjs +20 -0
- package/node_modules/@capyclaw/plugin-kit/INTEGRATION.md +100 -0
- package/node_modules/@capyclaw/plugin-kit/README.md +70 -0
- package/node_modules/@capyclaw/plugin-kit/bin/sync.mjs +103 -0
- package/node_modules/@capyclaw/plugin-kit/package.json +69 -0
- package/node_modules/@capyclaw/plugin-kit/src/cli.mjs +56 -0
- package/node_modules/@capyclaw/plugin-kit/src/config.mjs +142 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat-worker.js +129 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/lease.mjs +139 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/shadow.mjs +219 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/transport.js +13 -0
- package/node_modules/@capyclaw/plugin-kit/src/index.mjs +13 -0
- package/node_modules/@capyclaw/plugin-kit/src/pack.mjs +230 -0
- package/node_modules/@capyclaw/plugin-kit/src/runtime/context.mjs +128 -0
- package/node_modules/@capyclaw/plugin-kit/src/self-contained.mjs +307 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/api.mjs +45 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/http.mjs +113 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/mcp-http.mjs +150 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/server.mjs +131 -0
- package/node_modules/@capyclaw/plugin-kit/src/skills/provider.mjs +82 -0
- package/node_modules/@capyclaw/plugin-kit/src/store/blob-store.mjs +109 -0
- package/node_modules/@capyclaw/plugin-kit/src/store/content-store.mjs +17 -0
- package/node_modules/@capyclaw/plugin-kit/src/store/journal-store.mjs +89 -0
- package/node_modules/@capyclaw/plugin-kit/src/tools/result.mjs +57 -0
- package/node_modules/@capyclaw/plugin-kit/src/tools/validate.mjs +150 -0
- package/node_modules/@capyclaw/plugin-kit/src/tuning.mjs +82 -0
- package/node_modules/@capyclaw/plugin-kit/src/util/fs-lock.mjs +132 -0
- package/node_modules/@capyclaw/plugin-kit/src/util/http.mjs +130 -0
- package/node_modules/@capyclaw/plugin-kit/src/workspace/manager.mjs +656 -0
- package/node_modules/@capyclaw/plugin-kit/src/zip.mjs +181 -0
- package/node_modules/@capyclaw/service-host/README.md +86 -0
- package/node_modules/@capyclaw/service-host/cordis.patch.yml +4 -0
- package/node_modules/@capyclaw/service-host/dsh-suite.json +29 -0
- package/node_modules/@capyclaw/service-host/lib/host.mjs +22 -0
- package/node_modules/@capyclaw/service-host/package.json +60 -0
- package/node_modules/@capyclaw/service-host/src/index.mjs +571 -0
- package/node_modules/@capyclaw/service-host/src/tuning.mjs +63 -0
- package/node_modules/@capyclaw/suite-kit/README.md +238 -0
- package/node_modules/@capyclaw/suite-kit/bin/dsh-domain.mjs +98 -0
- package/node_modules/@capyclaw/suite-kit/bin/dsh-suite.mjs +174 -0
- package/node_modules/@capyclaw/suite-kit/client/embed.js +35 -0
- package/node_modules/@capyclaw/suite-kit/domain.schema.json +129 -0
- package/node_modules/@capyclaw/suite-kit/package.json +54 -0
- package/node_modules/@capyclaw/suite-kit/src/activation-registry.mjs +184 -0
- package/node_modules/@capyclaw/suite-kit/src/artifacts.mjs +131 -0
- package/node_modules/@capyclaw/suite-kit/src/capability.mjs +181 -0
- package/node_modules/@capyclaw/suite-kit/src/compose.mjs +201 -0
- package/node_modules/@capyclaw/suite-kit/src/domain.mjs +138 -0
- package/node_modules/@capyclaw/suite-kit/src/embed.mjs +81 -0
- package/node_modules/@capyclaw/suite-kit/src/index.mjs +10 -0
- package/node_modules/@capyclaw/suite-kit/src/manifest.mjs +263 -0
- package/node_modules/@capyclaw/suite-kit/src/orchestration.mjs +100 -0
- package/node_modules/@capyclaw/suite-kit/src/profile.mjs +584 -0
- package/node_modules/@capyclaw/suite-kit/src/surface.mjs +28 -0
- package/node_modules/@capyclaw/suite-kit/suite.schema.json +112 -0
- package/node_modules/@capyclaw/work-store/README.md +83 -0
- package/node_modules/@capyclaw/work-store/STANDARD.md +257 -0
- package/node_modules/@capyclaw/work-store/cordis.patch.yml +3 -0
- package/node_modules/@capyclaw/work-store/dsh-suite.json +26 -0
- package/node_modules/@capyclaw/work-store/icon.svg +7 -0
- package/node_modules/@capyclaw/work-store/lib/host.mjs +21 -0
- package/node_modules/@capyclaw/work-store/package.json +68 -0
- package/node_modules/@capyclaw/work-store/src/backend.mjs +89 -0
- package/node_modules/@capyclaw/work-store/src/db.mjs +199 -0
- package/node_modules/@capyclaw/work-store/src/envelope.mjs +132 -0
- package/node_modules/@capyclaw/work-store/src/index.mjs +570 -0
- package/node_modules/@capyclaw/work-store/src/interop.mjs +70 -0
- package/node_modules/@capyclaw/work-store/src/refs.mjs +99 -0
- package/node_modules/@capyclaw/work-store/src/routes.mjs +135 -0
- package/package.json +82 -4
- package/plugin.mjs +636 -0
- package/static/index.html +2068 -0
package/plugin.mjs
ADDED
|
@@ -0,0 +1,636 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @capyclaw/offline-ddr0:foundation ABI 入口。
|
|
3
|
+
*
|
|
4
|
+
* 把 port-8223(离线版 ddr0:零登录、单文件前端、快照回放 API)挂到共享宿主,
|
|
5
|
+
* 不自己 createServer/listen:
|
|
6
|
+
* - http-router 托管 static/index.html(同步自 port-8223 的打包产物),SPA 深链
|
|
7
|
+
* 回落到同一份 index.html;
|
|
8
|
+
* - /api/*、/config.json、/media/* 由 lib/proxy.mjs 反代到离线镜像后端,
|
|
9
|
+
* /config.json 的 apiBaseUrl 在回程里改写成本次请求的公共前缀,避免前端把
|
|
10
|
+
* 接口前缀打回主站子路径。
|
|
11
|
+
*
|
|
12
|
+
* 公共前缀(浏览器看到的那一段)不写死:service-host 只把 mount 之后的部分交给
|
|
13
|
+
* 这里,所以每次请求用 `req.url`(原始全路径)减掉 `url.pathname`(已剥前缀)
|
|
14
|
+
* 现算。宿主把插件挂在 /offline-ddr0 就得到 /offline-ddr0,挂在
|
|
15
|
+
* /plugins/xxx/offline-ddr0 就得到那一段,前端注入与 config.json 改写跟着走。
|
|
16
|
+
*/
|
|
17
|
+
import fs from 'node:fs'
|
|
18
|
+
import os from 'node:os'
|
|
19
|
+
import path from 'node:path'
|
|
20
|
+
import { fileURLToPath } from 'node:url'
|
|
21
|
+
import { clientPanel, createPlugin, httpRouter, lifecycleHook } from '@capyclaw/foundation/abi'
|
|
22
|
+
import { createProxyHandler } from './lib/proxy.mjs'
|
|
23
|
+
import { createMirror } from './lib/mirror.mjs'
|
|
24
|
+
import { createMirrorIndex } from './lib/mirror-index.mjs'
|
|
25
|
+
import { resolveLocalRepository } from './lib/repository.mjs'
|
|
26
|
+
import { createWorkStoreSource, mergeLocalItems } from './lib/workstore.mjs'
|
|
27
|
+
import { createSync } from './lib/sync.mjs'
|
|
28
|
+
import { syncButtonShim } from './lib/sync-ui.mjs'
|
|
29
|
+
import { createRemoteClient } from './lib/remote.mjs'
|
|
30
|
+
import { createMediaStore, createMediaHandler, resolveMediaStore } from './lib/media.mjs'
|
|
31
|
+
import { attachPrefixTolerantSurface, registerWorksSurface, unregisterWorksSurface } from './lib/surface.mjs'
|
|
32
|
+
import { sendCached } from './lib/http-cache.mjs'
|
|
33
|
+
import { writeFileAtomic } from './lib/atomic.mjs'
|
|
34
|
+
|
|
35
|
+
const MOUNT = '/offline-ddr0'
|
|
36
|
+
const DEFAULT_BACKEND = 'https://ddr0.com/apps/works/3904'
|
|
37
|
+
// foundation 的共享宿主默认挂在这个前缀下,浏览器里看到的站点地址就是
|
|
38
|
+
// `${DEFAULT_SITE_PATH}/`。宿主换过 http.basePath 时用 config.sitePath 覆盖。
|
|
39
|
+
const DEFAULT_SITE_PATH = '/plugins/foundation-shared/offline-ddr0'
|
|
40
|
+
|
|
41
|
+
// 打包器 D 相注入的 history.pushState 前缀;同步前端后这一行必须还在。
|
|
42
|
+
const SUB_MARKER = "var SUB = '/offline-ddr0';"
|
|
43
|
+
|
|
44
|
+
// routes() 里起的轮询/句柄由 lifecycleHook.onStop 统一回收(V2-4 谁开谁关)。
|
|
45
|
+
// http-router 适配器只 unmount 不清理 routes 内部资源,故在此显式登记。
|
|
46
|
+
let cleanup = null
|
|
47
|
+
|
|
48
|
+
function resolveConfig(config = {}) {
|
|
49
|
+
// storage:'workstore'(缺省,唯一真源 SQLite)| 'json'(回滚开关,保留一个发布周期)。
|
|
50
|
+
// env DSH_OFFLINE_DDR0_STORAGE 可覆盖;非法值回落 workstore 并告警在装配处。
|
|
51
|
+
const raw = String(config.storage || process.env.DSH_OFFLINE_DDR0_STORAGE || 'workstore').toLowerCase()
|
|
52
|
+
const storage = raw === 'json' ? 'json' : 'workstore'
|
|
53
|
+
// 主站同步(手动触发,离线优先):源站直连参数 + 可选会话令牌。
|
|
54
|
+
const syncCfg = (config && typeof config.sync === 'object' && config.sync) || {}
|
|
55
|
+
const sync = {
|
|
56
|
+
originIp: String(syncCfg.originIp || process.env.DDR0_ORIGIN_IP || '162.251.95.51'),
|
|
57
|
+
originHost: String(syncCfg.originHost || process.env.DDR0_ORIGIN_HOST || 'ddr0.com'),
|
|
58
|
+
token: String(syncCfg.token || process.env.DSH_DDR0_SESSION || ''),
|
|
59
|
+
includeNew: syncCfg.includeNew !== false,
|
|
60
|
+
}
|
|
61
|
+
return {
|
|
62
|
+
backendUrl: String(
|
|
63
|
+
config.backendUrl || process.env.DSH_OFFLINE_DDR0_BACKEND_URL || DEFAULT_BACKEND,
|
|
64
|
+
).replace(/\/+$/, ''),
|
|
65
|
+
sitePath: String(config.sitePath || DEFAULT_SITE_PATH).replace(/\/+$/, ''),
|
|
66
|
+
storage,
|
|
67
|
+
sync,
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// 本地作品库落在 profile 级 plugin-data:升级/重装插件不丢已入库的内容。
|
|
72
|
+
function resolveStoreDir() {
|
|
73
|
+
const profileDir = process.env.DSH_PROFILE_DIR
|
|
74
|
+
|| path.join(os.homedir(), '.dsh', 'profiles', process.env.DSH_PROFILE || 'desktop')
|
|
75
|
+
return path.join(profileDir, 'plugin-data', 'offline-ddr0')
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** 浏览器可见前缀:原始请求路径去掉 mount 之后的部分。 */
|
|
79
|
+
function splitMount(req, url) {
|
|
80
|
+
const pathname = url?.pathname || '/'
|
|
81
|
+
let original = pathname
|
|
82
|
+
try {
|
|
83
|
+
original = new URL(req.url || '/', 'http://127.0.0.1').pathname
|
|
84
|
+
} catch { /* 保留 pathname */ }
|
|
85
|
+
|
|
86
|
+
// service-host 剥前缀时用 `new URL(req.url)` 现算,子 app 可能看到三种形态:
|
|
87
|
+
// a) '/api/x' 已剥干净 → 公共前缀 = 原始路径减去它
|
|
88
|
+
// b) '/offline-ddr0/api/x' 只剥了外层 → 前缀 = mount 之前那一段
|
|
89
|
+
// c) '/<外层>/offline-ddr0/api/x' 什么都没剥 → 同上
|
|
90
|
+
let rest = pathname
|
|
91
|
+
const at = pathname.indexOf(MOUNT + '/')
|
|
92
|
+
if (pathname === MOUNT) rest = '/'
|
|
93
|
+
else if (at >= 0) rest = pathname.slice(at + MOUNT.length)
|
|
94
|
+
|
|
95
|
+
// rest 始终是原始路径的后缀,公共前缀 = 原始路径减掉 rest(含 mount 那一段)。
|
|
96
|
+
const prefix = original.endsWith(rest) ? original.slice(0, original.length - rest.length) : ''
|
|
97
|
+
return { rest: rest || '/', prefix: prefix.replace(/\/+$/, '') }
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export const { name, inject, apply } = createPlugin({
|
|
101
|
+
name: 'offline-ddr0',
|
|
102
|
+
// workStore.store:workstore 模式的唯一真源底座(声明依赖;非 os.* 能力不硬门禁,
|
|
103
|
+
// 服务不在场时装配层降级 JSON 库并告警,迁移推迟到服务可用的下次启动)。
|
|
104
|
+
requires: ['os.net@^1', 'workStore.store@^1'],
|
|
105
|
+
implements: [
|
|
106
|
+
// 浏览器半要的只有一段站点地址:入口图标挂在左侧全局面板区,主区域用
|
|
107
|
+
// 同源 iframe 打开它。地址由宿主半给,浏览器半再按请求探测兜底。
|
|
108
|
+
clientPanel({
|
|
109
|
+
// 站点地址优先问共享宿主:嵌入宿主网页端口时给相对前缀(同源,
|
|
110
|
+
// /plugins/foundation-shared/offline-ddr0);一旦共享宿主退化到独立端口
|
|
111
|
+
// (宿主没有网页端口、或 webserver 比 foundation 晚 settle),就给绝对地址,
|
|
112
|
+
// 浏览器半的探测和 iframe 都照这个地址走——插件已发 CORS 头,跨源也能用。
|
|
113
|
+
bootGlobals({ config = {}, os }) {
|
|
114
|
+
const { sitePath } = resolveConfig(config)
|
|
115
|
+
let site = sitePath
|
|
116
|
+
try {
|
|
117
|
+
const net = os?.net
|
|
118
|
+
const info = typeof net?.describe === 'function' ? net.describe() : null
|
|
119
|
+
const origin = String(info?.origin || '').replace(/\/+$/, '')
|
|
120
|
+
if (info && !info.embedded && origin) site = origin + MOUNT
|
|
121
|
+
} catch { /* 拿不到宿主信息就按配置里的默认前缀 */ }
|
|
122
|
+
return { __DSH_OFFLINE_DDR0__: { site } }
|
|
123
|
+
},
|
|
124
|
+
}),
|
|
125
|
+
httpRouter({
|
|
126
|
+
mount: MOUNT,
|
|
127
|
+
routes(app, { config, log, os, service }) {
|
|
128
|
+
const staticDir = fileURLToPath(new URL('./static', import.meta.url))
|
|
129
|
+
const indexFile = path.join(staticDir, 'index.html')
|
|
130
|
+
const proxy = createProxyHandler({
|
|
131
|
+
backendUrl: resolveConfig(config).backendUrl,
|
|
132
|
+
mount: MOUNT,
|
|
133
|
+
log,
|
|
134
|
+
})
|
|
135
|
+
// 断网可用的关键:mirror/ 里是 ddr0.com 真源站的接口快照与作品代码,
|
|
136
|
+
// 命中即由宿主半直出,一个字节都不出网;目录不存在时退回反代。
|
|
137
|
+
// manifest 索引整份只解析一次,mirror 与 work-store 桥共读(单一索引知识)。
|
|
138
|
+
const mirrorDir = fileURLToPath(new URL('./mirror', import.meta.url))
|
|
139
|
+
const mirrorIndex = createMirrorIndex(mirrorDir, { log })
|
|
140
|
+
// 本地作品库(其它会话投递的内容)持久化在 profile plugin-data。
|
|
141
|
+
// 装配层只面向 LocalWorkRepository 契约(lib/repository.mjs),JSON↔work-store 切换零改路由。
|
|
142
|
+
const { storage } = resolveConfig(config)
|
|
143
|
+
const storeDir = resolveStoreDir()
|
|
144
|
+
// work-store 公共服务(ctx.workStore):跨隔离安全探测;不在场给 null。
|
|
145
|
+
let workStoreService = null
|
|
146
|
+
try { workStoreService = (typeof service === 'function' && service('workStore')) || null } catch { /* 服务不在场 */ }
|
|
147
|
+
// asset-service 公共服务(ctx.assets):媒体仓优先接共享池/共享工厂;不在场给 null(私有仓兜底)。
|
|
148
|
+
let assetsService = null
|
|
149
|
+
try { assetsService = (typeof service === 'function' && service('assets')) || null } catch { /* 服务不在场 */ }
|
|
150
|
+
|
|
151
|
+
// 上墙注册(呈现面单一真源):向 tool-host 登记一条「只带 surface、不带 tool」的能力描述符,
|
|
152
|
+
// portal-hud 的 listSurfaces 会把它自动投影成展示墙工作台行(与 ppt/capyclaw 同等地位)。
|
|
153
|
+
// 绝不 registerTool → 零 agent 工具接口;描述符仅是 GUI/编排元数据。同名幂等覆盖,热重载安全。
|
|
154
|
+
// 服务不在场(离线独立)时静默跳过,不影响站点本身。
|
|
155
|
+
let toolHostService = null
|
|
156
|
+
try { toolHostService = (typeof service === 'function' && service('toolHost')) || null } catch { /* 服务不在场 */ }
|
|
157
|
+
registerWorksSurface(toolHostService, { embedPath: resolveConfig(config).sitePath, log })
|
|
158
|
+
|
|
159
|
+
// 装配决策(迁移/降级/回滚)在 lib/repository.mjs,可独立单测。
|
|
160
|
+
const { repo } = resolveLocalRepository({ storage, workStoreService, dir: storeDir, log })
|
|
161
|
+
// 本地 work-store(SQLite)作品源:只读、免登录,把 PPT 等本地生成物按 ddr0
|
|
162
|
+
// 作品契约并进画廊。有共享服务就注入(不自开句柄、不负责 close);无则桥自开只读句柄降级。
|
|
163
|
+
const wsSource = createWorkStoreSource({
|
|
164
|
+
dir: repo.dir,
|
|
165
|
+
mirrorDir,
|
|
166
|
+
log,
|
|
167
|
+
index: mirrorIndex,
|
|
168
|
+
store: workStoreService || undefined,
|
|
169
|
+
})
|
|
170
|
+
const mirror = createMirror(mirrorDir, {
|
|
171
|
+
log,
|
|
172
|
+
index: mirrorIndex,
|
|
173
|
+
transform(pathname, body, search) {
|
|
174
|
+
// 按真实门类分组:分区列表只前置「属于该频道」的本地作品(装饰器/标签推导),
|
|
175
|
+
// 收藏/搜索/总表前置全部;仅第一页前置,深翻页不重复。
|
|
176
|
+
return mergeLocalItems(pathname, body, search, [...wsSource.listItems(), ...repo.list()])
|
|
177
|
+
},
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
// 本地媒体资产仓(内容寻址,sha256 去重):同步时把外部/源站媒体物化进来,
|
|
181
|
+
// 本地直出路由先于 mirror——外部源失效不影响离线放映。两种存储模式都可用。
|
|
182
|
+
// 套件化复用公共基建:优先共享 ctx.assets(store/工厂),无服务时私有仓兜底(离线优先)。
|
|
183
|
+
const mediaStore = resolveMediaStore({ assets: assetsService, dir: path.join(storeDir, 'media-assets'), log })
|
|
184
|
+
const mediaHandler = createMediaHandler({ store: mediaStore, log })
|
|
185
|
+
|
|
186
|
+
// 主站同步(手动触发,离线优先):仅 workstore 模式可用(台账/版本落 SQLite)。
|
|
187
|
+
// remote 是唯一出网模块,只在 POST /api/local/sync 时调用;serving 不依赖它。
|
|
188
|
+
let sync = null
|
|
189
|
+
if (storage === 'workstore' && workStoreService) {
|
|
190
|
+
const { sync: syncCfg } = resolveConfig(config)
|
|
191
|
+
const remote = createRemoteClient({
|
|
192
|
+
originIp: syncCfg.originIp,
|
|
193
|
+
originHost: syncCfg.originHost,
|
|
194
|
+
token: syncCfg.token,
|
|
195
|
+
log,
|
|
196
|
+
})
|
|
197
|
+
sync = createSync({ store: workStoreService, remote, mirrorDir, wsSource, mediaStore, log })
|
|
198
|
+
}
|
|
199
|
+
async function syncApi(req, res) {
|
|
200
|
+
if (!sync) {
|
|
201
|
+
return sendJson(req, res, 501, {
|
|
202
|
+
status: 'error',
|
|
203
|
+
detail: '主站同步仅在 workstore 模式且 workStore 服务在场时可用(当前为 JSON 回滚/降级)',
|
|
204
|
+
})
|
|
205
|
+
}
|
|
206
|
+
if (req.method === 'GET' || req.method === 'HEAD') {
|
|
207
|
+
return sendJson(req, res, 200, { status: 'ok', ...sync.status() })
|
|
208
|
+
}
|
|
209
|
+
if (req.method === 'POST') {
|
|
210
|
+
const opts = (await readJsonBody(req, 8 * 1024)) || {}
|
|
211
|
+
const { sync: syncCfg } = resolveConfig(config)
|
|
212
|
+
const includeNew = opts.includeNew !== undefined ? Boolean(opts.includeNew) : syncCfg.includeNew
|
|
213
|
+
try {
|
|
214
|
+
const report = await sync.run({ force: Boolean(opts.force), includeNew })
|
|
215
|
+
return sendJson(req, res, 200, report)
|
|
216
|
+
} catch (error) {
|
|
217
|
+
const status = error?.code === 'E_SYNC_BUSY' ? 409 : 500
|
|
218
|
+
return sendJson(req, res, status, { status: 'error', detail: error?.message || String(error) })
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
return sendJson(req, res, 405, { status: 'error', detail: 'method not allowed' })
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// 跨会话发现文件:别的会话读它就知道往哪 POST / 往哪个目录丢文件。
|
|
225
|
+
const endpointFile = path.join(repo.dir, 'endpoint.json')
|
|
226
|
+
function endpointInfo(prefix) {
|
|
227
|
+
let net = null
|
|
228
|
+
try {
|
|
229
|
+
net = typeof os?.net?.describe === 'function' ? os.net.describe() : null
|
|
230
|
+
} catch { /* 宿主信息拿不到就给相对前缀 */ }
|
|
231
|
+
const origin = String(net?.origin || '').replace(/\/+$/, '')
|
|
232
|
+
const absolute = origin ? origin + MOUNT : ''
|
|
233
|
+
const base = absolute || prefix || resolveConfig(config).sitePath
|
|
234
|
+
return {
|
|
235
|
+
base,
|
|
236
|
+
absolute,
|
|
237
|
+
port: Number(net?.port) || 0,
|
|
238
|
+
mode: net?.mode || '',
|
|
239
|
+
inbox: repo.inboxDir,
|
|
240
|
+
store: repo.dir,
|
|
241
|
+
count: repo.count,
|
|
242
|
+
api: {
|
|
243
|
+
list: `${base}/api/local/works`,
|
|
244
|
+
create: `${base}/api/local/works`,
|
|
245
|
+
code: `${base}/api/local/works/{id}/code`,
|
|
246
|
+
},
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// inbox 投递口轮询 + endpoint.json 维护(unref:不拖着进程不让退)。
|
|
251
|
+
let lastEndpoint = ''
|
|
252
|
+
const storePoll = setInterval(() => {
|
|
253
|
+
try {
|
|
254
|
+
repo.importInbox()
|
|
255
|
+
} catch { /* 单个坏件不影响下一轮 */ }
|
|
256
|
+
try {
|
|
257
|
+
const info = endpointInfo('')
|
|
258
|
+
if (!info.absolute) return
|
|
259
|
+
const text = JSON.stringify(info, null, 2)
|
|
260
|
+
if (text !== lastEndpoint) {
|
|
261
|
+
lastEndpoint = text
|
|
262
|
+
writeFileAtomic(endpointFile, text)
|
|
263
|
+
}
|
|
264
|
+
} catch { /* 写盘失败下轮再试 */ }
|
|
265
|
+
}, 2000)
|
|
266
|
+
storePoll.unref?.()
|
|
267
|
+
|
|
268
|
+
// 卸载/停用时的统一清理:停轮询 + 关 work-store 只读句柄 + 摘掉上墙描述符(卸载即下墙)。
|
|
269
|
+
cleanup = () => {
|
|
270
|
+
clearInterval(storePoll)
|
|
271
|
+
try { wsSource.close() } catch { /* ignore */ }
|
|
272
|
+
try { unregisterWorksSurface(toolHostService) } catch { /* ignore */ }
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function sendCode(req, res, buf) {
|
|
276
|
+
return sendCached(req, res, {
|
|
277
|
+
buf,
|
|
278
|
+
type: 'application/javascript; charset=utf-8',
|
|
279
|
+
cacheControl: 'private, no-cache',
|
|
280
|
+
extra: { 'x-offline-mirror': '1' },
|
|
281
|
+
})
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function sendText(req, res, text) {
|
|
285
|
+
return sendCached(req, res, {
|
|
286
|
+
buf: Buffer.from(String(text), 'utf8'),
|
|
287
|
+
type: 'text/plain; charset=utf-8',
|
|
288
|
+
cacheControl: 'private, no-cache',
|
|
289
|
+
extra: { 'x-offline-mirror': '1' },
|
|
290
|
+
})
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// 存储层错误 → HTTP:锁竞争 503(可重试)、输入不合法 400、磁盘等内部故障 500。
|
|
294
|
+
// 写失败绝不回 200——调用方必须知道数据没落盘。
|
|
295
|
+
function storeError(req, res, error) {
|
|
296
|
+
const status = error?.code === 'E_LOCK_BUSY' ? 503
|
|
297
|
+
: error?.code === 'E_NO_CODE' ? 400
|
|
298
|
+
: 500
|
|
299
|
+
return sendJson(req, res, status, { status: 'error', detail: error?.message || String(error) })
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// 本地作品库 HTTP 接口:列表/搜索/入库/详情/更新/删除/代码/版本/会话/导出/导入。
|
|
303
|
+
async function localApi(req, res, rest) {
|
|
304
|
+
const url = new URL(req.url || '/', 'http://local')
|
|
305
|
+
const q = url.searchParams.get('q') || ''
|
|
306
|
+
const version = url.searchParams.get('version') || ''
|
|
307
|
+
|
|
308
|
+
if (rest === '/api/local/export') {
|
|
309
|
+
if (req.method !== 'GET' && req.method !== 'HEAD') return sendJson(req, res, 405, { status: 'error', detail: 'method not allowed' })
|
|
310
|
+
return sendJson(req, res, 200, { status: 'ok', ...repo.exportAll() })
|
|
311
|
+
}
|
|
312
|
+
if (rest === '/api/local/import') {
|
|
313
|
+
if (req.method !== 'POST') return sendJson(req, res, 405, { status: 'error', detail: 'method not allowed' })
|
|
314
|
+
const body = await readJsonBody(req, 64 * 1024 * 1024)
|
|
315
|
+
if (!body) return sendJson(req, res, 400, { status: 'error', detail: 'body 不是合法 JSON' })
|
|
316
|
+
try {
|
|
317
|
+
return sendJson(req, res, 200, { status: 'ok', imported: repo.importAll(body) })
|
|
318
|
+
} catch (error) {
|
|
319
|
+
return storeError(req, res, error)
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
const m = /^\/api\/local\/works(?:\/(\d+)(\/code|\/versions|\/messages|\/session)?)?$/.exec(rest)
|
|
324
|
+
if (!m) return sendJson(req, res, 404, { status: 'error', detail: 'unknown local path' })
|
|
325
|
+
const id = m[1]
|
|
326
|
+
const sub = m[2] || ''
|
|
327
|
+
|
|
328
|
+
if (!id) {
|
|
329
|
+
if (req.method === 'GET' || req.method === 'HEAD') {
|
|
330
|
+
const items = q ? repo.search(q) : repo.list()
|
|
331
|
+
return sendJson(req, res, 200, { status: 'ok', items, total: items.length, offline: true })
|
|
332
|
+
}
|
|
333
|
+
if (req.method === 'POST') {
|
|
334
|
+
const body = await readJsonBody(req, 32 * 1024 * 1024)
|
|
335
|
+
if (!body) return sendJson(req, res, 400, { status: 'error', detail: 'body 不是合法 JSON' })
|
|
336
|
+
try {
|
|
337
|
+
return sendJson(req, res, 200, { status: 'ok', item: repo.add(body) })
|
|
338
|
+
} catch (error) {
|
|
339
|
+
return storeError(req, res, error)
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
return sendJson(req, res, 405, { status: 'error', detail: 'method not allowed' })
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
if (sub === '/code') {
|
|
346
|
+
const buf = repo.get(id) && repo.code(id, version)
|
|
347
|
+
if (!buf) return sendJson(req, res, 404, { status: 'error', detail: 'code not found' })
|
|
348
|
+
return sendCode(req, res, buf)
|
|
349
|
+
}
|
|
350
|
+
if (sub === '/versions') {
|
|
351
|
+
return sendJson(req, res, 200, { status: 'ok', versions: repo.versions(id) })
|
|
352
|
+
}
|
|
353
|
+
if (sub === '/session') {
|
|
354
|
+
const session = repo.readSession(id)
|
|
355
|
+
return session
|
|
356
|
+
? sendJson(req, res, 200, { status: 'ok', session })
|
|
357
|
+
: sendJson(req, res, 404, { status: 'error', detail: 'not a session work' })
|
|
358
|
+
}
|
|
359
|
+
if (sub === '/messages') {
|
|
360
|
+
if (req.method !== 'POST') return sendJson(req, res, 405, { status: 'error', detail: 'method not allowed' })
|
|
361
|
+
const body = await readJsonBody(req, 8 * 1024 * 1024)
|
|
362
|
+
if (!body) return sendJson(req, res, 400, { status: 'error', detail: 'body 不是合法 JSON' })
|
|
363
|
+
let item
|
|
364
|
+
try {
|
|
365
|
+
item = repo.addMessages(id, body.messages || [])
|
|
366
|
+
} catch (error) {
|
|
367
|
+
return storeError(req, res, error)
|
|
368
|
+
}
|
|
369
|
+
return item
|
|
370
|
+
? sendJson(req, res, 200, { status: 'ok', item })
|
|
371
|
+
: sendJson(req, res, 404, { status: 'error', detail: 'not found' })
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
const item = repo.get(id)
|
|
375
|
+
if (req.method === 'GET' || req.method === 'HEAD') {
|
|
376
|
+
return item
|
|
377
|
+
? sendJson(req, res, 200, { status: 'ok', item })
|
|
378
|
+
: sendJson(req, res, 404, { status: 'error', detail: 'not found' })
|
|
379
|
+
}
|
|
380
|
+
if (req.method === 'PUT' || req.method === 'PATCH') {
|
|
381
|
+
const body = await readJsonBody(req, 32 * 1024 * 1024)
|
|
382
|
+
if (!body) return sendJson(req, res, 400, { status: 'error', detail: 'body 不是合法 JSON' })
|
|
383
|
+
let updated
|
|
384
|
+
try {
|
|
385
|
+
updated = repo.update(id, body)
|
|
386
|
+
} catch (error) {
|
|
387
|
+
return storeError(req, res, error)
|
|
388
|
+
}
|
|
389
|
+
return updated
|
|
390
|
+
? sendJson(req, res, 200, { status: 'ok', item: updated })
|
|
391
|
+
: sendJson(req, res, 404, { status: 'error', detail: 'not found' })
|
|
392
|
+
}
|
|
393
|
+
if (req.method === 'DELETE') {
|
|
394
|
+
try {
|
|
395
|
+
return sendJson(req, res, 200, { status: 'ok', removed: repo.remove(id) })
|
|
396
|
+
} catch (error) {
|
|
397
|
+
return storeError(req, res, error)
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
return sendJson(req, res, 405, { status: 'error', detail: 'method not allowed' })
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
// 单文件前端:实时读盘 + 按前缀缓存改写结果,换掉 static/index.html 即生效。
|
|
404
|
+
let cache = { mtime: -1, prefix: null, body: null }
|
|
405
|
+
|
|
406
|
+
// 站点是给「挂在根上的 port-8223」写的:bootstrap/gate/列表都是根绝对路径
|
|
407
|
+
// (`/api/...`、`/config.json`、`/media/...`),挂在子前缀下会 404。这里在
|
|
408
|
+
// 所有站点脚本之前插一段 shim,把根绝对路径补上本次请求的公共前缀。
|
|
409
|
+
function prefixShim(prefix) {
|
|
410
|
+
// 父页面(DSH 浏览器半)跨源也能让站点打开某个作品:postMessage 桥。
|
|
411
|
+
const bridge =
|
|
412
|
+
'window.addEventListener("message",function(ev){var d=ev.data;' +
|
|
413
|
+
'if(!d||d.type!=="offline-ddr0-open")return;' +
|
|
414
|
+
'try{if(window.ItemViewOrchestrator&&typeof window.ItemViewOrchestrator.resolveAndOpen==="function")' +
|
|
415
|
+
'window.ItemViewOrchestrator.resolveAndOpen(d.itemType||"works",d.id)}catch(e){}});'
|
|
416
|
+
if (!prefix) return `<script>(function(){${bridge}})()</script>`
|
|
417
|
+
const literal = JSON.stringify(prefix)
|
|
418
|
+
return (
|
|
419
|
+
'<script>(function(){' +
|
|
420
|
+
`var P=${literal};` +
|
|
421
|
+
'if(!P)return;' +
|
|
422
|
+
"function fx(u){if(typeof u!=='string'||!u)return u;" +
|
|
423
|
+
"if(u.charAt(0)!=='/')return u;" +
|
|
424
|
+
"if(u.indexOf('//')===0)return u;" +
|
|
425
|
+
"if(u===P||u.indexOf(P+'/')===0)return u;" +
|
|
426
|
+
"return /^\\/(api(\\/|$|\\?)|config\\.json|media\\/)/.test(u)?P+u:u}" +
|
|
427
|
+
'var f=window.fetch;if(f)window.fetch=function(i,o){try{' +
|
|
428
|
+
"if(typeof i==='string')i=fx(i);" +
|
|
429
|
+
'else if(i&&typeof i.url==="string"){var u=fx(i.url);if(u!==i.url)i=new Request(u,i)}' +
|
|
430
|
+
'}catch(e){}return f.call(this,i,o)};' +
|
|
431
|
+
'var o=XMLHttpRequest.prototype.open;' +
|
|
432
|
+
'XMLHttpRequest.prototype.open=function(m,u){try{' +
|
|
433
|
+
"arguments[1]=fx(String(u))}catch(e){}return o.apply(this,arguments)};" +
|
|
434
|
+
bridge +
|
|
435
|
+
'})()</script>'
|
|
436
|
+
)
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
function renderIndex(prefix) {
|
|
440
|
+
const stat = fs.statSync(indexFile)
|
|
441
|
+
if (cache.mtime !== stat.mtimeMs || cache.prefix !== prefix) {
|
|
442
|
+
const raw = fs.readFileSync(indexFile, 'utf8')
|
|
443
|
+
let html = raw.includes(SUB_MARKER)
|
|
444
|
+
? raw.split(SUB_MARKER).join(`var SUB = ${JSON.stringify(prefix)};`)
|
|
445
|
+
: raw
|
|
446
|
+
const shim = prefixShim(prefix) + syncButtonShim(prefix)
|
|
447
|
+
if (shim) {
|
|
448
|
+
const head = /<head[^>]*>/i.exec(html)
|
|
449
|
+
html = head
|
|
450
|
+
? html.slice(0, head.index + head[0].length) + shim + html.slice(head.index + head[0].length)
|
|
451
|
+
: shim + html
|
|
452
|
+
}
|
|
453
|
+
cache = { mtime: stat.mtimeMs, prefix, body: Buffer.from(html, 'utf8') }
|
|
454
|
+
}
|
|
455
|
+
return cache.body
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
function serveIndex(req, res, prefix) {
|
|
459
|
+
let data
|
|
460
|
+
try {
|
|
461
|
+
data = renderIndex(prefix)
|
|
462
|
+
} catch (error) {
|
|
463
|
+
log?.error?.(`offline-ddr0: 读取 index.html 失败: ${error.message}`)
|
|
464
|
+
res.writeHead(500, { 'content-type': 'text/plain; charset=utf-8' })
|
|
465
|
+
res.end('offline-ddr0: static/index.html missing')
|
|
466
|
+
return true
|
|
467
|
+
}
|
|
468
|
+
return sendCached(req, res, {
|
|
469
|
+
buf: data,
|
|
470
|
+
type: 'text/html; charset=utf-8',
|
|
471
|
+
cacheControl: 'no-cache',
|
|
472
|
+
extra: { 'x-content-type-options': 'nosniff', 'referrer-policy': 'no-referrer' },
|
|
473
|
+
})
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
function jsonNotFound(req, res, pathname) {
|
|
477
|
+
const payload = Buffer.from(
|
|
478
|
+
JSON.stringify({ ok: false, error: 'not found', path: pathname }),
|
|
479
|
+
'utf8',
|
|
480
|
+
)
|
|
481
|
+
res.writeHead(404, {
|
|
482
|
+
'content-type': 'application/json; charset=utf-8',
|
|
483
|
+
'content-length': payload.length,
|
|
484
|
+
})
|
|
485
|
+
res.end(req.method === 'HEAD' ? undefined : payload)
|
|
486
|
+
return true
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
function sendJson(req, res, status, value) {
|
|
490
|
+
const payload = Buffer.from(JSON.stringify(value), 'utf8')
|
|
491
|
+
// GET/HEAD 的成功响应走 ETag 协商:列表/详情未变时 304,切分区不重传几百 KB。
|
|
492
|
+
if (status === 200 && (req.method === 'GET' || req.method === 'HEAD')) {
|
|
493
|
+
return sendCached(req, res, { buf: payload, type: 'application/json; charset=utf-8', cacheControl: 'no-cache' })
|
|
494
|
+
}
|
|
495
|
+
res.writeHead(status, {
|
|
496
|
+
'content-type': 'application/json; charset=utf-8',
|
|
497
|
+
'content-length': payload.length,
|
|
498
|
+
'cache-control': 'no-store',
|
|
499
|
+
})
|
|
500
|
+
res.end(req.method === 'HEAD' ? undefined : payload)
|
|
501
|
+
return true
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
function readJsonBody(req, maxBytes = 64 * 1024) {
|
|
505
|
+
return new Promise((resolve) => {
|
|
506
|
+
const chunks = []
|
|
507
|
+
let size = 0
|
|
508
|
+
req.on('data', (chunk) => {
|
|
509
|
+
size += chunk.length
|
|
510
|
+
if (size > maxBytes) {
|
|
511
|
+
resolve(null)
|
|
512
|
+
req.destroy()
|
|
513
|
+
return
|
|
514
|
+
}
|
|
515
|
+
chunks.push(chunk)
|
|
516
|
+
})
|
|
517
|
+
req.on('end', () => {
|
|
518
|
+
try {
|
|
519
|
+
resolve(JSON.parse(Buffer.concat(chunks).toString('utf8') || '{}'))
|
|
520
|
+
} catch {
|
|
521
|
+
resolve(null)
|
|
522
|
+
}
|
|
523
|
+
})
|
|
524
|
+
req.on('error', () => resolve(null))
|
|
525
|
+
})
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
// 离线站新版的「外部主题」契约:前端开着 followExt 时每 10s 拉一次
|
|
529
|
+
// /api/theme/sync,只认 light/dark 两值;控制方(DSH 浏览器半)用 POST
|
|
530
|
+
// 把当前明暗推上来。后端镜像没有这个端点,所以由宿主半接管、不反代。
|
|
531
|
+
let externalThemeMode = null
|
|
532
|
+
|
|
533
|
+
async function themeSync(req, res) {
|
|
534
|
+
if (req.method === 'GET' || req.method === 'HEAD') {
|
|
535
|
+
return sendJson(req, res, 200, { mode: externalThemeMode })
|
|
536
|
+
}
|
|
537
|
+
if (req.method === 'POST' || req.method === 'PUT') {
|
|
538
|
+
const body = await readJsonBody(req)
|
|
539
|
+
const mode = body && body.mode
|
|
540
|
+
if (mode === 'light' || mode === 'dark') externalThemeMode = mode
|
|
541
|
+
return sendJson(req, res, 200, { ok: true, mode: externalThemeMode })
|
|
542
|
+
}
|
|
543
|
+
return sendJson(req, res, 405, { ok: false, error: 'method not allowed' })
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
// 套件统一呈现面(与 capyclaw/conductor 工作台一致):GET /embed.js + GET /api/surface
|
|
547
|
+
// 嵌入握手。共享宿主只剥 foundation 公共前缀、不剥插件 mount(/offline-ddr0),
|
|
548
|
+
// attachSurface 的精确 app.get(path) 在共享宿主下匹配不到;attachPrefixTolerantSurface
|
|
549
|
+
// 按 splitMount 的 rest 匹配,剥不剥 mount 都正确。先于下面的大 app.use 注册,精确短路。
|
|
550
|
+
attachPrefixTolerantSurface(app, {
|
|
551
|
+
suite: 'capyclaw-works-suite',
|
|
552
|
+
embedPath: resolveConfig(config).sitePath,
|
|
553
|
+
restOf: (req, u) => splitMount(req, u).rest,
|
|
554
|
+
})
|
|
555
|
+
|
|
556
|
+
// 前缀已由 service-host 剥掉,这里的 pathname 就是挂载点内的路径。
|
|
557
|
+
app.use((req, res, url) => {
|
|
558
|
+
res.setHeader('Access-Control-Allow-Origin', '*')
|
|
559
|
+
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS')
|
|
560
|
+
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Session-Token')
|
|
561
|
+
if (req.method === 'OPTIONS') {
|
|
562
|
+
res.writeHead(200)
|
|
563
|
+
res.end()
|
|
564
|
+
return true
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
const { rest, prefix } = splitMount(req, url)
|
|
568
|
+
if (rest === '/' || rest === '/index.html') return serveIndex(req, res, prefix)
|
|
569
|
+
if (rest === '/api/theme/sync') return themeSync(req, res)
|
|
570
|
+
|
|
571
|
+
// 本地作品库:先于镜像/反代,保证本地内容永远可用(不依赖上游)。
|
|
572
|
+
if (rest === '/api/local/endpoint') return sendJson(req, res, 200, endpointInfo(prefix))
|
|
573
|
+
if (rest === '/api/local/sync') return syncApi(req, res)
|
|
574
|
+
if (rest.startsWith('/api/local/')) {
|
|
575
|
+
return localApi(req, res, rest)
|
|
576
|
+
}
|
|
577
|
+
const localDetail = /^\/api\/works\/(\d+)$/.exec(rest)
|
|
578
|
+
if (localDetail) {
|
|
579
|
+
const item = repo.get(localDetail[1]) || wsSource.getItem(localDetail[1])
|
|
580
|
+
if (item) return sendJson(req, res, 200, { status: 'ok', item })
|
|
581
|
+
}
|
|
582
|
+
const localBundle = /^\/api\/works\/works\/(\d+)\/version\/\d+\/js$/.exec(rest)
|
|
583
|
+
if (localBundle) {
|
|
584
|
+
if (repo.get(localBundle[1])) return sendCode(req, res, repo.code(localBundle[1]))
|
|
585
|
+
if (wsSource.has(localBundle[1])) return sendCode(req, res, wsSource.getCode(localBundle[1]))
|
|
586
|
+
}
|
|
587
|
+
// work-store 作品的 PPT 放映面:装饰器代码 / viewSource(幻灯片数据)/ .works 包。
|
|
588
|
+
const wsDecorator = /^\/api\/works\/works\/(\d+)\/version\/\d+\/decorator\.js$/.exec(rest)
|
|
589
|
+
if (wsDecorator && wsSource.has(wsDecorator[1])) {
|
|
590
|
+
const code = wsSource.getDecoratorCode(wsDecorator[1])
|
|
591
|
+
return sendCode(req, res, code || Buffer.from('// 装饰器代码缺失(mirror 未打包)\n', 'utf8'))
|
|
592
|
+
}
|
|
593
|
+
const wsViewSource = /^\/api\/works\/works\/(\d+)\/description-md$/.exec(rest)
|
|
594
|
+
if (wsViewSource && wsSource.has(wsViewSource[1])) {
|
|
595
|
+
return sendJson(req, res, 200, { view_source: wsSource.getViewSource(wsViewSource[1]), description_md: '' })
|
|
596
|
+
}
|
|
597
|
+
const wsBundle = /^\/api\/works\/(\d+)\/bundle$/.exec(rest)
|
|
598
|
+
if (wsBundle && req.method === 'GET' && wsSource.has(wsBundle[1])) {
|
|
599
|
+
return sendText(req, res, wsSource.getBundle(wsBundle[1]) || '')
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
// 本地媒体直出(物化进资产仓的图/视频/音频,带 Range 分片):先于 mirror,
|
|
603
|
+
// 命中即本地字节,外部源失效不影响;仓里没有则交回 mirror/反代。
|
|
604
|
+
if (mediaHandler(req, res, rest)) return true
|
|
605
|
+
|
|
606
|
+
// 离线镜像优先:命中即直出,未命中给空载荷;只有镜像目录不存在时
|
|
607
|
+
// (mirror.serve 返回 false)才落到反代。
|
|
608
|
+
const fromMirror = () =>
|
|
609
|
+
mirror.serve(req, res, { pathname: rest, search: url?.search || '', mount: prefix })
|
|
610
|
+
|
|
611
|
+
if (rest === '/config.json' || rest === '/api' || rest.startsWith('/api/') || rest.startsWith('/media/')) {
|
|
612
|
+
if (fromMirror()) return true
|
|
613
|
+
return proxy(req, res, url, { mount: prefix, pathname: rest })
|
|
614
|
+
}
|
|
615
|
+
// 离线站不做执行器/LLM,别再让这些路径掉进 SPA 兜底拿到 200 HTML。
|
|
616
|
+
if (rest === '/v1' || rest.startsWith('/v1/')) return jsonNotFound(req, res, rest)
|
|
617
|
+
// 带扩展名的静态资源(favicon 等):镜像里有就直出,没有再问上游。
|
|
618
|
+
if (path.posix.extname(rest)) {
|
|
619
|
+
if (fromMirror()) return true
|
|
620
|
+
return proxy(req, res, url, { mount: prefix, pathname: rest })
|
|
621
|
+
}
|
|
622
|
+
return serveIndex(req, res, prefix)
|
|
623
|
+
})
|
|
624
|
+
},
|
|
625
|
+
}),
|
|
626
|
+
// 卸载/停用回收 routes() 内部资源(轮询 + SQLite 句柄)。
|
|
627
|
+
// lifecycle-hook 的 start 由 loader 保证执行,onStop 随之登记生效。
|
|
628
|
+
lifecycleHook({
|
|
629
|
+
onStop() {
|
|
630
|
+
const fn = cleanup
|
|
631
|
+
cleanup = null
|
|
632
|
+
try { fn?.() } catch { /* ignore */ }
|
|
633
|
+
},
|
|
634
|
+
}),
|
|
635
|
+
],
|
|
636
|
+
}, import.meta.url)
|