@capyclaw/browser-workbench 0.1.0 → 0.1.17
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 +29 -7
- package/contract/browser-contract.json +484 -25
- package/demo/form.html +27 -0
- package/lib/host.mjs +1 -68
- 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/document-workbench/README.md +125 -0
- package/node_modules/@capyclaw/document-workbench/package.json +44 -0
- package/node_modules/@capyclaw/document-workbench/src/bridge/index.mjs +321 -0
- package/node_modules/@capyclaw/document-workbench/src/client/index.mjs +625 -0
- package/node_modules/@capyclaw/document-workbench/src/client/screenshot.mjs +98 -0
- package/node_modules/@capyclaw/document-workbench/src/index.mjs +9 -0
- package/node_modules/@capyclaw/document-workbench/src/model/audit.mjs +101 -0
- package/node_modules/@capyclaw/document-workbench/src/model/conformance.mjs +92 -0
- package/node_modules/@capyclaw/document-workbench/src/model/ids.mjs +56 -0
- package/node_modules/@capyclaw/document-workbench/src/model/index.mjs +18 -0
- package/node_modules/@capyclaw/document-workbench/src/model/locate.mjs +118 -0
- package/node_modules/@capyclaw/document-workbench/src/model/manifest.mjs +148 -0
- package/node_modules/@capyclaw/document-workbench/src/model/ops.mjs +160 -0
- package/node_modules/@capyclaw/document-workbench/src/model/validate.mjs +72 -0
- package/node_modules/@capyclaw/document-workbench/src/ops/index.mjs +93 -0
- package/node_modules/@capyclaw/document-workbench/src/session/context.mjs +91 -0
- package/node_modules/@capyclaw/document-workbench/src/session/index.mjs +394 -0
- package/node_modules/@capyclaw/document-workbench/src/session/lease.mjs +105 -0
- package/node_modules/@capyclaw/document-workbench/src/session/mode.mjs +80 -0
- package/node_modules/@capyclaw/document-workbench/src/session/sanitize.mjs +107 -0
- package/node_modules/@capyclaw/document-workbench/src/session/state.mjs +159 -0
- package/node_modules/@capyclaw/document-workbench/src/session/watch.mjs +63 -0
- package/{server/shared/state-service.mjs → node_modules/@capyclaw/document-workbench/src/store/file-backend.mjs} +6 -97
- package/node_modules/@capyclaw/document-workbench/src/testing/index.mjs +75 -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 +143 -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 +41 -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/tool-host/README.md +56 -0
- package/node_modules/@capyclaw/tool-host/cordis.patch.yml +4 -0
- package/node_modules/@capyclaw/tool-host/dsh-suite.json +30 -0
- package/node_modules/@capyclaw/tool-host/lib/host.mjs +16 -0
- package/node_modules/@capyclaw/tool-host/package.json +59 -0
- package/node_modules/@capyclaw/tool-host/src/descriptors.mjs +142 -0
- package/node_modules/@capyclaw/tool-host/src/index.mjs +390 -0
- package/node_modules/playwright/LICENSE +202 -0
- package/node_modules/playwright/NOTICE +5 -0
- package/node_modules/playwright/README.md +318 -0
- package/node_modules/playwright/ThirdPartyNotices.txt +14 -0
- package/node_modules/playwright/cli.js +19 -0
- package/node_modules/playwright/index.d.ts +17 -0
- package/node_modules/playwright/index.js +17 -0
- package/node_modules/playwright/index.mjs +18 -0
- package/node_modules/playwright/lib/agents/agentParser.js +89 -0
- package/node_modules/playwright/lib/agents/copilot-setup-steps.yml +34 -0
- package/node_modules/playwright/lib/agents/generateAgents.js +395 -0
- package/node_modules/playwright/lib/agents/playwright-test-coverage.prompt.md +31 -0
- package/node_modules/playwright/lib/agents/playwright-test-generate.prompt.md +8 -0
- package/node_modules/playwright/lib/agents/playwright-test-generator.agent.md +88 -0
- package/node_modules/playwright/lib/agents/playwright-test-heal.prompt.md +6 -0
- package/node_modules/playwright/lib/agents/playwright-test-healer.agent.md +56 -0
- package/node_modules/playwright/lib/agents/playwright-test-plan.prompt.md +9 -0
- package/node_modules/playwright/lib/agents/playwright-test-planner.agent.md +74 -0
- package/node_modules/playwright/lib/cli/reportActions.js +78 -0
- package/node_modules/playwright/lib/cli/testActions.js +217 -0
- package/node_modules/playwright/lib/common/index.js +2937 -0
- package/node_modules/playwright/lib/common/index.js.txt +33 -0
- package/node_modules/playwright/lib/errorContext.js +129 -0
- package/node_modules/playwright/lib/globals.js +58 -0
- package/node_modules/playwright/lib/index.js +827 -0
- package/node_modules/playwright/lib/isomorphic.js +260 -0
- package/node_modules/playwright/lib/isomorphic.js.txt +9 -0
- package/node_modules/playwright/lib/loader/loaderProcessEntry.js +34 -0
- package/node_modules/playwright/lib/loader/loaderProcessEntry.js.txt +9 -0
- package/node_modules/playwright/lib/matchers/expect.js +13496 -0
- package/node_modules/playwright/lib/matchers/expect.js.LICENSE +640 -0
- package/node_modules/playwright/lib/matchers/expect.js.txt +67 -0
- package/node_modules/playwright/lib/package.js +47 -0
- package/node_modules/playwright/lib/program.js +242 -0
- package/node_modules/playwright/lib/runner/index.js +8721 -0
- package/node_modules/playwright/lib/runner/index.js.txt +62 -0
- package/node_modules/playwright/lib/transform/babelBundle.js +72490 -0
- package/node_modules/playwright/lib/transform/babelBundle.js.LICENSE +2384 -0
- package/node_modules/playwright/lib/transform/babelBundle.js.txt +328 -0
- package/node_modules/playwright/lib/transform/esmLoader.js +8066 -0
- package/node_modules/playwright/lib/transform/esmLoader.js.LICENSE +355 -0
- package/node_modules/playwright/lib/transform/esmLoader.js.txt +60 -0
- package/node_modules/playwright/lib/util.js +373 -0
- package/node_modules/playwright/lib/worker/workerProcessEntry.js +1860 -0
- package/node_modules/playwright/lib/worker/workerProcessEntry.js.txt +19 -0
- package/node_modules/playwright/package.json +50 -0
- package/node_modules/playwright/test.d.ts +18 -0
- package/node_modules/playwright/test.js +24 -0
- package/node_modules/playwright/test.mjs +35 -0
- package/node_modules/playwright/types/test.d.ts +11191 -0
- package/node_modules/playwright/types/testReporter.d.ts +950 -0
- package/node_modules/playwright-core/LICENSE +202 -0
- package/node_modules/playwright-core/NOTICE +5 -0
- package/node_modules/playwright-core/README.md +3 -0
- package/node_modules/playwright-core/ThirdPartyNotices.txt +13 -0
- package/node_modules/playwright-core/bin/install_media_pack.ps1 +5 -0
- package/node_modules/playwright-core/bin/install_webkit_wsl.ps1 +33 -0
- package/node_modules/playwright-core/bin/reinstall_chrome_beta_linux.sh +42 -0
- package/node_modules/playwright-core/bin/reinstall_chrome_beta_mac.sh +13 -0
- package/node_modules/playwright-core/bin/reinstall_chrome_beta_win.ps1 +24 -0
- package/node_modules/playwright-core/bin/reinstall_chrome_stable_linux.sh +42 -0
- package/node_modules/playwright-core/bin/reinstall_chrome_stable_mac.sh +12 -0
- package/node_modules/playwright-core/bin/reinstall_chrome_stable_win.ps1 +24 -0
- package/node_modules/playwright-core/bin/reinstall_msedge_beta_linux.sh +48 -0
- package/node_modules/playwright-core/bin/reinstall_msedge_beta_mac.sh +11 -0
- package/node_modules/playwright-core/bin/reinstall_msedge_beta_win.ps1 +23 -0
- package/node_modules/playwright-core/bin/reinstall_msedge_dev_linux.sh +48 -0
- package/node_modules/playwright-core/bin/reinstall_msedge_dev_mac.sh +11 -0
- package/node_modules/playwright-core/bin/reinstall_msedge_dev_win.ps1 +23 -0
- package/node_modules/playwright-core/bin/reinstall_msedge_stable_linux.sh +48 -0
- package/node_modules/playwright-core/bin/reinstall_msedge_stable_mac.sh +11 -0
- package/node_modules/playwright-core/bin/reinstall_msedge_stable_win.ps1 +24 -0
- package/node_modules/playwright-core/browsers.json +52 -0
- package/node_modules/playwright-core/cli.js +22 -0
- package/node_modules/playwright-core/index.d.ts +17 -0
- package/node_modules/playwright-core/index.js +17 -0
- package/node_modules/playwright-core/index.mjs +28 -0
- package/node_modules/playwright-core/lib/bootstrap.js +149 -0
- package/node_modules/playwright-core/lib/coreBundle.js +78041 -0
- package/node_modules/playwright-core/lib/entry/cliDaemon.js +5 -0
- package/node_modules/playwright-core/lib/entry/dashboardApp.js +3 -0
- package/node_modules/playwright-core/lib/entry/mcp.js +10 -0
- package/node_modules/playwright-core/lib/entry/oopBrowserDownload.js +3 -0
- package/node_modules/playwright-core/lib/package.js +50 -0
- package/node_modules/playwright-core/lib/server/chromium/appIcon.png +0 -0
- package/node_modules/playwright-core/lib/server/electron/loader.js +123 -0
- package/node_modules/playwright-core/lib/serverRegistry.js +1939 -0
- package/node_modules/playwright-core/lib/serverRegistry.js.LICENSE +65 -0
- package/node_modules/playwright-core/lib/tools/cli-client/channelSessions.js +141 -0
- package/node_modules/playwright-core/lib/tools/cli-client/cli.js +7 -0
- package/node_modules/playwright-core/lib/tools/cli-client/help.json +808 -0
- package/node_modules/playwright-core/lib/tools/cli-client/minimist.js +128 -0
- package/node_modules/playwright-core/lib/tools/cli-client/output.js +352 -0
- package/node_modules/playwright-core/lib/tools/cli-client/program.js +422 -0
- package/node_modules/playwright-core/lib/tools/cli-client/registry.js +180 -0
- package/node_modules/playwright-core/lib/tools/cli-client/session.js +261 -0
- package/node_modules/playwright-core/lib/tools/dashboard/appIcon.png +0 -0
- package/node_modules/playwright-core/lib/tools/skills/playwright-cli/SKILL.md +493 -0
- package/node_modules/playwright-core/lib/tools/skills/playwright-cli/references/element-attributes.md +23 -0
- package/node_modules/playwright-core/lib/tools/skills/playwright-cli/references/playwright-tests.md +39 -0
- package/node_modules/playwright-core/lib/tools/skills/playwright-cli/references/pr-attachments.md +60 -0
- package/node_modules/playwright-core/lib/tools/skills/playwright-cli/references/request-mocking.md +87 -0
- package/node_modules/playwright-core/lib/tools/skills/playwright-cli/references/running-code.md +245 -0
- package/node_modules/playwright-core/lib/tools/skills/playwright-cli/references/session-management.md +227 -0
- package/node_modules/playwright-core/lib/tools/skills/playwright-cli/references/storage-state.md +275 -0
- package/node_modules/playwright-core/lib/tools/skills/playwright-cli/references/test-generation.md +433 -0
- package/node_modules/playwright-core/lib/tools/skills/playwright-cli/references/tracing.md +139 -0
- package/node_modules/playwright-core/lib/tools/skills/playwright-cli/references/video-recording.md +216 -0
- package/node_modules/playwright-core/lib/tools/skills/playwright-component-testing/SKILL.md +136 -0
- package/node_modules/playwright-core/lib/tools/skills/playwright-component-testing/references/gallery-spec.md +144 -0
- package/node_modules/playwright-core/lib/tools/skills/playwright-component-testing/references/migration.md +89 -0
- package/node_modules/playwright-core/lib/tools/skills/playwright-component-testing/references/react.md +69 -0
- package/node_modules/playwright-core/lib/tools/skills/playwright-component-testing/references/typing.md +173 -0
- package/node_modules/playwright-core/lib/tools/skills/playwright-component-testing/references/vue.md +77 -0
- package/node_modules/playwright-core/lib/tools/skills/playwright-trace/SKILL.md +174 -0
- package/node_modules/playwright-core/lib/tools/utils/extension.js +112 -0
- package/node_modules/playwright-core/lib/tools/utils/socketConnection.js +108 -0
- package/node_modules/playwright-core/lib/utilsBundle.js +86078 -0
- package/node_modules/playwright-core/lib/utilsBundle.js.LICENSE +1890 -0
- package/node_modules/playwright-core/lib/vite/dashboard/assets/codeMirrorModule--QdMvsKi.css +1 -0
- package/node_modules/playwright-core/lib/vite/dashboard/assets/codeMirrorModule-CQ8RZGBm.js +32 -0
- package/node_modules/playwright-core/lib/vite/dashboard/assets/codicon-DCmgc-ay.ttf +0 -0
- package/node_modules/playwright-core/lib/vite/dashboard/assets/firefox-1bWoP6pv.svg +1 -0
- package/node_modules/playwright-core/lib/vite/dashboard/assets/firefox-beta-k3eOH_eK.svg +1 -0
- package/node_modules/playwright-core/lib/vite/dashboard/assets/firefox-nightly-Cp5nfeDT.svg +1 -0
- package/node_modules/playwright-core/lib/vite/dashboard/assets/index-AwgwcYie.css +1 -0
- package/node_modules/playwright-core/lib/vite/dashboard/assets/index-hRvO4yU2.js +12 -0
- package/node_modules/playwright-core/lib/vite/dashboard/assets/safari-na3_-uQk.svg +1 -0
- package/node_modules/playwright-core/lib/vite/dashboard/index.html +29 -0
- package/node_modules/playwright-core/lib/vite/dashboard/playwright-logo.svg +24 -0
- package/node_modules/playwright-core/lib/vite/htmlReport/index.html +16 -0
- package/node_modules/playwright-core/lib/vite/htmlReport/report.css +2 -0
- package/node_modules/playwright-core/lib/vite/htmlReport/report.js +32 -0
- package/node_modules/playwright-core/lib/vite/recorder/assets/codeMirrorModule--QdMvsKi.css +1 -0
- package/node_modules/playwright-core/lib/vite/recorder/assets/codeMirrorModule-DK72OMRi.js +32 -0
- package/node_modules/playwright-core/lib/vite/recorder/assets/codicon-DCmgc-ay.ttf +0 -0
- package/node_modules/playwright-core/lib/vite/recorder/assets/index-BZpYJZ-P.css +1 -0
- package/node_modules/playwright-core/lib/vite/recorder/assets/index-Cn0mmDuv.js +129 -0
- package/node_modules/playwright-core/lib/vite/recorder/index.html +29 -0
- package/node_modules/playwright-core/lib/vite/recorder/playwright-logo.svg +9 -0
- package/node_modules/playwright-core/lib/vite/traceViewer/assets/codeMirrorModule-BEB2e8M3.js +32 -0
- package/node_modules/playwright-core/lib/vite/traceViewer/assets/defaultSettingsView-D-g2vrvf.js +184 -0
- package/node_modules/playwright-core/lib/vite/traceViewer/assets/urlMatch-L3liM589.js +1 -0
- package/node_modules/playwright-core/lib/vite/traceViewer/assets/xtermModule-B794rD2Z.js +7 -0
- package/node_modules/playwright-core/lib/vite/traceViewer/codeMirrorModule.-QdMvsKi.css +1 -0
- package/node_modules/playwright-core/lib/vite/traceViewer/codicon.DCmgc-ay.ttf +0 -0
- package/node_modules/playwright-core/lib/vite/traceViewer/defaultSettingsView.C8BeHuxd.css +1 -0
- package/node_modules/playwright-core/lib/vite/traceViewer/index.B_TqY17P.css +1 -0
- package/node_modules/playwright-core/lib/vite/traceViewer/index.html +44 -0
- package/node_modules/playwright-core/lib/vite/traceViewer/index.oNry3Hjv.js +1 -0
- package/node_modules/playwright-core/lib/vite/traceViewer/manifest.webmanifest +16 -0
- package/node_modules/playwright-core/lib/vite/traceViewer/playwright-logo.svg +9 -0
- package/node_modules/playwright-core/lib/vite/traceViewer/snapshot.B_Jk1wbt.js +1 -0
- package/node_modules/playwright-core/lib/vite/traceViewer/snapshot.html +10 -0
- package/node_modules/playwright-core/lib/vite/traceViewer/sw.bundle.js +4 -0
- package/node_modules/playwright-core/lib/vite/traceViewer/uiMode.CYwC_gH1.js +5 -0
- package/node_modules/playwright-core/lib/vite/traceViewer/uiMode.CyMfwkXJ.css +1 -0
- package/node_modules/playwright-core/lib/vite/traceViewer/uiMode.html +18 -0
- package/node_modules/playwright-core/lib/vite/traceViewer/xtermModule.kHJ-D0s7.css +1 -0
- package/node_modules/playwright-core/lib/webp_codec.LICENSE +173 -0
- package/node_modules/playwright-core/lib/webp_codec.wasm +0 -0
- package/node_modules/playwright-core/lib/xdg-open +1267 -0
- package/node_modules/playwright-core/package.json +34 -0
- package/node_modules/playwright-core/types/protocol.d.ts +24776 -0
- package/node_modules/playwright-core/types/structs.d.ts +52 -0
- package/node_modules/playwright-core/types/types.d.ts +27167 -0
- package/package.json +30 -12
- package/plugin.mjs +1 -0
- package/server/contract.mjs +3 -3
- package/server/embedded-host.mjs +247 -0
- package/server/engine.mjs +1906 -115
- package/server/governor.mjs +189 -0
- package/server/http-utils.mjs +1 -1
- package/server/human.mjs +628 -0
- package/server/infra/http.mjs +2 -113
- package/server/infra/tuning.mjs +2 -82
- package/server/input.mjs +154 -0
- package/server/login-vault.mjs +109 -0
- package/server/mcp/handler.mjs +1 -1
- package/server/mcp/instructions.mjs +5 -2
- package/server/orphans.mjs +163 -0
- package/server/relay.mjs +160 -188
- package/server/routes/api.mjs +126 -13
- package/server/routes/assets.mjs +1 -1
- package/server/session-engines.mjs +184 -0
- package/server/shared/SHARED_MANIFEST.json +47 -24
- package/server/shared/asset-service.mjs +2 -217
- package/server/shared/editor-host.mjs +2 -125
- package/server/shared/kit/editor/lease.mjs +2 -139
- package/server/shared/kit/service/api.mjs +2 -45
- package/server/shared/kit/service/http.mjs +2 -113
- package/server/shared/kit/service/mcp-http.mjs +2 -142
- package/server/shared/kit/tools/validate.mjs +2 -115
- package/server/shared/kit/tuning.mjs +2 -82
- package/server/shared/kit/util/fs-lock.mjs +2 -132
- package/server/shared/service-host.mjs +2 -511
- package/server/shared/tool-host.mjs +2 -341
- package/server/shared/tuning.mjs +2 -63
- package/server/tools/capabilities/contract.mjs +25 -5
- package/server/tools/capabilities/interaction.mjs +113 -9
- package/server/tools/capabilities/navigation.mjs +5 -2
- package/server/tools/capabilities/screenshot.mjs +25 -8
- package/server/tools/capabilities/session.mjs +13 -7
- package/server/tools/capabilities/snapshot.mjs +1 -1
- package/server/tools/capabilities/tabs.mjs +2 -2
- package/server/tools/kernel/define.mjs +1 -1
- package/server/tools/kernel/output.mjs +8 -0
- package/server/tools/kernel/ports.mjs +12 -7
- package/server/tools/kernel/registry.mjs +1 -1
- package/server/workbench-framework.mjs +305 -0
- package/ui/app.js +409 -119
- package/ui/index.html +29 -39
- package/ui/style.css +131 -63
- package/cordis.patch.yml +0 -25
|
@@ -1,132 +1,2 @@
|
|
|
1
|
-
/**
|
|
2
|
-
|
|
3
|
-
import fs from 'node:fs'
|
|
4
|
-
import path from 'node:path'
|
|
5
|
-
import { tuning } from '../tuning.mjs'
|
|
6
|
-
|
|
7
|
-
const sleepCell = new Int32Array(new SharedArrayBuffer(4))
|
|
8
|
-
|
|
9
|
-
export function sleepSync(ms) {
|
|
10
|
-
Atomics.wait(sleepCell, 0, 0, ms)
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function pidDead(pid) {
|
|
14
|
-
if (!Number.isSafeInteger(pid) || pid <= 0) return false
|
|
15
|
-
try { process.kill(pid, 0); return false } catch (error) { return error.code === 'ESRCH' }
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function newToken() {
|
|
19
|
-
return crypto.randomBytes(12).toString('hex')
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function tryCreate(lockFile, token) {
|
|
23
|
-
fs.mkdirSync(path.dirname(lockFile), { recursive: true })
|
|
24
|
-
let fd
|
|
25
|
-
try {
|
|
26
|
-
fd = fs.openSync(lockFile, 'wx')
|
|
27
|
-
} catch (error) {
|
|
28
|
-
if (error.code === 'EEXIST') return false
|
|
29
|
-
throw error
|
|
30
|
-
}
|
|
31
|
-
try {
|
|
32
|
-
fs.writeFileSync(fd, JSON.stringify({ pid: process.pid, token, ts: Date.now() }))
|
|
33
|
-
try { fs.fsyncSync(fd) } catch { /* 部分文件系统不支持 */ }
|
|
34
|
-
} finally {
|
|
35
|
-
fs.closeSync(fd)
|
|
36
|
-
}
|
|
37
|
-
return true
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// Only a complete record whose owner is provably dead can be reclaimed. A
|
|
41
|
-
// record-specific claim serializes competing reclaimers, including across
|
|
42
|
-
// successive lock owners. An abandoned claim is intentionally not reclaimed.
|
|
43
|
-
function reclaimDead(lockFile, staleMs) {
|
|
44
|
-
let raw, info, stat
|
|
45
|
-
try {
|
|
46
|
-
raw = fs.readFileSync(lockFile, 'utf8')
|
|
47
|
-
info = JSON.parse(raw)
|
|
48
|
-
stat = fs.statSync(lockFile)
|
|
49
|
-
} catch { return false }
|
|
50
|
-
if (!/^[a-f0-9]{24}$/.test(info?.token || '') || !pidDead(info.pid) || Date.now() - stat.mtimeMs <= staleMs) return false
|
|
51
|
-
const claim = `${lockFile}.reclaim-${crypto.createHash('sha256').update(raw).digest('hex')}`
|
|
52
|
-
let fd
|
|
53
|
-
try { fd = fs.openSync(claim, 'wx') } catch { return false }
|
|
54
|
-
try {
|
|
55
|
-
try {
|
|
56
|
-
if (fs.readFileSync(lockFile, 'utf8') !== raw || !pidDead(info.pid)) return false
|
|
57
|
-
fs.rmSync(lockFile)
|
|
58
|
-
return true
|
|
59
|
-
} catch { return false }
|
|
60
|
-
} finally {
|
|
61
|
-
fs.closeSync(fd)
|
|
62
|
-
try { fs.rmSync(claim, { force: true }) } catch { /* 保留未知状态 */ }
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function releaseOwned(lockFile, token) {
|
|
67
|
-
try {
|
|
68
|
-
const info = JSON.parse(fs.readFileSync(lockFile, 'utf8'))
|
|
69
|
-
if (info.token === token) fs.rmSync(lockFile, { force: true })
|
|
70
|
-
} catch { /* 已被删除或被接管 */ }
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
function acquireSync(lockFile, { timeoutMs, staleMs }) {
|
|
74
|
-
const token = newToken()
|
|
75
|
-
const deadline = Date.now() + timeoutMs
|
|
76
|
-
const { fileLockPollMs: pollMs } = tuning()
|
|
77
|
-
while (true) {
|
|
78
|
-
if (tryCreate(lockFile, token)) return token
|
|
79
|
-
if (reclaimDead(lockFile, staleMs)) continue
|
|
80
|
-
if (Date.now() >= deadline) throw new Error(`等待文件锁超时: ${lockFile}`)
|
|
81
|
-
sleepSync(Math.min(pollMs, Math.max(1, deadline - Date.now())))
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
async function acquireAsync(lockFile, { timeoutMs, staleMs }) {
|
|
86
|
-
const token = newToken()
|
|
87
|
-
const deadline = Date.now() + timeoutMs
|
|
88
|
-
const { fileLockPollMs: pollMs } = tuning()
|
|
89
|
-
while (true) {
|
|
90
|
-
if (tryCreate(lockFile, token)) return token
|
|
91
|
-
if (reclaimDead(lockFile, staleMs)) continue
|
|
92
|
-
if (Date.now() >= deadline) throw new Error(`等待文件锁超时: ${lockFile}`)
|
|
93
|
-
await new Promise((resolve) => setTimeout(resolve, Math.min(pollMs, Math.max(1, deadline - Date.now()))))
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
export function withFileLock(lockFile, fn, opts = {}) {
|
|
98
|
-
const defaults = tuning()
|
|
99
|
-
const { timeoutMs = defaults.fileLockSyncTimeoutMs, staleMs = defaults.fileLockSyncStaleMs } = opts
|
|
100
|
-
const token = acquireSync(lockFile, { timeoutMs, staleMs })
|
|
101
|
-
try {
|
|
102
|
-
return fn()
|
|
103
|
-
} finally {
|
|
104
|
-
releaseOwned(lockFile, token)
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/** 异步版本:用于分配端口这类需要 await 探测的场景。 */
|
|
109
|
-
export async function withAsyncFileLock(lockFile, fn, opts = {}) {
|
|
110
|
-
const defaults = tuning()
|
|
111
|
-
const { timeoutMs = defaults.fileLockAsyncTimeoutMs, staleMs = defaults.fileLockSyncStaleMs } = opts
|
|
112
|
-
const token = await acquireAsync(lockFile, { timeoutMs, staleMs })
|
|
113
|
-
try {
|
|
114
|
-
return await fn()
|
|
115
|
-
} finally {
|
|
116
|
-
releaseOwned(lockFile, token)
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
export function writeJsonAtomic(file, value) {
|
|
121
|
-
fs.mkdirSync(path.dirname(file), { recursive: true })
|
|
122
|
-
const tmp = `${file}.tmp-${process.pid}-${Date.now()}-${Math.random().toString(16).slice(2)}`
|
|
123
|
-
fs.writeFileSync(tmp, JSON.stringify(value, null, 2) + '\n')
|
|
124
|
-
try { fs.renameSync(tmp, file) } catch {
|
|
125
|
-
try { fs.rmSync(file, { force: true }) } catch { /* 忽略 */ }
|
|
126
|
-
fs.renameSync(tmp, file)
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
export function readJson(file, fallback = null) {
|
|
131
|
-
try { return JSON.parse(fs.readFileSync(file, 'utf8')) } catch { return fallback }
|
|
132
|
-
}
|
|
1
|
+
/** 由 build/sync-shared.mjs 生成:实现只存在于 @capyclaw/plugin-kit/util/fs-lock,此处零拷贝。 */
|
|
2
|
+
export * from '@capyclaw/plugin-kit/util/fs-lock'
|
|
@@ -1,511 +1,2 @@
|
|
|
1
|
-
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* 功能插件只声明“我有哪些路由、健康字段、指标”,不再自己写:
|
|
5
|
-
* - http server / listen / 端口扫描 / strictPort
|
|
6
|
-
* - 单实例锁 / 复用探测 / discovery 文件
|
|
7
|
-
* - /healthz /metrics 标准骨架
|
|
8
|
-
* - 静态资源、路径穿越防护、优雅关闭
|
|
9
|
-
*
|
|
10
|
-
* 两种模式:
|
|
11
|
-
* standalone(默认):自建 loopback server,行为与旧 relay 一致;
|
|
12
|
-
* embedded:传入 DSH 的 ctx.webServer,把同一套路由挂到 `<basePath>` 前缀。
|
|
13
|
-
*/
|
|
14
|
-
import http from 'node:http'
|
|
15
|
-
import fs from 'node:fs'
|
|
16
|
-
import os from 'node:os'
|
|
17
|
-
import path from 'node:path'
|
|
18
|
-
import crypto from 'node:crypto'
|
|
19
|
-
import { listen, send, sendText, readBody } from '@capyclaw/plugin-kit/service/http'
|
|
20
|
-
import { createServiceApi } from '@capyclaw/plugin-kit/service/api'
|
|
21
|
-
import { tuning } from './tuning.mjs'
|
|
22
|
-
|
|
23
|
-
const MIME = {
|
|
24
|
-
'.html': 'text/html; charset=utf-8',
|
|
25
|
-
'.js': 'text/javascript; charset=utf-8',
|
|
26
|
-
'.mjs': 'text/javascript; charset=utf-8',
|
|
27
|
-
'.css': 'text/css; charset=utf-8',
|
|
28
|
-
'.json': 'application/json; charset=utf-8',
|
|
29
|
-
'.svg': 'image/svg+xml',
|
|
30
|
-
'.png': 'image/png',
|
|
31
|
-
'.jpg': 'image/jpeg',
|
|
32
|
-
'.jpeg': 'image/jpeg',
|
|
33
|
-
'.webp': 'image/webp',
|
|
34
|
-
'.woff2': 'font/woff2',
|
|
35
|
-
'.map': 'application/json; charset=utf-8',
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function slugOf(id) {
|
|
39
|
-
return String(id || 'service').toLowerCase().replace(/[^a-z0-9]+/g, '_').replace(/^_+|_+$/g, '') || 'service'
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function pidAlive(pid) {
|
|
43
|
-
if (!pid) return false
|
|
44
|
-
try { process.kill(pid, 0); return true } catch (error) { return error.code === 'EPERM' }
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/** 浏览器跨源写请求防护:只允许本机 loopback 页面 POST。 */
|
|
48
|
-
function isLoopbackOrigin(origin) {
|
|
49
|
-
if (!origin) return true
|
|
50
|
-
try {
|
|
51
|
-
const parsed = new URL(origin)
|
|
52
|
-
return parsed.hostname === '127.0.0.1' || parsed.hostname === 'localhost' || parsed.hostname === '::1' || parsed.hostname === '[::1]'
|
|
53
|
-
} catch {
|
|
54
|
-
return false
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function acquireLock(lockFile) {
|
|
59
|
-
fs.mkdirSync(path.dirname(lockFile), { recursive: true })
|
|
60
|
-
const token = crypto.randomBytes(12).toString('hex')
|
|
61
|
-
const { lockFreshGraceMs } = tuning()
|
|
62
|
-
for (let attempt = 0; attempt < 2; attempt++) {
|
|
63
|
-
try {
|
|
64
|
-
/* 原子创建:两个进程同时启动时只有一个能拿到 'wx'。 */
|
|
65
|
-
const fd = fs.openSync(lockFile, 'wx')
|
|
66
|
-
try {
|
|
67
|
-
fs.writeFileSync(fd, JSON.stringify({ pid: process.pid, token, port: 0, timestamp: Date.now() }))
|
|
68
|
-
try { fs.fsyncSync(fd) } catch { /* 部分文件系统不支持 */ }
|
|
69
|
-
} finally {
|
|
70
|
-
fs.closeSync(fd)
|
|
71
|
-
}
|
|
72
|
-
return { file: lockFile, token }
|
|
73
|
-
} catch (error) {
|
|
74
|
-
if (error.code !== 'EEXIST') throw error
|
|
75
|
-
let info = null
|
|
76
|
-
try { info = JSON.parse(fs.readFileSync(lockFile, 'utf8')) } catch { /* 可能正在写入 */ }
|
|
77
|
-
let age = 0
|
|
78
|
-
try { age = Date.now() - fs.statSync(lockFile).mtimeMs } catch { /* 已被移除 */ }
|
|
79
|
-
/* 有效且 pid 存活:绝不抢占;空/坏锁只有在存在超过宽限期后才视为残留。 */
|
|
80
|
-
if (info && pidAlive(info.pid)) return null
|
|
81
|
-
if (!info && age < lockFreshGraceMs) return null
|
|
82
|
-
try { fs.rmSync(lockFile, { force: true }) } catch { /* 忽略 */ }
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
return null
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
function writeLock(lock, port) {
|
|
89
|
-
let current = null
|
|
90
|
-
try { current = JSON.parse(fs.readFileSync(lock.file, 'utf8')) } catch { /* 忽略 */ }
|
|
91
|
-
if (!current || current.token !== lock.token) {
|
|
92
|
-
throw new Error(`锁已被其它实例接管: ${lock.file}`)
|
|
93
|
-
}
|
|
94
|
-
fs.writeFileSync(lock.file, JSON.stringify({
|
|
95
|
-
pid: process.pid, token: lock.token, port, timestamp: Date.now(),
|
|
96
|
-
}))
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
function releaseLock(lock) {
|
|
100
|
-
if (!lock) return
|
|
101
|
-
try {
|
|
102
|
-
const data = JSON.parse(fs.readFileSync(lock.file, 'utf8'))
|
|
103
|
-
if (data.token === lock.token) fs.rmSync(lock.file, { force: true })
|
|
104
|
-
} catch { /* 不是自己的锁或已被删除 */ }
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
async function probeExisting(origin, healthPath, id) {
|
|
108
|
-
try {
|
|
109
|
-
const res = await fetch(`${origin}${healthPath}`, { signal: AbortSignal.timeout(tuning().reuseProbeTimeoutMs) })
|
|
110
|
-
if (!res.ok) return null
|
|
111
|
-
const info = await res.json()
|
|
112
|
-
if (info?.service === id || info?.name === id || info?.bridge === id) return info
|
|
113
|
-
} catch { /* 端口没有本服务 */ }
|
|
114
|
-
return null
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
function safeJoin(root, rel) {
|
|
118
|
-
const target = path.resolve(root, `.${path.sep}${rel}`)
|
|
119
|
-
const base = path.resolve(root)
|
|
120
|
-
return target === base || target.startsWith(base + path.sep) ? target : null
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* @param {{
|
|
125
|
-
* id: string,
|
|
126
|
-
* title?: string,
|
|
127
|
-
* version?: string,
|
|
128
|
-
* contractVersion?: string,
|
|
129
|
-
* publicBasePath?: string,
|
|
130
|
-
* health?: (() => object)|object,
|
|
131
|
-
* metrics?: (() => Array<[string, number]>|string)|Array<[string, number]>|string,
|
|
132
|
-
* }} options
|
|
133
|
-
*/
|
|
134
|
-
export function createServiceApp(options = {}) {
|
|
135
|
-
if (!options.id) throw new Error('createServiceApp: id 必填')
|
|
136
|
-
const id = String(options.id)
|
|
137
|
-
const title = options.title || id
|
|
138
|
-
const version = options.version || '0.0.0'
|
|
139
|
-
const contractVersion = options.contractVersion || '1.0.0'
|
|
140
|
-
const slug = slugOf(options.slug || id)
|
|
141
|
-
const startedAt = Date.now()
|
|
142
|
-
const routes = []
|
|
143
|
-
let healthExtra = options.health || null
|
|
144
|
-
let metricsExtra = options.metrics || null
|
|
145
|
-
let server = null
|
|
146
|
-
let embeddedDispose = null
|
|
147
|
-
let basePath = options.publicBasePath || ''
|
|
148
|
-
let listeningPort = 0
|
|
149
|
-
let url = ''
|
|
150
|
-
let closed = false
|
|
151
|
-
|
|
152
|
-
const app = {
|
|
153
|
-
id,
|
|
154
|
-
title,
|
|
155
|
-
version,
|
|
156
|
-
contractVersion,
|
|
157
|
-
slug,
|
|
158
|
-
get port() { return listeningPort },
|
|
159
|
-
get url() { return url },
|
|
160
|
-
get running() { return !!server || !!embeddedDispose },
|
|
161
|
-
|
|
162
|
-
/** 注册一个中间件式路由:返回 true 表示已处理。 */
|
|
163
|
-
use(handler) {
|
|
164
|
-
if (typeof handler !== 'function') throw new Error('service-host: route handler 必须是函数')
|
|
165
|
-
routes.push(handler)
|
|
166
|
-
return () => {
|
|
167
|
-
const index = routes.indexOf(handler)
|
|
168
|
-
if (index >= 0) routes.splice(index, 1)
|
|
169
|
-
}
|
|
170
|
-
},
|
|
171
|
-
|
|
172
|
-
get(pathname, handler) {
|
|
173
|
-
return app.use((req, res, u) => {
|
|
174
|
-
if (req.method !== 'GET' && req.method !== 'HEAD') return false
|
|
175
|
-
if (u.pathname !== pathname) return false
|
|
176
|
-
return Promise.resolve(handler(req, res, u)).then(() => true)
|
|
177
|
-
})
|
|
178
|
-
},
|
|
179
|
-
|
|
180
|
-
post(pathname, handler) {
|
|
181
|
-
return app.use((req, res, u) => {
|
|
182
|
-
if (req.method !== 'POST' || u.pathname !== pathname) return false
|
|
183
|
-
return Promise.resolve(handler(req, res, u)).then(() => true)
|
|
184
|
-
})
|
|
185
|
-
},
|
|
186
|
-
|
|
187
|
-
/** 静态目录挂载。files 可给显式映射:{ '/': ['index.html','text/html'] }。 */
|
|
188
|
-
static(prefix, dir, { files = null, index = 'index.html' } = {}) {
|
|
189
|
-
const cleanPrefix = prefix === '/' ? '' : prefix.replace(/\/+$/, '')
|
|
190
|
-
return app.use((req, res, u) => {
|
|
191
|
-
if (req.method !== 'GET' && req.method !== 'HEAD') return false
|
|
192
|
-
if (cleanPrefix && u.pathname !== cleanPrefix && !u.pathname.startsWith(cleanPrefix + '/')) return false
|
|
193
|
-
const relRaw = cleanPrefix ? u.pathname.slice(cleanPrefix.length) : u.pathname
|
|
194
|
-
const rel = relRaw === '' || relRaw === '/' ? index : decodeURIComponent(relRaw.replace(/^\/+/, ''))
|
|
195
|
-
let file = null
|
|
196
|
-
let type = MIME[path.extname(rel).toLowerCase()] || 'application/octet-stream'
|
|
197
|
-
if (files) {
|
|
198
|
-
const hit = files[relRaw === '' ? '/' : relRaw] || files[relRaw]
|
|
199
|
-
if (!hit) return false
|
|
200
|
-
const [name, explicitType] = Array.isArray(hit) ? hit : [hit]
|
|
201
|
-
file = safeJoin(dir, name)
|
|
202
|
-
type = explicitType || type
|
|
203
|
-
} else {
|
|
204
|
-
file = safeJoin(dir, rel)
|
|
205
|
-
}
|
|
206
|
-
if (!file) return false
|
|
207
|
-
try {
|
|
208
|
-
const data = fs.readFileSync(file)
|
|
209
|
-
res.writeHead(200, {
|
|
210
|
-
'content-type': type,
|
|
211
|
-
'content-length': data.length,
|
|
212
|
-
'x-content-type-options': 'nosniff',
|
|
213
|
-
'referrer-policy': 'no-referrer',
|
|
214
|
-
'cache-control': 'no-cache',
|
|
215
|
-
})
|
|
216
|
-
res.end(req.method === 'HEAD' ? undefined : data)
|
|
217
|
-
return true
|
|
218
|
-
} catch {
|
|
219
|
-
return false
|
|
220
|
-
}
|
|
221
|
-
})
|
|
222
|
-
},
|
|
223
|
-
|
|
224
|
-
setHealth(fn) { healthExtra = fn; return app },
|
|
225
|
-
setMetrics(fn) { metricsExtra = fn; return app },
|
|
226
|
-
|
|
227
|
-
async health() {
|
|
228
|
-
const extra = typeof healthExtra === 'function' ? await healthExtra() : (healthExtra || {})
|
|
229
|
-
return {
|
|
230
|
-
...extra,
|
|
231
|
-
ok: true,
|
|
232
|
-
service: id,
|
|
233
|
-
name: id,
|
|
234
|
-
title,
|
|
235
|
-
version,
|
|
236
|
-
contractVersion,
|
|
237
|
-
pid: process.pid,
|
|
238
|
-
port: listeningPort,
|
|
239
|
-
uptimeSec: Math.max(0, Math.round((Date.now() - startedAt) / 1000)),
|
|
240
|
-
}
|
|
241
|
-
},
|
|
242
|
-
|
|
243
|
-
async metricsText() {
|
|
244
|
-
const uptime = Math.max(0, Math.round((Date.now() - startedAt) / 1000))
|
|
245
|
-
const lines = [
|
|
246
|
-
`# HELP ${slug}_up 服务是否在线`,
|
|
247
|
-
`# TYPE ${slug}_up gauge`,
|
|
248
|
-
`${slug}_up 1`,
|
|
249
|
-
`# HELP ${slug}_uptime_seconds 服务运行秒数`,
|
|
250
|
-
`# TYPE ${slug}_uptime_seconds gauge`,
|
|
251
|
-
`${slug}_uptime_seconds ${uptime}`,
|
|
252
|
-
`# HELP ${slug}_port 实际监听端口`,
|
|
253
|
-
`# TYPE ${slug}_port gauge`,
|
|
254
|
-
`${slug}_port ${listeningPort}`,
|
|
255
|
-
]
|
|
256
|
-
const extra = typeof metricsExtra === 'function' ? await metricsExtra() : metricsExtra
|
|
257
|
-
if (typeof extra === 'string' && extra) lines.push(extra.replace(/\s+$/, ''))
|
|
258
|
-
else if (Array.isArray(extra)) {
|
|
259
|
-
for (const row of extra) {
|
|
260
|
-
if (!Array.isArray(row)) continue
|
|
261
|
-
const [name, value] = row
|
|
262
|
-
if (name) lines.push(`${name} ${Number(value) || 0}`)
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
return lines.join('\n') + '\n'
|
|
266
|
-
},
|
|
267
|
-
|
|
268
|
-
async dispatch(req, res, stripPrefix = '') {
|
|
269
|
-
try {
|
|
270
|
-
/* 畸形请求行(如 absolute-form URL)不能让整个宿主进程崩溃。 */
|
|
271
|
-
const u = new URL(req.url || '/', 'http://127.0.0.1')
|
|
272
|
-
if (req.method !== 'GET' && req.method !== 'HEAD' && !isLoopbackOrigin(req.headers.origin)) {
|
|
273
|
-
return send(req, res, 403, { ok: false, error: 'cross-origin write blocked' })
|
|
274
|
-
}
|
|
275
|
-
if (stripPrefix && (u.pathname === stripPrefix || u.pathname.startsWith(stripPrefix + '/'))) {
|
|
276
|
-
u.pathname = u.pathname.slice(stripPrefix.length) || '/'
|
|
277
|
-
}
|
|
278
|
-
if (u.pathname === '/healthz') {
|
|
279
|
-
return send(req, res, 200, await app.health())
|
|
280
|
-
}
|
|
281
|
-
if (u.pathname === '/metrics') {
|
|
282
|
-
return sendText(req, res, 200, await app.metricsText(), 'text/plain; version=0.0.4; charset=utf-8')
|
|
283
|
-
}
|
|
284
|
-
for (const handler of routes) {
|
|
285
|
-
if (await handler(req, res, u)) return
|
|
286
|
-
}
|
|
287
|
-
return send(req, res, 404, { ok: false, error: 'not found', path: u.pathname })
|
|
288
|
-
} catch (error) {
|
|
289
|
-
const status = error?.code === 'BODY_TOO_LARGE'
|
|
290
|
-
? 413
|
|
291
|
-
: error?.code === 'BAD_JSON' || error?.code === 'ERR_INVALID_URL'
|
|
292
|
-
? 400
|
|
293
|
-
: 500
|
|
294
|
-
if (!res.headersSent) send(req, res, status, { ok: false, error: String((error && error.message) || error) })
|
|
295
|
-
else res.destroy()
|
|
296
|
-
}
|
|
297
|
-
},
|
|
298
|
-
|
|
299
|
-
/**
|
|
300
|
-
* @param {{
|
|
301
|
-
* port?: number, host?: string, strictPort?: boolean, allowReuse?: boolean,
|
|
302
|
-
* scanLimit?: number, lockFile?: string, hostDir?: string,
|
|
303
|
-
* discoveryFile?: string, discovery?: object, publicBasePath?: string,
|
|
304
|
-
* webServer?: object, basePath?: string,
|
|
305
|
-
* }} listenOptions
|
|
306
|
-
*/
|
|
307
|
-
async listen(listenOptions = {}) {
|
|
308
|
-
if (app.running) throw new Error(`service-host ${id}: 已经在运行`)
|
|
309
|
-
const host = listenOptions.host || '127.0.0.1'
|
|
310
|
-
const port = Number(listenOptions.port ?? 0)
|
|
311
|
-
const strictPort = !!listenOptions.strictPort
|
|
312
|
-
const allowReuse = listenOptions.allowReuse !== false
|
|
313
|
-
const scanLimit = Number(listenOptions.scanLimit ?? tuning().portScanLimit)
|
|
314
|
-
basePath = listenOptions.publicBasePath ?? listenOptions.basePath ?? basePath ?? ''
|
|
315
|
-
if (basePath === '/') basePath = ''
|
|
316
|
-
|
|
317
|
-
const webServer = listenOptions.webServer
|
|
318
|
-
if (webServer && typeof webServer.register === 'function') {
|
|
319
|
-
const prefix = basePath || `/plugins/${id}`
|
|
320
|
-
embeddedDispose = webServer.register({
|
|
321
|
-
kind: 'prefix',
|
|
322
|
-
path: prefix,
|
|
323
|
-
handler: (req, res) => app.dispatch(req, res, prefix),
|
|
324
|
-
})
|
|
325
|
-
listeningPort = Number(webServer.port) || 0
|
|
326
|
-
url = `http://127.0.0.1:${listeningPort}${prefix}`
|
|
327
|
-
return { url, port: listeningPort, reused: false, mode: 'embedded', stop: app.close }
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
const hostDir = listenOptions.hostDir || path.join(os.tmpdir(), `dsh-${id}`)
|
|
331
|
-
const lockFile = listenOptions.lockFile || path.join(hostDir, 'service.lock')
|
|
332
|
-
const publicBasePath = basePath
|
|
333
|
-
const healthPath = `${publicBasePath}/healthz` || '/healthz'
|
|
334
|
-
|
|
335
|
-
if (allowReuse && port > 0) {
|
|
336
|
-
const existing = await probeExisting(`http://127.0.0.1:${port}`, healthPath, id)
|
|
337
|
-
/* 只复用同契约版本的实例:旧代码/半初始化实例不能被当成本实例。 */
|
|
338
|
-
if (existing && existing.contractVersion && existing.contractVersion === contractVersion) {
|
|
339
|
-
listeningPort = port
|
|
340
|
-
url = `http://127.0.0.1:${port}${publicBasePath}`
|
|
341
|
-
return { url, port, reused: true, mode: 'standalone', stop: async () => {} }
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
const lock = acquireLock(lockFile)
|
|
346
|
-
if (!lock) {
|
|
347
|
-
throw new Error(`${id} 已有另一个实例在运行;如确认没有,请删除锁文件: ${lockFile}`)
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
let actualPort = port
|
|
351
|
-
let lastError = null
|
|
352
|
-
const requestTimeoutMs = Number(listenOptions.requestTimeoutMs) || 30000
|
|
353
|
-
const headersTimeoutMs = Number(listenOptions.headersTimeoutMs) || 10000
|
|
354
|
-
const keepAliveTimeoutMs = Number(listenOptions.keepAliveTimeoutMs) || 5000
|
|
355
|
-
for (let attempt = 0; attempt <= (strictPort ? 0 : scanLimit); attempt++) {
|
|
356
|
-
const tryPort = port === 0 ? 0 : port + attempt
|
|
357
|
-
try {
|
|
358
|
-
server = await listen(tryPort, { host, requestTimeoutMs, headersTimeoutMs, keepAliveTimeoutMs })
|
|
359
|
-
actualPort = server.address().port
|
|
360
|
-
break
|
|
361
|
-
} catch (error) {
|
|
362
|
-
lastError = error
|
|
363
|
-
if (port === 0 || strictPort || attempt === scanLimit) {
|
|
364
|
-
releaseLock(lock)
|
|
365
|
-
throw new Error(`端口 ${tryPort} 不可用${strictPort ? '(strictPort)' : ''}: ${(error && error.message) || error}`)
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
if (!server) {
|
|
370
|
-
releaseLock(lock)
|
|
371
|
-
throw lastError || new Error('无法监听端口')
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
try {
|
|
375
|
-
writeLock(lock, actualPort)
|
|
376
|
-
} catch (error) {
|
|
377
|
-
try { server.close() } catch { /* 忽略 */ }
|
|
378
|
-
server = null
|
|
379
|
-
releaseLock(lock)
|
|
380
|
-
throw error
|
|
381
|
-
}
|
|
382
|
-
listeningPort = actualPort
|
|
383
|
-
url = `http://127.0.0.1:${actualPort}${publicBasePath}`
|
|
384
|
-
|
|
385
|
-
server.on('request', (req, res) => {
|
|
386
|
-
Promise.resolve(app.dispatch(req, res, publicBasePath)).catch((error) => {
|
|
387
|
-
try {
|
|
388
|
-
if (!res.headersSent) send(req, res, 500, { ok: false, error: String((error && error.message) || error) })
|
|
389
|
-
else res.destroy()
|
|
390
|
-
} catch { /* 连接已断开 */ }
|
|
391
|
-
})
|
|
392
|
-
})
|
|
393
|
-
const discoveryFile = listenOptions.discoveryFile
|
|
394
|
-
|| (listenOptions.hostDir ? path.join(hostDir, 'discovery.json') : '')
|
|
395
|
-
if (discoveryFile) {
|
|
396
|
-
try {
|
|
397
|
-
fs.mkdirSync(path.dirname(discoveryFile), { recursive: true })
|
|
398
|
-
fs.writeFileSync(discoveryFile, JSON.stringify({
|
|
399
|
-
service: id,
|
|
400
|
-
...(listenOptions.discovery || {}),
|
|
401
|
-
url, port: actualPort, pid: process.pid, updated: new Date().toISOString(),
|
|
402
|
-
}, null, 2))
|
|
403
|
-
} catch { /* discovery 写失败不影响服务 */ }
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
app._lock = lock
|
|
407
|
-
app._discoveryFile = discoveryFile
|
|
408
|
-
return { url, port: actualPort, reused: false, mode: 'standalone', stop: app.close }
|
|
409
|
-
},
|
|
410
|
-
|
|
411
|
-
async close() {
|
|
412
|
-
if (closed) return
|
|
413
|
-
closed = true
|
|
414
|
-
if (embeddedDispose) {
|
|
415
|
-
try { embeddedDispose() } catch { /* 忽略 */ }
|
|
416
|
-
embeddedDispose = null
|
|
417
|
-
return
|
|
418
|
-
}
|
|
419
|
-
if (!server) return
|
|
420
|
-
const srv = server
|
|
421
|
-
server = null
|
|
422
|
-
await new Promise((resolve) => {
|
|
423
|
-
const { closeGraceMs, closeForceMs } = tuning()
|
|
424
|
-
let done = false
|
|
425
|
-
const finish = () => { if (done) return; done = true; resolve() }
|
|
426
|
-
srv.close(finish)
|
|
427
|
-
setTimeout(() => srv.closeAllConnections?.(), closeGraceMs).unref?.()
|
|
428
|
-
setTimeout(finish, closeForceMs).unref?.()
|
|
429
|
-
})
|
|
430
|
-
if (app._discoveryFile) {
|
|
431
|
-
try {
|
|
432
|
-
const data = JSON.parse(fs.readFileSync(app._discoveryFile, 'utf8'))
|
|
433
|
-
if (data.pid === process.pid) fs.rmSync(app._discoveryFile, { force: true })
|
|
434
|
-
} catch { /* 忽略 */ }
|
|
435
|
-
}
|
|
436
|
-
if (app._lock) releaseLock(app._lock)
|
|
437
|
-
},
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
return app
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
export function createServiceHost(config = {}) {
|
|
444
|
-
return {
|
|
445
|
-
api: createServiceApi('serviceHost', '1.0.0', ['http', 'static', 'health', 'metrics', 'lock', 'reuse', 'discovery', 'shared-host']),
|
|
446
|
-
version: '0.1.0',
|
|
447
|
-
createApp(options = {}) {
|
|
448
|
-
return createServiceApp({ ...config, ...options })
|
|
449
|
-
},
|
|
450
|
-
createSharedHost(options = {}) {
|
|
451
|
-
return createSharedHost({ ...config, ...options })
|
|
452
|
-
},
|
|
453
|
-
readBody,
|
|
454
|
-
}
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
/**
|
|
458
|
-
* 共享宿主:多个功能插件挂到同一个 HTTP server / 同一个端口。
|
|
459
|
-
*
|
|
460
|
-
* const host = ctx.serviceHost.createSharedHost({ id: 'dsh-app' })
|
|
461
|
-
* host.mount('/plugins/ppt', pptApp)
|
|
462
|
-
* host.mount('/plugins/canvas', canvasApp)
|
|
463
|
-
* await host.listen({ port: 3080 })
|
|
464
|
-
*
|
|
465
|
-
* 每个子 app 仍由 createServiceApp 创建;共享宿主只负责前缀分发和统一生命周期。
|
|
466
|
-
* @param {{ id?: string, title?: string, version?: string, contractVersion?: string, health?: Function|object }} options
|
|
467
|
-
*/
|
|
468
|
-
export function createSharedHost(options = {}) {
|
|
469
|
-
const mounts = new Map()
|
|
470
|
-
const id = options.id || 'dsh-shared-host'
|
|
471
|
-
const app = createServiceApp({
|
|
472
|
-
...options,
|
|
473
|
-
id,
|
|
474
|
-
title: options.title || 'DSH Shared Host',
|
|
475
|
-
version: options.version || '0.1.0',
|
|
476
|
-
contractVersion: options.contractVersion || '1.0.0',
|
|
477
|
-
health: options.health || (() => ({ children: [...mounts.keys()] })),
|
|
478
|
-
})
|
|
479
|
-
|
|
480
|
-
const mount = (prefix, child) => {
|
|
481
|
-
const clean = String(prefix || '').replace(/\/+$/, '') || '/'
|
|
482
|
-
if (clean === '/') throw new Error('shared-host: 子插件必须使用非根前缀')
|
|
483
|
-
if (mounts.has(clean)) throw new Error(`shared-host: 前缀已挂载: ${clean}`)
|
|
484
|
-
if (!child || typeof child.dispatch !== 'function') throw new Error(`shared-host: ${clean} 的子 app 必须提供 dispatch()`)
|
|
485
|
-
const dispose = app.use((req, res, u) => {
|
|
486
|
-
if (u.pathname !== clean && !u.pathname.startsWith(clean + '/')) return false
|
|
487
|
-
return child.dispatch(req, res, clean).then(() => true)
|
|
488
|
-
})
|
|
489
|
-
mounts.set(clean, { child, dispose })
|
|
490
|
-
return () => {
|
|
491
|
-
dispose()
|
|
492
|
-
mounts.delete(clean)
|
|
493
|
-
}
|
|
494
|
-
}
|
|
495
|
-
|
|
496
|
-
return {
|
|
497
|
-
id,
|
|
498
|
-
app,
|
|
499
|
-
api: createServiceApi('serviceHost', '1.0.0', ['shared-host']),
|
|
500
|
-
mount,
|
|
501
|
-
children() { return [...mounts.keys()] },
|
|
502
|
-
listen(listenOptions = {}) {
|
|
503
|
-
return app.listen(listenOptions)
|
|
504
|
-
},
|
|
505
|
-
close() {
|
|
506
|
-
return app.close()
|
|
507
|
-
},
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
export { send, sendText, readBody }
|
|
1
|
+
/** 由 build/sync-shared.mjs 生成:实现只存在于 @capyclaw/service-host/src,此处零拷贝。 */
|
|
2
|
+
export * from '@capyclaw/service-host/src'
|