@lapis-notes/api 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/LICENSE.md +4 -0
- package/README.md +43 -0
- package/dist/__tests__/data-adapter-conformance.js +205 -0
- package/dist/__tests__/lapis-runtime-manifest.js +32 -0
- package/dist/__tests__/svelte-internal-client.d.ts +3 -0
- package/dist/agent-result-views.d.ts +52 -0
- package/dist/agent-result-views.js +89 -0
- package/dist/agent-skills.d.ts +99 -0
- package/dist/agent-skills.js +182 -0
- package/dist/agent-tools.d.ts +92 -0
- package/dist/agent-tools.js +139 -0
- package/dist/app-database-host.d.ts +4 -0
- package/dist/app-database-host.js +4 -0
- package/dist/app-shell-plugin-persistence.d.ts +15 -0
- package/dist/app-shell-plugin-persistence.js +34 -0
- package/dist/app-url.d.ts +54 -0
- package/dist/app-url.js +269 -0
- package/dist/application-compatibility.d.ts +31 -0
- package/dist/application-compatibility.js +64 -0
- package/dist/application-state.svelte.d.ts +14 -0
- package/dist/application-state.svelte.js +30 -0
- package/dist/assets/lapis-logo.svg +471 -0
- package/dist/bases.d.ts +281 -0
- package/dist/bases.js +475 -0
- package/dist/cache.svelte.d.ts +569 -0
- package/dist/cache.svelte.js +1433 -0
- package/dist/command.svelte.d.ts +337 -0
- package/dist/command.svelte.js +571 -0
- package/dist/compat.d.ts +29 -0
- package/dist/compat.js +30 -0
- package/dist/components/configuration/configuration.css +199 -0
- package/dist/components/configuration/date-setting.svelte +71 -0
- package/dist/components/configuration/date-setting.svelte.d.ts +8 -0
- package/dist/components/configuration/list-setting.svelte +128 -0
- package/dist/components/configuration/list-setting.svelte.d.ts +13 -0
- package/dist/components/configuration/object-array-row-grip.svelte +78 -0
- package/dist/components/configuration/object-array-row-grip.svelte.d.ts +9 -0
- package/dist/components/configuration/object-array-row.svelte +163 -0
- package/dist/components/configuration/object-array-row.svelte.d.ts +19 -0
- package/dist/components/configuration/object-array-setting.svelte +196 -0
- package/dist/components/configuration/object-array-setting.svelte.d.ts +15 -0
- package/dist/components/configuration/object-array-table-dnd.d.ts +8 -0
- package/dist/components/configuration/object-array-table-dnd.js +10 -0
- package/dist/components/configuration/object-array-utils.d.ts +11 -0
- package/dist/components/configuration/object-array-utils.js +103 -0
- package/dist/components/configuration/object-grid-setting.svelte +79 -0
- package/dist/components/configuration/object-grid-setting.svelte.d.ts +10 -0
- package/dist/components/configuration/object-map-row.svelte +147 -0
- package/dist/components/configuration/object-map-row.svelte.d.ts +15 -0
- package/dist/components/configuration/object-map-setting.svelte +137 -0
- package/dist/components/configuration/object-map-setting.svelte.d.ts +14 -0
- package/dist/components/configuration/object-map-types.d.ts +5 -0
- package/dist/components/configuration/object-map-types.js +1 -0
- package/dist/components/configuration/options-combobox-setting.svelte +129 -0
- package/dist/components/configuration/options-combobox-setting.svelte.d.ts +12 -0
- package/dist/components/copy-button/button.svelte +93 -0
- package/dist/components/copy-button/button.svelte.d.ts +13 -0
- package/dist/components/copy-button/index.d.ts +2 -0
- package/dist/components/copy-button/index.js +2 -0
- package/dist/components/editor/editor.css +565 -0
- package/dist/components/editor/editor.d.ts +33 -0
- package/dist/components/editor/editor.js +132 -0
- package/dist/components/editor/editor.svelte +176 -0
- package/dist/components/editor/editor.svelte.d.ts +15 -0
- package/dist/components/editor/embedded-editor-surface.d.ts +12 -0
- package/dist/components/editor/embedded-editor-surface.js +22 -0
- package/dist/components/editor/embedded-editor-surface.svelte +115 -0
- package/dist/components/editor/embedded-editor-surface.svelte.d.ts +17 -0
- package/dist/components/editor/extensions/autocomplete/index.d.ts +2 -0
- package/dist/components/editor/extensions/autocomplete/index.js +2 -0
- package/dist/components/editor/extensions/autocomplete/lapis-code-mirror-autocomplete.d.ts +16 -0
- package/dist/components/editor/extensions/autocomplete/lapis-code-mirror-autocomplete.js +34 -0
- package/dist/components/editor/extensions/autocomplete/vault-completion-sources.d.ts +27 -0
- package/dist/components/editor/extensions/autocomplete/vault-completion-sources.js +55 -0
- package/dist/components/editor/extensions/class-highlighter/index.d.ts +1 -0
- package/dist/components/editor/extensions/class-highlighter/index.js +47 -0
- package/dist/components/editor/extensions/fold-gutter/index.d.ts +8 -0
- package/dist/components/editor/extensions/fold-gutter/index.js +40 -0
- package/dist/components/editor/extensions/gutter-line-style-sync/index.d.ts +2 -0
- package/dist/components/editor/extensions/gutter-line-style-sync/index.js +118 -0
- package/dist/components/editor/extensions/lint/index.d.ts +4 -0
- package/dist/components/editor/extensions/lint/index.js +4 -0
- package/dist/components/editor/extensions/lint/lapis-code-mirror-lint.d.ts +26 -0
- package/dist/components/editor/extensions/lint/lapis-code-mirror-lint.js +101 -0
- package/dist/components/editor/extensions/lint/lapis-lint-diagnostic-helpers.d.ts +8 -0
- package/dist/components/editor/extensions/lint/lapis-lint-diagnostic-helpers.js +7 -0
- package/dist/components/editor/extensions/lint/lapis-lint-diagnostic.d.ts +20 -0
- package/dist/components/editor/extensions/lint/lapis-lint-diagnostic.js +47 -0
- package/dist/components/editor/extensions/lint/lapis-lint-hover-tooltip.d.ts +9 -0
- package/dist/components/editor/extensions/lint/lapis-lint-hover-tooltip.js +373 -0
- package/dist/components/editor/extensions/lint/lapis-lint-inline-widget.d.ts +31 -0
- package/dist/components/editor/extensions/lint/lapis-lint-inline-widget.js +191 -0
- package/dist/components/editor/extensions/lint/lapis-lint-tooltip.svelte +176 -0
- package/dist/components/editor/extensions/lint/lapis-lint-tooltip.svelte.d.ts +15 -0
- package/dist/components/editor/extensions/lint/lint-tooltip-actions.d.ts +11 -0
- package/dist/components/editor/extensions/lint/lint-tooltip-actions.js +21 -0
- package/dist/components/editor/extensions/lint/mount-lint-tooltip.d.ts +16 -0
- package/dist/components/editor/extensions/lint/mount-lint-tooltip.js +60 -0
- package/dist/components/editor/extensions/search/dom.d.ts +2 -0
- package/dist/components/editor/extensions/search/dom.js +6 -0
- package/dist/components/editor/extensions/search/index.d.ts +35 -0
- package/dist/components/editor/extensions/search/index.js +44 -0
- package/dist/components/editor/extensions/search/search.svelte +335 -0
- package/dist/components/editor/extensions/search/search.svelte.d.ts +8 -0
- package/dist/components/editor/index.d.ts +9 -0
- package/dist/components/editor/index.js +9 -0
- package/dist/components/editor/language-service/dom.d.ts +1 -0
- package/dist/components/editor/language-service/dom.js +6 -0
- package/dist/components/editor/language-service/index.d.ts +28 -0
- package/dist/components/editor/language-service/index.js +342 -0
- package/dist/components/empty-view/empty-view.css +39 -0
- package/dist/components/empty-view/empty-view.svelte +26 -0
- package/dist/components/empty-view/empty-view.svelte.d.ts +9 -0
- package/dist/components/empty-view/index.d.ts +2 -0
- package/dist/components/empty-view/index.js +2 -0
- package/dist/components/icon/icon-list.css +74 -0
- package/dist/components/icon/icon-list.svelte +213 -0
- package/dist/components/icon/icon-list.svelte.d.ts +11 -0
- package/dist/components/icon/icon.svelte +15 -0
- package/dist/components/icon/icon.svelte.d.ts +7 -0
- package/dist/components/icon/index.d.ts +3 -0
- package/dist/components/icon/index.js +3 -0
- package/dist/components/menu/context-menu.svelte +89 -0
- package/dist/components/menu/context-menu.svelte.d.ts +11 -0
- package/dist/components/menu/drawer-menu.svelte +83 -0
- package/dist/components/menu/drawer-menu.svelte.d.ts +8 -0
- package/dist/components/menu/dropdown-menu.svelte +89 -0
- package/dist/components/menu/dropdown-menu.svelte.d.ts +11 -0
- package/dist/components/menu/index.d.ts +6 -0
- package/dist/components/menu/index.js +6 -0
- package/dist/components/menu/menu.css +139 -0
- package/dist/components/menu/menu.svelte +37 -0
- package/dist/components/menu/menu.svelte.d.ts +11 -0
- package/dist/components/menu/mobile-menu-query.svelte.d.ts +3 -0
- package/dist/components/menu/mobile-menu-query.svelte.js +21 -0
- package/dist/components/menu/popover-menu.svelte +90 -0
- package/dist/components/menu/popover-menu.svelte.d.ts +10 -0
- package/dist/components/select/index.d.ts +2 -0
- package/dist/components/select/index.js +2 -0
- package/dist/components/select/select.svelte +72 -0
- package/dist/components/select/select.svelte.d.ts +25 -0
- package/dist/configuration-option-source-providers.d.ts +17 -0
- package/dist/configuration-option-source-providers.js +59 -0
- package/dist/configuration-option-source-registry.d.ts +59 -0
- package/dist/configuration-option-source-registry.js +123 -0
- package/dist/configuration.svelte.d.ts +258 -0
- package/dist/configuration.svelte.js +887 -0
- package/dist/context-keys.svelte.d.ts +42 -0
- package/dist/context-keys.svelte.js +278 -0
- package/dist/context.svelte.d.ts +247 -0
- package/dist/context.svelte.js +540 -0
- package/dist/daily-document-provider.d.ts +38 -0
- package/dist/daily-document-provider.js +176 -0
- package/dist/design-core-button-types.d.ts +7 -0
- package/dist/design-core-button-types.js +1 -0
- package/dist/diagnostics.d.ts +93 -0
- package/dist/diagnostics.js +84 -0
- package/dist/dialog-portal.d.ts +3 -0
- package/dist/dialog-portal.js +3 -0
- package/dist/docs-api.d.ts +44 -0
- package/dist/docs-api.js +29 -0
- package/dist/editor-view-registry.d.ts +35 -0
- package/dist/editor-view-registry.js +112 -0
- package/dist/editor.svelte.d.ts +126 -0
- package/dist/editor.svelte.js +454 -0
- package/dist/embed-registry.d.ts +15 -0
- package/dist/embed-registry.js +16 -0
- package/dist/enhance.d.ts +111 -0
- package/dist/enhance.js +309 -0
- package/dist/events.d.ts +28 -0
- package/dist/events.js +49 -0
- package/dist/file-manager.d.ts +90 -0
- package/dist/file-manager.js +289 -0
- package/dist/file-tools/app-tools.d.ts +3 -0
- package/dist/file-tools/app-tools.js +242 -0
- package/dist/file-tools/index.d.ts +3 -0
- package/dist/file-tools/index.js +3 -0
- package/dist/file-tools/paths.d.ts +14 -0
- package/dist/file-tools/paths.js +33 -0
- package/dist/file-tools/vault-operations.d.ts +3 -0
- package/dist/file-tools/vault-operations.js +60 -0
- package/dist/generated/plugin-host-modules.schema.generated.d.ts +371 -0
- package/dist/generated/plugin-host-modules.schema.generated.js +374 -0
- package/dist/glob.d.ts +15 -0
- package/dist/glob.js +68 -0
- package/dist/history.svelte.d.ts +24 -0
- package/dist/history.svelte.js +74 -0
- package/dist/hooks/createVirtualizer.svelte.d.ts +7 -0
- package/dist/hooks/createVirtualizer.svelte.js +70 -0
- package/dist/hooks/index.d.ts +5 -0
- package/dist/hooks/index.js +5 -0
- package/dist/hooks/is-mobile.svelte.d.ts +5 -0
- package/dist/hooks/is-mobile.svelte.js +23 -0
- package/dist/hooks/mountComponent.svelte.d.ts +7 -0
- package/dist/hooks/mountComponent.svelte.js +16 -0
- package/dist/hooks/use-text-highlight.d.ts +17 -0
- package/dist/hooks/use-text-highlight.js +82 -0
- package/dist/hooks/useBoundingRect.svelte.d.ts +11 -0
- package/dist/hooks/useBoundingRect.svelte.js +46 -0
- package/dist/icons/index.d.ts +57 -0
- package/dist/icons/index.js +288 -0
- package/dist/index.d.ts +87 -0
- package/dist/index.js +87 -0
- package/dist/language-service/index.d.ts +9 -0
- package/dist/language-service/index.js +7 -0
- package/dist/language-service/manager.d.ts +47 -0
- package/dist/language-service/manager.js +404 -0
- package/dist/language-service/types.d.ts +92 -0
- package/dist/language-service/types.js +1 -0
- package/dist/language-service/virtual-document.d.ts +11 -0
- package/dist/language-service/virtual-document.js +45 -0
- package/dist/language-service/worker-protocol.d.ts +63 -0
- package/dist/language-service/worker-protocol.js +1 -0
- package/dist/language-service/worker-provider.d.ts +9 -0
- package/dist/language-service/worker-provider.js +38 -0
- package/dist/lapis-extension.d.ts +244 -0
- package/dist/lapis-extension.js +591 -0
- package/dist/links.d.ts +34 -0
- package/dist/links.js +191 -0
- package/dist/loaders.d.ts +9 -0
- package/dist/loaders.js +59 -0
- package/dist/localization-manager.svelte.d.ts +56 -0
- package/dist/localization-manager.svelte.js +157 -0
- package/dist/logging.d.ts +172 -0
- package/dist/logging.js +352 -0
- package/dist/markdown-extension-registry.d.ts +31 -0
- package/dist/markdown-extension-registry.js +31 -0
- package/dist/markdown-file-surface-registry.d.ts +25 -0
- package/dist/markdown-file-surface-registry.js +23 -0
- package/dist/markdown.d.ts +205 -0
- package/dist/markdown.js +147 -0
- package/dist/menu-utils.d.ts +1 -0
- package/dist/menu-utils.js +47 -0
- package/dist/menu.svelte.d.ts +121 -0
- package/dist/menu.svelte.js +295 -0
- package/dist/metadata-value.d.ts +10 -0
- package/dist/metadata-value.js +173 -0
- package/dist/metadata.svelte.d.ts +106 -0
- package/dist/metadata.svelte.js +608 -0
- package/dist/notifications.d.ts +209 -0
- package/dist/notifications.js +338 -0
- package/dist/open-file.d.ts +23 -0
- package/dist/open-file.js +102 -0
- package/dist/platform.d.ts +86 -0
- package/dist/platform.js +86 -0
- package/dist/plugin-asset-server.d.ts +60 -0
- package/dist/plugin-asset-server.js +222 -0
- package/dist/plugin-assets.d.ts +4 -0
- package/dist/plugin-assets.js +3 -0
- package/dist/plugin-capability-facade.d.ts +104 -0
- package/dist/plugin-capability-facade.js +61 -0
- package/dist/plugin-dependency-resolver.d.ts +36 -0
- package/dist/plugin-dependency-resolver.js +54 -0
- package/dist/plugin-dependency-scanner.d.ts +9 -0
- package/dist/plugin-dependency-scanner.js +274 -0
- package/dist/plugin-distribution/canonical-json.d.ts +1 -0
- package/dist/plugin-distribution/canonical-json.js +40 -0
- package/dist/plugin-distribution/compatibility.d.ts +5 -0
- package/dist/plugin-distribution/compatibility.js +38 -0
- package/dist/plugin-distribution/errors.d.ts +11 -0
- package/dist/plugin-distribution/errors.js +13 -0
- package/dist/plugin-distribution/hashes.d.ts +2 -0
- package/dist/plugin-distribution/hashes.js +17 -0
- package/dist/plugin-distribution/index.d.ts +18 -0
- package/dist/plugin-distribution/index.js +18 -0
- package/dist/plugin-distribution/installed-plugin-state.d.ts +20 -0
- package/dist/plugin-distribution/installed-plugin-state.js +73 -0
- package/dist/plugin-distribution/installer.d.ts +89 -0
- package/dist/plugin-distribution/installer.js +395 -0
- package/dist/plugin-distribution/manager.d.ts +128 -0
- package/dist/plugin-distribution/manager.js +358 -0
- package/dist/plugin-distribution/path-safety.d.ts +2 -0
- package/dist/plugin-distribution/path-safety.js +20 -0
- package/dist/plugin-distribution/plugin-bundle-verifier.d.ts +28 -0
- package/dist/plugin-distribution/plugin-bundle-verifier.js +96 -0
- package/dist/plugin-distribution/plugin-bundle-worker.d.ts +34 -0
- package/dist/plugin-distribution/plugin-bundle-worker.js +99 -0
- package/dist/plugin-distribution/plugin-bundle.d.ts +16 -0
- package/dist/plugin-distribution/plugin-bundle.js +244 -0
- package/dist/plugin-distribution/plugin-bundle.worker.d.ts +1 -0
- package/dist/plugin-distribution/plugin-bundle.worker.js +38 -0
- package/dist/plugin-distribution/registry-cache.d.ts +26 -0
- package/dist/plugin-distribution/registry-cache.js +85 -0
- package/dist/plugin-distribution/registry-client.d.ts +36 -0
- package/dist/plugin-distribution/registry-client.js +115 -0
- package/dist/plugin-distribution/reserved-ids.d.ts +4 -0
- package/dist/plugin-distribution/reserved-ids.js +10 -0
- package/dist/plugin-distribution/runtime-validation.d.ts +14 -0
- package/dist/plugin-distribution/runtime-validation.js +388 -0
- package/dist/plugin-distribution/schemas.d.ts +4895 -0
- package/dist/plugin-distribution/schemas.js +299 -0
- package/dist/plugin-distribution/signing.d.ts +5 -0
- package/dist/plugin-distribution/signing.js +38 -0
- package/dist/plugin-distribution/types.d.ts +217 -0
- package/dist/plugin-distribution/types.js +1 -0
- package/dist/plugin-install-progress.d.ts +15 -0
- package/dist/plugin-install-progress.js +86 -0
- package/dist/plugin-manager.d.ts +306 -0
- package/dist/plugin-manager.js +2848 -0
- package/dist/plugin-runtime-entry.d.ts +19 -0
- package/dist/plugin-runtime-entry.js +135 -0
- package/dist/plugin.d.ts +350 -0
- package/dist/plugin.js +754 -0
- package/dist/popover.d.ts +20 -0
- package/dist/popover.js +32 -0
- package/dist/promise-worker.d.ts +22 -0
- package/dist/promise-worker.js +65 -0
- package/dist/prompt-confirm.d.ts +9 -0
- package/dist/prompt-confirm.js +33 -0
- package/dist/search-document-provider.d.ts +45 -0
- package/dist/search-document-provider.js +54 -0
- package/dist/search-query/index.d.ts +78 -0
- package/dist/search-query/index.js +426 -0
- package/dist/search-query/parser.d.ts +3 -0
- package/dist/search-query/parser.js +16 -0
- package/dist/search-query/parser.terms.js +26 -0
- package/dist/search-query/search-query.grammar +128 -0
- package/dist/search.d.ts +19 -0
- package/dist/search.js +104 -0
- package/dist/setting-section-layout.d.ts +19 -0
- package/dist/setting-section-layout.js +194 -0
- package/dist/settings.svelte.d.ts +474 -0
- package/dist/settings.svelte.js +1421 -0
- package/dist/source-text-file-view.d.ts +25 -0
- package/dist/source-text-file-view.js +78 -0
- package/dist/status-bar.svelte.d.ts +29 -0
- package/dist/status-bar.svelte.js +72 -0
- package/dist/storage/app-database.d.ts +860 -0
- package/dist/storage/app-database.js +2372 -0
- package/dist/storage/blob-store.d.ts +12 -0
- package/dist/storage/blob-store.js +1 -0
- package/dist/storage/browser-app-database-coordination.d.ts +35 -0
- package/dist/storage/browser-app-database-coordination.js +207 -0
- package/dist/storage/browser-coordinated-app-database.d.ts +109 -0
- package/dist/storage/browser-coordinated-app-database.js +704 -0
- package/dist/storage/browser.d.ts +153 -0
- package/dist/storage/browser.js +794 -0
- package/dist/storage/desktop-native.d.ts +184 -0
- package/dist/storage/desktop-native.js +415 -0
- package/dist/storage/fs.d.ts +188 -0
- package/dist/storage/fs.js +307 -0
- package/dist/storage/index-projection.d.ts +236 -0
- package/dist/storage/index-projection.js +402 -0
- package/dist/storage/index.d.ts +17 -0
- package/dist/storage/index.js +17 -0
- package/dist/storage/keyval.d.ts +94 -0
- package/dist/storage/keyval.js +199 -0
- package/dist/storage/memory-vault-adapter.d.ts +44 -0
- package/dist/storage/memory-vault-adapter.js +351 -0
- package/dist/storage/native-vault-bootstrap-kv.d.ts +22 -0
- package/dist/storage/native-vault-bootstrap-kv.js +96 -0
- package/dist/storage/path.d.ts +22 -0
- package/dist/storage/path.js +110 -0
- package/dist/storage/search-embedding-provider.d.ts +75 -0
- package/dist/storage/search-embedding-provider.js +446 -0
- package/dist/storage/search-embedding-worker.d.ts +1 -0
- package/dist/storage/search-embedding-worker.js +63 -0
- package/dist/storage/search-query-evaluator.d.ts +14 -0
- package/dist/storage/search-query-evaluator.js +678 -0
- package/dist/storage/task-projection.d.ts +152 -0
- package/dist/storage/task-projection.js +154 -0
- package/dist/storage/turso-app-database.d.ts +150 -0
- package/dist/storage/turso-app-database.js +2671 -0
- package/dist/storage/vault-session.d.ts +37 -0
- package/dist/storage/vault-session.js +196 -0
- package/dist/storage/vault-state.d.ts +74 -0
- package/dist/storage/vault-state.js +181 -0
- package/dist/storage/vault.svelte.d.ts +257 -0
- package/dist/storage/vault.svelte.js +661 -0
- package/dist/suggest.d.ts +193 -0
- package/dist/suggest.js +391 -0
- package/dist/tasks.d.ts +7 -0
- package/dist/tasks.js +18 -0
- package/dist/telemetry.d.ts +116 -0
- package/dist/telemetry.js +103 -0
- package/dist/utils.d.ts +55 -0
- package/dist/utils.js +314 -0
- package/dist/vault-api.d.ts +5 -0
- package/dist/vault-api.js +3 -0
- package/dist/vault-maintenance-commands.d.ts +5 -0
- package/dist/vault-maintenance-commands.js +32 -0
- package/dist/view.svelte.d.ts +215 -0
- package/dist/view.svelte.js +381 -0
- package/dist/vite-env.d.ts +2 -0
- package/dist/workspace-host-internal.d.ts +7 -0
- package/dist/workspace-host-internal.js +7 -0
- package/dist/workspace-host.d.ts +10 -0
- package/dist/workspace-host.js +14 -0
- package/dist/workspace-layout-commands.d.ts +3 -0
- package/dist/workspace-layout-commands.js +72 -0
- package/dist/workspace-layout-normalizer.d.ts +92 -0
- package/dist/workspace-layout-normalizer.js +301 -0
- package/dist/workspace-layouts.d.ts +7 -0
- package/dist/workspace-layouts.js +23 -0
- package/dist/workspace-trust.d.ts +32 -0
- package/dist/workspace-trust.js +72 -0
- package/dist/workspace.svelte.d.ts +796 -0
- package/dist/workspace.svelte.js +4380 -0
- package/package.json +240 -0
|
@@ -0,0 +1,704 @@
|
|
|
1
|
+
import { BrowserAppDatabaseCoordinator } from "./browser-app-database-coordination";
|
|
2
|
+
import { TursoWasmAppDatabaseProvider } from "./turso-app-database";
|
|
3
|
+
const REQUEST_TIMEOUT_MS = 15_000;
|
|
4
|
+
const LOCAL_RECOVERY_POLL_MS = 100;
|
|
5
|
+
const MAX_RPC_ARGUMENTS = 4;
|
|
6
|
+
const MAX_RPC_MESSAGE_BYTES = 32 * 1024 * 1024;
|
|
7
|
+
const MAX_RPC_IDENTIFIER_LENGTH = 512;
|
|
8
|
+
const APP_DATABASE_RPC_METHODS = new Set([
|
|
9
|
+
"describe",
|
|
10
|
+
"migrate",
|
|
11
|
+
"beginSearchIndexingBatch",
|
|
12
|
+
"endSearchIndexingBatch",
|
|
13
|
+
"configureSearchEmbeddingProvider",
|
|
14
|
+
"getSearchEmbeddingProvider",
|
|
15
|
+
"getSearchEmbeddingRuntimeStatus",
|
|
16
|
+
"getSearchIndexStats",
|
|
17
|
+
"getMeta",
|
|
18
|
+
"setMeta",
|
|
19
|
+
"getNotebookState",
|
|
20
|
+
"setNotebookState",
|
|
21
|
+
"deleteNotebookState",
|
|
22
|
+
"loadMetadataSnapshot",
|
|
23
|
+
"saveMetadataSnapshot",
|
|
24
|
+
"getFileHistory",
|
|
25
|
+
"storeFileHistoryRevision",
|
|
26
|
+
"listNotifications",
|
|
27
|
+
"upsertNotification",
|
|
28
|
+
"markNotificationRead",
|
|
29
|
+
"clearNotification",
|
|
30
|
+
"clearAllNotifications",
|
|
31
|
+
"getMemorySourceState",
|
|
32
|
+
"listMemorySourceStates",
|
|
33
|
+
"upsertMemorySourceState",
|
|
34
|
+
"deleteMemorySourceState",
|
|
35
|
+
"upsertMemoryCandidate",
|
|
36
|
+
"queryMemoryCandidates",
|
|
37
|
+
"deleteMemoryCandidatesByConversation",
|
|
38
|
+
"recordMemoryRecallSignal",
|
|
39
|
+
"listMemoryRecallSignals",
|
|
40
|
+
"claimMemoryJob",
|
|
41
|
+
"updateMemoryJob",
|
|
42
|
+
"listMemoryJobs",
|
|
43
|
+
"clearMemoryDerivedState",
|
|
44
|
+
"getChangeRevision",
|
|
45
|
+
"upsertIndexedFile",
|
|
46
|
+
"getIndexedFile",
|
|
47
|
+
"listIndexedFileManifest",
|
|
48
|
+
"deleteIndexedFile",
|
|
49
|
+
"renameIndexedFile",
|
|
50
|
+
"queryIndexedMetadata",
|
|
51
|
+
"queryIndexedMetadataPage",
|
|
52
|
+
"queryMetadataFacets",
|
|
53
|
+
"queryMetadataLinks",
|
|
54
|
+
"upsertSearchDocument",
|
|
55
|
+
"deleteSearchDocument",
|
|
56
|
+
"getSearchDocument",
|
|
57
|
+
"listSearchDocumentManifest",
|
|
58
|
+
"listSearchDocuments",
|
|
59
|
+
"rebuildSearchIndex",
|
|
60
|
+
"searchDocuments",
|
|
61
|
+
"searchDocumentPaths",
|
|
62
|
+
"upsertTaskProjection",
|
|
63
|
+
"deleteTaskProjection",
|
|
64
|
+
"queryTasks",
|
|
65
|
+
"getTaskRow",
|
|
66
|
+
"listChildLinks",
|
|
67
|
+
"listTaskDescendants",
|
|
68
|
+
"registerProjectionDefinition",
|
|
69
|
+
"unregisterProjectionDefinition",
|
|
70
|
+
"replaceProjectionSource",
|
|
71
|
+
"markProjectionSourceError",
|
|
72
|
+
"deleteProjectionSource",
|
|
73
|
+
"queryProjection",
|
|
74
|
+
"getProjectionRow",
|
|
75
|
+
"queryRelated",
|
|
76
|
+
]);
|
|
77
|
+
function isBoundedIdentifier(value) {
|
|
78
|
+
return (typeof value === "string" &&
|
|
79
|
+
value.length > 0 &&
|
|
80
|
+
value.length <= MAX_RPC_IDENTIFIER_LENGTH);
|
|
81
|
+
}
|
|
82
|
+
function isBoundedRpcMessage(message) {
|
|
83
|
+
try {
|
|
84
|
+
const serialized = JSON.stringify(message);
|
|
85
|
+
return (typeof serialized === "string" &&
|
|
86
|
+
new TextEncoder().encode(serialized).byteLength <= MAX_RPC_MESSAGE_BYTES);
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
function isRequestMessage(message) {
|
|
93
|
+
return (typeof message === "object" &&
|
|
94
|
+
message !== null &&
|
|
95
|
+
message.type === "db-request" &&
|
|
96
|
+
isBoundedIdentifier(message.vaultId) &&
|
|
97
|
+
isBoundedIdentifier(message.requesterId) &&
|
|
98
|
+
isBoundedIdentifier(message.requestId) &&
|
|
99
|
+
(message.ownerId === undefined ||
|
|
100
|
+
isBoundedIdentifier(message.ownerId)) &&
|
|
101
|
+
typeof message.method === "string" &&
|
|
102
|
+
APP_DATABASE_RPC_METHODS.has(message.method) &&
|
|
103
|
+
Array.isArray(message.args) &&
|
|
104
|
+
message.args.length <= MAX_RPC_ARGUMENTS &&
|
|
105
|
+
isBoundedRpcMessage(message));
|
|
106
|
+
}
|
|
107
|
+
function isResponseMessage(message) {
|
|
108
|
+
return (typeof message === "object" &&
|
|
109
|
+
message !== null &&
|
|
110
|
+
message.type === "db-response" &&
|
|
111
|
+
isBoundedIdentifier(message.vaultId) &&
|
|
112
|
+
isBoundedIdentifier(message.responderId) &&
|
|
113
|
+
isBoundedIdentifier(message.requesterId) &&
|
|
114
|
+
isBoundedIdentifier(message.requestId) &&
|
|
115
|
+
typeof message.success === "boolean" &&
|
|
116
|
+
isBoundedRpcMessage(message));
|
|
117
|
+
}
|
|
118
|
+
function isChangeMessage(message) {
|
|
119
|
+
const change = message?.change;
|
|
120
|
+
return (typeof message === "object" &&
|
|
121
|
+
message !== null &&
|
|
122
|
+
message.type === "db-change" &&
|
|
123
|
+
isBoundedIdentifier(message.vaultId) &&
|
|
124
|
+
isBoundedIdentifier(message.ownerId) &&
|
|
125
|
+
typeof change === "object" &&
|
|
126
|
+
change !== null &&
|
|
127
|
+
Number.isSafeInteger(change.revision) &&
|
|
128
|
+
change.revision >= 0 &&
|
|
129
|
+
Array.isArray(change.domains) &&
|
|
130
|
+
Array.isArray(change.paths) &&
|
|
131
|
+
isBoundedRpcMessage(message));
|
|
132
|
+
}
|
|
133
|
+
function createRequestId() {
|
|
134
|
+
if (typeof crypto !== "undefined" &&
|
|
135
|
+
typeof crypto.randomUUID === "function") {
|
|
136
|
+
return crypto.randomUUID();
|
|
137
|
+
}
|
|
138
|
+
return `req-${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
|
139
|
+
}
|
|
140
|
+
export class BrowserCoordinatedAppDatabase {
|
|
141
|
+
vaultId;
|
|
142
|
+
coordinator;
|
|
143
|
+
startsOwned;
|
|
144
|
+
provider;
|
|
145
|
+
get kind() {
|
|
146
|
+
return this.localDatabase?.kind ?? "turso-wasm";
|
|
147
|
+
}
|
|
148
|
+
get descriptor() {
|
|
149
|
+
if (this.localDatabase)
|
|
150
|
+
return this.localDatabase.descriptor;
|
|
151
|
+
if (this.remoteDescriptor) {
|
|
152
|
+
return {
|
|
153
|
+
...this.remoteDescriptor,
|
|
154
|
+
transport: "broadcast-proxy",
|
|
155
|
+
role: "proxy",
|
|
156
|
+
capabilities: {
|
|
157
|
+
...this.remoteDescriptor.capabilities,
|
|
158
|
+
crossTabCoordination: true,
|
|
159
|
+
},
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
return {
|
|
163
|
+
providerId: this.provider.id,
|
|
164
|
+
engine: "turso",
|
|
165
|
+
transport: "broadcast-proxy",
|
|
166
|
+
role: "proxy",
|
|
167
|
+
storageMode: "local",
|
|
168
|
+
capabilities: {
|
|
169
|
+
nativeFullTextSearch: false,
|
|
170
|
+
vectorSearch: false,
|
|
171
|
+
approximateNearestNeighbors: false,
|
|
172
|
+
localEmbeddings: true,
|
|
173
|
+
crossTabCoordination: true,
|
|
174
|
+
sync: false,
|
|
175
|
+
},
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
localDatabase = null;
|
|
179
|
+
remoteDescriptor = null;
|
|
180
|
+
rpcChannel = null;
|
|
181
|
+
pendingRequests = new Map();
|
|
182
|
+
opened = false;
|
|
183
|
+
closed = false;
|
|
184
|
+
ownershipMonitorAbortController = null;
|
|
185
|
+
promotionPromise = null;
|
|
186
|
+
servingRequests = false;
|
|
187
|
+
coordinationModeListeners = new Set();
|
|
188
|
+
changeListeners = new Set();
|
|
189
|
+
localChangeUnsubscribe = null;
|
|
190
|
+
lastSeenRevision = 0;
|
|
191
|
+
constructor(vaultId, coordinator, startsOwned, provider = new TursoWasmAppDatabaseProvider()) {
|
|
192
|
+
this.vaultId = vaultId;
|
|
193
|
+
this.coordinator = coordinator;
|
|
194
|
+
this.startsOwned = startsOwned;
|
|
195
|
+
this.provider = provider;
|
|
196
|
+
}
|
|
197
|
+
get coordinationMode() {
|
|
198
|
+
return this.startsOwned || this.localDatabase
|
|
199
|
+
? "turso-owner"
|
|
200
|
+
: "turso-proxy";
|
|
201
|
+
}
|
|
202
|
+
onCoordinationModeChange(listener) {
|
|
203
|
+
this.coordinationModeListeners.add(listener);
|
|
204
|
+
return () => {
|
|
205
|
+
this.coordinationModeListeners.delete(listener);
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
async open() {
|
|
209
|
+
if (this.opened) {
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
this.opened = true;
|
|
213
|
+
this.ensureRpcChannel();
|
|
214
|
+
if (this.startsOwned) {
|
|
215
|
+
await this.promoteToOwner();
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
this.startOwnershipMonitor();
|
|
219
|
+
this.remoteDescriptor = await this.invokeRemote("describe", []);
|
|
220
|
+
}
|
|
221
|
+
async migrate() {
|
|
222
|
+
await this.invoke("migrate");
|
|
223
|
+
}
|
|
224
|
+
async close() {
|
|
225
|
+
this.closed = true;
|
|
226
|
+
this.ownershipMonitorAbortController?.abort();
|
|
227
|
+
this.ownershipMonitorAbortController = null;
|
|
228
|
+
this.pendingRequests.forEach(({ reject, timeoutId }) => {
|
|
229
|
+
clearTimeout(timeoutId);
|
|
230
|
+
reject(new Error("Coordinated app database closed"));
|
|
231
|
+
});
|
|
232
|
+
this.pendingRequests.clear();
|
|
233
|
+
try {
|
|
234
|
+
await this.localDatabase?.close();
|
|
235
|
+
}
|
|
236
|
+
finally {
|
|
237
|
+
this.localChangeUnsubscribe?.();
|
|
238
|
+
this.localChangeUnsubscribe = null;
|
|
239
|
+
this.changeListeners.clear();
|
|
240
|
+
this.localDatabase = null;
|
|
241
|
+
this.rpcChannel?.close();
|
|
242
|
+
this.rpcChannel = null;
|
|
243
|
+
if (!this.startsOwned) {
|
|
244
|
+
this.coordinator.close();
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
async beginSearchIndexingBatch() {
|
|
249
|
+
await this.invoke("beginSearchIndexingBatch");
|
|
250
|
+
}
|
|
251
|
+
async endSearchIndexingBatch() {
|
|
252
|
+
await this.invoke("endSearchIndexingBatch");
|
|
253
|
+
}
|
|
254
|
+
async configureSearchEmbeddingProvider(provider) {
|
|
255
|
+
await this.invoke("configureSearchEmbeddingProvider", provider);
|
|
256
|
+
}
|
|
257
|
+
async getSearchEmbeddingProvider() {
|
|
258
|
+
return this.invoke("getSearchEmbeddingProvider");
|
|
259
|
+
}
|
|
260
|
+
async getSearchEmbeddingRuntimeStatus() {
|
|
261
|
+
return this.invoke("getSearchEmbeddingRuntimeStatus");
|
|
262
|
+
}
|
|
263
|
+
async getSearchIndexStats() {
|
|
264
|
+
return this.invoke("getSearchIndexStats");
|
|
265
|
+
}
|
|
266
|
+
async getMeta(key) {
|
|
267
|
+
return this.invoke("getMeta", key);
|
|
268
|
+
}
|
|
269
|
+
async setMeta(key, value) {
|
|
270
|
+
await this.invoke("setMeta", key, value);
|
|
271
|
+
}
|
|
272
|
+
async getNotebookState(sourcePath) {
|
|
273
|
+
return this.invoke("getNotebookState", sourcePath);
|
|
274
|
+
}
|
|
275
|
+
async setNotebookState(sourcePath, state) {
|
|
276
|
+
await this.invoke("setNotebookState", sourcePath, state);
|
|
277
|
+
}
|
|
278
|
+
async deleteNotebookState(sourcePath) {
|
|
279
|
+
await this.invoke("deleteNotebookState", sourcePath);
|
|
280
|
+
}
|
|
281
|
+
async loadMetadataSnapshot() {
|
|
282
|
+
return this.invoke("loadMetadataSnapshot");
|
|
283
|
+
}
|
|
284
|
+
async saveMetadataSnapshot(snapshot) {
|
|
285
|
+
await this.invoke("saveMetadataSnapshot", snapshot);
|
|
286
|
+
}
|
|
287
|
+
async getFileHistory(path) {
|
|
288
|
+
return this.invoke("getFileHistory", path);
|
|
289
|
+
}
|
|
290
|
+
async storeFileHistoryRevision(input) {
|
|
291
|
+
return this.invoke("storeFileHistoryRevision", input);
|
|
292
|
+
}
|
|
293
|
+
async listNotifications() {
|
|
294
|
+
return this.invoke("listNotifications");
|
|
295
|
+
}
|
|
296
|
+
async upsertNotification(record) {
|
|
297
|
+
await this.invoke("upsertNotification", record);
|
|
298
|
+
}
|
|
299
|
+
async markNotificationRead(id) {
|
|
300
|
+
await this.invoke("markNotificationRead", id);
|
|
301
|
+
}
|
|
302
|
+
async clearNotification(id) {
|
|
303
|
+
await this.invoke("clearNotification", id);
|
|
304
|
+
}
|
|
305
|
+
async clearAllNotifications() {
|
|
306
|
+
await this.invoke("clearAllNotifications");
|
|
307
|
+
}
|
|
308
|
+
async getMemorySourceState(sourceKey) {
|
|
309
|
+
return this.invoke("getMemorySourceState", sourceKey);
|
|
310
|
+
}
|
|
311
|
+
async listMemorySourceStates() {
|
|
312
|
+
return this.invoke("listMemorySourceStates");
|
|
313
|
+
}
|
|
314
|
+
async upsertMemorySourceState(record) {
|
|
315
|
+
await this.invoke("upsertMemorySourceState", record);
|
|
316
|
+
}
|
|
317
|
+
async deleteMemorySourceState(sourceKey) {
|
|
318
|
+
await this.invoke("deleteMemorySourceState", sourceKey);
|
|
319
|
+
}
|
|
320
|
+
async upsertMemoryCandidate(input) {
|
|
321
|
+
await this.invoke("upsertMemoryCandidate", input);
|
|
322
|
+
}
|
|
323
|
+
async queryMemoryCandidates(query) {
|
|
324
|
+
return this.invoke("queryMemoryCandidates", query);
|
|
325
|
+
}
|
|
326
|
+
async deleteMemoryCandidatesByConversation(conversationId) {
|
|
327
|
+
await this.invoke("deleteMemoryCandidatesByConversation", conversationId);
|
|
328
|
+
}
|
|
329
|
+
async recordMemoryRecallSignal(record) {
|
|
330
|
+
await this.invoke("recordMemoryRecallSignal", record);
|
|
331
|
+
}
|
|
332
|
+
async listMemoryRecallSignals(targetRef) {
|
|
333
|
+
return this.invoke("listMemoryRecallSignals", targetRef);
|
|
334
|
+
}
|
|
335
|
+
async claimMemoryJob(input) {
|
|
336
|
+
return this.invoke("claimMemoryJob", input);
|
|
337
|
+
}
|
|
338
|
+
async updateMemoryJob(input) {
|
|
339
|
+
return this.invoke("updateMemoryJob", input);
|
|
340
|
+
}
|
|
341
|
+
async listMemoryJobs(scopeKey) {
|
|
342
|
+
return this.invoke("listMemoryJobs", scopeKey);
|
|
343
|
+
}
|
|
344
|
+
async clearMemoryDerivedState() {
|
|
345
|
+
await this.invoke("clearMemoryDerivedState");
|
|
346
|
+
}
|
|
347
|
+
async getChangeRevision() {
|
|
348
|
+
const revision = await this.invoke("getChangeRevision");
|
|
349
|
+
this.lastSeenRevision = Math.max(this.lastSeenRevision, revision);
|
|
350
|
+
return revision;
|
|
351
|
+
}
|
|
352
|
+
subscribeToChanges(listener) {
|
|
353
|
+
this.changeListeners.add(listener);
|
|
354
|
+
return () => this.changeListeners.delete(listener);
|
|
355
|
+
}
|
|
356
|
+
async upsertIndexedFile(record) {
|
|
357
|
+
await this.invoke("upsertIndexedFile", record);
|
|
358
|
+
}
|
|
359
|
+
async getIndexedFile(path) {
|
|
360
|
+
return this.invoke("getIndexedFile", path);
|
|
361
|
+
}
|
|
362
|
+
async listIndexedFileManifest(query) {
|
|
363
|
+
return this.invoke("listIndexedFileManifest", query);
|
|
364
|
+
}
|
|
365
|
+
async deleteIndexedFile(path) {
|
|
366
|
+
await this.invoke("deleteIndexedFile", path);
|
|
367
|
+
}
|
|
368
|
+
async renameIndexedFile(oldPath, newPath) {
|
|
369
|
+
await this.invoke("renameIndexedFile", oldPath, newPath);
|
|
370
|
+
}
|
|
371
|
+
async queryIndexedMetadata(query = {}) {
|
|
372
|
+
return this.invoke("queryIndexedMetadata", query);
|
|
373
|
+
}
|
|
374
|
+
async queryIndexedMetadataPage(query) {
|
|
375
|
+
return this.invoke("queryIndexedMetadataPage", query);
|
|
376
|
+
}
|
|
377
|
+
async queryMetadataFacets(query) {
|
|
378
|
+
return this.invoke("queryMetadataFacets", query);
|
|
379
|
+
}
|
|
380
|
+
async queryMetadataLinks(query) {
|
|
381
|
+
return this.invoke("queryMetadataLinks", query);
|
|
382
|
+
}
|
|
383
|
+
async upsertSearchDocument(document) {
|
|
384
|
+
await this.invoke("upsertSearchDocument", document);
|
|
385
|
+
}
|
|
386
|
+
async deleteSearchDocument(path) {
|
|
387
|
+
await this.invoke("deleteSearchDocument", path);
|
|
388
|
+
}
|
|
389
|
+
async getSearchDocument(path) {
|
|
390
|
+
return this.invoke("getSearchDocument", path);
|
|
391
|
+
}
|
|
392
|
+
async listSearchDocumentManifest(query) {
|
|
393
|
+
return this.invoke("listSearchDocumentManifest", query);
|
|
394
|
+
}
|
|
395
|
+
async listSearchDocuments() {
|
|
396
|
+
return this.invoke("listSearchDocuments");
|
|
397
|
+
}
|
|
398
|
+
async rebuildSearchIndex() {
|
|
399
|
+
await this.invoke("rebuildSearchIndex");
|
|
400
|
+
}
|
|
401
|
+
async searchDocuments(query, options) {
|
|
402
|
+
return this.invoke("searchDocuments", query, options);
|
|
403
|
+
}
|
|
404
|
+
async searchDocumentPaths(query, options) {
|
|
405
|
+
return this.invoke("searchDocumentPaths", query, options);
|
|
406
|
+
}
|
|
407
|
+
async upsertTaskProjection(record) {
|
|
408
|
+
await this.invoke("upsertTaskProjection", record);
|
|
409
|
+
}
|
|
410
|
+
async deleteTaskProjection(path) {
|
|
411
|
+
await this.invoke("deleteTaskProjection", path);
|
|
412
|
+
}
|
|
413
|
+
async queryTasks(query) {
|
|
414
|
+
return this.invoke("queryTasks", query);
|
|
415
|
+
}
|
|
416
|
+
async getTaskRow(lookup) {
|
|
417
|
+
return this.invoke("getTaskRow", lookup);
|
|
418
|
+
}
|
|
419
|
+
async listChildLinks(query) {
|
|
420
|
+
return this.invoke("listChildLinks", query);
|
|
421
|
+
}
|
|
422
|
+
async listTaskDescendants(path) {
|
|
423
|
+
return this.invoke("listTaskDescendants", path);
|
|
424
|
+
}
|
|
425
|
+
async registerProjectionDefinition(definition) {
|
|
426
|
+
await this.invoke("registerProjectionDefinition", definition);
|
|
427
|
+
}
|
|
428
|
+
async unregisterProjectionDefinition(projectionId) {
|
|
429
|
+
await this.invoke("unregisterProjectionDefinition", projectionId);
|
|
430
|
+
}
|
|
431
|
+
async replaceProjectionSource(input) {
|
|
432
|
+
await this.invoke("replaceProjectionSource", input);
|
|
433
|
+
}
|
|
434
|
+
async markProjectionSourceError(input) {
|
|
435
|
+
await this.invoke("markProjectionSourceError", input);
|
|
436
|
+
}
|
|
437
|
+
async deleteProjectionSource(projectionId, sourcePath, writerPluginId) {
|
|
438
|
+
await this.invoke("deleteProjectionSource", projectionId, sourcePath, writerPluginId);
|
|
439
|
+
}
|
|
440
|
+
async queryProjection(projectionId, query, readerPluginId) {
|
|
441
|
+
return this.invoke("queryProjection", projectionId, query, readerPluginId);
|
|
442
|
+
}
|
|
443
|
+
async getProjectionRow(projectionId, rowId, readerPluginId) {
|
|
444
|
+
return this.invoke("getProjectionRow", projectionId, rowId, readerPluginId);
|
|
445
|
+
}
|
|
446
|
+
async queryRelated(query, readerPluginId) {
|
|
447
|
+
return this.invoke("queryRelated", query, readerPluginId);
|
|
448
|
+
}
|
|
449
|
+
async invoke(method, ...args) {
|
|
450
|
+
if (!this.opened) {
|
|
451
|
+
await this.open();
|
|
452
|
+
}
|
|
453
|
+
if (this.localDatabase) {
|
|
454
|
+
return this.localDatabase[method](...args);
|
|
455
|
+
}
|
|
456
|
+
return this.invokeRemote(method, args);
|
|
457
|
+
}
|
|
458
|
+
ensureRpcChannel() {
|
|
459
|
+
if (this.rpcChannel || typeof BroadcastChannel === "undefined") {
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
this.rpcChannel = new BroadcastChannel(this.coordinator.rpcChannelName);
|
|
463
|
+
this.rpcChannel.addEventListener("message", (event) => {
|
|
464
|
+
const message = event.data;
|
|
465
|
+
if (!message ||
|
|
466
|
+
message.vaultId !== this.vaultId) {
|
|
467
|
+
return;
|
|
468
|
+
}
|
|
469
|
+
if (isResponseMessage(message)) {
|
|
470
|
+
if (message.requesterId !== this.coordinator.tabId) {
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
const pending = this.pendingRequests.get(message.requestId);
|
|
474
|
+
if (!pending) {
|
|
475
|
+
return;
|
|
476
|
+
}
|
|
477
|
+
if (pending.expectedResponderId &&
|
|
478
|
+
message.responderId !== pending.expectedResponderId) {
|
|
479
|
+
return;
|
|
480
|
+
}
|
|
481
|
+
clearTimeout(pending.timeoutId);
|
|
482
|
+
this.pendingRequests.delete(message.requestId);
|
|
483
|
+
if (message.success) {
|
|
484
|
+
pending.resolve(message.result);
|
|
485
|
+
}
|
|
486
|
+
else {
|
|
487
|
+
pending.reject(new Error(message.error ?? "Remote app database request failed"));
|
|
488
|
+
}
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
if (isChangeMessage(message)) {
|
|
492
|
+
if (!this.localDatabase && message.ownerId === this.coordinator.observedOwnerId) {
|
|
493
|
+
this.acceptChange(message.change);
|
|
494
|
+
}
|
|
495
|
+
return;
|
|
496
|
+
}
|
|
497
|
+
if (!isRequestMessage(message)) {
|
|
498
|
+
return;
|
|
499
|
+
}
|
|
500
|
+
if (!this.localDatabase || !this.servingRequests) {
|
|
501
|
+
return;
|
|
502
|
+
}
|
|
503
|
+
if (message.ownerId && message.ownerId !== this.coordinator.ownerId) {
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
void this.handleRemoteRequest(message);
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
async invokeRemote(method, args) {
|
|
510
|
+
if (!this.rpcChannel) {
|
|
511
|
+
throw new Error("BroadcastChannel is unavailable for app database delegation");
|
|
512
|
+
}
|
|
513
|
+
const requestId = createRequestId();
|
|
514
|
+
const message = {
|
|
515
|
+
type: "db-request",
|
|
516
|
+
vaultId: this.vaultId,
|
|
517
|
+
ownerId: this.coordinator.observedOwnerId ?? undefined,
|
|
518
|
+
requesterId: this.coordinator.tabId,
|
|
519
|
+
requestId,
|
|
520
|
+
method,
|
|
521
|
+
args,
|
|
522
|
+
};
|
|
523
|
+
return new Promise((resolve, reject) => {
|
|
524
|
+
let settled = false;
|
|
525
|
+
let recovering = false;
|
|
526
|
+
const settleResolve = (value) => {
|
|
527
|
+
if (settled) {
|
|
528
|
+
return;
|
|
529
|
+
}
|
|
530
|
+
settled = true;
|
|
531
|
+
clearTimeout(timeoutId);
|
|
532
|
+
clearInterval(recoveryIntervalId);
|
|
533
|
+
this.pendingRequests.delete(requestId);
|
|
534
|
+
resolve(value);
|
|
535
|
+
};
|
|
536
|
+
const settleReject = (error) => {
|
|
537
|
+
if (settled) {
|
|
538
|
+
return;
|
|
539
|
+
}
|
|
540
|
+
settled = true;
|
|
541
|
+
clearTimeout(timeoutId);
|
|
542
|
+
clearInterval(recoveryIntervalId);
|
|
543
|
+
this.pendingRequests.delete(requestId);
|
|
544
|
+
reject(error);
|
|
545
|
+
};
|
|
546
|
+
const timeoutId = setTimeout(() => {
|
|
547
|
+
settleReject(new Error(`Remote app database request timed out: ${method}`));
|
|
548
|
+
}, REQUEST_TIMEOUT_MS);
|
|
549
|
+
const tryRecoverLocally = async () => {
|
|
550
|
+
if (settled || recovering || !this.localDatabase) {
|
|
551
|
+
return;
|
|
552
|
+
}
|
|
553
|
+
recovering = true;
|
|
554
|
+
try {
|
|
555
|
+
const result = method === "describe"
|
|
556
|
+
? this.localDatabase.descriptor
|
|
557
|
+
: await this.localDatabase[method](...args);
|
|
558
|
+
settleResolve(result);
|
|
559
|
+
}
|
|
560
|
+
catch (error) {
|
|
561
|
+
settleReject(error instanceof Error ? error : new Error(String(error)));
|
|
562
|
+
}
|
|
563
|
+
finally {
|
|
564
|
+
recovering = false;
|
|
565
|
+
}
|
|
566
|
+
};
|
|
567
|
+
const recoveryIntervalId = setInterval(() => {
|
|
568
|
+
void tryRecoverLocally();
|
|
569
|
+
}, LOCAL_RECOVERY_POLL_MS);
|
|
570
|
+
this.pendingRequests.set(requestId, {
|
|
571
|
+
resolve: (value) => settleResolve(value),
|
|
572
|
+
reject: settleReject,
|
|
573
|
+
timeoutId,
|
|
574
|
+
expectedResponderId: message.ownerId,
|
|
575
|
+
});
|
|
576
|
+
this.rpcChannel.postMessage(message);
|
|
577
|
+
void tryRecoverLocally();
|
|
578
|
+
});
|
|
579
|
+
}
|
|
580
|
+
async handleRemoteRequest(message) {
|
|
581
|
+
let response;
|
|
582
|
+
try {
|
|
583
|
+
const result = message.method === "describe"
|
|
584
|
+
? this.localDatabase.descriptor
|
|
585
|
+
: await this.localDatabase[message.method](...message.args);
|
|
586
|
+
response = {
|
|
587
|
+
type: "db-response",
|
|
588
|
+
vaultId: this.vaultId,
|
|
589
|
+
responderId: this.coordinator.ownerId,
|
|
590
|
+
requesterId: message.requesterId,
|
|
591
|
+
requestId: message.requestId,
|
|
592
|
+
success: true,
|
|
593
|
+
result,
|
|
594
|
+
};
|
|
595
|
+
}
|
|
596
|
+
catch (error) {
|
|
597
|
+
response = {
|
|
598
|
+
type: "db-response",
|
|
599
|
+
vaultId: this.vaultId,
|
|
600
|
+
responderId: this.coordinator.ownerId,
|
|
601
|
+
requesterId: message.requesterId,
|
|
602
|
+
requestId: message.requestId,
|
|
603
|
+
success: false,
|
|
604
|
+
error: error instanceof Error ? error.message : String(error),
|
|
605
|
+
};
|
|
606
|
+
}
|
|
607
|
+
if (!isBoundedRpcMessage(response)) {
|
|
608
|
+
response = {
|
|
609
|
+
type: "db-response",
|
|
610
|
+
vaultId: this.vaultId,
|
|
611
|
+
responderId: this.coordinator.ownerId,
|
|
612
|
+
requesterId: message.requesterId,
|
|
613
|
+
requestId: message.requestId,
|
|
614
|
+
success: false,
|
|
615
|
+
error: "App database response exceeded the cross-tab payload limit",
|
|
616
|
+
};
|
|
617
|
+
}
|
|
618
|
+
this.rpcChannel?.postMessage(response);
|
|
619
|
+
}
|
|
620
|
+
startOwnershipMonitor() {
|
|
621
|
+
if (this.ownershipMonitorAbortController || this.closed) {
|
|
622
|
+
return;
|
|
623
|
+
}
|
|
624
|
+
this.ownershipMonitorAbortController = new AbortController();
|
|
625
|
+
void this.coordinator
|
|
626
|
+
.waitForOwnership({ signal: this.ownershipMonitorAbortController.signal })
|
|
627
|
+
.then((acquired) => {
|
|
628
|
+
if (!acquired || this.closed) {
|
|
629
|
+
return;
|
|
630
|
+
}
|
|
631
|
+
return this.promoteToOwner();
|
|
632
|
+
})
|
|
633
|
+
.catch(() => {
|
|
634
|
+
// Request callers surface operational failures. The monitor only promotes ownership.
|
|
635
|
+
});
|
|
636
|
+
}
|
|
637
|
+
async promoteToOwner() {
|
|
638
|
+
if (this.localDatabase) {
|
|
639
|
+
this.servingRequests = true;
|
|
640
|
+
return;
|
|
641
|
+
}
|
|
642
|
+
if (this.promotionPromise) {
|
|
643
|
+
await this.promotionPromise;
|
|
644
|
+
return;
|
|
645
|
+
}
|
|
646
|
+
this.promotionPromise = (async () => {
|
|
647
|
+
const previousMode = this.coordinationMode;
|
|
648
|
+
const database = await this.openLocalDatabase();
|
|
649
|
+
this.localDatabase = database;
|
|
650
|
+
this.localChangeUnsubscribe = database.subscribeToChanges((change) => {
|
|
651
|
+
this.acceptChange(change);
|
|
652
|
+
this.rpcChannel?.postMessage({
|
|
653
|
+
type: "db-change",
|
|
654
|
+
vaultId: this.vaultId,
|
|
655
|
+
ownerId: this.coordinator.ownerId,
|
|
656
|
+
change,
|
|
657
|
+
});
|
|
658
|
+
});
|
|
659
|
+
this.startsOwned = true;
|
|
660
|
+
this.coordinator.startHeartbeat();
|
|
661
|
+
this.servingRequests = true;
|
|
662
|
+
if (previousMode !== this.coordinationMode) {
|
|
663
|
+
this.notifyCoordinationModeChange();
|
|
664
|
+
}
|
|
665
|
+
const revision = await database.getChangeRevision();
|
|
666
|
+
this.acceptChange({
|
|
667
|
+
revision,
|
|
668
|
+
domains: ["metadata", "search", "history", "notification", "notebook", "task", "projection", "meta"],
|
|
669
|
+
paths: [],
|
|
670
|
+
reset: true,
|
|
671
|
+
committedAt: Date.now(),
|
|
672
|
+
});
|
|
673
|
+
})();
|
|
674
|
+
try {
|
|
675
|
+
await this.promotionPromise;
|
|
676
|
+
}
|
|
677
|
+
finally {
|
|
678
|
+
this.promotionPromise = null;
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
async openLocalDatabase() {
|
|
682
|
+
return this.provider.open({
|
|
683
|
+
vaultId: this.vaultId,
|
|
684
|
+
runtime: "web-pwa",
|
|
685
|
+
role: "owner",
|
|
686
|
+
});
|
|
687
|
+
}
|
|
688
|
+
notifyCoordinationModeChange() {
|
|
689
|
+
const mode = this.coordinationMode;
|
|
690
|
+
for (const listener of this.coordinationModeListeners) {
|
|
691
|
+
listener(mode);
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
acceptChange(change) {
|
|
695
|
+
if (!change.reset && change.revision <= this.lastSeenRevision)
|
|
696
|
+
return;
|
|
697
|
+
const next = !change.reset && this.lastSeenRevision > 0 && change.revision !== this.lastSeenRevision + 1
|
|
698
|
+
? { ...change, reset: true, paths: [] }
|
|
699
|
+
: change;
|
|
700
|
+
this.lastSeenRevision = Math.max(this.lastSeenRevision, change.revision);
|
|
701
|
+
for (const listener of this.changeListeners)
|
|
702
|
+
listener(structuredClone(next));
|
|
703
|
+
}
|
|
704
|
+
}
|