@jingyi0605/codingns 0.8.1 → 0.8.5
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/bin/codingns.mjs +180 -0
- package/dist/public/assets/{AdaptiveButlerPage-b7s6F_sd.js → AdaptiveButlerPage-CJw8Ae62.js} +3 -3
- package/dist/public/assets/App-BOHBGFOd.js +30 -0
- package/dist/public/assets/{BootstrapPage-jFfl7KNK.js → BootstrapPage-BxHQT4nA.js} +1 -1
- package/dist/public/assets/{ConversationPage-sj_rn4q0.js → ConversationPage-DWFsF6BB.js} +6 -6
- package/dist/public/assets/{DesktopDetachPreviewPage-C5c88SOX.js → DesktopDetachPreviewPage-DOgEjYEf.js} +1 -1
- package/dist/public/assets/DesktopModal-D_A8sgQU.js +1 -0
- package/dist/public/assets/DesktopWindowPage-DK7L7osV.js +2 -0
- package/dist/public/assets/FileContextPanel-BdCoubcJ.js +1 -0
- package/dist/public/assets/GitSidebar-BeZ0hj7A.js +6 -0
- package/dist/public/assets/MobileCreateSessionSheet-DfLMVu8q.js +1 -0
- package/dist/public/assets/MobileSheet-5kZ-w-gU.js +1 -0
- package/dist/public/assets/{MobileTopHeaderFrame-_I0t7cC_.js → MobileTopHeaderFrame-lcp2GscV.js} +1 -1
- package/dist/public/assets/MobileWorkspaceSwitcherHeader-C6JMiOq_.js +1 -0
- package/dist/public/assets/PluginAccessOverview-DGcKAMQl.js +1 -0
- package/dist/public/assets/PluginContainerPage-BlY-xJDh.js +1 -0
- package/dist/public/assets/PluginDetailPage-CAJ7LFpD.js +1 -0
- package/dist/public/assets/PluginsListPage-BxZG1NyT.js +1 -0
- package/dist/public/assets/{RelayConnectEntryPage-CMtDWyGS.js → RelayConnectEntryPage-CfNO_TIl.js} +1 -1
- package/dist/public/assets/ServerSettingsModal-by36Z_5k.js +1 -0
- package/dist/public/assets/SessionIndexPage-DkBp9Mqz.js +1 -0
- package/dist/public/assets/SettingsPage-C-ASmJAG.js +2 -0
- package/dist/public/assets/TerminalManagerPanel-NVZRxxmH.js +1 -0
- package/dist/public/assets/{TerminalPage-CBIKHI33.js → TerminalPage-C4LNoPBp.js} +3 -3
- package/dist/public/assets/TerminalRuntimeFallbackModal-Bzum5nZ0.js +1 -0
- package/dist/public/assets/ToolFilesPage-47zbdgTW.js +1 -0
- package/dist/public/assets/ToolGitPage-Fuk_b_jg.js +1 -0
- package/dist/public/assets/ToolProcessesPage-sWSMWD-9.js +1 -0
- package/dist/public/assets/ToolsHomePage-R1mZlbZi.js +1 -0
- package/dist/public/assets/WorkbenchLandingPage-CqmiFH2u.js +1 -0
- package/dist/public/assets/WorkbenchLayout-OFi6CWgH.js +244 -0
- package/dist/public/assets/WorkbenchModal-C7qoQElW.js +1 -0
- package/dist/public/assets/WorkbenchShellRoute-BAQe_E0O.js +1 -0
- package/dist/public/assets/WorkspaceDebugDetailPage-DhKa6e9y.js +1 -0
- package/dist/public/assets/WorkspaceDetailPage-BPsrFffw.js +1 -0
- package/dist/public/assets/WorkspaceHomePage-KAtqZOAb.js +1 -0
- package/dist/public/assets/{client-runtime-manager-CdJP7nUp.js → client-runtime-manager-wmCJZKYd.js} +1 -1
- package/dist/public/assets/file-tree-icon-Mg1DiBRX.js +590 -0
- package/dist/public/assets/{index-VVxfF0s5.css → index-C4t-vvqk.css} +1 -1
- package/dist/public/assets/index-CL97fwWB.js +42 -0
- package/dist/public/assets/{login-direct-candidate-resolver-C-mo698r.js → login-direct-candidate-resolver-BOAgTuUf.js} +1 -1
- package/dist/public/assets/plugin-permission-copy-Cq99cnzV.js +1 -0
- package/dist/public/assets/plugins-api-BQTV5DOp.js +1 -0
- package/dist/public/assets/{preferences-service-CnaTvZq2.js → preferences-service-DJxbEEeg.js} +1 -1
- package/dist/public/assets/realtime-client-CLafKzzJ.js +1 -0
- package/dist/public/assets/{relay-entry-D7OgZZN-.js → relay-entry-D-LfvdiX.js} +1 -1
- package/dist/public/assets/{terminal-runtime-meta-BCtAYB0E.js → terminal-runtime-meta-BJmy8dyK.js} +1 -1
- package/dist/public/assets/useRegisteredDebugTemplates-DQAWVdCo.js +1 -0
- package/dist/public/assets/workbench-navigation-MEzCSmsK.js +1 -0
- package/dist/public/index.html +2 -2
- package/dist/server/config/env.d.ts +1 -0
- package/dist/server/config/env.js +4 -0
- package/dist/server/config/env.js.map +1 -1
- package/dist/server/helpers/wechat-claw-runtime/modules/runtime-state-store.js +1 -1
- package/dist/server/helpers/wechat-claw-runtime/modules/runtime-state-store.js.map +1 -1
- package/dist/server/modules/channels/wechat-claw-client.d.ts +51 -0
- package/dist/server/modules/channels/wechat-claw-client.js +245 -0
- package/dist/server/modules/channels/wechat-claw-client.js.map +1 -0
- package/dist/server/modules/file/file-access-guard.d.ts +2 -0
- package/dist/server/modules/file/file-access-guard.js +24 -2
- package/dist/server/modules/file/file-access-guard.js.map +1 -1
- package/dist/server/modules/file/file-controller.d.ts +153 -1
- package/dist/server/modules/file/file-controller.js +215 -1
- package/dist/server/modules/file/file-controller.js.map +1 -1
- package/dist/server/modules/file/file-preview-link-service.d.ts +2 -0
- package/dist/server/modules/file/file-preview-link-service.js +5 -1
- package/dist/server/modules/file/file-preview-link-service.js.map +1 -1
- package/dist/server/modules/file/runtime/codingns-workspace-bridge.js +911 -0
- package/dist/server/modules/file/workspace-file-bridge-service.d.ts +126 -0
- package/dist/server/modules/file/workspace-file-bridge-service.js +471 -0
- package/dist/server/modules/file/workspace-file-bridge-service.js.map +1 -0
- package/dist/server/modules/file/workspace-file-bridge-watch-service.d.ts +42 -0
- package/dist/server/modules/file/workspace-file-bridge-watch-service.js +304 -0
- package/dist/server/modules/file/workspace-file-bridge-watch-service.js.map +1 -0
- package/dist/server/modules/model-switch/cc-switch-adapter.js +1 -1
- package/dist/server/modules/model-switch/cc-switch-adapter.js.map +1 -1
- package/dist/server/modules/plugins/plugin-controller.d.ts +131 -0
- package/dist/server/modules/plugins/plugin-controller.js +277 -0
- package/dist/server/modules/plugins/plugin-controller.js.map +1 -0
- package/dist/server/modules/plugins/plugin-file-gateway-service.d.ts +38 -0
- package/dist/server/modules/plugins/plugin-file-gateway-service.js +178 -0
- package/dist/server/modules/plugins/plugin-file-gateway-service.js.map +1 -0
- package/dist/server/modules/plugins/plugin-manifest.d.ts +8 -0
- package/dist/server/modules/plugins/plugin-manifest.js +420 -0
- package/dist/server/modules/plugins/plugin-manifest.js.map +1 -0
- package/dist/server/modules/plugins/plugin-permission-service.d.ts +49 -0
- package/dist/server/modules/plugins/plugin-permission-service.js +267 -0
- package/dist/server/modules/plugins/plugin-permission-service.js.map +1 -0
- package/dist/server/modules/plugins/plugin-process-runner.d.ts +21 -0
- package/dist/server/modules/plugins/plugin-process-runner.js +127 -0
- package/dist/server/modules/plugins/plugin-process-runner.js.map +1 -0
- package/dist/server/modules/plugins/plugin-registry-service.d.ts +41 -0
- package/dist/server/modules/plugins/plugin-registry-service.js +238 -0
- package/dist/server/modules/plugins/plugin-registry-service.js.map +1 -0
- package/dist/server/modules/plugins/plugin-runtime-service.d.ts +55 -0
- package/dist/server/modules/plugins/plugin-runtime-service.js +252 -0
- package/dist/server/modules/plugins/plugin-runtime-service.js.map +1 -0
- package/dist/server/modules/plugins/plugin-runtime-session-service.d.ts +21 -0
- package/dist/server/modules/plugins/plugin-runtime-session-service.js +83 -0
- package/dist/server/modules/plugins/plugin-runtime-session-service.js.map +1 -0
- package/dist/server/modules/plugins/plugin-scheduler-service.d.ts +31 -0
- package/dist/server/modules/plugins/plugin-scheduler-service.js +232 -0
- package/dist/server/modules/plugins/plugin-scheduler-service.js.map +1 -0
- package/dist/server/modules/plugins/plugin-static-service.d.ts +11 -0
- package/dist/server/modules/plugins/plugin-static-service.js +145 -0
- package/dist/server/modules/plugins/plugin-static-service.js.map +1 -0
- package/dist/server/modules/presentation/presentation-controller.d.ts +3 -0
- package/dist/server/modules/presentation/presentation-controller.js +11 -0
- package/dist/server/modules/presentation/presentation-controller.js.map +1 -1
- package/dist/server/modules/presentation/presentation-export-task-service.d.ts +6 -0
- package/dist/server/modules/presentation/presentation-export-task-service.js +52 -8
- package/dist/server/modules/presentation/presentation-export-task-service.js.map +1 -1
- package/dist/server/modules/presentation/presentation-pptx-export-service.js +200 -14
- package/dist/server/modules/presentation/presentation-pptx-export-service.js.map +1 -1
- package/dist/server/modules/presentation/presentation-renderer.d.ts +50 -0
- package/dist/server/modules/presentation/presentation-renderer.js +450 -37
- package/dist/server/modules/presentation/presentation-renderer.js.map +1 -1
- package/dist/server/modules/tasks/task-types.d.ts +2 -0
- package/dist/server/modules/tasks/task-types.js +2 -0
- package/dist/server/modules/tasks/task-types.js.map +1 -1
- package/dist/server/modules/terminal/runtime/terminal-log-writer-process.js +1 -1
- package/dist/server/modules/terminal/runtime/terminal-log-writer-process.js.map +1 -1
- package/dist/server/routes/files.js +40 -0
- package/dist/server/routes/files.js.map +1 -1
- package/dist/server/routes/plugins-public.d.ts +3 -0
- package/dist/server/routes/plugins-public.js +5 -0
- package/dist/server/routes/plugins-public.js.map +1 -0
- package/dist/server/routes/plugins.d.ts +3 -0
- package/dist/server/routes/plugins.js +19 -0
- package/dist/server/routes/plugins.js.map +1 -0
- package/dist/server/routes/presentation.js +1 -0
- package/dist/server/routes/presentation.js.map +1 -1
- package/dist/server/server/create-server.d.ts +20 -0
- package/dist/server/server/create-server.js +47 -1
- package/dist/server/server/create-server.js.map +1 -1
- package/dist/server/shared/http/error-handler.js +10 -6
- package/dist/server/shared/http/error-handler.js.map +1 -1
- package/dist/server/shared/runtime/better-sqlite3.d.ts +5 -0
- package/dist/server/shared/runtime/better-sqlite3.js +6 -0
- package/dist/server/shared/runtime/better-sqlite3.js.map +1 -0
- package/dist/server/storage/repositories/plugin-audit-event-repository.d.ts +8 -0
- package/dist/server/storage/repositories/plugin-audit-event-repository.js +49 -0
- package/dist/server/storage/repositories/plugin-audit-event-repository.js.map +1 -0
- package/dist/server/storage/repositories/plugin-definition-repository.d.ts +10 -0
- package/dist/server/storage/repositories/plugin-definition-repository.js +84 -0
- package/dist/server/storage/repositories/plugin-definition-repository.js.map +1 -0
- package/dist/server/storage/repositories/plugin-enablement-repository.d.ts +9 -0
- package/dist/server/storage/repositories/plugin-enablement-repository.js +74 -0
- package/dist/server/storage/repositories/plugin-enablement-repository.js.map +1 -0
- package/dist/server/storage/repositories/plugin-permission-grant-repository.d.ts +12 -0
- package/dist/server/storage/repositories/plugin-permission-grant-repository.js +142 -0
- package/dist/server/storage/repositories/plugin-permission-grant-repository.js.map +1 -0
- package/dist/server/storage/repositories/plugin-run-repository.d.ts +10 -0
- package/dist/server/storage/repositories/plugin-run-repository.js +111 -0
- package/dist/server/storage/repositories/plugin-run-repository.js.map +1 -0
- package/dist/server/storage/repositories/plugin-runtime-session-repository.d.ts +10 -0
- package/dist/server/storage/repositories/plugin-runtime-session-repository.js +86 -0
- package/dist/server/storage/repositories/plugin-runtime-session-repository.js.map +1 -0
- package/dist/server/storage/sqlite/client.d.ts +2 -2
- package/dist/server/storage/sqlite/client.js +250 -1
- package/dist/server/storage/sqlite/client.js.map +1 -1
- package/dist/server/storage/sqlite/schema.sql +140 -0
- package/dist/server/types/domain.d.ts +122 -0
- package/package.json +10 -3
- package/scripts/postinstall.mjs +248 -11
- package/vendor/better-sqlite3-win32-x64-node22/LICENSE +21 -0
- package/vendor/better-sqlite3-win32-x64-node22/README.md +12 -0
- package/vendor/better-sqlite3-win32-x64-node22/README.upstream.md +99 -0
- package/vendor/better-sqlite3-win32-x64-node22/binding.gyp +38 -0
- package/vendor/better-sqlite3-win32-x64-node22/build/Release/better_sqlite3.node +0 -0
- package/vendor/better-sqlite3-win32-x64-node22/deps/common.gypi +68 -0
- package/vendor/better-sqlite3-win32-x64-node22/deps/copy.js +31 -0
- package/vendor/better-sqlite3-win32-x64-node22/deps/defines.gypi +41 -0
- package/vendor/better-sqlite3-win32-x64-node22/deps/download.sh +122 -0
- package/vendor/better-sqlite3-win32-x64-node22/deps/patches/1208.patch +15 -0
- package/vendor/better-sqlite3-win32-x64-node22/deps/sqlite3/sqlite3.c +265994 -0
- package/vendor/better-sqlite3-win32-x64-node22/deps/sqlite3/sqlite3.h +13968 -0
- package/vendor/better-sqlite3-win32-x64-node22/deps/sqlite3/sqlite3ext.h +730 -0
- package/vendor/better-sqlite3-win32-x64-node22/deps/sqlite3.gyp +80 -0
- package/vendor/better-sqlite3-win32-x64-node22/deps/test_extension.c +21 -0
- package/vendor/better-sqlite3-win32-x64-node22/lib/database.js +90 -0
- package/vendor/better-sqlite3-win32-x64-node22/lib/index.js +3 -0
- package/vendor/better-sqlite3-win32-x64-node22/lib/methods/aggregate.js +43 -0
- package/vendor/better-sqlite3-win32-x64-node22/lib/methods/backup.js +67 -0
- package/vendor/better-sqlite3-win32-x64-node22/lib/methods/function.js +31 -0
- package/vendor/better-sqlite3-win32-x64-node22/lib/methods/inspect.js +7 -0
- package/vendor/better-sqlite3-win32-x64-node22/lib/methods/pragma.js +12 -0
- package/vendor/better-sqlite3-win32-x64-node22/lib/methods/serialize.js +16 -0
- package/vendor/better-sqlite3-win32-x64-node22/lib/methods/table.js +189 -0
- package/vendor/better-sqlite3-win32-x64-node22/lib/methods/transaction.js +78 -0
- package/vendor/better-sqlite3-win32-x64-node22/lib/methods/wrappers.js +54 -0
- package/vendor/better-sqlite3-win32-x64-node22/lib/sqlite-error.js +20 -0
- package/vendor/better-sqlite3-win32-x64-node22/lib/util.js +12 -0
- package/vendor/better-sqlite3-win32-x64-node22/package.json +38 -0
- package/vendor/better-sqlite3-win32-x64-node22/scripts/verify-runtime.cjs +42 -0
- package/vendor/better-sqlite3-win32-x64-node22/src/addon.cpp +47 -0
- package/vendor/better-sqlite3-win32-x64-node22/src/better_sqlite3.cpp +74 -0
- package/vendor/better-sqlite3-win32-x64-node22/src/objects/backup.cpp +120 -0
- package/vendor/better-sqlite3-win32-x64-node22/src/objects/backup.hpp +36 -0
- package/vendor/better-sqlite3-win32-x64-node22/src/objects/database.cpp +417 -0
- package/vendor/better-sqlite3-win32-x64-node22/src/objects/database.hpp +103 -0
- package/vendor/better-sqlite3-win32-x64-node22/src/objects/statement-iterator.cpp +113 -0
- package/vendor/better-sqlite3-win32-x64-node22/src/objects/statement-iterator.hpp +50 -0
- package/vendor/better-sqlite3-win32-x64-node22/src/objects/statement.cpp +383 -0
- package/vendor/better-sqlite3-win32-x64-node22/src/objects/statement.hpp +58 -0
- package/vendor/better-sqlite3-win32-x64-node22/src/util/bind-map.cpp +73 -0
- package/vendor/better-sqlite3-win32-x64-node22/src/util/binder.cpp +193 -0
- package/vendor/better-sqlite3-win32-x64-node22/src/util/constants.cpp +172 -0
- package/vendor/better-sqlite3-win32-x64-node22/src/util/custom-aggregate.cpp +121 -0
- package/vendor/better-sqlite3-win32-x64-node22/src/util/custom-function.cpp +59 -0
- package/vendor/better-sqlite3-win32-x64-node22/src/util/custom-table.cpp +409 -0
- package/vendor/better-sqlite3-win32-x64-node22/src/util/data-converter.cpp +17 -0
- package/vendor/better-sqlite3-win32-x64-node22/src/util/data.cpp +194 -0
- package/vendor/better-sqlite3-win32-x64-node22/src/util/helpers.cpp +109 -0
- package/vendor/better-sqlite3-win32-x64-node22/src/util/macros.cpp +83 -0
- package/vendor/better-sqlite3-win32-x64-node22/src/util/query-macros.cpp +71 -0
- package/vendor/better-sqlite3-win32-x64-node22/src/util/row-builder.cpp +49 -0
- package/vendor/node-pty-fork/README.md +1 -0
- package/vendor/node-pty-fork/scripts/runtime-install-context.cjs +24 -0
- package/vendor/node-pty-fork/scripts/verify-runtime.cjs +8 -0
- package/vendor/node-pty-fork/scripts/verify-runtime.test.cjs +30 -0
- package/vendor-src/better-sqlite3-win32-x64-node22/README.md +12 -0
- package/dist/public/assets/App-B0q8uDA-.js +0 -30
- package/dist/public/assets/DesktopWindowPage-BdKUZrKG.js +0 -2
- package/dist/public/assets/FileContextPanel-lM5mBcfn.js +0 -1
- package/dist/public/assets/GitSidebar-CmXAhyjo.js +0 -6
- package/dist/public/assets/MobileCreateSessionSheet-CceV0-AH.js +0 -1
- package/dist/public/assets/MobileWorkspaceSwitcherHeader-DhaM9fgx.js +0 -1
- package/dist/public/assets/ServerSettingsModal-Rkgn3jft.js +0 -1
- package/dist/public/assets/SessionIndexPage-BOumP3ij.js +0 -1
- package/dist/public/assets/SettingsPage-vx_c-00-.js +0 -2
- package/dist/public/assets/TerminalManagerPanel-D9JHCS6d.js +0 -1
- package/dist/public/assets/TerminalRuntimeFallbackModal-W5NjqFEz.js +0 -1
- package/dist/public/assets/ToolFilesPage-DWtpaN_7.js +0 -1
- package/dist/public/assets/ToolGitPage-MpGXsw4_.js +0 -1
- package/dist/public/assets/ToolProcessesPage-B1jHQKhZ.js +0 -1
- package/dist/public/assets/ToolsHomePage-CJrn8ZFl.js +0 -1
- package/dist/public/assets/WorkbenchLandingPage-CJQ1V5U-.js +0 -1
- package/dist/public/assets/WorkbenchLayout-D48i71PU.js +0 -244
- package/dist/public/assets/WorkbenchModal-BYNuMIoV.js +0 -1
- package/dist/public/assets/WorkbenchShellRoute-CGKjy4J-.js +0 -1
- package/dist/public/assets/WorkspaceDebugDetailPage-DCAYgays.js +0 -1
- package/dist/public/assets/WorkspaceDetailPage-D5v-nUIi.js +0 -1
- package/dist/public/assets/WorkspaceHomePage-I5G1p5SR.js +0 -1
- package/dist/public/assets/file-tree-icon-BFwaPxKs.js +0 -590
- package/dist/public/assets/index-CjewzOUG.js +0 -42
- package/dist/public/assets/model-switch-api-Br0ivfe5.js +0 -1
- package/dist/public/assets/realtime-client-BlQRqVmm.js +0 -1
- package/dist/public/assets/useRegisteredDebugTemplates-CXmxu69X.js +0 -1
|
@@ -0,0 +1,911 @@
|
|
|
1
|
+
(function () {
|
|
2
|
+
if (typeof window === "undefined") {
|
|
3
|
+
return;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
if (window.CodingNSWorkspace) {
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
var REQUEST_TIMEOUT_MS = 3000;
|
|
11
|
+
var WATCH_POLL_INTERVAL_MS = 700;
|
|
12
|
+
var RESPONSE_TYPE = "codingns.workspace.response";
|
|
13
|
+
var REQUEST_TYPE = "codingns.workspace.request";
|
|
14
|
+
var EVENT_TYPE = "codingns.workspace.event";
|
|
15
|
+
var DEBUG_TYPE = "codingns.workspace.debug";
|
|
16
|
+
var bootstrapConfig = readBootstrapConfig();
|
|
17
|
+
var parentOrigin = null;
|
|
18
|
+
var pendingRequests = Object.create(null);
|
|
19
|
+
var watchListeners = Object.create(null);
|
|
20
|
+
var activeWatchPollers = Object.create(null);
|
|
21
|
+
var requestSeq = 0;
|
|
22
|
+
var debugEvents = [];
|
|
23
|
+
var debugState = createDebugState();
|
|
24
|
+
var iframeHostBridge = createIframeHostBridge();
|
|
25
|
+
var iframeBridgeUnavailable = false;
|
|
26
|
+
debugLog("init", {
|
|
27
|
+
href: window.location.href,
|
|
28
|
+
referrer: document.referrer || "",
|
|
29
|
+
context: readWorkspaceContext(),
|
|
30
|
+
parentOrigin: isIframe() ? ensureParentOrigin() : null
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
function readBootstrapConfig() {
|
|
34
|
+
var raw = window.__CODINGNS_WORKSPACE_BRIDGE_BOOTSTRAP__;
|
|
35
|
+
if (raw && typeof raw === "object") {
|
|
36
|
+
return raw;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
var script = document.currentScript;
|
|
40
|
+
if (!script || typeof script.getAttribute !== "function") {
|
|
41
|
+
return {};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
var workspaceId = script.getAttribute("data-codingns-workspace-id") || "";
|
|
45
|
+
var hostOrigin = script.getAttribute("data-codingns-host-origin") || "";
|
|
46
|
+
var parentOrigin = script.getAttribute("data-codingns-parent-origin") || "";
|
|
47
|
+
var runtimeVersion = script.getAttribute("data-codingns-runtime-version") || "";
|
|
48
|
+
return {
|
|
49
|
+
workspaceId: workspaceId,
|
|
50
|
+
hostOrigin: hostOrigin,
|
|
51
|
+
parentOrigin: parentOrigin,
|
|
52
|
+
runtimeVersion: runtimeVersion,
|
|
53
|
+
runtimeScriptPath: script.getAttribute("src") || ""
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function createDebugState() {
|
|
58
|
+
return {
|
|
59
|
+
initializedAt: Date.now(),
|
|
60
|
+
runtimeUrl: window.location.href,
|
|
61
|
+
referrer: document.referrer || "",
|
|
62
|
+
bootstrapParentOrigin: typeof bootstrapConfig.parentOrigin === "string" ? bootstrapConfig.parentOrigin : "",
|
|
63
|
+
bootstrapHostOrigin: typeof bootstrapConfig.hostOrigin === "string" ? bootstrapConfig.hostOrigin : "",
|
|
64
|
+
runtimeVersion: typeof bootstrapConfig.runtimeVersion === "string" ? bootstrapConfig.runtimeVersion : "",
|
|
65
|
+
isIframe: isIframe(),
|
|
66
|
+
lastRequestId: null,
|
|
67
|
+
lastRequestAction: null,
|
|
68
|
+
lastRequestTargetOrigin: null,
|
|
69
|
+
lastResponseId: null,
|
|
70
|
+
lastResponseOrigin: null,
|
|
71
|
+
lastResponseAccepted: false,
|
|
72
|
+
lastEventType: null,
|
|
73
|
+
lastDropReason: null,
|
|
74
|
+
lastTimeoutRequestId: null,
|
|
75
|
+
lastTimeoutAction: null,
|
|
76
|
+
lastTimeoutTargetOrigin: null,
|
|
77
|
+
pendingRequestCount: 0,
|
|
78
|
+
pendingRequestIds: [],
|
|
79
|
+
iframeBridgeUnavailable: false,
|
|
80
|
+
lastHttpFallbackAction: null,
|
|
81
|
+
lastHttpRequestAction: null,
|
|
82
|
+
lastHttpRequestUrl: null,
|
|
83
|
+
debugEventCount: 0
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function updatePendingRequestCount() {
|
|
88
|
+
var ids = Object.keys(pendingRequests);
|
|
89
|
+
debugState.pendingRequestCount = ids.length;
|
|
90
|
+
debugState.pendingRequestIds = ids;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function debugLog(stage, payload) {
|
|
94
|
+
var entry = {
|
|
95
|
+
at: new Date().toISOString(),
|
|
96
|
+
source: "workspace-runtime",
|
|
97
|
+
stage: stage,
|
|
98
|
+
payload: payload || {}
|
|
99
|
+
};
|
|
100
|
+
debugEvents.push(entry);
|
|
101
|
+
if (debugEvents.length > 200) {
|
|
102
|
+
debugEvents.shift();
|
|
103
|
+
}
|
|
104
|
+
debugState.debugEventCount = debugEvents.length;
|
|
105
|
+
|
|
106
|
+
if (!window.console || typeof window.console.debug !== "function") {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
window.console.debug("[workspace-bridge] " + stage, payload);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function isIframe() {
|
|
114
|
+
return window.parent && window.parent !== window;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function ensureParentOrigin() {
|
|
118
|
+
if (parentOrigin) {
|
|
119
|
+
return parentOrigin;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
var context = readWorkspaceContext();
|
|
123
|
+
if (context.parentOrigin) {
|
|
124
|
+
parentOrigin = context.parentOrigin;
|
|
125
|
+
return parentOrigin;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (typeof bootstrapConfig.parentOrigin === "string" && bootstrapConfig.parentOrigin) {
|
|
129
|
+
parentOrigin = bootstrapConfig.parentOrigin;
|
|
130
|
+
return parentOrigin;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (document.referrer) {
|
|
134
|
+
try {
|
|
135
|
+
parentOrigin = new URL(document.referrer).origin;
|
|
136
|
+
} catch (_error) {
|
|
137
|
+
parentOrigin = window.location.origin;
|
|
138
|
+
}
|
|
139
|
+
} else {
|
|
140
|
+
parentOrigin = window.location.origin;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return parentOrigin;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function readWorkspaceContext() {
|
|
147
|
+
var currentUrl;
|
|
148
|
+
|
|
149
|
+
try {
|
|
150
|
+
currentUrl = new URL(window.location.href);
|
|
151
|
+
} catch (_error) {
|
|
152
|
+
currentUrl = null;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
var workspaceId = typeof bootstrapConfig.workspaceId === "string" && bootstrapConfig.workspaceId
|
|
156
|
+
? bootstrapConfig.workspaceId
|
|
157
|
+
: currentUrl ? currentUrl.searchParams.get("workspaceId") : null;
|
|
158
|
+
var previewPath = currentUrl ? currentUrl.pathname : "";
|
|
159
|
+
var previewToken = "";
|
|
160
|
+
var parentOriginFromQuery = currentUrl
|
|
161
|
+
? currentUrl.searchParams.get("_cns_parent_origin") || ""
|
|
162
|
+
: "";
|
|
163
|
+
|
|
164
|
+
if (previewPath.indexOf("/preview/files/") === 0) {
|
|
165
|
+
var prefix = "/preview/files/";
|
|
166
|
+
var remaining = previewPath.slice(prefix.length);
|
|
167
|
+
previewToken = remaining.split("/")[0] || "";
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (!workspaceId && previewToken) {
|
|
171
|
+
workspaceId = readWorkspaceIdFromPreviewToken(previewToken);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return {
|
|
175
|
+
workspaceId: workspaceId || "",
|
|
176
|
+
previewToken: previewToken,
|
|
177
|
+
hostOrigin: window.location.origin,
|
|
178
|
+
parentOrigin: parentOriginFromQuery
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function readWorkspaceIdFromPreviewToken(token) {
|
|
183
|
+
var payload = token.split(".")[0];
|
|
184
|
+
if (!payload) {
|
|
185
|
+
return "";
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
try {
|
|
189
|
+
var jsonText = decodeBase64Url(payload);
|
|
190
|
+
var parsed = JSON.parse(jsonText);
|
|
191
|
+
return parsed && typeof parsed.workspaceId === "string" ? parsed.workspaceId : "";
|
|
192
|
+
} catch (_error) {
|
|
193
|
+
return "";
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function decodeBase64Url(input) {
|
|
198
|
+
var normalized = input.replace(/-/g, "+").replace(/_/g, "/");
|
|
199
|
+
var remainder = normalized.length % 4;
|
|
200
|
+
|
|
201
|
+
if (remainder === 2) {
|
|
202
|
+
normalized += "==";
|
|
203
|
+
} else if (remainder === 3) {
|
|
204
|
+
normalized += "=";
|
|
205
|
+
} else if (remainder === 1) {
|
|
206
|
+
return "";
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
try {
|
|
210
|
+
return decodeURIComponent(escape(window.atob(normalized)));
|
|
211
|
+
} catch (_error) {
|
|
212
|
+
return "";
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function buildApiUrl(pathname, params) {
|
|
217
|
+
var url = new URL(pathname, window.location.origin);
|
|
218
|
+
if (params) {
|
|
219
|
+
Object.keys(params).forEach(function (key) {
|
|
220
|
+
var value = params[key];
|
|
221
|
+
if (value !== undefined && value !== null && value !== "") {
|
|
222
|
+
url.searchParams.set(key, String(value));
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
return url.toString();
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function ensureWorkspaceId(context) {
|
|
230
|
+
if (!context.workspaceId) {
|
|
231
|
+
throw new Error("当前 HTML 预览缺少 workspaceId,无法使用 CodingNSWorkspace");
|
|
232
|
+
}
|
|
233
|
+
return context.workspaceId;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function requestHost(action, payload) {
|
|
237
|
+
var context = readWorkspaceContext();
|
|
238
|
+
|
|
239
|
+
// 预览 token 已经能安全限定 workspace。静态 HTML 预览页直接走同源 HTTP,
|
|
240
|
+
// 不再先撞 Tauri postMessage 这条在 macOS WebView 中可能沉默超时的链路。
|
|
241
|
+
if (context.previewToken) {
|
|
242
|
+
debugLog("preview-token-http-first", {
|
|
243
|
+
action: action,
|
|
244
|
+
payload: payload || {}
|
|
245
|
+
});
|
|
246
|
+
return requestHttp(action, payload);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
if (iframeHostBridge && !iframeBridgeUnavailable) {
|
|
250
|
+
return iframeHostBridge.request(action, payload).catch(function (error) {
|
|
251
|
+
if (!shouldFallbackToHttp(error)) {
|
|
252
|
+
debugLog("iframe-request-error-no-fallback", {
|
|
253
|
+
action: action,
|
|
254
|
+
message: error && error.message ? error.message : String(error || ""),
|
|
255
|
+
code: error && error.code ? error.code : null
|
|
256
|
+
});
|
|
257
|
+
throw error;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
iframeBridgeUnavailable = true;
|
|
261
|
+
debugState.iframeBridgeUnavailable = true;
|
|
262
|
+
debugState.lastHttpFallbackAction = action;
|
|
263
|
+
debugLog("iframe-timeout-fallback-http", {
|
|
264
|
+
action: action,
|
|
265
|
+
payload: payload || {},
|
|
266
|
+
message: error.message || ""
|
|
267
|
+
});
|
|
268
|
+
return requestHttp(action, payload);
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
if (iframeHostBridge && iframeBridgeUnavailable) {
|
|
273
|
+
debugLog("iframe-skipped-http-fallback", {
|
|
274
|
+
action: action,
|
|
275
|
+
payload: payload || {}
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
return requestHttp(action, payload);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function shouldFallbackToHttp(error) {
|
|
283
|
+
var message = error && typeof error.message === "string" ? error.message : "";
|
|
284
|
+
return message.indexOf("等待宿主 workspace bridge 响应超时") >= 0;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function requestHttp(action, payload) {
|
|
288
|
+
var context = readWorkspaceContext();
|
|
289
|
+
var workspaceId = ensureWorkspaceId(context);
|
|
290
|
+
var usePreviewBridge = Boolean(context.previewToken);
|
|
291
|
+
var requestBody = usePreviewBridge
|
|
292
|
+
? Object.assign({}, payload || {})
|
|
293
|
+
: Object.assign({ workspaceId: workspaceId }, payload || {});
|
|
294
|
+
var requestConfig;
|
|
295
|
+
|
|
296
|
+
function bridgeUrl(pathname, params) {
|
|
297
|
+
if (usePreviewBridge) {
|
|
298
|
+
return buildApiUrl("/preview/workspace-bridge" + pathname, Object.assign({
|
|
299
|
+
token: context.previewToken
|
|
300
|
+
}, params || {}));
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
return buildApiUrl("/api/files/workspace-bridge" + pathname, Object.assign({
|
|
304
|
+
workspaceId: workspaceId
|
|
305
|
+
}, params || {}));
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function bridgePost(pathname, body) {
|
|
309
|
+
var url = bridgeUrl(pathname);
|
|
310
|
+
debugState.lastHttpRequestAction = action;
|
|
311
|
+
debugState.lastHttpRequestUrl = url;
|
|
312
|
+
debugLog("http-request", {
|
|
313
|
+
action: action,
|
|
314
|
+
url: url,
|
|
315
|
+
previewTokenMode: usePreviewBridge
|
|
316
|
+
});
|
|
317
|
+
return fetchJson(url, {
|
|
318
|
+
method: "POST",
|
|
319
|
+
body: JSON.stringify(body)
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
function bridgeGet(pathname, params) {
|
|
324
|
+
var url = bridgeUrl(pathname, params);
|
|
325
|
+
debugState.lastHttpRequestAction = action;
|
|
326
|
+
debugState.lastHttpRequestUrl = url;
|
|
327
|
+
debugLog("http-request", {
|
|
328
|
+
action: action,
|
|
329
|
+
url: url,
|
|
330
|
+
previewTokenMode: usePreviewBridge
|
|
331
|
+
});
|
|
332
|
+
return fetchJson(url);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
switch (action) {
|
|
336
|
+
case "capabilities":
|
|
337
|
+
return bridgeGet("/capabilities");
|
|
338
|
+
case "listDir":
|
|
339
|
+
requestConfig = requestBody;
|
|
340
|
+
return bridgePost("/list-dir", requestConfig);
|
|
341
|
+
case "readText":
|
|
342
|
+
requestConfig = requestBody;
|
|
343
|
+
return bridgePost("/read-text", requestConfig);
|
|
344
|
+
case "readTexts":
|
|
345
|
+
requestConfig = requestBody;
|
|
346
|
+
return bridgePost("/read-texts", requestConfig);
|
|
347
|
+
case "writeText":
|
|
348
|
+
requestConfig = requestBody;
|
|
349
|
+
return bridgePost("/write-text", requestConfig);
|
|
350
|
+
case "deleteFile":
|
|
351
|
+
requestConfig = requestBody;
|
|
352
|
+
return bridgePost("/delete-file", requestConfig);
|
|
353
|
+
case "stat":
|
|
354
|
+
return bridgeGet("/stat", {
|
|
355
|
+
path: payload && payload.path ? payload.path : ""
|
|
356
|
+
});
|
|
357
|
+
case "exists":
|
|
358
|
+
return bridgeGet("/exists", {
|
|
359
|
+
path: payload && payload.path ? payload.path : ""
|
|
360
|
+
});
|
|
361
|
+
case "openWorkspaceFile":
|
|
362
|
+
requestConfig = requestBody;
|
|
363
|
+
return bridgePost("/open-file", requestConfig).then(function (prepared) {
|
|
364
|
+
return runDesktopAction("openFile", prepared, "DESKTOP_OPEN_UNAVAILABLE", "当前环境不支持打开本地文件");
|
|
365
|
+
});
|
|
366
|
+
case "revealWorkspaceFile":
|
|
367
|
+
requestConfig = requestBody;
|
|
368
|
+
return bridgePost("/reveal-in-file-manager", requestConfig).then(function (prepared) {
|
|
369
|
+
return runDesktopAction("revealInFileManager", prepared, "DESKTOP_REVEAL_UNAVAILABLE", "当前环境不支持在文件管理器中定位文件");
|
|
370
|
+
});
|
|
371
|
+
case "watchDir":
|
|
372
|
+
requestConfig = requestBody;
|
|
373
|
+
return bridgePost("/watch-dir", requestConfig);
|
|
374
|
+
case "unwatch":
|
|
375
|
+
requestConfig = payload || {};
|
|
376
|
+
return bridgePost("/unwatch", requestConfig);
|
|
377
|
+
case "watchPoll":
|
|
378
|
+
return bridgeGet("/watch-events", {
|
|
379
|
+
watchId: payload && payload.watchId ? payload.watchId : "",
|
|
380
|
+
cursor: payload && typeof payload.cursor === "number" ? payload.cursor : ""
|
|
381
|
+
});
|
|
382
|
+
default:
|
|
383
|
+
return Promise.reject(new Error("不支持的 workspace bridge 动作: " + action));
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
function fetchJson(input, init) {
|
|
388
|
+
return fetch(input, Object.assign({
|
|
389
|
+
credentials: "include",
|
|
390
|
+
headers: {
|
|
391
|
+
"content-type": "application/json"
|
|
392
|
+
}
|
|
393
|
+
}, init || {})).then(function (response) {
|
|
394
|
+
return response.text().then(function (text) {
|
|
395
|
+
var body = null;
|
|
396
|
+
|
|
397
|
+
if (text) {
|
|
398
|
+
try {
|
|
399
|
+
body = JSON.parse(text);
|
|
400
|
+
} catch (parseError) {
|
|
401
|
+
debugLog("http-response-parse-error", {
|
|
402
|
+
url: typeof input === "string" ? input : "",
|
|
403
|
+
status: response.status,
|
|
404
|
+
statusText: response.statusText,
|
|
405
|
+
bodyPreview: text.slice(0, 500),
|
|
406
|
+
message: parseError && parseError.message ? parseError.message : String(parseError || "")
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
debugLog("http-response", {
|
|
412
|
+
url: typeof input === "string" ? input : "",
|
|
413
|
+
ok: response.ok,
|
|
414
|
+
status: response.status,
|
|
415
|
+
statusText: response.statusText,
|
|
416
|
+
errorCode: body && body.error_code ? body.error_code : null,
|
|
417
|
+
detail: body && body.detail ? body.detail : null,
|
|
418
|
+
bodyPreview: body ? null : text.slice(0, 500)
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
if (!response.ok) {
|
|
422
|
+
var error = new Error(body && body.detail ? body.detail : "请求失败 HTTP " + String(response.status));
|
|
423
|
+
error.code = body && body.error_code ? body.error_code : "HTTP_" + String(response.status);
|
|
424
|
+
error.status = response.status;
|
|
425
|
+
error.path = body && body.data && typeof body.data.path === "string" ? body.data.path : undefined;
|
|
426
|
+
error.bodyPreview = body ? undefined : text.slice(0, 500);
|
|
427
|
+
throw error;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
if (!body) {
|
|
431
|
+
var invalidJsonError = new Error("响应不是有效 JSON");
|
|
432
|
+
invalidJsonError.code = "INVALID_JSON_RESPONSE";
|
|
433
|
+
invalidJsonError.status = response.status;
|
|
434
|
+
invalidJsonError.bodyPreview = text.slice(0, 500);
|
|
435
|
+
throw invalidJsonError;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
return body;
|
|
439
|
+
});
|
|
440
|
+
});
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
function createIframeHostBridge() {
|
|
444
|
+
if (!isIframe()) {
|
|
445
|
+
return null;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
window.addEventListener("message", function (event) {
|
|
449
|
+
var payload = event.data;
|
|
450
|
+
debugState.lastEventType = payload && typeof payload === "object" && typeof payload.type === "string"
|
|
451
|
+
? payload.type
|
|
452
|
+
: null;
|
|
453
|
+
debugState.lastResponseOrigin = event.origin || null;
|
|
454
|
+
debugLog("message", {
|
|
455
|
+
origin: event.origin,
|
|
456
|
+
expectedOrigin: ensureParentOrigin(),
|
|
457
|
+
fromParent: event.source === window.parent,
|
|
458
|
+
type: payload && payload.type ? payload.type : null
|
|
459
|
+
});
|
|
460
|
+
|
|
461
|
+
if (event.source !== window.parent) {
|
|
462
|
+
debugState.lastDropReason = "message source is not parent";
|
|
463
|
+
debugLog("drop-message", {
|
|
464
|
+
reason: debugState.lastDropReason,
|
|
465
|
+
origin: event.origin,
|
|
466
|
+
expectedOrigin: ensureParentOrigin(),
|
|
467
|
+
type: payload && payload.type ? payload.type : null
|
|
468
|
+
});
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
if (event.origin !== ensureParentOrigin()) {
|
|
473
|
+
debugState.lastDropReason = "origin mismatch";
|
|
474
|
+
debugLog("drop-message", {
|
|
475
|
+
reason: debugState.lastDropReason,
|
|
476
|
+
origin: event.origin,
|
|
477
|
+
expectedOrigin: ensureParentOrigin(),
|
|
478
|
+
type: payload && payload.type ? payload.type : null
|
|
479
|
+
});
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
if (!payload || typeof payload !== "object") {
|
|
484
|
+
debugState.lastDropReason = "payload is not object";
|
|
485
|
+
debugLog("drop-message", {
|
|
486
|
+
reason: debugState.lastDropReason,
|
|
487
|
+
origin: event.origin
|
|
488
|
+
});
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
if (payload.type === DEBUG_TYPE) {
|
|
493
|
+
debugLog("host-debug", {
|
|
494
|
+
origin: event.origin,
|
|
495
|
+
detail: payload.payload || null
|
|
496
|
+
});
|
|
497
|
+
return;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
if (payload.type === EVENT_TYPE && typeof payload.watchId === "string") {
|
|
501
|
+
debugLog("watch-event", {
|
|
502
|
+
watchId: payload.watchId,
|
|
503
|
+
origin: event.origin
|
|
504
|
+
});
|
|
505
|
+
dispatchWatchEvent(payload.watchId, payload.payload);
|
|
506
|
+
return;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
if (payload.type !== RESPONSE_TYPE || typeof payload.id !== "string") {
|
|
510
|
+
debugState.lastDropReason = "not response payload";
|
|
511
|
+
debugLog("drop-message", {
|
|
512
|
+
reason: debugState.lastDropReason,
|
|
513
|
+
origin: event.origin,
|
|
514
|
+
type: payload.type || null,
|
|
515
|
+
id: payload.id || null
|
|
516
|
+
});
|
|
517
|
+
return;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
var pending = pendingRequests[payload.id];
|
|
521
|
+
if (!pending) {
|
|
522
|
+
debugState.lastDropReason = "no pending request for response";
|
|
523
|
+
debugLog("drop-response", {
|
|
524
|
+
reason: debugState.lastDropReason,
|
|
525
|
+
id: payload.id,
|
|
526
|
+
origin: event.origin,
|
|
527
|
+
pendingRequestIds: Object.keys(pendingRequests)
|
|
528
|
+
});
|
|
529
|
+
return;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
delete pendingRequests[payload.id];
|
|
533
|
+
updatePendingRequestCount();
|
|
534
|
+
window.clearTimeout(pending.timer);
|
|
535
|
+
debugState.lastResponseId = payload.id;
|
|
536
|
+
debugState.lastResponseAccepted = true;
|
|
537
|
+
|
|
538
|
+
debugLog("response-accepted", {
|
|
539
|
+
id: payload.id,
|
|
540
|
+
ok: payload.ok !== false,
|
|
541
|
+
origin: event.origin
|
|
542
|
+
});
|
|
543
|
+
|
|
544
|
+
if (payload.ok === false) {
|
|
545
|
+
var bridgeError = new Error(payload.error && payload.error.message ? payload.error.message : "宿主桥调用失败");
|
|
546
|
+
bridgeError.code = payload.error && payload.error.code ? payload.error.code : "INTERNAL_ERROR";
|
|
547
|
+
bridgeError.path = payload.error && payload.error.path ? payload.error.path : undefined;
|
|
548
|
+
pending.reject(bridgeError);
|
|
549
|
+
return;
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
pending.resolve(payload.payload);
|
|
553
|
+
});
|
|
554
|
+
|
|
555
|
+
return {
|
|
556
|
+
request: function (action, payload) {
|
|
557
|
+
return new Promise(function (resolve, reject) {
|
|
558
|
+
requestSeq += 1;
|
|
559
|
+
var requestId = "workspace-bridge-" + String(Date.now()) + "-" + String(requestSeq);
|
|
560
|
+
var targetOrigin = ensureParentOrigin();
|
|
561
|
+
pendingRequests[requestId] = {
|
|
562
|
+
resolve: resolve,
|
|
563
|
+
reject: reject,
|
|
564
|
+
timer: window.setTimeout(function () {
|
|
565
|
+
delete pendingRequests[requestId];
|
|
566
|
+
updatePendingRequestCount();
|
|
567
|
+
debugState.lastTimeoutRequestId = requestId;
|
|
568
|
+
debugState.lastTimeoutAction = action;
|
|
569
|
+
debugState.lastTimeoutTargetOrigin = targetOrigin;
|
|
570
|
+
debugLog("request-timeout", {
|
|
571
|
+
requestId: requestId,
|
|
572
|
+
action: action,
|
|
573
|
+
targetOrigin: targetOrigin,
|
|
574
|
+
pendingRequestIds: Object.keys(pendingRequests),
|
|
575
|
+
lastResponseId: debugState.lastResponseId,
|
|
576
|
+
lastResponseOrigin: debugState.lastResponseOrigin,
|
|
577
|
+
lastDropReason: debugState.lastDropReason
|
|
578
|
+
});
|
|
579
|
+
var timeoutError = new Error("等待宿主 workspace bridge 响应超时");
|
|
580
|
+
timeoutError.code = "INTERNAL_ERROR";
|
|
581
|
+
reject(timeoutError);
|
|
582
|
+
}, REQUEST_TIMEOUT_MS)
|
|
583
|
+
};
|
|
584
|
+
updatePendingRequestCount();
|
|
585
|
+
debugState.lastRequestId = requestId;
|
|
586
|
+
debugState.lastRequestAction = action;
|
|
587
|
+
debugState.lastRequestTargetOrigin = targetOrigin;
|
|
588
|
+
debugLog("request", {
|
|
589
|
+
requestId: requestId,
|
|
590
|
+
action: action,
|
|
591
|
+
targetOrigin: targetOrigin,
|
|
592
|
+
payload: payload || {}
|
|
593
|
+
});
|
|
594
|
+
|
|
595
|
+
window.parent.postMessage({
|
|
596
|
+
type: REQUEST_TYPE,
|
|
597
|
+
id: requestId,
|
|
598
|
+
action: action,
|
|
599
|
+
payload: payload || {}
|
|
600
|
+
}, targetOrigin);
|
|
601
|
+
});
|
|
602
|
+
}
|
|
603
|
+
};
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
function createBridgeError(code, message, path) {
|
|
607
|
+
var error = new Error(message);
|
|
608
|
+
error.code = code;
|
|
609
|
+
error.path = path;
|
|
610
|
+
return error;
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
function getDesktopFsBridge() {
|
|
614
|
+
var desktop = window.CodingNSDesktop;
|
|
615
|
+
if (!desktop || !desktop.fs) {
|
|
616
|
+
return null;
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
return desktop.fs;
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
function runDesktopAction(actionName, prepared, unavailableCode, unavailableMessage) {
|
|
623
|
+
if (!prepared || typeof prepared !== "object" || typeof prepared.absolutePath !== "string") {
|
|
624
|
+
return Promise.reject(createBridgeError("INTERNAL_ERROR", "桌面动作预处理结果无效"));
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
var fsBridge = getDesktopFsBridge();
|
|
628
|
+
if (!fsBridge || typeof fsBridge[actionName] !== "function") {
|
|
629
|
+
return Promise.reject(createBridgeError(unavailableCode, unavailableMessage, prepared.relativePath));
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
return fsBridge[actionName](prepared.absolutePath).then(function (result) {
|
|
633
|
+
if (!result || result.ok !== true) {
|
|
634
|
+
throw createBridgeError(
|
|
635
|
+
result && result.errorCode ? result.errorCode : "INTERNAL_ERROR",
|
|
636
|
+
result && result.detail ? result.detail : unavailableMessage,
|
|
637
|
+
prepared.relativePath
|
|
638
|
+
);
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
return prepared;
|
|
642
|
+
});
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
function dispatchWatchEvent(watchId, payload) {
|
|
646
|
+
var listeners = watchListeners[watchId];
|
|
647
|
+
if (!listeners || listeners.length === 0) {
|
|
648
|
+
return;
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
listeners.slice().forEach(function (listener) {
|
|
652
|
+
try {
|
|
653
|
+
listener(payload);
|
|
654
|
+
} catch (_error) {
|
|
655
|
+
// ignore listener error
|
|
656
|
+
}
|
|
657
|
+
});
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
function ensureWatchListenerBucket(watchId) {
|
|
661
|
+
if (!watchListeners[watchId]) {
|
|
662
|
+
watchListeners[watchId] = [];
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
return watchListeners[watchId];
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
function addWatchListener(watchId, callback) {
|
|
669
|
+
if (typeof callback !== "function") {
|
|
670
|
+
return;
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
ensureWatchListenerBucket(watchId).push(callback);
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
function removeWatchListener(watchId, callback) {
|
|
677
|
+
var listeners = watchListeners[watchId];
|
|
678
|
+
if (!listeners || listeners.length === 0) {
|
|
679
|
+
return;
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
watchListeners[watchId] = listeners.filter(function (item) {
|
|
683
|
+
return item !== callback;
|
|
684
|
+
});
|
|
685
|
+
|
|
686
|
+
if (watchListeners[watchId].length === 0) {
|
|
687
|
+
delete watchListeners[watchId];
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
function startDirectWatchPolling(watchId) {
|
|
692
|
+
if (iframeHostBridge || activeWatchPollers[watchId]) {
|
|
693
|
+
return;
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
var state = {
|
|
697
|
+
stopped: false,
|
|
698
|
+
cursor: 0
|
|
699
|
+
};
|
|
700
|
+
activeWatchPollers[watchId] = state;
|
|
701
|
+
|
|
702
|
+
function loop() {
|
|
703
|
+
if (state.stopped) {
|
|
704
|
+
delete activeWatchPollers[watchId];
|
|
705
|
+
return;
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
requestHost("watchPoll", {
|
|
709
|
+
watchId: watchId,
|
|
710
|
+
cursor: state.cursor
|
|
711
|
+
}).then(function (result) {
|
|
712
|
+
if (!result || typeof result !== "object") {
|
|
713
|
+
return;
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
if (typeof result.nextCursor === "number") {
|
|
717
|
+
state.cursor = result.nextCursor;
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
if (Array.isArray(result.events)) {
|
|
721
|
+
result.events.forEach(function (eventPayload) {
|
|
722
|
+
dispatchWatchEvent(watchId, eventPayload);
|
|
723
|
+
});
|
|
724
|
+
}
|
|
725
|
+
}).catch(function (error) {
|
|
726
|
+
if (error && error.code === "WATCH_NOT_FOUND") {
|
|
727
|
+
state.stopped = true;
|
|
728
|
+
}
|
|
729
|
+
}).finally(function () {
|
|
730
|
+
if (!state.stopped) {
|
|
731
|
+
window.setTimeout(loop, WATCH_POLL_INTERVAL_MS);
|
|
732
|
+
} else {
|
|
733
|
+
delete activeWatchPollers[watchId];
|
|
734
|
+
}
|
|
735
|
+
});
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
loop();
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
function stopDirectWatchPolling(watchId) {
|
|
742
|
+
if (!activeWatchPollers[watchId]) {
|
|
743
|
+
return;
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
activeWatchPollers[watchId].stopped = true;
|
|
747
|
+
delete activeWatchPollers[watchId];
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
function createWatchHandle(watchId, callback) {
|
|
751
|
+
return {
|
|
752
|
+
watchId: watchId,
|
|
753
|
+
unsubscribe: function () {
|
|
754
|
+
if (typeof callback === "function") {
|
|
755
|
+
removeWatchListener(watchId, callback);
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
stopDirectWatchPolling(watchId);
|
|
759
|
+
return requestHost("unwatch", {
|
|
760
|
+
watchId: watchId
|
|
761
|
+
}).catch(function (error) {
|
|
762
|
+
if (error && error.code === "WATCH_NOT_FOUND") {
|
|
763
|
+
return {
|
|
764
|
+
ok: true,
|
|
765
|
+
watchId: watchId
|
|
766
|
+
};
|
|
767
|
+
}
|
|
768
|
+
throw error;
|
|
769
|
+
});
|
|
770
|
+
}
|
|
771
|
+
};
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
var workspace = {
|
|
775
|
+
capabilities: function () {
|
|
776
|
+
return requestHost("capabilities");
|
|
777
|
+
},
|
|
778
|
+
requestPermission: function (_input) {
|
|
779
|
+
return Promise.resolve({
|
|
780
|
+
ok: true,
|
|
781
|
+
granted: true,
|
|
782
|
+
scope: "workspace"
|
|
783
|
+
});
|
|
784
|
+
},
|
|
785
|
+
listDir: function (relativePath, options) {
|
|
786
|
+
return requestHost("listDir", {
|
|
787
|
+
path: typeof relativePath === "string" ? relativePath : "",
|
|
788
|
+
options: options || {}
|
|
789
|
+
});
|
|
790
|
+
},
|
|
791
|
+
readText: function (relativePath, options) {
|
|
792
|
+
return requestHost("readText", {
|
|
793
|
+
path: typeof relativePath === "string" ? relativePath : "",
|
|
794
|
+
options: options || {}
|
|
795
|
+
});
|
|
796
|
+
},
|
|
797
|
+
readTexts: function (paths, options) {
|
|
798
|
+
return requestHost("readTexts", {
|
|
799
|
+
paths: Array.isArray(paths) ? paths : [],
|
|
800
|
+
options: options || {}
|
|
801
|
+
});
|
|
802
|
+
},
|
|
803
|
+
writeText: function (relativePath, content, options) {
|
|
804
|
+
return requestHost("writeText", {
|
|
805
|
+
path: typeof relativePath === "string" ? relativePath : "",
|
|
806
|
+
content: typeof content === "string" ? content : "",
|
|
807
|
+
options: options || {}
|
|
808
|
+
});
|
|
809
|
+
},
|
|
810
|
+
writeTexts: function () {
|
|
811
|
+
return Promise.reject(createBridgeError("INTERNAL_ERROR", "当前版本暂不支持批量写入"));
|
|
812
|
+
},
|
|
813
|
+
deleteFile: function (relativePath, options) {
|
|
814
|
+
return requestHost("deleteFile", {
|
|
815
|
+
path: typeof relativePath === "string" ? relativePath : "",
|
|
816
|
+
options: options || {}
|
|
817
|
+
});
|
|
818
|
+
},
|
|
819
|
+
stat: function (relativePath) {
|
|
820
|
+
return requestHost("stat", {
|
|
821
|
+
path: typeof relativePath === "string" ? relativePath : ""
|
|
822
|
+
});
|
|
823
|
+
},
|
|
824
|
+
exists: function (relativePath) {
|
|
825
|
+
return requestHost("exists", {
|
|
826
|
+
path: typeof relativePath === "string" ? relativePath : ""
|
|
827
|
+
});
|
|
828
|
+
},
|
|
829
|
+
openWorkspaceFile: function (relativePath) {
|
|
830
|
+
return requestHost("openWorkspaceFile", {
|
|
831
|
+
path: typeof relativePath === "string" ? relativePath : ""
|
|
832
|
+
});
|
|
833
|
+
},
|
|
834
|
+
revealWorkspaceFile: function (relativePath) {
|
|
835
|
+
return requestHost("revealWorkspaceFile", {
|
|
836
|
+
path: typeof relativePath === "string" ? relativePath : ""
|
|
837
|
+
});
|
|
838
|
+
},
|
|
839
|
+
watchDir: function (relativePath, options, callback) {
|
|
840
|
+
var resolvedOptions = options;
|
|
841
|
+
var resolvedCallback = callback;
|
|
842
|
+
|
|
843
|
+
if (typeof options === "function") {
|
|
844
|
+
resolvedCallback = options;
|
|
845
|
+
resolvedOptions = {};
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
return requestHost("watchDir", {
|
|
849
|
+
path: typeof relativePath === "string" ? relativePath : "",
|
|
850
|
+
options: resolvedOptions || {}
|
|
851
|
+
}).then(function (result) {
|
|
852
|
+
var watchId = result && typeof result.watchId === "string" ? result.watchId : "";
|
|
853
|
+
if (!watchId) {
|
|
854
|
+
throw createBridgeError("INTERNAL_ERROR", "目录监听返回了无效 watchId");
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
if (typeof resolvedCallback === "function") {
|
|
858
|
+
addWatchListener(watchId, resolvedCallback);
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
startDirectWatchPolling(watchId);
|
|
862
|
+
return createWatchHandle(watchId, resolvedCallback);
|
|
863
|
+
});
|
|
864
|
+
},
|
|
865
|
+
unwatch: function (watchIdOrHandle) {
|
|
866
|
+
if (watchIdOrHandle && typeof watchIdOrHandle === "object" && typeof watchIdOrHandle.unsubscribe === "function") {
|
|
867
|
+
return watchIdOrHandle.unsubscribe();
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
var watchId = typeof watchIdOrHandle === "string"
|
|
871
|
+
? watchIdOrHandle
|
|
872
|
+
: watchIdOrHandle && typeof watchIdOrHandle.watchId === "string"
|
|
873
|
+
? watchIdOrHandle.watchId
|
|
874
|
+
: "";
|
|
875
|
+
|
|
876
|
+
if (!watchId) {
|
|
877
|
+
return Promise.reject(createBridgeError("INVALID_WATCH_ID", "必须提供 watchId"));
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
stopDirectWatchPolling(watchId);
|
|
881
|
+
delete watchListeners[watchId];
|
|
882
|
+
return requestHost("unwatch", {
|
|
883
|
+
watchId: watchId
|
|
884
|
+
}).catch(function (error) {
|
|
885
|
+
if (error && error.code === "WATCH_NOT_FOUND") {
|
|
886
|
+
return {
|
|
887
|
+
ok: true,
|
|
888
|
+
watchId: watchId
|
|
889
|
+
};
|
|
890
|
+
}
|
|
891
|
+
throw error;
|
|
892
|
+
});
|
|
893
|
+
},
|
|
894
|
+
bridgeProtocol: {
|
|
895
|
+
requestType: REQUEST_TYPE,
|
|
896
|
+
responseType: RESPONSE_TYPE,
|
|
897
|
+
eventType: EVENT_TYPE,
|
|
898
|
+
debugType: DEBUG_TYPE,
|
|
899
|
+
runtimeVersion: typeof bootstrapConfig.runtimeVersion === "string" ? bootstrapConfig.runtimeVersion : "",
|
|
900
|
+
parentOrigin: isIframe() ? ensureParentOrigin() : null
|
|
901
|
+
},
|
|
902
|
+
debug: debugState,
|
|
903
|
+
debugEvents: debugEvents
|
|
904
|
+
};
|
|
905
|
+
|
|
906
|
+
Object.defineProperty(window, "CodingNSWorkspace", {
|
|
907
|
+
configurable: true,
|
|
908
|
+
writable: false,
|
|
909
|
+
value: workspace
|
|
910
|
+
});
|
|
911
|
+
})();
|