@hjd-hitsz/claudex-web 0.0.0-stage → 4.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +27 -0
- package/CHANGELOG.md +449 -0
- package/LICENSE +674 -0
- package/README.md +128 -2
- package/bin/claudex-web.mjs +129 -0
- package/build-info.json +8 -0
- package/client/dist/_baseMerge.js +1 -0
- package/client/dist/abnfDiagram-WEYOIA3C.js +1 -0
- package/client/dist/apl.js +1 -0
- package/client/dist/app.js +393 -0
- package/client/dist/apple-touch-icon.png +0 -0
- package/client/dist/arc.js +1 -0
- package/client/dist/architectureDiagram-ZHVYKT53.js +36 -0
- package/client/dist/asciiarmor.js +1 -0
- package/client/dist/asn1.js +1 -0
- package/client/dist/asterisk.js +1 -0
- package/client/dist/blockDiagram-RJAE7QA3.js +140 -0
- package/client/dist/brainfuck.js +1 -0
- package/client/dist/c4Diagram-XDPPPE54.js +38 -0
- package/client/dist/channel.js +1 -0
- package/client/dist/chunk-2E7WHVMD.js +88 -0
- package/client/dist/chunk-2Q5K7J3B.js +1 -0
- package/client/dist/chunk-4EMNKR2T.js +1 -0
- package/client/dist/chunk-55XCU76K.js +1 -0
- package/client/dist/chunk-5VM5RSS4.js +15 -0
- package/client/dist/chunk-JWPE2WC7.js +1 -0
- package/client/dist/chunk-VQ5W62QI.js +1 -0
- package/client/dist/chunk-XXDRQBXY.js +1 -0
- package/client/dist/chunk-ZCHMQSOR.js +2 -0
- package/client/dist/classDiagram-v2-4MYWR3UA.js +217 -0
- package/client/dist/clike.js +1 -0
- package/client/dist/clojure.js +1 -0
- package/client/dist/cmake.js +1 -0
- package/client/dist/cobol.js +1 -0
- package/client/dist/code-highlight-style.js +13 -0
- package/client/dist/coffeescript.js +1 -0
- package/client/dist/commonlisp.js +1 -0
- package/client/dist/cose-bilkent-JH36ORCC.js +1 -0
- package/client/dist/crystal.js +1 -0
- package/client/dist/css.js +1 -0
- package/client/dist/cynefin-EF2NZ3EQ.js +141 -0
- package/client/dist/cynefinDiagram-WF24UDRP.js +62 -0
- package/client/dist/cypher.js +1 -0
- package/client/dist/cytoscape.esm.js +321 -0
- package/client/dist/d.js +1 -0
- package/client/dist/dagre-NK4GIZVX.js +4 -0
- package/client/dist/defaultLocale.js +1 -0
- package/client/dist/diagram-CBK3MWTC.js +24 -0
- package/client/dist/diagram-I5XIIV2D.js +24 -0
- package/client/dist/diagram-ODIL2UCW.js +30 -0
- package/client/dist/diagram-PFBEJUXE.js +3 -0
- package/client/dist/diagram-RJA3I4VV.js +41 -0
- package/client/dist/diagram-TYUNFSNR.js +200 -0
- package/client/dist/diff.js +1 -0
- package/client/dist/dockerfile.js +1 -0
- package/client/dist/document-editor.js +1040 -0
- package/client/dist/dtd.js +1 -0
- package/client/dist/dylan.js +1 -0
- package/client/dist/ebnf.js +1 -0
- package/client/dist/ebnfDiagram-V4P3TTPT.js +1 -0
- package/client/dist/ecl.js +1 -0
- package/client/dist/eiffel.js +1 -0
- package/client/dist/elk-QONTBCTQ.js +27 -0
- package/client/dist/elm.js +1 -0
- package/client/dist/erDiagram-KYQIGQAA.js +99 -0
- package/client/dist/erlang.js +1 -0
- package/client/dist/factor.js +1 -0
- package/client/dist/favicon.ico +0 -0
- package/client/dist/favicon.png +0 -0
- package/client/dist/fcl.js +1 -0
- package/client/dist/file-panel.js +1 -0
- package/client/dist/flowDiagram-IUGLLWBU.js +213 -0
- package/client/dist/forth.js +1 -0
- package/client/dist/fortran.js +1 -0
- package/client/dist/ganttDiagram-R7GPNTJY.js +292 -0
- package/client/dist/gas.js +1 -0
- package/client/dist/gherkin.js +1 -0
- package/client/dist/gitGraphDiagram-PDYL7XHY.js +106 -0
- package/client/dist/graph.js +1 -0
- package/client/dist/groovy.js +1 -0
- package/client/dist/haskell.js +1 -0
- package/client/dist/haxe.js +1 -0
- package/client/dist/http.js +1 -0
- package/client/dist/idl.js +1 -0
- package/client/dist/index.html +25 -0
- package/client/dist/index.js +2 -0
- package/client/dist/index10.js +1 -0
- package/client/dist/index11.js +1 -0
- package/client/dist/index12.js +1 -0
- package/client/dist/index13.js +7 -0
- package/client/dist/index14.js +1 -0
- package/client/dist/index15.js +1 -0
- package/client/dist/index16.js +6 -0
- package/client/dist/index17.js +1 -0
- package/client/dist/index18.js +3 -0
- package/client/dist/index19.js +1 -0
- package/client/dist/index2.js +1 -0
- package/client/dist/index20.js +1 -0
- package/client/dist/index21.js +1 -0
- package/client/dist/index22.js +1 -0
- package/client/dist/index3.js +1 -0
- package/client/dist/index4.js +1 -0
- package/client/dist/index5.js +1 -0
- package/client/dist/index6.js +13 -0
- package/client/dist/index7.js +1 -0
- package/client/dist/index8.js +1 -0
- package/client/dist/index9.js +1 -0
- package/client/dist/infoDiagram-MVDRKAX5.js +2 -0
- package/client/dist/init.js +1 -0
- package/client/dist/isArrayLikeObject.js +1 -0
- package/client/dist/ishikawaDiagram-HPEHV2GE.js +70 -0
- package/client/dist/javascript.js +1 -0
- package/client/dist/journeyDiagram-BOSHW7O7.js +139 -0
- package/client/dist/julia.js +1 -0
- package/client/dist/kanban-definition-FNSA4HC5.js +89 -0
- package/client/dist/katex.js +257 -0
- package/client/dist/linear.js +1 -0
- package/client/dist/livescript.js +1 -0
- package/client/dist/lua.js +1 -0
- package/client/dist/mathematica.js +1 -0
- package/client/dist/mbox.js +1 -0
- package/client/dist/mermaid.core.js +310 -0
- package/client/dist/mindmap-definition-6OY57UG3.js +98 -0
- package/client/dist/mirc.js +1 -0
- package/client/dist/mllike.js +1 -0
- package/client/dist/modelica.js +1 -0
- package/client/dist/mscgen.js +1 -0
- package/client/dist/mumps.js +1 -0
- package/client/dist/nginx.js +1 -0
- package/client/dist/notification-worker.js +45 -0
- package/client/dist/nsis.js +1 -0
- package/client/dist/ntriples.js +1 -0
- package/client/dist/octave.js +1 -0
- package/client/dist/ordinal.js +1 -0
- package/client/dist/oz.js +1 -0
- package/client/dist/pascal.js +1 -0
- package/client/dist/pegDiagram-ECIUJNBX.js +1 -0
- package/client/dist/perl.js +1 -0
- package/client/dist/pieDiagram-GNMPY74T.js +39 -0
- package/client/dist/pig.js +1 -0
- package/client/dist/powershell.js +1 -0
- package/client/dist/properties.js +1 -0
- package/client/dist/protobuf.js +1 -0
- package/client/dist/pug.js +1 -0
- package/client/dist/puppet.js +1 -0
- package/client/dist/purify.es.js +3 -0
- package/client/dist/python.js +1 -0
- package/client/dist/q.js +1 -0
- package/client/dist/quadrantDiagram-D57K4MC3.js +7 -0
- package/client/dist/r.js +1 -0
- package/client/dist/railroadDiagram-Y72NRBL7.js +1 -0
- package/client/dist/requirementDiagram-EQYSW5RA.js +84 -0
- package/client/dist/rpm.js +1 -0
- package/client/dist/ruby.js +1 -0
- package/client/dist/sankeyDiagram-SY24JHO5.js +40 -0
- package/client/dist/sas.js +1 -0
- package/client/dist/scheme.js +1 -0
- package/client/dist/sequenceDiagram-BLEHNHQU.js +169 -0
- package/client/dist/shell.js +1 -0
- package/client/dist/sieve.js +1 -0
- package/client/dist/simple-mode.js +1 -0
- package/client/dist/sizeCapture-INFHLROL.js +1 -0
- package/client/dist/smalltalk.js +1 -0
- package/client/dist/solr.js +1 -0
- package/client/dist/sparql.js +1 -0
- package/client/dist/spreadsheet.js +1 -0
- package/client/dist/sql.js +1 -0
- package/client/dist/stateDiagram-v2-A6IESKWZ.js +291 -0
- package/client/dist/stex.js +1 -0
- package/client/dist/style.css +1 -0
- package/client/dist/stylus.js +1 -0
- package/client/dist/swift.js +1 -0
- package/client/dist/swimlanes-B3ENRKFQ.js +1 -0
- package/client/dist/swimlanesDiagram-AYQRV4AK.js +8 -0
- package/client/dist/tcl.js +1 -0
- package/client/dist/terminal-panel.js +18 -0
- package/client/dist/textile.js +1 -0
- package/client/dist/tiddlywiki.js +1 -0
- package/client/dist/tiki.js +1 -0
- package/client/dist/timeline-definition-JXYKGLGB.js +120 -0
- package/client/dist/toml.js +1 -0
- package/client/dist/troff.js +1 -0
- package/client/dist/ttcn-cfg.js +1 -0
- package/client/dist/ttcn.js +1 -0
- package/client/dist/turtle.js +1 -0
- package/client/dist/usecaseDiagram-WPOLBNNX.js +370 -0
- package/client/dist/vb.js +1 -0
- package/client/dist/vbscript.js +1 -0
- package/client/dist/velocity.js +1 -0
- package/client/dist/vennDiagram-Q3Y3IDCW.js +34 -0
- package/client/dist/verilog.js +1 -0
- package/client/dist/vhdl.js +1 -0
- package/client/dist/wardleyDiagram-IVAIDKWA.js +78 -0
- package/client/dist/webidl.js +1 -0
- package/client/dist/xquery.js +1 -0
- package/client/dist/xychartDiagram-4URNHDU4.js +7 -0
- package/client/dist/yacas.js +1 -0
- package/client/dist/z80.js +1 -0
- package/docs/architecture/backend-regressions.md +53 -0
- package/docs/architecture/backends.md +113 -0
- package/docs/architecture/behavior.md +206 -0
- package/docs/architecture.md +306 -0
- package/docs/background-tasks.md +39 -0
- package/docs/cli-command-coverage.md +60 -0
- package/docs/deployment.md +269 -0
- package/docs/development.md +385 -0
- package/docs/images/desktop-dark.png +0 -0
- package/docs/images/desktop-light.png +0 -0
- package/docs/images/mobile-sidebar.png +0 -0
- package/docs/images/mobile.png +0 -0
- package/docs/releases/v2.0.0.md +5 -0
- package/docs/releases/v2.1.0.md +6 -0
- package/docs/releases/v2.1.1.md +6 -0
- package/docs/releases/v2.1.2.md +8 -0
- package/docs/releases/v2.2.0.md +11 -0
- package/docs/releases/v2.3.0.md +12 -0
- package/docs/releases/v2.3.1.md +8 -0
- package/docs/releases/v2.4.0.md +8 -0
- package/docs/releases/v2.4.1.md +8 -0
- package/docs/releases/v2.4.2.md +8 -0
- package/docs/releases/v3.0.0.md +9 -0
- package/docs/releases/v3.1.0.md +10 -0
- package/docs/releases/v3.1.1.md +7 -0
- package/docs/releases/v4.0.0-beta.1.md +10 -0
- package/docs/releases/v4.0.0.md +20 -0
- package/docs/releases/v4.0.1.md +10 -0
- package/docs/versioning.md +119 -0
- package/package.json +70 -4
- package/scripts/install-services.mjs +134 -0
- package/scripts/restart-when-idle.mjs +125 -0
- package/scripts/start-backend.mjs +29 -0
- package/server/src/agent-messages.mjs +124 -0
- package/server/src/application.mjs +133 -0
- package/server/src/auth.mjs +328 -0
- package/server/src/backends/claude/account.mjs +171 -0
- package/server/src/backends/claude/adapter.mjs +366 -0
- package/server/src/backends/claude/agent-history.mjs +70 -0
- package/server/src/backends/claude/background-tasks.mjs +168 -0
- package/server/src/backends/claude/catalogue.mjs +266 -0
- package/server/src/backends/claude/execution-errors.mjs +44 -0
- package/server/src/backends/claude/execution-session.mjs +390 -0
- package/server/src/backends/claude/git-tool-changes.mjs +77 -0
- package/server/src/backends/claude/goals.mjs +319 -0
- package/server/src/backends/claude/history-actions.mjs +170 -0
- package/server/src/backends/claude/history.mjs +65 -0
- package/server/src/backends/claude/hooks.mjs +232 -0
- package/server/src/backends/claude/input-stream.mjs +24 -0
- package/server/src/backends/claude/messages-request.mjs +34 -0
- package/server/src/backends/claude/messages.mjs +300 -0
- package/server/src/backends/claude/model-options.mjs +18 -0
- package/server/src/backends/claude/native-commands.mjs +48 -0
- package/server/src/backends/claude/native-records.mjs +76 -0
- package/server/src/backends/claude/process-ownership.mjs +82 -0
- package/server/src/backends/claude/project-instructions.mjs +40 -0
- package/server/src/backends/claude/provider-balance.mjs +40 -0
- package/server/src/backends/claude/provider-environment.mjs +93 -0
- package/server/src/backends/claude/provider-transport.mjs +70 -0
- package/server/src/backends/claude/recovery.mjs +54 -0
- package/server/src/backends/claude/runtime.mjs +463 -0
- package/server/src/backends/claude/session-worker.mjs +32 -0
- package/server/src/backends/claude/sessions.mjs +347 -0
- package/server/src/backends/claude/token-usage.mjs +98 -0
- package/server/src/backends/claude/worker-client.mjs +344 -0
- package/server/src/backends/claude/worker-launcher.mjs +119 -0
- package/server/src/backends/claude/worker-location.mjs +31 -0
- package/server/src/backends/claude/worker-revision.mjs +45 -0
- package/server/src/backends/claude/worker-server.mjs +290 -0
- package/server/src/backends/cli-installation.mjs +140 -0
- package/server/src/backends/codex/adapter.mjs +153 -0
- package/server/src/backends/codex/compaction-recovery.mjs +78 -0
- package/server/src/backends/codex/events.mjs +125 -0
- package/server/src/backends/codex/methods.mjs +17 -0
- package/server/src/backends/codex/reference-mcp.mjs +97 -0
- package/server/src/backends/codex/reference-tools.mjs +71 -0
- package/server/src/backends/codex/requests.mjs +170 -0
- package/server/src/backends/codex/subscriptions.mjs +91 -0
- package/server/src/backends/codex/upstream.mjs +148 -0
- package/server/src/backends/configuration.mjs +122 -0
- package/server/src/backends/identity-mapping.mjs +87 -0
- package/server/src/backends/manager.mjs +412 -0
- package/server/src/backends/network-proxy.mjs +55 -0
- package/server/src/backends/provider-presets.mjs +45 -0
- package/server/src/backends/providers.mjs +132 -0
- package/server/src/backends/storage.mjs +35 -0
- package/server/src/bridge/conversation-policy.mjs +29 -0
- package/server/src/bridge/file-operations.mjs +47 -0
- package/server/src/bridge/product-methods.mjs +66 -0
- package/server/src/compaction.mjs +82 -0
- package/server/src/config.mjs +57 -0
- package/server/src/file-content.mjs +99 -0
- package/server/src/file-undo.mjs +142 -0
- package/server/src/http/router.mjs +80 -0
- package/server/src/http/static.mjs +62 -0
- package/server/src/product-config.mjs +80 -0
- package/server/src/project-models.mjs +59 -0
- package/server/src/rpc-transport.mjs +21 -0
- package/server/src/server.mjs +20 -0
- package/server/src/summary-translation.mjs +82 -0
- package/server/src/system-auth.mjs +45 -0
- package/server/src/thread-folders.mjs +76 -0
- package/server/src/thread-names.mjs +264 -0
- package/server/src/thread-projects.mjs +36 -0
- package/server/src/tls.mjs +87 -0
- package/server/src/version.mjs +55 -0
- package/server/src/web-config-migration.mjs +48 -0
- package/server/src/workspace/commands.mjs +33 -0
- package/server/src/workspace/files.mjs +44 -0
- package/server/src/workspace/service.mjs +27 -0
- package/server/src/workspace/terminals.mjs +122 -0
- package/shared/backend-activity.mjs +13 -0
- package/shared/backend-capabilities.mjs +33 -0
- package/shared/backend-identity.mjs +17 -0
- package/shared/compaction-state.mjs +49 -0
- package/shared/file-types.mjs +82 -0
- package/shared/product-contract.mjs +26 -0
- package/shared/rpc-limits.mjs +2 -0
- package/shared/terminal-workspace.mjs +26 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
const writes = new Set([
|
|
2
|
+
"thread/revert",
|
|
3
|
+
"thread/rollback",
|
|
4
|
+
"turn/start",
|
|
5
|
+
"turn/steer",
|
|
6
|
+
"thread/compact/start",
|
|
7
|
+
"review/start",
|
|
8
|
+
]);
|
|
9
|
+
const fileWrites = new Set([
|
|
10
|
+
...writes,
|
|
11
|
+
"thread/goal/get",
|
|
12
|
+
"thread/goal/set",
|
|
13
|
+
"thread/goal/clear",
|
|
14
|
+
"thread/backgroundTerminals/list",
|
|
15
|
+
"thread/backgroundTerminals/clean",
|
|
16
|
+
]);
|
|
17
|
+
|
|
18
|
+
// 只读取执行方提供的即时占用;原生订阅、队列与变更锁继续由执行方持有。
|
|
19
|
+
export function assertConversationOperation(
|
|
20
|
+
method,
|
|
21
|
+
{ compacting, undoing, active, pending },
|
|
22
|
+
) {
|
|
23
|
+
if (writes.has(method) && compacting)
|
|
24
|
+
throw new Error("正在整理上下文,完成后可继续发送。");
|
|
25
|
+
if (method === "thread/compact/start" && (active || pending))
|
|
26
|
+
throw new Error("请等待当前任务结束后整理上下文。");
|
|
27
|
+
if (fileWrites.has(method) && undoing)
|
|
28
|
+
throw new Error("正在撤销文件修改,完成后可继续发送。");
|
|
29
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { changesForPath, undoFileChanges } from "../file-undo.mjs";
|
|
2
|
+
|
|
3
|
+
export function createFileOperations({
|
|
4
|
+
call,
|
|
5
|
+
activeTurns,
|
|
6
|
+
activeThreads,
|
|
7
|
+
broadcast,
|
|
8
|
+
}) {
|
|
9
|
+
const undoing = new Set();
|
|
10
|
+
const undoneFiles = new Map();
|
|
11
|
+
|
|
12
|
+
async function undo({ threadId, turnId, path }) {
|
|
13
|
+
const key = JSON.stringify([threadId, turnId, path]);
|
|
14
|
+
if (undoneFiles.has(key)) {
|
|
15
|
+
const result = undoneFiles.get(key);
|
|
16
|
+
broadcast({ method: "lan/files/undone", params: result });
|
|
17
|
+
return result;
|
|
18
|
+
}
|
|
19
|
+
const isActive = () =>
|
|
20
|
+
activeTurns.has(threadId) || activeThreads.has(threadId);
|
|
21
|
+
if (isActive()) throw new Error("任务正在运行,请在结束后撤销修改。");
|
|
22
|
+
if (undoing.has(threadId)) throw new Error("正在撤销文件修改,请稍候。");
|
|
23
|
+
undoing.add(threadId);
|
|
24
|
+
try {
|
|
25
|
+
const { thread } = await call("thread/read", {
|
|
26
|
+
threadId,
|
|
27
|
+
includeTurns: true,
|
|
28
|
+
});
|
|
29
|
+
const turn = thread.turns.find((turn) => turn.id === turnId);
|
|
30
|
+
if (!turn || turn.status === "inProgress")
|
|
31
|
+
throw new Error("请重新加载此轮记录后再撤销。");
|
|
32
|
+
await undoFileChanges({
|
|
33
|
+
cwd: thread.cwd,
|
|
34
|
+
changes: changesForPath(turn.items, thread.cwd, path),
|
|
35
|
+
isActive,
|
|
36
|
+
});
|
|
37
|
+
const result = { threadId, turnId, path };
|
|
38
|
+
undoneFiles.set(key, result);
|
|
39
|
+
broadcast({ method: "lan/files/undone", params: result });
|
|
40
|
+
return result;
|
|
41
|
+
} finally {
|
|
42
|
+
undoing.delete(threadId);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return { undo, undoing, undoneFiles };
|
|
47
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { createProjectModels } from "../project-models.mjs";
|
|
2
|
+
import { createFileOperations } from "./file-operations.mjs";
|
|
3
|
+
import { prepareProjectlessDirectory } from "../thread-projects.mjs";
|
|
4
|
+
|
|
5
|
+
// 网页操作在所属后端的执行边界内组合;运行状态与原生历史由调用方提供。
|
|
6
|
+
export function createProductMethods({
|
|
7
|
+
config,
|
|
8
|
+
projectModelsDirectory,
|
|
9
|
+
legacyProjectModelsDirectory,
|
|
10
|
+
call,
|
|
11
|
+
activeTurns,
|
|
12
|
+
activeThreads,
|
|
13
|
+
checkCompaction,
|
|
14
|
+
readAgentMessages,
|
|
15
|
+
broadcast,
|
|
16
|
+
}) {
|
|
17
|
+
const projectModels = createProjectModels(
|
|
18
|
+
projectModelsDirectory,
|
|
19
|
+
legacyProjectModelsDirectory,
|
|
20
|
+
);
|
|
21
|
+
const files = createFileOperations({
|
|
22
|
+
call,
|
|
23
|
+
activeTurns,
|
|
24
|
+
activeThreads,
|
|
25
|
+
broadcast,
|
|
26
|
+
});
|
|
27
|
+
const offline = new Map([
|
|
28
|
+
[
|
|
29
|
+
"lan/projectModels/read",
|
|
30
|
+
async () => ({ projects: await projectModels.read() }),
|
|
31
|
+
],
|
|
32
|
+
[
|
|
33
|
+
"lan/projectModels/set",
|
|
34
|
+
async (params) => {
|
|
35
|
+
const projects = await projectModels.set(params);
|
|
36
|
+
broadcast({
|
|
37
|
+
method: "lan/projectModels/updated",
|
|
38
|
+
params: { projects },
|
|
39
|
+
});
|
|
40
|
+
return { projects };
|
|
41
|
+
},
|
|
42
|
+
],
|
|
43
|
+
]);
|
|
44
|
+
const online = new Map([
|
|
45
|
+
["lan/agents/messages", ({ threadId }) => readAgentMessages(threadId)],
|
|
46
|
+
[
|
|
47
|
+
"lan/compaction/check",
|
|
48
|
+
async ({ threadId }) => ({ compaction: await checkCompaction(threadId) }),
|
|
49
|
+
],
|
|
50
|
+
[
|
|
51
|
+
"lan/projectless/prepare",
|
|
52
|
+
async () => {
|
|
53
|
+
try {
|
|
54
|
+
return {
|
|
55
|
+
cwd: await prepareProjectlessDirectory(config.projectlessRoot),
|
|
56
|
+
root: config.projectlessRoot,
|
|
57
|
+
};
|
|
58
|
+
} catch (error) {
|
|
59
|
+
throw new Error("创建对话工作目录失败:" + error.message);
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
],
|
|
63
|
+
["lan/files/undo", files.undo],
|
|
64
|
+
]);
|
|
65
|
+
return { offline, online, files };
|
|
66
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { isCompacting } from "../../shared/compaction-state.mjs";
|
|
2
|
+
export { isCompacting } from "../../shared/compaction-state.mjs";
|
|
3
|
+
|
|
4
|
+
// 手动整理的 RPC 响应仅确认提交;轮次结束才释放整理占用。
|
|
5
|
+
export function createCompactions(notify, now = () => Date.now() / 1000) {
|
|
6
|
+
const entries = new Map();
|
|
7
|
+
function update(threadId, value) {
|
|
8
|
+
entries.set(threadId, value);
|
|
9
|
+
notify({
|
|
10
|
+
method: "lan/compaction",
|
|
11
|
+
params: { threadId, compaction: value },
|
|
12
|
+
});
|
|
13
|
+
return value;
|
|
14
|
+
}
|
|
15
|
+
function finish(threadId, status, error, completedAt = now()) {
|
|
16
|
+
const entry = entries.get(threadId);
|
|
17
|
+
return update(threadId, { ...entry, status, error, completedAt });
|
|
18
|
+
}
|
|
19
|
+
return {
|
|
20
|
+
entries,
|
|
21
|
+
update,
|
|
22
|
+
finish,
|
|
23
|
+
busy: (threadId) => isCompacting(entries.get(threadId)),
|
|
24
|
+
start(threadId, id) {
|
|
25
|
+
return update(threadId, {
|
|
26
|
+
id: String(id),
|
|
27
|
+
status: "starting",
|
|
28
|
+
startedAt: now(),
|
|
29
|
+
});
|
|
30
|
+
},
|
|
31
|
+
reject(threadId, id, error) {
|
|
32
|
+
const entry = entries.get(threadId);
|
|
33
|
+
if (entry?.id === String(id) && isCompacting(entry))
|
|
34
|
+
finish(threadId, "failed", error);
|
|
35
|
+
},
|
|
36
|
+
event(method, p) {
|
|
37
|
+
const entry = entries.get(p.threadId);
|
|
38
|
+
if (!isCompacting(entry)) return;
|
|
39
|
+
if (
|
|
40
|
+
method === "turn/started" &&
|
|
41
|
+
entry.status === "starting" &&
|
|
42
|
+
!entry.turnId
|
|
43
|
+
)
|
|
44
|
+
update(p.threadId, { ...entry, turnId: p.turn.id, status: "running" });
|
|
45
|
+
if (
|
|
46
|
+
(method === "item/started" || method === "item/completed") &&
|
|
47
|
+
p.item.type === "contextCompaction" &&
|
|
48
|
+
(!entry.turnId || entry.turnId === p.turnId)
|
|
49
|
+
)
|
|
50
|
+
update(p.threadId, { ...entry, turnId: p.turnId, status: "running" });
|
|
51
|
+
if (method === "turn/completed" && entry.turnId === p.turn.id) {
|
|
52
|
+
const status =
|
|
53
|
+
p.turn.status === "completed"
|
|
54
|
+
? "completed"
|
|
55
|
+
: p.turn.status === "interrupted"
|
|
56
|
+
? "interrupted"
|
|
57
|
+
: "failed";
|
|
58
|
+
finish(
|
|
59
|
+
p.threadId,
|
|
60
|
+
status,
|
|
61
|
+
p.turn.error?.message,
|
|
62
|
+
p.turn.completedAt ?? now(),
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
if (method === "thread/closed")
|
|
66
|
+
update(p.threadId, {
|
|
67
|
+
...entry,
|
|
68
|
+
status: "reconnecting",
|
|
69
|
+
error: "整理订阅已关闭,需要重新核对任务状态。",
|
|
70
|
+
});
|
|
71
|
+
},
|
|
72
|
+
disconnected() {
|
|
73
|
+
for (const [threadId, entry] of entries)
|
|
74
|
+
if (isCompacting(entry))
|
|
75
|
+
update(threadId, {
|
|
76
|
+
...entry,
|
|
77
|
+
status: "reconnecting",
|
|
78
|
+
error: "与 Codex 后端的连接中断,正在确认整理结果。",
|
|
79
|
+
});
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { homedir } from "node:os";
|
|
2
|
+
import { join, resolve } from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
|
|
5
|
+
import { productDirectories } from "./product-config.mjs";
|
|
6
|
+
|
|
7
|
+
export function readConfig(env = process.env) {
|
|
8
|
+
const port = Number(env.PORT || 8766);
|
|
9
|
+
if (!Number.isInteger(port) || port < 0 || port > 65535)
|
|
10
|
+
throw new Error("PORT 必须为 0 到 65535 之间的整数");
|
|
11
|
+
const upstreamUrl = env.CODEX_APP_SERVER_URL || "ws://127.0.0.1:8765";
|
|
12
|
+
if (!["ws:", "wss:"].includes(new URL(upstreamUrl).protocol))
|
|
13
|
+
throw new Error("CODEX_APP_SERVER_URL 必须是 WebSocket 地址");
|
|
14
|
+
const codexHome = resolve(env.CODEX_HOME || join(homedir(), ".codex"));
|
|
15
|
+
const { configDir, legacyConfigDir } = productDirectories(env);
|
|
16
|
+
const claudeHome = resolve(
|
|
17
|
+
env.CLAUDE_CONFIG_DIR || join(homedir(), ".claude"),
|
|
18
|
+
);
|
|
19
|
+
const backend = new URL(upstreamUrl);
|
|
20
|
+
if (
|
|
21
|
+
backend.protocol === "ws:" &&
|
|
22
|
+
!["127.0.0.1", "localhost", "[::1]"].includes(backend.hostname)
|
|
23
|
+
)
|
|
24
|
+
throw new Error("跨主机 CODEX_APP_SERVER_URL 必须使用 wss://");
|
|
25
|
+
if (!!env.TLS_CERT !== !!env.TLS_KEY)
|
|
26
|
+
throw new Error("TLS_CERT 与 TLS_KEY 必须同时配置");
|
|
27
|
+
const sessionDays = Number(env.AUTH_SESSION_DAYS || 30);
|
|
28
|
+
if (!Number.isInteger(sessionDays) || sessionDays < 1 || sessionDays > 90)
|
|
29
|
+
throw new Error("AUTH_SESSION_DAYS 必须是 1 到 90 之间的整数");
|
|
30
|
+
const pamService = env.AUTH_PAM_SERVICE || "login";
|
|
31
|
+
if (!/^[a-zA-Z0-9_-]+$/.test(pamService))
|
|
32
|
+
throw new Error("AUTH_PAM_SERVICE 必须是 PAM 服务名称");
|
|
33
|
+
return {
|
|
34
|
+
backendsFile: env.CLAUDEX_WEB_BACKENDS_FILE
|
|
35
|
+
? resolve(env.CLAUDEX_WEB_BACKENDS_FILE)
|
|
36
|
+
: join(configDir, "backends.json"),
|
|
37
|
+
sessionDays,
|
|
38
|
+
pamService,
|
|
39
|
+
pamtesterPath: env.AUTH_PAMTESTER
|
|
40
|
+
? resolve(env.AUTH_PAMTESTER)
|
|
41
|
+
: "pamtester",
|
|
42
|
+
tlsCert: env.TLS_CERT ? resolve(env.TLS_CERT) : null,
|
|
43
|
+
tlsKey: env.TLS_KEY ? resolve(env.TLS_KEY) : null,
|
|
44
|
+
host: env.HOST || "0.0.0.0",
|
|
45
|
+
port,
|
|
46
|
+
upstreamUrl,
|
|
47
|
+
codexHome,
|
|
48
|
+
claudeHome,
|
|
49
|
+
configDir,
|
|
50
|
+
legacyConfigDir,
|
|
51
|
+
projectlessRoot: join(configDir, "workspaces"),
|
|
52
|
+
defaultCwd: resolve(env.DEFAULT_CWD || homedir()),
|
|
53
|
+
staticDir: env.STATIC_DIR
|
|
54
|
+
? resolve(env.STATIC_DIR)
|
|
55
|
+
: fileURLToPath(new URL("../../client/dist/", import.meta.url)),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { open } from "node:fs/promises";
|
|
2
|
+
import { basename, isAbsolute } from "node:path";
|
|
3
|
+
import { pipeline } from "node:stream/promises";
|
|
4
|
+
import { filePresentation } from "../../shared/file-types.mjs";
|
|
5
|
+
|
|
6
|
+
export async function serveFile(request, response) {
|
|
7
|
+
response.setHeader("Cache-Control", "no-store");
|
|
8
|
+
response.setHeader("X-Content-Type-Options", "nosniff");
|
|
9
|
+
if (!["GET", "HEAD"].includes(request.method)) {
|
|
10
|
+
response.writeHead(405, { Allow: "GET, HEAD" }).end();
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
const url = new URL(request.url, "http://localhost");
|
|
14
|
+
const path = url.searchParams.get("path");
|
|
15
|
+
if (!path || !isAbsolute(path) || path.includes("\0")) {
|
|
16
|
+
response.writeHead(400).end("Invalid file path");
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const presentation = filePresentation(path);
|
|
20
|
+
const download =
|
|
21
|
+
url.searchParams.get("download") === "1" ||
|
|
22
|
+
!presentation ||
|
|
23
|
+
presentation.kind === "download";
|
|
24
|
+
const type = presentation?.mime || "application/octet-stream";
|
|
25
|
+
const filename = encodeURIComponent(basename(path)).replace(
|
|
26
|
+
/['()*]/g,
|
|
27
|
+
(character) => "%" + character.charCodeAt(0).toString(16).toUpperCase(),
|
|
28
|
+
);
|
|
29
|
+
let file;
|
|
30
|
+
try {
|
|
31
|
+
file = await open(path, "r");
|
|
32
|
+
const metadata = await file.stat();
|
|
33
|
+
const { size } = metadata;
|
|
34
|
+
if (!metadata.isFile()) {
|
|
35
|
+
response.writeHead(404).end("File not found");
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
let start = 0;
|
|
39
|
+
let end = size - 1;
|
|
40
|
+
const range = request.method === "GET" && request.headers.range;
|
|
41
|
+
if (range) {
|
|
42
|
+
const match = /^bytes=(\d*)-(\d*)$/.exec(range);
|
|
43
|
+
if (match && (match[1] || match[2])) {
|
|
44
|
+
if (match[1]) {
|
|
45
|
+
start = Number(match[1]);
|
|
46
|
+
if (match[2]) end = Math.min(Number(match[2]), end);
|
|
47
|
+
} else {
|
|
48
|
+
start = Math.max(0, size - Number(match[2]));
|
|
49
|
+
if (Number(match[2]) === 0) start = size;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if (
|
|
53
|
+
!match ||
|
|
54
|
+
!(match[1] || match[2]) ||
|
|
55
|
+
!Number.isSafeInteger(start) ||
|
|
56
|
+
!Number.isSafeInteger(end) ||
|
|
57
|
+
start < 0 ||
|
|
58
|
+
start >= size ||
|
|
59
|
+
end < start
|
|
60
|
+
) {
|
|
61
|
+
response.writeHead(416, { "Content-Range": `bytes */${size}` }).end();
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
response.writeHead(range ? 206 : 200, {
|
|
66
|
+
"Content-Type": type,
|
|
67
|
+
"Content-Length": size ? end - start + 1 : 0,
|
|
68
|
+
"Accept-Ranges": "bytes",
|
|
69
|
+
"Content-Disposition": `${download ? "attachment" : "inline"}; filename*=UTF-8''${filename}`,
|
|
70
|
+
...(type === "image/svg+xml"
|
|
71
|
+
? {
|
|
72
|
+
"Content-Security-Policy":
|
|
73
|
+
"sandbox; default-src 'none'; style-src 'unsafe-inline'; img-src data:",
|
|
74
|
+
}
|
|
75
|
+
: {}),
|
|
76
|
+
...(range ? { "Content-Range": `bytes ${start}-${end}/${size}` } : {}),
|
|
77
|
+
});
|
|
78
|
+
if (request.method === "HEAD" || !size) response.end();
|
|
79
|
+
else
|
|
80
|
+
await pipeline(
|
|
81
|
+
file.createReadStream({ start, end, autoClose: false }),
|
|
82
|
+
response,
|
|
83
|
+
);
|
|
84
|
+
} catch (error) {
|
|
85
|
+
if (response.headersSent) response.destroy();
|
|
86
|
+
else
|
|
87
|
+
response
|
|
88
|
+
.writeHead(
|
|
89
|
+
["ENOENT", "ENOTDIR"].includes(error.code)
|
|
90
|
+
? 404
|
|
91
|
+
: error.code === "EACCES"
|
|
92
|
+
? 403
|
|
93
|
+
: 500,
|
|
94
|
+
)
|
|
95
|
+
.end("Unable to load file");
|
|
96
|
+
} finally {
|
|
97
|
+
await file?.close();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { promisify } from "node:util";
|
|
3
|
+
import {
|
|
4
|
+
lstat,
|
|
5
|
+
readFile,
|
|
6
|
+
writeFile,
|
|
7
|
+
unlink,
|
|
8
|
+
mkdtemp,
|
|
9
|
+
rm,
|
|
10
|
+
} from "node:fs/promises";
|
|
11
|
+
import { tmpdir } from "node:os";
|
|
12
|
+
import { join, resolve } from "node:path";
|
|
13
|
+
|
|
14
|
+
const exec = promisify(execFile);
|
|
15
|
+
|
|
16
|
+
export function changesForPath(items, cwd, path) {
|
|
17
|
+
const files = new Map();
|
|
18
|
+
for (const item of items) {
|
|
19
|
+
if (item.type !== "fileChange" || item.status !== "completed") continue;
|
|
20
|
+
for (const change of item.changes || []) {
|
|
21
|
+
const before = resolve(cwd, change.path);
|
|
22
|
+
const after = resolve(cwd, change.kind?.move_path || change.path);
|
|
23
|
+
const changes = files.get(before) || [];
|
|
24
|
+
if (before !== after) files.delete(before);
|
|
25
|
+
files.set(after, [...changes, change]);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return files.get(resolve(cwd, path)) || [];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async function snapshot(path) {
|
|
32
|
+
try {
|
|
33
|
+
const info = await lstat(path);
|
|
34
|
+
if (!info.isFile()) throw new Error("仅支持撤销普通文本文件的修改。");
|
|
35
|
+
const data = await readFile(path);
|
|
36
|
+
if (data.includes(0) || !Buffer.from(data.toString("utf8")).equals(data))
|
|
37
|
+
throw new Error("此文件包含二进制内容,请在文件面板检查。");
|
|
38
|
+
return { data, mode: info.mode };
|
|
39
|
+
} catch (error) {
|
|
40
|
+
if (error.code === "ENOENT") return { data: null };
|
|
41
|
+
throw error;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function equal(a, b) {
|
|
46
|
+
return a === null ? b === null : b !== null && a.equals(b);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// 在临时文件上逆序校验每次补丁,再核对源文件快照,最后写回单个文件的结果。
|
|
50
|
+
export async function undoFileChanges({
|
|
51
|
+
cwd,
|
|
52
|
+
changes,
|
|
53
|
+
isActive = () => false,
|
|
54
|
+
}) {
|
|
55
|
+
if (!changes.length) throw new Error("此轮没有可撤销的文件修改。");
|
|
56
|
+
const paths = [
|
|
57
|
+
...new Set(
|
|
58
|
+
changes.flatMap((c) => [
|
|
59
|
+
resolve(cwd, c.path),
|
|
60
|
+
resolve(cwd, c.kind?.move_path || c.path),
|
|
61
|
+
]),
|
|
62
|
+
),
|
|
63
|
+
];
|
|
64
|
+
const snapshots = new Map();
|
|
65
|
+
for (const path of paths) snapshots.set(path, await snapshot(path));
|
|
66
|
+
const last = changes.at(-1);
|
|
67
|
+
const currentPath = resolve(cwd, last.kind?.move_path || last.path);
|
|
68
|
+
let path = currentPath;
|
|
69
|
+
let data = snapshots.get(currentPath).data;
|
|
70
|
+
const scratch = await mkdtemp(join(tmpdir(), "claudex-web-undo-"));
|
|
71
|
+
try {
|
|
72
|
+
for (const change of changes.slice().reverse()) {
|
|
73
|
+
const before = resolve(cwd, change.path);
|
|
74
|
+
const after = resolve(cwd, change.kind?.move_path || change.path);
|
|
75
|
+
if (path !== after)
|
|
76
|
+
throw new Error("文件移动记录不完整,请重新加载对话。");
|
|
77
|
+
const kind = change.kind?.type;
|
|
78
|
+
const hunk =
|
|
79
|
+
kind === "update" || /^--- .+\n\+\+\+ .+\n/m.test(change.diff)
|
|
80
|
+
? change.diff.search(/^@@ /m)
|
|
81
|
+
: -1;
|
|
82
|
+
if (hunk >= 0) {
|
|
83
|
+
const oldName = kind === "add" ? "/dev/null" : "a/file";
|
|
84
|
+
const newName = kind === "delete" ? "/dev/null" : "b/file";
|
|
85
|
+
await rm(join(scratch, "file"), { force: true });
|
|
86
|
+
if (data !== null) await writeFile(join(scratch, "file"), data);
|
|
87
|
+
await writeFile(
|
|
88
|
+
join(scratch, "patch"),
|
|
89
|
+
`--- ${oldName}\n+++ ${newName}\n${change.diff.slice(hunk)}`,
|
|
90
|
+
);
|
|
91
|
+
try {
|
|
92
|
+
await exec(
|
|
93
|
+
"git",
|
|
94
|
+
[
|
|
95
|
+
"apply",
|
|
96
|
+
"--reverse",
|
|
97
|
+
"--unidiff-zero",
|
|
98
|
+
"--whitespace=nowarn",
|
|
99
|
+
"patch",
|
|
100
|
+
],
|
|
101
|
+
{ cwd: scratch, timeout: 30000 },
|
|
102
|
+
);
|
|
103
|
+
} catch (error) {
|
|
104
|
+
if (error.code === "ENOENT")
|
|
105
|
+
throw new Error("服务环境缺少 Git,请安装 Git 后重试。");
|
|
106
|
+
throw new Error(
|
|
107
|
+
"撤销补丁校验失败,差异格式或当前内容存在冲突。已保留当前文件,请查看变更后处理。",
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
data = (await snapshot(join(scratch, "file"))).data;
|
|
111
|
+
} else if (kind === "add") {
|
|
112
|
+
if (!equal(data, Buffer.from(change.diff)))
|
|
113
|
+
throw new Error("新增文件已有后续修改,已保留当前内容。");
|
|
114
|
+
data = null;
|
|
115
|
+
} else if (kind === "delete") {
|
|
116
|
+
if (data !== null) throw new Error("原路径已有文件,已保留当前内容。");
|
|
117
|
+
data = Buffer.from(change.diff);
|
|
118
|
+
} else if (before === after || change.diff.trim()) {
|
|
119
|
+
throw new Error("此修改缺少可撤销的统一差异,请在文件面板检查。");
|
|
120
|
+
}
|
|
121
|
+
path = before;
|
|
122
|
+
}
|
|
123
|
+
if (path !== currentPath && snapshots.get(path).data !== null)
|
|
124
|
+
throw new Error("原路径已有文件,已保留当前内容。");
|
|
125
|
+
if (isActive()) throw new Error("任务正在运行,请在结束后撤销修改。");
|
|
126
|
+
for (const [file, saved] of snapshots) {
|
|
127
|
+
if (!equal(saved.data, (await snapshot(file)).data))
|
|
128
|
+
throw new Error("校验期间文件发生变化,请检查内容后重试。");
|
|
129
|
+
}
|
|
130
|
+
if (data !== null)
|
|
131
|
+
await writeFile(path, data, {
|
|
132
|
+
flag: snapshots.get(path).data === null ? "wx" : "w",
|
|
133
|
+
mode: snapshots.get(currentPath).mode,
|
|
134
|
+
});
|
|
135
|
+
if (currentPath !== path || data === null) {
|
|
136
|
+
if (snapshots.get(currentPath).data !== null) await unlink(currentPath);
|
|
137
|
+
}
|
|
138
|
+
return { path, currentPath };
|
|
139
|
+
} finally {
|
|
140
|
+
await rm(scratch, { recursive: true, force: true });
|
|
141
|
+
}
|
|
142
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { X509Certificate } from "node:crypto";
|
|
2
|
+
import { createSummaryTranslationHandler } from "../summary-translation.mjs";
|
|
3
|
+
import { serveFile } from "../file-content.mjs";
|
|
4
|
+
import { createStaticHandler } from "./static.mjs";
|
|
5
|
+
export function createHttpHandler({
|
|
6
|
+
config,
|
|
7
|
+
tls,
|
|
8
|
+
authentication,
|
|
9
|
+
version,
|
|
10
|
+
isReady,
|
|
11
|
+
}) {
|
|
12
|
+
const publicCertificate = new X509Certificate(tls.cert);
|
|
13
|
+
const translateSummary = createSummaryTranslationHandler();
|
|
14
|
+
const serveStatic = createStaticHandler(config.staticDir);
|
|
15
|
+
return async function handleHttp(request, response) {
|
|
16
|
+
const ready = isReady();
|
|
17
|
+
response.setHeader("X-Content-Type-Options", "nosniff");
|
|
18
|
+
response.setHeader("Referrer-Policy", "no-referrer");
|
|
19
|
+
response.setHeader(
|
|
20
|
+
"Content-Security-Policy",
|
|
21
|
+
"frame-ancestors 'self'; base-uri 'self'; object-src 'self'; upgrade-insecure-requests",
|
|
22
|
+
);
|
|
23
|
+
response.setHeader("Cache-Control", "no-store");
|
|
24
|
+
const path = new URL(request.url, "http://localhost").pathname;
|
|
25
|
+
if (["/api/tls/info", "/api/tls/certificate"].includes(path)) {
|
|
26
|
+
if (!["GET", "HEAD"].includes(request.method)) {
|
|
27
|
+
response.writeHead(405, { Allow: "GET, HEAD" });
|
|
28
|
+
response.end();
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
const download = path === "/api/tls/certificate";
|
|
32
|
+
const body = download
|
|
33
|
+
? publicCertificate.raw
|
|
34
|
+
: Buffer.from(
|
|
35
|
+
JSON.stringify({
|
|
36
|
+
fingerprint: publicCertificate.fingerprint256,
|
|
37
|
+
validTo: publicCertificate.validTo,
|
|
38
|
+
selfSigned:
|
|
39
|
+
publicCertificate.subject === publicCertificate.issuer &&
|
|
40
|
+
publicCertificate.verify(publicCertificate.publicKey),
|
|
41
|
+
}),
|
|
42
|
+
);
|
|
43
|
+
response.writeHead(200, {
|
|
44
|
+
"Content-Type": download
|
|
45
|
+
? "application/pkix-cert"
|
|
46
|
+
: "application/json; charset=utf-8",
|
|
47
|
+
"Content-Length": body.length,
|
|
48
|
+
...(download
|
|
49
|
+
? { "Content-Disposition": 'attachment; filename="claudex-web.cer"' }
|
|
50
|
+
: {}),
|
|
51
|
+
});
|
|
52
|
+
response.end(request.method === "HEAD" ? undefined : body);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (await authentication.handle(request, response, path)) return;
|
|
56
|
+
if (
|
|
57
|
+
path.startsWith("/api/") &&
|
|
58
|
+
!authentication.authorize(request, response)
|
|
59
|
+
)
|
|
60
|
+
return;
|
|
61
|
+
if (["/api/files", "/api/media"].includes(path)) {
|
|
62
|
+
await serveFile(request, response);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
if (path === "/api/translate") {
|
|
66
|
+
await translateSummary(request, response);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
if (path === "/healthz") {
|
|
70
|
+
response.writeHead(ready ? 200 : 503, {
|
|
71
|
+
"Content-Type": "application/json",
|
|
72
|
+
"Cache-Control": "no-store",
|
|
73
|
+
});
|
|
74
|
+
response.end(JSON.stringify({ ready, version }));
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
await serveStatic(request, response, path);
|
|
79
|
+
};
|
|
80
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { readFile, stat } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { gzipSync } from "node:zlib";
|
|
4
|
+
const files = new Map([
|
|
5
|
+
["/", ["index.html", "text/html; charset=utf-8"]],
|
|
6
|
+
["/app.js", ["app.js", "text/javascript; charset=utf-8"]],
|
|
7
|
+
["/style.css", ["style.css", "text/css; charset=utf-8"]],
|
|
8
|
+
["/favicon.ico", ["favicon.ico", "image/x-icon"]],
|
|
9
|
+
["/favicon.png", ["favicon.png", "image/png"]],
|
|
10
|
+
["/apple-touch-icon.png", ["apple-touch-icon.png", "image/png"]],
|
|
11
|
+
]);
|
|
12
|
+
export function createStaticHandler(staticDir) {
|
|
13
|
+
const staticCache = new Map();
|
|
14
|
+
return async function serveStatic(request, response, path) {
|
|
15
|
+
const asset = path.match(/^\/([\w.-]+\.(js|css))$/);
|
|
16
|
+
const file =
|
|
17
|
+
files.get(path) ||
|
|
18
|
+
(asset && [
|
|
19
|
+
asset[1],
|
|
20
|
+
asset[2] === "js"
|
|
21
|
+
? "text/javascript; charset=utf-8"
|
|
22
|
+
: "text/css; charset=utf-8",
|
|
23
|
+
]);
|
|
24
|
+
if (!file) {
|
|
25
|
+
response.writeHead(404);
|
|
26
|
+
response.end("Not found");
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
try {
|
|
30
|
+
const metadata = await stat(join(staticDir, file[0]));
|
|
31
|
+
if (staticCache.get(path)?.modified !== metadata.mtimeMs) {
|
|
32
|
+
const raw = await readFile(join(staticDir, file[0]));
|
|
33
|
+
staticCache.set(path, {
|
|
34
|
+
raw,
|
|
35
|
+
gzip: gzipSync(raw),
|
|
36
|
+
modified: metadata.mtimeMs,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
const content = staticCache.get(path);
|
|
40
|
+
const compressed = /\bgzip\b/.test(
|
|
41
|
+
request.headers["accept-encoding"] || "",
|
|
42
|
+
);
|
|
43
|
+
response.writeHead(200, {
|
|
44
|
+
"Content-Type": file[1],
|
|
45
|
+
"Cache-Control": "no-store",
|
|
46
|
+
Vary: "Accept-Encoding",
|
|
47
|
+
...(compressed ? { "Content-Encoding": "gzip" } : {}),
|
|
48
|
+
});
|
|
49
|
+
response.end(compressed ? content.gzip : content.raw);
|
|
50
|
+
} catch (error) {
|
|
51
|
+
if (error.code === "ENOENT" || error.code === "ENOTDIR") {
|
|
52
|
+
staticCache.delete(path);
|
|
53
|
+
response.writeHead(404);
|
|
54
|
+
response.end("Not found");
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
console.error("Static file:", error.message);
|
|
58
|
+
response.writeHead(500);
|
|
59
|
+
response.end("Unable to load page");
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
}
|