daftari 3.13.0 → 3.14.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 +28 -0
- package/dist/distill/propose.d.ts +10 -0
- package/dist/distill/propose.d.ts.map +1 -1
- package/dist/distill/propose.js +49 -10
- package/dist/distill/propose.js.map +1 -1
- package/dist/distill/state.d.ts +6 -0
- package/dist/distill/state.d.ts.map +1 -1
- package/dist/distill/state.js +1 -1
- package/dist/distill/state.js.map +1 -1
- package/dist/extract/index.d.ts +16 -0
- package/dist/extract/index.d.ts.map +1 -0
- package/dist/extract/index.js +95 -0
- package/dist/extract/index.js.map +1 -0
- package/dist/extract/normalize.d.ts +7 -0
- package/dist/extract/normalize.d.ts.map +1 -0
- package/dist/extract/normalize.js +52 -0
- package/dist/extract/normalize.js.map +1 -0
- package/dist/extract/office.d.ts +63 -0
- package/dist/extract/office.d.ts.map +1 -0
- package/dist/extract/office.js +763 -0
- package/dist/extract/office.js.map +1 -0
- package/dist/extract/pdf.d.ts +16 -0
- package/dist/extract/pdf.d.ts.map +1 -0
- package/dist/extract/pdf.js +196 -0
- package/dist/extract/pdf.js.map +1 -0
- package/dist/extract/types.d.ts +55 -0
- package/dist/extract/types.d.ts.map +1 -0
- package/dist/extract/types.js +47 -0
- package/dist/extract/types.js.map +1 -0
- package/dist/extract/worker.d.ts +2 -0
- package/dist/extract/worker.d.ts.map +1 -0
- package/dist/extract/worker.js +57 -0
- package/dist/extract/worker.js.map +1 -0
- package/dist/integrations/distill.d.ts.map +1 -1
- package/dist/integrations/distill.js +1 -0
- package/dist/integrations/distill.js.map +1 -1
- package/dist/integrations/engine.d.ts +208 -2
- package/dist/integrations/engine.d.ts.map +1 -1
- package/dist/integrations/engine.js +360 -30
- package/dist/integrations/engine.js.map +1 -1
- package/dist/integrations/google.d.ts +2 -1
- package/dist/integrations/google.d.ts.map +1 -1
- package/dist/integrations/google.js +13 -117
- package/dist/integrations/google.js.map +1 -1
- package/dist/integrations/http-json.d.ts +27 -0
- package/dist/integrations/http-json.d.ts.map +1 -0
- package/dist/integrations/http-json.js +148 -0
- package/dist/integrations/http-json.js.map +1 -0
- package/dist/integrations/microsoft.d.ts +22 -0
- package/dist/integrations/microsoft.d.ts.map +1 -0
- package/dist/integrations/microsoft.js +1780 -0
- package/dist/integrations/microsoft.js.map +1 -0
- package/dist/integrations/microsoft.ui.d.ts +18 -0
- package/dist/integrations/microsoft.ui.d.ts.map +1 -0
- package/dist/integrations/microsoft.ui.js +131 -0
- package/dist/integrations/microsoft.ui.js.map +1 -0
- package/dist/integrations/oauth.d.ts.map +1 -1
- package/dist/integrations/oauth.js +10 -2
- package/dist/integrations/oauth.js.map +1 -1
- package/dist/integrations/queue.d.ts +1 -1
- package/dist/integrations/queue.d.ts.map +1 -1
- package/dist/integrations/queue.js +23 -1
- package/dist/integrations/queue.js.map +1 -1
- package/dist/integrations/routes.d.ts +26 -0
- package/dist/integrations/routes.d.ts.map +1 -1
- package/dist/integrations/routes.js +525 -11
- package/dist/integrations/routes.js.map +1 -1
- package/dist/integrations/runtime.d.ts +14 -2
- package/dist/integrations/runtime.d.ts.map +1 -1
- package/dist/integrations/runtime.js +51 -11
- package/dist/integrations/runtime.js.map +1 -1
- package/dist/integrations/state.d.ts +1 -1
- package/dist/integrations/state.d.ts.map +1 -1
- package/dist/integrations/state.js +74 -5
- package/dist/integrations/state.js.map +1 -1
- package/dist/integrations/types.d.ts +84 -1
- package/dist/integrations/types.d.ts.map +1 -1
- package/dist/integrations/types.js +21 -1
- package/dist/integrations/types.js.map +1 -1
- package/dist/okf/import.d.ts.map +1 -1
- package/dist/okf/import.js +208 -65
- package/dist/okf/import.js.map +1 -1
- package/dist/okf/index.d.ts.map +1 -1
- package/dist/okf/index.js +5 -0
- package/dist/okf/index.js.map +1 -1
- package/dist/okf/map.d.ts.map +1 -1
- package/dist/okf/map.js +3 -2
- package/dist/okf/map.js.map +1 -1
- package/dist/search/watcher.d.ts.map +1 -1
- package/dist/search/watcher.js +3 -22
- package/dist/search/watcher.js.map +1 -1
- package/dist/serve/index.d.ts.map +1 -1
- package/dist/serve/index.js +7 -0
- package/dist/serve/index.js.map +1 -1
- package/dist/sleep/index.js +2 -2
- package/dist/sleep/report.js +1 -1
- package/dist/sleep/report.js.map +1 -1
- package/dist/sleep/tension-scan.d.ts.map +1 -1
- package/dist/sleep/tension-scan.js +0 -0
- package/dist/sleep/tension-scan.js.map +1 -1
- package/dist/storage/local.d.ts +1 -0
- package/dist/storage/local.d.ts.map +1 -1
- package/dist/storage/local.js +8 -1
- package/dist/storage/local.js.map +1 -1
- package/dist/utils/config.d.ts +8 -1
- package/dist/utils/config.d.ts.map +1 -1
- package/dist/utils/config.js +143 -18
- package/dist/utils/config.js.map +1 -1
- package/dist/view/pages.d.ts.map +1 -1
- package/dist/view/pages.js +11 -0
- package/dist/view/pages.js.map +1 -1
- package/dist/view/render.d.ts.map +1 -1
- package/dist/view/render.js +37 -2
- package/dist/view/render.js.map +1 -1
- package/package.json +11 -4
- package/src/integrations/microsoft/assets/VENDORING.md +109 -0
- package/src/integrations/microsoft/assets/glue.js +244 -0
- package/src/integrations/microsoft/assets/picker-serializer.js +99 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/app/IPublicClientApplication.mjs +91 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/app/PublicClientApplication.mjs +355 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/broker/nativeBroker/NativeStatusCodes.mjs +15 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/broker/nativeBroker/PlatformAuthDOMHandler.mjs +153 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/broker/nativeBroker/PlatformAuthExtensionHandler.mjs +271 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/broker/nativeBroker/PlatformAuthProvider.mjs +95 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/cache/AccountManager.mjs +49 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/cache/AsyncMemoryStorage.mjs +144 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/cache/BrowserCacheManager.mjs +1465 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/cache/CacheHelpers.mjs +45 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/cache/CacheKeys.mjs +32 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/cache/CookieStorage.mjs +94 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/cache/DatabaseStorage.mjs +208 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/cache/EncryptedData.mjs +13 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/cache/LocalStorage.mjs +319 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/cache/MemoryStorage.mjs +48 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/cache/SessionStorage.mjs +46 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/cache/TokenCache.mjs +220 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/config/Configuration.mjs +138 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/controllers/NestedAppAuthController.mjs +483 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/controllers/StandardController.mjs +1532 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/crypto/BrowserCrypto.mjs +354 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/crypto/CryptoOps.mjs +146 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/crypto/PkceGenerator.mjs +63 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/crypto/SignedHttpRequest.mjs +50 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/crypto/TokenBindingKeyManager.mjs +156 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/encode/Base64Decode.mjs +43 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/encode/Base64Encode.mjs +47 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/error/BrowserAuthError.mjs +26 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/error/BrowserAuthErrorCodes.mjs +65 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/error/BrowserConfigurationAuthError.mjs +24 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/error/BrowserConfigurationAuthErrorCodes.mjs +11 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/error/NativeAuthError.mjs +79 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/error/NativeAuthErrorCodes.mjs +12 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/error/NestedAppAuthError.mjs +25 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/event/EventHandler.mjs +110 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/event/EventMessage.mjs +60 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/event/EventType.mjs +30 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/index.mjs +48 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/interaction_client/BaseInteractionClient.mjs +150 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/interaction_client/HybridSpaAuthorizationCodeClient.mjs +16 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/interaction_client/PlatformAuthInteractionClient.mjs +734 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/interaction_client/PopupClient.mjs +528 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/interaction_client/RedirectClient.mjs +464 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/interaction_client/SilentAuthCodeClient.mjs +85 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/interaction_client/SilentCacheClient.mjs +58 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/interaction_client/SilentIframeClient.mjs +255 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/interaction_client/SilentRefreshClient.mjs +77 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/interaction_client/StandardInteractionClient.mjs +217 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/interaction_handler/InteractionHandler.mjs +95 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/interaction_handler/SilentHandler.mjs +71 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/naa/BridgeError.mjs +11 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/naa/BridgeProxy.mjs +152 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/naa/BridgeStatusCode.mjs +18 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/naa/mapping/NestedAppAuthAdapter.mjs +239 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/navigation/NavigationClient.mjs +47 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/network/FetchClient.mjs +153 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/operatingcontext/BaseOperatingContext.mjs +112 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/operatingcontext/NestedAppOperatingContext.mjs +79 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/operatingcontext/StandardOperatingContext.mjs +50 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/packageMetadata.mjs +7 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/popup_relay/constants.mjs +20 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/popup_relay/relayClient.mjs +113 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/protocol/Authorize.mjs +326 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/request/RequestHelpers.mjs +143 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/response/ResponseHandler.mjs +45 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/telemetry/BrowserPerformanceClient.mjs +126 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/telemetry/BrowserPerformanceEvents.mjs +133 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/telemetry/BrowserPerformanceMeasurement.mjs +96 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/telemetry/BrowserRootPerformanceEvents.mjs +53 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/utils/BrowserConstants.mjs +220 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/utils/BrowserProtocolUtils.mjs +27 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/utils/BrowserUtils.mjs +364 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/utils/Helpers.mjs +19 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/utils/MsalFrameStatsUtils.mjs +38 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/webBrokerBridge/PendingRequestRegistry.mjs +63 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/webBrokerBridge/WebBrokerBridgeError.mjs +38 -0
- package/src/integrations/microsoft/assets/vendor/msal-browser/webBrokerBridge/WebBrokerBridgeErrorMap.mjs +57 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/account/AccountInfo.mjs +91 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/account/AuthToken.mjs +70 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/account/CcsCredential.mjs +12 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/account/ClientInfo.mjs +43 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/account/TokenClaims.mjs +24 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/authority/Authority.mjs +988 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/authority/AuthorityFactory.mjs +41 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/authority/AuthorityMetadata.mjs +158 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/authority/AuthorityOptions.mjs +20 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/authority/AuthorityType.mjs +16 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/authority/CloudInstanceDiscoveryErrorResponse.mjs +12 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/authority/CloudInstanceDiscoveryResponse.mjs +12 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/authority/OpenIdConfigResponse.mjs +14 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/authority/ProtocolMode.mjs +26 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/authority/RegionDiscovery.mjs +116 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/cache/CacheManager.mjs +1202 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/cache/persistence/TokenCacheContext.mjs +29 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/cache/utils/AccountEntityUtils.mjs +222 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/cache/utils/CacheHelpers.mjs +294 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/client/AuthorizationCodeClient.mjs +289 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/client/RefreshTokenClient.mjs +262 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/client/SilentFlowClient.mjs +142 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/config/ClientConfiguration.mjs +111 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/constants/AADServerParamKeys.mjs +74 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/crypto/DpopProofGenerator.mjs +152 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/crypto/ICrypto.mjs +52 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/crypto/ITokenBindingKeyManager.mjs +27 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/crypto/JoseHeader.mjs +72 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/crypto/PopTokenGenerator.mjs +109 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/error/AuthError.mjs +31 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/error/AuthErrorCodes.mjs +13 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/error/CacheError.mjs +44 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/error/CacheErrorCodes.mjs +10 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/error/ClientAuthError.mjs +26 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/error/ClientAuthErrorCodes.mjs +46 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/error/ClientConfigurationError.mjs +23 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/error/ClientConfigurationErrorCodes.mjs +38 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/error/InteractionRequiredAuthError.mjs +73 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/error/InteractionRequiredAuthErrorCodes.mjs +53 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/error/JoseHeaderError.mjs +24 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/error/JoseHeaderErrorCodes.mjs +12 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/error/NetworkError.mjs +34 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/error/PlatformBrokerError.mjs +48 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/error/ServerError.mjs +22 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/index-browser.mjs +79 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/logger/Logger.mjs +291 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/network/INetworkModule.mjs +20 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/network/RequestThumbprint.mjs +27 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/network/ThrottlingUtils.mjs +91 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/packageMetadata.mjs +7 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/protocol/Authorize.mjs +239 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/protocol/Token.mjs +148 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/request/AuthenticationHeaderParser.mjs +63 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/request/BaseAuthRequest.mjs +37 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/request/RequestParameterBuilder.mjs +529 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/request/ScopeSet.mjs +204 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/response/ResponseHandler.mjs +432 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/telemetry/performance/PerformanceClient.mjs +557 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/telemetry/performance/PerformanceEvent.mjs +68 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/telemetry/performance/PerformanceEvents.mjs +73 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/telemetry/performance/StubPerformanceClient.mjs +61 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/telemetry/server/ServerTelemetryManager.mjs +297 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/url/UrlString.mjs +164 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/utils/ClientAssertionUtils.mjs +21 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/utils/Constants.mjs +304 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/utils/FunctionWrappers.mjs +95 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/utils/ObjectUtils.mjs +27 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/utils/ProtocolUtils.mjs +76 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/utils/StringUtils.mjs +82 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/utils/TimeUtils.mjs +75 -0
- package/src/integrations/microsoft/assets/vendor/msal-common/utils/UrlUtils.mjs +128 -0
|
@@ -1,15 +1,27 @@
|
|
|
1
1
|
// Provider-neutral reconciliation. Provider adapters own OAuth HTTP, discovery,
|
|
2
2
|
// and normalization; this module owns encrypted metadata and the change gate.
|
|
3
3
|
import { randomBytes, timingSafeEqual } from "node:crypto";
|
|
4
|
+
import { canWrite } from "../access/rbac.js";
|
|
4
5
|
import { err, ok } from "../frontmatter/types.js";
|
|
5
6
|
import { sha256Hex } from "../utils/hash.js";
|
|
7
|
+
import { TERMINAL_REFRESH_STATUSES, timingSafeSecretEqual } from "./http-json.js";
|
|
6
8
|
import { readIntegrationState, resolveIntegrationStateKey, withIntegrationStateLock, writeIntegrationState, } from "./state.js";
|
|
9
|
+
import { isSourceFailureReason, } from "./types.js";
|
|
10
|
+
/** The §13 per-source lifecycle state, derived from real SourceState fields. */
|
|
11
|
+
export const SOURCE_STATUS_STATES = [
|
|
12
|
+
"pending",
|
|
13
|
+
"current",
|
|
14
|
+
"failed",
|
|
15
|
+
"unavailable",
|
|
16
|
+
"over_limit",
|
|
17
|
+
];
|
|
7
18
|
const activeReconciliations = new Set();
|
|
8
19
|
const activeWebhookVerifications = new Set();
|
|
9
20
|
const DEFAULT_RECONCILE_LIMITS = {
|
|
10
21
|
maxSources: 10_000,
|
|
11
22
|
maxSourceTextBytes: 8 * 1024 * 1024,
|
|
12
23
|
maxCycleTextBytes: 64 * 1024 * 1024,
|
|
24
|
+
maxCycleMs: Number.POSITIVE_INFINITY,
|
|
13
25
|
};
|
|
14
26
|
function reconcileLimits(deps) {
|
|
15
27
|
return { ...DEFAULT_RECONCILE_LIMITS, ...deps.reconcileLimits };
|
|
@@ -45,6 +57,27 @@ export function configuredCredential(environment, name, label) {
|
|
|
45
57
|
export function sourceIdentity(provider, sourceId) {
|
|
46
58
|
return `${provider}:${sourceId}`;
|
|
47
59
|
}
|
|
60
|
+
// Stage-time write gate for enrollment (#506) — the same rationale as
|
|
61
|
+
// vault_stage_action's own gate (docs/architecture.md "Stage-time write
|
|
62
|
+
// gate"): manage_integrations alone must not let an operator aim distilled
|
|
63
|
+
// proposals at a collection the serve process cannot write to. #509's
|
|
64
|
+
// enroll route calls this before persisting an EnrollmentRecord.
|
|
65
|
+
export function requireCollectionWriteAccess(role, targetCollection) {
|
|
66
|
+
if (!canWrite(role, targetCollection)) {
|
|
67
|
+
return err(new Error(`the serve process cannot write to collection "${targetCollection}"`));
|
|
68
|
+
}
|
|
69
|
+
return ok(undefined);
|
|
70
|
+
}
|
|
71
|
+
// The EnrollmentRecord that owns a discovered source: an exact ref match for
|
|
72
|
+
// a directly-enrolled file, or the adapter-attested owner (RemoteSource
|
|
73
|
+
// .enrolledRef) for a folder descendant. Absent enrollment (google/notion)
|
|
74
|
+
// or no match ⇒ undefined, so targetCollection is never set for them.
|
|
75
|
+
function owningEnrollment(enrollment, remote) {
|
|
76
|
+
if (enrollment === undefined)
|
|
77
|
+
return undefined;
|
|
78
|
+
const ownerRef = remote.enrolledRef ?? remote.id;
|
|
79
|
+
return enrollment.find((record) => record.ref === ownerRef);
|
|
80
|
+
}
|
|
48
81
|
export function unavailableEventKey(provider, sourceId, revision) {
|
|
49
82
|
return `${sourceIdentity(provider, sourceId)}:${revision}`;
|
|
50
83
|
}
|
|
@@ -57,6 +90,11 @@ function currentTime(deps) {
|
|
|
57
90
|
function reconciliationKey(vaultRoot, provider) {
|
|
58
91
|
return `${vaultRoot}\u0000${provider}`;
|
|
59
92
|
}
|
|
93
|
+
// Adapters may mutate adapterData in place during discovery (delta links,
|
|
94
|
+
// subscription bookkeeping), so a replayable snapshot must be a deep copy.
|
|
95
|
+
function snapshotAdapterData(value) {
|
|
96
|
+
return value === undefined ? undefined : structuredClone(value);
|
|
97
|
+
}
|
|
60
98
|
function sourceState(source, previous, lastSeenAt) {
|
|
61
99
|
return {
|
|
62
100
|
id: source.id,
|
|
@@ -69,6 +107,36 @@ function sourceState(source, previous, lastSeenAt) {
|
|
|
69
107
|
: { lastDistillRunId: previous.lastDistillRunId }),
|
|
70
108
|
};
|
|
71
109
|
}
|
|
110
|
+
// Prefers a specific reason an adapter/extract error already carries (duck-typed,
|
|
111
|
+
// since ProviderAdapter.fetch returns a plain Error) over the generic stage name.
|
|
112
|
+
function failureReason(error, fallback) {
|
|
113
|
+
if (typeof error === "object" && error !== null) {
|
|
114
|
+
const candidate = error.reason;
|
|
115
|
+
if (isSourceFailureReason(candidate))
|
|
116
|
+
return candidate;
|
|
117
|
+
}
|
|
118
|
+
return fallback;
|
|
119
|
+
}
|
|
120
|
+
function markSourceFailure(previous, remoteId, revision, reason, at) {
|
|
121
|
+
return {
|
|
122
|
+
...(previous ?? {
|
|
123
|
+
id: remoteId,
|
|
124
|
+
revision,
|
|
125
|
+
contentHash: "",
|
|
126
|
+
available: true,
|
|
127
|
+
lastSeenAt: at,
|
|
128
|
+
}),
|
|
129
|
+
// A source that fails every cycle is still attempted every cycle — advance
|
|
130
|
+
// lastSeenAt/revision to reflect that attempt, so a status/staleness
|
|
131
|
+
// surface reading this state doesn't read a month-old "last seen" for a
|
|
132
|
+
// source that's actually failing daily. Retry logic already keys off the
|
|
133
|
+
// freshly-discovered remote.revision passed in here, not off this stored
|
|
134
|
+
// field, so this only fixes the stored reflection — no behavior change.
|
|
135
|
+
revision,
|
|
136
|
+
lastSeenAt: at,
|
|
137
|
+
lastFailure: { at, reason },
|
|
138
|
+
};
|
|
139
|
+
}
|
|
72
140
|
function validRemoteSource(source) {
|
|
73
141
|
return (typeof source.id === "string" &&
|
|
74
142
|
source.id.length > 0 &&
|
|
@@ -84,6 +152,41 @@ function accessTokenExpired(state, deps) {
|
|
|
84
152
|
const expiration = Date.parse(state.accessTokenExpiresAt);
|
|
85
153
|
return !Number.isFinite(expiration) || expiration <= currentTime(deps).getTime();
|
|
86
154
|
}
|
|
155
|
+
// Positive-identification only: default to transient (no reconnect_required)
|
|
156
|
+
// unless a refresh failure is affirmatively an auth/consent rejection. A
|
|
157
|
+
// network-transport throw or a 5xx response is the textbook transient case —
|
|
158
|
+
// this function is never even consulted for the former (see the catch branch
|
|
159
|
+
// in refreshExpiredTokens) and returns false for the latter. Two ways a
|
|
160
|
+
// failure can be positively terminal:
|
|
161
|
+
// 1. A structured signal on the error — `.terminal === true`, or a
|
|
162
|
+
// `.status` in {400, 401, 403}. This is the contract a future adapter
|
|
163
|
+
// (e.g. Microsoft/U13) should emit for a precise signal instead of
|
|
164
|
+
// relying on message sniffing.
|
|
165
|
+
// 2. A fallback for today's Google/Notion adapters, whose jsonResponse only
|
|
166
|
+
// embeds the HTTP status in the Error message: a message naming a
|
|
167
|
+
// 400/401/403 status, or a known terminal OAuth error code
|
|
168
|
+
// (invalid_grant, interaction_required, AADSTS70008 — Entra ID's
|
|
169
|
+
// "consent required" code, the Microsoft analog of interaction_required).
|
|
170
|
+
// A missed terminal case degrades to "prior state retained, refresh retried
|
|
171
|
+
// next cycle" — a lesser evil than a false "please reconnect" prompt.
|
|
172
|
+
// TERMINAL_REFRESH_STATUSES lives in http-json.ts so an adapter (e.g.
|
|
173
|
+
// Microsoft) can import the same set it's classified against.
|
|
174
|
+
const TERMINAL_REFRESH_MESSAGE_PATTERN = /invalid_grant|interaction_required|AADSTS70008/i;
|
|
175
|
+
function isTerminalRefreshError(error) {
|
|
176
|
+
if (typeof error === "object" && error !== null) {
|
|
177
|
+
const signal = error;
|
|
178
|
+
if (signal.terminal === true)
|
|
179
|
+
return true;
|
|
180
|
+
if (typeof signal.status === "number" && TERMINAL_REFRESH_STATUSES.has(signal.status)) {
|
|
181
|
+
return true;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
const message = error instanceof Error ? error.message : "";
|
|
185
|
+
if (TERMINAL_REFRESH_MESSAGE_PATTERN.test(message))
|
|
186
|
+
return true;
|
|
187
|
+
const statusMatch = /\bstatus (\d{3})\b/.exec(message);
|
|
188
|
+
return statusMatch !== null && TERMINAL_REFRESH_STATUSES.has(Number(statusMatch[1]));
|
|
189
|
+
}
|
|
87
190
|
async function refreshExpiredTokens(vaultRoot, adapter, key, persisted, state, deps) {
|
|
88
191
|
if (!accessTokenExpired(state, deps))
|
|
89
192
|
return ok(state);
|
|
@@ -99,6 +202,16 @@ async function refreshExpiredTokens(vaultRoot, adapter, key, persisted, state, d
|
|
|
99
202
|
const clientSecret = configuredCredential(deps.environment, configuration.value.clientSecretEnv, `${adapter.name} OAuth client secret`);
|
|
100
203
|
if (!clientSecret.ok)
|
|
101
204
|
return clientSecret;
|
|
205
|
+
// Marks state only; it does not change which error refreshExpiredTokens
|
|
206
|
+
// itself returns. Only called for a positively-identified terminal failure
|
|
207
|
+
// — see isTerminalRefreshError.
|
|
208
|
+
const markReconnectRequired = (reason) => {
|
|
209
|
+
state.authorization = { status: "reconnect_required", at: timestamp(deps), reason };
|
|
210
|
+
const written = writeState(vaultRoot, key, persisted, deps);
|
|
211
|
+
if (!written.ok)
|
|
212
|
+
return written;
|
|
213
|
+
return err(new Error(`integration provider ${adapter.name} token refresh failed`));
|
|
214
|
+
};
|
|
102
215
|
let refreshed;
|
|
103
216
|
try {
|
|
104
217
|
refreshed = await adapter.refreshTokens({
|
|
@@ -108,10 +221,19 @@ async function refreshExpiredTokens(vaultRoot, adapter, key, persisted, state, d
|
|
|
108
221
|
});
|
|
109
222
|
}
|
|
110
223
|
catch {
|
|
224
|
+
// A network-transport failure never reaches an HTTP response, so it can
|
|
225
|
+
// never be positively identified as terminal — always transient: retain
|
|
226
|
+
// state, retry next cycle.
|
|
111
227
|
return err(new Error(`integration provider ${adapter.name} token refresh failed`));
|
|
112
228
|
}
|
|
113
|
-
if (!refreshed.ok)
|
|
229
|
+
if (!refreshed.ok) {
|
|
230
|
+
if (isTerminalRefreshError(refreshed.error)) {
|
|
231
|
+
return markReconnectRequired(refreshed.error.message);
|
|
232
|
+
}
|
|
233
|
+
// Transient (e.g. a 5xx from the token endpoint): prior state retained,
|
|
234
|
+
// same as before this task — no reconnect prompt for a momentary blip.
|
|
114
235
|
return err(new Error(`integration provider ${adapter.name} token refresh failed`));
|
|
236
|
+
}
|
|
115
237
|
if (refreshed.value.accessToken.length === 0 || refreshed.value.refreshToken.length === 0) {
|
|
116
238
|
return err(new Error(`integration provider ${adapter.name} token refresh returned incomplete tokens`));
|
|
117
239
|
}
|
|
@@ -120,6 +242,7 @@ async function refreshExpiredTokens(vaultRoot, adapter, key, persisted, state, d
|
|
|
120
242
|
...unchanged,
|
|
121
243
|
accessToken: refreshed.value.accessToken,
|
|
122
244
|
refreshToken: refreshed.value.refreshToken,
|
|
245
|
+
authorization: { status: "ok", at: timestamp(deps) },
|
|
123
246
|
...(refreshed.value.accessTokenExpiresAt === undefined
|
|
124
247
|
? {}
|
|
125
248
|
: { accessTokenExpiresAt: refreshed.value.accessTokenExpiresAt }),
|
|
@@ -137,23 +260,39 @@ function validWebhookChannel(channel) {
|
|
|
137
260
|
channel.secret.length > 0 &&
|
|
138
261
|
(channel.expiresAt === undefined || typeof channel.expiresAt === "string") &&
|
|
139
262
|
(channel.verificationRequired === undefined ||
|
|
140
|
-
typeof channel.verificationRequired === "boolean")
|
|
263
|
+
typeof channel.verificationRequired === "boolean") &&
|
|
264
|
+
(channel.subscriptions === undefined ||
|
|
265
|
+
(Array.isArray(channel.subscriptions) &&
|
|
266
|
+
channel.subscriptions.every((subscription) => typeof subscription.id === "string" &&
|
|
267
|
+
subscription.id.length > 0 &&
|
|
268
|
+
typeof subscription.resource === "string" &&
|
|
269
|
+
subscription.resource.length > 0 &&
|
|
270
|
+
typeof subscription.expiresAt === "string"))));
|
|
141
271
|
}
|
|
142
272
|
function validRefreshHint(hint) {
|
|
143
273
|
if (hint.kind === "reconcile")
|
|
144
274
|
return true;
|
|
275
|
+
if (hint.kind === "lifecycle") {
|
|
276
|
+
return (hint.action === "reauthorize" || hint.action === "recreate" || hint.action === "reconcile");
|
|
277
|
+
}
|
|
145
278
|
return (hint.kind === "sources" &&
|
|
146
279
|
Array.isArray(hint.sourceIds) &&
|
|
147
280
|
hint.sourceIds.every((sourceId) => typeof sourceId === "string" && sourceId.length > 0) &&
|
|
148
281
|
typeof hint.rediscover === "boolean");
|
|
149
282
|
}
|
|
150
283
|
function validVerifiedWebhook(value) {
|
|
151
|
-
if (value.kind === "verification")
|
|
152
|
-
return validWebhookChannel(value.channel)
|
|
153
|
-
|
|
284
|
+
if (value.kind === "verification") {
|
|
285
|
+
return (validWebhookChannel(value.channel) &&
|
|
286
|
+
(value.respondBody === undefined || typeof value.respondBody === "string") &&
|
|
287
|
+
(value.respondContentType === undefined || typeof value.respondContentType === "string"));
|
|
288
|
+
}
|
|
289
|
+
if (value.kind === "event") {
|
|
290
|
+
return (typeof value.eventId === "string" && value.eventId.length > 0 && validRefreshHint(value.hint));
|
|
291
|
+
}
|
|
292
|
+
return (value.kind === "lifecycle" &&
|
|
154
293
|
typeof value.eventId === "string" &&
|
|
155
294
|
value.eventId.length > 0 &&
|
|
156
|
-
|
|
295
|
+
(value.action === "reauthorize" || value.action === "recreate" || value.action === "reconcile"));
|
|
157
296
|
}
|
|
158
297
|
async function invokeWebhookVerification(adapter, input, providerState) {
|
|
159
298
|
if (adapter.verifyWebhook === undefined) {
|
|
@@ -179,6 +318,22 @@ function equalSecret(left, right) {
|
|
|
179
318
|
const rightBytes = Buffer.from(right, "utf8");
|
|
180
319
|
return leftBytes.length === rightBytes.length && timingSafeEqual(leftBytes, rightBytes);
|
|
181
320
|
}
|
|
321
|
+
function mintPendingWebhook() {
|
|
322
|
+
return {
|
|
323
|
+
nonce: randomBytes(16).toString("base64url"),
|
|
324
|
+
secret: randomBytes(32).toString("base64url"),
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
function withPendingToken(callbackUrl, nonce) {
|
|
328
|
+
try {
|
|
329
|
+
const url = new URL(callbackUrl);
|
|
330
|
+
url.searchParams.set("pending_token", nonce);
|
|
331
|
+
return ok(url.toString());
|
|
332
|
+
}
|
|
333
|
+
catch {
|
|
334
|
+
return err(new Error("integration webhook callback URL is invalid"));
|
|
335
|
+
}
|
|
336
|
+
}
|
|
182
337
|
export async function armProviderWebhookSetup(vaultRoot, provider, deps, createToken = () => randomBytes(32).toString("base64url")) {
|
|
183
338
|
return withIntegrationStateLock(vaultRoot, () => {
|
|
184
339
|
const configured = providerConfig(deps.config, provider);
|
|
@@ -268,10 +423,23 @@ export async function reconcileProvider(vaultRoot, adapter, deps, hint = { kind:
|
|
|
268
423
|
return refreshed;
|
|
269
424
|
providerState = refreshed.value;
|
|
270
425
|
const limits = reconcileLimits(deps);
|
|
271
|
-
const
|
|
426
|
+
const cycleStart = currentTime(deps).getTime();
|
|
427
|
+
// A "lifecycle" hint reaching reconcileProvider directly (e.g. queued
|
|
428
|
+
// but not intercepted before this drain) falls back to the same full
|
|
429
|
+
// discovery a "reconcile" hint gets — conservative and lossless, since
|
|
430
|
+
// this function has no lifecycle-action dispatch of its own (that's a
|
|
431
|
+
// later unit's job; see the route-side queueing in routes.ts).
|
|
432
|
+
const shouldDiscover = hint.kind !== "sources" || hint.rediscover;
|
|
272
433
|
const previousCursor = providerState.cursor;
|
|
434
|
+
const previousAdapterData = snapshotAdapterData(providerState.adapterData);
|
|
273
435
|
let discovered;
|
|
274
|
-
if (
|
|
436
|
+
if (hint.kind === "sources" && !hint.rediscover) {
|
|
437
|
+
discovered = ok([...new Set(hint.sourceIds)].map((sourceId) => ({
|
|
438
|
+
id: sourceId,
|
|
439
|
+
revision: providerState.sources[sourceId]?.revision ?? "targeted-refresh",
|
|
440
|
+
})));
|
|
441
|
+
}
|
|
442
|
+
else {
|
|
275
443
|
try {
|
|
276
444
|
discovered = await adapter.discover(providerState);
|
|
277
445
|
}
|
|
@@ -281,20 +449,17 @@ export async function reconcileProvider(vaultRoot, adapter, deps, hint = { kind:
|
|
|
281
449
|
if (!discovered.ok)
|
|
282
450
|
return err(new Error(`integration provider ${adapter.name} discovery failed`));
|
|
283
451
|
}
|
|
284
|
-
else {
|
|
285
|
-
discovered = ok([...new Set(hint.sourceIds)].map((sourceId) => ({
|
|
286
|
-
id: sourceId,
|
|
287
|
-
revision: providerState.sources[sourceId]?.revision ?? "targeted-refresh",
|
|
288
|
-
})));
|
|
289
|
-
}
|
|
290
452
|
if (!discovered.ok) {
|
|
291
453
|
return err(new Error(`integration provider ${adapter.name} discovery failed`));
|
|
292
454
|
}
|
|
293
455
|
const discoveredCursor = providerState.cursor;
|
|
294
|
-
|
|
295
|
-
//
|
|
296
|
-
//
|
|
456
|
+
const discoveredAdapterData = providerState.adapterData;
|
|
457
|
+
// Discovery adapters may advance a remote change cursor or their opaque
|
|
458
|
+
// adapterData (per-drive delta links behave exactly like a cursor). Keep
|
|
459
|
+
// both provisional until every source in this page has been handled;
|
|
460
|
+
// all intermediate state writes must retain the replayable values.
|
|
297
461
|
providerState.cursor = previousCursor;
|
|
462
|
+
providerState.adapterData = previousAdapterData;
|
|
298
463
|
if (!discovered.value.every(validRemoteSource)) {
|
|
299
464
|
return err(new Error(`integration provider ${adapter.name} returned an invalid source`));
|
|
300
465
|
}
|
|
@@ -343,13 +508,24 @@ export async function reconcileProvider(vaultRoot, adapter, deps, hint = { kind:
|
|
|
343
508
|
const scopedSources = [...currentSources.values()];
|
|
344
509
|
for (const [index, remote] of scopedSources.entries()) {
|
|
345
510
|
const providerSourceId = sourceIdentity(adapter.name, remote.id);
|
|
511
|
+
if (currentTime(deps).getTime() - cycleStart > limits.maxCycleMs) {
|
|
512
|
+
outcome.failedSourceIds.push(providerSourceId);
|
|
513
|
+
for (const remaining of scopedSources.slice(index + 1)) {
|
|
514
|
+
outcome.failedSourceIds.push(sourceIdentity(adapter.name, remaining.id));
|
|
515
|
+
}
|
|
516
|
+
break;
|
|
517
|
+
}
|
|
346
518
|
const previous = providerState.sources[remote.id];
|
|
347
519
|
const targetedWithoutDiscovery = hint.kind === "sources" && !hint.rediscover;
|
|
348
520
|
if (!targetedWithoutDiscovery &&
|
|
349
521
|
previous?.available === true &&
|
|
350
522
|
previous.revision === remote.revision &&
|
|
351
523
|
previous.contentHash.length > 0) {
|
|
352
|
-
providerState.sources[remote.id] = {
|
|
524
|
+
providerState.sources[remote.id] = {
|
|
525
|
+
...previous,
|
|
526
|
+
lastSeenAt: seenAt,
|
|
527
|
+
lastFailure: undefined,
|
|
528
|
+
};
|
|
353
529
|
const written = writeState(vaultRoot, key.value, persisted.value, deps);
|
|
354
530
|
if (!written.ok)
|
|
355
531
|
return written;
|
|
@@ -360,27 +536,32 @@ export async function reconcileProvider(vaultRoot, adapter, deps, hint = { kind:
|
|
|
360
536
|
try {
|
|
361
537
|
fetched = await adapter.fetch(remote, providerState);
|
|
362
538
|
}
|
|
363
|
-
catch {
|
|
539
|
+
catch (error) {
|
|
540
|
+
providerState.sources[remote.id] = markSourceFailure(previous, remote.id, remote.revision, failureReason(error, "fetch"), seenAt);
|
|
364
541
|
outcome.failedSourceIds.push(providerSourceId);
|
|
365
542
|
continue;
|
|
366
543
|
}
|
|
367
544
|
if (!fetched.ok ||
|
|
368
545
|
!validRemoteSource(fetched.ok ? fetched.value : remote) ||
|
|
369
546
|
typeof fetched.value.text !== "string") {
|
|
547
|
+
providerState.sources[remote.id] = markSourceFailure(previous, remote.id, remote.revision, fetched.ok ? "fetch" : failureReason(fetched.error, "fetch"), seenAt);
|
|
370
548
|
outcome.failedSourceIds.push(providerSourceId);
|
|
371
549
|
continue;
|
|
372
550
|
}
|
|
373
551
|
if (fetched.value.id !== remote.id ||
|
|
374
552
|
(!targetedWithoutDiscovery && fetched.value.revision !== remote.revision)) {
|
|
553
|
+
providerState.sources[remote.id] = markSourceFailure(previous, remote.id, remote.revision, "fetch", seenAt);
|
|
375
554
|
outcome.failedSourceIds.push(providerSourceId);
|
|
376
555
|
continue;
|
|
377
556
|
}
|
|
378
557
|
const textBytes = Buffer.byteLength(fetched.value.text, "utf8");
|
|
379
558
|
if (textBytes > limits.maxSourceTextBytes ||
|
|
380
559
|
cycleTextBytes + textBytes > limits.maxCycleTextBytes) {
|
|
560
|
+
providerState.sources[remote.id] = markSourceFailure(previous, remote.id, remote.revision, "limit", seenAt);
|
|
381
561
|
outcome.failedSourceIds.push(providerSourceId);
|
|
382
562
|
if (cycleTextBytes + textBytes > limits.maxCycleTextBytes) {
|
|
383
563
|
for (const remaining of scopedSources.slice(index + 1)) {
|
|
564
|
+
providerState.sources[remaining.id] = markSourceFailure(providerState.sources[remaining.id], remaining.id, remaining.revision, "limit", seenAt);
|
|
384
565
|
outcome.failedSourceIds.push(sourceIdentity(adapter.name, remaining.id));
|
|
385
566
|
}
|
|
386
567
|
break;
|
|
@@ -405,19 +586,34 @@ export async function reconcileProvider(vaultRoot, adapter, deps, hint = { kind:
|
|
|
405
586
|
const beforeDistill = writeState(vaultRoot, key.value, persisted.value, deps);
|
|
406
587
|
if (!beforeDistill.ok)
|
|
407
588
|
return beforeDistill;
|
|
589
|
+
const owner = owningEnrollment(providerState.enrollment, remote);
|
|
408
590
|
let distilled;
|
|
409
591
|
try {
|
|
410
592
|
distilled = await deps.distill({
|
|
411
593
|
providerSourceId,
|
|
412
594
|
revision: fetched.value.revision,
|
|
413
595
|
text: fetched.value.text,
|
|
596
|
+
...(owner === undefined ? {} : { targetCollection: owner.targetCollection }),
|
|
414
597
|
});
|
|
415
598
|
}
|
|
416
|
-
catch {
|
|
599
|
+
catch (error) {
|
|
600
|
+
// Keep the revision pending (empty hash) so a later cycle retries it,
|
|
601
|
+
// just like the pre-distill write above — a non-empty hash would let
|
|
602
|
+
// the skip-guard treat this unprocessed revision as already done.
|
|
603
|
+
providerState.sources[remote.id] = {
|
|
604
|
+
...next,
|
|
605
|
+
contentHash: "",
|
|
606
|
+
lastFailure: { at: seenAt, reason: failureReason(error, "distill") },
|
|
607
|
+
};
|
|
417
608
|
outcome.failedSourceIds.push(providerSourceId);
|
|
418
609
|
continue;
|
|
419
610
|
}
|
|
420
611
|
if (!distilled.ok) {
|
|
612
|
+
providerState.sources[remote.id] = {
|
|
613
|
+
...next,
|
|
614
|
+
contentHash: "",
|
|
615
|
+
lastFailure: { at: seenAt, reason: failureReason(distilled.error, "distill") },
|
|
616
|
+
};
|
|
421
617
|
outcome.failedSourceIds.push(providerSourceId);
|
|
422
618
|
continue;
|
|
423
619
|
}
|
|
@@ -433,6 +629,7 @@ export async function reconcileProvider(vaultRoot, adapter, deps, hint = { kind:
|
|
|
433
629
|
}
|
|
434
630
|
if (shouldDiscover && outcome.failedSourceIds.length === 0) {
|
|
435
631
|
providerState.cursor = discoveredCursor;
|
|
632
|
+
providerState.adapterData = discoveredAdapterData;
|
|
436
633
|
}
|
|
437
634
|
const finalStateWritten = writeState(vaultRoot, key.value, persisted.value, deps);
|
|
438
635
|
if (!finalStateWritten.ok)
|
|
@@ -445,10 +642,23 @@ export async function reconcileProvider(vaultRoot, adapter, deps, hint = { kind:
|
|
|
445
642
|
}
|
|
446
643
|
}
|
|
447
644
|
export async function ensureProviderWebhook(vaultRoot, adapter, input, deps) {
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
645
|
+
if (adapter.ensureWebhook === undefined) {
|
|
646
|
+
return err(new Error(`integration provider ${adapter.name} cannot ensure webhooks`));
|
|
647
|
+
}
|
|
648
|
+
// Phase 1 (under the lock): refresh tokens if needed, then mint and
|
|
649
|
+
// persist a pending channel. The lock is released before calling the
|
|
650
|
+
// provider — Graph validates the notification URL synchronously *during*
|
|
651
|
+
// subscription creation, and holding the lock across that call would
|
|
652
|
+
// block every other state-locked operation (reconciliation, webhook
|
|
653
|
+
// verification, OAuth) for as long as the provider's create call takes.
|
|
654
|
+
// A stale pending channel from an interrupted prior attempt is simply
|
|
655
|
+
// overwritten here.
|
|
656
|
+
//
|
|
657
|
+
// When the adapter can tell us the existing webhook is already fresh
|
|
658
|
+
// (the steady-state case on every polling cycle), skip minting/persisting
|
|
659
|
+
// a pendingWebhook entirely and short-circuit to a direct, single
|
|
660
|
+
// ensureWebhook call — nothing changes, so there is nothing to write.
|
|
661
|
+
const prepared = await withIntegrationStateLock(vaultRoot, async () => {
|
|
452
662
|
const configured = providerConfig(deps.config, adapter.name);
|
|
453
663
|
if (!configured.ok)
|
|
454
664
|
return configured;
|
|
@@ -466,23 +676,85 @@ export async function ensureProviderWebhook(vaultRoot, adapter, input, deps) {
|
|
|
466
676
|
if (!refreshed.ok)
|
|
467
677
|
return refreshed;
|
|
468
678
|
providerState = refreshed.value;
|
|
679
|
+
if (adapter.needsWebhookRenewal?.(providerState, input) === false) {
|
|
680
|
+
return ok({ key: key.value, providerState, pending: undefined });
|
|
681
|
+
}
|
|
682
|
+
const pending = mintPendingWebhook();
|
|
683
|
+
providerState.pendingWebhook = pending;
|
|
684
|
+
const written = writeState(vaultRoot, key.value, persisted.value, deps);
|
|
685
|
+
if (!written.ok)
|
|
686
|
+
return written;
|
|
687
|
+
return ok({ key: key.value, providerState, pending });
|
|
688
|
+
});
|
|
689
|
+
if (!prepared.ok)
|
|
690
|
+
return prepared;
|
|
691
|
+
const { key, providerState, pending } = prepared.value;
|
|
692
|
+
if (pending === undefined) {
|
|
469
693
|
let ensured;
|
|
470
694
|
try {
|
|
471
695
|
ensured = await adapter.ensureWebhook(providerState, input);
|
|
472
696
|
}
|
|
473
697
|
catch {
|
|
474
|
-
|
|
698
|
+
ensured = err(new Error(`integration provider ${adapter.name} webhook setup failed`));
|
|
475
699
|
}
|
|
476
|
-
if (!ensured.ok)
|
|
700
|
+
if (!ensured.ok) {
|
|
477
701
|
return err(new Error(`integration provider ${adapter.name} webhook setup failed`));
|
|
702
|
+
}
|
|
478
703
|
if (!validWebhookChannel(ensured.value)) {
|
|
479
704
|
return err(new Error(`integration provider ${adapter.name} webhook setup returned invalid channel`));
|
|
480
705
|
}
|
|
481
|
-
|
|
482
|
-
|
|
706
|
+
return ok(ensured.value);
|
|
707
|
+
}
|
|
708
|
+
// Phase 2 (outside the lock): call the provider. A synchronous validation
|
|
709
|
+
// request the provider makes mid-call is answered by verifyProviderWebhook
|
|
710
|
+
// against `providerState.pendingWebhook`, set up in phase 1. A malformed
|
|
711
|
+
// callback URL is treated the same as any other phase-2 failure — it
|
|
712
|
+
// still needs phase 3 to clear the pendingWebhook minted above, not an
|
|
713
|
+
// early return that would leave it orphaned in state.
|
|
714
|
+
const callbackUrl = withPendingToken(input.callbackUrl, pending.nonce);
|
|
715
|
+
let ensured;
|
|
716
|
+
if (!callbackUrl.ok) {
|
|
717
|
+
ensured = callbackUrl;
|
|
718
|
+
}
|
|
719
|
+
else {
|
|
720
|
+
try {
|
|
721
|
+
ensured = await adapter.ensureWebhook(providerState, {
|
|
722
|
+
...input,
|
|
723
|
+
callbackUrl: callbackUrl.value,
|
|
724
|
+
});
|
|
725
|
+
}
|
|
726
|
+
catch {
|
|
727
|
+
ensured = err(new Error(`integration provider ${adapter.name} webhook setup failed`));
|
|
728
|
+
}
|
|
729
|
+
if (!ensured.ok) {
|
|
730
|
+
ensured = err(new Error(`integration provider ${adapter.name} webhook setup failed`));
|
|
731
|
+
}
|
|
732
|
+
else if (!validWebhookChannel(ensured.value)) {
|
|
733
|
+
ensured = err(new Error(`integration provider ${adapter.name} webhook setup returned invalid channel`));
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
// Phase 3 (under the lock): record the outcome. State is re-read rather
|
|
737
|
+
// than reusing the phase-1 snapshot, since reconciliation or another
|
|
738
|
+
// request may have changed unrelated fields meanwhile. pendingWebhook is
|
|
739
|
+
// only cleared if it is still the one this call minted — a newer,
|
|
740
|
+
// concurrent ensure may have already superseded it.
|
|
741
|
+
return withIntegrationStateLock(vaultRoot, () => {
|
|
742
|
+
const persisted = readIntegrationState(vaultRoot, key);
|
|
743
|
+
if (!persisted.ok)
|
|
744
|
+
return persisted;
|
|
745
|
+
const currentState = persisted.value.providers[adapter.name];
|
|
746
|
+
if (currentState === undefined) {
|
|
747
|
+
return err(new Error(`integration provider ${adapter.name} is not authorized`));
|
|
748
|
+
}
|
|
749
|
+
if (currentState.pendingWebhook?.nonce === pending.nonce) {
|
|
750
|
+
delete currentState.pendingWebhook;
|
|
751
|
+
}
|
|
752
|
+
if (ensured.ok)
|
|
753
|
+
currentState.webhook = ensured.value;
|
|
754
|
+
const written = writeState(vaultRoot, key, persisted.value, deps);
|
|
483
755
|
if (!written.ok)
|
|
484
756
|
return written;
|
|
485
|
-
return
|
|
757
|
+
return ensured;
|
|
486
758
|
});
|
|
487
759
|
}
|
|
488
760
|
export async function verifyProviderWebhook(vaultRoot, adapter, input, deps) {
|
|
@@ -502,6 +774,31 @@ export async function verifyProviderWebhook(vaultRoot, adapter, input, deps) {
|
|
|
502
774
|
if (snapshotProvider === undefined) {
|
|
503
775
|
return err(new Error(`integration provider ${adapter.name} is not authorized`));
|
|
504
776
|
}
|
|
777
|
+
// Two-phase webhook creation (#507): an automatic provider (Graph) may
|
|
778
|
+
// validate the notification URL synchronously during subscription
|
|
779
|
+
// creation, before ensureProviderWebhook's phase 2 has returned and has
|
|
780
|
+
// anything to commit. The pending token baked into the callback URL by
|
|
781
|
+
// ensureProviderWebhook correlates this request to that specific
|
|
782
|
+
// in-flight attempt, so a stale or mismatched token falls through to the
|
|
783
|
+
// branches below instead of being treated as a validation.
|
|
784
|
+
//
|
|
785
|
+
// The registered callback URL keeps carrying that same pending token for
|
|
786
|
+
// every future notification too, not just the validation handshake — the
|
|
787
|
+
// provider notifies whatever URL it was given at subscription-creation
|
|
788
|
+
// time, unscrubbed, for as long as the subscription lives. So a genuine
|
|
789
|
+
// event can legitimately arrive here with a matching pending token, in
|
|
790
|
+
// the narrow window between phase 2 returning and phase 3 clearing
|
|
791
|
+
// pendingWebhook under the lock. This branch does not decide validation
|
|
792
|
+
// vs. event itself — it forwards whatever the adapter's own protocol
|
|
793
|
+
// parsing determines and writes nothing either way; treating a
|
|
794
|
+
// `kind: "event"` result as an error here would silently drop that event
|
|
795
|
+
// instead of letting it reach the queue.
|
|
796
|
+
const pendingToken = input.query?.pending_token;
|
|
797
|
+
if (pendingToken !== undefined &&
|
|
798
|
+
snapshotProvider.pendingWebhook !== undefined &&
|
|
799
|
+
equalSecret(snapshotProvider.pendingWebhook.nonce, pendingToken)) {
|
|
800
|
+
return invokeWebhookVerification(adapter, input, snapshotProvider);
|
|
801
|
+
}
|
|
505
802
|
// Configured signed events only consume an atomic encrypted-state snapshot.
|
|
506
803
|
// They neither wait for reconciliation's read/await/write transaction nor
|
|
507
804
|
// exclude another independently valid event verification.
|
|
@@ -539,7 +836,7 @@ export async function verifyProviderWebhook(vaultRoot, adapter, input, deps) {
|
|
|
539
836
|
const expected = providerState.webhookSetupToken;
|
|
540
837
|
if (expected === undefined ||
|
|
541
838
|
input.setupToken === undefined ||
|
|
542
|
-
!
|
|
839
|
+
!timingSafeSecretEqual(input.setupToken, expected)) {
|
|
543
840
|
return err(new Error(`integration provider ${adapter.name} webhook setup is not armed`));
|
|
544
841
|
}
|
|
545
842
|
const verified = await invokeWebhookVerification(adapter, input, providerState);
|
|
@@ -563,6 +860,39 @@ export async function verifyProviderWebhook(vaultRoot, adapter, input, deps) {
|
|
|
563
860
|
activeWebhookVerifications.delete(lockKey);
|
|
564
861
|
}
|
|
565
862
|
}
|
|
863
|
+
export async function verifyProviderLifecycleWebhook(vaultRoot, adapter, input, deps) {
|
|
864
|
+
if (adapter.verifyLifecycleWebhook === undefined) {
|
|
865
|
+
return err(new Error(`integration provider ${adapter.name} cannot verify lifecycle webhooks`));
|
|
866
|
+
}
|
|
867
|
+
const configured = providerConfig(deps.config, adapter.name);
|
|
868
|
+
if (!configured.ok)
|
|
869
|
+
return configured;
|
|
870
|
+
const key = resolveIntegrationStateKey(deps.config.encryptionKeyEnv, deps.environment);
|
|
871
|
+
if (!key.ok)
|
|
872
|
+
return key;
|
|
873
|
+
const snapshot = readIntegrationState(vaultRoot, key.value);
|
|
874
|
+
if (!snapshot.ok)
|
|
875
|
+
return snapshot;
|
|
876
|
+
const snapshotProvider = snapshot.value.providers[adapter.name];
|
|
877
|
+
if (snapshotProvider === undefined) {
|
|
878
|
+
return err(new Error(`integration provider ${adapter.name} is not authorized`));
|
|
879
|
+
}
|
|
880
|
+
let verified;
|
|
881
|
+
try {
|
|
882
|
+
verified = await adapter.verifyLifecycleWebhook(input, snapshotProvider);
|
|
883
|
+
}
|
|
884
|
+
catch {
|
|
885
|
+
return err(new Error(`integration provider ${adapter.name} lifecycle verification failed`));
|
|
886
|
+
}
|
|
887
|
+
if (!verified.ok) {
|
|
888
|
+
return err(new Error(`integration provider ${adapter.name} lifecycle verification failed`));
|
|
889
|
+
}
|
|
890
|
+
const value = verified.value;
|
|
891
|
+
if (!validVerifiedWebhook(value) || value.kind !== "lifecycle") {
|
|
892
|
+
return err(new Error(`integration provider ${adapter.name} lifecycle verification returned invalid result`));
|
|
893
|
+
}
|
|
894
|
+
return ok(value);
|
|
895
|
+
}
|
|
566
896
|
export function startPeriodicIntegrationSync(vaultRoot, adapters, deps, intervalMinutes = deps.config.pollingIntervalMinutes) {
|
|
567
897
|
const intervalMilliseconds = intervalMinutes * 60_000;
|
|
568
898
|
const timer = setInterval(() => {
|